Add a bit more const, and kill of (finally!) sys_getpwnam and sys_getpwuid.
authorAndrew Bartlett <abartlet@samba.org>
Wed, 22 May 2002 12:44:45 +0000 (12:44 +0000)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 22 May 2002 12:44:45 +0000 (12:44 +0000)
These might be reimplmented as simple pass-through functions, but all users
really should be doing 'getpwnam_alloc' or 'getpwuid_alloc' to ensure that
there are not shared static buffers.

I don't beleive we actually need a getpw*() cache inside samba - if we do
then I think we should look at our code design first.

(some of these changes are for platforms I don't have access to, but
they look sane)

Andrew Bartlett

source/lib/sysacls.c
source/lib/system.c
source/lib/util.c
source/rpc_server/srv_samr_nt.c
source/utils/smbpasswd.c
source/utils/status.c

index 22245992f53594064258b8d9785cb3725e6b64bf..00d06e4a5aead067301bc9e8a707ef4d3e346400 100644 (file)
@@ -644,13 +644,7 @@ char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
                                break;
 
                        case SMB_ACL_USER:
-                               if ((pw = sys_getpwuid(ap->a_id)) == NULL) {
-                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
-                                               (long)ap->a_id);
-                                       id = idbuf;
-                               } else {
-                                       id = pw->pw_name;
-                               }
+                               id = uidtoname(ap->a_id);
                        case SMB_ACL_USER_OBJ:
                                tag = "user";
                                break;
@@ -1281,13 +1275,7 @@ char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
                                break;
 
                        case SMB_ACL_USER:
-                               if ((pw = sys_getpwuid(ap->a_id)) == NULL) {
-                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
-                                               (long)ap->a_id);
-                                       id = idbuf;
-                               } else {
-                                       id = pw->pw_name;
-                               }
+                               id = uidtoname(ap->a_id);
                        case SMB_ACL_USER_OBJ:
                                tag = "user";
                                break;
index 9953df70582351deb358fa7437fe61b2a23d8a37..3bf0994621c99270da143ffa652c497938b4689a 100644 (file)
@@ -744,133 +744,25 @@ int sys_setgroups(int setlen, gid_t *gidset)
 
 #endif /* HAVE_SETGROUPS */
 
-/*
- * We only wrap pw_name and pw_passwd for now as these
- * are the only potentially modified fields.
- */
-
-/**************************************************************************
- Helper function for getpwnam/getpwuid wrappers.
-****************************************************************************/
-
-struct saved_pw {
-       fstring         pw_name;
-       fstring         pw_passwd;
-       fstring         pw_gecos;
-       pstring         pw_dir;
-       pstring         pw_shell;
-       struct passwd pass;
-};
-
-static struct saved_pw pw_mod; /* This is the structure returned - can be modified. */
-static struct saved_pw pw_cache; /* This is the structure saved - used to check cache. */
-
-static int num_lookups; /* Counter so we don't always use cache. */
-#ifndef PW_RET_CACHE_MAX_LOOKUPS
-#define PW_RET_CACHE_MAX_LOOKUPS 100
-#endif
-
-static void copy_pwent(struct saved_pw *dst, struct passwd *pass)
-{
-       memcpy((char *)&dst->pass, pass, sizeof(struct passwd));
-
-       fstrcpy(dst->pw_name, pass->pw_name);
-       dst->pass.pw_name = dst->pw_name;
-
-       fstrcpy(dst->pw_passwd, pass->pw_passwd);
-       dst->pass.pw_passwd = dst->pw_passwd;
-
-       fstrcpy(dst->pw_gecos, pass->pw_gecos);
-       dst->pass.pw_gecos = dst->pw_gecos;
-
-       pstrcpy(dst->pw_dir, pass->pw_dir);
-       dst->pass.pw_dir = dst->pw_dir;
-
-       pstrcpy(dst->pw_shell, pass->pw_shell);
-       dst->pass.pw_shell = dst->pw_shell;
-}
-
-static struct passwd *setup_pwret(struct passwd *pass)
-{
-       if (pass == NULL) {
-               /* Clear the caches. */
-               memset(&pw_cache, '\0', sizeof(struct saved_pw));
-               memset(&pw_mod, '\0', sizeof(struct saved_pw));
-               num_lookups = 0;
-               return NULL;
-       }
-
-       copy_pwent( &pw_mod, pass);
-
-       if (pass != &pw_cache.pass) {
-
-               /* If it's a cache miss we must also refill the cache. */
-
-               copy_pwent( &pw_cache, pass);
-               num_lookups = 1;
-
-       } else {
-
-               /* Cache hit. */
-
-               num_lookups++;
-               num_lookups = (num_lookups % PW_RET_CACHE_MAX_LOOKUPS);
-       }
-
-       return &pw_mod.pass;
-}
-
 /**************************************************************************
  Wrappers for setpwent(), getpwent() and endpwent()
 ****************************************************************************/
 
 void sys_setpwent(void)
 {
-       setup_pwret(NULL); /* Clear cache. */
        setpwent();
 }
 
 struct passwd *sys_getpwent(void)
 {
-       return setup_pwret(getpwent());
+       return getpwent();
 }
 
 void sys_endpwent(void)
 {
-       setup_pwret(NULL); /* Clear cache. */
        endpwent();
 }
 
