]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/lib/dns/rdata/any_255/tsig_250.c
MFC: r253983-253984
[FreeBSD/stable/8.git] / contrib / bind9 / lib / dns / rdata / any_255 / tsig_250.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2012  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id$ */
19
20 /* Reviewed: Thu Mar 16 13:39:43 PST 2000 by gson */
21
22 #ifndef RDATA_ANY_255_TSIG_250_C
23 #define RDATA_ANY_255_TSIG_250_C
24
25 #define RRTYPE_TSIG_ATTRIBUTES \
26         (DNS_RDATATYPEATTR_META | DNS_RDATATYPEATTR_NOTQUESTION)
27
28 static inline isc_result_t
29 fromtext_any_tsig(ARGS_FROMTEXT) {
30         isc_token_t token;
31         dns_name_t name;
32         isc_uint64_t sigtime;
33         isc_buffer_t buffer;
34         dns_rcode_t rcode;
35         long i;
36         char *e;
37
38         REQUIRE(type == 250);
39         REQUIRE(rdclass == 255);
40
41         UNUSED(type);
42         UNUSED(rdclass);
43         UNUSED(callbacks);
44
45         /*
46          * Algorithm Name.
47          */
48         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
49                                       ISC_FALSE));
50         dns_name_init(&name, NULL);
51         buffer_fromregion(&buffer, &token.value.as_region);
52         origin = (origin != NULL) ? origin : dns_rootname;
53         RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
54
55         /*
56          * Time Signed: 48 bits.
57          */
58         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
59                                       ISC_FALSE));
60         sigtime = isc_string_touint64(DNS_AS_STR(token), &e, 10);
61         if (*e != 0)
62                 RETTOK(DNS_R_SYNTAX);
63         if ((sigtime >> 48) != 0)
64                 RETTOK(ISC_R_RANGE);
65         RETERR(uint16_tobuffer((isc_uint16_t)(sigtime >> 32), target));
66         RETERR(uint32_tobuffer((isc_uint32_t)(sigtime & 0xffffffffU), target));
67
68         /*
69          * Fudge.
70          */
71         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
72                                       ISC_FALSE));
73         if (token.value.as_ulong > 0xffffU)
74                 RETTOK(ISC_R_RANGE);
75         RETERR(uint16_tobuffer(token.value.as_ulong, target));
76
77         /*
78          * Signature Size.
79          */
80         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
81                                       ISC_FALSE));
82         if (token.value.as_ulong > 0xffffU)
83                 RETTOK(ISC_R_RANGE);
84         RETERR(uint16_tobuffer(token.value.as_ulong, target));
85
86         /*
87          * Signature.
88          */
89         RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
90
91         /*
92          * Original ID.
93          */
94         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
95                                       ISC_FALSE));
96         if (token.value.as_ulong > 0xffffU)
97                 RETTOK(ISC_R_RANGE);
98         RETERR(uint16_tobuffer(token.value.as_ulong, target));
99
100         /*
101          * Error.
102          */
103         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
104                                       ISC_FALSE));
105         if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
106                                 != ISC_R_SUCCESS)
107         {
108                 i = strtol(DNS_AS_STR(token), &e, 10);
109                 if (*e != 0)
110                         RETTOK(DNS_R_UNKNOWN);
111                 if (i < 0 || i > 0xffff)
112                         RETTOK(ISC_R_RANGE);
113                 rcode = (dns_rcode_t)i;
114         }
115         RETERR(uint16_tobuffer(rcode, target));
116
117         /*
118          * Other Len.
119          */
120         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
121                                       ISC_FALSE));
122         if (token.value.as_ulong > 0xffffU)
123                 RETTOK(ISC_R_RANGE);
124         RETERR(uint16_tobuffer(token.value.as_ulong, target));
125
126         /*
127          * Other Data.
128          */
129         return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
130 }
131
132 static inline isc_result_t
133 totext_any_tsig(ARGS_TOTEXT) {
134         isc_region_t sr;
135         isc_region_t sigr;
136         char buf[sizeof(" 281474976710655 ")];
137         char *bufp;
138         dns_name_t name;
139         dns_name_t prefix;
140         isc_boolean_t sub;
141         isc_uint64_t sigtime;
142         unsigned short n;
143
144         REQUIRE(rdata->type == 250);
145         REQUIRE(rdata->rdclass == 255);
146         REQUIRE(rdata->length != 0);
147
148         dns_rdata_toregion(rdata, &sr);
149         /*
150          * Algorithm Name.
151          */
152         dns_name_init(&name, NULL);
153         dns_name_init(&prefix, NULL);
154         dns_name_fromregion(&name, &sr);
155         sub = name_prefix(&name, tctx->origin, &prefix);
156         RETERR(dns_name_totext(&prefix, sub, target));
157         RETERR(str_totext(" ", target));
158         isc_region_consume(&sr, name_length(&name));
159
160         /*
161          * Time Signed.
162          */
163         sigtime = ((isc_uint64_t)sr.base[0] << 40) |
164                   ((isc_uint64_t)sr.base[1] << 32) |
165                   ((isc_uint64_t)sr.base[2] << 24) |
166                   ((isc_uint64_t)sr.base[3] << 16) |
167                   ((isc_uint64_t)sr.base[4] << 8) |
168                   (isc_uint64_t)sr.base[5];
169         isc_region_consume(&sr, 6);
170         bufp = &buf[sizeof(buf) - 1];
171         *bufp-- = 0;
172         *bufp-- = ' ';
173         do {
174                 *bufp-- = decdigits[sigtime % 10];
175                 sigtime /= 10;
176         } while (sigtime != 0);
177         bufp++;
178         RETERR(str_totext(bufp, target));
179
180         /*
181          * Fudge.
182          */
183         n = uint16_fromregion(&sr);
184         isc_region_consume(&sr, 2);
185         sprintf(buf, "%u ", n);
186         RETERR(str_totext(buf, target));
187
188         /*
189          * Signature Size.
190          */
191         n = uint16_fromregion(&sr);
192         isc_region_consume(&sr, 2);
193         sprintf(buf, "%u", n);
194         RETERR(str_totext(buf, target));
195
196         /*
197          * Signature.
198          */
199         REQUIRE(n <= sr.length);
200         sigr = sr;
201         sigr.length = n;
202         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
203                 RETERR(str_totext(" (", target));
204         RETERR(str_totext(tctx->linebreak, target));
205         RETERR(isc_base64_totext(&sigr, tctx->width - 2,
206                                  tctx->linebreak, target));
207         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
208                 RETERR(str_totext(" ) ", target));
209         else
210                 RETERR(str_totext(" ", target));
211         isc_region_consume(&sr, n);
212
213         /*
214          * Original ID.
215          */
216         n = uint16_fromregion(&sr);
217         isc_region_consume(&sr, 2);
218         sprintf(buf, "%u ", n);
219         RETERR(str_totext(buf, target));
220
221         /*
222          * Error.
223          */
224         n = uint16_fromregion(&sr);
225         isc_region_consume(&sr, 2);
226         RETERR(dns_tsigrcode_totext((dns_rcode_t)n, target));
227
228         /*
229          * Other Size.
230          */
231         n = uint16_fromregion(&sr);
232         isc_region_consume(&sr, 2);
233         sprintf(buf, " %u ", n);
234         RETERR(str_totext(buf, target));
235
236         /*
237          * Other.
238          */
239         return (isc_base64_totext(&sr, 60, " ", target));
240 }
241
242 static inline isc_result_t
243 fromwire_any_tsig(ARGS_FROMWIRE) {
244         isc_region_t sr;
245         dns_name_t name;
246         unsigned long n;
247
248         REQUIRE(type == 250);
249         REQUIRE(rdclass == 255);
250
251         UNUSED(type);
252         UNUSED(rdclass);
253
254         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
255
256         /*
257          * Algorithm Name.
258          */
259         dns_name_init(&name, NULL);
260         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
261
262         isc_buffer_activeregion(source, &sr);
263         /*
264          * Time Signed + Fudge.
265          */
266         if (sr.length < 8)
267                 return (ISC_R_UNEXPECTEDEND);
268         RETERR(mem_tobuffer(target, sr.base, 8));
269         isc_region_consume(&sr, 8);
270         isc_buffer_forward(source, 8);
271
272         /*
273          * Signature Length + Signature.
274          */
275         if (sr.length < 2)
276                 return (ISC_R_UNEXPECTEDEND);
277         n = uint16_fromregion(&sr);
278         if (sr.length < n + 2)
279                 return (ISC_R_UNEXPECTEDEND);
280         RETERR(mem_tobuffer(target, sr.base, n + 2));
281         isc_region_consume(&sr, n + 2);
282         isc_buffer_forward(source, n + 2);
283
284         /*
285          * Original ID + Error.
286          */
287         if (sr.length < 4)
288                 return (ISC_R_UNEXPECTEDEND);
289         RETERR(mem_tobuffer(target, sr.base,  4));
290         isc_region_consume(&sr, 4);
291         isc_buffer_forward(source, 4);
292
293         /*
294          * Other Length + Other.
295          */
296         if (sr.length < 2)
297                 return (ISC_R_UNEXPECTEDEND);
298         n = uint16_fromregion(&sr);
299         if (sr.length < n + 2)
300                 return (ISC_R_UNEXPECTEDEND);
301         isc_buffer_forward(source, n + 2);
302         return (mem_tobuffer(target, sr.base, n + 2));
303 }
304
305 static inline isc_result_t
306 towire_any_tsig(ARGS_TOWIRE) {
307         isc_region_t sr;
308         dns_name_t name;
309         dns_offsets_t offsets;
310
311         REQUIRE(rdata->type == 250);
312         REQUIRE(rdata->rdclass == 255);
313         REQUIRE(rdata->length != 0);
314
315         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
316         dns_rdata_toregion(rdata, &sr);
317         dns_name_init(&name, offsets);
318         dns_name_fromregion(&name, &sr);
319         RETERR(dns_name_towire(&name, cctx, target));
320         isc_region_consume(&sr, name_length(&name));
321         return (mem_tobuffer(target, sr.base, sr.length));
322 }
323
324 static inline int
325 compare_any_tsig(ARGS_COMPARE) {
326         isc_region_t r1;
327         isc_region_t r2;
328         dns_name_t name1;
329         dns_name_t name2;
330         int order;
331
332         REQUIRE(rdata1->type == rdata2->type);
333         REQUIRE(rdata1->rdclass == rdata2->rdclass);
334         REQUIRE(rdata1->type == 250);
335         REQUIRE(rdata1->rdclass == 255);
336         REQUIRE(rdata1->length != 0);
337         REQUIRE(rdata2->length != 0);
338
339         dns_rdata_toregion(rdata1, &r1);
340         dns_rdata_toregion(rdata2, &r2);
341         dns_name_init(&name1, NULL);
342         dns_name_init(&name2, NULL);
343         dns_name_fromregion(&name1, &r1);
344         dns_name_fromregion(&name2, &r2);
345         order = dns_name_rdatacompare(&name1, &name2);
346         if (order != 0)
347                 return (order);
348         isc_region_consume(&r1, name_length(&name1));
349         isc_region_consume(&r2, name_length(&name2));
350         return (isc_region_compare(&r1, &r2));
351 }
352
353 static inline isc_result_t
354 fromstruct_any_tsig(ARGS_FROMSTRUCT) {
355         dns_rdata_any_tsig_t *tsig = source;
356         isc_region_t tr;
357
358         REQUIRE(type == 250);
359         REQUIRE(rdclass == 255);
360         REQUIRE(source != NULL);
361         REQUIRE(tsig->common.rdclass == rdclass);
362         REQUIRE(tsig->common.rdtype == type);
363
364         UNUSED(type);
365         UNUSED(rdclass);
366
367         /*
368          * Algorithm Name.
369          */
370         RETERR(name_tobuffer(&tsig->algorithm, target));
371
372         isc_buffer_availableregion(target, &tr);
373         if (tr.length < 6 + 2 + 2)
374                 return (ISC_R_NOSPACE);
375
376         /*
377          * Time Signed: 48 bits.
378          */
379         RETERR(uint16_tobuffer((isc_uint16_t)(tsig->timesigned >> 32),
380                                target));
381         RETERR(uint32_tobuffer((isc_uint32_t)(tsig->timesigned & 0xffffffffU),
382                                target));
383
384         /*
385          * Fudge.
386          */
387         RETERR(uint16_tobuffer(tsig->fudge, target));
388
389         /*
390          * Signature Size.
391          */
392         RETERR(uint16_tobuffer(tsig->siglen, target));
393
394         /*
395          * Signature.
396          */
397         RETERR(mem_tobuffer(target, tsig->signature, tsig->siglen));
398
399         isc_buffer_availableregion(target, &tr);
400         if (tr.length < 2 + 2 + 2)
401                 return (ISC_R_NOSPACE);
402
403         /*
404          * Original ID.
405          */
406         RETERR(uint16_tobuffer(tsig->originalid, target));
407
408         /*
409          * Error.
410          */
411         RETERR(uint16_tobuffer(tsig->error, target));
412
413         /*
414          * Other Len.
415          */
416         RETERR(uint16_tobuffer(tsig->otherlen, target));
417
418         /*
419          * Other Data.
420          */
421         return (mem_tobuffer(target, tsig->other, tsig->otherlen));
422 }
423
424 static inline isc_result_t
425 tostruct_any_tsig(ARGS_TOSTRUCT) {
426         dns_rdata_any_tsig_t *tsig;
427         dns_name_t alg;
428         isc_region_t sr;
429
430         REQUIRE(rdata->type == 250);
431         REQUIRE(rdata->rdclass == 255);
432         REQUIRE(rdata->length != 0);
433
434         tsig = (dns_rdata_any_tsig_t *) target;
435         tsig->common.rdclass = rdata->rdclass;
436         tsig->common.rdtype = rdata->type;
437         ISC_LINK_INIT(&tsig->common, link);
438
439         dns_rdata_toregion(rdata, &sr);
440
441         /*
442          * Algorithm Name.
443          */
444         dns_name_init(&alg, NULL);
445         dns_name_fromregion(&alg, &sr);
446         dns_name_init(&tsig->algorithm, NULL);
447         RETERR(name_duporclone(&alg, mctx, &tsig->algorithm));
448
449         isc_region_consume(&sr, name_length(&tsig->algorithm));
450
451         /*
452          * Time Signed.
453          */
454         INSIST(sr.length >= 6);
455         tsig->timesigned = ((isc_uint64_t)sr.base[0] << 40) |
456                            ((isc_uint64_t)sr.base[1] << 32) |
457                            ((isc_uint64_t)sr.base[2] << 24) |
458                            ((isc_uint64_t)sr.base[3] << 16) |
459                            ((isc_uint64_t)sr.base[4] << 8) |
460                            (isc_uint64_t)sr.base[5];
461         isc_region_consume(&sr, 6);
462
463         /*
464          * Fudge.
465          */
466         tsig->fudge = uint16_fromregion(&sr);
467         isc_region_consume(&sr, 2);
468
469         /*
470          * Signature Size.
471          */
472         tsig->siglen = uint16_fromregion(&sr);
473         isc_region_consume(&sr, 2);
474
475         /*
476          * Signature.
477          */
478         INSIST(sr.length >= tsig->siglen);
479         tsig->signature = mem_maybedup(mctx, sr.base, tsig->siglen);
480         if (tsig->signature == NULL)
481                 goto cleanup;
482         isc_region_consume(&sr, tsig->siglen);
483
484         /*
485          * Original ID.
486          */
487         tsig->originalid = uint16_fromregion(&sr);
488         isc_region_consume(&sr, 2);
489
490         /*
491          * Error.
492          */
493         tsig->error = uint16_fromregion(&sr);
494         isc_region_consume(&sr, 2);
495
496         /*
497          * Other Size.
498          */
499         tsig->otherlen = uint16_fromregion(&sr);
500         isc_region_consume(&sr, 2);
501
502         /*
503          * Other.
504          */
505         INSIST(sr.length == tsig->otherlen);
506         tsig->other = mem_maybedup(mctx, sr.base, tsig->otherlen);
507         if (tsig->other == NULL)
508                 goto cleanup;
509
510         tsig->mctx = mctx;
511         return (ISC_R_SUCCESS);
512
513  cleanup:
514         if (mctx != NULL)
515                 dns_name_free(&tsig->algorithm, tsig->mctx);
516         if (mctx != NULL && tsig->signature != NULL)
517                 isc_mem_free(mctx, tsig->signature);
518         return (ISC_R_NOMEMORY);
519 }
520
521 static inline void
522 freestruct_any_tsig(ARGS_FREESTRUCT) {
523         dns_rdata_any_tsig_t *tsig = (dns_rdata_any_tsig_t *) source;
524
525         REQUIRE(source != NULL);
526         REQUIRE(tsig->common.rdclass == 255);
527         REQUIRE(tsig->common.rdtype == 250);
528
529         if (tsig->mctx == NULL)
530                 return;
531
532         dns_name_free(&tsig->algorithm, tsig->mctx);
533         if (tsig->signature != NULL)
534                 isc_mem_free(tsig->mctx, tsig->signature);
535         if (tsig->other != NULL)
536                 isc_mem_free(tsig->mctx, tsig->other);
537         tsig->mctx = NULL;
538 }
539
540 static inline isc_result_t
541 additionaldata_any_tsig(ARGS_ADDLDATA) {
542         REQUIRE(rdata->type == 250);
543         REQUIRE(rdata->rdclass == 255);
544
545         UNUSED(rdata);
546         UNUSED(add);
547         UNUSED(arg);
548
549         return (ISC_R_SUCCESS);
550 }
551
552 static inline isc_result_t
553 digest_any_tsig(ARGS_DIGEST) {
554
555         REQUIRE(rdata->type == 250);
556         REQUIRE(rdata->rdclass == 255);
557
558         UNUSED(rdata);
559         UNUSED(digest);
560         UNUSED(arg);
561
562         return (ISC_R_NOTIMPLEMENTED);
563 }
564
565 static inline isc_boolean_t
566 checkowner_any_tsig(ARGS_CHECKOWNER) {
567
568         REQUIRE(type == 250);
569         REQUIRE(rdclass == 255);
570
571         UNUSED(name);
572         UNUSED(type);
573         UNUSED(rdclass);
574         UNUSED(wildcard);
575
576         return (ISC_TRUE);
577 }
578
579 static inline isc_boolean_t
580 checknames_any_tsig(ARGS_CHECKNAMES) {
581
582         REQUIRE(rdata->type == 250);
583         REQUIRE(rdata->rdclass == 250);
584
585         UNUSED(rdata);
586         UNUSED(owner);
587         UNUSED(bad);
588
589         return (ISC_TRUE);
590 }
591
592 static inline int
593 casecompare_any_tsig(ARGS_COMPARE) {
594         return (compare_any_tsig(rdata1, rdata2));
595 }
596
597 #endif  /* RDATA_ANY_255_TSIG_250_C */