Expanding environment variables in smb.conf

Branko Cibej branko.cibej at hermes.si
Tue Feb 3 19:36:00 GMT 1998


Hello all,

We're using a slightly modified Samba as part of a larger product.
Among other things, we've taught Samba to expand environment
variables in the config file; occurrences of `%$(envvar)' are
replaced with the value of the environment variable `envvar'.

Is there any reason why Samba shouldn't have this capability? I
think that would be a nice feature to have (and it would save us
some work when merging our mods with a newer version, too :-).

Anyway, I've attached a patch against 1.9.18p2 to do this. Comments
appreciated.

    Regards,
        Brane

--
Branko Cibej   <branko.cibej at hermes.si>
HERMES SoftLab, Litijska 51, 1000 Ljubljana, Slovenia
phone: (++386 61) 186 53 49  fax: (++386 61) 186 52 70

-------------- next part --------------
1998-02-03 19:12  Branko Cibej  <branko.cibej at hermes.si>

	* util.c (standard_sub_basic): Replace occurrences of `%$(envvar)'
	with the value of the environment variable `envvar'.

*** util.c	Mon Jan 26 21:05:48 1998
--- util.c	Tue Feb  3 17:54:01 1998
***************
*** 3807,3812 ****
--- 3807,3838 ----
          case 'h' : string_sub(p,"%h", myhostname); break;
          case 'm' : string_sub(p,"%m", remote_machine); break;
          case 'v' : string_sub(p,"%v", VERSION); break;
+         case '$' : /* Expand environment variables */
+         {
+             fstring envname;
+             char *envval;
+             char *q, *r;
+ 
+             if (*(p+2) != '(') { p+=2; break; }
+             if ((q = strchr(p,')')) == NULL)
+             {
+                 DEBUG(0,("Unterminated environment variable [%s]\n", p));
+                 p+=2; break;
+             }
+ 
+             r = p+3;
+             strncpy(envname,r,q-r);
+             envname[q-r] = '\0';
+             if ((envval = getenv(envname)) == NULL)
+             {
+                 DEBUG(0,("Environment variable [%s] not set\n", envname));
+                 envval = "";
+             }
+             strncpy(envname,p,q+1-p);
+             envname[q+1-p] = '\0';
+             string_sub(p,envname,envval);
+             break;
+         }
          case '\0': p++; break; /* don't run off end if last character is % */
          default  : p+=2; break;
          }


More information about the samba mailing list