summaryrefslogtreecommitdiff
path: root/src/badge_service.c
blob: 9a91fc821f31ad4ab2a2e2e8bfbd514310c515f9 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * 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.
 */

#include <stdio.h>

#include <Eina.h>

#include <dlog.h>
#include <livebox-errno.h>
#include <packet.h>

#include <sys/smack.h>

#include <badge.h>
#include <badge_db.h>
#include <security-server.h>

#include "service_common.h"
#include "debug.h"
#include "util.h"
#include "conf.h"

static struct info {
	Eina_List *context_list;
	struct service_context *svc_ctx;
} s_info = {
	.context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
	.svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
};

#define ENABLE_BS_ACCESS_CONTROL 0

struct context {
	struct tcb *tcb;
	double seq;
};

struct badge_service {
	const char *cmd;
	void (*handler)(struct tcb *tcb, struct packet *packet, void *data);
	const char *rule;
	const char *access;
};

/*!
 * FUNCTIONS to handle badge
 */
static inline char *get_string(char *string)
{
	if (string == NULL) {
		return NULL;
	}
	if (string[0] == '\0') {
		return NULL;
	}

	return string;
}

/*!
 * SERVICE HANDLER
 */
static void _handler_insert_badge(struct tcb *tcb, struct packet *packet, void *data)
{
	int ret = 0, ret_p = 0;
	struct packet *packet_reply = NULL;
	struct packet *packet_service = NULL;
	char *pkgname = NULL;
	char *writable_pkg = NULL;
	char *caller = NULL;

	if (packet_get(packet, "sss", &pkgname, &writable_pkg, &caller) == 3) {
		pkgname = get_string(pkgname);
		writable_pkg = get_string(writable_pkg);
		caller = get_string(caller);

		if (pkgname != NULL && writable_pkg != NULL && caller != NULL) {
			ret = badge_db_insert(pkgname, writable_pkg, caller);

		} else {
			ret = BADGE_ERROR_INVALID_DATA;
		}

		packet_reply = packet_create_reply(packet, "i", ret);
		if (packet_reply) {
			if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
				ErrPrint("Failed to send a reply packet:%d", ret_p);
			}
			packet_destroy(packet_reply);
		} else {
			ErrPrint("Failed to create a reply packet");
		}

		if (ret == BADGE_ERROR_NONE) {
			packet_service = packet_create("insert_badge", "is", ret, pkgname);
			if (packet_service != NULL) {
				if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
					ErrPrint("Failed to send a muticast packet:%d", ret_p);
				}
				packet_destroy(packet_service);
			} else {
				ErrPrint("Failed to create a multicast packet");
			}
		} else {
			ErrPrint("Failed to insert a badge:%d", ret);
		}
	} else {
		ErrPrint("Failed to get data from the packet");
	}
}

static void _handler_delete_badge(struct tcb *tcb, struct packet *packet, void *data)
{
	int ret = 0, ret_p = 0;
	struct packet *packet_reply = NULL;
	struct packet *packet_service = NULL;
	char *pkgname = NULL;
	char *caller = NULL;

	if (packet_get(packet, "ss", &pkgname, &caller) == 2) {
		pkgname = get_string(pkgname);
		caller = get_string(caller);

		if (pkgname != NULL && caller != NULL) {
			ret = badge_db_delete(pkgname, caller);

		} else {
			ret = BADGE_ERROR_INVALID_DATA;
		}

		packet_reply = packet_create_reply(packet, "i", ret);
		if (packet_reply) {
			if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
				ErrPrint("Failed to send a reply packet:%d", ret_p);
			}
			packet_destroy(packet_reply);
		} else {
			ErrPrint("Failed to create a reply packet");
		}

		if (ret == BADGE_ERROR_NONE) {
			packet_service = packet_create("delete_badge", "is", ret, pkgname);
			if (packet_service != NULL) {
				if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
					ErrPrint("Failed to send a muticast packet:%d", ret_p);
				}
				packet_destroy(packet_service);
			} else {
				ErrPrint("Failed to create a multicast packet");
			}
		} else {
			ErrPrint("Failed to delete a badge:%d", ret);
		}
	} else {
		ErrPrint("Failed to get data from the packet");
	}
}

