Make getpwnam_alloc() static to lib/username.c, and ensure all username lookups go
authorJeremy Allison <jra@samba.org>
Wed, 20 Oct 2010 15:16:23 +0000 (08:16 -0700)
committerJeremy Allison <jra@samba.org>
Wed, 20 Oct 2010 16:02:12 +0000 (16:02 +0000)
through Get_Pwnam_alloc(), which is the correct wrapper function. We were using
it *some* of the time anyway, so this just makes us properly consistent.

Jeremy.

Autobuild-User: Jeremy Allison <jra@samba.org>
Autobuild-Date: Wed Oct 20 16:02:12 UTC 2010 on sn-devel-104

17 files changed:
source3/Makefile.in
source3/auth/auth_util.c
source3/auth/server_info_sam.c
source3/auth/token_util.c
source3/include/proto.h
source3/lib/username.c
source3/lib/util.c
source3/lib/util_pw.c [deleted file]
source3/passdb/pdb_interface.c
source3/passdb/pdb_smbpasswd.c
source3/passdb/util_unixsids.c
source3/smbd/password.c
source3/smbd/uid.c
source3/torture/pdbtest.c
source3/utils/net_sam.c
source3/web/cgi.c
source3/wscript_build

index 86cd88af646022c7a779d481e64a1118b9b3bfe2..5d390def48e73c67b4762f5592bf4c31ebea772b 100644 (file)
@@ -453,7 +453,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \
          lib/system.o lib/sendfile.o lib/recvfile.o lib/time.o \
          lib/username.o \
          ../libds/common/flag_mapping.o \
-         lib/util_pw.o lib/access.o lib/smbrun.o \
+         lib/access.o lib/smbrun.o \
          lib/bitmap.o lib/dprintf.o $(UTIL_REG_OBJ) \
          lib/wins_srv.o \
          lib/util_str.o lib/clobber.o lib/util_sid.o \
