--- stunnel-5.34/src/options.c.orig	2016-07-06 01:27:57.000000000 +0400
+++ stunnel-5.34/src/options.c	2016-07-07 14:49:30.027599350 +0400
@@ -330,10 +330,65 @@
     return 0;
 }
 
+#ifndef USE_WIN32
+unsigned int ExpandEnvironmentStringsA(const char *lpSrc, char *lpDst, size_t nSize) {
+    const char *from;
+    char *to;
+    const char *begin;
+    const char *end;
+    char *name;
+    const char *value;
+    size_t len;
+
+    from = lpSrc;
+    to = lpDst;
+    while ((begin = strchr(from, '$'))) {
+        len = (size_t)(begin - from);
+        if (*(begin + 1) == '{' && (end = strchr(begin, '}')) ) {
+            if ((size_t)(to - lpDst) + len >= nSize) return 0;
+            strncpy(to, from, len);
+            to += len;
+            len = (size_t)(end - begin - 2);
+            name = strndup(begin + 2, len);
+            value = getenv(name);
+            free(name);
+            if (value) {
+                len = strlen(value);
+                if ((size_t)(to - lpDst) + len >= nSize) return 0;
+                strncpy(to, value, len);
+            } else {
+                len = (size_t)(end - begin + 1);
+                if ((size_t)(to - lpDst) + len >= nSize) return 0;
+                strncpy(to, begin, len);
+            }
+        } else {
+            len++; /* +$ itself */
+            if ((size_t)(to - lpDst) + len >= nSize) return 0;
+            strncpy(to, from, len);
+            end = from + len - 1;
+        }
+        to += len;
+        from = end + 1;
+    }
+    len = strlen(from); /* rest of string */
+    if ((size_t)(to - lpDst) + len >= nSize) return 0;
+    strncpy(to, from, len);
+    to[len] = '\0';
+    return strlen(lpDst);
+}
+#endif /* !defined(USE_WIN32) */
+#ifdef _WIN32_WCE
+unsigned int ExpandEnvironmentStringsA(const char *lpSrc, char *lpDst, size_t nSize) {
+    strncpy(lpDst, lpSrc, nSize);
+    return strlen(lpDst);
+}
+#endif /* defined(_WIN32_WCE) */
+
 NOEXPORT int options_file(char *path, CONF_TYPE type, SERVICE_OPTIONS **section) {
     DISK_FILE *df;
     char line_text[CONFLINELEN], *errstr;
     char config_line[CONFLINELEN], *config_opt, *config_arg;
+    char env_expanded[CONFLINELEN];
     int i, line_number=0;
 #ifndef USE_WIN32
     int fd;
@@ -430,6 +485,16 @@
             continue;
         }
 
+        if(config_arg) {
+            if(ExpandEnvironmentStringsA(config_arg, env_expanded, sizeof(env_expanded))) {
+                config_arg=env_expanded;
+            } else {
+                s_log(LOG_ERR, "%s:%d: Failed to expand environment variables \"%s\"",
+                    path, line_number, config_arg);
+                return 1;
+            }
+        }
+
         errstr=option_not_found;
         /* try global options first (e.g. for 'debug') */
         if(!new_service_options.next)
