r23792: convert Samba4 to GPLv3
[tprouty/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 struct smb2_options {
23         uint32_t timeout;
24 };
25
26 /*
27   information returned from the negotiate response
28 */
29 struct smb2_negotiate {
30         DATA_BLOB secblob;
31 };
32
33 /* this is the context for the smb2 transport layer */
34 struct smb2_transport {
35         /* socket level info */
36         struct smbcli_socket *socket;
37
38         struct smb2_options options;
39         struct smb2_negotiate negotiate;
40
41         /* next seqnum to allocate */
42         uint64_t seqnum;
43
44         /* a list of requests that are pending for receive on this
45            connection */
46         struct smb2_request *pending_recv;
47
48         /* context of the stream -> packet parser */
49         struct packet_context *packet;
50
51         /* an idle function - if this is defined then it will be
52            called once every period microseconds while we are waiting
53            for a packet */
54         struct {
55                 void (*func)(struct smb2_transport *, void *);
56                 void *private;
57                 uint_t period;
58         } idle;
59 };
60
61
62 /*
63   SMB2 tree context
64 */
65 struct smb2_tree {
66         struct smb2_session *session;
67         uint32_t tid;
68 };
69
70 /*
71   SMB2 session context
72 */
73 struct smb2_session {
74         struct smb2_transport *transport;
75         struct gensec_security *gensec;
76         uint64_t uid;
77         DATA_BLOB session_key;
78 };
79
80
81 struct smb2_request_buffer {
82         /* the raw SMB2 buffer, including the 4 byte length header */
83         uint8_t *buffer;
84         
85         /* the size of the raw buffer, including 4 byte header */
86         size_t size;
87         
88         /* how much has been allocated - on reply the buffer is over-allocated to 
89            prevent too many realloc() calls 
90         */
91         size_t allocated;
92         
93         /* the start of the SMB2 header - this is always buffer+4 */
94         uint8_t *hdr;
95         
96         /* the packet body */
97         uint8_t *body;
98         size_t body_fixed;
99         size_t body_size;
100
101         /* this point to the next dynamic byte that can be used
102          * this will be moved when some dynamic data is pushed
103          */
104         uint8_t *dynamic;
105 };
106
107
108 /*
109   a client request moves between the following 4 states.
110 */
111 enum smb2_request_state {SMB2_REQUEST_INIT, /* we are creating the request */
112                         SMB2_REQUEST_RECV, /* we are waiting for a matching reply */
113                         SMB2_REQUEST_DONE, /* the request is finished */
114                         SMB2_REQUEST_ERROR}; /* a packet or transport level error has occurred */
115
116 /* the context for a single SMB2 request */
117 struct smb2_request {
118         /* allow a request to be part of a list of requests */
119         struct smb2_request *next, *prev;
120
121         /* each request is in one of 3 possible states */
122         enum smb2_request_state state;
123         
124         struct smb2_transport *transport;
125         struct smb2_session   *session;
126         struct smb2_tree      *tree;
127
128         uint64_t seqnum;
129
130         struct {
131                 BOOL do_cancel;
132                 BOOL can_cancel;
133                 uint32_t pending_id;
134         } cancel;
135
136         /* the NT status for this request. Set by packet receive code
137            or code detecting error. */
138         NTSTATUS status;
139         
140         struct smb2_request_buffer in;
141         struct smb2_request_buffer out;
142
143         /* information on what to do with a reply when it is received
144            asyncronously. If this is not setup when a reply is received then
145            the reply is discarded
146
147            The private pointer is private to the caller of the client
148            library (the application), not private to the library
149         */
150         struct {
151                 void (*fn)(struct smb2_request *);
152                 void *private;
153         } async;
154 };
155
156
157 #define SMB2_MIN_SIZE 0x42
158
159 /* offsets into header elements */
160 #define SMB2_HDR_LENGTH         0x04
161 #define SMB2_HDR_PAD1           0x06
162 #define SMB2_HDR_STATUS         0x08
163 #define SMB2_HDR_OPCODE         0x0c
164 #define SMB2_HDR_UNKNOWN1       0x0e
165 #define SMB2_HDR_FLAGS          0x10
166 #define SMB2_HDR_CHAIN_OFFSET   0x14
167 #define SMB2_HDR_SEQNUM         0x18
168 #define SMB2_HDR_PID            0x20
169 #define SMB2_HDR_TID            0x24
170 #define SMB2_HDR_UID            0x28 /* 64 bit */
171 #define SMB2_HDR_SIG            0x30 /* guess ... */
172 #define SMB2_HDR_BODY           0x40
173
174 /* SMB2 opcodes */
175 #define SMB2_OP_NEGPROT   0x00
176 #define SMB2_OP_SESSSETUP 0x01
177 #define SMB2_OP_LOGOFF    0x02
178 #define SMB2_OP_TCON      0x03
179 #define SMB2_OP_TDIS      0x04
180 #define SMB2_OP_CREATE    0x05
181 #define SMB2_OP_CLOSE     0x06
182 #define SMB2_OP_FLUSH     0x07
183 #define SMB2_OP_READ      0x08
184 #define SMB2_OP_WRITE     0x09
185 #define SMB2_OP_LOCK      0x0a
186 #define SMB2_OP_IOCTL     0x0b
187 #define SMB2_OP_CANCEL    0x0c
188 #define SMB2_OP_KEEPALIVE 0x0d
189 #define SMB2_OP_FIND      0x0e
190 #define SMB2_OP_NOTIFY    0x0f
191 #define SMB2_OP_GETINFO   0x10
192 #define SMB2_OP_SETINFO   0x11
193 #define SMB2_OP_BREAK     0x12
194
195 #define SMB2_MAGIC 0x424D53FE /* 0xFE 'S' 'M' 'B' */
196
197 /*
198   check that a body has the expected size
199 */
200 #define SMB2_CHECK_PACKET_RECV(req, size, dynamic) do { \
201         size_t is_size = req->in.body_size; \
202         uint16_t field_size = SVAL(req->in.body, 0); \
203         uint16_t want_size = ((dynamic)?(size)+1:(size)); \
204         if (is_size < (size)) { \
205                 DEBUG(0,("%s: buffer too small 0x%x. Expected 0x%x\n", \
206                          __location__, (unsigned)is_size, (unsigned)want_size)); \
207                 return NT_STATUS_BUFFER_TOO_SMALL; \
208         }\
209         if (field_size != want_size) { \
210                 DEBUG(0,("%s: unexpected fixed body size 0x%x. Expected 0x%x\n", \
211                          __location__, (unsigned)field_size, (unsigned)want_size)); \
212                 return NT_STATUS_INVALID_PARAMETER; \
213         } \
214 } while (0)