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