Add proxy setting function 98/167998/1
authornshero.lee <nshero.lee@samsung.com>
Tue, 23 Jan 2018 09:25:23 +0000 (18:25 +0900)
committernshero.lee <nshero.lee@samsung.com>
Tue, 23 Jan 2018 09:25:23 +0000 (18:25 +0900)
In order to set the proxy value in specific file to read from sytemd.
It has to parse the proxy and save it into /etc/dockzen/environment
the file is reading when systemd is started for dockzen-launcher services
It would be effect to the docker too.

Change-Id: Icc59eacd4aa08b02e6d557e95d8f14dbb7a86765
Signed-off-by: nshero.lee <nshero.lee@samsung.com>
packaging/setup-adaptor.spec
src/input_file.c
src/input_file.h
src/sa_types.h
src/setup_system.c
test/config.json

index a4aa32dbcbaa4a88f88280d7a4576b220bd991d6..3b758f069c3757bca098e5ede3e6c609d74965f4 100644 (file)
@@ -1,6 +1,6 @@
 Name:       setup-adaptor
 Summary:    Provide network and system data setting daemon
-Version:    0.3.0
+Version:    0.4.0
 Release:    0
 Group:      System & System Tools
 ExclusiveArch: armv7l
index 9238a7730416f136f7e9a9c38d2939ac81dfd3b8..175c2f79817c69e7645912621337e68f8db0b110 100644 (file)
 #define CONFIG_FILE "/opt/etc/setup-adaptor/config.json"
 #define CONFIG_FOLDER "/opt/etc/setup-adaptor"
 #define CONFIG_NAME "config.json"
+
+#define ENVIRONMENT_PROXY_FILE "/etc/dockzen/environment"
+
+#define FLAG_FILE_SYSTEM "/opt/etc/setup-adaptor/system_executed"
+#define FLAG_FILE_ETH "/opt/etc/setup-adaptor/ethernet_executed"
+#define FLAG_FILE_WIFI "/opt/etc/setup-adaptor/wifi_executed"
 #define EVENT_NAME_MAX 256
 #define EVENT_SIZE     (sizeof(struct inotify_event))
 #define EVENT_BUF_LEN  (512 * (EVENT_SIZE + EVENT_NAME_MAX))
@@ -249,10 +255,8 @@ static void print_network_config(sa_network_s * network)
 static void print_system_config(sa_system_s * systemData)
 {
        if (systemData != NULL) {
-               if (systemData->proxy != NULL) {
-                       _D("systemData::httpProxyHost [%s]", systemData->proxy->httpProxyHost);
-                       _D("systemData::HttpProxyPort [%d]", systemData->proxy->httpProxyPort);
-               }
+               if (systemData->proxy != NULL)
+                       _D("systemData::httpProxy [%s]", systemData->proxy->httpProxy);
        }
 }
 
@@ -443,22 +447,20 @@ static int __parse_network_data(json_object * inputObj, sa_config_s * setupConfi
 static int __parse_system_data(json_object * inputObj, sa_config_s * setupConfig)
 {
        int ret = 0;
-       char *httpProxyHost = NULL;
+       char *httpProxy = NULL;
+
        if (inputObj == NULL) {
                _D("__parse_system_data input error");
                return -1;
        }
-       //httpProxyHost
-       httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST);
-       if (httpProxyHost != NULL) {
+       //httpProxy
+       httpProxy = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXY);
+       if (httpProxy != NULL) {
                setupConfig->systemData = (sa_system_s *) malloc(sizeof(sa_system_s));
                if (setupConfig->systemData != NULL) {
                        setupConfig->systemData->proxy = (sa_proxy_s *) malloc(sizeof(sa_proxy_s));
                        if (setupConfig->systemData->proxy != NULL) {
-                               memcpy(setupConfig->systemData->proxy->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(setupConfig->systemData->proxy->httpProxyHost) - 1));
-                               //httpProxyPort
-                               setupConfig->systemData->proxy->httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT);
-
+                               memcpy(setupConfig->systemData->proxy->httpProxy, httpProxy, MIN(strlen(httpProxy), sizeof(setupConfig->systemData->proxy->httpProxy) - 1));
                        } else {
                                ret = -1;
                                _D("malloc fail setupConfig->systemData->proxy");
@@ -467,9 +469,11 @@ static int __parse_system_data(json_object * inputObj, sa_config_s * setupConfig
                        ret = -1;
                        _D("malloc fail setupConfig->systemData");
                }
+       }
 
-               free(httpProxyHost);
-               httpProxyHost = NULL;
+       if (httpProxy != NULL) {
+               free(httpProxy);
+               httpProxy = NULL;
        }
 
        return ret;
@@ -647,10 +651,6 @@ void sa_inputfile_release_resource(sa_config_s * config)
        }
 }
 
