s3-netdomjoin-gui: allow to switch between workgroups/domains with the same name.
[ira/wip.git] / source3 / lib / substitute.c
index bfe7dc814caf66d009a667f2f76e5fde65f45aae..c9dc1d2eb47e89434eaeaf2bfaad88ff05c3ce04 100644 (file)
@@ -6,7 +6,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 
 #include "includes.h"
 
-extern struct current_user current_user;
-
-fstring local_machine="";
-fstring remote_arch="UNKNOWN";
 userdom_struct current_user_info;
 fstring remote_proto="UNKNOWN";
 
-static fstring remote_machine;
-static fstring smb_user_name;
-
-/** 
+/**
  * Set the 'local' machine name
  * @param local_name the name we are being called
  * @param if this is the 'final' name for us, not be be changed again
  */
 
-void set_local_machine_name(const char* local_name, BOOL perm)
+static char *local_machine;
+
+void free_local_machine_name(void)
 {
-       static BOOL already_perm = False;
-       fstring tmp_local_machine;
+       SAFE_FREE(local_machine);
+}
 
-       fstrcpy(tmp_local_machine,local_name);
+bool set_local_machine_name(const char *local_name, bool perm)
+{
+       static bool already_perm = false;
+       char *tmp_local_machine = NULL;
+       char addr[INET6_ADDRSTRLEN];
+       size_t len;
+
+       tmp_local_machine = SMB_STRDUP(local_name);
+       if (!tmp_local_machine) {
+               return false;
+       }
        trim_char(tmp_local_machine,' ',' ');
 
        /*
         * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
-        * arrggg!!! 
+        * arrggg!!!
         */
 
-       if ( strequal(tmp_local_machine, "*SMBSERVER") || strequal(tmp_local_machine, "*SMBSERV") )  {
-               fstrcpy( local_machine, client_socket_addr() );
-               return;
+       if (strequal(tmp_local_machine, "*SMBSERVER") ||
+                       strequal(tmp_local_machine, "*SMBSERV") )  {
+               SAFE_FREE(local_machine);
+               local_machine = SMB_STRDUP(client_socket_addr(get_client_fd(),
+                                       addr, sizeof(addr)) );
+               SAFE_FREE(tmp_local_machine);
+               return local_machine ? true : false;
        }
 
-       if (already_perm)
-               return;
+       if (already_perm) {
+               return true;
+       }
+
+       SAFE_FREE(local_machine);
+       len = strlen(tmp_local_machine);
+       local_machine = SMB_CALLOC_ARRAY(char, len+1);
+       if (!local_machine) {
+               SAFE_FREE(tmp_local_machine);
+               return false;
+       }
+       /* alpha_strcpy includes the space for the terminating nul. */
+       alpha_strcpy(local_machine,tmp_local_machine,
+                       SAFE_NETBIOS_CHARS,len+1);
+       strlower_m(local_machine);
+       SAFE_FREE(tmp_local_machine);
 
        already_perm = perm;
 
-       alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
-       strlower_m(local_machine);
+       return true;
+}
+
+const char *get_local_machine_name(void)
+{
+       if (!local_machine || !*local_machine) {
+               return global_myname();
+       }
+
+       return local_machine;
 }
 
-/** 
+/**
  * Set the 'remote' machine name
  * @param remote_name the name our client wants to be called by
  * @param if this is the 'final' name for them, not be be changed again
  */
 
-void set_remote_machine_name(const char* remote_name, BOOL perm)
-{
-       static BOOL already_perm = False;
-       fstring tmp_remote_machine;
+static char *remote_machine;
 
-       if (already_perm)
-               return;
+bool set_remote_machine_name(const char *remote_name, bool perm)
+{
+       static bool already_perm = False;
+       char *tmp_remote_machine;
+       size_t len;
 
-       already_perm = perm;
+       if (already_perm) {
+               return true;
+       }
 
-       fstrcpy(tmp_remote_machine,remote_name);
+       tmp_remote_machine = SMB_STRDUP(remote_name);
+       if (!tmp_remote_machine) {
+               return false;
+       }
        trim_char(tmp_remote_machine,' ',' ');
-       alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
+
+       SAFE_FREE(remote_machine);
+       len = strlen(tmp_remote_machine);
+       remote_machine = SMB_CALLOC_ARRAY(char, len+1);
+       if (!remote_machine) {
+               SAFE_FREE(tmp_remote_machine);
+               return false;
+       }
+
+       /* alpha_strcpy includes the space for the terminating nul. */
+       alpha_strcpy(remote_machine,tmp_remote_machine,
+                       SAFE_NETBIOS_CHARS,len+1);
        strlower_m(remote_machine);
-}
+       SAFE_FREE(tmp_remote_machine);
 
-const char* get_remote_machine_name(void) 
-{
-       return remote_machine;
+       already_perm = perm;
+
+       return true;
 }
 
-const char* get_local_machine_name(void) 
+const char *get_remote_machine_name(void)
 {
-       if (!*local_machine) {
-               return global_myname();
-       }
-
-       return local_machine;
+       return remote_machine ? remote_machine : "";
 }
 
 /*******************************************************************
  Setup the string used by %U substitution.
 ********************************************************************/
 
+static char *smb_user_name;
+
 void sub_set_smb_name(const char *name)
 {
-       fstring tmp;
-       int len;
-       BOOL is_machine_account = False;
+       char *tmp;
+       size_t len;
+       bool is_machine_account = false;
 
        /* don't let anonymous logins override the name */
-       if (! *name)
+       if (!name || !*name) {
                return;
+       }
 
+       tmp = SMB_STRDUP(name);
+       if (!tmp) {
+               return;
+       }
+       trim_char(tmp, ' ', ' ');
+       strlower_m(tmp);
 
-       fstrcpy( tmp, name );
-       trim_char( tmp, ' ', ' ' );
-       strlower_m( tmp );
-
-       len = strlen( tmp );
+       len = strlen(tmp);
 
-       if ( len == 0 )
+       if (len == 0) {
+               SAFE_FREE(tmp);
                return;
+       }
 
        /* long story but here goes....we have to allow usernames
           ending in '$' as they are valid machine account names.
           So check for a machine account and re-add the '$'
           at the end after the call to alpha_strcpy().   --jerry  */
-          
-       if ( tmp[len-1] == '$' )
+
+       if (tmp[len-1] == '$') {
                is_machine_account = True;
-       
-       alpha_strcpy( smb_user_name, tmp, SAFE_NETBIOS_CHARS, sizeof(smb_user_name)-1 );
+       }
 
-       if ( is_machine_account ) {
-               len = strlen( smb_user_name );
+       SAFE_FREE(smb_user_name);
+       smb_user_name = SMB_CALLOC_ARRAY(char, len+1);
+       if (!smb_user_name) {
+               SAFE_FREE(tmp);
+               return;
+       }
+
+       /* alpha_strcpy includes the space for the terminating nul. */
+       alpha_strcpy(smb_user_name, tmp,
+                       SAFE_NETBIOS_CHARS,
+                       len+1);
+
+       SAFE_FREE(tmp);
+
+       if (is_machine_account) {
+               len = strlen(smb_user_name);
                smb_user_name[len-1] = '$';
        }
 }
 
-char* sub_get_smb_name( void )
+static const char *get_smb_user_name(void)
 {
-       return smb_user_name;
+       return smb_user_name ? smb_user_name : "";
 }
 
 /*******************************************************************
  Setup the strings used by substitutions. Called per packet. Ensure
  %U name is set correctly also.
+
+ smb_name must be sanitized by alpha_strcpy
 ********************************************************************/
 
-void set_current_user_info(const userdom_struct *pcui)
+void set_current_user_info(const char *smb_name, const char *unix_name,
+                          const char *domain)
 {
-       current_user_info = *pcui;
+       fstrcpy(current_user_info.smb_name, smb_name);
+       fstrcpy(current_user_info.unix_name, unix_name);
+       fstrcpy(current_user_info.domain, domain);
+
        /* The following is safe as current_user_info.smb_name
-        * has already been sanitised in register_vuid. */
-       fstrcpy(smb_user_name, current_user_info.smb_name);
+        * has already been sanitised in register_existing_vuid. */
+
+       sub_set_smb_name(current_user_info.smb_name);
 }
 
 /*******************************************************************
- return the current active user name
+ Return the current active user name.
 *******************************************************************/
 
-const char* get_current_username( void )
+const char *get_current_username(void)
 {
-       if ( current_user_info.smb_name[0] == '\0' )
-               return smb_user_name;
+       if (current_user_info.smb_name[0] == '\0' ) {
+               return get_smb_user_name();
+       }
 
-       return current_user_info.smb_name; 
+       return current_user_info.smb_name;
 }
 
 /*******************************************************************
@@ -241,13 +311,14 @@ static char * realloc_expand_env_var(char *str, char *p)
 static char *longvar_domainsid( void )
 {
        DOM_SID sid;
+       fstring tmp;
        char *sid_string;
        
        if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
                return NULL;
        }
        
-       sid_string = SMB_STRDUP( sid_string_static( &sid ) );
+       sid_string = SMB_STRDUP( sid_to_fstring( tmp, &sid ) );
        
        if ( !sid_string ) {
                DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
@@ -264,7 +335,7 @@ struct api_longvar {
        char* (*fn)( void );
 };
 
-struct api_longvar longvar_table[] = {
+static struct api_longvar longvar_table[] = {
        { "DomainSID",          longvar_domainsid },
        { NULL,                 NULL }
 };
@@ -327,9 +398,9 @@ static char *realloc_expand_longvar(char *str, char *p)
        varname[copylen] = '\0';
        r = realloc_string_sub(str, varname, value);
        SAFE_FREE( value );
-       
+
        /* skip over the %(varname) */
-       
+
        return r;
 }
 
@@ -338,37 +409,48 @@ static char *realloc_expand_longvar(char *str, char *p)
  Added this to implement %p (NIS auto-map version of %H)
 *******************************************************************/
 
-static char *automount_path(const char *user_name)
+static const char *automount_path(const char *user_name)
 {
-       static pstring server_path;
+       TALLOC_CTX *ctx = talloc_tos();
+       const char *server_path;
 
        /* use the passwd entry as the default */
        /* this will be the default if WITH_AUTOMOUNT is not used or fails */
 
-       pstrcpy(server_path, get_user_home_dir(user_name));
+       server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
+       if (!server_path) {
+               return "";
+       }
 
 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
 
        if (lp_nis_home_map()) {
                const char *home_path_start;
-               const char *automount_value = automount_lookup(user_name);
+               char *automount_value = automount_lookup(ctx, user_name);
 
-               if(strlen(automount_value) > 0) {
+               if(automount_value && strlen(automount_value) > 0) {
                        home_path_start = strchr_m(automount_value,':');
                        if (home_path_start != NULL) {
-                               DEBUG(5, ("NIS lookup succeeded.  Home path is: %s\n",
-                                               home_path_start?(home_path_start+1):""));
-                               pstrcpy(server_path, home_path_start+1);
+                               DEBUG(5, ("NIS lookup succeeded. "
+                                       "Home path is: %s\n",
+                                       home_path_start ?
+                                               (home_path_start+1):""));
+                               server_path = talloc_strdup(ctx,
+                                                       home_path_start+1);
+                               if (!server_path) {
+                                       server_path = "";
+                               }
                        }
                } else {
-                       /* NIS key lookup failed: default to user home directory from password file */
-                       DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
+                       /* NIS key lookup failed: default to
+                        * user home directory from password file */
+                       DEBUG(5, ("NIS lookup failed. Using Home path from "
+                       "passwd file. Home path is: %s\n", server_path ));
                }
        }
 #endif
 
        DEBUG(4,("Home server path: %s\n", server_path));
-
        return server_path;
 }
 
@@ -380,32 +462,46 @@ static char *automount_path(const char *user_name)
 
 static const char *automount_server(const char *user_name)
 {
-       static pstring server_name;
-       const char *local_machine_name = get_local_machine_name(); 
+       TALLOC_CTX *ctx = talloc_tos();
+       const char *server_name;
+       const char *local_machine_name = get_local_machine_name();
 
        /* use the local machine name as the default */
        /* this will be the default if WITH_AUTOMOUNT is not used or fails */
-       if (local_machine_name && *local_machine_name)
-               pstrcpy(server_name, local_machine_name);
-       else
-               pstrcpy(server_name, global_myname());
-       
-#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
+       if (local_machine_name && *local_machine_name) {
+               server_name = talloc_strdup(ctx, local_machine_name);
+       } else {
+               server_name = talloc_strdup(ctx, global_myname());
+       }
+
+       if (!server_name) {
+               return "";
+       }
 
+#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
        if (lp_nis_home_map()) {
-               int home_server_len;
-               char *automount_value = automount_lookup(user_name);
-               home_server_len = strcspn(automount_value,":");
-               DEBUG(5, ("NIS lookup succeeded.  Home server length: %d\n",home_server_len));
-               if (home_server_len > sizeof(pstring))
-                       home_server_len = sizeof(pstring);
-               strncpy(server_name, automount_value, home_server_len);
-                server_name[home_server_len] = '\0';
+               char *p;
+               char *srv;
+               char *automount_value = automount_lookup(ctx, user_name);
+               if (!automount_value) {
+                       return "";
+               }
+               srv = talloc_strdup(ctx, automount_value);
+               if (!srv) {
+                       return "";
+               }
+               p = strchr_m(srv, ':');
+               if (!p) {
+                       return "";
+               }
+               *p = '\0';
+               server_name = srv;
+               DEBUG(5, ("NIS lookup succeeded.  Home server %s\n",
+                                       server_name));
        }
 #endif
 
        DEBUG(4,("Home server: %s\n", server_name));
-
        return server_name;
 }
 
@@ -415,11 +511,12 @@ static const char *automount_server(const char *user_name)
  don't allow expansions.
 ****************************************************************************/
 
-void standard_sub_basic(const char *smb_name, char *str, size_t len)
+void standard_sub_basic(const char *smb_name, const char *domain_name,
+                       char *str, size_t len)
 {
        char *s;
        
-       if ( (s = alloc_sub_basic( smb_name, str )) != NULL ) {
+       if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
                strncpy( str, s, len );
        }
        
@@ -432,11 +529,12 @@ void standard_sub_basic(const char *smb_name, char *str, size_t len)
  This function will return an allocated string that have to be freed.
 ****************************************************************************/
 
-char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
+char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
+                      const char *domain_name, const char *str)
 {
        char *a, *t;
        
-       if ( (a = alloc_sub_basic(smb_name, str)) == NULL ) {
+       if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
                return NULL;
        }
        t = talloc_strdup(mem_ctx, a);
@@ -447,12 +545,14 @@ char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *st
 /****************************************************************************
 ****************************************************************************/
 
-char *alloc_sub_basic(const char *smb_name, const char *str)
+char *alloc_sub_basic(const char *smb_name, const char *domain_name,
+                     const char *str)
 {
        char *b, *p, *s, *r, *a_string;
-       fstring pidstr;
-       struct passwd *pass;
+       fstring pidstr, vnnstr;
+       char addr[INET6_ADDRSTRLEN];
        const char *local_machine_name = get_local_machine_name();
+       TALLOC_CTX *tmp_ctx = NULL;
 
        /* workaround to prevent a crash while looking at bug #687 */
        
@@ -463,44 +563,60 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
        
        a_string = SMB_STRDUP(str);
        if (a_string == NULL) {
-               DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
+               DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
                return NULL;
        }
-       
+
+       tmp_ctx = talloc_stackframe();
+
        for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
 
                r = NULL;
                b = a_string;
-               
+
                switch (*(p+1)) {
                case 'U' : 
-                       r = strdup_lower(smb_name);
+                       r = strlower_talloc(tmp_ctx, smb_name);
                        if (r == NULL) {
                                goto error;
                        }
                        a_string = realloc_string_sub(a_string, "%U", r);
                        break;
-               case 'G' :
-                       r = SMB_STRDUP(smb_name);
+               case 'G' : {
+                       struct passwd *pass;
+                       r = talloc_strdup(tmp_ctx, smb_name);
                        if (r == NULL) {
                                goto error;
                        }
-                       if ((pass = Get_Pwnam(r))!=NULL) {
-                               a_string = realloc_string_sub(a_string, "%G", gidtoname(pass->pw_gid));
-                       } 
+                       pass = Get_Pwnam_alloc(tmp_ctx, r);
+                       if (pass != NULL) {
+                               a_string = realloc_string_sub(
+                                       a_string, "%G",
+                                       gidtoname(pass->pw_gid));
+                       }
+                       TALLOC_FREE(pass);
                        break;
+               }
                case 'D' :
-                       r = strdup_upper(current_user_info.domain);
+                       r = strupper_talloc(tmp_ctx, domain_name);
                        if (r == NULL) {
                                goto error;
                        }
                        a_string = realloc_string_sub(a_string, "%D", r);
                        break;
-               case 'I' :
-                       a_string = realloc_string_sub(a_string, "%I", client_addr());
+               case 'I' : {
+                       int offset = 0;
+                       client_addr(get_client_fd(), addr, sizeof(addr));
+                       if (strnequal(addr,"::ffff:",7)) {
+                               offset = 7;
+                       }
+                       a_string = realloc_string_sub(a_string, "%I",
+                                                     addr + offset);
                        break;
+               }
                case 'i': 
-                       a_string = realloc_string_sub( a_string, "%i", client_socket_addr() );
+                       a_string = realloc_string_sub( a_string, "%i",
+                                       client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
                        break;
                case 'L' : 
                        if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
@@ -516,16 +632,17 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
                        a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
                        break;
                case 'M' :
-                       a_string = realloc_string_sub(a_string, "%M", client_name());
+                       a_string = realloc_string_sub(a_string, "%M", client_name(get_client_fd()));
                        break;
                case 'R' :
                        a_string = realloc_string_sub(a_string, "%R", remote_proto);
                        break;
                case 'T' :
-                       a_string = realloc_string_sub(a_string, "%T", timestring(False));
+                       a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
                        break;
                case 'a' :
-                       a_string = realloc_string_sub(a_string, "%a", remote_arch);
+                       a_string = realloc_string_sub(a_string, "%a",
+                                       get_remote_arch_str());
                        break;
                case 'd' :
                        slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
@@ -535,10 +652,13 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
                        a_string = realloc_string_sub(a_string, "%h", myhostname());
                        break;
                case 'm' :
-                       a_string = realloc_string_sub(a_string, "%m", remote_machine);
+                       a_string = realloc_string_sub(a_string, "%m",
+                                                     remote_machine
+                                                     ? remote_machine
+                                                     : "");
                        break;
                case 'v' :
-                       a_string = realloc_string_sub(a_string, "%v", SAMBA_VERSION_STRING);
+                       a_string = realloc_string_sub(a_string, "%v", samba_version_string());
                        break;
                case 'w' :
                        a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
@@ -549,23 +669,30 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
                case '(':
                        a_string = realloc_expand_longvar( a_string, p );
                        break;
+               case 'V' :
+                       slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
+                       a_string = realloc_string_sub(a_string, "%V", vnnstr);
+                       break;
                default: 
                        break;
                }
 
                p++;
-               SAFE_FREE(r);
-               
-               if ( !a_string ) {
-                       return NULL;
+               TALLOC_FREE(r);
+
+               if (a_string == NULL) {
+                       goto done;
                }
        }
 
-       return a_string;
+       goto done;
 
 error:
        SAFE_FREE(a_string);
-       return NULL;
+
+done:
+       TALLOC_FREE(tmp_ctx);
+       return a_string;
 }
 
 /****************************************************************************
@@ -580,32 +707,20 @@ char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
                        uid_t uid,
                        gid_t gid)
 {
-       char *a, *t;
-               a = alloc_sub_specified(input_string, username, domain, uid, gid);
-       if (!a) {
+       char *a_string;
+       char *ret_string = NULL;
+       char *b, *p, *s;
+       TALLOC_CTX *tmp_ctx;
+
+       if (!(tmp_ctx = talloc_new(mem_ctx))) {
+               DEBUG(0, ("talloc_new failed\n"));
                return NULL;
        }
-       t = talloc_strdup(mem_ctx, a);
-       SAFE_FREE(a);
-       return t;
-}
-
-/****************************************************************************
-****************************************************************************/
-
-char *alloc_sub_specified(const char *input_string,
-                       const char *username,
-                       const char *domain,
-                       uid_t uid,
-                       gid_t gid)
-{
-       char *a_string, *ret_string;
-       char *b, *p, *s;
 
-       a_string = SMB_STRDUP(input_string);
+       a_string = talloc_strdup(tmp_ctx, input_string);
        if (a_string == NULL) {
-               DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
-               return NULL;
+               DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
+               goto done;
        }
        
        for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
@@ -614,30 +729,42 @@ char *alloc_sub_specified(const char *input_string,
                
                switch (*(p+1)) {
                case 'U' : 
-                       a_string = realloc_string_sub(a_string, "%U", username);
+                       a_string = talloc_string_sub(
+                               tmp_ctx, a_string, "%U", username);
                        break;
                case 'u' : 
-                       a_string = realloc_string_sub(a_string, "%u", username);
+                       a_string = talloc_string_sub(
+                               tmp_ctx, a_string, "%u", username);
                        break;
                case 'G' :
                        if (gid != -1) {
-                               a_string = realloc_string_sub(a_string, "%G", gidtoname(gid));
+                               a_string = talloc_string_sub(
+                                       tmp_ctx, a_string, "%G",
+                                       gidtoname(gid));
                        } else {
-                               a_string = realloc_string_sub(a_string, "%G", "NO_GROUP");
+                               a_string = talloc_string_sub(
+                                       tmp_ctx, a_string,
+                                       "%G", "NO_GROUP");
                        }
                        break;
                case 'g' :
                        if (gid != -1) {
-                               a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
+                               a_string = talloc_string_sub(
+                                       tmp_ctx, a_string, "%g",
+                                       gidtoname(gid));
                        } else {
-                               a_string = realloc_string_sub(a_string, "%g", "NO_GROUP");
+                               a_string = talloc_string_sub(
+                                       tmp_ctx, a_string, "%g", "NO_GROUP");
                        }
                        break;
                case 'D' :
-                       a_string = realloc_string_sub(a_string, "%D", domain);
+                       a_string = talloc_string_sub(tmp_ctx, a_string,
+                                                    "%D", domain);
                        break;
                case 'N' : 
-                       a_string = realloc_string_sub(a_string, "%N", automount_server(username)); 
+                       a_string = talloc_string_sub(
+                               tmp_ctx, a_string, "%N",
+                               automount_server(username)); 
                        break;
                default: 
                        break;
@@ -645,49 +772,34 @@ char *alloc_sub_specified(const char *input_string,
 
                p++;
                if (a_string == NULL) {
-                       return NULL;
+                       goto done;
                }
        }
 
-       ret_string = alloc_sub_basic(username, a_string);
-       SAFE_FREE(a_string);
-       return ret_string;
-}
+       /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
+        * away with the TALLOC_FREE(tmp_ctx) further down. */
 
-/****************************************************************************
-****************************************************************************/
+       ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
 
-char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
-                       int snum,
-                       const char *user,
-                       const char *connectpath,
-                       gid_t gid,
-                       const char *smb_name,
-                       const char *str)
-{
-       char *a, *t;
-               a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
-       if (!a) {
-               return NULL;
-       }
-       t = talloc_strdup(mem_ctx, a);
-       SAFE_FREE(a);
-       return t;
+ done:
+       TALLOC_FREE(tmp_ctx);
+       return ret_string;
 }
 
 /****************************************************************************
 ****************************************************************************/
 
-char *alloc_sub_advanced(int snum, const char *user, 
-                                 const char *connectpath, gid_t gid, 
-                                 const char *smb_name, const char *str)
+static char *alloc_sub_advanced(const char *servicename, const char *user, 
+                        const char *connectpath, gid_t gid, 
+                        const char *smb_name, const char *domain_name,
+                        const char *str)
 {
        char *a_string, *ret_string;
-       char *b, *p, *s, *h;
+       char *b, *p, *s;
 
        a_string = SMB_STRDUP(str);
        if (a_string == NULL) {
-               DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
+               DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
                return NULL;
        }
        
@@ -699,15 +811,18 @@ char *alloc_sub_advanced(int snum, const char *user,
                case 'N' :
                        a_string = realloc_string_sub(a_string, "%N", automount_server(user));
                        break;
-               case 'H':
-                       if ((h = get_user_home_dir(user)))
+               case 'H': {
+                       char *h;
+                       if ((h = get_user_home_dir(talloc_tos(), user)))
                                a_string = realloc_string_sub(a_string, "%H", h);
+                       TALLOC_FREE(h);
                        break;
+               }
                case 'P': 
                        a_string = realloc_string_sub(a_string, "%P", connectpath); 
                        break;
                case 'S': 
-                       a_string = realloc_string_sub(a_string, "%S", lp_servicename(snum)); 
+                       a_string = realloc_string_sub(a_string, "%S", servicename);
                        break;
                case 'g': 
                        a_string = realloc_string_sub(a_string, "%g", gidtoname(gid)); 
@@ -724,7 +839,8 @@ char *alloc_sub_advanced(int snum, const char *user,
                         * "path =" string in [homes] and so needs the
                         * service name, not the username.  */
                case 'p': 
-                       a_string = realloc_string_sub(a_string, "%p", automount_path(lp_servicename(snum))); 
+                       a_string = realloc_string_sub(a_string, "%p",
+                                                     automount_path(servicename)); 
                        break;
                        
                default: 
@@ -737,70 +853,62 @@ char *alloc_sub_advanced(int snum, const char *user,
                }
        }
 
-       ret_string = alloc_sub_basic(smb_name, a_string);
+       ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
        SAFE_FREE(a_string);
        return ret_string;
 }
 
-/****************************************************************************
- Do some standard substitutions in a string.
-****************************************************************************/
+/*
+ * This obviously is inefficient and needs to be merged into
+ * alloc_sub_advanced...
+ */
 
-void standard_sub_conn(connection_struct *conn, char *str, size_t len)
+char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
+                         const char *servicename, const char *user,
+                         const char *connectpath, gid_t gid,
+                         const char *smb_name, const char *domain_name,
+                         const char *str)
 {
-       char *s;
-       
-       s = alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
-                       conn->gid, smb_user_name, str);
+       char *a, *t;
 
-       if ( s ) {
-               strncpy( str, s, len );
-               SAFE_FREE( s );
+       if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
+                                    smb_name, domain_name, str))) {
+               return NULL;
        }
+       t = talloc_strdup(mem_ctx, a);
+       SAFE_FREE(a);
+       return t;
 }
 
-/****************************************************************************
-****************************************************************************/
 
-char *talloc_sub_conn(TALLOC_CTX *mem_ctx, connection_struct *conn, const char *str)
+void standard_sub_advanced(const char *servicename, const char *user,
+                          const char *connectpath, gid_t gid,
+                          const char *smb_name, const char *domain_name,
+                          char *str, size_t len)
 {
-       return talloc_sub_advanced(mem_ctx, SNUM(conn), conn->user,
-                       conn->connectpath, conn->gid,
-                       smb_user_name, str);
-}
+       char *s;
 
-/****************************************************************************
-****************************************************************************/
+       s = alloc_sub_advanced(servicename, user, connectpath,
+                              gid, smb_name, domain_name, str);
 
-char *alloc_sub_conn(connection_struct *conn, const char *str)
-{
-       return alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
-                       conn->gid, smb_user_name, str);
+       if ( s ) {
+               strncpy( str, s, len );
+               SAFE_FREE( s );
+       }
 }
 
 /****************************************************************************
Like standard_sub but by snum.
Do some standard substitutions in a string.
 ****************************************************************************/
 
-void standard_sub_snum(int snum, char *str, size_t len)
+char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
 {
-       static uid_t cached_uid = -1;
-       static fstring cached_user;
-       char *s;
-       
-       /* calling uidtoname() on every substitute would be too expensive, so
-          we cache the result here as nearly every call is for the same uid */
-
-       if (cached_uid != current_user.ut.uid) {
-               fstrcpy(cached_user, uidtoname(current_user.ut.uid));
-               cached_uid = current_user.ut.uid;
-       }
-
-       s = alloc_sub_advanced(snum, cached_user, "", current_user.ut.gid,
-                             smb_user_name, str);
-
-       if ( s ) {
-               strncpy( str, s, len );
-               SAFE_FREE( s );
-       }
+       return talloc_sub_advanced(ctx,
+                               lp_servicename(SNUM(conn)),
+                               conn->server_info->unix_name,
+                               conn->connectpath,
+                               conn->server_info->utok.gid,
+                               get_smb_user_name(),
+                               "",
+                               str);
 }