1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Password and authentication handling
|
---|
4 | Copyright (C) Andrew Tridgell 1992-1998
|
---|
5 | Copyright (C) Jeremy Allison 2007.
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 |
|
---|
23 | /* users from session setup */
|
---|
24 | static char *session_userlist = NULL;
|
---|
25 | /* workgroup from session setup. */
|
---|
26 | static char *session_workgroup = NULL;
|
---|
27 |
|
---|
28 | /* this holds info on user ids that are already validated for this VC */
|
---|
29 | static user_struct *validated_users;
|
---|
30 | static uint16_t next_vuid = VUID_OFFSET;
|
---|
31 | static int num_validated_vuids;
|
---|
32 |
|
---|
33 | enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
|
---|
34 | SERVER_ALLOCATED_REQUIRED_NO,
|
---|
35 | SERVER_ALLOCATED_REQUIRED_ANY};
|
---|
36 |
|
---|
37 | static user_struct *get_valid_user_struct_internal(uint16 vuid,
|
---|
38 | enum server_allocated_state server_allocated)
|
---|
39 | {
|
---|
40 | user_struct *usp;
|
---|
41 | int count=0;
|
---|
42 |
|
---|
43 | if (vuid == UID_FIELD_INVALID)
|
---|
44 | return NULL;
|
---|
45 |
|
---|
46 | for (usp=validated_users;usp;usp=usp->next,count++) {
|
---|
47 | if (vuid == usp->vuid) {
|
---|
48 | switch (server_allocated) {
|
---|
49 | case SERVER_ALLOCATED_REQUIRED_YES:
|
---|
50 | if (usp->server_info == NULL) {
|
---|
51 | continue;
|
---|
52 | }
|
---|
53 | break;
|
---|
54 | case SERVER_ALLOCATED_REQUIRED_NO:
|
---|
55 | if (usp->server_info != NULL) {
|
---|
56 | continue;
|
---|
57 | }
|
---|
58 | case SERVER_ALLOCATED_REQUIRED_ANY:
|
---|
59 | break;
|
---|
60 | }
|
---|
61 | if (count > 10) {
|
---|
62 | DLIST_PROMOTE(validated_users, usp);
|
---|
63 | }
|
---|
64 | return usp;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | return NULL;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /****************************************************************************
|
---|
72 | Check if a uid has been validated, and return an pointer to the user_struct
|
---|
73 | if it has. NULL if not. vuid is biased by an offset. This allows us to
|
---|
74 | tell random client vuid's (normally zero) from valid vuids.
|
---|
75 | ****************************************************************************/
|
---|
76 |
|
---|
77 | user_struct *get_valid_user_struct(uint16 vuid)
|
---|
78 | {
|
---|
79 | return get_valid_user_struct_internal(vuid,
|
---|
80 | SERVER_ALLOCATED_REQUIRED_YES);
|
---|
81 | }
|
---|
82 |
|
---|
83 | bool is_partial_auth_vuid(uint16 vuid)
|
---|
84 | {
|
---|
85 | if (vuid == UID_FIELD_INVALID) {
|
---|
86 | return False;
|
---|
87 | }
|
---|
88 | return get_valid_user_struct_internal(vuid,
|
---|
89 | SERVER_ALLOCATED_REQUIRED_NO) ? True : False;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /****************************************************************************
|
---|
93 | Get the user struct of a partial NTLMSSP login
|
---|
94 | ****************************************************************************/
|
---|
95 |
|
---|
96 | user_struct *get_partial_auth_user_struct(uint16 vuid)
|
---|
97 | {
|
---|
98 | return get_valid_user_struct_internal(vuid,
|
---|
99 | SERVER_ALLOCATED_REQUIRED_NO);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /****************************************************************************
|
---|
103 | Invalidate a uid.
|
---|
104 | ****************************************************************************/
|
---|
105 |
|
---|
106 | void invalidate_vuid(uint16 vuid)
|
---|
107 | {
|
---|
108 | user_struct *vuser = NULL;
|
---|
109 |
|
---|
110 | if (vuid == UID_FIELD_INVALID) {
|
---|
111 | return;
|
---|
112 | }
|
---|
113 |
|
---|
114 | vuser = get_valid_user_struct_internal(vuid,
|
---|
115 | SERVER_ALLOCATED_REQUIRED_ANY);
|
---|
116 | if (vuser == NULL) {
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | session_yield(vuser);
|
---|
121 |
|
---|
122 | if (vuser->auth_ntlmssp_state) {
|
---|
123 | auth_ntlmssp_end(&vuser->auth_ntlmssp_state);
|
---|
124 | }
|
---|
125 |
|
---|
126 | DLIST_REMOVE(validated_users, vuser);
|
---|
127 |
|
---|
128 | /* clear the vuid from the 'cache' on each connection, and
|
---|
129 | from the vuid 'owner' of connections */
|
---|
130 | conn_clear_vuid_caches(vuid);
|
---|
131 |
|
---|
132 | TALLOC_FREE(vuser);
|
---|
133 | num_validated_vuids--;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /****************************************************************************
|
---|
137 | Invalidate all vuid entries for this process.
|
---|
138 | ****************************************************************************/
|
---|
139 |
|
---|
140 | void invalidate_all_vuids(void)
|
---|
141 | {
|
---|
142 | user_struct *usp, *next=NULL;
|
---|
143 |
|
---|
144 | for (usp=validated_users;usp;usp=next) {
|
---|
145 | next = usp->next;
|
---|
146 | invalidate_vuid(usp->vuid);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void increment_next_vuid(uint16_t *vuid)
|
---|
151 | {
|
---|
152 | *vuid += 1;
|
---|
153 |
|
---|
154 | /* Check for vuid wrap. */
|
---|
155 | if (*vuid == UID_FIELD_INVALID) {
|
---|
156 | *vuid = VUID_OFFSET;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /****************************************************
|
---|
161 | Create a new partial auth user struct.
|
---|
162 | *****************************************************/
|
---|
163 |
|
---|
164 | int register_initial_vuid(void)
|
---|
165 | {
|
---|
166 | user_struct *vuser;
|
---|
167 |
|
---|
168 | /* Paranoia check. */
|
---|
169 | if(lp_security() == SEC_SHARE) {
|
---|
170 | smb_panic("register_initial_vuid: "
|
---|
171 | "Tried to register uid in security=share");
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* Limit allowed vuids to 16bits - VUID_OFFSET. */
|
---|
175 | if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
|
---|
176 | return UID_FIELD_INVALID;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
|
---|
180 | DEBUG(0,("register_initial_vuid: "
|
---|
181 | "Failed to talloc users struct!\n"));
|
---|
182 | return UID_FIELD_INVALID;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* Allocate a free vuid. Yes this is a linear search... */
|
---|
186 | while( get_valid_user_struct_internal(next_vuid,
|
---|
187 | SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
|
---|
188 | increment_next_vuid(&next_vuid);
|
---|
189 | }
|
---|
190 |
|
---|
191 | DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
|
---|
192 | (unsigned int)next_vuid ));
|
---|
193 |
|
---|
194 | vuser->vuid = next_vuid;
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * This happens in an unfinished NTLMSSP session setup. We
|
---|
198 | * need to allocate a vuid between the first and second calls
|
---|
199 | * to NTLMSSP.
|
---|
200 | */
|
---|
201 | increment_next_vuid(&next_vuid);
|
---|
202 | num_validated_vuids++;
|
---|
203 |
|
---|
204 | DLIST_ADD(validated_users, vuser);
|
---|
205 | return vuser->vuid;
|
---|
206 | }
|
---|
207 |
|
---|
208 | static int register_homes_share(const char *username)
|
---|
209 | {
|
---|
210 | int result;
|
---|
211 | struct passwd *pwd;
|
---|
212 |
|
---|
213 | result = lp_servicenumber(username);
|
---|
214 | if (result != -1) {
|
---|
215 | DEBUG(3, ("Using static (or previously created) service for "
|
---|
216 | "user '%s'; path = '%s'\n", username,
|
---|
217 | lp_pathname(result)));
|
---|
218 | return result;
|
---|
219 | }
|
---|
220 |
|
---|
221 | pwd = getpwnam_alloc(talloc_tos(), username);
|
---|
222 |
|
---|
223 | if ((pwd == NULL) || (pwd->pw_dir[0] == '\0')) {
|
---|
224 | DEBUG(3, ("No home directory defined for user '%s'\n",
|
---|
225 | username));
|
---|
226 | TALLOC_FREE(pwd);
|
---|
227 | return -1;
|
---|
228 | }
|
---|
229 | #ifdef __OS2__
|
---|
230 | /* On OS/2 we use drive letters which have a colon. This is also the field
|
---|
231 | separator in master.passwd, so we use a $ instead of a colon for the drive
|
---|
232 | separator, ie e$/user instead of e:/user. This code simply exchanges any $
|
---|
233 | for a : in the user's homedir */
|
---|
234 | if (pwd->pw_dir[1] == '$')
|
---|
235 | pwd->pw_dir[1] = ':';
|
---|
236 | #endif
|
---|
237 | DEBUG(3, ("Adding homes service for user '%s' using home directory: "
|
---|
238 | "'%s'\n", username, pwd->pw_dir));
|
---|
239 |
|
---|
240 | result = add_home_service(username, username, pwd->pw_dir);
|
---|
241 |
|
---|
242 | TALLOC_FREE(pwd);
|
---|
243 | return result;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * register that a valid login has been performed, establish 'session'.
|
---|
248 | * @param server_info The token returned from the authentication process.
|
---|
249 | * (now 'owned' by register_existing_vuid)
|
---|
250 | *
|
---|
251 | * @param session_key The User session key for the login session (now also
|
---|
252 | * 'owned' by register_existing_vuid)
|
---|
253 | *
|
---|
254 | * @param respose_blob The NT challenge-response, if available. (May be
|
---|
255 | * freed after this call)
|
---|
256 | *
|
---|
257 | * @param smb_name The untranslated name of the user
|
---|
258 | *
|
---|
259 | * @return Newly allocated vuid, biased by an offset. (This allows us to
|
---|
260 | * tell random client vuid's (normally zero) from valid vuids.)
|
---|
261 | *
|
---|
262 | */
|
---|
263 |
|
---|
264 | int register_existing_vuid(uint16 vuid,
|
---|
265 | auth_serversupplied_info *server_info,
|
---|
266 | DATA_BLOB response_blob,
|
---|
267 | const char *smb_name)
|
---|
268 | {
|
---|
269 | fstring tmp;
|
---|
270 | user_struct *vuser;
|
---|
271 |
|
---|
272 | vuser = get_partial_auth_user_struct(vuid);
|
---|
273 | if (!vuser) {
|
---|
274 | goto fail;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /* Use this to keep tabs on all our info from the authentication */
|
---|
278 | vuser->server_info = talloc_move(vuser, &server_info);
|
---|
279 |
|
---|
280 | /* This is a potentially untrusted username */
|
---|
281 | alpha_strcpy(tmp, smb_name, ". _-$", sizeof(tmp));
|
---|
282 |
|
---|
283 | vuser->server_info->sanitized_username = talloc_strdup(
|
---|
284 | vuser->server_info, tmp);
|
---|
285 |
|
---|
286 | DEBUG(10,("register_existing_vuid: (%u,%u) %s %s %s guest=%d\n",
|
---|
287 | (unsigned int)vuser->server_info->utok.uid,
|
---|
288 | (unsigned int)vuser->server_info->utok.gid,
|
---|
289 | vuser->server_info->unix_name,
|
---|
290 | vuser->server_info->sanitized_username,
|
---|
291 | pdb_get_domain(vuser->server_info->sam_account),
|
---|
292 | vuser->server_info->guest ));
|
---|
293 |
|
---|
294 | DEBUG(3, ("register_existing_vuid: User name: %s\t"
|
---|
295 | "Real name: %s\n", vuser->server_info->unix_name,
|
---|
296 | pdb_get_fullname(vuser->server_info->sam_account)));
|
---|
297 |
|
---|
298 | if (!vuser->server_info->ptok) {
|
---|
299 | DEBUG(1, ("register_existing_vuid: server_info does not "
|
---|
300 | "contain a user_token - cannot continue\n"));
|
---|
301 | goto fail;
|
---|
302 | }
|
---|
303 |
|
---|
304 | DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
|
---|
305 | "and will be vuid %u\n", (int)vuser->server_info->utok.uid,
|
---|
306 | vuser->server_info->unix_name, vuser->vuid));
|
---|
307 |
|
---|
308 | if (!session_claim(vuser)) {
|
---|
309 | DEBUG(1, ("register_existing_vuid: Failed to claim session "
|
---|
310 | "for vuid=%d\n",
|
---|
311 | vuser->vuid));
|
---|
312 | goto fail;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /* Register a home dir service for this user if
|
---|
316 | (a) This is not a guest connection,
|
---|
317 | (b) we have a home directory defined
|
---|
318 | (c) there s not an existing static share by that name
|
---|
319 | If a share exists by this name (autoloaded or not) reuse it . */
|
---|
320 |
|
---|
321 | vuser->homes_snum = -1;
|
---|
322 |
|
---|
323 | if (!vuser->server_info->guest) {
|
---|
324 | vuser->homes_snum = register_homes_share(
|
---|
325 | vuser->server_info->unix_name);
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (srv_is_signing_negotiated() && !vuser->server_info->guest &&
|
---|
329 | !srv_signing_started()) {
|
---|
330 | /* Try and turn on server signing on the first non-guest
|
---|
331 | * sessionsetup. */
|
---|
332 | srv_set_signing(vuser->server_info->user_session_key, response_blob);
|
---|
333 | }
|
---|
334 |
|
---|
335 | /* fill in the current_user_info struct */
|
---|
336 | set_current_user_info(
|
---|
337 | vuser->server_info->sanitized_username,
|
---|
338 | vuser->server_info->unix_name,
|
---|
339 | pdb_get_fullname(vuser->server_info->sam_account),
|
---|
340 | pdb_get_domain(vuser->server_info->sam_account));
|
---|
341 |
|
---|
342 | return vuser->vuid;
|
---|
343 |
|
---|
344 | fail:
|
---|
345 |
|
---|
346 | if (vuser) {
|
---|
347 | invalidate_vuid(vuid);
|
---|
348 | }
|
---|
349 | return UID_FIELD_INVALID;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /****************************************************************************
|
---|
353 | Add a name to the session users list.
|
---|
354 | ****************************************************************************/
|
---|
355 |
|
---|
356 | void add_session_user(const char *user)
|
---|
357 | {
|
---|
358 | struct passwd *pw;
|
---|
359 | char *tmp;
|
---|
360 |
|
---|
361 | pw = Get_Pwnam_alloc(talloc_tos(), user);
|
---|
362 |
|
---|
363 | if (pw == NULL) {
|
---|
364 | return;
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (session_userlist == NULL) {
|
---|
368 | session_userlist = SMB_STRDUP(pw->pw_name);
|
---|
369 | goto done;
|
---|
370 | }
|
---|
371 |
|
---|
372 | if (in_list(pw->pw_name,session_userlist,False) ) {
|
---|
373 | goto done;
|
---|
374 | }
|
---|
375 |
|
---|
376 | if (strlen(session_userlist) > 128 * 1024) {
|
---|
377 | DEBUG(3,("add_session_user: session userlist already "
|
---|
378 | "too large.\n"));
|
---|
379 | goto done;
|
---|
380 | }
|
---|
381 |
|
---|
382 | if (asprintf(&tmp, "%s %s", session_userlist, pw->pw_name) == -1) {
|
---|
383 | DEBUG(3, ("asprintf failed\n"));
|
---|
384 | goto done;
|
---|
385 | }
|
---|
386 |
|
---|
387 | SAFE_FREE(session_userlist);
|
---|
388 | session_userlist = tmp;
|
---|
389 | done:
|
---|
390 | TALLOC_FREE(pw);
|
---|
391 | }
|
---|
392 |
|
---|
393 | /****************************************************************************
|
---|
394 | In security=share mode we need to store the client workgroup, as that's
|
---|
395 | what Vista uses for the NTLMv2 calculation.
|
---|
396 | ****************************************************************************/
|
---|
397 |
|
---|
398 | void add_session_workgroup(const char *workgroup)
|
---|
399 | {
|
---|
400 | if (session_workgroup) {
|
---|
401 | SAFE_FREE(session_workgroup);
|
---|
402 | }
|
---|
403 | session_workgroup = smb_xstrdup(workgroup);
|
---|
404 | }
|
---|
405 |
|
---|
406 | /****************************************************************************
|
---|
407 | In security=share mode we need to return the client workgroup, as that's
|
---|
408 | what Vista uses for the NTLMv2 calculation.
|
---|
409 | ****************************************************************************/
|
---|
410 |
|
---|
411 | const char *get_session_workgroup(void)
|
---|
412 | {
|
---|
413 | return session_workgroup;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /****************************************************************************
|
---|
417 | Check if a user is in a netgroup user list. If at first we don't succeed,
|
---|
418 | try lower case.
|
---|
419 | ****************************************************************************/
|
---|
420 |
|
---|
421 | bool user_in_netgroup(const char *user, const char *ngname)
|
---|
422 | {
|
---|
423 | #ifdef HAVE_NETGROUP
|
---|
424 | static char *mydomain = NULL;
|
---|
425 | fstring lowercase_user;
|
---|
426 |
|
---|
427 | if (mydomain == NULL)
|
---|
428 | yp_get_default_domain(&mydomain);
|
---|
429 |
|
---|
430 | if(mydomain == NULL) {
|
---|
431 | DEBUG(5,("Unable to get default yp domain, "
|
---|
432 | "let's try without specifying it\n"));
|
---|
433 | }
|
---|
434 |
|
---|
435 | DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
|
---|
436 | user, mydomain?mydomain:"(ANY)", ngname));
|
---|
437 |
|
---|
438 | if (innetgr(ngname, NULL, user, mydomain)) {
|
---|
439 | DEBUG(5,("user_in_netgroup: Found\n"));
|
---|
440 | return (True);
|
---|
441 | } else {
|
---|
442 |
|
---|
443 | /*
|
---|
444 | * Ok, innetgr is case sensitive. Try once more with lowercase
|
---|
445 | * just in case. Attempt to fix #703. JRA.
|
---|
446 | */
|
---|
447 |
|
---|
448 | fstrcpy(lowercase_user, user);
|
---|
449 | strlower_m(lowercase_user);
|
---|
450 |
|
---|
451 | DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
|
---|
452 | lowercase_user, mydomain?mydomain:"(ANY)", ngname));
|
---|
453 |
|
---|
454 | if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
|
---|
455 | DEBUG(5,("user_in_netgroup: Found\n"));
|
---|
456 | return (True);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | #endif /* HAVE_NETGROUP */
|
---|
460 | return False;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /****************************************************************************
|
---|
464 | Check if a user is in a user list - can check combinations of UNIX
|
---|
465 | and netgroup lists.
|
---|
466 | ****************************************************************************/
|
---|
467 |
|
---|
468 | bool user_in_list(const char *user,const char **list)
|
---|
469 | {
|
---|
470 | if (!list || !*list)
|
---|
471 | return False;
|
---|
472 |
|
---|
473 | DEBUG(10,("user_in_list: checking user %s in list\n", user));
|
---|
474 |
|
---|
475 | while (*list) {
|
---|
476 |
|
---|
477 | DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
|
---|
478 | user, *list));
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * Check raw username.
|
---|
482 | */
|
---|
483 | if (strequal(user, *list))
|
---|
484 | return(True);
|
---|
485 |
|
---|
486 | /*
|
---|
487 | * Now check to see if any combination
|
---|
488 | * of UNIX and netgroups has been specified.
|
---|
489 | */
|
---|
490 |
|
---|
491 | if(**list == '@') {
|
---|
492 | /*
|
---|
493 | * Old behaviour. Check netgroup list
|
---|
494 | * followed by UNIX list.
|
---|
495 | */
|
---|
496 | if(user_in_netgroup(user, *list +1))
|
---|
497 | return True;
|
---|
498 | if(user_in_group(user, *list +1))
|
---|
499 | return True;
|
---|
500 | } else if (**list == '+') {
|
---|
501 |
|
---|
502 | if((*(*list +1)) == '&') {
|
---|
503 | /*
|
---|
504 | * Search UNIX list followed by netgroup.
|
---|
505 | */
|
---|
506 | if(user_in_group(user, *list +2))
|
---|
507 | return True;
|
---|
508 | if(user_in_netgroup(user, *list +2))
|
---|
509 | return True;
|
---|
510 |
|
---|
511 | } else {
|
---|
512 |
|
---|
513 | /*
|
---|
514 | * Just search UNIX list.
|
---|
515 | */
|
---|
516 |
|
---|
517 | if(user_in_group(user, *list +1))
|
---|
518 | return True;
|
---|
519 | }
|
---|
520 |
|
---|
521 | } else if (**list == '&') {
|
---|
522 |
|
---|
523 | if(*(*list +1) == '+') {
|
---|
524 | /*
|
---|
525 | * Search netgroup list followed by UNIX list.
|
---|
526 | */
|
---|
527 | if(user_in_netgroup(user, *list +2))
|
---|
528 | return True;
|
---|
529 | if(user_in_group(user, *list +2))
|
---|
530 | return True;
|
---|
531 | } else {
|
---|
532 | /*
|
---|
533 | * Just search netgroup list.
|
---|
534 | */
|
---|
535 | if(user_in_netgroup(user, *list +1))
|
---|
536 | return True;
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 | list++;
|
---|
541 | }
|
---|
542 | return(False);
|
---|
543 | }
|
---|
544 |
|
---|
545 | /****************************************************************************
|
---|
546 | Check if a username is valid.
|
---|
547 | ****************************************************************************/
|
---|
548 |
|
---|
549 | static bool user_ok(const char *user, int snum)
|
---|
550 | {
|
---|
551 | char **valid, **invalid;
|
---|
552 | bool ret;
|
---|
553 |
|
---|
554 | valid = invalid = NULL;
|
---|
555 | ret = True;
|
---|
556 |
|
---|
557 | if (lp_invalid_users(snum)) {
|
---|
558 | str_list_copy(talloc_tos(), &invalid, lp_invalid_users(snum));
|
---|
559 | if (invalid &&
|
---|
560 | str_list_substitute(invalid, "%S", lp_servicename(snum))) {
|
---|
561 |
|
---|
562 | /* This is used in sec=share only, so no current user
|
---|
563 | * around to pass to str_list_sub_basic() */
|
---|
564 |
|
---|
565 | if ( invalid && str_list_sub_basic(invalid, "", "") ) {
|
---|
566 | ret = !user_in_list(user,
|
---|
567 | (const char **)invalid);
|
---|
568 | }
|
---|
569 | }
|
---|
570 | }
|
---|
571 | TALLOC_FREE(invalid);
|
---|
572 |
|
---|
573 | if (ret && lp_valid_users(snum)) {
|
---|
574 | str_list_copy(talloc_tos(), &valid, lp_valid_users(snum));
|
---|
575 | if ( valid &&
|
---|
576 | str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
|
---|
577 |
|
---|
578 | /* This is used in sec=share only, so no current user
|
---|
579 | * around to pass to str_list_sub_basic() */
|
---|
580 |
|
---|
581 | if ( valid && str_list_sub_basic(valid, "", "") ) {
|
---|
582 | ret = user_in_list(user, (const char **)valid);
|
---|
583 | }
|
---|
584 | }
|
---|
585 | }
|
---|
586 | TALLOC_FREE(valid);
|
---|
587 |
|
---|
588 | if (ret && lp_onlyuser(snum)) {
|
---|
589 | char **user_list = str_list_make(
|
---|
590 | talloc_tos(), lp_username(snum), NULL);
|
---|
591 | if (user_list &&
|
---|
592 | str_list_substitute(user_list, "%S",
|
---|
593 | lp_servicename(snum))) {
|
---|
594 | ret = user_in_list(user, (const char **)user_list);
|
---|
595 | }
|
---|
596 | TALLOC_FREE(user_list);
|
---|
597 | }
|
---|
598 |
|
---|
599 | return(ret);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /****************************************************************************
|
---|
603 | Validate a group username entry. Return the username or NULL.
|
---|
604 | ****************************************************************************/
|
---|
605 |
|
---|
606 | static char *validate_group(char *group, DATA_BLOB password,int snum)
|
---|
607 | {
|
---|
608 | #ifdef HAVE_NETGROUP
|
---|
609 | {
|
---|
610 | char *host, *user, *domain;
|
---|
611 | setnetgrent(group);
|
---|
612 | while (getnetgrent(&host, &user, &domain)) {
|
---|
613 | if (user) {
|
---|
614 | if (user_ok(user, snum) &&
|
---|
615 | password_ok(user,password)) {
|
---|
616 | endnetgrent();
|
---|
617 | return(user);
|
---|
618 | }
|
---|
619 | }
|
---|
620 | }
|
---|
621 | endnetgrent();
|
---|
622 | }
|
---|
623 | #endif
|
---|
624 |
|
---|
625 | #ifdef HAVE_GETGRENT
|
---|
626 | {
|
---|
627 | struct group *gptr;
|
---|
628 | setgrent();
|
---|
629 | while ((gptr = (struct group *)getgrent())) {
|
---|
630 | if (strequal(gptr->gr_name,group))
|
---|
631 | break;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * As user_ok can recurse doing a getgrent(), we must
|
---|
636 | * copy the member list onto the heap before
|
---|
637 | * use. Bug pointed out by leon@eatworms.swmed.edu.
|
---|
638 | */
|
---|
639 |
|
---|
640 | if (gptr) {
|
---|
641 | char *member_list = NULL;
|
---|
642 | size_t list_len = 0;
|
---|
643 | char *member;
|
---|
644 | int i;
|
---|
645 |
|
---|
646 | for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
|
---|
647 | list_len += strlen(gptr->gr_mem[i])+1;
|
---|
648 | }
|
---|
649 | list_len++;
|
---|
650 |
|
---|
651 | member_list = (char *)SMB_MALLOC(list_len);
|
---|
652 | if (!member_list) {
|
---|
653 | endgrent();
|
---|
654 | return NULL;
|
---|
655 | }
|
---|
656 |
|
---|
657 | *member_list = '\0';
|
---|
658 | member = member_list;
|
---|
659 |
|
---|
660 | for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
|
---|
661 | size_t member_len = strlen(gptr->gr_mem[i])+1;
|
---|
662 |
|
---|
663 | DEBUG(10,("validate_group: = gr_mem = "
|
---|
664 | "%s\n", gptr->gr_mem[i]));
|
---|
665 |
|
---|
666 | safe_strcpy(member, gptr->gr_mem[i],
|
---|
667 | list_len - (member-member_list));
|
---|
668 | member += member_len;
|
---|
669 | }
|
---|
670 |
|
---|
671 | endgrent();
|
---|
672 |
|
---|
673 | member = member_list;
|
---|
674 | while (*member) {
|
---|
675 | if (user_ok(member,snum) &&
|
---|
676 | password_ok(member,password)) {
|
---|
677 | char *name = talloc_strdup(talloc_tos(),
|
---|
678 | member);
|
---|
679 | SAFE_FREE(member_list);
|
---|
680 | return name;
|
---|
681 | }
|
---|
682 |
|
---|
683 | DEBUG(10,("validate_group = member = %s\n",
|
---|
684 | member));
|
---|
685 |
|
---|
686 | member += strlen(member) + 1;
|
---|
687 | }
|
---|
688 |
|
---|
689 | SAFE_FREE(member_list);
|
---|
690 | } else {
|
---|
691 | endgrent();
|
---|
692 | return NULL;
|
---|
693 | }
|
---|
694 | }
|
---|
695 | #endif
|
---|
696 | return(NULL);
|
---|
697 | }
|
---|
698 |
|
---|
699 | /****************************************************************************
|
---|
700 | Check for authority to login to a service with a given username/password.
|
---|
701 | Note this is *NOT* used when logging on using sessionsetup_and_X.
|
---|
702 | ****************************************************************************/
|
---|
703 |
|
---|
704 | bool authorise_login(int snum, fstring user, DATA_BLOB password,
|
---|
705 | bool *guest)
|
---|
706 | {
|
---|
707 | bool ok = False;
|
---|
708 |
|
---|
709 | #ifdef DEBUG_PASSWORD
|
---|
710 | DEBUG(100,("authorise_login: checking authorisation on "
|
---|
711 | "user=%s pass=%s\n", user,password.data));
|
---|
712 | #endif
|
---|
713 |
|
---|
714 | *guest = False;
|
---|
715 |
|
---|
716 | /* there are several possibilities:
|
---|
717 | 1) login as the given user with given password
|
---|
718 | 2) login as a previously registered username with the given
|
---|
719 | password
|
---|
720 | 3) login as a session list username with the given password
|
---|
721 | 4) login as a previously validated user/password pair
|
---|
722 | 5) login as the "user =" user with given password
|
---|
723 | 6) login as the "user =" user with no password
|
---|
724 | (guest connection)
|
---|
725 | 7) login as guest user with no password
|
---|
726 |
|
---|
727 | if the service is guest_only then steps 1 to 5 are skipped
|
---|
728 | */
|
---|
729 |
|
---|
730 | /* now check the list of session users */
|
---|
731 | if (!ok) {
|
---|
732 | char *auser;
|
---|
733 | char *user_list = NULL;
|
---|
734 | char *saveptr;
|
---|
735 |
|
---|
736 | if ( session_userlist )
|
---|
737 | user_list = SMB_STRDUP(session_userlist);
|
---|
738 | else
|
---|
739 | user_list = SMB_STRDUP("");
|
---|
740 |
|
---|
741 | if (!user_list)
|
---|
742 | return(False);
|
---|
743 |
|
---|
744 | for (auser = strtok_r(user_list, LIST_SEP, &saveptr);
|
---|
745 | !ok && auser;
|
---|
746 | auser = strtok_r(NULL, LIST_SEP, &saveptr)) {
|
---|
747 | fstring user2;
|
---|
748 | fstrcpy(user2,auser);
|
---|
749 | if (!user_ok(user2,snum))
|
---|
750 | continue;
|
---|
751 |
|
---|
752 | if (password_ok(user2,password)) {
|
---|
753 | ok = True;
|
---|
754 | fstrcpy(user,user2);
|
---|
755 | DEBUG(3,("authorise_login: ACCEPTED: session "
|
---|
756 | "list username (%s) and given "
|
---|
757 | "password ok\n", user));
|
---|
758 | }
|
---|
759 | }
|
---|
760 |
|
---|
761 | SAFE_FREE(user_list);
|
---|
762 | }
|
---|
763 |
|
---|
764 | /* check the user= fields and the given password */
|
---|
765 | if (!ok && lp_username(snum)) {
|
---|
766 | TALLOC_CTX *ctx = talloc_tos();
|
---|
767 | char *auser;
|
---|
768 | char *user_list = talloc_strdup(ctx, lp_username(snum));
|
---|
769 | char *saveptr;
|
---|
770 |
|
---|
771 | if (!user_list) {
|
---|
772 | goto check_guest;
|
---|
773 | }
|
---|
774 |
|
---|
775 | user_list = talloc_string_sub(ctx,
|
---|
776 | user_list,
|
---|
777 | "%S",
|
---|
778 | lp_servicename(snum));
|
---|
779 |
|
---|
780 | if (!user_list) {
|
---|
781 | goto check_guest;
|
---|
782 | }
|
---|
783 |
|
---|
784 | for (auser = strtok_r(user_list, LIST_SEP, &saveptr);
|
---|
785 | auser && !ok;
|
---|
786 | auser = strtok_r(NULL, LIST_SEP, &saveptr)) {
|
---|
787 | if (*auser == '@') {
|
---|
788 | auser = validate_group(auser+1,password,snum);
|
---|
789 | if (auser) {
|
---|
790 | ok = True;
|
---|
791 | fstrcpy(user,auser);
|
---|
792 | DEBUG(3,("authorise_login: ACCEPTED: "
|
---|
793 | "group username and given "
|
---|
794 | "password ok (%s)\n", user));
|
---|
795 | }
|
---|
796 | } else {
|
---|
797 | fstring user2;
|
---|
798 | fstrcpy(user2,auser);
|
---|
799 | if (user_ok(user2,snum) &&
|
---|
800 | password_ok(user2,password)) {
|
---|
801 | ok = True;
|
---|
802 | fstrcpy(user,user2);
|
---|
803 | DEBUG(3,("authorise_login: ACCEPTED: "
|
---|
804 | "user list username and "
|
---|
805 | "given password ok (%s)\n",
|
---|
806 | user));
|
---|
807 | }
|
---|
808 | }
|
---|
809 | }
|
---|
810 | }
|
---|
811 |
|
---|
812 | check_guest:
|
---|
813 |
|
---|
814 | /* check for a normal guest connection */
|
---|
815 | if (!ok && GUEST_OK(snum)) {
|
---|
816 | struct passwd *guest_pw;
|
---|
817 | fstring guestname;
|
---|
818 | fstrcpy(guestname,lp_guestaccount());
|
---|
819 | guest_pw = Get_Pwnam_alloc(talloc_tos(), guestname);
|
---|
820 | if (guest_pw != NULL) {
|
---|
821 | fstrcpy(user,guestname);
|
---|
822 | ok = True;
|
---|
823 | DEBUG(3,("authorise_login: ACCEPTED: guest account "
|
---|
824 | "and guest ok (%s)\n", user));
|
---|
825 | } else {
|
---|
826 | DEBUG(0,("authorise_login: Invalid guest account "
|
---|
827 | "%s??\n",guestname));
|
---|
828 | }
|
---|
829 | TALLOC_FREE(guest_pw);
|
---|
830 | *guest = True;
|
---|
831 | }
|
---|
832 |
|
---|
833 | if (ok && !user_ok(user, snum)) {
|
---|
834 | DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
|
---|
835 | ok = False;
|
---|
836 | }
|
---|
837 |
|
---|
838 | return(ok);
|
---|
839 | }
|
---|