r13316: Let the carnage begin....
[kai/samba.git] / source3 / lib / util_pw.c
index 13349bad34e256684fc6d2b60e2e795859ca466a..e026affb44768df85da11602a24fa31ad5dee06c 100644 (file)
 
 #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; i<PWNAMCACHE_SIZE; i++)
-               pwnam_cache[i] = NULL;
+       pwnam_cache = TALLOC_ZERO_ARRAY(NULL, struct passwd *,
+                                       PWNAMCACHE_SIZE);
+       if (pwnam_cache == NULL) {
+               smb_panic("Could not init pwnam_cache\n");
+       }
 
-       pwnam_cache_initialized = True;
        return;
 }
 
 void flush_pwnam_cache(void)
 {
-       int i;
-
+       talloc_free(pwnam_cache);
+       pwnam_cache = NULL;
        init_pwnam_cache();
-
-       for (i=0; i<PWNAMCACHE_SIZE; i++) {
-               if (pwnam_cache[i] == NULL)
-                       continue;
-
-               passwd_free(&pwnam_cache[i]);
-       }
 }
 
-struct passwd *getpwnam_alloc(const char *name) 
+struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
 {
        int i;
 
@@ -96,7 +72,7 @@ struct passwd *getpwnam_alloc(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 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);
 }