[SATIZENVUL-1315/SATIZENVUL-1316] Remove msg_size from ms_comm_msg_s
[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 [%d] %d, %s", recv_msg->pid, recv_msg->msg_type, recv_msg->msg);
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         ret = cynara_creds_socket_get_pid(sockfd, &(credentials->pid));
123         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get pid");
124
125         ret = cynara_creds_socket_get_user(sockfd, USER_METHOD_UID, &(credentials->uid));
126         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get uid");
127
128         ret = cynara_creds_socket_get_client(sockfd, CLIENT_METHOD_SMACK, &(credentials->smack));
129         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get smack");
130
131 //      MSAPI_DBG_ERR("cynara_creds_info : P[%d]U[%s]S[%s]", credentials->pid, credentials->uid, credentials->smack);
132
133         return MS_MEDIA_ERR_NONE;
134 }
135
136 int ms_cynara_receive_untrusted_message_thumb(int sockfd, thumbMsg *recv_msg, ms_peer_credentials *credentials)
137 {
138         int ret = 0;
139         int header_size = 0;
140         int recv_msg_size = 0;
141         unsigned char *buf = NULL;
142
143         if (!recv_msg || !credentials)
144                 return MS_MEDIA_ERR_INVALID_PARAMETER;
145
146         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
147         MS_MALLOC(buf, header_size);
148
149         if ((recv_msg_size = recv(sockfd, buf, header_size, 0)) < 0) {
150                 if (errno == EWOULDBLOCK) {
151                         MSAPI_DBG_ERR("Timeout. Can't try any more");
152                         MS_SAFE_FREE(buf);
153                         return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
154                 } else {
155                         MSAPI_DBG_ERR("recv failed");
156                         MS_SAFE_FREE(buf);
157                         return MS_MEDIA_ERR_SOCKET_RECEIVE;
158                 }
159         }
160
161         memcpy(recv_msg, buf, header_size);
162         MS_SAFE_FREE(buf);
163
164         if (recv_msg->origin_path_size <= 0) {
165                 MSAPI_DBG_ERR("msg->origin_path_size is invalid %d", recv_msg->origin_path_size);
166                 return MS_MEDIA_ERR_INVALID_PARAMETER;
167         }
168
169         MS_MALLOC(buf, (unsigned int)(recv_msg->origin_path_size));
170         if (buf == NULL) {
171                 MSAPI_DBG_STRERROR("malloc failed");
172                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
173         }
174
175         if ((recv_msg_size = recv(sockfd, buf, recv_msg->origin_path_size, 0)) < 0) {
176                 MS_SAFE_FREE(buf);
177                 if (errno == EWOULDBLOCK) {
178                         MSAPI_DBG_ERR("Timeout. Can't try any more");
179                         return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
180                 } else {
181                         MSAPI_DBG_ERR("recv failed");
182                         return MS_MEDIA_ERR_SOCKET_RECEIVE;
183                 }
184
185         }
186
187         SAFE_STRLCPY(recv_msg->org_path, (char*)buf, sizeof(recv_msg->org_path));
188         MS_SAFE_FREE(buf);
189
190         if (recv_msg->dest_path_size <= 0) {
191                 MSAPI_DBG_ERR("msg->origin_path_size is invalid %d", recv_msg->dest_path_size);
192                 return MS_MEDIA_ERR_INVALID_PARAMETER;
193         }
194
195         MS_MALLOC(buf, (unsigned int)(recv_msg->dest_path_size));
196         if (buf == NULL) {
197                 MSAPI_DBG_STRERROR("malloc failed");
198                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
199         }
200
201         if ((recv_msg_size = recv(sockfd, buf, recv_msg->dest_path_size, 0)) < 0) {
202                 MS_SAFE_FREE(buf);
203                 if (errno == EWOULDBLOCK) {
204                         MSAPI_DBG_ERR("Timeout. Can't try any more");
205                         return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
206                 } else {
207                         MSAPI_DBG_ERR("recv failed");
208                         return MS_MEDIA_ERR_SOCKET_RECEIVE;
209                 }
210
211         }
212
213         SAFE_STRLCPY(recv_msg->dst_path, (char*)buf, sizeof(recv_msg->dst_path));
214         MS_SAFE_FREE(buf);
215
216         ret = cynara_creds_socket_get_pid(sockfd, &(credentials->pid));
217         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get pid");
218
219         ret = cynara_creds_socket_get_user(sockfd, USER_METHOD_UID, &(credentials->uid));
220         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get uid");
221
222         ret = cynara_creds_socket_get_client(sockfd, CLIENT_METHOD_SMACK, &(credentials->smack));
223         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get smack");
224
225 //      MSAPI_DBG_ERR("cynara_creds_info : P[%d]U[%s]S[%s]", credentials->pid, credentials->uid, credentials->smack);
226
227         return MS_MEDIA_ERR_NONE;
228 }
229
230 int ms_cynara_check(const ms_peer_credentials *creds, const char *privilege)
231 {
232         int result;
233         char *session;
234
235         if (!creds || !privilege)
236                 return MS_MEDIA_ERR_INVALID_PARAMETER;
237
238         session = cynara_session_from_pid(creds->pid);
239         MSAPI_RETVM_IF(session == NULL, MS_MEDIA_ERR_INTERNAL, "cynara_session_from_pid failed");
240
241         G_LOCK(cynara_mutex);
242         result = cynara_check(_cynara, creds->smack, session, creds->uid, privilege);
243         G_UNLOCK(cynara_mutex);
244
245         if (result != CYNARA_API_ACCESS_ALLOWED)
246                 ms_cynara_dbg_err("cynara_check", result);
247
248         MS_SAFE_FREE(session);
249         return result == CYNARA_API_ACCESS_ALLOWED ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_PERMISSION_DENIED;
250 }
251
252 int ms_cynara_enable_credentials_passing(int fd)
253 {
254         const int optval = 1;
255         int err;
256
257         err = setsockopt(fd, SOL_SOCKET, SO_PASSSEC, &optval, sizeof(optval));
258         MSAPI_RETVM_IF(err != 0, MS_MEDIA_ERR_SOCKET_INTERNAL, "Failed to set SO_PASSSEC socket option");
259
260         err = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
261         MSAPI_RETVM_IF(err != 0, MS_MEDIA_ERR_SOCKET_INTERNAL, "Failed to set SO_PASSCRED socket option");
262
263         return MS_MEDIA_ERR_NONE;
264 }
265