xref: /titanic_52/usr/src/lib/smbsrv/libmlrpc/common/ndr_marshal.c (revision 2f172c55ef76964744bc62b4500ece87f3089b4d)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <assert.h>
27 #include <strings.h>
28 #include <sys/param.h>
29 
30 #include <smbsrv/libsmb.h>
31 #include <smbsrv/libmlrpc.h>
32 
33 #ifdef _BIG_ENDIAN
34 static const int ndr_native_byte_order = NDR_REPLAB_INTG_BIG_ENDIAN;
35 #else
36 static const int ndr_native_byte_order = NDR_REPLAB_INTG_LITTLE_ENDIAN;
37 #endif
38 
39 static int ndr_decode_hdr_common(ndr_stream_t *, ndr_common_header_t *);
40 
41 static int
42 ndr_encode_decode_common(ndr_stream_t *nds, unsigned opnum,
43     ndr_typeinfo_t *ti, void *datum)
44 {
45 	int rc;
46 
47 	/*
48 	 * Perform the (un)marshalling
49 	 */
50 	if (ndo_operation(nds, ti, opnum, datum))
51 		return (NDR_DRC_OK);
52 
53 	switch (nds->error) {
54 	case NDR_ERR_MALLOC_FAILED:
55 		rc = NDR_DRC_FAULT_OUT_OF_MEMORY;
56 		break;
57 
58 	case NDR_ERR_SWITCH_VALUE_INVALID:
59 		rc = NDR_DRC_FAULT_PARAM_0_INVALID;
60 		break;
61 
62 	case NDR_ERR_UNDERFLOW:
63 		rc = NDR_DRC_FAULT_RECEIVED_RUNT;
64 		break;
65 
66 	case NDR_ERR_GROW_FAILED:
67 		rc = NDR_DRC_FAULT_ENCODE_TOO_BIG;
68 		break;
69 
70 	default:
71 		if (nds->m_op == NDR_M_OP_MARSHALL)
72 			rc = NDR_DRC_FAULT_ENCODE_FAILED;
73 		else
74 			rc = NDR_DRC_FAULT_DECODE_FAILED;
75 		break;
76 	}
77 
78 	return (rc);
79 }
80 
81 ndr_buf_t *
82 ndr_buf_init(ndr_typeinfo_t *ti)
83 {
84 	ndr_buf_t		*nbuf;
85 
86 	if ((nbuf = calloc(1, sizeof (ndr_buf_t))) == NULL)
87 		return (NULL);
88 
89 	if ((nbuf->nb_heap = ndr_heap_create()) == NULL) {
90 		free(nbuf);
91 		return (NULL);
92 	}
93 
94 	nbuf->nb_ti = ti;
95 	nbuf->nb_magic = NDR_BUF_MAGIC;
96 	return (nbuf);
97 }
98 
99 void
100 ndr_buf_fini(ndr_buf_t *nbuf)
101 {
102 	assert(nbuf->nb_magic == NDR_BUF_MAGIC);
103 
104 	nds_destruct(&nbuf->nb_nds);
105 	ndr_heap_destroy(nbuf->nb_heap);
106 	nbuf->nb_magic = 0;
107 	free(nbuf);
108 }
109 
110 /*
111  * Decode an NDR encoded buffer.  The buffer is expected to contain
112  * a single fragment packet with a valid PDU header followed by NDR
113  * encoded data.  The structure to which result points should be
114  * of the appropriate type to hold the decoded output.  For example:
115  *
116  *	pac_info_t info;
117  *
118  * 	if ((nbuf = ndr_buf_init(&TYPEINFO(ndr_pac)) != NULL) {
119  *		rc = ndr_decode_buf(nbuf, opnum, data, datalen, &info);
120  *		...
121  *		ndr_buf_fini(nbuf);
122  *	}
123  */
124 int
125 ndr_buf_decode(ndr_buf_t *nbuf, unsigned opnum, const char *data,
126     size_t datalen, void *result)
127 {
128 	ndr_common_header_t	hdr;
129 	unsigned		pdu_size_hint;
130 	int			rc;
131 
132 	assert(nbuf->nb_magic == NDR_BUF_MAGIC);
133 	assert(nbuf->nb_heap != NULL);
134 	assert(nbuf->nb_ti != NULL);
135 
136 	if (datalen < NDR_PDU_SIZE_HINT_DEFAULT)
137 		pdu_size_hint = NDR_PDU_SIZE_HINT_DEFAULT;
138 	else
139 		pdu_size_hint = datalen;
140 
141 	nds_initialize(&nbuf->nb_nds, pdu_size_hint, NDR_MODE_BUF_DECODE,
142 	    nbuf->nb_heap);
143 	bcopy(data, nbuf->nb_nds.pdu_base_addr, datalen);
144 
145 	rc = ndr_decode_hdr_common(&nbuf->nb_nds, &hdr);
146 	if (NDR_DRC_IS_FAULT(rc))
147 		return (rc);
148 
149 	if (!NDR_IS_SINGLE_FRAG(hdr.pfc_flags))
150 		return (rc);
151 
152 	rc = ndr_encode_decode_common(&nbuf->nb_nds, opnum, nbuf->nb_ti,
153 	    result);
154 	return (rc);
155 }
156 
157 /*
158  * Use the receive stream to unmarshall data (NDR_MODE_CALL_RECV).
159  */
160 int
161 ndr_decode_call(ndr_xa_t *mxa, void *params)
162 {
163 	ndr_stream_t	*nds = &mxa->recv_nds;
164 	int		rc;
165 
166 	if (!NDR_MODE_MATCH(nds, NDR_MODE_CALL_RECV))
167 		return (NDR_DRC_FAULT_MODE_MISMATCH);
168 
169 	rc = ndr_encode_decode_common(nds, mxa->opnum,
170 	    mxa->binding->service->interface_ti, params);
171 
172 	return (rc + NDR_PTYPE_REQUEST);
173 }
174 
175 /*
176  * Use the send stream to marshall data (NDR_MODE_RETURN_SEND).
177  */
178 int
179 ndr_encode_return(ndr_xa_t *mxa, void *params)
180 {
181 	ndr_stream_t	*nds = &mxa->send_nds;
182 	int		rc;
183 
184 	if (!NDR_MODE_MATCH(nds, NDR_MODE_RETURN_SEND))
185 		return (NDR_DRC_FAULT_MODE_MISMATCH);
186 
187 	rc = ndr_encode_decode_common(nds, mxa->opnum,
188 	    mxa->binding->service->interface_ti, params);
189 
190 	return (rc + NDR_PTYPE_RESPONSE);
191 }
192 
193 /*
194  * Use the send stream to marshall data (NDR_MODE_CALL_SEND).
195  */
196 int
197 ndr_encode_call(ndr_xa_t *mxa, void *params)
198 {
199 	ndr_stream_t	*nds = &mxa->send_nds;
200 	int		rc;
201 
202 	if (!NDR_MODE_MATCH(nds, NDR_MODE_CALL_SEND))
203 		return (NDR_DRC_FAULT_MODE_MISMATCH);
204 
205 	rc = ndr_encode_decode_common(nds, mxa->opnum,
206 	    mxa->binding->service->interface_ti, params);
207 
208 	return (rc + NDR_PTYPE_REQUEST);
209 }
210 
211 /*
212  * Use the receive stream to unmarshall data (NDR_MODE_RETURN_RECV).
213  */
214 int
215 ndr_decode_return(ndr_xa_t *mxa, void *params)
216 {
217 	ndr_stream_t	*nds = &mxa->recv_nds;
218 	int		rc;
219 
220 	if (!NDR_MODE_MATCH(nds, NDR_MODE_RETURN_RECV))
221 		return (NDR_DRC_FAULT_MODE_MISMATCH);
222 
223 	rc = ndr_encode_decode_common(nds, mxa->opnum,
224 	    mxa->binding->service->interface_ti, params);
225 
226 	return (rc + NDR_PTYPE_RESPONSE);
227 }
228 
229 int
230 ndr_decode_pdu_hdr(ndr_xa_t *mxa)
231 {
232 	ndr_common_header_t	*hdr = &mxa->recv_hdr.common_hdr;
233 	ndr_stream_t		*nds = &mxa->recv_nds;
234 	int			rc;
235 
236 	rc = ndr_decode_hdr_common(nds, hdr);
237 	if (NDR_DRC_IS_FAULT(rc))
238 		return (rc);
239 
240 	/*
241 	 * Verify the protocol version.
242 	 */
243 	if ((hdr->rpc_vers != 5) || (hdr->rpc_vers_minor != 0))
244 		return (NDR_DRC_PTYPE_RPCHDR(NDR_DRC_FAULT_DECODE_FAILED));
245 
246 	mxa->ptype = hdr->ptype;
247 	return (NDR_DRC_OK);
248 }
249 
250 static int
251 ndr_decode_hdr_common(ndr_stream_t *nds, ndr_common_header_t *hdr)
252 {
253 	int			ptype;
254 	int			rc;
255 	int			charset;
256 	int			byte_order;
257 
258 	if (nds->m_op != NDR_M_OP_UNMARSHALL)
259 		return (NDR_DRC_PTYPE_RPCHDR(NDR_DRC_FAULT_MODE_MISMATCH));
260 
261 	/*
262 	 * All PDU headers are at least this big
263 	 */
264 	rc = NDS_GROW_PDU(nds, sizeof (ndr_common_header_t), 0);
265 	if (!rc)
266 		return (NDR_DRC_PTYPE_RPCHDR(NDR_DRC_FAULT_RECEIVED_RUNT));
267 
268 	/*
269 	 * Peek at the first eight bytes to figure out what we're doing.
270 	 */
271 	rc = NDS_GET_PDU(nds, 0, 8, (char *)hdr, 0, 0);
272 	if (!rc)
273 		return (NDR_DRC_PTYPE_RPCHDR(NDR_DRC_FAULT_DECODE_FAILED));
274 
275 	/*
276 	 * Check for ASCII as the character set.  This is an ASCII
277 	 * versus EBCDIC option and has nothing to do with Unicode.
278 	 */
279 	charset = hdr->packed_drep.intg_char_rep & NDR_REPLAB_CHAR_MASK;
280 	if (charset != NDR_REPLAB_CHAR_ASCII)
281 		return (NDR_DRC_PTYPE_RPCHDR(NDR_DRC_FAULT_DECODE_FAILED));
282 
283 	/*
284 	 * Set the byte swap flag if the PDU byte-order
285 	 * is different from the local byte-order.
286 	 */
287 	byte_order = hdr->packed_drep.intg_char_rep & NDR_REPLAB_INTG_MASK;
288 	nds->swap = (byte_order != ndr_native_byte_order) ? 1 : 0;
289 
290 	ptype = hdr->ptype;
291 	if (ptype == NDR_PTYPE_REQUEST &&
292 	    (hdr->pfc_flags & NDR_PFC_OBJECT_UUID) != 0) {
293 		ptype = NDR_PTYPE_REQUEST_WITH;	/* fake for sizing */
294 	}
295 
296 	rc = ndr_encode_decode_common(nds, ptype, &TYPEINFO(ndr_hdr), hdr);
297 
298 	return (NDR_DRC_PTYPE_RPCHDR(rc));
299 }
300 
301 /*
302  * Decode an RPC fragment header.  Use ndr_decode_pdu_hdr() to process
303  * the first fragment header then this function to process additional
304  * fragment headers.
305  */
306 void
307 ndr_decode_frag_hdr(ndr_stream_t *nds, ndr_common_header_t *hdr)
308 {
309 	ndr_common_header_t *tmp;
310 	uint8_t *pdu;
311 	int byte_order;
312 
313 	pdu = (uint8_t *)nds->pdu_base_offset + nds->pdu_scan_offset;
314 	bcopy(pdu, hdr, NDR_RSP_HDR_SIZE);
315 
316 	/*
317 	 * Swap non-byte fields if the PDU byte-order
318 	 * is different from the local byte-order.
319 	 */
320 	byte_order = hdr->packed_drep.intg_char_rep & NDR_REPLAB_INTG_MASK;
321 
322 	if (byte_order != ndr_native_byte_order) {
323 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
324 		tmp = (ndr_common_header_t *)pdu;
325 
326 		nds_bswap(&tmp->frag_length, &hdr->frag_length,
327 		    sizeof (WORD));
328 		nds_bswap(&tmp->auth_length, &hdr->auth_length,
329 		    sizeof (WORD));
330 		nds_bswap(&tmp->call_id, &hdr->call_id, sizeof (DWORD));
331 	}
332 }
333 
334 void
335 ndr_show_hdr(ndr_common_header_t *hdr)
336 {
337 	char	*fragtype;
338 
339 	if (hdr == NULL) {
340 		ndo_printf(NULL, NULL, "ndr hdr: <null>");
341 		return;
342 	}
343 
344 	if (NDR_IS_SINGLE_FRAG(hdr->pfc_flags))
345 		fragtype = "single";
346 	else if (NDR_IS_FIRST_FRAG(hdr->pfc_flags))
347 		fragtype = "first";
348 	else if (NDR_IS_LAST_FRAG(hdr->pfc_flags))
349 		fragtype = "last";
350 	else
351 		fragtype = "intermediate";
352 
353 	ndo_printf(NULL, NULL,
354 	    "ndr hdr: %d.%d ptype=%d, %s frag (flags=0x%08x) len=%d",
355 	    hdr->rpc_vers, hdr->rpc_vers_minor, hdr->ptype,
356 	    fragtype, hdr->pfc_flags, hdr->frag_length);
357 }
358 
359 int
360 ndr_encode_pdu_hdr(ndr_xa_t *mxa)
361 {
362 	ndr_common_header_t	*hdr = &mxa->send_hdr.common_hdr;
363 	ndr_stream_t		*nds = &mxa->send_nds;
364 	int			ptype;
365 	int			rc;
366 
367 	if (nds->m_op != NDR_M_OP_MARSHALL)
368 		return (NDR_DRC_PTYPE_RPCHDR(NDR_DRC_FAULT_MODE_MISMATCH));
369 
370 	ptype = hdr->ptype;
371 	if (ptype == NDR_PTYPE_REQUEST &&
372 	    (hdr->pfc_flags & NDR_PFC_OBJECT_UUID) != 0) {
373 		ptype = NDR_PTYPE_REQUEST_WITH;	/* fake for sizing */
374 	}
375 
376 	rc = ndr_encode_decode_common(nds, ptype, &TYPEINFO(ndr_hdr), hdr);
377 
378 	return (NDR_DRC_PTYPE_RPCHDR(rc));
379 }
380 
381 /*
382  * This is a hand-coded derivative of the automatically generated
383  * (un)marshalling routine for bind_ack headers. bind_ack headers
384  * have an interior conformant array, which is inconsistent with
385  * IDL/NDR rules.
386  */
387 extern struct ndr_typeinfo ndt__uchar;
388 extern struct ndr_typeinfo ndt__ushort;
389 extern struct ndr_typeinfo ndt__ulong;
390 
391 int ndr__ndr_bind_ack_hdr(ndr_ref_t *encl_ref);
392 ndr_typeinfo_t ndt__ndr_bind_ack_hdr = {
393     1,		/* NDR version */
394     3,		/* alignment */
395     NDR_F_STRUCT,	/* flags */
396     ndr__ndr_bind_ack_hdr,	/* ndr_func */
397     68,		/* pdu_size_fixed_part */
398     0,		/* pdu_size_variable_part */
399     68,		/* c_size_fixed_part */
400     0,		/* c_size_variable_part */
401 };
402 
403 /*
404  * [_no_reorder]
405  */
406 int
407 ndr__ndr_bind_ack_hdr(ndr_ref_t *encl_ref)
408 {
409 	ndr_stream_t		*nds = encl_ref->stream;
410 	struct ndr_bind_ack_hdr	*val = /*LINTED E_BAD_PTR_CAST_ALIGN*/
411 	    (struct ndr_bind_ack_hdr *)encl_ref->datum;
412 	ndr_ref_t		myref;
413 	unsigned long		offset;
414 
415 	bzero(&myref, sizeof (myref));
416 	myref.enclosing = encl_ref;
417 	myref.stream = encl_ref->stream;
418 	myref.packed_alignment = 0;
419 
420 	/* do all members in order */
421 	NDR_MEMBER(_ndr_common_header, common_hdr, 0UL);
422 	NDR_MEMBER(_ushort, max_xmit_frag, 16UL);
423 	NDR_MEMBER(_ushort, max_recv_frag, 18UL);
424 	NDR_MEMBER(_ulong, assoc_group_id, 20UL);
425 
426 	/* port any is the conformant culprit */
427 	offset = 24UL;
428 
429 	switch (nds->m_op) {
430 	case NDR_M_OP_MARSHALL:
431 		val->sec_addr.length =
432 		    strlen((char *)val->sec_addr.port_spec) + 1;
433 		break;
434 
435 	case NDR_M_OP_UNMARSHALL:
436 		break;
437 
438 	default:
439 		NDR_SET_ERROR(encl_ref, NDR_ERR_M_OP_INVALID);
440 		return (0);
441 	}
442 
443 	NDR_MEMBER(_ushort, sec_addr.length, offset);
444 	NDR_MEMBER_ARR_WITH_DIMENSION(_uchar, sec_addr.port_spec,
445 	    offset+2UL, val->sec_addr.length);
446 
447 	offset += 2;
448 	offset += val->sec_addr.length;
449 	offset += NDR_ALIGN4(offset);
450 
451 	NDR_MEMBER(_ndr_p_result_list, p_result_list, offset);
452 	return (1);
453 }
454 
455 /*
456  * Assume a single presentation context element in the result list.
457  */
458 unsigned
459 ndr_bind_ack_hdr_size(ndr_xa_t *mxa)
460 {
461 	ndr_bind_ack_hdr_t *bahdr = &mxa->send_hdr.bind_ack_hdr;
462 	unsigned	offset;
463 	unsigned	length;
464 
465 	/* port any is the conformant culprit */
466 	offset = 24UL;
467 
468 	length = strlen((char *)bahdr->sec_addr.port_spec) + 1;
469 
470 	offset += 2;
471 	offset += length;
472 	offset += NDR_ALIGN4(offset);
473 	offset += sizeof (ndr_p_result_list_t);
474 	return (offset);
475 }
476 
477 /*
478  * This is a hand-coded derivative of the automatically generated
479  * (un)marshalling routine for alter_context_rsp headers.
480  * Alter context response headers have an interior conformant array,
481  * which is inconsistent with IDL/NDR rules.
482  */
483 int ndr__ndr_alter_context_rsp_hdr(ndr_ref_t *encl_ref);
484 ndr_typeinfo_t ndt__ndr_alter_context_rsp_hdr = {
485     1,			/* NDR version */
486     3,			/* alignment */
487     NDR_F_STRUCT,	/* flags */
488     ndr__ndr_alter_context_rsp_hdr,	/* ndr_func */
489     56,			/* pdu_size_fixed_part */
490     0,			/* pdu_size_variable_part */
491     56,			/* c_size_fixed_part */
492     0,			/* c_size_variable_part */
493 };
494 
495 /*
496  * [_no_reorder]
497  */
498 int
499 ndr__ndr_alter_context_rsp_hdr(ndr_ref_t *encl_ref)
500 {
501 	ndr_stream_t		*nds = encl_ref->stream;
502 	ndr_alter_context_rsp_hdr_t *val = /*LINTED E_BAD_PTR_CAST_ALIGN*/
503 	    (ndr_alter_context_rsp_hdr_t *)encl_ref->datum;
504 	ndr_ref_t		myref;
505 	unsigned long		offset;
506 
507 	bzero(&myref, sizeof (myref));
508 	myref.enclosing = encl_ref;
509 	myref.stream = encl_ref->stream;
510 	myref.packed_alignment = 0;
511 
512 	/* do all members in order */
513 	NDR_MEMBER(_ndr_common_header, common_hdr, 0UL);
514 	NDR_MEMBER(_ushort, max_xmit_frag, 16UL);
515 	NDR_MEMBER(_ushort, max_recv_frag, 18UL);
516 	NDR_MEMBER(_ulong, assoc_group_id, 20UL);
517 
518 	offset = 24UL;	/* offset of sec_addr */
519 
520 	switch (nds->m_op) {
521 	case NDR_M_OP_MARSHALL:
522 		val->sec_addr.length = 0;
523 		break;
524 
525 	case NDR_M_OP_UNMARSHALL:
526 		break;
527 
528 	default:
529 		NDR_SET_ERROR(encl_ref, NDR_ERR_M_OP_INVALID);
530 		return (0);
531 	}
532 
533 	NDR_MEMBER(_ushort, sec_addr.length, offset);
534 	NDR_MEMBER_ARR_WITH_DIMENSION(_uchar, sec_addr.port_spec,
535 	    offset+2UL, val->sec_addr.length);
536 
537 	offset += 2;	/* sizeof (sec_addr.length) */
538 	offset += NDR_ALIGN4(offset);
539 
540 	NDR_MEMBER(_ndr_p_result_list, p_result_list, offset);
541 	return (1);
542 }
543 
544 /*
545  * Assume a single presentation context element in the result list.
546  */
547 unsigned
548 ndr_alter_context_rsp_hdr_size(void)
549 {
550 	unsigned	offset;
551 
552 	offset = 24UL;	/* offset of sec_addr */
553 	offset += 2;	/* sizeof (sec_addr.length) */
554 	offset += NDR_ALIGN4(offset);
555 	offset += sizeof (ndr_p_result_list_t);
556 	return (offset);
557 }
558