do not clamp read/write request to 60KB
[libsmb2.git] / lib / smb2-cmd-read.c
1 /* -*-  mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil;  -*- */
2 /*
3    Copyright (C) 2016 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU Lesser General Public License as published by
7    the Free Software Foundation; either version 2.1 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25
26 #ifdef HAVE_STDINT_H
27 #include <stdint.h>
28 #endif
29
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37
38 #ifdef STDC_HEADERS
39 #include <stddef.h>
40 #endif
41
42 #include <errno.h>
43
44 #include "smb2.h"
45 #include "libsmb2.h"
46 #include "libsmb2-private.h"
47
48 static int
49 smb2_encode_read_request(struct smb2_context *smb2,
50                          struct smb2_pdu *pdu,
51                          struct smb2_read_request *req)
52 {
53         int len;
54         uint8_t *buf;
55         struct smb2_iovec *iov;
56
57         len = SMB2_READ_REQUEST_SIZE & 0xfffffffe;
58         buf = calloc(len, sizeof(uint8_t));
59         if (buf == NULL) {
60                 smb2_set_error(smb2, "Failed to allocate read buffer");
61                 return -1;
62         }
63         
64         iov = smb2_add_iovector(smb2, &pdu->out, buf, len, free);
65
66         if (!smb2->supports_multi_credit && req->length > 64 * 1024) {
67                 req->length = 64 * 1024;
68                 req->minimum_count = 0;
69         }
70         smb2_set_uint16(iov, 0, SMB2_READ_REQUEST_SIZE);
71         smb2_set_uint8(iov, 3, req->flags);
72         smb2_set_uint32(iov, 4, req->length);
73         smb2_set_uint64(iov, 8, req->offset);
74         memcpy(iov->buf + 16, req->file_id, SMB2_FD_SIZE);
75         smb2_set_uint32(iov, 32, req->minimum_count);
76         smb2_set_uint32(iov, 36, req->channel);
77         smb2_set_uint32(iov, 40, req->remaining_bytes);
78         smb2_set_uint16(iov, 46, req->read_channel_info_length);
79
80         if (req->read_channel_info_length > 0 ||
81             req->read_channel_info != NULL) {
82                 smb2_set_error(smb2, "ChannelInfo not yet implemented");
83                 return -1;
84         }
85
86         /* The buffer must contain at least one byte, even if we do not
87          * have any read channel info.
88          */
89         if (req->read_channel_info == NULL) {
90                 static uint8_t zero;
91
92                 smb2_add_iovector(smb2, &pdu->out, &zero, 1, NULL);
93         }
94
95         return 0;
96 }
97
98 struct smb2_pdu *
99 smb2_cmd_read_async(struct smb2_context *smb2,
100                     struct smb2_read_request *req,
101                     smb2_command_cb cb, void *cb_data)
102 {
103         struct smb2_pdu *pdu;
104
105         pdu = smb2_allocate_pdu(smb2, SMB2_READ, cb, cb_data);
106         if (pdu == NULL) {
107                 return NULL;
108         }
109
110         if (smb2_encode_read_request(smb2, pdu, req)) {
111                 smb2_free_pdu(smb2, pdu);
112                 return NULL;
113         }
114
115         /* Add a vector for the buffer that the application gave us */
116         smb2_add_iovector(smb2, &pdu->in, req->buf,
117                           req->length, NULL);
118
119         if (smb2_pad_to_64bit(smb2, &pdu->out) != 0) {
120                 smb2_free_pdu(smb2, pdu);
121                 return NULL;
122         }
123
124         /* Adjust credit charge for large payloads */
125         if (smb2->supports_multi_credit) {
126                 pdu->header.credit_charge = (req->length - 1) / 65536 + 1; // 3.1.5.2 of [MS-SMB2]
127         }
128
129         return pdu;
130 }
131
132 int
133 smb2_process_read_fixed(struct smb2_context *smb2,
134                         struct smb2_pdu *pdu)
135 {
136         struct smb2_read_reply *rep;
137         struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1];
138         uint16_t struct_size;
139
140         rep = malloc(sizeof(*rep));
141         if (rep == NULL) {
142                 smb2_set_error(smb2, "Failed to allocate read reply");
143                 return -1;
144         }
145         pdu->payload = rep;
146
147         smb2_get_uint16(iov, 0, &struct_size);
148         if (struct_size > SMB2_READ_REPLY_SIZE) {
149                 smb2_set_error(smb2, "Unexpected size of Read "
150                                "reply. Expected %d, got %d",
151                                SMB2_READ_REPLY_SIZE,
152                                (int)iov->len);
153                 return -1;
154         }
155
156         smb2_get_uint8(iov, 2, &rep->data_offset);
157         smb2_get_uint32(iov, 4, &rep->data_length);
158         smb2_get_uint32(iov, 8, &rep->data_remaining);
159
160         if (rep->data_length == 0) {
161                 return 0;
162         }
163
164         if (rep->data_offset != SMB2_HEADER_SIZE + 16) {
165                 smb2_set_error(smb2, "Unexpected data offset in Read reply. "
166                                "Expected %d, got %d",
167                                16, rep->data_offset);
168                 return -1;
169         }
170
171         return rep->data_length;
172 }