s3-tevent: only include ../lib/util/tevent wrappers where needed.
[nivanova/samba-autobuild/.git] / source3 / smbd / smb2_create.c
index 25e995c3bd1c389858e966d36c47f8a2549bbdfa..73b8ade5bd36bee7b7e26564ad84b7de1a5e2ac9 100644 (file)
 */
 
 #include "includes.h"
+#include "printing.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "../libcli/smb/smb_common.h"
+#include "../librpc/gen_ndr/ndr_security.h"
+#include "../lib/util/tevent_ntstatus.h"
 
 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
 {
@@ -87,6 +91,7 @@ static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
                        uint64_t *out_allocation_size,
                        uint64_t *out_end_of_file,
                        uint32_t *out_file_attributes,
+                       uint64_t *out_file_id_persistent,
                        uint64_t *out_file_id_volatile,
                        struct smb2_create_blobs *out_context_blobs);
 
@@ -209,7 +214,7 @@ NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
                                   in_name_buffer.data,
                                   in_name_buffer.length,
                                   &in_name_string,
-                                  &in_name_string_size, false);
+                                  &in_name_string_size);
        if (!ok) {
                return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
        }
@@ -264,6 +269,7 @@ static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
        uint64_t out_allocation_size = 0;
        uint64_t out_end_of_file = 0;
        uint32_t out_file_attributes = 0;
+       uint64_t out_file_id_persistent = 0;
        uint64_t out_file_id_volatile = 0;
        struct smb2_create_blobs out_context_blobs;
        DATA_BLOB out_context_buffer;
@@ -295,6 +301,7 @@ static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
                                       &out_allocation_size,
                                       &out_end_of_file,
                                       &out_file_attributes,
