r11692: added a full composite (async) spnego session setup for SMB2. This
[ab/samba.git/.git] / source4 / libcli / smb2 / request.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client request handling
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "include/dlinklist.h"
27 #include "lib/events/events.h"
28
29 /*
30   initialise a smb2 request
31 */
32 struct smb2_request *smb2_request_init(struct smb2_transport *transport, 
33                                        uint16_t opcode, uint32_t body_size)
34 {
35         struct smb2_request *req;
36
37         req = talloc(transport, struct smb2_request);
38         if (req == NULL) return NULL;
39
40         req->state     = SMB2_REQUEST_INIT;
41         req->transport = transport;
42         req->session   = NULL;
43         req->tree      = NULL;
44         req->seqnum    = transport->seqnum++;
45         req->status    = NT_STATUS_OK;
46         req->async.fn  = NULL;
47         req->next = req->prev = NULL;
48
49         ZERO_STRUCT(req->in);
50         
51         req->out.allocated = SMB2_HDR_BODY+NBT_HDR_SIZE+body_size;
52         req->out.buffer    = talloc_size(req, req->out.allocated);
53         if (req->out.buffer == NULL) {
54                 talloc_free(req);
55                 return NULL;
56         }
57
58         req->out.size      = SMB2_HDR_BODY+NBT_HDR_SIZE + body_size;
59         req->out.hdr       = req->out.buffer + NBT_HDR_SIZE;
60         req->out.body      = req->out.hdr + SMB2_HDR_BODY;
61         req->out.body_size = body_size;
62         req->out.ptr       = req->out.body;
63
64         SIVAL(req->out.hdr, 0,                SMB2_MAGIC);
65         SSVAL(req->out.hdr, SMB2_HDR_LENGTH,  SMB2_HDR_BODY);
66         SSVAL(req->out.hdr, SMB2_HDR_PAD1,    0);
67         SIVAL(req->out.hdr, SMB2_HDR_STATUS,  0);
68         SSVAL(req->out.hdr, SMB2_HDR_OPCODE,  opcode);
69         SSVAL(req->out.hdr, SMB2_HDR_PAD2,    0);
70         SIVAL(req->out.hdr, SMB2_HDR_FLAGS,   0);
71         SIVAL(req->out.hdr, SMB2_HDR_UNKNOWN, 0);
72         SBVAL(req->out.hdr, SMB2_HDR_SEQNUM,  req->seqnum);
73         SIVAL(req->out.hdr, SMB2_HDR_PID,     0);
74         SIVAL(req->out.hdr, SMB2_HDR_TID,     0);
75         SBVAL(req->out.hdr, SMB2_HDR_UID,     0);
76         memset(req->out.hdr+SMB2_HDR_SIG, 0, 16);
77
78         return req;
79 }
80
81 /*
82     initialise a smb2 request for tree operations
83 */
84 struct smb2_request *smb2_request_init_tree(struct smb2_tree *tree, 
85                                             uint16_t opcode, uint32_t body_size)
86 {
87         struct smb2_request *req = smb2_request_init(tree->session->transport, opcode, 
88                                                      body_size);
89         if (req == NULL) return NULL;
90
91         SBVAL(req->out.hdr,  SMB2_HDR_UID, tree->session->uid);
92         SIVAL(req->out.hdr,  SMB2_HDR_TID, tree->tid);
93         req->session = tree->session;
94         req->tree = tree;
95
96         return req;     
97 }
98
99 /* destroy a request structure and return final status */
100 NTSTATUS smb2_request_destroy(struct smb2_request *req)
101 {
102         NTSTATUS status;
103
104         /* this is the error code we give the application for when a
105            _send() call fails completely */
106         if (!req) return NT_STATUS_UNSUCCESSFUL;
107
108         if (req->transport) {
109                 /* remove it from the list of pending requests (a null op if
110                    its not in the list) */
111                 DLIST_REMOVE(req->transport->pending_recv, req);
112         }
113
114         if (req->state == SMBCLI_REQUEST_ERROR &&
115             NT_STATUS_IS_OK(req->status)) {
116                 req->status = NT_STATUS_INTERNAL_ERROR;
117         }
118
119         status = req->status;
120         talloc_free(req);
121         return status;
122 }
123
124 /*
125   receive a response to a packet
126 */
127 BOOL smb2_request_receive(struct smb2_request *req)
128 {
129         /* req can be NULL when a send has failed. This eliminates lots of NULL
130            checks in each module */
131         if (!req) return False;
132
133         /* keep receiving packets until this one is replied to */
134         while (req->state <= SMB2_REQUEST_RECV) {
135                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
136                         return False;
137                 }
138         }
139
140         return req->state == SMB2_REQUEST_DONE;
141 }
142
143 /* Return true if the last packet was in error */
144 BOOL smb2_request_is_error(struct smb2_request *req)
145 {
146         return NT_STATUS_IS_ERR(req->status);
147 }
148
149 /*
150   check if a range in the reply body is out of bounds
151 */
152 BOOL smb2_oob_in(struct smb2_request *req, const uint8_t *ptr, uint_t size)
153 {
154         /* be careful with wraparound! */
155         if (ptr < req->in.body ||
156             ptr >= req->in.body + req->in.body_size ||
157             size > req->in.body_size ||
158             ptr + size > req->in.body + req->in.body_size) {
159                 return True;
160         }
161         return False;
162 }
163
164 /*
165   check if a range in the outgoing body is out of bounds
166 */
167 BOOL smb2_oob_out(struct smb2_request *req, const uint8_t *ptr, uint_t size)
168 {
169         /* be careful with wraparound! */
170         if (ptr < req->out.body ||
171             ptr >= req->out.body + req->out.body_size ||
172             size > req->out.body_size ||
173             ptr + size > req->out.body + req->out.body_size) {
174                 return True;
175         }
176         return False;
177 }
178
179 /*
180   pull a data blob from the body of a reply
181 */
182 DATA_BLOB smb2_pull_blob(struct smb2_request *req, uint8_t *ptr, uint_t size)
183 {
184         if (smb2_oob_in(req, ptr, size)) {
185                 return data_blob(NULL, 0);
186         }
187         return data_blob_talloc(req, ptr, size);
188 }
189
190 /*
191   pull a ofs/length/blob triple into a data blob
192   the ptr points to the start of the offset/length pair
193 */
194 NTSTATUS smb2_pull_ofs_blob(struct smb2_request *req, uint8_t *ptr, DATA_BLOB *blob)
195 {
196         uint16_t ofs, size;
197         if (smb2_oob_in(req, ptr, 4)) {
198                 return NT_STATUS_BUFFER_TOO_SMALL;
199         }
200         ofs  = SVAL(ptr, 0);
201         size = SVAL(ptr, 2);
202         if (smb2_oob_in(req, req->in.hdr + ofs, size)) {
203                 return NT_STATUS_BUFFER_TOO_SMALL;
204         }
205         *blob = data_blob_talloc(req, req->in.hdr+ofs, size);
206         NT_STATUS_HAVE_NO_MEMORY(blob->data);
207         return NT_STATUS_OK;
208 }
209
210 /*
211   push a ofs/length/blob triple into a data blob
212   the ptr points to the start of the offset/length pair
213
214   NOTE: assumes blob goes immediately after the offset/length pair. Needs 
215         to be generalised
216 */
217 NTSTATUS smb2_push_ofs_blob(struct smb2_request *req, uint8_t *ptr, DATA_BLOB blob)
218 {
219         if (smb2_oob_out(req, ptr, 4+blob.length)) {
220                 return NT_STATUS_BUFFER_TOO_SMALL;
221         }
222         SSVAL(ptr, 0, 4 + (ptr - req->out.hdr));
223         SSVAL(ptr, 2, blob.length);
224         memcpy(ptr+4, blob.data, blob.length);
225         return NT_STATUS_OK;
226 }
227
228 /*
229   pull a string in a ofs/length/blob format
230 */
231 NTSTATUS smb2_pull_ofs_string(struct smb2_request *req, uint8_t *ptr, 
232                               const char **str)
233 {
234         DATA_BLOB blob;
235         NTSTATUS status;
236         ssize_t size;
237         void *vstr;
238         status = smb2_pull_ofs_blob(req, ptr, &blob);
239         NT_STATUS_NOT_OK_RETURN(status);
240         size = convert_string_talloc(req, CH_UTF16, CH_UNIX, 
241                                      blob.data, blob.length, &vstr);
242         data_blob_free(&blob);
243         (*str) = vstr;
244         if (size == -1) {
245                 return NT_STATUS_ILLEGAL_CHARACTER;
246         }
247         return NT_STATUS_OK;
248 }
249
250 /*
251   create a UTF16 string in a blob from a char*
252 */
253 NTSTATUS smb2_string_blob(TALLOC_CTX *mem_ctx, const char *str, DATA_BLOB *blob)
254 {
255         ssize_t size;
256         size = convert_string_talloc(mem_ctx, CH_UNIX, CH_UTF16, 
257                                      str, strlen(str), (void **)&blob->data);
258         if (size == -1) {
259                 return NT_STATUS_ILLEGAL_CHARACTER;
260         }
261         blob->length = size;
262         return NT_STATUS_OK;    
263 }