The branch, v3-6-test has been updated via dde9059 Make getpwnam_alloc() static to lib/username.c, and ensure all username lookups go 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. from f74e696 s3: Cope with EINTR in smbd_[un]lock_socket (cherry picked from commit da00021a7c236f9ac87442b96881bf0b2701ad0b)
http://gitweb.samba.org/?p=samba.git;a=shortlog;h=v3-6-test - Log ----------------------------------------------------------------- commit dde9059287f24e920d5100933dbff1f03c09f326 Author: Jeremy Allison <j...@samba.org> Date: Wed Oct 20 11:06:06 2010 -0700 Make getpwnam_alloc() static to lib/username.c, and ensure all username lookups go 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. ----------------------------------------------------------------------- Summary of changes: source3/Makefile.in | 2 +- source3/auth/auth_util.c | 2 +- source3/auth/server_info_sam.c | 2 +- source3/auth/token_util.c | 6 +- source3/include/proto.h | 10 +--- source3/lib/username.c | 62 ++++++++++++++++++++++++++++ source3/lib/util.c | 2 +- source3/lib/util_pw.c | 88 ---------------------------------------- source3/passdb/pdb_interface.c | 4 +- source3/passdb/pdb_smbpasswd.c | 2 +- source3/passdb/util_unixsids.c | 2 +- source3/smbd/password.c | 2 +- source3/smbd/uid.c | 2 +- source3/torture/pdbtest.c | 2 +- source3/utils/net_sam.c | 4 +- source3/web/cgi.c | 4 +- 16 files changed, 83 insertions(+), 113 deletions(-) delete mode 100644 source3/lib/util_pw.c Changeset truncated at 500 lines: diff --git a/source3/Makefile.in b/source3/Makefile.in index ba75c9e..45dd891 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -451,7 +451,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 lib/util_uuid.o \ diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c index 16fa421..2fb488d 100644 --- a/source3/auth/auth_util.c +++ b/source3/auth/auth_util.c @@ -729,7 +729,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; } diff --git a/source3/auth/server_info_sam.c b/source3/auth/server_info_sam.c index 4731ad4..c09ae76 100644 --- a/source3/auth/server_info_sam.c +++ b/source3/auth/server_info_sam.c @@ -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); diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c index 8f66171..5c93839 100644 --- a/source3/auth/token_util.c +++ b/source3/auth/token_util.c @@ -789,14 +789,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; diff --git a/source3/include/proto.h b/source3/include/proto.h index 4535560..f7f751c 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -1134,6 +1134,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); @@ -1322,13 +1325,6 @@ NTSTATUS merge_nt_token(TALLOC_CTX *mem_ctx, struct nt_user_token **token_out); bool token_sid_in_ace(const NT_USER_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); diff --git a/source3/lib/username.c b/source3/lib/username.c index 3087bac..5a11848 100644 --- a/source3/lib/username.c +++ b/source3/lib/username.c @@ -3,6 +3,7 @@ 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 @@ -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,66 @@ 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_autofree_context(), 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 *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 *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) +{ + struct passwd *temp; + + temp = sys_getpwuid(uid); + + if (!temp) { + return NULL; + } + + return tcopy_passwd(mem_ctx, temp); +} + /**************************************************************************** Get a users home directory. ****************************************************************************/ diff --git a/source3/lib/util.c b/source3/lib/util.c index 7fd0fc3..fb527ac 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -1305,7 +1305,7 @@ uid_t nametouid(const char *name) char *p; uid_t u; - pass = getpwnam_alloc(talloc_autofree_context(), name); + pass = Get_Pwnam_alloc(talloc_autofree_context(), 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 index 4d4041c..0000000 --- a/source3/lib/util_pw.c +++ /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_autofree_context(), 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); -} diff --git a/source3/passdb/pdb_interface.c b/source3/passdb/pdb_interface.c index ddc7ad4..7a152a7 100644 --- a/source3/passdb/pdb_interface.c +++ b/source3/passdb/pdb_interface.c @@ -258,7 +258,7 @@ static bool guest_user_info( struct samu *user ) NTSTATUS result; const char *guestname = lp_guestaccount(); - if ( !(pwd = getpwnam_alloc(talloc_autofree_context(), guestname ) ) ) { + if ( !(pwd = Get_Pwnam_alloc(talloc_autofree_context(), guestname ) ) ) { DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n", guestname)); return False; @@ -1427,7 +1427,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; } diff --git a/source3/passdb/pdb_smbpasswd.c b/source3/passdb/pdb_smbpasswd.c index dd89c8e..13756b6 100644 --- a/source3/passdb/pdb_smbpasswd.c +++ b/source3/passdb/pdb_smbpasswd.c @@ -1197,7 +1197,7 @@ 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())); return False; diff --git a/source3/passdb/util_unixsids.c b/source3/passdb/util_unixsids.c index 0894804..1be5e4f 100644 --- a/source3/passdb/util_unixsids.c +++ b/source3/passdb/util_unixsids.c @@ -63,7 +63,7 @@ bool lookup_unix_user_name(const char *name, struct dom_sid *sid) struct passwd *pwd; bool ret; - pwd = getpwnam_alloc(talloc_autofree_context(), name); + pwd = Get_Pwnam_alloc(talloc_autofree_context(), name); if (pwd == NULL) { return False; } diff --git a/source3/smbd/password.c b/source3/smbd/password.c index f0989e6..bc42a99 100644 --- a/source3/smbd/password.c +++ b/source3/smbd/password.c @@ -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", diff --git a/source3/smbd/uid.c b/source3/smbd/uid.c index c7c216b..c27e9b2 100644 --- a/source3/smbd/uid.c +++ b/source3/smbd/uid.c @@ -32,7 +32,7 @@ bool change_to_guest(void) { struct passwd *pass; - pass = getpwnam_alloc(talloc_autofree_context(), lp_guestaccount()); + pass = Get_Pwnam_alloc(talloc_autofree_context(), lp_guestaccount()); if (!pass) { return false; } diff --git a/source3/torture/pdbtest.c b/source3/torture/pdbtest.c index a775791..9bfabaf 100644 --- a/source3/torture/pdbtest.c +++ b/source3/torture/pdbtest.c @@ -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); } diff --git a/source3/utils/net_sam.c b/source3/utils/net_sam.c index 44a0b79..2ff37ca 100644 --- a/source3/utils/net_sam.c +++ b/source3/utils/net_sam.c @@ -1855,7 +1855,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) { @@ -1926,7 +1926,7 @@ doma_done: d_printf(_("Checking Guest's group.\n")); - pwd = getpwnam_alloc(talloc_autofree_context(), lp_guestaccount()); + pwd = Get_Pwnam_alloc(talloc_autofree_context(), lp_guestaccount()); if (!pwd) { d_fprintf(stderr, _("Failed to find just created Guest account!\n" diff --git a/source3/web/cgi.c b/source3/web/cgi.c index c81ef87..13b2c5a 100644 --- a/source3/web/cgi.c +++ b/source3/web/cgi.c @@ -314,7 +314,7 @@ static void cgi_web_auth(void) exit(0); } - pwd = getpwnam_alloc(talloc_autofree_context(), user); + pwd = Get_Pwnam_alloc(talloc_autofree_context(), user); if (!pwd) { printf("%sCannot find user %s<br>%s\n", head, user, tail); exit(0); @@ -367,7 +367,7 @@ static bool cgi_handle_authorization(char *line) * Try and get the user from the UNIX password file. */ - pass = getpwnam_alloc(talloc_autofree_context(), user); + pass = Get_Pwnam_alloc(talloc_autofree_context(), user); /* * Validate the password they have given. -- Samba Shared Repository