xref: /haiku/src/system/libnetwork/netresolv/dst/support.c (revision c9afad22682e9f15753db4e9ca8684e2d4a643a9)
1 /*	$NetBSD: support.c,v 1.2 2014/10/18 08:33:23 snj Exp $	*/
2 
3 /*
4  * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
5  *
6  * Permission to use, copy modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
11  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
13  * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17  * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
18  */
19 #include <sys/cdefs.h>
20 #if 0
21 static const char rcsid[] = "Header: /proj/cvs/prod/libbind/dst/support.c,v 1.6 2005/10/11 00:10:13 marka Exp ";
22 #endif
23 
24 #include "port_before.h"
25 
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <memory.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
34 #include <resolv.h>
35 
36 #include "dst_internal.h"
37 
38 #include "port_after.h"
39 
40 /*%
41  * dst_s_verify_str()
42  *     Validate that the input string(*str) is at the head of the input
43  *     buffer(**buf).  If so, move the buffer head pointer (*buf) to
44  *     the first byte of data following the string(*str).
45  * Parameters
46  *     buf     Input buffer.
47  *     str     Input string.
48  * Return
49  *	0       *str is not the head of **buff
50  *	1       *str is the head of **buff, *buf is is advanced to
51  *	the tail of **buf.
52  */
53 
54 int
55 dst_s_verify_str(const char **buf, const char *str)
56 {
57 	size_t b, s;
58 	if (*buf == NULL)	/*%< error checks */
59 		return (0);
60 	if (str == NULL || *str == '\0')
61 		return (1);
62 
63 	b = strlen(*buf);	/*%< get length of strings */
64 	s = strlen(str);
65 	if (s > b || strncmp(*buf, str, s))	/*%< check if same */
66 		return (0);	/*%< not a match */
67 	(*buf) += s;		/*%< advance pointer */
68 	return (1);
69 }
70 
71 /*%
72  * dst_s_calculate_bits
73  *     Given a binary number represented in a u_char[], determine
74  *     the number of significant bits used.
75  * Parameters
76  *     str       An input character string containing a binary number.
77  *     max_bits The maximum possible significant bits.
78  * Return
79  *     N       The number of significant bits in str.
80  */
81 
82 int
83 dst_s_calculate_bits(const u_char *str, const int max_bits)
84 {
85 	const u_char *p = str;
86 	u_char i, j = 0x80;
87 	int bits;
88 	for (bits = max_bits; *p == 0x00 && bits > 0; p++)
89 		bits -= 8;
90 	for (i = *p; (i & j) != j; j >>= 1)
91 		bits--;
92 	return (bits);
93 }
94 
95 /*%
96  * calculates a checksum used in dst for an id.
97  * takes an array of bytes and a length.
98  * returns a 16  bit checksum.
99  */
100 u_int16_t
101 dst_s_id_calc(const u_char *key, const int keysize)
102 {
103 	u_int32_t ac;
104 	const u_char *kp = key;
105 	int size = keysize;
106 
107 	if (!key || (keysize <= 0))
108 		return (0xffffU);
109 
110 	for (ac = 0; size > 1; size -= 2, kp += 2)
111 		ac += ((*kp) << 8) + *(kp + 1);
112 
113 	if (size > 0)
114 		ac += ((*kp) << 8);
115 	ac += (ac >> 16) & 0xffff;
116 
117 	return (ac & 0xffff);
118 }
119 
120 /*%
121  * dst_s_dns_key_id() Function to calculate DNSSEC footprint from KEY record
122  *   rdata
123  * Input:
124  *	dns_key_rdata: the raw data in wire format
125  *      rdata_len: the size of the input data
126  * Output:
127  *      the key footprint/id calculated from the key data
128  */
129 u_int16_t
130 dst_s_dns_key_id(const u_char *dns_key_rdata, const int rdata_len)
131 {
132 	if (!dns_key_rdata)
133 		return 0;
134 
135 	/* compute id */
136 	if (dns_key_rdata[3] == KEY_RSA)	/*%< Algorithm RSA */
137 		return dst_s_get_int16((const u_char *)
138 				       &dns_key_rdata[rdata_len - 3]);
139 	else if (dns_key_rdata[3] == KEY_HMAC_MD5)
140 		/* compatibility */
141 		return 0;
142 	else
143 		/* compute a checksum on the key part of the key rr */
144 		return dst_s_id_calc(dns_key_rdata, rdata_len);
145 }
146 
147 /*%
148  * dst_s_get_int16
149  *     This routine extracts a 16 bit integer from a two byte character
150  *     string.  The character string is assumed to be in network byte
151  *     order and may be unaligned.  The number returned is in host order.
152  * Parameter
153  *     buf     A two byte character string.
154  * Return
155  *     The converted integer value.
156  */
157 
158 u_int16_t
159 dst_s_get_int16(const u_char *buf)
160 {
161 	register u_int16_t a = 0;
162 	a = ((u_int16_t)(buf[0] << 8)) | ((u_int16_t)(buf[1]));
163 	return (a);
164 }
165 
166 /*%
167  * dst_s_get_int32
168  *     This routine extracts a 32 bit integer from a four byte character
169  *     string.  The character string is assumed to be in network byte
170  *     order and may be unaligned.  The number returned is in host order.
171  * Parameter
172  *     buf     A four byte character string.
173  * Return
174  *     The converted integer value.
175  */
176 
177 u_int32_t
178 dst_s_get_int32(const u_char *buf)
179 {
180 	register u_int32_t a = 0;
181 	a = ((u_int32_t)(buf[0] << 24)) | ((u_int32_t)(buf[1] << 16)) |
182 		((u_int32_t)(buf[2] << 8)) | ((u_int32_t)(buf[3]));
183 	return (a);
184 }
185 
186 /*%
187  * dst_s_put_int16
188  *     Take a 16 bit integer and store the value in a two byte
189  *     character string.  The integer is assumed to be in network
190  *     order and the string is returned in host order.
191  *
192  * Parameters
193  *     buf     Storage for a two byte character string.
194  *     val     16 bit integer.
195  */
196 
197 void
198 dst_s_put_int16(u_int8_t *buf, const u_int16_t val)
199 {
200 	buf[0] = (u_int8_t)((uint32_t)val >> 8);
201 	buf[1] = (u_int8_t)(val);
202 }
203 
204 /*%
205  * dst_s_put_int32
206  *     Take a 32 bit integer and store the value in a four byte
207  *     character string.  The integer is assumed to be in network
208  *     order and the string is returned in host order.
209  *
210  * Parameters
211  *     buf     Storage for a four byte character string.
212  *     val     32 bit integer.
213  */
214 
215 void
216 dst_s_put_int32(u_int8_t *buf, const u_int32_t val)
217 {
218 	buf[0] = (u_int8_t)(val >> 24);
219 	buf[1] = (u_int8_t)(val >> 16);
220 	buf[2] = (u_int8_t)(val >> 8);
221 	buf[3] = (u_int8_t)(val);
222 }
223 
224 /*%
225  *  dst_s_filename_length
226  *
227  *	This function returns the number of bytes needed to hold the
228  *	filename for a key file.  '/', '\' and ':' are not allowed.
229  *	form:  K&lt;keyname&gt;+&lt;alg&gt;+&lt;id&gt;.&lt;suffix&gt;
230  *
231  *	Returns 0 if the filename would contain either '\', '/' or ':'
232  */
233 size_t
234 dst_s_filename_length(const char *name, const char *suffix)
235 {
236 	if (name == NULL)
237 		return (0);
238 	if (strrchr(name, '\\'))
239 		return (0);
240 	if (strrchr(name, '/'))
241 		return (0);
242 	if (strrchr(name, ':'))
243 		return (0);
244 	if (suffix == NULL)
245 		return (0);
246 	if (strrchr(suffix, '\\'))
247 		return (0);
248 	if (strrchr(suffix, '/'))
249 		return (0);
250 	if (strrchr(suffix, ':'))
251 		return (0);
252 	return (1 + strlen(name) + 6 + strlen(suffix));
253 }
254 
255 /*%
256  *  dst_s_build_filename ()
257  *	Builds a key filename from the key name, its id, and a
258  *	suffix.  '\', '/' and ':' are not allowed. fA filename is of the
259  *	form:  K&lt;keyname&gt;&lt;id&gt;.&lt;suffix&gt;
260  *	form: K&lt;keyname&gt;+&lt;alg&gt;+&lt;id&gt;.&lt;suffix&gt;
261  *
262  *	Returns -1 if the conversion fails:
263  *	  if the filename would be too long for space allotted
264  *	  if the filename would contain a '\', '/' or ':'
265  *	Returns 0 on success
266  */
267 
268 int
269 dst_s_build_filename(char *filename, const char *name, u_int16_t id,
270 		     int alg, const char *suffix, size_t filename_length)
271 {
272 	u_int32_t my_id;
273 	if (filename == NULL)
274 		return (-1);
275 	memset(filename, 0, filename_length);
276 	if (name == NULL)
277 		return (-1);
278 	if (suffix == NULL)
279 		return (-1);
280 	if (filename_length < 1 + strlen(name) + 4 + 6 + 1 + strlen(suffix))
281 		return (-1);
282 	my_id = id;
283 	sprintf(filename, "K%s+%03d+%05d.%s", name, alg, my_id,
284 		(const char *) suffix);
285 	if (strrchr(filename, '/'))
286 		return (-1);
287 	if (strrchr(filename, '\\'))
288 		return (-1);
289 	if (strrchr(filename, ':'))
290 		return (-1);
291 	return (0);
292 }
293 
294 /*%
295  *  dst_s_fopen ()
296  *     Open a file in the dst_path directory.  If perm is specified, the
297  *     file is checked for existence first, and not opened if it exists.
298  *  Parameters
299  *     filename  File to open
300  *     mode       Mode to open the file (passed directly to fopen)
301  *     perm       File permission, if creating a new file.
302  *  Returns
303  *     NULL       Failure
304  *     NON-NULL  (FILE *) of opened file.
305  */
306 FILE *
307 dst_s_fopen(const char *filename, const char *mode, int perm)
308 {
309 	FILE *fp;
310 	char pathname[PATH_MAX];
311 
312 	if (strlen(filename) + strlen(dst_path) >= sizeof(pathname))
313 		return (NULL);
314 
315 	if (*dst_path != '\0') {
316 		strcpy(pathname, dst_path);
317 		strcat(pathname, filename);
318 	} else
319 		strcpy(pathname, filename);
320 
321 	fp = fopen(pathname, mode);
322 	if (perm)
323 		chmod(pathname, (mode_t)perm);
324 	return (fp);
325 }
326 
327 void
328 dst_s_dump(const int mode, const u_char *data, const int size,
329 	    const char *msg)
330 {
331 	UNUSED(data);
332 
333 	if (size > 0) {
334 #ifdef LONG_TEST
335 		static u_char scratch[1000];
336 		int n ;
337 		n = b64_ntop(data, scratch, size, sizeof(scratch));
338 		printf("%s: %x %d %s\n", msg, mode, n, scratch);
339 #else
340 		printf("%s,%x %d\n", msg, mode, size);
341 #endif
342 	}
343 }
344 
345 /*! \file */
346