From c311d24ce32d2a8aa244f126bcec67ec03549727 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Thu, 17 Jan 2002 08:45:58 +0000 Subject: A nice *big* change to the fundemental way we do things. Samba (ab)uses the returns from getpwnam() a lot - in particular it keeps them around for a long time - often past the next call... This adds a getpwnam_alloc and a getpwuid_alloc to the collection. These function as expected, returning a malloced structure that can be free()ed with passwd_free(&passwd). This patch also cuts down on the number of calls to getpwnam - mostly by taking advantage of the fact that the passdb interface is already case-insensiteve. With this patch most of the recursive cases have been removed (that I know of) and the problems are reduced further by not using the sys_ interface in the new code. This means that pointers to the cache won't be affected. (This is a tempoary HACK, I intend to kill the password cache entirly). The only change I'm a little worried about is the change to rpc_server/srv_samr_nt.c for private groups. In this case we are getting groups from the new group mapping DB. Do we still need to check for private groups? I've toned down the check to a case sensitve match with the new code, but we might be able to kill it entirly. I've also added a make_modifyable_passwd() function, that copies a passwd struct into the form that the old sys_getpw* code provided. As far as I can tell this is only actually used in the pass_check.c crazies, where I moved the final 'special case' for shadow passwords (out of _Get_Pwnam()). The matching case for getpwent() is dealt with already, in lib/util_getent.c Also included in here is a small change to register the [homes] share at vuid creation rather than just in one varient of the session setup. (This picks up the SPNEGO cases). The home directory is now stored on the vuid, and I am hoping this might provide a saner way to do %H substitions. TODO: Kill off remaining Get_Pwnam_Modify calls (they are not needed), change the remaining sys_getpwnam() callers to use getpwnam_alloc() and move Get_Pwnam to return an allocated struct. Andrew Bartlett (This used to be commit 1d86c7f94230bc53daebd4d2cd829da6292e05da) --- source3/lib/util_pw.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 source3/lib/util_pw.c (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c new file mode 100644 index 0000000000..3367a6cdc0 --- /dev/null +++ b/source3/lib/util_pw.c @@ -0,0 +1,134 @@ +/* + Unix SMB/Netbios implementation. + Version 3.0. + + 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 2 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, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +struct passwd *make_modifyable_passwd(const struct passwd *from) +{ + struct passwd *ret = smb_xmalloc(sizeof(*ret)); +/* This is the assumed shape of the members by certain parts of the code... + fstring pw_name; + fstring pw_passwd; + fstring pw_gecos; + pstring pw_dir; + pstring pw_shell; +*/ + char *pw_name = smb_xmalloc(sizeof(fstring)); + char *pw_passwd = smb_xmalloc(sizeof(fstring)); + char *pw_gecos = smb_xmalloc(sizeof(fstring)); + char *pw_dir = smb_xmalloc(sizeof(pstring)); + char *pw_shell = smb_xmalloc(sizeof(pstring)); + + ZERO_STRUCTP(ret); + + /* + * Now point the struct's members as the + * newly allocated buffers: + */ + + ret->pw_name = pw_name; + fstrcpy(ret->pw_name, from->pw_name); + + ret->pw_passwd = pw_passwd; + fstrcpy(ret->pw_passwd, from->pw_passwd); + + ret->pw_uid = from->pw_uid; + ret->pw_gid = from->pw_gid; + + ret->pw_gecos = pw_gecos; + fstrcpy(ret->pw_gecos, from->pw_gecos); + + ret->pw_dir = pw_dir; + pstrcpy(ret->pw_dir, from->pw_dir); + + ret->pw_shell = pw_shell; + pstrcpy(ret->pw_shell, from->pw_shell); + + return ret; +} + +static struct passwd *alloc_copy_passwd(const struct passwd *from) +{ + struct passwd *ret = smb_xmalloc(sizeof(*ret)); + ZERO_STRUCTP(ret); + ret->pw_name = smb_xstrdup(from->pw_name); + ret->pw_passwd = smb_xstrdup(from->pw_passwd); + ret->pw_uid = from->pw_uid; + ret->pw_gid = from->pw_gid; + ret->pw_gecos = smb_xstrdup(from->pw_gecos); + ret->pw_dir = smb_xstrdup(from->pw_dir); + ret->pw_shell = smb_xstrdup(from->pw_shell); + return ret; +} + +void passwd_free (struct passwd **buf) +{ + if (!*buf) { + DEBUG(0, ("attempted double-free of allocated passwd\n")); + return; + } + + SAFE_FREE((*buf)->pw_name); + SAFE_FREE((*buf)->pw_passwd); + SAFE_FREE((*buf)->pw_gecos); + SAFE_FREE((*buf)->pw_dir); + SAFE_FREE((*buf)->pw_shell); + + SAFE_FREE(*buf); +} + +struct passwd *getpwnam_alloc(const char *name) +{ + struct passwd *temp; + + temp = getpwnam(name); + + if (!temp) { +#if 0 + if (errno == ENOMEM) { + /* what now? */ + } +#endif + return NULL; + } + + return alloc_copy_passwd(temp); +} + +struct passwd *getpwuid_alloc(uid_t uid) +{ + struct passwd *temp; + + temp = getpwuid(uid); + + if (!temp) { +#if 0 + if (errno == ENOMEM) { + /* what now? */ + } +#endif + return NULL; + } + + return alloc_copy_passwd(temp); +} -- cgit From cd68afe31256ad60748b34f7318a180cfc2127cc Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 30 Jan 2002 06:08:46 +0000 Subject: Removed version number from file header. Changed "SMB/Netbios" to "SMB/CIFS" in file header. (This used to be commit 6a58c9bd06d0d7502a24bf5ce5a2faf0a146edfa) --- source3/lib/util_pw.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 3367a6cdc0..67ed43f216 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -1,6 +1,5 @@ /* - Unix SMB/Netbios implementation. - Version 3.0. + Unix SMB/CIFS implementation. Safe versions of getpw* calls -- cgit From 32334bc6553c25b706e60a321f9c16f8931f94c1 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 18 Mar 2002 23:57:14 +0000 Subject: more verbose checking in talloc and util_pw fixed tdbsam memory corruption (and segfault) reducing calls to pdb_uid_to_user_rid and countrary to 0 to move to a non alghoritmic rid allocation with some passdb modules. (This used to be commit 9836af7cd623357feaec07bc49cfb78f0aa01fc3) --- source3/lib/util_pw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 67ed43f216..259649a064 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -68,7 +68,7 @@ struct passwd *make_modifyable_passwd(const struct passwd *from) static struct passwd *alloc_copy_passwd(const struct passwd *from) { - struct passwd *ret = smb_xmalloc(sizeof(*ret)); + struct passwd *ret = smb_xmalloc(sizeof(struct passwd)); ZERO_STRUCTP(ret); ret->pw_name = smb_xstrdup(from->pw_name); ret->pw_passwd = smb_xstrdup(from->pw_passwd); -- cgit From e90b65284812aaa5ff9e9935ce9bbad7791cbbcd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 15 Jul 2002 10:35:28 +0000 Subject: updated the 3.0 branch from the head branch - ready for alpha18 (This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce) --- source3/lib/util_pw.c | 48 ++---------------------------------------------- 1 file changed, 2 insertions(+), 46 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 259649a064..9d075a05e8 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -22,50 +22,6 @@ #include "includes.h" -struct passwd *make_modifyable_passwd(const struct passwd *from) -{ - struct passwd *ret = smb_xmalloc(sizeof(*ret)); -/* This is the assumed shape of the members by certain parts of the code... - fstring pw_name; - fstring pw_passwd; - fstring pw_gecos; - pstring pw_dir; - pstring pw_shell; -*/ - char *pw_name = smb_xmalloc(sizeof(fstring)); - char *pw_passwd = smb_xmalloc(sizeof(fstring)); - char *pw_gecos = smb_xmalloc(sizeof(fstring)); - char *pw_dir = smb_xmalloc(sizeof(pstring)); - char *pw_shell = smb_xmalloc(sizeof(pstring)); - - ZERO_STRUCTP(ret); - - /* - * Now point the struct's members as the - * newly allocated buffers: - */ - - ret->pw_name = pw_name; - fstrcpy(ret->pw_name, from->pw_name); - - ret->pw_passwd = pw_passwd; - fstrcpy(ret->pw_passwd, from->pw_passwd); - - ret->pw_uid = from->pw_uid; - ret->pw_gid = from->pw_gid; - - ret->pw_gecos = pw_gecos; - fstrcpy(ret->pw_gecos, from->pw_gecos); - - ret->pw_dir = pw_dir; - pstrcpy(ret->pw_dir, from->pw_dir); - - ret->pw_shell = pw_shell; - pstrcpy(ret->pw_shell, from->pw_shell); - - return ret; -} - static struct passwd *alloc_copy_passwd(const struct passwd *from) { struct passwd *ret = smb_xmalloc(sizeof(struct passwd)); @@ -100,7 +56,7 @@ struct passwd *getpwnam_alloc(const char *name) { struct passwd *temp; - temp = getpwnam(name); + temp = sys_getpwnam(name); if (!temp) { #if 0 @@ -118,7 +74,7 @@ struct passwd *getpwuid_alloc(uid_t uid) { struct passwd *temp; - temp = getpwuid(uid); + temp = sys_getpwuid(uid); if (!temp) { #if 0 -- cgit From c34ec6180a011f23de16cc5e19902470dc7feb6f Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 12 Nov 2004 15:01:40 +0000 Subject: r3702: This is a getpwnam-cache. It is mainly to speed up Samba with slow nss backends such as nss_ldap. Volker (This used to be commit a8bd0b75042f73b753fc1cb8a52e6e90372fd1fe) --- source3/lib/util_pw.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 9d075a05e8..e102f2ef81 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -52,10 +52,40 @@ void passwd_free (struct passwd **buf) SAFE_FREE(*buf); } +#define PWNAMCACHE_SIZE 4 +static struct passwd *pwnam_cache[PWNAMCACHE_SIZE]; +static BOOL pwnam_cache_initialized = False; + +static void init_pwnam_cache(void) +{ + int i; + + if (pwnam_cache_initialized) + return; + + for (i=0; ipw_name) == 0)) { + DEBUG(10, ("Got %s from pwnam_cache\n", name)); + return alloc_copy_passwd(pwnam_cache[i]); + } + } + temp = sys_getpwnam(name); if (!temp) { @@ -67,6 +97,19 @@ struct passwd *getpwnam_alloc(const char *name) return NULL; } + for (i=0; i Date: Tue, 7 Dec 2004 18:25:53 +0000 Subject: r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation functions so we can funnel through some well known functions. Should help greatly with malloc checking. HEAD patch to follow. Jeremy. (This used to be commit 620f2e608f70ba92f032720c031283d295c5c06a) --- source3/lib/util_pw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index e102f2ef81..0d7ffe09e9 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -24,7 +24,7 @@ static struct passwd *alloc_copy_passwd(const struct passwd *from) { - struct passwd *ret = smb_xmalloc(sizeof(struct passwd)); + struct passwd *ret = SMB_XMALLOC_P(struct passwd); ZERO_STRUCTP(ret); ret->pw_name = smb_xstrdup(from->pw_name); ret->pw_passwd = smb_xstrdup(from->pw_passwd); -- cgit From 140752fd35bd5701b3078abf695f811d933fe893 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 3 Mar 2005 16:52:44 +0000 Subject: r5647: Caches are good for performance, but you get a consistency problem. Fix bug # 2401. Volker (This used to be commit eb4ef94f244d28fe531d0b9f724a66ed3834b687) --- source3/lib/util_pw.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 0d7ffe09e9..13349bad34 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -70,6 +70,20 @@ static void init_pwnam_cache(void) return; } +void flush_pwnam_cache(void) +{ + int i; + + init_pwnam_cache(); + + for (i=0; i Date: Fri, 3 Feb 2006 22:19:41 +0000 Subject: r13316: Let the carnage begin.... Sync with trunk as off r13315 (This used to be commit 17e63ac4ed8325c0d44fe62b2442449f3298559f) --- source3/lib/util_pw.c | 79 +++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 49 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 13349bad34..e026affb44 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -22,69 +22,45 @@ #include "includes.h" -static struct passwd *alloc_copy_passwd(const struct passwd *from) +static struct passwd *talloc_copy_passwd(TALLOC_CTX *mem_ctx, + const struct passwd *from) { - struct passwd *ret = SMB_XMALLOC_P(struct passwd); - ZERO_STRUCTP(ret); - ret->pw_name = smb_xstrdup(from->pw_name); - ret->pw_passwd = smb_xstrdup(from->pw_passwd); + struct passwd *ret = TALLOC_P(mem_ctx, struct passwd); + 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 = smb_xstrdup(from->pw_gecos); - ret->pw_dir = smb_xstrdup(from->pw_dir); - ret->pw_shell = smb_xstrdup(from->pw_shell); + 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 passwd_free (struct passwd **buf) -{ - if (!*buf) { - DEBUG(0, ("attempted double-free of allocated passwd\n")); - return; - } - - SAFE_FREE((*buf)->pw_name); - SAFE_FREE((*buf)->pw_passwd); - SAFE_FREE((*buf)->pw_gecos); - SAFE_FREE((*buf)->pw_dir); - SAFE_FREE((*buf)->pw_shell); - - SAFE_FREE(*buf); -} - #define PWNAMCACHE_SIZE 4 -static struct passwd *pwnam_cache[PWNAMCACHE_SIZE]; -static BOOL pwnam_cache_initialized = False; +static struct passwd **pwnam_cache = NULL; static void init_pwnam_cache(void) { - int i; - - if (pwnam_cache_initialized) + if (pwnam_cache != NULL) return; - for (i=0; ipw_name) == 0)) { DEBUG(10, ("Got %s from pwnam_cache\n", name)); - return alloc_copy_passwd(pwnam_cache[i]); + return talloc_reference(mem_ctx, pwnam_cache[i]); } } @@ -119,15 +95,20 @@ struct passwd *getpwnam_alloc(const char *name) if (i == PWNAMCACHE_SIZE) i = rand() % PWNAMCACHE_SIZE; - if (pwnam_cache[i] != NULL) - passwd_free(&pwnam_cache[i]); + if (pwnam_cache[i] != NULL) { + talloc_free(pwnam_cache[i]); + } - pwnam_cache[i] = alloc_copy_passwd(temp); + pwnam_cache[i] = talloc_copy_passwd(pwnam_cache, temp); + + if (mem_ctx != NULL) { + return talloc_reference(mem_ctx, pwnam_cache[i]); + } - return alloc_copy_passwd(temp); + return talloc_copy_passwd(NULL, pwnam_cache[i]); } -struct passwd *getpwuid_alloc(uid_t uid) +struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) { struct passwd *temp; @@ -142,5 +123,5 @@ struct passwd *getpwuid_alloc(uid_t uid) return NULL; } - return alloc_copy_passwd(temp); + return talloc_copy_passwd(mem_ctx, temp); } -- cgit From fb5362c069b5b6548478b2217a0519c56d856705 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 20 Feb 2006 17:59:58 +0000 Subject: r13571: Replace all calls to talloc_free() with thye TALLOC_FREE() macro which sets the freed pointer to NULL. (This used to be commit b65be8874a2efe5a4b167448960a4fcf6bd995e2) --- source3/lib/util_pw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index e026affb44..e632846312 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -55,7 +55,7 @@ static void init_pwnam_cache(void) void flush_pwnam_cache(void) { - talloc_free(pwnam_cache); + TALLOC_FREE(pwnam_cache); pwnam_cache = NULL; init_pwnam_cache(); } @@ -96,7 +96,7 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) i = rand() % PWNAMCACHE_SIZE; if (pwnam_cache[i] != NULL) { - talloc_free(pwnam_cache[i]); + TALLOC_FREE(pwnam_cache[i]); } pwnam_cache[i] = talloc_copy_passwd(pwnam_cache, temp); -- cgit From 6622ba566ed3cc3ac362c4e257d7c8ed3c437a8a Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 21 Feb 2006 19:22:49 +0000 Subject: r13601: * Remove unused code from pdb_ldap.c * Add a 'struct passwd *' to the struct samu for later reference (I know this may be controversial but its easily reverted which is is why I'm checking this is as a seaparate patch before I get too deep). * Remove unix_homedir from struct samu {} and update the pdb wrapper functions associated with it. (This used to be commit 92c251fdf0f1f566cfeca3c75ba2284b644aef5d) --- source3/lib/util_pw.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index e632846312..e1bea1a646 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -22,8 +22,7 @@ #include "includes.h" -static struct passwd *talloc_copy_passwd(TALLOC_CTX *mem_ctx, - const struct passwd *from) +struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from) { struct passwd *ret = TALLOC_P(mem_ctx, struct passwd); ret->pw_name = talloc_strdup(ret, from->pw_name); @@ -99,13 +98,13 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) TALLOC_FREE(pwnam_cache[i]); } - pwnam_cache[i] = talloc_copy_passwd(pwnam_cache, temp); + pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp); if (mem_ctx != NULL) { return talloc_reference(mem_ctx, pwnam_cache[i]); } - return talloc_copy_passwd(NULL, pwnam_cache[i]); + return tcopy_passwd(NULL, pwnam_cache[i]); } struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) @@ -123,5 +122,5 @@ struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) return NULL; } - return talloc_copy_passwd(mem_ctx, temp); + return tcopy_passwd(mem_ctx, temp); } -- cgit From b84d06dae754ef2f637bc6549c63a5062afbbab6 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 29 Mar 2006 22:41:24 +0000 Subject: r14764: Fix possible null pointer deref. Coverity #253. Jeremy. (This used to be commit 7a18f38947385b8a5fb27a42610320003689e9e1) --- source3/lib/util_pw.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index e1bea1a646..754899f420 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -25,6 +25,9 @@ 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; @@ -99,8 +102,7 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) } pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp); - - if (mem_ctx != NULL) { + if (pwnam_cache[i]!= NULL && mem_ctx != NULL) { return talloc_reference(mem_ctx, pwnam_cache[i]); } -- cgit From 1cf1e648feed823244731eef5f56bd34e15cb045 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 31 Jul 2006 04:30:55 +0000 Subject: r17334: Some C++ warnings (This used to be commit 8ae7ed1f3cecbb5285313d17b5f9511e2e622f0b) --- source3/lib/util_pw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 754899f420..dc184233a6 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -74,7 +74,7 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) if ((pwnam_cache[i] != NULL) && (strcmp(name, pwnam_cache[i]->pw_name) == 0)) { DEBUG(10, ("Got %s from pwnam_cache\n", name)); - return talloc_reference(mem_ctx, pwnam_cache[i]); + return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]); } } @@ -103,7 +103,7 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp); if (pwnam_cache[i]!= NULL && mem_ctx != NULL) { - return talloc_reference(mem_ctx, pwnam_cache[i]); + return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]); } return tcopy_passwd(NULL, pwnam_cache[i]); -- cgit From fb3835846ef89a632230ff808259dad1cddc05c0 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 2 Apr 2007 03:46:13 +0000 Subject: r22020: Make it more clear that both the vuser struct and it's contents are talloc_free()'ed at the end of a session. Rework the passwd cache code to use talloc_unlink and talloc_reference, to more carefully manage the cache. Andrew Bartlett (This used to be commit e3e0ec25e67308de314aa61852905ee42aa2c8fe) --- source3/lib/util_pw.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index dc184233a6..8b3c78a3f9 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -98,15 +98,15 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) i = rand() % PWNAMCACHE_SIZE; if (pwnam_cache[i] != NULL) { - TALLOC_FREE(pwnam_cache[i]); + /* Remove this old cache entry, from the cache. We + * use talloc_unlink here because we want to be very + * clear which referece we are removing */ + talloc_unlink(pwnam_cache, pwnam_cache[i]); } pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp); - if (pwnam_cache[i]!= NULL && mem_ctx != NULL) { - return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]); - } - - return tcopy_passwd(NULL, pwnam_cache[i]); + /* while keeping this in the cache, reference a copy for the caller */ + return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]); } struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) -- cgit From ddf759c533f8869c319c726ee1773f969c54a487 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 2 Apr 2007 06:26:16 +0000 Subject: r22023: I don't like this cache, but I think Jeremy is right, the consequences of the talloc heirarchy created are too subtle, particularly with callers picking out individual members. This might fix the faults on the build farm. Andrew Bartlett (This used to be commit 82667bc75e63292c61f73c1f7cde809cc3dd55b0) --- source3/lib/util_pw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 8b3c78a3f9..fcbdd941fe 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -105,8 +105,8 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) } pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp); - /* while keeping this in the cache, reference a copy for the caller */ - return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]); + + return tcopy_passwd(pwnam_cache, temp); } struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) -- cgit From fd2e5b90a42c86736416adf1d5e06315605476ff Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 2 Apr 2007 06:57:37 +0000 Subject: r22024: Don't leak, actually use the provided memory context... (This used to be commit 46ff4e599e79f47254e05c7e4db75db9a19934bc) --- source3/lib/util_pw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index fcbdd941fe..d6dfbd5016 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -106,7 +106,7 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp); - return tcopy_passwd(pwnam_cache, temp); + return tcopy_passwd(mem_ctx, temp); } struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) -- cgit From ec497bf02c59e3a8c5c72f4addac1095e99c43c6 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 2 Apr 2007 12:52:08 +0000 Subject: r22026: Missed in my last commit, another case where we need to copy, not reference. Andrew Bartlett (This used to be commit 635b83a21683068eb3908506d5ee1191e55381d1) --- source3/lib/util_pw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index d6dfbd5016..52054ce90a 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -74,7 +74,7 @@ struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) if ((pwnam_cache[i] != NULL) && (strcmp(name, pwnam_cache[i]->pw_name) == 0)) { DEBUG(10, ("Got %s from pwnam_cache\n", name)); - return (struct passwd *)talloc_reference(mem_ctx, pwnam_cache[i]); + return tcopy_passwd(mem_ctx, pwnam_cache[i]); } } -- cgit From b1ce226af8b61ad7e3c37860a59c6715012e738b Mon Sep 17 00:00:00 2001 From: James Peach Date: Fri, 15 Jun 2007 21:58:49 +0000 Subject: r23510: Tidy calls to smb_panic by removing trailing newlines. Print the failed expression in SMB_ASSERT. (This used to be commit 171dc060e2a576d724eed1ca65636bdafffd7713) --- source3/lib/util_pw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 52054ce90a..2c8f2d4a7d 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -49,7 +49,7 @@ static void init_pwnam_cache(void) pwnam_cache = TALLOC_ZERO_ARRAY(NULL, struct passwd *, PWNAMCACHE_SIZE); if (pwnam_cache == NULL) { - smb_panic("Could not init pwnam_cache\n"); + smb_panic("Could not init pwnam_cache"); } return; -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/util_pw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 2c8f2d4a7d..ccd97c3ce5 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -7,7 +7,7 @@ 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 2 of the License, or + 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, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/util_pw.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index ccd97c3ce5..1973626d84 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -16,8 +16,7 @@ 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, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From 44d086a15f4908a01d0c719498fde953bd4809e7 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 20 Dec 2007 14:54:33 +0100 Subject: Convert the pwnam cache to memcache (This used to be commit 032c5589fe7f9f2fcb0f336e72517a81a720b6ce) --- source3/lib/util_pw.c | 68 ++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 53 deletions(-) (limited to 'source3/lib/util_pw.c') diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 1973626d84..428378505f 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -37,74 +37,36 @@ struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from) return ret; } -#define PWNAMCACHE_SIZE 4 -static struct passwd **pwnam_cache = NULL; - -static void init_pwnam_cache(void) -{ - if (pwnam_cache != NULL) - return; - - pwnam_cache = TALLOC_ZERO_ARRAY(NULL, struct passwd *, - PWNAMCACHE_SIZE); - if (pwnam_cache == NULL) { - smb_panic("Could not init pwnam_cache"); - } - - return; -} - void flush_pwnam_cache(void) { - TALLOC_FREE(pwnam_cache); - pwnam_cache = NULL; - init_pwnam_cache(); + memcache_flush(NULL, GETPWNAM_CACHE); } struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) { - int i; - - struct passwd *temp; + struct passwd *temp, *cached; - init_pwnam_cache(); - - for (i=0; ipw_name) == 0)) { - DEBUG(10, ("Got %s from pwnam_cache\n", name)); - return tcopy_passwd(mem_ctx, pwnam_cache[i]); - } + temp = (struct passwd *)memcache_lookup_talloc( + NULL, GETPWNAM_CACHE, data_blob_string_const(name)); + if (temp != NULL) { + return tcopy_passwd(mem_ctx, temp); } temp = sys_getpwnam(name); - - if (!temp) { -#if 0 - if (errno == ENOMEM) { - /* what now? */ - } -#endif + if (temp == NULL) { return NULL; } - for (i=0; i