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