Deprecate signup validation
[platform/core/system/tizen-platform-config.git] / src / shared-api.c
1 /*
2  * Copyright (C) 2013-2014 Intel Corporation.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors:
19  *   José Bollo <jose.bollo@open.eurogiciel.org>
20  *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21  *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
22  *
23  */
24 #define _GNU_SOURCE
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <assert.h>
34 #include <syslog.h>
35
36 #ifndef NOT_MULTI_THREAD_SAFE
37 #include <pthread.h>
38 #endif
39
40 #include "tzplatform_variables.h"
41 #include "tzplatform_config.h"
42 #include "heap.h"
43 #include "scratch.h"
44 #include "passwd.h"
45 #include "foreign.h"
46 #include "context.h"
47 #include "hashing.h"
48 #include "init.h"
49 #include "shared-api.h"
50
51
52 /* the global context */
53 static struct tzplatform_context global_context = {
54 #ifndef NOT_MULTI_THREAD_SAFE
55     .mutex = PTHREAD_MUTEX_INITIALIZER,
56 #endif
57     .state = RESET,
58     .user = _USER_NOT_SET_
59 };
60
61 /*
62  * Tizen 4.0: signup validation is deprecated
63  *  version matching will be conducted by numbering of so version
64  */
65
66 /* locks the context */
67 inline static void lock(struct tzplatform_context *context)
68 {
69 #ifndef NOT_MULTI_THREAD_SAFE
70     pthread_mutex_lock( &context->mutex);
71 #endif
72 }
73
74 /* unlock the context */
75 inline static void unlock(struct tzplatform_context *context)
76 {
77 #ifndef NOT_MULTI_THREAD_SAFE
78     pthread_mutex_unlock( &context->mutex);
79 #endif
80 }
81
82 static inline const char *get_lock(int id, struct tzplatform_context *context)
83 {
84     lock( context);
85
86     if (id < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= id)
87         return NULL;
88
89     if (context->state == RESET)
90         initialize( context);
91
92     return context->state == ERROR ? NULL : context->values[id];
93 }
94
95 /*************** PUBLIC API begins here **************/
96
97 int tzplatform_context_create(struct tzplatform_context **result)
98 {
99     struct tzplatform_context *context;
100
101     context = malloc( sizeof * context);
102     *result = context;
103     if (context == NULL)
104         return -1;
105
106     context->state = RESET;
107     context->user = _USER_NOT_SET_;
108 #ifndef NOT_MULTI_THREAD_SAFE
109     pthread_mutex_init( &context->mutex, NULL);
110 #endif
111     return 0;
112 }
113
114 void tzplatform_context_destroy(struct tzplatform_context *context)
115 {
116     if (context->state == VALID)
117             heap_destroy( &context->heap);
118     context->state = ERROR;
119     free( context);
120 }
121
122 void tzplatform_reset()
123 {
124     tzplatform_context_reset(&global_context);
125 }
126
127 void tzplatform_context_reset(struct tzplatform_context *context)
128 {
129     lock( context);
130     if (context->state != RESET) {
131         if (context->state == VALID)
132             heap_destroy( &context->heap);
133         context->state = RESET;
134     }
135     unlock( context);
136 }
137
138 uid_t tzplatform_get_user(void)
139 {
140     return tzplatform_context_get_user( &global_context);
141 }
142
143 uid_t tzplatform_context_get_user(struct tzplatform_context *context)
144 {
145     uid_t result;
146
147     lock( context);
148     result = get_uid( context);
149     unlock( context);
150
151     return result;
152 }
153
154 void tzplatform_reset_user()
155 {
156     tzplatform_context_reset_user( &global_context);
157 }
158
159 void tzplatform_context_reset_user(struct tzplatform_context *context)
160 {
161     tzplatform_context_set_user( context, _USER_NOT_SET_);
162 }
163
164 int tzplatform_set_user(uid_t uid)
165 {
166     return tzplatform_context_set_user( &global_context, uid);
167 }
168
169 int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid)
170 {
171     lock( context);
172     if (context->user != uid) {
173         if (uid != _USER_NOT_SET_ && !pw_has_uid( uid)) {
174             unlock( context);
175             return -1;
176         }
177         else {
178             if (context->state == VALID)
179                 heap_destroy( &context->heap);
180             context->state = RESET;
181             context->user = uid;
182         }
183     }
184     unlock( context);
185
186     return 0;
187 }
188
189 /*************** PUBLIC INTERNAL API begins here **************/
190
191 const char* _getname_tzplatform_(int id)
192 {
193     return 0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_ ? keyname(id) : NULL;
194 }
195
196 int _getid_tzplatform_(const char *name)
197 {
198     return hashid(name, strlen(name));
199 }
200
201 const char* _getenv_tzplatform_(int id)
202 {
203     return _context_getenv_tzplatform_(id, &global_context);
204 }
205
206 const char* _context_getenv_tzplatform_(int id, struct tzplatform_context *context)
207 {
208     const char *array[2];
209     const char *result;
210
211     result = get_lock(id, context);
212     if (result != NULL) {
213         array[0] = result;
214         array[1] = NULL;
215         result = scratchcat( 0, array);
216     }
217     unlock( context);
218     return result;
219 }
220
221 int _getenv_int_tzplatform_(int id)
222 {
223     return _context_getenv_int_tzplatform_(id, &global_context);
224 }
225
226 int _context_getenv_int_tzplatform_(int id, struct tzplatform_context *context)
227 {
228     const char *value;
229     int result;
230
231     value = get_lock(id, context);
232     result = value==NULL ? -1 : atoi(value);
233     unlock( context);
234     return result;
235 }
236
237 const char* _mkstr_tzplatform_(int id, const char * str)
238 {
239     return _context_mkstr_tzplatform_(id, str, &global_context);
240 }
241
242 const char* _context_mkstr_tzplatform_(int id, const char *str, struct tzplatform_context *context)
243 {
244     const char *array[3];
245     const char *result;
246
247     result = get_lock(id, context);
248     if (result != NULL) {
249         array[0] = result;
250         array[1] = str;
251         array[2] = NULL;
252         result = scratchcat( 0, array);
253     }
254     unlock( context);
255     return result;
256 }
257
258 const char* _mkpath_tzplatform_(int id, const char * path)
259 {
260     return _context_mkpath_tzplatform_(id, path, &global_context);
261 }
262
263 const char* _context_mkpath_tzplatform_(int id, const char *path, struct tzplatform_context *context)
264 {
265     const char *array[3];
266     const char *result;
267
268     result = get_lock(id, context);
269     if (result != NULL) {
270         array[0] = result;
271         array[1] = path;
272         array[2] = NULL;
273         result = scratchcat( 1, array);
274     }
275     unlock( context);
276     return result;
277 }
278
279 const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2)
280 {
281     return _context_mkpath3_tzplatform_( id, path, path2, &global_context);
282 }
283
284 const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, struct tzplatform_context *context)
285 {
286     const char *array[4];
287     const char *result;
288
289     result = get_lock(id, context);
290     if (result != NULL) {
291         array[0] = result;
292         array[1] = path;
293         array[2] = path2;
294         array[3] = NULL;
295         result = scratchcat( 1, array);
296     }
297     unlock( context);
298     return result;
299 }
300
301 const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3)
302 {
303     return _context_mkpath4_tzplatform_( id, path, path2, path3, &global_context);
304 }
305
306 const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, struct tzplatform_context *context)
307 {
308     const char *array[5];
309     const char *result;
310
311     result = get_lock(id, context);
312     if (result != NULL) {
313         array[0] = result;
314         array[1] = path;
315         array[2] = path2;
316         array[3] = path3;
317         array[4] = NULL;
318         result = scratchcat( 1, array);
319     }
320     unlock( context);
321     return result;
322 }
323
324 uid_t _getuid_tzplatform_(int id)
325 {
326     return _context_getuid_tzplatform_( id, &global_context);
327 }
328
329 uid_t _context_getuid_tzplatform_(int id, struct tzplatform_context *context)
330 {
331     uid_t result;
332     const char *value;
333
334     result = (uid_t)-1;
335     value = get_lock(id, context);
336     if (value != NULL) {
337         pw_get_uid( value, &result);
338     }
339     unlock( context);
340     return result;
341 }
342
343 gid_t _getgid_tzplatform_(int id)
344 {
345     return _context_getgid_tzplatform_( id, &global_context);
346 }
347
348 gid_t _context_getgid_tzplatform_(int id, struct tzplatform_context *context)
349 {
350     gid_t result;
351     const char *value;
352
353     result = (uid_t)-1;
354     value = get_lock(id, context);
355     if (value != NULL) {
356         pw_get_gid( value, &result);
357     }
358     unlock( context);
359     return result;
360 }
361