Fix misusing of strncat
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_appinfo.c
index d4f9dc5..e56c602 100644 (file)
@@ -57,81 +57,55 @@ static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
        const char *locale, uid_t uid, char **query, GList **bind_params)
 {
        int joined = 0;
-       size_t len = 0;
        char *condition = NULL;
        char buf[MAX_QUERY_LEN] = { '\0' };
        char tmp_query[MAX_QUERY_LEN] = { '\0' };
-       static const char query_pkg_disable[] = " AND ai.package IN "
-                       "(SELECT package FROM package_info WHERE package_disable='false')";
        GSList *list;
 
-       strncat(buf, " WHERE 1=1", MAX_QUERY_LEN - len - 1);
-       len += strlen(" WHERE 1=1");
-
-       if (filter == NULL) {
-               strncat(buf, query_pkg_disable, MAX_QUERY_LEN - len - 1);
-               *query = strdup(buf);
+       if (!filter)
                return PMINFO_R_OK;
-       }
+       strncat(buf, " WHERE 1=1", sizeof(buf) - strlen(buf) - 1);
 
        for (list = filter->list; list; list = list->next) {
                joined |= __get_filter_condition(list->data, uid, &condition, bind_params);
                if (condition == NULL)
                        continue;
 
-               strncat(buf, " AND ", MAX_QUERY_LEN - len - 1);
-               len += strlen(" AND ");
+               strncat(buf, " AND ", sizeof(buf) - strlen(buf) - 1);
 
-               strncat(buf, condition, sizeof(buf) - len - 1);
-               len += strlen(condition);
+               strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
                free(condition);
                condition = NULL;
        }
 
-       if (filter->list_metadata) {
-               strncat(buf, " AND (", MAX_QUERY_LEN - len - 1);
-               len += strlen(" AND (");
-       }
+       if (filter->list_metadata)
+               strncat(buf, " AND (", sizeof(buf) - strlen(buf) - 1);
        for (list = filter->list_metadata; list; list = list->next) {
                joined |= __get_metadata_filter_condition(list->data,
                                &condition, bind_params);
                if (condition == NULL)
                        continue;
-               strncat(buf, condition, sizeof(buf) - len - 1);
-               len += strlen(condition);
+               strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
                free(condition);
                condition = NULL;
 
-               strncat(buf, " OR ", MAX_QUERY_LEN - len - 1);
-               len += strlen(" OR ");
-       }
-       if (filter->list_metadata) {
-               strncat(buf, "1=0)", MAX_QUERY_LEN - len - 1);
-               len += strlen("1=0)");
+               strncat(buf, " OR ", sizeof(buf) - strlen(buf) - 1);
        }
+       if (filter->list_metadata)
+               strncat(buf, "1=0)", sizeof(buf) - strlen(buf) - 1);
 
        if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
-               strncat(tmp_query, join_localized_info, MAX_QUERY_LEN - len - 1);
-               len += strlen(join_localized_info);
+               strncat(tmp_query, join_localized_info, sizeof(tmp_query) - strlen(tmp_query) - 1);
                *bind_params = g_list_append(*bind_params, strdup(locale));
        }
-       if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY) {
-               strncat(tmp_query, join_category, MAX_QUERY_LEN - len - 1);
-               len += strlen(join_category);
-       }
-       if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL) {
-               strncat(tmp_query, join_app_control, MAX_QUERY_LEN - len - 1);
-               len += strlen(join_app_control);
-       }
-       if (joined & E_PMINFO_APPINFO_JOIN_METADATA) {
-               strncat(tmp_query, join_metadata, MAX_QUERY_LEN - len - 1);
-               len += strlen(join_metadata);
-       }
-
-       strncat(tmp_query, buf, MAX_QUERY_LEN - len - 1);
-       len += strlen(buf);
+       if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY)
+               strncat(tmp_query, join_category, sizeof(tmp_query) - strlen(tmp_query) - 1);
+       if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL)
+               strncat(tmp_query, join_app_control, sizeof(tmp_query) - strlen(tmp_query) - 1);
+       if (joined & E_PMINFO_APPINFO_JOIN_METADATA)
+               strncat(tmp_query, join_metadata, sizeof(tmp_query) - strlen(tmp_query) - 1);
 
-       strncat(tmp_query, query_pkg_disable, MAX_QUERY_LEN - len - 1);
+       strncat(tmp_query, buf, sizeof(tmp_query) - strlen(tmp_query) - 1);
 
        *query = strdup(tmp_query);
        if (*query == NULL)
