2f347d387618f5e813bb426eaf7a7bc053d99ce7
[tprouty/samba.git] / source4 / smb_server / smb2 / smb2_server.h
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         /* chained file handle */
62         uint8_t _chained_file_handle[16];
63         uint8_t *chained_file_handle;
64
65         struct smb2_request_buffer in;
66         struct smb2_request_buffer out;
67 };
68
69 struct smbsrv_request;
70
71 #include "smb_server/smb2/smb2_proto.h"
72
73 /* useful way of catching wct errors with file and line number */
74 #define SMB2SRV_CHECK_BODY_SIZE(req, size, dynamic) do { \
75         size_t is_size = req->in.body_size; \
76         uint16_t field_size = SVAL(req->in.body, 0); \
77         uint16_t want_size = ((dynamic)?(size)+1:(size)); \
78         if (is_size < (size)) { \
79                 DEBUG(0,("%s: buffer too small 0x%x. Expected 0x%x\n", \
80                          __location__, (unsigned)is_size, (unsigned)want_size)); \
81                 smb2srv_send_error(req,  NT_STATUS_FOOBAR); \
82                 return; \
83         }\
84         if (field_size != want_size) { \
85                 DEBUG(0,("%s: unexpected fixed body size 0x%x. Expected 0x%x\n", \
86                          __location__, (unsigned)field_size, (unsigned)want_size)); \
87                 smb2srv_send_error(req,  NT_STATUS_FOOBAR); \
88                 return; \
89         } \
90 } while (0)
91
92 #define SMB2SRV_CHECK(cmd) do {\
93         NTSTATUS _status; \
94         _status = cmd; \
95         if (!NT_STATUS_IS_OK(_status)) { \
96                 smb2srv_send_error(req,  _status); \
97                 return; \
98         } \
99 } while (0)
100
101 /* useful wrapper for talloc with NO_MEMORY reply */
102 #define SMB2SRV_TALLOC_IO_PTR(ptr, type) do { \
103         ptr = talloc(req, type); \
104         if (!ptr) { \
105                 smb2srv_send_error(req, NT_STATUS_NO_MEMORY); \
106                 return; \
107         } \
108         req->io_ptr = ptr; \
109 } while (0)
110
111 #define SMB2SRV_SETUP_NTVFS_REQUEST(send_fn, state) do { \
112         req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req, \
113                                           req->session->session_info,\
114                                           0, \
115                                           req->request_time, \
116                                           req, send_fn, state); \
117         if (!req->ntvfs) { \
118                 smb2srv_send_error(req, NT_STATUS_NO_MEMORY); \
119                 return; \
120         } \
121         (void)talloc_steal(req->tcon->ntvfs, req); \
122         req->ntvfs->frontend_data.private_data = req; \
123 } while (0)
124
125 #define SMB2SRV_CHECK_FILE_HANDLE(handle) do { \
126         if (!handle) { \
127                 smb2srv_send_error(req, NT_STATUS_FILE_CLOSED); \
128                 return; \
129         } \
130 } while (0)
131
132 /* 
133    check if the backend wants to handle the request asynchronously.
134    if it wants it handled synchronously then call the send function
135    immediately
136 */
137 #define SMB2SRV_CALL_NTVFS_BACKEND(cmd) do { \
138         req->ntvfs->async_states->status = cmd; \
139         if (req->ntvfs->async_states->state & NTVFS_ASYNC_STATE_ASYNC) { \
140                 NTSTATUS _status; \
141                 _status = smb2srv_queue_pending(req); \
142                 if (!NT_STATUS_IS_OK(_status)) { \
143                         ntvfs_cancel(req->ntvfs); \
144                 } \
145         } else { \
146                 req->ntvfs->async_states->send_fn(req->ntvfs); \
147         } \
148 } while (0)
149
150 /* check req->ntvfs->async_states->status and if not OK then send an error reply */
151 #define SMB2SRV_CHECK_ASYNC_STATUS_ERR_SIMPLE do { \
152         req = talloc_get_type(ntvfs->async_states->private_data, struct smb2srv_request); \
153         req->status = ntvfs->async_states->status; \
154         if (NT_STATUS_IS_ERR(ntvfs->async_states->status)) { \
155                 smb2srv_send_error(req, ntvfs->async_states->status); \
156                 return; \
157         } \
158 } while (0)
159 #define SMB2SRV_CHECK_ASYNC_STATUS_ERR(ptr, type) do { \
160         SMB2SRV_CHECK_ASYNC_STATUS_ERR_SIMPLE; \
161         ptr = talloc_get_type(req->io_ptr, type); \
162 } while (0)
163 #define SMB2SRV_CHECK_ASYNC_STATUS_SIMPLE do { \
164         req = talloc_get_type(ntvfs->async_states->private_data, struct smb2srv_request); \
165         req->status = ntvfs->async_states->status; \
166         if (!NT_STATUS_IS_OK(ntvfs->async_states->status)) { \
167                 smb2srv_send_error(req, ntvfs->async_states->status); \
168                 return; \
169         } \
170 } while (0)
171 #define SMB2SRV_CHECK_ASYNC_STATUS(ptr, type) do { \
172         SMB2SRV_CHECK_ASYNC_STATUS_SIMPLE; \
173         ptr = talloc_get_type(req->io_ptr, type); \
174 } while (0)