7 #include <cynara-session.h>
8 #include <cynara-client.h>
9 #include <cynara-creds-socket.h>
10 #include <sys/smack.h>
12 #include <libds/log.h>
15 #define CYNARA_BUFSIZE 128
23 static struct ds_cynara ds_cynara;
25 static bool ds_cynara_init(void);
26 static void ds_cynara_finish(void);
27 static bool ds_cynara_check_privilege(pid_t pid, uid_t uid,
28 const char *privilege);
31 tizen_security_check_privilege(pid_t pid, uid_t uid, const char *privilege)
33 return ds_cynara_check_privilege(pid, uid, privilege);
37 tizen_security_init(void)
39 return ds_cynara_init();
43 tizen_security_finish(void)
49 print_cynara_error(int err, const char *fmt, ...)
53 char buf[CYNARA_BUFSIZE] = "\0";
54 char tmp[CYNARA_BUFSIZE + CYNARA_BUFSIZE] = "\0";
58 vsnprintf(tmp, CYNARA_BUFSIZE + CYNARA_BUFSIZE, fmt, args);
62 ret = cynara_strerror(err, buf, CYNARA_BUFSIZE);
63 if (ret != CYNARA_API_SUCCESS) {
64 ds_err("Failed to get cynara_strerror. error : %d (error log about %s: %d)\n", ret, tmp, err);
68 ds_err("%s is failed. (%s)\n", tmp, buf);
74 int ret = CYNARA_API_SUCCESS;
77 if (++ds_cynara.references != 1)
80 for (retry_cnt = 0; retry_cnt < 5; retry_cnt++) {
81 ds_dbg("Retry cynara initialize: %d\n", retry_cnt + 1);
83 ret = cynara_initialize(&ds_cynara.handle, NULL);
85 if (CYNARA_API_SUCCESS == ret) {
86 ds_dbg("Succeed to initialize cynara !\n");
90 print_cynara_error(ret, "cynara_initialize");
93 ds_err("Failed to initialize cynara! (error:%d, retry_cnt=%d)\n",
96 --ds_cynara.references;
103 ds_cynara_finish(void)
105 if (ds_cynara.references < 1) {
106 ds_err("%s called without ds_cynara_init\n", __FUNCTION__);
110 if (--ds_cynara.references != 0)
113 cynara_finish(ds_cynara.handle);
114 ds_cynara.handle = NULL;
118 ds_cynara_check_privilege(pid_t pid, uid_t uid, const char *privilege)
121 char *client_smack = NULL;
122 char *client_session = NULL;
123 char uid_str[16] = { 0, };
127 if (!ds_cynara.handle) {
128 ds_err("ds_cynara has not been initialized.\n");
132 ret = smack_new_label_from_process((int)pid, &client_smack);
136 snprintf(uid_str, 15, "%d", (int)uid);
138 client_session = cynara_session_from_pid(pid);
142 ret = cynara_check(ds_cynara.handle, client_smack, client_session,
145 if (ret == CYNARA_API_ACCESS_ALLOWED)
148 print_cynara_error(ret, "privilege: %s, client_smack: %s, pid: %d",
149 privilege, client_smack, pid);
152 ds_dbg("Privilege Check For '%s' %s pid:%u uid:%u client_smack:%s(len:%d) "
153 "client_session:%s ret:%d",
154 privilege, res ? "SUCCESS" : "FAIL", pid, uid,
155 client_smack ? client_smack : "N/A", len,
156 client_session ? client_session: "N/A", ret);
159 free(client_session);