xref: /titanic_52/usr/src/lib/smbsrv/libmlrpc/common/ndr_ops.c (revision cd26c1c509540ef022ad03a6ecc491383525b786)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Server-side NDR stream (PDU) operations. Stream operations should
28  * return TRUE (non-zero) on success or FALSE (zero or a null pointer)
29  * on failure. When an operation returns FALSE, including ndo_malloc()
30  * returning NULL, it should set the nds->error to indicate what went
31  * wrong.
32  *
33  * When available, the relevant ndr reference is passed to the
34  * operation but keep in mind that it may be a null pointer.
35  *
36  * Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu()
37  * must never grow the PDU data. A request for out-of-bounds data is
38  * an error. The swap_bytes flag is 1 if NDR knows that the byte-
39  * order in the PDU is different from the local system.
40  */
41 
42 #include <sys/types.h>
43 #include <stdarg.h>
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <strings.h>
48 #include <string.h>
49 #include <assert.h>
50 
51 #include <smbsrv/libsmb.h>
52 #include <smbsrv/libmlrpc.h>
53 #include <smbsrv/ntstatus.h>
54 
55 #define	NDOBUFSZ		128
56 
57 #define	NDR_PDU_BLOCK_SIZE	(4*1024)
58 #define	NDR_PDU_BLOCK_MASK	(NDR_PDU_BLOCK_SIZE - 1)
59 #define	NDR_PDU_ALIGN(N) \
60 	(((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK)
61 #define	NDR_PDU_MAX_SIZE		(64*1024*1024)
62 
63 static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *);
64 static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *);
65 static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *);
66 static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long,
67     ndr_ref_t *);
68 static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long,
69     char *, int, ndr_ref_t *);
70 static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long,
71     char *, int, ndr_ref_t *);
72 static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *);
73 static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *);
74 static int ndo_reset(ndr_stream_t *);
75 static void ndo_destruct(ndr_stream_t *);
76 static void ndo_hexfmt(uint8_t *, int, int, char *, int);
77 
78 /*
79  * The ndr stream operations table.
80  */
81 static ndr_stream_ops_t nds_ops = {
82     ndo_malloc,
83     ndo_free,
84     ndo_grow_pdu,
85     ndo_pad_pdu,
86     ndo_get_pdu,
87     ndo_put_pdu,
88     ndo_tattle,
89     ndo_tattle_error,
90     ndo_reset,
91     ndo_destruct
92 };
93 
94 /*
95  * nds_bswap
96  *
97  * Copies len bytes from src to dst such that dst contains the bytes
98  * from src in reverse order.
99  *
100  * We expect to be dealing with bytes, words, dwords etc. So the
101  * length must be non-zero and a power of 2.
102  */
103 void
104 nds_bswap(void *srcbuf, void *dstbuf, size_t len)
105 {
106 	uint8_t *src = (uint8_t *)srcbuf;
107 	uint8_t *dst = (uint8_t *)dstbuf;
108 
109 	if ((len != 0) && ((len & (len - 1)) == 0)) {
110 		src += len;
111 
112 		while (len--)
113 			*dst++ = *(--src);
114 	}
115 }
116 
117 /*
118  * nds_initialize
119  *
120  * Initialize a stream. Sets up the PDU parameters and assigns the stream
121  * operations and the reference to the heap. An external heap is provided
122  * to the stream, rather than each stream creating its own heap.
123  */
124 void
125 nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint,
126     int composite_op, ndr_heap_t *heap)
127 {
128 	unsigned size;
129 
130 	assert(nds);
131 	assert(heap);
132 
133 	bzero(nds, sizeof (*nds));
134 
135 	if (pdu_size_hint > NDR_PDU_MAX_SIZE)
136 		return;
137 
138 	size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
139 	nds->pdu_base_addr = malloc(size);
140 	assert(nds->pdu_base_addr);
141 
142 	nds->pdu_max_size = size;
143 	nds->pdu_size = 0;
144 	nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
145 
146 	nds->ndo = &nds_ops;
147 	nds->heap = (struct ndr_heap *)heap;
148 
149 	nds->m_op = NDR_MODE_TO_M_OP(composite_op);
150 	nds->dir  = NDR_MODE_TO_DIR(composite_op);
151 
152 	nds->outer_queue_tailp = &nds->outer_queue_head;
153 }
154 
155 void
156 nds_finalize(ndr_stream_t *nds, ndr_fraglist_t *frags)
157 {
158 	iovec_t *iov;
159 	ndr_frag_t *frag;
160 	uint32_t size = 0;
161 
162 	bzero(frags, sizeof (ndr_fraglist_t));
163 
164 	for (frag = nds->frags.head; frag; frag = frag->next)
165 		size += frag->len;
166 
167 	if (size == 0 || size >= NDR_PDU_MAX_SIZE)
168 		return;
169 
170 	frags->iov = malloc(nds->frags.nfrag * sizeof (iovec_t));
171 	if (frags->iov == NULL)
172 		return;
173 
174 	frags->head = nds->frags.head;
175 	frags->tail = nds->frags.tail;
176 	frags->nfrag = nds->frags.nfrag;
177 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
178 
179 	frags->uio.uio_iov = frags->iov;
180 	frags->uio.uio_iovcnt = frags->nfrag;
181 	frags->uio.uio_offset = 0;
182 	frags->uio.uio_segflg = UIO_USERSPACE;
183 	frags->uio.uio_resid = size;
184 
185 	iov = frags->uio.uio_iov;
186 	for (frag = frags->head; frag; frag = frag->next) {
187 		iov->iov_base = (caddr_t)frag->buf;
188 		iov->iov_len = frag->len;
189 		++iov;
190 	}
191 }
192 
193 /*
194  * nds_destruct
195  *
196  * Destroy a stream. This is an external interface to provide access to
197  * the stream's destruct operation.
198  */
199 void
200 nds_destruct(ndr_stream_t *nds)
201 {
202 	NDS_DESTRUCT(nds);
203 }
204 
205 /*
206  * ndo_malloc
207  *
208  * Allocate memory from the stream heap.
209  */
210 /*ARGSUSED*/
211 static char *
212 ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
213 {
214 	return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
215 }
216 
217 /*
218  * ndo_free
219  *
220  * Always succeeds: cannot free individual stream allocations.
221  */
222 /*ARGSUSED*/
223 static int
224 ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
225 {
226 	return (1);
227 }
228 
229 /*
230  * ndo_grow_pdu
231  *
232  * This is the only place that should change the size of the PDU. If the
233  * desired offset is beyond the current PDU size, we realloc the PDU
234  * buffer to accommodate the request. For efficiency, the PDU is always
235  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
236  * beyond NDR_PDU_MAX_SIZE are rejected.
237  *
238  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
239  */
240 static int
241 ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
242 {
243 	unsigned char *pdu_addr;
244 	unsigned pdu_max_size;
245 
246 	ndo_printf(nds, ref, "grow %d", want_end_offset);
247 
248 	pdu_max_size = nds->pdu_max_size;
249 
250 	if (want_end_offset > pdu_max_size) {
251 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
252 
253 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
254 			return (0);
255 
256 		pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
257 		if (pdu_addr == 0)
258 			return (0);
259 
260 		nds->pdu_max_size = pdu_max_size;
261 		nds->pdu_base_addr = pdu_addr;
262 		nds->pdu_base_offset = (unsigned long)pdu_addr;
263 	}
264 
265 	nds->pdu_size = want_end_offset;
266 	return (1);
267 }
268 
269 static int
270 ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
271     unsigned long n_bytes, ndr_ref_t *ref)
272 {
273 	unsigned char *data;
274 
275 	data = (unsigned char *)nds->pdu_base_offset;
276 	data += pdu_offset;
277 
278 	ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
279 
280 	bzero(data, n_bytes);
281 	return (1);
282 }
283 
284 /*
285  * ndo_get_pdu
286  *
287  * The swap flag is 1 if NDR knows that the byte-order in the PDU
288  * is different from the local system.
289  *
290  * Returns 1 on success or 0 to indicate failure.
291  */
292 static int
293 ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
294     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
295 {
296 	unsigned char *data;
297 	char hexbuf[NDOBUFSZ];
298 
299 	data = (unsigned char *)nds->pdu_base_offset;
300 	data += pdu_offset;
301 
302 	ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
303 
304 	ndo_printf(nds, ref, "get %d@%-3d = %s",
305 	    n_bytes, pdu_offset, hexbuf);
306 
307 	if (!swap_bytes)
308 		bcopy(data, buf, n_bytes);
309 	else
310 		nds_bswap(data, (unsigned char *)buf, n_bytes);
311 
312 	return (1);
313 }
314 
315 /*
316  * ndo_put_pdu
317  *
318  * This is a receiver makes right protocol. So we do not need
319  * to be concerned about the byte-order of an outgoing PDU.
320  */
321 /*ARGSUSED*/
322 static int
323 ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
324     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
325 {
326 	unsigned char *data;
327 	char hexbuf[NDOBUFSZ];
328 
329 	data = (unsigned char *)nds->pdu_base_offset;
330 	data += pdu_offset;
331 
332 	ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
333 
334 	ndo_printf(nds, ref, "put %d@%-3d = %s",
335 	    n_bytes, pdu_offset, hexbuf);
336 
337 	bcopy(buf, data, n_bytes);
338 	return (1);
339 }
340 
341 static void
342 ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
343 {
344 	ndo_printf(nds, ref, what);
345 }
346 
347 static void
348 ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
349 {
350 	unsigned char *data;
351 	char hexbuf[NDOBUFSZ];
352 
353 	data = (unsigned char *)nds->pdu_base_offset;
354 	if (ref)
355 		data += ref->pdu_offset;
356 	else
357 		data += nds->pdu_scan_offset;
358 
359 	ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
360 
361 	ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
362 	    nds->error, nds->error_ref, nds->pdu_scan_offset,
363 	    nds->pdu_size, nds->pdu_max_size);
364 	ndo_printf(nds, ref, "      %s", hexbuf);
365 }
366 
367 /*
368  * ndo_reset
369  *
370  * Reset a stream: zap the outer_queue. We don't need to tamper
371  * with the stream heap: it's handled externally to the stream.
372  */
373 static int
374 ndo_reset(ndr_stream_t *nds)
375 {
376 	ndo_printf(nds, 0, "reset");
377 
378 	nds->pdu_size = 0;
379 	nds->pdu_scan_offset = 0;
380 	nds->outer_queue_head = 0;
381 	nds->outer_current = 0;
382 	nds->outer_queue_tailp = &nds->outer_queue_head;
383 
384 	return (1);
385 }
386 
387 /*
388  * ndo_destruct
389  *
390  * Destruct a stream: zap the outer_queue.
391  * Note: heap management (creation/destruction) is external to the stream.
392  */
393 static void
394 ndo_destruct(ndr_stream_t *nds)
395 {
396 	ndr_frag_t *frag;
397 
398 	ndo_printf(nds, 0, "destruct");
399 
400 	if (nds == NULL)
401 		return;
402 
403 	if (nds->pdu_base_addr != NULL) {
404 		free(nds->pdu_base_addr);
405 		nds->pdu_base_addr = NULL;
406 		nds->pdu_base_offset = 0;
407 	}
408 
409 	while ((frag = nds->frags.head) != NULL) {
410 		nds->frags.head = frag->next;
411 		free(frag);
412 	}
413 
414 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
415 
416 	nds->outer_queue_head = 0;
417 	nds->outer_current = 0;
418 	nds->outer_queue_tailp = &nds->outer_queue_head;
419 }
420 
421 /*
422  * Printf style formatting for NDR operations.
423  */
424 void
425 ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
426 {
427 	va_list ap;
428 	char buf[NDOBUFSZ];
429 
430 	va_start(ap, fmt);
431 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
432 	va_end(ap);
433 
434 	if (nds)
435 		ndo_fmt(nds, ref, buf);
436 	else
437 		ndo_trace(buf);
438 }
439 
440 /*
441  * Main output formatter for NDR operations.
442  *
443  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
444  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
445  *
446  *	U       Marshalling flag (M=marshal, U=unmarshal)
447  *	I       Direction flag (I=in, O=out)
448  *	...     Field name
449  *	get     PDU operation (get or put)
450  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
451  *	{05}    Value
452  */
453 void
454 ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
455 {
456 	ndr_ref_t	*p;
457 	int		indent;
458 	char		ref_name[NDOBUFSZ];
459 	char		buf[NDOBUFSZ];
460 	int		m_op_c = '?', dir_c = '?';
461 
462 	switch (nds->m_op) {
463 	case 0:				m_op_c = '-';	break;
464 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
465 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
466 	default:			m_op_c = '?';	break;
467 	}
468 
469 	switch (nds->dir) {
470 	case 0:				dir_c = '-';	break;
471 	case NDR_DIR_IN:		dir_c = 'I';	break;
472 	case NDR_DIR_OUT:		dir_c = 'O';	break;
473 	default:			dir_c = '?';	break;
474 	}
475 
476 	for (indent = 0, p = ref; p; p = p->enclosing)
477 		indent++;
478 
479 	if (ref && ref->name) {
480 		if (*ref->name == '[' && ref->enclosing) {
481 			indent--;
482 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
483 			    ref->enclosing->name, ref->name);
484 		} else {
485 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
486 		}
487 	} else {
488 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
489 	}
490 
491 	(void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s  %s",
492 	    m_op_c, dir_c, indent,
493 	    "....+....+....+....+....+....",
494 	    20 - indent, ref_name, note);
495 
496 	ndo_trace(buf);
497 }
498 
499 /*ARGSUSED*/
500 void
501 ndo_trace(const char *s)
502 {
503 	/*
504 	 * Temporary fbt for dtrace until user space sdt enabled.
505 	 */
506 }
507 
508 /*
509  * Format data as hex bytes (limit is 10 bytes):
510  *
511  *	1188689424 {10 f6 d9 46}
512  *
513  * If the input data is greater than 10 bytes, an ellipsis will
514  * be inserted before the closing brace.
515  */
516 static void
517 ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
518 {
519 	char *p = buf;
520 	int interp = 1;
521 	uint32_t c;
522 	int n;
523 	int i;
524 
525 	n = (size > 10) ? 10 : size;
526 	if (n > len-1)
527 		n = len-1;
528 
529 	switch (size) {
530 	case 1:
531 		c = *(uint8_t *)data;
532 		break;
533 	case 2:
534 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
535 			c = *(uint16_t *)data;
536 		else
537 			c = (data[0] << 8) | data[1];
538 		break;
539 	case 4:
540 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
541 			c = *(uint32_t *)data;
542 		} else {
543 			c = (data[0] << 24) | (data[1] << 16)
544 			    | (data[2] << 8) | data[3];
545 		}
546 		break;
547 	default:
548 		c = 0;
549 		interp = 0;
550 		break;
551 	}
552 
553 	if (interp)
554 		p += sprintf(p, "%4u {", c);
555 	else
556 		p += sprintf(p, " {");
557 
558 	p += sprintf(p, "%02x", data[0]);
559 	for (i = 1; i < n; i++)
560 		p += sprintf(p, " %02x", data[i]);
561 	if (size > 10)
562 		p += sprintf(p, " ...}");
563 	else
564 		p += sprintf(p, "}");
565 
566 	/*
567 	 * Show c if it's a printable character or wide-char.
568 	 */
569 	if (size < 4 && isprint((uint8_t)c))
570 		(void) sprintf(p, " %c", (uint8_t)c);
571 }
572