r21108: Send sys_notify_watch through the VFS, FAM is next
[ira/wip.git] / source3 / smbd / password.c
index b2687980acf5d28d20b82ca0502027896e8b8ede..38000e93f4b8ebaaff6395d9a8cf04fe90e9289f 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    Password and authentication handling
    Copyright (C) Andrew Tridgell 1992-1998
    
 
 #include "includes.h"
 
-extern struct in_addr ipzero;
-
 /* users from session setup */
-static pstring session_users="";
+static char *session_userlist = NULL;
+static int len_session_userlist = 0;
 
 /* this holds info on user ids that are already validated for this VC */
 static user_struct *validated_users;
@@ -32,10 +30,11 @@ static int next_vuid = VUID_OFFSET;
 static int num_validated_vuids;
 
 /****************************************************************************
-check if a uid has been validated, and return an pointer to the user_struct
-if it has. NULL if not. vuid is biased by an offset. This allows us to
-tell random client vuid's (normally zero) from valid vuids.
+ Check if a uid has been validated, and return an pointer to the user_struct
+ if it has. NULL if not. vuid is biased by an offset. This allows us to
+ tell random client vuid's (normally zero) from valid vuids.
 ****************************************************************************/
+
 user_struct *get_valid_user_struct(uint16 vuid)
 {
        user_struct *usp;
@@ -45,7 +44,31 @@ user_struct *get_valid_user_struct(uint16 vuid)
                return NULL;
 
        for (usp=validated_users;usp;usp=usp->next,count++) {
-               if (vuid == usp->vuid) {
+               if (vuid == usp->vuid && usp->server_info) {
+                       if (count > 10) {
+                               DLIST_PROMOTE(validated_users, usp);
+                       }
+                       return usp;
+               }
+       }
+
+       return NULL;
+}
+
+/****************************************************************************
+ Get the user struct of a partial NTLMSSP login
+****************************************************************************/
+
+user_struct *get_partial_auth_user_struct(uint16 vuid)
+{
+       user_struct *usp;
+       int count=0;
+
+       if (vuid == UID_FIELD_INVALID)
+               return NULL;
+
+       for (usp=validated_users;usp;usp=usp->next,count++) {
+               if (vuid == usp->vuid && !usp->server_info) {
                        if (count > 10) {
                                DLIST_PROMOTE(validated_users, usp);
                        }
@@ -57,28 +80,43 @@ user_struct *get_valid_user_struct(uint16 vuid)
 }
 
 /****************************************************************************
-invalidate a uid
+ Invalidate a uid.
 ****************************************************************************/
+
 void invalidate_vuid(uint16 vuid)
 {
        user_struct *vuser = get_valid_user_struct(vuid);
 
        if (vuser == NULL)
                return;
-
+       
+       SAFE_FREE(vuser->homedir);
+       SAFE_FREE(vuser->unix_homedir);
+       SAFE_FREE(vuser->logon_script);
+       
        session_yield(vuser);
+       SAFE_FREE(vuser->session_keystr);
+
+       TALLOC_FREE(vuser->server_info);
+
+       data_blob_free(&vuser->session_key);
 
        DLIST_REMOVE(validated_users, vuser);
 
+       /* clear the vuid from the 'cache' on each connection, and
+          from the vuid 'owner' of connections */
+       conn_clear_vuid_cache(vuid);
+
        SAFE_FREE(vuser->groups);
-       delete_nt_token(&vuser->nt_user_token);
+       TALLOC_FREE(vuser->nt_user_token);
        SAFE_FREE(vuser);
        num_validated_vuids--;
 }
 
 /****************************************************************************
-invalidate all vuid entries for this process
+ Invalidate all vuid entries for this process.
 ****************************************************************************/
+
 void invalidate_all_vuids(void)
 {
        user_struct *usp, *next=NULL;
@@ -90,238 +128,413 @@ void invalidate_all_vuids(void)
        }
 }
 
-/****************************************************************************
-return a validated username
-****************************************************************************/
-char *validated_username(uint16 vuid)
+/**
+ *  register that a valid login has been performed, establish 'session'.
+ *  @param server_info The token returned from the authentication process. 
+ *   (now 'owned' by register_vuid)
+ *
+ *  @param session_key The User session key for the login session (now also
+ *  'owned' by register_vuid)
+ *
+ *  @param respose_blob The NT challenge-response, if available.  (May be
+ *  freed after this call)
+ *
+ *  @param smb_name The untranslated name of the user
+ *
+ *  @return Newly allocated vuid, biased by an offset. (This allows us to
+ *   tell random client vuid's (normally zero) from valid vuids.)
+ *
+ */
+
+int register_vuid(auth_serversupplied_info *server_info,
+                 DATA_BLOB session_key, DATA_BLOB response_blob,
+                 const char *smb_name)
 {
-       user_struct *vuser = get_valid_user_struct(vuid);
-       if (vuser == NULL)
-               return 0;
-       return(vuser->user.unix_name);
-}
+       user_struct *vuser = NULL;
 
-/****************************************************************************
-return a validated domain
-****************************************************************************/
-char *validated_domain(uint16 vuid)
-{
-       user_struct *vuser = get_valid_user_struct(vuid);
-       if (vuser == NULL)
-               return 0;
-       return(vuser->user.domain);
-}
+       /* Paranoia check. */
+       if(lp_security() == SEC_SHARE) {
+               smb_panic("Tried to register uid in security=share\n");
+       }
 
+       /* Limit allowed vuids to 16bits - VUID_OFFSET. */
+       if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
+               data_blob_free(&session_key);
+               return UID_FIELD_INVALID;
+       }
 
-/****************************************************************************
- Create the SID list for this user.
-****************************************************************************/
+       if((vuser = SMB_MALLOC_P(user_struct)) == NULL) {
+               DEBUG(0,("Failed to malloc users struct!\n"));
+               data_blob_free(&session_key);
+               return UID_FIELD_INVALID;
+       }
 
-NT_USER_TOKEN *create_nt_token(uid_t uid, gid_t gid, int ngroups, gid_t *groups, BOOL is_guest, NT_USER_TOKEN *sup_tok)
-{
-       extern DOM_SID global_sid_World;
-       extern DOM_SID global_sid_Network;
-       extern DOM_SID global_sid_Builtin_Guests;
-       extern DOM_SID global_sid_Authenticated_Users;
-       NT_USER_TOKEN *token;
-       DOM_SID *psids;
-       int i, psid_ndx = 0;
-       size_t num_sids = 0;
-       fstring sid_str;
-
-       if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
-               return NULL;
+       ZERO_STRUCTP(vuser);
 
-       ZERO_STRUCTP(token);
+       /* Allocate a free vuid. Yes this is a linear search... :-) */
+       while( get_valid_user_struct(next_vuid) != NULL ) {
+               next_vuid++;
+               /* Check for vuid wrap. */
+               if (next_vuid == UID_FIELD_INVALID)
+                       next_vuid = VUID_OFFSET;
+       }
 
-       /* We always have uid/gid plus World and Network and Authenticated Users or Guest SIDs. */
-       num_sids = 5 + ngroups;
+       DEBUG(10,("register_vuid: allocated vuid = %u\n",
+                 (unsigned int)next_vuid ));
 
-       if (sup_tok && sup_tok->num_sids)
-               num_sids += sup_tok->num_sids;
+       vuser->vuid = next_vuid;
 
-       if ((token->user_sids = (DOM_SID *)malloc( num_sids*sizeof(DOM_SID))) == NULL) {
-               SAFE_FREE(token);
-               return NULL;
+       if (!server_info) {
+               /*
+                * This happens in an unfinished NTLMSSP session setup. We
+                * need to allocate a vuid between the first and second calls
+                * to NTLMSSP.
+                */
+               next_vuid++;
+               num_validated_vuids++;
+               
+               vuser->server_info = NULL;
+               
+               DLIST_ADD(validated_users, vuser);
+               
+               return vuser->vuid;
        }
 
-       psids = token->user_sids;
-
-       /*
-        * Note - user SID *MUST* be first in token !
-        * se_access_check depends on this.
+       /* the next functions should be done by a SID mapping system (SMS) as
+        * the new real sam db won't have reference to unix uids or gids
         */
+       
+       vuser->uid = server_info->uid;
+       vuser->gid = server_info->gid;
+       
+       vuser->n_groups = server_info->n_groups;
+       if (vuser->n_groups) {
+               if (!(vuser->groups = (gid_t *)memdup(server_info->groups,
+                                                     sizeof(gid_t) *
+                                                     vuser->n_groups))) {
+                       DEBUG(0,("register_vuid: failed to memdup "
+                                "vuser->groups\n"));
+                       data_blob_free(&session_key);
+                       free(vuser);
+                       TALLOC_FREE(server_info);
+                       return UID_FIELD_INVALID;
+               }
+       }
 
-       uid_to_sid( &psids[PRIMARY_USER_SID_INDEX], uid);
-       psid_ndx++;
-
-       /*
-        * Primary group SID is second in token. Convention.
-        */
+       vuser->guest = server_info->guest;
+       fstrcpy(vuser->user.unix_name, server_info->unix_name); 
 
-       gid_to_sid( &psids[PRIMARY_GROUP_SID_INDEX], gid);
-       psid_ndx++;
+       /* This is a potentially untrusted username */
+       alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$",
+                    sizeof(vuser->user.smb_name));
 
-       /* Now add the group SIDs. */
+       fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
+       fstrcpy(vuser->user.full_name,
+               pdb_get_fullname(server_info->sam_account));
 
-       for (i = 0; i < ngroups; i++) {
-               if (groups[i] != gid) {
-                       gid_to_sid( &psids[psid_ndx++], groups[i]);
+       {
+               /* Keep the homedir handy */
+               const char *homedir =
+                       pdb_get_homedir(server_info->sam_account);
+               const char *logon_script =
+                       pdb_get_logon_script(server_info->sam_account);
+
+               if (!IS_SAM_DEFAULT(server_info->sam_account,
+                                   PDB_UNIXHOMEDIR)) {
+                       const char *unix_homedir =
+                               pdb_get_unix_homedir(server_info->sam_account);
+                       if (unix_homedir) {
+                               vuser->unix_homedir =
+                                       smb_xstrdup(unix_homedir);
+                       }
+               } else {
+                       struct passwd *passwd =
+                               getpwnam_alloc(NULL, vuser->user.unix_name);
+                       if (passwd) {
+                               vuser->unix_homedir =
+                                       smb_xstrdup(passwd->pw_dir);
+                               TALLOC_FREE(passwd);
+                       }
+               }
+               
+               if (homedir) {
+                       vuser->homedir = smb_xstrdup(homedir);
+               }
+               if (logon_script) {
+                       vuser->logon_script = smb_xstrdup(logon_script);
                }
        }
 
-       if (sup_tok) {
-               /* Now add the additional SIDs from the supplimentary token. */
-               for (i = 0; i < sup_tok->num_sids; i++)
-                       sid_copy( &psids[psid_ndx++], &sup_tok->user_sids[i] );
+       vuser->session_key = session_key;
+
+       DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n", 
+                 (unsigned int)vuser->uid, 
+                 (unsigned int)vuser->gid,
+                 vuser->user.unix_name, vuser->user.smb_name,
+                 vuser->user.domain, vuser->guest ));
+
+       DEBUG(3, ("User name: %s\tReal name: %s\n", vuser->user.unix_name,
+                 vuser->user.full_name));      
+
+       if (server_info->ptok) {
+               vuser->nt_user_token = dup_nt_token(NULL, server_info->ptok);
+       } else {
+               DEBUG(1, ("server_info does not contain a user_token - "
+                         "cannot continue\n"));
+               TALLOC_FREE(server_info);
+               data_blob_free(&session_key);
+               SAFE_FREE(vuser->homedir);
+               SAFE_FREE(vuser->unix_homedir);
+               SAFE_FREE(vuser->logon_script);
+
+               SAFE_FREE(vuser);
+               return UID_FIELD_INVALID;
        }
 
-       /*
-        * Finally add the "standard" SIDs.
-        * The only difference between guest and "anonymous" (which we
-        * don't really support) is the addition of Authenticated_Users.
-        */
+       /* use this to keep tabs on all our info from the authentication */
+       vuser->server_info = server_info;
 
-       sid_copy( &psids[psid_ndx++], &global_sid_World);
-       sid_copy( &psids[psid_ndx++], &global_sid_Network);
+       DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",
+                (int)vuser->uid,vuser->user.unix_name, vuser->vuid));
 
-       if (is_guest)
-               sid_copy( &psids[psid_ndx++], &global_sid_Builtin_Guests);
-       else
-               sid_copy( &psids[psid_ndx++], &global_sid_Authenticated_Users);
+       next_vuid++;
+       num_validated_vuids++;
 
-       token->num_sids = psid_ndx;
+       DLIST_ADD(validated_users, vuser);
+
+       if (!session_claim(vuser)) {
+               DEBUG(1, ("Failed to claim session for vuid=%d\n",
+                         vuser->vuid));
+               invalidate_vuid(vuser->vuid);
+               return UID_FIELD_INVALID;
+       }
 
-       /* Dump list of sids in token */
+       /* Register a home dir service for this user iff
+       
+          (a) This is not a guest connection,
+          (b) we have a home directory defined 
+          (c) there s not an existing static share by that name
+          
+          If a share exists by this name (autoloaded or not) reuse it . */
 
-       for (i = 0; i < token->num_sids; i++) {
-               DEBUG(5, ("user token sid %s\n", 
-                         sid_to_string(sid_str, &token->user_sids[i])));
+       vuser->homes_snum = -1;
+
+       if ( (!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir)) 
+       {
+               int servicenumber = lp_servicenumber(vuser->user.unix_name);
+
+               if ( servicenumber == -1 ) {
+                       DEBUG(3, ("Adding homes service for user '%s' using "
+                                 "home directory: '%s'\n", 
+                               vuser->user.unix_name, vuser->unix_homedir));
+                       vuser->homes_snum =
+                               add_home_service(vuser->user.unix_name, 
+                                                vuser->user.unix_name,
+                                                vuser->unix_homedir);
+               } else {
+                       DEBUG(3, ("Using static (or previously created) "
+                                 "service for user '%s'; path = '%s'\n", 
+                                 vuser->user.unix_name,
+                                 lp_pathname(servicenumber) ));
+                       vuser->homes_snum = servicenumber;
+               }
+       } 
+       
+       if (srv_is_signing_negotiated() && !vuser->guest &&
+           !srv_signing_started()) {
+               /* Try and turn on server signing on the first non-guest
+                * sessionsetup. */
+               srv_set_signing(vuser->session_key, response_blob);
        }
+       
+       /* fill in the current_user_info struct */
+       set_current_user_info( &vuser->user );
+
 
-       return token;
+       return vuser->vuid;
 }
 
 /****************************************************************************
-register a uid/name pair as being valid and that a valid password
-has been given. vuid is biased by an offset. This allows us to
-tell random client vuid's (normally zero) from valid vuids.
+ Add a name to the session users list.
 ****************************************************************************/
 
-int register_vuid(auth_serversupplied_info *server_info, char *smb_name)
+void add_session_user(const char *user)
 {
-       user_struct *vuser = NULL;
-       uid_t *puid;
-       gid_t *pgid;
+       fstring suser;
+       struct passwd *passwd;
 
-       /* Ensure no vuid gets registered in share level security. */
-       if(lp_security() == SEC_SHARE)
-               return UID_FIELD_INVALID;
+       if (!(passwd = Get_Pwnam(user)))
+               return;
 
-       /* Limit allowed vuids to 16bits - VUID_OFFSET. */
-       if (num_validated_vuids >= 0xFFFF-VUID_OFFSET)
-               return UID_FIELD_INVALID;
+       fstrcpy(suser,passwd->pw_name);
 
-       if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
-               DEBUG(0,("Failed to malloc users struct!\n"));
-               return UID_FIELD_INVALID;
-       }
-
-       ZERO_STRUCTP(vuser);
+       if(!*suser)
+               return;
 
-       puid = pdb_get_uid(server_info->sam_account);
-       pgid = pdb_get_gid(server_info->sam_account);
+       if( session_userlist && in_list(suser,session_userlist,False) )
+               return;
 
-       if (!puid || !pgid) {
-               DEBUG(0,("Attempted session setup with invalid user.  No uid/gid in SAM_ACCOUNT\n"));
-               free(vuser);
-               return UID_FIELD_INVALID;
-       }
+       if( !session_userlist ||
+           (strlen(suser) + strlen(session_userlist) + 2 >=
+            len_session_userlist) ) {
+               char *newlist;
 
-       /* Allocate a free vuid. Yes this is a linear search... :-) */
-       while( get_valid_user_struct(next_vuid) != NULL ) {
-               next_vuid++;
-               /* Check for vuid wrap. */
-               if (next_vuid == UID_FIELD_INVALID)
-                       next_vuid = VUID_OFFSET;
+               if (len_session_userlist > 128 * PSTRING_LEN) {
+                       DEBUG(3,("add_session_user: session userlist already "
+                                "too large.\n"));
+                       return;
+               }
+               newlist = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(
+                       session_userlist,
+                       len_session_userlist + PSTRING_LEN );
+               if( newlist == NULL ) {
+                       DEBUG(1,("Unable to resize session_userlist\n"));
+                       return;
+               }
+               if (!session_userlist) {
+                       *newlist = '\0';
+               }
+               session_userlist = newlist;
+               len_session_userlist += PSTRING_LEN;
        }
 
-       DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid ));
-
-       vuser->vuid = next_vuid;
-       vuser->uid = *puid;
-       vuser->gid = *pgid;
-       vuser->guest = server_info->guest;
-       fstrcpy(vuser->user.unix_name, pdb_get_username(server_info->sam_account));
-       fstrcpy(vuser->user.smb_name, smb_name);
-       fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
-       fstrcpy(vuser->user.full_name, pdb_get_fullname(server_info->sam_account));
-
-       DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n", 
-                 (unsigned int)vuser->uid, 
-                 (unsigned int)vuser->gid,
-                 vuser->user.unix_name, vuser->user.smb_name, vuser->user.domain, vuser->guest ));
+       safe_strcat(session_userlist," ",len_session_userlist-1);
+       safe_strcat(session_userlist,suser,len_session_userlist-1);
+}
 