index 73713093c2183140b4c2461644b039daf7745792..2fcee89bbbaf8e3118eecb7b51a4f0edb75e62e9 100644 (file)
@@ -641,7 +641,7 @@ static NTSTATUS get_guest_info3(TALLOC_CTX *mem_ctx,
        struct passwd *pwd;
        const char *tmp;
 
-       pwd = getpwnam_alloc(mem_ctx, guest_account);
+       pwd = Get_Pwnam_alloc(mem_ctx, guest_account);
        if (pwd == NULL) {
                DEBUG(0,("SamInfo3_for_guest: Unable to locate guest "
                         "account [%s]!\n", guest_account));
@@ -793,7 +793,7 @@ NTSTATUS make_serverinfo_from_username(TALLOC_CTX *mem_ctx,
        struct passwd *pwd;
        NTSTATUS status;
 
-       pwd = getpwnam_alloc(talloc_tos(), username);
+       pwd = Get_Pwnam_alloc(talloc_tos(), username);
        if (pwd == NULL) {
                return NT_STATUS_NO_SUCH_USER;
        }
index 4731ad44f1ada9d5dcb0fcdb4f1b861a3a4cddc5..c09ae764b95cd60142e124b7bfc52dc74bce79c2 100644 (file)
@@ -70,7 +70,7 @@ NTSTATUS make_server_info_sam(struct auth_serversupplied_info **server_info,
                return NT_STATUS_NO_MEMORY;
        }
 
-       if ( !(pwd = getpwnam_alloc(result, username)) ) {
+       if ( !(pwd = Get_Pwnam_alloc(result, username)) ) {
                DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
                          pdb_get_username(sampass)));
                TALLOC_FREE(result);
index 1a7c4816e8223e0cdc6e3c615d8eb58af4bf5581..4a702ef23047fab309e12ea61535e9d626475200 100644 (file)
@@ -757,14 +757,14 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
                 * about the mapping of guest sid to lp_guestaccount()
                 * username and will return the unix_pw info for a guest
                 * user. Use it if it's there, else lookup the *uid details
-                * using getpwnam_alloc(). See bug #6291 for details. JRA.
+                * using Get_Pwnam_alloc(). See bug #6291 for details. JRA.
                 */
 
                /* We must always assign the *uid. */
                if (sam_acct->unix_pw == NULL) {
-                       struct passwd *pwd = getpwnam_alloc(sam_acct, *found_username );
+                       struct passwd *pwd = Get_Pwnam_alloc(sam_acct, *found_username );
                        if (!pwd) {
-                               DEBUG(10, ("getpwnam_alloc failed for %s\n",
+                               DEBUG(10, ("Get_Pwnam_alloc failed for %s\n",
                                        *found_username));
                                result = NT_STATUS_NO_SUCH_USER;
                                goto done;
index 9a8cf67756e3b9b1cb11d8f91bdbc8ebeb172aed..3e4655eeb5d7d152d679319d7949cbb42d882aee 100644 (file)
@@ -1033,6 +1033,9 @@ bool nt_time_is_set(const NTTIME *nt);
 
 /* The following definitions come from lib/username.c  */
 
+struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from) ;
+void flush_pwnam_cache(void);
+struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) ;
 char *get_user_home_dir(TALLOC_CTX *mem_ctx, const char *user);
 struct passwd *Get_Pwnam_alloc(TALLOC_CTX *mem_ctx, const char *user);
 
@@ -1221,13 +1224,6 @@ NTSTATUS merge_nt_token(TALLOC_CTX *mem_ctx,
                        struct security_token **token_out);
 bool token_sid_in_ace(const struct security_token *token, const struct security_ace *ace);
 
-/* The following definitions come from lib/util_pw.c  */
-
-struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from) ;
-void flush_pwnam_cache(void);
-struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name);
-struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) ;
-
 /* The following definitions come from ..libcli/registry/util_reg.c  */
 
 const char *str_regtype(int type);
index 3087bac0f4b1ec50d188a3bc6467959e69d0da01..afe83acfd9b61739c8d079c71023c7c873993e0f 100644 (file)
@@ -3,7 +3,8 @@
    Username handling
    Copyright (C) Andrew Tridgell 1992-1998
    Copyright (C) Jeremy Allison 1997-2001.
-   
+   Copyright (C) Andrew Bartlett 2002
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
@@ -19,6 +20,7 @@
 */
 
 #include "includes.h"
+#include "memcache.h"
 
 /* internal functions */
 static struct passwd *uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
@@ -28,6 +30,76 @@ static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx, i
                                                 struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
                                                 int N);
 
+static struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
+{
+       struct passwd *pw, *for_cache;
+
+       pw = (struct passwd *)memcache_lookup_talloc(
+               NULL, GETPWNAM_CACHE, data_blob_string_const_null(name));
+       if (pw != NULL) {
+               return tcopy_passwd(mem_ctx, pw);
+       }
+
+       pw = sys_getpwnam(name);
+       if (pw == NULL) {
+               return NULL;
+       }
+
+       for_cache = tcopy_passwd(talloc_tos(), pw);
+       if (for_cache == NULL) {
+               return NULL;
+       }
+
+       memcache_add_talloc(NULL, GETPWNAM_CACHE,
+                       data_blob_string_const_null(name), &for_cache);
+
+       return tcopy_passwd(mem_ctx, pw);
+}
+
+/****************************************************************************
+ talloc copy a struct passwd.
+****************************************************************************/
+
+struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from)
+{
+       struct passwd *ret = TALLOC_P(mem_ctx, struct passwd);
+       if (!ret) {
+               return NULL;
+       }
+       ret->pw_name = talloc_strdup(ret, from->pw_name);
+       ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
+       ret->pw_uid = from->pw_uid;
+       ret->pw_gid = from->pw_gid;
+       ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
+       ret->pw_dir = talloc_strdup(ret, from->pw_dir);
+       ret->pw_shell = talloc_strdup(ret, from->pw_shell);
+       return ret;
+}
+
+/****************************************************************************
+ Flush all cached passwd structs.
+****************************************************************************/
+
+void flush_pwnam_cache(void)
+{
+        memcache_flush(NULL, GETPWNAM_CACHE);
+}
+
+/****************************************************************************
+ talloc'ed version of getpwuid.
+****************************************************************************/
+
+struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid)
+{
+       struct passwd *temp = sys_getpwuid(uid);
+
+       if (!temp) {
+               return NULL;
+       }
+
+       return tcopy_passwd(mem_ctx, temp);
+}
+
 /****************************************************************************
  Get a users home directory.
 ****************************************************************************/
index 36d966126e1388c9140e175f0d51d1a4bc1a4857..36b2e422e2177b073ea48e8897fa6d9cb321cb98 100644 (file)
@@ -1306,7 +1306,7 @@ uid_t nametouid(const char *name)
        char *p;
        uid_t u;
 
