fix: fix memory corruption
[platform/core/api/system-settings.git] / src / sst_misc.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "sst_misc.h"
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <vconf.h>
22 #include <app_manager.h>
23 #include <package_manager.h>
24 #include "sst.h"
25 #include "sst_vconf.h"
26
27 bool sst_misc_file_exists(const char *path)
28 {
29         if (0 != access(path, R_OK)) {
30                 ERR("access(%s) Fail(%d)", path, errno);
31                 return false;
32         }
33         return true;
34 }
35
36 int sst_misc_get_wifi_QS_noti(sst_interface *iface, bool *value)
37 {
38         int vconf_value = 0;
39         if (vconf_get_int(iface->vconf_key, &vconf_value)) {
40                 ERR("vconf_get_int(%s) Fail", iface->vconf_key);
41                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
42         }
43
44         bool result;
45         result = (vconf_value == VCONFKEY_WIFI_QS_ENABLE) ? true : false;
46
47         *value = result;
48         return SYSTEM_SETTINGS_ERROR_NONE;
49 }
50
51 //It is related with Advertisements
52 #define DEFAULT_ADS_ID "00000000-0000-0000-0000-000000000000"
53 int sst_misc_get_ads_id(sst_interface *iface, char **value)
54 {
55         int result = 0;
56         const char *key = VCONFKEY_SETAPPL_AD_ID_OPT_OUT;
57         if (vconf_get_int(key, &result)) {
58                 ERR("vconf_get_int(%s) Fail", key);
59                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
60         }
61
62         if (result == 1) {
63                 *value = strdup(DEFAULT_ADS_ID);
64                 return SYSTEM_SETTINGS_ERROR_NONE;
65         }
66
67         char *vconf_value = NULL;
68         if (sst_vconf_get_str_ally(iface->vconf_key, &vconf_value)) {
69                 ERR("sst_vconf_get_str_ally(%s) Fail", iface->vconf_key);
70                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
71         }
72
73         *value = vconf_value;
74         return SYSTEM_SETTINGS_ERROR_NONE;
75 }
76
77 int sst_misc_set_ads_id(sst_interface *iface, const char *value)
78 {
79         if (vconf_set_str(iface->vconf_key, value)) {
80                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
81                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
82         }
83
84         return SYSTEM_SETTINGS_ERROR_NONE;
85 }
86
87 int sst_misc_get_uds_state(sst_interface *iface, int *value)
88 {
89         int int_val;
90         char *str_val = NULL;
91
92         if (vconf_get_int(iface->vconf_key, &int_val))
93                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
94
95         if (int_val == SYSTEM_SETTINGS_UDS_ON) {
96                 const char *vconf_key = VCONFKEY_SETAPPL_UDSM_PKGID_LIST;
97                 int ret = sst_vconf_get_str_ally(vconf_key, &str_val);
98                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
99                         ERR("sst_vconf_get_str_ally(%s) Fail(%d)", vconf_key, ret);
100                         return ret;
101                 }
102
103                 if (SST_EQUAL != strcmp(str_val, "NONE")) {
104                         char *app_id = NULL;
105                         char *package_id = NULL;
106                         pid_t pid = getpid();
107
108                         int ret = app_manager_get_app_id(pid, &app_id);
109                         if (ret != APP_MANAGER_ERROR_NONE) {
110                                 SECURE_ERR("app_manager_get_app_id(%d) Fail(%d)", pid, ret);
111                                 free(str_val);
112                                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
113                         }
114
115                         ret = package_manager_get_package_id_by_app_id(app_id, &package_id);
116                         if (PACKAGE_MANAGER_ERROR_NONE != ret) {
117                                 SECURE_ERR("package_manager_get_package_id_by_app_id(%s) Fail(%d)", app_id, ret);
118                                 free(app_id);
119                                 free(str_val);
120                                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
121                         }
122                         free(app_id);
123
124                         if (package_id && strstr(str_val, package_id))
125                                 int_val = SYSTEM_SETTINGS_UDS_ON_WHITELISTED;
126                         free(package_id);
127                 }
128                 free(str_val);
129         }
130
131         *value = int_val;
132
133         return SYSTEM_SETTINGS_ERROR_NONE;
134 }
135