2 * authreadkeys.c - routines to support the reading of the key file
9 #include "ntp_syslog.h"
10 #include "ntp_stdlib.h"
13 * Arbitrary long string of ASCII characters.
15 #define KEY_TYPE_MD5 4
18 static char *nexttok P((char **));
21 * nexttok - basic internal tokenizing routine
34 * Space past white space
36 while (*cp == ' ' || *cp == '\t')
40 * Save this and space to end of token
43 while (*cp != '\0' && *cp != '\n' && *cp != ' '
44 && *cp != '\t' && *cp != '#')
48 * If token length is zero return an error, else set end of
49 * token to zero and return start.
54 if (*cp == ' ' || *cp == '\t')
65 * authreadkeys - (re)read keys from a file.
77 char buf[512]; /* lots of room for line */
80 * Open file. Complain and return if it can't be opened.
82 fp = fopen(file, "r");
84 msyslog(LOG_ERR, "can't open key file %s: %m", file);
89 * Remove all existing keys
94 * Now read lines from the file, looking for key entries
96 while ((line = fgets(buf, sizeof buf, fp)) != NULL) {
97 token = nexttok(&line);
102 * First is key number. See if it is okay.
107 "cannot change keyid 0, key entry `%s' ignored",
112 if (keyno > NTP_MAXKEY) {
114 "keyid's > %d reserved for autokey, key entry `%s' ignored",
120 * Next is keytype. See if that is all right.
122 token = nexttok(&line);
125 "no key type for key number %ld, entry ignored",
132 keytype = KEY_TYPE_MD5; break;
135 "invalid key type for key number %ld, entry ignored",
141 * Finally, get key and insert it
143 token = nexttok(&line);
146 "no key for number %ld entry, entry ignored",
151 if (!authusekey(keyno, keytype,
154 "format/parity error for MD5 key %ld, not used",