-#define FLAG_FILE_SYSTEM "/opt/etc/setup-adaptor/system_executed"
-#define FLAG_FILE_ETH "/opt/etc/setup-adaptor/ethernet_executed"
-#define FLAG_FILE_WIFI "/opt/etc/setup-adaptor/wifi_executed"
-
 void sa_inputfile_remove(void)
 {
        char buff[256];
@@ -735,3 +735,42 @@ gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type)
        else
                return FALSE;
 }
+
+sa_error_e sa_inputfile_set_proxy_env(sa_proxy_s * proxy)
+{
+       int fd = 0;
+       int len = 0;
+       sa_error_e ret = SA_ERROR_NONE;
+       char value[256] = {0, };
+       char writeBuff[512] = {0, };
+
+       if (proxy == NULL) {
+               _E("input parameter is null");
+               return SA_ERROR_INVALID_PARAMETER;
+       }
+
+       // HTTP_PROXY
+       _D("Proxy[%s]", proxy->httpProxy);
+
+       // make string
+       if (strlen(proxy->httpProxy) > 0 && strlen(proxy->httpProxy) < (sizeof(proxy->httpProxy)-1))
+               snprintf(writeBuff, strlen(SA_CONFIG_SERVICE_HTTP_PROXY) + strlen(SA_CONFIG_SERVICE_OPERATOR) + strlen(proxy->httpProxy) + 1, "%s%s%s", SA_CONFIG_SERVICE_HTTP_PROXY, SA_CONFIG_SERVICE_OPERATOR, proxy->httpProxy);
+       else {
+               ret = SA_ERROR_NOT_AVAILABLE;
+               _E("Not available to set due to buffer overflow");
+       }
+
+       //write data into file
+       if (ret == SA_ERROR_NONE) {
+               fd = open(ENVIRONMENT_PROXY_FILE, O_WRONLY | O_CREAT, 0644);
+               if (fd > 0) {
+                       write(fd, writeBuff, strlen(writeBuff));
+                       close(fd);
+               } else {
+                       ret = SA_ERROR_NOT_AVAILABLE;
+                       _E("Cannot open file");
+               }
+       }
+
+       return ret;
+}
index 501c9f3addb84fdd6682bd525ebd2d08206b066b..0e979d701e93dd04432f2ddd825a73e063710cad 100644 (file)
@@ -82,4 +82,11 @@ void sa_inputfile_set_completion_flag(sa_file_config_e config_type);
  */
 gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type);
 
+/**
+ * @fn        sa_error_e sa_inputfile_set_proxy_env
+ * @brief     This function to set proxy value in environment file
+ * @param     sa_proxy_s for proxy information
+ * @return    sa_file_state_e  return of state
+ */
+sa_error_e sa_inputfile_set_proxy_env(sa_proxy_s * proxy);
 #endif                                                 /* __INPUT_FILE_H__ */