static void _handler_set_badge_count(struct tcb *tcb, struct packet *packet, void *data)
{
	int ret = 0, ret_p = 0;
	struct packet *packet_reply = NULL;
	struct packet *packet_service = NULL;
	char *pkgname = NULL;
	char *caller = NULL;
	int count = 0;

	if (packet_get(packet, "ssi", &pkgname, &caller, &count) == 3) {
		pkgname = get_string(pkgname);
		caller = get_string(caller);

		if (pkgname != NULL && caller != NULL) {
			ret = badge_db_set_count(pkgname, caller, count);

		} else {
			ret = BADGE_ERROR_INVALID_DATA;
		}

		packet_reply = packet_create_reply(packet, "i", ret);
		if (packet_reply) {
			if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
				ErrPrint("Failed to send a reply packet:%d", ret_p);
			}
			packet_destroy(packet_reply);
		} else {
			ErrPrint("Failed to create a reply packet");
		}

		if (ret == BADGE_ERROR_NONE) {
			packet_service = packet_create("set_badge_count", "isi", ret, pkgname, count);
			if (packet_service != NULL) {
				if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
					ErrPrint("Failed to send a muticast packet:%d", ret_p);
				}
				packet_destroy(packet_service);
			} else {
				ErrPrint("Failed to create a multicast packet");
			}
		} else {
			ErrPrint("Failed to set count of badge:%d", ret);
		}
	} else {
		ErrPrint("Failed to get data from the packet");
	}
}

static void _handler_set_display_option(struct tcb *tcb, struct packet *packet, void *data)
{
	int ret = 0, ret_p = 0;
	struct packet *packet_reply = NULL;
	struct packet *packet_service = NULL;
	char *pkgname = NULL;
	char *caller = NULL;
	int is_display = 0;

	if (packet_get(packet, "ssi", &pkgname, &caller, &is_display) == 3) {
		pkgname = get_string(pkgname);
		caller = get_string(caller);

		if (pkgname != NULL && caller != NULL) {
			ret = badge_db_set_display_option(pkgname, caller, is_display);

		} else {
			ret = BADGE_ERROR_INVALID_DATA;
		}

		packet_reply = packet_create_reply(packet, "i", ret);
		if (packet_reply) {
			if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
				ErrPrint("Failed to send a reply packet:%d", ret_p);
			}
			packet_destroy(packet_reply);
		} else {
			ErrPrint("Failed to create a reply packet");
		}

		if (ret == BADGE_ERROR_NONE) {
			packet_service = packet_create("set_disp_option", "isi", ret, pkgname, is_display);
			if (packet_service != NULL) {
				if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
					ErrPrint("Failed to send a muticast packet:%d", ret_p);
				}
				packet_destroy(packet_service);
			} else {
				ErrPrint("Failed to create a multicast packet");
			}
		} else {
			ErrPrint("Failed to set display option of badge:%d", ret);
		}
	} else {
		ErrPrint("Failed to get data from the packet");
	}
}

static void _handler_service_register(struct tcb *tcb, struct packet *packet, void *data)
{
	int ret = 0;
	struct packet *packet_reply;

	ret = tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
	if (ret < 0) {
		ErrPrint("Failed to set the type of client:%d", ret);
	}

	packet_reply = packet_create_reply(packet, "i", ret);
	if (packet_reply) {
		if ((ret = service_common_unicast_packet(tcb, packet_reply)) < 0) {
			ErrPrint("Failed to send a reply packet:%d", ret);
		}
		packet_destroy(packet_reply);
	} else {
		ErrPrint("Failed to create a reply packet");
	}
}

static int _is_valid_permission(int fd, struct badge_service *service)
{
	int ret;

	if (service->rule != NULL && service->access != NULL) {
		ret = security_server_check_privilege_by_sockfd(fd, service->rule, service->access);
		if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
			ErrPrint("SMACK:Access denied\n");
			return 0;
		}
	}

	return 1;
}

