Support legacy image for backward-compatibility
[platform/core/appfw/app2sd.git] / plugin / app2sd / server / app2sd_internals_registry.c
index 42ee967..52d4536 100644 (file)
 #include <dirent.h>
 #include <time.h>
 #include <db-util.h>
+#include <grp.h>
+#include <pwd.h>
 
 #include "app2sd_internals.h"
 
 #define MAX_QUERY_LEN 4096
 #define PASSWORD_LENGTH 64
+#define BUFSIZE 4096
+
 /*
 ########### Internal APIs ##################
  */
@@ -40,6 +44,68 @@ sqlite3 *app2sd_db;
        "(pkgid TEXT NOT NULL, password TEXT NOT NULL, " \
        "filename TEXT NOT NULL, uid INTEGER, PRIMARY KEY(pkgid, uid))"
 
+static int _app2sd_db_change_perm(const char *db_file)
+{
+       char buf[BUFSIZE];
+       char pwuid_buf[1024];
+       char journal_file[BUFSIZE];
+       int fd;
+       char *files[3];
+       int ret, i;
+       uid_t uid = APPFW_UID;
+       struct passwd userinfo, *result = NULL;
+       files[0] = (char *)db_file;
+       files[1] = journal_file;
+       files[2] = NULL;
+
+       if (db_file == NULL)
+               return -1;
+
+       if (getuid() != OWNER_ROOT) {
+               _E("not allowed user");
+               return -1;
+       }
+
+       snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
+
+       ret = getpwuid_r(uid, &userinfo, pwuid_buf, sizeof(pwuid_buf), &result);
+       if (ret != 0 || result == NULL) {
+               _E("user(%d) doesn't exist", uid);
+               return -1;
+       }
+
+       for (i = 0; files[i]; i++) {
+               fd = open(files[i], O_RDONLY);
+               if (fd == -1) {
+                       if (strerror_r(errno, buf, sizeof(buf)))
+                               strncpy(buf, "", BUFSIZE - 1);
+                       _E("failed to open %s : %s", files[i], buf);
+                       return -1;
+               }
+
+               ret = fchown(fd, uid, userinfo.pw_gid);
+               if (ret == -1) {
+                       if (strerror_r(errno, buf, sizeof(buf)))
+                               strncpy(buf, "", BUFSIZE - 1);
+                       _E("failed to fchown %s %d.%d : %s", files[i], uid,
+                                       userinfo.pw_gid, buf);
+                       close(fd);
+                       return -1;
+               }
+
+               ret = fchmod(fd, 0644);
+               if (ret == -1) {
+                       if (strerror_r(errno, buf, sizeof(buf)))
+                               strncpy(buf, "", BUFSIZE - 1);
+                       _E("failed to fchmod %s : %s", files[i], buf);
+                       close(fd);
+                       return -1;
+               }
+               close(fd);
+       }
+       return 0;
+}
+
 int _app2sd_initialize_db()
 {
        char *error_message = NULL;
@@ -78,6 +144,11 @@ int _app2sd_initialize_db()
                return -1;
        }
 
+       if (_app2sd_db_change_perm(APP2SD_DB_FILE) < 0) {
+               _E("failed to change permissions");
+               return -1;
+       }
+
        return 0;
 }
 
@@ -285,7 +356,7 @@ char *_app2sd_get_password_from_db(const char *pkgid, uid_t uid)
        sqlite3_stmt *stmt = NULL;
        int ret = 0;
 
-       query = sqlite3_mprintf("select * from app2sd_info " \
+       query = sqlite3_mprintf("select password from app2sd_info " \
                "where pkgid=%Q and uid=%d", pkgid, uid);
        if (query == NULL) {
                _E("failed to make a query");
@@ -304,14 +375,12 @@ char *_app2sd_get_password_from_db(const char *pkgid, uid_t uid)
                _W("no records found");
                goto FINISH_OFF;
        }
-       passwd = malloc(PASSWORD_LENGTH + 1);
-       if (passwd == NULL) {
+
+       passwd = strdup((const char *)sqlite3_column_text(stmt, 0));
+       if (!passwd) {
                _E("memory allocation failed");
                goto FINISH_OFF;
        }
-
-       strncpy(passwd, (const char *)sqlite3_column_text(stmt, 1),
-               PASSWORD_LENGTH);
        if (strlen(passwd) == 0) {
                _E("data is empty");
                goto FINISH_OFF;
@@ -333,3 +402,57 @@ FINISH_OFF:
 
        return NULL;
 }
+
+char *_app2sd_get_filename_from_db(const char *pkgid, uid_t uid)
+{
+       char *query = NULL;
+       char *filename = NULL;
+       sqlite3_stmt *stmt = NULL;
+       int ret = 0;
+
+       query = sqlite3_mprintf("select filename from app2sd_info " \
+               "where pkgid=%Q and uid=%d", pkgid, uid);
+       if (query == NULL) {
+               _E("failed to make a query");
+               return NULL;
+       }
+
+       ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
+               sqlite3_free(query);
+               return NULL;
+       }
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_ROW || ret == SQLITE_DONE) {
+               _W("no records found");
+               goto FINISH_OFF;
+       }
+
+       filename = strdup((const char *)sqlite3_column_text(stmt, 0));
+       if (!filename) {
+               _E("memory allocation failed");
+               goto FINISH_OFF;
+       }
+       if (strlen(filename) == 0) {
+               _E("data is empty");
+               goto FINISH_OFF;
+       }
+       if (SQLITE_OK != sqlite3_finalize(stmt)) {
+               _E("error : sqlite3_finalize");
+               goto FINISH_OFF;
+       }
+       sqlite3_free(query);
+
+       return filename;
+
+FINISH_OFF:
+       if (filename)
+               free(filename);
+
+       sqlite3_finalize(stmt);
+       sqlite3_free(query);
+
+       return NULL;
+}