Using gdbus for IPC instead of com-core package
[platform/core/api/notification.git] / src / notification_permission.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <db-util.h>
26
27 #include <notification.h>
28 #include <notification_db.h>
29 #include <notification_noti.h>
30 #include <notification_debug.h>
31 #include <notification_ipc.h>
32 #include <notification_private.h>
33 #include <notification_permission.h>
34 #include <cynara-client.h>
35
36 #define NOTIFICATION_DB_ACCESS_READ 0
37 #define NOTIFICATION_DB_ACCESS_WRITE 1
38 #define SMACK_LABEL_LEN 255
39
40 int notification_check_permission()
41 {
42         cynara *p_cynara;
43
44         int fd = 0;
45         int ret = 0;
46         char subject_label[SMACK_LABEL_LEN + 1] = "";
47         char uid[10] = {0,};
48         char *client_session = "";
49
50         static bool checked_privilege = FALSE;
51
52         if (checked_privilege)
53                 return NOTIFICATION_ERROR_NONE;
54
55         ret = cynara_initialize(&p_cynara, NULL);
56         if (ret != CYNARA_API_SUCCESS) {
57                 LOGE("cannot init cynara [%d] failed!", ret);
58                 ret = NOTIFICATION_ERROR_IO_ERROR;
59                 goto out;
60         }
61
62         fd = open("/proc/self/attr/current", O_RDONLY);
63         if (fd < 0) {
64                 LOGE("open [%d] failed!", errno);
65                 ret = NOTIFICATION_ERROR_IO_ERROR;
66                 goto out;
67         }
68
69         ret = read(fd, subject_label, SMACK_LABEL_LEN);
70         if (ret < 0) {
71                 LOGE("read [%d] failed!", errno);
72                 close(fd);
73                 ret = NOTIFICATION_ERROR_IO_ERROR;
74                 goto out;
75         }
76         close(fd);
77
78         snprintf(uid, 10, "%d", getuid());
79         ret = cynara_check(p_cynara, subject_label, client_session, uid,
80                         "http://tizen.org/privilege/notification");
81         if (ret != CYNARA_API_ACCESS_ALLOWED) {
82                 LOGE("cynara access check [%d] failed!", ret);
83                 ret = NOTIFICATION_ERROR_PERMISSION_DENIED;
84                 goto out;
85         }
86         ret = NOTIFICATION_ERROR_NONE;
87         checked_privilege = TRUE;
88 out:
89
90         if (p_cynara)
91                 cynara_finish(p_cynara);
92
93         return ret;
94 }
95