4 * authreadkeys.c - routines to support the reading of the key file
12 #include "ntp_syslog.h"
13 #include "ntp_stdlib.h"
16 #include "openssl/objects.h"
20 static char *nexttok (char **);
23 * nexttok - basic internal tokenizing routine
36 * Space past white space
38 while (*cp
== ' ' || *cp
== '\t')
42 * Save this and space to end of token
45 while (*cp
!= '\0' && *cp
!= '\n' && *cp
!= ' '
46 && *cp
!= '\t' && *cp
!= '#')
50 * If token length is zero return an error, else set end of
51 * token to zero and return start.
56 if (*cp
== ' ' || *cp
== '\t')
67 * authreadkeys - (re)read keys from a file.
79 char buf
[512]; /* lots of room for line */
85 * Open file. Complain and return if it can't be opened.
87 fp
= fopen(file
, "r");
89 msyslog(LOG_ERR
, "authreadkeys: file %s: %m",
96 * Remove all existing keys
101 * Now read lines from the file, looking for key entries
103 while ((line
= fgets(buf
, sizeof buf
, fp
)) != NULL
) {
104 token
= nexttok(&line
);
109 * First is key number. See if it is okay.
114 "authreadkeys: cannot change key %s", token
);
118 if (keyno
> NTP_MAXKEY
) {
120 "authreadkeys: key %s > %d reserved for Autokey",
126 * Next is keytype. See if that is all right.
128 token
= nexttok(&line
);
131 "authreadkeys: no key type for key %d", keyno
);
136 * The key type is the NID used by the message digest
137 * algorithm. There are a number of inconsistencies in
138 * the OpenSSL database. We attempt to discover them
139 * here and prevent use of inconsistent data later.
141 keytype
= keytype_from_text(token
, NULL
);
144 "authreadkeys: invalid type for key %d", keyno
);
147 if (EVP_get_digestbynid(keytype
) == NULL
) {
149 "authreadkeys: no algorithm for key %d", keyno
);
155 * The key type is unused, but is required to be 'M' or
156 * 'm' for compatibility.
158 if (!(*token
== 'M' || *token
== 'm')) {
160 "authreadkeys: invalid type for key %d", keyno
);
163 keytype
= KEY_TYPE_MD5
;
167 * Finally, get key and insert it. If it is longer than 20
168 * characters, it is a binary string encoded in hex;
169 * otherwise, it is a text string of printable ASCII
172 token
= nexttok(&line
);
175 "authreadkeys: no key for key %d", keyno
);
180 MD5auth_setkey(keyno
, keytype
, (u_char
*)token
, len
);
182 char hex
[] = "0123456789abcdef";
187 jlim
= min(len
, 2 * sizeof(keystr
));
188 for (j
= 0; j
< jlim
; j
++) {
189 ptr
= strchr(hex
, tolower(token
[j
]));
192 "authreadkeys: invalid hex digit for key %d", keyno
);
195 temp
= (u_char
)(ptr
- hex
);
197 keystr
[j
/ 2] |= temp
;
199 keystr
[j
/ 2] = temp
<< 4;
201 MD5auth_setkey(keyno
, keytype
, keystr
, jlim
/ 2);