@@ -381,24 +355,29 @@ static void __free_applications(gpointer data)
        pkgmgrinfo_basic_free_application((application_x *)data);
 }
 
-static gint __app_disable_chk_func(gconstpointer data1, gconstpointer data2)
+static gint __disable_chk_func(gconstpointer data1, gconstpointer data2)
 {
        pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)data1;
+       pkgmgrinfo_appinfo_disable_type *value =
+                       (pkgmgrinfo_appinfo_disable_type *)data2;
 
-       if (node->prop == E_PMINFO_APPINFO_PROP_APP_DISABLE)
-               return 0;
+       if (value == E_APPINFO_DISABLE_TYPE_PKG)
+               return (node->prop == E_PMINFO_APPINFO_PROP_PKG_DISABLE)
+                               ? 0 : 1;
        else
-               return 1;
+               return (node->prop == E_PMINFO_APPINFO_PROP_APP_DISABLE)
+                               ? 0 : 1;
 }
 
-static bool __check_disable_filter_exist(pkgmgrinfo_filter_x *filter)
+static bool __check_disable_filter_exist(pkgmgrinfo_filter_x *filter,
+               pkgmgrinfo_appinfo_disable_type type)
 {
        GSList *link;
 
        if (filter == NULL)
                return false;
 
-       link = g_slist_find_custom(filter->list, NULL, __app_disable_chk_func);
+       link = g_slist_find_custom(filter->list, &type, __disable_chk_func);
        if (link)
                return true;
 
@@ -490,7 +469,6 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
                "ON (ai.app_id=ui.app_id AND ui.uid=?)";
        int ret = PMINFO_R_ERROR;
        int idx;
-       int len = 0;
        char *dbpath;
        char *bg_category_str = NULL;
        char *constraint = NULL;
@@ -516,23 +494,18 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
        }
        free(dbpath);
 
-       len = strlen(query_raw);
        snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
 
        if (flag & PMINFO_APPINFO_GET_BASICINFO) {
-               strncat(query, query_basic, MAX_QUERY_LEN - len - 1);
-               len += strlen(query_basic);
-               strncat(query, query_uid_info, MAX_QUERY_LEN - len - 1);
-               len += strlen(query_uid_info);
+               strncat(query, query_basic, sizeof(query) - strlen(query) - 1);
+               strncat(query, query_uid_info, sizeof(query) - strlen(query) - 1);
        }
        if (flag & PMINFO_APPINFO_GET_LABEL) {
-               strncat(query, query_label, MAX_QUERY_LEN - len - 1);
-               len += strlen(query_label);
+               strncat(query, query_label, sizeof(query) - strlen(query) - 1);
                bind_params = g_list_append(bind_params, strdup(locale));
        }
        if (flag & PMINFO_APPINFO_GET_ICON) {
-               strncat(query, query_icon, MAX_QUERY_LEN - len - 1);
-               len += strlen(query_icon);
+               strncat(query, query_icon, sizeof(query) - strlen(query) - 1);
                bind_params = g_list_append(bind_params, strdup(locale));
        }
 
@@ -546,14 +519,12 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
                LOGE("Failed to get WHERE clause");
                goto catch;
        }
-       strncat(query, query_from_clause, MAX_QUERY_LEN - len - 1);
-       len += strlen(query_from_clause);
+       strncat(query, query_from_clause, sizeof(query) - strlen(query) - 1);
 
-       strncat(query, query_uid_info_clause, MAX_QUERY_LEN - len - 1);
-       len += strlen(query_uid_info_clause);
+       strncat(query, query_uid_info_clause, sizeof(query) - strlen(query) - 1);
 
        if (constraint)
-               strncat(query, constraint, MAX_QUERY_LEN - len - 1);
+               strncat(query, constraint, sizeof(query) - strlen(query) - 1);
 
        ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -764,7 +735,7 @@ static int _pkgmgrinfo_get_appinfo(const char *appid, uid_t uid,
                                PMINFO_APPINFO_GET_ALL, list);
 
        if (!g_hash_table_size(list)) {
-               _LOGI("appinfo for [%s] is not existed for user [%d]",
+               _LOGD("appinfo for [%s] is not existed for user [%d]",
                                appid, uid);
                g_hash_table_destroy(list);
                free(locale);
@@ -860,6 +831,13 @@ API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
                return PMINFO_R_ERROR;
        }
 
+       ret = pkgmgrinfo_appinfo_filter_add_bool(filter,
+                       PMINFO_APPINFO_PROP_PKG_DISABLE, false);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_appinfo_filter_destroy(filter);
+               return PMINFO_R_ERROR;
+       }
+
        ret = _pkgmgrinfo_get_appinfo(appid, uid, filter, handle);
        pkgmgrinfo_appinfo_filter_destroy(filter);
        return ret;
