RIP BOOL. Convert BOOL -> bool. I found a few interesting
[samba.git] / source3 / smbd / password.c
1 /*
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 2007.
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 /* users from session setup */
24 static char *session_userlist = NULL;
25 static int len_session_userlist = 0;
26 /* workgroup from session setup. */
27 static char *session_workgroup = NULL;
28
29 /* this holds info on user ids that are already validated for this VC */
30 static user_struct *validated_users;
31 static int next_vuid = VUID_OFFSET;
32 static int num_validated_vuids;
33
34 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
35                                 SERVER_ALLOCATED_REQUIRED_NO,
36                                 SERVER_ALLOCATED_REQUIRED_ANY};
37
38 static user_struct *get_valid_user_struct_internal(uint16 vuid,
39                         enum server_allocated_state server_allocated)
40 {
41         user_struct *usp;
42         int count=0;
43
44         if (vuid == UID_FIELD_INVALID)
45                 return NULL;
46
47         for (usp=validated_users;usp;usp=usp->next,count++) {
48                 if (vuid == usp->vuid) {
49                         switch (server_allocated) {
50                                 case SERVER_ALLOCATED_REQUIRED_YES:
51                                         if (usp->server_info == NULL) {
52                                                 continue;
53                                         }
54                                         break;
55                                 case SERVER_ALLOCATED_REQUIRED_NO:
56                                         if (usp->server_info != NULL) {
57                                                 continue;
58                                         }
59                                 case SERVER_ALLOCATED_REQUIRED_ANY:
60                                         break;
61                         }
62                         if (count > 10) {
63                                 DLIST_PROMOTE(validated_users, usp);
64                         }
65                         return usp;
66                 }
67         }
68
69         return NULL;
70 }
71
72 /****************************************************************************
73  Check if a uid has been validated, and return an pointer to the user_struct
74  if it has. NULL if not. vuid is biased by an offset. This allows us to
75  tell random client vuid's (normally zero) from valid vuids.
76 ****************************************************************************/
77
78 user_struct *get_valid_user_struct(uint16 vuid)
79 {
80         return get_valid_user_struct_internal(vuid,
81                         SERVER_ALLOCATED_REQUIRED_YES);
82 }
83
84 bool is_partial_auth_vuid(uint16 vuid)
85 {
86         if (vuid == UID_FIELD_INVALID) {
87                 return False;
88         }
89         return get_valid_user_struct_internal(vuid,
90                         SERVER_ALLOCATED_REQUIRED_NO) ? True : False;
91 }
92
93 /****************************************************************************
94  Get the user struct of a partial NTLMSSP login
95 ****************************************************************************/
96
97 user_struct *get_partial_auth_user_struct(uint16 vuid)
98 {
99         return get_valid_user_struct_internal(vuid,
100                         SERVER_ALLOCATED_REQUIRED_NO);
101 }
102
103 /****************************************************************************
104  Invalidate a uid.
105 ****************************************************************************/
106
107 void invalidate_vuid(uint16 vuid)
108 {
109         user_struct *vuser = NULL;
110
111         if (vuid == UID_FIELD_INVALID) {
112                 return;
113         }
114
115         vuser = get_valid_user_struct_internal(vuid,
116                         SERVER_ALLOCATED_REQUIRED_ANY);
117         if (vuser == NULL) {
118                 return;
119         }
120
121         session_yield(vuser);
122
123         data_blob_free(&vuser->session_key);
124
125         DLIST_REMOVE(validated_users, vuser);
126
127         /* clear the vuid from the 'cache' on each connection, and
128            from the vuid 'owner' of connections */
129         conn_clear_vuid_cache(vuid);
130
131         TALLOC_FREE(vuser);
132         num_validated_vuids--;
133 }
134
135 /****************************************************************************
136  Invalidate all vuid entries for this process.
137 ****************************************************************************/
138
139 void invalidate_all_vuids(void)
140 {
141         user_struct *usp, *next=NULL;
142
143         for (usp=validated_users;usp;usp=next) {
144                 next = usp->next;
145                 invalidate_vuid(usp->vuid);
146         }
147 }
148
149 /****************************************************
150  Create a new partial auth user struct.
151 *****************************************************/
152
153 int register_initial_vuid(void)
154 {
155         user_struct *vuser;
156
157         /* Paranoia check. */
158         if(lp_security() == SEC_SHARE) {
159                 smb_panic("register_initial_vuid: "
160                         "Tried to register uid in security=share");
161         }
162
163         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
164         if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
165                 return UID_FIELD_INVALID;
166         }
167
168         if((vuser = talloc_zero(NULL, user_struct)) == NULL) {
169                 DEBUG(0,("register_initial_vuid: "
170                                 "Failed to talloc users struct!\n"));
171                 return UID_FIELD_INVALID;
172         }
173
174         /* Allocate a free vuid. Yes this is a linear search... */
175         while( get_valid_user_struct_internal(next_vuid,
176                         SERVER_ALLOCATED_REQUIRED_ANY) != NULL ) {
177                 next_vuid++;
178                 /* Check for vuid wrap. */
179                 if (next_vuid == UID_FIELD_INVALID) {
180                         next_vuid = VUID_OFFSET;
181                 }
182         }
183
184         DEBUG(10,("register_initial_vuid: allocated vuid = %u\n",
185                 (unsigned int)next_vuid ));
186
187         vuser->vuid = next_vuid;
188
189         /*
190          * This happens in an unfinished NTLMSSP session setup. We
191          * need to allocate a vuid between the first and second calls
192          * to NTLMSSP.
193          */
194         next_vuid++;
195         num_validated_vuids++;
196
197         DLIST_ADD(validated_users, vuser);
198         return vuser->vuid;
199 }
200
201 /**
202  *  register that a valid login has been performed, establish 'session'.
203  *  @param server_info The token returned from the authentication process.
204  *   (now 'owned' by register_existing_vuid)
205  *
206  *  @param session_key The User session key for the login session (now also
207  *  'owned' by register_existing_vuid)
208  *
209  *  @param respose_blob The NT challenge-response, if available.  (May be
210  *  freed after this call)
211  *
212  *  @param smb_name The untranslated name of the user
213  *
214  *  @return Newly allocated vuid, biased by an offset. (This allows us to
215  *   tell random client vuid's (normally zero) from valid vuids.)
216  *
217  */
218
219 int register_existing_vuid(uint16 vuid,
220                         auth_serversupplied_info *server_info,
221                         DATA_BLOB session_key,
222                         DATA_BLOB response_blob,
223                         const char *smb_name)
224 {
225         user_struct *vuser = get_partial_auth_user_struct(vuid);
226         if (!vuser) {
227                 goto fail;
228         }
229
230         /* Use this to keep tabs on all our info from the authentication */
231         vuser->server_info = server_info;
232
233         /* Ensure that the server_info will disappear with
234          * the vuser it is now attached to */
235
236         talloc_steal(vuser, vuser->server_info);
237
238         /* the next functions should be done by a SID mapping system (SMS) as
239          * the new real sam db won't have reference to unix uids or gids
240          */
241
242         vuser->uid = server_info->uid;
243         vuser->gid = server_info->gid;
244
245         vuser->n_groups = server_info->n_groups;
246         if (vuser->n_groups) {
247                 if (!(vuser->groups = (gid_t *)talloc_memdup(vuser,
248                                         server_info->groups,
249                                         sizeof(gid_t)*vuser->n_groups))) {
250                         DEBUG(0,("register_existing_vuid: "
251                                 "failed to talloc_memdup vuser->groups\n"));
252                         goto fail;
253                 }
254         }
255
256         vuser->guest = server_info->guest;
257         fstrcpy(vuser->user.unix_name, server_info->unix_name);
258
259         /* This is a potentially untrusted username */
260         alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$",
261                 sizeof(vuser->user.smb_name));
262
263         fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
264         fstrcpy(vuser->user.full_name,
265         pdb_get_fullname(server_info->sam_account));
266
267         {
268                 /* Keep the homedir handy */
269                 const char *homedir =
270                         pdb_get_homedir(server_info->sam_account);
271                 const char *logon_script =
272                         pdb_get_logon_script(server_info->sam_account);
273
274                 if (!IS_SAM_DEFAULT(server_info->sam_account,
275                                         PDB_UNIXHOMEDIR)) {
276                         const char *unix_homedir =
277                                 pdb_get_unix_homedir(server_info->sam_account);
278                         if (unix_homedir) {
279                                 vuser->unix_homedir = unix_homedir;
280                         }
281                 } else {
282                         struct passwd *passwd =
283                                 getpwnam_alloc(vuser, vuser->user.unix_name);
284                         if (passwd) {
285                                 vuser->unix_homedir = passwd->pw_dir;
286                                 /* Ensure that the unix_homedir now
287                                  * belongs to vuser, so it goes away
288                                  * with it, not with passwd below: */
289                                 talloc_steal(vuser, vuser->unix_homedir);
290                                 TALLOC_FREE(passwd);
291                         }
292                 }
293
294                 if (homedir) {
295                         vuser->homedir = homedir;
296                 }
297                 if (logon_script) {
298                         vuser->logon_script = logon_script;
299                 }
300         }
301         vuser->session_key = session_key;
302
303         DEBUG(10,("register_existing_vuid: (%u,%u) %s %s %s guest=%d\n",
304                         (unsigned int)vuser->uid,
305                         (unsigned int)vuser->gid,
306                         vuser->user.unix_name, vuser->user.smb_name,
307                         vuser->user.domain, vuser->guest ));
308
309         DEBUG(3, ("register_existing_vuid: User name: %s\t"
310                 "Real name: %s\n", vuser->user.unix_name,
311                 vuser->user.full_name));
312
313         if (server_info->ptok) {
314                 vuser->nt_user_token = dup_nt_token(vuser, server_info->ptok);
315         } else {
316                 DEBUG(1, ("register_existing_vuid: server_info does not "
317                         "contain a user_token - cannot continue\n"));
318                 goto fail;
319         }
320
321         DEBUG(3,("register_existing_vuid: UNIX uid %d is UNIX user %s, "
322                 "and will be vuid %u\n",
323                 (int)vuser->uid,vuser->user.unix_name, vuser->vuid));
324
325         next_vuid++;
326         num_validated_vuids++;
327
328         if (!session_claim(vuser)) {
329                 DEBUG(1, ("register_existing_vuid: Failed to claim session "
330                         "for vuid=%d\n",
331                         vuser->vuid));
332                 goto fail;
333         }
334
335         /* Register a home dir service for this user if
336         (a) This is not a guest connection,
337         (b) we have a home directory defined
338         (c) there s not an existing static share by that name
339         If a share exists by this name (autoloaded or not) reuse it . */
340
341         vuser->homes_snum = -1;
342         if ( (!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir)) {
343                 int servicenumber = lp_servicenumber(vuser->user.unix_name);
344                 if ( servicenumber == -1 ) {
345                         DEBUG(3, ("Adding homes service for user '%s' using "
346                                 "home directory: '%s'\n",
347                                 vuser->user.unix_name, vuser->unix_homedir));
348                         vuser->homes_snum =
349                                 add_home_service(vuser->user.unix_name,
350                                                 vuser->user.unix_name,
351                                                 vuser->unix_homedir);
352                 } else {
353                         DEBUG(3, ("Using static (or previously created) "
354                                 "service for user '%s'; path = '%s'\n",
355                                 vuser->user.unix_name,
356                                 lp_pathname(servicenumber) ));
357                         vuser->homes_snum = servicenumber;
358                 }
359         }
360
361         if (srv_is_signing_negotiated() && !vuser->guest &&
362                         !srv_signing_started()) {
363                 /* Try and turn on server signing on the first non-guest
364                  * sessionsetup. */
365                 srv_set_signing(vuser->session_key, response_blob);
366         }
367
368         /* fill in the current_user_info struct */
369         set_current_user_info( &vuser->user );
370         return vuser->vuid;
371
372   fail:
373
374         if (vuser) {
375                 invalidate_vuid(vuid);
376         }
377         return UID_FIELD_INVALID;
378 }
379
380 /****************************************************************************
381  Add a name to the session users list.
382 ****************************************************************************/
383
384 void add_session_user(const char *user)
385 {
386         fstring suser;
387         struct passwd *passwd;
388
389         if (!(passwd = Get_Pwnam(user)))
390                 return;
391
392         fstrcpy(suser,passwd->pw_name);
393
394         if(!*suser)
395                 return;
396
397         if( session_userlist && in_list(suser,session_userlist,False) )
398                 return;
399
400         if( !session_userlist ||
401             (strlen(suser) + strlen(session_userlist) + 2 >=
402              len_session_userlist) ) {
403                 char *newlist;
404
405                 if (len_session_userlist > 128 * PSTRING_LEN) {
406                         DEBUG(3,("add_session_user: session userlist already "
407                                  "too large.\n"));
408                         return;
409                 }
410                 newlist = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
411                         session_userlist,
412                         len_session_userlist + PSTRING_LEN );
413                 if( newlist == NULL ) {
414                         DEBUG(1,("Unable to resize session_userlist\n"));
415                         return;
416                 }
417                 if (!session_userlist) {
418                         *newlist = '\0';
419                 }
420                 session_userlist = newlist;
421                 len_session_userlist += PSTRING_LEN;
422         }
423
424         safe_strcat(session_userlist," ",len_session_userlist-1);
425         safe_strcat(session_userlist,suser,len_session_userlist-1);
426 }
427
428 /****************************************************************************
429  In security=share mode we need to store the client workgroup, as that's
430   what Vista uses for the NTLMv2 calculation.
431 ****************************************************************************/
432
433 void add_session_workgroup(const char *workgroup)
434 {
435         if (session_workgroup) {
436                 SAFE_FREE(session_workgroup);
437         }
438         session_workgroup = smb_xstrdup(workgroup);
439 }
440
441 /****************************************************************************
442  In security=share mode we need to return the client workgroup, as that's
443   what Vista uses for the NTLMv2 calculation.
444 ****************************************************************************/
445
446 const char *get_session_workgroup(void)
447 {
448         return session_workgroup;
449 }
450
451 /****************************************************************************
452  Check if a user is in a netgroup user list. If at first we don't succeed,
453  try lower case.
454 ****************************************************************************/
455
456 bool user_in_netgroup(const char *user, const char *ngname)
457 {
458 #ifdef HAVE_NETGROUP
459         static char *mydomain = NULL;
460         fstring lowercase_user;
461
462         if (mydomain == NULL)
463                 yp_get_default_domain(&mydomain);
464
465         if(mydomain == NULL) {
466                 DEBUG(5,("Unable to get default yp domain, "
467                         "let's try without specifying it\n"));
468         }
469
470         DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
471                 user, mydomain?mydomain:"(ANY)", ngname));
472
473         if (innetgr(ngname, NULL, user, mydomain)) {
474                 DEBUG(5,("user_in_netgroup: Found\n"));
475                 return (True);
476         } else {
477
478                 /*
479                  * Ok, innetgr is case sensitive. Try once more with lowercase
480                  * just in case. Attempt to fix #703. JRA.
481                  */
482
483                 fstrcpy(lowercase_user, user);
484                 strlower_m(lowercase_user);
485
486                 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
487                         lowercase_user, mydomain?mydomain:"(ANY)", ngname));
488
489                 if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
490                         DEBUG(5,("user_in_netgroup: Found\n"));
491                         return (True);
492                 }
493         }
494 #endif /* HAVE_NETGROUP */
495         return False;
496 }
497
498 /****************************************************************************
499  Check if a user is in a user list - can check combinations of UNIX
500  and netgroup lists.
501 ****************************************************************************/
502
503 bool user_in_list(const char *user,const char **list)
504 {
505         if (!list || !*list)
506                 return False;
507
508         DEBUG(10,("user_in_list: checking user %s in list\n", user));
509
510         while (*list) {
511
512                 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
513                           user, *list));
514
515                 /*
516                  * Check raw username.
517                  */
518                 if (strequal(user, *list))
519                         return(True);
520
521                 /*
522                  * Now check to see if any combination
523                  * of UNIX and netgroups has been specified.
524                  */
525
526                 if(**list == '@') {
527                         /*
528                          * Old behaviour. Check netgroup list
529                          * followed by UNIX list.
530                          */
531                         if(user_in_netgroup(user, *list +1))
532                                 return True;
533                         if(user_in_group(user, *list +1))
534                                 return True;
535                 } else if (**list == '+') {
536
537                         if((*(*list +1)) == '&') {
538                                 /*
539                                  * Search UNIX list followed by netgroup.
540                                  */
541                                 if(user_in_group(user, *list +2))
542                                         return True;
543                                 if(user_in_netgroup(user, *list +2))
544                                         return True;
545
546                         } else {
547
548                                 /*
549                                  * Just search UNIX list.
550                                  */
551
552                                 if(user_in_group(user, *list +1))
553                                         return True;
554                         }
555
556                 } else if (**list == '&') {
557
558                         if(*(*list +1) == '+') {
559                                 /*
560                                  * Search netgroup list followed by UNIX list.
561                                  */
562                                 if(user_in_netgroup(user, *list +2))
563                                         return True;
564                                 if(user_in_group(user, *list +2))
565                                         return True;
566                         } else {
567                                 /*
568                                  * Just search netgroup list.
569                                  */
570                                 if(user_in_netgroup(user, *list +1))
571                                         return True;
572                         }
573                 }
574
575                 list++;
576         }
577         return(False);
578 }
579
580 /****************************************************************************
581  Check if a username is valid.
582 ****************************************************************************/
583
584 static bool user_ok(const char *user, int snum)
585 {
586         char **valid, **invalid;
587         bool ret;
588
589         valid = invalid = NULL;
590         ret = True;
591
592         if (lp_invalid_users(snum)) {
593                 str_list_copy(&invalid, lp_invalid_users(snum));
594                 if (invalid &&
595                     str_list_substitute(invalid, "%S", lp_servicename(snum))) {
596
597                         /* This is used in sec=share only, so no current user
598                          * around to pass to str_list_sub_basic() */
599
600                         if ( invalid && str_list_sub_basic(invalid, "", "") ) {
601                                 ret = !user_in_list(user,
602                                                     (const char **)invalid);
603                         }
604                 }
605         }
606         if (invalid)
607                 str_list_free (&invalid);
608
609         if (ret && lp_valid_users(snum)) {
610                 str_list_copy(&valid, lp_valid_users(snum));
611                 if ( valid &&
612                      str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
613
614                         /* This is used in sec=share only, so no current user
615                          * around to pass to str_list_sub_basic() */
616
617                         if ( valid && str_list_sub_basic(valid, "", "") ) {
618                                 ret = user_in_list(user, (const char **)valid);
619                         }
620                 }
621         }
622         if (valid)
623                 str_list_free (&valid);
624
625         if (ret && lp_onlyuser(snum)) {
626                 char **user_list = str_list_make (lp_username(snum), NULL);
627                 if (user_list &&
628                     str_list_substitute(user_list, "%S",
629                                         lp_servicename(snum))) {
630                         ret = user_in_list(user, (const char **)user_list);
631                 }
632                 if (user_list) str_list_free (&user_list);
633         }
634
635         return(ret);
636 }
637
638 /****************************************************************************
639  Validate a group username entry. Return the username or NULL.
640 ****************************************************************************/
641
642 static char *validate_group(char *group, DATA_BLOB password,int snum)
643 {
644 #ifdef HAVE_NETGROUP
645         {
646                 char *host, *user, *domain;
647                 setnetgrent(group);
648                 while (getnetgrent(&host, &user, &domain)) {
649                         if (user) {
650                                 if (user_ok(user, snum) && 
651                                     password_ok(user,password)) {
652                                         endnetgrent();
653                                         return(user);
654                                 }
655                         }
656                 }
657                 endnetgrent();
658         }
659 #endif
660
661 #ifdef HAVE_GETGRENT
662         {
663                 struct group *gptr;
664                 setgrent();
665                 while ((gptr = (struct group *)getgrent())) {
666                         if (strequal(gptr->gr_name,group))
667                                 break;
668                 }
669
670                 /*
671                  * As user_ok can recurse doing a getgrent(), we must
672                  * copy the member list into a pstring on the stack before
673                  * use. Bug pointed out by leon@eatworms.swmed.edu.
674                  */
675
676                 if (gptr) {
677                         pstring member_list;
678                         char *member;
679                         size_t copied_len = 0;
680                         int i;
681
682                         *member_list = '\0';
683                         member = member_list;
684
685                         for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
686                                 size_t member_len = strlen(gptr->gr_mem[i])+1;
687                                 if(copied_len+member_len < sizeof(pstring)) { 
688
689                                         DEBUG(10,("validate_group: = gr_mem = "
690                                                   "%s\n", gptr->gr_mem[i]));
691
692                                         safe_strcpy(member, gptr->gr_mem[i],
693                                                     sizeof(pstring) -
694                                                     copied_len - 1);
695                                         copied_len += member_len;
696                                         member += copied_len;
697                                 } else {
698                                         *member = '\0';
699                                 }
700                         }
701
702                         endgrent();
703
704                         member = member_list;
705                         while (*member) {
706                                 static fstring name;
707                                 fstrcpy(name,member);
708                                 if (user_ok(name,snum) &&
709                                     password_ok(name,password)) {
710                                         endgrent();
711                                         return(&name[0]);
712                                 }
713
714                                 DEBUG(10,("validate_group = member = %s\n",
715                                           member));
716
717                                 member += strlen(member) + 1;
718                         }
719                 } else {
720                         endgrent();
721                         return NULL;
722                 }
723         }
724 #endif
725         return(NULL);
726 }
727
728 /****************************************************************************
729  Check for authority to login to a service with a given username/password.
730  Note this is *NOT* used when logging on using sessionsetup_and_X.
731 ****************************************************************************/
732
733 bool authorise_login(int snum, fstring user, DATA_BLOB password,
734                      bool *guest)
735 {
736         bool ok = False;
737
738 #ifdef DEBUG_PASSWORD
739         DEBUG(100,("authorise_login: checking authorisation on "
740                    "user=%s pass=%s\n", user,password.data));
741 #endif
742
743         *guest = False;
744
745         /* there are several possibilities:
746                 1) login as the given user with given password
747                 2) login as a previously registered username with the given
748                    password
749                 3) login as a session list username with the given password
750                 4) login as a previously validated user/password pair
751                 5) login as the "user =" user with given password
752                 6) login as the "user =" user with no password
753                    (guest connection)
754                 7) login as guest user with no password
755
756                 if the service is guest_only then steps 1 to 5 are skipped
757         */
758
759         /* now check the list of session users */
760         if (!ok) {
761                 char *auser;
762                 char *user_list = NULL;
763
764                 if ( session_userlist )
765                         user_list = SMB_STRDUP(session_userlist);
766                 else
767                         user_list = SMB_STRDUP("");
768
769                 if (!user_list)
770                         return(False);
771
772                 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
773                      auser = strtok(NULL,LIST_SEP)) {
774                         fstring user2;
775                         fstrcpy(user2,auser);
776                         if (!user_ok(user2,snum))
777                                 continue;
778
779                         if (password_ok(user2,password)) {
780                                 ok = True;
781                                 fstrcpy(user,user2);
782                                 DEBUG(3,("authorise_login: ACCEPTED: session "
783                                          "list username (%s) and given "
784                                          "password ok\n", user));
785                         }
786                 }
787
788                 SAFE_FREE(user_list);
789         }
790
791         /* check the user= fields and the given password */
792         if (!ok && lp_username(snum)) {
793                 char *auser;
794                 pstring user_list;
795                 pstrcpy(user_list,lp_username(snum));
796
797                 pstring_sub(user_list,"%S",lp_servicename(snum));
798
799                 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
800                      auser = strtok(NULL,LIST_SEP)) {
801                         if (*auser == '@') {
802                                 auser = validate_group(auser+1,password,snum);
803                                 if (auser) {
804                                         ok = True;
805                                         fstrcpy(user,auser);
806                                         DEBUG(3,("authorise_login: ACCEPTED: "
807                                                  "group username and given "
808                                                  "password ok (%s)\n", user));
809                                 }
810                         } else {
811                                 fstring user2;
812                                 fstrcpy(user2,auser);
813                                 if (user_ok(user2,snum) &&
814                                     password_ok(user2,password)) {
815                                         ok = True;
816                                         fstrcpy(user,user2);
817                                         DEBUG(3,("authorise_login: ACCEPTED: "
818                                                  "user list username and "
819                                                  "given password ok (%s)\n",
820                                                  user));
821                                 }
822                         }
823                 }
824         }
825
826         /* check for a normal guest connection */
827         if (!ok && GUEST_OK(snum)) {
828                 fstring guestname;
829                 fstrcpy(guestname,lp_guestaccount());
830                 if (Get_Pwnam(guestname)) {
831                         fstrcpy(user,guestname);
832                         ok = True;
833                         DEBUG(3,("authorise_login: ACCEPTED: guest account "
834                                  "and guest ok (%s)\n", user));
835                 } else {
836                         DEBUG(0,("authorise_login: Invalid guest account "
837                                  "%s??\n",guestname));
838                 }
839                 *guest = True;
840         }
841
842         if (ok && !user_ok(user, snum)) {
843                 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
844                 ok = False;
845         }
846
847         return(ok);
848 }