6201b85357f41c1388e6e130dc8ae6e1e0eed513
[samba.git] / source / smbd / password.c
1 #define OLD_NTDOMAIN 1
2
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    Password and authentication handling
7    Copyright (C) Andrew Tridgell 1992-1998
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27 extern int Protocol;
28 extern struct in_addr ipzero;
29
30 /* users from session setup */
31 static pstring session_users="";
32
33 extern pstring global_myname;
34 extern fstring global_myworkgroup;
35
36 /* Data to do lanman1/2 password challenge. */
37 static unsigned char saved_challenge[8];
38 static BOOL challenge_sent=False;
39
40 /*******************************************************************
41 Get the next challenge value - no repeats.
42 ********************************************************************/
43 void generate_next_challenge(char *challenge)
44 {
45 #if 0
46         /* 
47          * Leave this ifdef'd out while we test
48          * the new crypto random number generator.
49          * JRA.
50          */
51         unsigned char buf[16];
52         static int counter = 0;
53         struct timeval tval;
54         int v1,v2;
55
56         /* get a sort-of random number */
57         GetTimeOfDay(&tval);
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);
62
63         /* mash it up with md4 */
64         mdfour(buf, (unsigned char *)challenge, 8);
65 #else
66         unsigned char buf[8];
67
68         generate_random_buffer(buf,8,False);
69 #endif 
70         memcpy(saved_challenge, buf, 8);
71         memcpy(challenge,buf,8);
72         challenge_sent = True;
73 }
74
75 /*******************************************************************
76 set the last challenge sent, usually from a password server
77 ********************************************************************/
78 BOOL set_challenge(unsigned char *challenge)
79 {
80   memcpy(saved_challenge,challenge,8);
81   challenge_sent = True;
82   return(True);
83 }
84
85 /*******************************************************************
86 get the last challenge sent
87 ********************************************************************/
88 static BOOL last_challenge(unsigned char *challenge)
89 {
90   if (!challenge_sent) return(False);
91   memcpy(challenge,saved_challenge,8);
92   return(True);
93 }
94
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;
98
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)
105 {
106   if (vuid == UID_FIELD_INVALID)
107     return NULL;
108   vuid -= VUID_OFFSET;
109   if ((vuid >= (uint16)num_validated_users) || 
110      (validated_users[vuid].uid == (uid_t)-1) || (validated_users[vuid].gid == (gid_t)-1))
111     return NULL;
112   return &validated_users[vuid];
113 }
114
115 /****************************************************************************
116 invalidate a uid
117 ****************************************************************************/
118 void invalidate_vuid(uint16 vuid)
119 {
120   user_struct *vuser = get_valid_user_struct(vuid);
121
122   if (vuser == NULL) return;
123
124   vuser->uid = (uid_t)-1;
125   vuser->gid = (gid_t)-1;
126
127   /* same number of igroups as groups */
128   vuser->n_groups = 0;
129
130   if (vuser->groups)
131     free((char *)vuser->groups);
132
133   vuser->groups  = NULL;
134 }
135
136
137 /****************************************************************************
138 return a validated username
139 ****************************************************************************/
140 char *validated_username(uint16 vuid)
141 {
142         user_struct *vuser = get_valid_user_struct(vuid);
143         if (vuser == NULL)
144                 return 0;
145         return(vuser->user.unix_name);
146 }
147
148 /****************************************************************************
149 return a validated domain
150 ****************************************************************************/
151 char *validated_domain(uint16 vuid)
152 {
153         user_struct *vuser = get_valid_user_struct(vuid);
154         if (vuser == NULL)
155                 return 0;
156         return(vuser->user.domain);
157 }
158
159
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)
165 {
166         int i,ngroups;
167         gid_t grp = 0;
168         gid_t *groups = NULL;
169
170         if (-1 == initgroups(user,gid))
171         {
172                 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
173                 if (getuid() == 0)
174                 {
175                         if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767)
176                         {
177                                 DEBUG(0,("This is probably a problem with the account %s\n", user));
178                         }
179                 }
180                 return -1;
181         }
182
183         ngroups = sys_getgroups(0,&grp);
184         if (ngroups <= 0)
185         {
186                 ngroups = groups_max();
187         }
188
189         if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL)
190         {
191                 DEBUG(0,("setup_groups malloc fail !\n"));
192                 return -1;
193         }
194
195         ngroups = sys_getgroups(ngroups,groups);
196
197         (*p_ngroups) = ngroups;
198         (*p_groups) = groups;
199
200         DEBUG( 3, ( "%s is in %d groups: ", user, ngroups ) );
201         for (i = 0; i < ngroups; i++ )
202         {
203                 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
204         }
205         DEBUG( 3, ( "\n" ) );
206
207         return 0;
208 }
209
210
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,
218                      NET_USER_INFO_3 *usr)
219 {
220   user_struct *vuser;
221   struct passwd *pwfile; /* for getting real name from passwd file */
222
223   /* Ensure no vuid gets registered in share level security. */
224   if(lp_security() == SEC_SHARE)
225     return UID_FIELD_INVALID;
226
227 #if 0
228   /*
229    * After observing MS-Exchange services writing to a Samba share
230    * I belive this code is incorrect. Each service does its own
231    * sessionsetup_and_X for the same user, and as each service shuts
232    * down, it does a user_logoff_and_X. As we are consolidating multiple
233    * sessionsetup_and_X's onto the same vuid here, when the first service
234    * shuts down, it invalidates all the open files for the other services.
235    * Hence I am removing this code and forcing each sessionsetup_and_X
236    * to get a new vuid.
237    * Jeremy Allison. (jallison@whistle.com).
238    */
239
240   int i;
241   for(i = 0; i < num_validated_users; i++) {
242     vuser = &validated_users[i];
243     if ( vuser->uid == uid )
244       return (uint16)(i + VUID_OFFSET); /* User already validated */
245   }
246 #endif
247
248   validated_users = (user_struct *)Realloc(validated_users,
249                            sizeof(user_struct)*
250                            (num_validated_users+1));
251   
252   if (!validated_users)
253     {
254       DEBUG(0,("Failed to realloc users struct!\n"));
255       num_validated_users = 0;
256       return UID_FIELD_INVALID;
257     }
258
259   vuser = &validated_users[num_validated_users];
260   num_validated_users++;
261
262   vuser->uid = uid;
263   vuser->gid = gid;
264   vuser->guest = guest;
265   fstrcpy(vuser->user.unix_name,unix_name);
266   fstrcpy(vuser->user.smb_name,requested_name);
267   fstrcpy(vuser->user.domain,domain);
268
269   vuser->n_groups = 0;
270   vuser->groups  = NULL;
271
272   /* Find all the groups this uid is in and store them. 
273      Used by become_user() */
274   setup_groups(unix_name,domain,uid,gid,
275                &vuser->n_groups,
276                &vuser->groups);
277
278   if (usr == NULL)
279   {
280         int i;
281         extern DOM_SID global_sam_sid;
282
283         DEBUG(0,("vuser struct usr being filled in with trash, today\n"));
284         DEBUG(0,("this needs to be replaced with a proper surs impl.\n"));
285         DEBUG(0,("e.g. the one used in winbindd.  in fact, all\n"));
286         DEBUG(0,("occurrences of pdb_xxx_to_xxx should be replaced\n"));
287         DEBUG(0,("as soon as possible.\n"));
288         vuser->usr.user_id = pdb_uid_to_user_rid(uid);
289         vuser->usr.group_id = pdb_gid_to_group_rid(gid);
290         vuser->usr.num_groups = vuser->n_groups;
291         if (vuser->n_groups != 0)
292         {
293                 vuser->usr.gids = g_new(DOM_GID, vuser->usr.num_groups);
294                 if (vuser->usr.gids == NULL)
295                         return UID_FIELD_INVALID;
296         }
297
298         for (i = 0; i < vuser->usr.num_groups; i++)
299         {
300                 DOM_GID *ntgid = &vuser->usr.gids[i];
301                 ntgid->attr = 0x7;
302                 ntgid->g_rid = pdb_gid_to_group_rid(vuser->groups[i]);
303         }
304
305         /* this is possibly the worst thing to do, ever.  it assumes */
306         /* that all users of this system are in the local SAM database */
307         /* however, because there is no code to do anything otherwise, */
308         /* we have no choice */
309
310         init_dom_sid2(&vuser->usr.dom_sid, &global_sam_sid);
311   }
312   else
313   {
314           vuser->usr = *usr;
315   }
316
317   DEBUG(3,("uid %d registered to name %s\n",(int)uid,unix_name));
318
319   DEBUG(3, ("Clearing default real name\n"));
320   fstrcpy(vuser->user.full_name, "<Full Name>");
321   if (lp_unix_realname()) {
322     if ((pwfile=sys_getpwnam(vuser->user.unix_name))!= NULL)
323       {
324       DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,pwfile->pw_gecos));
325       fstrcpy(vuser->user.full_name, pwfile->pw_gecos);
326       }
327   }
328
329   memset(&vuser->dc, '\0', sizeof(vuser->dc));
330
331   return (uint16)((num_validated_users - 1) + VUID_OFFSET);
332 }
333
334
335 /****************************************************************************
336 add a name to the session users list
337 ****************************************************************************/
338 void add_session_user(char *user)
339 {
340   fstring suser;
341   StrnCpy(suser,user,sizeof(suser)-1);
342
343   if (!Get_Pwnam(suser,True)) return;
344
345   if (suser && *suser && !in_list(suser,session_users,False))
346     {
347       if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
348         DEBUG(1,("Too many session users??\n"));
349       else
350         {
351           pstrcat(session_users," ");
352           pstrcat(session_users,suser);
353         }
354     }
355 }
356
357
358 /****************************************************************************
359 update the encrypted smbpasswd file from the plaintext username and password
360 *****************************************************************************/
361 static BOOL update_smbpassword_file(char *user, char *password)
362 {
363         struct smb_passwd *smbpw;
364         BOOL ret;
365         
366         become_root(0);
367         smbpw = getsmbpwnam(user);
368         unbecome_root(0);
369         
370         if(smbpw == NULL) {
371                 DEBUG(0,("getsmbpwnam returned NULL\n"));
372                 return False;
373         }
374
375         /*
376          * Remove the account disabled flag - we are updating the
377          * users password from a login.
378          */
379         smbpw->acct_ctrl &= ~ACB_DISABLED;
380
381         /* Here, the flag is one, because we want to ignore the
382            XXXXXXX'd out password */
383         ret = change_oem_password( smbpw, password, True);
384         if (ret == False) {
385                 DEBUG(3,("change_oem_password returned False\n"));
386         }
387
388         return ret;
389 }
390
391
392
393
394
395 /****************************************************************************
396 core of smb password checking routine.
397 ****************************************************************************/
398 BOOL smb_password_check(char *password, unsigned char *part_passwd, unsigned char *c8)
399 {
400   /* Finish the encryption of part_passwd. */
401   unsigned char p21[21];
402   unsigned char p24[24];
403
404   if (part_passwd == NULL)
405     DEBUG(10,("No password set - allowing access\n"));
406   /* No password set - always true ! */
407   if (part_passwd == NULL)
408     return 1;
409
410   memset(p21,'\0',21);
411   memcpy(p21,part_passwd,16);
412   E_P24(p21, c8, p24);
413 #if DEBUG_PASSWORD
414   {
415     int i;
416     DEBUG(100,("Part password (P16) was |"));
417     for(i = 0; i < 16; i++)
418       DEBUG(100,("%X ", (unsigned char)part_passwd[i]));
419     DEBUG(100,("|\n"));
420     DEBUG(100,("Password from client was |"));
421     for(i = 0; i < 24; i++)
422       DEBUG(100,("%X ", (unsigned char)password[i]));
423     DEBUG(100,("|\n"));
424     DEBUG(100,("Given challenge was |"));
425     for(i = 0; i < 8; i++)
426       DEBUG(100,("%X ", (unsigned char)c8[i]));
427     DEBUG(100,("|\n"));
428     DEBUG(100,("Value from encryption was |"));
429     for(i = 0; i < 24; i++)
430       DEBUG(100,("%X ", (unsigned char)p24[i]));
431     DEBUG(100,("|\n"));
432   }
433 #endif
434   return (memcmp(p24, password, 24) == 0);
435 }
436
437 /****************************************************************************
438  Do a specific test for an smb password being correct, given a smb_password and
439  the lanman and NT responses.
440 ****************************************************************************/
441 BOOL smb_password_ok(struct smb_passwd *smb_pass, uchar chal[8],
442                      uchar lm_pass[24], uchar nt_pass[24])
443 {
444         uchar challenge[8];
445
446         if (!lm_pass || !smb_pass) return(False);
447
448         DEBUG(4,("Checking SMB password for user %s\n", 
449                  smb_pass->smb_name));
450
451         if(smb_pass->acct_ctrl & ACB_DISABLED) {
452                 DEBUG(1,("account for user %s was disabled.\n", 
453                          smb_pass->smb_name));
454                 return(False);
455         }
456
457         if (chal == NULL)
458         {
459                 DEBUG(5,("use last SMBnegprot challenge\n"));
460                 if (!last_challenge(challenge))
461                 {
462                         DEBUG(1,("no challenge done - password failed\n"));
463                         return False;
464                 }
465         }
466         else
467         {
468                 DEBUG(5,("challenge received\n"));
469                 memcpy(challenge, chal, 8);
470         }
471
472         if ((Protocol >= PROTOCOL_NT1) && (smb_pass->smb_nt_passwd != NULL)) {
473                 /* We have the NT MD4 hash challenge available - see if we can
474                    use it (ie. does it exist in the smbpasswd file).
475                 */
476                 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
477                 if (smb_password_check((char *)nt_pass, 
478                                        (uchar *)smb_pass->smb_nt_passwd, 
479                                        challenge)) {
480                         DEBUG(4,("NT MD4 password check succeeded\n"));
481                         return(True);
482                 }
483                 DEBUG(4,("NT MD4 password check failed\n"));
484         }
485
486         /* Try against the lanman password. smb_pass->smb_passwd == NULL means
487            no password, allow access. */
488
489         DEBUG(4,("Checking LM MD4 password\n"));
490
491         if((smb_pass->smb_passwd == NULL) && 
492            (smb_pass->acct_ctrl & ACB_PWNOTREQ)) {
493                 DEBUG(4,("no password required for user %s\n",
494                          smb_pass->smb_name));
495                 return True;
496         }
497
498         if((smb_pass->smb_passwd != NULL) && 
499            smb_password_check((char *)lm_pass, 
500                               (uchar *)smb_pass->smb_passwd, challenge)) {
501                 DEBUG(4,("LM MD4 password check succeeded\n"));
502                 return(True);
503         }
504
505         DEBUG(4,("LM MD4 password check failed\n"));
506
507         return False;
508 }
509
510
511 /****************************************************************************
512 check if a username/password is OK assuming the password is a 24 byte
513 SMB hash
514 return True if the password is correct, False otherwise
515 ****************************************************************************/
516
517 BOOL pass_check_smb(char *user, char *domain,
518                 uchar *chal, uchar *lm_pwd, uchar *nt_pwd,
519                 struct passwd *pwd)
520 {
521         struct passwd *pass;
522         struct smb_passwd *smb_pass;
523
524         if (!lm_pwd || !nt_pwd)
525         {
526                 return(False);
527         }
528
529         if (pwd != NULL && user == NULL)
530         {
531                 pass = (struct passwd *) pwd;
532                 user = pass->pw_name;
533         }
534         else
535         {
536                 pass = smb_getpwnam(user,True);
537         }
538
539         if (pass == NULL)
540         {
541                 DEBUG(1,("Couldn't find user '%s' in UNIX password database.\n",user));
542                 return(False);
543         }
544
545         smb_pass = getsmbpwnam(user);
546
547         if (smb_pass == NULL)
548         {
549                 DEBUG(1,("Couldn't find user '%s' in smb_passwd file.\n", user));
550                 return(False);
551         }
552
553         /* Quit if the account was disabled. */
554         if(smb_pass->acct_ctrl & ACB_DISABLED) {
555                 DEBUG(1,("Account for user '%s' was disabled.\n", user));
556                 return(False);
557         }
558
559         /* Ensure the uid's match */
560         if (smb_pass->smb_userid != pass->pw_uid)
561         {
562                 DEBUG(0,("Error : UNIX and SMB uids in password files do not match for user '%s'!\n", user));
563                 return(False);
564         }
565
566         if (lm_pwd[0] == '\0' && IS_BITS_SET_ALL(smb_pass->acct_ctrl, ACB_PWNOTREQ) && lp_null_passwords())
567         {
568                 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", smb_pass->smb_name));
569                 return(True);
570         }
571
572         if (smb_password_ok(smb_pass, chal, lm_pwd, nt_pwd))
573         {
574                 return(True);
575         }
576         
577         DEBUG(2,("pass_check_smb failed - invalid password for user [%s]\n", user));
578         return False;
579 }
580
581 /****************************************************************************
582 check if a username/password pair is OK either via the system password
583 database or the encrypted SMB password database
584 return True if the password is correct, False otherwise
585 ****************************************************************************/
586 BOOL password_ok(char *user, char *password, int pwlen, struct passwd *pwd)
587 {
588         if (pwlen == 24 || (lp_encrypted_passwords() && (pwlen == 0) && lp_null_passwords()))
589         {
590                 /* if 24 bytes long assume it is an encrypted password */
591                 uchar challenge[8];
592
593                 if (!last_challenge(challenge))
594                 {
595                         DEBUG(0,("Error: challenge not done for user=%s\n", user));
596                         return False;
597                 }
598
599                 return pass_check_smb(user, global_myworkgroup,
600                                       challenge, (uchar *)password, (uchar *)password, pwd);
601         } 
602
603         return pass_check(user, password, pwlen, pwd, 
604                           lp_update_encrypted() ? 
605                           update_smbpassword_file : NULL);
606 }
607
608 /****************************************************************************
609 check if a username is valid
610 ****************************************************************************/
611 BOOL user_ok(char *user,int snum)
612 {
613         pstring valid, invalid;
614         BOOL ret;
615
616         StrnCpy(valid, lp_valid_users(snum), sizeof(pstring)-1);
617         StrnCpy(invalid, lp_invalid_users(snum), sizeof(pstring)-1);
618
619         pstring_sub(valid,"%S",lp_servicename(snum));
620         pstring_sub(invalid,"%S",lp_servicename(snum));
621         
622         ret = !user_in_list(user,invalid);
623         
624         if (ret && valid && *valid) {
625                 ret = user_in_list(user,valid);
626         }
627
628         if (ret && lp_onlyuser(snum)) {
629                 char *user_list = lp_username(snum);
630                 pstring_sub(user_list,"%S",lp_servicename(snum));
631                 ret = user_in_list(user,user_list);
632         }
633
634         return(ret);
635 }
636
637
638
639
640 /****************************************************************************
641 validate a group username entry. Return the username or NULL
642 ****************************************************************************/
643 static char *validate_group(char *group,char *password,int pwlen,int snum)
644 {
645 #ifdef HAVE_NETGROUP
646         {
647                 char *host, *user, *domain;
648                 setnetgrent(group);
649                 while (getnetgrent(&host, &user, &domain)) {
650                         if (user) {
651                                 if (user_ok(user, snum) && 
652                                     password_ok(user,password,pwlen,NULL)) {
653                                         endnetgrent();
654                                         return(user);
655                                 }
656                         }
657                 }
658                 endnetgrent();
659         }
660 #endif
661   
662 #ifdef HAVE_GETGRENT
663         {
664                 struct group *gptr;
665                 setgrent();
666                 while ((gptr = (struct group *)getgrent())) {
667                         if (strequal(gptr->gr_name,group))
668                                 break;
669                 }
670
671                 /*
672                  * As user_ok can recurse doing a getgrent(), we must
673                  * copy the member list into a pstring on the stack before
674                  * use. Bug pointed out by leon@eatworms.swmed.edu.
675                  */
676
677                 if (gptr) {
678                         pstring member_list;
679                         char *member;
680                         size_t copied_len = 0;
681                         int i;
682
683                         *member_list = '\0';
684                         member = member_list;
685
686                         for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
687                                 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
688                                 if( copied_len + member_len < sizeof(pstring)) { 
689
690                                         DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
691
692                                         safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
693                                         copied_len += member_len;
694                                         member += copied_len;
695                                 } else {
696                                         *member = '\0';
697                                 }
698                         }
699
700                         endgrent();
701
702                         member = member_list;
703                         while (*member) {
704                                 static fstring name;
705                                 fstrcpy(name,member);
706                                 if (user_ok(name,snum) &&
707                                     password_ok(name,password,pwlen,NULL)) {
708                                         endgrent();
709                                         return(&name[0]);
710                                 }
711
712                                 DEBUG(10,("validate_group = member = %s\n", member));
713
714                                 member += strlen(member) + 1;
715                         }
716                 } else {
717                         endgrent();
718                         return NULL;
719                 }
720         }
721 #endif
722         return(NULL);
723 }
724
725
726
727 /****************************************************************************
728 check for authority to login to a service with a given username/password
729 ****************************************************************************/
730 BOOL authorise_login(int snum,char *user,char *password, int pwlen, 
731                      BOOL *guest,BOOL *force,uint16 vuid)
732 {
733   BOOL ok = False;
734   
735   *guest = False;
736   
737 #if DEBUG_PASSWORD
738   DEBUG(100,("checking authorisation on user=%s pass=%s\n",user,password));
739 #endif
740
741   /* there are several possibilities:
742      1) login as the given user with given password
743      2) login as a previously registered username with the given password
744      3) login as a session list username with the given password
745      4) login as a previously validated user/password pair
746      5) login as the "user =" user with given password
747      6) login as the "user =" user with no password (guest connection)
748      7) login as guest user with no password
749
750      if the service is guest_only then steps 1 to 5 are skipped
751   */
752
753   if (GUEST_ONLY(snum)) *force = True;
754
755   if (!(GUEST_ONLY(snum) && GUEST_OK(snum)))
756     {
757
758       user_struct *vuser = get_valid_user_struct(vuid);
759
760       /* check the given username and password */
761       if (!ok && (*user) && user_ok(user,snum)) {
762         ok = password_ok(user,password, pwlen, NULL);
763         if (ok) DEBUG(3,("ACCEPTED: given username password ok\n"));
764       }
765
766       /* check for a previously registered guest username */
767       if (!ok && (vuser != 0) && vuser->guest) {          
768         if (user_ok(vuser->user.unix_name,snum) &&
769             password_ok(vuser->user.unix_name, password, pwlen, NULL)) {
770           fstrcpy(user, vuser->user.unix_name);
771           vuser->guest = False;
772           DEBUG(3,("ACCEPTED: given password with registered user %s\n", user));
773           ok = True;
774         }
775       }
776
777
778       /* now check the list of session users */
779     if (!ok)
780     {
781       char *auser;
782       char *user_list = strdup(session_users);
783       if (!user_list) return(False);
784
785       for (auser=strtok(user_list,LIST_SEP); 
786            !ok && auser; 
787            auser = strtok(NULL,LIST_SEP))
788       {
789         fstring user2;
790         fstrcpy(user2,auser);
791         if (!user_ok(user2,snum)) continue;
792                   
793         if (password_ok(user2,password, pwlen, NULL)) {
794           ok = True;
795           fstrcpy(user,user2);
796           DEBUG(3,("ACCEPTED: session list username and given password ok\n"));
797         }
798       }
799       free(user_list);
800     }
801
802     /* check for a previously validated username/password pair */
803     if (!ok && (lp_security() > SEC_SHARE) &&
804         (vuser != 0) && !vuser->guest &&
805         user_ok(vuser->user.unix_name,snum)) {
806       fstrcpy(user,vuser->user.unix_name);
807       *guest = False;
808       DEBUG(3,("ACCEPTED: validated uid ok as non-guest\n"));
809       ok = True;
810     }
811
812       /* check for a rhosts entry */
813       if (!ok && user_ok(user,snum) && check_hosts_equiv(user)) {
814         ok = True;
815         DEBUG(3,("ACCEPTED: hosts equiv or rhosts entry\n"));
816       }
817
818       /* check the user= fields and the given password */
819       if (!ok && lp_username(snum)) {
820         char *auser;
821         pstring user_list;
822         StrnCpy(user_list,lp_username(snum),sizeof(pstring));
823
824         pstring_sub(user_list,"%S",lp_servicename(snum));
825           
826         for (auser=strtok(user_list,LIST_SEP);
827              auser && !ok;
828              auser = strtok(NULL,LIST_SEP))
829           {
830             if (*auser == '@')
831               {
832                 auser = validate_group(auser+1,password,pwlen,snum);
833                 if (auser)
834                   {
835                     ok = True;
836                     fstrcpy(user,auser);
837                     DEBUG(3,("ACCEPTED: group username and given password ok\n"));
838                   }
839               }
840             else
841               {
842                 fstring user2;
843                 fstrcpy(user2,auser);
844                 if (user_ok(user2,snum) && 
845                     password_ok(user2,password,pwlen,NULL))
846                   {
847                     ok = True;
848                     fstrcpy(user,user2);
849                     DEBUG(3,("ACCEPTED: user list username and given password ok\n"));
850                   }
851               }
852           }
853       }      
854     } /* not guest only */
855
856   /* check for a normal guest connection */
857   if (!ok && GUEST_OK(snum))
858     {
859       fstring guestname;
860       StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
861       if (Get_Pwnam(guestname,True))
862         {
863           fstrcpy(user,guestname);
864           ok = True;
865           DEBUG(3,("ACCEPTED: guest account and guest ok\n"));
866         }
867       else
868         DEBUG(0,("Invalid guest account %s??\n",guestname));
869       *guest = True;
870       *force = True;
871     }
872
873   if (ok && !user_ok(user,snum))
874     {
875       DEBUG(0,("rejected invalid user %s\n",user));
876       ok = False;
877     }
878
879   return(ok);
880 }
881
882
883 /****************************************************************************
884 read the a hosts.equiv or .rhosts file and check if it
885 allows this user from this machine
886 ****************************************************************************/
887 static BOOL check_user_equiv(char *user, char *remote, char *equiv_file)
888 {
889   int plus_allowed = 1;
890   char *file_host;
891   char *file_user;
892   char **lines = file_lines_load(equiv_file, NULL);
893   int i;
894
895   DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
896   if (! lines) return False;
897   for (i=0; lines[i]; i++) {
898     char *buf = lines[i];
899     trim_string(buf," "," ");
900
901     if (buf[0] != '#' && buf[0] != '\n') 
902     {
903       BOOL is_group = False;
904       int plus = 1;
905       char *bp = buf;
906       if (strcmp(buf, "NO_PLUS\n") == 0)
907       {
908         DEBUG(6, ("check_user_equiv NO_PLUS\n"));
909         plus_allowed = 0;
910       }
911       else {
912         if (buf[0] == '+') 
913         {
914           bp++;
915           if (*bp == '\n' && plus_allowed) 
916           {
917             /* a bare plus means everbody allowed */
918             DEBUG(6, ("check_user_equiv everybody allowed\n"));
919             file_lines_free(lines);
920             return True;
921           }
922         }
923         else if (buf[0] == '-')
924         {
925           bp++;
926           plus = 0;
927         }
928         if (*bp == '@') 
929         {
930           is_group = True;
931           bp++;
932         }
933         file_host = strtok(bp, " \t\n");
934         file_user = strtok(NULL, " \t\n");
935         DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)", 
936                  file_user ? file_user : "(null)" ));
937         if (file_host && *file_host) 
938         {
939           BOOL host_ok = False;
940
941 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
942           if (is_group)
943             {
944               static char *mydomain = NULL;
945               if (!mydomain)
946                 yp_get_default_domain(&mydomain);
947               if (mydomain && innetgr(file_host,remote,user,mydomain))
948                 host_ok = True;
949             }
950 #else
951           if (is_group)
952             {
953               DEBUG(1,("Netgroups not configured\n"));
954               continue;
955             }
956 #endif
957
958           /* is it this host */
959           /* the fact that remote has come from a call of gethostbyaddr
960            * means that it may have the fully qualified domain name
961            * so we could look up the file version to get it into
962            * a canonical form, but I would rather just type it
963            * in full in the equiv file
964            */
965           if (!host_ok && !is_group && strequal(remote, file_host))
966             host_ok = True;
967
968           if (!host_ok)
969             continue;
970
971           /* is it this user */
972           if (file_user == 0 || strequal(user, file_user)) 
973             {
974               DEBUG(5, ("check_user_equiv matched %s%s %s\n",
975                         (plus ? "+" : "-"), file_host,
976                         (file_user ? file_user : "")));
977               file_lines_free(lines);
978               return (plus ? True : False);
979             }
980         }
981       }
982     }
983   }
984   file_lines_free(lines);
985   return False;
986 }
987
988
989 /****************************************************************************
990 check for a possible hosts equiv or rhosts entry for the user
991 ****************************************************************************/
992 BOOL check_hosts_equiv(char *user)
993 {
994   char *fname = NULL;
995   pstring rhostsfile;
996   struct passwd *pass = Get_Pwnam(user,True);
997
998   if (!pass) 
999     return(False);
1000
1001   fname = lp_hosts_equiv();
1002
1003   /* note: don't allow hosts.equiv on root */
1004   if (fname && *fname && (pass->pw_uid != 0)) {
1005           if (check_user_equiv(user,client_name(),fname))
1006                   return(True);
1007   }
1008   
1009   if (lp_use_rhosts())
1010     {
1011       char *home = get_user_home_dir(user);
1012       if (home) {
1013               slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
1014               if (check_user_equiv(user,client_name(),rhostsfile))
1015                       return(True);
1016       }
1017     }
1018
1019   return(False);
1020 }
1021
1022
1023 /****************************************************************************
1024  Return the client state structure.
1025 ****************************************************************************/
1026
1027 struct cli_state *server_client(void)
1028 {
1029         static struct cli_state pw_cli;
1030         return &pw_cli;
1031 }
1032
1033 /****************************************************************************
1034  Support for server level security.
1035 ****************************************************************************/
1036
1037 struct cli_state *server_cryptkey(void)
1038 {
1039         struct cli_state *cli;
1040         fstring desthost;
1041         struct in_addr dest_ip;
1042         char *p, *pserver;
1043         BOOL connected_ok = False;
1044
1045         cli = server_client();
1046
1047         if (!cli_initialise(cli))
1048                 return NULL;
1049
1050         pserver = strdup(lp_passwordserver());
1051         p = pserver;
1052
1053         while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
1054                 standard_sub_basic(desthost);
1055                 strupper(desthost);
1056
1057                 if(!resolve_name( desthost, &dest_ip, 0x20)) {
1058                         DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
1059                         continue;
1060                 }
1061
1062                 if (ismyip(dest_ip)) {
1063                         DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
1064                         continue;
1065                 }
1066
1067                 if (cli_connect(cli, desthost, &dest_ip)) {
1068                         DEBUG(3,("connected to password server %s\n",desthost));
1069                         connected_ok = True;
1070                         break;
1071                 }
1072         }
1073
1074         free(pserver);
1075
1076         if (!connected_ok) {
1077                 DEBUG(0,("password server not available\n"));
1078                 cli_shutdown(cli);
1079                 return NULL;
1080         }
1081
1082         if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
1083                 return NULL;
1084
1085         DEBUG(3,("got session\n"));
1086
1087         if (!cli_negprot(cli)) {
1088                 DEBUG(1,("%s rejected the negprot\n",desthost));
1089                 cli_shutdown(cli);
1090                 return NULL;
1091         }
1092
1093         if (cli->protocol < PROTOCOL_LANMAN2 ||
1094             !(cli->sec_mode & 1)) {
1095                 DEBUG(1,("%s isn't in user level security mode\n",desthost));
1096                 cli_shutdown(cli);
1097                 return NULL;
1098         }
1099
1100         DEBUG(3,("password server OK\n"));
1101
1102         return cli;
1103 }
1104
1105 /****************************************************************************
1106  Validate a password with the password server.
1107 ****************************************************************************/
1108
1109 BOOL server_validate(char *user, char *domain, 
1110                      char *pass, int passlen,
1111                      char *ntpass, int ntpasslen)
1112 {
1113   struct cli_state *cli;
1114   static unsigned char badpass[24];
1115   static BOOL tested_password_server = False;
1116   static BOOL bad_password_server = False;
1117
1118   cli = server_client();
1119
1120   if (!cli->initialised) {
1121     DEBUG(1,("password server %s is not connected\n", cli->desthost));
1122     return(False);
1123   }  
1124
1125   if(badpass[0] == 0)
1126     memset(badpass, 0x1f, sizeof(badpass));
1127
1128   if((passlen == sizeof(badpass)) && !memcmp(badpass, pass, passlen)) {
1129     /* 
1130      * Very unlikely, our random bad password is the same as the users
1131      * password. */
1132     memset(badpass, badpass[0]+1, sizeof(badpass));
1133   }
1134
1135   /*
1136    * Attempt a session setup with a totally incorrect password.
1137    * If this succeeds with the guest bit *NOT* set then the password
1138    * server is broken and is not correctly setting the guest bit. We
1139    * need to detect this as some versions of NT4.x are broken. JRA.
1140    */
1141
1142   if(!tested_password_server) {
1143     if (cli_session_setup(cli, user, (char *)badpass, sizeof(badpass), 
1144                               (char *)badpass, sizeof(badpass), domain)) {
1145
1146       /*
1147        * We connected to the password server so we
1148        * can say we've tested it.
1149        */
1150       tested_password_server = True;
1151
1152       if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
1153         DEBUG(0,("server_validate: password server %s allows users as non-guest \
1154 with a bad password.\n", cli->desthost));
1155         DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
1156 use this machine as the password server.\n"));
1157         cli_ulogoff(cli);
1158
1159         /*
1160          * Password server has the bug.
1161          */
1162         bad_password_server = True;
1163         return False;
1164       }
1165       cli_ulogoff(cli);
1166     }
1167   } else {
1168
1169     /*
1170      * We have already tested the password server.
1171      * Fail immediately if it has the bug.
1172      */
1173
1174     if(bad_password_server) {
1175       DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
1176 with a bad password.\n", cli->desthost));
1177       DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
1178 use this machine as the password server.\n"));
1179       return False;
1180     }
1181   }
1182
1183   /*
1184    * Now we know the password server will correctly set the guest bit, or is
1185    * not guest enabled, we can try with the real password.
1186    */
1187
1188   if (!cli_session_setup(cli, user, pass, passlen, ntpass, ntpasslen, domain)) {
1189     DEBUG(1,("password server %s rejected the password\n", cli->desthost));
1190     return False;
1191   }
1192
1193   /* if logged in as guest then reject */
1194   if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
1195     DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
1196     cli_ulogoff(cli);
1197     return(False);
1198   }
1199
1200   cli_ulogoff(cli);
1201
1202   return(True);
1203 }
1204
1205 /***********************************************************************
1206  Connect to a remote machine for domain security authentication
1207  given a name or IP address.
1208 ************************************************************************/
1209
1210 static BOOL connect_to_domain_password_server(struct cli_state *pcli, char *remote_machine,
1211                                               unsigned char *trust_passwd)
1212 {
1213   struct in_addr dest_ip;
1214
1215   if(cli_initialise(pcli) == False) {
1216     DEBUG(0,("connect_to_domain_password_server: unable to initialize client connection.\n"));
1217     return False;
1218   }
1219
1220   standard_sub_basic(remote_machine);
1221   strupper(remote_machine);
1222
1223   if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
1224     DEBUG(1,("connect_to_domain_password_server: Can't resolve address for %s\n", remote_machine));
1225     cli_shutdown(pcli);
1226     return False;
1227   }
1228   
1229   if (ismyip(dest_ip)) {
1230     DEBUG(1,("connect_to_domain_password_server: Password server loop - not using password server %s\n",
1231          remote_machine));
1232     cli_shutdown(pcli);
1233     return False;
1234   }
1235   
1236   if (!cli_connect(pcli, remote_machine, &dest_ip)) {
1237     DEBUG(0,("connect_to_domain_password_server: unable to connect to SMB server on \
1238 machine %s. Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1239     cli_shutdown(pcli);
1240     return False;
1241   }
1242   
1243   if (!attempt_netbios_session_request(pcli, global_myname, remote_machine, &dest_ip)) {
1244     DEBUG(0,("connect_to_password_server: machine %s rejected the NetBIOS \
1245 session request. Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1246     return False;
1247   }
1248   
1249   pcli->protocol = PROTOCOL_NT1;
1250
1251   if (!cli_negprot(pcli)) {
1252     DEBUG(0,("connect_to_domain_password_server: machine %s rejected the negotiate protocol. \
1253 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1254     cli_shutdown(pcli);
1255     return False;
1256   }
1257
1258   if (pcli->protocol != PROTOCOL_NT1) {
1259     DEBUG(0,("connect_to_domain_password_server: machine %s didn't negotiate NT protocol.\n",
1260                    remote_machine));
1261     cli_shutdown(pcli);
1262     return False;
1263   }
1264
1265   /*
1266    * Do an anonymous session setup.
1267    */
1268
1269   if (!cli_session_setup(pcli, "", "", 0, "", 0, "")) {
1270     DEBUG(0,("connect_to_domain_password_server: machine %s rejected the session setup. \
1271 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1272     cli_shutdown(pcli);
1273     return False;
1274   }
1275
1276   if (!(pcli->sec_mode & 1)) {
1277     DEBUG(1,("connect_to_domain_password_server: machine %s isn't in user level security mode\n",
1278                remote_machine));
1279     cli_shutdown(pcli);
1280     return False;
1281   }
1282
1283   if (!cli_send_tconX(pcli, "IPC$", "IPC", "", 1)) {
1284     DEBUG(0,("connect_to_domain_password_server: machine %s rejected the tconX on the IPC$ share. \
1285 Error was : %s.\n", remote_machine, cli_errstr(pcli) ));
1286     cli_shutdown(pcli);
1287     return False;
1288   }
1289
1290   /*
1291    * We now have an anonymous connection to IPC$ on the domain password server.
1292    */
1293
1294   /*
1295    * Even if the connect succeeds we need to setup the netlogon
1296    * pipe here. We do this as we may just have changed the domain
1297    * account password on the PDC and yet we may be talking to
1298    * a BDC that doesn't have this replicated yet. In this case
1299    * a successful connect to a DC needs to take the netlogon connect
1300    * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
1301    */
1302
1303   if(cli_nt_session_open(pcli, PIPE_NETLOGON) == False) {
1304     DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
1305 machine %s. Error was : %s.\n", remote_machine, cli_errstr(pcli)));
1306     cli_nt_session_close(pcli);
1307     cli_ulogoff(pcli);
1308     cli_shutdown(pcli);
1309     return False;
1310   }
1311
1312   if (cli_nt_setup_creds(pcli, trust_passwd) == False) {
1313     DEBUG(0,("connect_to_domain_password_server: unable to setup the PDC credentials to machine \
1314 %s. Error was : %s.\n", remote_machine, cli_errstr(pcli)));
1315     cli_nt_session_close(pcli);
1316     cli_ulogoff(pcli);
1317     cli_shutdown(pcli);
1318     return(False);
1319   }
1320
1321   return True;
1322 }
1323
1324 /***********************************************************************
1325  Utility function to attempt a connection to an IP address of a DC.
1326 ************************************************************************/
1327
1328 static BOOL attempt_connect_to_dc(struct cli_state *pcli, struct in_addr *ip, unsigned char *trust_passwd)
1329 {
1330   fstring dc_name;
1331
1332   /*
1333    * Ignore addresses we have already tried.
1334    */
1335
1336   if (ip_equal(ipzero, *ip))
1337           return False;
1338
1339   if (!lookup_pdc_name(global_myname, lp_workgroup(), ip, dc_name))
1340           return False;
1341
1342   return connect_to_domain_password_server(pcli, dc_name, trust_passwd);
1343 }
1344
1345
1346
1347 /***********************************************************************
1348  We have been asked to dynamcially determine the IP addresses of
1349  the PDC and BDC's for this DOMAIN, and query them in turn.
1350 ************************************************************************/
1351 static BOOL find_connect_pdc(struct cli_state *pcli, unsigned char *trust_passwd)
1352 {
1353         struct in_addr *ip_list = NULL;
1354         int count = 0;
1355         int i;
1356         BOOL connected_ok = False;
1357
1358         if (!get_dc_list(lp_workgroup(), &ip_list, &count))
1359                 return False;
1360
1361         /*
1362          * Firstly try and contact a PDC/BDC who has the same
1363          * network address as any of our interfaces.
1364          */
1365         for(i = 0; i < count; i++) {
1366                 if(!is_local_net(ip_list[i]))
1367                         continue;
1368
1369                 if((connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd))) 
1370                         break;
1371                 
1372                 ip_list[i] = ipzero; /* Tried and failed. */
1373         }
1374
1375         /*
1376          * Secondly try and contact a random PDC/BDC.
1377          */
1378         if(!connected_ok) {
1379                 i = (sys_random() % count);
1380
1381                 if (!(connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd)))
1382                         ip_list[i] = ipzero; /* Tried and failed. */
1383         }
1384
1385         /*
1386          * Finally go through the IP list in turn, ignoring any addresses
1387          * we have already tried.
1388          */
1389         if(!connected_ok) {
1390                 /*
1391                  * Try and connect to any of the other IP addresses in the PDC/BDC list.
1392                  * Note that from a WINS server the #1 IP address is the PDC.
1393                  */
1394                 for(i = 0; i < count; i++) {
1395                         if((connected_ok = attempt_connect_to_dc(pcli, &ip_list[i], trust_passwd)))
1396                                 break;
1397                 }
1398         }
1399
1400         if(ip_list != NULL)
1401                 free((char *)ip_list);
1402
1403
1404         return connected_ok;
1405 }
1406
1407
1408
1409 /***********************************************************************
1410  Do the same as security=server, but using NT Domain calls and a session
1411  key from the machine password.
1412 ************************************************************************/
1413
1414 BOOL domain_client_validate( char *user, char *domain, 
1415                              char *smb_apasswd, int smb_apasslen, 
1416                              char *smb_ntpasswd, int smb_ntpasslen,
1417                              BOOL *user_exists)
1418 {
1419   unsigned char local_challenge[8];
1420   unsigned char local_lm_response[24];
1421   unsigned char local_nt_reponse[24];
1422   unsigned char trust_passwd[16];
1423   fstring remote_machine;
1424   char *p, *pserver;
1425   NET_ID_INFO_CTR ctr;
1426   NET_USER_INFO_3 info3;
1427   struct cli_state cli;
1428   uint32 smb_uid_low;
1429   BOOL connected_ok = False;
1430
1431   if(user_exists != NULL)
1432     *user_exists = True; /* Only set false on a very specific error. */
1433  
1434   /* 
1435    * Check that the requested domain is not our own machine name.
1436    * If it is, we should never check the PDC here, we use our own local
1437    * password file.
1438    */
1439
1440   if(strequal( domain, global_myname)) {
1441     DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
1442     return False;
1443   }
1444
1445   /*
1446    * Next, check that the passwords given were encrypted.
1447    */
1448
1449   if(((smb_apasslen != 24) && (smb_apasslen != 0)) || 
1450      ((smb_ntpasslen != 24) && (smb_ntpasslen != 0))) {
1451
1452     /*
1453      * Not encrypted - do so.
1454      */
1455
1456     DEBUG(3,("domain_client_validate: User passwords not in encrypted format.\n"));
1457     generate_random_buffer( local_challenge, 8, False);
1458     SMBencrypt( (uchar *)smb_apasswd, local_challenge, local_lm_response);
1459     SMBNTencrypt((uchar *)smb_ntpasswd, local_challenge, local_nt_reponse);
1460     smb_apasslen = 24;
1461     smb_ntpasslen = 24;
1462     smb_apasswd = (char *)local_lm_response;
1463     smb_ntpasswd = (char *)local_nt_reponse;
1464   } else {
1465
1466     /*
1467      * Encrypted - get the challenge we sent for these
1468      * responses.
1469      */
1470
1471     if (!last_challenge(local_challenge)) {
1472       DEBUG(0,("domain_client_validate: no challenge done - password failed\n"));
1473       return False;
1474     }
1475   }
1476
1477   /*
1478    * Get the machine account password for our primary domain
1479    */
1480   if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, NULL))
1481   {
1482     return False;
1483   }
1484
1485   /*
1486    * At this point, smb_apasswd points to the lanman response to
1487    * the challenge in local_challenge, and smb_ntpasswd points to
1488    * the NT response to the challenge in local_challenge. Ship
1489    * these over the secure channel to a domain controller and
1490    * see if they were valid.
1491    */
1492
1493   ZERO_STRUCT(cli);
1494
1495   /*
1496    * Treat each name in the 'password server =' line as a potential
1497    * PDC/BDC. Contact each in turn and try and authenticate.
1498    */
1499
1500   pserver = lp_passwordserver();
1501   if (! *pserver) pserver = "*";
1502   p = pserver;
1503
1504   while (!connected_ok &&
1505          next_token(&p,remote_machine,LIST_SEP,sizeof(remote_machine))) {
1506           if(strequal(remote_machine, "*")) {
1507                   connected_ok = find_connect_pdc(&cli, trust_passwd);
1508           } else {
1509                   connected_ok = connect_to_domain_password_server(&cli, remote_machine, trust_passwd);
1510           }
1511   }
1512
1513   if (!connected_ok) {
1514     DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
1515     cli_shutdown(&cli);
1516     return False;
1517   }
1518
1519   /* We really don't care what LUID we give the user. */
1520   generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
1521
1522   if(cli_nt_login_network(&cli, domain, user, smb_uid_low, (char *)local_challenge,
1523                           ((smb_apasslen != 0) ? smb_apasswd : NULL),
1524                           ((smb_ntpasslen != 0) ? smb_ntpasswd : NULL),
1525                           &ctr, &info3) == False) {
1526     uint32 nt_rpc_err;
1527
1528     cli_error(&cli, NULL, NULL, &nt_rpc_err);
1529     DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
1530 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
1531     cli_nt_session_close(&cli);
1532     cli_ulogoff(&cli);
1533     cli_shutdown(&cli);
1534
1535     /* unused, so delete here. */
1536     if (info3.gids != NULL)
1537             free (info3.gids);
1538
1539     if((nt_rpc_err == NT_STATUS_NO_SUCH_USER) && (user_exists != NULL))
1540       *user_exists = False;
1541
1542     return False;
1543   }
1544
1545     /* unused, so delete here. */
1546     if (info3.gids != NULL)
1547             free (info3.gids);
1548   /*
1549    * Here, if we really want it, we have lots of info about the user in info3.
1550    */
1551
1552 #if 0
1553   /* 
1554    * We don't actually need to do this - plus it fails currently with
1555    * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
1556    * send here. JRA.
1557    */
1558
1559   if(cli_nt_logoff(&cli, &ctr) == False) {
1560     DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
1561 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));        
1562     cli_nt_session_close(&cli);
1563     cli_ulogoff(&cli);
1564     cli_shutdown(&cli);
1565     return False;
1566   }
1567 #endif /* 0 */
1568
1569   cli_nt_session_close(&cli);
1570   cli_ulogoff(&cli);
1571   cli_shutdown(&cli);
1572   return True;
1573 }
1574
1575 #undef OLD_NTDOMAIN