smbd: Remove struct open_symlink_err
[Samba.git] / librpc / ndr / ndr_dnsp.c
blobb4cad86392d5a5dc6209bf8c892138b3c0054438
1 /*
2 Unix SMB/CIFS implementation.
4 Manually parsed structures found in DNSP
6 Copyright (C) Andrew Tridgell 2010
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_dnsp.h"
26 print a dnsp_name
28 _PUBLIC_ void ndr_print_dnsp_name(struct ndr_print *ndr, const char *name,
29 const char *dns_name)
31 ndr->print(ndr, "%-25s: %s", name, dns_name);
35 pull a dnsp_name
37 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_name(struct ndr_pull *ndr, ndr_flags_type ndr_flags, const char **name)
39 uint8_t len, count, termination;
40 int i;
41 uint32_t total_len, raw_offset;
42 char *ret;
44 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
45 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &count));
47 raw_offset = ndr->offset;
49 ret = talloc_strdup(ndr->current_mem_ctx, "");
50 if (!ret) {
51 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name");
53 total_len = 1;
55 for (i=0; i<count; i++) {
56 uint8_t sublen, newlen;
57 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &sublen));
58 newlen = total_len + sublen;
59 if (newlen < total_len) {
60 return ndr_pull_error(ndr, NDR_ERR_RANGE,
61 "Failed to pull dnsp_name");
63 if (i != count-1) {
64 if (newlen == UINT8_MAX) {
65 return ndr_pull_error(
66 ndr, NDR_ERR_RANGE,
67 "Failed to pull dnsp_name");
69 newlen++; /* for the '.' */
71 ret = talloc_realloc(ndr->current_mem_ctx, ret, char, newlen);
72 if (!ret) {
73 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name");
75 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)&ret[total_len-1], sublen));
76 if (i != count-1) {
77 ret[newlen-2] = '.';
79 ret[newlen-1] = 0;
80 total_len = newlen;
82 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &termination));
83 if (termination != 0) {
84 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - not NUL terminated");
86 if (ndr->offset > raw_offset + len) {
87 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - overrun by %"PRIu32" bytes",
88 ndr->offset - (raw_offset + len));
90 /* there could be additional pad bytes */
91 while (ndr->offset < raw_offset + len) {
92 uint8_t pad;
93 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &pad));
95 (*name) = ret;
96 return NDR_ERR_SUCCESS;
99 enum ndr_err_code ndr_push_dnsp_name(struct ndr_push *ndr, ndr_flags_type ndr_flags, const char *name)
101 int count, total_len, i;
103 /* count the dots */
104 for (count=i=0; name[i]; i++) {
105 if (name[i] == '.') count++;
107 total_len = strlen(name) + 1;
110 * cope with names ending in '.'
112 if (name[0] == '\0') {
114 * Don't access name[-1] for the "" input, which has
115 * the same meaning as a lone '.'.
117 * This allows a round-trip of a dnsRecord from
118 * Windows of a MX record of '.'
120 } else if (name[strlen(name)-1] != '.') {
121 total_len++;
122 count++;
124 if (total_len > 255 || count > 255) {
125 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
126 "dns_name of length %d larger than 255", total_len);
128 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
129 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)count));
130 for (i=0; i<count; i++) {
131 const char *p = strchr(name, '.');
132 size_t sublen = p?(p-name):strlen(name);
133 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)sublen));
134 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)name, sublen));
135 name += sublen + 1;
137 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, 0));
139 return NDR_ERR_SUCCESS;
143 print a dnsp_string
145 _PUBLIC_ void ndr_print_dnsp_string(struct ndr_print *ndr, const char *name,
146 const char *dns_string)
148 ndr->print(ndr, "%-25s: %s", name, dns_string);
152 pull a dnsp_string
154 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string(struct ndr_pull *ndr, ndr_flags_type ndr_flags, const char **string)
156 uint8_t len;
157 char *ret;
159 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
161 ret = talloc_zero_array(ndr->current_mem_ctx, char, len + 1);
162 if (!ret) {
163 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string");
165 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)ret, len));
167 (*string) = ret;
168 NDR_PULL_ALIGN(ndr, 1);
169 return NDR_ERR_SUCCESS;
172 enum ndr_err_code ndr_push_dnsp_string(struct ndr_push *ndr, ndr_flags_type ndr_flags, const char *string)
174 int total_len;
175 total_len = strlen(string);
176 if (total_len > 255) {
177 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
178 "dns_name of length %d larger than 255", total_len);
180 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
181 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)string, total_len));
183 return NDR_ERR_SUCCESS;
187 * print a dnsp_string_list
189 _PUBLIC_ void ndr_print_dnsp_string_list(struct ndr_print *ndr, const char *name,
190 const struct dnsp_string_list *list)
192 uint32_t i;
194 ndr->no_newline = true;
195 for (i=0; i<ndr->depth; i++) {
196 ndr->print(ndr, " ");
198 ndr->print(ndr, "%-25s:", name);
199 for (i=0; i<list->count; i++) {
200 ndr->print(ndr, " \"%s\"", list->str[i]);
202 ndr->print(ndr, "\n");
203 ndr->no_newline = false;
207 * pull a dnsp_string_list
209 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string_list(struct ndr_pull *ndr, ndr_flags_type ndr_flags, struct dnsp_string_list *list)
211 list->count = 0;
212 list->str = talloc_array(ndr->current_mem_ctx, const char *,
213 list->count);
214 if (! list->str) {
215 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
218 while (ndr->offset < ndr->data_size) {
219 list->str = talloc_realloc(ndr->current_mem_ctx, list->str,
220 const char *, list->count+1);
221 if (! list->str) {
222 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
224 NDR_CHECK(ndr_pull_dnsp_string(ndr, ndr_flags, &list->str[list->count]));
225 list->count++;
228 return NDR_ERR_SUCCESS;
231 enum ndr_err_code ndr_push_dnsp_string_list(struct ndr_push *ndr, ndr_flags_type ndr_flags, const struct dnsp_string_list *list)
233 uint8_t i;
235 for (i=0; i<list->count; i++) {
236 NDR_CHECK(ndr_push_dnsp_string(ndr, ndr_flags, list->str[i]));
238 return NDR_ERR_SUCCESS;
241 enum ndr_err_code ndr_dnsp_string_list_copy(TALLOC_CTX *mem_ctx,
242 const struct dnsp_string_list *src,
243 struct dnsp_string_list *dst)
245 size_t i;
247 dst->count = 0;
248 dst->str = talloc_zero_array(mem_ctx, const char *, src->count);
249 if (dst->str == NULL) {
250 return NDR_ERR_ALLOC;
253 for (i = 0; i < src->count; i++) {
254 dst->str[i] = talloc_strdup(dst->str, src->str[i]);
255 if (dst->str[i] == NULL) {
256 TALLOC_FREE(dst->str);
257 return NDR_ERR_ALLOC;
261 dst->count = src->count;
262 return NDR_ERR_SUCCESS;