From: Harlan Stenn Date: Fri, 22 Mar 2002 03:50:18 +0000 (-0500) Subject: Provide strstr.c . X-Git-Tag: NTP_4_1_73~172^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34aa8da82fbed345c4ee48c79d359d1eb3ddbc40;p=thirdparty%2Fntp.git Provide strstr.c . ntp-genkeys should be usable now. bk: 3c9aa9faD6XYHrq1XotIKNQm6lm7jQ --- diff --git a/configure.in b/configure.in index 92e5832034..990cbec036 100644 --- a/configure.in +++ b/configure.in @@ -683,7 +683,7 @@ case "$host" in esac]) AC_CHECK_FUNCS(setvbuf sigaction) AC_CHECK_FUNCS(sigvec sigset sigsuspend stime strchr sysconf sysctl) -AC_REPLACE_FUNCS(snprintf strdup strerror) +AC_REPLACE_FUNCS(snprintf strdup strerror strstr) case "$host" in *-*-aix4*) # Just stubs. Idiots. diff --git a/libntp/Makefile.am b/libntp/Makefile.am index ebf33b582f..97f9a4f963 100644 --- a/libntp/Makefile.am +++ b/libntp/Makefile.am @@ -2,19 +2,18 @@ AUTOMAKE_OPTIONS = ../util/ansi2knr noinst_LIBRARIES = libntp.a libntp_a_SOURCES = a_md5encrypt.c adjtime.c atoint.c atolfp.c atouint.c \ - authkeys.c authreadkeys.c authusekey.c \ - buftvtots.c caljulian.c calleapwhen.c caltontp.c calyearstart.c \ - clocktime.c clocktypes.c decodenetnum.c dofptoa.c dolfptoa.c \ - emalloc.c findconfig.c fptoa.c fptoms.c getopt.c hextoint.c \ - hextolfp.c humandate.c inttoa.c lib_strbuf.c machines.c md5c.c \ - memmove.c mfptoa.c mfptoms.c modetoa.c mstolfp.c msutotsf.c \ - msyslog.c netof.c numtoa.c numtohost.c octtoint.c prettydate.c \ - ranny.c refnumtoa.c statestr.c syssignal.c systime.c tsftomsu.c \ - tstotv.c tvtoa.c tvtots.c uglydate.c uinttoa.c utvtoa.c ymd2yd.c \ - mfp_mul.c binio.c ieee754io.c gpstolfp.c recvbuff.c iosignal.c \ - icom.c audio.c + audio.c authkeys.c authreadkeys.c authusekey.c binio.c buftvtots.c \ + caljulian.c calleapwhen.c caltontp.c calyearstart.c clocktime.c \ + clocktypes.c decodenetnum.c dofptoa.c dolfptoa.c emalloc.c \ + findconfig.c fptoa.c fptoms.c getopt.c gpstolfp.c hextoint.c \ + hextolfp.c humandate.c icom.c ieee754io.c inttoa.c iosignal.c \ + lib_strbuf.c machines.c md5c.c memmove.c mfp_mul.c mfptoa.c \ + mfptoms.c modetoa.c mstolfp.c msutotsf.c msyslog.c netof.c numtoa.c \ + numtohost.c octtoint.c prettydate.c ranny.c recvbuff.c refnumtoa.c \ + statestr.c syssignal.c systime.c tsftomsu.c tstotv.c tvtoa.c \ + tvtots.c uglydate.c uinttoa.c utvtoa.c ymd2yd.c EXTRA_libntp_a_SOURCES = adjtimex.c log.c random.c -# mktime.c snprintf.c strdup.c strerror.c +# mktime.c snprintf.c strdup.c strerror.c strstr.c libntp_a_LIBADD = @LIBOBJS@ libntp_a_DEPENDENCIES = @LIBOBJS@ INCLUDES = -I$(top_srcdir)/include diff --git a/libntp/strstr.c b/libntp/strstr.c new file mode 100644 index 0000000000..bf44c1d716 --- /dev/null +++ b/libntp/strstr.c @@ -0,0 +1,46 @@ +/* + * Amanda, The Advanced Maryland Automatic Network Disk Archiver + * Copyright (c) 1991-1998 University of Maryland at College Park + * All Rights Reserved. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of U.M. not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. U.M. makes no representations about the + * suitability of this software for any purpose. It is provided "as is" + * without express or implied warranty. + * + * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. + * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: James da Silva, Systems Design and Analysis Group + * Computer Science Department + * University of Maryland at College Park + */ +/* + * $Id$ + * + * replacement for missing ANSI-C strstr function + */ +#include "amanda.h" + +char *strstr(a, b) +char *a, *b; +{ + int alen, blen, i; + + alen = strlen(a); + blen = strlen(b); + + for(i=0; i <= alen-blen; i++, a++) + if(strncmp(a, b, blen) == 0) return a; + + return NULL; +} diff --git a/util/ntp-genkeys.c b/util/ntp-genkeys.c index da024d2e01..4917854a6c 100644 --- a/util/ntp-genkeys.c +++ b/util/ntp-genkeys.c @@ -1,5 +1,8 @@ /* * Program to generate cryptographic keys for NTP clients and servers + * + * TODO: + * - do we need to make a symlink to the private key file? */ #ifdef HAVE_CONFIG_H @@ -23,11 +26,11 @@ #endif #ifdef OPENSSL -#include "openssl/evp.h" -#include "openssl/err.h" -#include "openssl/rand.h" -#include "openssl/pem.h" -#include "openssl/x509.h" +# include "openssl/evp.h" +# include "openssl/err.h" +# include "openssl/rand.h" +# include "openssl/pem.h" +# include "openssl/x509.h" #endif /* OPENSSL */ /* @@ -49,6 +52,10 @@ int genkeys P((void)); int genrest P((void)); int genthings P((void)); char *getpath P((char *, char *, char *)); +void set_filenames P((void)); +void sub_token P((char *, char *, char *, char *)); +void usage P((void)); + #ifdef OPENSSL u_long asn2ntp P((ASN1_TIME *)); /* ASN.1 time format to NTP seconds */ void cb P((int, int, void *)); /* callback routine */ @@ -56,14 +63,13 @@ void genkey_dsa P((char *, char *, char *, u_long)); void genkey_rsa P((char *, char *, char *, u_long)); int x509 P((u_char *, EVP_PKEY *, EVP_MD *, int)); /* generate req/cert */ #endif /* OPENSSL */ -void usage P((void)); /* * Program variables */ struct timeval tv; /* initialization vector */ u_long ntptime; /* NTP epoch */ -u_char hostname[PATH_MAX]; /* host name */ +char hostname[PATH_MAX]; /* host name */ #ifdef OPENSSL long d0, d1, d2, d3; /* callback counters */ #endif /* OPENSSL */ @@ -73,14 +79,17 @@ char *keysdir = NTP_KEYSDIR; char *f1_keys; /* Visible MD5 key file name */ char f2_keys[PATH_MAX]; /* timestamped */ -char *f1_privatekey; +char *f0_privatekey = "ntpkey_key_HOSTNAME"; +char f1_privatekey[PATH_MAX]; char f2_privatekey[PATH_MAX]; -char *f1_signkey; /* from ntp.conf */ +char *f0_signkey = "ntpkey_sign_HOSTNAME"; +char f1_signkey[PATH_MAX]; /* from ntp.conf */ char f2_signkey[PATH_MAX]; /* generated filename */ char f3_signkey[PATH_MAX]; /* generate new certs using this key */ -char *f1_cert; +char *f0_cert = "ntpkey_cert_HOSTNAME"; +char f1_cert[PATH_MAX]; char f2_cert[PATH_MAX]; char tmp_name[PATH_MAX]; @@ -92,7 +101,7 @@ u_long client_limit; u_long client_limit_period; keyid_t ctl_auth_keyid; /* keyid used to authenticate write requests */ u_long current_time; /* current time (s) */ -volatile int debug = 1; /* debugging flag */ +volatile int debug = 0; /* debugging flag */ keyid_t info_auth_keyid; /* keyid used to authenticate requests */ char * req_file; /* name of the file with configuration info */ keyid_t req_keyid; /* request keyid */ @@ -171,13 +180,13 @@ crypto_config( if (debug > 1) printf("crypto_config: PRIVATEKEY/<%d> <%s>\n", item, cp); - f1_privatekey = strdup(cp); + f0_privatekey = strdup(cp); break; case CRYPTO_CONF_SIGN: if (debug > 1) printf("crypto_config: SIGNKEY/<%d> <%s>\n", item, cp); - f1_signkey = strdup(cp); + f0_signkey = strdup(cp); break; case CRYPTO_CONF_KEYS: if (debug > 1) @@ -187,7 +196,7 @@ crypto_config( case CRYPTO_CONF_CERT: if (debug > 1) printf("crypto_config: CERT/<%d> <%s>\n", item, cp); - f1_cert = strdup(cp); + f0_cert = strdup(cp); break; #endif /* OPENSSL */ default: @@ -420,6 +429,38 @@ usage ( } +void +sub_token ( + char *src, + char *token, + char *rep, + char *dst + ) +{ + char *bot = strstr(src, token); + size_t t_len = strlen(token); + + if (bot) { + *dst = '\0'; + strncat(dst, src, (bot - src)); + strcat(dst, rep); + strcat(dst, src + (bot - src) + t_len); + } else { + strcpy(dst, src); + } + return; +} + + +void +set_filenames ( void ) +{ + sub_token(f0_cert, "HOSTNAME", hostname, f1_cert); + sub_token(f0_privatekey, "HOSTNAME", hostname, f1_privatekey); + sub_token(f0_signkey, "HOSTNAME", hostname, f1_signkey); +} + + void getCmdOpts ( int argc, @@ -533,8 +574,6 @@ main( { int i; - getconfig(argc, argv); /* ntpd/ntp_config.c */ - #ifdef OPENSSL if (SSLeay() != OPENSSL_VERSION_NUMBER) { printf("OpenSSL version mismatch. Built against %lx, you have %lx\n", @@ -545,6 +584,8 @@ main( } #endif + getconfig(argc, argv); /* ntpd/ntp_config.c */ + /* * Initialize the timestamp. */ @@ -553,12 +594,19 @@ main( i = 0; if (ntp_optind == argc) { /* No more args - generate key for us */ - gethostname(hostname, sizeof(hostname)); + if (-1 == gethostname(hostname, sizeof(hostname))) { + perror("gethostname()"); + exit(1); + } + + set_filenames(); i |= genthings(); } else while (ntp_optind < argc) { strncpy(hostname, argv[ntp_optind], sizeof hostname); + set_filenames(); + i |= genthings(); ++ntp_optind; @@ -725,77 +773,54 @@ genkeys( * Make sure f3_signkey is pointing to the right file. * * - If we built a sign key, use it. - * - If ntp.conf specifies a sign key, use it. - * - If there is a default sign key in f1_keys/, use it. (stat) + * - If ntp.conf specifies or defaults an existing sign key, use it. * - If we built an rsakey, use it. - * - If ntp.conf specifies an rsakey, use it. - * - If there is a default rsakey in f1_keys/, use it. (stat) + * - If ntp.conf specifies or defaults an existing rsakey, use it. */ gotsignkey = 0; if (!gotsignkey && *f2_signkey) { /* We built a sign key - use it. */ - if (f1_signkey) { /* Use the explicit signkey in ntp.conf */ - cp = getpath(keysdir, f1_signkey, f2_signkey); - if (!cp) - exit(-1); - strcpy(f3_signkey, cp); - if (debug > 1) - printf("f3: GS1: <%s>\n", f3_signkey); - ++gotsignkey; - } else { /* Use new signkey in default location */ - cp = getpath(keysdir, f2_signkey, NULL); - if (!cp) - exit(-1); - strcpy(f3_signkey, cp); - if (debug > 1) - printf("f3: GS0: <%s>\n", f3_signkey); - ++gotsignkey; - } - } - if (!gotsignkey && *f1_signkey) { /* Use sign key from ntp.conf */ - cp = getpath(keysdir, f1_signkey, NULL); + cp = getpath(keysdir, f1_signkey, f2_signkey); if (!cp) exit(-1); strcpy(f3_signkey, cp); if (debug > 1) - printf("f3: ES: <%s>\n", f3_signkey); + printf("f3: GS1: <%s>\n", f3_signkey); ++gotsignkey; } - if (!gotsignkey) { /* Iff a default sign key exists, use it. */ - /* build the name, stat() it, and if it exists, use it. */ - if (debug > 1) - printf("f3: DS: <%s>\n", f3_signkey); - } - if (!gotsignkey && *f2_privatekey) { /* We built an rsakey - use it. */ - if (f1_privatekey) { /* Use the explicit rsakey in ntp.conf */ - cp = getpath(keysdir, f1_privatekey, f2_privatekey); - if (!cp) - exit(-1); - strcpy(f3_signkey, cp); - if (debug > 1) - printf("f3: GR1: <%s>\n", f3_signkey); - ++gotsignkey; - } else { /* Use new rsakey in default location */ - cp = getpath(keysdir, f2_privatekey, NULL); - if (!cp) - exit(-1); + if (!gotsignkey && *f1_signkey) { /* Use sign key from ntp.conf */ + struct stat sb; + + cp = getpath(keysdir, f1_signkey, NULL); + if (!cp) + exit(-1); + if (stat(cp, &sb) == 0) { strcpy(f3_signkey, cp); if (debug > 1) - printf("f3: GR0: <%s>\n", f3_signkey); + printf("f3: ES: <%s>\n", f3_signkey); ++gotsignkey; } } - if (!gotsignkey && *f1_privatekey) { /* Use rsakey from ntp.conf */ - cp = getpath(keysdir, f1_privatekey, NULL); + if (!gotsignkey && *f2_privatekey) { /* We built an rsakey - use it. */ + cp = getpath(keysdir, f1_privatekey, f2_privatekey); if (!cp) exit(-1); strcpy(f3_signkey, cp); if (debug > 1) - printf("f3: ER: <%s>\n", f3_signkey); + printf("f3: GR1: <%s>\n", f3_signkey); ++gotsignkey; } - if (!gotsignkey) { /* Iff a default rsakey exists, use it. */ - if (debug > 1) - printf("f3: DR: <%s>\n", f3_signkey); + if (!gotsignkey && *f1_privatekey) { /* Use rsakey from ntp.conf */ + struct stat sb; + + cp = getpath(keysdir, f1_privatekey, NULL); + if (!cp) + exit(-1); + if (stat(cp, &sb) == 0) { + strcpy(f3_signkey, cp); + if (debug > 1) + printf("f3: ER: <%s>\n", f3_signkey); + ++gotsignkey; + } } if (!gotsignkey) @@ -1379,7 +1404,7 @@ getpath ( if (debug > 1) printf("getpath: gp_dir <%s> gp_path <%s> gp_file <%s>\n", - gp_dir, gp_path, gp_file); + gp_dir, gp_path, (gp_file) ? gp_file : "(null)"); if (*gp_path == '/') strcpy(filename, gp_path);