+                                      &out_file_id_persistent,
                                       &out_file_id_volatile,
                                       &out_context_blobs);
        if (!NT_STATUS_IS_OK(status)) {
@@ -356,7 +363,8 @@ static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
        SIVAL(outbody.data, 0x38,
              out_file_attributes);             /* file attributes */
        SIVAL(outbody.data, 0x3C, 0);           /* reserved */
-       SBVAL(outbody.data, 0x40, 0);           /* file id (persistent) */
+       SBVAL(outbody.data, 0x40,
+             out_file_id_persistent);          /* file id (persistent) */
        SBVAL(outbody.data, 0x48,
              out_file_id_volatile);            /* file id (volatile) */
        SIVAL(outbody.data, 0x50,
@@ -391,6 +399,7 @@ struct smbd_smb2_create_state {
        uint64_t out_allocation_size;
        uint64_t out_end_of_file;
        uint32_t out_file_attributes;
+       uint64_t out_file_id_persistent;
        uint64_t out_file_id_volatile;
        struct smb2_create_blobs out_context_blobs;
 };
@@ -416,9 +425,17 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
        int info;
        struct timespec write_time_ts;
        struct smb2_create_blobs out_context_blobs;
+       int requested_oplock_level;
 
        ZERO_STRUCT(out_context_blobs);
 
+       if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
+               requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
+       } else {
+               requested_oplock_level = in_oplock_level;
+       }
+
+
        if (!smb2req->async) {
                /* New create call. */
                req = tevent_req_create(mem_ctx, &state,
@@ -472,11 +489,8 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        return tevent_req_post(req, ev);
                }
 
-               status = print_fsp_open(smb1req,
-                                       smb1req->conn,
-                                       in_name,
-                                       smb1req->vuid,
-                                       result);
+               status = print_spool_open(result, in_name,
+                                         smb1req->vuid);
                if (!NT_STATUS_IS_OK(status)) {
                        file_free(smb1req, result);
                        tevent_req_nterror(req, status);
@@ -566,7 +580,7 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        }
 
                        ndr_err = ndr_pull_struct_blob(&secd->data,
-                               sec_desc, NULL, sec_desc,
+                               sec_desc, sec_desc,
                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
                        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                                DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
@@ -660,11 +674,18 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
                in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
 
-               /* convert '\\' into '/' */
-               status = check_path_syntax(fname);
-               if (!NT_STATUS_IS_OK(status)) {
-                       tevent_req_nterror(req, status);
-                       return tevent_req_post(req, ev);
+                /*
+                * For a DFS path the function parse_dfs_path()
+                * will do the path processing.
+                */
+
+               if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
+                       /* convert '\\' into '/' */
+                       status = check_path_syntax(fname);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               tevent_req_nterror(req, status);
+                               return tevent_req_post(req, ev);
+                       }
                }
 
                status = filename_convert(req,
@@ -690,7 +711,7 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                             in_create_disposition,
                                             in_create_options,
                                             in_file_attributes,
-                                            map_smb2_oplock_levels_to_samba(in_oplock_level),
+                                            map_smb2_oplock_levels_to_samba(requested_oplock_level),
                                             allocation_size,
                                             0, /* private_flags */
                                             sec_desc,
@@ -736,13 +757,18 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
 
                if (qfid) {
                        uint8_t p[32];
+                       uint64_t file_index = get_FileIndex(result->conn,
+                                                       &result->fsp_name->st);
                        DATA_BLOB blob = data_blob_const(p, sizeof(p));
 
                        ZERO_STRUCT(p);
 
-                       /* TODO: maybe use result->file_id */
-                       SIVAL(p, 0, result->fsp_name->st.st_ex_ino);/* FileIndexLow */
-                       SIVAL(p, 4, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
+                       /* From conversations with Microsoft engineers at
+                          the MS plugfest. The first 8 bytes are the "volume index"
+                          == inode, the second 8 bytes are the "volume id",
+                          == dev. This will be updated in the SMB2 doc. */
+                       SBVAL(p, 0, file_index);
+                       SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
 
                        status = smb2_create_blob_add(state, &out_context_blobs,
                                                      SMB2_CREATE_TAG_QFID,
@@ -756,7 +782,11 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
 
        smb2req->compat_chain_fsp = smb1req->chain_fsp;
 
-       state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
+       if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
+               state->out_oplock_level = in_oplock_level;
+       } else {
+               state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
+       }
 
        if ((in_create_disposition == FILE_SUPERSEDE)
            && (info == FILE_WAS_OVERWRITTEN)) {
@@ -769,7 +799,7 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
        /* Deal with other possible opens having a modified
           write time. JRA. */
        ZERO_STRUCT(write_time_ts);
-       get_file_infos(result->file_id, NULL, &write_time_ts);
+       get_file_infos(result->file_id, 0, NULL, &write_time_ts);
        if (!null_timespec(write_time_ts)) {
                update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
        }
@@ -791,6 +821,7 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
        if (state->out_file_attributes == 0) {
                state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
        }
+       state->out_file_id_persistent = result->fnum;
        state->out_file_id_volatile = result->fnum;
        state->out_context_blobs = out_context_blobs;
 
@@ -809,6 +840,7 @@ static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
                        uint64_t *out_allocation_size,
                        uint64_t *out_end_of_file,
                        uint32_t *out_file_attributes,
+                       uint64_t *out_file_id_persistent,
                        uint64_t *out_file_id_volatile,
                        struct smb2_create_blobs *out_context_blobs)
 {
@@ -830,6 +862,7 @@ static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
        *out_allocation_size    = state->out_allocation_size;
        *out_end_of_file        = state->out_end_of_file;
        *out_file_attributes    = state->out_file_attributes;
+       *out_file_id_persistent = state->out_file_id_persistent;
        *out_file_id_volatile   = state->out_file_id_volatile;
        *out_context_blobs      = state->out_context_blobs;
 
@@ -878,9 +911,9 @@ bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
  close.
 *********************************************************/
 
-static struct smbd_smb2_request *find_open_smb2req(uint64_t mid)
+static struct smbd_smb2_request *find_open_smb2req(
+       struct smbd_server_connection *sconn, uint64_t mid)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
        struct smbd_smb2_request *smb2req;
 
        for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
@@ -901,10 +934,12 @@ static struct smbd_smb2_request *find_open_smb2req(uint64_t mid)
        return NULL;
 }
 
-bool open_was_deferred_smb2(uint64_t mid)
+bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
 {
        struct smbd_smb2_create_state *state = NULL;
-       struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
+       struct smbd_smb2_request *smb2req;
+
+       smb2req = find_open_smb2req(sconn, mid);
 
        if (!smb2req) {
                DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
@@ -960,9 +995,12 @@ static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
        TALLOC_FREE(state->im);
 }
 
-void remove_deferred_open_message_smb2(uint64_t mid)
+void remove_deferred_open_message_smb2(
+       struct smbd_server_connection *sconn, uint64_t mid)
 {
-       struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
+       struct smbd_smb2_request *smb2req;
+
+       smb2req = find_open_smb2req(sconn, mid);
 
        if (!smb2req) {
                DEBUG(10,("remove_deferred_open_message_smb2: "
@@ -994,10 +1032,13 @@ static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *c
        }
 }
 
-void schedule_deferred_open_message_smb2(uint64_t mid)
+void schedule_deferred_open_message_smb2(
+       struct smbd_server_connection *sconn, uint64_t mid)
 {
        struct smbd_smb2_create_state *state = NULL;
-       struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
+       struct smbd_smb2_request *smb2req;
+
+       smb2req = find_open_smb2req(sconn, mid);
 
        if (!smb2req) {
                DEBUG(10,("schedule_deferred_open_message_smb2: "
@@ -1114,7 +1155,8 @@ static bool smbd_smb2_create_cancel(struct tevent_req *req)
        smb2req = state->smb2req;
        mid = get_mid_from_smb2req(smb2req);
 
-       remove_deferred_open_entry(state->id, mid);
+       remove_deferred_open_entry(state->id, mid,
+                                  sconn_server_id(smb2req->sconn));
        remove_deferred_open_message_smb2_internal(smb2req, mid);
        smb2req->cancelled = true;
 
@@ -1151,7 +1193,8 @@ bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
        if (!state->private_data.data) {
                return false;
        }
-#if 0
+
+#if 1
        /* Boo - turns out this isn't what W2K8R2
           does. It actually sends the STATUS_PENDING
           message followed by the STATUS_SHARING_VIOLATION
@@ -1159,6 +1202,10 @@ bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
           calls (even on directories) will potentially
           fail in a chain.... ? And I've seen directory
           opens as the start of a chain. JRA.
+
+          Update: 19th May 2010. Talking with Microsoft
+          engineers at the plugfest this is a bug in
+          Windows. Re-enable this code.
        */
        /*
         * More subtlety. To match W2K8R2 don't