2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 this file implements functions for manipulating the 'struct smbsrv_request' structure in smbd
26 #include "smb_server/smb_server.h"
27 #include "smbd/service_stream.h"
28 #include "lib/stream/packet.h"
29 #include "ntvfs/ntvfs.h"
32 /* we over allocate the data buffer to prevent too many realloc calls */
33 #define REQ_OVER_ALLOCATION 0
35 /****************************************************************************
36 construct a basic request packet, mostly used to construct async packets
37 such as change notify and oplock break requests
38 ****************************************************************************/
39 struct smbsrv_request *smbsrv_init_request(struct smbsrv_connection *smb_conn)
41 struct smbsrv_request *req;
43 req = talloc(smb_conn, struct smbsrv_request);
50 /* setup the request context */
51 req->smb_conn = smb_conn;
53 req->async_states = talloc(req, struct ntvfs_async_state);
54 if (!req->async_states) {
58 req->async_states->state = 0;
65 setup a chained reply in req->out with the given word count and initial data buffer size.
67 static void req_setup_chain_reply(struct smbsrv_request *req, uint_t wct, uint_t buflen)
69 uint32_t chain_base_size = req->out.size;
71 /* we need room for the wct value, the words, the buffer length and the buffer */
72 req->out.size += 1 + VWV(wct) + 2 + buflen;
74 /* over allocate by a small amount */
75 req->out.allocated = req->out.size + REQ_OVER_ALLOCATION;
77 req->out.buffer = talloc_realloc(req, req->out.buffer,
78 uint8_t, req->out.allocated);
79 if (!req->out.buffer) {
80 smbsrv_terminate_connection(req->smb_conn, "allocation failed");
84 req->out.hdr = req->out.buffer + NBT_HDR_SIZE;
85 req->out.vwv = req->out.buffer + chain_base_size + 1;
87 req->out.data = req->out.vwv + VWV(wct) + 2;
88 req->out.data_size = buflen;
89 req->out.ptr = req->out.data;
91 SCVAL(req->out.buffer, chain_base_size, wct);
92 SSVAL(req->out.vwv, VWV(wct), buflen);
97 setup a reply in req->out with the given word count and initial data buffer size.
98 the caller will then fill in the command words and data before calling req_send_reply() to
99 send the reply on its way
101 void smbsrv_setup_reply(struct smbsrv_request *req, uint_t wct, uint_t buflen)
105 if (req->chain_count != 0) {
106 req_setup_chain_reply(req, wct, buflen);
110 req->out.size = NBT_HDR_SIZE + MIN_SMB_SIZE + wct*2 + buflen;
112 /* over allocate by a small amount */
113 req->out.allocated = req->out.size + REQ_OVER_ALLOCATION;
115 req->out.buffer = talloc_size(req, req->out.allocated);
116 if (!req->out.buffer) {
117 smbsrv_terminate_connection(req->smb_conn, "allocation failed");
121 flags2 = FLAGS2_LONG_PATH_COMPONENTS |
122 FLAGS2_EXTENDED_ATTRIBUTES |
124 flags2 |= (req->flags2 & (FLAGS2_UNICODE_STRINGS|FLAGS2_EXTENDED_SECURITY));
125 if (req->smb_conn->negotiate.client_caps & CAP_STATUS32) {
126 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
129 req->out.hdr = req->out.buffer + NBT_HDR_SIZE;
130 req->out.vwv = req->out.hdr + HDR_VWV;
132 req->out.data = req->out.vwv + VWV(wct) + 2;
133 req->out.data_size = buflen;
134 req->out.ptr = req->out.data;
136 SIVAL(req->out.hdr, HDR_RCLS, 0);
138 SCVAL(req->out.hdr, HDR_WCT, wct);
139 SSVAL(req->out.vwv, VWV(wct), buflen);
141 memcpy(req->out.hdr, "\377SMB", 4);
142 SCVAL(req->out.hdr,HDR_FLG, FLAG_REPLY | FLAG_CASELESS_PATHNAMES);
143 SSVAL(req->out.hdr,HDR_FLG2, flags2);
144 SSVAL(req->out.hdr,HDR_PIDHIGH,0);
145 memset(req->out.hdr + HDR_SS_FIELD, 0, 10);
148 /* copy the cmd, tid, pid, uid and mid from the request */
149 SCVAL(req->out.hdr,HDR_COM,CVAL(req->in.hdr,HDR_COM));
150 SSVAL(req->out.hdr,HDR_TID,SVAL(req->in.hdr,HDR_TID));
151 SSVAL(req->out.hdr,HDR_PID,SVAL(req->in.hdr,HDR_PID));
152 SSVAL(req->out.hdr,HDR_UID,SVAL(req->in.hdr,HDR_UID));
153 SSVAL(req->out.hdr,HDR_MID,SVAL(req->in.hdr,HDR_MID));
155 SCVAL(req->out.hdr,HDR_COM,0);
156 SSVAL(req->out.hdr,HDR_TID,0);
157 SSVAL(req->out.hdr,HDR_PID,0);
158 SSVAL(req->out.hdr,HDR_UID,0);
159 SSVAL(req->out.hdr,HDR_MID,0);
165 setup a copy of a request, used when the server needs to send
166 more than one reply for a single request packet
168 struct smbsrv_request *smbsrv_setup_secondary_request(struct smbsrv_request *old_req)
170 struct smbsrv_request *req;
173 req = talloc_memdup(old_req, old_req, sizeof(struct smbsrv_request));
178 req->out.buffer = talloc_memdup(req, req->out.buffer, req->out.allocated);
179 if (req->out.buffer == NULL) {
184 diff = req->out.buffer - old_req->out.buffer;
186 req->out.hdr += diff;
187 req->out.vwv += diff;
188 req->out.data += diff;
189 req->out.ptr += diff;
195 work out the maximum data size we will allow for this reply, given
196 the negotiated max_xmit. The basic reply packet must be setup before
199 note that this is deliberately a signed integer reply
201 int req_max_data(struct smbsrv_request *req)
204 ret = req->smb_conn->negotiate.max_send;
205 ret -= PTR_DIFF(req->out.data, req->out.hdr);
206 if (ret < 0) ret = 0;
212 grow the allocation of the data buffer portion of a reply
213 packet. Note that as this can reallocate the packet buffer this
214 invalidates any local pointers into the packet.
216 To cope with this req->out.ptr is supplied. This will be updated to
217 point at the same offset into the packet as before this call
219 static void req_grow_allocation(struct smbsrv_request *req, uint_t new_size)
224 delta = new_size - req->out.data_size;
225 if (delta + req->out.size <= req->out.allocated) {
226 /* it fits in the preallocation */
230 /* we need to realloc */
231 req->out.allocated = req->out.size + delta + REQ_OVER_ALLOCATION;
232 buf2 = talloc_realloc(req, req->out.buffer, uint8_t, req->out.allocated);
234 smb_panic("out of memory in req_grow_allocation");
237 if (buf2 == req->out.buffer) {
238 /* the malloc library gave us the same pointer */
242 /* update the pointers into the packet */
243 req->out.data = buf2 + PTR_DIFF(req->out.data, req->out.buffer);
244 req->out.ptr = buf2 + PTR_DIFF(req->out.ptr, req->out.buffer);
245 req->out.vwv = buf2 + PTR_DIFF(req->out.vwv, req->out.buffer);
246 req->out.hdr = buf2 + PTR_DIFF(req->out.hdr, req->out.buffer);
248 req->out.buffer = buf2;
253 grow the data buffer portion of a reply packet. Note that as this
254 can reallocate the packet buffer this invalidates any local pointers
257 To cope with this req->out.ptr is supplied. This will be updated to
258 point at the same offset into the packet as before this call
260 void req_grow_data(struct smbsrv_request *req, uint_t new_size)
264 if (!(req->control_flags & REQ_CONTROL_LARGE) && new_size > req_max_data(req)) {
265 smb_panic("reply buffer too large!");
268 req_grow_allocation(req, new_size);
270 delta = new_size - req->out.data_size;
272 req->out.size += delta;
273 req->out.data_size += delta;
275 /* set the BCC to the new data size */
276 SSVAL(req->out.vwv, VWV(req->out.wct), new_size);
280 send a reply and destroy the request buffer
282 note that this only looks at req->out.buffer and req->out.size, allowing manually
283 constructed packets to be sent
285 void smbsrv_send_reply_nosign(struct smbsrv_request *req)
290 if (req->out.size > NBT_HDR_SIZE) {
291 _smb_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
294 blob = data_blob_const(req->out.buffer, req->out.size);
295 status = packet_send(req->smb_conn->packet, blob);
296 if (!NT_STATUS_IS_OK(status)) {
297 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
303 possibly sign a message then send a reply and destroy the request buffer
305 note that this only looks at req->out.buffer and req->out.size, allowing manually
306 constructed packets to be sent
308 void smbsrv_send_reply(struct smbsrv_request *req)
310 smbsrv_sign_packet(req);
312 smbsrv_send_reply_nosign(req);
318 construct and send an error packet with a forced DOS error code
319 this is needed to match win2000 behaviour for some parts of the protocol
321 void smbsrv_send_dos_error(struct smbsrv_request *req, uint8_t eclass, uint16_t ecode)
323 /* if the basic packet hasn't been setup yet then do it now */
324 if (req->out.buffer == NULL) {
325 smbsrv_setup_reply(req, 0, 0);
328 SCVAL(req->out.hdr, HDR_RCLS, eclass);
329 SSVAL(req->out.hdr, HDR_ERR, ecode);
330 SSVAL(req->out.hdr, HDR_FLG2, SVAL(req->out.hdr, HDR_FLG2) & ~FLAGS2_32_BIT_ERROR_CODES);
331 smbsrv_send_reply(req);
335 setup the header of a reply to include an NTSTATUS code
337 void smbsrv_setup_error(struct smbsrv_request *req, NTSTATUS status)
339 if (!req->smb_conn->config.nt_status_support || !(req->smb_conn->negotiate.client_caps & CAP_STATUS32)) {
340 /* convert to DOS error codes */
343 ntstatus_to_dos(status, &eclass, &ecode);
344 SCVAL(req->out.hdr, HDR_RCLS, eclass);
345 SSVAL(req->out.hdr, HDR_ERR, ecode);
346 SSVAL(req->out.hdr, HDR_FLG2, SVAL(req->out.hdr, HDR_FLG2) & ~FLAGS2_32_BIT_ERROR_CODES);
350 if (NT_STATUS_IS_DOS(status)) {
351 /* its a encoded DOS error, using the reserved range */
352 SSVAL(req->out.hdr, HDR_RCLS, NT_STATUS_DOS_CLASS(status));
353 SSVAL(req->out.hdr, HDR_ERR, NT_STATUS_DOS_CODE(status));
354 SSVAL(req->out.hdr, HDR_FLG2, SVAL(req->out.hdr, HDR_FLG2) & ~FLAGS2_32_BIT_ERROR_CODES);
356 SIVAL(req->out.hdr, HDR_RCLS, NT_STATUS_V(status));
357 SSVAL(req->out.hdr, HDR_FLG2, SVAL(req->out.hdr, HDR_FLG2) | FLAGS2_32_BIT_ERROR_CODES);
362 construct and send an error packet, then destroy the request
363 auto-converts to DOS error format when appropriate
365 void smbsrv_send_error(struct smbsrv_request *req, NTSTATUS status)
367 if (req->smb_conn->connection->event.fde == NULL) {
368 /* the socket has been destroyed - no point trying to send an error! */
372 smbsrv_setup_reply(req, 0, 0);
374 /* error returns never have any data */
375 req_grow_data(req, 0);
377 smbsrv_setup_error(req, status);
378 smbsrv_send_reply(req);
383 push a string into the data portion of the request packet, growing it if necessary
384 this gets quite tricky - please be very careful to cover all cases when modifying this
386 if dest is NULL, then put the string at the end of the data portion of the packet
388 if dest_len is -1 then no limit applies
390 size_t req_push_str(struct smbsrv_request *req, uint8_t *dest, const char *str, int dest_len, uint_t flags)
395 const int max_bytes_per_char = 3;
397 if (!(flags & (STR_ASCII|STR_UNICODE))) {
398 flags |= (req->flags2 & FLAGS2_UNICODE_STRINGS) ? STR_UNICODE : STR_ASCII;
402 dest = req->out.data + req->out.data_size;
405 if (dest_len != -1) {
408 len = (strlen(str)+2) * max_bytes_per_char;
411 grow_size = len + PTR_DIFF(dest, req->out.data);
412 buf0 = req->out.buffer;
414 req_grow_allocation(req, grow_size);
416 if (buf0 != req->out.buffer) {
417 dest = req->out.buffer + PTR_DIFF(dest, buf0);
420 len = push_string(dest, str, len, flags);
422 grow_size = len + PTR_DIFF(dest, req->out.data);
424 if (grow_size > req->out.data_size) {
425 req_grow_data(req, grow_size);
432 append raw bytes into the data portion of the request packet
433 return the number of bytes added
435 size_t req_append_bytes(struct smbsrv_request *req,
436 const uint8_t *bytes, size_t byte_len)
438 req_grow_allocation(req, byte_len + req->out.data_size);
439 memcpy(req->out.data + req->out.data_size, bytes, byte_len);
440 req_grow_data(req, byte_len + req->out.data_size);
444 append variable block (type 5 buffer) into the data portion of the request packet
445 return the number of bytes added
447 size_t req_append_var_block(struct smbsrv_request *req,
448 const uint8_t *bytes, uint16_t byte_len)
450 req_grow_allocation(req, byte_len + 3 + req->out.data_size);
451 SCVAL(req->out.data + req->out.data_size, 0, 5);
452 SSVAL(req->out.data + req->out.data_size, 1, byte_len); /* add field length */
454 memcpy(req->out.data + req->out.data_size + 3, bytes, byte_len);
456 req_grow_data(req, byte_len + 3 + req->out.data_size);
460 pull a UCS2 string from a request packet, returning a talloced unix string
462 the string length is limited by the 3 things:
463 - the data size in the request (end of packet)
464 - the passed 'byte_len' if it is not -1
465 - the end of string (null termination)
467 Note that 'byte_len' is the number of bytes in the packet
469 on failure zero is returned and *dest is set to NULL, otherwise the number
470 of bytes consumed in the packet is returned
472 static size_t req_pull_ucs2(struct smbsrv_request *req, const char **dest, const uint8_t *src, int byte_len, uint_t flags)
474 int src_len, src_len2, alignment=0;
478 if (!(flags & STR_NOALIGN) && ucs2_align(req->in.buffer, src, flags)) {
481 if (byte_len != -1) {
486 if (flags & STR_NO_RANGE_CHECK) {
489 src_len = req->in.data_size - PTR_DIFF(src, req->in.data);
490 if (byte_len != -1 && src_len > byte_len) {
500 src_len2 = utf16_len_n(src, src_len);
502 *dest = talloc_strdup(req, "");
503 return src_len2 + alignment;
506 ret = convert_string_talloc(req, CH_UTF16, CH_UNIX, src, src_len2, (void **)&dest2);
514 return src_len2 + alignment;
518 pull a ascii string from a request packet, returning a talloced string
520 the string length is limited by the 3 things:
521 - the data size in the request (end of packet)
522 - the passed 'byte_len' if it is not -1
523 - the end of string (null termination)
525 Note that 'byte_len' is the number of bytes in the packet
527 on failure zero is returned and *dest is set to NULL, otherwise the number
528 of bytes consumed in the packet is returned
530 static size_t req_pull_ascii(struct smbsrv_request *req, const char **dest, const uint8_t *src, int byte_len, uint_t flags)
532 int src_len, src_len2;
536 if (flags & STR_NO_RANGE_CHECK) {
539 src_len = req->in.data_size - PTR_DIFF(src, req->in.data);
544 if (byte_len != -1 && src_len > byte_len) {
549 src_len2 = strnlen((const char *)src, src_len);
550 if (src_len2 <= src_len - 1) {
551 /* include the termination if we didn't reach the end of the packet */
555 ret = convert_string_talloc(req, CH_DOS, CH_UNIX, src, src_len2, (void **)&dest2);
567 pull a string from a request packet, returning a talloced string
569 the string length is limited by the 3 things:
570 - the data size in the request (end of packet)
571 - the passed 'byte_len' if it is not -1
572 - the end of string (null termination)
574 Note that 'byte_len' is the number of bytes in the packet
576 on failure zero is returned and *dest is set to NULL, otherwise the number
577 of bytes consumed in the packet is returned
579 size_t req_pull_string(struct smbsrv_request *req, const char **dest, const uint8_t *src, int byte_len, uint_t flags)
581 if (!(flags & STR_ASCII) &&
582 (((flags & STR_UNICODE) || (req->flags2 & FLAGS2_UNICODE_STRINGS)))) {
583 return req_pull_ucs2(req, dest, src, byte_len, flags);
586 return req_pull_ascii(req, dest, src, byte_len, flags);
591 pull a ASCII4 string buffer from a request packet, returning a talloced string
593 an ASCII4 buffer is a null terminated string that has a prefix
594 of the character 0x4. It tends to be used in older parts of the protocol.
596 on failure *dest is set to the zero length string. This seems to
597 match win2000 behaviour
599 size_t req_pull_ascii4(struct smbsrv_request *req, const char **dest, const uint8_t *src, uint_t flags)
603 if (PTR_DIFF(src, req->in.data) + 1 > req->in.data_size) {
604 /* win2000 treats this as the empty string! */
605 (*dest) = talloc_strdup(req, "");
609 /* this consumes the 0x4 byte. We don't check whether the byte
610 is actually 0x4 or not. This matches win2000 server
614 ret = req_pull_string(req, dest, src, -1, flags);
616 (*dest) = talloc_strdup(req, "");
624 pull a DATA_BLOB from a request packet, returning a talloced blob
626 return False if any part is outside the data portion of the packet
628 BOOL req_pull_blob(struct smbsrv_request *req, const uint8_t *src, int len, DATA_BLOB *blob)
630 if (len != 0 && req_data_oob(req, src, len)) {
634 (*blob) = data_blob_talloc(req, src, len);
639 /* check that a lump of data in a request is within the bounds of the data section of
641 BOOL req_data_oob(struct smbsrv_request *req, const uint8_t *ptr, uint32_t count)
647 /* be careful with wraparound! */
648 if (ptr < req->in.data ||
649 ptr >= req->in.data + req->in.data_size ||
650 count > req->in.data_size ||
651 ptr + count > req->in.data + req->in.data_size) {
659 pull an open file handle from a packet, taking account of the chained_fnum
661 uint16_t req_fnum(struct smbsrv_request *req, const uint8_t *base, uint_t offset)
663 if (req->chained_fnum != -1) {
664 return req->chained_fnum;
666 return SVAL(base, offset);