Refactor codes
[platform/core/appfw/message-port.git] / src / message_port_common.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18
19 #include <bundle.h>
20 #include <bundle_internal.h>
21 #include <pkgmgr-info.h>
22 #include <aul.h>
23
24 #include <sys/socket.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <openssl/md5.h>
30 #include <gio/gio.h>
31 #include <pthread.h>
32 #include <glib-unix.h>
33 #include <poll.h>
34
35 #include "message_port_log.h"
36 #include "message_port.h"
37 #include "message_port_common.h"
38
39 #define MAX_PACKAGE_STR_SIZE 512
40 #define MAX_RETRY_CNT 10
41
42 static const int MAX_MESSAGE_SIZE = 16 * 1024;
43
44 int write_socket(int fd,
45                 const char *buffer,
46                 unsigned int nbytes,
47                 unsigned int *bytes_write,
48                 int *sequence)
49 {
50 #define SEND_TIMEOUT 500 /* milliseconds */
51
52         unsigned int left = nbytes;
53         ssize_t nb;
54         int retry_cnt = 0;
55         struct pollfd fds[1];
56         int ret;
57
58         *sequence += 1;
59         *bytes_write = 0;
60
61         fds[0].fd = fd;
62         fds[0].events = POLLOUT;
63         fds[0].revents = 0;
64
65         ret = poll(fds, 1, SEND_TIMEOUT);
66         if (ret == 0) {
67                 LOGE("write_socket: : fd %d poll timeout", fd);
68                 return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
69         }
70
71         while (left && (retry_cnt < MAX_RETRY_CNT)) {
72                 nb = write(fd, buffer, left);
73                 if (nb == -1) {
74                         if (errno == EINTR) {
75                                 LOGE("write_socket: EINTR error continue ...");
76                                 retry_cnt++;
77                                 continue;
78                         }
79                         LOGE("write_socket: ...error fd %d: errno %d\n", fd, errno);
80
81                         if (errno == EWOULDBLOCK || errno == EAGAIN)
82                                 return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
83
84                         return MESSAGE_PORT_ERROR_IO_ERROR;
85                 }
86
87                 left -= nb;
88                 buffer += nb;
89                 *bytes_write += nb;
90                 retry_cnt = 0;
91         }
92         return MESSAGE_PORT_ERROR_NONE;
93 }
94
95 int write_string_to_socket(int fd,
96                 const char *buffer,
97                 int string_len,
98                 unsigned int *bytes_write,
99                 int *sequence)
100 {
101         int ret;
102
103         ret = write_socket(fd, (char *)&string_len, sizeof(string_len),
104                         bytes_write, sequence);
105         if (ret != MESSAGE_PORT_ERROR_NONE) {
106                 _LOGE("write string_len fail");
107                 return ret;
108         }
109
110         if (string_len > 0) {
111                 ret = write_socket(fd, buffer, string_len, bytes_write, sequence);
112                 if (ret != MESSAGE_PORT_ERROR_NONE) {
113                         _LOGE("wirte buffer fail");
114                         return ret;
115                 }
116         } else {
117                 *sequence += 1;
118         }
119
120         return MESSAGE_PORT_ERROR_NONE;
121 }
122
123 int read_socket(int fd,
124                 char *buffer,
125                 unsigned int nbytes,
126                 unsigned int *bytes_read)
127 {
128         unsigned int left = nbytes;
129         ssize_t nb;
130         int retry_cnt = 0;
131         const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
132
133         *bytes_read = 0;
134         while (left && (retry_cnt < MAX_RETRY_CNT)) {
135                 nb = read(fd, buffer, left);
136                 if (nb == 0) {
137                         LOGE("read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
138                         return MESSAGE_PORT_ERROR_IO_ERROR;
139                 } else if (nb == -1) {
140                         /*  wrt(nodejs) could change socket to none-blocking socket :-( */
141                         if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
142                                 LOGE("read_socket: %d errno, sleep and retry ...", errno);
143                                 retry_cnt++;
144                                 nanosleep(&TRY_SLEEP_TIME, 0);
145                                 continue;
146                         }
147                         LOGE("read_socket: ...error fd %d: errno %d\n", fd, errno);
148                         return MESSAGE_PORT_ERROR_IO_ERROR;
149                 }
150
151                 left -= nb;
152                 buffer += nb;
153                 *bytes_read += nb;
154                 retry_cnt = 0;
155         }
156         return MESSAGE_PORT_ERROR_NONE;
157 }
158
159 int read_string_from_socket(int fd, char **buffer, int *string_len)
160 {
161         unsigned int nb;
162         if (read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGE_PORT_ERROR_NONE) {
163                 LOGE("read socket fail");
164                 return MESSAGE_PORT_ERROR_IO_ERROR;
165         }
166         if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
167                 *buffer = (char *)calloc(*string_len, sizeof(char));
168                 if (*buffer == NULL) {
169                         LOGE("Out of memory.");
170                         return MESSAGE_PORT_ERROR_IO_ERROR;
171                 }
172                 if (read_socket(fd, *buffer, *string_len, &nb) != MESSAGE_PORT_ERROR_NONE) {
173                         LOGE("read socket fail");
174                         return MESSAGE_PORT_ERROR_IO_ERROR;
175                 }
176         } else {
177                 LOGE("Invalid string len %d", *string_len);
178                 return MESSAGE_PORT_ERROR_IO_ERROR;
179         }
180         return MESSAGE_PORT_ERROR_NONE;
181 }
182
183 static int __dbus_init(void)
184 {
185         bool ret = false;
186         GError *error = NULL;
187
188         gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
189         if (gdbus_conn == NULL) {
190                 if (error != NULL) {
191                         _LOGE("Failed to get dbus [%s]", error->message);
192                         g_error_free(error);
193                 }
194                 goto out;
195         }
196
197         ret = true;
198
199 out:
200         if (!gdbus_conn)
201                 g_object_unref(gdbus_conn);
202
203         return ret;
204
205 }
206
207 bool initialize_common(void)
208 {
209
210 #if !GLIB_CHECK_VERSION(2, 35, 0)
211         g_type_init();
212 #endif
213
214         int pid = getpid();
215         int ret = 0;
216         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
217
218         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
219         retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
220
221         app_id = strdup(buffer);
222         retvm_if(!app_id, false, "Malloc failed");
223         _LOGI("init : %s", app_id);
224
225         if (!__dbus_init())
226                 return false;
227         initialized_common = true;
228
229         return true;
230 }
231
232 bool is_preloaded(const char *local_appid, const char *remote_appid)
233 {
234         _LOGD("IsPreloaded");
235
236         bool preload_local = false;
237         bool preload_remote = false;
238
239         pkgmgrinfo_appinfo_h handle = NULL;
240         int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
241         if (ret != PMINFO_R_OK) {
242                 _LOGE("Failed to get the appinfo. %d", ret);
243                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
244                 return false;
245         }
246         ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
247         if (ret != PMINFO_R_OK) {
248                 _LOGE("Failed to check the preloaded application. %d", ret);
249                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
250                 return false;
251         }
252         pkgmgrinfo_appinfo_destroy_appinfo(handle);
253
254         ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
255         if (ret != PMINFO_R_OK) {
256                 _LOGE("Failed to get the appinfo. %d", ret);
257                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
258                 return false;
259         }
260         ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
261         if (ret != PMINFO_R_OK) {
262                 _LOGE("Failed to check the preloaded application. %d", ret);
263                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
264                 return false;
265         }
266
267         if (preload_local && preload_remote) {
268                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
269                 return true;
270         }
271         pkgmgrinfo_appinfo_destroy_appinfo(handle);
272         return false;
273 }
274
275 int check_certificate(const char *local_appid, const char *remote_appid)
276 {
277         _LOGD("CheckCertificate");
278
279         pkgmgrinfo_cert_compare_result_type_e res;
280         int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
281         if (ret < 0) {
282                 _LOGE(":CheckCertificate() Failed");
283                 return MESSAGE_PORT_ERROR_IO_ERROR;
284         }
285         if (res != PMINFO_CERT_COMPARE_MATCH) {
286                 _LOGE("CheckCertificate() Failed : MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH");
287                 return MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
288         }
289
290         return MESSAGE_PORT_ERROR_NONE;
291 }
292
293 char *get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
294 {
295
296         int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
297         int postfix_len = 1;
298         char *postfix = is_trusted ? "1" : "0";
299
300         unsigned char c[MD5_DIGEST_LENGTH] = {0};
301         char *md5_interface = NULL;
302         char *temp;
303         int index = 0;
304         MD5_CTX mdContext;
305         int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
306         int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
307         char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
308         if (bus_name == NULL) {
309                 _LOGE("bus_name calloc failed");
310                 return 0;
311         }
312
313         snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
314
315         MD5_Init(&mdContext);
316         MD5_Update(&mdContext, bus_name, bus_name_len);
317         MD5_Final(c, &mdContext);
318
319         md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
320         if (md5_interface == NULL) {
321                 if (bus_name)
322                         free(bus_name);
323
324                 _LOGE("md5_interface calloc failed!!");
325                 return 0;
326         }
327
328         snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
329         temp = md5_interface;
330         temp += prefix_len;
331
332         for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
333                 snprintf(temp, 3, "%02x", c[index]);
334                 temp += 2;
335         }
336
337         if (postfix && postfix_len > 0)
338                 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
339         if (bus_name)
340                 free(bus_name);
341
342         _LOGD("encoded_bus_name : %s ", md5_interface);
343
344         return md5_interface;
345 }