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