fix TC-1516 85/26485/3 accepted/tizen/common/20140826.105411 submit/tizen/20140825.155632 submit/tizen/20140826.082542 submit/tizen_ivi/20140827.000000 submit/tizen_ivi/20140828.000000
authorSabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
Mon, 18 Aug 2014 08:38:20 +0000 (10:38 +0200)
committerSabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
Mon, 25 Aug 2014 15:01:32 +0000 (17:01 +0200)
fix permission icons dir
fix display image icon
fix db_exec_sqlite_query for Globalro db

Bug-Tizen: TC-1516

Change-Id: I64844fb5734958ca7990463a193bd9b32240ee74
Signed-off-by: Sabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
src/ail_db.c
src/ail_db.h
src/ail_desktop.c
tool/src/ail_fota.c

index df285da..46652ea 100755 (executable)
@@ -41,6 +41,8 @@
 
 #define QUERY_CREATE_VIEW_LOCAL "CREATE temp VIEW localname as select distinct * from (select  * from main.localname m union select * from Global.localname g)"
 
+#define SMACK_LABEL "_"
+
 #define retv_with_dbmsg_if(expr, val) do { \
        if (expr) { \
                _E("db_info.dbUserro: %s", sqlite3_errmsg(db_info.dbUserro)); \
@@ -113,25 +115,19 @@ static int ail_db_change_perm(const char *db_file)
 
 char* ail_get_icon_path(uid_t uid)
 {
-       char *result_psswd = NULL;
-       if(uid == GLOBAL_USER)
-       {
-               result_psswd = tzplatform_getenv(TZ_SYS_RW_ICONS);
-       }
-       else
-       {
-               const char *name = "users";
-               struct passwd *userinfo = NULL;
-               struct group *grpinfo = NULL;
-
-               userinfo = getpwuid(uid);
-               if(userinfo == NULL) {
+       char *result = NULL;
+       struct group *grpinfo = NULL;
+       char *dir = NULL;
+       struct passwd *userinfo = getpwuid(uid);
+
+       if (uid != GLOBAL_USER) {
+
+               if (userinfo == NULL) {
                        _E("getpwuid(%d) returns NULL !", uid);
                        return NULL;
                }
-               
-               grpinfo = getgrnam(name);
-               if(grpinfo == NULL) {
+               grpinfo = getgrnam("users");
+               if (grpinfo == NULL) {
                        _E("getgrnam(users) returns NULL !");
                        return NULL;
                }
@@ -140,34 +136,117 @@ char* ail_get_icon_path(uid_t uid)
                        _E("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               result_psswd = tzplatform_getenv(TZ_USER_ICONS);
+               asprintf(&result, "%s/.applications/icons/", userinfo->pw_dir);
+       } else {
+               result = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
+               grpinfo = getgrnam("root");
+               if (grpinfo == NULL) {
+                       _E("getgrnam(root) returns NULL !");
+                       return NULL;
+                       }
+                       if (grpinfo->gr_gid != userinfo->pw_gid) {
+                               _E("UID [%d] does not belong to 'root' group!", uid);
+                               return NULL;
+                       }
+                       /* chsmack */
+                       if (smack_setlabel(result, SMACK_LABEL, SMACK_LABEL_ACCESS)) {
+                               _E("failed chsmack -a \"%s\" %s", SMACK_LABEL, result);
+                       } else {
+                       _D("chsmack -a \"%s\" %s", SMACK_LABEL, result);
+                       }
        }
-       return result_psswd;
+       int ret;
+       mkdir(result, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+       ret = chown(result, uid, grpinfo->gr_gid);
+       if (ret == -1) {
+               char buf[BUFSIZE];
+               strerror_r(errno, buf, sizeof(buf));
+               _E("FAIL : chown %s %d.%d, because %s", result, uid, grpinfo->gr_gid, buf);
+       }
+       return result;
 }
 
 static char* ail_get_app_DB(uid_t uid)
 {
-       char *result_psswd = NULL;
+       char *result = NULL;
+       char *journal = NULL;
        struct group *grpinfo = NULL;
-       char * dir = NULL;
-       if(uid == GLOBAL_USER)
-       {
-               result_psswd = strdup(APP_INFO_DB_FILE);
-               grpinfo = getgrnam("root");
-               if(grpinfo == NULL) {
+       char *dir = NULL;
+       struct passwd *userinfo = getpwuid(uid);
+
+       if (uid != GLOBAL_USER) {
+
+               if (userinfo == NULL) {
+                       _E("getpwuid(%d) returns NULL !", uid);
+                       return NULL;
+               }
+               grpinfo = getgrnam("users");
+               if (grpinfo == NULL) {
                        _E("getgrnam(users) returns NULL !");
-                       return NULL;
+                       return NULL;
+               }
+               // Compare git_t type and not group name
+               if (grpinfo->gr_gid != userinfo->pw_gid) {
+                       _E("UID [%d] does not belong to 'users' group!", uid);
+                       return NULL;
+               }
+               asprintf(&result, "%s/.applications/dbspace/.app_info.db", userinfo->pw_dir);
+               asprintf(&journal, "%s/.applications/dbspace/.app_info.db-journal", userinfo->pw_dir);
+       } else {
+               grpinfo = getgrnam("root");
+               if (grpinfo == NULL) {
+                       _E("getgrnam(root) returns NULL !");
+                       return NULL;
+                       }
+                       if (grpinfo->gr_gid != userinfo->pw_gid) {
+                               _E("UID [%d] does not belong to 'root' group!", uid);
+                               return NULL;
+                       }
+                       result = strdup(APP_INFO_DB_FILE);
+                       journal = strdup(APP_INFO_DB_FILE_JOURNAL);
+                       /* chsmack */
+                       if (smack_setlabel(result, SMACK_LABEL, SMACK_LABEL_ACCESS)) {
+                               _E("failed chsmack -a \"%s\" %s", SMACK_LABEL, result);
+               } else {
+                       _D("chsmack -a \"%s\" %s", SMACK_LABEL, result);
+               }
+               if (smack_setlabel(journal, SMACK_LABEL, SMACK_LABEL_ACCESS)) {
+                       _E("failed chsmack -a \"%s\" %s", SMACK_LABEL, journal);
+               } else {
+                       _D("chsmack -a \"%s\" %s", SMACK_LABEL, journal);
                }
        }
-       else
-       {
-               struct passwd *userinfo = getpwuid(uid);
-               if(userinfo == NULL) {
+       dir = strrchr(result, '/');
+       if(!dir)
+               return result;
+
+       int ret;
+       mkdir(dir + 1, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+       ret = chown(dir + 1, uid, grpinfo->gr_gid);
+       if (ret == -1) {
+               char buf[BUFSIZE];
+               strerror_r(errno, buf, sizeof(buf));
+               _E("FAIL : chown %s %d.%d, because %s", dir + 1, uid, grpinfo->gr_gid, buf);
+       }
+
+       return result;
+}
+
+char* al_get_desktop_path(uid_t uid)
+{
+       char *result = NULL;
+       struct group *grpinfo = NULL;
+       char *dir = NULL;
+       struct passwd *userinfo = getpwuid(uid);
+
+       if (uid != GLOBAL_USER) {
+
+               if (userinfo == NULL) {
                        _E("getpwuid(%d) returns NULL !", uid);
                        return NULL;
                }
                grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
+               if (grpinfo == NULL) {
                        _E("getgrnam(users) returns NULL !");
                        return NULL;
                }
@@ -176,34 +255,38 @@ static char* ail_get_app_DB(uid_t uid)
                        _E("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               asprintf(&result_psswd, "%s/.applications/dbspace/.app_info.db", userinfo->pw_dir);
+               asprintf(&result, "%s/.applications/desktop/", userinfo->pw_dir);
+       } else {
+               grpinfo = getgrnam("root");
+               if (grpinfo == NULL) {
+                       _E("getgrnam(root) returns NULL !");
+                       return NULL;
+               }
+               if (grpinfo->gr_gid != userinfo->pw_gid) {
+                       _E("UID [%d] does not belong to 'root' group!", uid);
+                       return NULL;
+               }
+               result = tzplatform_mkpath(TZ_SYS_RW_DESKTOP_APP, "/");
+               /* chsmack */
+               if (smack_setlabel(result, SMACK_LABEL, SMACK_LABEL_ACCESS)) {
+                       _E("failed chsmack -a \"%s\" %s", SMACK_LABEL, result);
+               } else {
+                       _D("chsmack -a \"%s\" %s", SMACK_LABEL, result);
+               }
        }
 
-       dir = strrchr(result_psswd, '/');
-       if(!dir)
-               return result_psswd;
-
-       //Control if db exist create otherwise 
-       if(access(dir + 1, F_OK)) {
-               int ret;
-               mkdir(dir + 1, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-               ret = chown(dir + 1, uid, grpinfo->gr_gid);
-               if (ret == -1) {
-                       char buf[BUFSIZE];
-                       strerror_r(errno, buf, sizeof(buf));
-                       _E("FAIL : chown %s %d.%d, because %s", dir + 1, uid, grpinfo->gr_gid, buf);
-               }
-    /* chsmack */
-    if(smack_setlabel(result_psswd, "_", SMACK_LABEL_ACCESS))
-         {
-                 _E("failed chsmack -a \"_\" %s", result_psswd);
-         } else {
-                 _D("chsmack -a \"_\" %s", result_psswd);
-         }
+       int ret;
+       mkdir(result, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+       ret = chown(result, uid, grpinfo->gr_gid);
+       if (ret == -1) {
+               char buf[BUFSIZE];
+               strerror_r(errno, buf, sizeof(buf));
+               _E("FAIL : chown %s %d.%d, because %s", result, uid, grpinfo->gr_gid, buf);
        }
-       return result_psswd;
+       return result;
 }
 
+
 static ail_error_e db_do_prepare(sqlite3 *db, const char *query, sqlite3_stmt **stmt)
 {
        int ret;
@@ -272,51 +355,52 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
                                ret = do_db_exec(tbls[i], dbInit);
                                retv_if(ret != AIL_ERROR_OK, AIL_ERROR_DB_FAILED);
                        }
+                       if(AIL_ERROR_OK != ail_db_change_perm(ail_get_app_DB(uid))) {
+                               _E("Failed to change permission\n");
+               }
                } else {
                        dbInit = NULL;
-                       _E("Failed to create table %s\n",ail_get_app_DB(uid));
+                       _E("Failed to create table %s\n", ail_get_app_DB(uid));
                }
        }
        if(dbInit) {
-               if(AIL_ERROR_OK != ail_db_change_perm(ail_get_app_DB(uid))) {
-                               _E("Failed to change permission\n");
-               }
                ret = sqlite3_close(dbInit);
                retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
                dbInit = NULL;
        }
        if(mode & DB_OPEN_RO) {
-               if (!db_info.dbUserro) {
-                       db_util_open_with_options(ail_get_app_DB(uid), &db_info.dbUserro, SQLITE_OPEN_READONLY, NULL);
-                       char query_attach[AIL_SQL_QUERY_MAX_LEN];
-                       char query_view_app[AIL_SQL_QUERY_MAX_LEN];
-                       char query_view_local[AIL_SQL_QUERY_MAX_LEN];
-                       snprintf(query_attach, AIL_SQL_QUERY_MAX_LEN, QUERY_ATTACH, ail_get_app_DB(GLOBAL_USER));
-                       _E("info : execute query_attach : %s", query_attach );
-                       if (db_exec_usr_ro(query_attach) < 0) {
-                               return AIL_ERROR_DB_FAILED;
-                       }
-                       snprintf(query_view_app, AIL_SQL_QUERY_MAX_LEN, QUERY_CREATE_VIEW_APP);
-                       _E("info : execute query_attach : %s", query_view_app );
-                       if (db_exec_usr_ro(query_view_app) < 0) {
-                               return AIL_ERROR_DB_FAILED;
+               if(uid != GLOBAL_USER) {
+                       if (!db_info.dbUserro) {
+                               db_util_open_with_options(ail_get_app_DB(uid), &db_info.dbUserro, SQLITE_OPEN_READONLY, NULL);
+                               char query_attach[AIL_SQL_QUERY_MAX_LEN];
+                               char query_view_app[AIL_SQL_QUERY_MAX_LEN];
+                               char query_view_local[AIL_SQL_QUERY_MAX_LEN];
+                               snprintf(query_attach, AIL_SQL_QUERY_MAX_LEN, QUERY_ATTACH, ail_get_app_DB(GLOBAL_USER));
+                               _E("info : execute query_attach : %s", query_attach );
+                               if (db_exec_usr_ro(query_attach) < 0) {
+                                       return AIL_ERROR_DB_FAILED;
+                               }
+                               snprintf(query_view_app, AIL_SQL_QUERY_MAX_LEN, QUERY_CREATE_VIEW_APP);
+                               _E("info : execute query_attach : %s", query_view_app );
+                               if (db_exec_usr_ro(query_view_app) < 0) {
+                                       return AIL_ERROR_DB_FAILED;
+                               }
+
+                               snprintf(query_view_local, AIL_SQL_QUERY_MAX_LEN, QUERY_CREATE_VIEW_LOCAL);
+                               _E("info : execute query_attach : %s", query_view_local );
+                               if (db_exec_usr_ro(query_view_local) < 0) {
+                                       return AIL_ERROR_DB_FAILED;
+                               }
                        }
-
-                       snprintf(query_view_local, AIL_SQL_QUERY_MAX_LEN, QUERY_CREATE_VIEW_LOCAL);
-                       _E("info : execute query_attach : %s", query_view_local );
-                       if (db_exec_usr_ro(query_view_local) < 0) {
-                               return AIL_ERROR_DB_FAILED;
-                       }
-               }
-               if (!db_info.dbGlobalro) {
+               } else {
+                       if (!db_info.dbGlobalro) {
                                ret = db_util_open_with_options(ail_get_app_DB(GLOBAL_USER), &db_info.dbGlobalro, SQLITE_OPEN_READONLY, NULL);
                                retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
                        }
-       }
-
+               }
+ }
        if(mode & DB_OPEN_RW) {
-               //if (__is_admin) {
-               if(uid != GLOBAL_USER) {//TOCHANGETO is_admin
+               if(uid != GLOBAL_USER) {
                        if(!db_info.dbUserrw){
                                ret = db_util_open(ail_get_app_DB(uid), &db_info.dbUserrw, 0);
                        }
@@ -326,8 +410,6 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
                        }
                }
                retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
-       
-               //}
        }
 
        return AIL_ERROR_OK;
@@ -560,9 +642,9 @@ EXPORT_API ail_error_e ail_db_close(void)
 int db_exec_sqlite_query(char *query, sqlite_query_callback callback, void *data)
 {
        char *error_message = NULL;
-       if(db_info.dbUserro) {
+       if(db_info.dbGlobalro) {
                if (SQLITE_OK !=
-                       sqlite3_exec(db_info.dbUserro, query, callback, data, &error_message)) {
+                       sqlite3_exec(db_info.dbGlobalro, query, callback, data, &error_message)) {
                        _E("Don't execute query = %s error message = %s\n", query,
                                error_message);
                        sqlite3_free(error_message);
index e1ec80a..b551d20 100755 (executable)
@@ -36,8 +36,8 @@ typedef enum {
 } db_open_mode;
 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
 
-static int ail_db_change_perm(const char *db_file);
 char* ail_get_icon_path(uid_t uid);
+char* al_get_desktop_path(uid_t uid);
 ail_error_e db_open(db_open_mode mode, uid_t uid);
 ail_error_e db_prepare(const char *query, sqlite3_stmt **stmt);
 ail_error_e db_prepare_globalro(const char *query, sqlite3_stmt **stmt);
index 769bd13..3868b79 100755 (executable)
@@ -43,6 +43,7 @@
 #include "ail.h"
 
 #define BUFSIZE 4096
+#define GLOBAL_USER 0
 
 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
 #define argsdelimiter  " \t"
@@ -272,43 +273,27 @@ _get_icon_with_path(char* icon, uid_t uid)
                len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
                icon_with_path = malloc(len);
                if(icon_with_path == NULL) {
-                       _E("(icon_with_path == NULL) return\n");
+                       _E("icon_with_path == NULL\n");
                        free(package);
                        free(theme);
                        return NULL;
                }
 
                memset(icon_with_path, 0, len);
-
-               sqlite3_snprintf( len, icon_with_path,"%s/%q/small/%q", ail_get_icon_path(uid), theme, icon);
-               do {
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       sqlite3_snprintf( len, icon_with_path,"%s/%q/small/%q", tzplatform_getenv(TZ_SYS_RO_ICONS), theme, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       _D("cannot find icon %s", icon_with_path);
-                       sqlite3_snprintf( len, icon_with_path, "%s/default/small/%q", ail_get_icon_path(uid), icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       sqlite3_snprintf( len, icon_with_path, "%s/default/small/%q", tzplatform_getenv(TZ_SYS_RO_ICONS), icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-
-                       #if 1 /* this will be remove when finish the work for moving icon path */
-                       _E("icon file must be moved to %s", icon_with_path);
-                       sqlite3_snprintf( len, icon_with_path,  "%s/%q/res/icons/%q/small/%q", tzplatform_getenv(TZ_SYS_RW_APP), package, theme, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
+               if (uid != GLOBAL_USER)
+                       sqlite3_snprintf( len, icon_with_path, "%s%q", ail_get_icon_path(uid), icon);
+               else
+                       sqlite3_snprintf( len, icon_with_path, "%s/%q/small/%q", ail_get_icon_path(GLOBAL_USER), theme, icon);
+               if (!access (icon_with_path, F_OK))
                        sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/%q/small/%q", tzplatform_getenv(TZ_SYS_RO_APP), package, theme, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       _D("cannot find icon %s", icon_with_path);
-                       sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/default/small/%q", tzplatform_getenv(TZ_SYS_RW_APP), package, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/default/small/%q", tzplatform_getenv(TZ_SYS_RO_APP), package, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       #endif
-               } while (0);
-
+               else if (!access (icon_with_path, F_OK))
+                       sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/%q/small/%q", tzplatform_getenv(TZ_SYS_RW_APP), package, theme, icon);
+               else
+                       _D("Cannot find icon path");
                free(theme);
                free(package);
+               _D("Icon path : %s", icon_with_path);
 
-               _D("Icon path : %s ---> %s", icon, icon_with_path);
 
                return icon_with_path;
        } else {
@@ -802,46 +787,7 @@ char *_pkgname_to_desktop(const char *package, uid_t uid)
 
        retv_if(!package, NULL);
 
-       if(uid != GLOBAL_USER)
-    {
-    desktop_path = tzplatform_mkpath(TZ_USER_HOME, ".applications/desktop");
-    if(access(desktop_path, F_OK)) {
-               struct group *grpinfo = NULL;
-               const char *name = "users"; //to change
-               int ret;
-               char buf[BUFSIZE];
-               mkdir(desktop_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-               grpinfo = getgrnam(name);
-               if(grpinfo == NULL)
-                       _E("getgrnam(users) returns NULL !");
-
-               ret = chown(desktop_path, uid, grpinfo->gr_gid);
-               if (ret == -1) {
-                       strerror_r(errno, buf, sizeof(buf));
-                       _E("FAIL : chown %s %d.%d, because %s", desktop_path, uid, grpinfo->gr_gid, buf);
-               }       
-       }
-  }
-  else
-  {
-    desktop_path = tzplatform_getenv(TZ_SYS_RW_DESKTOP_APP);
-    if(access(desktop_path, F_OK)) {
-               struct group *grpinfo = NULL;
-               const char *name = "root"; //to change
-               int ret;
-               char buf[BUFSIZE];
-               mkdir(desktop_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-               grpinfo = getgrnam(name);
-               if(grpinfo == NULL)
-                       _E("getgrnam(users) returns NULL !");
-
-               ret = chown(desktop_path, uid, grpinfo->gr_gid);
-               if (ret == -1) {
-                       strerror_r(errno, buf, sizeof(buf));
-                       _E("FAIL : chown %s %d.%d, because %s", desktop_path, uid, grpinfo->gr_gid, buf);
-               }       
-       }
-  }
+  desktop_path = al_get_desktop_path(uid);
 
        size = strlen(desktop_path) + strlen(package) + 10;
        desktop = malloc(size);
index e48121e..85aa973 100644 (file)
@@ -59,8 +59,10 @@ static int initdb_count_app(uid_t uid)
                return -1;
        }
 //__isadmin
-       ret = ail_filter_count_usr_appinfo(filter,  &total, uid);
-       //ret = ail_filter_count_appinfo(filter,  &total);
+        if (uid != GLOBAL_USER)
+         ret = ail_filter_count_usr_appinfo(filter,  &total, uid);
+       else
+          ret = ail_filter_count_appinfo(filter,  &total);
        if (ret != AIL_ERROR_OK) {
                ail_filter_destroy(filter);
                return -1;