/*!
 * SERVICE THREAD
 */
static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
{
	int i = 0;
	const char *command;
	static struct badge_service service_req_table[] = {
		{
			.cmd = "insert_badge",
			.handler = _handler_insert_badge,
			.rule = "data-provider-master::badge.client",
			.access = "w",
		},
		{
			.cmd = "delete_badge",
			.handler = _handler_delete_badge,
			.rule = "data-provider-master::badge.client",
			.access = "w",
		},
		{
			.cmd = "set_badge_count",
			.handler = _handler_set_badge_count,
			.rule = "data-provider-master::badge.client",
			.access = "w",
		},
		{
			.cmd = "set_disp_option",
			.handler = _handler_set_display_option,
			.rule = "data-provider-master::badge.client",
			.access = "w",
		},
		{
			.cmd = "service_register",
			.handler = _handler_service_register,
			.rule = NULL,
			.access = NULL,
		},
		{
			.cmd = NULL,
			.handler = NULL,
			.rule = NULL,
			.access = NULL,
		},
	};

	if (!packet) {
		DbgPrint("TCB: %p is terminated (NIL packet)\n", tcb);
		return 0;
	}

	command = packet_command(packet);
	if (!command) {
		ErrPrint("Invalid command\n");
		return -EINVAL;
	}
	DbgPrint("Command: [%s], Packet type[%d]\n", command, packet_type(packet));

	switch (packet_type(packet)) {
	case PACKET_REQ:
		/* Need to send reply packet */
		for (i = 0; service_req_table[i].cmd; i++) {
			if (strcmp(service_req_table[i].cmd, command))
				continue;

#if ENABLE_BS_ACCESS_CONTROL
			if (_is_valid_permission(tcb_fd(tcb), &(service_req_table[i])) == 1) {
				service_req_table[i].handler(tcb, packet, data);
			}
#else
			_is_valid_permission(tcb_fd(tcb), &(service_req_table[i]));
			service_req_table[i].handler(tcb, packet, data);
#endif
			break;
		}

		break;
	case PACKET_REQ_NOACK:
		break;
	case PACKET_ACK:
		break;
	default:
		ErrPrint("Packet type is not valid[%s]\n", command);
		return -EINVAL;
	}

	/*!
	 * return value has no meanning,
	 * it will be printed by dlogutil.
	 */
	return 0;
}


/*!
 * MAIN THREAD
 * Do not try to do anyother operation in these functions
 */
HAPI int badge_service_init(void)
{
	if (s_info.svc_ctx) {
		ErrPrint("Already initialized\n");
		return LB_STATUS_ERROR_ALREADY;
	}

	s_info.svc_ctx = service_common_create(BADGE_SOCKET, service_thread_main, NULL);
	if (!s_info.svc_ctx) {
		ErrPrint("Unable to activate service thread\n");
		return LB_STATUS_ERROR_FAULT;
	}

	if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), BADGE_SMACK_LABEL, SMACK_LABEL_IPOUT) != 0) {
		if (errno != EOPNOTSUPP) {
			ErrPrint("Unable to set SMACK label(%d)\n", errno);
			service_common_destroy(s_info.svc_ctx);
			s_info.svc_ctx = NULL;
			return LB_STATUS_ERROR_FAULT;
		}
	}

	if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), BADGE_SMACK_LABEL, SMACK_LABEL_IPIN) != 0) {
		if (errno != EOPNOTSUPP) {
			ErrPrint("Unable to set SMACK label(%d)\n", errno);
			service_common_destroy(s_info.svc_ctx);
			s_info.svc_ctx = NULL;
			return LB_STATUS_ERROR_FAULT;
		}
	}

	DbgPrint("Successfully initiated\n");
	return LB_STATUS_SUCCESS;
}

HAPI int badge_service_fini(void)
{
	if (!s_info.svc_ctx)
		return LB_STATUS_ERROR_INVALID;

	service_common_destroy(s_info.svc_ctx);
	s_info.svc_ctx = NULL;
	DbgPrint("Successfully finalized\n");
	return LB_STATUS_SUCCESS;
}

/* End of a file */