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
|
/*
* authreadkeys.c - routines to support the reading of the key file
*/
#include <stdio.h>
#include <ctype.h>
#include "ntp_fp.h"
#include "ntp.h"
#include "ntp_syslog.h"
#include "ntp_stdlib.h"
/*
* Arbitrary long string of ASCII characters.
*/
#define KEY_TYPE_MD5 4
/* Forwards */
static char *nexttok P((char **));
/*
* nexttok - basic internal tokenizing routine
*/
static char *
nexttok(
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(
const char *file
)
{
FILE *fp;
char *line;
char *token;
u_long keyno;
int keytype;
char buf[512]; /* lots of room for line */
/*
* 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;
}
if (keyno > NTP_MAXKEY) {
msyslog(LOG_ERR,
"keyid's > %d reserved for autokey, key entry `%s' ignored",
NTP_MAXKEY, 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) {
case 'M':
case 'm':
keytype = KEY_TYPE_MD5; break;
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) {
case KEY_TYPE_MD5:
if (!authusekey(keyno, keytype,
(u_char *)token))
msyslog(LOG_ERR,
"format/parity error for MD5 key %ld, not used",
keyno);
break;
}
}
}
(void) fclose(fp);
return 1;
}
|