increase log level for this failed setsockopt call. EINVAL is a normal error on Solar...
[jra/samba/.git] / source / lib / substitute.c
index b456b9b9923db31110164c9ae2b3a1656101947d..62dfdb56b56c3d4829decd223c493081ec3a68db 100644 (file)
@@ -34,6 +34,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;
@@ -302,13 +307,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 +413,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 "";
        }
@@ -427,6 +433,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 +446,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;
 }
@@ -477,6 +483,9 @@ static const char *automount_server(const char *user_name)
                        return "";
                }
                srv = talloc_strdup(ctx, automount_value);
+               if (!srv) {
+                       return "";
+               }
                p = strchr_m(srv, ':');
                if (!p) {
                        return "";
@@ -488,9 +497,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 +546,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 +562,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 +578,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 +600,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 +634,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",
@@ -652,17 +672,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 +788,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 +804,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;