snprintf(): use sizeof() for size factor
[platform/framework/web/download-provider.git] / provider / download-provider-security.c
index 0128acc..0b72bec 100644 (file)
@@ -40,6 +40,7 @@
 #define MAX_ARRAY_LEN 1024
 #define SECURITY_ATTRIBUTES_PATH "/proc/%d/attr/current"
 #define TEMP_DIR "/tmp/"
+#define LEGACY_USER_APP "/opt/usr/apps/"
 
 static int dp_is_exist_dir(const char *dirpath)
 {
@@ -59,27 +60,28 @@ static char *_dp_get_pkg_id(dp_credential cred)
 {
        char *app_id = NULL;
        char *pkg_id = NULL;
-       app_context_h context;
+       app_info_h app_info = NULL;
 
        if (app_manager_get_app_id(cred.pid, &app_id) != APP_MANAGER_ERROR_NONE) {
                TRACE_ERROR("Failed to get application ID");
                return NULL;
        }
 
-       if (app_manager_get_app_context(app_id, &context) != APP_MANAGER_ERROR_NONE) {
-               TRACE_ERROR("Failed to get application ID");
+       if (app_info_create(app_id, &app_info) != APP_MANAGER_ERROR_NONE) {
+               TRACE_ERROR("Failed to create app_info");
                free(app_id);
                return NULL;
        }
 
-       if (app_context_get_package_id(context, &pkg_id) != APP_MANAGER_ERROR_NONE) {
-               TRACE_ERROR("Failed to get application ID");
-               app_context_destroy(context);
+       if (app_info_get_package(app_info, &pkg_id) != APP_MANAGER_ERROR_NONE) {
+               TRACE_ERROR("Failed to get package ID");
+               app_info_destroy(app_info);
                free(app_id);
+               free(pkg_id);
                return NULL;
        }
 
-       app_context_destroy(context);
+       app_info_destroy(app_info);
        free(app_id);
 
        return pkg_id;
@@ -144,7 +146,7 @@ int dp_is_valid_dir(dp_credential cred, const char *dirpath)
                return DP_ERROR_INVALID_DESTINATION;
        }
 
-       strncpy(resolved_path, res, PATH_MAX - 1);
+       strncpy(resolved_path, res, sizeof(resolved_path) - 1);
        free(res);
 
        end = strlen(resolved_path) - 1;
@@ -168,7 +170,7 @@ int dp_is_valid_dir(dp_credential cred, const char *dirpath)
        // Check whether directory is default directory or not.
        temp = tzplatform_getenv(TZ_USER_DOWNLOADS);
        if (temp) {
-               snprintf(default_storage, PATH_MAX - 1, "%s/", temp);
+               snprintf(default_storage, sizeof(default_storage) - 1, "%s/", temp);
                if (strncmp(resolved_path, default_storage,
                                        strlen(default_storage)) == 0)
                                return DP_ERROR_NONE;
@@ -178,7 +180,7 @@ int dp_is_valid_dir(dp_credential cred, const char *dirpath)
        // Check permission: media storage
        temp = tzplatform_getenv(TZ_USER_CONTENT);
        if (temp) {
-               snprintf(media_storage, PATH_MAX - 1, "%s/", temp);
+               snprintf(media_storage, sizeof(media_storage) - 1, "%s/", temp);
                if (strncmp(resolved_path, media_storage,
                                        strlen(media_storage)) == 0) {
                        if (_dp_check_dir_permission(cred, MEDIA_STORAGE_PRIVILEGE) < 0) {
@@ -194,7 +196,7 @@ int dp_is_valid_dir(dp_credential cred, const char *dirpath)
        // Check permission: external storage
        temp = tzplatform_getenv(TZ_SYS_STORAGE);
        if (temp) {
-               snprintf(external_storage, PATH_MAX - 1, "%s/", temp);
+               snprintf(external_storage, sizeof(external_storage) - 1, "%s/", temp);
                if (strncmp(resolved_path, external_storage,
                                        strlen(external_storage)) == 0) {
                        if (_dp_check_dir_permission(cred, EXTERNAL_STORAGE_PRIVILEGE) < 0) {
@@ -208,9 +210,16 @@ int dp_is_valid_dir(dp_credential cred, const char *dirpath)
        }
 
        // Check permission: private storage
-       temp = tzplatform_getenv(TZ_USER_APP);
-       if (temp) {
-               snprintf(apps_storage, PATH_MAX - 1, "%s/", temp);
+       if (strncmp(resolved_path, LEGACY_USER_APP, strlen(LEGACY_USER_APP)) == 0) {
+               // Some applications use a legacy app path.
+               snprintf(apps_storage, sizeof(apps_storage) - 1, "%s", LEGACY_USER_APP);
+       } else {
+               temp = tzplatform_getenv(TZ_USER_APP);
+               snprintf(apps_storage, sizeof(apps_storage) - 1, "%s/", temp);
+               temp = NULL;
+       }
+
+       if (strlen(apps_storage) > 0) {
                if (strncmp(resolved_path, apps_storage,
                                        strlen(apps_storage)) == 0) {
                        pkg_id = _dp_get_pkg_id(cred);
@@ -227,6 +236,14 @@ int dp_is_valid_dir(dp_credential cred, const char *dirpath)
                        free(pkg_id);
                        return DP_ERROR_NONE;
                }
+       }
+
+       // Check whether directory is shared directory or not.
+       temp = tzplatform_getenv(TZ_USER_SHARE);
+       if (temp) {
+               if (strncmp(resolved_path, temp,
+                                       strlen(temp)) == 0)
+                       return DP_ERROR_NONE;
                temp = NULL;
        }