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 BOOL global_machine_pasword_needs_changing;
29 /* users from session setup */
30 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++) + getpid() + tval.tv_sec;
59 v2 = (counter++) * 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;
129 /* same number of igroups as groups */
133 free((char *)vuser->groups);
136 free((char *)vuser->sids);
139 vuser->groups = NULL;
143 /****************************************************************************
144 return a validated username
145 ****************************************************************************/
146 char *validated_username(uint16 vuid)
148 user_struct *vuser = get_valid_user_struct(vuid);
155 /****************************************************************************
156 Setup the groups a user belongs to.
157 ****************************************************************************/
158 int setup_groups(char *user, uid_t uid, gid_t gid, int *p_ngroups, gid_t **p_groups)
162 gid_t *groups = NULL;
164 if (-1 == initgroups(user,gid)) {
166 DEBUG(0,("Unable to initgroups!\n"));
167 if (gid < 0 || gid > 16000 || uid < 0 || uid > 16000) {
168 DEBUG(0,("This is probably a problem with the account %s\n",
175 ngroups = sys_getgroups(0,&grp);
179 if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL) {
180 DEBUG(0,("setup_groups malloc fail !\n"));
184 ngroups = sys_getgroups(ngroups,groups);
186 (*p_ngroups) = ngroups;
187 (*p_groups) = groups;
189 DEBUG( 3, ( "%s is in %d groups: ", user, ngroups ) );
190 for (i = 0; i < ngroups; i++ ) {
191 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
193 DEBUG( 3, ( "\n" ) );
199 /****************************************************************************
200 register a uid/name pair as being valid and that a valid password
201 has been given. vuid is biased by an offset. This allows us to
202 tell random client vuid's (normally zero) from valid vuids.
203 ****************************************************************************/
204 uint16 register_vuid(uid_t uid,gid_t gid, char *unix_name, char *requested_name, BOOL guest)
207 struct passwd *pwfile; /* for getting real name from passwd file */
209 /* Ensure no vuid gets registered in share level security. */
210 if(lp_security() == SEC_SHARE)
211 return UID_FIELD_INVALID;
215 * After observing MS-Exchange services writing to a Samba share
216 * I belive this code is incorrect. Each service does its own
217 * sessionsetup_and_X for the same user, and as each service shuts
218 * down, it does a user_logoff_and_X. As we are consolidating multiple
219 * sessionsetup_and_X's onto the same vuid here, when the first service
220 * shuts down, it invalidates all the open files for the other services.
221 * Hence I am removing this code and forcing each sessionsetup_and_X
223 * Jeremy Allison. (jallison@whistle.com).
227 for(i = 0; i < num_validated_users; i++) {
228 vuser = &validated_users[i];
229 if ( vuser->uid == uid )
230 return (uint16)(i + VUID_OFFSET); /* User already validated */
234 validated_users = (user_struct *)Realloc(validated_users,
236 (num_validated_users+1));
238 if (!validated_users)
240 DEBUG(0,("Failed to realloc users struct!\n"));
241 num_validated_users = 0;
242 return UID_FIELD_INVALID;
245 vuser = &validated_users[num_validated_users];
246 num_validated_users++;
250 vuser->guest = guest;
251 fstrcpy(vuser->name,unix_name);
252 fstrcpy(vuser->requested_name,requested_name);
258 vuser->groups = NULL;
260 /* Find all the groups this uid is in and store them.
261 Used by become_user() */
262 setup_groups(unix_name,uid,gid,
266 DEBUG(3,("uid %d registered to name %s\n",(int)uid,unix_name));
268 DEBUG(3, ("Clearing default real name\n"));
269 fstrcpy(vuser->real_name, "<Full Name>\0");
270 if (lp_unix_realname()) {
271 if ((pwfile=getpwnam(vuser->name))!= NULL)
273 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->name,pwfile->pw_gecos));
274 fstrcpy(vuser->real_name, pwfile->pw_gecos);
278 return (uint16)((num_validated_users - 1) + VUID_OFFSET);
282 /****************************************************************************
283 add a name to the session users list
284 ****************************************************************************/
285 void add_session_user(char *user)
288 StrnCpy(suser,user,sizeof(suser)-1);
290 if (!Get_Pwnam(suser,True)) return;
292 if (suser && *suser && !in_list(suser,session_users,False))
294 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
295 DEBUG(1,("Too many session users??\n"));
298 pstrcat(session_users," ");
299 pstrcat(session_users,suser);
305 /****************************************************************************
306 update the encrypted smbpasswd file from the plaintext username and password
307 *****************************************************************************/
308 static BOOL update_smbpassword_file(char *user, char *password)
310 struct smb_passwd *smbpw;
314 smbpw = getsmbpwnam(user);
318 DEBUG(0,("getsmbpwnam returned NULL\n"));
322 /* Here, the flag is one, because we want to ignore the
323 XXXXXXX'd out password */
324 ret = change_oem_password( smbpw, password, True);
326 DEBUG(3,("change_oem_password returned False\n"));
336 /****************************************************************************
337 core of smb password checking routine.
338 ****************************************************************************/
339 BOOL smb_password_check(char *password, unsigned char *part_passwd, unsigned char *c8)
341 /* Finish the encryption of part_passwd. */
342 unsigned char p21[21];
343 unsigned char p24[24];
345 if (part_passwd == NULL)
346 DEBUG(10,("No password set - allowing access\n"));
347 /* No password set - always true ! */
348 if (part_passwd == NULL)
352 memcpy(p21,part_passwd,16);
357 DEBUG(100,("Part password (P16) was |"));
358 for(i = 0; i < 16; i++)
359 DEBUG(100,("%X ", (unsigned char)part_passwd[i]));
361 DEBUG(100,("Password from client was |"));
362 for(i = 0; i < 24; i++)
363 DEBUG(100,("%X ", (unsigned char)password[i]));
365 DEBUG(100,("Given challenge was |"));
366 for(i = 0; i < 8; i++)
367 DEBUG(100,("%X ", (unsigned char)c8[i]));
369 DEBUG(100,("Value from encryption was |"));
370 for(i = 0; i < 24; i++)
371 DEBUG(100,("%X ", (unsigned char)p24[i]));
375 return (memcmp(p24, password, 24) == 0);
378 /****************************************************************************
379 Do a specific test for an smb password being correct, given a smb_password and
380 the lanman and NT responses.
381 ****************************************************************************/
382 BOOL smb_password_ok(struct smb_passwd *smb_pass,
383 uchar lm_pass[24], uchar nt_pass[24])
387 if (!lm_pass || !smb_pass) return(False);
389 if(smb_pass->acct_ctrl & ACB_DISABLED) {
390 DEBUG(3,("account for user %s was disabled.\n",
391 smb_pass->smb_name));
395 if (!last_challenge(challenge)) {
396 DEBUG(1,("no challenge done - password failed\n"));
400 DEBUG(4,("Checking SMB password for user %s\n",
401 smb_pass->smb_name));
403 if ((Protocol >= PROTOCOL_NT1) && (smb_pass->smb_nt_passwd != NULL)) {
404 /* We have the NT MD4 hash challenge available - see if we can
405 use it (ie. does it exist in the smbpasswd file).
407 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
408 if (smb_password_check((char *)nt_pass,
409 (uchar *)smb_pass->smb_nt_passwd,
411 DEBUG(4,("NT MD4 password check succeeded\n"));
414 DEBUG(4,("NT MD4 password check failed\n"));
417 /* Try against the lanman password. smb_pass->smb_passwd == NULL means
418 no password, allow access. */
420 DEBUG(4,("Checking LM MD4 password\n"));
422 if((smb_pass->smb_passwd == NULL) &&
423 (smb_pass->acct_ctrl & ACB_PWNOTREQ)) {
424 DEBUG(4,("no password required for user %s\n",
425 smb_pass->smb_name));
429 if((smb_pass->smb_passwd != NULL) &&
430 smb_password_check((char *)lm_pass,
431 (uchar *)smb_pass->smb_passwd, challenge)) {
432 DEBUG(4,("LM MD4 password check succeeded\n"));
436 DEBUG(4,("LM MD4 password check failed\n"));
442 /****************************************************************************
443 check if a username/password is OK assuming the password is a 24 byte
445 return True if the password is correct, False otherwise
446 ****************************************************************************/
447 static BOOL pass_check_smb(char *user,char *password, struct passwd *pwd)
451 struct smb_passwd *smb_pass;
458 challenge_done = last_challenge(challenge);
460 if (!challenge_done) {
461 DEBUG(0,("Error: challenge not done for user=%s\n", user));
466 pass = (struct passwd *) pwd;
467 user = pass->pw_name;
469 pass = Get_Pwnam(user,True);
473 DEBUG(3,("Couldn't find user %s\n",user));
477 smb_pass = getsmbpwnam(user);
480 DEBUG(3,("Couldn't find user %s in smb_passwd file.\n", user));
484 /* Quit if the account was disabled. */
485 if(smb_pass->acct_ctrl & ACB_DISABLED) {
486 DEBUG(3,("account for user %s was disabled.\n", user));
490 /* Ensure the uid's match */
491 if (smb_pass->smb_userid != pass->pw_uid) {
492 DEBUG(3,("Error : UNIX and SMB uids in password files do not match !\n"));
496 if (smb_password_ok(smb_pass,
497 (unsigned char *)password,
498 (uchar *)password)) {
502 DEBUG(3,("Error smb_password_check failed\n"));
506 /****************************************************************************
507 check if a username/password pair is OK either via the system password
508 database or the encrypted SMB password database
509 return True if the password is correct, False otherwise
510 ****************************************************************************/
511 BOOL password_ok(char *user,char *password, int pwlen, struct passwd *pwd)
514 /* if it is 24 bytes long then assume it is an encrypted
516 return pass_check_smb(user, password, pwd);
519 return pass_check(user, password, pwlen, pwd,
520 lp_update_encrypted() ?
521 update_smbpassword_file : NULL);
524 /****************************************************************************
525 check if a username is valid
526 ****************************************************************************/
527 BOOL user_ok(char *user,int snum)
529 pstring valid, invalid;
532 StrnCpy(valid, lp_valid_users(snum), sizeof(pstring));
533 StrnCpy(invalid, lp_invalid_users(snum), sizeof(pstring));
535 string_sub(valid,"%S",lp_servicename(snum));
536 string_sub(invalid,"%S",lp_servicename(snum));
538 ret = !user_in_list(user,invalid);
540 if (ret && valid && *valid) {
541 ret = user_in_list(user,valid);
544 if (ret && lp_onlyuser(snum)) {
545 char *user_list = lp_username(snum);
546 string_sub(user_list,"%S",lp_servicename(snum));
547 ret = user_in_list(user,user_list);
556 /****************************************************************************
557 validate a group username entry. Return the username or NULL
558 ****************************************************************************/
559 static char *validate_group(char *group,char *password,int pwlen,int snum)
563 char *host, *user, *domain;
565 while (getnetgrent(&host, &user, &domain)) {
567 if (user_ok(user, snum) &&
568 password_ok(user,password,pwlen,NULL)) {
580 struct group *gptr = (struct group *)getgrnam(group);
584 member = gptr->gr_mem;
585 while (member && *member)
588 fstrcpy(name,*member);
589 if (user_ok(name,snum) &&
590 password_ok(name,password,pwlen,NULL))
594 #ifdef GROUP_CHECK_PWENT
600 while (pwd = getpwent ()) {
601 if (*(pwd->pw_passwd) && pwd->pw_gid == gptr->gr_gid) {
602 /* This Entry have PASSWORD and same GID then check pwd */
603 if (password_ok(NULL, password, pwlen, pwd)) {
604 fstrcpy(tm, pwd->pw_name);
612 #endif /* GROUP_CHECK_PWENT */
621 /****************************************************************************
622 check for authority to login to a service with a given username/password
623 ****************************************************************************/
624 BOOL authorise_login(int snum,char *user,char *password, int pwlen,
625 BOOL *guest,BOOL *force,uint16 vuid)
632 DEBUG(100,("checking authorisation on user=%s pass=%s\n",user,password));
635 /* there are several possibilities:
636 1) login as the given user with given password
637 2) login as a previously registered username with the given password
638 3) login as a session list username with the given password
639 4) login as a previously validated user/password pair
640 5) login as the "user =" user with given password
641 6) login as the "user =" user with no password (guest connection)
642 7) login as guest user with no password
644 if the service is guest_only then steps 1 to 5 are skipped
647 if (GUEST_ONLY(snum)) *force = True;
649 if (!(GUEST_ONLY(snum) && GUEST_OK(snum)))
652 user_struct *vuser = get_valid_user_struct(vuid);
654 /* check the given username and password */
655 if (!ok && (*user) && user_ok(user,snum)) {
656 ok = password_ok(user,password, pwlen, NULL);
657 if (ok) DEBUG(3,("ACCEPTED: given username password ok\n"));
660 /* check for a previously registered guest username */
661 if (!ok && (vuser != 0) && vuser->guest) {
662 if (user_ok(vuser->name,snum) &&
663 password_ok(vuser->name, password, pwlen, NULL)) {
664 fstrcpy(user, vuser->name);
665 vuser->guest = False;
666 DEBUG(3,("ACCEPTED: given password with registered user %s\n", user));
672 /* now check the list of session users */
676 char *user_list = strdup(session_users);
677 if (!user_list) return(False);
679 for (auser=strtok(user_list,LIST_SEP);
681 auser = strtok(NULL,LIST_SEP))
684 fstrcpy(user2,auser);
685 if (!user_ok(user2,snum)) continue;
687 if (password_ok(user2,password, pwlen, NULL)) {
690 DEBUG(3,("ACCEPTED: session list username and given password ok\n"));
696 /* check for a previously validated username/password pair */
697 if (!ok && (!lp_revalidate(snum) || lp_security() > SEC_SHARE) &&
698 (vuser != 0) && !vuser->guest &&
699 user_ok(vuser->name,snum)) {
700 fstrcpy(user,vuser->name);
702 DEBUG(3,("ACCEPTED: validated uid ok as non-guest\n"));
706 /* check for a rhosts entry */
707 if (!ok && user_ok(user,snum) && check_hosts_equiv(user)) {
709 DEBUG(3,("ACCEPTED: hosts equiv or rhosts entry\n"));
712 /* check the user= fields and the given password */
713 if (!ok && lp_username(snum)) {
716 StrnCpy(user_list,lp_username(snum),sizeof(pstring));
718 string_sub(user_list,"%S",lp_servicename(snum));
720 for (auser=strtok(user_list,LIST_SEP);
722 auser = strtok(NULL,LIST_SEP))
726 auser = validate_group(auser+1,password,pwlen,snum);
731 DEBUG(3,("ACCEPTED: group username and given password ok\n"));
737 fstrcpy(user2,auser);
738 if (user_ok(user2,snum) &&
739 password_ok(user2,password,pwlen,NULL))
743 DEBUG(3,("ACCEPTED: user list username and given password ok\n"));
748 } /* not guest only */
750 /* check for a normal guest connection */
751 if (!ok && GUEST_OK(snum))
754 StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
755 if (Get_Pwnam(guestname,True))
757 fstrcpy(user,guestname);
759 DEBUG(3,("ACCEPTED: guest account and guest ok\n"));
762 DEBUG(0,("Invalid guest account %s??\n",guestname));
767 if (ok && !user_ok(user,snum))
769 DEBUG(0,("rejected invalid user %s\n",user));
777 /****************************************************************************
778 read the a hosts.equiv or .rhosts file and check if it
779 allows this user from this machine
780 ****************************************************************************/
781 static BOOL check_user_equiv(char *user, char *remote, char *equiv_file)
784 int plus_allowed = 1;
787 FILE *fp = fopen(equiv_file, "r");
788 DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
789 if (! fp) return False;
790 while(fgets(buf, sizeof(buf), fp))
792 trim_string(buf," "," ");
794 if (buf[0] != '#' && buf[0] != '\n')
796 BOOL is_group = False;
799 if (strcmp(buf, "NO_PLUS\n") == 0)
801 DEBUG(6, ("check_user_equiv NO_PLUS\n"));
808 if (*bp == '\n' && plus_allowed)
810 /* a bare plus means everbody allowed */
811 DEBUG(6, ("check_user_equiv everybody allowed\n"));
816 else if (buf[0] == '-')
826 file_host = strtok(bp, " \t\n");
827 file_user = strtok(NULL, " \t\n");
828 DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)",
829 file_user ? file_user : "(null)" ));
830 if (file_host && *file_host)
832 BOOL host_ok = False;
834 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
837 static char *mydomain = NULL;
839 yp_get_default_domain(&mydomain);
840 if (mydomain && innetgr(file_host,remote,user,mydomain))
846 DEBUG(1,("Netgroups not configured\n"));
851 /* is it this host */
852 /* the fact that remote has come from a call of gethostbyaddr
853 * means that it may have the fully qualified domain name
854 * so we could look up the file version to get it into
855 * a canonical form, but I would rather just type it
856 * in full in the equiv file
858 if (!host_ok && !is_group && strequal(remote, file_host))
864 /* is it this user */
865 if (file_user == 0 || strequal(user, file_user))
868 DEBUG(5, ("check_user_equiv matched %s%s %s\n",
869 (plus ? "+" : "-"), file_host,
870 (file_user ? file_user : "")));
871 return (plus ? True : False);
882 /****************************************************************************
883 check for a possible hosts equiv or rhosts entry for the user
884 ****************************************************************************/
885 BOOL check_hosts_equiv(char *user)
889 struct passwd *pass = Get_Pwnam(user,True);
894 fname = lp_hosts_equiv();
896 /* note: don't allow hosts.equiv on root */
897 if (fname && *fname && (pass->pw_uid != 0)) {
899 if (check_user_equiv(user,client_name(Client),fname))
905 char *home = get_home_dir(user);
908 slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
909 if (check_user_equiv(user,client_name(Client),rhostsfile))
918 /****************************************************************************
919 return the client state structure
920 ****************************************************************************/
921 struct cli_state *server_client(void)
923 static struct cli_state pw_cli;
927 /****************************************************************************
928 support for server level security
929 ****************************************************************************/
930 struct cli_state *server_cryptkey(void)
932 struct cli_state *cli;
934 struct in_addr dest_ip;
935 extern fstring local_machine;
937 BOOL connected_ok = False;
938 struct nmb_name calling, called;
940 cli = server_client();
942 if (!cli_initialise(cli))
945 p = lp_passwordserver();
946 while(p && next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
947 standard_sub_basic(desthost);
950 if(!resolve_name( desthost, &dest_ip, 0x20)) {
951 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
955 if (ismyip(dest_ip)) {
956 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
960 if (cli_connect(cli, desthost, &dest_ip)) {
961 DEBUG(3,("connected to password server %s\n",desthost));
968 DEBUG(0,("password server not available\n"));
973 make_nmb_name(&calling, local_machine, 0x0 , scope);
974 make_nmb_name(&called , desthost , 0x20, scope);
976 if (!cli_session_request(cli, &calling, &called))
978 DEBUG(1,("%s rejected the session\n",desthost));
983 DEBUG(3,("got session\n"));
985 if (!cli_negprot(cli)) {
986 DEBUG(1,("%s rejected the negprot\n",desthost));
991 if (cli->protocol < PROTOCOL_LANMAN2 ||
992 !(cli->sec_mode & 1)) {
993 DEBUG(1,("%s isn't in user level security mode\n",desthost));
998 DEBUG(3,("password server OK\n"));
1003 /****************************************************************************
1004 validate a password with the password server
1005 ****************************************************************************/
1006 BOOL server_validate(char *user, char *domain,
1007 char *pass, int passlen,
1008 char *ntpass, int ntpasslen)
1010 struct cli_state *cli;
1011 extern fstring local_machine;
1012 static unsigned char badpass[24];
1013 cli = server_client();
1015 if (!cli->initialised) {
1016 DEBUG(1,("password server %s is not connected\n", cli->desthost));
1020 if(badpass[0] == 0) {
1021 memset(badpass, 0x1f, sizeof(badpass));
1024 if((passlen == sizeof(badpass)) && !memcmp(badpass, pass, passlen)) {
1025 /* Very unlikely, our random bad password is the same as the users
1027 memset(badpass, badpass[0]+1, sizeof(badpass));
1031 * Attempt a session setup with a totally incorrect password.
1032 * If this succeeds with the guest bit *NOT* set then the password
1033 * server is broken and is not correctly setting the guest bit. We
1034 * need to detect this as some versions of NT4.x are broken. JRA.
1037 if (cli_session_setup(cli, user, (char *)badpass, sizeof(badpass),
1038 (char *)badpass, sizeof(badpass), domain)) {
1039 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
1040 DEBUG(0,("server_validate: password server %s allows users as non-guest \
1041 with a bad password.\n", cli->desthost));
1042 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
1043 use this machine as the password server.\n"));
1051 * Now we know the password server will correctly set the guest bit, or is
1052 * not guest enabled, we can try with the real password.
1055 if (!cli_session_setup(cli, user, pass, passlen, ntpass, ntpasslen, domain)) {
1056 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
1060 /* if logged in as guest then reject */
1061 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
1062 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
1068 * This patch from Rob Nielsen <ran@adc.com> makes doing
1069 * the NetWksaUserLogon a dynamic, rather than compile-time
1070 * parameter, defaulting to on. This is somewhat dangerous
1071 * as it allows people to turn off this neccessary check,
1072 * but so many people have had problems with this that I
1073 * think it is a neccessary change. JRA.
1076 if (lp_net_wksta_user_logon()) {
1077 DEBUG(3,("trying NetWkstaUserLogon with password server %s\n", cli->desthost));
1079 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
1080 DEBUG(0,("password server %s refused IPC$ connect\n", cli->desthost));
1085 if (!cli_NetWkstaUserLogon(cli,user,local_machine)) {
1086 DEBUG(0,("password server %s failed NetWkstaUserLogon\n", cli->desthost));
1092 if (cli->privilages == 0) {
1093 DEBUG(0,("password server %s gave guest privilages\n", cli->desthost));
1099 if (!strequal(cli->eff_name, user)) {
1100 DEBUG(0,("password server %s gave different username %s\n",
1110 DEBUG(3,("skipping NetWkstaUserLogon with password server %s\n", cli->desthost));
1113 DEBUG(3,("password server %s accepted the password\n", cli->desthost));
1120 /***********************************************************************
1121 Do the same as security=server, but using NT Domain calls and a session
1122 key from the machine password.
1123 ************************************************************************/
1125 BOOL domain_client_validate( char *user, char *domain,
1126 char *smb_apasswd, int smb_apasslen,
1127 char *smb_ntpasswd, int smb_ntpasslen)
1129 unsigned char local_challenge[8];
1130 unsigned char local_lm_response[24];
1131 unsigned char local_nt_reponse[24];
1132 unsigned char trust_passwd[16];
1134 fstring remote_machine;
1136 struct in_addr dest_ip;
1137 NET_ID_INFO_CTR ctr;
1138 NET_USER_INFO_3 info3;
1139 struct cli_state cli;
1141 BOOL connected_ok = False;
1142 struct nmb_name calling, called;
1145 * Check that the requested domain is not our own machine name.
1146 * If it is, we should never check the PDC here, we use our own local
1150 if(strequal( domain, global_myname)) {
1151 DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
1156 * Next, check that the passwords given were encrypted.
1159 if(((smb_apasslen != 24) && (smb_apasslen != 0)) ||
1160 ((smb_ntpasslen != 24) && (smb_ntpasslen != 0))) {
1163 * Not encrypted - do so.
1166 DEBUG(3,("domain_client_validate: User passwords not in encrypted format.\n"));
1167 generate_random_buffer( local_challenge, 8, False);
1168 SMBencrypt( (uchar *)smb_apasswd, local_challenge, local_lm_response);
1169 SMBNTencrypt((uchar *)smb_ntpasswd, local_challenge, local_nt_reponse);
1172 smb_apasswd = (char *)local_lm_response;
1173 smb_ntpasswd = (char *)local_nt_reponse;
1177 * Encrypted - get the challenge we sent for these
1181 if (!last_challenge(local_challenge)) {
1182 DEBUG(0,("domain_client_validate: no challenge done - password failed\n"));
1188 * Get the machine account password.
1190 if(!trust_password_lock( global_myworkgroup, global_myname, False)) {
1191 DEBUG(0,("domain_client_validate: unable to open the machine account password file for \
1192 machine %s in domain %s.\n", global_myname, global_myworkgroup ));
1196 if(get_trust_account_password( trust_passwd, &lct) == False) {
1197 DEBUG(0,("domain_client_validate: unable to read the machine account password for \
1198 machine %s in domain %s.\n", global_myname, global_myworkgroup ));
1199 trust_password_unlock();
1203 trust_password_unlock();
1206 * Here we check the last change time to see if the machine
1207 * password needs changing. JRA.
1210 if(time(NULL) > lct + lp_machine_password_timeout())
1211 global_machine_pasword_needs_changing = True;
1214 * At this point, smb_apasswd points to the lanman response to
1215 * the challenge in local_challenge, and smb_ntpasswd points to
1216 * the NT response to the challenge in local_challenge. Ship
1217 * these over the secure channel to a domain controller and
1218 * see if they were valid.
1223 if(cli_initialise(&cli) == False) {
1224 DEBUG(0,("domain_client_validate: unable to initialize client connection.\n"));
1229 * Treat each name in the 'password server =' line as a potential
1230 * PDC/BDC. Contact each in turn and try and authenticate.
1233 p = lp_passwordserver();
1234 while(p && next_token(&p,remote_machine,LIST_SEP,sizeof(remote_machine))) {
1236 standard_sub_basic(remote_machine);
1237 strupper(remote_machine);
1239 if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
1240 DEBUG(1,("domain_client_validate: Can't resolve address for %s\n", remote_machine));
1244 if (ismyip(dest_ip)) {
1245 DEBUG(1,("domain_client_validate: Password server loop - not using password server %s\n",remote_machine));
1249 if (!cli_connect(&cli, remote_machine, &dest_ip)) {
1250 DEBUG(0,("domain_client_validate: unable to connect to SMB server on \
1251 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1255 make_nmb_name(&calling, global_myname , 0x0 , scope);
1256 make_nmb_name(&called , remote_machine, 0x20, scope);
1258 if (!cli_session_request(&cli, &calling, &called))
1260 DEBUG(0,("domain_client_validate: machine %s rejected the session setup. \
1261 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1266 cli.protocol = PROTOCOL_NT1;
1268 if (!cli_negprot(&cli)) {
1269 DEBUG(0,("domain_client_validate: machine %s rejected the negotiate protocol. \
1270 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1275 if (cli.protocol != PROTOCOL_NT1) {
1276 DEBUG(0,("domain_client_validate: machine %s didn't negotiate NT protocol.\n",
1283 * Do an anonymous session setup.
1286 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
1287 DEBUG(0,("domain_client_validate: machine %s rejected the session setup. \
1288 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1293 if (!(cli.sec_mode & 1)) {
1294 DEBUG(1,("domain_client_validate: machine %s isn't in user level security mode\n",
1300 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
1301 DEBUG(0,("domain_client_validate: machine %s rejected the tconX on the IPC$ share. \
1302 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1308 * We have an anonymous connection to IPC$.
1310 connected_ok = True;
1314 if (!connected_ok) {
1315 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
1321 * Ok - we have an anonymous connection to the IPC$ share.
1322 * Now start the NT Domain stuff :-).
1325 if(cli_nt_session_open(&cli, PIPE_NETLOGON, False) == False) {
1326 DEBUG(0,("domain_client_validate: unable to open the domain client session to \
1327 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
1328 cli_nt_session_close(&cli);
1334 if(cli_nt_setup_creds(&cli, trust_passwd) == False) {
1335 DEBUG(0,("domain_client_validate: unable to setup the PDC credentials to machine \
1336 %s. Error was : %s.\n", remote_machine, cli_errstr(&cli)));
1337 cli_nt_session_close(&cli);
1343 /* We really don't care what LUID we give the user. */
1344 generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
1346 if(cli_nt_login_network(&cli, domain, user, smb_uid_low, (char *)local_challenge,
1347 ((smb_apasslen != 0) ? smb_apasswd : NULL),
1348 ((smb_ntpasslen != 0) ? smb_ntpasswd : NULL),
1349 &ctr, &info3) == False) {
1350 DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
1351 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1352 cli_nt_session_close(&cli);
1359 * Here, if we really want it, we have lots of info about the user in info3.
1364 * We don't actually need to do this - plus it fails currently with
1365 * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
1369 if(cli_nt_logoff(&cli, &ctr) == False) {
1370 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
1371 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1372 cli_nt_session_close(&cli);
1379 cli_nt_session_close(&cli);