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