index 4c0537144824ad0b40c9fd877dc904788639bd12..f42e20d175f499272e9bc7d176f9b197064099a2 100644 (file)
 #define SA_CONFIG_NETWORKDATA_PRIMARYDNSSERVER         "primaryDnsServer"
 #define SA_CONFIG_NETWORKDATA_SECONDARYDNSSERVER    "secondaryDnsServer"
 
-#define SA_CONFIG_NETWORKDATA_HTTPPROXYHOST                    "httpProxyHost"
-#define SA_CONFIG_NETWORKDATA_HTTPPROXYPORT                    "httpProxyPort"
+#define SA_CONFIG_NETWORKDATA_HTTPPROXY                                "httpProxy"
+
+#define SA_CONFIG_SERVICE_ENVIRONMENTFILE                      "EnvironmentFile"
+#define SA_CONFIG_SERVICE_HTTP_PROXY                           "HTTP_PROXY"
+#define SA_CONFIG_SERVICE_OPERATOR                                     "="
+
 
 /**
  * @brief  This enum contains setup-adaptor error information
@@ -157,12 +161,11 @@ typedef struct {
  * @struct sa_proxy_s
  * @brief  This struct contains proxy information
  *
- * The sa_network_s struct encapsulate httpproxyHost and httpProxyPort information in the one data
+ * The sa_network_s struct encapsulate httpProxy information in the one data
  *
  */
 typedef struct {
-       char httpProxyHost[255 + 1];
-       int httpProxyPort;
+       char httpProxy[255 + 1];
 } sa_proxy_s;
 
 /**
index 7d09c86a3a6891344e765485b2fbdbb3ab3fc9f6..4c51357ad21361536ee26a80b31b54128d585a87 100644 (file)
 #include <stdio.h>
 #include "sa_common.h"
 #include "sa_types.h"
+#include "input_file.h"
 #include "setup_system.h"
 
-static sa_error_e system_set_proxy(sa_proxy_s * info)
-{
-       return SA_ERROR_NOT_SUPPORTED;
-}
-
 static sa_error_e system_get_proxy(sa_proxy_s * info)
 {
        return SA_ERROR_NOT_SUPPORTED;
 }
 
-static int compare_proxy_info(sa_proxy_s * devProxy, sa_proxy_s * newProxy)
-{
-       // comapre proxy
-       if (devProxy != NULL && newProxy != NULL) {
-               if (strlen(devProxy->httpProxyHost) > 0) {
-                       if (strcmp(devProxy->httpProxyHost, newProxy->httpProxyHost)) {
-                               // proxy info is different
-                               return 1;
-                       } else {
-                               // Host address is the same, but port is different
-                               if (devProxy->httpProxyPort != newProxy->httpProxyPort)
-                                       return 1;
-                       }
-               }
-       }
-
-       return 0;
-}
-
 sa_error_e sa_setup_system(sa_system_s * system)
 {
        sa_proxy_s *devProxy = NULL;
@@ -58,11 +35,8 @@ sa_error_e sa_setup_system(sa_system_s * system)
        }
 
        if (system->proxy != NULL) {
-               ret = system_get_proxy(devProxy);
-               if (ret == SA_ERROR_NONE && compare_proxy_info(devProxy, system->proxy)) {
-                       // set proxy
-                       ret = system_set_proxy(system->proxy);
-               }
+               // set proxy
+               ret = sa_inputfile_set_proxy_env(system->proxy);
        }
 
        return ret;
index bf0ac9a5354bd49a25e4abf6bb67d37bfb904c10..7ebc29deb12d149864d45a8811462b0177cb8fc1 100644 (file)
@@ -2,6 +2,5 @@
 "version":"0.1",
 "wifi":{"ssid":"docker","password":"dockerdocker","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},
 "ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},
-"httpProxyHost":"127.0.0.1",
-"httpProxyPort":8080
+"httpProxy":"127.0.0.1:8080"
 }
\ No newline at end of file