1 /*	$NetBSD: spf_99.c,v 1.5 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2002  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: spf_99.c,v 1.6 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Thu Mar 16 15:40:00 PST 2000 by bwelling */
23 
24 #ifndef RDATA_GENERIC_SPF_99_C
25 #define RDATA_GENERIC_SPF_99_C
26 
27 #define RRTYPE_SPF_ATTRIBUTES (0)
28 
29 static inline isc_result_t
30 fromtext_spf(ARGS_FROMTEXT) {
31 	isc_token_t token;
32 	int strings;
33 
34 	REQUIRE(type == 99);
35 
36 	UNUSED(type);
37 	UNUSED(rdclass);
38 	UNUSED(origin);
39 	UNUSED(options);
40 	UNUSED(callbacks);
41 
42 	strings = 0;
43 	for (;;) {
44 		RETERR(isc_lex_getmastertoken(lexer, &token,
45 					      isc_tokentype_qstring,
46 					      ISC_TRUE));
47 		if (token.type != isc_tokentype_qstring &&
48 		    token.type != isc_tokentype_string)
49 			break;
50 		RETTOK(txt_fromtext(&token.value.as_textregion, target));
51 		strings++;
52 	}
53 	/* Let upper layer handle eol/eof. */
54 	isc_lex_ungettoken(lexer, &token);
55 	return (strings == 0 ? ISC_R_UNEXPECTEDEND : ISC_R_SUCCESS);
56 }
57 
58 static inline isc_result_t
59 totext_spf(ARGS_TOTEXT) {
60 	isc_region_t region;
61 
62 	UNUSED(tctx);
63 
64 	REQUIRE(rdata->type == 99);
65 
66 	dns_rdata_toregion(rdata, &region);
67 
68 	while (region.length > 0) {
69 		RETERR(txt_totext(&region, ISC_TRUE, target));
70 		if (region.length > 0)
71 			RETERR(str_totext(" ", target));
72 	}
73 
74 	return (ISC_R_SUCCESS);
75 }
76 
77 static inline isc_result_t
78 fromwire_spf(ARGS_FROMWIRE) {
79 	isc_result_t result;
80 
81 	REQUIRE(type == 99);
82 
83 	UNUSED(type);
84 	UNUSED(dctx);
85 	UNUSED(rdclass);
86 	UNUSED(options);
87 
88 	do {
89 		result = txt_fromwire(source, target);
90 		if (result != ISC_R_SUCCESS)
91 			return (result);
92 	} while (!buffer_empty(source));
93 	return (ISC_R_SUCCESS);
94 }
95 
96 static inline isc_result_t
97 towire_spf(ARGS_TOWIRE) {
98 	isc_region_t region;
99 
100 	REQUIRE(rdata->type == 99);
101 
102 	UNUSED(cctx);
103 
104 	isc_buffer_availableregion(target, &region);
105 	if (region.length < rdata->length)
106 		return (ISC_R_NOSPACE);
107 
108 	memmove(region.base, rdata->data, rdata->length);
109 	isc_buffer_add(target, rdata->length);
110 	return (ISC_R_SUCCESS);
111 }
112 
113 static inline int
114 compare_spf(ARGS_COMPARE) {
115 	isc_region_t r1;
116 	isc_region_t r2;
117 
118 	REQUIRE(rdata1->type == rdata2->type);
119 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
120 	REQUIRE(rdata1->type == 99);
121 
122 	dns_rdata_toregion(rdata1, &r1);
123 	dns_rdata_toregion(rdata2, &r2);
124 	return (isc_region_compare(&r1, &r2));
125 }
126 
127 static inline isc_result_t
128 fromstruct_spf(ARGS_FROMSTRUCT) {
129 	dns_rdata_spf_t *txt = source;
130 	isc_region_t region;
131 	isc_uint8_t length;
132 
133 	REQUIRE(type == 99);
134 	REQUIRE(source != NULL);
135 	REQUIRE(txt->common.rdtype == type);
136 	REQUIRE(txt->common.rdclass == rdclass);
137 	REQUIRE(txt->txt != NULL && txt->txt_len != 0);
138 
139 	UNUSED(type);
140 	UNUSED(rdclass);
141 
142 	region.base = txt->txt;
143 	region.length = txt->txt_len;
144 	while (region.length > 0) {
145 		length = uint8_fromregion(&region);
146 		isc_region_consume(&region, 1);
147 		if (region.length <= length)
148 			return (ISC_R_UNEXPECTEDEND);
149 		isc_region_consume(&region, length);
150 	}
151 
152 	return (mem_tobuffer(target, txt->txt, txt->txt_len));
153 }
154 
155 static inline isc_result_t
156 tostruct_spf(ARGS_TOSTRUCT) {
157 	dns_rdata_spf_t *txt = target;
158 	isc_region_t r;
159 
160 	REQUIRE(rdata->type == 99);
161 	REQUIRE(target != NULL);
162 
163 	txt->common.rdclass = rdata->rdclass;
164 	txt->common.rdtype = rdata->type;
165 	ISC_LINK_INIT(&txt->common, link);
166 
167 	dns_rdata_toregion(rdata, &r);
168 	txt->txt_len = r.length;
169 	txt->txt = mem_maybedup(mctx, r.base, r.length);
170 	if (txt->txt == NULL)
171 		return (ISC_R_NOMEMORY);
172 
173 	txt->offset = 0;
174 	txt->mctx = mctx;
175 	return (ISC_R_SUCCESS);
176 }
177 
178 static inline void
179 freestruct_spf(ARGS_FREESTRUCT) {
180 	dns_rdata_spf_t *txt = source;
181 
182 	REQUIRE(source != NULL);
183 	REQUIRE(txt->common.rdtype == 99);
184 
185 	if (txt->mctx == NULL)
186 		return;
187 
188 	if (txt->txt != NULL)
189 		isc_mem_free(txt->mctx, txt->txt);
190 	txt->mctx = NULL;
191 }
192 
193 static inline isc_result_t
194 additionaldata_spf(ARGS_ADDLDATA) {
195 	REQUIRE(rdata->type == 99);
196 
197 	UNUSED(rdata);
198 	UNUSED(add);
199 	UNUSED(arg);
200 
201 	return (ISC_R_SUCCESS);
202 }
203 
204 static inline isc_result_t
205 digest_spf(ARGS_DIGEST) {
206 	isc_region_t r;
207 
208 	REQUIRE(rdata->type == 99);
209 
210 	dns_rdata_toregion(rdata, &r);
211 
212 	return ((digest)(arg, &r));
213 }
214 
215 static inline isc_boolean_t
216 checkowner_spf(ARGS_CHECKOWNER) {
217 
218 	REQUIRE(type == 99);
219 
220 	UNUSED(name);
221 	UNUSED(type);
222 	UNUSED(rdclass);
223 	UNUSED(wildcard);
224 
225 	return (ISC_TRUE);
226 }
227 
228 static inline isc_boolean_t
229 checknames_spf(ARGS_CHECKNAMES) {
230 
231 	REQUIRE(rdata->type == 99);
232 
233 	UNUSED(rdata);
234 	UNUSED(owner);
235 	UNUSED(bad);
236 
237 	return (ISC_TRUE);
238 }
239 
240 static inline int
241 casecompare_spf(ARGS_COMPARE) {
242 	return (compare_spf(rdata1, rdata2));
243 }
244 #endif	/* RDATA_GENERIC_SPF_99_C */
245