This commit was manufactured by cvs2svn to create branch 'SAMBA_2_0'.
[Samba.git] / source / smbd / password.c
blob50a0ebd79e025a6cc03368f92c9e56936f738704
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Password and authentication handling
5 Copyright (C) Andrew Tridgell 1992-1998
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 extern int DEBUGLEVEL;
25 extern int Protocol;
27 /* users from session setup */
28 static pstring session_users="";
30 extern pstring scope;
31 extern pstring global_myname;
32 extern fstring global_myworkgroup;
34 /* Data to do lanman1/2 password challenge. */
35 static unsigned char saved_challenge[8];
36 static BOOL challenge_sent=False;
38 /*******************************************************************
39 Get the next challenge value - no repeats.
40 ********************************************************************/
41 void generate_next_challenge(char *challenge)
43 #if 0
44 /*
45 * Leave this ifdef'd out while we test
46 * the new crypto random number generator.
47 * JRA.
49 unsigned char buf[16];
50 static int counter = 0;
51 struct timeval tval;
52 int v1,v2;
54 /* get a sort-of random number */
55 GetTimeOfDay(&tval);
56 v1 = (counter++) + getpid() + tval.tv_sec;
57 v2 = (counter++) * getpid() + tval.tv_usec;
58 SIVAL(challenge,0,v1);
59 SIVAL(challenge,4,v2);
61 /* mash it up with md4 */
62 mdfour(buf, (unsigned char *)challenge, 8);
63 #else
64 unsigned char buf[8];
66 generate_random_buffer(buf,8,False);
67 #endif
68 memcpy(saved_challenge, buf, 8);
69 memcpy(challenge,buf,8);
70 challenge_sent = True;
73 /*******************************************************************
74 set the last challenge sent, usually from a password server
75 ********************************************************************/
76 BOOL set_challenge(unsigned char *challenge)
78 memcpy(saved_challenge,challenge,8);
79 challenge_sent = True;
80 return(True);
83 /*******************************************************************
84 get the last challenge sent
85 ********************************************************************/
86 static BOOL last_challenge(unsigned char *challenge)
88 if (!challenge_sent) return(False);
89 memcpy(challenge,saved_challenge,8);
90 return(True);
93 /* this holds info on user ids that are already validated for this VC */
94 static user_struct *validated_users = NULL;
95 static int num_validated_users = 0;
97 /****************************************************************************
98 check if a uid has been validated, and return an pointer to the user_struct
99 if it has. NULL if not. vuid is biased by an offset. This allows us to
100 tell random client vuid's (normally zero) from valid vuids.
101 ****************************************************************************/
102 user_struct *get_valid_user_struct(uint16 vuid)
104 if (vuid == UID_FIELD_INVALID)
105 return NULL;
106 vuid -= VUID_OFFSET;
107 if ((vuid >= (uint16)num_validated_users) ||
108 (validated_users[vuid].uid == (uid_t)-1) || (validated_users[vuid].gid == (gid_t)-1))
109 return NULL;
110 return &validated_users[vuid];
113 /****************************************************************************
114 invalidate a uid
115 ****************************************************************************/
116 void invalidate_vuid(uint16 vuid)
118 user_struct *vuser = get_valid_user_struct(vuid);
120 if (vuser == NULL) return;
122 vuser->uid = (uid_t)-1;
123 vuser->gid = (gid_t)-1;
125 vuser->n_sids = 0;
127 /* same number of igroups as groups */
128 vuser->n_groups = 0;
130 if (vuser->groups)
131 free((char *)vuser->groups);
133 if (vuser->sids)
134 free((char *)vuser->sids);
136 vuser->sids = NULL;
137 vuser->groups = NULL;
141 /****************************************************************************
142 return a validated username
143 ****************************************************************************/
144 char *validated_username(uint16 vuid)
146 user_struct *vuser = get_valid_user_struct(vuid);
147 if (vuser == NULL)
148 return 0;
149 return(vuser->name);
153 /****************************************************************************
154 Setup the groups a user belongs to.
155 ****************************************************************************/
156 int setup_groups(char *user, uid_t uid, gid_t gid, int *p_ngroups, gid_t **p_groups)
158 int i,ngroups;
159 gid_t grp = 0;
160 gid_t *groups = NULL;
162 if (-1 == initgroups(user,gid))
164 if (getuid() == 0)
166 DEBUG(0,("Unable to initgroups!\n"));
167 if (gid < 0 || gid > 16000 || uid < 0 || uid > 16000)
169 DEBUG(0,("This is probably a problem with the account %s\n", user));
172 return -1;
175 ngroups = sys_getgroups(0,&grp);
176 if (ngroups <= 0)
178 ngroups = 32;
181 if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL)
183 DEBUG(0,("setup_groups malloc fail !\n"));
184 return -1;
187 ngroups = sys_getgroups(ngroups,groups);
189 (*p_ngroups) = ngroups;
190 (*p_groups) = groups;
192 DEBUG( 3, ( "%s is in %d groups: ", user, ngroups ) );
193 for (i = 0; i < ngroups; i++ )
195 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
197 DEBUG( 3, ( "\n" ) );
199 return 0;
203 /****************************************************************************
204 register a uid/name pair as being valid and that a valid password
205 has been given. vuid is biased by an offset. This allows us to
206 tell random client vuid's (normally zero) from valid vuids.
207 ****************************************************************************/
208 uint16 register_vuid(uid_t uid,gid_t gid, char *unix_name, char *requested_name, BOOL guest)
210 user_struct *vuser;
211 struct passwd *pwfile; /* for getting real name from passwd file */
213 /* Ensure no vuid gets registered in share level security. */
214 if(lp_security() == SEC_SHARE)
215 return UID_FIELD_INVALID;
217 #if 0
219 * After observing MS-Exchange services writing to a Samba share
220 * I belive this code is incorrect. Each service does its own
221 * sessionsetup_and_X for the same user, and as each service shuts
222 * down, it does a user_logoff_and_X. As we are consolidating multiple
223 * sessionsetup_and_X's onto the same vuid here, when the first service
224 * shuts down, it invalidates all the open files for the other services.
225 * Hence I am removing this code and forcing each sessionsetup_and_X
226 * to get a new vuid.
227 * Jeremy Allison. (jallison@whistle.com).
230 int i;
231 for(i = 0; i < num_validated_users; i++) {
232 vuser = &validated_users[i];
233 if ( vuser->uid == uid )
234 return (uint16)(i + VUID_OFFSET); /* User already validated */
236 #endif
238 validated_users = (user_struct *)Realloc(validated_users,
239 sizeof(user_struct)*
240 (num_validated_users+1));
242 if (!validated_users)
244 DEBUG(0,("Failed to realloc users struct!\n"));
245 num_validated_users = 0;
246 return UID_FIELD_INVALID;
249 vuser = &validated_users[num_validated_users];
250 num_validated_users++;
252 vuser->uid = uid;
253 vuser->gid = gid;
254 vuser->guest = guest;
255 fstrcpy(vuser->name,unix_name);
256 fstrcpy(vuser->requested_name,requested_name);
258 vuser->n_sids = 0;
259 vuser->sids = NULL;
261 vuser->n_groups = 0;
262 vuser->groups = NULL;
264 /* Find all the groups this uid is in and store them.
265 Used by become_user() */
266 setup_groups(unix_name,uid,gid,
267 &vuser->n_groups,
268 &vuser->groups);
270 DEBUG(3,("uid %d registered to name %s\n",(int)uid,unix_name));
272 DEBUG(3, ("Clearing default real name\n"));
273 fstrcpy(vuser->real_name, "<Full Name>\0");
274 if (lp_unix_realname()) {
275 if ((pwfile=getpwnam(vuser->name))!= NULL)
277 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->name,pwfile->pw_gecos));
278 fstrcpy(vuser->real_name, pwfile->pw_gecos);
282 return (uint16)((num_validated_users - 1) + VUID_OFFSET);
286 /****************************************************************************
287 add a name to the session users list
288 ****************************************************************************/
289 void add_session_user(char *user)
291 fstring suser;
292 StrnCpy(suser,user,sizeof(suser)-1);
294 if (!Get_Pwnam(suser,True)) return;
296 if (suser && *suser && !in_list(suser,session_users,False))
298 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
299 DEBUG(1,("Too many session users??\n"));
300 else
302 pstrcat(session_users," ");
303 pstrcat(session_users,suser);
309 /****************************************************************************
310 update the encrypted smbpasswd file from the plaintext username and password
311 *****************************************************************************/
312 static BOOL update_smbpassword_file(char *user, char *password)
314 struct smb_passwd *smbpw;
315 BOOL ret;
317 become_root(0);
318 smbpw = getsmbpwnam(user);
319 unbecome_root(0);
321 if(smbpw == NULL) {
322 DEBUG(0,("getsmbpwnam returned NULL\n"));
323 return False;
326 /* Here, the flag is one, because we want to ignore the
327 XXXXXXX'd out password */
328 ret = change_oem_password( smbpw, password, True);
329 if (ret == False) {
330 DEBUG(3,("change_oem_password returned False\n"));
333 return ret;
340 /****************************************************************************
341 core of smb password checking routine.
342 ****************************************************************************/
343 BOOL smb_password_check(char *password, unsigned char *part_passwd, unsigned char *c8)
345 /* Finish the encryption of part_passwd. */
346 unsigned char p21[21];
347 unsigned char p24[24];
349 if (part_passwd == NULL)
350 DEBUG(10,("No password set - allowing access\n"));
351 /* No password set - always true ! */
352 if (part_passwd == NULL)
353 return 1;
355 memset(p21,'\0',21);
356 memcpy(p21,part_passwd,16);
357 E_P24(p21, c8, p24);
358 #if DEBUG_PASSWORD
360 int i;
361 DEBUG(100,("Part password (P16) was |"));
362 for(i = 0; i < 16; i++)
363 DEBUG(100,("%X ", (unsigned char)part_passwd[i]));
364 DEBUG(100,("|\n"));
365 DEBUG(100,("Password from client was |"));
366 for(i = 0; i < 24; i++)
367 DEBUG(100,("%X ", (unsigned char)password[i]));
368 DEBUG(100,("|\n"));
369 DEBUG(100,("Given challenge was |"));
370 for(i = 0; i < 8; i++)
371 DEBUG(100,("%X ", (unsigned char)c8[i]));
372 DEBUG(100,("|\n"));
373 DEBUG(100,("Value from encryption was |"));
374 for(i = 0; i < 24; i++)
375 DEBUG(100,("%X ", (unsigned char)p24[i]));
376 DEBUG(100,("|\n"));
378 #endif
379 return (memcmp(p24, password, 24) == 0);
382 /****************************************************************************
383 Do a specific test for an smb password being correct, given a smb_password and
384 the lanman and NT responses.
385 ****************************************************************************/
386 BOOL smb_password_ok(struct smb_passwd *smb_pass, uchar chal[8],
387 uchar lm_pass[24], uchar nt_pass[24])
389 uchar challenge[8];
391 if (!lm_pass || !smb_pass) return(False);
393 DEBUG(4,("Checking SMB password for user %s\n",
394 smb_pass->smb_name));
396 if(smb_pass->acct_ctrl & ACB_DISABLED) {
397 DEBUG(3,("account for user %s was disabled.\n",
398 smb_pass->smb_name));
399 return(False);
402 if (chal == NULL)
404 DEBUG(5,("use last SMBnegprot challenge\n"));
405 if (!last_challenge(challenge))
407 DEBUG(1,("no challenge done - password failed\n"));
408 return False;
411 else
413 DEBUG(5,("challenge received\n"));
414 memcpy(challenge, chal, 8);
417 if ((Protocol >= PROTOCOL_NT1) && (smb_pass->smb_nt_passwd != NULL)) {
418 /* We have the NT MD4 hash challenge available - see if we can
419 use it (ie. does it exist in the smbpasswd file).
421 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
422 if (smb_password_check((char *)nt_pass,
423 (uchar *)smb_pass->smb_nt_passwd,
424 challenge)) {
425 DEBUG(4,("NT MD4 password check succeeded\n"));
426 return(True);
428 DEBUG(4,("NT MD4 password check failed\n"));
431 /* Try against the lanman password. smb_pass->smb_passwd == NULL means
432 no password, allow access. */
434 DEBUG(4,("Checking LM MD4 password\n"));
436 if((smb_pass->smb_passwd == NULL) &&
437 (smb_pass->acct_ctrl & ACB_PWNOTREQ)) {
438 DEBUG(4,("no password required for user %s\n",
439 smb_pass->smb_name));
440 return True;
443 if((smb_pass->smb_passwd != NULL) &&
444 smb_password_check((char *)lm_pass,
445 (uchar *)smb_pass->smb_passwd, challenge)) {
446 DEBUG(4,("LM MD4 password check succeeded\n"));
447 return(True);
450 DEBUG(4,("LM MD4 password check failed\n"));
452 return False;
456 /****************************************************************************
457 check if a username/password is OK assuming the password is a 24 byte
458 SMB hash
459 return True if the password is correct, False otherwise
460 ****************************************************************************/
462 BOOL pass_check_smb(char *user, char *domain,
463 uchar *chal, uchar *lm_pwd, uchar *nt_pwd,
464 struct passwd *pwd)
466 struct passwd *pass;
467 struct smb_passwd *smb_pass;
469 if (!lm_pwd || !nt_pwd)
471 return(False);
474 if (pwd != NULL && user == NULL)
476 pass = (struct passwd *) pwd;
477 user = pass->pw_name;
479 else
481 pass = Get_Pwnam(user,True);
484 if (pass == NULL)
486 DEBUG(3,("Couldn't find user %s\n",user));
487 return(False);
490 smb_pass = getsmbpwnam(user);
492 if (smb_pass == NULL)
494 DEBUG(3,("Couldn't find user %s in smb_passwd file.\n", user));
495 return(False);
498 /* Quit if the account was disabled. */
499 if(smb_pass->acct_ctrl & ACB_DISABLED) {
500 DEBUG(3,("account for user %s was disabled.\n", user));
501 return(False);
504 /* Ensure the uid's match */
505 if (smb_pass->smb_userid != pass->pw_uid)
507 DEBUG(3,("Error : UNIX and SMB uids in password files do not match !\n"));
508 return(False);
511 if (lm_pwd[0] == '\0' && IS_BITS_SET_ALL(smb_pass->acct_ctrl, ACB_PWNOTREQ) && lp_null_passwords())
513 DEBUG(3,("account for user %s has no password and null passwords are allowed.\n", smb_pass->smb_name));
514 return(True);
517 if (smb_password_ok(smb_pass, chal, lm_pwd, nt_pwd))
519 return(True);
522 DEBUG(3,("Error smb_password_check failed\n"));
523 return False;
526 /****************************************************************************
527 check if a username/password pair is OK either via the system password
528 database or the encrypted SMB password database
529 return True if the password is correct, False otherwise
530 ****************************************************************************/
531 BOOL password_ok(char *user, char *password, int pwlen, struct passwd *pwd)
533 if (pwlen == 24 || (lp_encrypted_passwords() && (pwlen == 0) && lp_null_passwords()))
535 /* if 24 bytes long assume it is an encrypted password */
536 uchar challenge[8];
538 if (!last_challenge(challenge))
540 DEBUG(0,("Error: challenge not done for user=%s\n", user));
541 return False;
544 return pass_check_smb(user, global_myworkgroup,
545 challenge, (uchar *)password, (uchar *)password, pwd);
548 return pass_check(user, password, pwlen, pwd,
549 lp_update_encrypted() ?
550 update_smbpassword_file : NULL);
553 /****************************************************************************
554 check if a username is valid
555 ****************************************************************************/
556 BOOL user_ok(char *user,int snum)
558 pstring valid, invalid;
559 BOOL ret;
561 StrnCpy(valid, lp_valid_users(snum), sizeof(pstring));
562 StrnCpy(invalid, lp_invalid_users(snum), sizeof(pstring));
564 string_sub(valid,"%S",lp_servicename(snum));
565 string_sub(invalid,"%S",lp_servicename(snum));
567 ret = !user_in_list(user,invalid);
569 if (ret && valid && *valid) {
570 ret = user_in_list(user,valid);
573 if (ret && lp_onlyuser(snum)) {
574 char *user_list = lp_username(snum);
575 string_sub(user_list,"%S",lp_servicename(snum));
576 ret = user_in_list(user,user_list);
579 return(ret);
585 /****************************************************************************
586 validate a group username entry. Return the username or NULL
587 ****************************************************************************/
588 static char *validate_group(char *group,char *password,int pwlen,int snum)
590 #ifdef HAVE_NETGROUP
592 char *host, *user, *domain;
593 setnetgrent(group);
594 while (getnetgrent(&host, &user, &domain)) {
595 if (user) {
596 if (user_ok(user, snum) &&
597 password_ok(user,password,pwlen,NULL)) {
598 endnetgrent();
599 return(user);
603 endnetgrent();
605 #endif
607 #ifdef HAVE_GETGRNAM
609 struct group *gptr = (struct group *)getgrnam(group);
610 char **member;
611 if (gptr)
613 member = gptr->gr_mem;
614 while (member && *member)
616 static fstring name;
617 fstrcpy(name,*member);
618 if (user_ok(name,snum) &&
619 password_ok(name,password,pwlen,NULL))
620 return(&name[0]);
621 member++;
623 #ifdef GROUP_CHECK_PWENT
625 struct passwd *pwd;
626 static fstring tm;
628 setpwent ();
629 while (pwd = getpwent ()) {
630 if (*(pwd->pw_passwd) && pwd->pw_gid == gptr->gr_gid) {
631 /* This Entry have PASSWORD and same GID then check pwd */
632 if (password_ok(NULL, password, pwlen, pwd)) {
633 fstrcpy(tm, pwd->pw_name);
634 endpwent ();
635 return tm;
639 endpwent ();
641 #endif /* GROUP_CHECK_PWENT */
644 #endif
645 return(NULL);
650 /****************************************************************************
651 check for authority to login to a service with a given username/password
652 ****************************************************************************/
653 BOOL authorise_login(int snum,char *user,char *password, int pwlen,
654 BOOL *guest,BOOL *force,uint16 vuid)
656 BOOL ok = False;
658 *guest = False;
660 #if DEBUG_PASSWORD
661 DEBUG(100,("checking authorisation on user=%s pass=%s\n",user,password));
662 #endif
664 /* there are several possibilities:
665 1) login as the given user with given password
666 2) login as a previously registered username with the given password
667 3) login as a session list username with the given password
668 4) login as a previously validated user/password pair
669 5) login as the "user =" user with given password
670 6) login as the "user =" user with no password (guest connection)
671 7) login as guest user with no password
673 if the service is guest_only then steps 1 to 5 are skipped
676 if (GUEST_ONLY(snum)) *force = True;
678 if (!(GUEST_ONLY(snum) && GUEST_OK(snum)))
681 user_struct *vuser = get_valid_user_struct(vuid);
683 /* check the given username and password */
684 if (!ok && (*user) && user_ok(user,snum)) {
685 ok = password_ok(user,password, pwlen, NULL);
686 if (ok) DEBUG(3,("ACCEPTED: given username password ok\n"));
689 /* check for a previously registered guest username */
690 if (!ok && (vuser != 0) && vuser->guest) {
691 if (user_ok(vuser->name,snum) &&
692 password_ok(vuser->name, password, pwlen, NULL)) {
693 fstrcpy(user, vuser->name);
694 vuser->guest = False;
695 DEBUG(3,("ACCEPTED: given password with registered user %s\n", user));
696 ok = True;
701 /* now check the list of session users */
702 if (!ok)
704 char *auser;
705 char *user_list = strdup(session_users);
706 if (!user_list) return(False);
708 for (auser=strtok(user_list,LIST_SEP);
709 !ok && auser;
710 auser = strtok(NULL,LIST_SEP))
712 fstring user2;
713 fstrcpy(user2,auser);
714 if (!user_ok(user2,snum)) continue;
716 if (password_ok(user2,password, pwlen, NULL)) {
717 ok = True;
718 fstrcpy(user,user2);
719 DEBUG(3,("ACCEPTED: session list username and given password ok\n"));
722 free(user_list);
725 /* check for a previously validated username/password pair */
726 if (!ok && (!lp_revalidate(snum) || lp_security() > SEC_SHARE) &&
727 (vuser != 0) && !vuser->guest &&
728 user_ok(vuser->name,snum)) {
729 fstrcpy(user,vuser->name);
730 *guest = False;
731 DEBUG(3,("ACCEPTED: validated uid ok as non-guest\n"));
732 ok = True;
735 /* check for a rhosts entry */
736 if (!ok && user_ok(user,snum) && check_hosts_equiv(user)) {
737 ok = True;
738 DEBUG(3,("ACCEPTED: hosts equiv or rhosts entry\n"));
741 /* check the user= fields and the given password */
742 if (!ok && lp_username(snum)) {
743 char *auser;
744 pstring user_list;
745 StrnCpy(user_list,lp_username(snum),sizeof(pstring));
747 string_sub(user_list,"%S",lp_servicename(snum));
749 for (auser=strtok(user_list,LIST_SEP);
750 auser && !ok;
751 auser = strtok(NULL,LIST_SEP))
753 if (*auser == '@')
755 auser = validate_group(auser+1,password,pwlen,snum);
756 if (auser)
758 ok = True;
759 fstrcpy(user,auser);
760 DEBUG(3,("ACCEPTED: group username and given password ok\n"));
763 else
765 fstring user2;
766 fstrcpy(user2,auser);
767 if (user_ok(user2,snum) &&
768 password_ok(user2,password,pwlen,NULL))
770 ok = True;
771 fstrcpy(user,user2);
772 DEBUG(3,("ACCEPTED: user list username and given password ok\n"));
777 } /* not guest only */
779 /* check for a normal guest connection */
780 if (!ok && GUEST_OK(snum))
782 fstring guestname;
783 StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
784 if (Get_Pwnam(guestname,True))
786 fstrcpy(user,guestname);
787 ok = True;
788 DEBUG(3,("ACCEPTED: guest account and guest ok\n"));
790 else
791 DEBUG(0,("Invalid guest account %s??\n",guestname));
792 *guest = True;
793 *force = True;
796 if (ok && !user_ok(user,snum))
798 DEBUG(0,("rejected invalid user %s\n",user));
799 ok = False;
802 return(ok);
806 /****************************************************************************
807 read the a hosts.equiv or .rhosts file and check if it
808 allows this user from this machine
809 ****************************************************************************/
810 static BOOL check_user_equiv(char *user, char *remote, char *equiv_file)
812 pstring buf;
813 int plus_allowed = 1;
814 char *file_host;
815 char *file_user;
816 FILE *fp = sys_fopen(equiv_file, "r");
817 DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
818 if (! fp) return False;
819 while(fgets(buf, sizeof(buf), fp))
821 trim_string(buf," "," ");
823 if (buf[0] != '#' && buf[0] != '\n')
825 BOOL is_group = False;
826 int plus = 1;
827 char *bp = buf;
828 if (strcmp(buf, "NO_PLUS\n") == 0)
830 DEBUG(6, ("check_user_equiv NO_PLUS\n"));
831 plus_allowed = 0;
833 else {
834 if (buf[0] == '+')
836 bp++;
837 if (*bp == '\n' && plus_allowed)
839 /* a bare plus means everbody allowed */
840 DEBUG(6, ("check_user_equiv everybody allowed\n"));
841 fclose(fp);
842 return True;
845 else if (buf[0] == '-')
847 bp++;
848 plus = 0;
850 if (*bp == '@')
852 is_group = True;
853 bp++;
855 file_host = strtok(bp, " \t\n");
856 file_user = strtok(NULL, " \t\n");
857 DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)",
858 file_user ? file_user : "(null)" ));
859 if (file_host && *file_host)
861 BOOL host_ok = False;
863 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
864 if (is_group)
866 static char *mydomain = NULL;
867 if (!mydomain)
868 yp_get_default_domain(&mydomain);
869 if (mydomain && innetgr(file_host,remote,user,mydomain))
870 host_ok = True;
872 #else
873 if (is_group)
875 DEBUG(1,("Netgroups not configured\n"));
876 continue;
878 #endif
880 /* is it this host */
881 /* the fact that remote has come from a call of gethostbyaddr
882 * means that it may have the fully qualified domain name
883 * so we could look up the file version to get it into
884 * a canonical form, but I would rather just type it
885 * in full in the equiv file
887 if (!host_ok && !is_group && strequal(remote, file_host))
888 host_ok = True;
890 if (!host_ok)
891 continue;
893 /* is it this user */
894 if (file_user == 0 || strequal(user, file_user))
896 fclose(fp);
897 DEBUG(5, ("check_user_equiv matched %s%s %s\n",
898 (plus ? "+" : "-"), file_host,
899 (file_user ? file_user : "")));
900 return (plus ? True : False);
906 fclose(fp);
907 return False;
911 /****************************************************************************
912 check for a possible hosts equiv or rhosts entry for the user
913 ****************************************************************************/
914 BOOL check_hosts_equiv(char *user)
916 char *fname = NULL;
917 pstring rhostsfile;
918 struct passwd *pass = Get_Pwnam(user,True);
920 if (!pass)
921 return(False);
923 fname = lp_hosts_equiv();
925 /* note: don't allow hosts.equiv on root */
926 if (fname && *fname && (pass->pw_uid != 0)) {
927 extern int Client;
928 if (check_user_equiv(user,client_name(Client),fname))
929 return(True);
932 if (lp_use_rhosts())
934 char *home = get_home_dir(user);
935 if (home) {
936 extern int Client;
937 slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
938 if (check_user_equiv(user,client_name(Client),rhostsfile))
939 return(True);
943 return(False);
947 /****************************************************************************
948 return the client state structure
949 ****************************************************************************/
950 struct cli_state *server_client(void)
952 static struct cli_state pw_cli;
953 return &pw_cli;
956 /****************************************************************************
957 support for server level security
958 ****************************************************************************/
959 struct cli_state *server_cryptkey(void)
961 struct cli_state *cli;
962 fstring desthost;
963 struct in_addr dest_ip;
964 extern fstring local_machine;
965 char *p;
966 BOOL connected_ok = False;
967 struct nmb_name calling, called;
969 cli = server_client();
971 if (!cli_initialise(cli))
972 return NULL;
974 p = lp_passwordserver();
975 while(p && next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
976 standard_sub_basic(desthost);
977 strupper(desthost);
979 if(!resolve_name( desthost, &dest_ip, 0x20)) {
980 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
981 continue;
984 if (ismyip(dest_ip)) {
985 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
986 continue;
989 if (cli_connect(cli, desthost, &dest_ip)) {
990 DEBUG(3,("connected to password server %s\n",desthost));
991 connected_ok = True;
992 break;
996 if (!connected_ok) {
997 DEBUG(0,("password server not available\n"));
998 cli_shutdown(cli);
999 return NULL;
1002 make_nmb_name(&calling, local_machine, 0x0 , scope);
1003 make_nmb_name(&called , desthost , 0x20, scope);
1005 if (!cli_session_request(cli, &calling, &called)) {
1006 /* try with *SMBSERVER if the first name fails */
1007 cli_shutdown(cli);
1008 make_nmb_name(&called , "*SMBSERVER", 0x20, scope);
1009 if (!cli_initialise(cli) ||
1010 !cli_connect(cli, desthost, &dest_ip) ||
1011 !cli_session_request(cli, &calling, &called)) {
1012 DEBUG(1,("%s rejected the session\n",desthost));
1013 cli_shutdown(cli);
1014 return NULL;
1018 DEBUG(3,("got session\n"));
1020 if (!cli_negprot(cli)) {
1021 DEBUG(1,("%s rejected the negprot\n",desthost));
1022 cli_shutdown(cli);
1023 return NULL;
1026 if (cli->protocol < PROTOCOL_LANMAN2 ||
1027 !(cli->sec_mode & 1)) {
1028 DEBUG(1,("%s isn't in user level security mode\n",desthost));
1029 cli_shutdown(cli);
1030 return NULL;
1033 DEBUG(3,("password server OK\n"));
1035 return cli;
1038 /****************************************************************************
1039 validate a password with the password server
1040 ****************************************************************************/
1041 BOOL server_validate(char *user, char *domain,
1042 char *pass, int passlen,
1043 char *ntpass, int ntpasslen)
1045 struct cli_state *cli;
1046 static unsigned char badpass[24];
1047 static BOOL tested_password_server = False;
1048 static BOOL bad_password_server = False;
1050 cli = server_client();
1052 if (!cli->initialised) {
1053 DEBUG(1,("password server %s is not connected\n", cli->desthost));
1054 return(False);
1057 if(badpass[0] == 0)
1058 memset(badpass, 0x1f, sizeof(badpass));
1060 if((passlen == sizeof(badpass)) && !memcmp(badpass, pass, passlen)) {
1062 * Very unlikely, our random bad password is the same as the users
1063 * password. */
1064 memset(badpass, badpass[0]+1, sizeof(badpass));
1068 * Attempt a session setup with a totally incorrect password.
1069 * If this succeeds with the guest bit *NOT* set then the password
1070 * server is broken and is not correctly setting the guest bit. We
1071 * need to detect this as some versions of NT4.x are broken. JRA.
1074 if(!tested_password_server) {
1075 if (cli_session_setup(cli, user, (char *)badpass, sizeof(badpass),
1076 (char *)badpass, sizeof(badpass), domain)) {
1079 * We connected to the password server so we
1080 * can say we've tested it.
1082 tested_password_server = True;
1084 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
1085 DEBUG(0,("server_validate: password server %s allows users as non-guest \
1086 with a bad password.\n", cli->desthost));
1087 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
1088 use this machine as the password server.\n"));
1089 cli_ulogoff(cli);
1092 * Password server has the bug.
1094 bad_password_server = True;
1095 return False;
1097 cli_ulogoff(cli);
1099 } else {
1102 * We have already tested the password server.
1103 * Fail immediately if it has the bug.
1106 if(bad_password_server) {
1107 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
1108 with a bad password.\n", cli->desthost));
1109 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
1110 use this machine as the password server.\n"));
1111 return False;
1116 * Now we know the password server will correctly set the guest bit, or is
1117 * not guest enabled, we can try with the real password.
1120 if (!cli_session_setup(cli, user, pass, passlen, ntpass, ntpasslen, domain)) {
1121 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
1122 return False;
1125 /* if logged in as guest then reject */
1126 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
1127 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
1128 cli_ulogoff(cli);
1129 return(False);
1133 cli_ulogoff(cli);
1135 return(True);
1138 /***********************************************************************
1139 Do the same as security=server, but using NT Domain calls and a session
1140 key from the machine password.
1141 ************************************************************************/
1143 BOOL domain_client_validate( char *user, char *domain,
1144 char *smb_apasswd, int smb_apasslen,
1145 char *smb_ntpasswd, int smb_ntpasslen,
1146 BOOL *user_exists)
1148 unsigned char local_challenge[8];
1149 unsigned char local_lm_response[24];
1150 unsigned char local_nt_reponse[24];
1151 unsigned char trust_passwd[16];
1152 fstring remote_machine;
1153 char *p;
1154 struct in_addr dest_ip;
1155 NET_ID_INFO_CTR ctr;
1156 NET_USER_INFO_3 info3;
1157 struct cli_state cli;
1158 uint32 smb_uid_low;
1159 BOOL connected_ok = False;
1160 struct nmb_name calling, called;
1162 if(user_exists != NULL)
1163 *user_exists = True; /* Only set false on a very specific error. */
1166 * Check that the requested domain is not our own machine name.
1167 * If it is, we should never check the PDC here, we use our own local
1168 * password file.
1171 if(strequal( domain, global_myname)) {
1172 DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
1173 return False;
1177 * Next, check that the passwords given were encrypted.
1180 if(((smb_apasslen != 24) && (smb_apasslen != 0)) ||
1181 ((smb_ntpasslen != 24) && (smb_ntpasslen != 0))) {
1184 * Not encrypted - do so.
1187 DEBUG(3,("domain_client_validate: User passwords not in encrypted format.\n"));
1188 generate_random_buffer( local_challenge, 8, False);
1189 SMBencrypt( (uchar *)smb_apasswd, local_challenge, local_lm_response);
1190 SMBNTencrypt((uchar *)smb_ntpasswd, local_challenge, local_nt_reponse);
1191 smb_apasslen = 24;
1192 smb_ntpasslen = 24;
1193 smb_apasswd = (char *)local_lm_response;
1194 smb_ntpasswd = (char *)local_nt_reponse;
1195 } else {
1198 * Encrypted - get the challenge we sent for these
1199 * responses.
1202 if (!last_challenge(local_challenge)) {
1203 DEBUG(0,("domain_client_validate: no challenge done - password failed\n"));
1204 return False;
1209 * Get the machine account password.
1211 if (!trust_get_passwd( trust_passwd, global_myworkgroup, global_myname))
1213 return False;
1217 * At this point, smb_apasswd points to the lanman response to
1218 * the challenge in local_challenge, and smb_ntpasswd points to
1219 * the NT response to the challenge in local_challenge. Ship
1220 * these over the secure channel to a domain controller and
1221 * see if they were valid.
1224 ZERO_STRUCT(cli);
1226 if(cli_initialise(&cli) == False) {
1227 DEBUG(0,("domain_client_validate: unable to initialize client connection.\n"));
1228 return False;
1232 * Treat each name in the 'password server =' line as a potential
1233 * PDC/BDC. Contact each in turn and try and authenticate.
1236 p = lp_passwordserver();
1237 while(p && next_token(&p,remote_machine,LIST_SEP,sizeof(remote_machine))) {
1239 standard_sub_basic(remote_machine);
1240 strupper(remote_machine);
1242 if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
1243 DEBUG(1,("domain_client_validate: Can't resolve address for %s\n", remote_machine));
1244 continue;
1247 if (ismyip(dest_ip)) {
1248 DEBUG(1,("domain_client_validate: Password server loop - not using password server %s\n",remote_machine));
1249 continue;
1252 if (!cli_connect(&cli, remote_machine, &dest_ip)) {
1253 DEBUG(0,("domain_client_validate: unable to connect to SMB server on \
1254 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1255 continue;
1258 make_nmb_name(&calling, global_myname , 0x0 , scope);
1259 make_nmb_name(&called , remote_machine, 0x20, scope);
1261 if (!cli_session_request(&cli, &calling, &called))
1263 DEBUG(0,("domain_client_validate: machine %s rejected the session setup. \
1264 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1265 cli_shutdown(&cli);
1266 continue;
1269 cli.protocol = PROTOCOL_NT1;
1271 if (!cli_negprot(&cli)) {
1272 DEBUG(0,("domain_client_validate: machine %s rejected the negotiate protocol. \
1273 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1274 cli_shutdown(&cli);
1275 continue;
1278 if (cli.protocol != PROTOCOL_NT1) {
1279 DEBUG(0,("domain_client_validate: machine %s didn't negotiate NT protocol.\n",
1280 remote_machine));
1281 cli_shutdown(&cli);
1282 continue;
1286 * Do an anonymous session setup.
1289 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
1290 DEBUG(0,("domain_client_validate: machine %s rejected the session setup. \
1291 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1292 cli_shutdown(&cli);
1293 continue;
1296 if (!(cli.sec_mode & 1)) {
1297 DEBUG(1,("domain_client_validate: machine %s isn't in user level security mode\n",
1298 remote_machine));
1299 cli_shutdown(&cli);
1300 continue;
1303 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
1304 DEBUG(0,("domain_client_validate: machine %s rejected the tconX on the IPC$ share. \
1305 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1306 cli_shutdown(&cli);
1307 continue;
1311 * We have an anonymous connection to IPC$.
1313 connected_ok = True;
1314 break;
1317 if (!connected_ok) {
1318 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
1319 cli_shutdown(&cli);
1320 return False;
1324 * Ok - we have an anonymous connection to the IPC$ share.
1325 * Now start the NT Domain stuff :-).
1328 if(cli_nt_session_open(&cli, PIPE_NETLOGON) == False) {
1329 DEBUG(0,("domain_client_validate: unable to open the domain client session to \
1330 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
1331 cli_nt_session_close(&cli);
1332 cli_ulogoff(&cli);
1333 cli_shutdown(&cli);
1334 return False;
1337 if(cli_nt_setup_creds(&cli, trust_passwd) == False) {
1338 DEBUG(0,("domain_client_validate: unable to setup the PDC credentials to machine \
1339 %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
1340 cli_nt_session_close(&cli);
1341 cli_ulogoff(&cli);
1342 cli_shutdown(&cli);
1343 return False;
1346 /* We really don't care what LUID we give the user. */
1347 generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
1349 if(cli_nt_login_network(&cli, domain, user, smb_uid_low, (char *)local_challenge,
1350 ((smb_apasslen != 0) ? smb_apasswd : NULL),
1351 ((smb_ntpasslen != 0) ? smb_ntpasswd : NULL),
1352 &ctr, &info3) == False) {
1353 uint32 nt_rpc_err;
1355 cli_error(&cli, NULL, NULL, &nt_rpc_err);
1356 DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
1357 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1358 cli_nt_session_close(&cli);
1359 cli_ulogoff(&cli);
1360 cli_shutdown(&cli);
1362 if((nt_rpc_err == NT_STATUS_NO_SUCH_USER) && (user_exists != NULL))
1363 *user_exists = False;
1365 return False;
1369 * Here, if we really want it, we have lots of info about the user in info3.
1372 #if 0
1374 * We don't actually need to do this - plus it fails currently with
1375 * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
1376 * send here. JRA.
1379 if(cli_nt_logoff(&cli, &ctr) == False) {
1380 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
1381 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1382 cli_nt_session_close(&cli);
1383 cli_ulogoff(&cli);
1384 cli_shutdown(&cli);
1385 return False;
1387 #endif /* 0 */
1389 cli_nt_session_close(&cli);
1390 cli_ulogoff(&cli);
1391 cli_shutdown(&cli);
1392 return True;