Remove more static fstrings (yes this little cache should be
authorJeremy Allison <jra@samba.org>
Tue, 18 Dec 2007 01:27:29 +0000 (17:27 -0800)
committerJeremy Allison <jra@samba.org>
Tue, 18 Dec 2007 01:27:29 +0000 (17:27 -0800)
in the rbtree....).
Jeremy.
(This used to be commit 97cfdae4052d46a35040d4c1a4ade8bf2c41dbc7)

source3/smbd/map_username.c

index c04e0f1ae236877798e61bbc799b7546f09e6e05..bde755eff6a40a4f20407f00d183837d209ad3dd 100644 (file)
  Returns True if username was changed, false otherwise.
 ********************************************************************/
 
+static char *last_from, *last_to;
+
+static const char *get_last_from(void)
+{
+       if (!last_from) {
+               return "";
+       }
+       return last_from;
+}
+
+static const char *get_last_to(void)
+{
+       if (!last_to) {
+               return "";
+       }
+       return last_to;
+}
+
+static bool set_last_from_to(const char *from, const char *to)
+{
+       char *orig_from = last_from;
+       char *orig_to = last_to;
+
+       last_from = SMB_STRDUP(from);
+       last_to = SMB_STRDUP(to);
+
+       SAFE_FREE(orig_from);
+       SAFE_FREE(orig_to);
+
+       if (!last_from || !last_to) {
+               SAFE_FREE(last_from);
+               SAFE_FREE(last_to);
+               return false;
+       }
+       return true;
+}
+
 bool map_username(fstring user)
 {
-       static bool initialised=False;
-       static fstring last_from,last_to;
        XFILE *f;
        char *mapfile = lp_username_map();
        char *s;
@@ -46,12 +81,12 @@ bool map_username(fstring user)
        if (!*user)
                return false;
 
-       if (strequal(user,last_to))
+       if (strequal(user,get_last_to()))
                return false;
 
-       if (strequal(user,last_from)) {
-               DEBUG(3,("Mapped user %s to %s\n",user,last_to));
-               fstrcpy(user,last_to);
+       if (strequal(user,get_last_from())) {
+               DEBUG(3,("Mapped user %s to %s\n",user,get_last_to()));
+               fstrcpy(user,get_last_to());
                return true;
        }
 
@@ -98,15 +133,9 @@ bool map_username(fstring user)
        }
 
        /* ok.  let's try the mapfile */
-       
        if (!*mapfile)
                return False;
 
-       if (!initialised) {
-               *last_from = *last_to = 0;
-               initialised = True;
-       }
-  
        f = x_fopen(mapfile,O_RDONLY, 0);
        if (!f) {
                DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
@@ -135,7 +164,7 @@ bool map_username(fstring user)
                        while (*unixname && isspace((int)*unixname))
                                unixname++;
                }
-    
+
                if (!*unixname || strchr_m("#;",*unixname))
                        continue;
 
@@ -159,27 +188,28 @@ bool map_username(fstring user)
                    user_in_list(user, (const char **)dosuserlist)) {
                        DEBUG(3,("Mapped user %s to %s\n",user,unixname));
                        mapped_user = True;
-                       fstrcpy( last_from,user );
+
+                       set_last_from_to(user, unixname);
                        fstrcpy( user, unixname );
-                       fstrcpy( last_to,user );
+
                        if ( return_if_mapped ) {
                                str_list_free (&dosuserlist);
                                x_fclose(f);
                                return True;
                        }
                }
-    
+
                str_list_free (&dosuserlist);
        }
 
        x_fclose(f);
 
        /*
-        * Setup the last_from and last_to as an optimization so 
+        * Setup the last_from and last_to as an optimization so
         * that we don't scan the file again for the same user.
         */
-       fstrcpy(last_from,user);
-       fstrcpy(last_to,user);
+
+       set_last_from_to(user, user);
 
        return mapped_user;
 }