r23346: Fix offline caching with XP/Vista. It was an off-by-one
[tprouty/samba.git] / source / smbd / uid.c
index 09cf8995220c679b161ef34072d15a2c92d03549..c6d4e3329c8b9ff8513c20c8eed2dd257dc51e7c 100644 (file)
 /* what user is current? */
 extern struct current_user current_user;
 
+/****************************************************************************
+ Iterator functions for getting all gid's from current_user.
+****************************************************************************/
+
+gid_t get_current_user_gid_first(int *piterator)
+{
+       *piterator = 0;
+       return current_user.ut.gid;
+}
+
+gid_t get_current_user_gid_next(int *piterator)
+{
+       gid_t ret;
+
+       if (!current_user.ut.groups || *piterator >= current_user.ut.ngroups) {
+               return (gid_t)-1;
+       }
+
+       ret = current_user.ut.groups[*piterator];
+       (*piterator) += 1;
+       return ret;
+}
+
 /****************************************************************************
  Become the guest user without changing the security context stack.
 ****************************************************************************/
@@ -33,7 +56,7 @@ BOOL change_to_guest(void)
 
        if (!pass) {
                /* Don't need to free() this as its stored in a static */
-               pass = getpwnam_alloc(lp_guestaccount());
+               pass = getpwnam_alloc(NULL, lp_guestaccount());
                if (!pass)
                        return(False);
        }
@@ -48,9 +71,10 @@ BOOL change_to_guest(void)
        
        current_user.conn = NULL;
        current_user.vuid = UID_FIELD_INVALID;
-       
-       passwd_free(&pass);
 
+       TALLOC_FREE(pass);
+       pass = NULL;
+       
        return True;
 }
 
