tizen 2.3.1 release
[framework/appfw/app-checker.git] / src / ac_server.c
index 5373524..c88820f 100755 (executable)
@@ -36,7 +36,7 @@
 #include "internal.h"
 
 #define PLUGINS_PREFIX "/usr/lib/ac-plugins"
-#define MAX_LOCAL_BUFSZ        512
+#define MAX_LOCAL_BUFSZ        512
 
 #ifndef SLPAPI
 #define SLPAPI __attribute__ ((visibility("default")))
@@ -76,21 +76,21 @@ static int __check_launch_privilege(const char *pkg_name, const char *pkg_type,
        GSList *iter2 = NULL;
        ac_type_list_t *type_t;
        ac_so_list_t *so_t;
-       
+
        for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
                type_t = iter->data;
                if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
                        for (iter2 = type_t->so_list; iter2 != NULL; iter2 = g_slist_next(iter2)) {
                                so_t = iter2->data;
-                               _D("type : %s / so name : %s / func : %x", type_t->pkg_type, so_t->so_name, so_t->ac_check);
+                               SECURE_LOGD("type : %s / so name : %s / func : %x", type_t->pkg_type, so_t->so_name, so_t->ac_check);
                                if (so_t->ac_check && so_t->ac_check(pkg_name) < 0) {
                                        if(pid > 0)
-                                               __send_to_sigkill(pid);                                 
+                                               __send_to_sigkill(pid);
                                        return AC_R_ERROR;
                                }
                        }
                        return AC_R_OK;
-               }                                               
+               }
        }
 
        return AC_R_ENOPULUGINS;
@@ -103,7 +103,7 @@ static int __register_launch_privilege(const char *pkg_name, const char *pkg_typ
        ac_type_list_t *type_t;
        ac_so_list_t *so_t;
        int ret = AC_R_OK;
-       
+
        for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
                type_t = iter->data;
                if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
@@ -114,7 +114,7 @@ static int __register_launch_privilege(const char *pkg_name, const char *pkg_typ
                                }
                        }
                        return ret;
-               }                                               
+               }
        }
 
        return AC_R_ENOPULUGINS;
@@ -128,7 +128,7 @@ static int __unregister_launch_privilege(const char *pkg_name, const char *pkg_t
        ac_type_list_t *type_t;
        ac_so_list_t *so_t;
        int ret = AC_R_OK;
-       
+
        for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
                type_t = iter->data;
                if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
@@ -139,7 +139,7 @@ static int __unregister_launch_privilege(const char *pkg_name, const char *pkg_t
                                }
                        }
                        return ret;
-               }                                               
+               }
        }
 
        return AC_R_ENOPULUGINS;;
@@ -163,29 +163,35 @@ static gboolean __ac_handler(gpointer data)
        }
 
        ad = (struct ac_data *)g_base64_decode((const gchar*)pkt->data, (gsize *)&size);
+       if (ad == NULL) {
+               _E("out of memory");
+               free(pkt);
+               close(clifd);
+               return FALSE;
+       }
 
-       _D("cmd : %d, pkgname : %s, pkgtype : %s", pkt->cmd, ad->pkg_name, ad->pkg_type);
+       SECURE_LOGD("cmd : %d, pkgname : %s, pkgtype : %s", pkt->cmd, ad->pkg_name, ad->pkg_type);
 
        switch (pkt->cmd) {
        case AC_CHECK:
                _send_result_to_server(clifd, AC_R_OK);
                ret = __check_launch_privilege(ad->pkg_name, ad->pkg_type, ad->pid);
-               g_free(ad); 
+               g_free(ad);
                free(pkt);
                return TRUE;
                break;
        case AC_REGISTER:
                ret = __register_launch_privilege(ad->pkg_name, ad->pkg_type);
-               break;          
+               break;
        case AC_UNREGISTER:
                ret = __unregister_launch_privilege(ad->pkg_name, ad->pkg_type);
-               break;          
+               break;
        default:
                _E("no support packet");
        }
 
        _send_result_to_server(clifd, ret);
-       
+
        g_free(ad);
        free(pkt);
        return TRUE;
@@ -232,7 +238,7 @@ static void __pkt_type_list_free()
        GSList *iter2 = NULL;
        ac_type_list_t *type_t;
        ac_so_list_t *so_t;
