Remove more static fstring/pstrings. Fix socket option
authorJeremy Allison <jra@samba.org>
Fri, 9 Nov 2007 23:09:16 +0000 (15:09 -0800)
committerJeremy Allison <jra@samba.org>
Fri, 9 Nov 2007 23:09:16 +0000 (15:09 -0800)
set on wrong fd (-1).
Jeremy.

source/lib/substitute.c
source/lib/util.c
source/nmbd/nmbd_synclists.c
source/smbd/server.c

index 7f357e9f67f4fcc9aa32e91703bb6755156da4aa..ce88a78e776d900551c2dca966a335d788ce55a3 100644 (file)
 
 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;
+
+bool set_local_machine_name(const char *local_name, bool perm)
 {
-       static bool already_perm = False;
-       fstring tmp_local_machine;
+       static bool already_perm = false;
+       char *tmp_local_machine = NULL;
        char addr[INET6_ADDRSTRLEN];
+       size_t len;
 
-       fstrcpy(tmp_local_machine,local_name);
+       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(get_client_fd(), addr, sizeof(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(local_machine,tmp_local_machine,
+                       SAFE_NETBIOS_CHARS,len);
+       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 char *remote_machine;
+
+bool set_remote_machine_name(const char *remote_name, bool perm)
 {
        static bool already_perm = False;
-       fstring tmp_remote_machine;
-
-       if (already_perm)
-               return;
+       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(remote_machine,tmp_remote_machine,
+                       SAFE_NETBIOS_CHARS,len);
        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 );
+       }
+
+       SAFE_FREE(smb_user_name);
+       smb_user_name = SMB_CALLOC_ARRAY(char, len+1);
+       if (!smb_user_name) {
+               SAFE_FREE(tmp);
+               return;
+       }
+
+       alpha_strcpy(smb_user_name, tmp,
+                       SAFE_NETBIOS_CHARS,
+                       len);
 
-       if ( is_machine_account ) {
-               len = strlen( smb_user_name );
+       SAFE_FREE(tmp);
+
+       if (is_machine_account) {
+               len = strlen(smb_user_name);
                smb_user_name[len-1] = '$';
        }
 }
 
+static const char *get_smb_user_name(void)
+{
+       return smb_user_name ? smb_user_name : "";
+}
+
 /*******************************************************************
  Setup the strings used by substitutions. Called per packet. Ensure
  %U name is set correctly also.
@@ -152,19 +212,21 @@ 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_existing_vuid. */
-       fstrcpy(smb_user_name, current_user_info.smb_name);
+
+       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;
 }
 
 /*******************************************************************
@@ -323,9 +385,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;
 }
 
@@ -334,14 +396,18 @@ 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)
 {
-       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(user_name));
+       if (!server_path) {
+               return "";
+       }
 
 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
 
@@ -352,20 +418,27 @@ static char *automount_path(const char *user_name)
                if(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);
                        }
                } 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
 
+       if (!server_path) {
+               server_path = "";
+       }
        DEBUG(4,("Home server path: %s\n", server_path));
-
-       return talloc_strdup(talloc_tos(), server_path);
+       return server_path;
 }
 
 /*******************************************************************
@@ -376,33 +449,47 @@ static char *automount_path(const char *user_name)
 
 static const char *automount_server(const char *user_name)
 {
-       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 *p;
+               char *srv;
                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';
+               if (!automount_value) {
+                       return "";
+               }
+               srv = talloc_strdup(ctx, automount_value);
+               p = strchr_m(srv, ':');
+               if (!p) {
+                       return "";
+               }
+               *p = '\0';
+               server_name = srv;
+               DEBUG(5, ("NIS lookup succeeded.  Home server %s\n",
+                                       server_name));
        }
 #endif
 
+       if (!server_name) {
+               server_name = "";
+       }
        DEBUG(4,("Home server: %s\n", server_name));
-
-       return talloc_strdup(talloc_tos(), server_name);
+       return server_name;
 }
 
 /****************************************************************************
@@ -527,7 +614,8 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                        a_string = realloc_string_sub(a_string, "%T", current_timestring(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());
@@ -782,7 +870,7 @@ void standard_sub_conn(connection_struct *conn, char *str, size_t len)
        char *s;
 
        s = alloc_sub_advanced(lp_servicename(SNUM(conn)), conn->user, conn->connectpath,
-                              conn->gid, smb_user_name, "", str);
+                              conn->gid, get_smb_user_name(), "", str);
 
        if ( s ) {
                strncpy( str, s, len );
index 7f0ec0b13260808f52b81b12e5db793aa1588890..4bb6f57d527ba26fd1f1d1d581148fe432da72a5 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/CIFS implementation.
    Samba utility functions
    Copyright (C) Andrew Tridgell 1992-1998
-   Copyright (C) Jeremy Allison 2001-2002
+   Copyright (C) Jeremy Allison 2001-2007
    Copyright (C) Simo Sorce 2001
    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
    Copyright (C) James Peach 2006
 
 #include "includes.h"
 
-extern fstring local_machine;
 extern char *global_clobber_region_function;
 extern unsigned int global_clobber_region_line;
-extern fstring remote_arch;
 
 /* Max allowable allococation - 256mb - 0x10000000 */
 #define MAX_ALLOC_SIZE (1024*1024*256)
@@ -259,7 +257,6 @@ bool set_netbios_aliases(const char **str_array)
 
 bool init_names(void)
 {
-       char *p;
        int n;
 
        if (global_myname() == NULL || *global_myname() == '\0') {
@@ -272,18 +269,15 @@ bool init_names(void)
        if (!set_netbios_aliases(lp_netbios_aliases())) {
                DEBUG( 0, ( "init_structs: malloc fail.\n" ) );
                return False;
-       }                       
+       }
 
-       fstrcpy( local_machine, global_myname() );
-       trim_char( local_machine, ' ', ' ' );
-       p = strchr( local_machine, ' ' );
-       if (p)
-               *p = 0;
-       strlower_m( local_machine );
+       set_local_machine_name(global_myname(),false);
 
        DEBUG( 5, ("Netbios name list:-\n") );
-       for( n=0; my_netbios_names(n); n++ )
-               DEBUGADD( 5, ( "my_netbios_names[%d]=\"%s\"\n", n, my_netbios_names(n) ) );
+       for( n=0; my_netbios_names(n); n++ ) {
+               DEBUGADD( 5, ("my_netbios_names[%d]=\"%s\"\n",
+                                       n, my_netbios_names(n) ) );
+       }
 
        return( True );
 }
@@ -1988,6 +1982,16 @@ void ra_lanman_string( const char *native_lanman )
                set_remote_arch( RA_WIN2K3 );
 }
 
