2 Unix SMB/CIFS implementation.
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/>.
23 #include "system/passwd.h"
25 #include "../lib/util/util_pw.h"
27 /* internal functions */
28 static struct passwd
*uname_string_combinations(char *s
, TALLOC_CTX
*mem_ctx
,
29 struct passwd
* (*fn
) (TALLOC_CTX
*mem_ctx
, const char *),
31 static struct passwd
*uname_string_combinations2(char *s
, TALLOC_CTX
*mem_ctx
, int offset
,
32 struct passwd
* (*fn
) (TALLOC_CTX
*mem_ctx
, const char *),
35 static struct passwd
*getpwnam_alloc_cached(TALLOC_CTX
*mem_ctx
, const char *name
)
37 struct passwd
*pw
, *for_cache
;
39 pw
= (struct passwd
*)memcache_lookup_talloc(
40 NULL
, GETPWNAM_CACHE
, data_blob_string_const_null(name
));
42 return tcopy_passwd(mem_ctx
, pw
);
45 pw
= sys_getpwnam(name
);
50 for_cache
= tcopy_passwd(talloc_tos(), pw
);
51 if (for_cache
== NULL
) {
55 memcache_add_talloc(NULL
, GETPWNAM_CACHE
,
56 data_blob_string_const_null(name
), &for_cache
);
58 return tcopy_passwd(mem_ctx
, pw
);
61 /****************************************************************************
62 Flush all cached passwd structs.
63 ****************************************************************************/
65 void flush_pwnam_cache(void)
67 memcache_flush(NULL
, GETPWNAM_CACHE
);
70 /****************************************************************************
71 Get a users home directory.
72 ****************************************************************************/
74 char *get_user_home_dir(TALLOC_CTX
*mem_ctx
, const char *user
)
79 /* Ensure the user exists. */
81 pass
= Get_Pwnam_alloc(mem_ctx
, user
);
86 /* Return home directory from struct passwd. */
88 result
= talloc_move(mem_ctx
, &pass
->pw_dir
);
94 /****************************************************************************
95 * A wrapper for sys_getpwnam(). The following variations are tried:
97 * - in all lower case if this differs from transmitted
98 * - in all upper case if this differs from transmitted
99 * - using lp_usernamelevel() for permutations.
100 ****************************************************************************/
102 static struct passwd
*Get_Pwnam_internals(TALLOC_CTX
*mem_ctx
,
103 const char *user
, char *user2
)
105 struct passwd
*ret
= NULL
;
107 if (!user2
|| !(*user2
))
110 if (!user
|| !(*user
))
113 /* Try in all lower case first as this is the most
114 common case on UNIX systems */
116 DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2
));
117 ret
= getpwnam_alloc_cached(mem_ctx
, user2
);
121 /* Try as given, if username wasn't originally lowercase */
122 if(strcmp(user
, user2
) != 0) {
123 DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",
125 ret
= getpwnam_alloc_cached(mem_ctx
, user
);
130 /* Try as uppercase, if username wasn't originally uppercase */
132 if(strcmp(user
, user2
) != 0) {
133 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",
135 ret
= getpwnam_alloc_cached(mem_ctx
, user2
);
140 /* Try all combinations up to usernamelevel */
142 DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",
143 lp_usernamelevel(), user2
));
144 ret
= uname_string_combinations(user2
, mem_ctx
, getpwnam_alloc_cached
,
148 DEBUG(5,("Get_Pwnam_internals %s find user [%s]!\n",ret
?
149 "did":"didn't", user
));
154 /****************************************************************************
155 Get_Pwnam wrapper without modification.
156 NOTE: This with NOT modify 'user'!
157 This will return an allocated structure
158 ****************************************************************************/
160 struct passwd
*Get_Pwnam_alloc(TALLOC_CTX
*mem_ctx
, const char *user
)
164 if ( *user
== '\0' ) {
165 DEBUG(10,("Get_Pwnam: empty username!\n"));
169 fstrcpy(user2
, user
);
171 DEBUG(5,("Finding user %s\n", user
));
173 return Get_Pwnam_internals(mem_ctx
, user
, user2
);
176 /* The functions below have been taken from password.c and slightly modified */
177 /****************************************************************************
178 Apply a function to upper/lower case combinations
179 of a string and return true if one of them returns true.
180 Try all combinations with N uppercase letters.
181 offset is the first char to try and change (start with 0)
182 it assumes the string starts lowercased
183 ****************************************************************************/
185 static struct passwd
*uname_string_combinations2(char *s
, TALLOC_CTX
*mem_ctx
,
187 struct passwd
*(*fn
)(TALLOC_CTX
*mem_ctx
, const char *),
190 ssize_t len
= (ssize_t
)strlen(s
);
194 if (N
<= 0 || offset
>= len
)
195 return(fn(mem_ctx
, s
));
197 for (i
=offset
;i
<(len
-(N
-1));i
++) {
199 if (!islower_ascii((int)c
))
201 s
[i
] = toupper_ascii(c
);
202 ret
= uname_string_combinations2(s
, mem_ctx
, i
+1, fn
, N
-1);
210 /****************************************************************************
211 Apply a function to upper/lower case combinations
212 of a string and return true if one of them returns true.
213 Try all combinations with up to N uppercase letters.
214 offset is the first char to try and change (start with 0)
215 it assumes the string starts lowercased
216 ****************************************************************************/
218 static struct passwd
* uname_string_combinations(char *s
, TALLOC_CTX
*mem_ctx
,
219 struct passwd
* (*fn
)(TALLOC_CTX
*mem_ctx
, const char *),
226 ret
= uname_string_combinations2(s
,mem_ctx
,0,fn
,n
);