r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / smbd / password.c
blob1a7dc33c61e5c2de5a7286fdcee9cd2bc24bbfb7
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 /* users from session setup */
23 static char *session_userlist = NULL;
24 static int len_session_userlist = 0;
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 /****************************************************************************
34 Check if a uid has been validated, and return an pointer to the user_struct
35 if it has. NULL if not. vuid is biased by an offset. This allows us to
36 tell random client vuid's (normally zero) from valid vuids.
37 ****************************************************************************/
39 user_struct *get_valid_user_struct(uint16 vuid)
41 user_struct *usp;
42 int count=0;
44 if (vuid == UID_FIELD_INVALID)
45 return NULL;
47 for (usp=validated_users;usp;usp=usp->next,count++) {
48 if (vuid == usp->vuid && usp->server_info) {
49 if (count > 10) {
50 DLIST_PROMOTE(validated_users, usp);
52 return usp;
56 return NULL;
59 /****************************************************************************
60 Get the user struct of a partial NTLMSSP login
61 ****************************************************************************/
63 user_struct *get_partial_auth_user_struct(uint16 vuid)
65 user_struct *usp;
66 int count=0;
68 if (vuid == UID_FIELD_INVALID)
69 return NULL;
71 for (usp=validated_users;usp;usp=usp->next,count++) {
72 if (vuid == usp->vuid && !usp->server_info) {
73 if (count > 10) {
74 DLIST_PROMOTE(validated_users, usp);
76 return usp;
80 return NULL;
83 /****************************************************************************
84 Invalidate a uid.
85 ****************************************************************************/
87 void invalidate_vuid(uint16 vuid)
89 user_struct *vuser = get_valid_user_struct(vuid);
91 if (vuser == NULL)
92 return;
94 session_yield(vuser);
96 data_blob_free(&vuser->session_key);
98 DLIST_REMOVE(validated_users, vuser);
100 /* clear the vuid from the 'cache' on each connection, and
101 from the vuid 'owner' of connections */
102 conn_clear_vuid_cache(vuid);
104 TALLOC_FREE(vuser);
105 num_validated_vuids--;
108 /****************************************************************************
109 Invalidate all vuid entries for this process.
110 ****************************************************************************/
112 void invalidate_all_vuids(void)
114 user_struct *usp, *next=NULL;
116 for (usp=validated_users;usp;usp=next) {
117 next = usp->next;
119 invalidate_vuid(usp->vuid);
124 * register that a valid login has been performed, establish 'session'.
125 * @param server_info The token returned from the authentication process.
126 * (now 'owned' by register_vuid)
128 * @param session_key The User session key for the login session (now also
129 * 'owned' by register_vuid)
131 * @param respose_blob The NT challenge-response, if available. (May be
132 * freed after this call)
134 * @param smb_name The untranslated name of the user
136 * @return Newly allocated vuid, biased by an offset. (This allows us to
137 * tell random client vuid's (normally zero) from valid vuids.)
141 int register_vuid(auth_serversupplied_info *server_info,
142 DATA_BLOB session_key, DATA_BLOB response_blob,
143 const char *smb_name)
145 user_struct *vuser;
147 /* Paranoia check. */
148 if(lp_security() == SEC_SHARE) {
149 smb_panic("Tried to register uid in security=share");
152 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
153 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
154 data_blob_free(&session_key);
155 return UID_FIELD_INVALID;
158 if((vuser = TALLOC_ZERO_P(NULL, user_struct)) == NULL) {
159 DEBUG(0,("Failed to talloc users struct!\n"));
160 data_blob_free(&session_key);
161 return UID_FIELD_INVALID;
164 /* Allocate a free vuid. Yes this is a linear search... :-) */
165 while( get_valid_user_struct(next_vuid) != NULL ) {
166 next_vuid++;
167 /* Check for vuid wrap. */
168 if (next_vuid == UID_FIELD_INVALID)
169 next_vuid = VUID_OFFSET;
172 DEBUG(10,("register_vuid: allocated vuid = %u\n",
173 (unsigned int)next_vuid ));
175 vuser->vuid = next_vuid;
177 if (!server_info) {
179 * This happens in an unfinished NTLMSSP session setup. We
180 * need to allocate a vuid between the first and second calls
181 * to NTLMSSP.
183 next_vuid++;
184 num_validated_vuids++;
186 vuser->server_info = NULL;
188 DLIST_ADD(validated_users, vuser);
190 return vuser->vuid;
193 /* use this to keep tabs on all our info from the authentication */
194 vuser->server_info = server_info;
195 /* Ensure that the server_info will dissapear with the vuser it is now attached to */
196 talloc_steal(vuser, vuser->server_info);
198 /* the next functions should be done by a SID mapping system (SMS) as
199 * the new real sam db won't have reference to unix uids or gids
202 vuser->uid = server_info->uid;
203 vuser->gid = server_info->gid;
205 vuser->n_groups = server_info->n_groups;
206 if (vuser->n_groups) {
207 if (!(vuser->groups = (gid_t *)talloc_memdup(vuser, server_info->groups,
208 sizeof(gid_t) *
209 vuser->n_groups))) {
210 DEBUG(0,("register_vuid: failed to talloc_memdup "
211 "vuser->groups\n"));
212 data_blob_free(&session_key);
213 TALLOC_FREE(vuser);
214 return UID_FIELD_INVALID;
218 vuser->guest = server_info->guest;
219 fstrcpy(vuser->user.unix_name, server_info->unix_name);
221 /* This is a potentially untrusted username */
222 alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$",
223 sizeof(vuser->user.smb_name));
225 fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
226 fstrcpy(vuser->user.full_name,
227 pdb_get_fullname(server_info->sam_account));
230 /* Keep the homedir handy */
231 const char *homedir =
232 pdb_get_homedir(server_info->sam_account);
233 const char *logon_script =
234 pdb_get_logon_script(server_info->sam_account);
236 if (!IS_SAM_DEFAULT(server_info->sam_account,
237 PDB_UNIXHOMEDIR)) {
238 const char *unix_homedir =
239 pdb_get_unix_homedir(server_info->sam_account);
240 if (unix_homedir) {
241 vuser->unix_homedir = unix_homedir;
243 } else {
244 struct passwd *passwd =
245 getpwnam_alloc(vuser, vuser->user.unix_name);
246 if (passwd) {
247 vuser->unix_homedir = passwd->pw_dir;
248 /* Ensure that the unix_homedir now
249 * belongs to vuser, so it goes away
250 * with it, not with passwd below: */
251 talloc_steal(vuser, vuser->unix_homedir);
252 TALLOC_FREE(passwd);
256 if (homedir) {
257 vuser->homedir = homedir;
259 if (logon_script) {
260 vuser->logon_script = logon_script;
264 vuser->session_key = session_key;
266 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
267 (unsigned int)vuser->uid,
268 (unsigned int)vuser->gid,
269 vuser->user.unix_name, vuser->user.smb_name,
270 vuser->user.domain, vuser->guest ));
272 DEBUG(3, ("User name: %s\tReal name: %s\n", vuser->user.unix_name,
273 vuser->user.full_name));
275 if (server_info->ptok) {
276 vuser->nt_user_token = dup_nt_token(vuser, server_info->ptok);
277 } else {
278 DEBUG(1, ("server_info does not contain a user_token - "
279 "cannot continue\n"));
280 TALLOC_FREE(vuser);
281 data_blob_free(&session_key);
282 return UID_FIELD_INVALID;
285 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",
286 (int)vuser->uid,vuser->user.unix_name, vuser->vuid));
288 next_vuid++;
289 num_validated_vuids++;
291 DLIST_ADD(validated_users, vuser);
293 if (!session_claim(vuser)) {
294 DEBUG(1, ("Failed to claim session for vuid=%d\n",
295 vuser->vuid));
296 invalidate_vuid(vuser->vuid);
297 return UID_FIELD_INVALID;
300 /* Register a home dir service for this user iff
302 (a) This is not a guest connection,
303 (b) we have a home directory defined
304 (c) there s not an existing static share by that name
306 If a share exists by this name (autoloaded or not) reuse it . */
308 vuser->homes_snum = -1;
310 if ( (!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir))
312 int servicenumber = lp_servicenumber(vuser->user.unix_name);
314 if ( servicenumber == -1 ) {
315 DEBUG(3, ("Adding homes service for user '%s' using "
316 "home directory: '%s'\n",
317 vuser->user.unix_name, vuser->unix_homedir));
318 vuser->homes_snum =
319 add_home_service(vuser->user.unix_name,
320 vuser->user.unix_name,
321 vuser->unix_homedir);
322 } else {
323 DEBUG(3, ("Using static (or previously created) "
324 "service for user '%s'; path = '%s'\n",
325 vuser->user.unix_name,
326 lp_pathname(servicenumber) ));
327 vuser->homes_snum = servicenumber;
331 if (srv_is_signing_negotiated() && !vuser->guest &&
332 !srv_signing_started()) {
333 /* Try and turn on server signing on the first non-guest
334 * sessionsetup. */
335 srv_set_signing(vuser->session_key, response_blob);
338 /* fill in the current_user_info struct */
339 set_current_user_info( &vuser->user );
342 return vuser->vuid;
345 /****************************************************************************
346 Add a name to the session users list.
347 ****************************************************************************/
349 void add_session_user(const char *user)
351 fstring suser;
352 struct passwd *passwd;
354 if (!(passwd = Get_Pwnam(user)))
355 return;
357 fstrcpy(suser,passwd->pw_name);
359 if(!*suser)
360 return;
362 if( session_userlist && in_list(suser,session_userlist,False) )
363 return;
365 if( !session_userlist ||
366 (strlen(suser) + strlen(session_userlist) + 2 >=
367 len_session_userlist) ) {
368 char *newlist;
370 if (len_session_userlist > 128 * PSTRING_LEN) {
371 DEBUG(3,("add_session_user: session userlist already "
372 "too large.\n"));
373 return;
375 newlist = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
376 session_userlist,
377 len_session_userlist + PSTRING_LEN );
378 if( newlist == NULL ) {
379 DEBUG(1,("Unable to resize session_userlist\n"));
380 return;
382 if (!session_userlist) {
383 *newlist = '\0';
385 session_userlist = newlist;
386 len_session_userlist += PSTRING_LEN;
389 safe_strcat(session_userlist," ",len_session_userlist-1);
390 safe_strcat(session_userlist,suser,len_session_userlist-1);
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 ****************************************************************************/
398 void add_session_workgroup(const char *workgroup)
400 if (session_workgroup) {
401 SAFE_FREE(session_workgroup);
403 session_workgroup = smb_xstrdup(workgroup);
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 ****************************************************************************/
411 const char *get_session_workgroup(void)
413 return session_workgroup;
416 /****************************************************************************
417 Check if a user is in a netgroup user list. If at first we don't succeed,
418 try lower case.
419 ****************************************************************************/
421 BOOL user_in_netgroup(const char *user, const char *ngname)
423 #ifdef HAVE_NETGROUP
424 static char *mydomain = NULL;
425 fstring lowercase_user;
427 if (mydomain == NULL)
428 yp_get_default_domain(&mydomain);
430 if(mydomain == NULL) {
431 DEBUG(5,("Unable to get default yp domain, let's try without specifying it\n"));
434 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
435 user, mydomain?mydomain:"(ANY)", ngname));
437 if (innetgr(ngname, NULL, user, mydomain)) {
438 DEBUG(5,("user_in_netgroup: Found\n"));
439 return (True);
440 } else {
443 * Ok, innetgr is case sensitive. Try once more with lowercase
444 * just in case. Attempt to fix #703. JRA.
447 fstrcpy(lowercase_user, user);
448 strlower_m(lowercase_user);
450 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
451 lowercase_user, mydomain?mydomain:"(ANY)", ngname));
453 if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
454 DEBUG(5,("user_in_netgroup: Found\n"));
455 return (True);
458 #endif /* HAVE_NETGROUP */
459 return False;
462 /****************************************************************************
463 Check if a user is in a user list - can check combinations of UNIX
464 and netgroup lists.
465 ****************************************************************************/
467 BOOL user_in_list(const char *user,const char **list)
469 if (!list || !*list)
470 return False;
472 DEBUG(10,("user_in_list: checking user %s in list\n", user));
474 while (*list) {
476 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
477 user, *list));
480 * Check raw username.
482 if (strequal(user, *list))
483 return(True);
486 * Now check to see if any combination
487 * of UNIX and netgroups has been specified.
490 if(**list == '@') {
492 * Old behaviour. Check netgroup list
493 * followed by UNIX list.
495 if(user_in_netgroup(user, *list +1))
496 return True;
497 if(user_in_group(user, *list +1))
498 return True;
499 } else if (**list == '+') {
501 if((*(*list +1)) == '&') {
503 * Search UNIX list followed by netgroup.
505 if(user_in_group(user, *list +2))
506 return True;
507 if(user_in_netgroup(user, *list +2))
508 return True;
510 } else {
513 * Just search UNIX list.
516 if(user_in_group(user, *list +1))
517 return True;
520 } else if (**list == '&') {
522 if(*(*list +1) == '+') {
524 * Search netgroup list followed by UNIX list.
526 if(user_in_netgroup(user, *list +2))
527 return True;
528 if(user_in_group(user, *list +2))
529 return True;
530 } else {
532 * Just search netgroup list.
534 if(user_in_netgroup(user, *list +1))
535 return True;
539 list++;
541 return(False);
544 /****************************************************************************
545 Check if a username is valid.
546 ****************************************************************************/
548 static BOOL user_ok(const char *user, int snum)
550 char **valid, **invalid;
551 BOOL ret;
553 valid = invalid = NULL;
554 ret = True;
556 if (lp_invalid_users(snum)) {
557 str_list_copy(&invalid, lp_invalid_users(snum));
558 if (invalid &&
559 str_list_substitute(invalid, "%S", lp_servicename(snum))) {
561 /* This is used in sec=share only, so no current user
562 * around to pass to str_list_sub_basic() */
564 if ( invalid && str_list_sub_basic(invalid, "", "") ) {
565 ret = !user_in_list(user,
566 (const char **)invalid);
570 if (invalid)
571 str_list_free (&invalid);
573 if (ret && lp_valid_users(snum)) {
574 str_list_copy(&valid, lp_valid_users(snum));
575 if ( valid &&
576 str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
578 /* This is used in sec=share only, so no current user
579 * around to pass to str_list_sub_basic() */
581 if ( valid && str_list_sub_basic(valid, "", "") ) {
582 ret = user_in_list(user, (const char **)valid);
586 if (valid)
587 str_list_free (&valid);
589 if (ret && lp_onlyuser(snum)) {
590 char **user_list = str_list_make (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);
596 if (user_list) str_list_free (&user_list);
599 return(ret);
602 /****************************************************************************
603 Validate a group username entry. Return the username or NULL.
604 ****************************************************************************/
606 static char *validate_group(char *group, DATA_BLOB password,int snum)
608 #ifdef HAVE_NETGROUP
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);
621 endnetgrent();
623 #endif
625 #ifdef HAVE_GETGRENT
627 struct group *gptr;
628 setgrent();
629 while ((gptr = (struct group *)getgrent())) {
630 if (strequal(gptr->gr_name,group))
631 break;
635 * As user_ok can recurse doing a getgrent(), we must
636 * copy the member list into a pstring on the stack before
637 * use. Bug pointed out by leon@eatworms.swmed.edu.
640 if (gptr) {
641 pstring member_list;
642 char *member;
643 size_t copied_len = 0;
644 int i;
646 *member_list = '\0';
647 member = member_list;
649 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
650 size_t member_len = strlen(gptr->gr_mem[i])+1;
651 if(copied_len+member_len < sizeof(pstring)) {
653 DEBUG(10,("validate_group: = gr_mem = "
654 "%s\n", gptr->gr_mem[i]));
656 safe_strcpy(member, gptr->gr_mem[i],
657 sizeof(pstring) -
658 copied_len - 1);
659 copied_len += member_len;
660 member += copied_len;
661 } else {
662 *member = '\0';
666 endgrent();
668 member = member_list;
669 while (*member) {
670 static fstring name;
671 fstrcpy(name,member);
672 if (user_ok(name,snum) &&
673 password_ok(name,password)) {
674 endgrent();
675 return(&name[0]);
678 DEBUG(10,("validate_group = member = %s\n",
679 member));
681 member += strlen(member) + 1;
683 } else {
684 endgrent();
685 return NULL;
688 #endif
689 return(NULL);
692 /****************************************************************************
693 Check for authority to login to a service with a given username/password.
694 Note this is *NOT* used when logging on using sessionsetup_and_X.
695 ****************************************************************************/
697 BOOL authorise_login(int snum, fstring user, DATA_BLOB password,
698 BOOL *guest)
700 BOOL ok = False;
702 #ifdef DEBUG_PASSWORD
703 DEBUG(100,("authorise_login: checking authorisation on "
704 "user=%s pass=%s\n", user,password.data));
705 #endif
707 *guest = False;
709 /* there are several possibilities:
710 1) login as the given user with given password
711 2) login as a previously registered username with the given
712 password
713 3) login as a session list username with the given password
714 4) login as a previously validated user/password pair
715 5) login as the "user =" user with given password
716 6) login as the "user =" user with no password
717 (guest connection)
718 7) login as guest user with no password
720 if the service is guest_only then steps 1 to 5 are skipped
723 /* now check the list of session users */
724 if (!ok) {
725 char *auser;
726 char *user_list = NULL;
728 if ( session_userlist )
729 user_list = SMB_STRDUP(session_userlist);
730 else
731 user_list = SMB_STRDUP("");
733 if (!user_list)
734 return(False);
736 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
737 auser = strtok(NULL,LIST_SEP)) {
738 fstring user2;
739 fstrcpy(user2,auser);
740 if (!user_ok(user2,snum))
741 continue;
743 if (password_ok(user2,password)) {
744 ok = True;
745 fstrcpy(user,user2);
746 DEBUG(3,("authorise_login: ACCEPTED: session "
747 "list username (%s) and given "
748 "password ok\n", user));
752 SAFE_FREE(user_list);
755 /* check the user= fields and the given password */
756 if (!ok && lp_username(snum)) {
757 char *auser;
758 pstring user_list;
759 pstrcpy(user_list,lp_username(snum));
761 pstring_sub(user_list,"%S",lp_servicename(snum));
763 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
764 auser = strtok(NULL,LIST_SEP)) {
765 if (*auser == '@') {
766 auser = validate_group(auser+1,password,snum);
767 if (auser) {
768 ok = True;
769 fstrcpy(user,auser);
770 DEBUG(3,("authorise_login: ACCEPTED: "
771 "group username and given "
772 "password ok (%s)\n", user));
774 } else {
775 fstring user2;
776 fstrcpy(user2,auser);
777 if (user_ok(user2,snum) &&
778 password_ok(user2,password)) {
779 ok = True;
780 fstrcpy(user,user2);
781 DEBUG(3,("authorise_login: ACCEPTED: "
782 "user list username and "
783 "given password ok (%s)\n",
784 user));
790 /* check for a normal guest connection */
791 if (!ok && GUEST_OK(snum)) {
792 fstring guestname;
793 fstrcpy(guestname,lp_guestaccount());
794 if (Get_Pwnam(guestname)) {
795 fstrcpy(user,guestname);
796 ok = True;
797 DEBUG(3,("authorise_login: ACCEPTED: guest account "
798 "and guest ok (%s)\n", user));
799 } else {
800 DEBUG(0,("authorise_login: Invalid guest account "
801 "%s??\n",guestname));
803 *guest = True;
806 if (ok && !user_ok(user, snum)) {
807 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
808 ok = False;
811 return(ok);