1 /*
2    Unix SMB/CIFS implementation.
3    rpc interface definitions
4    Copyright (C) Andrew Tridgell 2003
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #ifndef __LIBNDR_H__
22 #define __LIBNDR_H__
23 
24 #include "core.h"
25 #include "lib/talloc/talloc.h"
26 #include "lib/util/util.h" /* for discard_const */
27 #include "lib/charset/charset.h"
28 
29 /*
30   this provides definitions for the libcli/rpc/ MSRPC library
31 */
32 
33 
34 /*
35   this is used by the token store/retrieve code
36 */
37 struct ndr_token_list {
38 	struct ndr_token_list *next, *prev;
39 	const void *key;
40 	uint32_t value;
41 };
42 
43 /* this is the base structure passed to routines that
44    parse MSRPC formatted data
45 
46    note that in Samba4 we use separate routines and structures for
47    MSRPC marshalling and unmarshalling. Also note that these routines
48    are being kept deliberately very simple, and are not tied to a
49    particular transport
50 */
51 struct ndr_pull {
52 	uint32_t flags; /* LIBNDR_FLAG_* */
53 	uint8_t *data;
54 	uint32_t data_size;
55 	uint32_t offset;
56 
57 	uint32_t relative_base_offset;
58 	struct ndr_token_list *relative_base_list;
59 
60 	struct ndr_token_list *relative_list;
61 	struct ndr_token_list *array_size_list;
62 	struct ndr_token_list *array_length_list;
63 	struct ndr_token_list *switch_list;
64 
65 	TALLOC_CTX *current_mem_ctx;
66 
67 	/* this is used to ensure we generate unique reference IDs
68 	   between request and reply */
69 	uint32_t ptr_count;
70 };
71 
72 struct ndr_pull_save {
73 	uint32_t data_size;
74 	uint32_t offset;
75 	struct ndr_pull_save *next;
76 };
77 
78 /* structure passed to functions that generate NDR formatted data */
79 struct ndr_push {
80 	uint32_t flags; /* LIBNDR_FLAG_* */
81 	uint8_t *data;
82 	uint32_t alloc_size;
83 	uint32_t offset;
84 
85 	uint32_t relative_base_offset;
86 	struct ndr_token_list *relative_base_list;
87 
88 	struct ndr_token_list *switch_list;
89 	struct ndr_token_list *relative_list;
90 	struct ndr_token_list *nbt_string_list;
91 	struct ndr_token_list *full_ptr_list;
92 
93 	/* this is used to ensure we generate unique reference IDs */
94 	uint32_t ptr_count;
95 };
96 
97 struct ndr_push_save {
98 	uint32_t offset;
99 	struct ndr_push_save *next;
100 };
101 
102 
103 /* structure passed to functions that print IDL structures */
104 struct ndr_print {
105 	uint32_t flags; /* LIBNDR_FLAG_* */
106 	uint32_t depth;
107 	struct ndr_token_list *switch_list;
108 	void (*print)(struct ndr_print *, const char *, ...) PRINTF_ATTRIBUTE(2,3);
109 	void *private_data;
110 };
111 
112 #define LIBNDR_FLAG_BIGENDIAN  (1<<0)
113 #define LIBNDR_FLAG_NOALIGN    (1<<1)
114 
115 #define LIBNDR_FLAG_STR_ASCII		(1<<2)
116 #define LIBNDR_FLAG_STR_LEN4		(1<<3)
117 #define LIBNDR_FLAG_STR_SIZE4		(1<<4)
118 #define LIBNDR_FLAG_STR_NOTERM		(1<<5)
119 #define LIBNDR_FLAG_STR_NULLTERM	(1<<6)
120 #define LIBNDR_FLAG_STR_SIZE2		(1<<7)
121 #define LIBNDR_FLAG_STR_BYTESIZE	(1<<8)
122 #define LIBNDR_FLAG_STR_FIXLEN32	(1<<9)
123 #define LIBNDR_FLAG_STR_CONFORMANT	(1<<10)
124 #define LIBNDR_FLAG_STR_CHARLEN		(1<<11)
125 #define LIBNDR_FLAG_STR_UTF8		(1<<12)
126 #define LIBNDR_FLAG_STR_FIXLEN15	(1<<13)
127 #define LIBNDR_STRING_FLAGS		(0x7FFC)
128 
129 
130 #define LIBNDR_FLAG_REF_ALLOC    (1<<20)
131 #define LIBNDR_FLAG_REMAINING    (1<<21)
132 #define LIBNDR_FLAG_ALIGN2       (1<<22)
133 #define LIBNDR_FLAG_ALIGN4       (1<<23)
134 #define LIBNDR_FLAG_ALIGN8       (1<<24)
135 
136 #define LIBNDR_ALIGN_FLAGS (LIBNDR_FLAG_ALIGN2|LIBNDR_FLAG_ALIGN4|LIBNDR_FLAG_ALIGN8)
137 
138 #define LIBNDR_PRINT_ARRAY_HEX   (1<<25)
139 #define LIBNDR_PRINT_SET_VALUES  (1<<26)
140 
141 /* used to force a section of IDL to be little-endian */
142 #define LIBNDR_FLAG_LITTLE_ENDIAN (1<<27)
143 
144 /* used to check if alignment padding is zero */
145 #define LIBNDR_FLAG_PAD_CHECK     (1<<28)
146 
147 /* set if an object uuid will be present */
148 #define LIBNDR_FLAG_OBJECT_PRESENT    (1<<30)
149 
150 /* set to avoid recursion in ndr_size_*() calculation */
151 #define LIBNDR_FLAG_NO_NDR_SIZE		(1<<31)
152 
153 /* useful macro for debugging */
154 #define NDR_PRINT_DEBUG(type, p) ndr_print_debug((ndr_print_fn_t)ndr_print_ ##type, #p, p)
155 #define NDR_PRINT_UNION_DEBUG(type, level, p) ndr_print_union_debug((ndr_print_fn_t)ndr_print_ ##type, #p, level, p)
156 #define NDR_PRINT_FUNCTION_DEBUG(type, flags, p) ndr_print_function_debug((ndr_print_function_t)ndr_print_ ##type, #type, flags, p)
157 #define NDR_PRINT_BOTH_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_BOTH, p)
158 #define NDR_PRINT_OUT_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_OUT, p)
159 #define NDR_PRINT_IN_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_IN | NDR_SET_VALUES, p)
160 
161 #define NDR_BE(ndr) (((ndr)->flags & (LIBNDR_FLAG_BIGENDIAN|LIBNDR_FLAG_LITTLE_ENDIAN)) == LIBNDR_FLAG_BIGENDIAN)
162 
163 enum ndr_err_code {
164 	NDR_ERR_ARRAY_SIZE,
165 	NDR_ERR_BAD_SWITCH,
166 	NDR_ERR_OFFSET,
167 	NDR_ERR_RELATIVE,
168 	NDR_ERR_CHARCNV,
169 	NDR_ERR_LENGTH,
170 	NDR_ERR_SUBCONTEXT,
171 	NDR_ERR_COMPRESSION,
172 	NDR_ERR_STRING,
173 	NDR_ERR_VALIDATE,
174 	NDR_ERR_BUFSIZE,
175 	NDR_ERR_ALLOC,
176 	NDR_ERR_RANGE,
177 	NDR_ERR_TOKEN,
178 	NDR_ERR_IPV4ADDRESS
179 };
180 
181 enum ndr_compression_alg {
182 	NDR_COMPRESSION_MSZIP	= 2,
183 	NDR_COMPRESSION_XPRESS	= 3
184 };
185 
186 /*
187   flags passed to control parse flow
188 */
189 #define NDR_SCALARS 1
190 #define NDR_BUFFERS 2
191 
192 /*
193   flags passed to ndr_print_*()
194 */
195 #define NDR_IN 1
196 #define NDR_OUT 2
197 #define NDR_BOTH 3
198 #define NDR_SET_VALUES 4
199 
200 #define NDR_PULL_NEED_BYTES(ndr, n) do { \
201 	if ((n) > ndr->data_size || ndr->offset + (n) > ndr->data_size) { \
202 		return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull bytes %u", (unsigned)n); \
203 	} \
204 } while(0)
205 
206 #define NDR_ALIGN(ndr, n) ndr_align_size(ndr->offset, n)
207 
208 #define NDR_ROUND(size, n) (((size)+((n)-1)) & ~((n)-1))
209 
210 #define NDR_PULL_ALIGN(ndr, n) do { \
211 	if (!(ndr->flags & LIBNDR_FLAG_NOALIGN)) { \
212 		if (ndr->flags & LIBNDR_FLAG_PAD_CHECK) { \
213 			ndr_check_padding(ndr, n); \
214 		} \
215 		ndr->offset = (ndr->offset + (n-1)) & ~(n-1); \
216 	} \
217 	if (ndr->offset > ndr->data_size) { \
218 		return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull align %u", (unsigned)n); \
219 	} \
220 } while(0)
221 
222 #define NDR_PUSH_NEED_BYTES(ndr, n) NDR_CHECK(ndr_push_expand(ndr, ndr->offset+(n)))
223 
224 #define NDR_PUSH_ALIGN(ndr, n) do { \
225 	if (!(ndr->flags & LIBNDR_FLAG_NOALIGN)) { \
226 		uint32_t _pad = ((ndr->offset + (n-1)) & ~(n-1)) - ndr->offset; \
227 		while (_pad--) NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 0)); \
228 	} \
229 } while(0)
230 
231 /*#define NDR_CHECK_DEBUG*/
232 #ifndef NDR_CHECK_DEBUG
233 #define NDR_CHECK_set_shift(n)
234 /* these are used to make the error checking on each element in libndr
235    less tedious, hopefully making the code more readable */
236 #define NDR_CHECK(call) do { NTSTATUS _status; \
237                              _status = call; \
238                              if (!NT_STATUS_IS_OK(_status)) \
239                                 return _status; \
240                         } while (0)
241 #else
242 #define NDR_CHECK_set_shift(n) NDR_CHECK_shift = (n)
243 extern int NDR_CHECK_depth;
244 extern int NDR_CHECK_shift;
245 /* these are used to make the error checking on each element in libndr
246    less tedious, hopefully making the code more readable */
247 #define NDR_CHECK(call) do { NTSTATUS _status; \
248 DEBUG(9, ("%*s%10.10s: BEG(%08X) %s\n", 2*NDR_CHECK_depth++, "", __location__, ndr->offset+NDR_CHECK_shift, #call)); \
249                              _status = call; \
250                              if (!NT_STATUS_IS_OK(_status)) \
251                                 return _status; \
252 DEBUG(9, ("%*s%10.10s: END(%08X) %s\n", --NDR_CHECK_depth*2, "", __location__, ndr->offset+NDR_CHECK_shift, #call)); \
253                         } while (0)
254 #endif
255 
256 #define NDR_PULL_GET_MEM_CTX(ndr) (ndr->current_mem_ctx)
257 
258 #define NDR_PULL_SET_MEM_CTX(ndr, mem_ctx, flgs) do {\
259 	if ( !(flgs) || (ndr->flags & flgs) ) {\
260 		if (!(mem_ctx)) {\
261 			return ndr_pull_error(ndr, NDR_ERR_ALLOC, "NDR_PULL_SET_MEM_CTX(NULL): %s\n", __location__); \
262 		}\
263 		ndr->current_mem_ctx = discard_const(mem_ctx);\
264 	}\
265 } while(0)
266 
267 #define _NDR_PULL_FIX_CURRENT_MEM_CTX(ndr) do {\
268 	if (!ndr->current_mem_ctx) {\
269 		ndr->current_mem_ctx = talloc_new(ndr);\
270 		if (!ndr->current_mem_ctx) {\
271 			return ndr_pull_error(ndr, NDR_ERR_ALLOC, "_NDR_PULL_FIX_CURRENT_MEM_CTX() failed: %s\n", __location__); \
272 		}\
273 	}\
274 } while(0)
275 
276 #define NDR_PULL_ALLOC(ndr, s) do { \
277 	_NDR_PULL_FIX_CURRENT_MEM_CTX(ndr);\
278 	(s) = talloc_ptrtype(ndr->current_mem_ctx, (s)); \
279 	if (!(s)) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Alloc %s failed: %s\n", # s, __location__); \
280 } while (0)
281 
282 #define NDR_PULL_ALLOC_N(ndr, s, n) do { \
283 	_NDR_PULL_FIX_CURRENT_MEM_CTX(ndr);\
284 	(s) = talloc_array_ptrtype(ndr->current_mem_ctx, (s), n); \
285 	if (!(s)) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Alloc %u * %s failed: %s\n", (unsigned)n, # s, __location__); \
286 } while (0)
287 
288 
289 #define NDR_PUSH_ALLOC_SIZE(ndr, s, size) do { \
290        (s) = talloc_size(ndr, size); \
291        if (!(s)) return ndr_push_error(ndr, NDR_ERR_ALLOC, "push alloc %u failed: %s\n", (unsigned)size, __location__); \
292 } while (0)
293 
294 #define NDR_PUSH_ALLOC(ndr, s) do { \
295        (s) = talloc_ptrtype(ndr, (s)); \
296        if (!(s)) return ndr_push_error(ndr, NDR_ERR_ALLOC, "push alloc %s failed: %s\n", # s, __location__); \
297 } while (0)
298 
299 /* these are used when generic fn pointers are needed for ndr push/pull fns */
300 typedef NTSTATUS (*ndr_push_flags_fn_t)(struct ndr_push *, int ndr_flags, const void *);
301 typedef NTSTATUS (*ndr_pull_flags_fn_t)(struct ndr_pull *, int ndr_flags, void *);
302 typedef void (*ndr_print_fn_t)(struct ndr_print *, const char *, const void *);
303 typedef void (*ndr_print_function_t)(struct ndr_print *, const char *, int, const void *);
304 
305 extern const struct dcerpc_syntax_id ndr_transfer_syntax;
306 extern const struct dcerpc_syntax_id ndr64_transfer_syntax;
307 
308 #include "librpc/gen_ndr/misc.h"
309 #include "librpc/ndr/libndr_proto.h"
310 
311 /* FIXME: Use represent_as instead */
312 struct dom_sid;
313 NTSTATUS ndr_push_dom_sid2(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid);
314 NTSTATUS ndr_pull_dom_sid2(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid);
315 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, const struct dom_sid *sid);
316 NTSTATUS ndr_push_dom_sid28(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid);
317 NTSTATUS ndr_pull_dom_sid28(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid);
318 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid);
319 size_t ndr_size_dom_sid28(const struct dom_sid *sid, int flags);
320 
321 struct IWbemClassObject;
322 NTSTATUS ndr_push_IWbemClassObject(struct ndr_push *ndr, int ndr_flags, const struct IWbemClassObject *r);
323 NTSTATUS ndr_pull_IWbemClassObject(struct ndr_pull *ndr, int ndr_flags, struct IWbemClassObject *r);
324 
325 
326 #endif /* __LIBNDR_H__ */
327