"userdom_struct" does not need "full_name" anymore -- unused
[samba.git] / source3 / lib / substitute.c
index db79a9f2b5cbb9f53cc301cf46e3e554971e8785..0cb326961d767478086984e8ab9ad246fcb966fb 100644 (file)
@@ -21,8 +21,6 @@
 
 #include "includes.h"
 
-extern struct current_user current_user;
-
 userdom_struct current_user_info;
 fstring remote_proto="UNKNOWN";
 
@@ -34,6 +32,11 @@ fstring remote_proto="UNKNOWN";
 
 static char *local_machine;
 
+void free_local_machine_name(void)
+{
+       SAFE_FREE(local_machine);
+}
+
 bool set_local_machine_name(const char *local_name, bool perm)
 {
        static bool already_perm = false;
@@ -208,11 +211,17 @@ static const char *get_smb_user_name(void)
 /*******************************************************************
  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_existing_vuid. */
 
@@ -302,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"));
@@ -407,7 +417,7 @@ static const char *automount_path(const char *user_name)
        /* use the passwd entry as the default */
        /* this will be the default if WITH_AUTOMOUNT is not used or fails */
 
-       server_path = talloc_strdup(ctx, get_user_home_dir(user_name));
+       server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
        if (!server_path) {
                return "";
        }
@@ -416,9 +426,9 @@ static const char *automount_path(const char *user_name)
 
        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. "
@@ -427,6 +437,9 @@ static const char *automount_path(const char *user_name)
                                                (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
@@ -437,9 +450,6 @@ static const char *automount_path(const char *user_name)
        }
 #endif
 
-       if (!server_path) {
-               server_path = "";
-       }
        DEBUG(4,("Home server path: %s\n", server_path));
        return server_path;
 }
@@ -472,11 +482,14 @@ static const char *automount_server(const char *user_name)
        if (lp_nis_home_map()) {
                char *p;
                char *srv;
-               char *automount_value = automount_lookup(user_name);
+               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 "";
@@ -488,9 +501,6 @@ static const char *automount_server(const char *user_name)
        }
 #endif
 
-       if (!server_name) {
-               server_name = "";
-       }
        DEBUG(4,("Home server: %s\n", server_name));
        return server_name;
 }
@@ -540,9 +550,9 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
 {
        char *b, *p, *s, *r, *a_string;
        fstring pidstr, vnnstr;
-       struct passwd *pass;
        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 */
        
@@ -556,12 +566,14 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                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);
@@ -570,15 +582,21 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                        }
                        a_string = realloc_string_sub(a_string, "%U", r);
                        break;
-               case 'G' :
+               case 'G' : {
+                       struct passwd *pass;
                        r = SMB_STRDUP(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(domain_name);
                        if (r == NULL) {
@@ -586,10 +604,16 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                        }
                        a_string = realloc_string_sub(a_string, "%D", r);
                        break;
-               case 'I' :
+               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",
-                                       client_addr(get_client_fd(),addr, sizeof(addr)));
+                                                     addr + offset);
                        break;
+               }
                case 'i': 
                        a_string = realloc_string_sub( a_string, "%i",
                                        client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
@@ -614,7 +638,7 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                        a_string = realloc_string_sub(a_string, "%R", remote_proto);
                        break;
                case 'T' :
-                       a_string = realloc_string_sub(a_string, "%T", current_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",
@@ -628,10 +652,13 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                        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());
@@ -652,17 +679,20 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
 
                p++;
                SAFE_FREE(r);
-               
-               if ( !a_string ) {
-                       return NULL;
+
+               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;
 }
 
 /****************************************************************************
@@ -765,7 +795,7 @@ static char *alloc_sub_advanced(const char *servicename, const char *user,
                         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) {
@@ -781,10 +811,13 @@ static char *alloc_sub_advanced(const char *servicename, 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;
@@ -831,8 +864,8 @@ static char *alloc_sub_advanced(const char *servicename, const char *user,
  */
 
 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
-                         const char *servicename, const char *user, 
-                         const char *connectpath, gid_t gid, 
+                         const char *servicename, const char *user,
+                         const char *connectpath, gid_t gid,
                          const char *smb_name, const char *domain_name,
                          const char *str)
 {
@@ -848,13 +881,13 @@ char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
 }
 
 
-void standard_sub_advanced(const char *servicename, const char *user, 
-                          const char *connectpath, gid_t gid, 
+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)
 {
        char *s;
-       
+
        s = alloc_sub_advanced(servicename, user, connectpath,
                               gid, smb_name, domain_name, str);
 
@@ -865,19 +898,17 @@ void standard_sub_advanced(const char *servicename, const char *user,
 }
 
 /****************************************************************************
*  Do some standard substitutions in a string.
- *  ****************************************************************************/
+ Do some standard substitutions in a string.
+****************************************************************************/
 
-void standard_sub_conn(connection_struct *conn, char *str, size_t len)
+char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
 {
-       char *s;
-
-       s = alloc_sub_advanced(lp_servicename(SNUM(conn)), conn->user, conn->connectpath,
-                              conn->gid, get_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);
 }
-