23
23
#include "includes.h"
25
static struct passwd *alloc_copy_passwd(const struct passwd *from)
25
struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from)
27
struct passwd *ret = SMB_XMALLOC_P(struct passwd);
29
ret->pw_name = smb_xstrdup(from->pw_name);
30
ret->pw_passwd = smb_xstrdup(from->pw_passwd);
27
struct passwd *ret = TALLOC_P(mem_ctx, struct passwd);
31
ret->pw_name = talloc_strdup(ret, from->pw_name);
32
ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
31
33
ret->pw_uid = from->pw_uid;
32
34
ret->pw_gid = from->pw_gid;
33
ret->pw_gecos = smb_xstrdup(from->pw_gecos);
34
ret->pw_dir = smb_xstrdup(from->pw_dir);
35
ret->pw_shell = smb_xstrdup(from->pw_shell);
35
ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
36
ret->pw_dir = talloc_strdup(ret, from->pw_dir);
37
ret->pw_shell = talloc_strdup(ret, from->pw_shell);
39
void passwd_free (struct passwd **buf)
42
DEBUG(0, ("attempted double-free of allocated passwd\n"));
46
SAFE_FREE((*buf)->pw_name);
47
SAFE_FREE((*buf)->pw_passwd);
48
SAFE_FREE((*buf)->pw_gecos);
49
SAFE_FREE((*buf)->pw_dir);
50
SAFE_FREE((*buf)->pw_shell);
55
41
#define PWNAMCACHE_SIZE 4
56
static struct passwd *pwnam_cache[PWNAMCACHE_SIZE];
57
static BOOL pwnam_cache_initialized = False;
42
static struct passwd **pwnam_cache = NULL;
59
44
static void init_pwnam_cache(void)
63
if (pwnam_cache_initialized)
46
if (pwnam_cache != NULL)
66
for (i=0; i<PWNAMCACHE_SIZE; i++)
67
pwnam_cache[i] = NULL;
49
pwnam_cache = TALLOC_ZERO_ARRAY(NULL, struct passwd *,
51
if (pwnam_cache == NULL) {
52
smb_panic("Could not init pwnam_cache\n");
69
pwnam_cache_initialized = True;
73
58
void flush_pwnam_cache(void)
60
TALLOC_FREE(pwnam_cache);
77
62
init_pwnam_cache();
79
for (i=0; i<PWNAMCACHE_SIZE; i++) {
80
if (pwnam_cache[i] == NULL)
83
passwd_free(&pwnam_cache[i]);
87
struct passwd *getpwnam_alloc(const char *name)
65
struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
96
74
if ((pwnam_cache[i] != NULL) &&
97
75
(strcmp(name, pwnam_cache[i]->pw_name) == 0)) {
98
76
DEBUG(10, ("Got %s from pwnam_cache\n", name));
99
return alloc_copy_passwd(pwnam_cache[i]);
77
return talloc_reference(mem_ctx, pwnam_cache[i]);
119
97
if (i == PWNAMCACHE_SIZE)
120
98
i = rand() % PWNAMCACHE_SIZE;
122
if (pwnam_cache[i] != NULL)
123
passwd_free(&pwnam_cache[i]);
125
pwnam_cache[i] = alloc_copy_passwd(temp);
127
return alloc_copy_passwd(temp);
100
if (pwnam_cache[i] != NULL) {
101
TALLOC_FREE(pwnam_cache[i]);
104
pwnam_cache[i] = tcopy_passwd(pwnam_cache, temp);
105
if (pwnam_cache[i]!= NULL && mem_ctx != NULL) {
106
return talloc_reference(mem_ctx, pwnam_cache[i]);
109
return tcopy_passwd(NULL, pwnam_cache[i]);
130
struct passwd *getpwuid_alloc(uid_t uid)
112
struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid)
132
114
struct passwd *temp;