[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / smbd / password.c
blobbf4e9258ff266307887b00223fb29c012e70eba9
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 /* users from session setup */
24 static char *session_userlist = NULL;
25 static int len_session_userlist = 0;
26 /* workgroup from session setup. */
27 static char *session_workgroup = NULL;
29 /* this holds info on user ids that are already validated for this VC */
30 static user_struct *validated_users;
31 static int next_vuid = VUID_OFFSET;
32 static int num_validated_vuids;
34 /****************************************************************************
35 Check if a uid has been validated, and return an pointer to the user_struct
36 if it has. NULL if not. vuid is biased by an offset. This allows us to
37 tell random client vuid's (normally zero) from valid vuids.
38 ****************************************************************************/
40 user_struct *get_valid_user_struct(uint16 vuid)
42 user_struct *usp;
43 int count=0;
45 if (vuid == UID_FIELD_INVALID)
46 return NULL;
48 for (usp=validated_users;usp;usp=usp->next,count++) {
49 if (vuid == usp->vuid && usp->server_info) {
50 if (count > 10) {
51 DLIST_PROMOTE(validated_users, usp);
53 return usp;
57 return NULL;
60 /****************************************************************************
61 Get the user struct of a partial NTLMSSP login
62 ****************************************************************************/
64 user_struct *get_partial_auth_user_struct(uint16 vuid)
66 user_struct *usp;
67 int count=0;
69 if (vuid == UID_FIELD_INVALID)
70 return NULL;
72 for (usp=validated_users;usp;usp=usp->next,count++) {
73 if (vuid == usp->vuid && !usp->server_info) {
74 if (count > 10) {
75 DLIST_PROMOTE(validated_users, usp);
77 return usp;
81 return NULL;
84 /****************************************************************************
85 Invalidate a uid.
86 ****************************************************************************/
88 void invalidate_vuid(uint16 vuid)
90 user_struct *vuser = get_valid_user_struct(vuid);
92 if (vuser == NULL)
93 return;
95 SAFE_FREE(vuser->homedir);
96 SAFE_FREE(vuser->unix_homedir);
97 SAFE_FREE(vuser->logon_script);
99 session_yield(vuser);
100 SAFE_FREE(vuser->session_keystr);
102 TALLOC_FREE(vuser->server_info);
104 data_blob_free(&vuser->session_key);
106 DLIST_REMOVE(validated_users, vuser);
108 /* clear the vuid from the 'cache' on each connection, and
109 from the vuid 'owner' of connections */
110 conn_clear_vuid_cache(vuid);
112 SAFE_FREE(vuser->groups);
113 TALLOC_FREE(vuser->nt_user_token);
115 SAFE_FREE(vuser);
116 num_validated_vuids--;
119 /****************************************************************************
120 Invalidate all vuid entries for this process.
121 ****************************************************************************/
123 void invalidate_all_vuids(void)
125 user_struct *usp, *next=NULL;
127 for (usp=validated_users;usp;usp=next) {
128 next = usp->next;
130 invalidate_vuid(usp->vuid);
135 * register that a valid login has been performed, establish 'session'.
136 * @param server_info The token returned from the authentication process.
137 * (now 'owned' by register_vuid)
139 * @param session_key The User session key for the login session (now also
140 * 'owned' by register_vuid)
142 * @param respose_blob The NT challenge-response, if available. (May be
143 * freed after this call)
145 * @param smb_name The untranslated name of the user
147 * @return Newly allocated vuid, biased by an offset. (This allows us to
148 * tell random client vuid's (normally zero) from valid vuids.)
152 int register_vuid(auth_serversupplied_info *server_info,
153 DATA_BLOB session_key, DATA_BLOB response_blob,
154 const char *smb_name)
156 user_struct *vuser = NULL;
158 /* Paranoia check. */
159 if(lp_security() == SEC_SHARE) {
160 smb_panic("Tried to register uid in security=share\n");
163 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
164 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
165 data_blob_free(&session_key);
166 return UID_FIELD_INVALID;
169 if((vuser = SMB_MALLOC_P(user_struct)) == NULL) {
170 DEBUG(0,("Failed to malloc users struct!\n"));
171 data_blob_free(&session_key);
172 return UID_FIELD_INVALID;
175 ZERO_STRUCTP(vuser);
177 /* Allocate a free vuid. Yes this is a linear search... :-) */
178 while( get_valid_user_struct(next_vuid) != NULL ) {
179 next_vuid++;
180 /* Check for vuid wrap. */
181 if (next_vuid == UID_FIELD_INVALID)
182 next_vuid = VUID_OFFSET;
185 DEBUG(10,("register_vuid: allocated vuid = %u\n",
186 (unsigned int)next_vuid ));
188 vuser->vuid = next_vuid;
190 if (!server_info) {
192 * This happens in an unfinished NTLMSSP session setup. We
193 * need to allocate a vuid between the first and second calls
194 * to NTLMSSP.
196 next_vuid++;
197 num_validated_vuids++;
199 vuser->server_info = NULL;
201 DLIST_ADD(validated_users, vuser);
203 return vuser->vuid;
206 /* the next functions should be done by a SID mapping system (SMS) as
207 * the new real sam db won't have reference to unix uids or gids
210 vuser->uid = server_info->uid;
211 vuser->gid = server_info->gid;
213 vuser->n_groups = server_info->n_groups;
214 if (vuser->n_groups) {
215 if (!(vuser->groups = (gid_t *)memdup(server_info->groups,
216 sizeof(gid_t) *
217 vuser->n_groups))) {
218 DEBUG(0,("register_vuid: failed to memdup "
219 "vuser->groups\n"));
220 data_blob_free(&session_key);
221 free(vuser);
222 TALLOC_FREE(server_info);
223 return UID_FIELD_INVALID;
227 vuser->guest = server_info->guest;
228 fstrcpy(vuser->user.unix_name, server_info->unix_name);
230 /* This is a potentially untrusted username */
231 alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$",
232 sizeof(vuser->user.smb_name));
234 fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
235 fstrcpy(vuser->user.full_name,
236 pdb_get_fullname(server_info->sam_account));
239 /* Keep the homedir handy */
240 const char *homedir =
241 pdb_get_homedir(server_info->sam_account);
242 const char *logon_script =
243 pdb_get_logon_script(server_info->sam_account);
245 if (!IS_SAM_DEFAULT(server_info->sam_account,
246 PDB_UNIXHOMEDIR)) {
247 const char *unix_homedir =
248 pdb_get_unix_homedir(server_info->sam_account);
249 if (unix_homedir) {
250 vuser->unix_homedir =
251 smb_xstrdup(unix_homedir);
253 } else {
254 struct passwd *passwd =
255 getpwnam_alloc(NULL, vuser->user.unix_name);
256 if (passwd) {
257 vuser->unix_homedir =
258 smb_xstrdup(passwd->pw_dir);
259 TALLOC_FREE(passwd);
263 if (homedir) {
264 vuser->homedir = smb_xstrdup(homedir);
266 if (logon_script) {
267 vuser->logon_script = smb_xstrdup(logon_script);
271 vuser->session_key = session_key;
273 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
274 (unsigned int)vuser->uid,
275 (unsigned int)vuser->gid,
276 vuser->user.unix_name, vuser->user.smb_name,
277 vuser->user.domain, vuser->guest ));
279 DEBUG(3, ("User name: %s\tReal name: %s\n", vuser->user.unix_name,
280 vuser->user.full_name));
282 if (server_info->ptok) {
283 vuser->nt_user_token = dup_nt_token(NULL, server_info->ptok);
284 } else {
285 DEBUG(1, ("server_info does not contain a user_token - "
286 "cannot continue\n"));
287 TALLOC_FREE(server_info);
288 data_blob_free(&session_key);
289 SAFE_FREE(vuser->homedir);
290 SAFE_FREE(vuser->unix_homedir);
291 SAFE_FREE(vuser->logon_script);
293 SAFE_FREE(vuser);
294 return UID_FIELD_INVALID;
297 /* use this to keep tabs on all our info from the authentication */
298 vuser->server_info = server_info;
300 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",
301 (int)vuser->uid,vuser->user.unix_name, vuser->vuid));
303 next_vuid++;
304 num_validated_vuids++;
306 DLIST_ADD(validated_users, vuser);
308 if (!session_claim(vuser)) {
309 DEBUG(1, ("Failed to claim session for vuid=%d\n",
310 vuser->vuid));
311 invalidate_vuid(vuser->vuid);
312 return UID_FIELD_INVALID;
315 /* Register a home dir service for this user iff
317 (a) This is not a guest connection,
318 (b) we have a home directory defined
319 (c) there s not an existing static share by that name
321 If a share exists by this name (autoloaded or not) reuse it . */
323 vuser->homes_snum = -1;
325 if ( (!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir))
327 int servicenumber = lp_servicenumber(vuser->user.unix_name);
329 if ( servicenumber == -1 ) {
330 DEBUG(3, ("Adding homes service for user '%s' using "
331 "home directory: '%s'\n",
332 vuser->user.unix_name, vuser->unix_homedir));
333 vuser->homes_snum =
334 add_home_service(vuser->user.unix_name,
335 vuser->user.unix_name,
336 vuser->unix_homedir);
337 } else {
338 DEBUG(3, ("Using static (or previously created) "
339 "service for user '%s'; path = '%s'\n",
340 vuser->user.unix_name,
341 lp_pathname(servicenumber) ));
342 vuser->homes_snum = servicenumber;
346 if (srv_is_signing_negotiated() && !vuser->guest &&
347 !srv_signing_started()) {
348 /* Try and turn on server signing on the first non-guest
349 * sessionsetup. */
350 srv_set_signing(vuser->session_key, response_blob);
353 /* fill in the current_user_info struct */
354 set_current_user_info( &vuser->user );
357 return vuser->vuid;
360 /****************************************************************************
361 Add a name to the session users list.
362 ****************************************************************************/
364 void add_session_user(const char *user)
366 fstring suser;
367 struct passwd *passwd;
369 if (!(passwd = Get_Pwnam(user)))
370 return;
372 fstrcpy(suser,passwd->pw_name);
374 if(!*suser)
375 return;
377 if( session_userlist && in_list(suser,session_userlist,False) )
378 return;
380 if( !session_userlist ||
381 (strlen(suser) + strlen(session_userlist) + 2 >=
382 len_session_userlist) ) {
383 char *newlist;
385 if (len_session_userlist > 128 * PSTRING_LEN) {
386 DEBUG(3,("add_session_user: session userlist already "
387 "too large.\n"));
388 return;
390 newlist = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
391 session_userlist,
392 len_session_userlist + PSTRING_LEN );
393 if( newlist == NULL ) {
394 DEBUG(1,("Unable to resize session_userlist\n"));
395 return;
397 if (!session_userlist) {
398 *newlist = '\0';
400 session_userlist = newlist;
401 len_session_userlist += PSTRING_LEN;
404 safe_strcat(session_userlist," ",len_session_userlist-1);
405 safe_strcat(session_userlist,suser,len_session_userlist-1);
408 /****************************************************************************
409 In security=share mode we need to store the client workgroup, as that's
410 what Vista uses for the NTLMv2 calculation.
411 ****************************************************************************/
413 void add_session_workgroup(const char *workgroup)
415 if (session_workgroup) {
416 SAFE_FREE(session_workgroup);
418 session_workgroup = smb_xstrdup(workgroup);
421 /****************************************************************************
422 In security=share mode we need to return the client workgroup, as that's
423 what Vista uses for the NTLMv2 calculation.
424 ****************************************************************************/
426 const char *get_session_workgroup(void)
428 return session_workgroup;
431 /****************************************************************************
432 Check if a user is in a netgroup user list. If at first we don't succeed,
433 try lower case.
434 ****************************************************************************/
436 BOOL user_in_netgroup(const char *user, const char *ngname)
438 #ifdef HAVE_NETGROUP
439 static char *mydomain = NULL;
440 fstring lowercase_user;
442 if (mydomain == NULL)
443 yp_get_default_domain(&mydomain);
445 if(mydomain == NULL) {
446 DEBUG(5,("Unable to get default yp domain, let's try without specifying it\n"));
449 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
450 user, mydomain?mydomain:"(ANY)", ngname));
452 if (innetgr(ngname, NULL, user, mydomain)) {
453 DEBUG(5,("user_in_netgroup: Found\n"));
454 return (True);
455 } else {
458 * Ok, innetgr is case sensitive. Try once more with lowercase
459 * just in case. Attempt to fix #703. JRA.
462 fstrcpy(lowercase_user, user);
463 strlower_m(lowercase_user);
465 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
466 lowercase_user, mydomain?mydomain:"(ANY)", ngname));
468 if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
469 DEBUG(5,("user_in_netgroup: Found\n"));
470 return (True);
473 #endif /* HAVE_NETGROUP */
474 return False;
477 /****************************************************************************
478 Check if a user is in a user list - can check combinations of UNIX
479 and netgroup lists.
480 ****************************************************************************/
482 BOOL user_in_list(const char *user,const char **list)
484 if (!list || !*list)
485 return False;
487 DEBUG(10,("user_in_list: checking user %s in list\n", user));
489 while (*list) {
491 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
492 user, *list));
495 * Check raw username.
497 if (strequal(user, *list))
498 return(True);
501 * Now check to see if any combination
502 * of UNIX and netgroups has been specified.
505 if(**list == '@') {
507 * Old behaviour. Check netgroup list
508 * followed by UNIX list.
510 if(user_in_netgroup(user, *list +1))
511 return True;
512 if(user_in_group(user, *list +1))
513 return True;
514 } else if (**list == '+') {
516 if((*(*list +1)) == '&') {
518 * Search UNIX list followed by netgroup.
520 if(user_in_group(user, *list +2))
521 return True;
522 if(user_in_netgroup(user, *list +2))
523 return True;
525 } else {
528 * Just search UNIX list.
531 if(user_in_group(user, *list +1))
532 return True;
535 } else if (**list == '&') {
537 if(*(*list +1) == '+') {
539 * Search netgroup list followed by UNIX list.
541 if(user_in_netgroup(user, *list +2))
542 return True;
543 if(user_in_group(user, *list +2))
544 return True;
545 } else {
547 * Just search netgroup list.
549 if(user_in_netgroup(user, *list +1))
550 return True;
554 list++;
556 return(False);
559 /****************************************************************************
560 Check if a username is valid.
561 ****************************************************************************/
563 static BOOL user_ok(const char *user, int snum)
565 char **valid, **invalid;
566 BOOL ret;
568 valid = invalid = NULL;
569 ret = True;
571 if (lp_invalid_users(snum)) {
572 str_list_copy(&invalid, lp_invalid_users(snum));
573 if (invalid &&
574 str_list_substitute(invalid, "%S", lp_servicename(snum))) {
576 /* This is used in sec=share only, so no current user
577 * around to pass to str_list_sub_basic() */
579 if ( invalid && str_list_sub_basic(invalid, "", "") ) {
580 ret = !user_in_list(user,
581 (const char **)invalid);
585 if (invalid)
586 str_list_free (&invalid);
588 if (ret && lp_valid_users(snum)) {
589 str_list_copy(&valid, lp_valid_users(snum));
590 if ( valid &&
591 str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
593 /* This is used in sec=share only, so no current user
594 * around to pass to str_list_sub_basic() */
596 if ( valid && str_list_sub_basic(valid, "", "") ) {
597 ret = user_in_list(user, (const char **)valid);
601 if (valid)
602 str_list_free (&valid);
604 if (ret && lp_onlyuser(snum)) {
605 char **user_list = str_list_make (lp_username(snum), NULL);
606 if (user_list &&
607 str_list_substitute(user_list, "%S",
608 lp_servicename(snum))) {
609 ret = user_in_list(user, (const char **)user_list);
611 if (user_list) str_list_free (&user_list);
614 return(ret);
617 /****************************************************************************
618 Validate a group username entry. Return the username or NULL.
619 ****************************************************************************/
621 static char *validate_group(char *group, DATA_BLOB password,int snum)
623 #ifdef HAVE_NETGROUP
625 char *host, *user, *domain;
626 setnetgrent(group);
627 while (getnetgrent(&host, &user, &domain)) {
628 if (user) {
629 if (user_ok(user, snum) &&
630 password_ok(user,password)) {
631 endnetgrent();
632 return(user);
636 endnetgrent();
638 #endif
640 #ifdef HAVE_GETGRENT
642 struct group *gptr;
643 setgrent();
644 while ((gptr = (struct group *)getgrent())) {
645 if (strequal(gptr->gr_name,group))
646 break;
650 * As user_ok can recurse doing a getgrent(), we must
651 * copy the member list into a pstring on the stack before
652 * use. Bug pointed out by leon@eatworms.swmed.edu.
655 if (gptr) {
656 pstring member_list;
657 char *member;
658 size_t copied_len = 0;
659 int i;
661 *member_list = '\0';
662 member = member_list;
664 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
665 size_t member_len = strlen(gptr->gr_mem[i])+1;
666 if(copied_len+member_len < sizeof(pstring)) {
668 DEBUG(10,("validate_group: = gr_mem = "
669 "%s\n", gptr->gr_mem[i]));
671 safe_strcpy(member, gptr->gr_mem[i],
672 sizeof(pstring) -
673 copied_len - 1);
674 copied_len += member_len;
675 member += copied_len;
676 } else {
677 *member = '\0';
681 endgrent();
683 member = member_list;
684 while (*member) {
685 static fstring name;
686 fstrcpy(name,member);
687 if (user_ok(name,snum) &&
688 password_ok(name,password)) {
689 endgrent();
690 return(&name[0]);
693 DEBUG(10,("validate_group = member = %s\n",
694 member));
696 member += strlen(member) + 1;
698 } else {
699 endgrent();
700 return NULL;
703 #endif
704 return(NULL);
707 /****************************************************************************
708 Check for authority to login to a service with a given username/password.
709 Note this is *NOT* used when logging on using sessionsetup_and_X.
710 ****************************************************************************/
712 BOOL authorise_login(int snum, fstring user, DATA_BLOB password,
713 BOOL *guest)
715 BOOL ok = False;
717 #ifdef DEBUG_PASSWORD
718 DEBUG(100,("authorise_login: checking authorisation on "
719 "user=%s pass=%s\n", user,password.data));
720 #endif
722 *guest = False;
724 /* there are several possibilities:
725 1) login as the given user with given password
726 2) login as a previously registered username with the given
727 password
728 3) login as a session list username with the given password
729 4) login as a previously validated user/password pair
730 5) login as the "user =" user with given password
731 6) login as the "user =" user with no password
732 (guest connection)
733 7) login as guest user with no password
735 if the service is guest_only then steps 1 to 5 are skipped
738 /* now check the list of session users */
739 if (!ok) {
740 char *auser;
741 char *user_list = NULL;
743 if ( session_userlist )
744 user_list = SMB_STRDUP(session_userlist);
745 else
746 user_list = SMB_STRDUP("");
748 if (!user_list)
749 return(False);
751 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
752 auser = strtok(NULL,LIST_SEP)) {
753 fstring user2;
754 fstrcpy(user2,auser);
755 if (!user_ok(user2,snum))
756 continue;
758 if (password_ok(user2,password)) {
759 ok = True;
760 fstrcpy(user,user2);
761 DEBUG(3,("authorise_login: ACCEPTED: session "
762 "list username (%s) and given "
763 "password ok\n", user));
767 SAFE_FREE(user_list);
770 /* check the user= fields and the given password */
771 if (!ok && lp_username(snum)) {
772 char *auser;
773 pstring user_list;
774 pstrcpy(user_list,lp_username(snum));
776 pstring_sub(user_list,"%S",lp_servicename(snum));
778 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
779 auser = strtok(NULL,LIST_SEP)) {
780 if (*auser == '@') {
781 auser = validate_group(auser+1,password,snum);
782 if (auser) {
783 ok = True;
784 fstrcpy(user,auser);
785 DEBUG(3,("authorise_login: ACCEPTED: "
786 "group username and given "
787 "password ok (%s)\n", user));
789 } else {
790 fstring user2;
791 fstrcpy(user2,auser);
792 if (user_ok(user2,snum) &&
793 password_ok(user2,password)) {
794 ok = True;
795 fstrcpy(user,user2);
796 DEBUG(3,("authorise_login: ACCEPTED: "
797 "user list username and "
798 "given password ok (%s)\n",
799 user));
805 /* check for a normal guest connection */
806 if (!ok && GUEST_OK(snum)) {
807 fstring guestname;
808 fstrcpy(guestname,lp_guestaccount());
809 if (Get_Pwnam(guestname)) {
810 fstrcpy(user,guestname);
811 ok = True;
812 DEBUG(3,("authorise_login: ACCEPTED: guest account "
813 "and guest ok (%s)\n", user));
814 } else {
815 DEBUG(0,("authorise_login: Invalid guest account "
816 "%s??\n",guestname));
818 *guest = True;
821 if (ok && !user_ok(user, snum)) {
822 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
823 ok = False;
826 return(ok);