@@ -1275,11 +1253,6 @@ static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
                return PMINFO_R_ERROR;
        }
 
-       if (__check_disable_filter_exist(filter) == false) {
-               pkgmgrinfo_appinfo_filter_add_bool(filter,
-                               PMINFO_APPINFO_PROP_APP_DISABLE, false);
-       }
-
        ret = _appinfo_get_applications(uid, uid, locale, filter,
                        flag | PMINFO_APPINFO_GET_BASICINFO, list);
        if (ret == PMINFO_R_OK && uid != GLOBAL_USER)
@@ -1337,6 +1310,12 @@ API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
                return PMINFO_R_ERROR;
        }
 
+       if (pkgmgrinfo_appinfo_filter_add_bool(filter,
+                       PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
+               pkgmgrinfo_appinfo_filter_destroy(filter);
+               return PMINFO_R_ERROR;
+       }
+
        comp_str = __appcomponent_str(component);
 
        if (comp_str) {
@@ -1379,7 +1358,13 @@ API int pkgmgrinfo_appinfo_get_usr_installed_list_full(
                return PMINFO_R_ERROR;
 
        if (pkgmgrinfo_appinfo_filter_add_bool(filter,
-                               PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
+                       PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
+               pkgmgrinfo_appinfo_filter_destroy(filter);
+               return PMINFO_R_ERROR;
+       }
+
+       if (pkgmgrinfo_appinfo_filter_add_bool(filter,
+                       PMINFO_APPINFO_PROP_PKG_DISABLE, false)) {
                pkgmgrinfo_appinfo_filter_destroy(filter);
                return PMINFO_R_ERROR;
        }
@@ -1421,6 +1406,18 @@ API int pkgmgrinfo_appinfo_get_usr_installed_list(
        if (ret != PMINFO_R_OK)
                return ret;
 
+       if (pkgmgrinfo_appinfo_filter_add_bool(filter,
+                       PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
+               pkgmgrinfo_appinfo_filter_destroy(filter);
+               return PMINFO_R_ERROR;
+       }
+
+       if (pkgmgrinfo_appinfo_filter_add_bool(filter,
+                       PMINFO_APPINFO_PROP_PKG_DISABLE, false)) {
+               pkgmgrinfo_appinfo_filter_destroy(filter);
+               return PMINFO_R_ERROR;
+       }
+
        ret = _appinfo_get_filtered_foreach_appinfo(uid, filter,
                        PMINFO_APPINFO_GET_ALL, app_func, user_data);
 
@@ -1795,35 +1792,6 @@ API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
        return PMINFO_R_OK;
 }
 
-API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h handle, char **icon)
-{
-       char *val;
-       icon_x *ptr;
-       GList *tmp;
-       pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
-
-       retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
-       retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
-
-       if (info->app_info == NULL)
-               return PMINFO_R_ERROR;
-
-       for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
-               ptr = (icon_x *)tmp->data;
-               if (ptr == NULL || ptr->section == NULL)
-                       continue;
-
-               val = (char *)ptr->section;
-               if (val && strcmp(val, "setting") == 0) {
-                       *icon = (char *)ptr->text;
-                       return PMINFO_R_OK;
-               }
-       }
-
-       return PMINFO_R_ERROR;
-}
-
-
 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
 {
        char *val;
@@ -2398,32 +2366,6 @@ API int pkgmgrinfo_appinfo_get_support_mode(pkgmgrinfo_appinfo_h  handle, int *s
        return PMINFO_R_OK;
 }
 
-API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
-                       pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
-{
-       retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
-       retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
-       int ret = -1;
-       permission_x *ptr;
-       GList *tmp;
-       pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
-
-       if (info->app_info == NULL)
-               return PMINFO_R_ERROR;
-
-       for (tmp = info->app_info->permission; tmp; tmp = tmp->next) {
-               ptr = (permission_x *)tmp->data;
-               if (ptr == NULL)
-                       continue;
-               if (ptr->value) {
-                       ret = permission_func(ptr->value, user_data);
-                       if (ret < 0)
-                               break;
-               }
-       }
-       return PMINFO_R_OK;
-}
-
 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
                        pkgmgrinfo_app_category_list_cb category_func, void *user_data)
 {
@@ -2474,6 +2416,73 @@ API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
        return PMINFO_R_OK;
 }
 
+API int pkgmgrinfo_appinfo_usr_foreach_appcontrol_privileges(const char *appid,
+               const char *operation, const char *uri, const char *mime,
+               pkgmgrinfo_pkg_privilege_list_cb privilege_func,
+               void *user_data, uid_t uid)
+{
+       static const char query_raw[] =
+               "SELECT privilege FROM package_app_app_control_privilege "
+               "WHERE app_id=%Q AND app_control=%Q";
+       int ret;
+       char *query;
+       sqlite3_stmt *stmt;
+       char *privilege;
+       char app_control[BUFSIZE];
+
+       if (appid == NULL || operation == NULL || privilege_func == NULL) {
+               LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
+       }
+
+       snprintf(app_control, sizeof(app_control), "%s|%s|%s", operation,
+                       uri ? uri : "NULL", mime ? mime : "NULL");
+
+       ret = __open_manifest_db(uid, true);
+       if (ret != SQLITE_OK) {
+               LOGE("open db failed");
+               return PMINFO_R_ERROR;
+       }
+
+       query = sqlite3_mprintf(query_raw, appid, app_control);
+       if (query == NULL) {
+               LOGE("out of memory");
+               __close_manifest_db();
+               return PMINFO_R_ERROR;
+       }
+
+       ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query),
+                       &stmt, NULL);
+       sqlite3_free(query);
+       if (ret != PMINFO_R_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(GET_DB(manifest_db)));
+               __close_manifest_db();
+               return PMINFO_R_ERROR;
+       }
+
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               privilege = (char *)sqlite3_column_text(stmt, 0);
+               ret = privilege_func(privilege, user_data);
+               if (ret < 0)
+                       break;
+       }
+
+       sqlite3_finalize(stmt);
+       __close_manifest_db();
+
+       return PMINFO_R_OK;
+}
+
+API int pkgmgrinfo_appinfo_foreach_appcontrol_privileges(const char *appid,
+               const char *operation, const char *uri, const char *mime,
+               pkgmgrinfo_pkg_privilege_list_cb privilege_func,
+               void *user_data)
+{
+       return pkgmgrinfo_appinfo_usr_foreach_appcontrol_privileges(appid,
+                       operation, uri, mime, privilege_func, user_data,
+                       _getuid());
+}
+
 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
                        pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
 {
@@ -3091,6 +3100,26 @@ API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle,
                return PMINFO_R_ERROR;
        }
 