-       DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,vuser->user.full_name));       
+/****************************************************************************
+ Check if a user is in a netgroup user list. If at first we don't succeed,
+ try lower case.
+****************************************************************************/
 
-       vuser->n_groups = 0;
-       vuser->groups  = NULL;
+BOOL user_in_netgroup(const char *user, const char *ngname)
+{
+#ifdef HAVE_NETGROUP
+       static char *mydomain = NULL;
+       fstring lowercase_user;
 
-       /* Find all the groups this uid is in and store them. 
-               Used by change_to_user() */
-       initialise_groups(vuser->user.unix_name, vuser->uid, vuser->gid);
-       get_current_groups( &vuser->n_groups, &vuser->groups);
+       if (mydomain == NULL)
+               yp_get_default_domain(&mydomain);
 
-       if (server_info->ptok)
-               add_supplementary_nt_login_groups(&vuser->n_groups, &vuser->groups, &server_info->ptok);
+       if(mydomain == NULL) {
+               DEBUG(5,("Unable to get default yp domain, let's try without specifying it\n"));
+       }
 
-       /* Create an NT_USER_TOKEN struct for this user. */
-       vuser->nt_user_token = create_nt_token(vuser->uid, vuser->gid, vuser->n_groups, vuser->groups, vuser->guest, server_info->ptok);
+       DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
+               user, mydomain?mydomain:"(ANY)", ngname));
 
