2 Unix SMB/Netbios implementation.
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.
24 extern int DEBUGLEVEL;
27 /* users from session setup */
28 static pstring session_users="";
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)
45 * Leave this ifdef'd out while we test
46 * the new crypto random number generator.
49 unsigned char buf[16];
50 static int counter = 0;
54 /* get a sort-of random number */
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);
66 generate_random_buffer(buf,8,False);
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;
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);
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)
107 if ((vuid >= (uint16)num_validated_users) ||
108 (validated_users[vuid].uid == (uid_t)-1) || (validated_users[vuid].gid == (gid_t)-1))
110 return &validated_users[vuid];
113 /****************************************************************************
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;
127 /* same number of igroups as groups */
131 free((char *)vuser->groups);
134 free((char *)vuser->sids);
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);
154 /****************************************************************************
155 register a uid/name pair as being valid and that a valid password
156 has been given. vuid is biased by an offset. This allows us to
157 tell random client vuid's (normally zero) from valid vuids.
158 ****************************************************************************/
159 uint16 register_vuid(uid_t uid,gid_t gid, char *unix_name, char *requested_name, BOOL guest, uchar user_sess_key[16])
162 struct passwd *pwfile; /* for getting real name from passwd file */
164 /* Ensure no vuid gets registered in share level security. */
165 if(lp_security() == SEC_SHARE)
166 return UID_FIELD_INVALID;
170 * After observing MS-Exchange services writing to a Samba share
171 * I belive this code is incorrect. Each service does its own
172 * sessionsetup_and_X for the same user, and as each service shuts
173 * down, it does a user_logoff_and_X. As we are consolidating multiple
174 * sessionsetup_and_X's onto the same vuid here, when the first service
175 * shuts down, it invalidates all the open files for the other services.
176 * Hence I am removing this code and forcing each sessionsetup_and_X
178 * Jeremy Allison. (jallison@whistle.com).
182 for(i = 0; i < num_validated_users; i++) {
183 vuser = &validated_users[i];
184 if ( vuser->uid == uid )
185 return (uint16)(i + VUID_OFFSET); /* User already validated */
189 validated_users = (user_struct *)Realloc(validated_users,
191 (num_validated_users+1));
193 if (!validated_users)
195 DEBUG(0,("Failed to realloc users struct!\n"));
196 num_validated_users = 0;
197 return UID_FIELD_INVALID;
200 vuser = &validated_users[num_validated_users];
201 num_validated_users++;
205 vuser->guest = guest;
206 fstrcpy(vuser->name,unix_name);
207 fstrcpy(vuser->requested_name,requested_name);
208 memcpy(vuser->dc.user_sess_key, user_sess_key, sizeof(vuser->dc.user_sess_key));
214 vuser->groups = NULL;
216 /* Find all the groups this uid is in and store them.
217 Used by become_user() */
218 get_unixgroups(unix_name,uid,gid,
222 DEBUG(3,("uid %d registered to name %s\n",(int)uid,unix_name));
224 DEBUG(3, ("Clearing default real name\n"));
225 fstrcpy(vuser->real_name, "<Full Name>\0");
226 if (lp_unix_realname()) {
227 if ((pwfile=hashed_getpwnam(vuser->name))!= NULL)
229 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->name,pwfile->pw_gecos));
230 fstrcpy(vuser->real_name, pwfile->pw_gecos);
234 return (uint16)((num_validated_users - 1) + VUID_OFFSET);
238 /****************************************************************************
239 add a name to the session users list
240 ****************************************************************************/
241 void add_session_user(char *user)
244 StrnCpy(suser,user,sizeof(suser)-1);
246 if (!Get_Pwnam(suser,True)) return;
248 if (suser && *suser && !in_list(suser,session_users,False))
250 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
251 DEBUG(1,("Too many session users??\n"));
254 pstrcat(session_users," ");
255 pstrcat(session_users,suser);
261 /****************************************************************************
262 update the encrypted smbpasswd file from the plaintext username and password
263 *****************************************************************************/
264 static BOOL update_smbpassword_file(char *user, char *password)
266 struct smb_passwd *smbpw;
270 smbpw = getsmbpwnam(user);
274 DEBUG(0,("getsmbpwnam returned NULL\n"));
278 /* Here, the flag is one, because we want to ignore the
279 XXXXXXX'd out password */
280 ret = change_oem_password( smbpw, password, True);
283 DEBUG(3,("change_oem_password returned False\n"));
293 /****************************************************************************
294 core of smb password checking routine.
295 ****************************************************************************/
296 static BOOL smb_pwd_check_ntlmv1(char *password, unsigned char *part_passwd,
300 /* Finish the encryption of part_passwd. */
301 unsigned char p24[24];
303 if (part_passwd == NULL)
304 DEBUG(10,("No password set - allowing access\n"));
305 /* No password set - always true ! */
306 if (part_passwd == NULL)
309 SMBOWFencrypt(part_passwd, c8, p24);
310 if (sess_key != NULL)
312 SMBsesskeygen_ntv1(part_passwd, NULL, sess_key);
316 DEBUG(100,("Part password (P16) was |"));
317 dump_data(100, part_passwd, 16);
318 DEBUG(100,("Password from client was |"));
319 dump_data(100, password, 24);
320 DEBUG(100,("Given challenge was |"));
321 dump_data(100, c8, 8);
322 DEBUG(100,("Value from encryption was |"));
323 dump_data(100, p24, 24);
325 return (memcmp(p24, password, 24) == 0);
328 /****************************************************************************
329 core of smb password checking routine.
330 ****************************************************************************/
331 static BOOL smb_pwd_check_ntlmv2(char *password, size_t pwd_len,
332 unsigned char *part_passwd,
333 unsigned char const *c8,
334 const char *user, const char *domain,
337 /* Finish the encryption of part_passwd. */
338 unsigned char kr[16];
339 unsigned char resp[16];
341 if (part_passwd == NULL)
343 DEBUG(10,("No password set - allowing access\n"));
345 /* No password set - always true ! */
346 if (part_passwd == NULL)
351 ntv2_owf_gen(part_passwd, user, domain, kr);
352 SMBOWFencrypt_ntv2(kr, c8, 8, password+16, pwd_len-16, resp);
353 if (sess_key != NULL)
355 SMBsesskeygen_ntv2(kr, resp, sess_key);
359 DEBUG(100,("Part password (P16) was |"));
360 dump_data(100, part_passwd, 16);
361 DEBUG(100,("Password from client was |"));
362 dump_data(100, password, pwd_len);
363 DEBUG(100,("Given challenge was |"));
364 dump_data(100, c8, 8);
365 DEBUG(100,("Value from encryption was |"));
366 dump_data(100, resp, 16);
369 return (memcmp(resp, password, 16) == 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, uchar chal[8],
377 const char *user, const char *domain,
378 uchar *lm_pass, size_t lm_pwd_len,
379 uchar *nt_pass, size_t nt_pwd_len,
384 if (smb_pass == NULL)
389 DEBUG(4,("Checking SMB password for user %s\n",
390 smb_pass->unix_name));
392 if (smb_pass->acct_ctrl & ACB_DISABLED)
394 DEBUG(3,("account for user %s was disabled.\n",
395 smb_pass->unix_name));
401 DEBUG(5,("use last SMBnegprot challenge\n"));
402 if (!last_challenge(challenge))
404 DEBUG(1,("no challenge done - password failed\n"));
410 DEBUG(5,("challenge received\n"));
411 memcpy(challenge, chal, 8);
414 if ((Protocol >= PROTOCOL_NT1) && (smb_pass->smb_nt_passwd != NULL))
416 /* We have the NT MD4 hash challenge available - see if we can
417 use it (ie. does it exist in the smbpasswd file).
419 if (lp_server_ntlmv2() != False && nt_pwd_len > 24)
421 DEBUG(4,("smb_password_ok: Check NTLMv2 password\n"));
422 if (smb_pwd_check_ntlmv2(nt_pass, nt_pwd_len,
423 (uchar *)smb_pass->smb_nt_passwd,
424 challenge, user, domain,
430 if (lp_server_ntlmv2() != True && nt_pwd_len == 24)
432 DEBUG(4,("smb_password_ok: Check NT MD4 password\n"));
433 if (smb_pwd_check_ntlmv1((char *)nt_pass,
434 (uchar *)smb_pass->smb_nt_passwd,
438 DEBUG(4,("NT MD4 password check succeeded\n"));
442 DEBUG(4,("NT MD4 password check failed\n"));
445 if (lp_server_ntlmv2() == True)
447 DEBUG(4,("Not checking LM MD4 password\n"));
451 /* Try against the lanman password. smb_pass->smb_passwd == NULL means
452 no password, allow access. */
454 DEBUG(4,("Checking LM MD4 password\n"));
456 if ((smb_pass->smb_passwd == NULL) &&
457 (smb_pass->acct_ctrl & ACB_PWNOTREQ))
459 DEBUG(4,("no password required for user %s\n",
460 smb_pass->unix_name));
464 if ((smb_pass->smb_passwd != NULL) &&
465 smb_pwd_check_ntlmv1((char *)lm_pass,
466 (uchar *)smb_pass->smb_passwd,
469 DEBUG(4,("LM MD4 password check succeeded\n"));
473 DEBUG(4,("LM MD4 password check failed\n"));
479 /****************************************************************************
480 check if a username/password is OK assuming the password is a 24 byte
482 return True if the password is correct, False otherwise
483 ****************************************************************************/
485 BOOL pass_check_smb(struct smb_passwd *smb_pass, char *domain, uchar *chal,
486 uchar *lm_pwd, size_t lm_pwd_len,
487 uchar *nt_pwd, size_t nt_pwd_len,
488 struct passwd *pwd, uchar user_sess_key[16])
490 const struct passwd *pass;
494 if (smb_pass == NULL)
496 DEBUG(3,("Couldn't find user %s in smb_passwd file.\n", user));
500 user = smb_pass->unix_name;
502 if (lm_pwd == NULL || nt_pwd == NULL)
507 if (pwd != NULL && user == NULL)
509 pass = (struct passwd *) pwd;
510 user = pass->pw_name;
514 pass = Get_Pwnam(user,True);
517 DEBUG(3,("Couldn't find user %s\n",user));
520 memcpy(&pw, pass, sizeof(struct passwd));
524 /* Quit if the account was disabled. */
525 if (smb_pass->acct_ctrl & ACB_DISABLED) {
526 DEBUG(3,("account for user %s was disabled.\n", user));
530 /* Ensure the uid's match */
531 if (smb_pass->unix_uid != pass->pw_uid)
533 DEBUG(3,("Error : UNIX (%d) and SMB (%d) uids in password files do not match !\n", pass->pw_uid, smb_pass->unix_uid));
537 if (lm_pwd[0] == '\0' && IS_BITS_SET_ALL(smb_pass->acct_ctrl, ACB_PWNOTREQ) && lp_null_passwords())
539 DEBUG(3,("account for user %s has no password and null passwords are allowed.\n", smb_pass->unix_name));
543 if (smb_password_ok(smb_pass, chal, user, domain,
548 if (user_sess_key != NULL)
550 #ifdef DEBUG_PASSWORD
551 DEBUG(100,("user session key: "));
552 dump_data(100, user_sess_key, 16);
558 DEBUG(3,("Error pass_check_smb failed\n"));
562 /****************************************************************************
563 check if a username/password pair is OK either via the system password
564 database or the encrypted SMB password database
565 return True if the password is correct, False otherwise
566 ****************************************************************************/
567 BOOL password_ok(char *user, char *password, int pwlen, struct passwd *pwd,
568 uchar user_sess_key[16])
570 if (pwlen >= 24 || (lp_encrypted_passwords() && (pwlen == 0) && lp_null_passwords()))
572 /* if 24 bytes or longer assume it is an encrypted password */
575 if (!last_challenge(challenge))
577 DEBUG(0,("Error: challenge not done for user=%s\n", user));
581 return pass_check_smb(getsmbpwnam(user), global_myworkgroup,
582 challenge, (uchar *)password,
583 pwlen, (uchar *)password, pwlen,
587 return pass_check(user, password, pwlen, pwd,
588 lp_update_encrypted() ?
589 update_smbpassword_file : NULL);
592 /****************************************************************************
593 check if a username is valid
594 ****************************************************************************/
595 BOOL user_ok(char *user,int snum)
597 pstring valid, invalid;
600 StrnCpy(valid, lp_valid_users(snum), sizeof(pstring));
601 StrnCpy(invalid, lp_invalid_users(snum), sizeof(pstring));
603 string_sub(valid,"%S",lp_servicename(snum));
604 string_sub(invalid,"%S",lp_servicename(snum));
606 ret = !user_in_list(user,invalid);
608 if (ret && valid && *valid) {
609 ret = user_in_list(user,valid);
612 if (ret && lp_onlyuser(snum)) {
613 char *user_list = lp_username(snum);
614 string_sub(user_list,"%S",lp_servicename(snum));
615 ret = user_in_list(user,user_list);
624 /****************************************************************************
625 validate a group username entry. Return the username or NULL
626 ****************************************************************************/
627 static char *validate_group(char *group,char *password,int pwlen,int snum,
628 uchar user_sess_key[16])
630 #if defined(HAVE_NETGROUP) && defined(HAVE_GETNETGRENT) && defined(HAVE_SETNETGRENT) && defined(HAVE_ENDNETGRENT)
632 char *host, *user, *domain;
634 while (getnetgrent(&host, &user, &domain)) {
636 if (user_ok(user, snum) &&
637 password_ok(user,password,pwlen,NULL, user_sess_key)) {
649 struct group *gptr = (struct group *)getgrnam(group);
653 member = gptr->gr_mem;
654 while (member && *member)
657 fstrcpy(name,*member);
658 if (user_ok(name,snum) &&
659 password_ok(name,password,pwlen,NULL, user_sess_key))
663 #ifdef GROUP_CHECK_PWENT
669 while (pwd = getpwent ()) {
670 if (*(pwd->pw_passwd) && pwd->pw_gid == gptr->gr_gid) {
671 /* This Entry have PASSWORD and same GID then check pwd */
672 if (password_ok(NULL, password, pwlen, pwd, user_sess_key)) {
673 fstrcpy(tm, pwd->pw_name);
681 #endif /* GROUP_CHECK_PWENT */
690 /****************************************************************************
691 check for authority to login to a service with a given username/password
692 ****************************************************************************/
693 BOOL authorise_login(int snum,char *user,char *password, int pwlen,
694 BOOL *guest,BOOL *force,uint16 vuid)
701 DEBUG(100,("checking authorisation on user=%s pass=%s\n",user,password));
704 /* there are several possibilities:
705 1) login as the given user with given password
706 2) login as a previously registered username with the given password
707 3) login as a session list username with the given password
708 4) login as a previously validated user/password pair
709 5) login as the "user =" user with given password
710 6) login as the "user =" user with no password (guest connection)
711 7) login as guest user with no password
713 if the service is guest_only then steps 1 to 5 are skipped
716 if (GUEST_ONLY(snum)) *force = True;
718 if (!(GUEST_ONLY(snum) && GUEST_OK(snum)))
721 user_struct *vuser = get_valid_user_struct(vuid);
723 /* check the given username and password */
724 if (!ok && (*user) && user_ok(user,snum)) {
725 ok = password_ok(user,password, pwlen, NULL, vuser->dc.user_sess_key);
726 if (ok) DEBUG(3,("ACCEPTED: given username password ok\n"));
729 /* check for a previously registered guest username */
730 if (!ok && (vuser != 0) && vuser->guest) {
731 if (user_ok(vuser->name,snum) &&
732 password_ok(vuser->name, password, pwlen, NULL, vuser->dc.user_sess_key)) {
733 fstrcpy(user, vuser->name);
734 vuser->guest = False;
735 DEBUG(3,("ACCEPTED: given password with registered user %s\n", user));
741 /* now check the list of session users */
745 char *user_list = strdup(session_users);
746 if (!user_list) return False;
748 for (auser=strtok(user_list,LIST_SEP);
750 auser = strtok(NULL,LIST_SEP))
753 fstrcpy(user2,auser);
754 if (!user_ok(user2,snum)) continue;
756 if (password_ok(user2,password, pwlen, NULL, vuser->dc.user_sess_key)) {
759 DEBUG(3,("ACCEPTED: session list username and given password ok\n"));
765 /* check for a previously validated username/password pair */
766 if (!ok && (!lp_revalidate(snum) || lp_security() > SEC_SHARE) &&
767 (vuser != 0) && !vuser->guest &&
768 user_ok(vuser->name,snum)) {
769 fstrcpy(user,vuser->name);
771 DEBUG(3,("ACCEPTED: validated uid ok as non-guest\n"));
775 /* check for a rhosts entry */
776 if (!ok && user_ok(user,snum) && check_hosts_equiv(user)) {
778 DEBUG(3,("ACCEPTED: hosts equiv or rhosts entry\n"));
781 /* check the user= fields and the given password */
782 if (!ok && lp_username(snum)) {
785 StrnCpy(user_list,lp_username(snum),sizeof(pstring));
787 string_sub(user_list,"%S",lp_servicename(snum));
789 for (auser=strtok(user_list,LIST_SEP);
791 auser = strtok(NULL,LIST_SEP))
795 auser = validate_group(auser+1,password,pwlen,snum, vuser->dc.user_sess_key);
800 DEBUG(3,("ACCEPTED: group username and given password ok\n"));
806 fstrcpy(user2,auser);
807 if (user_ok(user2,snum) &&
808 password_ok(user2,password,pwlen,NULL, vuser->dc.user_sess_key))
812 DEBUG(3,("ACCEPTED: user list username and given password ok\n"));
817 } /* not guest only */
819 /* check for a normal guest connection */
820 if (!ok && GUEST_OK(snum))
823 StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
824 if (Get_Pwnam(guestname,True))
826 fstrcpy(user,guestname);
828 DEBUG(3,("ACCEPTED: guest account and guest ok\n"));
831 DEBUG(0,("Invalid guest account %s??\n",guestname));
836 if (ok && !user_ok(user,snum))
838 DEBUG(0,("rejected invalid user %s\n",user));
846 /****************************************************************************
847 read the a hosts.equiv or .rhosts file and check if it
848 allows this user from this machine
849 ****************************************************************************/
850 static BOOL check_user_equiv(char *user, char *remote, char *equiv_file)
853 int plus_allowed = 1;
856 FILE *fp = sys_fopen(equiv_file, "r");
857 DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
858 if (! fp) return False;
859 while(fgets(buf, sizeof(buf), fp))
861 trim_string(buf," "," ");
863 if (buf[0] != '#' && buf[0] != '\n')
865 BOOL is_group = False;
868 if (strcmp(buf, "NO_PLUS\n") == 0)
870 DEBUG(6, ("check_user_equiv NO_PLUS\n"));
877 if (*bp == '\n' && plus_allowed)
879 /* a bare plus means everbody allowed */
880 DEBUG(6, ("check_user_equiv everybody allowed\n"));
885 else if (buf[0] == '-')
895 file_host = strtok(bp, " \t\n");
896 file_user = strtok(NULL, " \t\n");
897 DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)",
898 file_user ? file_user : "(null)" ));
899 if (file_host && *file_host)
901 BOOL host_ok = False;
903 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
906 static char *mydomain = NULL;
908 yp_get_default_domain(&mydomain);
909 if (mydomain && innetgr(file_host,remote,user,mydomain))
915 DEBUG(1,("Netgroups not configured\n"));
920 /* is it this host */
921 /* the fact that remote has come from a call of gethostbyaddr
922 * means that it may have the fully qualified domain name
923 * so we could look up the file version to get it into
924 * a canonical form, but I would rather just type it
925 * in full in the equiv file
927 if (!host_ok && !is_group && strequal(remote, file_host))
933 /* is it this user */
934 if (file_user == 0 || strequal(user, file_user))
937 DEBUG(5, ("check_user_equiv matched %s%s %s\n",
938 (plus ? "+" : "-"), file_host,
939 (file_user ? file_user : "")));
940 return (plus ? True : False);
951 /****************************************************************************
952 check for a possible hosts equiv or rhosts entry for the user
953 ****************************************************************************/
954 BOOL check_hosts_equiv(char *user)
958 const struct passwd *pass = Get_Pwnam(user,True);
963 fname = lp_hosts_equiv();
965 /* note: don't allow hosts.equiv on root */
966 if (fname && *fname && (pass->pw_uid != 0)) {
968 if (check_user_equiv(user,client_name(Client),fname))
974 char *home = get_home_dir(user);
977 slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
978 if (check_user_equiv(user,client_name(Client),rhostsfile))
987 /****************************************************************************
988 return the client state structure
989 ****************************************************************************/
990 struct cli_state *server_client(void)
992 static struct cli_state pw_cli;
996 /****************************************************************************
997 support for server level security
998 ****************************************************************************/
999 struct cli_state *server_cryptkey(void)
1001 if (cli_connect_serverlist(server_client(), lp_passwordserver()))
1003 return server_client();
1008 /****************************************************************************
1009 validate a password with the password server
1010 ****************************************************************************/
1011 BOOL server_validate(char *user, char *domain,
1012 char *pass, int passlen,
1013 char *ntpass, int ntpasslen)
1015 struct cli_state *cli;
1016 static unsigned char badpass[24];
1017 static BOOL tested_password_server = False;
1018 static BOOL bad_password_server = False;
1020 cli = server_client();
1022 if (!cli->initialised) {
1023 DEBUG(1,("password server %s is not connected\n", cli->desthost));
1028 memset(badpass, 0x1f, sizeof(badpass));
1030 if((passlen == sizeof(badpass)) && !memcmp(badpass, pass, passlen)) {
1032 * Very unlikely, our random bad password is the same as the users
1034 memset(badpass, badpass[0]+1, sizeof(badpass));
1038 * Attempt a session setup with a totally incorrect password.
1039 * If this succeeds with the guest bit *NOT* set then the password
1040 * server is broken and is not correctly setting the guest bit. We
1041 * need to detect this as some versions of NT4.x are broken. JRA.
1044 if(!tested_password_server) {
1045 if (cli_session_setup(cli, user, (char *)badpass, sizeof(badpass),
1046 (char *)badpass, sizeof(badpass), domain)) {
1049 * We connected to the password server so we
1050 * can say we've tested it.
1052 tested_password_server = True;
1054 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
1055 DEBUG(0,("server_validate: password server %s allows users as non-guest \
1056 with a bad password.\n", cli->desthost));
1057 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
1058 use this machine as the password server.\n"));
1062 * Password server has the bug.
1064 bad_password_server = True;
1072 * We have already tested the password server.
1073 * Fail immediately if it has the bug.
1076 if(bad_password_server) {
1077 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
1078 with a bad password.\n", cli->desthost));
1079 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
1080 use this machine as the password server.\n"));
1086 * Now we know the password server will correctly set the guest bit, or is
1087 * not guest enabled, we can try with the real password.
1090 if (!cli_session_setup(cli, user, pass, passlen, ntpass, ntpasslen, domain)) {
1091 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
1095 /* if logged in as guest then reject */
1096 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
1097 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
1108 /***********************************************************************
1109 Do the same as security=server, but using NT Domain calls and a session
1110 key from the workstation trust account password.
1111 ************************************************************************/
1113 BOOL domain_client_validate( char *user, char *domain, char *server_list,
1114 char *acct_name, uint16 acct_type,
1115 char *smb_apasswd, int smb_apasslen,
1116 char *smb_ntpasswd, int smb_ntpasslen,
1117 uchar user_sess_key[16])
1119 uint16 nt_pipe_fnum;
1120 unsigned char local_challenge[8];
1121 unsigned char local_lm_response[24];
1122 unsigned char local_nt_reponse[24];
1123 unsigned char trust_passwd[16];
1124 NET_ID_INFO_CTR ctr;
1125 NET_USER_INFO_3 info3;
1126 struct cli_state cli;
1130 fstrcpy(trust_acct, acct_name);
1131 fstrcat(trust_acct, "$");
1134 * Check that the requested domain is not our own machine name.
1135 * If it is, we should never check the PDC here, we use our own local
1139 if(strequal( domain, global_myname))
1141 DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
1146 * Next, check that the passwords given were encrypted.
1149 if(((smb_apasslen != 24) && (smb_apasslen != 0)) ||
1150 ((smb_ntpasslen <= 24) && (smb_ntpasslen != 0)))
1153 * Not encrypted - do so.
1156 DEBUG(3,("domain_client_validate: User passwords not in encrypted format.\n"));
1157 generate_random_buffer( local_challenge, 8, False);
1158 SMBencrypt( (uchar *)smb_apasswd, local_challenge, local_lm_response);
1159 SMBNTencrypt((uchar *)smb_ntpasswd, local_challenge, local_nt_reponse);
1162 smb_apasswd = (char *)local_lm_response;
1163 smb_ntpasswd = (char *)local_nt_reponse;
1168 * Encrypted - get the challenge we sent for these
1172 if (!last_challenge(local_challenge))
1174 DEBUG(0,("domain_client_validate: no challenge done - password failed\n"));
1180 * Get the workstation trust account password.
1182 if (!trust_get_passwd( trust_passwd, domain, acct_name))
1188 * At this point, smb_apasswd points to the lanman response to
1189 * the challenge in local_challenge, and smb_ntpasswd points to
1190 * the NT response to the challenge in local_challenge. Ship
1191 * these over the secure channel to a domain controller and
1192 * see if they were valid.
1195 if (!cli_connect_serverlist(&cli, server_list))
1197 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
1202 * Ok - we have an anonymous connection to the IPC$ share.
1203 * Now start the NT Domain stuff :-).
1206 if (!cli_nt_session_open(&cli, PIPE_NETLOGON, &nt_pipe_fnum)) {
1207 DEBUG(0,("domain_client_validate: unable to open the domain client session to \
1208 machine %s. Error was : %s.\n", cli.desthost, cli_errstr(&cli)));
1209 cli_nt_session_close(&cli, nt_pipe_fnum);
1215 if(cli_nt_setup_creds(&cli, nt_pipe_fnum,
1216 trust_acct, global_myname, trust_passwd, acct_type) != 0x0)
1218 DEBUG(0,("domain_client_validate: unable to setup the PDC credentials to machine \
1219 %s. Error was : %s.\n", cli.desthost, cli_errstr(&cli)));
1220 cli_nt_session_close(&cli, nt_pipe_fnum);
1226 /* We really don't care what LUID we give the user. */
1227 generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
1229 if (!cli_nt_login_network(&cli, nt_pipe_fnum, domain, user, smb_uid_low, (char *)local_challenge,
1230 ((smb_apasslen != 0) ? smb_apasswd : NULL),
1231 ((smb_ntpasslen != 0) ? smb_ntpasswd : NULL),
1234 DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
1235 %s to Domain controller %s. Error was %s.\n", user, domain, cli.desthost, cli_errstr(&cli)));
1236 cli_nt_session_close(&cli, nt_pipe_fnum);
1243 * Here, if we really want it, we have lots of info about the user in info3.
1244 * LKCLXXXX - really important to check things like "is this user acct
1245 * locked out / disabled" etc!!!!
1250 * We don't actually need to do this - plus it fails currently with
1251 * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
1255 if (!cli_nt_logoff(&cli, nt_pipe_fnum, &ctr))
1257 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
1258 %s to Domain controller %s. Error was %s.\n", user, domain, cli.desthost, cli_errstr(&cli)));
1259 cli_nt_session_close(&cli, nt_pipe_fnum);
1266 cli_nt_session_close(&cli, nt_pipe_fnum);