Fix to handle invalid package type input parameter as an error
[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_INVALID_PARAMETER) {
327         return ret;
328     } else if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
329                 char* tempPrivilege = NULL;
330                 char* token = NULL;
331                 char* temp = NULL;
332                 char* save = NULL;
333                 tempPrivilege = strdup(privilege);
334                 TryReturn(tempPrivilege != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] tempPrivilege's strdup is failed.");
335                 token = strtok_r(tempPrivilege, "/", &save);
336                 while (token) {
337                         temp = token;
338                         token = strtok_r(NULL, "/", &save);
339                 }
340                 *display_name = (char*)calloc(strlen(temp) + 1, sizeof(char));
341                 TryReturn(*display_name != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
342                 memcpy(*display_name, temp, strlen(temp));
343                 free(tempPrivilege);
344         } else if (ret == PRVINFO_ERROR_NONE) {
345                 ret = privilege_info_get_display_name_by_string_id(display_name_string_id, display_name);
346                 free(display_name_string_id);
347                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
348         } else {
349                 return PRVINFO_ERROR_INTERNAL_ERROR;
350         }
351         return PRVINFO_ERROR_NONE;
352 }
353
354
355 int privilege_info_get_description_string_id_by_pkgtype(const char* package_type, const char* api_version, const char *privilege, char **description_string_id)
356 {
357         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
358         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
359         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
360
361         char* temp = NULL;
362
363         /* Check package_type */
364         int ret = 0;
365
366         if (strcmp(package_type, "PRVINFO_PACKAGE_TYPE_WEB") == 0) {
367                 LOGD("package_type = %s", package_type);
368                 ret = privilege_db_manager_get_privilege_description(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_WRT, privilege, api_version, &temp);
369                 if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
370                         if (temp == NULL)
371                                 *description_string_id = NULL;
372                         else if (strcmp(temp, "") == 0)
373                                 *description_string_id = NULL;
374                         else
375                                 goto err_none;
376                 } else if (ret != PRIVILEGE_DB_NO_EXIST_RESULT) {
377                         goto err_internal_error;
378                 }
379
380                 if (temp != NULL) {
381                         free(temp);
382                         temp = NULL;
383                 }
384         } else if (strcmp(package_type, "PRVINFO_PACKAGE_TYPE_NATIVE") == 0) {
385                 LOGD("package_type = %s", package_type);
386         } else {
387                 LOGD("package_type = %s", package_type);
388                 free(temp);
389                 return PRVINFO_ERROR_INVALID_PARAMETER;
390         }
391
392         ret = privilege_db_manager_get_privilege_description(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
393
394         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) {
395                 if (temp == NULL)
396                         goto err_no_matching_privilege;
397                 else if (strcmp(temp, "") == 0)
398                         goto err_no_matching_privilege;
399                 else
400                         goto err_none;
401         } else if (ret == PRIVILEGE_DB_NO_EXIST_RESULT) {
402                 goto err_no_matching_privilege;
403         } else {
404                 goto err_internal_error;
405         }
406
407
408 err_none:
409         *description_string_id = (char*)calloc(strlen(temp) + 1, sizeof(char));
410         TryReturn(*description_string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
411         memcpy(*description_string_id, temp, strlen(temp));
412         LOGD("description_string_id = %s", *description_string_id);
413         free(temp);
414         return PRVINFO_ERROR_NONE;
415
416 err_no_matching_privilege:
417         *description_string_id = NULL;
418         free(temp);
419         return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
420
421 err_internal_error:
422         free(temp);
423         return PRVINFO_ERROR_INTERNAL_ERROR;
424 }
425
426 PI_API
427 int privilege_info_get_description_by_pkgtype(const char* package_type, const char* api_version, const char* privilege, char** description)
428 {
429         int ret = 0;
430         char* description_string_id = NULL;
431
432         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
433         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
434         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
435
436         ret = privilege_info_get_description_string_id_by_pkgtype(package_type, api_version, privilege, &description_string_id);
437
438         if (ret == PRVINFO_ERROR_INVALID_PARAMETER) {
439                 return ret;
440         } else if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
441                 char* temp = NULL;
442                 temp = dgettext("privilege", "IDS_TPLATFORM_BODY_THIS_PRIVILEGE_IS_NOT_DEFINED");
443                 *description = (char*)calloc(strlen(temp) + 1, sizeof(char));
444                 TryReturn(*description != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
445                 memcpy(*description, temp, strlen(temp));
446                 /* return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE; */
447         } else if (ret == PRVINFO_ERROR_NONE) {
448                 ret = privilege_info_get_description_by_string_id(description_string_id, description);
449                 free(description_string_id);
450                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
451         } else {
452                 return PRVINFO_ERROR_INTERNAL_ERROR;
453         }
454         return PRVINFO_ERROR_NONE;
455 }
456