-       DEBUG(3,("uid %d registered to name %s\n",(int)vuser->uid,vuser->user.unix_name));
+       if (innetgr(ngname, NULL, user, mydomain)) {
+               DEBUG(5,("user_in_netgroup: Found\n"));
+               return (True);
+       } else {
 
-       next_vuid++;
-       num_validated_vuids++;
+               /*
+                * Ok, innetgr is case sensitive. Try once more with lowercase
+                * just in case. Attempt to fix #703. JRA.
+                */
 
-       DLIST_ADD(validated_users, vuser);
+               fstrcpy(lowercase_user, user);
+               strlower_m(lowercase_user);
+       
+               DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
+                       lowercase_user, mydomain?mydomain:"(ANY)", ngname));
 
-       if (!session_claim(vuser)) {
-               DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
-               invalidate_vuid(vuser->vuid);
-               return -1;
+               if (innetgr(ngname, NULL, lowercase_user, mydomain)) {
+                       DEBUG(5,("user_in_netgroup: Found\n"));
+                       return (True);
+               }
        }
-
-       return vuser->vuid;
+#endif /* HAVE_NETGROUP */
+       return False;
 }
 
-
 /****************************************************************************
-add a name to the session users list
+ Check if a user is in a user list - can check combinations of UNIX
+ and netgroup lists.
 ****************************************************************************/
