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