Fix SMB2_CAP_DFS
[metze/samba/wip.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 #include "libcli/raw/libcliraw.h"
27
28 struct smb2_handle;
29 struct smb2_lease_break;
30
31 /*
32   information returned from the negotiate process
33 */
34 struct smb2_negotiate {
35         DATA_BLOB secblob;
36         NTTIME system_time;
37         NTTIME server_start_time;
38         uint16_t security_mode;
39         uint16_t dialect_revision;
40 };
41
42 /* this is the context for the smb2 transport layer */
43 struct smb2_transport {
44         /* socket level info */
45         struct smbcli_socket *socket;
46
47         struct smb2_negotiate negotiate;
48
49         /* next seqnum to allocate */
50         uint64_t seqnum;
51
52         /* a list of requests that are pending for receive on this
53            connection */
54         struct smb2_request *pending_recv;
55
56         /* context of the stream -> packet parser */
57         struct packet_context *packet;
58
59         /* an idle function - if this is defined then it will be
60            called once every period microseconds while we are waiting
61            for a packet */
62         struct {
63                 void (*func)(struct smb2_transport *, void *);
64                 void *private_data;
65                 uint_t period;
66         } idle;
67
68         struct {
69                 /* a oplock break request handler */
70                 bool (*handler)(struct smb2_transport *transport,
71                                 const struct smb2_handle *handle,
72                                 uint8_t level, void *private_data);
73                 /* private data passed to the oplock handler */
74                 void *private_data;
75         } oplock;
76
77         struct {
78                 /* a lease break request handler */
79                 bool (*handler)(struct smb2_transport *transport,
80                                 const struct smb2_lease_break *lease_break,
81                                 void *private_data);
82                 /* private data passed to the oplock handler */
83                 void *private_data;
84         } lease;
85
86         struct smbcli_options options;
87
88         bool signing_required;
89 };
90
91
92 /*
93   SMB2 tree context
94 */
95 struct smb2_tree {
96         struct smb2_session *session;
97         uint32_t tid;
98 };
99
100 /*
101   SMB2 session context
102 */
103 struct smb2_session {
104         struct smb2_transport *transport;
105         struct gensec_security *gensec;
106         uint64_t uid;
107         DATA_BLOB session_key;
108         bool signing_active;
109 };
110
111
112 struct smb2_request_buffer {
113         /* the raw SMB2 buffer, including the 4 byte length header */
114         uint8_t *buffer;
115         
116         /* the size of the raw buffer, including 4 byte header */
117         size_t size;
118         
119         /* how much has been allocated - on reply the buffer is over-allocated to 
120            prevent too many realloc() calls 
121         */
122         size_t allocated;
123         
124         /* the start of the SMB2 header - this is always buffer+4 */
125         uint8_t *hdr;
126         
127         /* the packet body */
128         uint8_t *body;
129         size_t body_fixed;
130         size_t body_size;
131
132         /* this point to the next dynamic byte that can be used
133          * this will be moved when some dynamic data is pushed
134          */
135         uint8_t *dynamic;
136
137         /* this is used to range check and align strings and buffers */
138         struct request_bufinfo bufinfo;
139 };
140
141
142 /*
143   a client request moves between the following 4 states.
144 */
145 enum smb2_request_state {SMB2_REQUEST_INIT, /* we are creating the request */
146                         SMB2_REQUEST_RECV, /* we are waiting for a matching reply */
147                         SMB2_REQUEST_DONE, /* the request is finished */
148                         SMB2_REQUEST_ERROR}; /* a packet or transport level error has occurred */
149
150 /* the context for a single SMB2 request */
151 struct smb2_request {
152         /* allow a request to be part of a list of requests */
153         struct smb2_request *next, *prev;
154
155         /* each request is in one of 3 possible states */
156         enum smb2_request_state state;
157         
158         struct smb2_transport *transport;
159         struct smb2_session   *session;
160         struct smb2_tree      *tree;
161
162         uint64_t seqnum;
163
164         struct {
165                 bool do_cancel;
166                 bool can_cancel;
167                 uint32_t pending_id;
168         } cancel;
169
170         /* the NT status for this request. Set by packet receive code
171            or code detecting error. */
172         NTSTATUS status;
173         
174         struct smb2_request_buffer in;
175         struct smb2_request_buffer out;
176
177         /* information on what to do with a reply when it is received
178            asyncronously. If this is not setup when a reply is received then
179            the reply is discarded
180
181            The private pointer is private to the caller of the client
182            library (the application), not private to the library
183         */
184         struct {
185                 void (*fn)(struct smb2_request *);
186                 void *private_data;
187         } async;
188 };
189
190
191 #define SMB2_MIN_SIZE 0x42
192 #define SMB2_MIN_SIZE_NO_BODY 0x40
193
194 /* offsets into header elements for a sync SMB2 request */
195 #define SMB2_HDR_PROTOCOL_ID    0x00
196 #define SMB2_HDR_LENGTH         0x04
197 #define SMB2_HDR_EPOCH          0x06
198 #define SMB2_HDR_STATUS         0x08
199 #define SMB2_HDR_OPCODE         0x0c
200 #define SMB2_HDR_CREDIT         0x0e
201 #define SMB2_HDR_FLAGS          0x10
202 #define SMB2_HDR_NEXT_COMMAND   0x14
203 #define SMB2_HDR_MESSAGE_ID     0x18
204 #define SMB2_HDR_PID            0x20
205 #define SMB2_HDR_TID            0x24
206 #define SMB2_HDR_SESSION_ID     0x28
207 #define SMB2_HDR_SIGNATURE      0x30 /* 16 bytes */
208 #define SMB2_HDR_BODY           0x40
209
210 /* header flags */
211 #define SMB2_HDR_FLAG_REDIRECT  0x01
212 #define SMB2_HDR_FLAG_ASYNC     0x02
213 #define SMB2_HDR_FLAG_CHAINED   0x04
214 #define SMB2_HDR_FLAG_SIGNED    0x08
215 #define SMB2_HDR_FLAG_DFS       0x10000000
216
217 /* SMB2 opcodes */
218 #define SMB2_OP_NEGPROT   0x00
219 #define SMB2_OP_SESSSETUP 0x01
220 #define SMB2_OP_LOGOFF    0x02
221 #define SMB2_OP_TCON      0x03
222 #define SMB2_OP_TDIS      0x04
223 #define SMB2_OP_CREATE    0x05
224 #define SMB2_OP_CLOSE     0x06
225 #define SMB2_OP_FLUSH     0x07
226 #define SMB2_OP_READ      0x08
227 #define SMB2_OP_WRITE     0x09
228 #define SMB2_OP_LOCK      0x0a
229 #define SMB2_OP_IOCTL     0x0b
230 #define SMB2_OP_CANCEL    0x0c
231 #define SMB2_OP_KEEPALIVE 0x0d
232 #define SMB2_OP_FIND      0x0e
233 #define SMB2_OP_NOTIFY    0x0f
234 #define SMB2_OP_GETINFO   0x10
235 #define SMB2_OP_SETINFO   0x11
236 #define SMB2_OP_BREAK     0x12
237
238 #define SMB2_MAGIC 0x424D53FE /* 0xFE 'S' 'M' 'B' */
239
240 /* the dialects we support */
241 #define SMB2_DIALECT_REVISION           0x202
242 #define SMB21_DIALECT_REVISION          0x210
243 #define SMB2_LONGHORN_BETA_DIALECT_REVISION     0x0 /* early beta dialect */
244
245 /* SMB2 negotiate security_mode */
246 #define SMB2_NEGOTIATE_SIGNING_ENABLED   0x01
247 #define SMB2_NEGOTIATE_SIGNING_REQUIRED  0x02
248
249 /* SMB2 capabilities - only 1 so far. I'm sure more will be added */
250 #define SMB2_CAP_DFS                     0x00000001
251 /* so we can spot new caps as added */
252 #define SMB2_CAP_ALL                     SMB2_CAP_DFS 
253
254 /* SMB2 share flags */
255 #define SMB2_SHAREFLAG_MANUAL_CACHING                    0x0000
256 #define SMB2_SHAREFLAG_AUTO_CACHING                      0x0010
257 #define SMB2_SHAREFLAG_VDO_CACHING                       0x0020
258 #define SMB2_SHAREFLAG_NO_CACHING                        0x0030
259 #define SMB2_SHAREFLAG_DFS                               0x0001
260 #define SMB2_SHAREFLAG_DFS_ROOT                          0x0002
261 #define SMB2_SHAREFLAG_RESTRICT_EXCLUSIVE_OPENS          0x0100
262 #define SMB2_SHAREFLAG_FORCE_SHARED_DELETE               0x0200
263 #define SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING           0x0400
264 #define SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM       0x0800
265 #define SMB2_SHAREFLAG_ALL                               0x0F33
266
267 /* SMB2 create security flags */
268 #define SMB2_SECURITY_DYNAMIC_TRACKING                   0x01
269 #define SMB2_SECURITY_EFFECTIVE_ONLY                     0x02
270
271 /* SMB2 requested oplock levels */
272 #define SMB2_OPLOCK_LEVEL_NONE                           0x00
273 #define SMB2_OPLOCK_LEVEL_II                             0x01
274 #define SMB2_OPLOCK_LEVEL_EXCLUSIVE                      0x08
275 #define SMB2_OPLOCK_LEVEL_BATCH                          0x09
276 #define SMB2_OPLOCK_LEVEL_LEASE                          0xFF
277
278 /* SMB2 lease bits */
279 #define SMB2_LEASE_NONE                                  0x00
280 #define SMB2_LEASE_READ                                  0x01
281 #define SMB2_LEASE_HANDLE                                0x02
282 #define SMB2_LEASE_WRITE                                 0x04
283
284 /* SMB2 lease break flags */
285 #define SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED        0x01
286
287 /* SMB2 impersonation levels */
288 #define SMB2_IMPERSONATION_ANONYMOUS                     0x00
289 #define SMB2_IMPERSONATION_IDENTIFICATION                0x01
290 #define SMB2_IMPERSONATION_IMPERSONATION                 0x02
291 #define SMB2_IMPERSONATION_DELEGATE                      0x03
292
293 /* SMB2 create tags */
294 #define SMB2_CREATE_TAG_EXTA "ExtA"
295 #define SMB2_CREATE_TAG_MXAC "MxAc"
296 #define SMB2_CREATE_TAG_SECD "SecD"
297 #define SMB2_CREATE_TAG_DHNQ "DHnQ"
298 #define SMB2_CREATE_TAG_DHNC "DHnC"
299 #define SMB2_CREATE_TAG_ALSI "AlSi"
300 #define SMB2_CREATE_TAG_TWRP "TWrp"
301 #define SMB2_CREATE_TAG_QFID "QFid"
302 #define SMB2_CREATE_TAG_RQLS "RqLs"
303
304 /* SMB2 Create ignore some more create_options */
305 #define SMB2_CREATE_OPTIONS_NOT_SUPPORTED_MASK  (NTCREATEX_OPTIONS_TREE_CONNECTION | \
306                                                  NTCREATEX_OPTIONS_OPFILTER)
307
308 /*
309   check that a body has the expected size
310 */
311 #define SMB2_CHECK_PACKET_RECV(req, size, dynamic) do { \
312         size_t is_size = req->in.body_size; \
313         uint16_t field_size = SVAL(req->in.body, 0); \
314         uint16_t want_size = ((dynamic)?(size)+1:(size)); \
315         if (is_size < (size)) { \
316                 DEBUG(0,("%s: buffer too small 0x%x. Expected 0x%x\n", \
317                          __location__, (unsigned)is_size, (unsigned)want_size)); \
318                 return NT_STATUS_BUFFER_TOO_SMALL; \
319         }\
320         if (field_size != want_size) { \
321                 DEBUG(0,("%s: unexpected fixed body size 0x%x. Expected 0x%x\n", \
322                          __location__, (unsigned)field_size, (unsigned)want_size)); \
323                 return NT_STATUS_INVALID_PARAMETER; \
324         } \
325 } while (0)
326
327 #endif