68d75b933fc9739f82171165f0460c36fcdbac48
[samba.git] / source3 / smbd / password.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Password and authentication handling
5    Copyright (C) Andrew Tridgell 1992-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25 extern int Protocol;
26
27 /* users from session setup */
28 static pstring session_users="";
29
30 extern pstring scope;
31 extern pstring global_myname;
32 extern fstring global_myworkgroup;
33
34 /* Data to do lanman1/2 password challenge. */
35 static unsigned char saved_challenge[8];
36 static BOOL challenge_sent=False;
37
38 /*******************************************************************
39 Get the next challenge value - no repeats.
40 ********************************************************************/
41 void generate_next_challenge(char *challenge)
42 {
43 #if 0
44         /* 
45          * Leave this ifdef'd out while we test
46          * the new crypto random number generator.
47          * JRA.
48          */
49         unsigned char buf[16];
50         static int counter = 0;
51         struct timeval tval;
52         int v1,v2;
53
54         /* get a sort-of random number */
55         GetTimeOfDay(&tval);
56         v1 = (counter++) + getpid() + tval.tv_sec;
57         v2 = (counter++) * getpid() + tval.tv_usec;
58         SIVAL(challenge,0,v1);
59         SIVAL(challenge,4,v2);
60
61         /* mash it up with md4 */
62         mdfour(buf, (unsigned char *)challenge, 8);
63 #else
64         unsigned char buf[8];
65
66         generate_random_buffer(buf,8,False);
67 #endif 
68         memcpy(saved_challenge, buf, 8);
69         memcpy(challenge,buf,8);
70         challenge_sent = True;
71 }
72
73 /*******************************************************************
74 set the last challenge sent, usually from a password server
75 ********************************************************************/
76 BOOL set_challenge(unsigned char *challenge)
77 {
78   memcpy(saved_challenge,challenge,8);
79   challenge_sent = True;
80   return(True);
81 }
82
83 /*******************************************************************
84 get the last challenge sent
85 ********************************************************************/
86 static BOOL last_challenge(unsigned char *challenge)
87 {
88   if (!challenge_sent) return(False);
89   memcpy(challenge,saved_challenge,8);
90   return(True);
91 }
92
93 /* this holds info on user ids that are already validated for this VC */
94 static user_struct *validated_users = NULL;
95 static int num_validated_users = 0;
96
97 /****************************************************************************
98 check if a uid has been validated, and return an pointer to the user_struct
99 if it has. NULL if not. vuid is biased by an offset. This allows us to
100 tell random client vuid's (normally zero) from valid vuids.
101 ****************************************************************************/
102 user_struct *get_valid_user_struct(uint16 vuid)
103 {
104   if (vuid == UID_FIELD_INVALID)
105     return NULL;
106   vuid -= VUID_OFFSET;
107   if ((vuid >= (uint16)num_validated_users) || 
108      (validated_users[vuid].uid == (uid_t)-1) || (validated_users[vuid].gid == (gid_t)-1))
109     return NULL;
110   return &validated_users[vuid];
111 }
112
113 /****************************************************************************
114 invalidate a uid
115 ****************************************************************************/
116 void invalidate_vuid(uint16 vuid)
117 {
118   user_struct *vuser = get_valid_user_struct(vuid);
119
120   if (vuser == NULL) return;
121
122   vuser->uid = (uid_t)-1;
123   vuser->gid = (gid_t)-1;
124
125   vuser->n_sids = 0;
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   if (vuser->sids)
134     free((char *)vuser->sids);
135
136   vuser->sids    = NULL;
137   vuser->groups  = NULL;
138 }
139
140
141 /****************************************************************************
142 return a validated username
143 ****************************************************************************/
144 char *validated_username(uint16 vuid)
145 {
146   user_struct *vuser = get_valid_user_struct(vuid);
147   if (vuser == NULL)
148     return 0;
149   return(vuser->name);
150 }
151
152
153
154 /****************************************************************************
155 register a uid/name pair as being valid and that a valid password
156 has been given. vuid is biased by an offset. This allows us to
157 tell random client vuid's (normally zero) from valid vuids.
158 ****************************************************************************/
159 uint16 register_vuid(uid_t uid,gid_t gid, char *unix_name, char *requested_name, BOOL guest)
160 {
161   user_struct *vuser;
162   struct passwd *pwfile; /* for getting real name from passwd file */
163
164   /* Ensure no vuid gets registered in share level security. */
165   if(lp_security() == SEC_SHARE)
166     return UID_FIELD_INVALID;
167
168 #if 0
169   /*
170    * After observing MS-Exchange services writing to a Samba share
171    * I belive this code is incorrect. Each service does its own
172    * sessionsetup_and_X for the same user, and as each service shuts
173    * down, it does a user_logoff_and_X. As we are consolidating multiple
174    * sessionsetup_and_X's onto the same vuid here, when the first service
175    * shuts down, it invalidates all the open files for the other services.
176    * Hence I am removing this code and forcing each sessionsetup_and_X
177    * to get a new vuid.
178    * Jeremy Allison. (jallison@whistle.com).
179    */
180
181   int i;
182   for(i = 0; i < num_validated_users; i++) {
183     vuser = &validated_users[i];
184     if ( vuser->uid == uid )
185       return (uint16)(i + VUID_OFFSET); /* User already validated */
186   }
187 #endif
188
189   validated_users = (user_struct *)Realloc(validated_users,
190                            sizeof(user_struct)*
191                            (num_validated_users+1));
192   
193   if (!validated_users)
194     {
195       DEBUG(0,("Failed to realloc users struct!\n"));
196       num_validated_users = 0;
197       return UID_FIELD_INVALID;
198     }
199
200   vuser = &validated_users[num_validated_users];
201   num_validated_users++;
202
203   vuser->uid = uid;
204   vuser->gid = gid;
205   vuser->guest = guest;
206   fstrcpy(vuser->name,unix_name);
207   fstrcpy(vuser->requested_name,requested_name);
208
209   vuser->n_sids = 0;
210   vuser->sids   = NULL;
211
212   vuser->n_groups = 0;
213   vuser->groups  = NULL;
214
215   /* Find all the groups this uid is in and store them. 
216      Used by become_user() */
217   get_unixgroups(unix_name,uid,gid,
218                &vuser->n_groups,
219                &vuser->groups);
220
221   DEBUG(3,("uid %d registered to name %s\n",(int)uid,unix_name));
222
223   DEBUG(3, ("Clearing default real name\n"));
224   fstrcpy(vuser->real_name, "<Full Name>\0");
225   if (lp_unix_realname()) {
226     if ((pwfile=getpwnam(vuser->name))!= NULL)
227       {
228       DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->name,pwfile->pw_gecos));
229       fstrcpy(vuser->real_name, pwfile->pw_gecos);
230       }
231   }
232
233   return (uint16)((num_validated_users - 1) + VUID_OFFSET);
234 }
235
236
237 /****************************************************************************
238 add a name to the session users list
239 ****************************************************************************/
240 void add_session_user(char *user)
241 {
242   fstring suser;
243   StrnCpy(suser,user,sizeof(suser)-1);
244
245   if (!Get_Pwnam(suser,True)) return;
246
247   if (suser && *suser && !in_list(suser,session_users,False))
248     {
249       if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
250         DEBUG(1,("Too many session users??\n"));
251       else
252         {
253           pstrcat(session_users," ");
254           pstrcat(session_users,suser);
255         }
256     }
257 }
258
259
260 /****************************************************************************
261 update the encrypted smbpasswd file from the plaintext username and password
262 *****************************************************************************/
263 static BOOL update_smbpassword_file(char *user, char *password)
264 {
265         struct smb_passwd *smbpw;
266         BOOL ret;
267         
268         become_root(0);
269         smbpw = getsmbpwnam(user);
270         unbecome_root(0);
271         
272         if(smbpw == NULL) {
273                 DEBUG(0,("getsmbpwnam returned NULL\n"));
274                 return False;
275         }
276  
277         /* Here, the flag is one, because we want to ignore the
278            XXXXXXX'd out password */
279         ret = change_oem_password( smbpw, password, True);
280         if (ret == False) {
281                 DEBUG(3,("change_oem_password returned False\n"));
282         }
283
284         return ret;
285 }
286
287
288
289
290
291 /****************************************************************************
292 core of smb password checking routine.
293 ****************************************************************************/
294 BOOL smb_password_check(char *password, unsigned char *part_passwd, unsigned char *c8)
295 {
296   /* Finish the encryption of part_passwd. */
297   unsigned char p21[21];
298   unsigned char p24[24];
299
300   if (part_passwd == NULL)
301     DEBUG(10,("No password set - allowing access\n"));
302   /* No password set - always true ! */
303   if (part_passwd == NULL)
304     return 1;
305
306   memset(p21,'\0',21);
307   memcpy(p21,part_passwd,16);
308   E_P24(p21, c8, p24);
309 #if DEBUG_PASSWORD
310   {
311     int i;
312     DEBUG(100,("Part password (P16) was |"));
313     for(i = 0; i < 16; i++)
314       DEBUG(100,("%X ", (unsigned char)part_passwd[i]));
315     DEBUG(100,("|\n"));
316     DEBUG(100,("Password from client was |"));
317     for(i = 0; i < 24; i++)
318       DEBUG(100,("%X ", (unsigned char)password[i]));
319     DEBUG(100,("|\n"));
320     DEBUG(100,("Given challenge was |"));
321     for(i = 0; i < 8; i++)
322       DEBUG(100,("%X ", (unsigned char)c8[i]));
323     DEBUG(100,("|\n"));
324     DEBUG(100,("Value from encryption was |"));
325     for(i = 0; i < 24; i++)
326       DEBUG(100,("%X ", (unsigned char)p24[i]));
327     DEBUG(100,("|\n"));
328   }
329 #endif
330   return (memcmp(p24, password, 24) == 0);
331 }
332
333 /****************************************************************************
334  Do a specific test for an smb password being correct, given a smb_password and
335  the lanman and NT responses.
336 ****************************************************************************/
337 BOOL smb_password_ok(struct smb_passwd *smb_pass, uchar chal[8],
338                      uchar lm_pass[24], uchar nt_pass[24])
339 {
340         uchar challenge[8];
341
342         if (!lm_pass || !smb_pass) return(False);
343
344         DEBUG(4,("Checking SMB password for user %s\n", 
345                  smb_pass->unix_name));
346
347         if(smb_pass->acct_ctrl & ACB_DISABLED) {
348                 DEBUG(3,("account for user %s was disabled.\n", 
349                          smb_pass->unix_name));
350                 return(False);
351         }
352
353         if (chal == NULL)
354         {
355                 DEBUG(5,("use last SMBnegprot challenge\n"));
356                 if (!last_challenge(challenge))
357                 {
358                         DEBUG(1,("no challenge done - password failed\n"));
359                         return False;
360                 }
361         }
362         else
363         {
364                 DEBUG(5,("challenge received\n"));
365                 memcpy(challenge, chal, 8);
366         }
367
368         if ((Protocol >= PROTOCOL_NT1) && (smb_pass->smb_nt_passwd != NULL)) {
369                 /* We have the NT MD4 hash challenge available - see if we can
370                    use it (ie. does it exist in the smbpasswd file).
371                 */
372                 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
373                 if (smb_password_check((char *)nt_pass, 
374                                        (uchar *)smb_pass->smb_nt_passwd, 
375                                        challenge)) {
376                         DEBUG(4,("NT MD4 password check succeeded\n"));
377                         return(True);
378                 }
379                 DEBUG(4,("NT MD4 password check failed\n"));
380         }
381
382         /* Try against the lanman password. smb_pass->smb_passwd == NULL means
383            no password, allow access. */
384
385         DEBUG(4,("Checking LM MD4 password\n"));
386
387         if((smb_pass->smb_passwd == NULL) && 
388            (smb_pass->acct_ctrl & ACB_PWNOTREQ)) {
389                 DEBUG(4,("no password required for user %s\n",
390                          smb_pass->unix_name));
391                 return True;
392         }
393
394         if((smb_pass->smb_passwd != NULL) && 
395            smb_password_check((char *)lm_pass, 
396                               (uchar *)smb_pass->smb_passwd, challenge)) {
397                 DEBUG(4,("LM MD4 password check succeeded\n"));
398                 return(True);
399         }
400
401         DEBUG(4,("LM MD4 password check failed\n"));
402
403         return False;
404 }
405
406
407 /****************************************************************************
408 check if a username/password is OK assuming the password is a 24 byte
409 SMB hash
410 return True if the password is correct, False otherwise
411 ****************************************************************************/
412
413 BOOL pass_check_smb(char *user, char *domain,
414                 uchar *chal, uchar *lm_pwd, uchar *nt_pwd,
415                 struct passwd *pwd)
416 {
417         struct passwd *pass;
418         struct smb_passwd *smb_pass;
419
420         if (!lm_pwd || !nt_pwd)
421         {
422                 return(False);
423         }
424
425         if (pwd != NULL && user == NULL)
426         {
427                 pass = (struct passwd *) pwd;
428                 user = pass->pw_name;
429         }
430         else
431         {
432                 pass = Get_Pwnam(user,True);
433         }
434
435         if (pass == NULL)
436         {
437                 DEBUG(3,("Couldn't find user %s\n",user));
438                 return(False);
439         }
440
441         smb_pass = getsmbpwnam(user);
442
443         if (smb_pass == NULL)
444         {
445                 DEBUG(3,("Couldn't find user %s in smb_passwd file.\n", user));
446                 return(False);
447         }
448
449         /* Quit if the account was disabled. */
450         if(smb_pass->acct_ctrl & ACB_DISABLED) {
451                 DEBUG(3,("account for user %s was disabled.\n", user));
452                 return(False);
453         }
454
455         /* Ensure the uid's match */
456         if (smb_pass->unix_uid != pass->pw_uid)
457         {
458                 DEBUG(3,("Error : UNIX and SMB uids in password files do not match !\n"));
459                 return(False);
460         }
461
462         if (lm_pwd[0] == '\0' && IS_BITS_SET_ALL(smb_pass->acct_ctrl, ACB_PWNOTREQ) && lp_null_passwords())
463         {
464                 DEBUG(3,("account for user %s has no password and null passwords are allowed.\n", smb_pass->unix_name));
465                 return(True);
466         }
467
468         if (smb_password_ok(smb_pass, chal, lm_pwd, nt_pwd))
469         {
470                 return(True);
471         }
472         
473         DEBUG(3,("Error smb_password_check failed\n"));
474         return False;
475 }
476
477 /****************************************************************************
478 check if a username/password pair is OK either via the system password
479 database or the encrypted SMB password database
480 return True if the password is correct, False otherwise
481 ****************************************************************************/
482 BOOL password_ok(char *user, char *password, int pwlen, struct passwd *pwd)
483 {
484         if (pwlen == 24 || (lp_encrypted_passwords() && (pwlen == 0) && lp_null_passwords()))
485         {
486                 /* if 24 bytes long assume it is an encrypted password */
487                 uchar challenge[8];
488
489                 if (!last_challenge(challenge))
490                 {
491                         DEBUG(0,("Error: challenge not done for user=%s\n", user));
492                         return False;
493                 }
494
495                 return pass_check_smb(user, global_myworkgroup,
496                                       challenge, (uchar *)password, (uchar *)password, pwd);
497         } 
498
499         return pass_check(user, password, pwlen, pwd, 
500                           lp_update_encrypted() ? 
501                           update_smbpassword_file : NULL);
502 }
503
504 /****************************************************************************
505 check if a username is valid
506 ****************************************************************************/
507 BOOL user_ok(char *user,int snum)
508 {
509         pstring valid, invalid;
510         BOOL ret;
511
512         StrnCpy(valid, lp_valid_users(snum), sizeof(pstring));
513         StrnCpy(invalid, lp_invalid_users(snum), sizeof(pstring));
514
515         string_sub(valid,"%S",lp_servicename(snum));
516         string_sub(invalid,"%S",lp_servicename(snum));
517         
518         ret = !user_in_list(user,invalid);
519         
520         if (ret && valid && *valid) {
521                 ret = user_in_list(user,valid);
522         }
523
524         if (ret && lp_onlyuser(snum)) {
525                 char *user_list = lp_username(snum);
526                 string_sub(user_list,"%S",lp_servicename(snum));
527                 ret = user_in_list(user,user_list);
528         }
529
530         return(ret);
531 }
532
533
534
535
536 /****************************************************************************
537 validate a group username entry. Return the username or NULL
538 ****************************************************************************/
539 static char *validate_group(char *group,char *password,int pwlen,int snum)
540 {
541 #if defined(HAVE_NETGROUP) && defined(HAVE_GETNETGRENT) && defined(HAVE_SETNETGRENT) && defined(HAVE_ENDNETGRENT)
542   {
543     char *host, *user, *domain;
544     setnetgrent(group);
545     while (getnetgrent(&host, &user, &domain)) {
546       if (user) {
547         if (user_ok(user, snum) && 
548             password_ok(user,password,pwlen,NULL)) {
549           endnetgrent();
550           return(user);
551         }
552       }
553     }
554     endnetgrent();
555   }
556 #endif
557   
558 #ifdef HAVE_GETGRNAM 
559   {
560     struct group *gptr = (struct group *)getgrnam(group);
561     char **member;
562     if (gptr)
563       {
564         member = gptr->gr_mem;
565         while (member && *member)
566           {
567             static fstring name;
568             fstrcpy(name,*member);
569             if (user_ok(name,snum) &&
570                 password_ok(name,password,pwlen,NULL))
571               return(&name[0]);
572             member++;
573           }
574 #ifdef GROUP_CHECK_PWENT
575         {
576           struct passwd *pwd;
577           static fstring tm;
578           
579           setpwent ();
580           while (pwd = getpwent ()) {
581             if (*(pwd->pw_passwd) && pwd->pw_gid == gptr->gr_gid) {
582               /* This Entry have PASSWORD and same GID then check pwd */
583               if (password_ok(NULL, password, pwlen, pwd)) {
584                 fstrcpy(tm, pwd->pw_name);
585                 endpwent ();
586                 return tm;
587               }
588             }
589           }
590           endpwent ();
591         }
592 #endif /* GROUP_CHECK_PWENT */
593       }
594   }      
595 #endif
596   return(NULL);
597 }
598
599
600
601 /****************************************************************************
602 check for authority to login to a service with a given username/password
603 ****************************************************************************/
604 BOOL authorise_login(int snum,char *user,char *password, int pwlen, 
605                      BOOL *guest,BOOL *force,uint16 vuid)
606 {
607   BOOL ok = False;
608   
609   *guest = False;
610   
611 #if DEBUG_PASSWORD
612   DEBUG(100,("checking authorisation on user=%s pass=%s\n",user,password));
613 #endif
614
615   /* there are several possibilities:
616      1) login as the given user with given password
617      2) login as a previously registered username with the given password
618      3) login as a session list username with the given password
619      4) login as a previously validated user/password pair
620      5) login as the "user =" user with given password
621      6) login as the "user =" user with no password (guest connection)
622      7) login as guest user with no password
623
624      if the service is guest_only then steps 1 to 5 are skipped
625   */
626
627   if (GUEST_ONLY(snum)) *force = True;
628
629   if (!(GUEST_ONLY(snum) && GUEST_OK(snum)))
630     {
631
632       user_struct *vuser = get_valid_user_struct(vuid);
633
634       /* check the given username and password */
635       if (!ok && (*user) && user_ok(user,snum)) {
636         ok = password_ok(user,password, pwlen, NULL);
637         if (ok) DEBUG(3,("ACCEPTED: given username password ok\n"));
638       }
639
640       /* check for a previously registered guest username */
641       if (!ok && (vuser != 0) && vuser->guest) {          
642         if (user_ok(vuser->name,snum) &&
643             password_ok(vuser->name, password, pwlen, NULL)) {
644           fstrcpy(user, vuser->name);
645           vuser->guest = False;
646           DEBUG(3,("ACCEPTED: given password with registered user %s\n", user));
647           ok = True;
648         }
649       }
650
651
652       /* now check the list of session users */
653     if (!ok)
654     {
655       char *auser;
656       char *user_list = strdup(session_users);
657       if (!user_list) return(False);
658
659       for (auser=strtok(user_list,LIST_SEP); 
660            !ok && auser; 
661            auser = strtok(NULL,LIST_SEP))
662       {
663         fstring user2;
664         fstrcpy(user2,auser);
665         if (!user_ok(user2,snum)) continue;
666                   
667         if (password_ok(user2,password, pwlen, NULL)) {
668           ok = True;
669           fstrcpy(user,user2);
670           DEBUG(3,("ACCEPTED: session list username and given password ok\n"));
671         }
672       }
673       free(user_list);
674     }
675
676     /* check for a previously validated username/password pair */
677     if (!ok && (!lp_revalidate(snum) || lp_security() > SEC_SHARE) &&
678         (vuser != 0) && !vuser->guest &&
679         user_ok(vuser->name,snum)) {
680       fstrcpy(user,vuser->name);
681       *guest = False;
682       DEBUG(3,("ACCEPTED: validated uid ok as non-guest\n"));
683       ok = True;
684     }
685
686       /* check for a rhosts entry */
687       if (!ok && user_ok(user,snum) && check_hosts_equiv(user)) {
688         ok = True;
689         DEBUG(3,("ACCEPTED: hosts equiv or rhosts entry\n"));
690       }
691
692       /* check the user= fields and the given password */
693       if (!ok && lp_username(snum)) {
694         char *auser;
695         pstring user_list;
696         StrnCpy(user_list,lp_username(snum),sizeof(pstring));
697
698         string_sub(user_list,"%S",lp_servicename(snum));
699           
700         for (auser=strtok(user_list,LIST_SEP);
701              auser && !ok;
702              auser = strtok(NULL,LIST_SEP))
703           {
704             if (*auser == '@')
705               {
706                 auser = validate_group(auser+1,password,pwlen,snum);
707                 if (auser)
708                   {
709                     ok = True;
710                     fstrcpy(user,auser);
711                     DEBUG(3,("ACCEPTED: group username and given password ok\n"));
712                   }
713               }
714             else
715               {
716                 fstring user2;
717                 fstrcpy(user2,auser);
718                 if (user_ok(user2,snum) && 
719                     password_ok(user2,password,pwlen,NULL))
720                   {
721                     ok = True;
722                     fstrcpy(user,user2);
723                     DEBUG(3,("ACCEPTED: user list username and given password ok\n"));
724                   }
725               }
726           }
727       }      
728     } /* not guest only */
729
730   /* check for a normal guest connection */
731   if (!ok && GUEST_OK(snum))
732     {
733       fstring guestname;
734       StrnCpy(guestname,lp_guestaccount(snum),sizeof(guestname)-1);
735       if (Get_Pwnam(guestname,True))
736         {
737           fstrcpy(user,guestname);
738           ok = True;
739           DEBUG(3,("ACCEPTED: guest account and guest ok\n"));
740         }
741       else
742         DEBUG(0,("Invalid guest account %s??\n",guestname));
743       *guest = True;
744       *force = True;
745     }
746
747   if (ok && !user_ok(user,snum))
748     {
749       DEBUG(0,("rejected invalid user %s\n",user));
750       ok = False;
751     }
752
753   return(ok);
754 }
755
756
757 /****************************************************************************
758 read the a hosts.equiv or .rhosts file and check if it
759 allows this user from this machine
760 ****************************************************************************/
761 static BOOL check_user_equiv(char *user, char *remote, char *equiv_file)
762 {
763   pstring buf;
764   int plus_allowed = 1;
765   char *file_host;
766   char *file_user;
767   FILE *fp = sys_fopen(equiv_file, "r");
768   DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
769   if (! fp) return False;
770   while(fgets(buf, sizeof(buf), fp)) 
771   {
772     trim_string(buf," "," ");
773
774     if (buf[0] != '#' && buf[0] != '\n') 
775     {
776       BOOL is_group = False;
777       int plus = 1;
778       char *bp = buf;
779       if (strcmp(buf, "NO_PLUS\n") == 0)
780       {
781         DEBUG(6, ("check_user_equiv NO_PLUS\n"));
782         plus_allowed = 0;
783       }
784       else {
785         if (buf[0] == '+') 
786         {
787           bp++;
788           if (*bp == '\n' && plus_allowed) 
789           {
790             /* a bare plus means everbody allowed */
791             DEBUG(6, ("check_user_equiv everybody allowed\n"));
792             fclose(fp);
793             return True;
794           }
795         }
796         else if (buf[0] == '-')
797         {
798           bp++;
799           plus = 0;
800         }
801         if (*bp == '@') 
802         {
803           is_group = True;
804           bp++;
805         }
806         file_host = strtok(bp, " \t\n");
807         file_user = strtok(NULL, " \t\n");
808         DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)", 
809                  file_user ? file_user : "(null)" ));
810         if (file_host && *file_host) 
811         {
812           BOOL host_ok = False;
813
814 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
815           if (is_group)
816             {
817               static char *mydomain = NULL;
818               if (!mydomain)
819                 yp_get_default_domain(&mydomain);
820               if (mydomain && innetgr(file_host,remote,user,mydomain))
821                 host_ok = True;
822             }
823 #else
824           if (is_group)
825             {
826               DEBUG(1,("Netgroups not configured\n"));
827               continue;
828             }
829 #endif
830
831           /* is it this host */
832           /* the fact that remote has come from a call of gethostbyaddr
833            * means that it may have the fully qualified domain name
834            * so we could look up the file version to get it into
835            * a canonical form, but I would rather just type it
836            * in full in the equiv file
837            */
838           if (!host_ok && !is_group && strequal(remote, file_host))
839             host_ok = True;
840
841           if (!host_ok)
842             continue;
843
844           /* is it this user */
845           if (file_user == 0 || strequal(user, file_user)) 
846             {
847               fclose(fp);
848               DEBUG(5, ("check_user_equiv matched %s%s %s\n",
849                         (plus ? "+" : "-"), file_host,
850                         (file_user ? file_user : "")));
851               return (plus ? True : False);
852             }
853         }
854       }
855     }
856   }
857   fclose(fp);
858   return False;
859 }
860
861
862 /****************************************************************************
863 check for a possible hosts equiv or rhosts entry for the user
864 ****************************************************************************/
865 BOOL check_hosts_equiv(char *user)
866 {
867   char *fname = NULL;
868   pstring rhostsfile;
869   struct passwd *pass = Get_Pwnam(user,True);
870
871   if (!pass) 
872     return(False);
873
874   fname = lp_hosts_equiv();
875
876   /* note: don't allow hosts.equiv on root */
877   if (fname && *fname && (pass->pw_uid != 0)) {
878           extern int Client;
879           if (check_user_equiv(user,client_name(Client),fname))
880                   return(True);
881   }
882   
883   if (lp_use_rhosts())
884     {
885       char *home = get_home_dir(user);
886       if (home) {
887               extern int Client;
888               slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
889               if (check_user_equiv(user,client_name(Client),rhostsfile))
890                       return(True);
891       }
892     }
893
894   return(False);
895 }
896
897
898 /****************************************************************************
899 return the client state structure
900 ****************************************************************************/
901 struct cli_state *server_client(void)
902 {
903         static struct cli_state pw_cli;
904         return &pw_cli;
905 }
906
907 /****************************************************************************
908 support for server level security 
909 ****************************************************************************/
910 struct cli_state *server_cryptkey(void)
911 {
912         if (cli_connect_serverlist(server_client(), lp_passwordserver()))
913         {
914                 return server_client();
915         }
916         return NULL;
917 }
918
919 /****************************************************************************
920 validate a password with the password server
921 ****************************************************************************/
922 BOOL server_validate(char *user, char *domain, 
923                      char *pass, int passlen,
924                      char *ntpass, int ntpasslen)
925 {
926   struct cli_state *cli;
927   static unsigned char badpass[24];
928   static BOOL tested_password_server = False;
929   static BOOL bad_password_server = False;
930
931   cli = server_client();
932
933   if (!cli->initialised) {
934     DEBUG(1,("password server %s is not connected\n", cli->desthost));
935     return(False);
936   }  
937
938   if(badpass[0] == 0)
939     memset(badpass, 0x1f, sizeof(badpass));
940
941   if((passlen == sizeof(badpass)) && !memcmp(badpass, pass, passlen)) {
942     /* 
943      * Very unlikely, our random bad password is the same as the users
944      * password. */
945     memset(badpass, badpass[0]+1, sizeof(badpass));
946   }
947
948   /*
949    * Attempt a session setup with a totally incorrect password.
950    * If this succeeds with the guest bit *NOT* set then the password
951    * server is broken and is not correctly setting the guest bit. We
952    * need to detect this as some versions of NT4.x are broken. JRA.
953    */
954
955   if(!tested_password_server) {
956     if (cli_session_setup(cli, user, (char *)badpass, sizeof(badpass), 
957                               (char *)badpass, sizeof(badpass), domain)) {
958
959       /*
960        * We connected to the password server so we
961        * can say we've tested it.
962        */
963       tested_password_server = True;
964
965       if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
966         DEBUG(0,("server_validate: password server %s allows users as non-guest \
967 with a bad password.\n", cli->desthost));
968         DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
969 use this machine as the password server.\n"));
970         cli_ulogoff(cli);
971
972         /*
973          * Password server has the bug.
974          */
975         bad_password_server = True;
976         return False;
977       }
978       cli_ulogoff(cli);
979     }
980   } else {
981
982     /*
983      * We have already tested the password server.
984      * Fail immediately if it has the bug.
985      */
986
987     if(bad_password_server) {
988       DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
989 with a bad password.\n", cli->desthost));
990       DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
991 use this machine as the password server.\n"));
992       return False;
993     }
994   }
995
996   /*
997    * Now we know the password server will correctly set the guest bit, or is
998    * not guest enabled, we can try with the real password.
999    */
1000
1001   if (!cli_session_setup(cli, user, pass, passlen, ntpass, ntpasslen, domain)) {
1002     DEBUG(1,("password server %s rejected the password\n", cli->desthost));
1003     return False;
1004   }
1005
1006   /* if logged in as guest then reject */
1007   if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
1008     DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
1009     cli_ulogoff(cli);
1010     return(False);
1011   }
1012
1013
1014   cli_ulogoff(cli);
1015
1016   return(True);
1017 }
1018
1019 /***********************************************************************
1020  Do the same as security=server, but using NT Domain calls and a session
1021  key from the workstation trust account password.
1022 ************************************************************************/
1023
1024 BOOL domain_client_validate( char *user, char *domain, 
1025                              char *smb_apasswd, int smb_apasslen, 
1026                              char *smb_ntpasswd, int smb_ntpasslen)
1027 {
1028         uint16 nt_pipe_fnum;
1029         unsigned char local_challenge[8];
1030         unsigned char local_lm_response[24];
1031         unsigned char local_nt_reponse[24];
1032         unsigned char trust_passwd[16];
1033         NET_ID_INFO_CTR ctr;
1034         NET_USER_INFO_3 info3;
1035         struct cli_state cli;
1036         uint32 smb_uid_low;
1037
1038         /* 
1039         * Check that the requested domain is not our own machine name.
1040         * If it is, we should never check the PDC here, we use our own local
1041         * password file.
1042         */
1043
1044         if(strequal( domain, global_myname))
1045         {
1046                 DEBUG(3,("domain_client_validate: Requested domain was for this machine.\n"));
1047                 return False;
1048         }
1049
1050         /*
1051         * Next, check that the passwords given were encrypted.
1052         */
1053
1054         if(((smb_apasslen  != 24) && (smb_apasslen  != 0)) || 
1055            ((smb_ntpasslen != 24) && (smb_ntpasslen != 0)))
1056         {
1057                 /*
1058                  * Not encrypted - do so.
1059                  */
1060
1061                 DEBUG(3,("domain_client_validate: User passwords not in encrypted format.\n"));
1062                 generate_random_buffer( local_challenge, 8, False);
1063                 SMBencrypt( (uchar *)smb_apasswd, local_challenge, local_lm_response);
1064                 SMBNTencrypt((uchar *)smb_ntpasswd, local_challenge, local_nt_reponse);
1065                 smb_apasslen = 24;
1066                 smb_ntpasslen = 24;
1067                 smb_apasswd = (char *)local_lm_response;
1068                 smb_ntpasswd = (char *)local_nt_reponse;
1069         }
1070         else
1071         {
1072                 /*
1073                  * Encrypted - get the challenge we sent for these
1074                  * responses.
1075                  */
1076
1077                 if (!last_challenge(local_challenge))
1078                 {
1079                         DEBUG(0,("domain_client_validate: no challenge done - password failed\n"));
1080                         return False;
1081                 }
1082         }
1083
1084         /*
1085          * Get the workstation trust account password.
1086          */
1087         if (!trust_get_passwd( trust_passwd, global_myworkgroup, global_myname))
1088         {
1089                 return False;
1090         }
1091
1092         /*
1093          * At this point, smb_apasswd points to the lanman response to
1094          * the challenge in local_challenge, and smb_ntpasswd points to
1095          * the NT response to the challenge in local_challenge. Ship
1096          * these over the secure channel to a domain controller and
1097          * see if they were valid.
1098          */
1099
1100         if (!cli_connect_serverlist(&cli, lp_passwordserver()))
1101         {
1102                 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
1103                 return False;
1104         }
1105
1106         /*
1107         * Ok - we have an anonymous connection to the IPC$ share.
1108         * Now start the NT Domain stuff :-).
1109         */
1110
1111         if(cli_nt_session_open(&cli, PIPE_NETLOGON, &nt_pipe_fnum) == False) {
1112         DEBUG(0,("domain_client_validate: unable to open the domain client session to \
1113         machine %s. Error was : %s.\n", cli.desthost, cli_errstr(&cli)));
1114         cli_nt_session_close(&cli, nt_pipe_fnum);
1115         cli_ulogoff(&cli);
1116         cli_shutdown(&cli);
1117         return False; 
1118         }
1119
1120         if(cli_nt_setup_creds(&cli, nt_pipe_fnum,
1121            cli.mach_acct, trust_passwd, SEC_CHAN_WKSTA) == False)
1122         {
1123                 DEBUG(0,("domain_client_validate: unable to setup the PDC credentials to machine \
1124                 %s. Error was : %s.\n", cli.desthost, cli_errstr(&cli)));
1125                 cli_nt_session_close(&cli, nt_pipe_fnum);
1126                 cli_ulogoff(&cli);
1127                 cli_shutdown(&cli);
1128                 return False;
1129         }
1130
1131         /* We really don't care what LUID we give the user. */
1132         generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
1133
1134         if (!cli_nt_login_network(&cli, nt_pipe_fnum, domain, user, smb_uid_low, (char *)local_challenge,
1135         ((smb_apasslen != 0) ? smb_apasswd : NULL),
1136         ((smb_ntpasslen != 0) ? smb_ntpasswd : NULL),
1137         &ctr, &info3))
1138         {
1139                 DEBUG(0,("domain_client_validate: unable to validate password for user %s in domain \
1140                 %s to Domain controller %s. Error was %s.\n", user, domain, cli.desthost, cli_errstr(&cli)));
1141                 cli_nt_session_close(&cli, nt_pipe_fnum);
1142                 cli_ulogoff(&cli);
1143                 cli_shutdown(&cli);
1144                 return False;
1145         }
1146
1147         /*
1148          * Here, if we really want it, we have lots of info about the user in info3.
1149          * LKCLXXXX - really important to check things like "is this user acct
1150          * locked out / disabled" etc!!!!
1151          */
1152
1153 #if 0
1154         /* 
1155         * We don't actually need to do this - plus it fails currently with
1156         * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
1157         * send here. JRA.
1158         */
1159
1160         if (!cli_nt_logoff(&cli, nt_pipe_fnum, &ctr))
1161         {
1162                 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
1163                 %s to Domain controller %s. Error was %s.\n", user, domain, cli.desthost, cli_errstr(&cli)));        
1164                 cli_nt_session_close(&cli, nt_pipe_fnum);
1165                 cli_ulogoff(&cli);
1166                 cli_shutdown(&cli);
1167                 return False;
1168         }
1169 #endif /* 0 */
1170
1171         cli_nt_session_close(&cli, nt_pipe_fnum);
1172         cli_ulogoff(&cli);
1173         cli_shutdown(&cli);
1174         return True;
1175 }