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