Add UID-based APIs:
[platform/core/system/tizen-platform-config.git] / src / tzplatform_config.h
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 #ifndef LIBTIZEN_PLATFORM_WRAPPER
25 #define LIBTIZEN_PLATFORM_WRAPPER
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #include <sys/types.h>
32 #include <tzplatform_variables.h>
33
34 //=================================================================================
35 //======================== COMMON APIs (No context) ===============================
36 //=================================================================================
37
38 /*
39  Return the count of variables.
40 */
41 extern
42 int tzplatform_getcount();
43
44 /*
45  Return the name of the variable 'id' as a string.
46  Return NULL if 'id' is invalid.
47 */
48 extern
49 const char* tzplatform_getname(enum tzplatform_variable id);
50
51 /*
52  Return the id of the variable of 'name'.
53  Return _TZPLATFORM_VARIABLES_INVALID_ if 'name' doesn't match a
54  valid variable name.
55 */
56 extern
57 enum tzplatform_variable tzplatform_getid(const char *name);
58
59 //=================================================================================
60 //====================== GLOBAL APIs (default global context) =====================
61 //=================================================================================
62
63 /*
64  Enforces the removal of the previously evaluated tizen platform variables.
65
66  Call this function in case of changing of user inside the application.
67 */
68 extern
69 void tzplatform_reset();
70
71 /*
72  Set the user used for UID/EUID/USER/EUSER computations.
73  Using uid==(uid_t)-1 reset the context as if tzplatform_reset_user
74  was called.
75
76  Returns 0 if uid is valid or -1 if not valid.
77 */
78 extern
79 int tzplatform_set_user(uid_t uid);
80
81 /*
82 */
83 extern
84 uid_t tzplatform_get_user();
85
86 /*
87  Reset the user context to use the values returned by
88  getuid and geteuid.
89 */
90 extern
91 void tzplatform_reset_user();
92
93 /*
94  Return the read-only string value of the tizen plaform variable 'id'.
95
96  The returned value is an allocated unique string that MUST not be freed.
97
98  Can return NULL in case of internal error or when 'id' isn't defined.
99 */
100 extern
101 const char* tzplatform_getenv(enum tzplatform_variable id);
102
103 /*
104  Return the integer value of the tizen plaform variable 'id'.
105 */
106 extern
107 int tzplatform_getenv_int(enum tzplatform_variable id);
108
109 /*
110  Return the string resulting of the concatenation of string value of the
111  tizen plaform variable 'id' and the given string 'str'.
112
113  The returned value is an allocated unique string that MUST not be freed.
114
115  Can return NULL in case of internal error.
116
117  Example:
118         if TZ_SYS_HOME == "/opt/home" then calling
119
120            tzplatform_mkstr(TZ_SYS_HOME,"-yes")
121
122         will return "/opt/home-yes"
123 */
124 extern
125 const char* tzplatform_mkstr(enum tzplatform_variable id, const char *str);
126
127 /*
128  Return the string resulting of the path-concatenation of string value of the
129  tizen plaform variable 'id' and the given string 'path'.
130
131  path-concatenation is the concatenation taking care of / characters.
132
133  The returned value is an allocated unique string that MUST not be freed.
134
135  Can return NULL in case of internal error.
136
137  Example:
138         if TZ_SYS_HOME == "/opt/home" then calling
139
140            tzplatform_mkpath(TZ_SYS_HOME,"yes")
141
142         will return "/opt/home/yes"
143 */
144 extern
145 const char* tzplatform_mkpath(enum tzplatform_variable id, const char *path);
146
147 /*
148  Return the string resulting of the path-concatenation of string value of the
149  tizen plaform variable 'id' and the given strings 'path' and 'path2'.
150
151  path-concatenation is the concatenation taking care of / characters.
152
153  The returned value is an allocated unique string that MUST not be freed.
154
155  Can return NULL in case of internal error.
156
157  Example:
158         if TZ_SYS_HOME == "/opt/home" then calling
159
160            tzplatform_mkpath3(TZ_SYS_HOME,"yes","no")
161
162         will return "/opt/home/yes/no"
163 */
164 extern
165 const char* tzplatform_mkpath3(enum tzplatform_variable id, const char *path,
166                                 const char *path2);
167
168 /*
169  Return the string resulting of the path-concatenation of string value of the
170  tizen plaform variable 'id' and the given strings 'path', 'path2' and 'path3'.
171
172  path-concatenation is the concatenation taking care of / characters.
173
174  The returned value is an allocated unique string that MUST not be freed.
175
176  Can return NULL in case of internal error.
177
178  Example:
179         if TZ_SYS_HOME == "/opt/home" then calling
180
181            tzplatform_mkpath4(TZ_SYS_HOME,"yes","no","/maybe")
182
183         will return "/opt/home/yes/no/maybe"
184 */
185 extern
186 const char* tzplatform_mkpath4(enum tzplatform_variable id, const char *path,
187                                 const char *path2, const char *path3);
188
189 /*
190  Return the uid for a given user name, stored in variable <id>
191  Retun -1 in case of error.
192
193  Example:
194         if TZ_USER_NAME=="app" then calling:
195
196            tzplatform_getuid(TZ_USER_NAME)
197
198         will return the uid of the user 'app'
199 */
200 extern
201 uid_t tzplatform_getuid(enum tzplatform_variable id);
202
203 /*
204  Return the gid for a given user name, stored in variable <id>
205  Retun -1 in case of error.
206
207  Example:
208         if TZ_USER_NAME=="app" then calling:
209
210            tzplatform_getuid(TZ_USER_NAME)
211
212         will return the gid of the user 'app'
213 */
214 extern
215 gid_t tzplatform_getgid(enum tzplatform_variable id);
216
217 //=================================================================================
218 //============================ UID-based APIs =====================================
219 //=================================================================================
220
221 /*
222  In the following APIs, a 'uid' parameter is added to the form of GLOBAL APIs.
223
224  How-to-use is the same with GLOBAL APIs, but contexts are internally created and set for each uid.
225
226  uid should be one of root(0), owner(5001), and so on.
227 */
228
229 extern
230 const char* tzplatform_uid_getenv(uid_t uid, enum tzplatform_variable id);
231
232 extern
233 const char* tzplatform_uid_mkpath(uid_t uid, enum tzplatform_variable id, const char *path);
234
235 extern
236 const char* tzplatform_uid_mkpath3(uid_t uid, enum tzplatform_variable id, const char *path,
237                                 const char *path2);
238
239 extern
240 const char* tzplatform_uid_mkpath4(uid_t uid, enum tzplatform_variable id, const char *path,
241                                 const char *path2, const char *path3);
242
243 //=================================================================================
244 //============================ CONTEXTUAL APIs ====================================
245 //=================================================================================
246
247 struct tzplatform_context;
248
249 /*
250  Creates a new platform 'context'.
251  Return 0 in case of success or a negative value in case of error.
252 */
253 extern
254 int tzplatform_context_create(struct tzplatform_context **context);
255
256 /*
257  Destroys the platform 'context' previously created with 'tzplatform_context_create'.
258  The destroyed context must not be used after calling this function.
259 */
260 extern
261 void tzplatform_context_destroy(struct tzplatform_context *context);
262
263 /*
264  Enforces the removal of the previously evaluated tizen platform variables.
265 */
266 extern
267 void tzplatform_context_reset(struct tzplatform_context *context);
268
269 /*
270  Set the user used for UID/EUID/USER/EUSER computations.
271  Using uid==(uid_t)-1 reset the context as if tzplatform_context_reset_user
272  was called.
273
274  Returns 0 if uid is valid or -1 if not valid.
275 */
276 extern
277 int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid);
278
279 /*
280  Get the user set to the context.
281 */
282 extern
283 uid_t tzplatform_context_get_user(struct tzplatform_context *context);
284
285 /*
286  Reset the user context to use the values returned by
287  getuid and geteuid.
288 */
289 extern
290 void tzplatform_context_reset_user(struct tzplatform_context *context);
291
292 /*
293  Return the read-only string value of the tizen plaform variable 'id'.
294
295  The returned value is an allocated unique string that MUST not be freed.
296
297  Can return NULL in case of internal error or when 'id' isn't defined.
298 */
299 extern
300 const char* tzplatform_context_getenv(struct tzplatform_context *context,
301                                         enum tzplatform_variable id);
302
303 /*
304  Return the integer value of the tizen plaform variable 'id'.
305 */
306 extern
307 int tzplatform_context_getenv_int(struct tzplatform_context *context,
308                                         enum tzplatform_variable id);
309
310 /*
311  Return the string resulting of the concatenation of string value of the
312  tizen plaform variable 'id' and the given string 'str'.
313
314  The returned value is an allocated unique string that MUST not be freed.
315
316  Can return NULL in case of internal error.
317
318  Example:
319         if TZ_SYS_HOME == "/opt/home" then calling
320
321            tzplatform_context_mkstr(context, TZ_SYS_HOME,"-yes")
322
323         will return "/opt/home-yes"
324 */
325 extern
326 const char* tzplatform_context_mkstr(struct tzplatform_context *context,
327                                         enum tzplatform_variable id, const char *str);
328
329 /*
330  Return the string resulting of the path-concatenation of string value of the
331  tizen plaform variable 'id' and the given string 'path'.
332
333  path-concatenation is the concatenation taking care of / characters.
334
335  The returned value is an allocated unique string that MUST not be freed.
336
337  Can return NULL in case of internal error.
338
339  Example:
340         if TZ_SYS_HOME == "/opt/home" then calling
341
342            tzplatform_context_mkpath(context, TZ_SYS_HOME,"yes")
343
344         will return "/opt/home/yes"
345 */
346 extern
347 const char* tzplatform_context_mkpath(struct tzplatform_context *context,
348                                         enum tzplatform_variable id, const char *path);
349
350 /*
351  Return the string resulting of the path-concatenation of string value of the
352  tizen plaform variable 'id' and the given strings 'path' and 'path2'.
353
354  path-concatenation is the concatenation taking care of / characters.
355
356  The returned value is an allocated unique string that MUST not be freed.
357
358  Can return NULL in case of internal error.
359
360  Example:
361         if TZ_SYS_HOME == "/opt/home" then calling
362
363            tzplatform_context_mkpath3(context, TZ_SYS_HOME,"yes","no")
364
365         will return "/opt/home/yes/no"
366 */
367 extern
368 const char* tzplatform_context_mkpath3(struct tzplatform_context *context,
369                                         enum tzplatform_variable id, const char *path,
370                                         const char *path2);
371
372 /*
373  Return the string resulting of the path-concatenation of string value of the
374  tizen plaform variable 'id' and the given strings 'path', 'path2' and 'path3'.
375
376  path-concatenation is the concatenation taking care of / characters.
377
378  The returned value is an allocated unique string that MUST not be freed.
379
380  Can return NULL in case of internal error.
381
382  Example:
383         if TZ_SYS_HOME == "/opt/home" then calling
384
385            tzplatform_context_mkpath4(context, TZ_SYS_HOME,"yes","no","/maybe")
386
387         will return "/opt/home/yes/no/maybe"
388 */
389 extern
390 const char* tzplatform_context_mkpath4(struct tzplatform_context *context,
391                                         enum tzplatform_variable id, const char *path,
392                                         const char *path2, const char *path3);
393
394 /*
395  Return the uid for a given user name, stored in variable <id>
396  Retun -1 in case of error.
397
398  Example:
399         if TZ_USER_NAME=="app" then calling:
400
401            tzplatform_context_getuid(context, TZ_USER_NAME)
402
403         will return the uid of the user 'app'
404 */
405 extern
406 uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id);
407
408 /*
409  Return the gid for a given user name, stored in variable <id>
410  Retun -1 in case of error.
411
412  Example:
413         if TZ_USER_NAME=="app" then calling:
414
415            tzplatform_context_getuid(context, TZ_USER_NAME)
416
417         will return the gid of the user 'app'
418 */
419 extern
420 gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id);
421
422
423 /*
424  Return 1 if given uid is in the system admin group (named "system")
425  Return 0 if not
426  Return -1 in case of error.
427
428  Example:
429                 tzplatform_has_system_group(1000)
430
431         will return 0 or 1 depends on right of given uid.
432
433    NOTE :
434    * If you pass the -1 value to this function it will take the current uid given
435    * by the POSIX function getuid();
436
437    *** WARNING : ***
438    * This is a temporary feature
439    * This will be managed by Cynara
440
441 */
442 extern
443 int tzplatform_has_system_group(uid_t uid);
444
445 #ifdef __cplusplus
446 }
447 #endif
448
449 #endif /* LIBTIZEN_PLATFORM_WRAPPER */
450