-       
+
        for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
                type_t = iter->data;
                if(type_t) {
@@ -245,8 +251,8 @@ static void __pkt_type_list_free()
                                }
                        }
                        g_slist_free(type_t->so_list);
-               
-               
+
+
                        if(type_t->pkg_type)
                                free(type_t->pkg_type);
                        free(type_t);
@@ -270,6 +276,13 @@ int __initialize()
        src = g_source_new(&funcs, sizeof(GSource));
 
        gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
+       if (gpollfd == NULL) {
+               _E("out of memory");
+               g_source_unref(src);
+               close(fd);
+               return AC_R_ERROR;
+       }
+
        gpollfd->events = POLLIN;
        gpollfd->fd = fd;
 
@@ -289,31 +302,32 @@ int __initialize()
 
        DIR *dp;
        struct dirent *dentry;
-       DIR *sub_dp;
+       DIR *sub_dp = NULL;
        struct dirent *sub_dentry;
        char buf[MAX_LOCAL_BUFSZ];
        char buf2[MAX_LOCAL_BUFSZ];
        ac_type_list_t *type_t = NULL;
        void *handle = NULL;
        ac_so_list_t *so_t = NULL;
-       
+
        dp = opendir(PLUGINS_PREFIX);
        if (dp == NULL) {
                return AC_R_ERROR;
        }
        while ((dentry = readdir(dp)) != NULL) {
-               
-               if(dentry->d_type != DT_DIR) 
+
+               if(dentry->d_type != DT_DIR)
                        continue;
                if(strcmp(dentry->d_name,".") == 0 || strcmp(dentry->d_name,"..") == 0) 
                        continue;
-               
+
                snprintf(buf,MAX_LOCAL_BUFSZ,"%s/%s",PLUGINS_PREFIX,dentry->d_name);
-               _D("type : %s", dentry->d_name);
+               SECURE_LOGD("type : %s", dentry->d_name);
 
                type_t = malloc(sizeof(ac_type_list_t));
                if(type_t == NULL) {
                        __pkt_type_list_free();
+                       closedir(dp);
                        return AC_R_ERROR;
                }
                memset(type_t, 0, sizeof(ac_type_list_t));
@@ -321,22 +335,31 @@ int __initialize()
                type_t->so_list = NULL;
 
                pkg_type_list = g_slist_append(pkg_type_list, (void *)type_t);
-               
+
                sub_dp = opendir(buf);
-               
+               if (sub_dp == NULL) {
+                       __pkt_type_list_free();
+                       closedir(dp);
+                       return AC_R_ERROR;
+               }
+
                while ((sub_dentry = readdir(sub_dp)) != NULL) {
-                       
-                       if(sub_dentry->d_type == DT_DIR) 
+
+                       if(sub_dentry->d_type == DT_DIR)
                                continue;
                        snprintf(buf2,MAX_LOCAL_BUFSZ,"%s/%s", buf, sub_dentry->d_name);
-                       _D("so_name : %s", buf2);
-                       
+                       SECURE_LOGD("so_name : %s", buf2);
+
                        handle = dlopen(buf2, RTLD_LAZY);
-                       if(handle == NULL) 
+                       if(handle == NULL)
                                continue;
                        so_t = malloc(sizeof(ac_so_list_t));
                        if(so_t == NULL) {
                                __pkt_type_list_free();
+                               dlclose(handle);
+                               handle = NULL;
+                               closedir(sub_dp);
+                               closedir(dp);
                                return AC_R_ERROR;
                        }
                        memset(so_t, 0, sizeof(ac_so_list_t));
@@ -348,17 +371,26 @@ int __initialize()
                        type_t->so_list = g_slist_append(type_t->so_list, (void *)so_t);
                        handle = NULL;
                }
+               closedir(sub_dp);
        }
+       closedir(dp);
 
        return AC_R_OK;
 }
 
-SLPAPI int ac_server_initailize()
+SLPAPI int ac_server_initialize()
 {
        int ret = AC_R_OK;
-       
+
        ret = __initialize();
-       
+
        return ret;
 }
 
+SLPAPI int ac_server_check_launch_privilege(const char *pkg_name, const char *pkg_type, int pid)
+{
+       int ret = -1;
+       ret = __check_launch_privilege(pkg_name, pkg_type, pid);
+
+       return ret;
+}