summaryrefslogtreecommitdiff
path: root/source3/libads/sasl_wrapping.c
blob: 1dbd357a8de5a05e36d92fa595ff5d740c5a655f (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* 
   Unix SMB/CIFS implementation.
   ads sasl wrapping code
   Copyright (C) Stefan Metzmacher 2007
   
   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 "includes.h"
#include "ads.h"

void ndr_print_ads_saslwrap_struct(struct ndr_print *ndr, const char *name, const struct ads_saslwrap *r)
{
	ndr_print_struct(ndr, name, "saslwrap");
	ndr->depth++;
	ndr_print_uint16(ndr, "wrap_type", r->wrap_type);
#ifdef HAVE_LDAP_SASL_WRAPPING
	ndr_print_ptr(ndr, "sbiod", r->sbiod);
#endif /* HAVE_LDAP_SASL_WRAPPING */
	ndr_print_ptr(ndr, "mem_ctx", r->mem_ctx);
	ndr_print_ptr(ndr, "wrap_ops", r->wrap_ops);
	ndr_print_ptr(ndr, "wrap_private_data", r->wrap_private_data);
	ndr_print_struct(ndr, name, "in");
	ndr->depth++;
	ndr_print_uint32(ndr, "ofs", r->in.ofs);
	ndr_print_uint32(ndr, "needed", r->in.needed);
	ndr_print_uint32(ndr, "left", r->in.left);
	ndr_print_uint32(ndr, "max_wrapped", r->in.max_wrapped);
	ndr_print_uint32(ndr, "min_wrapped", r->in.min_wrapped);
	ndr_print_uint32(ndr, "size", r->in.size);
	ndr_print_array_uint8(ndr, "buf", r->in.buf, r->in.size);
	ndr->depth--;
	ndr_print_struct(ndr, name, "out");
	ndr->depth++;
	ndr_print_uint32(ndr, "ofs", r->out.ofs);
	ndr_print_uint32(ndr, "left", r->out.left);
	ndr_print_uint32(ndr, "max_unwrapped", r->out.max_unwrapped);
	ndr_print_uint32(ndr, "sig_size", r->out.sig_size);
	ndr_print_uint32(ndr, "size", r->out.size);
	ndr_print_array_uint8(ndr, "buf", r->out.buf, r->out.size);
	ndr->depth--;
}

#ifdef HAVE_LDAP_SASL_WRAPPING

static int ads_saslwrap_setup(Sockbuf_IO_Desc *sbiod, void *arg)
{
	struct ads_saslwrap *wrap = (struct ads_saslwrap *)arg;

	wrap->sbiod	= sbiod;

	sbiod->sbiod_pvt = wrap;

	return 0;
}

static int ads_saslwrap_remove(Sockbuf_IO_Desc *sbiod)
{
	return 0;
}

static ber_slen_t ads_saslwrap_prepare_inbuf(struct ads_saslwrap *wrap)
{
	wrap->in.ofs	= 0;
	wrap->in.needed	= 0;
	wrap->in.left	= 0;
	wrap->in.size	= 4 + wrap->in.min_wrapped;
	wrap->in.buf	= talloc_array(wrap->mem_ctx,
				       uint8_t, wrap->in.size);
	if (!wrap->in.buf) {
		return -1;
	}

	return 0;
}

static ber_slen_t ads_saslwrap_grow_inbuf(struct ads_saslwrap *wrap)
{
	if (wrap->in.size == (4 + wrap->in.needed)) {
		return 0;
	}

	wrap->in.size	= 4 + wrap->in.needed;
	wrap->in.buf	= talloc_realloc(wrap->mem_ctx,
					 wrap->in.buf,
					 uint8_t, wrap->in.size);
	if (!wrap->in.buf) {
		return -1;
	}

	return 0;
}

static void ads_saslwrap_shrink_inbuf(struct ads_saslwrap *wrap)
{
	talloc_free(wrap->in.buf);

	wrap->in.buf	= NULL;
	wrap->in.size	= 0;
	wrap->in.ofs	= 0;
	wrap->in.needed	= 0;
	wrap->in.left	= 0;
}

static ber_slen_t ads_saslwrap_read(Sockbuf_IO_Desc *sbiod,
				    void *buf, ber_len_t len)
{
	struct ads_saslwrap *wrap =
			(struct ads_saslwrap *)sbiod->sbiod_pvt;
	ber_slen_t ret;

	/* If ofs < 4 it means we don't have read the length header yet */
	if (wrap->in.ofs < 4) {
		ret = ads_saslwrap_prepare_inbuf(wrap);
		if (ret < 0) return ret;

		ret = LBER_SBIOD_READ_NEXT(sbiod,
					   wrap->in.buf + wrap->in.ofs,
					   4 - wrap->in.ofs);
		if (ret <= 0) return ret;
		wrap->in.ofs += ret;

		if (wrap->in.ofs < 4) goto eagain;

		wrap->in.needed = RIVAL(wrap->in.buf, 0);
		if (wrap->in.needed > wrap->in.max_wrapped) {
			errno = EINVAL;
			return -1;
		}
		if (wrap->in.needed < wrap->in.min_wrapped) {
			errno = EINVAL;
			return -1;
		}

		ret = ads_saslwrap_grow_inbuf(wrap);
		if (ret < 0) return ret;
	}

	/*
	 * if there's more data needed from the remote end,
	 * we need to read more
	 */
	if (wrap->in.needed > 0) {
		ret = LBER_SBIOD_READ_NEXT(sbiod,
					   wrap->in.buf + wrap->in.ofs,
					   wrap->in.needed);
		if (ret <= 0) return ret;
		wrap->in.ofs += ret;
		wrap->in.needed -= ret;

		if (wrap->in.needed > 0) goto eagain;
	}

	/*
	 * if we have a complete packet and have not yet unwrapped it
	 * we need to call the mech specific unwrap() hook
	 */
	if (wrap->in.needed == 0 && wrap->in.left == 0) {
		ADS_STATUS status;
		status = wrap->wrap_ops->unwrap(wrap);
		if (!ADS_ERR_OK(status)) {
			errno = EACCES;
			return -1;
		}
	}

	/*
	 * if we have unwrapped data give it to the caller
	 */
	if (wrap->in.left > 0) {
		ret = MIN(wrap->in.left, len);
		memcpy(buf, wrap->in.buf + wrap->in.ofs, ret);
		wrap->in.ofs += ret;
		wrap->in.left -= ret;

		/*
		 * if no more is left shrink the inbuf,
		 * this will trigger reading a new SASL packet
		 * from the remote stream in the next call
		 */
		if (wrap->in.left == 0) {
			ads_saslwrap_shrink_inbuf(wrap);
		}

		return ret;
	}

	/*
	 * if we don't have anything for the caller yet,
	 * tell him to ask again
	 */
eagain:
	errno = EAGAIN;
	return -1;
}

static ber_slen_t ads_saslwrap_prepare_outbuf(struct ads_saslwrap *wrap,
					      uint32_t len)
{
	wrap->out.ofs	= 0;
	wrap->out.left	= 0;
	wrap->out.size	= 4 + wrap->out.sig_size + len;
	wrap->out.buf	= talloc_array(wrap->mem_ctx,
					       uint8_t, wrap->out.size);
	if (!wrap->out.buf) {
		return -1;
	}

	return 0;
}

static void ads_saslwrap_shrink_outbuf(struct ads_saslwrap *wrap)
{
	talloc_free(wrap->out.buf);

	wrap->out.buf	= NULL;
	wrap->out.size	= 0;
	wrap->out.ofs	= 0;
	wrap->out.left	= 0;
}

static ber_slen_t ads_saslwrap_write(Sockbuf_IO_Desc *sbiod,
				     void *buf, ber_len_t len)
{
	struct ads_saslwrap *wrap =
			(struct ads_saslwrap *)sbiod->sbiod_pvt;
	ber_slen_t ret, rlen;

	/* if the buffer is empty, we need to wrap in incoming buffer */
	if (wrap->out.left == 0) {
		ADS_STATUS status;

		if (len == 0) {
			errno = EINVAL;
			return -1;
		}

		rlen = MIN(len, wrap->out.max_unwrapped);

		ret = ads_saslwrap_prepare_outbuf(wrap, rlen);
		if (ret < 0) return ret;
		
		status = wrap->wrap_ops->wrap(wrap, (uint8_t *)buf, rlen);
		if (!ADS_ERR_OK(status)) {
			errno = EACCES;
			return -1;
		}

		RSIVAL(wrap->out.buf, 0, wrap->out.left - 4);
	} else {
		rlen = -1;
	}

	ret = LBER_SBIOD_WRITE_NEXT(sbiod,
				    wrap->out.buf + wrap->out.ofs,
				    wrap->out.left);
	if (ret <= 0) return ret;
	wrap->out.ofs += ret;
	wrap->out.left -= ret;

	if (wrap->out.left == 0) {
		ads_saslwrap_shrink_outbuf(wrap);
	}

	if (rlen > 0) return rlen;

	errno = EAGAIN;
	return -1;
}

static int ads_saslwrap_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
{
	struct ads_saslwrap *wrap =
			(struct ads_saslwrap *)sbiod->sbiod_pvt;
	int ret;

	switch (opt) {
	case LBER_SB_OPT_DATA_READY:
		if (wrap->in.left > 0) {
			return 1;
		}
		ret = LBER_SBIOD_CTRL_NEXT(sbiod, opt, arg);
		break;
	default:
		ret = LBER_SBIOD_CTRL_NEXT(sbiod, opt, arg);
		break;
	}

	return ret;
}

static int ads_saslwrap_close(Sockbuf_IO_Desc *sbiod)
{
	return 0;
}

static const Sockbuf_IO ads_saslwrap_sockbuf_io = {
	ads_saslwrap_setup,	/* sbi_setup */
	ads_saslwrap_remove,	/* sbi_remove */
	ads_saslwrap_ctrl,	/* sbi_ctrl */
	ads_saslwrap_read,	/* sbi_read */
	ads_saslwrap_write,	/* sbi_write */
	ads_saslwrap_close	/* sbi_close */
};

ADS_STATUS ads_setup_sasl_wrapping(struct ads_saslwrap *wrap, LDAP *ld,
				   const struct ads_saslwrap_ops *ops,
				   void *private_data)
{
	ADS_STATUS status;
	Sockbuf *sb;
	Sockbuf_IO *io = discard_const_p(Sockbuf_IO, &ads_saslwrap_sockbuf_io);
	int rc;

	rc = ldap_get_option(ld, LDAP_OPT_SOCKBUF, &sb);
	status = ADS_ERROR_LDAP(rc);
	if (!ADS_ERR_OK(status)) {
		return status;
	}

	/* setup the real wrapping callbacks */
	rc = ber_sockbuf_add_io(sb, io, LBER_SBIOD_LEVEL_TRANSPORT, wrap);
	status = ADS_ERROR_LDAP(rc);
	if (!ADS_ERR_OK(status)) {
		return status;
	}

	wrap->wrap_ops		= ops;
	wrap->wrap_private_data	= private_data;

	return ADS_SUCCESS;
}
#else
ADS_STATUS ads_setup_sasl_wrapping(struct ads_saslwrap *wrap, LDAP *ld,
				   const struct ads_saslwrap_ops *ops,
				   void *private_data)
{
	return ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
}
#endif /* HAVE_LDAP_SASL_WRAPPING */