Added 'environment_variables' capability 37/81937/7
authorshingil.kang <shingil.kang@samsung.com>
Fri, 29 Jul 2016 09:44:24 +0000 (18:44 +0900)
committershingil.kang <shingil.kang@samsung.com>
Mon, 1 Aug 2016 09:38:01 +0000 (18:38 +0900)
- Environment variables are applied to sdb shell process environment.
- Each environment variable should be separated by new line character('\n').
  e.g.) A=B\nC=D
- The length of environment string should be less than 4096 byte.
- The total number of environment variables should be less than 100.

Change-Id: If5b52217b364ecf45eb88f0d8d3644cec40d3d30
Signed-off-by: shingil.kang <shingil.kang@samsung.com>
src/sdb.c
src/sdbd_plugin.h
src/services.c
src/strutils.c
src/strutils.h

index 5667e73..22d2794 100644 (file)
--- a/src/sdb.c
+++ b/src/sdb.c
@@ -1687,6 +1687,11 @@ static int verify_root_cmd(const char* in_buf, sdbd_plugin_param out) {
     return ret;
 }
 
+static int get_shell_env(const char* in_buf, sdbd_plugin_param out) {
+    snprintf(out.data, out.len, "%s", "");
+    return SDBD_PLUGIN_RET_SUCCESS;
+}
+
 int default_cmd_proc(const char* cmd,
                     const char* in_buf, sdbd_plugin_param out) {
     int ret = SDBD_PLUGIN_RET_NOT_SUPPORT;
@@ -1712,6 +1717,8 @@ int default_cmd_proc(const char* cmd,
         ret = verify_sdbd_launch(in_buf, out);
     } else if (SDBD_CMP_CMD(cmd, VERIFY_ROOTCMD)) {
         ret = verify_root_cmd(in_buf, out);
+    } else if (SDBD_CMP_CMD(cmd, SHELL_ENVVAR)) {
+        ret = get_shell_env(in_buf, out);
     } else {
         D("Not supported command : %s\n", cmd);
         ret = SDBD_PLUGIN_RET_NOT_SUPPORT;
index 9bfc4b9..2d94157 100644 (file)
@@ -26,6 +26,7 @@
 #define SDBD_CMD_VERIFY_PEERIP          "verify_peer_ip"
 #define SDBD_CMD_VERIFY_LAUNCH          "verify_sdbd_launch"
 #define SDBD_CMD_VERIFY_ROOTCMD         "verify_root_cmd"
+#define SDBD_CMD_SHELL_ENVVAR           "sdbd_shell_env_var"
 
 /* plugin capabilities */
 #define SDBD_CAP_TYPE_SECURE            "secure_protocol_support"
@@ -36,8 +37,8 @@
 #define SDBD_CAP_TYPE_ROOTONOFF         "root_onoff_support"
 #define SDBD_CAP_TYPE_PLUGIN_VER        "sdbd_plugin_version"
 #define SDBD_CAP_TYPE_PRODUCT_VER       "product_version"
-#define SDBD_CAP_TYPE_LOG_ENABLE       "sdbd_log_enable"
-#define SDBD_CAP_TYPE_LOG_PATH       "sdbd_log_path"
+#define SDBD_CAP_TYPE_LOG_ENABLE        "sdbd_log_enable"
+#define SDBD_CAP_TYPE_LOG_PATH          "sdbd_log_path"
 
 /* capability return string */
 #define SDBD_CAP_RET_ENABLED            "enabled"
index 84e959b..bc2f35a 100644 (file)
@@ -57,7 +57,6 @@ struct stinfo {
     void *cookie;
 };
 
-
 void *service_bootstrap_func(void *x)
 {
     stinfo *sti = x;
@@ -599,22 +598,16 @@ static int create_subproc_thread(const char *name, int lines, int columns)
     char *trim_value = NULL;
     char path[PATH_MAX];
     memset(path, 0, sizeof(path));
-
-    char *envp[] = {
-        "TERM=linux", /* without this, some programs based on screen can't work, e.g. top */
-        "DISPLAY=:0", /* without this, some programs based on without launchpad can't work */
-        NULL,
-        NULL,
-        NULL,
-        NULL,
-        NULL
-    };
+    char *envp[MAX_TOKENS];
+    int envp_cnt = 0;
+    envp[envp_cnt++] = strdup("TERM=linux");
+    envp[envp_cnt++] = strdup("DISPLAY=:0");
 
     if (should_drop_privileges()) {
         if (g_sdk_home_dir_env) {
-            envp[2] = g_sdk_home_dir_env;
+            envp[envp_cnt++] = strdup(g_sdk_home_dir_env);
         } else {
-            envp[2] = "HOME=/home/owner";
+            envp[envp_cnt++] = strdup("HOME=/home/owner");
         }
         get_env("ENV_PATH", &value);
     } else {
@@ -622,7 +615,7 @@ static int create_subproc_thread(const char *name, int lines, int columns)
         if(value == NULL) {
             get_env("ENV_ROOTPATH", &value);
         }
-        envp[2] = "HOME=/root";
+        envp[envp_cnt++] = strdup("HOME=/root");
     }
     if (value != NULL) {
         trim_value = str_trim(value);
@@ -633,15 +626,32 @@ static int create_subproc_thread(const char *name, int lines, int columns)
             } else {
                 snprintf(path, sizeof(path), "%s", trim_value);
             }
-            envp[3] = path;
+            envp[envp_cnt++] = strdup(path);
         } else {
             snprintf(path, sizeof(path), "%s", value);
-            envp[3] = path;
+            envp[envp_cnt++] = strdup(path);
         }
         free(value);
     }
 
-    D("path env:%s,%s,%s,%s\n", envp[0], envp[1], envp[2], envp[3]);
+    /* get environment variables from plugin */
+    char *envp_plugin = NULL;
+    envp_plugin = malloc(SDBD_PLUGIN_OUTBUF_MAX);
+    if (envp_plugin == NULL) {
+        D("Cannot allocate the shell commnad buffer.");
+        return -1;
+    }
+    memset(envp_plugin, 0, SDBD_PLUGIN_OUTBUF_MAX);
+    if (!request_plugin_cmd(SDBD_CMD_SHELL_ENVVAR, "", envp_plugin, SDBD_PLUGIN_OUTBUF_MAX)) {
+        D("Failed to convert the shell command. (%s)\n", name);
+        free(envp_plugin);
+        return -1;
+    } else {
+       if(envp_plugin[0] != '\0') {
+            envp_cnt = tokenize_append(envp_plugin, "\n", envp, MAX_TOKENS, envp_cnt);
+        }
+    }
+    free(envp_plugin);
 
     if(name) { // in case of shell execution directly
         // Check the shell command validation.
@@ -709,6 +719,18 @@ static int create_subproc_thread(const char *name, int lines, int columns)
         }
 #endif
     }
