librpc/ndr/ndr.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003 
00004    libndr interface
00005 
00006    Copyright (C) Andrew Tridgell 2003
00007    
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 /*
00024   this provides the core routines for NDR parsing functions
00025 
00026   see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
00027   of NDR encoding rules
00028 */
00029 
00030 #include "includes.h"
00031 
00032 #define NDR_BASE_MARSHALL_SIZE 1024
00033 
00034 /* this guid indicates NDR encoding in a protocol tower */
00035 const struct dcerpc_syntax_id ndr_transfer_syntax = {
00036   { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
00037   2
00038 };
00039 
00040 const struct dcerpc_syntax_id ndr64_transfer_syntax = {
00041   { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
00042   1
00043 };
00044 
00045 /*
00046   work out the number of bytes needed to align on a n byte boundary
00047 */
00048 size_t ndr_align_size(uint32_t offset, size_t n)
00049 {
00050         if ((offset & (n-1)) == 0) return 0;
00051         return n - (offset & (n-1));
00052 }
00053 
00054 /*
00055   initialise a ndr parse structure from a data blob
00056 */
00057 struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
00058 {
00059         struct ndr_pull *ndr;
00060 
00061         ndr = talloc_zero(mem_ctx, struct ndr_pull);
00062         if (!ndr) return NULL;
00063         ndr->current_mem_ctx = mem_ctx;
00064 
00065         ndr->data = blob->data;
00066         ndr->data_size = blob->length;
00067 
00068         return ndr;
00069 }
00070 
00071 /*
00072   advance by 'size' bytes
00073 */
00074 NTSTATUS ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
00075 {
00076         ndr->offset += size;
00077         if (ndr->offset > ndr->data_size) {
00078                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
00079                                       "ndr_pull_advance by %u failed",
00080                                       size);
00081         }
00082         return NT_STATUS_OK;
00083 }
00084 
00085 /*
00086   set the parse offset to 'ofs'
00087 */
00088 static NTSTATUS ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
00089 {
00090         ndr->offset = ofs;
00091         if (ndr->offset > ndr->data_size) {
00092                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
00093                                       "ndr_pull_set_offset %u failed",
00094                                       ofs);
00095         }
00096         return NT_STATUS_OK;
00097 }
00098 
00099 /* save the offset/size of the current ndr state */
00100 void ndr_pull_save(struct ndr_pull *ndr, struct ndr_pull_save *save)
00101 {
00102         save->offset = ndr->offset;
00103         save->data_size = ndr->data_size;
00104 }
00105 
00106 /* restore the size/offset of a ndr structure */
00107 void ndr_pull_restore(struct ndr_pull *ndr, struct ndr_pull_save *save)
00108 {
00109         ndr->offset = save->offset;
00110         ndr->data_size = save->data_size;
00111 }
00112 
00113 
00114 /* create a ndr_push structure, ready for some marshalling */
00115 struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
00116 {
00117         struct ndr_push *ndr;
00118 
00119         ndr = talloc_zero(mem_ctx, struct ndr_push);
00120         if (!ndr) {
00121                 return NULL;
00122         }
00123 
00124         ndr->flags = 0;
00125         ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
00126         ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
00127         if (!ndr->data) {
00128                 return NULL;
00129         }
00130 
00131         return ndr;
00132 }
00133 
00134 
00135 /* create a ndr_push structure, ready for some marshalling */
00136 struct ndr_push *ndr_push_init(void)
00137 {
00138         return ndr_push_init_ctx(NULL);
00139 }
00140 
00141 /* free a ndr_push structure */
00142 void ndr_push_free(struct ndr_push *ndr)
00143 {
00144         talloc_free(ndr);
00145 }
00146 
00147 
00148 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
00149 DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
00150 {
00151         DATA_BLOB blob;
00152         blob.data = ndr->data;
00153         blob.length = ndr->offset;
00154         blob.free = NULL;
00155         if (ndr->alloc_size > ndr->offset) {
00156                 ndr->data[ndr->offset] = 0;
00157         }
00158         return blob;
00159 }
00160 
00161 
00162 /*
00163   expand the available space in the buffer to ndr->offset + extra_size
00164 */
00165 NTSTATUS ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
00166 {
00167         uint32_t size = extra_size + ndr->offset;
00168 
00169         if (size < ndr->offset) {
00170                 /* extra_size overflowed the offset */
00171                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
00172                                         size);
00173         }
00174 
00175         if (ndr->alloc_size > size) {
00176                 return NT_STATUS_OK;
00177         }
00178 
00179         ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
00180         if (size+1 > ndr->alloc_size) {
00181                 ndr->alloc_size = size+1;
00182         }
00183         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
00184         if (!ndr->data) {
00185                 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
00186                                       ndr->alloc_size);
00187         }
00188 
00189         return NT_STATUS_OK;
00190 }
00191 
00192 void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
00193 {
00194         va_list ap;
00195         char *s = NULL;
00196         int i;
00197 
00198         va_start(ap, format);
00199         vasprintf(&s, format, ap);
00200         va_end(ap);
00201 
00202         for (i=0;i<ndr->depth;i++) {
00203                 DEBUG(0,("    "));
00204         }
00205 
00206         DEBUG(0,("%s\n", s));
00207         free(s);
00208 }
00209 
00210 static void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
00211 {
00212         va_list ap;
00213         int i;
00214 
00215         for (i=0;i<ndr->depth;i++) {
00216                 ndr->private_data = talloc_asprintf_append(
00217                         (char *)ndr->private_data, "    ");
00218         }
00219 
00220         va_start(ap, format);
00221         ndr->private_data = talloc_vasprintf_append(
00222                 (char *)ndr->private_data, format, ap);
00223         va_end(ap);
00224         ndr->private_data = talloc_asprintf_append(
00225                 (char *)ndr->private_data, "\n");
00226 }
00227 
00228 /*
00229   a useful helper function for printing idl structures via DEBUG()
00230 */
00231 void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
00232 {
00233         struct ndr_print *ndr;
00234 
00235         ndr = talloc_zero(NULL, struct ndr_print);
00236         if (!ndr) return;
00237         ndr->print = ndr_print_debug_helper;
00238         ndr->depth = 1;
00239         ndr->flags = 0;
00240         fn(ndr, name, ptr);
00241         talloc_free(ndr);
00242 }
00243 
00244 /*
00245   a useful helper function for printing idl unions via DEBUG()
00246 */
00247 void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
00248 {
00249         struct ndr_print *ndr;
00250 
00251         ndr = talloc_zero(NULL, struct ndr_print);
00252         if (!ndr) return;
00253         ndr->print = ndr_print_debug_helper;
00254         ndr->depth = 1;
00255         ndr->flags = 0;
00256         ndr_print_set_switch_value(ndr, ptr, level);
00257         fn(ndr, name, ptr);
00258         talloc_free(ndr);
00259 }
00260 
00261 /*
00262   a useful helper function for printing idl function calls via DEBUG()
00263 */
00264 void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
00265 {
00266         struct ndr_print *ndr;
00267 
00268         ndr = talloc_zero(NULL, struct ndr_print);
00269         if (!ndr) return;
00270         ndr->print = ndr_print_debug_helper;
00271         ndr->depth = 1;
00272         ndr->flags = 0;
00273         fn(ndr, name, flags, ptr);
00274         talloc_free(ndr);
00275 }
00276 
00277 
00278 /*
00279   a useful helper function for printing idl function calls to a string
00280 */
00281 char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
00282                                 ndr_print_function_t fn, const char *name, 
00283                                 int flags, void *ptr)
00284 {
00285         struct ndr_print *ndr;
00286         char *ret = NULL;
00287 
00288         ndr = talloc_zero(mem_ctx, struct ndr_print);
00289         if (!ndr) return NULL;
00290         if (!(ndr->private_data = talloc_strdup(mem_ctx, ""))) {
00291                 TALLOC_FREE(ndr);
00292                 return NULL;
00293         }
00294         ndr->print = ndr_print_string_helper;
00295         ndr->depth = 1;
00296         ndr->flags = 0;
00297         fn(ndr, name, flags, ptr);
00298         ret = (char *)ndr->private_data;
00299         talloc_free(ndr);
00300         return ret;
00301 }
00302 
00303 void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
00304 {
00305         /* the big/little endian flags are inter-dependent */
00306         if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
00307                 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
00308         }
00309         if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
00310                 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
00311         }
00312         if (new_flags & LIBNDR_FLAG_REMAINING) {
00313                 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
00314         }
00315         if (new_flags & LIBNDR_ALIGN_FLAGS) {
00316                 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
00317         }
00318         (*pflags) |= new_flags;
00319 }
00320 
00321 static NTSTATUS ndr_map_error(enum ndr_err_code ndr_err)
00322 {
00323         switch (ndr_err) {
00324         case NDR_ERR_BUFSIZE:
00325                 return NT_STATUS_BUFFER_TOO_SMALL;
00326         case NDR_ERR_TOKEN:
00327                 return NT_STATUS_INTERNAL_ERROR;
00328         case NDR_ERR_ALLOC:
00329                 return NT_STATUS_NO_MEMORY;
00330         case NDR_ERR_ARRAY_SIZE:
00331                 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
00332         default:
00333                 break;
00334         }
00335 
00336         /* we should map all error codes to different status codes */
00337         return NT_STATUS_INVALID_PARAMETER;
00338 }
00339 
00340 /*
00341   return and possibly log an NDR error
00342 */
00343 NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
00344                                  enum ndr_err_code ndr_err,
00345                                  const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
00346 {
00347         char *s=NULL;
00348         va_list ap;
00349 
00350         va_start(ap, format);
00351         vasprintf(&s, format, ap);
00352         va_end(ap);
00353 
00354         DEBUG(3,("ndr_pull_error(%u): %s\n", ndr_err, s));
00355 
00356         free(s);
00357 
00358         return ndr_map_error(ndr_err);
00359 }
00360 
00361 /*
00362   return and possibly log an NDR error
00363 */
00364 NTSTATUS ndr_push_error(struct ndr_push *ndr,
00365                                  enum ndr_err_code ndr_err,
00366                                  const char *format, ...)  _PRINTF_ATTRIBUTE(3,4)
00367 {
00368         char *s=NULL;
00369         va_list ap;
00370 
00371         va_start(ap, format);
00372         vasprintf(&s, format, ap);
00373         va_end(ap);
00374 
00375         DEBUG(3,("ndr_push_error(%u): %s\n", ndr_err, s));
00376 
00377         free(s);
00378 
00379         return ndr_map_error(ndr_err);
00380 }
00381 
00382 /*
00383   handle subcontext buffers, which in midl land are user-marshalled, but
00384   we use magic in pidl to make them easier to cope with
00385 */
00386 NTSTATUS ndr_pull_subcontext_start(struct ndr_pull *ndr, 
00387                                    struct ndr_pull **_subndr,
00388                                    size_t header_size,
00389                                    ssize_t size_is)
00390 {
00391         struct ndr_pull *subndr;
00392         uint32_t r_content_size;
00393 
00394         switch (header_size) {
00395         case 0: {
00396                 uint32_t content_size = ndr->data_size - ndr->offset;
00397                 if (size_is >= 0) {
00398                         content_size = size_is;
00399                 }
00400                 r_content_size = content_size;
00401                 break;
00402         }
00403 
00404         case 2: {
00405                 uint16_t content_size;
00406                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
00407                 if (size_is >= 0 && size_is != content_size) {
00408                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
00409                                                 (int)size_is, (int)content_size);
00410                 }
00411                 r_content_size = content_size;
00412                 break;
00413         }
00414 
00415         case 4: {
00416                 uint32_t content_size;
00417                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
00418                 if (size_is >= 0 && size_is != content_size) {
00419                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
00420                                                 (int)size_is, (int)content_size);
00421                 }
00422                 r_content_size = content_size;
00423                 break;
00424         }
00425         default:
00426                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d", 
00427                                       (int)header_size);
00428         }
00429 
00430         NDR_PULL_NEED_BYTES(ndr, r_content_size);
00431 
00432         subndr = talloc_zero(ndr, struct ndr_pull);
00433         NT_STATUS_HAVE_NO_MEMORY(subndr);
00434         subndr->flags           = ndr->flags;
00435         subndr->current_mem_ctx = ndr->current_mem_ctx;
00436 
00437         subndr->data = ndr->data + ndr->offset;
00438         subndr->offset = 0;
00439         subndr->data_size = r_content_size;
00440 
00441         *_subndr = subndr;
00442         return NT_STATUS_OK;
00443 }
00444 
00445 NTSTATUS ndr_pull_subcontext_end(struct ndr_pull *ndr, 
00446                                  struct ndr_pull *subndr,
00447                                  size_t header_size,
00448                                  ssize_t size_is)
00449 {
00450         uint32_t advance;
00451         if (size_is >= 0) {
00452                 advance = size_is;
00453         } else if (header_size > 0) {
00454                 advance = subndr->data_size;
00455         } else {
00456                 advance = subndr->offset;
00457         }
00458         NDR_CHECK(ndr_pull_advance(ndr, advance));
00459         return NT_STATUS_OK;
00460 }
00461 
00462 NTSTATUS ndr_push_subcontext_start(struct ndr_push *ndr,
00463                                    struct ndr_push **_subndr,
00464                                    size_t header_size,
00465                                    ssize_t size_is)
00466 {
00467         struct ndr_push *subndr;
00468 
00469         subndr = ndr_push_init_ctx(ndr);
00470         NT_STATUS_HAVE_NO_MEMORY(subndr);
00471         subndr->flags   = ndr->flags;
00472 
00473         *_subndr = subndr;
00474         return NT_STATUS_OK;
00475 }
00476 
00477 /*
00478   push a subcontext header 
00479 */
00480 NTSTATUS ndr_push_subcontext_end(struct ndr_push *ndr,
00481                                  struct ndr_push *subndr,
00482                                  size_t header_size,
00483                                  ssize_t size_is)
00484 {
00485         if (size_is >= 0) {
00486                 ssize_t padding_len = size_is - subndr->offset;
00487                 if (padding_len > 0) {
00488                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
00489                 } else if (padding_len < 0) {
00490                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
00491                                               (int)subndr->offset, (int)size_is);
00492                 }
00493         }
00494 
00495         switch (header_size) {
00496         case 0: 
00497                 break;
00498 
00499         case 2: 
00500                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
00501                 break;
00502 
00503         case 4: 
00504                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
00505                 break;
00506 
00507         default:
00508                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d", 
00509                                       (int)header_size);
00510         }
00511 
00512         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
00513         return NT_STATUS_OK;
00514 }
00515 
00516 /*
00517   store a token in the ndr context, for later retrieval
00518 */
00519 NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx, 
00520                          struct ndr_token_list **list, 
00521                          const void *key, 
00522                          uint32_t value)
00523 {
00524         struct ndr_token_list *tok;
00525         tok = talloc(mem_ctx, struct ndr_token_list);
00526         if (tok == NULL) {
00527                 return NT_STATUS_NO_MEMORY;
00528         }
00529         tok->key = key;
00530         tok->value = value;
00531         DLIST_ADD((*list), tok);
00532         return NT_STATUS_OK;
00533 }
00534 
00535 /*
00536   retrieve a token from a ndr context, using cmp_fn to match the tokens
00537 */
00538 NTSTATUS ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
00539                                    comparison_fn_t _cmp_fn, BOOL _remove_tok)
00540 {
00541         struct ndr_token_list *tok;
00542         for (tok=*list;tok;tok=tok->next) {
00543                 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
00544                 else if (!_cmp_fn && tok->key == key) goto found;
00545         }
00546         return ndr_map_error(NDR_ERR_TOKEN);
00547 found:
00548         *v = tok->value;
00549         if (_remove_tok) {
00550                 DLIST_REMOVE((*list), tok);
00551                 talloc_free(tok);
00552         }
00553         return NT_STATUS_OK;            
00554 }
00555 
00556 /*
00557   retrieve a token from a ndr context
00558 */
00559 NTSTATUS ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
00560 {
00561         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, True);
00562 }
00563 
00564 /*
00565   peek at but don't removed a token from a ndr context
00566 */
00567 uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
00568 {
00569         NTSTATUS status;
00570         uint32_t v;
00571         status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, False);
00572         if (NT_STATUS_IS_OK(status)) return v;
00573         return 0;
00574 }
00575 
00576 /*
00577   pull an array size field and add it to the array_size_list token list
00578 */
00579 NTSTATUS ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
00580 {
00581         uint32_t size;
00582         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
00583         return ndr_token_store(ndr, &ndr->array_size_list, p, size);
00584 }
00585 
00586 /*
00587   get the stored array size field
00588 */
00589 uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
00590 {
00591         return ndr_token_peek(&ndr->array_size_list, p);
00592 }
00593 
00594 /*
00595   check the stored array size field
00596 */
00597 NTSTATUS ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
00598 {
00599         uint32_t stored;
00600         stored = ndr_token_peek(&ndr->array_size_list, p);
00601         if (stored != size) {
00602                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
00603                                       "Bad array size - got %u expected %u\n",
00604                                       stored, size);
00605         }
00606         return NT_STATUS_OK;
00607 }
00608 
00609 /*
00610   pull an array length field and add it to the array_length_list token list
00611 */
00612 NTSTATUS ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
00613 {
00614         uint32_t length, offset;
00615         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
00616         if (offset != 0) {
00617                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
00618                                       "non-zero array offset %u\n", offset);
00619         }
00620         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
00621         return ndr_token_store(ndr, &ndr->array_length_list, p, length);
00622 }
00623 
00624 /*
00625   get the stored array length field
00626 */
00627 uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
00628 {
00629         return ndr_token_peek(&ndr->array_length_list, p);
00630 }
00631 
00632 /*
00633   check the stored array length field
00634 */
00635 NTSTATUS ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
00636 {
00637         uint32_t stored;
00638         stored = ndr_token_peek(&ndr->array_length_list, p);
00639         if (stored != length) {
00640                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
00641                                       "Bad array length - got %u expected %u\n",
00642                                       stored, length);
00643         }
00644         return NT_STATUS_OK;
00645 }
00646 
00647 /*
00648   store a switch value
00649  */
00650 NTSTATUS ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
00651 {
00652         return ndr_token_store(ndr, &ndr->switch_list, p, val);
00653 }
00654 
00655 NTSTATUS ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
00656 {
00657         return ndr_token_store(ndr, &ndr->switch_list, p, val);
00658 }
00659 
00660 NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
00661 {
00662         return ndr_token_store(ndr, &ndr->switch_list, p, val);
00663 }
00664 
00665 /*
00666   retrieve a switch value
00667  */
00668 uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
00669 {
00670         return ndr_token_peek(&ndr->switch_list, p);
00671 }
00672 
00673 uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
00674 {
00675         return ndr_token_peek(&ndr->switch_list, p);
00676 }
00677 
00678 uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
00679 {
00680         return ndr_token_peek(&ndr->switch_list, p);
00681 }
00682 
00683 /*
00684   pull a struct from a blob using NDR
00685 */
00686 NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
00687                               ndr_pull_flags_fn_t fn)
00688 {
00689         struct ndr_pull *ndr;
00690         ndr = ndr_pull_init_blob(blob, mem_ctx);
00691         if (!ndr) {
00692                 return NT_STATUS_NO_MEMORY;
00693         }
00694         return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
00695 }
00696 
00697 /*
00698   pull a struct from a blob using NDR - failing if all bytes are not consumed
00699 */
00700 NTSTATUS ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
00701                                   ndr_pull_flags_fn_t fn)
00702 {
00703         struct ndr_pull *ndr;
00704         NTSTATUS status;
00705 
00706         ndr = ndr_pull_init_blob(blob, mem_ctx);
00707         if (!ndr) {
00708                 return NT_STATUS_NO_MEMORY;
00709         }
00710         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
00711         if (!NT_STATUS_IS_OK(status)) return status;
00712         if (ndr->offset != ndr->data_size) {
00713                 return NT_STATUS_BUFFER_TOO_SMALL;
00714         }
00715         return status;
00716 }
00717 
00718 /*
00719   pull a union from a blob using NDR, given the union discriminator
00720 */
00721 NTSTATUS ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
00722                              uint32_t level, ndr_pull_flags_fn_t fn)
00723 {
00724         struct ndr_pull *ndr;
00725         NTSTATUS status;
00726 
00727         ndr = ndr_pull_init_blob(blob, mem_ctx);
00728         if (!ndr) {
00729                 return NT_STATUS_NO_MEMORY;
00730         }
00731         ndr_pull_set_switch_value(ndr, p, level);
00732         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
00733         if (!NT_STATUS_IS_OK(status)) return status;
00734         if (ndr->offset != ndr->data_size) {
00735                 return NT_STATUS_BUFFER_TOO_SMALL;
00736         }
00737         return status;
00738 }
00739 
00740 /*
00741   push a struct to a blob using NDR
00742 */
00743 NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p,
00744                               ndr_push_flags_fn_t fn)
00745 {
00746         NTSTATUS status;
00747         struct ndr_push *ndr;
00748         ndr = ndr_push_init_ctx(mem_ctx);
00749         if (!ndr) {
00750                 return NT_STATUS_NO_MEMORY;
00751         }
00752         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
00753         if (!NT_STATUS_IS_OK(status)) {
00754                 return status;
00755         }
00756 
00757         *blob = ndr_push_blob(ndr);
00758 
00759         return NT_STATUS_OK;
00760 }
00761 
00762 /*
00763   push a union to a blob using NDR
00764 */
00765 NTSTATUS ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
00766                              uint32_t level, ndr_push_flags_fn_t fn)
00767 {
00768         NTSTATUS status;
00769         struct ndr_push *ndr;
00770         ndr = ndr_push_init_ctx(mem_ctx);
00771         if (!ndr) {
00772                 return NT_STATUS_NO_MEMORY;
00773         }
00774         ndr_push_set_switch_value(ndr, p, level);
00775         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
00776         if (!NT_STATUS_IS_OK(status)) {
00777                 return status;
00778         }
00779 
00780         *blob = ndr_push_blob(ndr);
00781 
00782         return NT_STATUS_OK;
00783 }
00784 
00785 /*
00786   generic ndr_size_*() handler for structures
00787 */
00788 size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
00789 {
00790         struct ndr_push *ndr;
00791         NTSTATUS status;
00792         size_t ret;
00793 
00794         /* avoid recursion */
00795         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
00796 
00797         ndr = ndr_push_init_ctx(NULL);
00798         if (!ndr) return 0;
00799         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
00800         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
00801         if (!NT_STATUS_IS_OK(status)) {
00802                 return 0;
00803         }
00804         ret = ndr->offset;
00805         talloc_free(ndr);
00806         return ret;
00807 }
00808 
00809 /*
00810   generic ndr_size_*() handler for unions
00811 */
00812 size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
00813 {
00814         struct ndr_push *ndr;
00815         NTSTATUS status;
00816         size_t ret;
00817 
00818         /* avoid recursion */
00819         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
00820 
00821         ndr = ndr_push_init_ctx(NULL);
00822         if (!ndr) return 0;
00823         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
00824         ndr_push_set_switch_value(ndr, p, level);
00825         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
00826         if (!NT_STATUS_IS_OK(status)) {
00827                 return 0;
00828         }
00829         ret = ndr->offset;
00830         talloc_free(ndr);
00831         return ret;
00832 }
00833 
00834 /*
00835   get the current base for relative pointers for the push
00836 */
00837 uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
00838 {
00839         return ndr->relative_base_offset;
00840 }
00841 
00842 /*
00843   restore the old base for relative pointers for the push
00844 */
00845 void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
00846 {
00847         ndr->relative_base_offset = offset;
00848 }
00849 
00850 /*
00851   setup the current base for relative pointers for the push
00852   called in the NDR_SCALAR stage
00853 */
00854 NTSTATUS ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
00855 {
00856         ndr->relative_base_offset = offset;
00857         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
00858 }
00859 
00860 /*
00861   setup the current base for relative pointers for the push
00862   called in the NDR_BUFFERS stage
00863 */
00864 NTSTATUS ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
00865 {
00866         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
00867 }
00868 
00869 /*
00870   push a relative object - stage1
00871   this is called during SCALARS processing
00872 */
00873 NTSTATUS ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
00874 {
00875         if (p == NULL) {
00876                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
00877                 return NT_STATUS_OK;
00878         }
00879         NDR_CHECK(ndr_push_align(ndr, 4));
00880         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
00881         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
00882 }
00883 
00884 /*
00885   push a relative object - stage2
00886   this is called during buffers processing
00887 */
00888 NTSTATUS ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
00889 {
00890         struct ndr_push_save save;
00891         uint32_t ptr_offset = 0xFFFFFFFF;
00892         if (p == NULL) {
00893                 return NT_STATUS_OK;
00894         }
00895         ndr_push_save(ndr, &save);
00896         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
00897         if (ptr_offset > ndr->offset) {
00898                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
00899                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
00900                                       ptr_offset, ndr->offset);
00901         }
00902         ndr->offset = ptr_offset;
00903         if (save.offset < ndr->relative_base_offset) {
00904                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
00905                                       "ndr_push_relative_ptr2 save.offset(%u) < ndr->relative_base_offset(%u)",
00906                                       save.offset, ndr->relative_base_offset);
00907         }       
00908         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset - ndr->relative_base_offset));
00909         ndr_push_restore(ndr, &save);
00910         return NT_STATUS_OK;
00911 }
00912 
00913 /*
00914   get the current base for relative pointers for the pull
00915 */
00916 uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
00917 {
00918         return ndr->relative_base_offset;
00919 }
00920 
00921 /*
00922   restore the old base for relative pointers for the pull
00923 */
00924 void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
00925 {
00926         ndr->relative_base_offset = offset;
00927 }
00928 
00929 /*
00930   setup the current base for relative pointers for the pull
00931   called in the NDR_SCALAR stage
00932 */
00933 NTSTATUS ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
00934 {
00935         ndr->relative_base_offset = offset;
00936         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
00937 }
00938 
00939 /*
00940   setup the current base for relative pointers for the pull
00941   called in the NDR_BUFFERS stage
00942 */
00943 NTSTATUS ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
00944 {
00945         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
00946 }
00947 
00948 /*
00949   pull a relative object - stage1
00950   called during SCALARS processing
00951 */
00952 NTSTATUS ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
00953 {
00954         rel_offset += ndr->relative_base_offset;
00955         if (rel_offset > ndr->data_size) {
00956                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
00957                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
00958                                       rel_offset, ndr->data_size);
00959         }
00960         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
00961 }
00962 
00963 /*
00964   pull a relative object - stage2
00965   called during BUFFERS processing
00966 */
00967 NTSTATUS ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
00968 {
00969         uint32_t rel_offset;
00970         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
00971         return ndr_pull_set_offset(ndr, rel_offset);
00972 }

Sambaに対してSat Aug 29 21:23:04 2009に生成されました。  doxygen 1.4.7