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