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