private -> private_data for struct smb2_request
[kai/samba.git] / source4 / libcli / smb2 / smb2.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client library header
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef __LIBCLI_SMB2_SMB2_H__
23 #define __LIBCLI_SMB2_SMB2_H__
24
25 #include "libcli/raw/request.h"
26
27 struct smb2_handle;
28
29 struct smb2_options {
30         uint32_t timeout;
31 };
32
33 /*
34   information returned from the negotiate response
35 */
36 struct smb2_negotiate {
37         DATA_BLOB secblob;
38 };
39
40 /* this is the context for the smb2 transport layer */
41 struct smb2_transport {
42         /* socket level info */
43         struct smbcli_socket *socket;
44
45         struct smb2_options options;
46         struct smb2_negotiate negotiate;
47
48         /* next seqnum to allocate */
49         uint64_t seqnum;
50
51         /* a list of requests that are pending for receive on this
52            connection */
53         struct smb2_request *pending_recv;
54
55         /* context of the stream -> packet parser */
56         struct packet_context *packet;
57
58         /* an idle function - if this is defined then it will be
59            called once every period microseconds while we are waiting
60            for a packet */
61         struct {
62                 void (*func)(struct smb2_transport *, void *);
63                 void *private;
64                 uint_t period;
65         } idle;
66
67         struct {
68                 /* a oplock break request handler */
69                 bool (*handler)(struct smb2_transport *transport,
70                                 const struct smb2_handle *handle,
71                                 uint8_t level, void *private_data);
72                 /* private data passed to the oplock handler */
73                 void *private_data;
74         } oplock;
75 };
76
77
78 /*
79   SMB2 tree context
80 */
81 struct smb2_tree {
82         struct smb2_session *session;
83         uint32_t tid;
84 };
85
86 /*
87   SMB2 session context
88 */
89 struct smb2_session {
90         struct smb2_transport *transport;
91         struct gensec_security *gensec;
92         uint64_t uid;
93         DATA_BLOB session_key;
94 };
95
96
97 struct smb2_request_buffer {
98         /* the raw SMB2 buffer, including the 4 byte length header */
99         uint8_t *buffer;
100         
101         /* the size of the raw buffer, including 4 byte header */
102         size_t size;
103         
104         /* how much has been allocated - on reply the buffer is over-allocated to 
105            prevent too many realloc() calls 
106         */
107         size_t allocated;
108         
109         /* the start of the SMB2 header - this is always buffer+4 */
110         uint8_t *hdr;
111         
112         /* the packet body */
113         uint8_t *body;
114         size_t body_fixed;
115         size_t body_size;
116
117         /* this point to the next dynamic byte that can be used
118          * this will be moved when some dynamic data is pushed
119          */
120         uint8_t *dynamic;
121
122         /* this is used to range check and align strings and buffers */
123         struct request_bufinfo bufinfo;
124 };
125
126
127 /*
128   a client request moves between the following 4 states.
129 */
130 enum smb2_request_state {SMB2_REQUEST_INIT, /* we are creating the request */
131                         SMB2_REQUEST_RECV, /* we are waiting for a matching reply */
132                         SMB2_REQUEST_DONE, /* the request is finished */
133                         SMB2_REQUEST_ERROR}; /* a packet or transport level error has occurred */
134
135 /* the context for a single SMB2 request */
136 struct smb2_request {
137         /* allow a request to be part of a list of requests */
138         struct smb2_request *next, *prev;
139
140         /* each request is in one of 3 possible states */
141         enum smb2_request_state state;
142         
143         struct smb2_transport *transport;
144         struct smb2_session   *session;
145         struct smb2_tree      *tree;
146
147         uint64_t seqnum;
148
149         struct {
150                 bool do_cancel;
151                 bool can_cancel;
152                 uint32_t pending_id;
153         } cancel;
154
155         /* the NT status for this request. Set by packet receive code
156            or code detecting error. */
157         NTSTATUS status;
158         
159         struct smb2_request_buffer in;
160         struct smb2_request_buffer out;
161
162         /* information on what to do with a reply when it is received
163            asyncronously. If this is not setup when a reply is received then
164            the reply is discarded
165
166            The private pointer is private to the caller of the client
167            library (the application), not private to the library
168         */
169         struct {
170                 void (*fn)(struct smb2_request *);
171                 void *private_data;
172         } async;
173 };
174
175
176 #define SMB2_MIN_SIZE 0x42
177
178 /* offsets into header elements for a sync SMB2 request */
179 #define SMB2_HDR_PROTOCOL_ID    0x00
180 #define SMB2_HDR_LENGTH         0x04
181 #define SMB2_HDR_EPOCH          0x06
182 #define SMB2_HDR_STATUS         0x08
183 #define SMB2_HDR_OPCODE         0x0c
184 #define SMB2_HDR_CREDIT         0x0e
185 #define SMB2_HDR_FLAGS          0x10
186 #define SMB2_HDR_NEXT_COMMAND   0x14
187 #define SMB2_HDR_MESSAGE_ID     0x18
188 #define SMB2_HDR_PID            0x20
189 #define SMB2_HDR_TID            0x24
190 #define SMB2_HDR_SESSION_ID     0x28
191 #define SMB2_HDR_SIGNATURE      0x30 /* 16 bytes */
192 #define SMB2_HDR_BODY           0x40
193
194 /* SMB2 opcodes */
195 #define SMB2_OP_NEGPROT   0x00
196 #define SMB2_OP_SESSSETUP 0x01
197 #define SMB2_OP_LOGOFF    0x02
198 #define SMB2_OP_TCON      0x03
199 #define SMB2_OP_TDIS      0x04
200 #define SMB2_OP_CREATE    0x05
201 #define SMB2_OP_CLOSE     0x06
202 #define SMB2_OP_FLUSH     0x07
203 #define SMB2_OP_READ      0x08
204 #define SMB2_OP_WRITE     0x09
205 #define SMB2_OP_LOCK      0x0a
206 #define SMB2_OP_IOCTL     0x0b
207 #define SMB2_OP_CANCEL    0x0c
208 #define SMB2_OP_KEEPALIVE 0x0d
209 #define SMB2_OP_FIND      0x0e
210 #define SMB2_OP_NOTIFY    0x0f
211 #define SMB2_OP_GETINFO   0x10
212 #define SMB2_OP_SETINFO   0x11
213 #define SMB2_OP_BREAK     0x12
214
215 #define SMB2_MAGIC 0x424D53FE /* 0xFE 'S' 'M' 'B' */
216
217 /* the dialect we support */
218 #define SMB2_DIALECT_REVISION           0x202
219
220 /* SMB2 negotiate security_mode */
221 #define SMB2_NEGOTIATE_SIGNING_ENABLED   0x01
222 #define SMB2_NEGOTIATE_SIGNING_REQUIRED  0x02
223
224 /* SMB2 capabilities - only 1 so far. I'm sure more will be added */
225 #define SMB2_CAP_DFS                     0x0
226 /* so we can spot new caps as added */
227 #define SMB2_CAP_ALL                     SMB2_CAP_DFS 
228
229 /* SMB2 share flags */
230 #define SMB2_SHAREFLAG_MANUAL_CACHING                    0x0000
231 #define SMB2_SHAREFLAG_AUTO_CACHING                      0x0010
232 #define SMB2_SHAREFLAG_VDO_CACHING                       0x0020
233 #define SMB2_SHAREFLAG_NO_CACHING                        0x0030
234 #define SMB2_SHAREFLAG_DFS                               0x0001
235 #define SMB2_SHAREFLAG_DFS_ROOT                          0x0002
236 #define SMB2_SHAREFLAG_RESTRICT_EXCLUSIVE_OPENS          0x0100
237 #define SMB2_SHAREFLAG_FORCE_SHARED_DELETE               0x0200
238 #define SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING           0x0400
239 #define SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM       0x0800
240 #define SMB2_SHAREFLAG_ALL                               0x0F33
241
242 /* SMB2 create security flags */
243 #define SMB2_SECURITY_DYNAMIC_TRACKING                   0x01
244 #define SMB2_SECURITY_EFFECTIVE_ONLY                     0x02
245
246 /* SMB2 requested oplock levels */
247 #define SMB2_OPLOCK_LEVEL_NONE                           0x00
248 #define SMB2_OPLOCK_LEVEL_II                             0x01
249 #define SMB2_OPLOCK_LEVEL_EXCLUSIVE                      0x08
250 #define SMB2_OPLOCK_LEVEL_BATCH                          0x09
251
252 /* SMB2 impersonation levels */
253 #define SMB2_IMPERSONATION_ANONYMOUS                     0x00
254 #define SMB2_IMPERSONATION_IDENTIFICATION                0x01
255 #define SMB2_IMPERSONATION_IMPERSONATION                 0x02
256 #define SMB2_IMPERSONATION_DELEGATE                      0x03
257
258 /* SMB2 create tags */
259 #define SMB2_CREATE_TAG_EXTA "ExtA"
260 #define SMB2_CREATE_TAG_MXAC "MxAc"
261 #define SMB2_CREATE_TAG_SECD "SecD"
262 #define SMB2_CREATE_TAG_DHNQ "DHnQ"
263 #define SMB2_CREATE_TAG_DHNC "DHnC"
264 #define SMB2_CREATE_TAG_ALSI "AlSi"
265 #define SMB2_CREATE_TAG_TWRP "TWrp"
266 #define SMB2_CREATE_TAG_QFID "QFid"
267
268
269
270 /*
271   check that a body has the expected size
272 */
273 #define SMB2_CHECK_PACKET_RECV(req, size, dynamic) do { \
274         size_t is_size = req->in.body_size; \
275         uint16_t field_size = SVAL(req->in.body, 0); \
276         uint16_t want_size = ((dynamic)?(size)+1:(size)); \
277         if (is_size < (size)) { \
278                 DEBUG(0,("%s: buffer too small 0x%x. Expected 0x%x\n", \
279                          __location__, (unsigned)is_size, (unsigned)want_size)); \
280                 return NT_STATUS_BUFFER_TOO_SMALL; \
281         }\
282         if (field_size != want_size) { \
283                 DEBUG(0,("%s: unexpected fixed body size 0x%x. Expected 0x%x\n", \
284                          __location__, (unsigned)field_size, (unsigned)want_size)); \
285                 return NT_STATUS_INVALID_PARAMETER; \
286         } \
287 } while (0)
288
289 #endif