summaryrefslogtreecommitdiff
path: root/libcli/named_pipe_auth/tstream_u32_read.c
blob: c8e95ef6b9986b72bd1e1a8659fdf5db32533fd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Unix SMB/CIFS implementation.
 *
 * Copyright (C) Volker Lendecke 2019
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "replace.h"
#include "system/filesys.h"
#include "tstream_u32_read.h"
#include "lib/util/byteorder.h"
#include "lib/util/tevent_unix.h"

struct tstream_u32_read_state {
	size_t max_msglen;
	size_t buflen;
	uint8_t *buf;
};

static int tstream_u32_read_next_vector(struct tstream_context *stream,
					void *private_data,
					TALLOC_CTX *mem_ctx,
					struct iovec **_vector,
					size_t *_count);
static void tstream_u32_read_done(struct tevent_req *subreq);

struct tevent_req *tstream_u32_read_send(
	TALLOC_CTX *mem_ctx,
	struct tevent_context *ev,
	uint32_t max_msglen,
	struct tstream_context *stream)
{
	struct tevent_req *req = NULL, *subreq = NULL;
	struct tstream_u32_read_state *state = NULL;

	req = tevent_req_create(
		mem_ctx, &state, struct tstream_u32_read_state);
	if (req == NULL) {
		return NULL;
	}
	state->max_msglen = max_msglen;

	subreq = tstream_readv_pdu_send(
		state,
		ev,
		stream,
		tstream_u32_read_next_vector,
		state);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, tstream_u32_read_done, req);
	return req;
}

static int tstream_u32_read_next_vector(struct tstream_context *stream,
					void *private_data,
					TALLOC_CTX *mem_ctx,
					struct iovec **_vector,
					size_t *_count)
{
	struct tstream_u32_read_state *state = talloc_get_type_abort(
		private_data, struct tstream_u32_read_state);
	size_t buflen = talloc_get_size(state->buf);
	struct iovec *vector;
	uint32_t msg_len;
	size_t ofs = 0;
	size_t count;

	if (buflen == 0) {
		msg_len = 4;
		state->buf = talloc_array(state, uint8_t, msg_len);
		if (state->buf == NULL) {
			return -1;
		}
	} else if (buflen == 4) {

		ofs = 4;

		msg_len = RIVAL(state->buf, 0);
		if ((msg_len == 0) || (msg_len > state->max_msglen)) {
			errno = EMSGSIZE;
			return -1;
		}
		msg_len += ofs;
		if (msg_len < ofs) {
			errno = EMSGSIZE;
			return -1;
		}

		state->buf = talloc_realloc(
			state, state->buf, uint8_t, msg_len);
		if (state->buf == NULL) {
			return -1;
		}
	} else {
		*_vector = NULL;
		*_count = 0;
		return 0;
	}

	vector = talloc(mem_ctx, struct iovec);
	if (vector == NULL) {
		return -1;
	}
	*vector = (struct iovec) {
		.iov_base = state->buf + ofs, .iov_len = msg_len - ofs,
	};
	count = 1;

	*_vector = vector;
	*_count = count;
	return 0;
}

static void tstream_u32_read_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	int ret, err;

	ret = tstream_readv_pdu_recv(subreq, &err);
	TALLOC_FREE(subreq);
	if (ret == -1) {
		tevent_req_error(req, err);
		return;
	}
	tevent_req_done(req);
}

int tstream_u32_read_recv(
	struct tevent_req *req,
	TALLOC_CTX *mem_ctx,
	uint8_t **buf,
	size_t *buflen)
{
	struct tstream_u32_read_state *state = tevent_req_data(
		req, struct tstream_u32_read_state);
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		return err;
	}
	*buflen = talloc_get_size(state->buf);
	*buf = talloc_move(mem_ctx, &state->buf);
	return 0;
}