Check privilege in app_context_set_event_cb
[platform/core/api/app-manager.git] / src / app_manager.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23
24 #include <aul.h>
25 #include <dlog.h>
26 #include <cynara-client.h>
27
28 #include "app_manager.h"
29 #include "app_manager_internal.h"
30
31 #ifdef LOG_TAG
32 #undef LOG_TAG
33 #endif
34
35 #define LOG_TAG "CAPI_APPFW_APP_MANAGER"
36
37 #define SMACK_LABEL_LEN 255
38
39 static const char* app_manager_error_to_string(app_manager_error_e error)
40 {
41         switch (error) {
42         case APP_MANAGER_ERROR_NONE:
43                 return "Successful";
44         case APP_MANAGER_ERROR_INVALID_PARAMETER:
45                 return "Invalid parameter";
46         case APP_MANAGER_ERROR_OUT_OF_MEMORY:
47                 return "Out of memory";
48         case APP_MANAGER_ERROR_IO_ERROR:
49                 return "IO error";
50         case APP_MANAGER_ERROR_NO_SUCH_APP:
51                 return "No such application";
52         case APP_MANAGER_ERROR_DB_FAILED:
53                 return "DB error";
54         case APP_MANAGER_ERROR_INVALID_PACKAGE:
55                 return "Invalid package";
56         case APP_MANAGER_ERROR_NOT_SUPPORTED:
57                 return "Not supported";
58         case APP_MANAGER_ERROR_PERMISSION_DENIED:
59                 return "Permission denied";
60         default:
61                 return "Unknown";
62         }
63 }
64
65 int app_manager_error(app_manager_error_e error, const char* function, const char *description)
66 {
67         if (description)
68                 LOGE("[%s] %s(0x%08x) : %s", function, app_manager_error_to_string(error), error, description);
69         else
70                 LOGE("[%s] %s(0x%08x)", function, app_manager_error_to_string(error), error);
71
72         return error;
73 }
74
75 int app_manager_check_privilege(char *privilege)
76 {
77         cynara *p_cynara;
78         int fd;
79         int ret;
80
81         char client[SMACK_LABEL_LEN + 1] = "";
82         char uid[10] = {0,};
83         char *client_session = "";
84
85         if (privilege == NULL) {
86                 LOGE("invalid parameter");
87                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
88         }
89
90         ret = cynara_initialize(&p_cynara, NULL);
91         if (ret != CYNARA_API_SUCCESS) {
92                 LOGE("cynara_initialize [%d] failed!", ret);
93                 return APP_MANAGER_ERROR_IO_ERROR;
94         }
95
96         fd = open("/proc/self/attr/current", O_RDONLY);
97         if (fd < 0) {
98                 LOGE("open [%d] failed!", errno);
99                 ret = APP_MANAGER_ERROR_IO_ERROR;
100                 goto out;
101         }
102
103         ret = read(fd, client, SMACK_LABEL_LEN);
104         if (ret < 0) {
105                 LOGE("read [%d] failed!", errno);
106                 close(fd);
107                 ret = APP_MANAGER_ERROR_IO_ERROR;
108                 goto out;
109         }
110
111         close(fd);
112
113         snprintf(uid, 10, "%d", getuid());
114
115         ret = cynara_check(p_cynara, client, client_session, uid, privilege);
116         if (ret != CYNARA_API_ACCESS_ALLOWED) {
117                 LOGE("cynara access check [%d] failed!", ret);
118
119                 if (ret == CYNARA_API_ACCESS_DENIED)
120                         ret = APP_MANAGER_ERROR_PERMISSION_DENIED;
121                 else
122                         ret = APP_MANAGER_ERROR_IO_ERROR;
123
124                 goto out;
125         }
126
127         ret = APP_MANAGER_ERROR_NONE;
128
129 out:
130         if (p_cynara)
131                 cynara_finish(p_cynara);
132
133         return ret;
134 }
135
136 API int app_manager_set_app_context_event_cb(app_manager_app_context_event_cb callback, void *user_data)
137 {
138         int retval = app_context_set_event_cb(callback, user_data);
139
140         if (retval != APP_MANAGER_ERROR_NONE)
141                 return app_manager_error(retval, __FUNCTION__, NULL);
142         else
143                 return APP_MANAGER_ERROR_NONE;
144 }
145
146 API void app_manager_unset_app_context_event_cb(void)
147 {
148         app_context_unset_event_cb();
149 }
150
151 API int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
152 {
153         int retval = app_context_foreach_app_context(callback, user_data);
154
155         if (retval != APP_MANAGER_ERROR_NONE)
156                 return app_manager_error(retval, __FUNCTION__, NULL);
157         else
158                 return APP_MANAGER_ERROR_NONE;
159 }
160
161 API int app_manager_foreach_running_app_context(app_manager_app_context_cb callback, void *user_data)
162 {
163         int retval = app_context_foreach_running_app_context(callback, user_data);
164
165         if (retval != APP_MANAGER_ERROR_NONE)
166                 return app_manager_error(retval, __FUNCTION__, NULL);
167         else
168                 return APP_MANAGER_ERROR_NONE;
169 }
170
171 API int app_manager_get_app_context(const char *app_id, app_context_h *app_context)
172 {
173         int retval = app_context_get_app_context(app_id, app_context);
174
175         if (retval != APP_MANAGER_ERROR_NONE)
176                 return app_manager_error(retval, __FUNCTION__, NULL);
177         else
178                 return APP_MANAGER_ERROR_NONE;
179 }
180
181 API int app_manager_resume_app(app_context_h app_context)
182 {
183         char *app_id;
184         int retval = APP_MANAGER_ERROR_NONE;
185
186         if (app_context == NULL)
187                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
188
189         if (app_context_get_app_id(app_context, &app_id) != APP_MANAGER_ERROR_NONE)
190                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the application ID");
191
192         if (aul_app_is_running(app_id) == 0) {
193                 if (app_id) {
194                         free(app_id);
195                         app_id = NULL;
196                 }
197                 return app_manager_error(APP_MANAGER_ERROR_APP_NO_RUNNING, __FUNCTION__, NULL);
198         }
199
200         retval = aul_resume_app(app_id);
201
202         if (app_id)
203                 free(app_id);
204
205         if (retval == AUL_R_EINVAL)
206                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
207         else if (retval == AUL_R_EILLACC)
208                 return app_manager_error(APP_MANAGER_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
209         else if (retval < 0)
210                 return app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
211
212         return APP_MANAGER_ERROR_NONE;
213 }
214
215 API int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
216 {
217         int retval;
218
219         retval = app_info_foreach_app_info(callback, user_data);
220
221         if (retval != APP_MANAGER_ERROR_NONE)
222                 return app_manager_error(retval, __FUNCTION__, NULL);
223         else
224                 return APP_MANAGER_ERROR_NONE;
225 }
226
227 API int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
228 {
229         int retval;
230
231         retval = app_info_create(app_id, app_info);
232
233         if (retval != APP_MANAGER_ERROR_NONE)
234                 return app_manager_error(retval, __FUNCTION__, NULL);
235         else
236                 return APP_MANAGER_ERROR_NONE;
237 }
238
239 API int app_manager_get_app_id(pid_t pid, char **app_id)
240 {
241         char buffer[256] = {0, };
242         char *app_id_dup = NULL;
243
244         if (app_id == NULL)
245                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
246
247         if (aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)) != AUL_R_OK)
248                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "Invalid process ID");
249
250         app_id_dup = strdup(buffer);
251         if (app_id_dup == NULL)
252                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
253
254         *app_id = app_id_dup;
255
256         return APP_MANAGER_ERROR_NONE;
257 }
258
259 API int app_manager_terminate_app(app_context_h app_context)
260 {
261         int retval;
262         pid_t pid = 0;
263
264         if (app_context == NULL)
265                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
266
267         if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
268                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
269
270         retval = aul_terminate_pid(pid);
271         if (retval == AUL_R_EINVAL) {
272                 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
273                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
274         } else if (retval == AUL_R_EILLACC) {
275                 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
276                 return APP_MANAGER_ERROR_PERMISSION_DENIED;
277         } else if (retval < 0) {
278                 return APP_MANAGER_ERROR_REQUEST_FAILED;
279         }
280
281         return APP_MANAGER_ERROR_NONE;
282 }
283
284 API int app_manager_request_terminate_bg_app(app_context_h app_context)
285 {
286         int retval = APP_MANAGER_ERROR_NONE;
287         pid_t pid = 0;
288
289         if (app_context == NULL)
290                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
291
292         if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
293                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
294
295         retval = aul_terminate_bgapp_pid(pid);
296         if (retval == AUL_R_EINVAL) {
297                 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
298                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
299         } else if (retval == AUL_R_EILLACC) {
300                 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
301                 return APP_MANAGER_ERROR_PERMISSION_DENIED;
302         } else if (retval < 0) {
303                 return APP_MANAGER_ERROR_REQUEST_FAILED;
304         }
305
306         return APP_MANAGER_ERROR_NONE;
307 }
308
309 API int app_manager_is_running(const char *app_id, bool *running)
310 {
311         if (app_id == NULL) {
312                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
313                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
314         }
315
316         if (running == NULL) {
317                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
318                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
319         }
320
321         *running = aul_app_is_running(app_id);
322
323         return APP_MANAGER_ERROR_NONE;
324 }
325
326 API int app_manager_get_shared_data_path(const char *app_id, char **path)
327 {
328         int r;
329         int retval = aul_get_app_shared_data_path_by_appid(app_id, path);
330
331         switch (retval) {
332         case AUL_R_OK:
333                 r = APP_MANAGER_ERROR_NONE;
334                 break;
335         case AUL_R_ENOAPP:
336                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
337                 break;
338         case AUL_R_EINVAL:
339                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
340                 break;
341         case AUL_R_ERROR:
342                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
343                 break;
344         case AUL_R_EREJECTED:
345                 r = app_manager_error(APP_MANAGER_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
346                 break;
347         default:
348                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
349                 break;
350         }
351
352         return r;
353 }
354
355 API int app_manager_get_shared_resource_path(const char *app_id, char **path)
356 {
357         int r;
358         int retval = aul_get_app_shared_resource_path_by_appid(app_id, path);
359
360         switch (retval) {
361         case AUL_R_OK:
362                 r = APP_MANAGER_ERROR_NONE;
363                 break;
364         case AUL_R_ENOAPP:
365                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
366                 break;
367         case AUL_R_EINVAL:
368                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
369                 break;
370         case AUL_R_ERROR:
371                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
372                 break;
373         default:
374                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
375                 break;
376         }
377
378         return r;
379 }
380
381 API int app_manager_get_shared_trusted_path(const char *app_id, char **path)
382 {
383         int r;
384         int retval = aul_get_app_shared_trusted_path_by_appid(app_id, path);
385
386         switch (retval) {
387         case AUL_R_OK:
388                 r = APP_MANAGER_ERROR_NONE;
389                 break;
390         case AUL_R_ENOAPP:
391                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
392                 break;
393         case AUL_R_EINVAL:
394                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
395                 break;
396         case AUL_R_ERROR:
397                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
398                 break;
399         default:
400                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
401                 break;
402         }
403
404         return r;
405 }
406
407 API int app_manager_get_external_shared_data_path(const char *app_id, char **path)
408 {
409         int r;
410         int retval = aul_get_app_external_shared_data_path_by_appid(app_id, path);
411
412         switch (retval) {
413         case AUL_R_OK:
414                 r = APP_MANAGER_ERROR_NONE;
415                 break;
416         case AUL_R_ENOAPP:
417                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
418                 break;
419         case AUL_R_EINVAL:
420                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
421                 break;
422         case AUL_R_ERROR:
423                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
424                 break;
425         default:
426                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
427                 break;
428         }
429
430         return r;
431 }