-       pass = getpwnam_alloc(talloc_tos(), name);
+       pass = Get_Pwnam_alloc(talloc_tos(), name);
        if (pass) {
                u = pass->pw_uid;
                TALLOC_FREE(pass);
diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c
deleted file mode 100644 (file)
index 5ba5fde..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/* 
-   Unix SMB/CIFS implementation.
-
-   Safe versions of getpw* calls
-
-   Copyright (C) Andrew Bartlett 2002
-   
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-   
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-   
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#include "includes.h"
-#include "memcache.h"
-
-struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from) 
-{
-       struct passwd *ret = TALLOC_P(mem_ctx, struct passwd);
-       if (!ret) {
-               return NULL;
-       }
-       ret->pw_name = talloc_strdup(ret, from->pw_name);
-       ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
-       ret->pw_uid = from->pw_uid;
-       ret->pw_gid = from->pw_gid;
-       ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
-       ret->pw_dir = talloc_strdup(ret, from->pw_dir);
-       ret->pw_shell = talloc_strdup(ret, from->pw_shell);
-       return ret;
-}
-
-void flush_pwnam_cache(void)
-{
-       memcache_flush(NULL, GETPWNAM_CACHE);
-}
-
-struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
-{
-       struct passwd *pw, *for_cache;
-
-       pw = (struct passwd *)memcache_lookup_talloc(
-               NULL, GETPWNAM_CACHE, data_blob_string_const_null(name));
-       if (pw != NULL) {
-               return tcopy_passwd(mem_ctx, pw);
-       }
-
-       pw = sys_getpwnam(name);
-       if (pw == NULL) {
-               return NULL;
-       }
-
-       for_cache = tcopy_passwd(talloc_tos(), pw);
-       if (for_cache == NULL) {
-               return NULL;
-       }
-
-       memcache_add_talloc(NULL, GETPWNAM_CACHE,
-                           data_blob_string_const_null(name), &for_cache);
-
-       return tcopy_passwd(mem_ctx, pw);
-}
-
-struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) 
-{
-       struct passwd *temp;
-
-       temp = sys_getpwuid(uid);
-       
-       if (!temp) {
-#if 0
-               if (errno == ENOMEM) {
-                       /* what now? */
-               }
-#endif
-               return NULL;
-       }
-
-       return tcopy_passwd(mem_ctx, temp);
-}
index a0913a3ea926ba2cc23ac0a4562f84f3de3eaf91..a6fe2e0408777ba9e3884f29a954615a8039f349 100644 (file)
@@ -352,7 +352,7 @@ static bool guest_user_info( struct samu *user )
        NTSTATUS result;
        const char *guestname = lp_guestaccount();
 
-       pwd = getpwnam_alloc(talloc_tos(), guestname);
+       pwd = Get_Pwnam_alloc(talloc_tos(), guestname);
        if (pwd == NULL) {
                DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n", 
                        guestname));
@@ -1546,7 +1546,7 @@ static NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
        /* Ignore the primary group SID.  Honor the real Unix primary group.
           The primary group SID is only of real use to Windows clients */
 
