+ if (getpwuid_r(uid, &pwd, pwd_buf, buflen, &userinfo) != 0) {
+ _E("getpwuid(%d) returns NULL !", uid);
+ free(pwd_buf);
+ return NULL;
+ }
+
+ buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (buflen <= 0)
+ buflen = 1024;
+
+ buf = (char *)malloc(buflen);
+ if (buf == NULL) {
+ _E("out of memory");
+ free(pwd_buf);
+ return NULL;
+ }
+
+ if (getgrnam_r("users", &grpbuf, buf, buflen, &grpinfo) != 0) {
+ _E("getgrnam_r(users) returns NULL !");
+ free(pwd_buf);
+ free(buf);
+ 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);
+ free(pwd_buf);
+ free(buf);
+ return NULL;
+ }
+
+ if (asprintf(&result, "%s/.applications/icons/", userinfo->pw_dir) == -1)
+ _E("out of memory: asprintf() is failed.");
+
+ free(pwd_buf);
+ } else {
+ result = strdup(tzplatform_mkpath(TZ_SYS_RW_ICONS, "/"));
+ if (result == NULL) {
+ _E("out of memory");
+ return NULL;
+ }
+ }
+
+ int ret = mkdir(result, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+ if (ret == -1 && errno != EEXIST) {
+ _E("FAIL : to create directory %s %d", result, errno);
+ } else if (getuid() == OWNER_ROOT) {
+ ret = chown(result, uid, grpinfo ? grpinfo->gr_gid : 0);
+ SET_SMACK_LABEL(result, uid);
+ if (ret == -1) {
+ _E("FAIL : chown %s %d.%d, because %d", result, uid,
+ grpinfo ? grpinfo->gr_gid : 0, errno);
+ }
+ }
+
+ free(buf);
+
+ return result;
+}
+
+char *ail_get_app_DB_journal(uid_t uid)
+{
+ char *app_path = ail_get_app_DB(uid);
+ char *result = NULL;
+
+ if (asprintf(&result, "%s-journal", app_path) == -1)
+ _E("out of memory: asprintf() is failed.");
+
+ free(app_path);
+
+ return result;
+}
+
+char *ail_get_app_DB(uid_t uid)
+{
+ char *result = NULL;
+ struct group grpbuf;
+ struct group *grpinfo = NULL;
+ char *dir = NULL;
+ char *temp = NULL;
+ struct passwd pwd;
+ struct passwd *userinfo;
+ int buflen;
+ char *buf = NULL;
+ char *pwd_buf;
+
+ if (uid == 0) {
+ _E("FAIL : Root is not allowed! switch to DEFAULT_USER");
+ return NULL;
+ }
+
+ if (uid != GLOBAL_USER) {
+ buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (buflen <= 0)
+ buflen = 16384;
+
+ pwd_buf = (char *)malloc(buflen);
+ if (pwd_buf == NULL) {
+ _E("out of memory");
+ return NULL;
+ }
+
+ if (getpwuid_r(uid, &pwd, pwd_buf, buflen, &userinfo) != 0) {
+ _E("getpwuid(%d) returns NULL !", uid);
+ free(pwd_buf);
+ return NULL;
+ }
+
+ buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (buflen <= 0)
+ buflen = 1024;
+
+ buf = (char *)malloc(buflen);
+ if (buf == NULL) {
+ _E("out of memory");
+ free(pwd_buf);
+ return NULL;
+ }
+
+ if (getgrnam_r("users", &grpbuf, buf, buflen, &grpinfo) != 0) {
+ _E("getgrnam(users) returns NULL !");
+ free(pwd_buf);
+ free(buf);
+ 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);
+ free(pwd_buf);
+ free(buf);
+ return NULL;
+ }
+
+ if (asprintf(&result, "%s/.applications/dbspace/.app_info.db", userinfo->pw_dir) == -1)
+ _E("out of memory: asprintf() is failed.");
+
+ free(pwd_buf);
+ } else {
+ result = strdup(APP_INFO_DB_FILE);
+ if (result == NULL) {
+ _E("ouf of memory");
+ return NULL;
+ }
+ }
+
+ temp = strdup(result);
+ if (temp == NULL) {
+ free(buf);
+ return result;
+ }
+
+ dir = strrchr(temp, '/');
+ if (!dir) {
+ free(temp);
+ free(buf);
+ return result;
+ }
+ *dir = 0;
+
+ int ret = mkdir(temp, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+ if (ret == -1 && errno != EEXIST) {
+ _E("FAIL : to create directory %s %d", temp, errno);
+ } else if (getuid() == OWNER_ROOT) {
+ ret = chown(temp, uid, grpinfo ? grpinfo->gr_gid : 0);
+ SET_SMACK_LABEL(temp, uid);
+ if (ret == -1) {
+ _E("FAIL : chown %s %d.%d, because %d", temp, uid,
+ grpinfo ? grpinfo->gr_gid : 0, errno);
+ }
+ }
+
+ free(temp);
+ free(buf);
+
+ return result;
+}
+
+char *ail_get_desktop_path(uid_t uid)
+{
+ char *result = NULL;
+ struct group grpbuf;
+ struct group *grpinfo = NULL;
+ struct passwd pwd;
+ struct passwd *userinfo = NULL;
+ int buflen;
+ char *buf = NULL;
+ char *pwd_buf;
+
+ if (uid == 0) {
+ _E("FAIL : Root is not allowed user! please fix it replacing with DEFAULT_USER");
+ return NULL;
+ }
+
+ if (uid != GLOBAL_USER) {
+ buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (buflen <= 0)
+ buflen = 16384;
+
+ pwd_buf = (char *)malloc(buflen);
+ if (pwd_buf == NULL) {
+ _E("out of memory");
+ return NULL;
+ }
+
+ if (getpwuid_r(uid, &pwd, pwd_buf, buflen, &userinfo) != 0) {
+ _E("getpwuid(%d) returns NULL !", uid);
+ free(pwd_buf);
+ return NULL;
+ }
+
+ buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (buflen <= 0)
+ buflen = 1024;
+
+ buf = (char *)malloc(buflen);
+ if (buf == NULL) {
+ _E("out of memory");
+ free(pwd_buf);
+ return NULL;
+ }
+
+ if (getgrnam_r("users", &grpbuf, buf, buflen, &grpinfo) != 0) {
+ _E("getgrnam(users) returns NULL !");
+ free(pwd_buf);
+ free(buf);
+ 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);
+ free(pwd_buf);
+ free(buf);
+ return NULL;
+ }
+
+ if (asprintf(&result, "%s/.applications/desktop/", userinfo->pw_dir) == -1)
+ _E("out of memory: asprintf() is failed.");
+
+ free(pwd_buf);
+ } else {
+ result = strdup(tzplatform_mkpath(TZ_SYS_RW_DESKTOP_APP, "/"));
+ if (result == NULL) {
+ _E("out of memory");
+ return NULL;
+ }
+ }
+
+ int ret = mkdir(result, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+ if (ret == -1 && errno != EEXIST) {
+ _E("FAIL : to create directory %s %d", result, errno);
+ } else if (getuid() == OWNER_ROOT) {
+ ret = chown(result, uid, grpinfo ? grpinfo->gr_gid : 0);
+ SET_SMACK_LABEL(result, uid);
+ if (ret == -1) {
+ _E("FAIL : chown %s %d.%d, because %d", result, uid,
+ grpinfo ? grpinfo->gr_gid : 0, errno);
+ }
+ }
+
+ free(buf);
+
+ return result;
+}
+
+
+static ail_error_e db_do_prepare(sqlite3 *db, const char *query, sqlite3_stmt **stmt)