1 /*
2    Unix SMB/CIFS implementation.
3 
4    routines for marshalling/unmarshalling basic types
5 
6    Copyright (C) Andrew Tridgell 2003
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 
23 #include "includes.h"
24 #include "system/network.h"
25 #include "librpc/ndr/libndr.h"
26 
27 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
28 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
29 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
30 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr))  { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
31 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr))  { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
32 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr))  { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
33 
34 
35 /*
36   check for data leaks from the server by looking for non-zero pad bytes
37   these could also indicate that real structure elements have been
38   mistaken for padding in the IDL
39 */
ndr_check_padding(struct ndr_pull * ndr,size_t n)40 _PUBLIC_ void ndr_check_padding(struct ndr_pull *ndr, size_t n)
41 {
42 	size_t ofs2 = (ndr->offset + (n-1)) & ~(n-1);
43 	int i;
44 	for (i=ndr->offset;i<ofs2;i++) {
45 		if (ndr->data[i] != 0) {
46 			break;
47 		}
48 	}
49 	if (i<ofs2) {
50 		DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n));
51 		for (i=ndr->offset;i<ofs2;i++) {
52 			DEBUG(0,("%02x ", ndr->data[i]));
53 		}
54 		DEBUG(0,("\n"));
55 	}
56 
57 }
58 
59 /*
60   parse a int8_t
61 */
ndr_pull_int8(struct ndr_pull * ndr,int ndr_flags,int8_t * v)62 _PUBLIC_ NTSTATUS ndr_pull_int8(struct ndr_pull *ndr, int ndr_flags, int8_t *v)
63 {
64 	NDR_PULL_NEED_BYTES(ndr, 1);
65 	*v = (int8_t)CVAL(ndr->data, ndr->offset);
66 	ndr->offset += 1;
67 	return NT_STATUS_OK;
68 }
69 
70 /*
71   parse a uint8_t
72 */
ndr_pull_uint8(struct ndr_pull * ndr,int ndr_flags,uint8_t * v)73 _PUBLIC_ NTSTATUS ndr_pull_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
74 {
75 	NDR_PULL_NEED_BYTES(ndr, 1);
76 	*v = CVAL(ndr->data, ndr->offset);
77 	ndr->offset += 1;
78 	return NT_STATUS_OK;
79 }
80 
81 /*
82   parse a int16_t
83 */
ndr_pull_int16(struct ndr_pull * ndr,int ndr_flags,int16_t * v)84 _PUBLIC_ NTSTATUS ndr_pull_int16(struct ndr_pull *ndr, int ndr_flags, int16_t *v)
85 {
86 	NDR_PULL_ALIGN(ndr, 2);
87 	NDR_PULL_NEED_BYTES(ndr, 2);
88 	*v = (uint16_t)NDR_SVAL(ndr, ndr->offset);
89 	ndr->offset += 2;
90 	return NT_STATUS_OK;
91 }
92 
93 /*
94   parse a uint16_t
95 */
ndr_pull_uint16(struct ndr_pull * ndr,int ndr_flags,uint16_t * v)96 _PUBLIC_ NTSTATUS ndr_pull_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
97 {
98 	NDR_PULL_ALIGN(ndr, 2);
99 	NDR_PULL_NEED_BYTES(ndr, 2);
100 	*v = NDR_SVAL(ndr, ndr->offset);
101 	ndr->offset += 2;
102 	return NT_STATUS_OK;
103 }
104 
105 /*
106   parse a int32_t
107 */
ndr_pull_int32(struct ndr_pull * ndr,int ndr_flags,int32_t * v)108 _PUBLIC_ NTSTATUS ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
109 {
110 	NDR_PULL_ALIGN(ndr, 4);
111 	NDR_PULL_NEED_BYTES(ndr, 4);
112 	*v = NDR_IVALS(ndr, ndr->offset);
113 	ndr->offset += 4;
114 	return NT_STATUS_OK;
115 }
116 
117 /*
118   parse a uint32_t
119 */
ndr_pull_uint32(struct ndr_pull * ndr,int ndr_flags,uint32_t * v)120 _PUBLIC_ NTSTATUS ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
121 {
122 	NDR_PULL_ALIGN(ndr, 4);
123 	NDR_PULL_NEED_BYTES(ndr, 4);
124 	*v = NDR_IVAL(ndr, ndr->offset);
125 	ndr->offset += 4;
126 	return NT_STATUS_OK;
127 }
128 
129 /*
130   parse a pointer referent identifier
131 */
ndr_pull_generic_ptr(struct ndr_pull * ndr,uint32_t * v)132 _PUBLIC_ NTSTATUS ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
133 {
134 	NTSTATUS status;
135 	status = ndr_pull_uint32(ndr, NDR_SCALARS, v);
136 	if (NT_STATUS_IS_OK(status) && *v != 0) {
137 		ndr->ptr_count++;
138 	}
139 	return status;
140 }
141 
142 /*
143   parse a ref pointer referent identifier
144 */
ndr_pull_ref_ptr(struct ndr_pull * ndr,uint32_t * v)145 _PUBLIC_ NTSTATUS ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
146 {
147 	NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, v));
148 	/* ref pointers always point to data */
149 	*v = 1;
150 	return NT_STATUS_OK;
151 }
152 
153 /*
154   parse a udlong
155 */
ndr_pull_udlong(struct ndr_pull * ndr,int ndr_flags,uint64_t * v)156 _PUBLIC_ NTSTATUS ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
157 {
158 	NDR_PULL_ALIGN(ndr, 4);
159 	NDR_PULL_NEED_BYTES(ndr, 8);
160 	*v = NDR_IVAL(ndr, ndr->offset);
161 	*v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
162 	ndr->offset += 8;
163 	return NT_STATUS_OK;
164 }
165 
166 /*
167   parse a udlongr
168 */
ndr_pull_udlongr(struct ndr_pull * ndr,int ndr_flags,uint64_t * v)169 _PUBLIC_ NTSTATUS ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
170 {
171 	NDR_PULL_ALIGN(ndr, 4);
172 	NDR_PULL_NEED_BYTES(ndr, 8);
173 	*v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
174 	*v |= NDR_IVAL(ndr, ndr->offset+4);
175 	ndr->offset += 8;
176 	return NT_STATUS_OK;
177 }
178 
179 /*
180   parse a dlong
181 */
ndr_pull_dlong(struct ndr_pull * ndr,int ndr_flags,int64_t * v)182 _PUBLIC_ NTSTATUS ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
183 {
184 	return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
185 }
186 
187 /*
188   parse a hyper
189 */
ndr_pull_hyper(struct ndr_pull * ndr,int ndr_flags,uint64_t * v)190 _PUBLIC_ NTSTATUS ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
191 {
192 	NDR_PULL_ALIGN(ndr, 8);
193 	return ndr_pull_udlong(ndr, ndr_flags, v);
194 }
195 
196 /*
197   parse a pointer
198 */
ndr_pull_pointer(struct ndr_pull * ndr,int ndr_flags,void ** v)199 _PUBLIC_ NTSTATUS ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
200 {
201 	intptr_t h;
202 	NDR_PULL_ALIGN(ndr, sizeof(h));
203 	NDR_PULL_NEED_BYTES(ndr, sizeof(h));
204 	memcpy(&h, ndr->data+ndr->offset, sizeof(h));
205 	ndr->offset += sizeof(h);
206 	*v = (void *)h;
207 	return NT_STATUS_OK;
208 }
209 
210 /*
211   pull a NTSTATUS
212 */
ndr_pull_NTSTATUS(struct ndr_pull * ndr,int ndr_flags,NTSTATUS * status)213 _PUBLIC_ NTSTATUS ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
214 {
215 	uint32_t v;
216 	NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
217 	*status = NT_STATUS(v);
218 	return NT_STATUS_OK;
219 }
220 
221 /*
222   push a NTSTATUS
223 */
ndr_push_NTSTATUS(struct ndr_push * ndr,int ndr_flags,NTSTATUS status)224 _PUBLIC_ NTSTATUS ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
225 {
226 	return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
227 }
228 
ndr_print_NTSTATUS(struct ndr_print * ndr,const char * name,NTSTATUS r)229 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
230 {
231 	ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
232 }
233 
234 /*
235   pull a WERROR
236 */
ndr_pull_WERROR(struct ndr_pull * ndr,int ndr_flags,WERROR * status)237 _PUBLIC_ NTSTATUS ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
238 {
239 	uint32_t v;
240 	NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
241 	*status = W_ERROR(v);
242 	return NT_STATUS_OK;
243 }
244 
245 /*
246   push a WERROR
247 */
ndr_push_WERROR(struct ndr_push * ndr,int ndr_flags,WERROR status)248 _PUBLIC_ NTSTATUS ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
249 {
250 	return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
251 }
252 
ndr_print_WERROR(struct ndr_print * ndr,const char * name,WERROR r)253 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
254 {
255 	ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
256 }
257 
258 /*
259   parse a set of bytes
260 */
ndr_pull_bytes(struct ndr_pull * ndr,uint8_t * data,uint32_t n)261 _PUBLIC_ NTSTATUS ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
262 {
263 	NDR_PULL_NEED_BYTES(ndr, n);
264 	memcpy(data, ndr->data + ndr->offset, n);
265 	ndr->offset += n;
266 	return NT_STATUS_OK;
267 }
268 
269 /*
270   pull an array of uint8
271 */
ndr_pull_array_uint8(struct ndr_pull * ndr,int ndr_flags,uint8_t * data,uint32_t n)272 _PUBLIC_ NTSTATUS ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
273 {
274 	if (!(ndr_flags & NDR_SCALARS)) {
275 		return NT_STATUS_OK;
276 	}
277 	return ndr_pull_bytes(ndr, data, n);
278 }
279 
280 /*
281   push a int8_t
282 */
ndr_push_int8(struct ndr_push * ndr,int ndr_flags,int8_t v)283 _PUBLIC_ NTSTATUS ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
284 {
285 	NDR_PUSH_NEED_BYTES(ndr, 1);
286 	SCVAL(ndr->data, ndr->offset, (uint8_t)v);
287 	ndr->offset += 1;
288 	return NT_STATUS_OK;
289 }
290 
291 /*
292   push a uint8_t
293 */
ndr_push_uint8(struct ndr_push * ndr,int ndr_flags,uint8_t v)294 _PUBLIC_ NTSTATUS ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
295 {
296 	NDR_PUSH_NEED_BYTES(ndr, 1);
297 	SCVAL(ndr->data, ndr->offset, v);
298 	ndr->offset += 1;
299 	return NT_STATUS_OK;
300 }
301 
302 /*
303   push a int16_t
304 */
ndr_push_int16(struct ndr_push * ndr,int ndr_flags,int16_t v)305 _PUBLIC_ NTSTATUS ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
306 {
307 	NDR_PUSH_ALIGN(ndr, 2);
308 	NDR_PUSH_NEED_BYTES(ndr, 2);
309 	NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
310 	ndr->offset += 2;
311 	return NT_STATUS_OK;
312 }
313 
314 /*
315   push a uint16_t
316 */
ndr_push_uint16(struct ndr_push * ndr,int ndr_flags,uint16_t v)317 _PUBLIC_ NTSTATUS ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
318 {
319 	NDR_PUSH_ALIGN(ndr, 2);
320 	NDR_PUSH_NEED_BYTES(ndr, 2);
321 	NDR_SSVAL(ndr, ndr->offset, v);
322 	ndr->offset += 2;
323 	return NT_STATUS_OK;
324 }
325 
326 /*
327   push a int32_t
328 */
ndr_push_int32(struct ndr_push * ndr,int ndr_flags,int32_t v)329 _PUBLIC_ NTSTATUS ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
330 {
331 	NDR_PUSH_ALIGN(ndr, 4);
332 	NDR_PUSH_NEED_BYTES(ndr, 4);
333 	NDR_SIVALS(ndr, ndr->offset, v);
334 	ndr->offset += 4;
335 	return NT_STATUS_OK;
336 }
337 
338 /*
339   push a uint32_t
340 */
ndr_push_uint32(struct ndr_push * ndr,int ndr_flags,uint32_t v)341 _PUBLIC_ NTSTATUS ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
342 {
343 	NDR_PUSH_ALIGN(ndr, 4);
344 	NDR_PUSH_NEED_BYTES(ndr, 4);
345 	NDR_SIVAL(ndr, ndr->offset, v);
346 	ndr->offset += 4;
347 	return NT_STATUS_OK;
348 }
349 
350 /*
351   push a udlong
352 */
ndr_push_udlong(struct ndr_push * ndr,int ndr_flags,uint64_t v)353 _PUBLIC_ NTSTATUS ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
354 {
355 	NDR_PUSH_ALIGN(ndr, 4);
356 	NDR_PUSH_NEED_BYTES(ndr, 8);
357 	NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
358 	NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
359 	ndr->offset += 8;
360 	return NT_STATUS_OK;
361 }
362 
363 /*
364   push a udlongr
365 */
ndr_push_udlongr(struct ndr_push * ndr,int ndr_flags,uint64_t v)366 _PUBLIC_ NTSTATUS ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
367 {
368 	NDR_PUSH_ALIGN(ndr, 4);
369 	NDR_PUSH_NEED_BYTES(ndr, 8);
370 	NDR_SIVAL(ndr, ndr->offset, (v>>32));
371 	NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
372 	ndr->offset += 8;
373 	return NT_STATUS_OK;
374 }
375 
376 /*
377   push a dlong
378 */
ndr_push_dlong(struct ndr_push * ndr,int ndr_flags,int64_t v)379 _PUBLIC_ NTSTATUS ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
380 {
381 	return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
382 }
383 
384 /*
385   push a hyper
386 */
ndr_push_hyper(struct ndr_push * ndr,int ndr_flags,uint64_t v)387 _PUBLIC_ NTSTATUS ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
388 {
389 	NDR_PUSH_ALIGN(ndr, 8);
390 	return ndr_push_udlong(ndr, NDR_SCALARS, v);
391 }
392 
393 /*
394   push a pointer
395 */
ndr_push_pointer(struct ndr_push * ndr,int ndr_flags,void * v)396 _PUBLIC_ NTSTATUS ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
397 {
398 	intptr_t h = (intptr_t)v;
399 	NDR_PUSH_ALIGN(ndr, sizeof(h));
400 	NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
401 	memcpy(ndr->data+ndr->offset, &h, sizeof(h));
402 	ndr->offset += sizeof(h);
403 	return NT_STATUS_OK;
404 }
405 
ndr_push_align(struct ndr_push * ndr,size_t size)406 _PUBLIC_ NTSTATUS ndr_push_align(struct ndr_push *ndr, size_t size)
407 {
408 	NDR_PUSH_ALIGN(ndr, size);
409 	return NT_STATUS_OK;
410 }
411 
ndr_pull_align(struct ndr_pull * ndr,size_t size)412 _PUBLIC_ NTSTATUS ndr_pull_align(struct ndr_pull *ndr, size_t size)
413 {
414 	NDR_PULL_ALIGN(ndr, size);
415 	return NT_STATUS_OK;
416 }
417 
418 /*
419   push some bytes
420 */
ndr_push_bytes(struct ndr_push * ndr,const uint8_t * data,uint32_t n)421 _PUBLIC_ NTSTATUS ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
422 {
423 	NDR_PUSH_NEED_BYTES(ndr, n);
424 	memcpy(ndr->data + ndr->offset, data, n);
425 	ndr->offset += n;
426 	return NT_STATUS_OK;
427 }
428 
429 /*
430   push some zero bytes
431 */
ndr_push_zero(struct ndr_push * ndr,uint32_t n)432 _PUBLIC_ NTSTATUS ndr_push_zero(struct ndr_push *ndr, uint32_t n)
433 {
434 	NDR_PUSH_NEED_BYTES(ndr, n);
435 	memset(ndr->data + ndr->offset, 0, n);
436 	ndr->offset += n;
437 	return NT_STATUS_OK;
438 }
439 
440 /*
441   push an array of uint8
442 */
ndr_push_array_uint8(struct ndr_push * ndr,int ndr_flags,const uint8_t * data,uint32_t n)443 _PUBLIC_ NTSTATUS ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
444 {
445 	if (!(ndr_flags & NDR_SCALARS)) {
446 		return NT_STATUS_OK;
447 	}
448 	return ndr_push_bytes(ndr, data, n);
449 }
450 
451 /*
452   save the current position
453  */
ndr_push_save(struct ndr_push * ndr,struct ndr_push_save * save)454 _PUBLIC_ void ndr_push_save(struct ndr_push *ndr, struct ndr_push_save *save)
455 {
456 	save->offset = ndr->offset;
457 }
458 
459 /*
460   restore the position
461  */
ndr_push_restore(struct ndr_push * ndr,struct ndr_push_save * save)462 _PUBLIC_ void ndr_push_restore(struct ndr_push *ndr, struct ndr_push_save *save)
463 {
464 	ndr->offset = save->offset;
465 }
466 
467 /*
468   push a unique non-zero value if a pointer is non-NULL, otherwise 0
469 */
ndr_push_unique_ptr(struct ndr_push * ndr,const void * p)470 _PUBLIC_ NTSTATUS ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
471 {
472 	uint32_t ptr = 0;
473 	if (p) {
474 		ptr = ndr->ptr_count * 4;
475 		ptr |= 0x00020000;
476 		ndr->ptr_count++;
477 	}
478 	return ndr_push_uint32(ndr, NDR_SCALARS, ptr);
479 }
480 
481 /*
482   push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
483 */
ndr_push_full_ptr(struct ndr_push * ndr,const void * p)484 _PUBLIC_ NTSTATUS ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
485 {
486 	uint32_t ptr = 0;
487 	if (p) {
488 		/* Check if the pointer already exists and has an id */
489 		ptr = ndr_token_peek(&ndr->full_ptr_list, p);
490 		if (ptr == 0) {
491 			ndr->ptr_count++;
492 			ptr = ndr->ptr_count;
493 			ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
494 		}
495 	}
496 	return ndr_push_uint32(ndr, NDR_SCALARS, ptr);
497 }
498 
499 /*
500   push always a 0, if a pointer is NULL it's a fatal error
501 */
ndr_push_ref_ptr(struct ndr_push * ndr)502 _PUBLIC_ NTSTATUS ndr_push_ref_ptr(struct ndr_push *ndr)
503 {
504 	return ndr_push_uint32(ndr, NDR_SCALARS, 0xAEF1AEF1);
505 }
506 
507 
508 /*
509   push a NTTIME
510 */
ndr_push_NTTIME(struct ndr_push * ndr,int ndr_flags,NTTIME t)511 _PUBLIC_ NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
512 {
513 	NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
514 	return NT_STATUS_OK;
515 }
516 
517 /*
518   pull a NTTIME
519 */
ndr_pull_NTTIME(struct ndr_pull * ndr,int ndr_flags,NTTIME * t)520 _PUBLIC_ NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
521 {
522 	NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
523 	return NT_STATUS_OK;
524 }
525 
526 /*
527   push a NTTIME
528 */
ndr_push_NTTIME_1sec(struct ndr_push * ndr,int ndr_flags,NTTIME t)529 _PUBLIC_ NTSTATUS ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
530 {
531 	t /= 10000000;
532 	NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
533 	return NT_STATUS_OK;
534 }
535 
536 /*
537   pull a NTTIME_1sec
538 */
ndr_pull_NTTIME_1sec(struct ndr_pull * ndr,int ndr_flags,NTTIME * t)539 _PUBLIC_ NTSTATUS ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
540 {
541 	NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
542 	(*t) *= 10000000;
543 	return NT_STATUS_OK;
544 }
545 
546 /*
547   pull a NTTIME_hyper
548 */
ndr_pull_NTTIME_hyper(struct ndr_pull * ndr,int ndr_flags,NTTIME * t)549 _PUBLIC_ NTSTATUS ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
550 {
551 	NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
552 	return NT_STATUS_OK;
553 }
554 
555 /*
556   push a NTTIME_hyper
557 */
ndr_push_NTTIME_hyper(struct ndr_push * ndr,int ndr_flags,NTTIME t)558 _PUBLIC_ NTSTATUS ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
559 {
560 	NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
561 	return NT_STATUS_OK;
562 }
563 
564 /*
565   push a time_t
566 */
ndr_push_time_t(struct ndr_push * ndr,int ndr_flags,time_t t)567 _PUBLIC_ NTSTATUS ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
568 {
569 	return ndr_push_uint32(ndr, ndr_flags, t);
570 }
571 
572 /*
573   pull a time_t
574 */
ndr_pull_time_t(struct ndr_pull * ndr,int ndr_flags,time_t * t)575 _PUBLIC_ NTSTATUS ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
576 {
577 	uint32_t tt;
578 	NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
579 	*t = tt;
580 	return NT_STATUS_OK;
581 }
582 
583 
584 /*
585   pull a ipv4address
586 */
ndr_pull_ipv4address(struct ndr_pull * ndr,int ndr_flags,const char ** address)587 _PUBLIC_ NTSTATUS ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
588 {
589 	struct ipv4_addr in;
590 	NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &in.addr));
591 	in.addr = htonl(in.addr);
592 	*address = talloc_strdup(ndr->current_mem_ctx, sys_inet_ntoa(in));
593 	NT_STATUS_HAVE_NO_MEMORY(*address);
594 	return NT_STATUS_OK;
595 }
596 
597 /*
598   push a ipv4address
599 */
ndr_push_ipv4address(struct ndr_push * ndr,int ndr_flags,const char * address)600 _PUBLIC_ NTSTATUS ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
601 {
602 	uint32_t addr;
603 	if (!is_ipaddress(address)) {
604 		return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
605 				      "Invalid IPv4 address: '%s'",
606 				      address);
607 	}
608 	addr = inet_addr(address);
609 	NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
610 	return NT_STATUS_OK;
611 }
612 
613 /*
614   print a ipv4address
615 */
ndr_print_ipv4address(struct ndr_print * ndr,const char * name,const char * address)616 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
617 			   const char *address)
618 {
619 	ndr->print(ndr, "%-25s: %s", name, address);
620 }
621 
622 
ndr_print_struct(struct ndr_print * ndr,const char * name,const char * type)623 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
624 {
625 	ndr->print(ndr, "%s: struct %s", name, type);
626 }
627 
ndr_print_enum(struct ndr_print * ndr,const char * name,const char * type,const char * val,uint32_t value)628 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
629 		    const char *val, uint32_t value)
630 {
631 	if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
632 		ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
633 	} else {
634 		ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
635 	}
636 }
637 
ndr_print_bitmap_flag(struct ndr_print * ndr,size_t size,const char * flag_name,uint32_t flag,uint32_t value)638 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
639 {
640 	/* this is an attempt to support multi-bit bitmap masks */
641 	value &= flag;
642 
643 	while (!(flag & 1)) {
644 		flag >>= 1;
645 		value >>= 1;
646 	}
647 	if (flag == 1) {
648 		ndr->print(ndr, "   %d: %-25s", value, flag_name);
649 	} else {
650 		ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
651 	}
652 }
653 
ndr_print_int8(struct ndr_print * ndr,const char * name,int8_t v)654 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
655 {
656 	ndr->print(ndr, "%-25s: %d", name, v);
657 }
658 
ndr_print_uint8(struct ndr_print * ndr,const char * name,uint8_t v)659 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
660 {
661 	ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
662 }
663 
ndr_print_int16(struct ndr_print * ndr,const char * name,int16_t v)664 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
665 {
666 	ndr->print(ndr, "%-25s: %d", name, v);
667 }
668 
ndr_print_uint16(struct ndr_print * ndr,const char * name,uint16_t v)669 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
670 {
671 	ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
672 }
673 
ndr_print_int32(struct ndr_print * ndr,const char * name,int32_t v)674 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
675 {
676 	ndr->print(ndr, "%-25s: %d", name, v);
677 }
678 
ndr_print_uint32(struct ndr_print * ndr,const char * name,uint32_t v)679 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
680 {
681 	ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
682 }
683 
ndr_print_udlong(struct ndr_print * ndr,const char * name,uint64_t v)684 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
685 {
686 	ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
687 }
688 
ndr_print_udlongr(struct ndr_print * ndr,const char * name,uint64_t v)689 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
690 {
691 	ndr_print_udlong(ndr, name, v);
692 }
693 
ndr_print_dlong(struct ndr_print * ndr,const char * name,int64_t v)694 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
695 {
696 	ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
697 }
698 
ndr_print_hyper(struct ndr_print * ndr,const char * name,uint64_t v)699 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
700 {
701 	ndr_print_dlong(ndr, name, v);
702 }
703 
ndr_print_pointer(struct ndr_print * ndr,const char * name,void * v)704 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
705 {
706 	ndr->print(ndr, "%-25s: %p", name, v);
707 }
708 
ndr_print_ptr(struct ndr_print * ndr,const char * name,const void * p)709 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
710 {
711 	if (p) {
712 		ndr->print(ndr, "%-25s: *", name);
713 	} else {
714 		ndr->print(ndr, "%-25s: NULL", name);
715 	}
716 }
717 
ndr_print_NTTIME(struct ndr_print * ndr,const char * name,NTTIME t)718 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
719 {
720 	ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
721 }
722 
ndr_print_NTTIME_1sec(struct ndr_print * ndr,const char * name,NTTIME t)723 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
724 {
725 	/* this is a standard NTTIME here
726 	 * as it's already converted in the pull/push code
727 	 */
728 	ndr_print_NTTIME(ndr, name, t);
729 }
730 
ndr_print_NTTIME_hyper(struct ndr_print * ndr,const char * name,NTTIME t)731 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
732 {
733 	ndr_print_NTTIME(ndr, name, t);
734 }
735 
ndr_print_time_t(struct ndr_print * ndr,const char * name,time_t t)736 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
737 {
738 	if (t == (time_t)-1 || t == 0) {
739 		ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
740 	} else {
741 		ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
742 	}
743 }
744 
ndr_print_union(struct ndr_print * ndr,const char * name,int level,const char * type)745 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
746 {
747 	if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
748 		ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
749 	} else {
750 		ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
751 	}
752 }
753 
ndr_print_bad_level(struct ndr_print * ndr,const char * name,uint16_t level)754 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
755 {
756 	ndr->print(ndr, "UNKNOWN LEVEL %u", level);
757 }
758 
ndr_print_array_uint8(struct ndr_print * ndr,const char * name,const uint8_t * data,uint32_t count)759 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
760 			   const uint8_t *data, uint32_t count)
761 {
762 	int i;
763 
764 	if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
765 		char s[1202];
766 		for (i=0;i<count;i++) {
767 			snprintf(&s[i*2], 3, "%02x", data[i]);
768 		}
769 		s[i*2] = 0;
770 		ndr->print(ndr, "%-25s: %s", name, s);
771 		return;
772 	}
773 
774 	ndr->print(ndr, "%s: ARRAY(%d)", name, count);
775 	ndr->depth++;
776 	for (i=0;i<count;i++) {
777 		char *idx=NULL;
778 		asprintf(&idx, "[%d]", i);
779 		if (idx) {
780 			ndr_print_uint8(ndr, idx, data[i]);
781 			free(idx);
782 		}
783 	}
784 	ndr->depth--;
785 }
786 
ndr_print_DATA_BLOB(struct ndr_print * ndr,const char * name,DATA_BLOB r)787 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
788 {
789 	ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
790 	if (r.length) {
791 		dump_data(10, r.data, r.length);
792 	}
793 }
794 
795 
796 /*
797   push a DATA_BLOB onto the wire.
798 */
ndr_push_DATA_BLOB(struct ndr_push * ndr,int ndr_flags,DATA_BLOB blob)799 _PUBLIC_ NTSTATUS ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
800 {
801 	if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
802 		if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
803 			blob.length = NDR_ALIGN(ndr, 2);
804 		} else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
805 			blob.length = NDR_ALIGN(ndr, 4);
806 		} else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
807 			blob.length = NDR_ALIGN(ndr, 8);
808 		}
809 		NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
810 		data_blob_clear(&blob);
811 	} else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
812 		NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
813 	}
814 	NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
815 	return NT_STATUS_OK;
816 }
817 
818 /*
819   pull a DATA_BLOB from the wire.
820 */
ndr_pull_DATA_BLOB(struct ndr_pull * ndr,int ndr_flags,DATA_BLOB * blob)821 _PUBLIC_ NTSTATUS ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
822 {
823 	uint32_t length = 0;
824 
825 	if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
826 		if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
827 			length = NDR_ALIGN(ndr, 2);
828 		} else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
829 			length = NDR_ALIGN(ndr, 4);
830 		} else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
831 			length = NDR_ALIGN(ndr, 8);
832 		}
833 		if (ndr->data_size - ndr->offset < length) {
834 			length = ndr->data_size - ndr->offset;
835 		}
836 	} else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
837 		length = ndr->data_size - ndr->offset;
838 	} else {
839 		NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
840 	}
841 	NDR_PULL_NEED_BYTES(ndr, length);
842 	*blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
843 	ndr->offset += length;
844 	return NT_STATUS_OK;
845 }
846 
ndr_size_DATA_BLOB(int ret,const DATA_BLOB * data,int flags)847 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
848 {
849 	return ret + data->length;
850 }
851