xref: /titanic_50/usr/src/lib/smbsrv/libmlrpc/common/ndr_ops.c (revision b56bf881a9655cb27b53cba1468312f7c6dfb0a2)
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 2010 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 int
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 	nds->ndo = &nds_ops;
135 	nds->heap = (struct ndr_heap *)heap;
136 
137 	if (pdu_size_hint > NDR_PDU_MAX_SIZE) {
138 		nds->error = NDR_ERR_BOUNDS_CHECK;
139 		nds->error_ref = __LINE__;
140 		NDS_TATTLE_ERROR(nds, NULL, NULL);
141 		return (NDR_DRC_FAULT_RESOURCE_1);
142 	}
143 
144 	size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint;
145 
146 	if ((nds->pdu_base_addr = malloc(size)) == NULL) {
147 		nds->error = NDR_ERR_MALLOC_FAILED;
148 		nds->error_ref = __LINE__;
149 		NDS_TATTLE_ERROR(nds, NULL, NULL);
150 		return (NDR_DRC_FAULT_OUT_OF_MEMORY);
151 	}
152 
153 	nds->pdu_max_size = size;
154 	nds->pdu_size = 0;
155 	nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr;
156 
157 	nds->m_op = NDR_MODE_TO_M_OP(composite_op);
158 	nds->dir  = NDR_MODE_TO_DIR(composite_op);
159 
160 	nds->outer_queue_tailp = &nds->outer_queue_head;
161 	return (0);
162 }
163 
164 void
165 nds_finalize(ndr_stream_t *nds, ndr_fraglist_t *frags)
166 {
167 	iovec_t *iov;
168 	ndr_frag_t *frag;
169 	uint32_t size = 0;
170 
171 	bzero(frags, sizeof (ndr_fraglist_t));
172 
173 	for (frag = nds->frags.head; frag; frag = frag->next)
174 		size += frag->len;
175 
176 	if (size == 0 || size >= NDR_PDU_MAX_SIZE)
177 		return;
178 
179 	frags->iov = malloc(nds->frags.nfrag * sizeof (iovec_t));
180 	if (frags->iov == NULL)
181 		return;
182 
183 	frags->head = nds->frags.head;
184 	frags->tail = nds->frags.tail;
185 	frags->nfrag = nds->frags.nfrag;
186 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
187 
188 	frags->uio.uio_iov = frags->iov;
189 	frags->uio.uio_iovcnt = frags->nfrag;
190 	frags->uio.uio_offset = 0;
191 	frags->uio.uio_segflg = UIO_USERSPACE;
192 	frags->uio.uio_resid = size;
193 
194 	iov = frags->uio.uio_iov;
195 	for (frag = frags->head; frag; frag = frag->next) {
196 		iov->iov_base = (caddr_t)frag->buf;
197 		iov->iov_len = frag->len;
198 		++iov;
199 	}
200 }
201 
202 /*
203  * nds_destruct
204  *
205  * Destroy a stream. This is an external interface to provide access to
206  * the stream's destruct operation.
207  */
208 void
209 nds_destruct(ndr_stream_t *nds)
210 {
211 	if ((nds == NULL) || (nds->ndo == NULL))
212 		return;
213 
214 	NDS_DESTRUCT(nds);
215 }
216 
217 /*
218  * Print NDR stream state.
219  */
220 void
221 nds_show_state(ndr_stream_t *nds)
222 {
223 	if (nds == NULL) {
224 		ndo_printf(NULL, NULL, "nds: <null");
225 		return;
226 	}
227 
228 	ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d",
229 	    nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size,
230 	    nds->pdu_scan_offset);
231 }
232 
233 /*
234  * ndo_malloc
235  *
236  * Allocate memory from the stream heap.
237  */
238 /*ARGSUSED*/
239 static char *
240 ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref)
241 {
242 	return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len));
243 }
244 
245 /*
246  * ndo_free
247  *
248  * Always succeeds: cannot free individual stream allocations.
249  */
250 /*ARGSUSED*/
251 static int
252 ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref)
253 {
254 	return (1);
255 }
256 
257 /*
258  * ndo_grow_pdu
259  *
260  * This is the only place that should change the size of the PDU. If the
261  * desired offset is beyond the current PDU size, we realloc the PDU
262  * buffer to accommodate the request. For efficiency, the PDU is always
263  * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU
264  * beyond NDR_PDU_MAX_SIZE are rejected.
265  *
266  * Returns 1 to indicate success. Otherwise 0 to indicate failure.
267  */
268 static int
269 ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref)
270 {
271 	unsigned char *pdu_addr;
272 	unsigned pdu_max_size;
273 
274 	ndo_printf(nds, ref, "grow %d", want_end_offset);
275 
276 	pdu_max_size = nds->pdu_max_size;
277 
278 	if (want_end_offset > pdu_max_size) {
279 		pdu_max_size = NDR_PDU_ALIGN(want_end_offset);
280 
281 		if (pdu_max_size >= NDR_PDU_MAX_SIZE)
282 			return (0);
283 
284 		pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size);
285 		if (pdu_addr == 0)
286 			return (0);
287 
288 		nds->pdu_max_size = pdu_max_size;
289 		nds->pdu_base_addr = pdu_addr;
290 		nds->pdu_base_offset = (unsigned long)pdu_addr;
291 	}
292 
293 	nds->pdu_size = want_end_offset;
294 	return (1);
295 }
296 
297 static int
298 ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
299     unsigned long n_bytes, ndr_ref_t *ref)
300 {
301 	unsigned char *data;
302 
303 	data = (unsigned char *)nds->pdu_base_offset;
304 	data += pdu_offset;
305 
306 	ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset);
307 
308 	bzero(data, n_bytes);
309 	return (1);
310 }
311 
312 /*
313  * ndo_get_pdu
314  *
315  * The swap flag is 1 if NDR knows that the byte-order in the PDU
316  * is different from the local system.
317  *
318  * Returns 1 on success or 0 to indicate failure.
319  */
320 static int
321 ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
322     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
323 {
324 	unsigned char *data;
325 	char hexbuf[NDOBUFSZ];
326 
327 	data = (unsigned char *)nds->pdu_base_offset;
328 	data += pdu_offset;
329 
330 	ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ);
331 
332 	ndo_printf(nds, ref, "get %d@%-3d = %s",
333 	    n_bytes, pdu_offset, hexbuf);
334 
335 	if (!swap_bytes)
336 		bcopy(data, buf, n_bytes);
337 	else
338 		nds_bswap(data, (unsigned char *)buf, n_bytes);
339 
340 	return (1);
341 }
342 
343 /*
344  * ndo_put_pdu
345  *
346  * This is a receiver makes right protocol. So we do not need
347  * to be concerned about the byte-order of an outgoing PDU.
348  */
349 /*ARGSUSED*/
350 static int
351 ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset,
352     unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref)
353 {
354 	unsigned char *data;
355 	char hexbuf[NDOBUFSZ];
356 
357 	data = (unsigned char *)nds->pdu_base_offset;
358 	data += pdu_offset;
359 
360 	ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ);
361 
362 	ndo_printf(nds, ref, "put %d@%-3d = %s",
363 	    n_bytes, pdu_offset, hexbuf);
364 
365 	bcopy(buf, data, n_bytes);
366 	return (1);
367 }
368 
369 static void
370 ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref)
371 {
372 	ndo_printf(nds, ref, what);
373 }
374 
375 static void
376 ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref)
377 {
378 	unsigned char *data;
379 	char hexbuf[NDOBUFSZ];
380 
381 	if (nds->pdu_base_addr != NULL) {
382 		data = (unsigned char *)nds->pdu_base_offset;
383 		if (ref)
384 			data += ref->pdu_offset;
385 		else
386 			data += nds->pdu_scan_offset;
387 
388 		ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ);
389 	} else {
390 		bzero(hexbuf, NDOBUFSZ);
391 	}
392 
393 	ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d",
394 	    nds->error, nds->error_ref, nds->pdu_scan_offset,
395 	    nds->pdu_size, nds->pdu_max_size);
396 	ndo_printf(nds, ref, "      %s", hexbuf);
397 }
398 
399 /*
400  * ndo_reset
401  *
402  * Reset a stream: zap the outer_queue. We don't need to tamper
403  * with the stream heap: it's handled externally to the stream.
404  */
405 static int
406 ndo_reset(ndr_stream_t *nds)
407 {
408 	ndo_printf(nds, 0, "reset");
409 
410 	nds->pdu_size = 0;
411 	nds->pdu_scan_offset = 0;
412 	nds->outer_queue_head = 0;
413 	nds->outer_current = 0;
414 	nds->outer_queue_tailp = &nds->outer_queue_head;
415 
416 	return (1);
417 }
418 
419 /*
420  * ndo_destruct
421  *
422  * Destruct a stream: zap the outer_queue.
423  * Note: heap management (creation/destruction) is external to the stream.
424  */
425 static void
426 ndo_destruct(ndr_stream_t *nds)
427 {
428 	ndr_frag_t *frag;
429 
430 	ndo_printf(nds, 0, "destruct");
431 
432 	if (nds == NULL)
433 		return;
434 
435 	if (nds->pdu_base_addr != NULL) {
436 		free(nds->pdu_base_addr);
437 		nds->pdu_base_addr = NULL;
438 		nds->pdu_base_offset = 0;
439 	}
440 
441 	while ((frag = nds->frags.head) != NULL) {
442 		nds->frags.head = frag->next;
443 		free(frag);
444 	}
445 
446 	bzero(&nds->frags, sizeof (ndr_fraglist_t));
447 
448 	nds->outer_queue_head = 0;
449 	nds->outer_current = 0;
450 	nds->outer_queue_tailp = &nds->outer_queue_head;
451 }
452 
453 /*
454  * Printf style formatting for NDR operations.
455  */
456 void
457 ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...)
458 {
459 	va_list ap;
460 	char buf[NDOBUFSZ];
461 
462 	va_start(ap, fmt);
463 	(void) vsnprintf(buf, NDOBUFSZ, fmt, ap);
464 	va_end(ap);
465 
466 	if (nds)
467 		ndo_fmt(nds, ref, buf);
468 	else
469 		ndo_trace(buf);
470 }
471 
472 /*
473  * Main output formatter for NDR operations.
474  *
475  *	UI 03 ... rpc_vers           get 1@0   =    5 {05}
476  *	UI 03 ... rpc_vers_minor     get 1@1   =    0 {00}
477  *
478  *	U       Marshalling flag (M=marshal, U=unmarshal)
479  *	I       Direction flag (I=in, O=out)
480  *	...     Field name
481  *	get     PDU operation (get or put)
482  *	1@0	Bytes @ offset (i.e. 1 byte at offset 0)
483  *	{05}    Value
484  */
485 void
486 ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note)
487 {
488 	ndr_ref_t	*p;
489 	int		indent;
490 	char		ref_name[NDOBUFSZ];
491 	char		buf[NDOBUFSZ];
492 	int		m_op_c = '?', dir_c = '?';
493 
494 	switch (nds->m_op) {
495 	case 0:				m_op_c = '-';	break;
496 	case NDR_M_OP_MARSHALL:		m_op_c = 'M';	break;
497 	case NDR_M_OP_UNMARSHALL:	m_op_c = 'U';	break;
498 	default:			m_op_c = '?';	break;
499 	}
500 
501 	switch (nds->dir) {
502 	case 0:				dir_c = '-';	break;
503 	case NDR_DIR_IN:		dir_c = 'I';	break;
504 	case NDR_DIR_OUT:		dir_c = 'O';	break;
505 	default:			dir_c = '?';	break;
506 	}
507 
508 	for (indent = 0, p = ref; p; p = p->enclosing)
509 		indent++;
510 
511 	if (ref && ref->name) {
512 		if (*ref->name == '[' && ref->enclosing) {
513 			indent--;
514 			(void) snprintf(ref_name, NDOBUFSZ, "%s%s",
515 			    ref->enclosing->name, ref->name);
516 		} else {
517 			(void) strlcpy(ref_name, ref->name, NDOBUFSZ);
518 		}
519 	} else {
520 		(void) strlcpy(ref_name, "----", NDOBUFSZ);
521 	}
522 
523 	(void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s  %s",
524 	    m_op_c, dir_c, indent,
525 	    "....+....+....+....+....+....",
526 	    20 - indent, ref_name, note);
527 
528 	ndo_trace(buf);
529 }
530 
531 /*ARGSUSED*/
532 void
533 ndo_trace(const char *s)
534 {
535 	/*
536 	 * Temporary fbt for dtrace until user space sdt enabled.
537 	 */
538 }
539 
540 /*
541  * Format data as hex bytes (limit is 10 bytes):
542  *
543  *	1188689424 {10 f6 d9 46}
544  *
545  * If the input data is greater than 10 bytes, an ellipsis will
546  * be inserted before the closing brace.
547  */
548 static void
549 ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len)
550 {
551 	char *p = buf;
552 	int interp = 1;
553 	uint32_t c;
554 	int n;
555 	int i;
556 
557 	n = (size > 10) ? 10 : size;
558 	if (n > len-1)
559 		n = len-1;
560 
561 	switch (size) {
562 	case 1:
563 		c = *(uint8_t *)data;
564 		break;
565 	case 2:
566 		if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/
567 			c = *(uint16_t *)data;
568 		else
569 			c = (data[0] << 8) | data[1];
570 		break;
571 	case 4:
572 		if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/
573 			c = *(uint32_t *)data;
574 		} else {
575 			c = (data[0] << 24) | (data[1] << 16)
576 			    | (data[2] << 8) | data[3];
577 		}
578 		break;
579 	default:
580 		c = 0;
581 		interp = 0;
582 		break;
583 	}
584 
585 	if (interp)
586 		p += sprintf(p, "%4u {", c);
587 	else
588 		p += sprintf(p, " {");
589 
590 	p += sprintf(p, "%02x", data[0]);
591 	for (i = 1; i < n; i++)
592 		p += sprintf(p, " %02x", data[i]);
593 	if (size > 10)
594 		p += sprintf(p, " ...}");
595 	else
596 		p += sprintf(p, "}");
597 
598 	/*
599 	 * Show c if it's a printable character or wide-char.
600 	 */
601 	if (size < 4 && isprint((uint8_t)c))
602 		(void) sprintf(p, " %c", (uint8_t)c);
603 }
604