+       if (__check_disable_filter_exist(
+                       handle, E_APPINFO_DISABLE_TYPE_APP) == false) {
+               if (pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_APP_DISABLE, false)) {
+                       g_hash_table_destroy(list);
+                       free(locale);
+                       return PMINFO_R_ERROR;
+               }
+       }
+
+       if (__check_disable_filter_exist(
+                       handle, E_APPINFO_DISABLE_TYPE_PKG) == false) {
+               if (pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_PKG_DISABLE, false)) {
+                       g_hash_table_destroy(list);
+                       free(locale);
+                       return PMINFO_R_ERROR;
+               }
+       }
+
        ret = _appinfo_get_applications(uid, uid, locale, handle, 0, list);
        if (ret == PMINFO_R_OK && uid != GLOBAL_USER)
                ret = _appinfo_get_applications(GLOBAL_USER, uid, locale,
@@ -3123,6 +3152,20 @@ API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(
                return PMINFO_R_EINVAL;
        }
 
+       if (__check_disable_filter_exist(
+                       handle, E_APPINFO_DISABLE_TYPE_APP) == false) {
+               if (pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_APP_DISABLE, false))
+                       return PMINFO_R_ERROR;
+       }
+
+       if (__check_disable_filter_exist(
+                       handle, E_APPINFO_DISABLE_TYPE_PKG) == false) {
+               if (pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_PKG_DISABLE, false))
+                       return PMINFO_R_ERROR;
+       }
+
        return _appinfo_get_filtered_foreach_appinfo(uid, handle, PMINFO_APPINFO_GET_ALL, app_cb,
                        user_data);
 }
@@ -3183,6 +3226,15 @@ API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
                return PMINFO_R_EINVAL;
        }
 
+       pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
+       if (pkgmgrinfo_appinfo_filter_add_bool(filter,
+                       PMINFO_APPINFO_PROP_APP_DISABLE, false))
+               return PMINFO_R_ERROR;
+
+       if (pkgmgrinfo_appinfo_filter_add_bool(filter,
+                       PMINFO_APPINFO_PROP_PKG_DISABLE, false))
+               return PMINFO_R_ERROR;
+
        return _appinfo_get_filtered_foreach_appinfo(uid, handle, PMINFO_APPINFO_GET_ALL, app_cb,
                        user_data);
 }