+
+    /* free environment variables */
+    int i = 0;
+    if(envp_cnt > 0) {
+        for(i = 0; i < envp_cnt; i++) {
+            if(envp[i]) {
+                D("envp[%d] = %s\n", i, envp[i]);
+                free(envp[i]);
+            }
+        }
+    }
+
     D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
 
     if (ret_fd < 0) {
@@ -958,7 +980,6 @@ static void get_capability(int fd, void *cookie) {
    offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
                                "log_path", g_capabilities.log_path);
 
-
     offset++; // for '\0' character
 
     writex(fd, &offset, sizeof(uint16_t));
index 910e3be..cba6a23 100644 (file)
@@ -8,9 +8,12 @@
 #define PATH_MAX 4096
 #endif
 
-
 size_t tokenize(const char *str, const char *delim, char *tokens[], size_t max_tokens ) {
-    int cnt = 0;
+    return tokenize_append(str, delim, tokens, max_tokens, 0);
+}
+
+size_t tokenize_append(const char *str, const char *delim, char *tokens[], size_t max_tokens, int append_position) {
+    int cnt = append_position;
 
     char tmp[PATH_MAX+1];
     char *ptr;
index 897579f..e101091 100644 (file)
@@ -1,9 +1,10 @@
 #ifndef _STRUTILS_H_
 #define _STRUTILS_H_
 
-#define MAX_TOKENS 30
+#define MAX_TOKENS 100
 
 size_t tokenize(const char *str, const char *delim, char *tokens[], size_t max_tokens);
+size_t tokenize_append(const char *str, const char *delim, char *tokens[], size_t max_tokens, int append_position);
 void free_strings(char **array, int n);
 int read_line(const int fd, char* ptr, const unsigned int maxlen);
 char *s_strncpy(char *dest, const char *source, size_t n);