-       if ( !(pw = getpwnam_alloc(mem_ctx, username)) ) {
+       if ( !(pw = Get_Pwnam_alloc(mem_ctx, username)) ) {
                return NT_STATUS_NO_SUCH_USER;
        }
 
index f188399f78005d7b83ca21fb906370575acdd330..8d6ac318cb751ee3344166e921d2c42434d000c3 100644 (file)
@@ -1198,9 +1198,9 @@ static bool build_smb_pass (struct smb_passwd *smb_pw, const struct samu *sampas
 
                /* If the user specified a RID, make sure its able to be both stored and retreived */
                if (rid == DOMAIN_RID_GUEST) {
-                       struct passwd *passwd = getpwnam_alloc(NULL, lp_guestaccount());
+                       struct passwd *passwd = Get_Pwnam_alloc(NULL, lp_guestaccount());
                        if (!passwd) {
-                               DEBUG(0, ("Could not find guest account via getpwnam()! (%s)\n", lp_guestaccount()));
+                               DEBUG(0, ("Could not find guest account via Get_Pwnam_alloc()! (%s)\n", lp_guestaccount()));
                                return False;
                        }
                        smb_pw->smb_userid=passwd->pw_uid;
index ce64dc2d8869600a684f4c7add8f9a2bb3dd9350..1f830b35c5e8948b5c0854880bc86caa1feb92b4 100644 (file)
@@ -64,7 +64,7 @@ bool lookup_unix_user_name(const char *name, struct dom_sid *sid)
        struct passwd *pwd;
        bool ret;
 
-       pwd = getpwnam_alloc(talloc_tos(), name);
+       pwd = Get_Pwnam_alloc(talloc_tos(), name);
        if (pwd == NULL) {
                return False;
        }
index afe4f1b91f314e3c9947b0938a285a15fe58fedb..cbe4d62595739d372e24f990ea0a7c81f45d5691 100644 (file)
@@ -222,7 +222,7 @@ int register_homes_share(const char *username)
                return result;
        }
 
-       pwd = getpwnam_alloc(talloc_tos(), username);
+       pwd = Get_Pwnam_alloc(talloc_tos(), username);
 
        if ((pwd == NULL) || (pwd->pw_dir[0] == '\0')) {
                DEBUG(3, ("No home directory defined for user '%s'\n",
index 2b16d05b7c9414ad2bbfdb855eb780a626d9d218..dd6af6d9c018950f38ffffd5607a43d98a24b471 100644 (file)
@@ -33,7 +33,7 @@ bool change_to_guest(void)
 {
        struct passwd *pass;
 
-       pass = getpwnam_alloc(talloc_tos(), lp_guestaccount());
+       pass = Get_Pwnam_alloc(talloc_tos(), lp_guestaccount());
        if (!pass) {
                return false;
        }
index cb61489f008990d2a039caf2bfe836b1aad8f990..6745ab0f7d24d9ee1787de8ea3c9b21aa984c816 100644 (file)
@@ -278,7 +278,7 @@ int main(int argc, char **argv)
                exit(1);
        }
        
-       if ((pwd = getpwnam_alloc(ctx, unix_user)) == NULL) {
+       if ((pwd = Get_Pwnam_alloc(ctx, unix_user)) == NULL) {
                fprintf(stderr, "Error getting user information for %s\n", unix_user);
                exit(1);
        }
index 51d49849ae135697048ca58550574b2aa347d8ad..8a9ca8ad1819bb293589d804e57858373a79e806 100644 (file)
@@ -1856,7 +1856,7 @@ doma_done:
 
                sid_compose(&sid, get_global_sam_sid(), DOMAIN_RID_GUEST);
 
-               pwd = getpwnam_alloc(tc, lp_guestaccount());
+               pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
 
                if (!pwd) {
                        if (domusers_gid == -1) {
@@ -1927,7 +1927,7 @@ doma_done:
 
        d_printf(_("Checking Guest's group.\n"));
 
-       pwd = getpwnam_alloc(tc, lp_guestaccount());
+       pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
        if (!pwd) {
                d_fprintf(stderr,
                          _("Failed to find just created Guest account!\n"
index 1a472a77c7060aa0af90b970bd7d71e2c5d6de6f..e3356a2f7ed8a475efa08bf6ebbcf4c0c53070c1 100644 (file)
@@ -314,7 +314,7 @@ static void cgi_web_auth(void)
                exit(0);
        }
 
-       pwd = getpwnam_alloc(talloc_tos(), user);
+       pwd = Get_Pwnam_alloc(talloc_tos(), user);
        if (!pwd) {
                printf("%sCannot find user %s<br>%s\n", head, user, tail);
                exit(0);
@@ -369,7 +369,7 @@ static bool cgi_handle_authorization(char *line)
         * Try and get the user from the UNIX password file.
         */
 
-       pass = getpwnam_alloc(talloc_tos(), user);
+       pass = Get_Pwnam_alloc(talloc_tos(), user);
 
        rhost = client_name(1);
        if (strequal(rhost,"UNKNOWN"))
index 9f781741b34b931fdc92fce17d783dbb76a0ed52..75025970ace73b683c5886bc31b5fc1a23170abc 100644 (file)
@@ -178,7 +178,7 @@ LIB_SRC = '''${LIBSAMBAUTIL_SRC} ${UTIL_SRC}
           lib/system.c lib/sendfile.c lib/recvfile.c lib/time.c
           lib/username.c
           ../libds/common/flag_mapping.c
-          lib/util_pw.c lib/access.c lib/smbrun.c
+          lib/access.c lib/smbrun.c
           lib/bitmap.c lib/dprintf.c ${UTIL_REG_SRC}
           lib/wins_srv.c
           lib/util_str.c lib/clobber.c lib/util_sid.c