[SATIZENVUL-1315/SATIZENVUL-1316] Remove msg size from thumbMsg
[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) - 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         ret = cynara_creds_socket_get_pid(sockfd, &(credentials->pid));
165         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get pid");
166
167         ret = cynara_creds_socket_get_user(sockfd, USER_METHOD_UID, &(credentials->uid));
168         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get uid");
169
170         ret = cynara_creds_socket_get_client(sockfd, CLIENT_METHOD_SMACK, &(credentials->smack));
171         MSAPI_RETVM_IF(ret != 0, MS_MEDIA_ERR_INTERNAL, "[CYNARA]Failed to get smack");
172
173 //      MSAPI_DBG_ERR("cynara_creds_info : P[%d]U[%s]S[%s]", credentials->pid, credentials->uid, credentials->smack);
174
175         return MS_MEDIA_ERR_NONE;
176 }
177
178 int ms_cynara_check(const ms_peer_credentials *creds, const char *privilege)
179 {
180         int result;
181         char *session;
182
183         if (!creds || !privilege)
184                 return MS_MEDIA_ERR_INVALID_PARAMETER;
185
186         session = cynara_session_from_pid(creds->pid);
187         MSAPI_RETVM_IF(session == NULL, MS_MEDIA_ERR_INTERNAL, "cynara_session_from_pid failed");
188
189         G_LOCK(cynara_mutex);
190         result = cynara_check(_cynara, creds->smack, session, creds->uid, privilege);
191         G_UNLOCK(cynara_mutex);
192
193         if (result != CYNARA_API_ACCESS_ALLOWED)
194                 ms_cynara_dbg_err("cynara_check", result);
195
196         MS_SAFE_FREE(session);
197         return result == CYNARA_API_ACCESS_ALLOWED ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_PERMISSION_DENIED;
198 }
199
200 int ms_cynara_enable_credentials_passing(int fd)
201 {
202         const int optval = 1;
203         int err;
204
205         err = setsockopt(fd, SOL_SOCKET, SO_PASSSEC, &optval, sizeof(optval));
206         MSAPI_RETVM_IF(err != 0, MS_MEDIA_ERR_SOCKET_INTERNAL, "Failed to set SO_PASSSEC socket option");
207
208         err = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
209         MSAPI_RETVM_IF(err != 0, MS_MEDIA_ERR_SOCKET_INTERNAL, "Failed to set SO_PASSCRED socket option");
210
211         return MS_MEDIA_ERR_NONE;
212 }
213