1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* authreadkeys.c - routines to support the reading of the key file
*/
#include <stdio.h>
#include <ctype.h>
#include "ntp_stdlib.h"
#include "ntp_syslog.h"
#ifdef DES
/*
* Types of ascii representations for keys. "Standard" means a 64 bit
* hex number in NBS format, i.e. with the low order bit of each byte
* a parity bit. "NTP" means a 64 bit key in NTP format, with the
* high order bit of each byte a parity bit. "Ascii" means a 1-to-8
* character string whose ascii representation is used as the key.
*/
#define KEY_TYPE_STD 1
#define KEY_TYPE_NTP 2
#define KEY_TYPE_ASCII 3
#endif
#ifdef MD5
/*
* Arbitrary long string of ASCII characters.
*/
#define KEY_TYPE_MD5 4
#endif
/* Forwards */
static char *nexttok P((char **));
/*
* nexttok - basic internal tokenizing routine
*/
static char *
nexttok(str)
char **str;
{
register char *cp;
char *starttok;
cp = *str;
/*
* Space past white space
*/
while (*cp == ' ' || *cp == '\t')
cp++;
/*
* Save this and space to end of token
*/
starttok = cp;
while (*cp != '\0' && *cp != '\n' && *cp != ' '
&& *cp != '\t' && *cp != '#')
cp++;
/*
* If token length is zero return an error, else set end of
* token to zero and return start.
*/
if (starttok == cp)
return 0;
if (*cp == ' ' || *cp == '\t')
*cp++ = '\0';
else
*cp = '\0';
*str = cp;
return starttok;
}
/*
* authreadkeys - (re)read keys from a file.
*/
int
authreadkeys(file)
const char *file;
{
FILE *fp;
char *line;
char *token;
u_int32 keyno;
int keytype;
char buf[512]; /* lots of room for line? */
#if !defined(VMS) /* wjm - what are these lines doing here? */
extern FILE * fopen P((const char *filename, const char *type));
extern int fclose P((FILE *stream));
#endif /* !VMS */
/*
* Open file. Complain and return if it can't be opened.
*/
fp = fopen(file, "r");
if (fp == NULL) {
msyslog(LOG_ERR, "can't open key file %s: %m", file);
return 0;
}
/*
* Remove all existing keys
*/
auth_delkeys();
/*
* Now read lines from the file, looking for key entries
*/
while ((line = fgets(buf, sizeof buf, fp)) != NULL) {
token = nexttok(&line);
if (token == 0)
continue;
/*
* First is key number. See if it is okay.
*/
keyno = atoi(token);
if (keyno == 0) {
msyslog(LOG_ERR,
"cannot change keyid 0, key entry `%s' ignored",
token);
continue;
}
/*
* Next is keytype. See if that is all right.
*/
token = nexttok(&line);
if (token == 0) {
msyslog(LOG_ERR,
"no key type for key number %ld, entry ignored",
keyno);
continue;
}
switch (*token) {
#ifdef DES
case 'S':
case 's':
keytype = KEY_TYPE_STD; break;
case 'N':
case 'n':
keytype = KEY_TYPE_NTP; break;
case 'A':
case 'a':
keytype = KEY_TYPE_ASCII; break;
#endif
#ifdef MD5
case 'M':
case 'm':
keytype = KEY_TYPE_MD5; break;
#endif
default:
msyslog(LOG_ERR,
"invalid key type for key number %ld, entry ignored",
keyno);
continue;
}
/*
* Finally, get key and insert it
*/
token = nexttok(&line);
if (token == 0) {
msyslog(LOG_ERR,
"no key for number %ld entry, entry ignored",
keyno);
} else {
switch(keytype) {
#ifdef DES
case KEY_TYPE_STD:
case KEY_TYPE_NTP:
case KEY_TYPE_ASCII:
if (!authusekey(keyno, keytype, token))
msyslog(LOG_ERR,
"format/parity error for DES key %ld, not used",
keyno);
break;
#endif
#ifdef MD5
case KEY_TYPE_MD5:
if (!authusekey(keyno, keytype, token))
msyslog(LOG_ERR,
"format/parity error for MD5 key %ld, not used",
keyno);
break;
#endif
}
}
}
(void) fclose(fp);
return 1;
}
|