Final part of patchset to fix bug #8556 - ACL permissions ignored when SMBsetatr...
[kai/samba.git] / source3 / smbd / reply.c
index a804884ef7be9f564e96c8ea0af7391803fca8f6..7dd3260f0b05532a992cc26b447b9070c45835c5 100644 (file)
@@ -39,6 +39,7 @@
 #include "libsmb/nmblib.h"
 #include "auth.h"
 #include "smbprofile.h"
+#include "../lib/tsocket/tsocket.h"
 
 /****************************************************************************
  Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
@@ -481,7 +482,7 @@ static bool netbios_session_retarget(struct smbd_server_connection *sconn,
 
        if (!srv_send_smb(sconn, (char *)outbuf, false, 0, false,
                          NULL)) {
-               exit_server_cleanly("netbios_session_regarget: srv_send_smb "
+               exit_server_cleanly("netbios_session_retarget: srv_send_smb "
                                    "failed.");
        }
 
@@ -518,7 +519,7 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
        smb_setlen(outbuf,0);
 
        switch (msg_type) {
-       case 0x81: /* session request */
+       case NBSSrequest: /* session request */
        {
                /* inbuf_size is guarenteed to be at least 4. */
                fstring name1,name2;
@@ -531,7 +532,7 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
                        exit_server_cleanly("multiple session request not permitted");
                }
 
-               SCVAL(outbuf,0,0x82);
+               SCVAL(outbuf,0,NBSSpositive);
                SCVAL(outbuf,3,0);
 
                /* inbuf_size is guaranteed to be at least 4. */
@@ -572,7 +573,15 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
                 */
                if (strequal(name1, "*SMBSERVER     ")
                    || strequal(name1, "*SMBSERV       "))  {
-                       fstrcpy(name1, sconn->client_id.addr);
+                       char *raddr;
+
+                       raddr = tsocket_address_inet_addr_string(sconn->remote_address,
+                                                                talloc_tos());
+                       if (raddr == NULL) {
+                               exit_server_cleanly("could not allocate raddr");
+                       }
+
+                       fstrcpy(name1, raddr);
                }
 
                set_local_machine_name(name1, True);
@@ -605,17 +614,17 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
 
        case 0x89: /* session keepalive request 
                      (some old clients produce this?) */
-               SCVAL(outbuf,0,SMBkeepalive);
+               SCVAL(outbuf,0,NBSSkeepalive);
                SCVAL(outbuf,3,0);
                break;
 
-       case 0x82: /* positive session response */
-       case 0x83: /* negative session response */
-       case 0x84: /* retarget session response */
+       case NBSSpositive: /* positive session response */
+       case NBSSnegative: /* negative session response */
+       case NBSSretarget: /* retarget session response */
                DEBUG(0,("Unexpected session response\n"));
                break;
 
-       case SMBkeepalive: /* session keepalive */
+       case NBSSkeepalive: /* session keepalive */
        default:
                return;
        }
@@ -849,9 +858,7 @@ void reply_tcon_and_X(struct smb_request *req)
                                perm1 = FILE_ALL_ACCESS;
                                perm2 = FILE_ALL_ACCESS;
                        } else {
-                               perm1 = CAN_WRITE(conn) ?
-                                               SHARE_ALL_ACCESS :
-                                               SHARE_READ_ONLY;
+                               perm1 = conn->share_access;
                        }
 
                        SIVAL(req->outbuf, smb_vwv3, perm1);
@@ -1262,19 +1269,19 @@ void reply_setatr(struct smb_request *req)
        mode = SVAL(req->vwv+0, 0);
        mtime = srv_make_unix_date3(req->vwv+1);
 
-       ft.mtime = convert_time_t_to_timespec(mtime);
-       status = smb_set_file_time(conn, NULL, smb_fname, &ft, true);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
        if (mode != FILE_ATTRIBUTE_NORMAL) {
                if (VALID_STAT_OF_DIR(smb_fname->st))
                        mode |= FILE_ATTRIBUTE_DIRECTORY;
                else
                        mode &= ~FILE_ATTRIBUTE_DIRECTORY;
 
+               status = check_access(conn, NULL, smb_fname,
+                                       FILE_WRITE_ATTRIBUTES);
+               if (!NT_STATUS_IS_OK(status)) {
+                       reply_nterror(req, status);
+                       goto out;
+               }
+
                if (file_set_dosmode(conn, smb_fname, mode, NULL,
                                     false) != 0) {
                        reply_nterror(req, map_nt_error_from_unix(errno));
@@ -1282,6 +1289,13 @@ void reply_setatr(struct smb_request *req)
                }
        }
 
