Registry server LDB backend: Don't make copies of the same type
[kai/samba-autobuild/.git] / source3 / librpc / ndr / libndr.h
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef __LIBNDR_H__
21 #define __LIBNDR_H__
22
23 #define _PRINTF_ATTRIBUTE(a,b) 
24
25 #include "librpc/gen_ndr/misc.h"
26 #include "librpc/gen_ndr/security.h"
27
28 /*
29   this provides definitions for the libcli/rpc/ MSRPC library
30 */
31
32
33 /*
34   this is used by the token store/retrieve code
35 */
36 struct ndr_token_list {
37         struct ndr_token_list *next, *prev;
38         const void *key;
39         uint32_t value;
40 };
41
42 /* this is the base structure passed to routines that 
43    parse MSRPC formatted data 
44
45    note that in Samba4 we use separate routines and structures for
46    MSRPC marshalling and unmarshalling. Also note that these routines
47    are being kept deliberately very simple, and are not tied to a
48    particular transport
49 */
50 struct ndr_pull {
51         uint32_t flags; /* LIBNDR_FLAG_* */
52         uint8_t *data;
53         uint32_t data_size;
54         uint32_t offset;
55
56         uint32_t relative_base_offset;
57         struct ndr_token_list *relative_base_list;
58
59         struct ndr_token_list *relative_list;
60         struct ndr_token_list *array_size_list;
61         struct ndr_token_list *array_length_list;
62         struct ndr_token_list *switch_list;
63
64         TALLOC_CTX *current_mem_ctx;
65
66         /* this is used to ensure we generate unique reference IDs
67            between request and reply */
68         uint32_t ptr_count;
69 };
70
71 struct ndr_pull_save {
72         uint32_t data_size;
73         uint32_t offset;
74         struct ndr_pull_save *next;
75 };
76
77 /* structure passed to functions that generate NDR formatted data */
78 struct ndr_push {
79         uint32_t flags; /* LIBNDR_FLAG_* */
80         uint8_t *data;
81         uint32_t alloc_size;
82         uint32_t offset;
83
84         uint32_t relative_base_offset;
85         struct ndr_token_list *relative_base_list;
86
87         struct ndr_token_list *switch_list;
88         struct ndr_token_list *relative_list;
89         struct ndr_token_list *nbt_string_list;
90         struct ndr_token_list *full_ptr_list;
91
92         /* this is used to ensure we generate unique reference IDs */
93         uint32_t ptr_count;
94 };
95
96 struct ndr_push_save {
97         uint32_t offset;
98         struct ndr_push_save *next;
99 };
100
101
102 /* structure passed to functions that print IDL structures */
103 struct ndr_print {
104         uint32_t flags; /* LIBNDR_FLAG_* */
105         uint32_t depth;
106         struct ndr_token_list *switch_list;
107         void (*print)(struct ndr_print *, const char *, ...) PRINTF_ATTRIBUTE(2,3);
108         void *private_data;
109 };
110
111 #define LIBNDR_FLAG_BIGENDIAN  (1<<0)
112 #define LIBNDR_FLAG_NOALIGN    (1<<1)
113
114 #define LIBNDR_FLAG_STR_ASCII           (1<<2)
115 #define LIBNDR_FLAG_STR_LEN4            (1<<3)
116 #define LIBNDR_FLAG_STR_SIZE4           (1<<4)
117 #define LIBNDR_FLAG_STR_NOTERM          (1<<5)
118 #define LIBNDR_FLAG_STR_NULLTERM        (1<<6)
119 #define LIBNDR_FLAG_STR_SIZE2           (1<<7)
120 #define LIBNDR_FLAG_STR_BYTESIZE        (1<<8)
121 #define LIBNDR_FLAG_STR_FIXLEN32        (1<<9)
122 #define LIBNDR_FLAG_STR_CONFORMANT      (1<<10)
123 #define LIBNDR_FLAG_STR_CHARLEN         (1<<11)
124 #define LIBNDR_FLAG_STR_UTF8            (1<<12)
125 #define LIBNDR_FLAG_STR_FIXLEN15        (1<<13)
126 #define LIBNDR_STRING_FLAGS             (0x7FFC)
127
128
129 #define LIBNDR_FLAG_REF_ALLOC    (1<<20)
130 #define LIBNDR_FLAG_REMAINING    (1<<21)
131 #define LIBNDR_FLAG_ALIGN2       (1<<22)
132 #define LIBNDR_FLAG_ALIGN4       (1<<23)
133 #define LIBNDR_FLAG_ALIGN8       (1<<24)
134
135 #define LIBNDR_ALIGN_FLAGS (LIBNDR_FLAG_ALIGN2|LIBNDR_FLAG_ALIGN4|LIBNDR_FLAG_ALIGN8)
136
137 #define LIBNDR_PRINT_ARRAY_HEX   (1<<25)
138 #define LIBNDR_PRINT_SET_VALUES  (1<<26)
139
140 /* used to force a section of IDL to be little-endian */
141 #define LIBNDR_FLAG_LITTLE_ENDIAN (1<<27)
142
143 /* used to check if alignment padding is zero */
144 #define LIBNDR_FLAG_PAD_CHECK     (1<<28)
145
146 /* set if an object uuid will be present */
147 #define LIBNDR_FLAG_OBJECT_PRESENT    (1<<30)
148
149 /* set to avoid recursion in ndr_size_*() calculation */
150 #define LIBNDR_FLAG_NO_NDR_SIZE         (1<<31)
151
152 /* useful macro for debugging with DEBUG */
153 #define NDR_PRINT_DEBUG(type, p) ndr_print_debug((ndr_print_fn_t)ndr_print_ ##type, #p, p)
154 #define NDR_PRINT_UNION_DEBUG(type, level, p) ndr_print_union_debug((ndr_print_fn_t)ndr_print_ ##type, #p, level, p)
155 #define NDR_PRINT_FUNCTION_DEBUG(type, flags, p) ndr_print_function_debug((ndr_print_function_t)ndr_print_ ##type, #type, flags, p)
156 #define NDR_PRINT_BOTH_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_BOTH, p)
157 #define NDR_PRINT_OUT_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_OUT, p)
158 #define NDR_PRINT_IN_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_IN | NDR_SET_VALUES, p)
159
160 /* useful macro for debugging in strings */
161 #define NDR_PRINT_STRUCT_STRING(ctx, type, p) ndr_print_struct_string(ctx, (ndr_print_fn_t)ndr_print_ ##type, #p, p)
162 #define NDR_PRINT_UNION_STRING(ctx, type, level, p) ndr_print_union_string(ctx, (ndr_print_fn_t)ndr_print_ ##type, #p, level, p)
163 #define NDR_PRINT_FUNCTION_STRING(ctx, type, flags, p) ndr_print_function_string(ctx, (ndr_print_function_t)ndr_print_ ##type, #type, flags, p)
164 #define NDR_PRINT_BOTH_STRING(ctx, type, p) NDR_PRINT_FUNCTION_STRING(ctx, type, NDR_BOTH, p)
165 #define NDR_PRINT_OUT_STRING(ctx, type, p) NDR_PRINT_FUNCTION_STRING(ctx, type, NDR_OUT, p)
166 #define NDR_PRINT_IN_STRING(ctx, type, p) NDR_PRINT_FUNCTION_STRING(ctx, type, NDR_IN | NDR_SET_VALUES, p)
167
168 #define NDR_BE(ndr) (((ndr)->flags & (LIBNDR_FLAG_BIGENDIAN|LIBNDR_FLAG_LITTLE_ENDIAN)) == LIBNDR_FLAG_BIGENDIAN)
169
170 enum ndr_err_code {
171         NDR_ERR_SUCCESS = 0,
172         NDR_ERR_ARRAY_SIZE,
173         NDR_ERR_BAD_SWITCH,
174         NDR_ERR_OFFSET,
175         NDR_ERR_RELATIVE,
176         NDR_ERR_CHARCNV,
177         NDR_ERR_LENGTH,
178         NDR_ERR_SUBCONTEXT,
179         NDR_ERR_COMPRESSION,
180         NDR_ERR_STRING,
181         NDR_ERR_VALIDATE,
182         NDR_ERR_BUFSIZE,
183         NDR_ERR_ALLOC,
184         NDR_ERR_RANGE,
185         NDR_ERR_TOKEN,
186         NDR_ERR_IPV4ADDRESS,
187         NDR_ERR_INVALID_POINTER,
188         NDR_ERR_UNREAD_BYTES
189 };
190
191 #define NDR_ERR_CODE_IS_SUCCESS(x) (x == NDR_ERR_SUCCESS)
192
193 #define NDR_ERR_HAVE_NO_MEMORY(x) do { \
194         if (NULL == (x)) { \
195                 return NDR_ERR_ALLOC; \
196         } \
197 } while (0)
198
199 enum ndr_compression_alg {
200         NDR_COMPRESSION_MSZIP   = 2,
201         NDR_COMPRESSION_XPRESS  = 3
202 };
203
204 /*
205   flags passed to control parse flow
206 */
207 #define NDR_SCALARS 1
208 #define NDR_BUFFERS 2
209
210 /*
211   flags passed to ndr_print_*()
212 */
213 #define NDR_IN 1
214 #define NDR_OUT 2
215 #define NDR_BOTH 3
216 #define NDR_SET_VALUES 4
217
218 #define NDR_PULL_NEED_BYTES(ndr, n) do { \
219         if ((n) > ndr->data_size || ndr->offset + (n) > ndr->data_size) { \
220                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull bytes %u", (unsigned)n); \
221         } \
222 } while(0)
223
224 #define NDR_ALIGN(ndr, n) ndr_align_size(ndr->offset, n)
225
226 #define NDR_ROUND(size, n) (((size)+((n)-1)) & ~((n)-1))
227
228 #define NDR_PULL_ALIGN(ndr, n) do { \
229         if (!(ndr->flags & LIBNDR_FLAG_NOALIGN)) { \
230                 if (ndr->flags & LIBNDR_FLAG_PAD_CHECK) { \
231                         ndr_check_padding(ndr, n); \
232                 } \
233                 ndr->offset = (ndr->offset + (n-1)) & ~(n-1); \
234         } \
235         if (ndr->offset > ndr->data_size) { \
236                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull align %u", (unsigned)n); \
237         } \
238 } while(0)
239
240 #define NDR_PUSH_NEED_BYTES(ndr, n) NDR_CHECK(ndr_push_expand(ndr, n))
241
242 #define NDR_PUSH_ALIGN(ndr, n) do { \
243         if (!(ndr->flags & LIBNDR_FLAG_NOALIGN)) { \
244                 uint32_t _pad = ((ndr->offset + (n-1)) & ~(n-1)) - ndr->offset; \
245                 while (_pad--) NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 0)); \
246         } \
247 } while(0)
248
249 /* these are used to make the error checking on each element in libndr
250    less tedious, hopefully making the code more readable */
251 #define NDR_CHECK(call) do { \
252         enum ndr_err_code _status; \
253         _status = call; \
254         if (!NDR_ERR_CODE_IS_SUCCESS(_status)) { \
255                 return _status; \
256         } \
257 } while (0)
258
259 #define NDR_PULL_GET_MEM_CTX(ndr) (ndr->current_mem_ctx)
260
261 #define NDR_PULL_SET_MEM_CTX(ndr, mem_ctx, flgs) do {\
262         if ( !(flgs) || (ndr->flags & flgs) ) {\
263                 if (!(mem_ctx)) {\
264                         return ndr_pull_error(ndr, NDR_ERR_ALLOC, "NDR_PULL_SET_MEM_CTX(NULL): %s\n", __location__); \
265                 }\
266                 ndr->current_mem_ctx = discard_const(mem_ctx);\
267         }\
268 } while(0)
269
270 #define _NDR_PULL_FIX_CURRENT_MEM_CTX(ndr) do {\
271         if (!ndr->current_mem_ctx) {\
272                 ndr->current_mem_ctx = talloc_new(ndr);\
273                 if (!ndr->current_mem_ctx) {\
274                         return ndr_pull_error(ndr, NDR_ERR_ALLOC, "_NDR_PULL_FIX_CURRENT_MEM_CTX() failed: %s\n", __location__); \
275                 }\
276         }\
277 } while(0)
278
279 #define NDR_PULL_ALLOC(ndr, s) do { \
280         _NDR_PULL_FIX_CURRENT_MEM_CTX(ndr);\
281         (s) = talloc_ptrtype(ndr->current_mem_ctx, (s)); \
282         if (!(s)) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Alloc %s failed: %s\n", # s, __location__); \
283 } while (0)
284
285 #define NDR_PULL_ALLOC_N(ndr, s, n) do { \
286         _NDR_PULL_FIX_CURRENT_MEM_CTX(ndr);\
287         (s) = talloc_array_ptrtype(ndr->current_mem_ctx, (s), n); \
288         if (!(s)) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Alloc %u * %s failed: %s\n", (unsigned)n, # s, __location__); \
289 } while (0)
290
291
292 #define NDR_PUSH_ALLOC_SIZE(ndr, s, size) do { \
293        (s) = talloc_array(ndr, uint8_t, size); \
294        if (!(s)) return ndr_push_error(ndr, NDR_ERR_ALLOC, "push alloc %u failed: %s\n", (unsigned)size, __location__); \
295 } while (0)
296
297 #define NDR_PUSH_ALLOC(ndr, s) do { \
298        (s) = talloc_ptrtype(ndr, (s)); \
299        if (!(s)) return ndr_push_error(ndr, NDR_ERR_ALLOC, "push alloc %s failed: %s\n", # s, __location__); \
300 } while (0)
301
302 /* these are used when generic fn pointers are needed for ndr push/pull fns */
303 typedef enum ndr_err_code (*ndr_push_flags_fn_t)(struct ndr_push *, int ndr_flags, const void *);
304 typedef enum ndr_err_code (*ndr_pull_flags_fn_t)(struct ndr_pull *, int ndr_flags, void *);
305 typedef void (*ndr_print_fn_t)(struct ndr_print *, const char *, const void *);
306 typedef void (*ndr_print_function_t)(struct ndr_print *, const char *, int, const void *);
307
308 extern const struct ndr_syntax_id ndr_transfer_syntax;
309 extern const struct ndr_syntax_id ndr64_transfer_syntax;
310
311 struct ndr_interface_call {
312         const char *name;
313         size_t struct_size;
314         ndr_push_flags_fn_t ndr_push;
315         ndr_pull_flags_fn_t ndr_pull;
316         ndr_print_function_t ndr_print;
317         bool async;
318 };
319
320 struct ndr_interface_string_array {
321         uint32_t count;
322         const char * const *names;
323 };
324
325 struct ndr_interface_table {
326         const char *name;
327         struct ndr_syntax_id syntax_id;
328         const char *helpstring;
329         uint32_t num_calls;
330         const struct ndr_interface_call *calls;
331         const struct ndr_interface_string_array *endpoints;
332         const struct ndr_interface_string_array *authservices;
333 };
334
335 struct ndr_interface_list {
336         struct ndr_interface_list *prev, *next;
337         const struct ndr_interface_table *table;
338 };
339
340 #define NDR_SCALAR_PROTO(name, type) \
341 enum ndr_err_code ndr_push_ ## name(struct ndr_push *ndr, int ndr_flags, type v); \
342 enum ndr_err_code ndr_pull_ ## name(struct ndr_pull *ndr, int ndr_flags, type *v); \
343 void ndr_print_ ## name(struct ndr_print *ndr, const char *var_name, type v);
344
345 #define NDR_BUFFER_PROTO(name, type) \
346 enum ndr_err_code ndr_push_ ## name(struct ndr_push *ndr, int ndr_flags, const type *v); \
347 enum ndr_err_code ndr_pull_ ## name(struct ndr_pull *ndr, int ndr_flags, type *v); \
348 void ndr_print_ ## name(struct ndr_print *ndr, const char *var_name, const type *v);
349
350
351 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid);
352 size_t ndr_size_dom_sid28(const struct dom_sid *sid, int flags);
353
354 #endif /* __LIBNDR_H__ */