Change file name
[platform/core/system/setup-adaptor.git] / src / setup_system.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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 <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <json.h>
23 #include "sa_common.h"
24 #include "sa_types.h"
25 #include "sa_systemdata.h"
26
27
28 static int json_get_int_from_obj(json_object *inputObj, char *key)
29 {
30         struct json_object *bodyObj;
31         enum json_type type;
32         int ret = 0;
33
34         type = json_object_get_type(inputObj);
35
36         if (type == json_type_object) {
37                 if (json_object_object_get_ex(inputObj, key, &bodyObj)) {
38                         ret = json_object_get_int(bodyObj);
39                 } 
40         }
41         return ret;
42 }
43
44 static int json_get_boolean_from_obj(json_object *inputObj, char *key)
45 {
46         struct json_object *bodyObj;
47         enum json_type type;
48         int ret = 0;
49
50         type = json_object_get_type(inputObj);
51
52         if (type == json_type_object) {
53                 if (json_object_object_get_ex(inputObj, key, &bodyObj)) {
54                         ret = json_object_get_boolean(bodyObj);
55                 } 
56         }
57         return ret;
58 }
59
60 static char *json_get_string_from_obj(json_object *inputObj, char *key)
61 {
62         const char *buf = NULL;
63         char *ret_buf = NULL;
64         json_object *elementObj = NULL;
65         int i = 0, j = 0;
66
67         if (json_object_object_get_ex(inputObj, key, &elementObj)) {
68                 buf = json_object_to_json_string_ext(elementObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
69                 ret_buf = malloc(strlen(buf) - 1);
70                 if (ret_buf != NULL) {
71                         memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude
72                         for (i = 1, j = 0; i < strlen(buf) - 1; i++) {  // "\ <-- exclude
73                                 if (buf[i] == '\\')
74                                         continue;
75                                 ret_buf[j++] = buf[i];
76                         }
77                 } else {
78                         return NULL;
79                 }
80         } else {
81                 return NULL;
82         }
83
84         _D("string object [%s]", ret_buf);
85         return ret_buf;
86 }
87 static int __parse_network_proxy(json_object *inputObj, sa_network_s *network)
88 {
89         char *httpProxyHost = NULL;
90         int httpProxyPort;
91
92         if (inputObj == NULL || network == NULL) {
93                 _E("__parse_network_proxy input error");
94                 return -1;
95         }
96
97         //httpProxyHost
98         httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST);
99         if (httpProxyHost != NULL) {
100                 memcpy(network->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(network->httpProxyHost)-1));
101                 free(httpProxyHost);
102                 httpProxyHost = NULL;
103         }
104
105         //httpProxyPort
106         httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT);
107         
108 }
109
110 static int __parse_network_static_info(json_object *inputObj, sa_network_static_s *staticInfo)
111 {
112         char *ipAddress = NULL;
113         char *netmask = NULL;
114         char *defaultGateway = NULL;
115         char *primaryDnsServer = NULL;
116         char *secondaryDnsServer = NULL;
117
118         if (inputObj == NULL || staticInfo == NULL) {
119                 _E("__parse_network_static_info input error");
120                 return -1;
121         }
122
123         //ipAddress
124         ipAddress = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_IPADDRESS);
125         if (ipAddress != NULL) {
126                 memcpy(staticInfo->ipAddress, ipAddress, MIN(strlen(ipAddress), sizeof(staticInfo->ipAddress)-1));
127                 free(ipAddress);
128                 ipAddress = NULL;
129         }
130
131         //netmask
132         netmask = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_NETMASK);
133         if (netmask != NULL) {
134                 memcpy(staticInfo->netmask, netmask, MIN(strlen(netmask), sizeof(staticInfo->netmask)-1));
135                 free(netmask);
136                 netmask = NULL;
137         }
138
139         //defaultGateway
140         defaultGateway = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DEFAULTGATEWAY);
141         if (defaultGateway != NULL) {
142                 memcpy(staticInfo->defaultGateway, defaultGateway, MIN(strlen(defaultGateway), sizeof(staticInfo->defaultGateway)-1));
143                 free(defaultGateway);
144                 defaultGateway = NULL;
145         }
146         
147         //primaryDnsServer
148         primaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_PRIMARYDNSSERVER);
149         if (primaryDnsServer != NULL) {
150                 memcpy(staticInfo->primaryDnsServer, primaryDnsServer, MIN(strlen(primaryDnsServer), sizeof(staticInfo->primaryDnsServer)-1));
151                 free(primaryDnsServer);
152                 primaryDnsServer = NULL;
153         }
154         
155         //secondaryDnsServer
156         secondaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_SECONDARYDNSSERVER);
157         if (secondaryDnsServer != NULL) {
158                 memcpy(staticInfo->secondaryDnsServer, secondaryDnsServer, MIN(strlen(secondaryDnsServer), sizeof(staticInfo->secondaryDnsServer)-1));
159                 free(secondaryDnsServer);
160                 secondaryDnsServer = NULL;
161         }
162
163         return 0;
164 }
165
166 static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth)
167 {
168         int ret = 0;
169
170         if (inputObj == NULL || eth == NULL) {
171                 _E("__parse_network_eth input error");
172                 return -1;
173         }
174
175         // enabled
176         eth->enabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_ENABLED);
177
178         // dhcpEnabled
179         eth->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED);
180         if (eth->dhcpEnabled == FALSE) {
181                 eth->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
182                 if (eth->staticInfo != NULL) {
183                         ret = __parse_network_static_info(inputObj, eth->staticInfo);
184                 } else {
185                         ret = -1;
186                 }
187         } else {
188                 _D("dhcp is true");
189         }
190
191         return ret;
192 }
193
194 static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi)
195 {
196         char *ssid = NULL;
197         char *password = NULL;
198         int ret = 0;
199
200         if (inputObj == NULL || wifi == NULL) {
201                 _E("__parse_network_wifi input error");
202                 return -1;
203         }
204         // enabled
205         wifi->enabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_ENABLED);
206
207         // ssid
208         ssid = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_SSID);
209         if (ssid != NULL) {
210                 memcpy(wifi->ssid, ssid, MIN(strlen(ssid), sizeof(wifi->ssid)-1));
211                 free(ssid);
212                 ssid = NULL;
213         } else {
214                 ret = -1;
215         }
216
217         // password
218         password = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_PASSWORD);
219         if (password != NULL) {
220                 memcpy(wifi->password, password, MIN(strlen(password), sizeof(wifi->password)-1));
221                 free(password);
222                 password = NULL;
223         } else {
224                 ret = -1;
225         }
226
227         // dhcpEnabled
228         wifi->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED);
229         if (wifi->dhcpEnabled == FALSE) {
230                 wifi->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
231                 if (wifi->staticInfo != NULL) {
232                         ret = __parse_network_static_info(inputObj, wifi->staticInfo);
233                 } else {
234                         ret = -1;
235                 }
236         } else {
237                 _D("dhcp is true");
238         }
239
240         return ret;
241 }
242
243 static int __parse_network_data(json_object *inputObj, sa_network_s *network)
244 {
245         struct json_object *wifiObj = NULL;
246         struct json_object *ethernetObj = NULL;
247         int wifiRet = 0, ethRet = 0;
248
249         if (inputObj == NULL || network == NULL) {
250                 _E("__parse_network_data input error");
251                 return -1;
252         }
253
254         //parse wifi
255         if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_WIFI, &wifiObj)) {
256                 if (json_object_get_type(wifiObj) == json_type_object) {
257                         // malloc
258                         network->wifi = (sa_wifi_s *)malloc(sizeof(sa_wifi_s));
259                         if (network->wifi != NULL) {
260                                 wifiRet = __parse_network_wifi(wifiObj, network->wifi);
261                         } else {
262                                 _E("network->wifi malloc fail");
263                         }
264                 }
265         }
266
267         //parse eth
268         if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_ETHERNET, &ethernetObj)) {
269                 if (json_object_get_type(ethernetObj) == json_type_object) {
270                         // malloc
271                         network->eth = (sa_eth_s *)malloc(sizeof(sa_eth_s));
272                         if (network->eth != NULL) {
273                                 ethRet = __parse_network_eth(ethernetObj, network->eth);
274                         } else {
275                                 _E("network->eth malloc fail"); 
276                         }
277                 }
278         }
279
280         // parse proxy
281         __parse_network_proxy(inputObj, network);
282
283         // if both of network interfaces are failed, it would return -1
284         if (wifiRet != 0 && ethRet != 0) {
285                 _E("__parse_network_data fail");        
286                 return -1;
287         }
288         
289         return 0;
290 }
291
292
293 static int __parse_system_data(json_object *inputObj, sa_systemdata_s *system)
294 {
295         if (inputObj == NULL || system == NULL) {
296                 _E("__parse_system_data input error");
297                 return -1;
298         }
299
300         return 0;
301 }
302
303
304 static int __parse_version(json_object *inputObj, sa_config_s *setupConfig)
305 {
306         char *version = NULL;
307
308         if (inputObj == NULL || setupConfig == NULL) {
309                 _E("__parse_version input error");
310                 return -1;
311         }
312
313         version = json_get_string_from_obj(inputObj, SA_CONFIG_VERSION);
314
315         if (version != NULL) {
316                 memcpy(setupConfig->version, version, MIN(strlen(version), sizeof(setupConfig->version)-1));
317                 free(version);
318                 version = NULL;
319         }
320
321         return 0;
322 }
323
324 static int __parse_config(char *file, sa_config_s *setupConfig)
325 {
326         struct json_object *configObj = NULL;
327         struct json_object *networkObj = NULL;
328         struct json_object *systemObj = NULL;
329         char *jsonData = NULL;
330         int readLen = 0;
331         int fd = 0;
332         int len = 0;
333         int ret = 0;
334
335         fd = open(file, O_RDONLY);
336         if (fd) {
337                 len = lseek(fd, 0L, SEEK_END);
338
339                 if (len > 0) {
340                         lseek(fd, 0L, SEEK_SET);
341                         jsonData = (char *)malloc(len + 1);
342                         if (jsonData != NULL) {
343                                 memset(jsonData, 0x00, len + 1);
344                                 readLen = read(fd, jsonData, len);
345                                 _D("JSON full data[%s]", jsonData);
346
347                                 configObj = json_tokener_parse(jsonData);
348
349                                 if (configObj != NULL) {
350                                         // parse version
351                                         ret = __parse_version(configObj, setupConfig);
352
353                                         // parse system data
354                                         if (ret == 0) {
355                                                 if (json_object_object_get_ex(configObj, SA_CONFIG_SYSTEMDATA, &systemObj)) {
356                                                         if (json_object_get_type(systemObj) == json_type_object) {
357                                                                 // malloc
358                                                                 setupConfig->systemData = (sa_systemdata_s *)malloc(sizeof(sa_systemdata_s));
359                                                                 ret = __parse_system_data(systemObj, setupConfig->systemData);
360                                                         }
361                                                 }
362                                         }
363
364                                         // parse network data
365                                         if (ret == 0) {
366                                                 if (json_object_object_get_ex(configObj, SA_CONFIG_NETWORKDATA, &networkObj)) {
367                                                         if (json_object_get_type(networkObj) == json_type_object) {
368                                                                 // malloc
369                                                                 setupConfig->networkData = (sa_network_s *)malloc(sizeof(sa_network_s));
370                                                                 ret = __parse_network_data(networkObj, setupConfig->networkData);
371                                                         }
372                                                 }                                               
373                                         }
374                                 } else {
375                                         ret = -1;
376                                         _E("ConfigObj is not existed");
377                                 }
378                                 
379                                 json_object_put(configObj);
380                                 free(jsonData);
381                         } else {
382                                 ret = -1;
383                                 _E("memory allocation fail for jsonData");
384                         }
385                 }
386
387                 close(fd);
388         } else {
389                 ret = -1;
390                 _E("config file can't be opened");
391         }
392
393         return ret;
394 }
395
396 int sa_systemdata_get_config_info(char *file, sa_config_s *config)
397 {
398         sa_error_e ret = SA_ERROR_NONE;
399
400         // check file
401         if (access(file, 0) != 0) {
402                 _D("there is no config file");
403                 return SA_ERROR_UNKNOWN;
404         }
405         // parsing and fill into struct
406         ret = __parse_config(file, config);
407         if (ret != 0) {
408                 _D("ret:DOCKER_STATUS_UNKNOWN --> exit for user recovery");
409                 return SA_ERROR_UNKNOWN;
410         }
411
412         return ret;
413 }