2 * authreadkeys.c - routines to support the reading of the key file
9 #include "ntp_syslog.h"
10 #include "ntp_stdlib.h"
14 * Types of ascii representations for keys. "Standard" means a 64 bit
15 * hex number in NBS format, i.e. with the low order bit of each byte
16 * a parity bit. "NTP" means a 64 bit key in NTP format, with the
17 * high order bit of each byte a parity bit. "Ascii" means a 1-to-8
18 * character string whose ascii representation is used as the key.
20 #define KEY_TYPE_STD 1
21 #define KEY_TYPE_NTP 2
22 #define KEY_TYPE_ASCII 3
26 * Arbitrary long string of ASCII characters.
28 #define KEY_TYPE_MD5 4
31 static char *nexttok P((char **));
34 * nexttok - basic internal tokenizing routine
47 * Space past white space
49 while (*cp == ' ' || *cp == '\t')
53 * Save this and space to end of token
56 while (*cp != '\0' && *cp != '\n' && *cp != ' '
57 && *cp != '\t' && *cp != '#')
61 * If token length is zero return an error, else set end of
62 * token to zero and return start.
67 if (*cp == ' ' || *cp == '\t')
78 * authreadkeys - (re)read keys from a file.
90 char buf[512]; /* lots of room for line */
93 * Open file. Complain and return if it can't be opened.
95 fp = fopen(file, "r");
97 msyslog(LOG_ERR, "can't open key file %s: %m", file);
102 * Remove all existing keys
107 * Now read lines from the file, looking for key entries
109 while ((line = fgets(buf, sizeof buf, fp)) != NULL) {
110 token = nexttok(&line);
115 * First is key number. See if it is okay.
120 "cannot change keyid 0, key entry `%s' ignored",
125 if (keyno > NTP_MAXKEY) {
127 "keyid's > %d reserved for autokey, key entry `%s' ignored",
133 * Next is keytype. See if that is all right.
135 token = nexttok(&line);
138 "no key type for key number %ld, entry ignored",
146 keytype = KEY_TYPE_STD; break;
150 keytype = KEY_TYPE_NTP; break;
154 keytype = KEY_TYPE_ASCII; break;
158 keytype = KEY_TYPE_MD5; break;
161 "invalid key type for key number %ld, entry ignored",
167 * Finally, get key and insert it
169 token = nexttok(&line);
172 "no key for number %ld entry, entry ignored",
180 if (!authusekey(keyno, keytype,
183 "format/parity error for DES key %ld, not used",
188 if (!authusekey(keyno, keytype,
191 "format/parity error for MD5 key %ld, not used",