]> git.ipfire.org Git - thirdparty/chrony.git/blob - cmdparse.c
conf: rework allow/deny parser
[thirdparty/chrony.git] / cmdparse.c
1 /*
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
6 * Copyright (C) Miroslav Lichvar 2013-2014, 2016
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 **********************************************************************
22
23 =======================================================================
24
25 Module for parsing various forms of directive and command lines that
26 are common to the configuration file and to the command client.
27
28 */
29
30 #include "config.h"
31
32 #include "sysincl.h"
33
34 #include "cmdparse.h"
35 #include "memory.h"
36 #include "nameserv.h"
37 #include "ntp.h"
38 #include "util.h"
39
40 /* ================================================== */
41
42 int
43 CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
44 {
45 char *hostname, *cmd;
46 int n;
47
48 src->port = SRC_DEFAULT_PORT;
49 src->params.minpoll = SRC_DEFAULT_MINPOLL;
50 src->params.maxpoll = SRC_DEFAULT_MAXPOLL;
51 src->params.connectivity = SRC_ONLINE;
52 src->params.auto_offline = 0;
53 src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL;
54 src->params.burst = 0;
55 src->params.iburst = 0;
56 src->params.min_stratum = SRC_DEFAULT_MINSTRATUM;
57 src->params.poll_target = SRC_DEFAULT_POLLTARGET;
58 src->params.version = 0;
59 src->params.max_sources = SRC_DEFAULT_MAXSOURCES;
60 src->params.min_samples = SRC_DEFAULT_MINSAMPLES;
61 src->params.max_samples = SRC_DEFAULT_MAXSAMPLES;
62 src->params.filter_length = 0;
63 src->params.interleaved = 0;
64 src->params.sel_options = 0;
65 src->params.nts = 0;
66 src->params.nts_port = SRC_DEFAULT_NTSPORT;
67 src->params.copy = 0;
68 src->params.authkey = INACTIVE_AUTHKEY;
69 src->params.cert_set = SRC_DEFAULT_CERTSET;
70 src->params.max_delay = SRC_DEFAULT_MAXDELAY;
71 src->params.max_delay_ratio = SRC_DEFAULT_MAXDELAYRATIO;
72 src->params.max_delay_dev_ratio = SRC_DEFAULT_MAXDELAYDEVRATIO;
73 src->params.min_delay = 0.0;
74 src->params.asymmetry = SRC_DEFAULT_ASYMMETRY;
75 src->params.offset = 0.0;
76
77 hostname = line;
78 line = CPS_SplitWord(line);
79
80 if (!*hostname)
81 return 0;
82
83 src->name = hostname;
84
85 /* Parse options */
86 for (; *line; line += n) {
87 cmd = line;
88 line = CPS_SplitWord(line);
89 n = 0;
90
91 if (!strcasecmp(cmd, "auto_offline")) {
92 src->params.auto_offline = 1;
93 } else if (!strcasecmp(cmd, "burst")) {
94 src->params.burst = 1;
95 } else if (!strcasecmp(cmd, "copy")) {
96 src->params.copy = 1;
97 } else if (!strcasecmp(cmd, "iburst")) {
98 src->params.iburst = 1;
99 } else if (!strcasecmp(cmd, "offline")) {
100 src->params.connectivity = SRC_OFFLINE;
101 } else if (!strcasecmp(cmd, "noselect")) {
102 src->params.sel_options |= SRC_SELECT_NOSELECT;
103 } else if (!strcasecmp(cmd, "prefer")) {
104 src->params.sel_options |= SRC_SELECT_PREFER;
105 } else if (!strcasecmp(cmd, "require")) {
106 src->params.sel_options |= SRC_SELECT_REQUIRE;
107 } else if (!strcasecmp(cmd, "trust")) {
108 src->params.sel_options |= SRC_SELECT_TRUST;
109 } else if (!strcasecmp(cmd, "certset")) {
110 if (sscanf(line, "%"SCNu32"%n", &src->params.cert_set, &n) != 1)
111 return 0;
112 } else if (!strcasecmp(cmd, "key")) {
113 if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
114 src->params.authkey == INACTIVE_AUTHKEY)
115 return 0;
116 } else if (!strcasecmp(cmd, "asymmetry")) {
117 if (sscanf(line, "%lf%n", &src->params.asymmetry, &n) != 1)
118 return 0;
119 } else if (!strcasecmp(cmd, "filter")) {
120 if (sscanf(line, "%d%n", &src->params.filter_length, &n) != 1)
121 return 0;
122 } else if (!strcasecmp(cmd, "maxdelay")) {
123 if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1)
124 return 0;
125 } else if (!strcasecmp(cmd, "maxdelayratio")) {
126 if (sscanf(line, "%lf%n", &src->params.max_delay_ratio, &n) != 1)
127 return 0;
128 } else if (!strcasecmp(cmd, "maxdelaydevratio")) {
129 if (sscanf(line, "%lf%n", &src->params.max_delay_dev_ratio, &n) != 1)
130 return 0;
131 } else if (!strcasecmp(cmd, "maxpoll")) {
132 if (sscanf(line, "%d%n", &src->params.maxpoll, &n) != 1)
133 return 0;
134 } else if (!strcasecmp(cmd, "maxsamples")) {
135 if (sscanf(line, "%d%n", &src->params.max_samples, &n) != 1)
136 return 0;
137 } else if (!strcasecmp(cmd, "maxsources")) {
138 if (sscanf(line, "%d%n", &src->params.max_sources, &n) != 1)
139 return 0;
140 } else if (!strcasecmp(cmd, "mindelay")) {
141 if (sscanf(line, "%lf%n", &src->params.min_delay, &n) != 1)
142 return 0;
143 } else if (!strcasecmp(cmd, "minpoll")) {
144 if (sscanf(line, "%d%n", &src->params.minpoll, &n) != 1)
145 return 0;
146 } else if (!strcasecmp(cmd, "minsamples")) {
147 if (sscanf(line, "%d%n", &src->params.min_samples, &n) != 1)
148 return 0;
149 } else if (!strcasecmp(cmd, "minstratum")) {
150 if (sscanf(line, "%d%n", &src->params.min_stratum, &n) != 1)
151 return 0;
152 } else if (!strcasecmp(cmd, "nts")) {
153 src->params.nts = 1;
154 } else if (!strcasecmp(cmd, "ntsport")) {
155 if (sscanf(line, "%d%n", &src->params.nts_port, &n) != 1)
156 return 0;
157 } else if (!strcasecmp(cmd, "offset")) {
158 if (sscanf(line, "%lf%n", &src->params.offset, &n) != 1)
159 return 0;
160 } else if (!strcasecmp(cmd, "port")) {
161 if (sscanf(line, "%d%n", &src->port, &n) != 1)
162 return 0;
163 } else if (!strcasecmp(cmd, "polltarget")) {
164 if (sscanf(line, "%d%n", &src->params.poll_target, &n) != 1)
165 return 0;
166 } else if (!strcasecmp(cmd, "presend")) {
167 if (sscanf(line, "%d%n", &src->params.presend_minpoll, &n) != 1)
168 return 0;
169 } else if (!strcasecmp(cmd, "version")) {
170 if (sscanf(line, "%d%n", &src->params.version, &n) != 1)
171 return 0;
172 } else if (!strcasecmp(cmd, "xleave")) {
173 src->params.interleaved = 1;
174 } else {
175 return 0;
176 }
177 }
178
179 return 1;
180 }
181
182 /* ================================================== */
183
184 int
185 CPS_ParseAllowDeny(char *line, int *all, IPAddr *ip, int *subnet_bits)
186 {
187 char *p, *net, *slash;
188 uint32_t a, b, c;
189 int bits, len, n;
190
191 p = CPS_SplitWord(line);
192
193 if (strcmp(line, "all") == 0) {
194 *all = 1;
195 net = p;
196 p = CPS_SplitWord(p);
197 } else {
198 *all = 0;
199 net = line;
200 }
201
202 /* Make sure there are no other arguments */
203 if (*p)
204 return 0;
205
206 /* No specified address or network means all IPv4 and IPv6 addresses */
207 if (!*net) {
208 ip->family = IPADDR_UNSPEC;
209 *subnet_bits = 0;
210 return 1;
211 }
212
213 slash = strchr(net, '/');
214 if (slash) {
215 if (sscanf(slash + 1, "%d%n", &bits, &len) != 1 || slash[len + 1] || bits < 0)
216 return 0;
217 *slash = '\0';
218 } else {
219 bits = -1;
220 }
221
222 if (UTI_StringToIP(net, ip)) {
223 if (bits >= 0)
224 *subnet_bits = bits;
225 else
226 *subnet_bits = ip->family == IPADDR_INET6 ? 128 : 32;
227 return 1;
228 }
229
230 /* Check for a shortened IPv4 network notation using only 1, 2, or 3 decimal
231 numbers. This is different than the numbers-and-dots notation accepted
232 by inet_aton()! */
233
234 a = b = c = 0;
235 n = sscanf(net, "%"PRIu32"%n.%"PRIu32"%n.%"PRIu32"%n", &a, &len, &b, &len, &c, &len);
236
237 if (n > 0 && !net[len]) {
238 if (a > 255 || b > 255 || c > 255)
239 return 0;
240
241 ip->family = IPADDR_INET4;
242 ip->addr.in4 = (a << 24) | (b << 16) | (c << 8);
243
244 if (bits >= 0)
245 *subnet_bits = bits;
246 else
247 *subnet_bits = n * 8;
248
249 return 1;
250 }
251
252 /* The last possibility is a hostname */
253 if (bits < 0 && DNS_Name2IPAddress(net, ip, 1) == DNS_Success) {
254 *subnet_bits = ip->family == IPADDR_INET6 ? 128 : 32;
255 return 1;
256 }
257
258 return 0;
259 }
260
261 /* ================================================== */
262
263 int
264 CPS_ParseLocal(char *line, int *stratum, int *orphan, double *distance)
265 {
266 int n;
267 char *cmd;
268
269 *stratum = 10;
270 *distance = 1.0;
271 *orphan = 0;
272
273 while (*line) {
274 cmd = line;
275 line = CPS_SplitWord(line);
276
277 if (!strcasecmp(cmd, "stratum")) {
278 if (sscanf(line, "%d%n", stratum, &n) != 1 ||
279 *stratum >= NTP_MAX_STRATUM || *stratum <= 0)
280 return 0;
281 } else if (!strcasecmp(cmd, "orphan")) {
282 *orphan = 1;
283 n = 0;
284 } else if (!strcasecmp(cmd, "distance")) {
285 if (sscanf(line, "%lf%n", distance, &n) != 1)
286 return 0;
287 } else {
288 return 0;
289 }
290
291 line += n;
292 }
293
294 return 1;
295 }
296
297 /* ================================================== */
298
299 void
300 CPS_NormalizeLine(char *line)
301 {
302 char *p, *q;
303 int space = 1, first = 1;
304
305 /* Remove white-space at beginning and replace white-spaces with space char */
306 for (p = q = line; *p; p++) {
307 if (isspace((unsigned char)*p)) {
308 if (!space)
309 *q++ = ' ';
310 space = 1;
311 continue;
312 }
313
314 /* Discard comment lines */
315 if (first && strchr("!;#%", *p))
316 break;
317
318 *q++ = *p;
319 space = first = 0;
320 }
321
322 /* Strip trailing space */
323 if (q > line && q[-1] == ' ')
324 q--;
325
326 *q = '\0';
327 }
328
329 /* ================================================== */
330
331 char *
332 CPS_SplitWord(char *line)
333 {
334 char *p = line, *q = line;
335
336 /* Skip white-space before the word */
337 while (*q && isspace((unsigned char)*q))
338 q++;
339
340 /* Move the word to the beginning */
341 while (*q && !isspace((unsigned char)*q))
342 *p++ = *q++;
343
344 /* Find the next word */
345 while (*q && isspace((unsigned char)*q))
346 q++;
347
348 *p = '\0';
349
350 /* Return pointer to the next word or NUL */
351 return q;
352 }
353
354 /* ================================================== */
355
356 int
357 CPS_ParseKey(char *line, uint32_t *id, const char **type, char **key)
358 {
359 char *s1, *s2, *s3, *s4;
360
361 s1 = line;
362 s2 = CPS_SplitWord(s1);
363 s3 = CPS_SplitWord(s2);
364 s4 = CPS_SplitWord(s3);
365
366 /* Require two or three words */
367 if (!*s2 || *s4)
368 return 0;
369
370 if (sscanf(s1, "%"SCNu32, id) != 1)
371 return 0;
372
373 if (*s3) {
374 *type = s2;
375 *key = s3;
376 } else {
377 *type = "MD5";
378 *key = s2;
379 }
380
381 return 1;
382 }