Apply tizen coding rule
[platform/core/security/privilege-info.git] / src / privilege_info.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <libintl.h>
4 #include <dlog.h>
5 #include <privilege_db_manager.h>
6 #include "privilege_information.h"
7 #ifdef LOG_TAG
8 #undef LOG_TAG
9 #define LOG_TAG "PRIVILEGE_INFO"
10 #endif
11
12 #define TryReturn(condition, expr, returnValue, ...)  \
13         if (!(condition)) { \
14                 expr; \
15                 LOGE(__VA_ARGS__); \
16                 return returnValue; \
17         }
18
19 #ifndef PI_API
20 #define PI_API __attribute__((visibility("default")))
21 #endif
22
23 typedef enum {
24         PRVINFO_ERROR_NO_MATCHING_PRIVILEGE             = TIZEN_ERROR_PRIVILEGE_INFORMATION | 0x01
25 } privilege_info_internal_error_e;
26
27 int privilege_info_get_display_name_string_id(const char* api_version, const char *privilege, char **display_name_string_id)
28 {
29         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
30         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
31
32         char* temp = NULL;
33
34         /* Check NATIVE */
35         int ret = privilege_db_manager_get_privilege_display(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
36
37         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
38                 if (temp == NULL)
39                         *display_name_string_id = NULL;
40                 else if (strcmp(temp, "") == 0)
41                         *display_name_string_id = NULL;
42                 else
43                         goto err_none;
44         } else if (ret != PRIVILEGE_DB_NO_EXIST_RESULT) {
45                 goto err_internal_error;
46         }
47
48         if (temp != NULL) {
49                 free(temp);
50                 temp = NULL;
51         }
52
53         /* Check WEB */
54         ret = privilege_db_manager_get_privilege_display(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_WRT, privilege, api_version, &temp);
55
56         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
57                 if (temp == NULL)
58                         goto err_no_matching_privilege;
59                 else if (strcmp(temp, "") == 0)
60                         goto err_no_matching_privilege;
61                 else
62                         goto err_none;
63         } else if (ret == PRIVILEGE_DB_NO_EXIST_RESULT) {
64                 goto err_no_matching_privilege;
65         } else {
66                 goto err_internal_error;
67         }
68
69 err_none:
70         *display_name_string_id = (char*)calloc(strlen(temp) + 1, sizeof(char));
71         TryReturn(*display_name_string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
72         memcpy(*display_name_string_id, temp, strlen(temp));
73         LOGD("display_name_string_id = %s", *display_name_string_id);
74         free(temp);
75         return PRVINFO_ERROR_NONE;
76
77 err_no_matching_privilege:
78         *display_name_string_id = NULL;
79         free(temp);
80         return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
81
82 err_internal_error:
83         free(temp);
84         return PRVINFO_ERROR_INTERNAL_ERROR;
85 }
86
87
88 int privilege_info_get_display_name_by_string_id(const char *string_id, char **display_name)
89 {
90         char *temp = NULL;
91         TryReturn(string_id != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] string_id is NULL");
92
93         temp = dgettext("privilege", string_id);
94
95         *display_name = (char*)calloc(strlen(temp) + 1, sizeof(char));
96         TryReturn(*display_name != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
97
98         memcpy(*display_name, temp, strlen(temp));
99
100         return PRVINFO_ERROR_NONE;
101 }
102
103 PI_API
104 int privilege_info_get_display_name(const char* api_version, const char* privilege, char** display_name)
105 {
106         int ret = 0;
107         char* display_name_string_id = NULL;
108
109         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
110         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
111
112         ret = privilege_info_get_display_name_string_id(api_version, privilege, &display_name_string_id);
113
114         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
115                 char* tempPrivilege = NULL;
116                 char* token = NULL;
117                 char* temp = NULL;
118                 char* save = NULL;
119
120                 tempPrivilege = strdup(privilege);
121                 TryReturn(tempPrivilege != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] tempPrivilege's strdup is failed.");
122
123                 token = strtok_r(tempPrivilege, "/", &save);
124                 while (token) {
125                         temp = token;
126                         token = strtok_r(NULL, "/", &save);
127                 }
128                 *display_name = (char*)calloc(strlen(temp) + 1, sizeof(char));
129                 TryReturn(*display_name != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
130                 memcpy(*display_name, temp, strlen(temp));
131                 free(tempPrivilege);
132         } else if (ret == PRVINFO_ERROR_NONE) {
133                 ret = privilege_info_get_display_name_by_string_id(display_name_string_id, display_name);
134                 free(display_name_string_id);
135                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
136         } else {
137                 return PRVINFO_ERROR_INTERNAL_ERROR;
138         }
139         return PRVINFO_ERROR_NONE;
140 }
141
142
143 int privilege_info_get_description_string_id(const char* api_version, const char *privilege, char **description_string_id)
144 {
145         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
146         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
147
148         char* temp = NULL;
149
150         /* Check NATIVE */
151         int ret = privilege_db_manager_get_privilege_description(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
152
153         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
154                 if (temp == NULL)
155                         *description_string_id = NULL;
156                 else if (strcmp(temp, "") == 0)
157                         *description_string_id = NULL;
158                 else
159                         goto err_none;
160         } else if (ret != PRIVILEGE_DB_NO_EXIST_RESULT) {
161                 goto err_internal_error;
162         }
163
164         if (temp != NULL) {
165                 free(temp);
166                 temp = NULL;
167         }
168         /* Check WEB */
169         ret = privilege_db_manager_get_privilege_description(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_WRT, privilege, api_version, &temp);
170
171         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
172                 if (temp == NULL)
173                         goto err_no_matching_privilege;
174                 else if (strcmp(temp, "") == 0)
175                         goto err_no_matching_privilege;
176                 else
177                         goto err_none;
178         } else if (ret == PRIVILEGE_DB_NO_EXIST_RESULT) {
179                 goto err_no_matching_privilege;
180         } else {
181                 goto err_internal_error;
182         }
183
184 err_none:
185         *description_string_id = (char*)calloc(strlen(temp) + 1, sizeof(char));
186         TryReturn(*description_string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
187         memcpy(*description_string_id, temp, strlen(temp));
188         LOGD("description_string_id = %s", *description_string_id);
189         free(temp);
190         return PRVINFO_ERROR_NONE;
191
192 err_no_matching_privilege:
193         *description_string_id = NULL;
194         free(temp);
195         return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
196
197 err_internal_error:
198         free(temp);
199         return PRVINFO_ERROR_INTERNAL_ERROR;
200 }
201
202 int privilege_info_get_description_by_string_id(const char *string_id, char **description)
203 {
204         char *temp = NULL;
205         TryReturn(string_id != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] string_id is NULL");
206
207         temp = dgettext("privilege", string_id);
208
209         *description = (char*)calloc(strlen(temp) + 1, sizeof(char));
210         TryReturn(*description != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
211
212         memcpy(*description, temp, strlen(temp));
213
214         return PRVINFO_ERROR_NONE;
215 }
216
217 PI_API
218 int privilege_info_get_description(const char* api_version, const char* privilege, char** description)
219 {
220         int ret = 0;
221         char* description_string_id = NULL;
222
223         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
224         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
225
226         ret = privilege_info_get_description_string_id(api_version, privilege, &description_string_id);
227
228         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
229                 char* temp = NULL;
230                 temp = dgettext("privilege", "IDS_TPLATFORM_BODY_THIS_PRIVILEGE_IS_NOT_DEFINED");
231                 *description = (char*)calloc(strlen(temp) + 1, sizeof(char));
232                 TryReturn(*description != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
233
234                 memcpy(*description, temp, strlen(temp));
235         } else if (ret == PRVINFO_ERROR_NONE) {
236                 ret = privilege_info_get_description_by_string_id(description_string_id, description);
237                 free(description_string_id);
238                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
239         } else {
240                 return PRVINFO_ERROR_INTERNAL_ERROR;
241         }
242         return PRVINFO_ERROR_NONE;
243 }
244
245 int privilege_info_get_display_name_string_id_by_pkgtype(const char* package_type, const char* api_version, const char *privilege, char **display_name_string_id)
246 {
247         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
248         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
249         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
250
251         char* temp = NULL;
252
253         /* Check package_type */
254         int ret = 0;
255         if (strcmp(package_type, "PRVINFO_PACKAGE_TYPE_NATIVE") == 0) {
256                 LOGD("package_type = %s", package_type);
257         } else if (strcmp(package_type, "PRVINFO_PACKAGE_TYPE_WEB") == 0) {
258                 LOGD("package_type = %s", package_type);
259                 ret = privilege_db_manager_get_privilege_display(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_WRT, privilege, api_version, &temp);
260                 if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
261                         if (temp == NULL)
262                                 *display_name_string_id = NULL;
263                         else if (strcmp(temp, "") == 0)
264                                 *display_name_string_id = NULL;
265                         else
266                                 goto err_none;
267                 } else if (ret != PRIVILEGE_DB_NO_EXIST_RESULT) {
268                         goto err_internal_error;
269                 }
270
271                 if (temp != NULL) {
272                         free(temp);
273                         temp = NULL;
274                 }
275         } else {
276                 LOGD("package_type = %s", package_type);
277                 free(temp);
278                 return PRVINFO_ERROR_INVALID_PARAMETER;
279         }
280
281         ret = privilege_db_manager_get_privilege_display(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
282
283         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
284                 if (temp == NULL)
285                         goto err_no_matching_privilege;
286                 else if (strcmp(temp, "") == 0)
287                         goto err_no_matching_privilege;
288                 else
289                         goto err_none;
290         } else if (ret == PRIVILEGE_DB_NO_EXIST_RESULT) {
291                 goto err_no_matching_privilege;
292         } else {
293                 goto err_internal_error;
294         }
295
296 err_none:
297         *display_name_string_id = (char*)calloc(strlen(temp) + 1, sizeof(char));
298         TryReturn(*display_name_string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
299         memcpy(*display_name_string_id, temp, strlen(temp));
300         LOGD("display_name_string_id = %s", *display_name_string_id);
301         free(temp);
302         return PRVINFO_ERROR_NONE;
303
304 err_no_matching_privilege:
305         *display_name_string_id = NULL;
306         free(temp);
307         return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
308
309 err_internal_error:
310         free(temp);
311         return PRVINFO_ERROR_INTERNAL_ERROR;
312 }
313
314 PI_API
315 int privilege_info_get_display_name_by_pkgtype(const const char* package_type, const char* api_version, const char* privilege, char** display_name)
316 {
317         int ret = 0;
318         char* display_name_string_id = NULL;
319
320         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
321         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
322         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
323
324         ret = privilege_info_get_display_name_string_id_by_pkgtype(package_type, api_version, privilege, &display_name_string_id);
325
326         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
327                 char* tempPrivilege = NULL;
328                 char* token = NULL;
329                 char* temp = NULL;
330                 char* save = NULL;
331                 tempPrivilege = strdup(privilege);
332                 TryReturn(tempPrivilege != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] tempPrivilege's strdup is failed.");
333                 token = strtok_r(tempPrivilege, "/", &save);
334                 while (token) {
335                         temp = token;
336                         token = strtok_r(NULL, "/", &save);
337                 }
338                 *display_name = (char*)calloc(strlen(temp) + 1, sizeof(char));
339                 TryReturn(*display_name != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
340                 memcpy(*display_name, temp, strlen(temp));
341                 free(tempPrivilege);
342         } else if (ret == PRVINFO_ERROR_NONE) {
343                 ret = privilege_info_get_display_name_by_string_id(display_name_string_id, display_name);
344                 free(display_name_string_id);
345                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
346         } else {
347                 return PRVINFO_ERROR_INTERNAL_ERROR;
348         }
349         return PRVINFO_ERROR_NONE;
350 }
351
352
353 int privilege_info_get_description_string_id_by_pkgtype(const char* package_type, const char* api_version, const char *privilege, char **description_string_id)
354 {
355         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
356         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
357         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
358
359         char* temp = NULL;
360
361         /* Check package_type */
362         int ret = 0;
363
364         if (strcmp(package_type, "PRVINFO_PACKAGE_TYPE_WEB") == 0) {
365                 LOGD("package_type = %s", package_type);
366                 ret = privilege_db_manager_get_privilege_description(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_WRT, privilege, api_version, &temp);
367                 if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
368                         if (temp == NULL)
369                                 *description_string_id = NULL;
370                         else if (strcmp(temp, "") == 0)
371                                 *description_string_id = NULL;
372                         else
373                                 goto err_none;
374                 } else if (ret != PRIVILEGE_DB_NO_EXIST_RESULT) {
375                         goto err_internal_error;
376                 }
377
378                 if (temp != NULL) {
379                         free(temp);
380                         temp = NULL;
381                 }
382         } else if (strcmp(package_type, "PRVINFO_PACKAGE_TYPE_NATIVE") == 0) {
383                 LOGD("package_type = %s", package_type);
384         } else {
385                 LOGD("package_type = %s", package_type);
386                 free(temp);
387                 return PRVINFO_ERROR_INVALID_PARAMETER;
388         }
389
390         ret = privilege_db_manager_get_privilege_description(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
391
392         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
393                 if (temp == NULL)
394                         goto err_no_matching_privilege;
395                 else if (strcmp(temp, "") == 0)
396                         goto err_no_matching_privilege;
397                 else
398                         goto err_none;
399         } else if (ret == PRIVILEGE_DB_NO_EXIST_RESULT) {
400                 goto err_no_matching_privilege;
401         } else {
402                 goto err_internal_error;
403         }
404
405
406 err_none:
407         *description_string_id = (char*)calloc(strlen(temp) + 1, sizeof(char));
408         TryReturn(*description_string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
409         memcpy(*description_string_id, temp, strlen(temp));
410         LOGD("description_string_id = %s", *description_string_id);
411         free(temp);
412         return PRVINFO_ERROR_NONE;
413
414 err_no_matching_privilege:
415         *description_string_id = NULL;
416         free(temp);
417         return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
418
419 err_internal_error:
420         free(temp);
421         return PRVINFO_ERROR_INTERNAL_ERROR;
422 }
423
424 PI_API
425 int privilege_info_get_description_by_pkgtype(const char* package_type, const char* api_version, const char* privilege, char** description)
426 {
427         int ret = 0;
428         char* description_string_id = NULL;
429
430         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
431         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
432         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
433
434         ret = privilege_info_get_description_string_id_by_pkgtype(package_type, api_version, privilege, &description_string_id);
435
436         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
437                 char* temp = NULL;
438                 temp = dgettext("privilege", "IDS_TPLATFORM_BODY_THIS_PRIVILEGE_IS_NOT_DEFINED");
439                 *description = (char*)calloc(strlen(temp) + 1, sizeof(char));
440                 TryReturn(*description != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
441                 memcpy(*description, temp, strlen(temp));
442                 /* return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE; */
443         } else if (ret == PRVINFO_ERROR_NONE) {
444                 ret = privilege_info_get_description_by_string_id(description_string_id, description);
445                 free(description_string_id);
446                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
447         } else {
448                 return PRVINFO_ERROR_INTERNAL_ERROR;
449         }
450         return PRVINFO_ERROR_NONE;
451 }
452