[SATIZENVUL-1315,1316] Reinforced the missing code
[platform/core/multimedia/media-server.git] / lib / media-util-cynara.c
1 /*
2  * Media Server
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@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 /**
23  * This file contains Cynara integration code
24  *
25  * @file                media-util-cynara.c
26  * @author      Jacek Bukarewicz (j.bukarewicz@samsung.com)
27  * @version     1.0
28  * @brief
29  */
30
31 #include <glib.h>
32 #include <unistd.h>
33
34 #include <media-util-cynara.h>
35 #include <media-util-dbg.h>
36 #include <media-util-err.h>
37 #include <media-util-internal.h>
38
39 #include <cynara-client.h>
40 #include <cynara-session.h>
41 #include <cynara-error.h>
42 #include <cynara-creds-socket.h>
43
44 /* this definition is missing in glibc headers (version 2.21). It was introduced in kernel version 2.6.17 */
45 #ifndef SCM_SECURITY
46 #define SCM_SECURITY 0x03
47 #endif
48
49 static cynara *_cynara = NULL;
50 static cynara_configuration *_p_conf = NULL;
51 G_LOCK_DEFINE_STATIC(cynara_mutex);
52
53 static void ms_cynara_dbg_err(const char *prefix, int error_code)
54 {
55         char error_buffer[256];
56         int err;
57         error_buffer[0] = '\0';
58
59         err = cynara_strerror(error_code, error_buffer, sizeof(error_buffer));
60         if (err == CYNARA_API_SUCCESS)
61                 MSAPI_DBG_ERR("%s: %s", prefix, error_buffer);
62         else
63                 MSAPI_DBG_ERR("%s: error code %i", prefix, error_code);
64 }
65
66 int ms_cynara_initialize(void)
67 {
68         int ret = 0;
69
70         ret = cynara_configuration_create(&_p_conf);
71         if (ret != CYNARA_API_SUCCESS)  {
72                 ms_cynara_dbg_err("cynara_configuration_create", ret);
73                 return MS_MEDIA_ERR_INTERNAL;
74         }
75         ret = cynara_configuration_set_cache_size(_p_conf, 100);
76         if (ret != CYNARA_API_SUCCESS)  {
77                 ms_cynara_dbg_err("cynara_configuration_set_cache_size", ret);
78                 return MS_MEDIA_ERR_INTERNAL;
79         }
80         ret = cynara_initialize(&_cynara, _p_conf);
81         if (ret  != CYNARA_API_SUCCESS) {
82                 ms_cynara_dbg_err("cynara_initialize", ret);
83                 return MS_MEDIA_ERR_INTERNAL;
84         }
85
86         cynara_configuration_destroy(_p_conf);
87
88         return MS_MEDIA_ERR_NONE;
89 }
90
91 void ms_cynara_finish(void)
92 {
93         cynara_finish(_cynara);
94         _cynara = NULL;
95 }
96
97 int ms_cynara_receive_untrusted_message(int sockfd, ms_comm_msg_s *recv_msg, ms_peer_credentials *credentials)
98 {
99         int ret = 0;
100         int recv_msg_size = 0;
101
102         if (!recv_msg || !credentials)
103                 return MS_MEDIA_ERR_INVALID_PARAMETER;
104
105         if ((recv_msg_size = read(sockfd, recv_msg, sizeof(ms_comm_msg_s))) < 0) {
106                 if (errno == EWOULDBLOCK) {
107                         MSAPI_DBG_ERR("Timeout. Can't try any more");
108                         return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
109                 } else {
110                         MSAPI_DBG_ERR("recv failed");
111                         return MS_MEDIA_ERR_SOCKET_RECEIVE;
112                 }
113         }
114
115         MSAPI_DBG_SLOG("receive msg from P[%d] T[%d] M[%.*s] S[%.*s]", recv_msg->pid, recv_msg->msg_type, MAX_MSG_SIZE, recv_msg->msg, MS_UUID_SIZE, recv_msg->storage_id);
116
117         if (strlen(recv_msg->msg) == 0 || strlen(recv_msg->msg) >= MAX_MSG_SIZE) {
118                 MSAPI_DBG_ERR("IPC message is wrong. message size is %d", strlen(recv_msg->msg));
119                 return MS_MEDIA_ERR_INVALID_IPC_MESSAGE;
120         }
121
122         if (strlen(recv_msg->storage_id) >= MS_UUID_SIZE) {
123                 MSAPI_DBG_ERR("IPC message is wrong. storage_id size is %d", strlen(recv_msg->storage_id));
124                 return MS_MEDIA_ERR_INVALID_IPC_MESSAGE;
125         }
126
127         ret = cynara_creds_socket_get_pid(sockfd, &(credentials->pid));
128         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get pid");
129
130         ret = cynara_creds_socket_get_user(sockfd, USER_METHOD_UID, &(credentials->uid));
131         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get uid");
132
133         ret = cynara_creds_socket_get_client(sockfd, CLIENT_METHOD_SMACK, &(credentials->smack));
134         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get smack");
135
136 //      MSAPI_DBG_ERR("cynara_creds_info : P[%d]U[%s]S[%s]", credentials->pid, credentials->uid, credentials->smack);
137
138         return MS_MEDIA_ERR_NONE;
139 }
140
141 int ms_cynara_receive_untrusted_message_thumb(int sockfd, thumbMsg *recv_msg, ms_peer_credentials *credentials)
142 {
143         int ret = 0;
144         int header_size = 0;
145         int recv_msg_size = 0;
146         unsigned char *buf = NULL;
147
148         if (!recv_msg || !credentials)
149                 return MS_MEDIA_ERR_INVALID_PARAMETER;
150
151         header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
152         MS_MALLOC(buf, header_size);
153
154         if ((recv_msg_size = recv(sockfd, buf, header_size, 0)) < 0) {
155                 if (errno == EWOULDBLOCK) {
156                         MSAPI_DBG_ERR("Timeout. Can't try any more");
157                         MS_SAFE_FREE(buf);
158                         return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
159                 } else {
160                         MSAPI_DBG_ERR("recv failed");
161                         MS_SAFE_FREE(buf);
162                         return MS_MEDIA_ERR_SOCKET_RECEIVE;
163                 }
164         }
165
166         memcpy(recv_msg, buf, header_size);
167         MS_SAFE_FREE(buf);
168
169         if (strlen(recv_msg->org_path) == 0 || strlen(recv_msg->org_path) >= MAX_FILEPATH_LEN) {
170                 MSAPI_DBG_ERR("org_path size is invlid[%d]", strlen(recv_msg->org_path));
171                 return MS_MEDIA_ERR_INVALID_IPC_MESSAGE;
172         }
173
174         if (strlen(recv_msg->dst_path) >= MAX_FILEPATH_LEN) {
175                 MSAPI_DBG_ERR("dst_path size is invlid[%d]", strlen(recv_msg->dst_path));
176                 return MS_MEDIA_ERR_INVALID_IPC_MESSAGE;
177         }
178
179         ret = cynara_creds_socket_get_pid(sockfd, &(credentials->pid));
180         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get pid");
181
182         ret = cynara_creds_socket_get_user(sockfd, USER_METHOD_UID, &(credentials->uid));
183         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get uid");
184
185         ret = cynara_creds_socket_get_client(sockfd, CLIENT_METHOD_SMACK, &(credentials->smack));
186         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get smack");
187
188 //      MSAPI_DBG_ERR("cynara_creds_info : P[%d]U[%s]S[%s]", credentials->pid, credentials->uid, credentials->smack);
189
190         return MS_MEDIA_ERR_NONE;
191 }
192
193 int ms_cynara_check(const ms_peer_credentials *creds, const char *privilege)
194 {
195         int result;
196         char *session;
197
198         if (!creds || !privilege)
199                 return MS_MEDIA_ERR_INVALID_PARAMETER;
200
201         session = cynara_session_from_pid(creds->pid);
202         MSAPI_RETVM_IF(session == NULL, MS_MEDIA_ERR_INTERNAL, "cynara_session_from_pid failed");
203
204         G_LOCK(cynara_mutex);
205         result = cynara_check(_cynara, creds->smack, session, creds->uid, privilege);
206         G_UNLOCK(cynara_mutex);
207
208         if (result != CYNARA_API_ACCESS_ALLOWED)
209                 ms_cynara_dbg_err("cynara_check", result);
210
211         MS_SAFE_FREE(session);
212         return result == CYNARA_API_ACCESS_ALLOWED ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_PERMISSION_DENIED;
213 }
214
215 int ms_cynara_enable_credentials_passing(int fd)
216 {
217         const int optval = 1;
218         int err;
219
220         err = setsockopt(fd, SOL_SOCKET, SO_PASSSEC, &optval, sizeof(optval));
221         MSAPI_RETVM_IF(err != 0, MS_MEDIA_ERR_SOCKET_INTERNAL, "Failed to set SO_PASSSEC socket option");
222
223         err = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
224         MSAPI_RETVM_IF(err != 0, MS_MEDIA_ERR_SOCKET_INTERNAL, "Failed to set SO_PASSCRED socket option");
225
226         return MS_MEDIA_ERR_NONE;
227 }
228