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