2N/Astatic const char rcsid[] = "$Header: /proj/cvs/prod/libbind/dst/support.c,v 1.6 2005/10/11 00:10:13 marka Exp $";
2N/A
2N/A
2N/A/*
2N/A * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
2N/A *
2N/A * Permission to use, copy modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
2N/A * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
2N/A * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
2N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
2N/A * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
2N/A * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
2N/A * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
2N/A */
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <memory.h>
2N/A#include <string.h>
2N/A#include <errno.h>
2N/A#include <sys/stat.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/nameser.h>
2N/A#include <resolv.h>
2N/A
2N/A#include "dst_internal.h"
2N/A
2N/A#include "port_after.h"
2N/A
2N/A/*%
2N/A * dst_s_verify_str()
2N/A * Validate that the input string(*str) is at the head of the input
2N/A * buffer(**buf). If so, move the buffer head pointer (*buf) to
2N/A * the first byte of data following the string(*str).
2N/A * Parameters
2N/A * buf Input buffer.
2N/A * str Input string.
2N/A * Return
2N/A * 0 *str is not the head of **buff
2N/A * 1 *str is the head of **buff, *buf is is advanced to
2N/A * the tail of **buf.
2N/A */
2N/A
2N/Aint
2N/Adst_s_verify_str(const char **buf, const char *str)
2N/A{
2N/A int b, s;
2N/A if (*buf == NULL) /*%< error checks */
2N/A return (0);
2N/A if (str == NULL || *str == '\0')
2N/A return (1);
2N/A
2N/A b = strlen(*buf); /*%< get length of strings */
2N/A s = strlen(str);
2N/A if (s > b || strncmp(*buf, str, s)) /*%< check if same */
2N/A return (0); /*%< not a match */
2N/A (*buf) += s; /*%< advance pointer */
2N/A return (1);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_calculate_bits
2N/A * Given a binary number represented in a u_char[], determine
2N/A * the number of significant bits used.
2N/A * Parameters
2N/A * str An input character string containing a binary number.
2N/A * max_bits The maximum possible significant bits.
2N/A * Return
2N/A * N The number of significant bits in str.
2N/A */
2N/A
2N/Aint
2N/Adst_s_calculate_bits(const u_char *str, const int max_bits)
2N/A{
2N/A const u_char *p = str;
2N/A u_char i, j = 0x80;
2N/A int bits;
2N/A for (bits = max_bits; *p == 0x00 && bits > 0; p++)
2N/A bits -= 8;
2N/A for (i = *p; (i & j) != j; j >>= 1)
2N/A bits--;
2N/A return (bits);
2N/A}
2N/A
2N/A/*%
2N/A * calculates a checksum used in dst for an id.
2N/A * takes an array of bytes and a length.
2N/A * returns a 16 bit checksum.
2N/A */
2N/Au_int16_t
2N/Adst_s_id_calc(const u_char *key, const int keysize)
2N/A{
2N/A u_int32_t ac;
2N/A const u_char *kp = key;
2N/A int size = keysize;
2N/A
2N/A if (!key || (keysize <= 0))
2N/A return (0xffffU);
2N/A
2N/A for (ac = 0; size > 1; size -= 2, kp += 2)
2N/A ac += ((*kp) << 8) + *(kp + 1);
2N/A
2N/A if (size > 0)
2N/A ac += ((*kp) << 8);
2N/A ac += (ac >> 16) & 0xffff;
2N/A
2N/A return (ac & 0xffff);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_dns_key_id() Function to calculate DNSSEC footprint from KEY record
2N/A * rdata
2N/A * Input:
2N/A * dns_key_rdata: the raw data in wire format
2N/A * rdata_len: the size of the input data
2N/A * Output:
2N/A * the key footprint/id calculated from the key data
2N/A */
2N/Au_int16_t
2N/Adst_s_dns_key_id(const u_char *dns_key_rdata, const int rdata_len)
2N/A{
2N/A if (!dns_key_rdata)
2N/A return 0;
2N/A
2N/A /* compute id */
2N/A if (dns_key_rdata[3] == KEY_RSA) /*%< Algorithm RSA */
2N/A return dst_s_get_int16((const u_char *)
2N/A &dns_key_rdata[rdata_len - 3]);
2N/A else if (dns_key_rdata[3] == KEY_HMAC_MD5)
2N/A /* compatibility */
2N/A return 0;
2N/A else
2N/A /* compute a checksum on the key part of the key rr */
2N/A return dst_s_id_calc(dns_key_rdata, rdata_len);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_get_int16
2N/A * This routine extracts a 16 bit integer from a two byte character
2N/A * string. The character string is assumed to be in network byte
2N/A * order and may be unaligned. The number returned is in host order.
2N/A * Parameter
2N/A * buf A two byte character string.
2N/A * Return
2N/A * The converted integer value.
2N/A */
2N/A
2N/Au_int16_t
2N/Adst_s_get_int16(const u_char *buf)
2N/A{
2N/A register u_int16_t a = 0;
2N/A a = ((u_int16_t)(buf[0] << 8)) | ((u_int16_t)(buf[1]));
2N/A return (a);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_get_int32
2N/A * This routine extracts a 32 bit integer from a four byte character
2N/A * string. The character string is assumed to be in network byte
2N/A * order and may be unaligned. The number returned is in host order.
2N/A * Parameter
2N/A * buf A four byte character string.
2N/A * Return
2N/A * The converted integer value.
2N/A */
2N/A
2N/Au_int32_t
2N/Adst_s_get_int32(const u_char *buf)
2N/A{
2N/A register u_int32_t a = 0;
2N/A a = ((u_int32_t)(buf[0] << 24)) | ((u_int32_t)(buf[1] << 16)) |
2N/A ((u_int32_t)(buf[2] << 8)) | ((u_int32_t)(buf[3]));
2N/A return (a);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_put_int16
2N/A * Take a 16 bit integer and store the value in a two byte
2N/A * character string. The integer is assumed to be in network
2N/A * order and the string is returned in host order.
2N/A *
2N/A * Parameters
2N/A * buf Storage for a two byte character string.
2N/A * val 16 bit integer.
2N/A */
2N/A
2N/Avoid
2N/Adst_s_put_int16(u_int8_t *buf, const u_int16_t val)
2N/A{
2N/A buf[0] = (u_int8_t)(val >> 8);
2N/A buf[1] = (u_int8_t)(val);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_put_int32
2N/A * Take a 32 bit integer and store the value in a four byte
2N/A * character string. The integer is assumed to be in network
2N/A * order and the string is returned in host order.
2N/A *
2N/A * Parameters
2N/A * buf Storage for a four byte character string.
2N/A * val 32 bit integer.
2N/A */
2N/A
2N/Avoid
2N/Adst_s_put_int32(u_int8_t *buf, const u_int32_t val)
2N/A{
2N/A buf[0] = (u_int8_t)(val >> 24);
2N/A buf[1] = (u_int8_t)(val >> 16);
2N/A buf[2] = (u_int8_t)(val >> 8);
2N/A buf[3] = (u_int8_t)(val);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_filename_length
2N/A *
2N/A * This function returns the number of bytes needed to hold the
2N/A * filename for a key file. '/', '\' and ':' are not allowed.
2N/A * form: K&lt;keyname&gt;+&lt;alg&gt;+&lt;id&gt;.&lt;suffix&gt;
2N/A *
2N/A * Returns 0 if the filename would contain either '\', '/' or ':'
2N/A */
2N/Asize_t
2N/Adst_s_filename_length(const char *name, const char *suffix)
2N/A{
2N/A if (name == NULL)
2N/A return (0);
2N/A if (strrchr(name, '\\'))
2N/A return (0);
2N/A if (strrchr(name, '/'))
2N/A return (0);
2N/A if (strrchr(name, ':'))
2N/A return (0);
2N/A if (suffix == NULL)
2N/A return (0);
2N/A if (strrchr(suffix, '\\'))
2N/A return (0);
2N/A if (strrchr(suffix, '/'))
2N/A return (0);
2N/A if (strrchr(suffix, ':'))
2N/A return (0);
2N/A return (1 + strlen(name) + 6 + strlen(suffix));
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_build_filename ()
2N/A * Builds a key filename from the key name, it's id, and a
2N/A * suffix. '\', '/' and ':' are not allowed. fA filename is of the
2N/A * form: K&lt;keyname&gt;&lt;id&gt;.&lt;suffix&gt;
2N/A * form: K&lt;keyname&gt;+&lt;alg&gt;+&lt;id&gt;.&lt;suffix&gt;
2N/A *
2N/A * Returns -1 if the conversion fails:
2N/A * if the filename would be too long for space allotted
2N/A * if the filename would contain a '\', '/' or ':'
2N/A * Returns 0 on success
2N/A */
2N/A
2N/Aint
2N/Adst_s_build_filename(char *filename, const char *name, u_int16_t id,
2N/A int alg, const char *suffix, size_t filename_length)
2N/A{
2N/A u_int32_t my_id;
2N/A if (filename == NULL)
2N/A return (-1);
2N/A memset(filename, 0, filename_length);
2N/A if (name == NULL)
2N/A return (-1);
2N/A if (suffix == NULL)
2N/A return (-1);
2N/A if (filename_length < 1 + strlen(name) + 4 + 6 + 1 + strlen(suffix))
2N/A return (-1);
2N/A my_id = id;
2N/A sprintf(filename, "K%s+%03d+%05d.%s", name, alg, my_id,
2N/A (const char *) suffix);
2N/A if (strrchr(filename, '/'))
2N/A return (-1);
2N/A if (strrchr(filename, '\\'))
2N/A return (-1);
2N/A if (strrchr(filename, ':'))
2N/A return (-1);
2N/A return (0);
2N/A}
2N/A
2N/A/*%
2N/A * dst_s_fopen ()
2N/A * Open a file in the dst_path directory. If perm is specified, the
2N/A * file is checked for existence first, and not opened if it exists.
2N/A * Parameters
2N/A * filename File to open
2N/A * mode Mode to open the file (passed directly to fopen)
2N/A * perm File permission, if creating a new file.
2N/A * Returns
2N/A * NULL Failure
2N/A * NON-NULL (FILE *) of opened file.
2N/A */
2N/AFILE *
2N/Adst_s_fopen(const char *filename, const char *mode, int perm)
2N/A{
2N/A FILE *fp;
2N/A char pathname[PATH_MAX];
2N/A
2N/A if (strlen(filename) + strlen(dst_path) >= sizeof(pathname))
2N/A return (NULL);
2N/A
2N/A if (*dst_path != '\0') {
2N/A strcpy(pathname, dst_path);
2N/A strcat(pathname, filename);
2N/A } else
2N/A strcpy(pathname, filename);
2N/A
2N/A fp = fopen(pathname, mode);
2N/A if (perm)
2N/A chmod(pathname, perm);
2N/A return (fp);
2N/A}
2N/A
2N/Avoid
2N/Adst_s_dump(const int mode, const u_char *data, const int size,
2N/A const char *msg)
2N/A{
2N/A UNUSED(data);
2N/A
2N/A if (size > 0) {
2N/A#ifdef LONG_TEST
2N/A static u_char scratch[1000];
2N/A int n ;
2N/A n = b64_ntop(data, scratch, size, sizeof(scratch));
2N/A printf("%s: %x %d %s\n", msg, mode, n, scratch);
2N/A#else
2N/A printf("%s,%x %d\n", msg, mode, size);
2N/A#endif
2N/A }
2N/A}
2N/A
2N/A/*! \file */