summaryrefslogtreecommitdiff
path: root/src/message_port_common.c
blob: aa601d13321c9ef046276719048640f00cf0f615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <bundle.h>
#include <bundle_internal.h>
#include <pkgmgr-info.h>
#include <aul.h>

#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <gio/gio.h>
#include <openssl/md5.h>
#include <gio/gio.h>
#include <pthread.h>
#include <glib-unix.h>
#include <poll.h>

#include "message_port_log.h"
#include "message_port.h"
#include "message_port_common.h"

#define MAX_PACKAGE_STR_SIZE 512
#define MAX_RETRY_CNT 10

bool initialized_common;
GDBusConnection *gdbus_conn;
char *app_id;

static const int MAX_MESSAGE_SIZE = 16 * 1024;
static pthread_mutex_t _message_port_mutex = PTHREAD_MUTEX_INITIALIZER;

int write_socket(int fd,
		const char *buffer,
		unsigned int nbytes,
		unsigned int *bytes_write,
		int *sequence)
{
	unsigned int left = nbytes;
	ssize_t nb;
	int retry_cnt = 0;

	*sequence += 1;
	*bytes_write = 0;

	while (left && (retry_cnt < MAX_RETRY_CNT)) {
		nb = write(fd, buffer, left);
		if (nb == -1) {
			if (errno == EINTR) {
				LOGW("write_socket: EINTR continue ...");
				retry_cnt++;
				continue;
			}
			LOGE("write_socket: ...error fd %d: errno %d\n", fd, errno);

			if (errno == EWOULDBLOCK || errno == EAGAIN)
				return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;

			return MESSAGE_PORT_ERROR_IO_ERROR;
		}

		left -= nb;
		buffer += nb;
		*bytes_write += nb;
		retry_cnt = 0;
	}

	if (left != 0) {
		_LOGE("error fd %d: retry_cnt %d", fd, retry_cnt);
		return MESSAGE_PORT_ERROR_IO_ERROR;
	}

	return MESSAGE_PORT_ERROR_NONE;
}

int write_string_to_socket(int fd,
		const char *buffer,
		int string_len,
		unsigned int *bytes_write,
		int *sequence)
{
	int ret;

	ret = write_socket(fd, (char *)&string_len, sizeof(string_len),
			bytes_write, sequence);
	if (ret != MESSAGE_PORT_ERROR_NONE) {
		_LOGE("write string_len fail");
		return ret;
	}

	if (string_len > 0) {
		ret = write_socket(fd, buffer, string_len, bytes_write, sequence);
		if (ret != MESSAGE_PORT_ERROR_NONE) {
			_LOGE("wirte buffer fail");
			return ret;
		}
	} else {
		*sequence += 1;
	}

	return MESSAGE_PORT_ERROR_NONE;
}

int read_socket(int fd,
		char *buffer,
		unsigned int nbytes,
		unsigned int *bytes_read)
{
	unsigned int left = nbytes;
	ssize_t nb;
	int retry_cnt = 0;
	struct timespec TRY_SLEEP_TIME = { 0, 5 * 1000 * 1000 };

	*bytes_read = 0;
	while (left && (retry_cnt < MAX_RETRY_CNT)) {
		nb = read(fd, buffer, left);
		if (nb == 0) {
			LOGE("read_socket: ...read EOF, socket closed %d: nb %zd\n", fd, nb);
			return MESSAGE_PORT_ERROR_IO_ERROR;
		} else if (nb == -1) {
			/*  wrt(nodejs) could change socket to none-blocking socket :-( */
			if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
				LOGW("read_socket: %d , sleep and retry ...", errno);
				retry_cnt++;
				nanosleep(&TRY_SLEEP_TIME, 0);
				TRY_SLEEP_TIME.tv_nsec *= 2;
				continue;
			}
			LOGE("read_socket: ...error fd %d: errno %d\n", fd, errno);
			return MESSAGE_PORT_ERROR_IO_ERROR;
		}

		left -= nb;
		buffer += nb;
		*bytes_read += nb;
		retry_cnt = 0;
	}

	if (left != 0) {
		_LOGE("error fd %d: retry_cnt %d", fd, retry_cnt);
		return MESSAGE_PORT_ERROR_IO_ERROR;
	}

	return MESSAGE_PORT_ERROR_NONE;
}

