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