4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Sunghyun Kwon <sh0701.kwon@samsung.com>, Minsoo Kim <minnsoo.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * This file contains functionality related to cynara(privilege)
25 * to interact with email-service.
26 * @file email-core-cynara.c
27 * @author sh0701.kwon@samsung.com
29 * @brief This file contains functionality to provide cynara support in email-service.
33 #include <cynara-error.h>
34 #include <cynara-client.h>
35 #include <cynara-session.h>
36 #include <cynara-creds-commons.h>
37 #include <cynara-creds-socket.h>
39 #include "email-debug-log.h"
40 #include "email-utilities.h"
42 typedef struct _cynara_info_t {
44 enum cynara_client_creds client_method;
45 enum cynara_user_creds user_method;
48 static cynara_info_t *cynara_info = NULL;
49 pthread_mutex_t cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
51 INTERNAL_FUNC int emcore_init_cynara()
53 EM_DEBUG_FUNC_BEGIN();
54 int ret = CYNARA_API_SUCCESS;
55 int err = EMAIL_ERROR_NONE;
56 char errno_buf[ERRNO_BUF_SIZE] = {0};
58 ENTER_CRITICAL_SECTION(cynara_mutex);
59 cynara_info = (cynara_info_t *)em_malloc(sizeof(cynara_info));
60 if (cynara_info == NULL) {
61 EM_DEBUG_EXCEPTION("em_mallocfailed");
62 err = EMAIL_ERROR_OUT_OF_MEMORY;
66 ret = cynara_initialize(&(cynara_info->email_cynara), NULL);
67 if (ret != CYNARA_API_SUCCESS) {
68 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
69 EM_DEBUG_EXCEPTION("cynara_initialize failed : [%d], error : [%s]",
72 err = EMAIL_ERROR_NOT_INITIALIZED;
76 ret = cynara_creds_get_default_client_method(&(cynara_info->client_method));
77 if (ret != CYNARA_API_SUCCESS) {
78 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
79 EM_DEBUG_EXCEPTION("cynara_creds_get_default_client_method failed : [%d], error : [%s]",
82 err = EMAIL_ERROR_NOT_INITIALIZED;
86 ret = cynara_creds_get_default_user_method(&(cynara_info->user_method));
87 if (ret != CYNARA_API_SUCCESS) {
88 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
89 EM_DEBUG_EXCEPTION("cynara_creds_get_default_user_method failed : [%d], error : [%s]",
92 err = EMAIL_ERROR_NOT_INITIALIZED;
98 LEAVE_CRITICAL_SECTION(cynara_mutex);
104 INTERNAL_FUNC void emcore_finish_cynara()
106 EM_DEBUG_FUNC_BEGIN();
107 int ret = CYNARA_API_SUCCESS;
108 char errno_buf[ERRNO_BUF_SIZE] = {0};
110 if (cynara_info == NULL) {
111 EM_DEBUG_EXCEPTION("cynara did not initialize");
115 ENTER_CRITICAL_SECTION(cynara_mutex);
116 ret = cynara_finish(cynara_info->email_cynara);
117 if (ret != CYNARA_API_SUCCESS) {
118 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
119 EM_DEBUG_EXCEPTION("cynara_finish failed : [%d], error : [%s]",
123 EM_SAFE_FREE(cynara_info);
124 LEAVE_CRITICAL_SECTION(cynara_mutex);
129 INTERNAL_FUNC int emcore_check_privilege(unsigned int socket_fd)
131 EM_DEBUG_FUNC_BEGIN();
132 int ret = CYNARA_API_SUCCESS;
133 int err = EMAIL_ERROR_NONE;
134 char errno_buf[ERRNO_BUF_SIZE] = {0};
137 EM_DEBUG_EXCEPTION("Invalid parameter");
138 err = EMAIL_ERROR_INVALID_PARAM;
142 if (cynara_info->email_cynara == NULL) {
143 err = emcore_init_cynara();
144 if (err != EMAIL_ERROR_NONE) {
145 EM_DEBUG_EXCEPTION("emcore_init_cynara failed : [%d]", err);
150 err = EMAIL_ERROR_PERMISSION_DENIED;
152 pid_t client_pid = 0;
153 char *client_uid = NULL;
154 char *client_smack = NULL;
155 char *client_session = NULL;
157 ENTER_CRITICAL_SECTION(cynara_mutex);
159 ret = cynara_creds_socket_get_client(socket_fd, cynara_info->client_method, &client_smack);
160 if (ret != CYNARA_API_SUCCESS) {
161 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
162 EM_DEBUG_EXCEPTION("cynara_creds_socket_get_client failed : [%d], error : [%s]",
168 ret = cynara_creds_socket_get_user(socket_fd, cynara_info->user_method, &client_uid);
169 if (ret != CYNARA_API_SUCCESS) {
170 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
171 EM_DEBUG_EXCEPTION("cynara_creds_socket_get_user failed : [%d], error : [%s]",
177 ret = cynara_creds_socket_get_pid(socket_fd, &client_pid);
178 if (ret != CYNARA_API_SUCCESS) {
179 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
180 EM_DEBUG_EXCEPTION("cynara_creds_socket_get_pid failed : [%d], error : [%s]",
186 client_session = cynara_session_from_pid(client_pid);
187 if (client_session == NULL) {
188 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
189 EM_DEBUG_EXCEPTION("cynara_session_from_pid failed error : [%s]",
194 ret = cynara_check(cynara_info->email_cynara, client_smack, client_session, client_uid,
195 "http://tizen.org/privilege/email");
196 if (ret != CYNARA_API_ACCESS_ALLOWED) {
197 cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE);
198 EM_DEBUG_EXCEPTION("cynara_check failed : [%d], error : [%s]",
204 err = EMAIL_ERROR_NONE;
208 LEAVE_CRITICAL_SECTION(cynara_mutex);
210 EM_SAFE_FREE(client_uid);
211 EM_SAFE_FREE(client_smack);
212 EM_SAFE_FREE(client_session);