[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / lib / substitute.c
index bfe7dc814caf66d009a667f2f76e5fde65f45aae..57df02f7216f7b7a7141104ecaaf6f30368e5b77 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,
@@ -15,8 +15,7 @@
    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/>.
 */
 
 
@@ -141,11 +140,6 @@ void sub_set_smb_name(const char *name)
        }
 }
 
-char* sub_get_smb_name( void )
-{
-       return smb_user_name;
-}
-
 /*******************************************************************
  Setup the strings used by substitutions. Called per packet. Ensure
  %U name is set correctly also.
@@ -155,7 +149,7 @@ void set_current_user_info(const userdom_struct *pcui)
 {
        current_user_info = *pcui;
        /* The following is safe as current_user_info.smb_name
-        * has already been sanitised in register_vuid. */
+        * has already been sanitised in register_existing_vuid. */
        fstrcpy(smb_user_name, current_user_info.smb_name);
 }
 
@@ -264,7 +258,7 @@ struct api_longvar {
        char* (*fn)( void );
 };
 
-struct api_longvar longvar_table[] = {
+static struct api_longvar longvar_table[] = {
        { "DomainSID",          longvar_domainsid },
        { NULL,                 NULL }
 };
@@ -340,7 +334,7 @@ static char *realloc_expand_longvar(char *str, char *p)
 
 static char *automount_path(const char *user_name)
 {
-       static pstring server_path;
+       pstring server_path;
 
        /* use the passwd entry as the default */
        /* this will be the default if WITH_AUTOMOUNT is not used or fails */
@@ -369,7 +363,7 @@ static char *automount_path(const char *user_name)
 
        DEBUG(4,("Home server path: %s\n", server_path));
 
-       return server_path;
+       return talloc_strdup(talloc_tos(), server_path);
 }
 
 /*******************************************************************
@@ -380,7 +374,7 @@ static char *automount_path(const char *user_name)
 
 static const char *automount_server(const char *user_name)
 {
-       static pstring server_name;
+       pstring server_name;
        const char *local_machine_name = get_local_machine_name(); 
 
        /* use the local machine name as the default */
@@ -406,7 +400,7 @@ static const char *automount_server(const char *user_name)
 
        DEBUG(4,("Home server: %s\n", server_name));
 
-       return server_name;
+       return talloc_strdup(talloc_tos(), server_name);
 }
 
 /****************************************************************************
@@ -415,11 +409,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 +427,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,10 +443,11 @@ 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;
+       fstring pidstr, vnnstr;
        struct passwd *pass;
        const char *local_machine_name = get_local_machine_name();
 
@@ -463,7 +460,7 @@ 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;
        }
        
@@ -490,7 +487,7 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
                        } 
                        break;
                case 'D' :
-                       r = strdup_upper(current_user_info.domain);
+                       r = strdup_upper(domain_name);
                        if (r == NULL) {
                                goto error;
                        }
@@ -522,7 +519,7 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
                        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(False));
                        break;
                case 'a' :
                        a_string = realloc_string_sub(a_string, "%a", remote_arch);
@@ -549,6 +546,10 @@ 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;
                }
@@ -580,32 +581,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 +603,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 +646,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;
 
        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;
        }
        
@@ -707,7 +693,7 @@ char *alloc_sub_advanced(int snum, const char *user,
                        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 +710,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 +724,64 @@ 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)
+void standard_sub_conn(connection_struct *conn, char *str, size_t len)
 {
-       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);
+       s = alloc_sub_advanced(lp_servicename(SNUM(conn)), conn->user, conn->connectpath,
+                              conn->gid, smb_user_name, "", str);
 
        if ( s ) {
                strncpy( str, s, len );
                SAFE_FREE( s );
        }
 }
+