4 Unix SMB/Netbios implementation.
6 Password and authentication handling
7 Copyright (C) Andrew Tridgell 1992-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern int DEBUGLEVEL;
28 extern struct in_addr ipzero;
30 /* users from session setup */
31 static pstring session_users="";
33 extern pstring global_myname;
34 extern fstring global_myworkgroup;
36 /* Data to do lanman1/2 password challenge. */
37 static unsigned char saved_challenge[8];
38 static BOOL challenge_sent=False;
40 /*******************************************************************
41 Get the next challenge value - no repeats.
42 ********************************************************************/
43 void generate_next_challenge(char *challenge)
47 * Leave this ifdef'd out while we test
48 * the new crypto random number generator.
51 unsigned char buf[16];
52 static int counter = 0;
56 /* get a sort-of random number */
58 v1 = (counter++) + sys_getpid() + tval.tv_sec;
59 v2 = (counter++) * sys_getpid() + tval.tv_usec;
60 SIVAL(challenge,0,v1);
61 SIVAL(challenge,4,v2);
63 /* mash it up with md4 */
64 mdfour(buf, (unsigned char *)challenge, 8);
68 generate_random_buffer(buf,8,False);
70 memcpy(saved_challenge, buf, 8);
71 memcpy(challenge,buf,8);
72 challenge_sent = True;
75 /*******************************************************************
76 set the last challenge sent, usually from a password server
77 ********************************************************************/
78 BOOL set_challenge(unsigned char *challenge)
80 memcpy(saved_challenge,challenge,8);
81 challenge_sent = True;
85 /*******************************************************************
86 get the last challenge sent
87 ********************************************************************/
88 static BOOL last_challenge(unsigned char *challenge)
90 if (!challenge_sent) return(False);
91 memcpy(challenge,saved_challenge,8);
95 /* this holds info on user ids that are already validated for this VC */
96 static user_struct *validated_users = NULL;
97 static int num_validated_users = 0;
99 /****************************************************************************
100 check if a uid has been validated, and return an pointer to the user_struct
101 if it has. NULL if not. vuid is biased by an offset. This allows us to
102 tell random client vuid's (normally zero) from valid vuids.
103 ****************************************************************************/
104 user_struct *get_valid_user_struct(uint16 vuid)
106 if (vuid == UID_FIELD_INVALID)
109 if ((vuid >= (uint16)num_validated_users) ||
110 (validated_users[vuid].uid == (uid_t)-1) || (validated_users[vuid].gid == (gid_t)-1))
112 return &validated_users[vuid];
115 /****************************************************************************
117 ****************************************************************************/
118 void invalidate_vuid(uint16 vuid)
120 user_struct *vuser = get_valid_user_struct(vuid);
122 if (vuser == NULL) return;
124 vuser->uid = (uid_t)-1;
125 vuser->gid = (gid_t)-1;
127 /* same number of igroups as groups */
131 free((char *)vuser->groups);
133 vuser->groups = NULL;
137 /****************************************************************************
138 return a validated username
139 ****************************************************************************/
140 char *validated_username(uint16 vuid)
142 user_struct *vuser = get_valid_user_struct(vuid);
145 return(vuser->user.unix_name);
148 /****************************************************************************
149 return a validated domain
150 ****************************************************************************/
151 char *validated_domain(uint16 vuid)
153 user_struct *vuser = get_valid_user_struct(vuid);
156 return(vuser->user.domain);
160 /****************************************************************************
161 Setup the groups a user belongs to.
162 ****************************************************************************/
163 int setup_groups(char *user, char *domain,
164 uid_t uid, gid_t gid, int *p_ngroups, gid_t **p_groups)
168 gid_t *groups = NULL;
170 if (-1 == smb_initgroups(user,domain,gid))
172 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
175 if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767)
177 DEBUG(0,("This is probably a problem with the account %s\n", user));
183 ngroups = sys_getgroups(0,&grp);
186 ngroups = groups_max();
189 if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL)
191 DEBUG(0,("setup_groups malloc fail !\n"));
195 ngroups = sys_getgroups(ngroups,groups);
197 (*p_ngroups) = ngroups;
198 (*p_groups) = groups;
200 DEBUG( 3, ( "%s is in %d groups: ", user, ngroups ) );
201 for (i = 0; i < ngroups; i++ )
203 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
205 DEBUG( 3, ( "\n" ) );
211 /****************************************************************************
212 register a uid/name pair as being valid and that a valid password
213 has been given. vuid is biased by an offset. This allows us to
214 tell random client vuid's (normally zero) from valid vuids.
215 ****************************************************************************/
216 uint16 register_vuid(uid_t uid,gid_t gid, char *unix_name, char *requested_name,
217 char *domain,BOOL guest)
220 struct passwd *pwfile; /* for getting real name from passwd file */
222 /* Ensure no vuid gets registered in share level security. */
223 if(lp_security() == SEC_SHARE)
224 return UID_FIELD_INVALID;
228 * After observing MS-Exchange services writing to a Samba share
229 * I belive this code is incorrect. Each service does its own
230 * sessionsetup_and_X for the same user, and as each service shuts
231 * down, it does a user_logoff_and_X. As we are consolidating multiple
232 * sessionsetup_and_X's onto the same vuid here, when the first service
233 * shuts down, it invalidates all the open files for the other services.
234 * Hence I am removing this code and forcing each sessionsetup_and_X
236 * Jeremy Allison. (jallison@whistle.com).
240 for(i = 0; i < num_validated_users; i++) {
241 vuser = &validated_users[i];
242 if ( vuser->uid == uid )
243 return (uint16)(i + VUID_OFFSET); /* User already validated */
247 validated_users = (user_struct *)Realloc(validated_users,
249 (num_validated_users+1));
251 if (!validated_users)
253 DEBUG(0,("Failed to realloc users struct!\n"));
254 num_validated_users = 0;
255 return UID_FIELD_INVALID;
258 vuser = &validated_users[num_validated_users];
259 num_validated_users++;
263 vuser->guest = guest;
264 fstrcpy(vuser->user.unix_name,unix_name);
265 fstrcpy(vuser->user.smb_name,requested_name);
266 fstrcpy(vuser->user.domain,domain);
269 vuser->groups = NULL;
271 /* Find all the groups this uid is in and store them.
272 Used by become_user() */
273 setup_groups(unix_name,domain,uid,gid,
277 DEBUG(3,("uid %d registered to name %s\n",(int)uid,unix_name));
279 DEBUG(3, ("Clearing default real name\n"));
280 fstrcpy(vuser->user.full_name, "<Full Name>");
281 if (lp_unix_realname()) {
282 if ((pwfile=sys_getpwnam(vuser->user.unix_name))!= NULL)
284 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,pwfile->pw_gecos));
285 fstrcpy(vuser->user.full_name, pwfile->pw_gecos);
289 memset(&vuser->dc, '\0', sizeof(vuser->dc));
291 return (uint16)((num_validated_users - 1) + VUID_OFFSET);
295 /****************************************************************************
296 add a name to the session users list
297 ****************************************************************************/
298 void add_session_user(char *user)
301 StrnCpy(suser,user,sizeof(suser)-1);
303 if (!Get_Pwnam(suser,True)) return;
305 if (suser && *suser && !in_list(suser,session_users,False))
307 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
308 DEBUG(1,("Too many session users??\n"));
311 pstrcat(session_users," ");
312 pstrcat(session_users,suser);
318 /****************************************************************************
319 update the encrypted smbpasswd file from the plaintext username and password
320 *****************************************************************************/
321 static BOOL update_smbpassword_file(char *user, char *password)
323 struct smb_passwd *smbpw;
327 smbpw = getsmbpwnam(user);
331 DEBUG(0,("getsmbpwnam returned NULL\n"));
336 * Remove the account disabled flag - we are updating the
337 * users password from a login.
339 smbpw->acct_ctrl &= ~ACB_DISABLED;
341 /* Here, the flag is one, because we want to ignore the
342 XXXXXXX'd out password */
343 ret = change_oem_password( smbpw, password, True);
345 DEBUG(3,("change_oem_password returned False\n"));
355 /****************************************************************************
356 core of smb password checking routine.
357 ****************************************************************************/
358 BOOL smb_password_check(char *password, unsigned char *part_passwd, unsigned char *c8)
360 /* Finish the encryption of part_passwd. */
361 unsigned char p21[21];
362 unsigned char p24[24];
364 if (part_passwd == NULL)
365 DEBUG(10,("No password set - allowing access\n"));
366 /* No password set - always true ! */
367 if (part_passwd == NULL)
371 memcpy(p21,part_passwd,16);
376 DEBUG(100,("Part password (P16) was |"));
377 for(i = 0; i < 16; i++)
378 DEBUG(100,("%X ", (unsigned char)part_passwd[i]));
380 DEBUG(100,("Password from client was |"));
381 for(i = 0; i < 24; i++)
382 DEBUG(100,("%X ", (unsigned char)password[i]));
384 DEBUG(100,("Given challenge was |"));
385 for(i = 0; i < 8; i++)
386 DEBUG(100,("%X ", (unsigned char)c8[i]));
388 DEBUG(100,("Value from encryption was |"));
389 for(i = 0; i < 24; i++)
390 DEBUG(100,("%X ", (unsigned char)p24[i]));
394 return (memcmp(p24, password, 24) == 0);
397 /****************************************************************************
398 Do a specific test for an smb password being correct, given a smb_password and
399 the lanman and NT responses.
400 ****************************************************************************/
401 BOOL smb_password_ok(struct smb_passwd *smb_pass, uchar chal[8],
402 uchar lm_pass[24], uchar nt_pass[24])
406 if (!lm_pass || !smb_pass) return(False);
408 DEBUG(4,("Checking SMB password for user %s\n",
409 smb_pass->smb_name));
411 if(smb_pass->acct_ctrl & ACB_DISABLED) {
412 DEBUG(1,("account for user %s was disabled.\n",
413 smb_pass->smb_name));
419 DEBUG(5,("use last SMBnegprot challenge\n"));
420 if (!last_challenge(challenge))
422 DEBUG(1,("no challenge done - password failed\n"));
428 DEBUG(5,("challenge received\n"));
429 memcpy(challenge, chal, 8);
432 if ((Protocol >= PROTOCOL_NT1) && (smb_pass->smb_nt_passwd != NULL)) {
433 /* We have the NT MD4 hash challenge available - see if we can
434 use it (ie. does it exist in the smbpasswd file).
436 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
437 if (smb_password_check((char *)nt_pass,
438 (uchar *)smb_pass->smb_nt_passwd,
440 DEBUG(4,("NT MD4 password check succeeded\n"));
443 DEBUG(4,("NT MD4 password check failed\n"));
446 /* Try against the lanman password. smb_pass->smb_passwd == NULL means
447 no password, allow access. */
449 DEBUG(4,("Checking LM MD4 password\n"));
451 if((smb_pass->smb_passwd == NULL) &&
452 (smb_pass->acct_ctrl & ACB_PWNOTREQ)) {
453 DEBUG(4,("no password required for user %s\n",
454 smb_pass->smb_name));
458 if((smb_pass->smb_passwd != NULL) &&
459 smb_password_check((char *)lm_pass,
460 (uchar *)smb_pass->smb_passwd, challenge)) {
461 DEBUG(4,("LM MD4 password check succeeded\n"));
465 DEBUG(4,("LM MD4 password check failed\n"));
471 /****************************************************************************
472 check if a username/password is OK assuming the password is a 24 byte
474 return True if the password is correct, False otherwise
475 ****************************************************************************/
477 BOOL pass_check_smb(char *user, char *domain,
478 uchar *chal, uchar *lm_pwd, uchar *nt_pwd,
482 struct smb_passwd *smb_pass;
484 if (!lm_pwd || !nt_pwd)
489 if (pwd != NULL && user == NULL)
491 pass = (struct passwd *) pwd;
492 user = pass->pw_name;
496 pass = smb_getpwnam(user,domain,True);
501 DEBUG(1,("Couldn't find user '%s' in UNIX password database.\n",user));
505 smb_pass = getsmbpwnam(user);
507 if (smb_pass == NULL)
509 DEBUG(1,("Couldn't find user '%s' in smb_passwd file.\n", user));
513 /* Quit if the account was disabled. */
514 if(smb_pass->acct_ctrl & ACB_DISABLED) {
515 DEBUG(1,("Account for user '%s' was disabled.\n", user));
519 /* Ensure the uid's match */
520 if (smb_pass->smb_userid != pass->pw_uid)
522 DEBUG(0,("Error : UNIX and SMB uids in password files do not match for user '%s'!\n", user));
526 if (lm_pwd[0] == '\0' && IS_BITS_SET_ALL(smb_pass->acct_ctrl, ACB_PWNOTREQ) && lp_null_passwords())
528 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", smb_pass->smb_name));
532 if (smb_password_ok(smb_pass, chal, lm_pwd, nt_pwd))
537 DEBUG(2,("pass_check_smb failed - invalid password for user [%s]\n", user));
541 /****************************************************************************
542 check if a username/password pair is OK either via the system password
543 database or the encrypted SMB password database
544 return True if the password is correct, False otherwise
545 ****************************************************************************/
546 BOOL password_ok(char *user, char *password, int pwlen, struct passwd *pwd)
548 if (pwlen == 24 || (lp_encrypted_passwords() && (pwlen == 0) && lp_null_passwords()))
550 /* if 24 bytes long assume it is an encrypted password */
553 if (!last_challenge(challenge))
555 DEBUG(0,("Error: challenge not done for user=%s\n", user));
559 return pass_check_smb(user, global_myworkgroup,
560 challenge, (uchar *)password, (uchar *)password, pwd);
563 return pass_check(user, password, pwlen, pwd,
564 lp_update_encrypted() ?
565 update_smbpassword_file : NULL);
568 /****************************************************************************
569 check if a username is valid
570 ****************************************************************************/
571 BOOL user_ok(char *user,int snum)
573 pstring valid, invalid;
576 StrnCpy(valid, lp_valid_users(snum), sizeof(pstring)-1);
577 StrnCpy(invalid, lp_invalid_users(snum), sizeof(pstring)-1);
579 pstring_sub(valid,"%S",lp_servicename(snum));
580 pstring_sub(invalid,"%S",lp_servicename(snum));
582 ret = !user_in_list(user,invalid);
584 if (ret && valid && *valid) {
585 ret = user_in_list(user,valid);
588 if (ret && lp_onlyuser(snum)) {
589 char *user_list = lp_username(snum);
590 pstring_sub(user_list,"%S",lp_servicename(snum));
591 ret = user_in_list(user,user_list);
600 /****************************************************************************
601 validate a group username entry. Return the username or NULL
602 ****************************************************************************/
603 static char *validate_group(char *group,char *password,int pwlen,int snum)
607 char *host, *user, *domain;
609 while (getnetgrent(&host, &user, &domain)) {
611 if (user_ok(user, snum) &&
612 password_ok(user,password,pwlen,NULL)) {
626 while ((gptr = (struct group *)getgrent())) {
627 if (strequal(gptr->gr_name,group))
632 * As user_ok can recurse doing a getgrent(), we must
633 * copy the member list into a pstring on the stack before
634 * use. Bug pointed out by leon@eatworms.swmed.edu.
640 size_t copied_len = 0;
644 member = member_list;
646 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
647 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
648 if( copied_len + member_len < sizeof(pstring)) {
650 DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
652 safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
653 copied_len += member_len;
654 member += copied_len;
662 member = member_list;
665 fstrcpy(name,member);
666 if (user_ok(name,snum) &&
667 password_ok(name,password,pwlen,NULL)) {
672 DEBUG(10,("validate_group = member = %s\n", member));
674 member += strlen(member) + 1;
687 /****************************************************************************
688 check for authority to login to a service with a given username/password
689 ****************************************************************************/
690 BOOL authorise_login(int snum,char *user,char *password, int pwlen,
691 BOOL *guest,BOOL *force,uint16 vuid)
698 DEBUG(100,("checking authorisation on user=%s pass=%s\n",user,password));
701 /* there are several possibilities:
702 1) login as the given user with given password
703 2) login as a previously registered username with the given password
704 3) login as a session list username with the given password
705 4) login as a previously validated user/password pair
706 5) login as the "user =" user with given password
707 6) login as the "user =" user with no password (guest connection)
708 7) login as guest user with no password
710 if the service is guest_only then steps 1 to 5 are skipped
713 if (GUEST_ONLY(snum)) *force = True;
715 if (!(GUEST_ONLY(snum) && GUEST_OK(snum)))
718 user_struct *vuser = get_valid_user_struct(vuid);
720 /* check the given username and password */
721 if (!ok && (*user) && user_ok(user,snum)) {
722 ok = password_ok(user,password, pwlen, NULL);
723 if (ok) DEBUG(3,("ACCEPTED: given username password ok\n"));
726 /* check for a previously registered guest username */
727 if (!ok && (vuser != 0) && vuser->guest) {
728 if (user_ok(vuser->user.unix_name,snum) &&
729 password_ok(vuser->user.unix_name, password, pwlen, NULL)) {
730 fstrcpy(user, vuser->user.unix_name);
731 vuser->guest = False;
732 DEBUG(3,("ACCEPTED: given password with registered user %s\n", user));
738 /* now check the list of session users */
742 char *user_list = strdup(session_users);
743 if (!user_list) return(False);
745 for (auser=strtok(user_list,LIST_SEP);
747 auser = strtok(NULL,LIST_SEP))
750 fstrcpy(user2,auser);
751 if (!user_ok(user2,snum)) continue;
753 if (password_ok(user2,password, pwlen, NULL)) {
756 DEBUG(3,("ACCEPTED: session list username and given password ok\n"));
762 /* check for a previously validated username/password pair */
763 if (!ok && (!lp_revalidate(snum) || lp_security() > SEC_SHARE) &&
764 (vuser != 0) && !vuser->guest &&
765 user_ok(vuser->user.unix_name,snum)) {
766 fstrcpy(user,vuser->user.unix_name);
768 DEBUG(3,("ACCEPTED: validated uid ok as non-guest\n"));
772 /* check for a rhosts entry */
773 if (!ok && user_ok(user,snum) && check_hosts_equiv(user)) {
775 DEBUG(3,("ACCEPTED: hosts equiv or rhosts entry\n"));
778 /* check the user= fields and the given password */
779 if (!ok && lp_username(snum)) {
782 StrnCpy(user_list,lp_username(snum),sizeof(pstring));
784 pstring_sub(user_list,"%S",lp_servicename(snum));
786 for (auser=strtok(user_list,LIST_SEP);
788 auser = strtok(NULL,LIST_SEP))
792 auser = validate_group(auser+1,password,pwlen,snum);
797 DEBUG(3,("ACCEPTED: group username and given password ok\n"));
803 fstrcpy(user2,auser);
804 if (user_ok(user2,snum) &&
805 password_ok(user2,password,pwlen,NULL))
809 DEBUG(3,("ACCEPTED: user list username and given password ok\n"));
814 } /* not guest only */
816 /* check for a normal guest connection */
817 if (!ok && GUEST_OK(snum))
820 StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
821 if (Get_Pwnam(guestname,True))
823 fstrcpy(user,guestname);
825 DEBUG(3,("ACCEPTED: guest account and guest ok\n"));
828 DEBUG(0,("Invalid guest account %s??\n",guestname));
833 if (ok && !user_ok(user,snum))
835 DEBUG(0,("rejected invalid user %s\n",user));
843 /****************************************************************************
844 read the a hosts.equiv or .rhosts file and check if it
845 allows this user from this machine
846 ****************************************************************************/
847 static BOOL check_user_equiv(char *user, char *remote, char *equiv_file)
849 int plus_allowed = 1;
852 char **lines = file_lines_load(equiv_file, NULL);
855 DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
856 if (! lines) return False;
857 for (i=0; lines[i]; i++) {
858 char *buf = lines[i];
859 trim_string(buf," "," ");
861 if (buf[0] != '#' && buf[0] != '\n')
863 BOOL is_group = False;
866 if (strcmp(buf, "NO_PLUS\n") == 0)
868 DEBUG(6, ("check_user_equiv NO_PLUS\n"));
875 if (*bp == '\n' && plus_allowed)
877 /* a bare plus means everbody allowed */
878 DEBUG(6, ("check_user_equiv everybody allowed\n"));
879 file_lines_free(lines);
883 else if (buf[0] == '-')
893 file_host = strtok(bp, " \t\n");
894 file_user = strtok(NULL, " \t\n");
895 DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)",
896 file_user ? file_user : "(null)" ));
897 if (file_host && *file_host)
899 BOOL host_ok = False;
901 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
904 static char *mydomain = NULL;
906 yp_get_default_domain(&mydomain);
907 if (mydomain && innetgr(file_host,remote,user,mydomain))
913 DEBUG(1,("Netgroups not configured\n"));
918 /* is it this host */
919 /* the fact that remote has come from a call of gethostbyaddr
920 * means that it may have the fully qualified domain name
921 * so we could look up the file version to get it into
922 * a canonical form, but I would rather just type it
923 * in full in the equiv file
925 if (!host_ok && !is_group && strequal(remote, file_host))
931 /* is it this user */
932 if (file_user == 0 || strequal(user, file_user))
934 DEBUG(5, ("check_user_equiv matched %s%s %s\n",
935 (plus ? "+" : "-"), file_host,
936 (file_user ? file_user : "")));
937 file_lines_free(lines);
938 return (plus ? True : False);
944 file_lines_free(lines);
949 /****************************************************************************
950 check for a possible hosts equiv or rhosts entry for the user
951 ****************************************************************************/
952 BOOL check_hosts_equiv(char *user)
956 struct passwd *pass = Get_Pwnam(user,True);
961 fname = lp_hosts_equiv();
963 /* note: don't allow hosts.equiv on root */
964 if (fname && *fname && (pass->pw_uid != 0)) {
965 if (check_user_equiv(user,client_name(),fname))
971 char *home = get_user_home_dir(user);
973 slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
974 if (check_user_equiv(user,client_name(),rhostsfile))
983 /****************************************************************************
984 Return the client state structure.
985 ****************************************************************************/
987 struct cli_state *server_client(void)
989 static struct cli_state pw_cli;
993 /****************************************************************************
994 Support for server level security.
995 ****************************************************************************/
997 struct cli_state *server_cryptkey(void)
999 struct cli_state *cli;
1001 struct in_addr dest_ip;
1003 BOOL connected_ok = False;
1005 cli = server_client();
1007 if (!cli_initialise(cli))
1010 pserver = strdup(lp_passwordserver());
1013 while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
1014 standard_sub_basic(desthost);
1017 if(!resolve_name( desthost, &dest_ip, 0x20)) {
1018 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
1022 if (ismyip(dest_ip)) {
1023 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
1027 if (cli_connect(cli, desthost, &dest_ip)) {
1028 DEBUG(3,("connected to password server %s\n",desthost));
1029 connected_ok = True;
1036 if (!connected_ok) {
1037 DEBUG(0,("password server not available\n"));
1042 if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
1045 DEBUG(3,("got session\n"));
1047 if (!cli_negprot(cli)) {
1048 DEBUG(1,("%s rejected the negprot\n",desthost));
1053 if (cli->protocol < PROTOCOL_LANMAN2 ||
1054 !(cli->sec_mode & 1)) {
1055 DEBUG(1,("%s isn't in user level security mode\n",desthost));
1060 DEBUG(3,("password server OK\n"));
1065 /****************************************************************************
1066 Validate a password with the password server.
1067 ****************************************************************************/
1069 BOOL server_validate(char *user, char *domain,
1070 char *pass, int passlen,
1071 char *ntpass, int ntpasslen)
1073 struct cli_state *cli;
1074 static unsigned char badpass[24];
1075 static BOOL tested_password_server = False;
1076 static BOOL bad_password_server = False;
1078 cli = server_client();
1080 if (!cli->initialised) {
1081 DEBUG(1,("password server %s is not connected\n", cli->desthost));
1086 memset(badpass, 0x1f, sizeof(badpass));
1088 if((passlen == sizeof(badpass)) && !memcmp(badpass, pass, passlen)) {
1090 * Very unlikely, our random bad password is the same as the users
1092 memset(badpass, badpass[0]+1, sizeof(badpass));
1096 * Attempt a session setup with a totally incorrect password.
1097 * If this succeeds with the guest bit *NOT* set then the password
1098 * server is broken and is not correctly setting the guest bit. We
1099 * need to detect this as some versions of NT4.x are broken. JRA.
1102 if(!tested_password_server) {
1103 if (cli_session_setup(cli, user, (char *)badpass, sizeof(badpass),
1104 (char *)badpass, sizeof(badpass), domain)) {
1107 * We connected to the password server so we
1108 * can say we've tested it.
1110 tested_password_server = True;
1112 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
1113 DEBUG(0,("server_validate: password server %s allows users as non-guest \
1114 with a bad password.\n", cli->desthost));
1115 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
1116 use this machine as the password server.\n"));
1120 * Password server has the bug.
1122 bad_password_server = True;
1130 * We have already tested the password server.
1131 * Fail immediately if it has the bug.
1134 if(bad_password_server) {
1135 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
1136 with a bad password.\n", cli->desthost));
1137 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
1138 use this machine as the password server.\n"));
1144 * Now we know the password server will correctly set the guest bit, or is
1145 * not guest enabled, we can try with the real password.
1148 if (!cli_session_setup(cli, user, pass, passlen, ntpass, ntpasslen, domain)) {
1149 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
1153 /* if logged in as guest then reject */
1154 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
1155 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
1165 /***********************************************************************
1166 Connect to a remote machine for domain security authentication
1167 given a name or IP address.
1168 ************************************************************************/
1170 static BOOL connect_to_domain_password_server(struct cli_state *pcli, char *remote_machine,
1171 unsigned char *trust_passwd)
1173 struct in_addr dest_ip;
1175 if(cli_initialise(pcli) == False) {
1176 DEBUG(0,("connect_to_domain_password_server: unable to initialize client connection.\n"));
1180 standard_sub_basic(remote_machine);
1181 strupper(remote_machine);
1183 if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
1184 DEBUG(1,("connect_to_domain_password_server: Can't resolve address for %s\n", remote_machine));
1189 if (ismyip(dest_ip)) {
1190 DEBUG(1,("connect_to_domain_password_server: Password server loop - not using password server %s\n",
1196 if (!cli_connect(pcli, remote_machine, &dest_ip)) {
1197 DEBUG(0,("connect_to_domain_password_server: unable to connect to SMB server on \
1198 machine %s. Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1203 if (!attempt_netbios_session_request(pcli, global_myname, remote_machine, &dest_ip)) {
1204 DEBUG(0,("connect_to_password_server: machine %s rejected the NetBIOS \
1205 session request. Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1209 pcli->protocol = PROTOCOL_NT1;
1211 if (!cli_negprot(pcli)) {
1212 DEBUG(0,("connect_to_domain_password_server: machine %s rejected the negotiate protocol. \
1213 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1218 if (pcli->protocol != PROTOCOL_NT1) {
1219 DEBUG(0,("connect_to_domain_password_server: machine %s didn't negotiate NT protocol.\n",
1226 * Do an anonymous session setup.
1229 if (!cli_session_setup(pcli, "", "", 0, "", 0, "")) {
1230 DEBUG(0,("connect_to_domain_password_server: machine %s rejected the session setup. \
1231 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1236 if (!(pcli->sec_mode & 1)) {
1237 DEBUG(1,("connect_to_domain_password_server: machine %s isn't in user level security mode\n",
1243 if (!cli_send_tconX(pcli, "IPC$", "IPC", "", 1)) {
1244 DEBUG(0,("connect_to_domain_password_server: machine %s rejected the tconX on the IPC$ share. \
1245 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1251 * We now have an anonymous connection to IPC$ on the domain password server.
1255 * Even if the connect succeeds we need to setup the netlogon
1256 * pipe here. We do this as we may just have changed the domain
1257 * account password on the PDC and yet we may be talking to
1258 * a BDC that doesn't have this replicated yet. In this case
1259 * a successful connect to a DC needs to take the netlogon connect
1260 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
1263 if(cli_nt_session_open(pcli, PIPE_NETLOGON) == False) {
1264 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
1265 machine %s. Error was : %s.\n", remote_machine, cli_errstr(pcli)));
1266 cli_nt_session_close(pcli);
1272 if (cli_nt_setup_creds(pcli, trust_passwd) == False) {
1273 DEBUG(0,("connect_to_domain_password_server: unable to setup the PDC credentials to machine \
1274 %s. Error was : %s.\n", remote_machine, cli_errstr(pcli)));
1275 cli_nt_session_close(pcli);
1284 /***********************************************************************
1285 Utility function to attempt a connection to an IP address of a DC.
1286 ************************************************************************/
1288 static BOOL attempt_connect_to_dc(struct cli_state *pcli, struct in_addr *ip, unsigned char *trust_passwd)
1293 * Ignore addresses we have already tried.
1296 if (ip_equal(ipzero, *ip))
1299 if (!lookup_pdc_name(global_myname, lp_workgroup(), ip, dc_name))
1302 return connect_to_domain_password_server(pcli, dc_name, trust_passwd);
1307 /***********************************************************************
1308 We have been asked to dynamcially determine the IP addresses of
1309 the PDC and BDC's for this DOMAIN, and query them in turn.
1310 ************************************************************************/
1311 static BOOL find_connect_pdc(struct cli_state *pcli, unsigned char *trust_passwd)
1313 struct in_addr *ip_list = NULL;
1316 BOOL connected_ok = False;
1318 if (!get_dc_list(lp_workgroup(), &ip_list, &count))
1322 * Firstly try and contact a PDC/BDC who has the same
1323 * network address as any of our interfaces.
1325 for(i = 0; i < count; i++) {
1326 if(!is_local_net(ip_list[i]))
1329 if((connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd)))
1332 ip_list[i] = ipzero; /* Tried and failed. */
1336 * Secondly try and contact a random PDC/BDC.
1339 i = (sys_random() % count);
1341 if (!(connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd)))
1342 ip_list[i] = ipzero; /* Tried and failed. */
1346 * Finally go through the IP list in turn, ignoring any addresses
1347 * we have already tried.
1351 * Try and connect to any of the other IP addresses in the PDC/BDC list.
1352 * Note that from a WINS server the #1 IP address is the PDC.
1354 for(i = 0; i < count; i++) {
1355 if((connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd)))
1361 free((char *)ip_list);
1364 return connected_ok;
1369 /***********************************************************************
1370 Do the same as security=server, but using NT Domain calls and a session
1371 key from the machine password.
1372 ************************************************************************/
1374 BOOL domain_client_validate( char *user, char *domain,
1375 char *smb_apasswd, int smb_apasslen,
1376 char *smb_ntpasswd, int smb_ntpasslen,
1379 unsigned char local_challenge[8];
1380 unsigned char local_lm_response[24];
1381 unsigned char local_nt_reponse[24];
1382 unsigned char trust_passwd[16];
1383 fstring remote_machine;
1385 NET_ID_INFO_CTR ctr;
1386 NET_USER_INFO_3 info3;
1387 struct cli_state cli;
1389 BOOL connected_ok = False;
1391 if(user_exists != NULL)
1392 *user_exists = True; /* Only set false on a very specific error. */
1395 * Check that the requested domain is not our own machine name.
1396 * If it is, we should never check the PDC here, we use our own local
1400 if(strequal( domain, global_myname)) {
1401 DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
1406 * Next, check that the passwords given were encrypted.
1409 if(((smb_apasslen != 24) && (smb_apasslen != 0)) ||
1410 ((smb_ntpasslen != 24) && (smb_ntpasslen != 0))) {
1413 * Not encrypted - do so.
1416 DEBUG(3,("domain_client_validate: User passwords not in encrypted format.\n"));
1417 generate_random_buffer( local_challenge, 8, False);
1418 SMBencrypt( (uchar *)smb_apasswd, local_challenge, local_lm_response);
1419 SMBNTencrypt((uchar *)smb_ntpasswd, local_challenge, local_nt_reponse);
1422 smb_apasswd = (char *)local_lm_response;
1423 smb_ntpasswd = (char *)local_nt_reponse;
1427 * Encrypted - get the challenge we sent for these
1431 if (!last_challenge(local_challenge)) {
1432 DEBUG(0,("domain_client_validate: no challenge done - password failed\n"));
1438 * Get the machine account password.
1440 if (!get_trust_account_password(domain, trust_passwd, NULL))
1446 * At this point, smb_apasswd points to the lanman response to
1447 * the challenge in local_challenge, and smb_ntpasswd points to
1448 * the NT response to the challenge in local_challenge. Ship
1449 * these over the secure channel to a domain controller and
1450 * see if they were valid.
1456 * Treat each name in the 'password server =' line as a potential
1457 * PDC/BDC. Contact each in turn and try and authenticate.
1460 pserver = strdup(lp_passwordserver());
1463 while (!connected_ok &&
1464 next_token(&p,remote_machine,LIST_SEP,sizeof(remote_machine))) {
1465 if(strequal(remote_machine, "*")) {
1466 connected_ok = find_connect_pdc(&cli, trust_passwd);
1468 connected_ok = connect_to_domain_password_server(&cli, remote_machine, trust_passwd);
1474 if (!connected_ok) {
1475 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
1480 /* We really don't care what LUID we give the user. */
1481 generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
1483 if(cli_nt_login_network(&cli, domain, user, smb_uid_low, (char *)local_challenge,
1484 ((smb_apasslen != 0) ? smb_apasswd : NULL),
1485 ((smb_ntpasslen != 0) ? smb_ntpasswd : NULL),
1486 &ctr, &info3) == False) {
1489 cli_error(&cli, NULL, NULL, &nt_rpc_err);
1490 DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
1491 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1492 cli_nt_session_close(&cli);
1496 if((nt_rpc_err == NT_STATUS_NO_SUCH_USER) && (user_exists != NULL))
1497 *user_exists = False;
1503 * Here, if we really want it, we have lots of info about the user in info3.
1508 * We don't actually need to do this - plus it fails currently with
1509 * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
1513 if(cli_nt_logoff(&cli, &ctr) == False) {
1514 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
1515 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1516 cli_nt_session_close(&cli);
1523 cli_nt_session_close(&cli);