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