1*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Header: /proj/cvs/prod/libbind/dst/support.c,v 1.6 2005/10/11 00:10:13 marka Exp $";
20Sstevel@tonic-gate
30Sstevel@tonic-gate
40Sstevel@tonic-gate /*
50Sstevel@tonic-gate * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * Permission to use, copy modify, and distribute this software for any
80Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
90Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
120Sstevel@tonic-gate * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
130Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
140Sstevel@tonic-gate * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
150Sstevel@tonic-gate * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
160Sstevel@tonic-gate * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
170Sstevel@tonic-gate * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
180Sstevel@tonic-gate * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
190Sstevel@tonic-gate */
200Sstevel@tonic-gate
210Sstevel@tonic-gate #include "port_before.h"
220Sstevel@tonic-gate
230Sstevel@tonic-gate #include <stdio.h>
240Sstevel@tonic-gate #include <unistd.h>
250Sstevel@tonic-gate #include <memory.h>
260Sstevel@tonic-gate #include <string.h>
270Sstevel@tonic-gate #include <errno.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <netinet/in.h>
300Sstevel@tonic-gate #include <arpa/nameser.h>
310Sstevel@tonic-gate #include <resolv.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "dst_internal.h"
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include "port_after.h"
360Sstevel@tonic-gate
37*11038SRao.Shoaib@Sun.COM /*%
380Sstevel@tonic-gate * dst_s_verify_str()
390Sstevel@tonic-gate * Validate that the input string(*str) is at the head of the input
400Sstevel@tonic-gate * buffer(**buf). If so, move the buffer head pointer (*buf) to
410Sstevel@tonic-gate * the first byte of data following the string(*str).
420Sstevel@tonic-gate * Parameters
430Sstevel@tonic-gate * buf Input buffer.
440Sstevel@tonic-gate * str Input string.
450Sstevel@tonic-gate * Return
460Sstevel@tonic-gate * 0 *str is not the head of **buff
470Sstevel@tonic-gate * 1 *str is the head of **buff, *buf is is advanced to
480Sstevel@tonic-gate * the tail of **buf.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
510Sstevel@tonic-gate int
dst_s_verify_str(const char ** buf,const char * str)520Sstevel@tonic-gate dst_s_verify_str(const char **buf, const char *str)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate int b, s;
55*11038SRao.Shoaib@Sun.COM if (*buf == NULL) /*%< error checks */
560Sstevel@tonic-gate return (0);
570Sstevel@tonic-gate if (str == NULL || *str == '\0')
580Sstevel@tonic-gate return (1);
590Sstevel@tonic-gate
60*11038SRao.Shoaib@Sun.COM b = strlen(*buf); /*%< get length of strings */
610Sstevel@tonic-gate s = strlen(str);
62*11038SRao.Shoaib@Sun.COM if (s > b || strncmp(*buf, str, s)) /*%< check if same */
63*11038SRao.Shoaib@Sun.COM return (0); /*%< not a match */
64*11038SRao.Shoaib@Sun.COM (*buf) += s; /*%< advance pointer */
650Sstevel@tonic-gate return (1);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
68*11038SRao.Shoaib@Sun.COM /*%
690Sstevel@tonic-gate * dst_s_calculate_bits
700Sstevel@tonic-gate * Given a binary number represented in a u_char[], determine
710Sstevel@tonic-gate * the number of significant bits used.
720Sstevel@tonic-gate * Parameters
730Sstevel@tonic-gate * str An input character string containing a binary number.
740Sstevel@tonic-gate * max_bits The maximum possible significant bits.
750Sstevel@tonic-gate * Return
760Sstevel@tonic-gate * N The number of significant bits in str.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate
790Sstevel@tonic-gate int
dst_s_calculate_bits(const u_char * str,const int max_bits)800Sstevel@tonic-gate dst_s_calculate_bits(const u_char *str, const int max_bits)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate const u_char *p = str;
830Sstevel@tonic-gate u_char i, j = 0x80;
840Sstevel@tonic-gate int bits;
850Sstevel@tonic-gate for (bits = max_bits; *p == 0x00 && bits > 0; p++)
860Sstevel@tonic-gate bits -= 8;
870Sstevel@tonic-gate for (i = *p; (i & j) != j; j >>= 1)
880Sstevel@tonic-gate bits--;
890Sstevel@tonic-gate return (bits);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
92*11038SRao.Shoaib@Sun.COM /*%
930Sstevel@tonic-gate * calculates a checksum used in dst for an id.
940Sstevel@tonic-gate * takes an array of bytes and a length.
950Sstevel@tonic-gate * returns a 16 bit checksum.
960Sstevel@tonic-gate */
970Sstevel@tonic-gate u_int16_t
dst_s_id_calc(const u_char * key,const int keysize)980Sstevel@tonic-gate dst_s_id_calc(const u_char *key, const int keysize)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate u_int32_t ac;
1010Sstevel@tonic-gate const u_char *kp = key;
1020Sstevel@tonic-gate int size = keysize;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if (!key || (keysize <= 0))
105*11038SRao.Shoaib@Sun.COM return (0xffffU);
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate for (ac = 0; size > 1; size -= 2, kp += 2)
1080Sstevel@tonic-gate ac += ((*kp) << 8) + *(kp + 1);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (size > 0)
1110Sstevel@tonic-gate ac += ((*kp) << 8);
1120Sstevel@tonic-gate ac += (ac >> 16) & 0xffff;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate return (ac & 0xffff);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
117*11038SRao.Shoaib@Sun.COM /*%
1180Sstevel@tonic-gate * dst_s_dns_key_id() Function to calculate DNSSEC footprint from KEY record
1190Sstevel@tonic-gate * rdata
1200Sstevel@tonic-gate * Input:
1210Sstevel@tonic-gate * dns_key_rdata: the raw data in wire format
1220Sstevel@tonic-gate * rdata_len: the size of the input data
1230Sstevel@tonic-gate * Output:
1240Sstevel@tonic-gate * the key footprint/id calculated from the key data
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate u_int16_t
dst_s_dns_key_id(const u_char * dns_key_rdata,const int rdata_len)1270Sstevel@tonic-gate dst_s_dns_key_id(const u_char *dns_key_rdata, const int rdata_len)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate if (!dns_key_rdata)
1300Sstevel@tonic-gate return 0;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /* compute id */
133*11038SRao.Shoaib@Sun.COM if (dns_key_rdata[3] == KEY_RSA) /*%< Algorithm RSA */
1340Sstevel@tonic-gate return dst_s_get_int16((const u_char *)
1350Sstevel@tonic-gate &dns_key_rdata[rdata_len - 3]);
1360Sstevel@tonic-gate else if (dns_key_rdata[3] == KEY_HMAC_MD5)
1370Sstevel@tonic-gate /* compatibility */
1380Sstevel@tonic-gate return 0;
1390Sstevel@tonic-gate else
1400Sstevel@tonic-gate /* compute a checksum on the key part of the key rr */
1410Sstevel@tonic-gate return dst_s_id_calc(dns_key_rdata, rdata_len);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
144*11038SRao.Shoaib@Sun.COM /*%
1450Sstevel@tonic-gate * dst_s_get_int16
1460Sstevel@tonic-gate * This routine extracts a 16 bit integer from a two byte character
1470Sstevel@tonic-gate * string. The character string is assumed to be in network byte
1480Sstevel@tonic-gate * order and may be unaligned. The number returned is in host order.
1490Sstevel@tonic-gate * Parameter
1500Sstevel@tonic-gate * buf A two byte character string.
1510Sstevel@tonic-gate * Return
1520Sstevel@tonic-gate * The converted integer value.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate u_int16_t
dst_s_get_int16(const u_char * buf)1560Sstevel@tonic-gate dst_s_get_int16(const u_char *buf)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate register u_int16_t a = 0;
1590Sstevel@tonic-gate a = ((u_int16_t)(buf[0] << 8)) | ((u_int16_t)(buf[1]));
1600Sstevel@tonic-gate return (a);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
163*11038SRao.Shoaib@Sun.COM /*%
1640Sstevel@tonic-gate * dst_s_get_int32
1650Sstevel@tonic-gate * This routine extracts a 32 bit integer from a four byte character
1660Sstevel@tonic-gate * string. The character string is assumed to be in network byte
1670Sstevel@tonic-gate * order and may be unaligned. The number returned is in host order.
1680Sstevel@tonic-gate * Parameter
1690Sstevel@tonic-gate * buf A four byte character string.
1700Sstevel@tonic-gate * Return
1710Sstevel@tonic-gate * The converted integer value.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate u_int32_t
dst_s_get_int32(const u_char * buf)1750Sstevel@tonic-gate dst_s_get_int32(const u_char *buf)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate register u_int32_t a = 0;
1780Sstevel@tonic-gate a = ((u_int32_t)(buf[0] << 24)) | ((u_int32_t)(buf[1] << 16)) |
1790Sstevel@tonic-gate ((u_int32_t)(buf[2] << 8)) | ((u_int32_t)(buf[3]));
1800Sstevel@tonic-gate return (a);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
183*11038SRao.Shoaib@Sun.COM /*%
1840Sstevel@tonic-gate * dst_s_put_int16
1850Sstevel@tonic-gate * Take a 16 bit integer and store the value in a two byte
1860Sstevel@tonic-gate * character string. The integer is assumed to be in network
1870Sstevel@tonic-gate * order and the string is returned in host order.
1880Sstevel@tonic-gate *
1890Sstevel@tonic-gate * Parameters
1900Sstevel@tonic-gate * buf Storage for a two byte character string.
1910Sstevel@tonic-gate * val 16 bit integer.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate void
dst_s_put_int16(u_int8_t * buf,const u_int16_t val)1950Sstevel@tonic-gate dst_s_put_int16(u_int8_t *buf, const u_int16_t val)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate buf[0] = (u_int8_t)(val >> 8);
1980Sstevel@tonic-gate buf[1] = (u_int8_t)(val);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
201*11038SRao.Shoaib@Sun.COM /*%
2020Sstevel@tonic-gate * dst_s_put_int32
2030Sstevel@tonic-gate * Take a 32 bit integer and store the value in a four byte
2040Sstevel@tonic-gate * character string. The integer is assumed to be in network
2050Sstevel@tonic-gate * order and the string is returned in host order.
2060Sstevel@tonic-gate *
2070Sstevel@tonic-gate * Parameters
2080Sstevel@tonic-gate * buf Storage for a four byte character string.
2090Sstevel@tonic-gate * val 32 bit integer.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate void
dst_s_put_int32(u_int8_t * buf,const u_int32_t val)2130Sstevel@tonic-gate dst_s_put_int32(u_int8_t *buf, const u_int32_t val)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate buf[0] = (u_int8_t)(val >> 24);
2160Sstevel@tonic-gate buf[1] = (u_int8_t)(val >> 16);
2170Sstevel@tonic-gate buf[2] = (u_int8_t)(val >> 8);
2180Sstevel@tonic-gate buf[3] = (u_int8_t)(val);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
221*11038SRao.Shoaib@Sun.COM /*%
2220Sstevel@tonic-gate * dst_s_filename_length
2230Sstevel@tonic-gate *
2240Sstevel@tonic-gate * This function returns the number of bytes needed to hold the
2250Sstevel@tonic-gate * filename for a key file. '/', '\' and ':' are not allowed.
226*11038SRao.Shoaib@Sun.COM * form: K<keyname>+<alg>+<id>.<suffix>
2270Sstevel@tonic-gate *
2280Sstevel@tonic-gate * Returns 0 if the filename would contain either '\', '/' or ':'
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate size_t
dst_s_filename_length(const char * name,const char * suffix)2310Sstevel@tonic-gate dst_s_filename_length(const char *name, const char *suffix)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate if (name == NULL)
2340Sstevel@tonic-gate return (0);
2350Sstevel@tonic-gate if (strrchr(name, '\\'))
2360Sstevel@tonic-gate return (0);
2370Sstevel@tonic-gate if (strrchr(name, '/'))
2380Sstevel@tonic-gate return (0);
2390Sstevel@tonic-gate if (strrchr(name, ':'))
2400Sstevel@tonic-gate return (0);
2410Sstevel@tonic-gate if (suffix == NULL)
2420Sstevel@tonic-gate return (0);
2430Sstevel@tonic-gate if (strrchr(suffix, '\\'))
2440Sstevel@tonic-gate return (0);
2450Sstevel@tonic-gate if (strrchr(suffix, '/'))
2460Sstevel@tonic-gate return (0);
2470Sstevel@tonic-gate if (strrchr(suffix, ':'))
2480Sstevel@tonic-gate return (0);
2490Sstevel@tonic-gate return (1 + strlen(name) + 6 + strlen(suffix));
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
252*11038SRao.Shoaib@Sun.COM /*%
2530Sstevel@tonic-gate * dst_s_build_filename ()
2540Sstevel@tonic-gate * Builds a key filename from the key name, it's id, and a
2550Sstevel@tonic-gate * suffix. '\', '/' and ':' are not allowed. fA filename is of the
256*11038SRao.Shoaib@Sun.COM * form: K<keyname><id>.<suffix>
257*11038SRao.Shoaib@Sun.COM * form: K<keyname>+<alg>+<id>.<suffix>
2580Sstevel@tonic-gate *
2590Sstevel@tonic-gate * Returns -1 if the conversion fails:
2600Sstevel@tonic-gate * if the filename would be too long for space allotted
2610Sstevel@tonic-gate * if the filename would contain a '\', '/' or ':'
2620Sstevel@tonic-gate * Returns 0 on success
2630Sstevel@tonic-gate */
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate int
dst_s_build_filename(char * filename,const char * name,u_int16_t id,int alg,const char * suffix,size_t filename_length)2660Sstevel@tonic-gate dst_s_build_filename(char *filename, const char *name, u_int16_t id,
2670Sstevel@tonic-gate int alg, const char *suffix, size_t filename_length)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate u_int32_t my_id;
2700Sstevel@tonic-gate if (filename == NULL)
2710Sstevel@tonic-gate return (-1);
2720Sstevel@tonic-gate memset(filename, 0, filename_length);
2730Sstevel@tonic-gate if (name == NULL)
2740Sstevel@tonic-gate return (-1);
2750Sstevel@tonic-gate if (suffix == NULL)
2760Sstevel@tonic-gate return (-1);
2770Sstevel@tonic-gate if (filename_length < 1 + strlen(name) + 4 + 6 + 1 + strlen(suffix))
2780Sstevel@tonic-gate return (-1);
2790Sstevel@tonic-gate my_id = id;
2800Sstevel@tonic-gate sprintf(filename, "K%s+%03d+%05d.%s", name, alg, my_id,
2810Sstevel@tonic-gate (const char *) suffix);
2820Sstevel@tonic-gate if (strrchr(filename, '/'))
2830Sstevel@tonic-gate return (-1);
2840Sstevel@tonic-gate if (strrchr(filename, '\\'))
2850Sstevel@tonic-gate return (-1);
2860Sstevel@tonic-gate if (strrchr(filename, ':'))
2870Sstevel@tonic-gate return (-1);
2880Sstevel@tonic-gate return (0);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
291*11038SRao.Shoaib@Sun.COM /*%
2920Sstevel@tonic-gate * dst_s_fopen ()
2930Sstevel@tonic-gate * Open a file in the dst_path directory. If perm is specified, the
2940Sstevel@tonic-gate * file is checked for existence first, and not opened if it exists.
2950Sstevel@tonic-gate * Parameters
2960Sstevel@tonic-gate * filename File to open
2970Sstevel@tonic-gate * mode Mode to open the file (passed directly to fopen)
2980Sstevel@tonic-gate * perm File permission, if creating a new file.
2990Sstevel@tonic-gate * Returns
3000Sstevel@tonic-gate * NULL Failure
3010Sstevel@tonic-gate * NON-NULL (FILE *) of opened file.
3020Sstevel@tonic-gate */
3030Sstevel@tonic-gate FILE *
dst_s_fopen(const char * filename,const char * mode,int perm)3040Sstevel@tonic-gate dst_s_fopen(const char *filename, const char *mode, int perm)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate FILE *fp;
3070Sstevel@tonic-gate char pathname[PATH_MAX];
308*11038SRao.Shoaib@Sun.COM
309*11038SRao.Shoaib@Sun.COM if (strlen(filename) + strlen(dst_path) >= sizeof(pathname))
310*11038SRao.Shoaib@Sun.COM return (NULL);
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate if (*dst_path != '\0') {
3130Sstevel@tonic-gate strcpy(pathname, dst_path);
314*11038SRao.Shoaib@Sun.COM strcat(pathname, filename);
315*11038SRao.Shoaib@Sun.COM } else
316*11038SRao.Shoaib@Sun.COM strcpy(pathname, filename);
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate fp = fopen(pathname, mode);
3190Sstevel@tonic-gate if (perm)
3200Sstevel@tonic-gate chmod(pathname, perm);
3210Sstevel@tonic-gate return (fp);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate void
dst_s_dump(const int mode,const u_char * data,const int size,const char * msg)3250Sstevel@tonic-gate dst_s_dump(const int mode, const u_char *data, const int size,
3260Sstevel@tonic-gate const char *msg)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate UNUSED(data);
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate if (size > 0) {
3310Sstevel@tonic-gate #ifdef LONG_TEST
3320Sstevel@tonic-gate static u_char scratch[1000];
3330Sstevel@tonic-gate int n ;
3340Sstevel@tonic-gate n = b64_ntop(data, scratch, size, sizeof(scratch));
3350Sstevel@tonic-gate printf("%s: %x %d %s\n", msg, mode, n, scratch);
3360Sstevel@tonic-gate #else
3370Sstevel@tonic-gate printf("%s,%x %d\n", msg, mode, size);
3380Sstevel@tonic-gate #endif
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate }
341*11038SRao.Shoaib@Sun.COM
342*11038SRao.Shoaib@Sun.COM /*! \file */
343