@@ -60,34 +84,67 @@ BOOL change_to_guest(void)
 
 static BOOL check_user_ok(connection_struct *conn, user_struct *vuser,int snum)
 {
-       unsigned i;
-       for (i=0;i<conn->vuid_cache.entries && i< VUID_CACHE_SIZE;i++)
-               if (conn->vuid_cache.list[i] == vuser->vuid)
+       unsigned int i;
+       struct vuid_cache_entry *ent = NULL;
+       BOOL readonly_share;
+       NT_USER_TOKEN *token;
+
+       for (i=0;i<conn->vuid_cache.entries && i< VUID_CACHE_SIZE;i++) {
+               if (conn->vuid_cache.array[i].vuid == vuser->vuid) {
+                       ent = &conn->vuid_cache.array[i];
+                       conn->read_only = ent->read_only;
+                       conn->admin_user = ent->admin_user;
                        return(True);
-
-       if ((conn->force_user || conn->force_group) 
-           && (conn->vuid != vuser->vuid)) {
-               return False;
+               }
        }
-       
-       if (!user_ok(vuser->user.unix_name,snum, vuser->groups, vuser->n_groups))
+
+       if (!user_ok_token(vuser->user.unix_name, vuser->nt_user_token, snum))
                return(False);
 
-       if (!share_access_check(conn, snum, vuser, conn->read_only ? FILE_READ_DATA : FILE_WRITE_DATA)) {
+       readonly_share = is_share_read_only_for_token(vuser->user.unix_name,
+                                                     vuser->nt_user_token,
+                                                     SNUM(conn));
+
+       token = conn->nt_user_token ?
+               conn->nt_user_token : vuser->nt_user_token;
+
+       if (!readonly_share &&
+           !share_access_check(token, lp_servicename(snum),
+                               FILE_WRITE_DATA)) {
+               /* smb.conf allows r/w, but the security descriptor denies
+                * write. Fall back to looking at readonly. */
+               readonly_share = True;
+               DEBUG(5,("falling back to read-only access-evaluation due to "
+                        "security descriptor\n"));
+       }
+
+       if (!share_access_check(token, lp_servicename(snum),
+                               readonly_share ?
+                               FILE_READ_DATA : FILE_WRITE_DATA)) {
                return False;
        }
 
        i = conn->vuid_cache.entries % VUID_CACHE_SIZE;
-       conn->vuid_cache.list[i] = vuser->vuid;
+       if (conn->vuid_cache.entries < VUID_CACHE_SIZE)
+               conn->vuid_cache.entries++;
+
+       ent = &conn->vuid_cache.array[i];
+       ent->vuid = vuser->vuid;
+       ent->read_only = readonly_share;
+
+       ent->admin_user = token_contains_name_in_list(
+               vuser->user.unix_name, NULL, vuser->nt_user_token,
+               lp_admin_users(SNUM(conn)));
 
-       conn->vuid_cache.entries++;
+       conn->read_only = ent->read_only;
+       conn->admin_user = ent->admin_user;
 
        return(True);
 }
 
 /****************************************************************************
  Become the user of a connection number without changing the security context
- stack, but modify the currnet_user entries.
+ stack, but modify the current_user entries.
 ****************************************************************************/
 
 BOOL change_to_user(connection_struct *conn, uint16 vuid)
@@ -99,7 +156,9 @@ BOOL change_to_user(connection_struct *conn, uint16 vuid)
        char group_c;
        BOOL must_free_token = False;
        NT_USER_TOKEN *token = NULL;
-
+       int num_groups = 0;
+       gid_t *group_list = NULL;
+       
        if (!conn) {
                DEBUG(2,("change_to_user: Connection not open\n"));
                return(False);
@@ -113,32 +172,43 @@ BOOL change_to_user(connection_struct *conn, uint16 vuid)
         */
 
        if((lp_security() == SEC_SHARE) && (current_user.conn == conn) &&
-          (current_user.uid == conn->uid)) {
-               DEBUG(4,("change_to_user: Skipping user change - already user\n"));
+          (current_user.ut.uid == conn->uid)) {
+               DEBUG(4,("change_to_user: Skipping user change - already "
+                        "user\n"));
                return(True);
        } else if ((current_user.conn == conn) && 
                   (vuser != 0) && (current_user.vuid == vuid) && 
-                  (current_user.uid == vuser->uid)) {
-               DEBUG(4,("change_to_user: Skipping user change - already user\n"));
+                  (current_user.ut.uid == vuser->uid)) {
+               DEBUG(4,("change_to_user: Skipping user change - already "
+                        "user\n"));
                return(True);
        }
 
        snum = SNUM(conn);
 
+       if ((vuser) && !check_user_ok(conn, vuser, snum)) {
+               DEBUG(2,("change_to_user: SMB user %s (unix user %s, vuid %d) "
+                        "not permitted access to share %s.\n",
+                        vuser->user.smb_name, vuser->user.unix_name, vuid,
+                        lp_servicename(snum)));
+               return False;
+       }
+
        if (conn->force_user) /* security = share sets this too */ {
                uid = conn->uid;
                gid = conn->gid;
-               current_user.groups = conn->groups;
-               current_user.ngroups = conn->ngroups;
+               group_list = conn->groups;
+               num_groups = conn->ngroups;
                token = conn->nt_user_token;
-       } else if ((vuser) && check_user_ok(conn, vuser, snum)) {
-               uid = vuser->uid;
+       } else if (vuser) {
+               uid = conn->admin_user ? 0 : vuser->uid;
                gid = vuser->gid;
-               current_user.ngroups = vuser->n_groups;
-               current_user.groups  = vuser->groups;
+               num_groups = vuser->n_groups;
+               group_list  = vuser->groups;
                token = vuser->nt_user_token;
        } else {
-               DEBUG(2,("change_to_user: Invalid vuid used %d or vuid not permitted access to share.\n",vuid));
+               DEBUG(2,("change_to_user: Invalid vuid used %d in accessing "
+                        "share %s.\n",vuid, lp_servicename(snum) ));
                return False;
        }
 
@@ -149,7 +219,13 @@ BOOL change_to_user(connection_struct *conn, uint16 vuid)
         */
 
        if((group_c = *lp_force_group(snum))) {
-               BOOL is_guest = False;
+
+               token = dup_nt_token(NULL, token);
+               if (token == NULL) {
+                       DEBUG(0, ("dup_nt_token failed\n"));
+                       return False;
+               }
+               must_free_token = True;
 
                if(group_c == '+') {
 
@@ -161,40 +237,34 @@ BOOL change_to_user(connection_struct *conn, uint16 vuid)
                         */
 
                        int i;
-                       for (i = 0; i < current_user.ngroups; i++) {
-                               if (current_user.groups[i] == conn->gid) {
+                       for (i = 0; i < num_groups; i++) {
+                               if (group_list[i] == conn->gid) {
                                        gid = conn->gid;
+                                       gid_to_sid(&token->user_sids[1], gid);
                                        break;
                                }
                        }
                } else {
                        gid = conn->gid;
+                       gid_to_sid(&token->user_sids[1], gid);
                }
-
-               /*
-                * We've changed the group list in the token - we must
-                * re-create it.
-                */
-
-               if (vuser && vuser->guest)
-                       is_guest = True;
-
-               token = create_nt_token(uid, gid, current_user.ngroups, current_user.groups, is_guest);
-               if (!token) {
-                       DEBUG(1, ("change_to_user: create_nt_token failed!\n"));
-                       return False;
-               }
-               must_free_token = True;
        }
        
-       set_sec_ctx(uid, gid, current_user.ngroups, current_user.groups, token);
+       /* Now set current_user since we will immediately also call
+          set_sec_ctx() */
+
+       current_user.ut.ngroups = num_groups;
+       current_user.ut.groups  = group_list;   
+
+       set_sec_ctx(uid, gid, current_user.ut.ngroups, current_user.ut.groups,
+                   token);
 
        /*
         * Free the new token (as set_sec_ctx copies it).
         */
 
        if (must_free_token)
-               delete_nt_token(&token);
+               TALLOC_FREE(token);
 
        current_user.conn = conn;
        current_user.vuid = vuid;
@@ -234,8 +304,9 @@ BOOL become_authenticated_pipe_user(pipes_struct *p)
        if (!push_sec_ctx())
                return False;
 
-       set_sec_ctx(p->pipe_user.uid, p->pipe_user.gid, 
-                   p->pipe_user.ngroups, p->pipe_user.groups, p->pipe_user.nt_user_token);
+       set_sec_ctx(p->pipe_user.ut.uid, p->pipe_user.ut.gid, 
+                   p->pipe_user.ut.ngroups, p->pipe_user.ut.groups,
+                   p->pipe_user.nt_user_token);
 
        return True;
 }
@@ -310,17 +381,6 @@ static void pop_conn_ctx(void)
        ctx_p->vuid = UID_FIELD_INVALID;
 }
 
-void init_conn_ctx(void)
-{
-    int i;
-    /* Initialise connection context stack */
-       for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
-               conn_ctx_stack[i].conn = NULL;
-               conn_ctx_stack[i].vuid = UID_FIELD_INVALID;
-    }
-}
-
 /****************************************************************************
  Temporarily become a root user.  Must match with unbecome_root(). Saves and
  restores the connection context.
@@ -368,4 +428,3 @@ BOOL unbecome_user(void)
        pop_conn_ctx();
        return True;
 }
-