4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/types.h>
34 #include <app-checker.h>
38 #define PLUGINS_PREFIX "/usr/lib/ac-plugins"
39 #define MAX_LOCAL_BUFSZ 512
42 #define SLPAPI __attribute__ ((visibility("default")))
45 GSList *pkg_type_list = NULL;
47 typedef struct _ac_type_list_t {
52 typedef struct _ac_so_list_t {
54 int (*ac_check)(const char*);
55 int (*ac_register)(const char*);
56 int (*ac_unregister)(const char*);
59 static int __send_to_sigkill(int pid)
67 if (killpg(pgid, SIGKILL) < 0)
73 static int __check_launch_privilege(const char *pkg_name, const char *pkg_type, int pid)
77 ac_type_list_t *type_t;
80 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
82 if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
83 for (iter2 = type_t->so_list; iter2 != NULL; iter2 = g_slist_next(iter2)) {
85 SECURE_LOGD("type : %s / so name : %s / func : %x", type_t->pkg_type, so_t->so_name, so_t->ac_check);
86 if (so_t->ac_check && so_t->ac_check(pkg_name) < 0) {
88 __send_to_sigkill(pid);
96 return AC_R_ENOPULUGINS;
99 static int __register_launch_privilege(const char *pkg_name, const char *pkg_type)
102 GSList *iter2 = NULL;
103 ac_type_list_t *type_t;
107 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
109 if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
110 for (iter2 = type_t->so_list; iter2 != NULL; iter = g_slist_next(iter2)) {
112 if (so_t->ac_register && so_t->ac_register(pkg_name) < 0) {
120 return AC_R_ENOPULUGINS;
124 static int __unregister_launch_privilege(const char *pkg_name, const char *pkg_type)
127 GSList *iter2 = NULL;
128 ac_type_list_t *type_t;
132 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
134 if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
135 for (iter2 = type_t->so_list; iter2 != NULL; iter = g_slist_next(iter2)) {
137 if (so_t->ac_unregister && so_t->ac_unregister(pkg_name) < 0) {
145 return AC_R_ENOPULUGINS;;
149 static gboolean __ac_handler(gpointer data)
151 GPollFD *gpollfd = (GPollFD *) data;
152 int fd = gpollfd->fd;
160 if ((pkt = _app_recv_raw(fd, &clifd, &cr)) == NULL) {
165 ad = (struct ac_data *)g_base64_decode((const gchar*)pkt->data, (gsize *)&size);
173 SECURE_LOGD("cmd : %d, pkgname : %s, pkgtype : %s", pkt->cmd, ad->pkg_name, ad->pkg_type);
177 _send_result_to_server(clifd, AC_R_OK);
178 ret = __check_launch_privilege(ad->pkg_name, ad->pkg_type, ad->pid);
184 ret = __register_launch_privilege(ad->pkg_name, ad->pkg_type);
187 ret = __unregister_launch_privilege(ad->pkg_name, ad->pkg_type);
190 _E("no support packet");
193 _send_result_to_server(clifd, ret);
200 static gboolean __ac_glib_check(GSource *src)
205 fd_list = src->poll_fds;
207 tmp = (GPollFD *) fd_list->data;
208 if ((tmp->revents & (POLLIN | POLLPRI)))
210 fd_list = fd_list->next;
216 static gboolean __ac_glib_dispatch(GSource *src, GSourceFunc callback,
223 static gboolean __ac_glib_prepare(GSource *src, gint *timeout)
228 static GSourceFuncs funcs = {
229 .prepare = __ac_glib_prepare,
230 .check = __ac_glib_check,
231 .dispatch = __ac_glib_dispatch,
235 static void __pkt_type_list_free()
238 GSList *iter2 = NULL;
239 ac_type_list_t *type_t;
242 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
245 for (iter2 = type_t->so_list; iter2 != NULL; iter2 = g_slist_next(iter2)) {
253 g_slist_free(type_t->so_list);
257 free(type_t->pkg_type);
261 g_slist_free(pkg_type_list);
272 _D("app checker server initialize");
274 fd = _create_server_sock();
276 src = g_source_new(&funcs, sizeof(GSource));
278 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
279 if (gpollfd == NULL) {
286 gpollfd->events = POLLIN;
289 g_source_add_poll(src, gpollfd);
290 g_source_set_callback(src, (GSourceFunc) __ac_handler,
291 (gpointer) gpollfd, NULL);
292 g_source_set_priority(src, G_PRIORITY_DEFAULT);
294 ret = g_source_attach(src, NULL);
297 /* TODO: error handle*/
304 struct dirent *dentry;
306 struct dirent *sub_dentry;
307 char buf[MAX_LOCAL_BUFSZ];
308 char buf2[MAX_LOCAL_BUFSZ];
309 ac_type_list_t *type_t = NULL;
311 ac_so_list_t *so_t = NULL;
313 dp = opendir(PLUGINS_PREFIX);
317 while ((dentry = readdir(dp)) != NULL) {
319 if(dentry->d_type != DT_DIR)
321 if(strcmp(dentry->d_name,".") == 0 || strcmp(dentry->d_name,"..") == 0)
324 snprintf(buf,MAX_LOCAL_BUFSZ,"%s/%s",PLUGINS_PREFIX,dentry->d_name);
325 SECURE_LOGD("type : %s", dentry->d_name);
327 type_t = malloc(sizeof(ac_type_list_t));
329 __pkt_type_list_free();
333 memset(type_t, 0, sizeof(ac_type_list_t));
334 type_t->pkg_type = strdup(dentry->d_name);
335 type_t->so_list = NULL;
337 pkg_type_list = g_slist_append(pkg_type_list, (void *)type_t);
339 sub_dp = opendir(buf);
340 if (sub_dp == NULL) {
341 __pkt_type_list_free();
346 while ((sub_dentry = readdir(sub_dp)) != NULL) {
348 if(sub_dentry->d_type == DT_DIR)
350 snprintf(buf2,MAX_LOCAL_BUFSZ,"%s/%s", buf, sub_dentry->d_name);
351 SECURE_LOGD("so_name : %s", buf2);
353 handle = dlopen(buf2, RTLD_LAZY);
356 so_t = malloc(sizeof(ac_so_list_t));
358 __pkt_type_list_free();
365 memset(so_t, 0, sizeof(ac_so_list_t));
366 so_t->so_name = strdup(sub_dentry->d_name);
367 so_t->ac_check = dlsym(handle, "check_launch_privilege");
368 so_t->ac_register = dlsym(handle, "check_register_privilege");
369 so_t->ac_unregister = dlsym(handle, "check_unregister_privilege");
371 type_t->so_list = g_slist_append(type_t->so_list, (void *)so_t);
381 SLPAPI int ac_server_initialize()
385 ret = __initialize();
390 SLPAPI int ac_server_check_launch_privilege(const char *pkg_name, const char *pkg_type, int pid)
393 ret = __check_launch_privilege(pkg_name, pkg_type, pid);