1dc20a302Sas200622 /* 2dc20a302Sas200622 * CDDL HEADER START 3dc20a302Sas200622 * 4dc20a302Sas200622 * The contents of this file are subject to the terms of the 5dc20a302Sas200622 * Common Development and Distribution License (the "License"). 6dc20a302Sas200622 * You may not use this file except in compliance with the License. 7dc20a302Sas200622 * 8dc20a302Sas200622 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9dc20a302Sas200622 * or http://www.opensolaris.org/os/licensing. 10dc20a302Sas200622 * See the License for the specific language governing permissions 11dc20a302Sas200622 * and limitations under the License. 12dc20a302Sas200622 * 13dc20a302Sas200622 * When distributing Covered Code, include this CDDL HEADER in each 14dc20a302Sas200622 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15dc20a302Sas200622 * If applicable, add the following below this CDDL HEADER, with the 16dc20a302Sas200622 * fields enclosed by brackets "[]" replaced with your own identifying 17dc20a302Sas200622 * information: Portions Copyright [yyyy] [name of copyright owner] 18dc20a302Sas200622 * 19dc20a302Sas200622 * CDDL HEADER END 20dc20a302Sas200622 */ 21dc20a302Sas200622 /* 227f667e74Sjose borrego * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23dc20a302Sas200622 * Use is subject to license terms. 24dc20a302Sas200622 */ 25dc20a302Sas200622 26dc20a302Sas200622 /* 278d7e4166Sjose borrego * NDR heap management. The heap is used for temporary storage by 28dc20a302Sas200622 * both the client and server side library routines. In order to 29dc20a302Sas200622 * support the different requirements of the various RPCs, the heap 30dc20a302Sas200622 * can grow dynamically if required. We start with a single block 31dc20a302Sas200622 * and perform sub-allocations from it. If an RPC requires more space 32dc20a302Sas200622 * we will continue to add it a block at a time. This means that we 33dc20a302Sas200622 * don't hog lots of memory on every call to support the few times 34dc20a302Sas200622 * that we actually need a lot heap space. 35dc20a302Sas200622 * 36dc20a302Sas200622 * Note that there is no individual free function. Once space has been 37dc20a302Sas200622 * allocated, it remains allocated until the heap is destroyed. This 38dc20a302Sas200622 * shouldn't be an issue because the heap is being filled with data to 39dc20a302Sas200622 * be marshalled or unmarshalled and we need it all to be there until 40dc20a302Sas200622 * the point that the entire heap is no longer required. 41dc20a302Sas200622 */ 42dc20a302Sas200622 43dc20a302Sas200622 #include <sys/errno.h> 44dc20a302Sas200622 #include <stdlib.h> 45dc20a302Sas200622 #include <string.h> 46dc20a302Sas200622 #include <strings.h> 47dc20a302Sas200622 #include <sys/uio.h> 48dc20a302Sas200622 49dc20a302Sas200622 #include <smbsrv/libsmb.h> 508d7e4166Sjose borrego #include <smbsrv/libmlrpc.h> 518d7e4166Sjose borrego #include <smbsrv/smb_sid.h> 52dc20a302Sas200622 53dc20a302Sas200622 /* 54dc20a302Sas200622 * Allocate a heap structure and the first heap block. For many RPC 55dc20a302Sas200622 * operations this will be the only time we need to malloc memory 56dc20a302Sas200622 * in this instance of the heap. The only point of note here is that 57dc20a302Sas200622 * we put the heap management data in the first block to avoid a 588d7e4166Sjose borrego * second malloc. Make sure that sizeof(ndr_heap_t) is smaller 598d7e4166Sjose borrego * than NDR_HEAP_BLKSZ. 60dc20a302Sas200622 * 61dc20a302Sas200622 * Note that the heap management data is at the start of the first block. 62dc20a302Sas200622 * 63dc20a302Sas200622 * Returns a pointer to the newly created heap, which is used like an 64dc20a302Sas200622 * opaque handle with the rest of the heap management interface.. 65dc20a302Sas200622 */ 668d7e4166Sjose borrego ndr_heap_t * 678d7e4166Sjose borrego ndr_heap_create(void) 68dc20a302Sas200622 { 698d7e4166Sjose borrego ndr_heap_t *heap; 70dc20a302Sas200622 char *base; 717f667e74Sjose borrego size_t allocsize = sizeof (ndr_heap_t) + NDR_HEAP_BLKSZ; 72dc20a302Sas200622 737f667e74Sjose borrego if ((heap = malloc(allocsize)) == NULL) 74dc20a302Sas200622 return (NULL); 75dc20a302Sas200622 767f667e74Sjose borrego base = (char *)heap; 778d7e4166Sjose borrego bzero(heap, sizeof (ndr_heap_t)); 78dc20a302Sas200622 798d7e4166Sjose borrego heap->iovcnt = NDR_HEAP_MAXIOV; 80dc20a302Sas200622 heap->iov = heap->iovec; 81dc20a302Sas200622 heap->iov->iov_base = base; 828d7e4166Sjose borrego heap->iov->iov_len = sizeof (ndr_heap_t); 837f667e74Sjose borrego heap->top = base + allocsize; 848d7e4166Sjose borrego heap->next = base + sizeof (ndr_heap_t); 85dc20a302Sas200622 86dc20a302Sas200622 return (heap); 87dc20a302Sas200622 } 88dc20a302Sas200622 89dc20a302Sas200622 /* 90dc20a302Sas200622 * Deallocate all of the memory associated with a heap. This is the 91dc20a302Sas200622 * only way to deallocate heap memory, it isn't possible to free the 92dc20a302Sas200622 * space obtained by individual malloc calls. 93dc20a302Sas200622 * 94dc20a302Sas200622 * Note that the first block contains the heap management data, which 95dc20a302Sas200622 * is deleted last. 96dc20a302Sas200622 */ 97dc20a302Sas200622 void 988d7e4166Sjose borrego ndr_heap_destroy(ndr_heap_t *heap) 99dc20a302Sas200622 { 100dc20a302Sas200622 int i; 101dc20a302Sas200622 char *p; 102dc20a302Sas200622 103dc20a302Sas200622 if (heap) { 1048d7e4166Sjose borrego for (i = 1; i < NDR_HEAP_MAXIOV; ++i) { 105dc20a302Sas200622 if ((p = heap->iovec[i].iov_base) != NULL) 106dc20a302Sas200622 free(p); 107dc20a302Sas200622 } 108dc20a302Sas200622 109dc20a302Sas200622 free(heap); 110dc20a302Sas200622 } 111dc20a302Sas200622 } 112dc20a302Sas200622 113dc20a302Sas200622 /* 114dc20a302Sas200622 * Allocate space in the specified heap. All requests are padded, if 115dc20a302Sas200622 * required, to ensure dword alignment. If the current iov will be 116dc20a302Sas200622 * exceeded, we allocate a new block and setup the next iov. Otherwise 117dc20a302Sas200622 * all we have to do is move the next pointer and update the current 118dc20a302Sas200622 * iov length. 119dc20a302Sas200622 * 120dc20a302Sas200622 * On success, a pointer to the allocated (dword aligned) area is 121dc20a302Sas200622 * returned. Otherwise a null pointer is returned. 122dc20a302Sas200622 */ 123dc20a302Sas200622 void * 1248d7e4166Sjose borrego ndr_heap_malloc(ndr_heap_t *heap, unsigned size) 125dc20a302Sas200622 { 126dc20a302Sas200622 char *p; 127dc20a302Sas200622 int incr_size; 128dc20a302Sas200622 1298d7e4166Sjose borrego size += NDR_ALIGN4(size); 130dc20a302Sas200622 131dc20a302Sas200622 if (heap == NULL || size == 0) 132dc20a302Sas200622 return (NULL); 133dc20a302Sas200622 134dc20a302Sas200622 p = heap->next; 135dc20a302Sas200622 136dc20a302Sas200622 if (p + size > heap->top) { 137dc20a302Sas200622 if ((heap->iovcnt == 0) || ((--heap->iovcnt) == 0)) 138dc20a302Sas200622 return (NULL); 139dc20a302Sas200622 1408d7e4166Sjose borrego incr_size = (size < NDR_HEAP_BLKSZ) ? NDR_HEAP_BLKSZ : size; 141dc20a302Sas200622 142dc20a302Sas200622 if ((p = (char *)malloc(incr_size)) == NULL) 143dc20a302Sas200622 return (NULL); 144dc20a302Sas200622 145dc20a302Sas200622 ++heap->iov; 146dc20a302Sas200622 heap->iov->iov_base = p; 147dc20a302Sas200622 heap->iov->iov_len = 0; 148dc20a302Sas200622 heap->top = p + incr_size; 149dc20a302Sas200622 } 150dc20a302Sas200622 151dc20a302Sas200622 heap->next = p + size; 152dc20a302Sas200622 heap->iov->iov_len += size; 153dc20a302Sas200622 return ((void *)p); 154dc20a302Sas200622 } 155dc20a302Sas200622 156dc20a302Sas200622 /* 157dc20a302Sas200622 * Convenience function to do heap strdup. 158dc20a302Sas200622 */ 159dc20a302Sas200622 void * 1608d7e4166Sjose borrego ndr_heap_strdup(ndr_heap_t *heap, const char *s) 161dc20a302Sas200622 { 162dc20a302Sas200622 int len; 163dc20a302Sas200622 void *p; 164dc20a302Sas200622 165dc20a302Sas200622 if (s == NULL) 166dc20a302Sas200622 return (NULL); 167dc20a302Sas200622 168dc20a302Sas200622 /* 169dc20a302Sas200622 * We don't need to clutter the heap with empty strings. 170dc20a302Sas200622 */ 171dc20a302Sas200622 if ((len = strlen(s)) == 0) 172dc20a302Sas200622 return (""); 173dc20a302Sas200622 1748d7e4166Sjose borrego if ((p = ndr_heap_malloc(heap, len+1)) != NULL) 175dc20a302Sas200622 (void) strcpy((char *)p, s); 176dc20a302Sas200622 177dc20a302Sas200622 return (p); 178dc20a302Sas200622 } 179dc20a302Sas200622 180dc20a302Sas200622 /* 1818d7e4166Sjose borrego * Make an ndr_mstring_t from a regular string. 1828d7e4166Sjose borrego */ 1838d7e4166Sjose borrego int 1848d7e4166Sjose borrego ndr_heap_mstring(ndr_heap_t *heap, const char *s, ndr_mstring_t *out) 1858d7e4166Sjose borrego { 1868d7e4166Sjose borrego if (s == NULL || out == NULL) 1878d7e4166Sjose borrego return (-1); 1888d7e4166Sjose borrego 189*bbf6f00cSJordan Brown out->length = smb_wcequiv_strlen(s); 190*bbf6f00cSJordan Brown out->allosize = out->length + sizeof (smb_wchar_t); 1918d7e4166Sjose borrego 1928d7e4166Sjose borrego if ((out->str = ndr_heap_strdup(heap, s)) == NULL) 1938d7e4166Sjose borrego return (-1); 1948d7e4166Sjose borrego 1958d7e4166Sjose borrego return (0); 1968d7e4166Sjose borrego } 1978d7e4166Sjose borrego 1988d7e4166Sjose borrego /* 199dc20a302Sas200622 * Our regular string marshalling always creates null terminated strings 200dc20a302Sas200622 * but some Windows clients and servers are pedantic about the string 201dc20a302Sas200622 * formats they will accept and require non-null terminated strings. 202dc20a302Sas200622 * This function can be used to build a wide-char, non-null terminated 203dc20a302Sas200622 * string in the heap as a varying/conformant array. We need to do the 204dc20a302Sas200622 * wide-char conversion here because the marshalling code won't be 205dc20a302Sas200622 * aware that this is really a string. 206dc20a302Sas200622 */ 207dc20a302Sas200622 void 2088d7e4166Sjose borrego ndr_heap_mkvcs(ndr_heap_t *heap, char *s, ndr_vcstr_t *vc) 209dc20a302Sas200622 { 210dc20a302Sas200622 int mlen; 211dc20a302Sas200622 212*bbf6f00cSJordan Brown vc->wclen = smb_wcequiv_strlen(s); 2132c1b14e5Sjose borrego vc->wcsize = vc->wclen; 214dc20a302Sas200622 215*bbf6f00cSJordan Brown mlen = sizeof (ndr_vcs_t) + vc->wcsize + sizeof (smb_wchar_t); 216dc20a302Sas200622 2178d7e4166Sjose borrego vc->vcs = ndr_heap_malloc(heap, mlen); 218dc20a302Sas200622 2192c1b14e5Sjose borrego if (vc->vcs) { 2202c1b14e5Sjose borrego vc->vcs->vc_first_is = 0; 221*bbf6f00cSJordan Brown vc->vcs->vc_length_is = vc->wclen / sizeof (smb_wchar_t); 222*bbf6f00cSJordan Brown (void) smb_mbstowcs((smb_wchar_t *)vc->vcs->buffer, s, 2232c1b14e5Sjose borrego vc->vcs->vc_length_is); 2242c1b14e5Sjose borrego } 2252c1b14e5Sjose borrego } 2262c1b14e5Sjose borrego 2272c1b14e5Sjose borrego void 2288d7e4166Sjose borrego ndr_heap_mkvcb(ndr_heap_t *heap, uint8_t *data, uint32_t datalen, 2298d7e4166Sjose borrego ndr_vcbuf_t *vcbuf) 2302c1b14e5Sjose borrego { 2312c1b14e5Sjose borrego int mlen; 2322c1b14e5Sjose borrego 2332c1b14e5Sjose borrego if (data == NULL || datalen == 0) { 2348d7e4166Sjose borrego bzero(vcbuf, sizeof (ndr_vcbuf_t)); 2352c1b14e5Sjose borrego return; 2362c1b14e5Sjose borrego } 2372c1b14e5Sjose borrego 2382c1b14e5Sjose borrego vcbuf->len = datalen; 2392c1b14e5Sjose borrego vcbuf->size = datalen; 2402c1b14e5Sjose borrego 2418d7e4166Sjose borrego mlen = sizeof (ndr_vcbuf_t) + datalen; 2422c1b14e5Sjose borrego 2438d7e4166Sjose borrego vcbuf->vcb = ndr_heap_malloc(heap, mlen); 2442c1b14e5Sjose borrego 2452c1b14e5Sjose borrego if (vcbuf->vcb) { 2462c1b14e5Sjose borrego vcbuf->vcb->vc_first_is = 0; 2472c1b14e5Sjose borrego vcbuf->vcb->vc_length_is = datalen; 2482c1b14e5Sjose borrego bcopy(data, vcbuf->vcb->buffer, datalen); 249dc20a302Sas200622 } 250dc20a302Sas200622 } 251dc20a302Sas200622 2528d7e4166Sjose borrego /* 2538d7e4166Sjose borrego * Duplcate a SID in the heap. 2548d7e4166Sjose borrego */ 2558d7e4166Sjose borrego smb_sid_t * 2568d7e4166Sjose borrego ndr_heap_siddup(ndr_heap_t *heap, smb_sid_t *sid) 2578d7e4166Sjose borrego { 2588d7e4166Sjose borrego smb_sid_t *new_sid; 2598d7e4166Sjose borrego unsigned size; 2608d7e4166Sjose borrego 2618d7e4166Sjose borrego if (sid == NULL) 2628d7e4166Sjose borrego return (NULL); 2638d7e4166Sjose borrego 2648d7e4166Sjose borrego size = smb_sid_len(sid); 2658d7e4166Sjose borrego 2668d7e4166Sjose borrego if ((new_sid = ndr_heap_malloc(heap, size)) == NULL) 2678d7e4166Sjose borrego return (NULL); 2688d7e4166Sjose borrego 2698d7e4166Sjose borrego bcopy(sid, new_sid, size); 2708d7e4166Sjose borrego return (new_sid); 2718d7e4166Sjose borrego } 2728d7e4166Sjose borrego 273dc20a302Sas200622 int 2748d7e4166Sjose borrego ndr_heap_used(ndr_heap_t *heap) 275dc20a302Sas200622 { 276dc20a302Sas200622 int used = 0; 277dc20a302Sas200622 int i; 278dc20a302Sas200622 2798d7e4166Sjose borrego for (i = 0; i < NDR_HEAP_MAXIOV; ++i) 280dc20a302Sas200622 used += heap->iovec[i].iov_len; 281dc20a302Sas200622 282dc20a302Sas200622 return (used); 283dc20a302Sas200622 } 284dc20a302Sas200622 285dc20a302Sas200622 int 2868d7e4166Sjose borrego ndr_heap_avail(ndr_heap_t *heap) 287dc20a302Sas200622 { 288dc20a302Sas200622 int avail; 289dc20a302Sas200622 int count; 290dc20a302Sas200622 291dc20a302Sas200622 count = (heap->iovcnt == 0) ? 0 : (heap->iovcnt - 1); 292dc20a302Sas200622 2938d7e4166Sjose borrego avail = count * NDR_HEAP_BLKSZ; 294dc20a302Sas200622 avail += (heap->top - heap->next); 295dc20a302Sas200622 296dc20a302Sas200622 return (avail); 297dc20a302Sas200622 } 298