libcli/raw: add a recv_helper hook infrastructure
[sfrench/samba-autobuild/.git] / source4 / libcli / raw / rawrequest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Andrew Tridgell  2003
5    Copyright (C) James Myers 2003 <myersjj@samba.org>
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22   this file implements functions for manipulating the 'struct smbcli_request' structure in libsmb
23 */
24
25 #include "includes.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/raw/raw_proto.h"
28 #include "lib/util/dlinklist.h"
29 #include "lib/events/events.h"
30 #include "param/param.h"
31 #include "librpc/ndr/libndr.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33
34 /* we over allocate the data buffer to prevent too many realloc calls */
35 #define REQ_OVER_ALLOCATION 0
36
37 /* assume that a character will not consume more than 3 bytes per char */
38 #define MAX_BYTES_PER_CHAR 3
39
40 /* setup the bufinfo used for strings and range checking */
41 void smb_setup_bufinfo(struct smbcli_request *req)
42 {
43         req->in.bufinfo.mem_ctx    = req;
44         req->in.bufinfo.flags      = 0;
45         if (req->flags2 & FLAGS2_UNICODE_STRINGS) {
46                 req->in.bufinfo.flags = BUFINFO_FLAG_UNICODE;
47         }
48         req->in.bufinfo.align_base = req->in.buffer;
49         req->in.bufinfo.data       = req->in.data;
50         req->in.bufinfo.data_size  = req->in.data_size;
51 }
52
53
54 /* destroy a request structure and return final status */
55 _PUBLIC_ NTSTATUS smbcli_request_destroy(struct smbcli_request *req)
56 {
57         NTSTATUS status;
58
59         /* this is the error code we give the application for when a
60            _send() call fails completely */
61         if (!req) return NT_STATUS_UNSUCCESSFUL;
62
63         if (req->transport) {
64                 /* remove it from the list of pending requests (a null op if
65                    its not in the list) */
66                 DLIST_REMOVE(req->transport->pending_recv, req);
67         }
68
69         if (req->state == SMBCLI_REQUEST_ERROR &&
70             NT_STATUS_IS_OK(req->status)) {
71                 req->status = NT_STATUS_INTERNAL_ERROR;
72         }
73
74         status = req->status;
75         talloc_free(req);
76         return status;
77 }
78
79
80 /*
81   low-level function to setup a request buffer for a non-SMB packet 
82   at the transport level
83 */
84 struct smbcli_request *smbcli_request_setup_nonsmb(struct smbcli_transport *transport, size_t size)
85 {
86         struct smbcli_request *req;
87
88         req = talloc(transport, struct smbcli_request);
89         if (!req) {
90                 return NULL;
91         }
92         ZERO_STRUCTP(req);
93
94         /* setup the request context */
95         req->state = SMBCLI_REQUEST_INIT;
96         req->transport = transport;
97         req->session = NULL;
98         req->tree = NULL;
99         req->out.size = size;
100
101         /* over allocate by a small amount */
102         req->out.allocated = req->out.size + REQ_OVER_ALLOCATION; 
103
104         req->out.buffer = talloc_array(req, uint8_t, req->out.allocated);
105         if (!req->out.buffer) {
106                 return NULL;
107         }
108
109         SIVAL(req->out.buffer, 0, 0);
110
111         return req;
112 }
113
114
115 /*
116   setup a SMB packet at transport level
117 */
118 struct smbcli_request *smbcli_request_setup_transport(struct smbcli_transport *transport,
119                                                       uint8_t command, uint_t wct, uint_t buflen)
120 {
121         struct smbcli_request *req;
122
123         req = smbcli_request_setup_nonsmb(transport, NBT_HDR_SIZE + MIN_SMB_SIZE + wct*2 + buflen);
124
125         if (!req) return NULL;
126         
127         req->out.hdr = req->out.buffer + NBT_HDR_SIZE;
128         req->out.vwv = req->out.hdr + HDR_VWV;
129         req->out.wct = wct;
130         req->out.data = req->out.vwv + VWV(wct) + 2;
131         req->out.data_size = buflen;
132         req->out.ptr = req->out.data;
133
134         SCVAL(req->out.hdr, HDR_WCT, wct);
135         SSVAL(req->out.vwv, VWV(wct), buflen);
136
137         memcpy(req->out.hdr, "\377SMB", 4);
138         SCVAL(req->out.hdr,HDR_COM,command);
139
140         SCVAL(req->out.hdr,HDR_FLG, FLAG_CASELESS_PATHNAMES);
141         SSVAL(req->out.hdr,HDR_FLG2, 0);
142
143         if (command != SMBtranss && command != SMBtranss2) {
144                 /* assign a mid */
145                 req->mid = smbcli_transport_next_mid(transport);
146         }
147
148         /* copy the pid, uid and mid to the request */
149         SSVAL(req->out.hdr, HDR_PID, 0);
150         SSVAL(req->out.hdr, HDR_UID, 0);
151         SSVAL(req->out.hdr, HDR_MID, req->mid);
152         SSVAL(req->out.hdr, HDR_TID,0);
153         SSVAL(req->out.hdr, HDR_PIDHIGH,0);
154         SIVAL(req->out.hdr, HDR_RCLS, 0);
155         memset(req->out.hdr+HDR_SS_FIELD, 0, 10);
156         
157         return req;
158 }
159
160 /*
161   setup a reply in req->out with the given word count and initial data
162   buffer size.  the caller will then fill in the command words and
163   data before calling smbcli_request_send() to send the reply on its
164   way. This interface is used before a session is setup.
165 */
166 struct smbcli_request *smbcli_request_setup_session(struct smbcli_session *session,
167                                                     uint8_t command, uint_t wct, size_t buflen)
168 {
169         struct smbcli_request *req;
170
171         req = smbcli_request_setup_transport(session->transport, command, wct, buflen);
172
173         if (!req) return NULL;
174
175         req->session = session;
176
177         SSVAL(req->out.hdr, HDR_FLG2, session->flags2);
178         SSVAL(req->out.hdr, HDR_PID, session->pid & 0xFFFF);
179         SSVAL(req->out.hdr, HDR_PIDHIGH, session->pid >> 16);
180         SSVAL(req->out.hdr, HDR_UID, session->vuid);
181         
182         return req;
183 }
184
185 /*
186   setup a request for tree based commands
187 */
188 struct smbcli_request *smbcli_request_setup(struct smbcli_tree *tree,
189                                             uint8_t command, 
190                                             uint_t wct, uint_t buflen)
191 {
192         struct smbcli_request *req;
193
194         req = smbcli_request_setup_session(tree->session, command, wct, buflen);
195         if (req) {
196                 req->tree = tree;
197                 SSVAL(req->out.hdr,HDR_TID,tree->tid);
198         }
199         return req;
200 }
201
202
203 /*
204   grow the allocation of the data buffer portion of a reply
205   packet. Note that as this can reallocate the packet buffer this
206   invalidates any local pointers into the packet.
207
208   To cope with this req->out.ptr is supplied. This will be updated to
209   point at the same offset into the packet as before this call
210 */
211 static void smbcli_req_grow_allocation(struct smbcli_request *req, uint_t new_size)
212 {
213         int delta;
214         uint8_t *buf2;
215
216         delta = new_size - req->out.data_size;
217         if (delta + req->out.size <= req->out.allocated) {
218                 /* it fits in the preallocation */
219                 return;
220         }
221
222         /* we need to realloc */
223         req->out.allocated = req->out.size + delta + REQ_OVER_ALLOCATION;
224         buf2 = talloc_realloc(req, req->out.buffer, uint8_t, req->out.allocated);
225         if (buf2 == NULL) {
226                 smb_panic("out of memory in req_grow_allocation");
227         }
228
229         if (buf2 == req->out.buffer) {
230                 /* the malloc library gave us the same pointer */
231                 return;
232         }
233         
234         /* update the pointers into the packet */
235         req->out.data = buf2 + PTR_DIFF(req->out.data, req->out.buffer);
236         req->out.ptr  = buf2 + PTR_DIFF(req->out.ptr,  req->out.buffer);
237         req->out.vwv  = buf2 + PTR_DIFF(req->out.vwv,  req->out.buffer);
238         req->out.hdr  = buf2 + PTR_DIFF(req->out.hdr,  req->out.buffer);
239
240         req->out.buffer = buf2;
241 }
242
243
244 /*
245   grow the data buffer portion of a reply packet. Note that as this
246   can reallocate the packet buffer this invalidates any local pointers
247   into the packet. 
248
249   To cope with this req->out.ptr is supplied. This will be updated to
250   point at the same offset into the packet as before this call
251 */
252 static void smbcli_req_grow_data(struct smbcli_request *req, uint_t new_size)
253 {
254         int delta;
255
256         smbcli_req_grow_allocation(req, new_size);
257
258         delta = new_size - req->out.data_size;
259
260         req->out.size += delta;
261         req->out.data_size += delta;
262
263         /* set the BCC to the new data size */
264         SSVAL(req->out.vwv, VWV(req->out.wct), new_size);
265 }
266
267
268 /*
269   setup a chained reply in req->out with the given word count and
270   initial data buffer size.
271 */
272 NTSTATUS smbcli_chained_request_setup(struct smbcli_request *req,
273                                       uint8_t command, 
274                                       uint_t wct, size_t buflen)
275 {
276         uint_t new_size = 1 + (wct*2) + 2 + buflen;
277
278         SSVAL(req->out.vwv, VWV(0), command);
279         SSVAL(req->out.vwv, VWV(1), req->out.size - NBT_HDR_SIZE);
280
281         smbcli_req_grow_allocation(req, req->out.data_size + new_size);
282
283         req->out.vwv = req->out.buffer + req->out.size + 1;
284         SCVAL(req->out.vwv, -1, wct);
285         SSVAL(req->out.vwv, VWV(wct), buflen);
286
287         req->out.size += new_size;
288         req->out.data_size += new_size;
289
290         return NT_STATUS_OK;
291 }
292
293 /*
294   aadvance to the next chained reply in a request
295 */
296 NTSTATUS smbcli_chained_advance(struct smbcli_request *req)
297 {
298         uint8_t *buffer;
299
300         if (CVAL(req->in.vwv, VWV(0)) == SMB_CHAIN_NONE) {
301                 return NT_STATUS_NOT_FOUND;
302         }
303
304         buffer = req->in.hdr + SVAL(req->in.vwv, VWV(1));
305
306         if (buffer + 3 > req->in.buffer + req->in.size) {
307                 return NT_STATUS_BUFFER_TOO_SMALL;
308         }
309
310         req->in.vwv = buffer + 1;
311         req->in.wct = CVAL(buffer, 0);
312         if (buffer + 3 + req->in.wct*2 > req->in.buffer + req->in.size) {
313                 return NT_STATUS_BUFFER_TOO_SMALL;
314         }
315         req->in.data = req->in.vwv + 2 + req->in.wct * 2;
316         req->in.data_size = SVAL(req->in.vwv, VWV(req->in.wct));
317
318         /* fix the bufinfo */
319         smb_setup_bufinfo(req);
320
321         if (buffer + 3 + req->in.wct*2 + req->in.data_size > 
322             req->in.buffer + req->in.size) {
323                 return NT_STATUS_BUFFER_TOO_SMALL;
324         }
325
326         return NT_STATUS_OK;
327 }
328
329
330 /*
331   send a message
332 */
333 bool smbcli_request_send(struct smbcli_request *req)
334 {
335         if (IVAL(req->out.buffer, 0) == 0) {
336                 _smb_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
337         }
338
339         smbcli_request_calculate_sign_mac(req);
340
341         smbcli_transport_send(req);
342
343         return true;
344 }
345
346
347 /*
348   receive a response to a packet
349 */
350 bool smbcli_request_receive(struct smbcli_request *req)
351 {
352         /* req can be NULL when a send has failed. This eliminates lots of NULL
353            checks in each module */
354         if (!req) return false;
355
356         /* keep receiving packets until this one is replied to */
357         while (req->state <= SMBCLI_REQUEST_RECV) {
358                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
359                         return false;
360                 }
361         }
362
363         return req->state == SMBCLI_REQUEST_DONE;
364 }
365
366
367 /*
368   receive another reply to a request - this is used for requests that
369   have multi-part replies (such as SMBtrans2)
370 */
371 bool smbcli_request_receive_more(struct smbcli_request *req)
372 {
373         req->state = SMBCLI_REQUEST_RECV;
374         DLIST_ADD(req->transport->pending_recv, req);
375
376         return smbcli_request_receive(req);
377 }
378
379
380 /*
381   handle oplock break requests from the server - return true if the request was
382   an oplock break
383 */
384 bool smbcli_handle_oplock_break(struct smbcli_transport *transport, uint_t len, const uint8_t *hdr, const uint8_t *vwv)
385 {
386         /* we must be very fussy about what we consider an oplock break to avoid
387            matching readbraw replies */
388         if (len != MIN_SMB_SIZE + VWV(8) + NBT_HDR_SIZE ||
389             (CVAL(hdr, HDR_FLG) & FLAG_REPLY) ||
390             CVAL(hdr,HDR_COM) != SMBlockingX ||
391             SVAL(hdr, HDR_MID) != 0xFFFF ||
392             SVAL(vwv,VWV(6)) != 0 ||
393             SVAL(vwv,VWV(7)) != 0) {
394                 return false;
395         }
396
397         if (transport->oplock.handler) {
398                 uint16_t tid = SVAL(hdr, HDR_TID);
399                 uint16_t fnum = SVAL(vwv,VWV(2));
400                 uint8_t level = CVAL(vwv,VWV(3)+1);
401                 transport->oplock.handler(transport, tid, fnum, level, transport->oplock.private);
402         }
403
404         return true;
405 }
406
407 /*
408   wait for a reply to be received for a packet that just returns an error
409   code and nothing more
410 */
411 _PUBLIC_ NTSTATUS smbcli_request_simple_recv(struct smbcli_request *req)
412 {
413         (void) smbcli_request_receive(req);
414         return smbcli_request_destroy(req);
415 }
416
417
418 /* Return true if the last packet was in error */
419 bool smbcli_request_is_error(struct smbcli_request *req)
420 {
421         return NT_STATUS_IS_ERR(req->status);
422 }
423
424 /*
425   append a string into the data portion of the request packet
426
427   return the number of bytes added to the packet
428 */
429 size_t smbcli_req_append_string(struct smbcli_request *req, const char *str, uint_t flags)
430 {
431         size_t len;
432
433         /* determine string type to use */
434         if (!(flags & (STR_ASCII|STR_UNICODE))) {
435                 flags |= (req->transport->negotiate.capabilities & CAP_UNICODE) ? STR_UNICODE : STR_ASCII;
436         }
437
438         len = (strlen(str)+2) * MAX_BYTES_PER_CHAR;             
439
440         smbcli_req_grow_allocation(req, len + req->out.data_size);
441
442         len = push_string(lp_iconv_convenience(global_loadparm), req->out.data + req->out.data_size, str, len, flags);
443
444         smbcli_req_grow_data(req, len + req->out.data_size);
445
446         return len;
447 }
448
449
450 /*
451   this is like smbcli_req_append_string but it also return the
452   non-terminated string byte length, which can be less than the number
453   of bytes consumed in the packet for 2 reasons:
454
455    1) the string in the packet may be null terminated
456    2) the string in the packet may need a 1 byte UCS2 alignment
457
458  this is used in places where the non-terminated string byte length is
459  placed in the packet as a separate field  
460 */
461 size_t smbcli_req_append_string_len(struct smbcli_request *req, const char *str, uint_t flags, int *len)
462 {
463         int diff = 0;
464         size_t ret;
465
466         /* determine string type to use */
467         if (!(flags & (STR_ASCII|STR_UNICODE))) {
468                 flags |= (req->transport->negotiate.capabilities & CAP_UNICODE) ? STR_UNICODE : STR_ASCII;
469         }
470
471         /* see if an alignment byte will be used */
472         if ((flags & STR_UNICODE) && !(flags & STR_NOALIGN)) {
473                 diff = ucs2_align(NULL, req->out.data + req->out.data_size, flags);
474         }
475
476         /* do the hard work */
477         ret = smbcli_req_append_string(req, str, flags);
478
479         /* see if we need to subtract the termination */
480         if (flags & STR_TERMINATE) {
481                 diff += (flags & STR_UNICODE) ? 2 : 1;
482         }
483
484         if (ret >= diff) {
485                 (*len) = ret - diff;
486         } else {
487                 (*len) = ret;
488         }
489
490         return ret;
491 }
492
493
494 /*
495   push a string into the data portion of the request packet, growing it if necessary
496   this gets quite tricky - please be very careful to cover all cases when modifying this
497
498   if dest is NULL, then put the string at the end of the data portion of the packet
499
500   if dest_len is -1 then no limit applies
501 */
502 size_t smbcli_req_append_ascii4(struct smbcli_request *req, const char *str, uint_t flags)
503 {
504         size_t size;
505         smbcli_req_append_bytes(req, (const uint8_t *)"\4", 1);
506         size = smbcli_req_append_string(req, str, flags);
507         return size + 1;
508 }
509
510
511 /*
512   push a blob into the data portion of the request packet, growing it if necessary
513   this gets quite tricky - please be very careful to cover all cases when modifying this
514
515   if dest is NULL, then put the blob at the end of the data portion of the packet
516 */
517 size_t smbcli_req_append_blob(struct smbcli_request *req, const DATA_BLOB *blob)
518 {
519         smbcli_req_grow_allocation(req, req->out.data_size + blob->length);
520         memcpy(req->out.data + req->out.data_size, blob->data, blob->length);
521         smbcli_req_grow_data(req, req->out.data_size + blob->length);
522         return blob->length;
523 }
524
525 /*
526   append raw bytes into the data portion of the request packet
527   return the number of bytes added
528 */
529 size_t smbcli_req_append_bytes(struct smbcli_request *req, const uint8_t *bytes, size_t byte_len)
530 {
531         smbcli_req_grow_allocation(req, byte_len + req->out.data_size);
532         memcpy(req->out.data + req->out.data_size, bytes, byte_len);
533         smbcli_req_grow_data(req, byte_len + req->out.data_size);
534         return byte_len;
535 }
536
537 /*
538   append variable block (type 5 buffer) into the data portion of the request packet
539   return the number of bytes added
540 */
541 size_t smbcli_req_append_var_block(struct smbcli_request *req, const uint8_t *bytes, uint16_t byte_len)
542 {
543         smbcli_req_grow_allocation(req, byte_len + 3 + req->out.data_size);
544         SCVAL(req->out.data + req->out.data_size, 0, 5);
545         SSVAL(req->out.data + req->out.data_size, 1, byte_len);         /* add field length */
546         if (byte_len > 0) {
547                 memcpy(req->out.data + req->out.data_size + 3, bytes, byte_len);
548         }
549         smbcli_req_grow_data(req, byte_len + 3 + req->out.data_size);
550         return byte_len + 3;
551 }
552
553
554 /*
555   pull a UCS2 string from a request packet, returning a talloced unix string
556
557   the string length is limited by the 3 things:
558    - the data size in the request (end of packet)
559    - the passed 'byte_len' if it is not -1
560    - the end of string (null termination)
561
562   Note that 'byte_len' is the number of bytes in the packet
563
564   on failure zero is returned and *dest is set to NULL, otherwise the number
565   of bytes consumed in the packet is returned
566 */
567 static size_t smbcli_req_pull_ucs2(struct request_bufinfo *bufinfo, TALLOC_CTX *mem_ctx,
568                                 char **dest, const uint8_t *src, int byte_len, uint_t flags)
569 {
570         int src_len, src_len2, alignment=0;
571         ssize_t ret;
572
573         if (!(flags & STR_NOALIGN) && ucs2_align(bufinfo->align_base, src, flags)) {
574                 src++;
575                 alignment=1;
576                 if (byte_len != -1) {
577                         byte_len--;
578                 }
579         }
580
581         src_len = bufinfo->data_size - PTR_DIFF(src, bufinfo->data);
582         if (src_len < 0) {
583                 *dest = NULL;
584                 return 0;
585         }
586         if (byte_len != -1 && src_len > byte_len) {
587                 src_len = byte_len;
588         }
589
590         src_len2 = utf16_len_n(src, src_len);
591
592         /* ucs2 strings must be at least 2 bytes long */
593         if (src_len2 < 2) {
594                 *dest = NULL;
595                 return 0;
596         }
597
598         ret = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX, src, src_len2, (void **)dest);
599         if (ret == -1) {
600                 *dest = NULL;
601                 return 0;
602         }
603
604         return src_len2 + alignment;
605 }
606
607 /*
608   pull a ascii string from a request packet, returning a talloced string
609
610   the string length is limited by the 3 things:
611    - the data size in the request (end of packet)
612    - the passed 'byte_len' if it is not -1
613    - the end of string (null termination)
614
615   Note that 'byte_len' is the number of bytes in the packet
616
617   on failure zero is returned and *dest is set to NULL, otherwise the number
618   of bytes consumed in the packet is returned
619 */
620 size_t smbcli_req_pull_ascii(struct request_bufinfo *bufinfo, TALLOC_CTX *mem_ctx,
621                              char **dest, const uint8_t *src, int byte_len, uint_t flags)
622 {
623         int src_len, src_len2;
624         ssize_t ret;
625
626         src_len = bufinfo->data_size - PTR_DIFF(src, bufinfo->data);
627         if (src_len < 0) {
628                 *dest = NULL;
629                 return 0;
630         }
631         if (byte_len != -1 && src_len > byte_len) {
632                 src_len = byte_len;
633         }
634         src_len2 = strnlen((const char *)src, src_len);
635         if (src_len2 < src_len - 1) {
636                 /* include the termination if we didn't reach the end of the packet */
637                 src_len2++;
638         }
639
640         ret = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_DOS, CH_UNIX, src, src_len2, (void **)dest);
641
642         if (ret == -1) {
643                 *dest = NULL;
644                 return 0;
645         }
646
647         return ret;
648 }
649
650 /**
651   pull a string from a request packet, returning a talloced string
652
653   the string length is limited by the 3 things:
654    - the data size in the request (end of packet)
655    - the passed 'byte_len' if it is not -1
656    - the end of string (null termination)
657
658   Note that 'byte_len' is the number of bytes in the packet
659
660   on failure zero is returned and *dest is set to NULL, otherwise the number
661   of bytes consumed in the packet is returned
662 */
663 size_t smbcli_req_pull_string(struct request_bufinfo *bufinfo, TALLOC_CTX *mem_ctx, 
664                            char **dest, const uint8_t *src, int byte_len, uint_t flags)
665 {
666         if (!(flags & STR_ASCII) && 
667             (((flags & STR_UNICODE) || (bufinfo->flags & BUFINFO_FLAG_UNICODE)))) {
668                 return smbcli_req_pull_ucs2(bufinfo, mem_ctx, dest, src, byte_len, flags);
669         }
670
671         return smbcli_req_pull_ascii(bufinfo, mem_ctx, dest, src, byte_len, flags);
672 }
673
674
675 /**
676   pull a DATA_BLOB from a reply packet, returning a talloced blob
677   make sure we don't go past end of packet
678
679   if byte_len is -1 then limit the blob only by packet size
680 */
681 DATA_BLOB smbcli_req_pull_blob(struct request_bufinfo *bufinfo, TALLOC_CTX *mem_ctx, const uint8_t *src, int byte_len)
682 {
683         int src_len;
684
685         src_len = bufinfo->data_size - PTR_DIFF(src, bufinfo->data);
686
687         if (src_len < 0) {
688                 return data_blob(NULL, 0);
689         }
690
691         if (byte_len != -1 && src_len > byte_len) {
692                 src_len = byte_len;
693         }
694
695         return data_blob_talloc(mem_ctx, src, src_len);
696 }
697
698 /* check that a lump of data in a request is within the bounds of the data section of
699    the packet */
700 static bool smbcli_req_data_oob(struct request_bufinfo *bufinfo, const uint8_t *ptr, uint32_t count)
701 {
702         /* be careful with wraparound! */
703         if ((uintptr_t)ptr < (uintptr_t)bufinfo->data ||
704             (uintptr_t)ptr >= (uintptr_t)bufinfo->data + bufinfo->data_size ||
705             count > bufinfo->data_size ||
706             (uintptr_t)ptr + count > (uintptr_t)bufinfo->data + bufinfo->data_size) {
707                 return true;
708         }
709         return false;
710 }
711
712 /*
713   pull a lump of data from a request packet
714
715   return false if any part is outside the data portion of the packet
716 */
717 bool smbcli_raw_pull_data(struct request_bufinfo *bufinfo, const uint8_t *src, int len, uint8_t *dest)
718 {
719         if (len == 0) return true;
720
721         if (smbcli_req_data_oob(bufinfo, src, len)) {
722                 return false;
723         }
724
725         memcpy(dest, src, len);
726         return true;
727 }
728
729
730 /*
731   put a NTTIME into a packet
732 */
733 void smbcli_push_nttime(void *base, uint16_t offset, NTTIME t)
734 {
735         SBVAL(base, offset, t);
736 }
737
738 /*
739   pull a NTTIME from a packet
740 */
741 NTTIME smbcli_pull_nttime(void *base, uint16_t offset)
742 {
743         NTTIME ret = BVAL(base, offset);
744         return ret;
745 }
746
747 /**
748   pull a UCS2 string from a blob, returning a talloced unix string
749
750   the string length is limited by the 3 things:
751    - the data size in the blob
752    - the passed 'byte_len' if it is not -1
753    - the end of string (null termination)
754
755   Note that 'byte_len' is the number of bytes in the packet
756
757   on failure zero is returned and *dest is set to NULL, otherwise the number
758   of bytes consumed in the blob is returned
759 */
760 size_t smbcli_blob_pull_ucs2(TALLOC_CTX* mem_ctx,
761                              const DATA_BLOB *blob, const char **dest, 
762                              const uint8_t *src, int byte_len, uint_t flags)
763 {
764         int src_len, src_len2, alignment=0;
765         ssize_t ret;
766         char *dest2;
767
768         if (src < blob->data ||
769             src >= (blob->data + blob->length)) {
770                 *dest = NULL;
771                 return 0;
772         }
773
774         src_len = blob->length - PTR_DIFF(src, blob->data);
775
776         if (byte_len != -1 && src_len > byte_len) {
777                 src_len = byte_len;
778         }
779
780         if (!(flags & STR_NOALIGN) && ucs2_align(blob->data, src, flags)) {
781                 src++;
782                 alignment=1;
783                 src_len--;
784         }
785
786         if (src_len < 2) {
787                 *dest = NULL;
788                 return 0;
789         }
790
791         src_len2 = utf16_len_n(src, src_len);
792
793         ret = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX, src, src_len2, (void **)&dest2);
794         if (ret == -1) {
795                 *dest = NULL;
796                 return 0;
797         }
798         *dest = dest2;
799
800         return src_len2 + alignment;
801 }
802
803 /**
804   pull a ascii string from a blob, returning a talloced string
805
806   the string length is limited by the 3 things:
807    - the data size in the blob
808    - the passed 'byte_len' if it is not -1
809    - the end of string (null termination)
810
811   Note that 'byte_len' is the number of bytes in the blob
812
813   on failure zero is returned and *dest is set to NULL, otherwise the number
814   of bytes consumed in the blob is returned
815 */
816 static size_t smbcli_blob_pull_ascii(TALLOC_CTX *mem_ctx,
817                                      const DATA_BLOB *blob, const char **dest, 
818                                      const uint8_t *src, int byte_len, uint_t flags)
819 {
820         int src_len, src_len2;
821         ssize_t ret;
822         char *dest2;
823
824         src_len = blob->length - PTR_DIFF(src, blob->data);
825         if (src_len < 0) {
826                 *dest = NULL;
827                 return 0;
828         }
829         if (byte_len != -1 && src_len > byte_len) {
830                 src_len = byte_len;
831         }
832         src_len2 = strnlen((const char *)src, src_len);
833
834         if (src_len2 < src_len - 1) {
835                 /* include the termination if we didn't reach the end of the packet */
836                 src_len2++;
837         }
838
839         ret = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_DOS, CH_UNIX, src, src_len2, (void **)&dest2);
840
841         if (ret == -1) {
842                 *dest = NULL;
843                 return 0;
844         }
845         *dest = dest2;
846
847         return ret;
848 }
849
850 /**
851   pull a string from a blob, returning a talloced struct smb_wire_string
852
853   the string length is limited by the 3 things:
854    - the data size in the blob
855    - length field on the wire
856    - the end of string (null termination)
857
858    if STR_LEN8BIT is set in the flags then assume the length field is
859    8 bits, instead of 32
860
861   on failure zero is returned and dest->s is set to NULL, otherwise the number
862   of bytes consumed in the blob is returned
863 */
864 size_t smbcli_blob_pull_string(struct smbcli_session *session,
865                                TALLOC_CTX *mem_ctx,
866                                const DATA_BLOB *blob, 
867                                struct smb_wire_string *dest, 
868                                uint16_t len_offset, uint16_t str_offset, 
869                                uint_t flags)
870 {
871         int extra;
872         dest->s = NULL;
873
874         if (!(flags & STR_ASCII)) {
875                 /* this is here to cope with SMB2 calls using the SMB
876                    parsers. SMB2 will pass smbcli_session==NULL, which forces
877                    unicode on (as used by SMB2) */
878                 if (session == NULL) {
879                         flags |= STR_UNICODE;
880                 } else if (session->transport->negotiate.capabilities & CAP_UNICODE) {
881                         flags |= STR_UNICODE;
882                 }
883         }
884
885         if (flags & STR_LEN8BIT) {
886                 if (len_offset > blob->length-1) {
887                         return 0;
888                 }
889                 dest->private_length = CVAL(blob->data, len_offset);
890         } else {
891                 if (len_offset > blob->length-4) {
892                         return 0;
893                 }
894                 dest->private_length = IVAL(blob->data, len_offset);
895         }
896         extra = 0;
897         dest->s = NULL;
898         if (!(flags & STR_ASCII) && (flags & STR_UNICODE)) {
899                 int align = 0;
900                 if ((str_offset&1) && !(flags & STR_NOALIGN)) {
901                         align = 1;
902                 }
903                 if (flags & STR_LEN_NOTERM) {
904                         extra = 2;
905                 }
906                 return align + extra + smbcli_blob_pull_ucs2(mem_ctx, blob, &dest->s, 
907                                                           blob->data+str_offset+align, 
908                                                           dest->private_length, flags);
909         }
910
911         if (flags & STR_LEN_NOTERM) {
912                 extra = 1;
913         }
914
915         return extra + smbcli_blob_pull_ascii(mem_ctx, blob, &dest->s, 
916                                            blob->data+str_offset, dest->private_length, flags);
917 }
918
919 /**
920   pull a string from a blob, returning a talloced char *
921
922   Currently only used by the UNIX search info level.
923
924   the string length is limited by 2 things:
925    - the data size in the blob
926    - the end of string (null termination)
927
928   on failure zero is returned and dest->s is set to NULL, otherwise the number
929   of bytes consumed in the blob is returned
930 */
931 size_t smbcli_blob_pull_unix_string(struct smbcli_session *session,
932                             TALLOC_CTX *mem_ctx,
933                             DATA_BLOB *blob, 
934                             const char **dest, 
935                             uint16_t str_offset, 
936                             uint_t flags)
937 {
938         int extra = 0;
939         *dest = NULL;
940         
941         if (!(flags & STR_ASCII) && 
942             ((flags & STR_UNICODE) || 
943              (session->transport->negotiate.capabilities & CAP_UNICODE))) {
944                 int align = 0;
945                 if ((str_offset&1) && !(flags & STR_NOALIGN)) {
946                         align = 1;
947                 }
948                 if (flags & STR_LEN_NOTERM) {
949                         extra = 2;
950                 }
951                 return align + extra + smbcli_blob_pull_ucs2(mem_ctx, blob, dest, 
952                                                           blob->data+str_offset+align, 
953                                                           -1, flags);
954         }
955
956         if (flags & STR_LEN_NOTERM) {
957                 extra = 1;
958         }
959
960         return extra + smbcli_blob_pull_ascii(mem_ctx, blob, dest,
961                                            blob->data+str_offset, -1, flags);
962 }
963
964
965 /*
966   append a string into a blob
967 */
968 size_t smbcli_blob_append_string(struct smbcli_session *session,
969                               TALLOC_CTX *mem_ctx, DATA_BLOB *blob, 
970                               const char *str, uint_t flags)
971 {
972         size_t max_len;
973         int len;
974
975         if (!str) return 0;
976
977         /* determine string type to use */
978         if (!(flags & (STR_ASCII|STR_UNICODE))) {
979                 flags |= (session->transport->negotiate.capabilities & CAP_UNICODE) ? STR_UNICODE : STR_ASCII;
980         }
981
982         max_len = (strlen(str)+2) * MAX_BYTES_PER_CHAR;         
983
984         blob->data = talloc_realloc(mem_ctx, blob->data, uint8_t, blob->length + max_len);
985         if (!blob->data) {
986                 return 0;
987         }
988
989         len = push_string(lp_iconv_convenience(global_loadparm), blob->data + blob->length, str, max_len, flags);
990
991         blob->length += len;
992
993         return len;
994 }
995
996 /*
997   pull a GUID structure from the wire. The buffer must be at least 16
998   bytes long
999  */
1000 enum ndr_err_code smbcli_pull_guid(void *base, uint16_t offset, 
1001                                    struct GUID *guid)
1002 {
1003         DATA_BLOB blob;
1004         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1005         enum ndr_err_code ndr_err;
1006
1007         ZERO_STRUCTP(guid);
1008
1009         blob.data       = offset + (uint8_t *)base;
1010         blob.length     = 16;
1011         ndr_err = ndr_pull_struct_blob(&blob, tmp_ctx, NULL, guid, 
1012                                        (ndr_pull_flags_fn_t)ndr_pull_GUID);
1013         talloc_free(tmp_ctx);
1014         return ndr_err;
1015 }
1016
1017 /*
1018   push a guid onto the wire. The buffer must hold 16 bytes
1019  */
1020 enum ndr_err_code smbcli_push_guid(void *base, uint16_t offset, 
1021                                    const struct GUID *guid)
1022 {
1023         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1024         enum ndr_err_code ndr_err;
1025         DATA_BLOB blob;
1026         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, NULL,
1027                                        guid, (ndr_push_flags_fn_t)ndr_push_GUID);
1028         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err) || blob.length != 16) {
1029                 talloc_free(tmp_ctx);
1030                 return ndr_err;
1031         }
1032         memcpy(offset + (uint8_t *)base, blob.data, blob.length);
1033         talloc_free(tmp_ctx);
1034         return ndr_err;
1035 }