-void add_session_user(char *user)
+
+BOOL user_in_list(const char *user,const char **list)
 {
-  fstring suser;
-  StrnCpy(suser,user,sizeof(suser)-1);
+       if (!list || !*list)
+               return False;
 
-  if (!Get_Pwnam_Modify(suser)) return;
+       DEBUG(10,("user_in_list: checking user %s in list\n", user));
 
-  if (suser && *suser && !in_list(suser,session_users,False))
-    {
-      if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring))
-       DEBUG(1,("Too many session users??\n"));
-      else
-       {
-         pstrcat(session_users," ");
-         pstrcat(session_users,suser);
+       while (*list) {
+
+               DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
+                         user, *list));
+
+               /*
+                * Check raw username.
+                */
+               if (strequal(user, *list))
+                       return(True);
+
+               /*
+                * Now check to see if any combination
+                * of UNIX and netgroups has been specified.
+                */
+
+               if(**list == '@') {
+                       /*
+                        * Old behaviour. Check netgroup list
+                        * followed by UNIX list.
+                        */
+                       if(user_in_netgroup(user, *list +1))
+                               return True;
+                       if(user_in_group(user, *list +1))
+                               return True;
+               } else if (**list == '+') {
+
+                       if((*(*list +1)) == '&') {
+                               /*
+                                * Search UNIX list followed by netgroup.
+                                */
+                               if(user_in_group(user, *list +2))
+                                       return True;
+                               if(user_in_netgroup(user, *list +2))
+                                       return True;
+
+                       } else {
+
+                               /*
+                                * Just search UNIX list.
+                                */
+
+                               if(user_in_group(user, *list +1))
+                                       return True;
+                       }
+
+               } else if (**list == '&') {
+
+                       if(*(*list +1) == '+') {
+                               /*
+                                * Search netgroup list followed by UNIX list.
+                                */
+                               if(user_in_netgroup(user, *list +2))
+                                       return True;
+                               if(user_in_group(user, *list +2))
+                                       return True;
+                       } else {
+                               /*
+                                * Just search netgroup list.
+                                */
+                               if(user_in_netgroup(user, *list +1))
+                                       return True;
+                       }
+               }
+    
+               list++;
        }
