10cdc616c41587b95b6beb82524781fe99f6fa59
[platform/core/system/tizen-platform-wrapper.git] / src / init.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 <stdio.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <pthread.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <stdarg.h>
40 #include <alloca.h>
41 #include <pwd.h>
42 #include <assert.h>
43
44 #ifndef NOT_MULTI_THREAD_SAFE
45 #include <pthread.h>
46 #endif
47
48 #ifndef CONFIGPATH
49 #define CONFIGPATH "/etc/tizen-platform.conf"
50 #endif
51
52 #include "tzplatform_variables.h"
53 #include "tzplatform_config.h"
54 #include "parser.h"
55 #include "heap.h"
56 #include "buffer.h"
57 #include "foreign.h"
58 #include "scratch.h"
59 #include "passwd.h"
60 #include "context.h"
61 #include "hashing.h"
62 #include "init.h"
63
64 #define _HAS_IDS_   (  _FOREIGN_HAS_(UID)  \
65                     || _FOREIGN_HAS_(EUID) \
66                     || _FOREIGN_HAS_(GID)  )
67
68 #define _HAS_PWS_   (  _FOREIGN_HAS_(HOME)  \
69                     || _FOREIGN_HAS_(USER)  \
70                     || _FOREIGN_HAS_(EHOME) \
71                     || _FOREIGN_HAS_(EUSER) )
72
73 /* local and static variables */
74 static const char metafilepath[] = CONFIGPATH;
75 static const char emptystring[] = "";
76
77 /* structure for reading config files */
78 struct reading {
79     int errcount;
80     struct tzplatform_context *context;
81     size_t dynvars[_FOREIGN_COUNT_];
82     size_t offsets[_TZPLATFORM_VARIABLES_COUNT_];
83 };
84
85 /* write the error message */
86 static void writerror( const char *format, ...)
87 {
88     va_list ap;
89     fprintf( stderr, "tzplatform_config ERROR: ");
90     va_start(ap, format);
91     vfprintf(stderr, format, ap);
92     va_end(ap);
93     fprintf( stderr, "\n");
94 }
95
96 #if _HAS_IDS_
97 /* fill the foreign variables for ids */
98 static void foreignid( struct reading *reading)
99 {
100     int n;
101     char buffer[50];
102
103 #if _FOREIGN_HAS_(UID)
104     /* set the uid */
105     if (reading->dynvars[UID] == HNULL) {
106         n = snprintf( buffer, sizeof buffer, "%d", (int)get_uid(reading->context));
107         if (0 < n && n < (int)(sizeof buffer))
108             reading->dynvars[UID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
109     }
110 #endif
111
112 #if _FOREIGN_HAS_(EUID)
113     /* set the euid */
114     if (reading->dynvars[EUID] == HNULL) {
115         n = snprintf( buffer, sizeof buffer, "%d", (int)get_euid(reading->context));
116         if (0 < n && n < (int)(sizeof buffer))
117             reading->dynvars[EUID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
118     }
119 #endif
120
121 #if _FOREIGN_HAS_(GID)
122     /* set the gid */
123     if (reading->dynvars[GID] == HNULL) {
124         n = snprintf( buffer, sizeof buffer, "%d", (int)get_gid(reading->context));
125         if (0 < n && n < (int)(sizeof buffer))
126             reading->dynvars[GID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
127     }
128 #endif
129 }
130 #endif
131
132 #if _HAS_PWS_
133 /* fill the foreign variables for home and user */
134 static void foreignpw( struct reading *reading)
135 {
136     int n = 0;
137     struct pwget *array[3];
138 #if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
139     struct pwget uid;
140     char suid[50];
141 #endif
142 #if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
143     struct pwget euid;
144     char seuid[50];
145 #endif
146
147 #if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
148     if (
149 #if _FOREIGN_HAS_(HOME)
150         reading->dynvars[HOME] == HNULL
151 #endif
152 #if _FOREIGN_HAS_(HOME) && _FOREIGN_HAS_(USER)
153         ||
154 #endif
155 #if _FOREIGN_HAS_(USER)
156         reading->dynvars[USER] == HNULL
157 #endif
158     ) {
159         snprintf( suid, sizeof suid, "%u", (unsigned)get_uid(reading->context));
160         uid.id = suid;
161         array[n++] = &uid;
162         uid.set = 1;
163     }
164     else {
165         uid.set = 0;
166     }
167 #endif
168
169 #if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
170     if (
171 #if _FOREIGN_HAS_(EHOME)
172         reading->dynvars[EHOME] == HNULL
173 #endif
174 #if _FOREIGN_HAS_(EHOME) && _FOREIGN_HAS_(USER)
175         ||
176 #endif
177 #if _FOREIGN_HAS_(EUSER)
178         reading->dynvars[EUSER] == HNULL
179 #endif
180     ) {
181         snprintf( seuid, sizeof seuid, "%u", (unsigned)get_euid(reading->context));
182         euid.id = seuid;
183         array[n++] = &euid;
184         euid.set = 1;
185     }
186     else {
187         euid.set = 0;
188     }
189 #endif
190
191     if (n) {
192         array[n] = NULL;
193         if (pw_get( &reading->context->heap, array) == 0) {
194 #if _FOREIGN_HAS_(HOME)
195             if (uid.set)
196                 reading->dynvars[HOME] = uid.home;
197 #endif
198 #if _FOREIGN_HAS_(USER)
199             if (uid.set)
200                 reading->dynvars[USER] = uid.user;
201 #endif
202 #if _FOREIGN_HAS_(EHOME)
203             if (euid.set)
204                 reading->dynvars[EHOME] = euid.home;
205 #endif
206 #if _FOREIGN_HAS_(EUSER)
207             if (euid.set)
208                 reading->dynvars[EUSER] = euid.user;
209 #endif
210         }
211     }
212 }
213 #endif
214
215 /* get the foreign variable */
216 static const char *foreignvar( struct reading *reading, 
217                                             const char *name, size_t length)
218 {
219     enum fkey key = foreign( name, length);
220     size_t offset;
221
222     switch (key) {
223 #if _HAS_PWS_
224 #if _FOREIGN_HAS_(HOME)
225     case HOME:
226 #endif
227 #if _FOREIGN_HAS_(USER)
228     case USER:
229 #endif
230 #if _FOREIGN_HAS_(EHOME)
231     case EHOME:
232 #endif
233 #if _FOREIGN_HAS_(EUSER)
234     case EUSER:
235 #endif
236         foreignpw( reading);
237         break;
238 #endif
239  
240 #if _HAS_IDS_
241 #if _FOREIGN_HAS_(UID)
242     case UID:
243 #endif
244 #if _FOREIGN_HAS_(GID)
245     case GID:
246 #endif
247 #if _FOREIGN_HAS_(EUID)
248     case EUID:
249 #endif
250         foreignid( reading);
251         break;
252 #endif
253
254     default:
255         return NULL;
256     }
257     offset = reading->dynvars[key];
258     return offset==HNULL ? NULL : heap_address( &reading->context->heap, offset);
259 }
260
261 /* callback for parsing errors */
262 static int errcb( struct parsing *parsing, 
263             size_t position, const char *message)
264 {
265     struct parsinfo info;
266     struct reading *reading = parsing->data;
267
268     /* count the error */
269     reading->errcount++;
270
271     /* print the error */
272     parse_utf8_info( parsing, &info, position);
273     writerror( "%s (file %s line %d)", message, metafilepath, info.lino);
274
275     /* continue to parse */
276     return 0;
277 }
278
279 /* callback for solving variables */
280 static const char *getcb( struct parsing *parsing, 
281             const char *key, size_t length,
282             size_t begin_pos, size_t end_pos)
283 {
284     struct parsinfo info;
285     const char *result;
286     size_t offset;
287     struct reading *reading = parsing->data;
288     int id;
289
290     /* try to find a tzplatform variable */
291     id = hashid(key, length);
292     if (id >= 0) {
293         /* found: try to use it */
294         offset = reading->offsets[id];
295         if (offset != HNULL)
296             result = heap_address( &reading->context->heap, offset);
297         else 
298             result = NULL;
299     }
300     else {
301         /* that is a foreign variable */
302         result = foreignvar( reading, key, length);
303     }
304
305     /* emit the error and then return */
306     if (result == NULL) {
307         reading->errcount++;
308         parse_utf8_info( parsing, &info, begin_pos);
309         writerror( "undefined value for %.*s (file %s line %d)",
310             length, key, metafilepath, info.lino);
311         result = emptystring; /* avoid error of the parser */
312     }
313     return result;
314 }
315
316 /* callback to define variables */
317 static int putcb( struct parsing *parsing, 
318             const char *key, size_t key_length, 
319             const char *value, size_t value_length,
320             size_t begin_pos, size_t end_pos)
321 {
322     struct parsinfo info;
323     size_t offset;
324     char *string;
325     struct reading *reading = parsing->data;
326     int id;
327
328     /* try to find a tzplatform variable */
329     id = hashid( key, key_length);
330     if (id >= 0) {
331         /* check that the variable isn't already defined */
332         offset = reading->offsets[id];
333         if (offset != HNULL) {
334             reading->errcount++;
335             parse_utf8_info( parsing, &info, begin_pos);
336             writerror( "redefinition of variable %.*s (file %s line %d)",
337                     key_length, key, metafilepath, info.lino);
338         }
339
340         /* allocate the variable value */
341         offset = heap_alloc( &reading->context->heap, value_length+1);
342         if (offset == HNULL) {
343             /* error of allocation */
344             reading->errcount++;
345             writerror( "out of memory");
346         }
347         else {
348             /* record the variable value */
349             reading->offsets[id] = offset;
350             string = heap_address( &reading->context->heap, offset);
351             memcpy( string, value, value_length);
352             string[value_length] = 0;
353         }
354     }
355     else {
356         /* forbidden variable */
357         parse_utf8_info( parsing, &info, begin_pos);
358         writerror( "forbidden variable name %.*s (file %s line %d)",
359             key_length, key, metafilepath, info.lino);
360         
361     }
362
363     /* continue to parse */
364     return 0;
365 }
366
367 /* initialize the environment */
368 inline void initialize(struct tzplatform_context *context)
369 {
370     struct buffer buffer;
371     struct parsing parsing;
372     struct reading reading;
373     size_t offset;
374     int i, result;
375
376     /* clear the variables */
377     reading.errcount = 0;
378     reading.context = context;
379     for (i = 0 ; i < (int)_FOREIGN_COUNT_ ; i++) {
380         reading.dynvars[i] = HNULL;
381     }
382     for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
383         context->values[i] = NULL;
384         reading.offsets[i] = HNULL;
385     }
386
387     /* read the configuration file */
388     result = buffer_create( &buffer, metafilepath);
389     if (result != 0) {
390         writerror( "can't read file %s",metafilepath);
391         context->state = ERROR;
392         return;
393     }
394
395     /* create the heap */
396     result = heap_create( &context->heap, 1);
397     if (result != 0) {
398         buffer_destroy( &buffer);
399         writerror( "out of memory");
400         context->state = ERROR;
401         return;
402     }
403
404     /* read the file */
405     parsing.buffer = buffer.buffer;
406     parsing.length = buffer.length;
407     parsing.maximum_data_size = 0;
408     parsing.should_escape = 0;
409     parsing.data = &reading;
410     parsing.get = getcb;
411     parsing.put = putcb;
412     parsing.error = errcb;
413     result = parse_utf8_config( &parsing);
414     buffer_destroy( &buffer);
415     if (result != 0 || reading.errcount != 0) {
416         writerror( "%d errors while parsing file %s",
417                                             reading.errcount, metafilepath);
418     }
419
420     /* set the variables */
421     heap_read_only( &context->heap);
422     for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
423         offset = reading.offsets[i];
424         if (offset != HNULL)
425             context->values[i] = heap_address( &context->heap, offset);
426         else
427             writerror( "the variable %s isn't defined in file %s",
428                 keyname(i), metafilepath);
429             /* TODO undefined variable */;
430     }
431     context->state = VALID;
432 }
433