int read_string_from_socket(int fd, char **buffer, int *string_len)
{
	unsigned int nb;
	if (read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGE_PORT_ERROR_NONE) {
		LOGE("read socket fail");
		return MESSAGE_PORT_ERROR_IO_ERROR;
	}
	if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
		*buffer = (char *)calloc(*string_len, sizeof(char));
		if (*buffer == NULL) {
			LOGE("Out of memory.");
			return MESSAGE_PORT_ERROR_IO_ERROR;
		}
		if (read_socket(fd, *buffer, *string_len, &nb) != MESSAGE_PORT_ERROR_NONE) {
			LOGE("read socket fail");
			return MESSAGE_PORT_ERROR_IO_ERROR;
		}
	} else {
		LOGE("Invalid string len %d", *string_len);
		return MESSAGE_PORT_ERROR_IO_ERROR;
	}
	return MESSAGE_PORT_ERROR_NONE;
}

static int __dbus_init(void)
{
	bool ret = true;
	GError *error = NULL;

	gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
	if (gdbus_conn == NULL) {
		if (error != NULL) {
			_LOGE("Failed to get dbus [%s]", error->message);
			g_error_free(error);
		}
		ret = false;
	}

	return ret;
}

bool initialize_common(void)
{

#if !GLIB_CHECK_VERSION(2, 35, 0)
	g_type_init();
#endif

	int pid = getpid();
	int ret = 0;
	char buffer[MAX_PACKAGE_STR_SIZE] = {0, };

	ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
	retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);

	app_id = strdup(buffer);
	retvm_if(!app_id, false, "Malloc failed");
	_LOGI("init : %s", app_id);

	if (!__dbus_init())
		return false;
	initialized_common = true;

	return true;
}

bool is_preloaded(const char *local_appid, const char *remote_appid)
{
	_LOGD("IsPreloaded");

	bool preload_local = false;
	bool preload_remote = false;

	pkgmgrinfo_appinfo_h handle = NULL;
	int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
	if (ret != PMINFO_R_OK)	{
		_LOGE("Failed to get the appinfo. %d", ret);
		pkgmgrinfo_appinfo_destroy_appinfo(handle);
		return false;
	}
	ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
	if (ret != PMINFO_R_OK) {
		_LOGE("Failed to check the preloaded application. %d", ret);
		pkgmgrinfo_appinfo_destroy_appinfo(handle);
		return false;
	}
	pkgmgrinfo_appinfo_destroy_appinfo(handle);

	ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
	if (ret != PMINFO_R_OK) {
		_LOGE("Failed to get the appinfo. %d", ret);
		return false;
	}
	ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
	if (ret != PMINFO_R_OK) {
		_LOGE("Failed to check the preloaded application. %d", ret);
		pkgmgrinfo_appinfo_destroy_appinfo(handle);
		return false;
	}

	if (preload_local && preload_remote) {
		pkgmgrinfo_appinfo_destroy_appinfo(handle);
		return true;
	}
	pkgmgrinfo_appinfo_destroy_appinfo(handle);
	return false;
}

int check_certificate(const char *local_appid, const char *remote_appid)
{
	_LOGD("CheckCertificate");

	pkgmgrinfo_cert_compare_result_type_e res;
	int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
	if (ret < 0) {
		_LOGE(":CheckCertificate() Failed");
		return MESSAGE_PORT_ERROR_IO_ERROR;
	}
	if (res != PMINFO_CERT_COMPARE_MATCH) {
		_LOGE("CheckCertificate() Failed : MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH");
		return MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
	}

	return MESSAGE_PORT_ERROR_NONE;
}

char *get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
{

	int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
	int postfix_len = 1;
	char *postfix = is_trusted ? "1" : "0";

	unsigned char c[MD5_DIGEST_LENGTH] = {0};
	char *md5_interface = NULL;
	char *temp;
	int index = 0;
	MD5_CTX mdContext;
	int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
	int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
	char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
	if (bus_name == NULL) {
		_LOGE("bus_name calloc failed");
		return 0;
	}

	snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);

	MD5_Init(&mdContext);
	MD5_Update(&mdContext, bus_name, bus_name_len);
	MD5_Final(c, &mdContext);

	md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
	if (md5_interface == NULL) {
		if (bus_name)
			free(bus_name);

		_LOGE("md5_interface calloc failed!!");
		return 0;
	}

	snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
	temp = md5_interface;
	temp += prefix_len;

	for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
		snprintf(temp, 3, "%02x", c[index]);
		temp += 2;
	}

	if (postfix && postfix_len > 0)
		snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
	if (bus_name)
		free(bus_name);

	_LOGD("encoded_bus_name : %s ", md5_interface);

	return md5_interface;
}

void message_port_lock_mutex()
{
	pthread_mutex_lock(&_message_port_mutex);
}

void message_port_unlock_mutex()
{
	pthread_mutex_unlock(&_message_port_mutex);
}