Try and fix bug #8472 - Crash in asn.1 parsing code.
[ira/wip.git] / source3 / libsmb / smb2cli_write.c
1 /*
2    Unix SMB/CIFS implementation.
3    smb2 lib
4    Copyright (C) Volker Lendecke 2011
5
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 3 of the License, or
9    (at your option) any later version.
10
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.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "client.h"
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
27
28 struct smb2cli_write_state {
29         uint8_t fixed[48];
30         uint8_t dyn_pad[1];
31 };
32
33 static void smb2cli_write_done(struct tevent_req *subreq);
34
35 struct tevent_req *smb2cli_write_send(TALLOC_CTX *mem_ctx,
36                                       struct tevent_context *ev,
37                                       struct cli_state *cli,
38                                       uint32_t length,
39                                       uint64_t offset,
40                                       uint64_t fid_persistent,
41                                       uint64_t fid_volatile,
42                                       uint32_t remaining_bytes,
43                                       uint32_t flags,
44                                       const uint8_t *data)
45 {
46         struct tevent_req *req, *subreq;
47         struct smb2cli_write_state *state;
48         uint8_t *fixed;
49         const uint8_t *dyn;
50         size_t dyn_len;
51
52         req = tevent_req_create(mem_ctx, &state,
53                                 struct smb2cli_write_state);
54         if (req == NULL) {
55                 return NULL;
56         }
57
58         fixed = state->fixed;
59
60         SSVAL(fixed, 0, 49);
61         SSVAL(fixed, 2, SMB2_HDR_BODY + 48);
62         SIVAL(fixed, 4, length);
63         SBVAL(fixed, 8, offset);
64         SBVAL(fixed, 16, fid_persistent);
65         SBVAL(fixed, 24, fid_volatile);
66         SIVAL(fixed, 36, remaining_bytes);
67         SIVAL(fixed, 44, flags);
68
69         if (length > 0) {
70                 dyn = data;
71                 dyn_len = length;
72         } else {
73                 dyn = state->dyn_pad;;
74                 dyn_len = sizeof(state->dyn_pad);
75         }
76
77         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_WRITE,
78                                   0, 0, /* flags */
79                                   cli->timeout,
80                                   cli->smb2.pid,
81                                   cli->smb2.tid,
82                                   cli->smb2.uid,
83                                   state->fixed, sizeof(state->fixed),
84                                   dyn, dyn_len);
85         if (tevent_req_nomem(subreq, req)) {
86                 return tevent_req_post(req, ev);
87         }
88         tevent_req_set_callback(subreq, smb2cli_write_done, req);
89         return req;
90 }
91
92 static void smb2cli_write_done(struct tevent_req *subreq)
93 {
94         struct tevent_req *req =
95                 tevent_req_callback_data(subreq,
96                 struct tevent_req);
97         NTSTATUS status;
98         struct iovec *iov;
99         static const struct smb2cli_req_expected_response expected[] = {
100         {
101                 .status = NT_STATUS_OK,
102                 .body_size = 0x11
103         }
104         };
105
106         status = smb2cli_req_recv(subreq, talloc_tos(), &iov,
107                                   expected, ARRAY_SIZE(expected));
108         if (tevent_req_nterror(req, status)) {
109                 return;
110         }
111         tevent_req_done(req);
112 }
113
114 NTSTATUS smb2cli_write_recv(struct tevent_req *req)
115 {
116         return tevent_req_simple_recv_ntstatus(req);
117 }
118
119 NTSTATUS smb2cli_write(struct cli_state *cli,
120                        uint32_t length,
121                        uint64_t offset,
122                        uint64_t fid_persistent,
123                        uint64_t fid_volatile,
124                        uint32_t remaining_bytes,
125                        uint32_t flags,
126                        const uint8_t *data)
127 {
128         TALLOC_CTX *frame = talloc_stackframe();
129         struct event_context *ev;
130         struct tevent_req *req;
131         NTSTATUS status = NT_STATUS_NO_MEMORY;
132
133         if (cli_has_async_calls(cli)) {
134                 /*
135                  * Can't use sync call while an async call is in flight
136                  */
137                 status = NT_STATUS_INVALID_PARAMETER;
138                 goto fail;
139         }
140         ev = event_context_init(frame);
141         if (ev == NULL) {
142                 goto fail;
143         }
144         req = smb2cli_write_send(frame, ev, cli, length, offset,
145                                  fid_persistent, fid_volatile,
146                                  remaining_bytes, flags, data);
147         if (req == NULL) {
148                 goto fail;
149         }
150         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
151                 goto fail;
152         }
153         status = smb2cli_write_recv(req);
154  fail:
155         TALLOC_FREE(frame);
156         return status;
157 }