+static const char *remote_arch_str;
+
+const char *get_remote_arch_str(void)
+{
+       if (!remote_arch_str) {
+               return "UNKNOWN";
+       }
+       return remote_arch_str;
+}
+
 /*******************************************************************
  Set the horrid remote_arch string based on an enum.
 ********************************************************************/
@@ -1997,42 +2001,43 @@ void set_remote_arch(enum remote_arch_types type)
        ra_type = type;
        switch( type ) {
        case RA_WFWG:
-               fstrcpy(remote_arch, "WfWg");
+               remote_arch_str = "WfWg";
                break;
        case RA_OS2:
-               fstrcpy(remote_arch, "OS2");
+               remote_arch_str = "OS2";
                break;
        case RA_WIN95:
-               fstrcpy(remote_arch, "Win95");
+               remote_arch_str = "Win95";
                break;
        case RA_WINNT:
-               fstrcpy(remote_arch, "WinNT");
+               remote_arch_str = "WinNT";
                break;
        case RA_WIN2K:
-               fstrcpy(remote_arch, "Win2K");
+               remote_arch_str = "Win2K";
                break;
        case RA_WINXP:
-               fstrcpy(remote_arch, "WinXP");
+               remote_arch_str = "WinXP";
                break;
        case RA_WIN2K3:
-               fstrcpy(remote_arch, "Win2K3");
+               remote_arch_str = "Win2K3";
                break;
        case RA_VISTA:
-               fstrcpy(remote_arch, "Vista");
+               remote_arch_str = "Vista";
                break;
        case RA_SAMBA:
-               fstrcpy(remote_arch,"Samba");
+               remote_arch_str = "Samba";
                break;
        case RA_CIFSFS:
-               fstrcpy(remote_arch,"CIFSFS");
+               remote_arch_str = "CIFSFS";
                break;
        default:
                ra_type = RA_UNKNOWN;
-               fstrcpy(remote_arch, "UNKNOWN");
+               remote_arch_str = "UNKNOWN";
                break;
        }
 
-       DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n", remote_arch));
+       DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
+                               remote_arch_str));
 }
 
 /*******************************************************************
index aaa56f92169dc7c6abb974ec1493af45be49da66..300368cd60de37b2c11d09c9e6097fc91013db41 100644 (file)
@@ -28,8 +28,6 @@
 
 #include "includes.h"
 
-extern fstring local_machine;
-
 struct sync_record {
        struct sync_record *next, *prev;
        unstring workgroup;
@@ -92,7 +90,7 @@ static void sync_child(char *name, int nm_type,
                return;
        }
 
-       make_nmb_name(&calling, local_machine, 0x0);
+       make_nmb_name(&calling, get_local_machine_name(), 0x0);
        make_nmb_name(&called , name, nm_type);
 
        if (!cli_session_request(cli, &calling, &called)) {
index 547e9c6e8162db5179e6ebab626de88bf6bc82b7..e89a94599efee951874e113dc3987ad21d2063b7 100644 (file)
@@ -379,8 +379,7 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
 
                                /* ready to listen */
                                set_socket_options(s,"SO_KEEPALIVE");
-                               set_socket_options(smbd_server_fd(),
-                                                  lp_socket_options());
+                               set_socket_options(s,lp_socket_options());
 
                                /* Set server socket to
                                 * non-blocking for the accept. */
@@ -446,8 +445,7 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
 
                                /* ready to listen */
                                set_socket_options(s,"SO_KEEPALIVE");
-                               set_socket_options(smbd_server_fd(),
-                                                  lp_socket_options());
+                               set_socket_options(s,lp_socket_options());
 
                                /* Set server socket to non-blocking
                                 * for the accept. */