do not clamp read/write request to 60KB
[libsmb2.git] / lib / smb2-cmd-write.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_write_request(struct smb2_context *smb2,
50                           struct smb2_pdu *pdu,
51                           struct smb2_write_request *req)
52 {
53         int len;
54         uint8_t *buf;
55         struct smb2_iovec *iov;
56
57         len = SMB2_WRITE_REQUEST_SIZE & 0xfffffffe;
58         buf = calloc(len, sizeof(uint8_t));
59         if (buf == NULL) {
60                 smb2_set_error(smb2, "Failed to allocate write 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         }
69         smb2_set_uint16(iov, 0, SMB2_WRITE_REQUEST_SIZE);
70         smb2_set_uint16(iov, 2, SMB2_HEADER_SIZE + 48);
71         smb2_set_uint32(iov, 4, req->length);
72         smb2_set_uint64(iov, 8, req->offset);
73         memcpy(iov->buf + 16, req->file_id, SMB2_FD_SIZE);
74         smb2_set_uint32(iov, 32, req->channel);
75         smb2_set_uint32(iov, 36, req->remaining_bytes);
76         smb2_set_uint16(iov, 42, req->write_channel_info_length);
77         smb2_set_uint32(iov, 44, req->flags);
78
79         if (req->write_channel_info_length > 0 ||
80             req->write_channel_info != NULL) {
81                 smb2_set_error(smb2, "ChannelInfo not yet implemented");
82                 return -1;
83         }
84
85         return 0;
86 }
87
88 struct smb2_pdu *
89 smb2_cmd_write_async(struct smb2_context *smb2,
90                      struct smb2_write_request *req,
91                      smb2_command_cb cb, void *cb_data)
92 {
93         struct smb2_pdu *pdu;
94
95         pdu = smb2_allocate_pdu(smb2, SMB2_WRITE, cb, cb_data);
96         if (pdu == NULL) {
97                 return NULL;
98         }
99
100         if (smb2_encode_write_request(smb2, pdu, req)) {
101                 smb2_free_pdu(smb2, pdu);
102                 return NULL;
103         }
104
105         smb2_add_iovector(smb2, &pdu->out, req->buf,
106                           req->length, NULL);
107         
108         if (smb2_pad_to_64bit(smb2, &pdu->out) != 0) {
109                 smb2_free_pdu(smb2, pdu);
110                 return NULL;
111         }
112
113         /* Adjust credit charge for large payloads */
114         if (smb2->supports_multi_credit) {
115                 pdu->header.credit_charge = (req->length - 1) / 65536 + 1; // 3.1.5.2 of [MS-SMB2]
116         }
117
118         return pdu;
119 }
120
121 int
122 smb2_process_write_fixed(struct smb2_context *smb2,
123                          struct smb2_pdu *pdu)
124 {
125         struct smb2_write_reply *rep;
126         struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1];
127         uint16_t struct_size;
128
129         rep = malloc(sizeof(*rep));
130         if (rep == NULL) {
131                 smb2_set_error(smb2, "Failed to allocate write reply");
132                 return -1;
133         }
134         pdu->payload = rep;
135
136         smb2_get_uint16(iov, 0, &struct_size);
137         if (struct_size != SMB2_WRITE_REPLY_SIZE ||
138             (struct_size & 0xfffe) != iov->len) {
139                 smb2_set_error(smb2, "Unexpected size of Write "
140                                "reply. Expected %d, got %d",
141                                SMB2_WRITE_REPLY_SIZE,
142                                (int)iov->len);
143                 return -1;
144         }
145
146         smb2_get_uint32(iov, 4, &rep->count);
147         smb2_get_uint32(iov, 8, &rep->remaining);
148
149         return 0;
150 }