Initial import
[samba] / source / lib / util_pw.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Safe versions of getpw* calls
5
6    Copyright (C) Andrew Bartlett 2002
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 static struct passwd *alloc_copy_passwd(const struct passwd *from) 
26 {
27         struct passwd *ret = SMB_XMALLOC_P(struct passwd);
28         ZERO_STRUCTP(ret);
29         ret->pw_name = smb_xstrdup(from->pw_name);
30         ret->pw_passwd = smb_xstrdup(from->pw_passwd);
31         ret->pw_uid = from->pw_uid;
32         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);
36         return ret;
37 }
38
39 void passwd_free (struct passwd **buf)
40 {
41         if (!*buf) {
42                 DEBUG(0, ("attempted double-free of allocated passwd\n"));
43                 return;
44         }
45
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);
51
52         SAFE_FREE(*buf);
53 }
54
55 #define PWNAMCACHE_SIZE 4
56 static struct passwd *pwnam_cache[PWNAMCACHE_SIZE];
57 static BOOL pwnam_cache_initialized = False;
58
59 static void init_pwnam_cache(void)
60 {
61         int i;
62
63         if (pwnam_cache_initialized)
64                 return;
65
66         for (i=0; i<PWNAMCACHE_SIZE; i++)
67                 pwnam_cache[i] = NULL;
68
69         pwnam_cache_initialized = True;
70         return;
71 }
72
73 void flush_pwnam_cache(void)
74 {
75         int i;
76
77         init_pwnam_cache();
78
79         for (i=0; i<PWNAMCACHE_SIZE; i++) {
80                 if (pwnam_cache[i] == NULL)
81                         continue;
82
83                 passwd_free(&pwnam_cache[i]);
84         }
85 }
86
87 struct passwd *getpwnam_alloc(const char *name) 
88 {
89         int i;
90
91         struct passwd *temp;
92
93         init_pwnam_cache();
94
95         for (i=0; i<PWNAMCACHE_SIZE; i++) {
96                 if ((pwnam_cache[i] != NULL) && 
97                     (strcmp(name, pwnam_cache[i]->pw_name) == 0)) {
98                         DEBUG(10, ("Got %s from pwnam_cache\n", name));
99                         return alloc_copy_passwd(pwnam_cache[i]);
100                 }
101         }
102
103         temp = sys_getpwnam(name);
104         
105         if (!temp) {
106 #if 0
107                 if (errno == ENOMEM) {
108                         /* what now? */
109                 }
110 #endif
111                 return NULL;
112         }
113
114         for (i=0; i<PWNAMCACHE_SIZE; i++) {
115                 if (pwnam_cache[i] == NULL)
116                         break;
117         }
118
119         if (i == PWNAMCACHE_SIZE)
120                 i = rand() % PWNAMCACHE_SIZE;
121
122         if (pwnam_cache[i] != NULL)
123                 passwd_free(&pwnam_cache[i]);
124
125         pwnam_cache[i] = alloc_copy_passwd(temp);
126
127         return alloc_copy_passwd(temp);
128 }
129
130 struct passwd *getpwuid_alloc(uid_t uid) 
131 {
132         struct passwd *temp;
133
134         temp = sys_getpwuid(uid);
135         
136         if (!temp) {
137 #if 0
138                 if (errno == ENOMEM) {
139                         /* what now? */
140                 }
141 #endif
142                 return NULL;
143         }
144
145         return alloc_copy_passwd(temp);
146 }