First part of bugfix for #5933
[Samba.git] / source3 / smbd / password.c
blobd729c2e77b13ffca665c47559aef7eb2255a4618
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2007.
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.
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.
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/>.
21 #include "includes.h"
23 /* users from session setup */
24 static char *session_userlist = NULL;
25 /* workgroup from session setup. */
26 static char *session_workgroup = NULL;
28 /* this holds info on user ids that are already validated for this VC */
29 static user_struct *validated_users;
30 static int next_vuid = VUID_OFFSET;
31 static int num_validated_vuids;
33 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
34 SERVER_ALLOCATED_REQUIRED_NO,
35 SERVER_ALLOCATED_REQUIRED_ANY};
37 static user_struct *get_valid_user_struct_internal(uint16 vuid,
38 enum server_allocated_state server_allocated)
40 user_struct *usp;
41 int count=0;
43 if (vuid == UID_FIELD_INVALID)
44 return NULL;
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;
53 break;
54 case SERVER_ALLOCATED_REQUIRED_NO:
55 if (usp->server_info != NULL) {
56 continue;
58 case SERVER_ALLOCATED_REQUIRED_ANY:
59 break;
61 if (count > 10) {
62 DLIST_PROMOTE(validated_users, usp);
64 return usp;
68 return NULL;
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 ****************************************************************************/
77 user_struct *get_valid_user_struct(uint16 vuid)
79 return get_valid_user_struct_internal(vuid,
80 SERVER_ALLOCATED_REQUIRED_YES);
83 bool is_partial_auth_vuid(uint16 vuid)
85 return (get_partial_auth_user_struct(vuid) != NULL);
88 /****************************************************************************
89 Get the user struct of a partial NTLMSSP login
90 ****************************************************************************/
92 user_struct *get_partial_auth_user_struct(uint16 vuid)
94 return get_valid_user_struct_internal(vuid,
95 SERVER_ALLOCATED_REQUIRED_NO);
98 /****************************************************************************
99 Invalidate a uid.
100 ****************************************************************************/
102 void invalidate_vuid(uint16 vuid)
104 user_struct *vuser = NULL;
106 vuser = get_valid_user_struct_internal(vuid,
107 SERVER_ALLOCATED_REQUIRED_ANY);
108 if (vuser == NULL) {
109 return;
112 session_yield(vuser);
114 if (vuser->auth_ntlmssp_state) {
115 auth_ntlmssp_end(&vuser->auth_ntlmssp_state);
118 DLIST_REMOVE(validated_users, vuser);
120 /* clear the vuid from the 'cache' on each connection, and
121 from the vuid 'owner' of connections */
122 conn_clear_vuid_caches(vuid);
124 TALLOC_FREE(vuser);
125 num_validated_vuids--;
128 /****************************************************************************
129 Invalidate all vuid entries for this process.
130 ****************************************************************************/
132 void invalidate_all_vuids(void)
134 while (validated_users != NULL) {
135 invalidate_vuid(validated_users->vuid);
139 /****************************************************
140 Create a new partial auth user struct.
141 *****************************************************/
143 int register_initial_vuid(void)
145 user_struct *vuser;
147 /* Paranoia check. */
148 if(lp_security() == SEC_SHARE) {
149 smb_panic("register_initial_vuid: "
150 "Tried to register uid in security=share");
153 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
154 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
155 return UID_FIELD_INVALID;
158 if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
159 DEBUG(0,("register_initial_vuid: "
160 "Failed to talloc users struct!\n"));
161 return UID_FIELD_INVALID;
164 /* Allocate a free vuid. Yes this is a linear search... */
165 while( get_valid_user_struct_internal(next_vuid,
166 SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
167 next_vuid++;
168 /* Check for vuid wrap. */
169 if (next_vuid == UID_FIELD_INVALID) {
170 next_vuid = VUID_OFFSET;
174 DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
175 (unsigned int)next_vuid ));
177 vuser->vuid = next_vuid;
180 * This happens in an unfinished NTLMSSP session setup. We
181 * need to allocate a vuid between the first and second calls
182 * to NTLMSSP.
184 next_vuid++;
185 num_validated_vuids++;
187 DLIST_ADD(validated_users, vuser);
188 return vuser->vuid;
191 static int register_homes_share(const char *username)
193 int result;
194 struct passwd *pwd;
196 result = lp_servicenumber(username);
197 if (result != -1) {
198 DEBUG(3, ("Using static (or previously created) service for "
199 "user '%s'; path = '%s'\n", username,
200 lp_pathname(result)));
201 return result;
204 pwd = getpwnam_alloc(talloc_tos(), username);
206 if ((pwd == NULL) || (pwd->pw_dir[0] == '\0')) {
207 DEBUG(3, ("No home directory defined for user '%s'\n",
208 username));
209 TALLOC_FREE(pwd);
210 return -1;
213 DEBUG(3, ("Adding homes service for user '%s' using home directory: "
214 "'%s'\n", username, pwd->pw_dir));
216 result = add_home_service(username, username, pwd->pw_dir);
218 TALLOC_FREE(pwd);
219 return result;
223 * register that a valid login has been performed, establish 'session'.
224 * @param server_info The token returned from the authentication process.
225 * (now 'owned' by register_existing_vuid)
227 * @param session_key The User session key for the login session (now also
228 * 'owned' by register_existing_vuid)
230 * @param respose_blob The NT challenge-response, if available. (May be
231 * freed after this call)
233 * @param smb_name The untranslated name of the user
235 * @return Newly allocated vuid, biased by an offset. (This allows us to
236 * tell random client vuid's (normally zero) from valid vuids.)
240 int register_existing_vuid(uint16 vuid,
241 auth_serversupplied_info *server_info,
242 DATA_BLOB response_blob,
243 const char *smb_name)
245 fstring tmp;
246 user_struct *vuser;
248 vuser = get_partial_auth_user_struct(vuid);
249 if (!vuser) {
250 goto fail;
253 /* Use this to keep tabs on all our info from the authentication */
254 vuser->server_info = talloc_move(vuser, &server_info);
256 /* This is a potentially untrusted username */
257 alpha_strcpy(tmp, smb_name, ". _-$", sizeof(tmp));
259 vuser->server_info->sanitized_username = talloc_strdup(
260 vuser->server_info, tmp);
262 DEBUG(10,("register_existing_vuid: (%u,%u) %s %s %s guest=%d\n",
263 (unsigned int)vuser->server_info->utok.uid,
264 (unsigned int)vuser->server_info->utok.gid,
265 vuser->server_info->unix_name,
266 vuser->server_info->sanitized_username,
267 pdb_get_domain(vuser->server_info->sam_account),
268 vuser->server_info->guest ));
270 DEBUG(3, ("register_existing_vuid: User name: %s\t"
271 "Real name: %s\n", vuser->server_info->unix_name,
272 pdb_get_fullname(vuser->server_info->sam_account)));
274 if (!vuser->server_info->ptok) {
275 DEBUG(1, ("register_existing_vuid: server_info does not "
276 "contain a user_token - cannot continue\n"));
277 goto fail;
280 DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
281 "and will be vuid %u\n", (int)vuser->server_info->utok.uid,
282 vuser->server_info->unix_name, vuser->vuid));
284 if (!session_claim(vuser)) {
285 DEBUG(1, ("register_existing_vuid: Failed to claim session "
286 "for vuid=%d\n",
287 vuser->vuid));
288 goto fail;
291 /* Register a home dir service for this user if
292 (a) This is not a guest connection,
293 (b) we have a home directory defined
294 (c) there s not an existing static share by that name
295 If a share exists by this name (autoloaded or not) reuse it . */
297 vuser->homes_snum = -1;
299 if (!vuser->server_info->guest) {
300 vuser->homes_snum = register_homes_share(
301 vuser->server_info->unix_name);
304 if (srv_is_signing_negotiated() && !vuser->server_info->guest &&
305 !srv_signing_started()) {
306 /* Try and turn on server signing on the first non-guest
307 * sessionsetup. */
308 srv_set_signing(vuser->server_info->user_session_key, response_blob);
311 /* fill in the current_user_info struct */
312 set_current_user_info(
313 vuser->server_info->sanitized_username,
314 vuser->server_info->unix_name,
315 pdb_get_fullname(vuser->server_info->sam_account),
316 pdb_get_domain(vuser->server_info->sam_account));
318 return vuser->vuid;
320 fail:
322 if (vuser) {
323 invalidate_vuid(vuid);
325 return UID_FIELD_INVALID;
328 /****************************************************************************
329 Add a name to the session users list.
330 ****************************************************************************/
332 void add_session_user(const char *user)
334 struct passwd *pw;
335 char *tmp;
337 pw = Get_Pwnam_alloc(talloc_tos(), user);
339 if (pw == NULL) {
340 return;
343 if (session_userlist == NULL) {
344 session_userlist = SMB_STRDUP(pw->pw_name);
345 goto done;
348 if (in_list(pw->pw_name,session_userlist,False) ) {
349 goto done;
352 if (strlen(session_userlist) > 128 * 1024) {
353 DEBUG(3,("add_session_user: session userlist already "
354 "too large.\n"));
355 goto done;
358 if (asprintf(&tmp, "%s %s", session_userlist, pw->pw_name) == -1) {
359 DEBUG(3, ("asprintf failed\n"));
360 goto done;
363 SAFE_FREE(session_userlist);
364 session_userlist = tmp;
365 done:
366 TALLOC_FREE(pw);
369 /****************************************************************************
370 In security=share mode we need to store the client workgroup, as that's
371 what Vista uses for the NTLMv2 calculation.
372 ****************************************************************************/
374 void add_session_workgroup(const char *workgroup)
376 if (session_workgroup) {
377 SAFE_FREE(session_workgroup);
379 session_workgroup = smb_xstrdup(workgroup);
382 /****************************************************************************
383 In security=share mode we need to return the client workgroup, as that's
384 what Vista uses for the NTLMv2 calculation.
385 ****************************************************************************/
387 const char *get_session_workgroup(void)
389 return session_workgroup;
392 /****************************************************************************
393 Check if a user is in a netgroup user list. If at first we don't succeed,
394 try lower case.
395 ****************************************************************************/
397 bool user_in_netgroup(const char *user, const char *ngname)
399 #ifdef HAVE_NETGROUP
400 static char *mydomain = NULL;
401 fstring lowercase_user;
403 if (mydomain == NULL)
404 yp_get_default_domain(&mydomain);
406 if(mydomain == NULL) {
407 DEBUG(5,("Unable to get default yp domain, "
408 "let's try without specifying it\n"));
411 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
412 user, mydomain?mydomain:"(ANY)", ngname));
414 if (innetgr(ngname, NULL, user, mydomain)) {
415 DEBUG(5,("user_in_netgroup: Found\n"));
416 return (True);
417 } else {
420 * Ok, innetgr is case sensitive. Try once more with lowercase
421 * just in case. Attempt to fix #703. JRA.
424 fstrcpy(lowercase_user, user);
425 strlower_m(lowercase_user);
427 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
428 lowercase_user, mydomain?mydomain:"(ANY)", ngname));
430 if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
431 DEBUG(5,("user_in_netgroup: Found\n"));
432 return (True);
435 #endif /* HAVE_NETGROUP */
436 return False;
439 /****************************************************************************
440 Check if a user is in a user list - can check combinations of UNIX
441 and netgroup lists.
442 ****************************************************************************/
444 bool user_in_list(const char *user,const char **list)
446 if (!list || !*list)
447 return False;
449 DEBUG(10,("user_in_list: checking user %s in list\n", user));
451 while (*list) {
453 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
454 user, *list));
457 * Check raw username.
459 if (strequal(user, *list))
460 return(True);
463 * Now check to see if any combination
464 * of UNIX and netgroups has been specified.
467 if(**list == '@') {
469 * Old behaviour. Check netgroup list
470 * followed by UNIX list.
472 if(user_in_netgroup(user, *list +1))
473 return True;
474 if(user_in_group(user, *list +1))
475 return True;
476 } else if (**list == '+') {
478 if((*(*list +1)) == '&') {
480 * Search UNIX list followed by netgroup.
482 if(user_in_group(user, *list +2))
483 return True;
484 if(user_in_netgroup(user, *list +2))
485 return True;
487 } else {
490 * Just search UNIX list.
493 if(user_in_group(user, *list +1))
494 return True;
497 } else if (**list == '&') {
499 if(*(*list +1) == '+') {
501 * Search netgroup list followed by UNIX list.
503 if(user_in_netgroup(user, *list +2))
504 return True;
505 if(user_in_group(user, *list +2))
506 return True;
507 } else {
509 * Just search netgroup list.
511 if(user_in_netgroup(user, *list +1))
512 return True;
516 list++;
518 return(False);
521 /****************************************************************************
522 Check if a username is valid.
523 ****************************************************************************/
525 static bool user_ok(const char *user, int snum)
527 char **valid, **invalid;
528 bool ret;
530 valid = invalid = NULL;
531 ret = True;
533 if (lp_invalid_users(snum)) {
534 invalid = str_list_copy(talloc_tos(), lp_invalid_users(snum));
535 if (invalid &&
536 str_list_substitute(invalid, "%S", lp_servicename(snum))) {
538 /* This is used in sec=share only, so no current user
539 * around to pass to str_list_sub_basic() */
541 if ( invalid && str_list_sub_basic(invalid, "", "") ) {
542 ret = !user_in_list(user,
543 (const char **)invalid);
547 TALLOC_FREE(invalid);
549 if (ret && lp_valid_users(snum)) {
550 valid = str_list_copy(talloc_tos(), lp_valid_users(snum));
551 if ( valid &&
552 str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
554 /* This is used in sec=share only, so no current user
555 * around to pass to str_list_sub_basic() */
557 if ( valid && str_list_sub_basic(valid, "", "") ) {
558 ret = user_in_list(user, (const char **)valid);
562 TALLOC_FREE(valid);
564 if (ret && lp_onlyuser(snum)) {
565 char **user_list = str_list_make_v3(
566 talloc_tos(), lp_username(snum), NULL);
567 if (user_list &&
568 str_list_substitute(user_list, "%S",
569 lp_servicename(snum))) {
570 ret = user_in_list(user, (const char **)user_list);
572 TALLOC_FREE(user_list);
575 return(ret);
578 /****************************************************************************
579 Validate a group username entry. Return the username or NULL.
580 ****************************************************************************/
582 static char *validate_group(char *group, DATA_BLOB password,int snum)
584 #ifdef HAVE_NETGROUP
586 char *host, *user, *domain;
587 setnetgrent(group);
588 while (getnetgrent(&host, &user, &domain)) {
589 if (user) {
590 if (user_ok(user, snum) &&
591 password_ok(user,password)) {
592 endnetgrent();
593 return(user);
597 endnetgrent();
599 #endif
601 #ifdef HAVE_GETGRENT
603 struct group *gptr;
604 setgrent();
605 while ((gptr = (struct group *)getgrent())) {
606 if (strequal(gptr->gr_name,group))
607 break;
611 * As user_ok can recurse doing a getgrent(), we must
612 * copy the member list onto the heap before
613 * use. Bug pointed out by leon@eatworms.swmed.edu.
616 if (gptr) {
617 char *member_list = NULL;
618 size_t list_len = 0;
619 char *member;
620 int i;
622 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
623 list_len += strlen(gptr->gr_mem[i])+1;
625 list_len++;
627 member_list = (char *)SMB_MALLOC(list_len);
628 if (!member_list) {
629 endgrent();
630 return NULL;
633 *member_list = '\0';
634 member = member_list;
636 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
637 size_t member_len = strlen(gptr->gr_mem[i])+1;
639 DEBUG(10,("validate_group: = gr_mem = "
640 "%s\n", gptr->gr_mem[i]));
642 safe_strcpy(member, gptr->gr_mem[i],
643 list_len - (member-member_list));
644 member += member_len;
647 endgrent();
649 member = member_list;
650 while (*member) {
651 if (user_ok(member,snum) &&
652 password_ok(member,password)) {
653 char *name = talloc_strdup(talloc_tos(),
654 member);
655 SAFE_FREE(member_list);
656 return name;
659 DEBUG(10,("validate_group = member = %s\n",
660 member));
662 member += strlen(member) + 1;
665 SAFE_FREE(member_list);
666 } else {
667 endgrent();
668 return NULL;
671 #endif
672 return(NULL);
675 /****************************************************************************
676 Check for authority to login to a service with a given username/password.
677 Note this is *NOT* used when logging on using sessionsetup_and_X.
678 ****************************************************************************/
680 bool authorise_login(int snum, fstring user, DATA_BLOB password,
681 bool *guest)
683 bool ok = False;
685 #ifdef DEBUG_PASSWORD
686 DEBUG(100,("authorise_login: checking authorisation on "
687 "user=%s pass=%s\n", user,password.data));
688 #endif
690 *guest = False;
692 /* there are several possibilities:
693 1) login as the given user with given password
694 2) login as a previously registered username with the given
695 password
696 3) login as a session list username with the given password
697 4) login as a previously validated user/password pair
698 5) login as the "user =" user with given password
699 6) login as the "user =" user with no password
700 (guest connection)
701 7) login as guest user with no password
703 if the service is guest_only then steps 1 to 5 are skipped
706 /* now check the list of session users */
707 if (!ok) {
708 char *auser;
709 char *user_list = NULL;
710 char *saveptr;
712 if ( session_userlist )
713 user_list = SMB_STRDUP(session_userlist);
714 else
715 user_list = SMB_STRDUP("");
717 if (!user_list)
718 return(False);
720 for (auser = strtok_r(user_list, LIST_SEP, &saveptr);
721 !ok && auser;
722 auser = strtok_r(NULL, LIST_SEP, &saveptr)) {
723 fstring user2;
724 fstrcpy(user2,auser);
725 if (!user_ok(user2,snum))
726 continue;
728 if (password_ok(user2,password)) {
729 ok = True;
730 fstrcpy(user,user2);
731 DEBUG(3,("authorise_login: ACCEPTED: session "
732 "list username (%s) and given "
733 "password ok\n", user));
737 SAFE_FREE(user_list);
740 /* check the user= fields and the given password */
741 if (!ok && lp_username(snum)) {
742 TALLOC_CTX *ctx = talloc_tos();
743 char *auser;
744 char *user_list = talloc_strdup(ctx, lp_username(snum));
745 char *saveptr;
747 if (!user_list) {
748 goto check_guest;
751 user_list = talloc_string_sub(ctx,
752 user_list,
753 "%S",
754 lp_servicename(snum));
756 if (!user_list) {
757 goto check_guest;
760 for (auser = strtok_r(user_list, LIST_SEP, &saveptr);
761 auser && !ok;
762 auser = strtok_r(NULL, LIST_SEP, &saveptr)) {
763 if (*auser == '@') {
764 auser = validate_group(auser+1,password,snum);
765 if (auser) {
766 ok = True;
767 fstrcpy(user,auser);
768 DEBUG(3,("authorise_login: ACCEPTED: "
769 "group username and given "
770 "password ok (%s)\n", user));
772 } else {
773 fstring user2;
774 fstrcpy(user2,auser);
775 if (user_ok(user2,snum) &&
776 password_ok(user2,password)) {
777 ok = True;
778 fstrcpy(user,user2);
779 DEBUG(3,("authorise_login: ACCEPTED: "
780 "user list username and "
781 "given password ok (%s)\n",
782 user));
788 check_guest:
790 /* check for a normal guest connection */
791 if (!ok && GUEST_OK(snum)) {
792 struct passwd *guest_pw;
793 fstring guestname;
794 fstrcpy(guestname,lp_guestaccount());
795 guest_pw = Get_Pwnam_alloc(talloc_tos(), guestname);
796 if (guest_pw != NULL) {
797 fstrcpy(user,guestname);
798 ok = True;
799 DEBUG(3,("authorise_login: ACCEPTED: guest account "
800 "and guest ok (%s)\n", user));
801 } else {
802 DEBUG(0,("authorise_login: Invalid guest account "
803 "%s??\n",guestname));
805 TALLOC_FREE(guest_pw);
806 *guest = True;
809 if (ok && !user_ok(user, snum)) {
810 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
811 ok = False;
814 return(ok);