+       ft.mtime = convert_time_t_to_timespec(mtime);
+       status = smb_set_file_time(conn, NULL, smb_fname, &ft, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+               goto out;
+       }
+
        reply_outbuf(req, 0, 0);
 
        DEBUG(3, ("setatr name=%s mode=%d\n", smb_fname_str_dbg(smb_fname),
@@ -1782,7 +1796,7 @@ void reply_open(struct smb_request *req)
                goto out;
        }
 
-       if (!map_open_params_to_ntcreate(smb_fname, deny_mode,
+       if (!map_open_params_to_ntcreate(smb_fname->base_name, deny_mode,
                                         OPENX_FILE_EXISTS_OPEN, &access_mask,
                                         &share_mode, &create_disposition,
                                         &create_options, &private_flags)) {
@@ -1809,7 +1823,7 @@ void reply_open(struct smb_request *req)
                &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -1957,7 +1971,8 @@ void reply_open_and_X(struct smb_request *req)
                goto out;
        }
 
-       if (!map_open_params_to_ntcreate(smb_fname, deny_mode, smb_ofun,
+       if (!map_open_params_to_ntcreate(smb_fname->base_name, deny_mode,
+                                        smb_ofun,
                                         &access_mask, &share_mode,
                                         &create_disposition,
                                         &create_options,
@@ -1985,7 +2000,7 @@ void reply_open_and_X(struct smb_request *req)
                &smb_action);                           /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -2207,7 +2222,7 @@ void reply_mknew(struct smb_request *req)
                NULL);                                  /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -2339,7 +2354,7 @@ void reply_ctemp(struct smb_request *req)
        close(tmpfd);
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -2556,7 +2571,7 @@ static NTSTATUS do_unlink(connection_struct *conn,
        }
 
        /* The set is across all open files on this dev/inode pair. */
-       if (!set_delete_on_close(fsp, True, &conn->session_info->utok)) {
+       if (!set_delete_on_close(fsp, True, conn->session_info->unix_token)) {
                close_file(req, fsp, NORMAL_CLOSE);
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -2814,7 +2829,7 @@ void reply_unlink(struct smb_request *req)
        status = unlink_internals(conn, req, dirtype, smb_fname,
                                  path_contains_wcard);
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -3150,7 +3165,7 @@ void reply_readbraw(struct smb_request *req)
        START_PROFILE(SMBreadbraw);
 
        if (srv_is_signing_active(sconn) ||
-           is_encrypted_packet(req->inbuf)) {
+           is_encrypted_packet(sconn, req->inbuf)) {
                exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
                        "raw reads/writes are disallowed.");
        }
@@ -3574,7 +3589,8 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
         */
 
        if (!req_is_in_chain(req) &&
-           !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
+           !is_encrypted_packet(req->sconn, req->inbuf) &&
+           (fsp->base_fsp == NULL) &&
            (fsp->wcp == NULL) &&
            lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
                uint8 headerbuf[smb_size + 12 * 2];
@@ -3729,6 +3745,7 @@ nosendfile_read:
 
 void reply_read_and_X(struct smb_request *req)
 {
+       struct smbd_server_connection *sconn = req->sconn;
        connection_struct *conn = req->conn;
        files_struct *fsp;
        SMB_OFF_T startpos;
@@ -3767,7 +3784,15 @@ void reply_read_and_X(struct smb_request *req)
                return;
        }
 
-       if (global_client_caps & CAP_LARGE_READX) {
+       if ((sconn->smb1.unix_info.client_cap_low & CIFS_UNIX_LARGE_READ_CAP) ||
+           (get_remote_arch() == RA_SAMBA)) {
+               /*
+                * This is Samba only behavior (up to Samba 3.6)!
+                *
+                * Windows 2008 R2 ignores the upper_size,
+                * so we do unless unix extentions are active
+                * or "smbclient" is talking to us.
+                */
                size_t upper_size = SVAL(req->vwv+7, 0);
                smb_maxcnt |= (upper_size<<16);
                if (upper_size > 1) {
@@ -3779,7 +3804,7 @@ void reply_read_and_X(struct smb_request *req)
                        }
                        /* We currently don't do this on signed or sealed data. */
                        if (srv_is_signing_active(req->sconn) ||
-                           is_encrypted_packet(req->inbuf)) {
+                           is_encrypted_packet(req->sconn, req->inbuf)) {
                                reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
                                END_PROFILE(SMBreadX);
                                return;
@@ -3873,9 +3898,9 @@ void error_to_writebrawerr(struct smb_request *req)
 static NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout,
                                size_t *len)
 {
-       uint8_t msgtype = SMBkeepalive;
+       uint8_t msgtype = NBSSkeepalive;
 
-       while (msgtype == SMBkeepalive) {
+       while (msgtype == NBSSkeepalive) {
                NTSTATUS status;
 
                status = read_smb_length_return_keepalive(fd, inbuf, timeout,
@@ -4129,7 +4154,7 @@ void reply_writebraw(struct smb_request *req)
 #if RABBIT_PELLET_FIX
                /*
                 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
-                * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
+                * sending a NBSSkeepalive. Thanks to DaveCB at Sun for this.
                 * JRA.
                 */
                if (!send_keepalive(req->sconn->sock)) {
@@ -4406,7 +4431,7 @@ bool is_valid_writeX_buffer(struct smbd_server_connection *sconn,
        unsigned int doff = 0;
        size_t len = smb_len_large(inbuf);
 
-       if (is_encrypted_packet(inbuf)) {
+       if (is_encrypted_packet(sconn, inbuf)) {
                /* Can't do this on encrypted
                 * connections. */
                return false;
@@ -5355,7 +5380,7 @@ void reply_printqueue(struct smb_request *req)
                status = rpc_pipe_open_interface(conn,
                                                 &ndr_table_spoolss.syntax_id,
                                                 conn->session_info,
-                                                &conn->sconn->client_id,
+                                                conn->sconn->remote_address,
                                                 conn->sconn->msg_ctx,
                                                 &cli);
                if (!NT_STATUS_IS_OK(status)) {
@@ -5655,7 +5680,7 @@ void reply_rmdir(struct smb_request *req)
                &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -5670,7 +5695,7 @@ void reply_rmdir(struct smb_request *req)
                goto out;
        }
 
-       if (!set_delete_on_close(fsp, true, &conn->session_info->utok)) {
+       if (!set_delete_on_close(fsp, true, conn->session_info->unix_token)) {
                close_file(req, fsp, ERROR_CLOSE);
                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
@@ -5950,6 +5975,47 @@ static void notify_rename(connection_struct *conn, bool is_dir,
        TALLOC_FREE(parent_dir_dst);
 }
 
+/****************************************************************************
+ Returns an error if the parent directory for a filename is open in an
+ incompatible way.
+****************************************************************************/
+
+static NTSTATUS parent_dirname_compatible_open(connection_struct *conn,
+                                       const struct smb_filename *smb_fname_dst_in)
+{
+       char *parent_dir = NULL;
+       struct smb_filename smb_fname_parent;
+       struct file_id id;
+       files_struct *fsp = NULL;
+       int ret;
+
+       if (!parent_dirname(talloc_tos(), smb_fname_dst_in->base_name,
+                       &parent_dir, NULL)) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       ZERO_STRUCT(smb_fname_parent);
+       smb_fname_parent.base_name = parent_dir;
+
+       ret = SMB_VFS_LSTAT(conn, &smb_fname_parent);
+       if (ret == -1) {
+               return map_nt_error_from_unix(errno);
+       }
+
+       /*
+        * We're only checking on this smbd here, mostly good
+        * enough.. and will pass tests.
+        */
+
+       id = vfs_file_id_from_sbuf(conn, &smb_fname_parent.st);
+       for (fsp = file_find_di_first(conn->sconn, id); fsp;
+                       fsp = file_find_di_next(fsp)) {
+               if (fsp->access_mask & DELETE_ACCESS) {
+                       return NT_STATUS_SHARING_VIOLATION;
+                }
+        }
+       return NT_STATUS_OK;
+}
+
 /****************************************************************************
  Rename an open file - given an fsp.
 ****************************************************************************/
@@ -5971,6 +6037,11 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                return status;
        }
 
+       status = parent_dirname_compatible_open(conn, smb_fname_dst_in);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
        /* Make a copy of the dst smb_fname structs */
 
        status = copy_smb_filename(ctx, smb_fname_dst_in, &smb_fname_dst);
@@ -6670,7 +6741,7 @@ void reply_mv(struct smb_request *req)
                                  attrs, False, src_has_wcard, dest_has_wcard,
                                  DELETE_ACCESS);
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -6748,7 +6819,8 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
        if (!target_is_directory && count) {
                new_create_disposition = FILE_OPEN;
        } else {
-               if (!map_open_params_to_ntcreate(smb_fname_dst_tmp, 0, ofun,
+               if (!map_open_params_to_ntcreate(smb_fname_dst_tmp->base_name,
+                                                0, ofun,
                                                 NULL, NULL,
                                                 &new_create_disposition,
                                                 NULL,
@@ -7887,6 +7959,11 @@ void reply_setattrE(struct smb_request *req)
                goto out;
        }
 
+       if (!(fsp->access_mask & FILE_WRITE_ATTRIBUTES)) {
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+               goto out;
+       }
+
        status = smb_set_file_time(conn, fsp, fsp->fsp_name, &ft, true);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);