s3-waf: use SAMBA3_*() build rules in source3/build
[Samba.git] / source3 / lib / username.c
blobafe83acfd9b61739c8d079c71023c7c873993e0f
1 /*
2 Unix SMB/CIFS implementation.
3 Username handling
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1997-2001.
6 Copyright (C) Andrew Bartlett 2002
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "memcache.h"
25 /* internal functions */
26 static struct passwd *uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
27 struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
28 int N);
29 static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx, int offset,
30 struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
31 int N);
33 static struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
35 struct passwd *pw, *for_cache;
37 pw = (struct passwd *)memcache_lookup_talloc(
38 NULL, GETPWNAM_CACHE, data_blob_string_const_null(name));
39 if (pw != NULL) {
40 return tcopy_passwd(mem_ctx, pw);
43 pw = sys_getpwnam(name);
44 if (pw == NULL) {
45 return NULL;
48 for_cache = tcopy_passwd(talloc_tos(), pw);
49 if (for_cache == NULL) {
50 return NULL;
53 memcache_add_talloc(NULL, GETPWNAM_CACHE,
54 data_blob_string_const_null(name), &for_cache);
56 return tcopy_passwd(mem_ctx, pw);
59 /****************************************************************************
60 talloc copy a struct passwd.
61 ****************************************************************************/
63 struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx, const struct passwd *from)
65 struct passwd *ret = TALLOC_P(mem_ctx, struct passwd);
66 if (!ret) {
67 return NULL;
69 ret->pw_name = talloc_strdup(ret, from->pw_name);
70 ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
71 ret->pw_uid = from->pw_uid;
72 ret->pw_gid = from->pw_gid;
73 ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
74 ret->pw_dir = talloc_strdup(ret, from->pw_dir);
75 ret->pw_shell = talloc_strdup(ret, from->pw_shell);
76 return ret;
79 /****************************************************************************
80 Flush all cached passwd structs.
81 ****************************************************************************/
83 void flush_pwnam_cache(void)
85 memcache_flush(NULL, GETPWNAM_CACHE);
88 /****************************************************************************
89 talloc'ed version of getpwuid.
90 ****************************************************************************/
92 struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid)
94 struct passwd *temp = sys_getpwuid(uid);
96 if (!temp) {
97 return NULL;
100 return tcopy_passwd(mem_ctx, temp);
103 /****************************************************************************
104 Get a users home directory.
105 ****************************************************************************/
107 char *get_user_home_dir(TALLOC_CTX *mem_ctx, const char *user)
109 struct passwd *pass;
110 char *result;
112 /* Ensure the user exists. */
114 pass = Get_Pwnam_alloc(mem_ctx, user);
116 if (!pass)
117 return(NULL);
119 /* Return home directory from struct passwd. */
121 result = talloc_move(mem_ctx, &pass->pw_dir);
123 TALLOC_FREE(pass);
124 return result;
127 /****************************************************************************
128 * A wrapper for sys_getpwnam(). The following variations are tried:
129 * - as transmitted
130 * - in all lower case if this differs from transmitted
131 * - in all upper case if this differs from transmitted
132 * - using lp_usernamelevel() for permutations.
133 ****************************************************************************/
135 static struct passwd *Get_Pwnam_internals(TALLOC_CTX *mem_ctx,
136 const char *user, char *user2)
138 struct passwd *ret = NULL;
140 if (!user2 || !(*user2))
141 return(NULL);
143 if (!user || !(*user))
144 return(NULL);
146 /* Try in all lower case first as this is the most
147 common case on UNIX systems */
148 strlower_m(user2);
149 DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
150 ret = getpwnam_alloc(mem_ctx, user2);
151 if(ret)
152 goto done;
154 /* Try as given, if username wasn't originally lowercase */
155 if(strcmp(user, user2) != 0) {
156 DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",
157 user));
158 ret = getpwnam_alloc(mem_ctx, user);
159 if(ret)
160 goto done;
163 /* Try as uppercase, if username wasn't originally uppercase */
164 strupper_m(user2);
165 if(strcmp(user, user2) != 0) {
166 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",
167 user2));
168 ret = getpwnam_alloc(mem_ctx, user2);
169 if(ret)
170 goto done;
173 /* Try all combinations up to usernamelevel */
174 strlower_m(user2);
175 DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",
176 lp_usernamelevel(), user2));
177 ret = uname_string_combinations(user2, mem_ctx, getpwnam_alloc,
178 lp_usernamelevel());
180 done:
181 DEBUG(5,("Get_Pwnam_internals %s find user [%s]!\n",ret ?
182 "did":"didn't", user));
184 return ret;
187 /****************************************************************************
188 Get_Pwnam wrapper without modification.
189 NOTE: This with NOT modify 'user'!
190 This will return an allocated structure
191 ****************************************************************************/
193 struct passwd *Get_Pwnam_alloc(TALLOC_CTX *mem_ctx, const char *user)
195 fstring user2;
196 struct passwd *ret;
198 if ( *user == '\0' ) {
199 DEBUG(10,("Get_Pwnam: empty username!\n"));
200 return NULL;
203 fstrcpy(user2, user);
205 DEBUG(5,("Finding user %s\n", user));
207 ret = Get_Pwnam_internals(mem_ctx, user, user2);
209 return ret;
212 /* The functions below have been taken from password.c and slightly modified */
213 /****************************************************************************
214 Apply a function to upper/lower case combinations
215 of a string and return true if one of them returns true.
216 Try all combinations with N uppercase letters.
217 offset is the first char to try and change (start with 0)
218 it assumes the string starts lowercased
219 ****************************************************************************/
221 static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx,
222 int offset,
223 struct passwd *(*fn)(TALLOC_CTX *mem_ctx, const char *),
224 int N)
226 ssize_t len = (ssize_t)strlen(s);
227 int i;
228 struct passwd *ret;
230 if (N <= 0 || offset >= len)
231 return(fn(mem_ctx, s));
233 for (i=offset;i<(len-(N-1));i++) {
234 char c = s[i];
235 if (!islower_ascii((int)c))
236 continue;
237 s[i] = toupper_ascii(c);
238 ret = uname_string_combinations2(s, mem_ctx, i+1, fn, N-1);
239 if(ret)
240 return(ret);
241 s[i] = c;
243 return(NULL);
246 /****************************************************************************
247 Apply a function to upper/lower case combinations
248 of a string and return true if one of them returns true.
249 Try all combinations with up to N uppercase letters.
250 offset is the first char to try and change (start with 0)
251 it assumes the string starts lowercased
252 ****************************************************************************/
254 static struct passwd * uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
255 struct passwd * (*fn)(TALLOC_CTX *mem_ctx, const char *),
256 int N)
258 int n;
259 struct passwd *ret;
261 for (n=1;n<=N;n++) {
262 ret = uname_string_combinations2(s,mem_ctx,0,fn,n);
263 if(ret)
264 return(ret);
266 return(NULL);