Try and fix bug #8472 - Crash in asn.1 parsing code.
[ira/wip.git] / source3 / libsmb / read_smb.c
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async SMB client requests
4    Copyright (C) Volker Lendecke 2008
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 "lib/async_req/async_sock.h"
22 #include "read_smb.h"
23 #include "lib/util/tevent_unix.h"
24
25 /*
26  * Read an smb packet asynchronously, discard keepalives
27  */
28
29 struct read_smb_state {
30         struct tevent_context *ev;
31         int fd;
32         uint8_t *buf;
33 };
34
35 static ssize_t read_smb_more(uint8_t *buf, size_t buflen, void *private_data);
36 static void read_smb_done(struct tevent_req *subreq);
37
38 struct tevent_req *read_smb_send(TALLOC_CTX *mem_ctx,
39                                  struct tevent_context *ev,
40                                  int fd)
41 {
42         struct tevent_req *result, *subreq;
43         struct read_smb_state *state;
44
45         result = tevent_req_create(mem_ctx, &state, struct read_smb_state);
46         if (result == NULL) {
47                 return NULL;
48         }
49         state->ev = ev;
50         state->fd = fd;
51
52         subreq = read_packet_send(state, ev, fd, 4, read_smb_more, NULL);
53         if (subreq == NULL) {
54                 goto fail;
55         }
56         tevent_req_set_callback(subreq, read_smb_done, result);
57         return result;
58  fail:
59         TALLOC_FREE(result);
60         return NULL;
61 }
62
63 static ssize_t read_smb_more(uint8_t *buf, size_t buflen, void *private_data)
64 {
65         if (buflen > 4) {
66                 return 0;       /* We've been here, we're done */
67         }
68         return smb_len_large(buf);
69 }
70
71 static void read_smb_done(struct tevent_req *subreq)
72 {
73         struct tevent_req *req = tevent_req_callback_data(
74                 subreq, struct tevent_req);
75         struct read_smb_state *state = tevent_req_data(
76                 req, struct read_smb_state);
77         ssize_t len;
78         int err;
79
80         len = read_packet_recv(subreq, state, &state->buf, &err);
81         TALLOC_FREE(subreq);
82         if (len == -1) {
83                 tevent_req_error(req, err);
84                 return;
85         }
86
87         if (CVAL(state->buf, 0) == NBSSkeepalive) {
88                 subreq = read_packet_send(state, state->ev, state->fd, 4,
89                                           read_smb_more, NULL);
90                 if (tevent_req_nomem(subreq, req)) {
91                         return;
92                 }
93                 tevent_req_set_callback(subreq, read_smb_done, req);
94                 return;
95         }
96         tevent_req_done(req);
97 }
98
99 ssize_t read_smb_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
100                       uint8_t **pbuf, int *perrno)
101 {
102         struct read_smb_state *state = tevent_req_data(
103                 req, struct read_smb_state);
104
105         if (tevent_req_is_unix_error(req, perrno)) {
106                 return -1;
107         }
108         *pbuf = talloc_move(mem_ctx, &state->buf);
109         return talloc_get_size(*pbuf);
110 }
111
112 ssize_t read_smb(int fd, TALLOC_CTX *mem_ctx, uint8_t **pbuf, int *perrno)
113 {
114         TALLOC_CTX *frame = talloc_stackframe();
115         struct event_context *ev;
116         struct tevent_req *req;
117         ssize_t ret = -1;
118
119         ev = event_context_init(frame);
120         if (ev == NULL) {
121                 goto fail;
122         }
123         req = read_smb_send(frame, ev, fd);
124         if (req == NULL) {
125                 goto fail;
126         }
127         if (!tevent_req_poll(req, ev)) {
128                 goto fail;
129         }
130         ret = read_smb_recv(req, mem_ctx, pbuf, perrno);
131  fail:
132         TALLOC_FREE(frame);
133         return ret;
134 }