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