15772Sas200622 /* 25772Sas200622 * CDDL HEADER START 35772Sas200622 * 45772Sas200622 * The contents of this file are subject to the terms of the 55772Sas200622 * Common Development and Distribution License (the "License"). 65772Sas200622 * You may not use this file except in compliance with the License. 75772Sas200622 * 85772Sas200622 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95772Sas200622 * or http://www.opensolaris.org/os/licensing. 105772Sas200622 * See the License for the specific language governing permissions 115772Sas200622 * and limitations under the License. 125772Sas200622 * 135772Sas200622 * When distributing Covered Code, include this CDDL HEADER in each 145772Sas200622 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155772Sas200622 * If applicable, add the following below this CDDL HEADER, with the 165772Sas200622 * fields enclosed by brackets "[]" replaced with your own identifying 175772Sas200622 * information: Portions Copyright [yyyy] [name of copyright owner] 185772Sas200622 * 195772Sas200622 * CDDL HEADER END 205772Sas200622 */ 215772Sas200622 /* 22*11963SAfshin.Ardakani@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 235772Sas200622 * Use is subject to license terms. 245772Sas200622 */ 255772Sas200622 265772Sas200622 /* 278334SJose.Borrego@Sun.COM * Server-side NDR stream (PDU) operations. Stream operations should 288334SJose.Borrego@Sun.COM * return TRUE (non-zero) on success or FALSE (zero or a null pointer) 298334SJose.Borrego@Sun.COM * on failure. When an operation returns FALSE, including ndo_malloc() 308334SJose.Borrego@Sun.COM * returning NULL, it should set the nds->error to indicate what went 318334SJose.Borrego@Sun.COM * wrong. 325772Sas200622 * 338334SJose.Borrego@Sun.COM * When available, the relevant ndr reference is passed to the 345772Sas200622 * operation but keep in mind that it may be a null pointer. 355772Sas200622 * 368334SJose.Borrego@Sun.COM * Functions ndo_get_pdu(), ndo_put_pdu(), and ndo_pad_pdu() 375772Sas200622 * must never grow the PDU data. A request for out-of-bounds data is 385772Sas200622 * an error. The swap_bytes flag is 1 if NDR knows that the byte- 395772Sas200622 * order in the PDU is different from the local system. 405772Sas200622 */ 415772Sas200622 425772Sas200622 #include <sys/types.h> 435772Sas200622 #include <stdarg.h> 445772Sas200622 #include <ctype.h> 455772Sas200622 #include <stdio.h> 465772Sas200622 #include <stdlib.h> 475772Sas200622 #include <strings.h> 485772Sas200622 #include <string.h> 495772Sas200622 #include <assert.h> 505772Sas200622 515772Sas200622 #include <smbsrv/libsmb.h> 528334SJose.Borrego@Sun.COM #include <smbsrv/libmlrpc.h> 535772Sas200622 #include <smbsrv/ntstatus.h> 545772Sas200622 555772Sas200622 #define NDOBUFSZ 128 565772Sas200622 575772Sas200622 #define NDR_PDU_BLOCK_SIZE (4*1024) 585772Sas200622 #define NDR_PDU_BLOCK_MASK (NDR_PDU_BLOCK_SIZE - 1) 595772Sas200622 #define NDR_PDU_ALIGN(N) \ 605772Sas200622 (((N) + NDR_PDU_BLOCK_SIZE) & ~NDR_PDU_BLOCK_MASK) 615772Sas200622 #define NDR_PDU_MAX_SIZE (64*1024*1024) 625772Sas200622 638334SJose.Borrego@Sun.COM static char *ndo_malloc(ndr_stream_t *, unsigned, ndr_ref_t *); 648334SJose.Borrego@Sun.COM static int ndo_free(ndr_stream_t *, char *, ndr_ref_t *); 658334SJose.Borrego@Sun.COM static int ndo_grow_pdu(ndr_stream_t *, unsigned long, ndr_ref_t *); 668334SJose.Borrego@Sun.COM static int ndo_pad_pdu(ndr_stream_t *, unsigned long, unsigned long, 678334SJose.Borrego@Sun.COM ndr_ref_t *); 688334SJose.Borrego@Sun.COM static int ndo_get_pdu(ndr_stream_t *, unsigned long, unsigned long, 698334SJose.Borrego@Sun.COM char *, int, ndr_ref_t *); 708334SJose.Borrego@Sun.COM static int ndo_put_pdu(ndr_stream_t *, unsigned long, unsigned long, 718334SJose.Borrego@Sun.COM char *, int, ndr_ref_t *); 728334SJose.Borrego@Sun.COM static void ndo_tattle(ndr_stream_t *, char *, ndr_ref_t *); 738334SJose.Borrego@Sun.COM static void ndo_tattle_error(ndr_stream_t *, ndr_ref_t *); 748334SJose.Borrego@Sun.COM static int ndo_reset(ndr_stream_t *); 758334SJose.Borrego@Sun.COM static void ndo_destruct(ndr_stream_t *); 768334SJose.Borrego@Sun.COM static void ndo_hexfmt(uint8_t *, int, int, char *, int); 775772Sas200622 785772Sas200622 /* 798334SJose.Borrego@Sun.COM * The ndr stream operations table. 805772Sas200622 */ 818334SJose.Borrego@Sun.COM static ndr_stream_ops_t nds_ops = { 828334SJose.Borrego@Sun.COM ndo_malloc, 838334SJose.Borrego@Sun.COM ndo_free, 848334SJose.Borrego@Sun.COM ndo_grow_pdu, 858334SJose.Borrego@Sun.COM ndo_pad_pdu, 868334SJose.Borrego@Sun.COM ndo_get_pdu, 878334SJose.Borrego@Sun.COM ndo_put_pdu, 888334SJose.Borrego@Sun.COM ndo_tattle, 898334SJose.Borrego@Sun.COM ndo_tattle_error, 908334SJose.Borrego@Sun.COM ndo_reset, 918334SJose.Borrego@Sun.COM ndo_destruct 925772Sas200622 }; 935772Sas200622 945772Sas200622 /* 958334SJose.Borrego@Sun.COM * nds_bswap 965772Sas200622 * 975772Sas200622 * Copies len bytes from src to dst such that dst contains the bytes 985772Sas200622 * from src in reverse order. 995772Sas200622 * 1005772Sas200622 * We expect to be dealing with bytes, words, dwords etc. So the 1015772Sas200622 * length must be non-zero and a power of 2. 1025772Sas200622 */ 1035772Sas200622 void 1048334SJose.Borrego@Sun.COM nds_bswap(void *srcbuf, void *dstbuf, size_t len) 1055772Sas200622 { 1065772Sas200622 uint8_t *src = (uint8_t *)srcbuf; 1075772Sas200622 uint8_t *dst = (uint8_t *)dstbuf; 1085772Sas200622 1095772Sas200622 if ((len != 0) && ((len & (len - 1)) == 0)) { 1105772Sas200622 src += len; 1115772Sas200622 1125772Sas200622 while (len--) 1135772Sas200622 *dst++ = *(--src); 1145772Sas200622 } 1155772Sas200622 } 1165772Sas200622 1175772Sas200622 /* 1188334SJose.Borrego@Sun.COM * nds_initialize 1195772Sas200622 * 1205772Sas200622 * Initialize a stream. Sets up the PDU parameters and assigns the stream 1215772Sas200622 * operations and the reference to the heap. An external heap is provided 1225772Sas200622 * to the stream, rather than each stream creating its own heap. 1235772Sas200622 */ 12411337SWilliam.Krier@Sun.COM int 1258334SJose.Borrego@Sun.COM nds_initialize(ndr_stream_t *nds, unsigned pdu_size_hint, 1268334SJose.Borrego@Sun.COM int composite_op, ndr_heap_t *heap) 1275772Sas200622 { 1285772Sas200622 unsigned size; 1295772Sas200622 1308334SJose.Borrego@Sun.COM assert(nds); 1315772Sas200622 assert(heap); 1325772Sas200622 1338334SJose.Borrego@Sun.COM bzero(nds, sizeof (*nds)); 134*11963SAfshin.Ardakani@Sun.COM nds->ndo = &nds_ops; 135*11963SAfshin.Ardakani@Sun.COM nds->heap = (struct ndr_heap *)heap; 1365772Sas200622 137*11963SAfshin.Ardakani@Sun.COM if (pdu_size_hint > NDR_PDU_MAX_SIZE) { 138*11963SAfshin.Ardakani@Sun.COM nds->error = NDR_ERR_BOUNDS_CHECK; 139*11963SAfshin.Ardakani@Sun.COM nds->error_ref = __LINE__; 140*11963SAfshin.Ardakani@Sun.COM NDS_TATTLE_ERROR(nds, NULL, NULL); 141*11963SAfshin.Ardakani@Sun.COM return (NDR_DRC_FAULT_RESOURCE_1); 142*11963SAfshin.Ardakani@Sun.COM } 1435772Sas200622 1445772Sas200622 size = (pdu_size_hint == 0) ? NDR_PDU_BLOCK_SIZE : pdu_size_hint; 14511337SWilliam.Krier@Sun.COM 14611337SWilliam.Krier@Sun.COM if ((nds->pdu_base_addr = malloc(size)) == NULL) { 14711337SWilliam.Krier@Sun.COM nds->error = NDR_ERR_MALLOC_FAILED; 14811337SWilliam.Krier@Sun.COM nds->error_ref = __LINE__; 14911337SWilliam.Krier@Sun.COM NDS_TATTLE_ERROR(nds, NULL, NULL); 15011337SWilliam.Krier@Sun.COM return (NDR_DRC_FAULT_OUT_OF_MEMORY); 15111337SWilliam.Krier@Sun.COM } 1525772Sas200622 1538334SJose.Borrego@Sun.COM nds->pdu_max_size = size; 1548334SJose.Borrego@Sun.COM nds->pdu_size = 0; 1558334SJose.Borrego@Sun.COM nds->pdu_base_offset = (unsigned long)nds->pdu_base_addr; 1565772Sas200622 1578334SJose.Borrego@Sun.COM nds->m_op = NDR_MODE_TO_M_OP(composite_op); 1588334SJose.Borrego@Sun.COM nds->dir = NDR_MODE_TO_DIR(composite_op); 1595772Sas200622 1608334SJose.Borrego@Sun.COM nds->outer_queue_tailp = &nds->outer_queue_head; 16111337SWilliam.Krier@Sun.COM return (0); 1625772Sas200622 } 1635772Sas200622 1647052Samw void 1658334SJose.Borrego@Sun.COM nds_finalize(ndr_stream_t *nds, ndr_fraglist_t *frags) 1666482Samw { 1677052Samw iovec_t *iov; 1686482Samw ndr_frag_t *frag; 1696482Samw uint32_t size = 0; 1706482Samw 1717052Samw bzero(frags, sizeof (ndr_fraglist_t)); 1727052Samw 1738334SJose.Borrego@Sun.COM for (frag = nds->frags.head; frag; frag = frag->next) 1746482Samw size += frag->len; 1756482Samw 1767052Samw if (size == 0 || size >= NDR_PDU_MAX_SIZE) 1777052Samw return; 1787052Samw 1798334SJose.Borrego@Sun.COM frags->iov = malloc(nds->frags.nfrag * sizeof (iovec_t)); 1807052Samw if (frags->iov == NULL) 1817052Samw return; 1827052Samw 1838334SJose.Borrego@Sun.COM frags->head = nds->frags.head; 1848334SJose.Borrego@Sun.COM frags->tail = nds->frags.tail; 1858334SJose.Borrego@Sun.COM frags->nfrag = nds->frags.nfrag; 1868334SJose.Borrego@Sun.COM bzero(&nds->frags, sizeof (ndr_fraglist_t)); 1876482Samw 1887052Samw frags->uio.uio_iov = frags->iov; 1897052Samw frags->uio.uio_iovcnt = frags->nfrag; 1907052Samw frags->uio.uio_offset = 0; 1917052Samw frags->uio.uio_segflg = UIO_USERSPACE; 1927052Samw frags->uio.uio_resid = size; 1937052Samw 1947052Samw iov = frags->uio.uio_iov; 1957052Samw for (frag = frags->head; frag; frag = frag->next) { 1967052Samw iov->iov_base = (caddr_t)frag->buf; 1977052Samw iov->iov_len = frag->len; 1987052Samw ++iov; 1996482Samw } 2006482Samw } 2016482Samw 2025772Sas200622 /* 2038334SJose.Borrego@Sun.COM * nds_destruct 2045772Sas200622 * 2055772Sas200622 * Destroy a stream. This is an external interface to provide access to 2065772Sas200622 * the stream's destruct operation. 2075772Sas200622 */ 2085772Sas200622 void 2098334SJose.Borrego@Sun.COM nds_destruct(ndr_stream_t *nds) 2105772Sas200622 { 21110475Samw@Sun.COM if ((nds == NULL) || (nds->ndo == NULL)) 21210475Samw@Sun.COM return; 21310475Samw@Sun.COM 2148334SJose.Borrego@Sun.COM NDS_DESTRUCT(nds); 2155772Sas200622 } 2165772Sas200622 2175772Sas200622 /* 21810475Samw@Sun.COM * Print NDR stream state. 21910475Samw@Sun.COM */ 22010475Samw@Sun.COM void 22110475Samw@Sun.COM nds_show_state(ndr_stream_t *nds) 22210475Samw@Sun.COM { 22310475Samw@Sun.COM if (nds == NULL) { 22410475Samw@Sun.COM ndo_printf(NULL, NULL, "nds: <null"); 22510475Samw@Sun.COM return; 22610475Samw@Sun.COM } 22710475Samw@Sun.COM 22810475Samw@Sun.COM ndo_printf(NULL, NULL, "nds: base=0x%x, size=%d, max=%d, scan=%d", 22910475Samw@Sun.COM nds->pdu_base_offset, nds->pdu_size, nds->pdu_max_size, 23010475Samw@Sun.COM nds->pdu_scan_offset); 23110475Samw@Sun.COM } 23210475Samw@Sun.COM 23310475Samw@Sun.COM /* 2348334SJose.Borrego@Sun.COM * ndo_malloc 2355772Sas200622 * 2365772Sas200622 * Allocate memory from the stream heap. 2375772Sas200622 */ 2385772Sas200622 /*ARGSUSED*/ 2395772Sas200622 static char * 2408334SJose.Borrego@Sun.COM ndo_malloc(ndr_stream_t *nds, unsigned len, ndr_ref_t *ref) 2415772Sas200622 { 2428334SJose.Borrego@Sun.COM return (ndr_heap_malloc((ndr_heap_t *)nds->heap, len)); 2435772Sas200622 } 2445772Sas200622 2455772Sas200622 /* 2468334SJose.Borrego@Sun.COM * ndo_free 2475772Sas200622 * 2485772Sas200622 * Always succeeds: cannot free individual stream allocations. 2495772Sas200622 */ 2505772Sas200622 /*ARGSUSED*/ 2515772Sas200622 static int 2528334SJose.Borrego@Sun.COM ndo_free(ndr_stream_t *nds, char *p, ndr_ref_t *ref) 2535772Sas200622 { 2545772Sas200622 return (1); 2555772Sas200622 } 2565772Sas200622 2575772Sas200622 /* 2588334SJose.Borrego@Sun.COM * ndo_grow_pdu 2595772Sas200622 * 2605772Sas200622 * This is the only place that should change the size of the PDU. If the 2615772Sas200622 * desired offset is beyond the current PDU size, we realloc the PDU 2625772Sas200622 * buffer to accommodate the request. For efficiency, the PDU is always 2635772Sas200622 * extended to a NDR_PDU_BLOCK_SIZE boundary. Requests to grow the PDU 2645772Sas200622 * beyond NDR_PDU_MAX_SIZE are rejected. 2655772Sas200622 * 2665772Sas200622 * Returns 1 to indicate success. Otherwise 0 to indicate failure. 2675772Sas200622 */ 2685772Sas200622 static int 2698334SJose.Borrego@Sun.COM ndo_grow_pdu(ndr_stream_t *nds, unsigned long want_end_offset, ndr_ref_t *ref) 2705772Sas200622 { 2715772Sas200622 unsigned char *pdu_addr; 2725772Sas200622 unsigned pdu_max_size; 2735772Sas200622 2748334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "grow %d", want_end_offset); 2755772Sas200622 2768334SJose.Borrego@Sun.COM pdu_max_size = nds->pdu_max_size; 2775772Sas200622 2785772Sas200622 if (want_end_offset > pdu_max_size) { 2795772Sas200622 pdu_max_size = NDR_PDU_ALIGN(want_end_offset); 2805772Sas200622 2815772Sas200622 if (pdu_max_size >= NDR_PDU_MAX_SIZE) 2825772Sas200622 return (0); 2835772Sas200622 2848334SJose.Borrego@Sun.COM pdu_addr = realloc(nds->pdu_base_addr, pdu_max_size); 2855772Sas200622 if (pdu_addr == 0) 2865772Sas200622 return (0); 2875772Sas200622 2888334SJose.Borrego@Sun.COM nds->pdu_max_size = pdu_max_size; 2898334SJose.Borrego@Sun.COM nds->pdu_base_addr = pdu_addr; 2908334SJose.Borrego@Sun.COM nds->pdu_base_offset = (unsigned long)pdu_addr; 2915772Sas200622 } 2925772Sas200622 2938334SJose.Borrego@Sun.COM nds->pdu_size = want_end_offset; 2945772Sas200622 return (1); 2955772Sas200622 } 2965772Sas200622 2975772Sas200622 static int 2988334SJose.Borrego@Sun.COM ndo_pad_pdu(ndr_stream_t *nds, unsigned long pdu_offset, 2998334SJose.Borrego@Sun.COM unsigned long n_bytes, ndr_ref_t *ref) 3005772Sas200622 { 3015772Sas200622 unsigned char *data; 3025772Sas200622 3038334SJose.Borrego@Sun.COM data = (unsigned char *)nds->pdu_base_offset; 3045772Sas200622 data += pdu_offset; 3055772Sas200622 3068334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "pad %d@%-3d", n_bytes, pdu_offset); 3075772Sas200622 3085772Sas200622 bzero(data, n_bytes); 3095772Sas200622 return (1); 3105772Sas200622 } 3115772Sas200622 3125772Sas200622 /* 3138334SJose.Borrego@Sun.COM * ndo_get_pdu 3145772Sas200622 * 3155772Sas200622 * The swap flag is 1 if NDR knows that the byte-order in the PDU 3165772Sas200622 * is different from the local system. 3175772Sas200622 * 3185772Sas200622 * Returns 1 on success or 0 to indicate failure. 3195772Sas200622 */ 3205772Sas200622 static int 3218334SJose.Borrego@Sun.COM ndo_get_pdu(ndr_stream_t *nds, unsigned long pdu_offset, 3228334SJose.Borrego@Sun.COM unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref) 3235772Sas200622 { 3245772Sas200622 unsigned char *data; 3255772Sas200622 char hexbuf[NDOBUFSZ]; 3265772Sas200622 3278334SJose.Borrego@Sun.COM data = (unsigned char *)nds->pdu_base_offset; 3285772Sas200622 data += pdu_offset; 3295772Sas200622 3308334SJose.Borrego@Sun.COM ndo_hexfmt(data, n_bytes, swap_bytes, hexbuf, NDOBUFSZ); 3315772Sas200622 3328334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "get %d@%-3d = %s", 3335772Sas200622 n_bytes, pdu_offset, hexbuf); 3345772Sas200622 3355772Sas200622 if (!swap_bytes) 3365772Sas200622 bcopy(data, buf, n_bytes); 3375772Sas200622 else 3388334SJose.Borrego@Sun.COM nds_bswap(data, (unsigned char *)buf, n_bytes); 3395772Sas200622 3405772Sas200622 return (1); 3415772Sas200622 } 3425772Sas200622 3435772Sas200622 /* 3448334SJose.Borrego@Sun.COM * ndo_put_pdu 3455772Sas200622 * 3465772Sas200622 * This is a receiver makes right protocol. So we do not need 3475772Sas200622 * to be concerned about the byte-order of an outgoing PDU. 3485772Sas200622 */ 3495772Sas200622 /*ARGSUSED*/ 3505772Sas200622 static int 3518334SJose.Borrego@Sun.COM ndo_put_pdu(ndr_stream_t *nds, unsigned long pdu_offset, 3528334SJose.Borrego@Sun.COM unsigned long n_bytes, char *buf, int swap_bytes, ndr_ref_t *ref) 3535772Sas200622 { 3545772Sas200622 unsigned char *data; 3555772Sas200622 char hexbuf[NDOBUFSZ]; 3565772Sas200622 3578334SJose.Borrego@Sun.COM data = (unsigned char *)nds->pdu_base_offset; 3585772Sas200622 data += pdu_offset; 3595772Sas200622 3608334SJose.Borrego@Sun.COM ndo_hexfmt((uint8_t *)buf, n_bytes, 0, hexbuf, NDOBUFSZ); 3615772Sas200622 3628334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "put %d@%-3d = %s", 3635772Sas200622 n_bytes, pdu_offset, hexbuf); 3645772Sas200622 3655772Sas200622 bcopy(buf, data, n_bytes); 3665772Sas200622 return (1); 3675772Sas200622 } 3685772Sas200622 3695772Sas200622 static void 3708334SJose.Borrego@Sun.COM ndo_tattle(ndr_stream_t *nds, char *what, ndr_ref_t *ref) 3715772Sas200622 { 3728334SJose.Borrego@Sun.COM ndo_printf(nds, ref, what); 3735772Sas200622 } 3745772Sas200622 3755772Sas200622 static void 3768334SJose.Borrego@Sun.COM ndo_tattle_error(ndr_stream_t *nds, ndr_ref_t *ref) 3775772Sas200622 { 3785772Sas200622 unsigned char *data; 3795772Sas200622 char hexbuf[NDOBUFSZ]; 3805772Sas200622 381*11963SAfshin.Ardakani@Sun.COM if (nds->pdu_base_addr != NULL) { 382*11963SAfshin.Ardakani@Sun.COM data = (unsigned char *)nds->pdu_base_offset; 383*11963SAfshin.Ardakani@Sun.COM if (ref) 384*11963SAfshin.Ardakani@Sun.COM data += ref->pdu_offset; 385*11963SAfshin.Ardakani@Sun.COM else 386*11963SAfshin.Ardakani@Sun.COM data += nds->pdu_scan_offset; 3875772Sas200622 388*11963SAfshin.Ardakani@Sun.COM ndo_hexfmt(data, 16, 0, hexbuf, NDOBUFSZ); 389*11963SAfshin.Ardakani@Sun.COM } else { 390*11963SAfshin.Ardakani@Sun.COM bzero(hexbuf, NDOBUFSZ); 391*11963SAfshin.Ardakani@Sun.COM } 3925772Sas200622 3938334SJose.Borrego@Sun.COM ndo_printf(nds, ref, "ERROR=%d REF=%d OFFSET=%d SIZE=%d/%d", 3948334SJose.Borrego@Sun.COM nds->error, nds->error_ref, nds->pdu_scan_offset, 3958334SJose.Borrego@Sun.COM nds->pdu_size, nds->pdu_max_size); 3968334SJose.Borrego@Sun.COM ndo_printf(nds, ref, " %s", hexbuf); 3975772Sas200622 } 3985772Sas200622 3995772Sas200622 /* 4008334SJose.Borrego@Sun.COM * ndo_reset 4015772Sas200622 * 4025772Sas200622 * Reset a stream: zap the outer_queue. We don't need to tamper 4035772Sas200622 * with the stream heap: it's handled externally to the stream. 4045772Sas200622 */ 4055772Sas200622 static int 4068334SJose.Borrego@Sun.COM ndo_reset(ndr_stream_t *nds) 4075772Sas200622 { 4088334SJose.Borrego@Sun.COM ndo_printf(nds, 0, "reset"); 4095772Sas200622 4108334SJose.Borrego@Sun.COM nds->pdu_size = 0; 4118334SJose.Borrego@Sun.COM nds->pdu_scan_offset = 0; 4128334SJose.Borrego@Sun.COM nds->outer_queue_head = 0; 4138334SJose.Borrego@Sun.COM nds->outer_current = 0; 4148334SJose.Borrego@Sun.COM nds->outer_queue_tailp = &nds->outer_queue_head; 4155772Sas200622 4165772Sas200622 return (1); 4175772Sas200622 } 4185772Sas200622 4195772Sas200622 /* 4208334SJose.Borrego@Sun.COM * ndo_destruct 4215772Sas200622 * 4226482Samw * Destruct a stream: zap the outer_queue. 4236482Samw * Note: heap management (creation/destruction) is external to the stream. 4245772Sas200622 */ 4255772Sas200622 static void 4268334SJose.Borrego@Sun.COM ndo_destruct(ndr_stream_t *nds) 4275772Sas200622 { 4286482Samw ndr_frag_t *frag; 4296482Samw 4308334SJose.Borrego@Sun.COM ndo_printf(nds, 0, "destruct"); 4318334SJose.Borrego@Sun.COM 4328334SJose.Borrego@Sun.COM if (nds == NULL) 4338334SJose.Borrego@Sun.COM return; 4345772Sas200622 4358334SJose.Borrego@Sun.COM if (nds->pdu_base_addr != NULL) { 4368334SJose.Borrego@Sun.COM free(nds->pdu_base_addr); 4378334SJose.Borrego@Sun.COM nds->pdu_base_addr = NULL; 4388334SJose.Borrego@Sun.COM nds->pdu_base_offset = 0; 4395772Sas200622 } 4405772Sas200622 4418334SJose.Borrego@Sun.COM while ((frag = nds->frags.head) != NULL) { 4428334SJose.Borrego@Sun.COM nds->frags.head = frag->next; 4436482Samw free(frag); 4446482Samw } 4456482Samw 4468334SJose.Borrego@Sun.COM bzero(&nds->frags, sizeof (ndr_fraglist_t)); 4477052Samw 4488334SJose.Borrego@Sun.COM nds->outer_queue_head = 0; 4498334SJose.Borrego@Sun.COM nds->outer_current = 0; 4508334SJose.Borrego@Sun.COM nds->outer_queue_tailp = &nds->outer_queue_head; 4515772Sas200622 } 4525772Sas200622 4535772Sas200622 /* 4545772Sas200622 * Printf style formatting for NDR operations. 4555772Sas200622 */ 4565772Sas200622 void 4578334SJose.Borrego@Sun.COM ndo_printf(ndr_stream_t *nds, ndr_ref_t *ref, const char *fmt, ...) 4585772Sas200622 { 4595772Sas200622 va_list ap; 4605772Sas200622 char buf[NDOBUFSZ]; 4615772Sas200622 4625772Sas200622 va_start(ap, fmt); 4635772Sas200622 (void) vsnprintf(buf, NDOBUFSZ, fmt, ap); 4645772Sas200622 va_end(ap); 4655772Sas200622 4668334SJose.Borrego@Sun.COM if (nds) 4678334SJose.Borrego@Sun.COM ndo_fmt(nds, ref, buf); 4685772Sas200622 else 4698334SJose.Borrego@Sun.COM ndo_trace(buf); 4705772Sas200622 } 4715772Sas200622 4725772Sas200622 /* 4735772Sas200622 * Main output formatter for NDR operations. 4745772Sas200622 * 4755772Sas200622 * UI 03 ... rpc_vers get 1@0 = 5 {05} 4765772Sas200622 * UI 03 ... rpc_vers_minor get 1@1 = 0 {00} 4775772Sas200622 * 4785772Sas200622 * U Marshalling flag (M=marshal, U=unmarshal) 4795772Sas200622 * I Direction flag (I=in, O=out) 4805772Sas200622 * ... Field name 4815772Sas200622 * get PDU operation (get or put) 4825772Sas200622 * 1@0 Bytes @ offset (i.e. 1 byte at offset 0) 4835772Sas200622 * {05} Value 4845772Sas200622 */ 4855772Sas200622 void 4868334SJose.Borrego@Sun.COM ndo_fmt(ndr_stream_t *nds, ndr_ref_t *ref, char *note) 4875772Sas200622 { 4888334SJose.Borrego@Sun.COM ndr_ref_t *p; 4898334SJose.Borrego@Sun.COM int indent; 4908334SJose.Borrego@Sun.COM char ref_name[NDOBUFSZ]; 4918334SJose.Borrego@Sun.COM char buf[NDOBUFSZ]; 4928334SJose.Borrego@Sun.COM int m_op_c = '?', dir_c = '?'; 4935772Sas200622 4948334SJose.Borrego@Sun.COM switch (nds->m_op) { 4955772Sas200622 case 0: m_op_c = '-'; break; 4965772Sas200622 case NDR_M_OP_MARSHALL: m_op_c = 'M'; break; 4975772Sas200622 case NDR_M_OP_UNMARSHALL: m_op_c = 'U'; break; 4985772Sas200622 default: m_op_c = '?'; break; 4995772Sas200622 } 5005772Sas200622 5018334SJose.Borrego@Sun.COM switch (nds->dir) { 5025772Sas200622 case 0: dir_c = '-'; break; 5035772Sas200622 case NDR_DIR_IN: dir_c = 'I'; break; 5045772Sas200622 case NDR_DIR_OUT: dir_c = 'O'; break; 5055772Sas200622 default: dir_c = '?'; break; 5065772Sas200622 } 5075772Sas200622 5085772Sas200622 for (indent = 0, p = ref; p; p = p->enclosing) 5095772Sas200622 indent++; 5105772Sas200622 5115772Sas200622 if (ref && ref->name) { 5125772Sas200622 if (*ref->name == '[' && ref->enclosing) { 5135772Sas200622 indent--; 5145772Sas200622 (void) snprintf(ref_name, NDOBUFSZ, "%s%s", 5155772Sas200622 ref->enclosing->name, ref->name); 5165772Sas200622 } else { 5175772Sas200622 (void) strlcpy(ref_name, ref->name, NDOBUFSZ); 5185772Sas200622 } 5195772Sas200622 } else { 5205772Sas200622 (void) strlcpy(ref_name, "----", NDOBUFSZ); 5215772Sas200622 } 5225772Sas200622 5239914Samw@Sun.COM (void) snprintf(buf, NDOBUFSZ, "%c%c %-.*s %-*s %s", 5249914Samw@Sun.COM m_op_c, dir_c, indent, 5255772Sas200622 "....+....+....+....+....+....", 5265772Sas200622 20 - indent, ref_name, note); 5275772Sas200622 5288334SJose.Borrego@Sun.COM ndo_trace(buf); 5295772Sas200622 } 5305772Sas200622 5315772Sas200622 /*ARGSUSED*/ 5325772Sas200622 void 5338334SJose.Borrego@Sun.COM ndo_trace(const char *s) 5345772Sas200622 { 5355772Sas200622 /* 5365772Sas200622 * Temporary fbt for dtrace until user space sdt enabled. 5375772Sas200622 */ 5385772Sas200622 } 5395772Sas200622 5405772Sas200622 /* 5415772Sas200622 * Format data as hex bytes (limit is 10 bytes): 5425772Sas200622 * 5435772Sas200622 * 1188689424 {10 f6 d9 46} 5445772Sas200622 * 5455772Sas200622 * If the input data is greater than 10 bytes, an ellipsis will 5465772Sas200622 * be inserted before the closing brace. 5475772Sas200622 */ 5485772Sas200622 static void 5498334SJose.Borrego@Sun.COM ndo_hexfmt(uint8_t *data, int size, int swap_bytes, char *buf, int len) 5505772Sas200622 { 5515772Sas200622 char *p = buf; 5525772Sas200622 int interp = 1; 5535772Sas200622 uint32_t c; 5545772Sas200622 int n; 5555772Sas200622 int i; 5565772Sas200622 5575772Sas200622 n = (size > 10) ? 10 : size; 5585772Sas200622 if (n > len-1) 5595772Sas200622 n = len-1; 5605772Sas200622 5615772Sas200622 switch (size) { 5625772Sas200622 case 1: 5635772Sas200622 c = *(uint8_t *)data; 5645772Sas200622 break; 5655772Sas200622 case 2: 5665772Sas200622 if (swap_bytes == 0) /*LINTED E_BAD_PTR_CAST_ALIGN*/ 5675772Sas200622 c = *(uint16_t *)data; 5685772Sas200622 else 5695772Sas200622 c = (data[0] << 8) | data[1]; 5705772Sas200622 break; 5715772Sas200622 case 4: 5725772Sas200622 if (swap_bytes == 0) { /*LINTED E_BAD_PTR_CAST_ALIGN*/ 5735772Sas200622 c = *(uint32_t *)data; 5745772Sas200622 } else { 5755772Sas200622 c = (data[0] << 24) | (data[1] << 16) 5765772Sas200622 | (data[2] << 8) | data[3]; 5775772Sas200622 } 5785772Sas200622 break; 5795772Sas200622 default: 5805772Sas200622 c = 0; 5815772Sas200622 interp = 0; 5825772Sas200622 break; 5835772Sas200622 } 5845772Sas200622 5855772Sas200622 if (interp) 5865772Sas200622 p += sprintf(p, "%4u {", c); 5875772Sas200622 else 5885772Sas200622 p += sprintf(p, " {"); 5895772Sas200622 5905772Sas200622 p += sprintf(p, "%02x", data[0]); 5915772Sas200622 for (i = 1; i < n; i++) 5925772Sas200622 p += sprintf(p, " %02x", data[i]); 5935772Sas200622 if (size > 10) 5945772Sas200622 p += sprintf(p, " ...}"); 5955772Sas200622 else 5965772Sas200622 p += sprintf(p, "}"); 5975772Sas200622 5985772Sas200622 /* 5995772Sas200622 * Show c if it's a printable character or wide-char. 6005772Sas200622 */ 6015772Sas200622 if (size < 4 && isprint((uint8_t)c)) 6025772Sas200622 (void) sprintf(p, " %c", (uint8_t)c); 6035772Sas200622 } 604