Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / any_255 / tsig_250.c
blobb18868d8a163e1bb4cbff9bd3e56d35e1ae51f54
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: tsig_250.c,v 1.65 2009/12/04 22:06:37 tbox Exp */
22 /* Reviewed: Thu Mar 16 13:39:43 PST 2000 by gson */
24 #ifndef RDATA_ANY_255_TSIG_250_C
25 #define RDATA_ANY_255_TSIG_250_C
27 #define RRTYPE_TSIG_ATTRIBUTES \
28 (DNS_RDATATYPEATTR_META | DNS_RDATATYPEATTR_NOTQUESTION)
30 static inline isc_result_t
31 fromtext_any_tsig(ARGS_FROMTEXT) {
32 isc_token_t token;
33 dns_name_t name;
34 isc_uint64_t sigtime;
35 isc_buffer_t buffer;
36 dns_rcode_t rcode;
37 long i;
38 char *e;
40 REQUIRE(type == 250);
41 REQUIRE(rdclass == 255);
43 UNUSED(type);
44 UNUSED(rdclass);
45 UNUSED(callbacks);
48 * Algorithm Name.
50 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
51 ISC_FALSE));
52 dns_name_init(&name, NULL);
53 buffer_fromregion(&buffer, &token.value.as_region);
54 origin = (origin != NULL) ? origin : dns_rootname;
55 RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
58 * Time Signed: 48 bits.
60 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
61 ISC_FALSE));
62 sigtime = isc_string_touint64(DNS_AS_STR(token), &e, 10);
63 if (*e != 0)
64 RETTOK(DNS_R_SYNTAX);
65 if ((sigtime >> 48) != 0)
66 RETTOK(ISC_R_RANGE);
67 RETERR(uint16_tobuffer((isc_uint16_t)(sigtime >> 32), target));
68 RETERR(uint32_tobuffer((isc_uint32_t)(sigtime & 0xffffffffU), target));
71 * Fudge.
73 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
74 ISC_FALSE));
75 if (token.value.as_ulong > 0xffffU)
76 RETTOK(ISC_R_RANGE);
77 RETERR(uint16_tobuffer(token.value.as_ulong, target));
80 * Signature Size.
82 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
83 ISC_FALSE));
84 if (token.value.as_ulong > 0xffffU)
85 RETTOK(ISC_R_RANGE);
86 RETERR(uint16_tobuffer(token.value.as_ulong, target));
89 * Signature.
91 RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
94 * Original ID.
96 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
97 ISC_FALSE));
98 if (token.value.as_ulong > 0xffffU)
99 RETTOK(ISC_R_RANGE);
100 RETERR(uint16_tobuffer(token.value.as_ulong, target));
103 * Error.
105 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
106 ISC_FALSE));
107 if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
108 != ISC_R_SUCCESS)
110 i = strtol(DNS_AS_STR(token), &e, 10);
111 if (*e != 0)
112 RETTOK(DNS_R_UNKNOWN);
113 if (i < 0 || i > 0xffff)
114 RETTOK(ISC_R_RANGE);
115 rcode = (dns_rcode_t)i;
117 RETERR(uint16_tobuffer(rcode, target));
120 * Other Len.
122 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
123 ISC_FALSE));
124 if (token.value.as_ulong > 0xffffU)
125 RETTOK(ISC_R_RANGE);
126 RETERR(uint16_tobuffer(token.value.as_ulong, target));
129 * Other Data.
131 return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
134 static inline isc_result_t
135 totext_any_tsig(ARGS_TOTEXT) {
136 isc_region_t sr;
137 isc_region_t sigr;
138 char buf[sizeof("281474976710655 ")];
139 char *bufp;
140 dns_name_t name;
141 dns_name_t prefix;
142 isc_boolean_t sub;
143 isc_uint64_t sigtime;
144 unsigned short n;
146 REQUIRE(rdata->type == 250);
147 REQUIRE(rdata->rdclass == 255);
148 REQUIRE(rdata->length != 0);
150 dns_rdata_toregion(rdata, &sr);
152 * Algorithm Name.
154 dns_name_init(&name, NULL);
155 dns_name_init(&prefix, NULL);
156 dns_name_fromregion(&name, &sr);
157 sub = name_prefix(&name, tctx->origin, &prefix);
158 RETERR(dns_name_totext(&prefix, sub, target));
159 RETERR(str_totext(" ", target));
160 isc_region_consume(&sr, name_length(&name));
163 * Time Signed.
165 sigtime = ((isc_uint64_t)sr.base[0] << 40) |
166 ((isc_uint64_t)sr.base[1] << 32) |
167 ((isc_uint64_t)sr.base[2] << 24) |
168 ((isc_uint64_t)sr.base[3] << 16) |
169 ((isc_uint64_t)sr.base[4] << 8) |
170 (isc_uint64_t)sr.base[5];
171 isc_region_consume(&sr, 6);
172 bufp = &buf[sizeof(buf) - 1];
173 *bufp-- = 0;
174 *bufp-- = ' ';
175 do {
176 *bufp-- = decdigits[sigtime % 10];
177 sigtime /= 10;
178 } while (sigtime != 0);
179 bufp++;
180 RETERR(str_totext(bufp, target));
183 * Fudge.
185 n = uint16_fromregion(&sr);
186 isc_region_consume(&sr, 2);
187 sprintf(buf, "%u ", n);
188 RETERR(str_totext(buf, target));
191 * Signature Size.
193 n = uint16_fromregion(&sr);
194 isc_region_consume(&sr, 2);
195 sprintf(buf, "%u", n);
196 RETERR(str_totext(buf, target));
199 * Signature.
201 REQUIRE(n <= sr.length);
202 sigr = sr;
203 sigr.length = n;
204 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
205 RETERR(str_totext(" (", target));
206 RETERR(str_totext(tctx->linebreak, target));
207 RETERR(isc_base64_totext(&sigr, tctx->width - 2,
208 tctx->linebreak, target));
209 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
210 RETERR(str_totext(" ) ", target));
211 else
212 RETERR(str_totext(" ", target));
213 isc_region_consume(&sr, n);
216 * Original ID.
218 n = uint16_fromregion(&sr);
219 isc_region_consume(&sr, 2);
220 sprintf(buf, "%u ", n);
221 RETERR(str_totext(buf, target));
224 * Error.
226 n = uint16_fromregion(&sr);
227 isc_region_consume(&sr, 2);
228 if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
229 RETERR(str_totext(" ", target));
230 else {
231 sprintf(buf, "%u ", n);
232 RETERR(str_totext(buf, target));
236 * Other Size.
238 n = uint16_fromregion(&sr);
239 isc_region_consume(&sr, 2);
240 sprintf(buf, "%u ", n);
241 RETERR(str_totext(buf, target));
244 * Other.
246 return (isc_base64_totext(&sr, 60, " ", target));
249 static inline isc_result_t
250 fromwire_any_tsig(ARGS_FROMWIRE) {
251 isc_region_t sr;
252 dns_name_t name;
253 unsigned long n;
255 REQUIRE(type == 250);
256 REQUIRE(rdclass == 255);
258 UNUSED(type);
259 UNUSED(rdclass);
261 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
264 * Algorithm Name.
266 dns_name_init(&name, NULL);
267 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
269 isc_buffer_activeregion(source, &sr);
271 * Time Signed + Fudge.
273 if (sr.length < 8)
274 return (ISC_R_UNEXPECTEDEND);
275 RETERR(mem_tobuffer(target, sr.base, 8));
276 isc_region_consume(&sr, 8);
277 isc_buffer_forward(source, 8);
280 * Signature Length + Signature.
282 if (sr.length < 2)
283 return (ISC_R_UNEXPECTEDEND);
284 n = uint16_fromregion(&sr);
285 if (sr.length < n + 2)
286 return (ISC_R_UNEXPECTEDEND);
287 RETERR(mem_tobuffer(target, sr.base, n + 2));
288 isc_region_consume(&sr, n + 2);
289 isc_buffer_forward(source, n + 2);
292 * Original ID + Error.
294 if (sr.length < 4)
295 return (ISC_R_UNEXPECTEDEND);
296 RETERR(mem_tobuffer(target, sr.base, 4));
297 isc_region_consume(&sr, 4);
298 isc_buffer_forward(source, 4);
301 * Other Length + Other.
303 if (sr.length < 2)
304 return (ISC_R_UNEXPECTEDEND);
305 n = uint16_fromregion(&sr);
306 if (sr.length < n + 2)
307 return (ISC_R_UNEXPECTEDEND);
308 isc_buffer_forward(source, n + 2);
309 return (mem_tobuffer(target, sr.base, n + 2));
312 static inline isc_result_t
313 towire_any_tsig(ARGS_TOWIRE) {
314 isc_region_t sr;
315 dns_name_t name;
316 dns_offsets_t offsets;
318 REQUIRE(rdata->type == 250);
319 REQUIRE(rdata->rdclass == 255);
320 REQUIRE(rdata->length != 0);
322 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
323 dns_rdata_toregion(rdata, &sr);
324 dns_name_init(&name, offsets);
325 dns_name_fromregion(&name, &sr);
326 RETERR(dns_name_towire(&name, cctx, target));
327 isc_region_consume(&sr, name_length(&name));
328 return (mem_tobuffer(target, sr.base, sr.length));
331 static inline int
332 compare_any_tsig(ARGS_COMPARE) {
333 isc_region_t r1;
334 isc_region_t r2;
335 dns_name_t name1;
336 dns_name_t name2;
337 int order;
339 REQUIRE(rdata1->type == rdata2->type);
340 REQUIRE(rdata1->rdclass == rdata2->rdclass);
341 REQUIRE(rdata1->type == 250);
342 REQUIRE(rdata1->rdclass == 255);
343 REQUIRE(rdata1->length != 0);
344 REQUIRE(rdata2->length != 0);
346 dns_rdata_toregion(rdata1, &r1);
347 dns_rdata_toregion(rdata2, &r2);
348 dns_name_init(&name1, NULL);
349 dns_name_init(&name2, NULL);
350 dns_name_fromregion(&name1, &r1);
351 dns_name_fromregion(&name2, &r2);
352 order = dns_name_rdatacompare(&name1, &name2);
353 if (order != 0)
354 return (order);
355 isc_region_consume(&r1, name_length(&name1));
356 isc_region_consume(&r2, name_length(&name2));
357 return (isc_region_compare(&r1, &r2));
360 static inline isc_result_t
361 fromstruct_any_tsig(ARGS_FROMSTRUCT) {
362 dns_rdata_any_tsig_t *tsig = source;
363 isc_region_t tr;
365 REQUIRE(type == 250);
366 REQUIRE(rdclass == 255);
367 REQUIRE(source != NULL);
368 REQUIRE(tsig->common.rdclass == rdclass);
369 REQUIRE(tsig->common.rdtype == type);
371 UNUSED(type);
372 UNUSED(rdclass);
375 * Algorithm Name.
377 RETERR(name_tobuffer(&tsig->algorithm, target));
379 isc_buffer_availableregion(target, &tr);
380 if (tr.length < 6 + 2 + 2)
381 return (ISC_R_NOSPACE);
384 * Time Signed: 48 bits.
386 RETERR(uint16_tobuffer((isc_uint16_t)(tsig->timesigned >> 32),
387 target));
388 RETERR(uint32_tobuffer((isc_uint32_t)(tsig->timesigned & 0xffffffffU),
389 target));
392 * Fudge.
394 RETERR(uint16_tobuffer(tsig->fudge, target));
397 * Signature Size.
399 RETERR(uint16_tobuffer(tsig->siglen, target));
402 * Signature.
404 RETERR(mem_tobuffer(target, tsig->signature, tsig->siglen));
406 isc_buffer_availableregion(target, &tr);
407 if (tr.length < 2 + 2 + 2)
408 return (ISC_R_NOSPACE);
411 * Original ID.
413 RETERR(uint16_tobuffer(tsig->originalid, target));
416 * Error.
418 RETERR(uint16_tobuffer(tsig->error, target));
421 * Other Len.
423 RETERR(uint16_tobuffer(tsig->otherlen, target));
426 * Other Data.
428 return (mem_tobuffer(target, tsig->other, tsig->otherlen));
431 static inline isc_result_t
432 tostruct_any_tsig(ARGS_TOSTRUCT) {
433 dns_rdata_any_tsig_t *tsig;
434 dns_name_t alg;
435 isc_region_t sr;
437 REQUIRE(rdata->type == 250);
438 REQUIRE(rdata->rdclass == 255);
439 REQUIRE(rdata->length != 0);
441 tsig = (dns_rdata_any_tsig_t *) target;
442 tsig->common.rdclass = rdata->rdclass;
443 tsig->common.rdtype = rdata->type;
444 ISC_LINK_INIT(&tsig->common, link);
446 dns_rdata_toregion(rdata, &sr);
449 * Algorithm Name.
451 dns_name_init(&alg, NULL);
452 dns_name_fromregion(&alg, &sr);
453 dns_name_init(&tsig->algorithm, NULL);
454 RETERR(name_duporclone(&alg, mctx, &tsig->algorithm));
456 isc_region_consume(&sr, name_length(&tsig->algorithm));
459 * Time Signed.
461 INSIST(sr.length >= 6);
462 tsig->timesigned = ((isc_uint64_t)sr.base[0] << 40) |
463 ((isc_uint64_t)sr.base[1] << 32) |
464 ((isc_uint64_t)sr.base[2] << 24) |
465 ((isc_uint64_t)sr.base[3] << 16) |
466 ((isc_uint64_t)sr.base[4] << 8) |
467 (isc_uint64_t)sr.base[5];
468 isc_region_consume(&sr, 6);
471 * Fudge.
473 tsig->fudge = uint16_fromregion(&sr);
474 isc_region_consume(&sr, 2);
477 * Signature Size.
479 tsig->siglen = uint16_fromregion(&sr);
480 isc_region_consume(&sr, 2);
483 * Signature.
485 INSIST(sr.length >= tsig->siglen);
486 tsig->signature = mem_maybedup(mctx, sr.base, tsig->siglen);
487 if (tsig->signature == NULL)
488 goto cleanup;
489 isc_region_consume(&sr, tsig->siglen);
492 * Original ID.
494 tsig->originalid = uint16_fromregion(&sr);
495 isc_region_consume(&sr, 2);
498 * Error.
500 tsig->error = uint16_fromregion(&sr);
501 isc_region_consume(&sr, 2);
504 * Other Size.
506 tsig->otherlen = uint16_fromregion(&sr);
507 isc_region_consume(&sr, 2);
510 * Other.
512 INSIST(sr.length == tsig->otherlen);
513 tsig->other = mem_maybedup(mctx, sr.base, tsig->otherlen);
514 if (tsig->other == NULL)
515 goto cleanup;
517 tsig->mctx = mctx;
518 return (ISC_R_SUCCESS);
520 cleanup:
521 if (mctx != NULL)
522 dns_name_free(&tsig->algorithm, tsig->mctx);
523 if (mctx != NULL && tsig->signature != NULL)
524 isc_mem_free(mctx, tsig->signature);
525 return (ISC_R_NOMEMORY);
528 static inline void
529 freestruct_any_tsig(ARGS_FREESTRUCT) {
530 dns_rdata_any_tsig_t *tsig = (dns_rdata_any_tsig_t *) source;
532 REQUIRE(source != NULL);
533 REQUIRE(tsig->common.rdclass == 255);
534 REQUIRE(tsig->common.rdtype == 250);
536 if (tsig->mctx == NULL)
537 return;
539 dns_name_free(&tsig->algorithm, tsig->mctx);
540 if (tsig->signature != NULL)
541 isc_mem_free(tsig->mctx, tsig->signature);
542 if (tsig->other != NULL)
543 isc_mem_free(tsig->mctx, tsig->other);
544 tsig->mctx = NULL;
547 static inline isc_result_t
548 additionaldata_any_tsig(ARGS_ADDLDATA) {
549 REQUIRE(rdata->type == 250);
550 REQUIRE(rdata->rdclass == 255);
552 UNUSED(rdata);
553 UNUSED(add);
554 UNUSED(arg);
556 return (ISC_R_SUCCESS);
559 static inline isc_result_t
560 digest_any_tsig(ARGS_DIGEST) {
562 REQUIRE(rdata->type == 250);
563 REQUIRE(rdata->rdclass == 255);
565 UNUSED(rdata);
566 UNUSED(digest);
567 UNUSED(arg);
569 return (ISC_R_NOTIMPLEMENTED);
572 static inline isc_boolean_t
573 checkowner_any_tsig(ARGS_CHECKOWNER) {
575 REQUIRE(type == 250);
576 REQUIRE(rdclass == 255);
578 UNUSED(name);
579 UNUSED(type);
580 UNUSED(rdclass);
581 UNUSED(wildcard);
583 return (ISC_TRUE);
586 static inline isc_boolean_t
587 checknames_any_tsig(ARGS_CHECKNAMES) {
589 REQUIRE(rdata->type == 250);
590 REQUIRE(rdata->rdclass == 250);
592 UNUSED(rdata);
593 UNUSED(owner);
594 UNUSED(bad);
596 return (ISC_TRUE);
599 static inline int
600 casecompare_any_tsig(ARGS_COMPARE) {
601 return (compare_any_tsig(rdata1, rdata2));
604 #endif /* RDATA_ANY_255_TSIG_250_C */