1 /* 2 Unix SMB2 implementation. 3 4 Copyright (C) Stefan Metzmacher 2005 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 /* the context for a single SMB2 request. This is passed to any request-context 21 functions */ 22 struct smb2srv_request { 23 /* the smbsrv_connection needs a list of requests queued for send */ 24 struct smb2srv_request *next, *prev; 25 26 /* the server_context contains all context specific to this SMB socket */ 27 struct smbsrv_connection *smb_conn; 28 29 /* conn is only set for operations that have a valid TID */ 30 struct smbsrv_tcon *tcon; 31 32 /* the session context is derived from the vuid */ 33 struct smbsrv_session *session; 34 35 #define SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY (1<<0) 36 uint32_t control_flags; 37 38 /* the system time when the request arrived */ 39 struct timeval request_time; 40 41 /* a pointer to the per request union smb_* io structure */ 42 void *io_ptr; 43 44 /* the ntvfs_request */ 45 struct ntvfs_request *ntvfs; 46 47 /* Now the SMB2 specific stuff */ 48 49 /* the status the backend returned */ 50 NTSTATUS status; 51 52 /* for matching request and reply */ 53 uint64_t seqnum; 54 55 /* the id that can be used to cancel the request */ 56 uint32_t pending_id; 57 58 /* the offset to the next SMB2 Header for chained requests */ 59 uint32_t chain_offset; 60 61 /* the status we return for following chained requests */ 62 NTSTATUS chain_status; 63 64 /* chained file handle */ 65 uint8_t _chained_file_handle[16]; 66 uint8_t *chained_file_handle; 67 uint64_t chained_session_id; 68 uint32_t chained_tree_id; 69 70 bool is_signed; 71 72 struct smb2_request_buffer in; 73 struct smb2_request_buffer out; 74 }; 75 76 struct smbsrv_request; 77 78 #include "smb_server/smb2/smb2_proto.h" 79 80 /* useful way of catching field size errors with file and line number */ 81 #define SMB2SRV_CHECK_BODY_SIZE(req, size, dynamic) do { \ 82 size_t is_size = req->in.body_size; \ 83 uint16_t field_size; \ 84 uint16_t want_size = ((dynamic)?(size)+1:(size)); \ 85 if (is_size < (size)) { \ 86 DEBUG(0,("%s: buffer too small 0x%x. Expected 0x%x\n", \ 87 __location__, (unsigned)is_size, (unsigned)want_size)); \ 88 smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER); \ 89 return; \ 90 }\ 91 field_size = SVAL(req->in.body, 0); \ 92 if (field_size != want_size) { \ 93 DEBUG(0,("%s: unexpected fixed body size 0x%x. Expected 0x%x\n", \ 94 __location__, (unsigned)field_size, (unsigned)want_size)); \ 95 smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER); \ 96 return; \ 97 } \ 98 } while (0) 99 100 #define SMB2SRV_CHECK(cmd) do {\ 101 NTSTATUS _status; \ 102 _status = cmd; \ 103 if (!NT_STATUS_IS_OK(_status)) { \ 104 smb2srv_send_error(req, _status); \ 105 return; \ 106 } \ 107 } while (0) 108 109 /* useful wrapper for talloc with NO_MEMORY reply */ 110 #define SMB2SRV_TALLOC_IO_PTR(ptr, type) do { \ 111 ptr = talloc(req, type); \ 112 if (!ptr) { \ 113 smb2srv_send_error(req, NT_STATUS_NO_MEMORY); \ 114 return; \ 115 } \ 116 req->io_ptr = ptr; \ 117 } while (0) 118 119 #define SMB2SRV_SETUP_NTVFS_REQUEST(send_fn, state) do { \ 120 req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req, \ 121 req->session->session_info,\ 122 0, \ 123 req->request_time, \ 124 req, send_fn, state); \ 125 if (!req->ntvfs) { \ 126 smb2srv_send_error(req, NT_STATUS_NO_MEMORY); \ 127 return; \ 128 } \ 129 (void)talloc_steal(req->tcon->ntvfs, req); \ 130 req->ntvfs->frontend_data.private_data = req; \ 131 } while (0) 132 133 #define SMB2SRV_CHECK_FILE_HANDLE(handle) do { \ 134 if (!handle) { \ 135 smb2srv_send_error(req, NT_STATUS_FILE_CLOSED); \ 136 return; \ 137 } \ 138 } while (0) 139 140 /* 141 check if the backend wants to handle the request asynchronously. 142 if it wants it handled synchronously then call the send function 143 immediately 144 */ 145 #define SMB2SRV_CALL_NTVFS_BACKEND(cmd) do { \ 146 req->ntvfs->async_states->status = cmd; \ 147 if (req->ntvfs->async_states->state & NTVFS_ASYNC_STATE_ASYNC) { \ 148 NTSTATUS _status; \ 149 _status = smb2srv_queue_pending(req); \ 150 if (!NT_STATUS_IS_OK(_status)) { \ 151 ntvfs_cancel(req->ntvfs); \ 152 } \ 153 } else { \ 154 req->ntvfs->async_states->send_fn(req->ntvfs); \ 155 } \ 156 } while (0) 157 158 /* check req->ntvfs->async_states->status and if not OK then send an error reply */ 159 #define SMB2SRV_CHECK_ASYNC_STATUS_ERR_SIMPLE do { \ 160 req = talloc_get_type(ntvfs->async_states->private_data, struct smb2srv_request); \ 161 if (ntvfs->async_states->state & NTVFS_ASYNC_STATE_CLOSE || NT_STATUS_EQUAL(ntvfs->async_states->status, NT_STATUS_NET_WRITE_FAULT)) { \ 162 smbsrv_terminate_connection(req->smb_conn, get_friendly_nt_error_msg (ntvfs->async_states->status)); \ 163 talloc_free(req); \ 164 return; \ 165 } \ 166 req->status = ntvfs->async_states->status; \ 167 if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \ 168 smb2srv_send_error(req, ntvfs->async_states->status); \ 169 return; \ 170 } \ 171 } while (0) 172 #define SMB2SRV_CHECK_ASYNC_STATUS_ERR(ptr, type) do { \ 173 SMB2SRV_CHECK_ASYNC_STATUS_ERR_SIMPLE; \ 174 ptr = talloc_get_type(req->io_ptr, type); \ 175 } while (0) 176 #define SMB2SRV_CHECK_ASYNC_STATUS_SIMPLE do { \ 177 req = talloc_get_type(ntvfs->async_states->private_data, struct smb2srv_request); \ 178 if (ntvfs->async_states->state & NTVFS_ASYNC_STATE_CLOSE || NT_STATUS_EQUAL(ntvfs->async_states->status, NT_STATUS_NET_WRITE_FAULT)) { \ 179 smbsrv_terminate_connection(req->smb_conn, get_friendly_nt_error_msg (ntvfs->async_states->status)); \ 180 talloc_free(req); \ 181 return; \ 182 } \ 183 req->status = ntvfs->async_states->status; \ 184 if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \ 185 smb2srv_send_error(req, ntvfs->async_states->status); \ 186 return; \ 187 } \ 188 } while (0) 189 #define SMB2SRV_CHECK_ASYNC_STATUS(ptr, type) do { \ 190 SMB2SRV_CHECK_ASYNC_STATUS_SIMPLE; \ 191 ptr = talloc_get_type(req->io_ptr, type); \ 192 } while (0) 193