-    }
+       return(False);
 }
 
-
 /****************************************************************************
-check if a username is valid
+ Check if a username is valid.
 ****************************************************************************/
-BOOL user_ok(char *user,int snum)
+
+static BOOL user_ok(const char *user, int snum)
 {
        char **valid, **invalid;
        BOOL ret;
@@ -330,35 +543,55 @@ BOOL user_ok(char *user,int snum)
        ret = True;
 
        if (lp_invalid_users(snum)) {
-               lp_list_copy(&invalid, lp_invalid_users(snum));
-               if (invalid && lp_list_substitute(invalid, "%S", lp_servicename(snum))) {
-                       ret = !user_in_list(user, invalid);
+               str_list_copy(&invalid, lp_invalid_users(snum));
+               if (invalid &&
+                   str_list_substitute(invalid, "%S", lp_servicename(snum))) {
+
+                       /* This is used in sec=share only, so no current user
+                        * around to pass to str_list_sub_basic() */
+
+                       if ( invalid && str_list_sub_basic(invalid, "", "") ) {
+                               ret = !user_in_list(user,
+                                                   (const char **)invalid);
+                       }
                }
        }
-       if (invalid) lp_list_free (&invalid);
+       if (invalid)
+               str_list_free (&invalid);
 
        if (ret && lp_valid_users(snum)) {
-               lp_list_copy(&valid, lp_valid_users(snum));
-               if (valid && lp_list_substitute(valid, "%S", lp_servicename(snum))) {
-                       ret = user_in_list(user,valid);
+               str_list_copy(&valid, lp_valid_users(snum));
+               if ( valid &&
+                    str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
+
+                       /* This is used in sec=share only, so no current user
+                        * around to pass to str_list_sub_basic() */
+
+                       if ( valid && str_list_sub_basic(valid, "", "") ) {
+                               ret = user_in_list(user, (const char **)valid);
+                       }
                }
        }
-       if (valid) lp_list_free (&valid);
+       if (valid)
+               str_list_free (&valid);
 
        if (ret && lp_onlyuser(snum)) {
-               char **user_list = lp_list_make (lp_username(snum));
-               if (user_list && lp_list_substitute(user_list, "%S", lp_servicename(snum))) {
-                       ret = user_in_list(user, user_list);
+               char **user_list = str_list_make (lp_username(snum), NULL);
+               if (user_list &&
+                   str_list_substitute(user_list, "%S",
+                                       lp_servicename(snum))) {
+                       ret = user_in_list(user, (const char **)user_list);
                }
-               if (user_list) lp_list_free (&user_list);
+               if (user_list) str_list_free (&user_list);
        }
 
        return(ret);
 }
 
 /****************************************************************************
-validate a group username entry. Return the username or NULL
+ Validate a group username entry. Return the username or NULL.
 ****************************************************************************/
+
 static char *validate_group(char *group, DATA_BLOB password,int snum)
 {
 #ifdef HAVE_NETGROUP
@@ -403,12 +636,15 @@ static char *validate_group(char *group, DATA_BLOB password,int snum)
                        member = member_list;
 
                        for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
-                               size_t member_len = strlen(gptr->gr_mem[i]) + 1;
-                               if( copied_len + member_len < sizeof(pstring)) { 
+                               size_t member_len = strlen(gptr->gr_mem[i])+1;
+                               if(copied_len+member_len < sizeof(pstring)) { 
 
-                                       DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
+                                       DEBUG(10,("validate_group: = gr_mem = "
+                                                 "%s\n", gptr->gr_mem[i]));
 
-                                       safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
+                                       safe_strcpy(member, gptr->gr_mem[i],
+                                                   sizeof(pstring) -
+                                                   copied_len - 1);
                                        copied_len += member_len;
                                        member += copied_len;
                                } else {
@@ -428,7 +664,8 @@ static char *validate_group(char *group, DATA_BLOB password,int snum)
                                        return(&name[0]);
                                }
 
-                               DEBUG(10,("validate_group = member = %s\n", member));
+                               DEBUG(10,("validate_group = member = %s\n",
+                                         member));
 
                                member += strlen(member) + 1;
                        }
@@ -446,151 +683,116 @@ static char *validate_group(char *group, DATA_BLOB password,int snum)
  Note this is *NOT* used when logging on using sessionsetup_and_X.
 ****************************************************************************/
 
-BOOL authorise_login(int snum,char *user, DATA_BLOB password, 
-                    BOOL *guest,BOOL *force,uint16 vuid)
+BOOL authorise_login(int snum, fstring user, DATA_BLOB password, 
+                    BOOL *guest)
 {
        BOOL ok = False;
-       user_struct *vuser = get_valid_user_struct(vuid);
-
-#if DEBUG_PASSWORD
-       DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s vuid=%d\n",
-                       user,password.data, vuid));
+       
+#ifdef DEBUG_PASSWORD
+       DEBUG(100,("authorise_login: checking authorisation on "
+                  "user=%s pass=%s\n", user,password.data));
 #endif
 
        *guest = False;
   
-       if (GUEST_ONLY(snum))
-               *force = True;
-
-       if (!GUEST_ONLY(snum) && (lp_security() > SEC_SHARE)) {
-
-               /*
-                * We should just use the given vuid from a sessionsetup_and_X.
-                */
-
-               if (!vuser) {
-                       DEBUG(1,("authorise_login: refusing user %s with no session setup\n",
-                                       user));
-                       return False;
-               }
-
-               if ((!vuser->guest && user_ok(vuser->user.unix_name,snum)) || 
-                   (vuser->guest && GUEST_OK(snum))) {
-                       fstrcpy(user,vuser->user.unix_name);
-                       *guest = vuser->guest;
-                       DEBUG(3,("authorise_login: ACCEPTED: validated based on vuid as %sguest \
-(user=%s)\n", vuser->guest ? "" : "non-", user));
-                       return True;
-               }
-       }
        /* there are several possibilities:
                1) login as the given user with given password
-               2) login as a previously registered username with the given password
+               2) login as a previously registered username with the given 
+                  password
                3) login as a session list username with the given password
                4) login as a previously validated user/password pair
                5) login as the "user =" user with given password
-               6) login as the "user =" user with no password (guest connection)
+               6) login as the "user =" user with no password 
+                  (guest connection)
                7) login as guest user with no password
 
                if the service is guest_only then steps 1 to 5 are skipped
        */
 
-       if (!(GUEST_ONLY(snum) && GUEST_OK(snum))) {
-               /* check for a previously registered guest username */
-               if (!ok && (vuser != 0) && vuser->guest) {        
-                       if (user_ok(vuser->user.unix_name,snum) &&
-                                       password_ok(vuser->user.unix_name, password)) {
-                               fstrcpy(user, vuser->user.unix_name);
-                               *guest = False;
-                               DEBUG(3,("authorise_login: ACCEPTED: given password with registered user %s\n", user));
+       /* now check the list of session users */
+       if (!ok) {
+               char *auser;
+               char *user_list = NULL;
+
+               if ( session_userlist )
+                       user_list = SMB_STRDUP(session_userlist);
+               else
+                       user_list = SMB_STRDUP("");
+
+               if (!user_list)
+                       return(False);
+               
+               for (auser=strtok(user_list,LIST_SEP); !ok && auser;
+                    auser = strtok(NULL,LIST_SEP)) {
+                       fstring user2;
+                       fstrcpy(user2,auser);
+                       if (!user_ok(user2,snum))
+                               continue;
+                       
+                       if (password_ok(user2,password)) {
                                ok = True;
+                               fstrcpy(user,user2);
+                               DEBUG(3,("authorise_login: ACCEPTED: session "
+                                        "list username (%s) and given "
+                                        "password ok\n", user));
                        }
                }
 
-               /* now check the list of session users */
-               if (!ok) {
-                       char *auser;
-                       char *user_list = strdup(session_users);
-                       if (!user_list)
-                               return(False);
-
-                       for (auser=strtok(user_list,LIST_SEP); !ok && auser;
-                                                                       auser = strtok(NULL,LIST_SEP)) {
+               SAFE_FREE(user_list);
+       }
+       
+       /* check the user= fields and the given password */
+       if (!ok && lp_username(snum)) {
+               char *auser;
+               pstring user_list;
+               pstrcpy(user_list,lp_username(snum));
+               
+               pstring_sub(user_list,"%S",lp_servicename(snum));
+               
+               for (auser=strtok(user_list,LIST_SEP); auser && !ok;
+                    auser = strtok(NULL,LIST_SEP)) {
+                       if (*auser == '@') {
+                               auser = validate_group(auser+1,password,snum);
+                               if (auser) {
+                                       ok = True;
+                                       fstrcpy(user,auser);
+                                       DEBUG(3,("authorise_login: ACCEPTED: "
+                                                "group username and given "
+                                                "password ok (%s)\n", user));
+                               }
+                       } else {
                                fstring user2;
                                fstrcpy(user2,auser);
-                               if (!user_ok(user2,snum))
-                                       continue;
-                 
-                               if (password_ok(user2,password)) {
+                               if (user_ok(user2,snum) &&
+                                   password_ok(user2,password)) {
                                        ok = True;
                                        fstrcpy(user,user2);
-                                       DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
-and given password ok\n", user));
-                               }
-                       }
-
-                       SAFE_FREE(user_list);
-               }
-
-               /* check for a previously validated username/password pair */
-               if (!ok && (lp_security() > SEC_SHARE) && (vuser != 0) && !vuser->guest &&
-                                                       user_ok(vuser->user.unix_name,snum)) {
-                       fstrcpy(user,vuser->user.unix_name);
-                       *guest = False;
-                       DEBUG(3,("authorise_login: ACCEPTED: validated uid (%s) as non-guest\n",
-                               user));
-                       ok = True;
-               }
-
-               /* check the user= fields and the given password */
-               if (!ok && lp_username(snum)) {
-                       char *auser;
-                       pstring user_list;
-                       StrnCpy(user_list,lp_username(snum),sizeof(pstring));
-
-                       pstring_sub(user_list,"%S",lp_servicename(snum));
-         
-                       for (auser=strtok(user_list,LIST_SEP); auser && !ok;
-                                                                                       auser = strtok(NULL,LIST_SEP)) {
-                               if (*auser == '@') {
-                                       auser = validate_group(auser+1,password,snum);
-                                       if (auser) {
-                                               ok = True;
-                                               fstrcpy(user,auser);
-                                               DEBUG(3,("authorise_login: ACCEPTED: group username \
-and given password ok (%s)\n", user));
-                                       }
-                               } else {
-                                       fstring user2;
-                                       fstrcpy(user2,auser);
-                                       if (user_ok(user2,snum) && password_ok(user2,password)) {
-                                               ok = True;
-                                               fstrcpy(user,user2);
-                                               DEBUG(3,("authorise_login: ACCEPTED: user list username \
-and given password ok (%s)\n", user));
-                                       }
+                                       DEBUG(3,("authorise_login: ACCEPTED: "
+                                                "user list username and "
+                                                "given password ok (%s)\n",
+                                                user));
                                }
                        }
                }
-       } /* not guest only */
+       }
 
        /* check for a normal guest connection */
        if (!ok && GUEST_OK(snum)) {
                fstring guestname;
-               StrnCpy(guestname,lp_guestaccount(),sizeof(guestname)-1);
+               fstrcpy(guestname,lp_guestaccount());
                if (Get_Pwnam(guestname)) {
                        fstrcpy(user,guestname);
                        ok = True;
-                       DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
-                                       user));
+                       DEBUG(3,("authorise_login: ACCEPTED: guest account "
+                                "and guest ok (%s)\n", user));
                } else {
-                       DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
+                       DEBUG(0,("authorise_login: Invalid guest account "
+                                "%s??\n",guestname));
                }
                *guest = True;
        }
 
-       if (ok && !user_ok(user,snum)) {
+       if (ok && !user_ok(user, snum)) {
                DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
                ok = False;
        }