40e284a01a7a150878d7226ad3e98855c7f99148
[samba.git] / source3 / lib / username.c
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
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "memcache.h"
24 #include "../lib/util/util_pw.h"
25
26 /* internal functions */
27 static struct passwd *uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
28                                                 struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
29                                                 int N);
30 static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx, int offset,
31                                                  struct passwd * (*fn) (TALLOC_CTX *mem_ctx, const char *),
32                                                  int N);
33
34 static struct passwd *getpwnam_alloc_cached(TALLOC_CTX *mem_ctx, const char *name)
35 {
36         struct passwd *pw, *for_cache;
37
38         pw = (struct passwd *)memcache_lookup_talloc(
39                 NULL, GETPWNAM_CACHE, data_blob_string_const_null(name));
40         if (pw != NULL) {
41                 return tcopy_passwd(mem_ctx, pw);
42         }
43
44         pw = sys_getpwnam(name);
45         if (pw == NULL) {
46                 return NULL;
47         }
48
49         for_cache = tcopy_passwd(talloc_tos(), pw);
50         if (for_cache == NULL) {
51                 return NULL;
52         }
53
54         memcache_add_talloc(NULL, GETPWNAM_CACHE,
55                         data_blob_string_const_null(name), &for_cache);
56
57         return tcopy_passwd(mem_ctx, pw);
58 }
59
60 /****************************************************************************
61  Flush all cached passwd structs.
62 ****************************************************************************/
63
64 void flush_pwnam_cache(void)
65 {
66         memcache_flush(NULL, GETPWNAM_CACHE);
67 }
68
69 /****************************************************************************
70  Get a users home directory.
71 ****************************************************************************/
72
73 char *get_user_home_dir(TALLOC_CTX *mem_ctx, const char *user)
74 {
75         struct passwd *pass;
76         char *result;
77
78         /* Ensure the user exists. */
79
80         pass = Get_Pwnam_alloc(mem_ctx, user);
81
82         if (!pass)
83                 return(NULL);
84
85         /* Return home directory from struct passwd. */
86
87         result = talloc_move(mem_ctx, &pass->pw_dir);
88
89         TALLOC_FREE(pass);
90         return result;
91 }
92
93 /****************************************************************************
94  * A wrapper for sys_getpwnam().  The following variations are tried:
95  *   - as transmitted
96  *   - in all lower case if this differs from transmitted
97  *   - in all upper case if this differs from transmitted
98  *   - using lp_usernamelevel() for permutations.
99 ****************************************************************************/
100
101 static struct passwd *Get_Pwnam_internals(TALLOC_CTX *mem_ctx,
102                                           const char *user, char *user2)
103 {
104         struct passwd *ret = NULL;
105
106         if (!user2 || !(*user2))
107                 return(NULL);
108
109         if (!user || !(*user))
110                 return(NULL);
111
112         /* Try in all lower case first as this is the most 
113            common case on UNIX systems */
114         strlower_m(user2);
115         DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
116         ret = getpwnam_alloc_cached(mem_ctx, user2);
117         if(ret)
118                 goto done;
119
120         /* Try as given, if username wasn't originally lowercase */
121         if(strcmp(user, user2) != 0) {
122                 DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",
123                          user));
124                 ret = getpwnam_alloc_cached(mem_ctx, user);
125                 if(ret)
126                         goto done;
127         }
128
129         /* Try as uppercase, if username wasn't originally uppercase */
130         strupper_m(user2);
131         if(strcmp(user, user2) != 0) {
132                 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",
133                          user2));
134                 ret = getpwnam_alloc_cached(mem_ctx, user2);
135                 if(ret)
136                         goto done;
137         }
138
139         /* Try all combinations up to usernamelevel */
140         strlower_m(user2);
141         DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",
142                  lp_usernamelevel(), user2));
143         ret = uname_string_combinations(user2, mem_ctx, getpwnam_alloc_cached,
144                                         lp_usernamelevel());
145
146 done:
147         DEBUG(5,("Get_Pwnam_internals %s find user [%s]!\n",ret ?
148                  "did":"didn't", user));
149
150         return ret;
151 }
152
153 /****************************************************************************
154  Get_Pwnam wrapper without modification.
155   NOTE: This with NOT modify 'user'! 
156   This will return an allocated structure
157 ****************************************************************************/
158
159 struct passwd *Get_Pwnam_alloc(TALLOC_CTX *mem_ctx, const char *user)
160 {
161         fstring user2;
162
163         if ( *user == '\0' ) {
164                 DEBUG(10,("Get_Pwnam: empty username!\n"));
165                 return NULL;
166         }
167
168         fstrcpy(user2, user);
169
170         DEBUG(5,("Finding user %s\n", user));
171
172         return Get_Pwnam_internals(mem_ctx, user, user2);
173 }
174
175 /* The functions below have been taken from password.c and slightly modified */
176 /****************************************************************************
177  Apply a function to upper/lower case combinations
178  of a string and return true if one of them returns true.
179  Try all combinations with N uppercase letters.
180  offset is the first char to try and change (start with 0)
181  it assumes the string starts lowercased
182 ****************************************************************************/
183
184 static struct passwd *uname_string_combinations2(char *s, TALLOC_CTX *mem_ctx,
185                                                  int offset,
186                                                  struct passwd *(*fn)(TALLOC_CTX *mem_ctx, const char *),
187                                                  int N)
188 {
189         ssize_t len = (ssize_t)strlen(s);
190         int i;
191         struct passwd *ret;
192
193         if (N <= 0 || offset >= len)
194                 return(fn(mem_ctx, s));
195
196         for (i=offset;i<(len-(N-1));i++) {
197                 char c = s[i];
198                 if (!islower_ascii((int)c))
199                         continue;
200                 s[i] = toupper_ascii(c);
201                 ret = uname_string_combinations2(s, mem_ctx, i+1, fn, N-1);
202                 if(ret)
203                         return(ret);
204                 s[i] = c;
205         }
206         return(NULL);
207 }
208
209 /****************************************************************************
210  Apply a function to upper/lower case combinations
211  of a string and return true if one of them returns true.
212  Try all combinations with up to N uppercase letters.
213  offset is the first char to try and change (start with 0)
214  it assumes the string starts lowercased
215 ****************************************************************************/
216
217 static struct passwd * uname_string_combinations(char *s, TALLOC_CTX *mem_ctx,
218                                                  struct passwd * (*fn)(TALLOC_CTX *mem_ctx, const char *),
219                                                  int N)
220 {
221         int n;
222         struct passwd *ret;
223
224         for (n=1;n<=N;n++) {
225                 ret = uname_string_combinations2(s,mem_ctx,0,fn,n);
226                 if(ret)
227                         return(ret);
228         }  
229         return(NULL);
230 }
231