bounds check next_token() to prevent possible buffer overflows
[Samba/gbeck.git] / source / smbd / password.c
blobdadbcad11e026e4231ee86d9b97ed7a7d1116fef
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 BOOL global_machine_pasword_needs_changing;
29 /* users from session setup */
30 static pstring session_users="";
32 extern pstring global_myname;
33 extern fstring global_myworkgroup;
35 /* Data to do lanman1/2 password challenge. */
36 static unsigned char saved_challenge[8];
37 static BOOL challenge_sent=False;
39 /*******************************************************************
40 Get the next challenge value - no repeats.
41 ********************************************************************/
42 void generate_next_challenge(char *challenge)
44 #if 0
45 /*
46 * Leave this ifdef'd out while we test
47 * the new crypto random number generator.
48 * JRA.
50 unsigned char buf[16];
51 static int counter = 0;
52 struct timeval tval;
53 int v1,v2;
55 /* get a sort-of random number */
56 GetTimeOfDay(&tval);
57 v1 = (counter++) + getpid() + tval.tv_sec;
58 v2 = (counter++) * getpid() + tval.tv_usec;
59 SIVAL(challenge,0,v1);
60 SIVAL(challenge,4,v2);
62 /* mash it up with md4 */
63 mdfour(buf, (unsigned char *)challenge, 8);
64 #else
65 unsigned char buf[8];
67 generate_random_buffer(buf,8,False);
68 #endif
69 memcpy(saved_challenge, buf, 8);
70 memcpy(challenge,buf,8);
71 challenge_sent = True;
74 /*******************************************************************
75 set the last challenge sent, usually from a password server
76 ********************************************************************/
77 BOOL set_challenge(char *challenge)
79 memcpy(saved_challenge,challenge,8);
80 challenge_sent = True;
81 return(True);
84 /*******************************************************************
85 get the last challenge sent
86 ********************************************************************/
87 BOOL last_challenge(unsigned char *challenge)
89 if (!challenge_sent) return(False);
90 memcpy(challenge,saved_challenge,8);
91 return(True);
94 /* this holds info on user ids that are already validated for this VC */
95 static user_struct *validated_users = NULL;
96 static int num_validated_users = 0;
98 /****************************************************************************
99 check if a uid has been validated, and return an pointer to the user_struct
100 if it has. NULL if not. vuid is biased by an offset. This allows us to
101 tell random client vuid's (normally zero) from valid vuids.
102 ****************************************************************************/
103 user_struct *get_valid_user_struct(uint16 vuid)
105 if (vuid == UID_FIELD_INVALID)
106 return NULL;
107 vuid -= VUID_OFFSET;
108 if ((vuid >= (uint16)num_validated_users) ||
109 (validated_users[vuid].uid == -1) || (validated_users[vuid].gid == -1))
110 return NULL;
111 return &validated_users[vuid];
114 /****************************************************************************
115 invalidate a uid
116 ****************************************************************************/
117 void invalidate_vuid(uint16 vuid)
119 user_struct *vuser = get_valid_user_struct(vuid);
121 if (vuser == NULL) return;
123 vuser->uid = -1;
124 vuser->gid = -1;
126 vuser->n_sids = 0;
128 /* same number of igroups as groups */
129 vuser->n_groups = 0;
131 if (vuser->groups) free(vuser->groups);
133 if (vuser->sids) free(vuser->sids);
135 vuser->sids = NULL;
136 vuser->groups = NULL;
140 /****************************************************************************
141 return a validated username
142 ****************************************************************************/
143 char *validated_username(uint16 vuid)
145 user_struct *vuser = get_valid_user_struct(vuid);
146 if (vuser == NULL)
147 return 0;
148 return(vuser->name);
152 /****************************************************************************
153 Setup the groups a user belongs to.
154 ****************************************************************************/
155 int setup_groups(char *user, int uid, int gid, int *p_ngroups, GID_T **p_groups)
157 int i,ngroups;
158 GID_T *groups;
159 GID_T grp = 0;
161 if (-1 == initgroups(user,gid)) {
162 if (getuid() == 0) {
163 DEBUG(0,("Unable to initgroups!\n"));
164 if (gid < 0 || gid > 16000 || uid < 0 || uid > 16000) {
165 DEBUG(0,("This is probably a problem with the account %s\n",
166 user));
169 return -1;
172 ngroups = getgroups(0,&grp);
173 if (ngroups <= 0) ngroups = 32;
175 groups = (GID_T *)malloc(sizeof(groups[0])*ngroups);
177 ngroups = getgroups(ngroups,(gid_t *)groups);
179 (*p_ngroups) = ngroups;
181 (*p_groups) = groups;
183 DEBUG( 3, ( "%s is in %d groups: ", user, ngroups ) );
184 for (i = 0; i < ngroups; i++ ) {
185 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
187 DEBUG( 3, ( "\n" ) );
189 return 0;
193 /****************************************************************************
194 register a uid/name pair as being valid and that a valid password
195 has been given. vuid is biased by an offset. This allows us to
196 tell random client vuid's (normally zero) from valid vuids.
197 ****************************************************************************/
198 uint16 register_vuid(int uid,int gid, char *unix_name, char *requested_name, BOOL guest)
200 user_struct *vuser;
201 struct passwd *pwfile; /* for getting real name from passwd file */
203 /* Ensure no vuid gets registered in share level security. */
204 if(lp_security() == SEC_SHARE)
205 return UID_FIELD_INVALID;
207 #if 0
209 * After observing MS-Exchange services writing to a Samba share
210 * I belive this code is incorrect. Each service does its own
211 * sessionsetup_and_X for the same user, and as each service shuts
212 * down, it does a user_logoff_and_X. As we are consolidating multiple
213 * sessionsetup_and_X's onto the same vuid here, when the first service
214 * shuts down, it invalidates all the open files for the other services.
215 * Hence I am removing this code and forcing each sessionsetup_and_X
216 * to get a new vuid.
217 * Jeremy Allison. (jallison@whistle.com).
220 int i;
221 for(i = 0; i < num_validated_users; i++) {
222 vuser = &validated_users[i];
223 if ( vuser->uid == uid )
224 return (uint16)(i + VUID_OFFSET); /* User already validated */
226 #endif
228 validated_users = (user_struct *)Realloc(validated_users,
229 sizeof(user_struct)*
230 (num_validated_users+1));
232 if (!validated_users)
234 DEBUG(0,("Failed to realloc users struct!\n"));
235 num_validated_users = 0;
236 return UID_FIELD_INVALID;
239 vuser = &validated_users[num_validated_users];
240 num_validated_users++;
242 vuser->uid = uid;
243 vuser->gid = gid;
244 vuser->guest = guest;
245 fstrcpy(vuser->name,unix_name);
246 fstrcpy(vuser->requested_name,requested_name);
248 vuser->n_sids = 0;
249 vuser->sids = NULL;
251 vuser->n_groups = 0;
252 vuser->groups = NULL;
254 /* Find all the groups this uid is in and store them.
255 Used by become_user() */
256 setup_groups(unix_name,uid,gid,
257 &vuser->n_groups,
258 &vuser->groups);
260 DEBUG(3,("uid %d registered to name %s\n",uid,unix_name));
262 DEBUG(3, ("Clearing default real name\n"));
263 fstrcpy(vuser->real_name, "<Full Name>\0");
264 if (lp_unix_realname()) {
265 if ((pwfile=getpwnam(vuser->name))!= NULL)
267 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->name,pwfile->pw_gecos));
268 fstrcpy(vuser->real_name, pwfile->pw_gecos);
272 return (uint16)((num_validated_users - 1) + VUID_OFFSET);
276 /****************************************************************************
277 add a name to the session users list
278 ****************************************************************************/
279 void add_session_user(char *user)
281 fstring suser;
282 StrnCpy(suser,user,sizeof(suser)-1);
284 if (!Get_Pwnam(suser,True)) return;
286 if (suser && *suser && !in_list(suser,session_users,False))
288 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
289 DEBUG(1,("Too many session users??\n"));
290 else
292 pstrcat(session_users," ");
293 pstrcat(session_users,suser);
299 /****************************************************************************
300 update the encrypted smbpasswd file from the plaintext username and password
301 *****************************************************************************/
302 BOOL update_smbpassword_file(char *user, char *password)
304 struct smb_passwd *smbpw;
305 BOOL ret;
307 become_root(0);
308 smbpw = getsmbpwnam(user);
309 unbecome_root(0);
311 if(smbpw == NULL) {
312 DEBUG(0,("getsmbpwnam returned NULL\n"));
313 return False;
316 /* Here, the flag is one, because we want to ignore the
317 XXXXXXX'd out password */
318 ret = change_oem_password( smbpw, password, True);
319 if (ret == False) {
320 DEBUG(3,("change_oem_password returned False\n"));
323 return ret;
330 /****************************************************************************
331 core of smb password checking routine.
332 ****************************************************************************/
333 BOOL smb_password_check(char *password, unsigned char *part_passwd, unsigned char *c8)
335 /* Finish the encryption of part_passwd. */
336 unsigned char p21[21];
337 unsigned char p24[24];
339 if (part_passwd == NULL)
340 DEBUG(10,("No password set - allowing access\n"));
341 /* No password set - always true ! */
342 if (part_passwd == NULL)
343 return 1;
345 memset(p21,'\0',21);
346 memcpy(p21,part_passwd,16);
347 E_P24(p21, c8, p24);
348 #if DEBUG_PASSWORD
350 int i;
351 DEBUG(100,("Part password (P16) was |"));
352 for(i = 0; i < 16; i++)
353 DEBUG(100,("%X ", (unsigned char)part_passwd[i]));
354 DEBUG(100,("|\n"));
355 DEBUG(100,("Password from client was |"));
356 for(i = 0; i < 24; i++)
357 DEBUG(100,("%X ", (unsigned char)password[i]));
358 DEBUG(100,("|\n"));
359 DEBUG(100,("Given challenge was |"));
360 for(i = 0; i < 8; i++)
361 DEBUG(100,("%X ", (unsigned char)c8[i]));
362 DEBUG(100,("|\n"));
363 DEBUG(100,("Value from encryption was |"));
364 for(i = 0; i < 24; i++)
365 DEBUG(100,("%X ", (unsigned char)p24[i]));
366 DEBUG(100,("|\n"));
368 #endif
369 return (memcmp(p24, password, 24) == 0);
372 /****************************************************************************
373 Do a specific test for an smb password being correct, given a smb_password and
374 the lanman and NT responses.
375 ****************************************************************************/
376 BOOL smb_password_ok(struct smb_passwd *smb_pass,
377 uchar lm_pass[24], uchar nt_pass[24])
379 uchar challenge[8];
381 if (!lm_pass || !smb_pass) return(False);
383 if(smb_pass->acct_ctrl & ACB_DISABLED) {
384 DEBUG(3,("account for user %s was disabled.\n",
385 smb_pass->smb_name));
386 return(False);
389 if (!last_challenge(challenge)) {
390 DEBUG(1,("no challenge done - password failed\n"));
391 return False;
394 DEBUG(4,("Checking SMB password for user %s\n",
395 smb_pass->smb_name));
397 if ((Protocol >= PROTOCOL_NT1) && (smb_pass->smb_nt_passwd != NULL)) {
398 /* We have the NT MD4 hash challenge available - see if we can
399 use it (ie. does it exist in the smbpasswd file).
401 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
402 if (smb_password_check((char *)nt_pass,
403 (uchar *)smb_pass->smb_nt_passwd,
404 challenge)) {
405 DEBUG(4,("NT MD4 password check succeeded\n"));
406 return(True);
408 DEBUG(4,("NT MD4 password check failed\n"));
411 /* Try against the lanman password. smb_pass->smb_passwd == NULL means
412 no password, allow access. */
414 DEBUG(4,("Checking LM MD4 password\n"));
416 if((smb_pass->smb_passwd == NULL) &&
417 (smb_pass->acct_ctrl & ACB_PWNOTREQ)) {
418 DEBUG(4,("no password required for user %s\n",
419 smb_pass->smb_name));
420 return True;
423 if((smb_pass->smb_passwd != NULL) &&
424 smb_password_check((char *)lm_pass,
425 (uchar *)smb_pass->smb_passwd, challenge)) {
426 DEBUG(4,("LM MD4 password check succeeded\n"));
427 return(True);
430 DEBUG(4,("LM MD4 password check failed\n"));
432 return False;
436 /****************************************************************************
437 check if a username/password is OK assuming the password is a 24 byte
438 SMB hash
439 return True if the password is correct, False otherwise
440 ****************************************************************************/
441 static BOOL pass_check_smb(char *user,char *password, struct passwd *pwd)
443 struct passwd *pass;
444 uchar challenge[8];
445 struct smb_passwd *smb_pass;
446 BOOL challenge_done;
448 if (!password) {
449 return(False);
452 challenge_done = last_challenge(challenge);
454 if (!challenge_done) {
455 DEBUG(0,("Error: challenge not done for user=%s\n", user));
456 return False;
459 if (pwd && !user) {
460 pass = (struct passwd *) pwd;
461 user = pass->pw_name;
462 } else {
463 pass = Get_Pwnam(user,True);
466 if (!pass) {
467 DEBUG(3,("Couldn't find user %s\n",user));
468 return(False);
471 smb_pass = getsmbpwnam(user);
473 if (!smb_pass) {
474 DEBUG(3,("Couldn't find user %s in smb_passwd file.\n", user));
475 return(False);
478 /* Quit if the account was disabled. */
479 if(smb_pass->acct_ctrl & ACB_DISABLED) {
480 DEBUG(3,("account for user %s was disabled.\n", user));
481 return(False);
484 /* Ensure the uid's match */
485 if (smb_pass->smb_userid != pass->pw_uid) {
486 DEBUG(3,("Error : UNIX and SMB uids in password files do not match !\n"));
487 return(False);
490 if (smb_password_ok(smb_pass,
491 (unsigned char *)password,
492 (uchar *)password)) {
493 return(True);
496 DEBUG(3,("Error smb_password_check failed\n"));
497 return False;
500 /****************************************************************************
501 check if a username/password pair is OK either via the system password
502 database or the encrypted SMB password database
503 return True if the password is correct, False otherwise
504 ****************************************************************************/
505 BOOL password_ok(char *user,char *password, int pwlen, struct passwd *pwd)
507 if (pwlen == 24) {
508 /* if it is 24 bytes long then assume it is an encrypted
509 password */
510 return pass_check_smb(user, password, pwd);
513 return pass_check(user, password, pwlen, pwd,
514 lp_update_encrypted() ?
515 update_smbpassword_file : NULL);
518 /****************************************************************************
519 check if a username is valid
520 ****************************************************************************/
521 BOOL user_ok(char *user,int snum)
523 pstring valid, invalid;
524 BOOL ret;
526 StrnCpy(valid, lp_valid_users(snum), sizeof(pstring));
527 StrnCpy(invalid, lp_invalid_users(snum), sizeof(pstring));
529 string_sub(valid,"%S",lp_servicename(snum));
530 string_sub(invalid,"%S",lp_servicename(snum));
532 ret = !user_in_list(user,invalid);
534 if (ret && valid && *valid) {
535 ret = user_in_list(user,valid);
538 if (ret && lp_onlyuser(snum)) {
539 char *user_list = lp_username(snum);
540 string_sub(user_list,"%S",lp_servicename(snum));
541 ret = user_in_list(user,user_list);
544 return(ret);
550 /****************************************************************************
551 validate a group username entry. Return the username or NULL
552 ****************************************************************************/
553 static char *validate_group(char *group,char *password,int pwlen,int snum)
555 #ifdef HAVE_NETGROUP
557 char *host, *user, *domain;
558 setnetgrent(group);
559 while (getnetgrent(&host, &user, &domain)) {
560 if (user) {
561 if (user_ok(user, snum) &&
562 password_ok(user,password,pwlen,NULL)) {
563 endnetgrent();
564 return(user);
568 endnetgrent();
570 #endif
572 #ifdef HAVE_GETGRNAM
574 struct group *gptr = (struct group *)getgrnam(group);
575 char **member;
576 if (gptr)
578 member = gptr->gr_mem;
579 while (member && *member)
581 static fstring name;
582 fstrcpy(name,*member);
583 if (user_ok(name,snum) &&
584 password_ok(name,password,pwlen,NULL))
585 return(&name[0]);
586 member++;
588 #ifdef GROUP_CHECK_PWENT
590 struct passwd *pwd;
591 static fstring tm;
593 setpwent ();
594 while (pwd = getpwent ()) {
595 if (*(pwd->pw_passwd) && pwd->pw_gid == gptr->gr_gid) {
596 /* This Entry have PASSWORD and same GID then check pwd */
597 if (password_ok(NULL, password, pwlen, pwd)) {
598 fstrcpy(tm, pwd->pw_name);
599 endpwent ();
600 return tm;
604 endpwent ();
606 #endif /* GROUP_CHECK_PWENT */
609 #endif
610 return(NULL);
615 /****************************************************************************
616 check for authority to login to a service with a given username/password
617 ****************************************************************************/
618 BOOL authorise_login(int snum,char *user,char *password, int pwlen,
619 BOOL *guest,BOOL *force,uint16 vuid)
621 BOOL ok = False;
623 *guest = False;
625 #if DEBUG_PASSWORD
626 DEBUG(100,("checking authorisation on user=%s pass=%s\n",user,password));
627 #endif
629 /* there are several possibilities:
630 1) login as the given user with given password
631 2) login as a previously registered username with the given password
632 3) login as a session list username with the given password
633 4) login as a previously validated user/password pair
634 5) login as the "user =" user with given password
635 6) login as the "user =" user with no password (guest connection)
636 7) login as guest user with no password
638 if the service is guest_only then steps 1 to 5 are skipped
641 if (GUEST_ONLY(snum)) *force = True;
643 if (!(GUEST_ONLY(snum) && GUEST_OK(snum)))
646 user_struct *vuser = get_valid_user_struct(vuid);
648 /* check the given username and password */
649 if (!ok && (*user) && user_ok(user,snum)) {
650 ok = password_ok(user,password, pwlen, NULL);
651 if (ok) DEBUG(3,("ACCEPTED: given username password ok\n"));
654 /* check for a previously registered guest username */
655 if (!ok && (vuser != 0) && vuser->guest) {
656 if (user_ok(vuser->name,snum) &&
657 password_ok(vuser->name, password, pwlen, NULL)) {
658 fstrcpy(user, vuser->name);
659 vuser->guest = False;
660 DEBUG(3,("ACCEPTED: given password with registered user %s\n", user));
661 ok = True;
666 /* now check the list of session users */
667 if (!ok)
669 char *auser;
670 char *user_list = strdup(session_users);
671 if (!user_list) return(False);
673 for (auser=strtok(user_list,LIST_SEP);
674 !ok && auser;
675 auser = strtok(NULL,LIST_SEP))
677 fstring user2;
678 fstrcpy(user2,auser);
679 if (!user_ok(user2,snum)) continue;
681 if (password_ok(user2,password, pwlen, NULL)) {
682 ok = True;
683 fstrcpy(user,user2);
684 DEBUG(3,("ACCEPTED: session list username and given password ok\n"));
687 free(user_list);
690 /* check for a previously validated username/password pair */
691 if (!ok && (!lp_revalidate(snum) || lp_security() > SEC_SHARE) &&
692 (vuser != 0) && !vuser->guest &&
693 user_ok(vuser->name,snum)) {
694 fstrcpy(user,vuser->name);
695 *guest = False;
696 DEBUG(3,("ACCEPTED: validated uid ok as non-guest\n"));
697 ok = True;
700 /* check for a rhosts entry */
701 if (!ok && user_ok(user,snum) && check_hosts_equiv(user)) {
702 ok = True;
703 DEBUG(3,("ACCEPTED: hosts equiv or rhosts entry\n"));
706 /* check the user= fields and the given password */
707 if (!ok && lp_username(snum)) {
708 char *auser;
709 pstring user_list;
710 StrnCpy(user_list,lp_username(snum),sizeof(pstring));
712 string_sub(user_list,"%S",lp_servicename(snum));
714 for (auser=strtok(user_list,LIST_SEP);
715 auser && !ok;
716 auser = strtok(NULL,LIST_SEP))
718 if (*auser == '@')
720 auser = validate_group(auser+1,password,pwlen,snum);
721 if (auser)
723 ok = True;
724 fstrcpy(user,auser);
725 DEBUG(3,("ACCEPTED: group username and given password ok\n"));
728 else
730 fstring user2;
731 fstrcpy(user2,auser);
732 if (user_ok(user2,snum) &&
733 password_ok(user2,password,pwlen,NULL))
735 ok = True;
736 fstrcpy(user,user2);
737 DEBUG(3,("ACCEPTED: user list username and given password ok\n"));
742 } /* not guest only */
744 /* check for a normal guest connection */
745 if (!ok && GUEST_OK(snum))
747 fstring guestname;
748 StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
749 if (Get_Pwnam(guestname,True))
751 fstrcpy(user,guestname);
752 ok = True;
753 DEBUG(3,("ACCEPTED: guest account and guest ok\n"));
755 else
756 DEBUG(0,("Invalid guest account %s??\n",guestname));
757 *guest = True;
758 *force = True;
761 if (ok && !user_ok(user,snum))
763 DEBUG(0,("rejected invalid user %s\n",user));
764 ok = False;
767 return(ok);
771 /****************************************************************************
772 read the a hosts.equiv or .rhosts file and check if it
773 allows this user from this machine
774 ****************************************************************************/
775 static BOOL check_user_equiv(char *user, char *remote, char *equiv_file)
777 pstring buf;
778 int plus_allowed = 1;
779 char *file_host;
780 char *file_user;
781 FILE *fp = fopen(equiv_file, "r");
782 DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
783 if (! fp) return False;
784 while(fgets(buf, sizeof(buf), fp))
786 trim_string(buf," "," ");
788 if (buf[0] != '#' && buf[0] != '\n')
790 BOOL is_group = False;
791 int plus = 1;
792 char *bp = buf;
793 if (strcmp(buf, "NO_PLUS\n") == 0)
795 DEBUG(6, ("check_user_equiv NO_PLUS\n"));
796 plus_allowed = 0;
798 else {
799 if (buf[0] == '+')
801 bp++;
802 if (*bp == '\n' && plus_allowed)
804 /* a bare plus means everbody allowed */
805 DEBUG(6, ("check_user_equiv everybody allowed\n"));
806 fclose(fp);
807 return True;
810 else if (buf[0] == '-')
812 bp++;
813 plus = 0;
815 if (*bp == '@')
817 is_group = True;
818 bp++;
820 file_host = strtok(bp, " \t\n");
821 file_user = strtok(NULL, " \t\n");
822 DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)",
823 file_user ? file_user : "(null)" ));
824 if (file_host && *file_host)
826 BOOL host_ok = False;
828 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
829 if (is_group)
831 static char *mydomain = NULL;
832 if (!mydomain)
833 yp_get_default_domain(&mydomain);
834 if (mydomain && innetgr(file_host,remote,user,mydomain))
835 host_ok = True;
837 #else
838 if (is_group)
840 DEBUG(1,("Netgroups not configured\n"));
841 continue;
843 #endif
845 /* is it this host */
846 /* the fact that remote has come from a call of gethostbyaddr
847 * means that it may have the fully qualified domain name
848 * so we could look up the file version to get it into
849 * a canonical form, but I would rather just type it
850 * in full in the equiv file
852 if (!host_ok && !is_group && strequal(remote, file_host))
853 host_ok = True;
855 if (!host_ok)
856 continue;
858 /* is it this user */
859 if (file_user == 0 || strequal(user, file_user))
861 fclose(fp);
862 DEBUG(5, ("check_user_equiv matched %s%s %s\n",
863 (plus ? "+" : "-"), file_host,
864 (file_user ? file_user : "")));
865 return (plus ? True : False);
871 fclose(fp);
872 return False;
876 /****************************************************************************
877 check for a possible hosts equiv or rhosts entry for the user
878 ****************************************************************************/
879 BOOL check_hosts_equiv(char *user)
881 char *fname = NULL;
882 pstring rhostsfile;
883 struct passwd *pass = Get_Pwnam(user,True);
885 if (!pass)
886 return(False);
888 fname = lp_hosts_equiv();
890 /* note: don't allow hosts.equiv on root */
891 if (fname && *fname && (pass->pw_uid != 0)) {
892 extern int Client;
893 if (check_user_equiv(user,client_name(Client),fname))
894 return(True);
897 if (lp_use_rhosts())
899 char *home = get_home_dir(user);
900 if (home) {
901 extern int Client;
902 slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
903 if (check_user_equiv(user,client_name(Client),rhostsfile))
904 return(True);
908 return(False);
912 static struct cli_state pw_cli;
914 /****************************************************************************
915 return the client state structure
916 ****************************************************************************/
917 struct cli_state *server_client(void)
919 return &pw_cli;
922 /****************************************************************************
923 support for server level security
924 ****************************************************************************/
925 struct cli_state *server_cryptkey(void)
927 fstring desthost;
928 struct in_addr dest_ip;
929 extern fstring local_machine;
930 char *p;
931 BOOL connected_ok = False;
933 if (!cli_initialise(&pw_cli))
934 return NULL;
936 p = lp_passwordserver();
937 while(p && next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
938 standard_sub_basic(desthost);
939 strupper(desthost);
941 if(!resolve_name( desthost, &dest_ip)) {
942 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
943 continue;
946 if (ismyip(dest_ip)) {
947 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
948 continue;
951 if (cli_connect(&pw_cli, desthost, &dest_ip)) {
952 DEBUG(3,("connected to password server %s\n",desthost));
953 connected_ok = True;
954 break;
958 if (!connected_ok) {
959 DEBUG(0,("password server not available\n"));
960 cli_shutdown(&pw_cli);
961 return NULL;
964 if (!cli_session_request(&pw_cli, desthost, 0x20, local_machine)) {
965 DEBUG(1,("%s rejected the session\n",desthost));
966 cli_shutdown(&pw_cli);
967 return NULL;
970 DEBUG(3,("got session\n"));
972 if (!cli_negprot(&pw_cli)) {
973 DEBUG(1,("%s rejected the negprot\n",desthost));
974 cli_shutdown(&pw_cli);
975 return NULL;
978 if (pw_cli.protocol < PROTOCOL_LANMAN2 ||
979 !(pw_cli.sec_mode & 1)) {
980 DEBUG(1,("%s isn't in user level security mode\n",desthost));
981 cli_shutdown(&pw_cli);
982 return NULL;
985 DEBUG(3,("password server OK\n"));
987 return &pw_cli;
990 /****************************************************************************
991 validate a password with the password server
992 ****************************************************************************/
993 BOOL server_validate(char *user, char *domain,
994 char *pass, int passlen,
995 char *ntpass, int ntpasslen)
997 extern fstring local_machine;
998 static unsigned char badpass[24];
1000 if (!pw_cli.initialised) {
1001 DEBUG(1,("password server %s is not connected\n", pw_cli.desthost));
1002 return(False);
1005 if(badpass[0] == 0) {
1006 memset(badpass, 0x1f, sizeof(badpass));
1009 if((passlen == sizeof(badpass)) && !memcmp(badpass, pass, passlen)) {
1010 /* Very unlikely, our random bad password is the same as the users
1011 password. */
1012 memset(badpass, badpass[0]+1, sizeof(badpass));
1016 * Attempt a session setup with a totally incorrect password.
1017 * If this succeeds with the guest bit *NOT* set then the password
1018 * server is broken and is not correctly setting the guest bit. We
1019 * need to detect this as some versions of NT4.x are broken. JRA.
1022 if (cli_session_setup(&pw_cli, user, (char *)badpass, sizeof(badpass),
1023 (char *)badpass, sizeof(badpass), domain)) {
1024 if ((SVAL(pw_cli.inbuf,smb_vwv2) & 1) == 0) {
1025 DEBUG(0,("server_validate: password server %s allows users as non-guest \
1026 with a bad password.\n", pw_cli.desthost));
1027 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
1028 use this machine as the password server.\n"));
1029 cli_ulogoff(&pw_cli);
1030 return False;
1032 cli_ulogoff(&pw_cli);
1036 * Now we know the password server will correctly set the guest bit, or is
1037 * not guest enabled, we can try with the real password.
1040 if (!cli_session_setup(&pw_cli, user, pass, passlen, ntpass, ntpasslen, domain)) {
1041 DEBUG(1,("password server %s rejected the password\n", pw_cli.desthost));
1042 return False;
1045 /* if logged in as guest then reject */
1046 if ((SVAL(pw_cli.inbuf,smb_vwv2) & 1) != 0) {
1047 DEBUG(1,("password server %s gave us guest only\n", pw_cli.desthost));
1048 cli_ulogoff(&pw_cli);
1049 return(False);
1053 * This patch from Rob Nielsen <ran@adc.com> makes doing
1054 * the NetWksaUserLogon a dynamic, rather than compile-time
1055 * parameter, defaulting to on. This is somewhat dangerous
1056 * as it allows people to turn off this neccessary check,
1057 * but so many people have had problems with this that I
1058 * think it is a neccessary change. JRA.
1061 if (lp_net_wksta_user_logon()) {
1062 DEBUG(3,("trying NetWkstaUserLogon with password server %s\n", pw_cli.desthost));
1064 if (!cli_send_tconX(&pw_cli, "IPC$", "IPC", "", 1)) {
1065 DEBUG(0,("password server %s refused IPC$ connect\n", pw_cli.desthost));
1066 cli_ulogoff(&pw_cli);
1067 return False;
1070 if (!cli_NetWkstaUserLogon(&pw_cli,user,local_machine)) {
1071 DEBUG(0,("password server %s failed NetWkstaUserLogon\n", pw_cli.desthost));
1072 cli_tdis(&pw_cli);
1073 cli_ulogoff(&pw_cli);
1074 return False;
1077 if (pw_cli.privilages == 0) {
1078 DEBUG(0,("password server %s gave guest privilages\n", pw_cli.desthost));
1079 cli_tdis(&pw_cli);
1080 cli_ulogoff(&pw_cli);
1081 return False;
1084 if (!strequal(pw_cli.eff_name, user)) {
1085 DEBUG(0,("password server %s gave different username %s\n",
1086 pw_cli.desthost,
1087 pw_cli.eff_name));
1088 cli_tdis(&pw_cli);
1089 cli_ulogoff(&pw_cli);
1090 return False;
1092 cli_tdis(&pw_cli);
1094 else {
1095 DEBUG(3,("skipping NetWkstaUserLogon with password server %s\n", pw_cli.desthost));
1098 DEBUG(3,("password server %s accepted the password\n", pw_cli.desthost));
1100 cli_ulogoff(&pw_cli);
1102 return(True);
1105 /***********************************************************************
1106 Do the same as security=server, but using NT Domain calls and a session
1107 key from the machine password.
1108 ************************************************************************/
1110 BOOL domain_client_validate( char *user, char *domain,
1111 char *smb_apasswd, int smb_apasslen,
1112 char *smb_ntpasswd, int smb_ntpasslen)
1114 unsigned char local_challenge[8];
1115 unsigned char local_lm_response[24];
1116 unsigned char local_nt_reponse[24];
1117 unsigned char trust_passwd[16];
1118 time_t lct;
1119 fstring remote_machine;
1120 char *p;
1121 struct in_addr dest_ip;
1122 NET_ID_INFO_CTR ctr;
1123 NET_USER_INFO_3 info3;
1124 struct cli_state cli;
1125 uint32 smb_uid_low;
1126 BOOL connected_ok = False;
1129 * Check that the requested domain is not our own machine name.
1130 * If it is, we should never check the PDC here, we use our own local
1131 * password file.
1134 if(strequal( domain, global_myname)) {
1135 DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
1136 return False;
1140 * Next, check that the passwords given were encrypted.
1143 if(((smb_apasslen != 24) && (smb_apasslen != 0)) ||
1144 ((smb_ntpasslen != 24) && (smb_ntpasslen != 0))) {
1147 * Not encrypted - do so.
1150 DEBUG(3,("domain_client_validate: User passwords not in encrypted format.\n"));
1151 generate_random_buffer( local_challenge, 8, False);
1152 SMBencrypt( (uchar *)smb_apasswd, local_challenge, local_lm_response);
1153 SMBNTencrypt((uchar *)smb_ntpasswd, local_challenge, local_nt_reponse);
1154 smb_apasslen = 24;
1155 smb_ntpasslen = 24;
1156 smb_apasswd = (char *)local_lm_response;
1157 smb_ntpasswd = (char *)local_nt_reponse;
1158 } else {
1161 * Encrypted - get the challenge we sent for these
1162 * responses.
1165 if (!last_challenge(local_challenge)) {
1166 DEBUG(0,("domain_client_validate: no challenge done - password failed\n"));
1167 return False;
1172 * Get the machine account password.
1174 if(!trust_password_lock( global_myworkgroup, global_myname, False)) {
1175 DEBUG(0,("domain_client_validate: unable to open the machine account password file for \
1176 machine %s in domain %s.\n", global_myname, global_myworkgroup ));
1177 return False;
1180 if(get_trust_account_password( trust_passwd, &lct) == False) {
1181 DEBUG(0,("domain_client_validate: unable to read the machine account password for \
1182 machine %s in domain %s.\n", global_myname, global_myworkgroup ));
1183 trust_password_unlock();
1184 return False;
1187 trust_password_unlock();
1190 * Here we check the last change time to see if the machine
1191 * password needs changing. JRA.
1194 if(time(NULL) > lct + lp_machine_password_timeout())
1195 global_machine_pasword_needs_changing = True;
1198 * At this point, smb_apasswd points to the lanman response to
1199 * the challenge in local_challenge, and smb_ntpasswd points to
1200 * the NT response to the challenge in local_challenge. Ship
1201 * these over the secure channel to a domain controller and
1202 * see if they were valid.
1205 memset(&cli, '\0', sizeof(struct cli_state));
1206 if(cli_initialise(&cli) == False) {
1207 DEBUG(0,("domain_client_validate: unable to initialize client connection.\n"));
1208 return False;
1212 * Treat each name in the 'password server =' line as a potential
1213 * PDC/BDC. Contact each in turn and try and authenticate.
1216 p = lp_passwordserver();
1217 while(p && next_token(&p,remote_machine,LIST_SEP,sizeof(remote_machine))) {
1219 standard_sub_basic(remote_machine);
1220 strupper(remote_machine);
1222 if(!resolve_name( remote_machine, &dest_ip)) {
1223 DEBUG(1,("domain_client_validate: Can't resolve address for %s\n", remote_machine));
1224 continue;
1227 if (ismyip(dest_ip)) {
1228 DEBUG(1,("domain_client_validate: Password server loop - not using password server %s\n",remote_machine));
1229 continue;
1232 if (!cli_connect(&cli, remote_machine, &dest_ip)) {
1233 DEBUG(0,("domain_client_validate: unable to connect to SMB server on \
1234 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1235 continue;
1238 if (!cli_session_request(&cli, remote_machine, 0x20, global_myname)) {
1239 DEBUG(0,("domain_client_validate: machine %s rejected the session setup. \
1240 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1241 cli_shutdown(&cli);
1242 continue;
1245 cli.protocol = PROTOCOL_NT1;
1247 if (!cli_negprot(&cli)) {
1248 DEBUG(0,("domain_client_validate: machine %s rejected the negotiate protocol. \
1249 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1250 cli_shutdown(&cli);
1251 continue;
1254 if (cli.protocol != PROTOCOL_NT1) {
1255 DEBUG(0,("domain_client_validate: machine %s didn't negotiate NT protocol.\n",
1256 remote_machine));
1257 cli_shutdown(&cli);
1258 continue;
1262 * Do an anonymous session setup.
1265 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
1266 DEBUG(0,("domain_client_validate: machine %s rejected the session setup. \
1267 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1268 cli_shutdown(&cli);
1269 continue;
1272 if (!(cli.sec_mode & 1)) {
1273 DEBUG(1,("domain_client_validate: machine %s isn't in user level security mode\n",
1274 remote_machine));
1275 cli_shutdown(&cli);
1276 continue;
1279 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
1280 DEBUG(0,("domain_client_validate: machine %s rejected the tconX on the IPC$ share. \
1281 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1282 cli_shutdown(&cli);
1283 continue;
1287 * We have an anonymous connection to IPC$.
1289 connected_ok = True;
1290 break;
1293 if (!connected_ok) {
1294 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
1295 cli_shutdown(&cli);
1296 return False;
1300 * Ok - we have an anonymous connection to the IPC$ share.
1301 * Now start the NT Domain stuff :-).
1304 if(cli_nt_session_open(&cli, PIPE_NETLOGON, False) == False) {
1305 DEBUG(0,("domain_client_validate: unable to open the domain client session to \
1306 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
1307 cli_nt_session_close(&cli);
1308 cli_ulogoff(&cli);
1309 cli_shutdown(&cli);
1310 return False;
1313 if(cli_nt_setup_creds(&cli, trust_passwd) == False) {
1314 DEBUG(0,("domain_client_validate: unable to setup the PDC credentials to machine \
1315 %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
1316 cli_nt_session_close(&cli);
1317 cli_ulogoff(&cli);
1318 cli_shutdown(&cli);
1319 return False;
1322 /* We really don't care what LUID we give the user. */
1323 generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
1325 if(cli_nt_login_network(&cli, domain, user, smb_uid_low, (char *)local_challenge,
1326 ((smb_apasslen != 0) ? smb_apasswd : NULL),
1327 ((smb_ntpasslen != 0) ? smb_ntpasswd : NULL),
1328 &ctr, &info3) == False) {
1329 DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
1330 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1331 cli_nt_session_close(&cli);
1332 cli_ulogoff(&cli);
1333 cli_shutdown(&cli);
1334 return False;
1338 * Here, if we really want it, we have lots of info about the user in info3.
1341 #if 0
1343 * We don't actually need to do this - plus it fails currently with
1344 * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
1345 * send here. JRA.
1348 if(cli_nt_logoff(&cli, &ctr) == False) {
1349 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
1350 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1351 cli_nt_session_close(&cli);
1352 cli_ulogoff(&cli);
1353 cli_shutdown(&cli);
1354 return False;
1356 #endif /* 0 */
1358 cli_nt_session_close(&cli);
1359 cli_ulogoff(&cli);
1360 cli_shutdown(&cli);
1361 return True;