-/**************************************************************************
- Wrapper for getpwnam(). Always returns a static that can be modified.
-****************************************************************************/
-
-struct passwd *sys_getpwnam(const char *name)
-{
-       if (!name || !name[0])
-               return NULL;
-
-       /* check for a cache hit first */
-       if (num_lookups && pw_cache.pass.pw_name && !strcmp(name, pw_cache.pass.pw_name)) {
-               return setup_pwret(&pw_cache.pass);
-       }
-
-       return setup_pwret(getpwnam(name));
-}
-
-/**************************************************************************
- Wrapper for getpwuid(). Always returns a static that can be modified.
-****************************************************************************/
-
-struct passwd *sys_getpwuid(uid_t uid)
-{
-       if (num_lookups && pw_cache.pass.pw_name && (uid == pw_cache.pass.pw_uid)) {
-               return setup_pwret(&pw_cache.pass);
-       }
-       
-       return setup_pwret(getpwuid(uid));
-}
-
 #if 0 /* NOT CURRENTLY USED - JRA */
 /**************************************************************************
  The following are the UNICODE versions of *all* system interface functions
index a23ef91a31e194a833aaec7dc09ecd385075c355..2fe9ec331b20fa58daca42929a153fadaf33fc2a 100644 (file)
@@ -1052,16 +1052,19 @@ BOOL process_exists(pid_t pid)
  Convert a uid into a user name.
 ********************************************************************/
 
-char *uidtoname(uid_t uid)
+const char *uidtoname(uid_t uid)
 {
        static fstring name;
        struct passwd *pass;
 
-       pass = sys_getpwuid(uid);
-       if (pass)
-               return(pass->pw_name);
-       slprintf(name, sizeof(name) - 1, "%d",(int)uid);
-       return(name);
+       pass = getpwuid_alloc(uid);
+       if (pass) {
+               fstrcpy(name, pass->pw_name);
+               passwd_free(&pass);
+       } else {
+               slprintf(name, sizeof(name) - 1, "%ld",(long int)uid);
+       }
+       return name;
 }
 
 
index 106d7c19234486c76520db7f0e1fa6af3ec8fe18..6a623bd2a0a8149aa39d38d6bbba236e95177b9d 100644 (file)
@@ -2935,8 +2935,11 @@ NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_AD
 
        pdb_free_sam(&sam_user);
 
-       if ((pwd=getpwuid(uid)) == NULL)
+       if ((pwd=getpwuid_alloc(uid)) == NULL) {
                return NT_STATUS_NO_SUCH_USER;
+       } else {
+               passwd_free(&pwd);
+       }
 
        if ((grp=getgrgid(map.gid)) == NULL)
                return NT_STATUS_NO_SUCH_ALIAS;
@@ -3076,8 +3079,11 @@ NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_AD
 
        pdb_free_sam(&sam_user);
 
-       if ((pwd=getpwuid(uid)) == NULL)
+       if ((pwd=getpwuid_alloc(uid)) == NULL) {
                return NT_STATUS_NO_SUCH_USER;
+       } else {
+               passwd_free(&pwd);
+       }
 
        if ((grp=getgrgid(map.gid)) == NULL)
                return NT_STATUS_NO_SUCH_GROUP;
index 5d219b6907b35685fe11697c26b0cd8c6a212660..70bf551edbfdb3561971e0230bf22477a849a22f 100644 (file)
@@ -367,8 +367,9 @@ static int process_root(int local_flags)
                load_interfaces();
        }
 
-       if (!user_name[0] && (pwd = sys_getpwuid(geteuid()))) {
+       if (!user_name[0] && (pwd = getpwuid_alloc(geteuid()))) {
                fstrcpy(user_name, pwd->pw_name);
+               passwd_free(&pwd);
        } 
 
        if (!user_name[0]) {
@@ -504,9 +505,10 @@ static int process_nonroot(int local_flags)
        }
 
        if (!user_name[0]) {
-               pwd = sys_getpwuid(getuid());
+               pwd = getpwuid_alloc(getuid());
                if (pwd) {
                        fstrcpy(user_name,pwd->pw_name);
+                       passwd_free(&pwd);
                } else {
                        fprintf(stderr, "smbpasswd: you don't exist - go away\n");
                        exit(1);
index 7755ce9ab4e3d5dcde9b326cc31806d5671a114a..f230cd8466d8768451543e363bedc8623568c0d4 100644 (file)
@@ -61,14 +61,14 @@ static int show_brl;
 
 
 /* added by OH */
-static void Ucrit_addUsername(char *username)
+static void Ucrit_addUsername(const char *username)
 {
        pstrcpy(Ucrit_username, username);
        if(strlen(Ucrit_username) > 0)
                Ucrit_IsActive = 1;
 }
 
-static unsigned int Ucrit_checkUsername(char *username)
+static unsigned int Ucrit_checkUsername(const char *username)
 {
        if ( !Ucrit_IsActive) return 1;
        if (strcmp(Ucrit_username,username) ==0) return 1;