2 * ntp_control.c - respond to mode 6 control messages and send async
3 * traps. Provides service to ntpq and others.
7 * $FreeBSD: projects/release-pkg/contrib/ntp/ntpd/ntp_control.c 277386 2015-01-19 16:15:12Z gjb $
18 #ifdef HAVE_NETINET_IN_H
19 # include <netinet/in.h>
21 #include <arpa/inet.h>
25 #include "ntp_refclock.h"
26 #include "ntp_control.h"
27 #include "ntp_unixtime.h"
28 #include "ntp_stdlib.h"
29 #include "ntp_config.h"
30 #include "ntp_crypto.h"
31 #include "ntp_assert.h"
32 #include "ntp_leapsec.h"
33 #include "ntp_md5.h" /* provides OpenSSL digest API */
34 #include "lib_strbuf.h"
35 #include <rc_cmdlength.h>
37 # include "ntp_syscall.h"
42 * Structure to hold request procedure information
46 short control_code; /* defined request code */
47 #define NO_REQUEST (-1)
48 u_short flags; /* flags word */
49 /* Only one flag. Authentication required or not. */
52 void (*handler) (struct recvbuf *, int); /* handle request */
57 * Request processing routines
59 static void ctl_error (u_char);
61 static u_short ctlclkstatus (struct refclockstat *);
63 static void ctl_flushpkt (u_char);
64 static void ctl_putdata (const char *, unsigned int, int);
65 static void ctl_putstr (const char *, const char *, size_t);
66 static void ctl_putdblf (const char *, int, int, double);
67 #define ctl_putdbl(tag, d) ctl_putdblf(tag, 1, 3, d)
68 #define ctl_putdbl6(tag, d) ctl_putdblf(tag, 1, 6, d)
69 #define ctl_putsfp(tag, sfp) ctl_putdblf(tag, 0, -1, \
71 static void ctl_putuint (const char *, u_long);
72 static void ctl_puthex (const char *, u_long);
73 static void ctl_putint (const char *, long);
74 static void ctl_putts (const char *, l_fp *);
75 static void ctl_putadr (const char *, u_int32,
77 static void ctl_putrefid (const char *, u_int32);
78 static void ctl_putarray (const char *, double *, int);
79 static void ctl_putsys (int);
80 static void ctl_putpeer (int, struct peer *);
81 static void ctl_putfs (const char *, tstamp_t);
83 static void ctl_putclock (int, struct refclockstat *, int);
85 static const struct ctl_var *ctl_getitem(const struct ctl_var *,
87 static u_short count_var (const struct ctl_var *);
88 static void control_unspec (struct recvbuf *, int);
89 static void read_status (struct recvbuf *, int);
90 static void read_sysvars (void);
91 static void read_peervars (void);
92 static void read_variables (struct recvbuf *, int);
93 static void write_variables (struct recvbuf *, int);
94 static void read_clockstatus(struct recvbuf *, int);
95 static void write_clockstatus(struct recvbuf *, int);
96 static void set_trap (struct recvbuf *, int);
97 static void save_config (struct recvbuf *, int);
98 static void configure (struct recvbuf *, int);
99 static void send_mru_entry (mon_entry *, int);
100 static void send_random_tag_value(int);
101 static void read_mru_list (struct recvbuf *, int);
102 static void send_ifstats_entry(endpt *, u_int);
103 static void read_ifstats (struct recvbuf *);
104 static void sockaddrs_from_restrict_u(sockaddr_u *, sockaddr_u *,
106 static void send_restrict_entry(restrict_u *, int, u_int);
107 static void send_restrict_list(restrict_u *, int, u_int *);
108 static void read_addr_restrictions(struct recvbuf *);
109 static void read_ordlist (struct recvbuf *, int);
110 static u_int32 derive_nonce (sockaddr_u *, u_int32, u_int32);
111 static void generate_nonce (struct recvbuf *, char *, size_t);
112 static int validate_nonce (const char *, struct recvbuf *);
113 static void req_nonce (struct recvbuf *, int);
114 static void unset_trap (struct recvbuf *, int);
115 static struct ctl_trap *ctlfindtrap(sockaddr_u *,
118 static const struct ctl_proc control_codes[] = {
119 { CTL_OP_UNSPEC, NOAUTH, control_unspec },
120 { CTL_OP_READSTAT, NOAUTH, read_status },
121 { CTL_OP_READVAR, NOAUTH, read_variables },
122 { CTL_OP_WRITEVAR, AUTH, write_variables },
123 { CTL_OP_READCLOCK, NOAUTH, read_clockstatus },
124 { CTL_OP_WRITECLOCK, NOAUTH, write_clockstatus },
125 { CTL_OP_SETTRAP, NOAUTH, set_trap },
126 { CTL_OP_CONFIGURE, AUTH, configure },
127 { CTL_OP_SAVECONFIG, AUTH, save_config },
128 { CTL_OP_READ_MRU, NOAUTH, read_mru_list },
129 { CTL_OP_READ_ORDLIST_A, AUTH, read_ordlist },
130 { CTL_OP_REQ_NONCE, NOAUTH, req_nonce },
131 { CTL_OP_UNSETTRAP, NOAUTH, unset_trap },
132 { NO_REQUEST, 0, NULL }
136 * System variables we understand
140 #define CS_PRECISION 3
141 #define CS_ROOTDELAY 4
142 #define CS_ROOTDISPERSION 5
152 #define CS_PROCESSOR 15
154 #define CS_VERSION 17
156 #define CS_VARLIST 19
158 #define CS_LEAPTAB 21
159 #define CS_LEAPEND 22
161 #define CS_MRU_ENABLED 24
162 #define CS_MRU_DEPTH 25
163 #define CS_MRU_DEEPEST 26
164 #define CS_MRU_MINDEPTH 27
165 #define CS_MRU_MAXAGE 28
166 #define CS_MRU_MAXDEPTH 29
167 #define CS_MRU_MEM 30
168 #define CS_MRU_MAXMEM 31
169 #define CS_SS_UPTIME 32
170 #define CS_SS_RESET 33
171 #define CS_SS_RECEIVED 34
172 #define CS_SS_THISVER 35
173 #define CS_SS_OLDVER 36
174 #define CS_SS_BADFORMAT 37
175 #define CS_SS_BADAUTH 38
176 #define CS_SS_DECLINED 39
177 #define CS_SS_RESTRICTED 40
178 #define CS_SS_LIMITED 41
179 #define CS_SS_KODSENT 42
180 #define CS_SS_PROCESSED 43
181 #define CS_PEERADR 44
182 #define CS_PEERMODE 45
183 #define CS_BCASTDELAY 46
184 #define CS_AUTHDELAY 47
185 #define CS_AUTHKEYS 48
186 #define CS_AUTHFREEK 49
187 #define CS_AUTHKLOOKUPS 50
188 #define CS_AUTHKNOTFOUND 51
189 #define CS_AUTHKUNCACHED 52
190 #define CS_AUTHKEXPIRED 53
191 #define CS_AUTHENCRYPTS 54
192 #define CS_AUTHDECRYPTS 55
193 #define CS_AUTHRESET 56
194 #define CS_K_OFFSET 57
196 #define CS_K_MAXERR 59
197 #define CS_K_ESTERR 60
198 #define CS_K_STFLAGS 61
199 #define CS_K_TIMECONST 62
200 #define CS_K_PRECISION 63
201 #define CS_K_FREQTOL 64
202 #define CS_K_PPS_FREQ 65
203 #define CS_K_PPS_STABIL 66
204 #define CS_K_PPS_JITTER 67
205 #define CS_K_PPS_CALIBDUR 68
206 #define CS_K_PPS_CALIBS 69
207 #define CS_K_PPS_CALIBERRS 70
208 #define CS_K_PPS_JITEXC 71
209 #define CS_K_PPS_STBEXC 72
210 #define CS_KERN_FIRST CS_K_OFFSET
211 #define CS_KERN_LAST CS_K_PPS_STBEXC
212 #define CS_IOSTATS_RESET 73
213 #define CS_TOTAL_RBUF 74
214 #define CS_FREE_RBUF 75
215 #define CS_USED_RBUF 76
216 #define CS_RBUF_LOWATER 77
217 #define CS_IO_DROPPED 78
218 #define CS_IO_IGNORED 79
219 #define CS_IO_RECEIVED 80
220 #define CS_IO_SENT 81
221 #define CS_IO_SENDFAILED 82
222 #define CS_IO_WAKEUPS 83
223 #define CS_IO_GOODWAKEUPS 84
224 #define CS_TIMERSTATS_RESET 85
225 #define CS_TIMER_OVERRUNS 86
226 #define CS_TIMER_XMTS 87
228 #define CS_WANDER_THRESH 89
229 #define CS_LEAPSMEARINTV 90
230 #define CS_LEAPSMEAROFFS 91
231 #define CS_MAX_NOAUTOKEY CS_LEAPSMEAROFFS
233 #define CS_FLAGS (1 + CS_MAX_NOAUTOKEY)
234 #define CS_HOST (2 + CS_MAX_NOAUTOKEY)
235 #define CS_PUBLIC (3 + CS_MAX_NOAUTOKEY)
236 #define CS_CERTIF (4 + CS_MAX_NOAUTOKEY)
237 #define CS_SIGNATURE (5 + CS_MAX_NOAUTOKEY)
238 #define CS_REVTIME (6 + CS_MAX_NOAUTOKEY)
239 #define CS_IDENT (7 + CS_MAX_NOAUTOKEY)
240 #define CS_DIGEST (8 + CS_MAX_NOAUTOKEY)
241 #define CS_MAXCODE CS_DIGEST
242 #else /* !AUTOKEY follows */
243 #define CS_MAXCODE CS_MAX_NOAUTOKEY
244 #endif /* !AUTOKEY */
247 * Peer variables we understand
250 #define CP_AUTHENABLE 2
251 #define CP_AUTHENTIC 3
258 #define CP_STRATUM 10
261 #define CP_PRECISION 13
262 #define CP_ROOTDELAY 14
263 #define CP_ROOTDISPERSION 15
265 #define CP_REFTIME 17
270 #define CP_UNREACH 22
275 #define CP_DISPERSION 27
277 #define CP_FILTDELAY 29
278 #define CP_FILTOFFSET 30
280 #define CP_RECEIVED 32
282 #define CP_FILTERROR 34
285 #define CP_VARLIST 37
290 #define CP_SRCHOST 42
291 #define CP_TIMEREC 43
292 #define CP_TIMEREACH 44
293 #define CP_BADAUTH 45
294 #define CP_BOGUSORG 46
296 #define CP_SELDISP 48
297 #define CP_SELBROKEN 49
298 #define CP_CANDIDATE 50
299 #define CP_MAX_NOAUTOKEY CP_CANDIDATE
301 #define CP_FLAGS (1 + CP_MAX_NOAUTOKEY)
302 #define CP_HOST (2 + CP_MAX_NOAUTOKEY)
303 #define CP_VALID (3 + CP_MAX_NOAUTOKEY)
304 #define CP_INITSEQ (4 + CP_MAX_NOAUTOKEY)
305 #define CP_INITKEY (5 + CP_MAX_NOAUTOKEY)
306 #define CP_INITTSP (6 + CP_MAX_NOAUTOKEY)
307 #define CP_SIGNATURE (7 + CP_MAX_NOAUTOKEY)
308 #define CP_IDENT (8 + CP_MAX_NOAUTOKEY)
309 #define CP_MAXCODE CP_IDENT
310 #else /* !AUTOKEY follows */
311 #define CP_MAXCODE CP_MAX_NOAUTOKEY
312 #endif /* !AUTOKEY */
315 * Clock variables we understand
318 #define CC_TIMECODE 2
321 #define CC_BADFORMAT 5
323 #define CC_FUDGETIME1 7
324 #define CC_FUDGETIME2 8
325 #define CC_FUDGEVAL1 9
326 #define CC_FUDGEVAL2 10
329 #define CC_VARLIST 13
330 #define CC_MAXCODE CC_VARLIST
333 * System variable values. The array can be indexed by the variable
334 * index to find the textual name.
336 static const struct ctl_var sys_var[] = {
337 { 0, PADDING, "" }, /* 0 */
338 { CS_LEAP, RW, "leap" }, /* 1 */
339 { CS_STRATUM, RO, "stratum" }, /* 2 */
340 { CS_PRECISION, RO, "precision" }, /* 3 */
341 { CS_ROOTDELAY, RO, "rootdelay" }, /* 4 */
342 { CS_ROOTDISPERSION, RO, "rootdisp" }, /* 5 */
343 { CS_REFID, RO, "refid" }, /* 6 */
344 { CS_REFTIME, RO, "reftime" }, /* 7 */
345 { CS_POLL, RO, "tc" }, /* 8 */
346 { CS_PEERID, RO, "peer" }, /* 9 */
347 { CS_OFFSET, RO, "offset" }, /* 10 */
348 { CS_DRIFT, RO, "frequency" }, /* 11 */
349 { CS_JITTER, RO, "sys_jitter" }, /* 12 */
350 { CS_ERROR, RO, "clk_jitter" }, /* 13 */
351 { CS_CLOCK, RO, "clock" }, /* 14 */
352 { CS_PROCESSOR, RO, "processor" }, /* 15 */
353 { CS_SYSTEM, RO, "system" }, /* 16 */
354 { CS_VERSION, RO, "version" }, /* 17 */
355 { CS_STABIL, RO, "clk_wander" }, /* 18 */
356 { CS_VARLIST, RO, "sys_var_list" }, /* 19 */
357 { CS_TAI, RO, "tai" }, /* 20 */
358 { CS_LEAPTAB, RO, "leapsec" }, /* 21 */
359 { CS_LEAPEND, RO, "expire" }, /* 22 */
360 { CS_RATE, RO, "mintc" }, /* 23 */
361 { CS_MRU_ENABLED, RO, "mru_enabled" }, /* 24 */
362 { CS_MRU_DEPTH, RO, "mru_depth" }, /* 25 */
363 { CS_MRU_DEEPEST, RO, "mru_deepest" }, /* 26 */
364 { CS_MRU_MINDEPTH, RO, "mru_mindepth" }, /* 27 */
365 { CS_MRU_MAXAGE, RO, "mru_maxage" }, /* 28 */
366 { CS_MRU_MAXDEPTH, RO, "mru_maxdepth" }, /* 29 */
367 { CS_MRU_MEM, RO, "mru_mem" }, /* 30 */
368 { CS_MRU_MAXMEM, RO, "mru_maxmem" }, /* 31 */
369 { CS_SS_UPTIME, RO, "ss_uptime" }, /* 32 */
370 { CS_SS_RESET, RO, "ss_reset" }, /* 33 */
371 { CS_SS_RECEIVED, RO, "ss_received" }, /* 34 */
372 { CS_SS_THISVER, RO, "ss_thisver" }, /* 35 */
373 { CS_SS_OLDVER, RO, "ss_oldver" }, /* 36 */
374 { CS_SS_BADFORMAT, RO, "ss_badformat" }, /* 37 */
375 { CS_SS_BADAUTH, RO, "ss_badauth" }, /* 38 */
376 { CS_SS_DECLINED, RO, "ss_declined" }, /* 39 */
377 { CS_SS_RESTRICTED, RO, "ss_restricted" }, /* 40 */
378 { CS_SS_LIMITED, RO, "ss_limited" }, /* 41 */
379 { CS_SS_KODSENT, RO, "ss_kodsent" }, /* 42 */
380 { CS_SS_PROCESSED, RO, "ss_processed" }, /* 43 */
381 { CS_PEERADR, RO, "peeradr" }, /* 44 */
382 { CS_PEERMODE, RO, "peermode" }, /* 45 */
383 { CS_BCASTDELAY, RO, "bcastdelay" }, /* 46 */
384 { CS_AUTHDELAY, RO, "authdelay" }, /* 47 */
385 { CS_AUTHKEYS, RO, "authkeys" }, /* 48 */
386 { CS_AUTHFREEK, RO, "authfreek" }, /* 49 */
387 { CS_AUTHKLOOKUPS, RO, "authklookups" }, /* 50 */
388 { CS_AUTHKNOTFOUND, RO, "authknotfound" }, /* 51 */
389 { CS_AUTHKUNCACHED, RO, "authkuncached" }, /* 52 */
390 { CS_AUTHKEXPIRED, RO, "authkexpired" }, /* 53 */
391 { CS_AUTHENCRYPTS, RO, "authencrypts" }, /* 54 */
392 { CS_AUTHDECRYPTS, RO, "authdecrypts" }, /* 55 */
393 { CS_AUTHRESET, RO, "authreset" }, /* 56 */
394 { CS_K_OFFSET, RO, "koffset" }, /* 57 */
395 { CS_K_FREQ, RO, "kfreq" }, /* 58 */
396 { CS_K_MAXERR, RO, "kmaxerr" }, /* 59 */
397 { CS_K_ESTERR, RO, "kesterr" }, /* 60 */
398 { CS_K_STFLAGS, RO, "kstflags" }, /* 61 */
399 { CS_K_TIMECONST, RO, "ktimeconst" }, /* 62 */
400 { CS_K_PRECISION, RO, "kprecis" }, /* 63 */
401 { CS_K_FREQTOL, RO, "kfreqtol" }, /* 64 */
402 { CS_K_PPS_FREQ, RO, "kppsfreq" }, /* 65 */
403 { CS_K_PPS_STABIL, RO, "kppsstab" }, /* 66 */
404 { CS_K_PPS_JITTER, RO, "kppsjitter" }, /* 67 */
405 { CS_K_PPS_CALIBDUR, RO, "kppscalibdur" }, /* 68 */
406 { CS_K_PPS_CALIBS, RO, "kppscalibs" }, /* 69 */
407 { CS_K_PPS_CALIBERRS, RO, "kppscaliberrs" }, /* 70 */
408 { CS_K_PPS_JITEXC, RO, "kppsjitexc" }, /* 71 */
409 { CS_K_PPS_STBEXC, RO, "kppsstbexc" }, /* 72 */
410 { CS_IOSTATS_RESET, RO, "iostats_reset" }, /* 73 */
411 { CS_TOTAL_RBUF, RO, "total_rbuf" }, /* 74 */
412 { CS_FREE_RBUF, RO, "free_rbuf" }, /* 75 */
413 { CS_USED_RBUF, RO, "used_rbuf" }, /* 76 */
414 { CS_RBUF_LOWATER, RO, "rbuf_lowater" }, /* 77 */
415 { CS_IO_DROPPED, RO, "io_dropped" }, /* 78 */
416 { CS_IO_IGNORED, RO, "io_ignored" }, /* 79 */
417 { CS_IO_RECEIVED, RO, "io_received" }, /* 80 */
418 { CS_IO_SENT, RO, "io_sent" }, /* 81 */
419 { CS_IO_SENDFAILED, RO, "io_sendfailed" }, /* 82 */
420 { CS_IO_WAKEUPS, RO, "io_wakeups" }, /* 83 */
421 { CS_IO_GOODWAKEUPS, RO, "io_goodwakeups" }, /* 84 */
422 { CS_TIMERSTATS_RESET, RO, "timerstats_reset" },/* 85 */
423 { CS_TIMER_OVERRUNS, RO, "timer_overruns" }, /* 86 */
424 { CS_TIMER_XMTS, RO, "timer_xmts" }, /* 87 */
425 { CS_FUZZ, RO, "fuzz" }, /* 88 */
426 { CS_WANDER_THRESH, RO, "clk_wander_threshold" }, /* 89 */
428 { CS_LEAPSMEARINTV, RO, "leapsmearinterval" }, /* 90 */
429 { CS_LEAPSMEAROFFS, RO, "leapsmearoffset" }, /* 91 */
432 { CS_FLAGS, RO, "flags" }, /* 1 + CS_MAX_NOAUTOKEY */
433 { CS_HOST, RO, "host" }, /* 2 + CS_MAX_NOAUTOKEY */
434 { CS_PUBLIC, RO, "update" }, /* 3 + CS_MAX_NOAUTOKEY */
435 { CS_CERTIF, RO, "cert" }, /* 4 + CS_MAX_NOAUTOKEY */
436 { CS_SIGNATURE, RO, "signature" }, /* 5 + CS_MAX_NOAUTOKEY */
437 { CS_REVTIME, RO, "until" }, /* 6 + CS_MAX_NOAUTOKEY */
438 { CS_IDENT, RO, "ident" }, /* 7 + CS_MAX_NOAUTOKEY */
439 { CS_DIGEST, RO, "digest" }, /* 8 + CS_MAX_NOAUTOKEY */
441 { 0, EOV, "" } /* 87/95 */
444 static struct ctl_var *ext_sys_var = NULL;
447 * System variables we print by default (in fuzzball order,
450 static const u_char def_sys_var[] = {
491 static const struct ctl_var peer_var[] = {
492 { 0, PADDING, "" }, /* 0 */
493 { CP_CONFIG, RO, "config" }, /* 1 */
494 { CP_AUTHENABLE, RO, "authenable" }, /* 2 */
495 { CP_AUTHENTIC, RO, "authentic" }, /* 3 */
496 { CP_SRCADR, RO, "srcadr" }, /* 4 */
497 { CP_SRCPORT, RO, "srcport" }, /* 5 */
498 { CP_DSTADR, RO, "dstadr" }, /* 6 */
499 { CP_DSTPORT, RO, "dstport" }, /* 7 */
500 { CP_LEAP, RO, "leap" }, /* 8 */
501 { CP_HMODE, RO, "hmode" }, /* 9 */
502 { CP_STRATUM, RO, "stratum" }, /* 10 */
503 { CP_PPOLL, RO, "ppoll" }, /* 11 */
504 { CP_HPOLL, RO, "hpoll" }, /* 12 */
505 { CP_PRECISION, RO, "precision" }, /* 13 */
506 { CP_ROOTDELAY, RO, "rootdelay" }, /* 14 */
507 { CP_ROOTDISPERSION, RO, "rootdisp" }, /* 15 */
508 { CP_REFID, RO, "refid" }, /* 16 */
509 { CP_REFTIME, RO, "reftime" }, /* 17 */
510 { CP_ORG, RO, "org" }, /* 18 */
511 { CP_REC, RO, "rec" }, /* 19 */
512 { CP_XMT, RO, "xleave" }, /* 20 */
513 { CP_REACH, RO, "reach" }, /* 21 */
514 { CP_UNREACH, RO, "unreach" }, /* 22 */
515 { CP_TIMER, RO, "timer" }, /* 23 */
516 { CP_DELAY, RO, "delay" }, /* 24 */
517 { CP_OFFSET, RO, "offset" }, /* 25 */
518 { CP_JITTER, RO, "jitter" }, /* 26 */
519 { CP_DISPERSION, RO, "dispersion" }, /* 27 */
520 { CP_KEYID, RO, "keyid" }, /* 28 */
521 { CP_FILTDELAY, RO, "filtdelay" }, /* 29 */
522 { CP_FILTOFFSET, RO, "filtoffset" }, /* 30 */
523 { CP_PMODE, RO, "pmode" }, /* 31 */
524 { CP_RECEIVED, RO, "received"}, /* 32 */
525 { CP_SENT, RO, "sent" }, /* 33 */
526 { CP_FILTERROR, RO, "filtdisp" }, /* 34 */
527 { CP_FLASH, RO, "flash" }, /* 35 */
528 { CP_TTL, RO, "ttl" }, /* 36 */
529 { CP_VARLIST, RO, "peer_var_list" }, /* 37 */
530 { CP_IN, RO, "in" }, /* 38 */
531 { CP_OUT, RO, "out" }, /* 39 */
532 { CP_RATE, RO, "headway" }, /* 40 */
533 { CP_BIAS, RO, "bias" }, /* 41 */
534 { CP_SRCHOST, RO, "srchost" }, /* 42 */
535 { CP_TIMEREC, RO, "timerec" }, /* 43 */
536 { CP_TIMEREACH, RO, "timereach" }, /* 44 */
537 { CP_BADAUTH, RO, "badauth" }, /* 45 */
538 { CP_BOGUSORG, RO, "bogusorg" }, /* 46 */
539 { CP_OLDPKT, RO, "oldpkt" }, /* 47 */
540 { CP_SELDISP, RO, "seldisp" }, /* 48 */
541 { CP_SELBROKEN, RO, "selbroken" }, /* 49 */
542 { CP_CANDIDATE, RO, "candidate" }, /* 50 */
544 { CP_FLAGS, RO, "flags" }, /* 1 + CP_MAX_NOAUTOKEY */
545 { CP_HOST, RO, "host" }, /* 2 + CP_MAX_NOAUTOKEY */
546 { CP_VALID, RO, "valid" }, /* 3 + CP_MAX_NOAUTOKEY */
547 { CP_INITSEQ, RO, "initsequence" }, /* 4 + CP_MAX_NOAUTOKEY */
548 { CP_INITKEY, RO, "initkey" }, /* 5 + CP_MAX_NOAUTOKEY */
549 { CP_INITTSP, RO, "timestamp" }, /* 6 + CP_MAX_NOAUTOKEY */
550 { CP_SIGNATURE, RO, "signature" }, /* 7 + CP_MAX_NOAUTOKEY */
551 { CP_IDENT, RO, "ident" }, /* 8 + CP_MAX_NOAUTOKEY */
553 { 0, EOV, "" } /* 50/58 */
558 * Peer variables we print by default
560 static const u_char def_peer_var[] = {
609 * Clock variable list
611 static const struct ctl_var clock_var[] = {
612 { 0, PADDING, "" }, /* 0 */
613 { CC_TYPE, RO, "type" }, /* 1 */
614 { CC_TIMECODE, RO, "timecode" }, /* 2 */
615 { CC_POLL, RO, "poll" }, /* 3 */
616 { CC_NOREPLY, RO, "noreply" }, /* 4 */
617 { CC_BADFORMAT, RO, "badformat" }, /* 5 */
618 { CC_BADDATA, RO, "baddata" }, /* 6 */
619 { CC_FUDGETIME1, RO, "fudgetime1" }, /* 7 */
620 { CC_FUDGETIME2, RO, "fudgetime2" }, /* 8 */
621 { CC_FUDGEVAL1, RO, "stratum" }, /* 9 */
622 { CC_FUDGEVAL2, RO, "refid" }, /* 10 */
623 { CC_FLAGS, RO, "flags" }, /* 11 */
624 { CC_DEVICE, RO, "device" }, /* 12 */
625 { CC_VARLIST, RO, "clock_var_list" }, /* 13 */
626 { 0, EOV, "" } /* 14 */
631 * Clock variables printed by default
633 static const u_char def_clock_var[] = {
635 CC_TYPE, /* won't be output if device = known */
651 * MRU string constants shared by send_mru_entry() and read_mru_list().
653 static const char addr_fmt[] = "addr.%d";
654 static const char last_fmt[] = "last.%d";
657 * System and processor definitions.
661 # define STR_SYSTEM "UNIX"
663 # ifndef STR_PROCESSOR
664 # define STR_PROCESSOR "unknown"
667 static const char str_system[] = STR_SYSTEM;
668 static const char str_processor[] = STR_PROCESSOR;
670 # include <sys/utsname.h>
671 static struct utsname utsnamebuf;
672 #endif /* HAVE_UNAME */
675 * Trap structures. We only allow a few of these, and send a copy of
676 * each async message to each live one. Traps time out after an hour, it
677 * is up to the trap receipient to keep resetting it to avoid being
681 struct ctl_trap ctl_traps[CTL_MAXTRAPS];
685 * Type bits, for ctlsettrap() call.
687 #define TRAP_TYPE_CONFIG 0 /* used by configuration code */
688 #define TRAP_TYPE_PRIO 1 /* priority trap */
689 #define TRAP_TYPE_NONPRIO 2 /* nonpriority trap */
693 * List relating reference clock types to control message time sources.
694 * Index by the reference clock type. This list will only be used iff
695 * the reference clock driver doesn't set peer->sstclktype to something
696 * different than CTL_SST_TS_UNSPEC.
699 static const u_char clocktypes[] = {
700 CTL_SST_TS_NTP, /* REFCLK_NONE (0) */
701 CTL_SST_TS_LOCAL, /* REFCLK_LOCALCLOCK (1) */
702 CTL_SST_TS_UHF, /* deprecated REFCLK_GPS_TRAK (2) */
703 CTL_SST_TS_HF, /* REFCLK_WWV_PST (3) */
704 CTL_SST_TS_LF, /* REFCLK_WWVB_SPECTRACOM (4) */
705 CTL_SST_TS_UHF, /* REFCLK_TRUETIME (5) */
706 CTL_SST_TS_UHF, /* REFCLK_IRIG_AUDIO (6) */
707 CTL_SST_TS_HF, /* REFCLK_CHU (7) */
708 CTL_SST_TS_LF, /* REFCLOCK_PARSE (default) (8) */
709 CTL_SST_TS_LF, /* REFCLK_GPS_MX4200 (9) */
710 CTL_SST_TS_UHF, /* REFCLK_GPS_AS2201 (10) */
711 CTL_SST_TS_UHF, /* REFCLK_GPS_ARBITER (11) */
712 CTL_SST_TS_UHF, /* REFCLK_IRIG_TPRO (12) */
713 CTL_SST_TS_ATOM, /* REFCLK_ATOM_LEITCH (13) */
714 CTL_SST_TS_LF, /* deprecated REFCLK_MSF_EES (14) */
715 CTL_SST_TS_NTP, /* not used (15) */
716 CTL_SST_TS_UHF, /* REFCLK_IRIG_BANCOMM (16) */
717 CTL_SST_TS_UHF, /* REFCLK_GPS_DATU (17) */
718 CTL_SST_TS_TELEPHONE, /* REFCLK_NIST_ACTS (18) */
719 CTL_SST_TS_HF, /* REFCLK_WWV_HEATH (19) */
720 CTL_SST_TS_UHF, /* REFCLK_GPS_NMEA (20) */
721 CTL_SST_TS_UHF, /* REFCLK_GPS_VME (21) */
722 CTL_SST_TS_ATOM, /* REFCLK_ATOM_PPS (22) */
723 CTL_SST_TS_NTP, /* not used (23) */
724 CTL_SST_TS_NTP, /* not used (24) */
725 CTL_SST_TS_NTP, /* not used (25) */
726 CTL_SST_TS_UHF, /* REFCLK_GPS_HP (26) */
727 CTL_SST_TS_LF, /* REFCLK_ARCRON_MSF (27) */
728 CTL_SST_TS_UHF, /* REFCLK_SHM (28) */
729 CTL_SST_TS_UHF, /* REFCLK_PALISADE (29) */
730 CTL_SST_TS_UHF, /* REFCLK_ONCORE (30) */
731 CTL_SST_TS_UHF, /* REFCLK_JUPITER (31) */
732 CTL_SST_TS_LF, /* REFCLK_CHRONOLOG (32) */
733 CTL_SST_TS_LF, /* REFCLK_DUMBCLOCK (33) */
734 CTL_SST_TS_LF, /* REFCLK_ULINK (34) */
735 CTL_SST_TS_LF, /* REFCLK_PCF (35) */
736 CTL_SST_TS_HF, /* REFCLK_WWV (36) */
737 CTL_SST_TS_LF, /* REFCLK_FG (37) */
738 CTL_SST_TS_UHF, /* REFCLK_HOPF_SERIAL (38) */
739 CTL_SST_TS_UHF, /* REFCLK_HOPF_PCI (39) */
740 CTL_SST_TS_LF, /* REFCLK_JJY (40) */
741 CTL_SST_TS_UHF, /* REFCLK_TT560 (41) */
742 CTL_SST_TS_UHF, /* REFCLK_ZYFER (42) */
743 CTL_SST_TS_UHF, /* REFCLK_RIPENCC (43) */
744 CTL_SST_TS_UHF, /* REFCLK_NEOCLOCK4X (44) */
745 CTL_SST_TS_UHF, /* REFCLK_TSYNCPCI (45) */
746 CTL_SST_TS_UHF /* REFCLK_GPSDJSON (46) */
748 #endif /* REFCLOCK */
752 * Keyid used for authenticating write requests.
754 keyid_t ctl_auth_keyid;
757 * We keep track of the last error reported by the system internally
759 static u_char ctl_sys_last_event;
760 static u_char ctl_sys_num_events;
764 * Statistic counters to keep track of requests and responses.
766 u_long ctltimereset; /* time stats reset */
767 u_long numctlreq; /* number of requests we've received */
768 u_long numctlbadpkts; /* number of bad control packets */
769 u_long numctlresponses; /* number of resp packets sent with data */
770 u_long numctlfrags; /* number of fragments sent */
771 u_long numctlerrors; /* number of error responses sent */
772 u_long numctltooshort; /* number of too short input packets */
773 u_long numctlinputresp; /* number of responses on input */
774 u_long numctlinputfrag; /* number of fragments on input */
775 u_long numctlinputerr; /* number of input pkts with err bit set */
776 u_long numctlbadoffset; /* number of input pkts with nonzero offset */
777 u_long numctlbadversion; /* number of input pkts with unknown version */
778 u_long numctldatatooshort; /* data too short for count */
779 u_long numctlbadop; /* bad op code found in packet */
780 u_long numasyncmsgs; /* number of async messages we've sent */
783 * Response packet used by these routines. Also some state information
784 * so that we can handle packet formatting within a common set of
785 * subroutines. Note we try to enter data in place whenever possible,
786 * but the need to set the more bit correctly means we occasionally
787 * use the extra buffer and copy.
789 static struct ntp_control rpkt;
790 static u_char res_version;
791 static u_char res_opcode;
792 static associd_t res_associd;
793 static u_short res_frags; /* datagrams in this response */
794 static int res_offset; /* offset of payload in response */
795 static u_char * datapt;
796 static u_char * dataend;
797 static int datalinelen;
798 static int datasent; /* flag to avoid initial ", " */
799 static int datanotbinflag;
800 static sockaddr_u *rmt_addr;
801 static struct interface *lcl_inter;
803 static u_char res_authenticate;
804 static u_char res_authokay;
805 static keyid_t res_keyid;
807 #define MAXDATALINELEN (72)
809 static u_char res_async; /* sending async trap response? */
812 * Pointers for saving state when decoding request packets
818 #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
822 * init_control - initialize request data
831 #endif /* HAVE_UNAME */
836 ctl_sys_last_event = EVNT_UNSPEC;
837 ctl_sys_num_events = 0;
840 for (i = 0; i < COUNTOF(ctl_traps); i++)
841 ctl_traps[i].tr_flags = 0;
846 * ctl_error - send an error response for the current request
856 DPRINTF(3, ("sending control error %u\n", errcode));
859 * Fill in the fields. We assume rpkt.sequence and rpkt.associd
860 * have already been filled in.
862 rpkt.r_m_e_op = (u_char)CTL_RESPONSE | CTL_ERROR |
863 (res_opcode & CTL_OP_MASK);
864 rpkt.status = htons((u_short)(errcode << 8) & 0xff00);
868 * send packet and bump counters
870 if (res_authenticate && sys_authenticate) {
871 maclen = authencrypt(res_keyid, (u_int32 *)&rpkt,
873 sendpkt(rmt_addr, lcl_inter, -2, (void *)&rpkt,
874 CTL_HEADER_LEN + maclen);
876 sendpkt(rmt_addr, lcl_inter, -3, (void *)&rpkt,
881 * save_config - Implements ntpq -c "saveconfig <filename>"
882 * Writes current configuration including any runtime
883 * changes by ntpq's :config or config-from-file
887 struct recvbuf *rbufp,
891 /* block directory traversal by searching for characters that
892 * indicate directory components in a file path.
894 * Conceptually we should be searching for DIRSEP in filename,
895 * however Windows actually recognizes both forward and
896 * backslashes as equivalent directory separators at the API
897 * level. On POSIX systems we could allow '\\' but such
898 * filenames are tricky to manipulate from a shell, so just
899 * reject both types of slashes on all platforms.
901 /* TALOS-CAN-0062: block directory traversal for VMS, too */
902 static const char * illegal_in_filename =
904 ":[]" /* do not allow drive and path components here */
905 #elif defined(SYS_WINNT)
906 ":\\/" /* path and drive separators */
908 "\\/" /* separator and critical char for POSIX */
918 const char savedconfig_eq[] = "savedconfig=";
919 char savedconfig[sizeof(savedconfig_eq) + sizeof(filename)];
925 if (RES_NOMODIFY & restrict_mask) {
926 snprintf(reply, sizeof(reply),
927 "saveconfig prohibited by restrict ... nomodify");
928 ctl_putdata(reply, strlen(reply), 0);
932 "saveconfig from %s rejected due to nomodify restriction",
933 stoa(&rbufp->recv_srcadr));
939 if (NULL == saveconfigdir) {
940 snprintf(reply, sizeof(reply),
941 "saveconfig prohibited, no saveconfigdir configured");
942 ctl_putdata(reply, strlen(reply), 0);
946 "saveconfig from %s rejected, no saveconfigdir",
947 stoa(&rbufp->recv_srcadr));
951 if (0 == reqend - reqpt)
954 strlcpy(filespec, reqpt, sizeof(filespec));
958 * allow timestamping of the saved config filename with
959 * strftime() format such as:
960 * ntpq -c "saveconfig ntp-%Y%m%d-%H%M%S.conf"
961 * XXX: Nice feature, but not too safe.
963 if (0 == strftime(filename, sizeof(filename), filespec,
965 strlcpy(filename, filespec, sizeof(filename));
967 /* block directory/drive traversal */
968 /* TALOS-CAN-0062: block directory traversal for VMS, too */
969 if (NULL != strpbrk(filename, illegal_in_filename)) {
970 snprintf(reply, sizeof(reply),
971 "saveconfig does not allow directory in filename");
972 ctl_putdata(reply, strlen(reply), 0);
975 "saveconfig with path from %s rejected",
976 stoa(&rbufp->recv_srcadr));
980 snprintf(fullpath, sizeof(fullpath), "%s%s",
981 saveconfigdir, filename);
983 fd = open(fullpath, O_CREAT | O_TRUNC | O_WRONLY,
988 fptr = fdopen(fd, "w");
990 if (NULL == fptr || -1 == dump_all_config_trees(fptr, 1)) {
991 snprintf(reply, sizeof(reply),
992 "Unable to save configuration to file %s",
995 "saveconfig %s from %s failed", filename,
996 stoa(&rbufp->recv_srcadr));
998 snprintf(reply, sizeof(reply),
999 "Configuration saved to %s", filename);
1001 "Configuration saved to %s (requested by %s)",
1002 fullpath, stoa(&rbufp->recv_srcadr));
1004 * save the output filename in system variable
1005 * savedconfig, retrieved with:
1006 * ntpq -c "rv 0 savedconfig"
1008 snprintf(savedconfig, sizeof(savedconfig), "%s%s",
1009 savedconfig_eq, filename);
1010 set_sys_var(savedconfig, strlen(savedconfig) + 1, RO);
1015 #else /* !SAVECONFIG follows */
1016 snprintf(reply, sizeof(reply),
1017 "saveconfig unavailable, configured with --disable-saveconfig");
1020 ctl_putdata(reply, strlen(reply), 0);
1026 * process_control - process an incoming control message
1030 struct recvbuf *rbufp,
1034 struct ntp_control *pkt;
1037 const struct ctl_proc *cc;
1042 DPRINTF(3, ("in process_control()\n"));
1045 * Save the addresses for error responses
1048 rmt_addr = &rbufp->recv_srcadr;
1049 lcl_inter = rbufp->dstadr;
1050 pkt = (struct ntp_control *)&rbufp->recv_pkt;
1053 * If the length is less than required for the header, or
1054 * it is a response or a fragment, ignore this.
1056 if (rbufp->recv_length < (int)CTL_HEADER_LEN
1057 || (CTL_RESPONSE | CTL_MORE | CTL_ERROR) & pkt->r_m_e_op
1058 || pkt->offset != 0) {
1059 DPRINTF(1, ("invalid format in control packet\n"));
1060 if (rbufp->recv_length < (int)CTL_HEADER_LEN)
1062 if (CTL_RESPONSE & pkt->r_m_e_op)
1064 if (CTL_MORE & pkt->r_m_e_op)
1066 if (CTL_ERROR & pkt->r_m_e_op)
1068 if (pkt->offset != 0)
1072 res_version = PKT_VERSION(pkt->li_vn_mode);
1073 if (res_version > NTP_VERSION || res_version < NTP_OLDVERSION) {
1074 DPRINTF(1, ("unknown version %d in control packet\n",
1081 * Pull enough data from the packet to make intelligent
1084 rpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, res_version,
1086 res_opcode = pkt->r_m_e_op;
1087 rpkt.sequence = pkt->sequence;
1088 rpkt.associd = pkt->associd;
1092 res_associd = htons(pkt->associd);
1094 res_authenticate = FALSE;
1096 res_authokay = FALSE;
1097 req_count = (int)ntohs(pkt->count);
1098 datanotbinflag = FALSE;
1101 datapt = rpkt.u.data;
1102 dataend = &rpkt.u.data[CTL_MAX_DATA_LEN];
1104 if ((rbufp->recv_length & 0x3) != 0)
1105 DPRINTF(3, ("Control packet length %d unrounded\n",
1106 rbufp->recv_length));
1109 * We're set up now. Make sure we've got at least enough
1110 * incoming data space to match the count.
1112 req_data = rbufp->recv_length - CTL_HEADER_LEN;
1113 if (req_data < req_count || rbufp->recv_length & 0x3) {
1114 ctl_error(CERR_BADFMT);
1115 numctldatatooshort++;
1119 properlen = req_count + CTL_HEADER_LEN;
1120 /* round up proper len to a 8 octet boundary */
1122 properlen = (properlen + 7) & ~7;
1123 maclen = rbufp->recv_length - properlen;
1124 if ((rbufp->recv_length & 3) == 0 &&
1125 maclen >= MIN_MAC_LEN && maclen <= MAX_MAC_LEN &&
1127 res_authenticate = TRUE;
1128 pkid = (void *)((char *)pkt + properlen);
1129 res_keyid = ntohl(*pkid);
1130 DPRINTF(3, ("recv_len %d, properlen %d, wants auth with keyid %08x, MAC length=%zu\n",
1131 rbufp->recv_length, properlen, res_keyid,
1134 if (!authistrusted(res_keyid))
1135 DPRINTF(3, ("invalid keyid %08x\n", res_keyid));
1136 else if (authdecrypt(res_keyid, (u_int32 *)pkt,
1137 rbufp->recv_length - maclen,
1139 res_authokay = TRUE;
1140 DPRINTF(3, ("authenticated okay\n"));
1143 DPRINTF(3, ("authentication failed\n"));
1148 * Set up translate pointers
1150 reqpt = (char *)pkt->u.data;
1151 reqend = reqpt + req_count;
1154 * Look for the opcode processor
1156 for (cc = control_codes; cc->control_code != NO_REQUEST; cc++) {
1157 if (cc->control_code == res_opcode) {
1158 DPRINTF(3, ("opcode %d, found command handler\n",
1160 if (cc->flags == AUTH
1162 || res_keyid != ctl_auth_keyid)) {
1163 ctl_error(CERR_PERMISSION);
1166 (cc->handler)(rbufp, restrict_mask);
1172 * Can't find this one, return an error.
1175 ctl_error(CERR_BADOP);
1181 * ctlpeerstatus - return a status word for this peer
1185 register struct peer *p
1191 if (FLAG_CONFIG & p->flags)
1192 status |= CTL_PST_CONFIG;
1194 status |= CTL_PST_AUTHENABLE;
1195 if (FLAG_AUTHENTIC & p->flags)
1196 status |= CTL_PST_AUTHENTIC;
1198 status |= CTL_PST_REACH;
1199 if (MDF_TXONLY_MASK & p->cast_flags)
1200 status |= CTL_PST_BCAST;
1202 return CTL_PEER_STATUS(status, p->num_events, p->last_event);
1207 * ctlclkstatus - return a status word for this clock
1212 struct refclockstat *pcs
1215 return CTL_PEER_STATUS(0, pcs->lastevent, pcs->currentstatus);
1221 * ctlsysstatus - return the system status word
1226 register u_char this_clock;
1228 this_clock = CTL_SST_TS_UNSPEC;
1230 if (sys_peer != NULL) {
1231 if (CTL_SST_TS_UNSPEC != sys_peer->sstclktype)
1232 this_clock = sys_peer->sstclktype;
1233 else if (sys_peer->refclktype < COUNTOF(clocktypes))
1234 this_clock = clocktypes[sys_peer->refclktype];
1236 #else /* REFCLOCK */
1238 this_clock = CTL_SST_TS_NTP;
1239 #endif /* REFCLOCK */
1240 return CTL_SYS_STATUS(sys_leap, this_clock, ctl_sys_num_events,
1241 ctl_sys_last_event);
1246 * ctl_flushpkt - write out the current packet and prepare
1247 * another if necessary.
1261 dlen = datapt - rpkt.u.data;
1262 if (!more && datanotbinflag && dlen + 2 < CTL_MAX_DATA_LEN) {
1264 * Big hack, output a trailing \r\n
1270 sendlen = dlen + CTL_HEADER_LEN;
1273 * Pad to a multiple of 32 bits
1275 while (sendlen & 0x3) {
1281 * Fill in the packet with the current info
1283 rpkt.r_m_e_op = CTL_RESPONSE | more |
1284 (res_opcode & CTL_OP_MASK);
1285 rpkt.count = htons((u_short)dlen);
1286 rpkt.offset = htons((u_short)res_offset);
1288 for (i = 0; i < COUNTOF(ctl_traps); i++) {
1289 if (TRAP_INUSE & ctl_traps[i].tr_flags) {
1293 ctl_traps[i].tr_version,
1296 htons(ctl_traps[i].tr_sequence);
1297 sendpkt(&ctl_traps[i].tr_addr,
1298 ctl_traps[i].tr_localaddr, -4,
1299 (struct pkt *)&rpkt, sendlen);
1301 ctl_traps[i].tr_sequence++;
1306 if (res_authenticate && sys_authenticate) {
1309 * If we are going to authenticate, then there
1310 * is an additional requirement that the MAC
1311 * begin on a 64 bit boundary.
1313 while (totlen & 7) {
1317 keyid = htonl(res_keyid);
1318 memcpy(datapt, &keyid, sizeof(keyid));
1319 maclen = authencrypt(res_keyid,
1320 (u_int32 *)&rpkt, totlen);
1321 sendpkt(rmt_addr, lcl_inter, -5,
1322 (struct pkt *)&rpkt, totlen + maclen);
1324 sendpkt(rmt_addr, lcl_inter, -6,
1325 (struct pkt *)&rpkt, sendlen);
1334 * Set us up for another go around.
1338 datapt = rpkt.u.data;
1343 * ctl_putdata - write data into the packet, fragmenting and starting
1344 * another if this one is full.
1350 int bin /* set to 1 when data is binary */
1354 unsigned int currentlen;
1358 datanotbinflag = TRUE;
1363 if ((dlen + datalinelen + 1) >= MAXDATALINELEN) {
1375 * Save room for trailing junk
1377 while (dlen + overhead + datapt > dataend) {
1379 * Not enough room in this one, flush it out.
1381 currentlen = MIN(dlen, (unsigned int)(dataend - datapt));
1383 memcpy(datapt, dp, currentlen);
1385 datapt += currentlen;
1388 datalinelen += currentlen;
1390 ctl_flushpkt(CTL_MORE);
1393 memcpy(datapt, dp, dlen);
1395 datalinelen += dlen;
1401 * ctl_putstr - write a tagged string into the response packet
1406 * len is the data length excluding the NUL terminator,
1407 * as in ctl_putstr("var", "value", strlen("value"));
1421 memcpy(buffer, tag, tl);
1424 INSIST(tl + 3 + len <= sizeof(buffer));
1427 memcpy(cp, data, len);
1431 ctl_putdata(buffer, (u_int)(cp - buffer), 0);
1436 * ctl_putunqstr - write a tagged string into the response packet
1441 * len is the data length excluding the NUL terminator.
1442 * data must not contain a comma or whitespace.
1456 memcpy(buffer, tag, tl);
1459 INSIST(tl + 1 + len <= sizeof(buffer));
1461 memcpy(cp, data, len);
1464 ctl_putdata(buffer, (u_int)(cp - buffer), 0);
1469 * ctl_putdblf - write a tagged, signed double into the response packet
1488 INSIST((size_t)(cp - buffer) < sizeof(buffer));
1489 snprintf(cp, sizeof(buffer) - (cp - buffer), use_f ? "%.*f" : "%.*g",
1492 ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
1496 * ctl_putuint - write a tagged unsigned integer into the response
1505 register const char *cq;
1514 INSIST((cp - buffer) < (int)sizeof(buffer));
1515 snprintf(cp, sizeof(buffer) - (cp - buffer), "%lu", uval);
1517 ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
1521 * ctl_putcal - write a decoded calendar data into the response
1526 const struct calendar *pcal
1532 numch = snprintf(buffer, sizeof(buffer),
1533 "%s=%04d%02d%02d%02d%02d",
1541 INSIST(numch < sizeof(buffer));
1542 ctl_putdata(buffer, numch, 0);
1548 * ctl_putfs - write a decoded filestamp into the response
1557 register const char *cq;
1559 struct tm *tm = NULL;
1568 fstamp = uval - JAN_1970;
1569 tm = gmtime(&fstamp);
1572 INSIST((cp - buffer) < (int)sizeof(buffer));
1573 snprintf(cp, sizeof(buffer) - (cp - buffer),
1574 "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
1575 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);
1577 ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
1582 * ctl_puthex - write a tagged unsigned integer, in hex, into the
1592 register const char *cq;
1601 INSIST((cp - buffer) < (int)sizeof(buffer));
1602 snprintf(cp, sizeof(buffer) - (cp - buffer), "0x%lx", uval);
1604 ctl_putdata(buffer,(unsigned)( cp - buffer ), 0);
1609 * ctl_putint - write a tagged signed integer into the response
1618 register const char *cq;
1627 INSIST((cp - buffer) < (int)sizeof(buffer));
1628 snprintf(cp, sizeof(buffer) - (cp - buffer), "%ld", ival);
1630 ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
1635 * ctl_putts - write a tagged timestamp, in hex, into the response
1644 register const char *cq;
1653 INSIST((size_t)(cp - buffer) < sizeof(buffer));
1654 snprintf(cp, sizeof(buffer) - (cp - buffer), "0x%08x.%08x",
1655 (u_int)ts->l_ui, (u_int)ts->l_uf);
1657 ctl_putdata(buffer, (unsigned)( cp - buffer ), 0);
1662 * ctl_putadr - write an IP address into the response
1672 register const char *cq;
1682 cq = numtoa(addr32);
1685 INSIST((cp - buffer) < (int)sizeof(buffer));
1686 snprintf(cp, sizeof(buffer) - (cp - buffer), "%s", cq);
1688 ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
1693 * ctl_putrefid - send a u_int32 refid as printable text
1709 oplim = output + sizeof(output);
1710 while (optr < oplim && '\0' != *tag)
1716 if (!(optr < oplim))
1718 iptr = (char *)&refid;
1719 iplim = iptr + sizeof(refid);
1720 for ( ; optr < oplim && iptr < iplim && '\0' != *iptr;
1722 if (isprint((int)*iptr))
1726 if (!(optr <= oplim))
1728 ctl_putdata(output, (u_int)(optr - output), FALSE);
1733 * ctl_putarray - write a tagged eight element double array into the response
1743 register const char *cq;
1756 INSIST((cp - buffer) < (int)sizeof(buffer));
1757 snprintf(cp, sizeof(buffer) - (cp - buffer),
1758 " %.2f", arr[i] * 1e3);
1760 } while (i != start);
1761 ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
1766 * ctl_putsys - output a system variable
1780 struct cert_info *cp;
1781 #endif /* AUTOKEY */
1783 static struct timex ntx;
1784 static u_long ntp_adjtime_time;
1786 static const double to_ms =
1788 1.0e-6; /* nsec to msec */
1790 1.0e-3; /* usec to msec */
1794 * CS_K_* variables depend on up-to-date output of ntp_adjtime()
1796 if (CS_KERN_FIRST <= varid && varid <= CS_KERN_LAST &&
1797 current_time != ntp_adjtime_time) {
1799 if (ntp_adjtime(&ntx) < 0)
1800 msyslog(LOG_ERR, "ntp_adjtime() for mode 6 query failed: %m");
1802 ntp_adjtime_time = current_time;
1804 #endif /* KERNEL_PLL */
1809 ctl_putuint(sys_var[CS_LEAP].text, sys_leap);
1813 ctl_putuint(sys_var[CS_STRATUM].text, sys_stratum);
1817 ctl_putint(sys_var[CS_PRECISION].text, sys_precision);
1821 ctl_putdbl(sys_var[CS_ROOTDELAY].text, sys_rootdelay *
1825 case CS_ROOTDISPERSION:
1826 ctl_putdbl(sys_var[CS_ROOTDISPERSION].text,
1827 sys_rootdisp * 1e3);
1831 if (sys_stratum > 1 && sys_stratum < STRATUM_UNSPEC)
1832 ctl_putadr(sys_var[varid].text, sys_refid, NULL);
1834 ctl_putrefid(sys_var[varid].text, sys_refid);
1838 ctl_putts(sys_var[CS_REFTIME].text, &sys_reftime);
1842 ctl_putuint(sys_var[CS_POLL].text, sys_poll);
1846 if (sys_peer == NULL)
1847 ctl_putuint(sys_var[CS_PEERID].text, 0);
1849 ctl_putuint(sys_var[CS_PEERID].text,
1854 if (sys_peer != NULL && sys_peer->dstadr != NULL)
1855 ss = sptoa(&sys_peer->srcadr);
1858 ctl_putunqstr(sys_var[CS_PEERADR].text, ss, strlen(ss));
1862 u = (sys_peer != NULL)
1865 ctl_putuint(sys_var[CS_PEERMODE].text, u);
1869 ctl_putdbl6(sys_var[CS_OFFSET].text, last_offset * 1e3);
1873 ctl_putdbl(sys_var[CS_DRIFT].text, drift_comp * 1e6);
1877 ctl_putdbl6(sys_var[CS_JITTER].text, sys_jitter * 1e3);
1881 ctl_putdbl(sys_var[CS_ERROR].text, clock_jitter * 1e3);
1886 ctl_putts(sys_var[CS_CLOCK].text, &tmp);
1891 ctl_putstr(sys_var[CS_PROCESSOR].text, str_processor,
1892 sizeof(str_processor) - 1);
1894 ctl_putstr(sys_var[CS_PROCESSOR].text,
1895 utsnamebuf.machine, strlen(utsnamebuf.machine));
1896 #endif /* HAVE_UNAME */
1901 ctl_putstr(sys_var[CS_SYSTEM].text, str_system,
1902 sizeof(str_system) - 1);
1904 snprintf(str, sizeof(str), "%s/%s", utsnamebuf.sysname,
1905 utsnamebuf.release);
1906 ctl_putstr(sys_var[CS_SYSTEM].text, str, strlen(str));
1907 #endif /* HAVE_UNAME */
1911 ctl_putstr(sys_var[CS_VERSION].text, Version,
1916 ctl_putdbl(sys_var[CS_STABIL].text, clock_stability *
1922 char buf[CTL_MAX_DATA_LEN];
1923 //buffPointer, firstElementPointer, buffEndPointer
1924 char *buffp, *buffend;
1928 const struct ctl_var *k;
1931 buffend = buf + sizeof(buf);
1932 if (buffp + strlen(sys_var[CS_VARLIST].text) + 4 > buffend)
1933 break; /* really long var name */
1935 snprintf(buffp, sizeof(buf), "%s=\"",sys_var[CS_VARLIST].text);
1936 buffp += strlen(buffp);
1937 firstVarName = TRUE;
1938 for (k = sys_var; !(k->flags & EOV); k++) {
1939 if (k->flags & PADDING)
1941 len = strlen(k->text);
1942 if (buffp + len + 1 >= buffend)
1947 firstVarName = FALSE;
1948 memcpy(buffp, k->text, len);
1952 for (k = ext_sys_var; k && !(k->flags & EOV); k++) {
1953 if (k->flags & PADDING)
1955 if (NULL == k->text)
1957 ss1 = strchr(k->text, '=');
1959 len = strlen(k->text);
1961 len = ss1 - k->text;
1962 if (buffp + len + 1 >= buffend)
1966 firstVarName = FALSE;
1968 memcpy(buffp, k->text,(unsigned)len);
1971 if (buffp + 2 >= buffend)
1977 ctl_putdata(buf, (unsigned)( buffp - buf ), 0);
1983 ctl_putuint(sys_var[CS_TAI].text, sys_tai);
1988 leap_signature_t lsig;
1989 leapsec_getsig(&lsig);
1991 ctl_putfs(sys_var[CS_LEAPTAB].text, lsig.ttime);
1997 leap_signature_t lsig;
1998 leapsec_getsig(&lsig);
2000 ctl_putfs(sys_var[CS_LEAPEND].text, lsig.etime);
2005 case CS_LEAPSMEARINTV:
2006 if (leap_smear_intv > 0)
2007 ctl_putuint(sys_var[CS_LEAPSMEARINTV].text, leap_smear_intv);
2010 case CS_LEAPSMEAROFFS:
2011 if (leap_smear_intv > 0)
2012 ctl_putdbl(sys_var[CS_LEAPSMEAROFFS].text,
2013 leap_smear.doffset * 1e3);
2015 #endif /* LEAP_SMEAR */
2018 ctl_putuint(sys_var[CS_RATE].text, ntp_minpoll);
2021 case CS_MRU_ENABLED:
2022 ctl_puthex(sys_var[varid].text, mon_enabled);
2026 ctl_putuint(sys_var[varid].text, mru_entries);
2030 kb = mru_entries * (sizeof(mon_entry) / 1024.);
2034 ctl_putuint(sys_var[varid].text, u);
2037 case CS_MRU_DEEPEST:
2038 ctl_putuint(sys_var[varid].text, mru_peakentries);
2041 case CS_MRU_MINDEPTH:
2042 ctl_putuint(sys_var[varid].text, mru_mindepth);
2046 ctl_putint(sys_var[varid].text, mru_maxage);
2049 case CS_MRU_MAXDEPTH:
2050 ctl_putuint(sys_var[varid].text, mru_maxdepth);
2054 kb = mru_maxdepth * (sizeof(mon_entry) / 1024.);
2058 ctl_putuint(sys_var[varid].text, u);
2062 ctl_putuint(sys_var[varid].text, current_time);
2066 ctl_putuint(sys_var[varid].text,
2067 current_time - sys_stattime);
2070 case CS_SS_RECEIVED:
2071 ctl_putuint(sys_var[varid].text, sys_received);
2075 ctl_putuint(sys_var[varid].text, sys_newversion);
2079 ctl_putuint(sys_var[varid].text, sys_oldversion);
2082 case CS_SS_BADFORMAT:
2083 ctl_putuint(sys_var[varid].text, sys_badlength);
2087 ctl_putuint(sys_var[varid].text, sys_badauth);
2090 case CS_SS_DECLINED:
2091 ctl_putuint(sys_var[varid].text, sys_declined);
2094 case CS_SS_RESTRICTED:
2095 ctl_putuint(sys_var[varid].text, sys_restricted);
2099 ctl_putuint(sys_var[varid].text, sys_limitrejected);
2103 ctl_putuint(sys_var[varid].text, sys_kodsent);
2106 case CS_SS_PROCESSED:
2107 ctl_putuint(sys_var[varid].text, sys_processed);
2111 ctl_putdbl(sys_var[varid].text, sys_bdelay * 1e3);
2115 LFPTOD(&sys_authdelay, dtemp);
2116 ctl_putdbl(sys_var[varid].text, dtemp * 1e3);
2120 ctl_putuint(sys_var[varid].text, authnumkeys);
2124 ctl_putuint(sys_var[varid].text, authnumfreekeys);
2127 case CS_AUTHKLOOKUPS:
2128 ctl_putuint(sys_var[varid].text, authkeylookups);
2131 case CS_AUTHKNOTFOUND:
2132 ctl_putuint(sys_var[varid].text, authkeynotfound);
2135 case CS_AUTHKUNCACHED:
2136 ctl_putuint(sys_var[varid].text, authkeyuncached);
2139 case CS_AUTHKEXPIRED:
2140 ctl_putuint(sys_var[varid].text, authkeyexpired);
2143 case CS_AUTHENCRYPTS:
2144 ctl_putuint(sys_var[varid].text, authencryptions);
2147 case CS_AUTHDECRYPTS:
2148 ctl_putuint(sys_var[varid].text, authdecryptions);
2152 ctl_putuint(sys_var[varid].text,
2153 current_time - auth_timereset);
2157 * CTL_IF_KERNLOOP() puts a zero if the kernel loop is
2158 * unavailable, otherwise calls putfunc with args.
2161 # define CTL_IF_KERNLOOP(putfunc, args) \
2162 ctl_putint(sys_var[varid].text, 0)
2164 # define CTL_IF_KERNLOOP(putfunc, args) \
2169 * CTL_IF_KERNPPS() puts a zero if either the kernel
2170 * loop is unavailable, or kernel hard PPS is not
2171 * active, otherwise calls putfunc with args.
2174 # define CTL_IF_KERNPPS(putfunc, args) \
2175 ctl_putint(sys_var[varid].text, 0)
2177 # define CTL_IF_KERNPPS(putfunc, args) \
2178 if (0 == ntx.shift) \
2179 ctl_putint(sys_var[varid].text, 0); \
2181 putfunc args /* no trailing ; */
2187 (sys_var[varid].text, 0, -1, to_ms * ntx.offset)
2194 (sys_var[varid].text, ntx.freq)
2201 (sys_var[varid].text, 0, 6,
2202 to_ms * ntx.maxerror)
2209 (sys_var[varid].text, 0, 6,
2210 to_ms * ntx.esterror)
2218 ss = k_st_flags(ntx.status);
2220 ctl_putstr(sys_var[varid].text, ss, strlen(ss));
2223 case CS_K_TIMECONST:
2226 (sys_var[varid].text, ntx.constant)
2230 case CS_K_PRECISION:
2233 (sys_var[varid].text, 0, 6,
2234 to_ms * ntx.precision)
2241 (sys_var[varid].text, ntx.tolerance)
2248 (sys_var[varid].text, ntx.ppsfreq)
2252 case CS_K_PPS_STABIL:
2255 (sys_var[varid].text, ntx.stabil)
2259 case CS_K_PPS_JITTER:
2262 (sys_var[varid].text, to_ms * ntx.jitter)
2266 case CS_K_PPS_CALIBDUR:
2269 (sys_var[varid].text, 1 << ntx.shift)
2273 case CS_K_PPS_CALIBS:
2276 (sys_var[varid].text, ntx.calcnt)
2280 case CS_K_PPS_CALIBERRS:
2283 (sys_var[varid].text, ntx.errcnt)
2287 case CS_K_PPS_JITEXC:
2290 (sys_var[varid].text, ntx.jitcnt)
2294 case CS_K_PPS_STBEXC:
2297 (sys_var[varid].text, ntx.stbcnt)
2301 case CS_IOSTATS_RESET:
2302 ctl_putuint(sys_var[varid].text,
2303 current_time - io_timereset);
2307 ctl_putuint(sys_var[varid].text, total_recvbuffs());
2311 ctl_putuint(sys_var[varid].text, free_recvbuffs());
2315 ctl_putuint(sys_var[varid].text, full_recvbuffs());
2318 case CS_RBUF_LOWATER:
2319 ctl_putuint(sys_var[varid].text, lowater_additions());
2323 ctl_putuint(sys_var[varid].text, packets_dropped);
2327 ctl_putuint(sys_var[varid].text, packets_ignored);
2330 case CS_IO_RECEIVED:
2331 ctl_putuint(sys_var[varid].text, packets_received);
2335 ctl_putuint(sys_var[varid].text, packets_sent);
2338 case CS_IO_SENDFAILED:
2339 ctl_putuint(sys_var[varid].text, packets_notsent);
2343 ctl_putuint(sys_var[varid].text, handler_calls);
2346 case CS_IO_GOODWAKEUPS:
2347 ctl_putuint(sys_var[varid].text, handler_pkts);
2350 case CS_TIMERSTATS_RESET:
2351 ctl_putuint(sys_var[varid].text,
2352 current_time - timer_timereset);
2355 case CS_TIMER_OVERRUNS:
2356 ctl_putuint(sys_var[varid].text, alarm_overflow);
2360 ctl_putuint(sys_var[varid].text, timer_xmtcalls);
2364 ctl_putdbl(sys_var[varid].text, sys_fuzz * 1e3);
2366 case CS_WANDER_THRESH:
2367 ctl_putdbl(sys_var[varid].text, wander_threshold * 1e6);
2372 ctl_puthex(sys_var[CS_FLAGS].text,
2378 strlcpy(str, OBJ_nid2ln(crypto_nid),
2380 ctl_putstr(sys_var[CS_DIGEST].text, str,
2389 dp = EVP_get_digestbynid(crypto_flags >> 16);
2390 strlcpy(str, OBJ_nid2ln(EVP_MD_pkey_type(dp)),
2392 ctl_putstr(sys_var[CS_SIGNATURE].text, str,
2398 if (hostval.ptr != NULL)
2399 ctl_putstr(sys_var[CS_HOST].text, hostval.ptr,
2400 strlen(hostval.ptr));
2404 if (sys_ident != NULL)
2405 ctl_putstr(sys_var[CS_IDENT].text, sys_ident,
2410 for (cp = cinfo; cp != NULL; cp = cp->link) {
2411 snprintf(str, sizeof(str), "%s %s 0x%x",
2412 cp->subject, cp->issuer, cp->flags);
2413 ctl_putstr(sys_var[CS_CERTIF].text, str,
2415 ctl_putcal(sys_var[CS_REVTIME].text, &(cp->last));
2420 if (hostval.tstamp != 0)
2421 ctl_putfs(sys_var[CS_PUBLIC].text,
2422 ntohl(hostval.tstamp));
2424 #endif /* AUTOKEY */
2433 * ctl_putpeer - output a peer variable
2441 char buf[CTL_MAX_DATA_LEN];
2446 const struct ctl_var *k;
2451 #endif /* AUTOKEY */
2456 ctl_putuint(peer_var[id].text,
2457 !(FLAG_PREEMPT & p->flags));
2461 ctl_putuint(peer_var[id].text, !(p->keyid));
2465 ctl_putuint(peer_var[id].text,
2466 !!(FLAG_AUTHENTIC & p->flags));
2470 ctl_putadr(peer_var[id].text, 0, &p->srcadr);
2474 ctl_putuint(peer_var[id].text, SRCPORT(&p->srcadr));
2478 if (p->hostname != NULL)
2479 ctl_putstr(peer_var[id].text, p->hostname,
2480 strlen(p->hostname));
2484 ctl_putadr(peer_var[id].text, 0,
2491 ctl_putuint(peer_var[id].text,
2493 ? SRCPORT(&p->dstadr->sin)
2499 ctl_putdbl(peer_var[id].text, p->r21 / 1e3);
2504 ctl_putdbl(peer_var[id].text, p->r34 / 1e3);
2508 ctl_putuint(peer_var[id].text, p->throttle);
2512 ctl_putuint(peer_var[id].text, p->leap);
2516 ctl_putuint(peer_var[id].text, p->hmode);
2520 ctl_putuint(peer_var[id].text, p->stratum);
2524 ctl_putuint(peer_var[id].text, p->ppoll);
2528 ctl_putuint(peer_var[id].text, p->hpoll);
2532 ctl_putint(peer_var[id].text, p->precision);
2536 ctl_putdbl(peer_var[id].text, p->rootdelay * 1e3);
2539 case CP_ROOTDISPERSION:
2540 ctl_putdbl(peer_var[id].text, p->rootdisp * 1e3);
2545 if (p->flags & FLAG_REFCLOCK) {
2546 ctl_putrefid(peer_var[id].text, p->refid);
2550 if (p->stratum > 1 && p->stratum < STRATUM_UNSPEC)
2551 ctl_putadr(peer_var[id].text, p->refid,
2554 ctl_putrefid(peer_var[id].text, p->refid);
2558 ctl_putts(peer_var[id].text, &p->reftime);
2562 ctl_putts(peer_var[id].text, &p->aorg);
2566 ctl_putts(peer_var[id].text, &p->dst);
2571 ctl_putdbl(peer_var[id].text, p->xleave * 1e3);
2576 ctl_putdbl(peer_var[id].text, p->bias * 1e3);
2580 ctl_puthex(peer_var[id].text, p->reach);
2584 ctl_puthex(peer_var[id].text, p->flash);
2589 if (p->flags & FLAG_REFCLOCK) {
2590 ctl_putuint(peer_var[id].text, p->ttl);
2594 if (p->ttl > 0 && p->ttl < COUNTOF(sys_ttl))
2595 ctl_putint(peer_var[id].text,
2600 ctl_putuint(peer_var[id].text, p->unreach);
2604 ctl_putuint(peer_var[id].text,
2605 p->nextdate - current_time);
2609 ctl_putdbl(peer_var[id].text, p->delay * 1e3);
2613 ctl_putdbl(peer_var[id].text, p->offset * 1e3);
2617 ctl_putdbl(peer_var[id].text, p->jitter * 1e3);
2621 ctl_putdbl(peer_var[id].text, p->disp * 1e3);
2625 if (p->keyid > NTP_MAXKEY)
2626 ctl_puthex(peer_var[id].text, p->keyid);
2628 ctl_putuint(peer_var[id].text, p->keyid);
2632 ctl_putarray(peer_var[id].text, p->filter_delay,
2637 ctl_putarray(peer_var[id].text, p->filter_offset,
2642 ctl_putarray(peer_var[id].text, p->filter_disp,
2647 ctl_putuint(peer_var[id].text, p->pmode);
2651 ctl_putuint(peer_var[id].text, p->received);
2655 ctl_putuint(peer_var[id].text, p->sent);
2660 be = buf + sizeof(buf);
2661 if (strlen(peer_var[id].text) + 4 > sizeof(buf))
2662 break; /* really long var name */
2664 snprintf(s, sizeof(buf), "%s=\"", peer_var[id].text);
2667 for (k = peer_var; !(EOV & k->flags); k++) {
2668 if (PADDING & k->flags)
2670 i = strlen(k->text);
2671 if (s + i + 1 >= be)
2675 memcpy(s, k->text, i);
2681 ctl_putdata(buf, (u_int)(s - buf), 0);
2686 ctl_putuint(peer_var[id].text,
2687 current_time - p->timereceived);
2691 ctl_putuint(peer_var[id].text,
2692 current_time - p->timereachable);
2696 ctl_putuint(peer_var[id].text, p->badauth);
2700 ctl_putuint(peer_var[id].text, p->bogusorg);
2704 ctl_putuint(peer_var[id].text, p->oldpkt);
2708 ctl_putuint(peer_var[id].text, p->seldisptoolarge);
2712 ctl_putuint(peer_var[id].text, p->selbroken);
2716 ctl_putuint(peer_var[id].text, p->status);
2721 ctl_puthex(peer_var[id].text, p->crypto);
2726 dp = EVP_get_digestbynid(p->crypto >> 16);
2727 str = OBJ_nid2ln(EVP_MD_pkey_type(dp));
2728 ctl_putstr(peer_var[id].text, str, strlen(str));
2733 if (p->subject != NULL)
2734 ctl_putstr(peer_var[id].text, p->subject,
2735 strlen(p->subject));
2738 case CP_VALID: /* not used */
2742 if (NULL == (ap = p->recval.ptr))
2745 ctl_putint(peer_var[CP_INITSEQ].text, ap->seq);
2746 ctl_puthex(peer_var[CP_INITKEY].text, ap->key);
2747 ctl_putfs(peer_var[CP_INITTSP].text,
2748 ntohl(p->recval.tstamp));
2752 if (p->ident != NULL)
2753 ctl_putstr(peer_var[id].text, p->ident,
2758 #endif /* AUTOKEY */
2765 * ctl_putclock - output clock variables
2770 struct refclockstat *pcs,
2774 char buf[CTL_MAX_DATA_LEN];
2778 const struct ctl_var *k;
2783 if (mustput || pcs->clockdesc == NULL
2784 || *(pcs->clockdesc) == '\0') {
2785 ctl_putuint(clock_var[id].text, pcs->type);
2789 ctl_putstr(clock_var[id].text,
2791 (unsigned)pcs->lencode);
2795 ctl_putuint(clock_var[id].text, pcs->polls);
2799 ctl_putuint(clock_var[id].text,
2804 ctl_putuint(clock_var[id].text,
2809 ctl_putuint(clock_var[id].text,
2814 if (mustput || (pcs->haveflags & CLK_HAVETIME1))
2815 ctl_putdbl(clock_var[id].text,
2816 pcs->fudgetime1 * 1e3);
2820 if (mustput || (pcs->haveflags & CLK_HAVETIME2))
2821 ctl_putdbl(clock_var[id].text,
2822 pcs->fudgetime2 * 1e3);
2826 if (mustput || (pcs->haveflags & CLK_HAVEVAL1))
2827 ctl_putint(clock_var[id].text,
2832 if (mustput || (pcs->haveflags & CLK_HAVEVAL2)) {
2833 if (pcs->fudgeval1 > 1)
2834 ctl_putadr(clock_var[id].text,
2835 pcs->fudgeval2, NULL);
2837 ctl_putrefid(clock_var[id].text,
2843 ctl_putuint(clock_var[id].text, pcs->flags);
2847 if (pcs->clockdesc == NULL ||
2848 *(pcs->clockdesc) == '\0') {
2850 ctl_putstr(clock_var[id].text,
2853 ctl_putstr(clock_var[id].text,
2855 strlen(pcs->clockdesc));
2861 be = buf + sizeof(buf);
2862 if (strlen(clock_var[CC_VARLIST].text) + 4 >
2864 break; /* really long var name */
2866 snprintf(s, sizeof(buf), "%s=\"",
2867 clock_var[CC_VARLIST].text);
2871 for (k = clock_var; !(EOV & k->flags); k++) {
2872 if (PADDING & k->flags)
2875 i = strlen(k->text);
2876 if (s + i + 1 >= be)
2881 memcpy(s, k->text, i);
2885 for (k = pcs->kv_list; k && !(EOV & k->flags); k++) {
2886 if (PADDING & k->flags)
2893 while (*ss && *ss != '=')
2896 if (s + i + 1 >= be)
2901 memcpy(s, k->text, (unsigned)i);
2910 ctl_putdata(buf, (unsigned)(s - buf), 0);
2919 * ctl_getitem - get the next data item from the incoming packet
2921 static const struct ctl_var *
2923 const struct ctl_var *var_list,
2927 static const struct ctl_var eol = { 0, EOV, NULL };
2928 static char buf[128];
2929 static u_long quiet_until;
2930 const struct ctl_var *v;
2936 * Delete leading commas and white space
2938 while (reqpt < reqend && (*reqpt == ',' ||
2939 isspace((unsigned char)*reqpt)))
2941 if (reqpt >= reqend)
2944 if (NULL == var_list)
2948 * Look for a first character match on the tag. If we find
2949 * one, see if it is a full match.
2952 for (v = var_list; !(EOV & v->flags); v++) {
2953 if (!(PADDING & v->flags) && *cp == *(v->text)) {
2955 while ('\0' != *pch && '=' != *pch && cp < reqend
2960 if ('\0' == *pch || '=' == *pch) {
2961 while (cp < reqend && isspace((u_char)*cp))
2963 if (cp == reqend || ',' == *cp) {
2974 while (cp < reqend && isspace((u_char)*cp))
2976 while (cp < reqend && *cp != ',') {
2978 if ((size_t)(tp - buf) >= sizeof(buf)) {
2979 ctl_error(CERR_BADFMT);
2982 if (quiet_until <= current_time) {
2983 quiet_until = current_time + 300;
2984 msyslog(LOG_WARNING,
2985 "Possible 'ntpdx' exploit from %s#%u (possibly spoofed)", stoa(rmt_addr), SRCPORT(rmt_addr));
2993 while (tp >= buf && isspace((u_char)*tp))
3008 * control_unspec - response to an unspecified op-code
3013 struct recvbuf *rbufp,
3020 * What is an appropriate response to an unspecified op-code?
3021 * I return no errors and no data, unless a specified assocation
3025 peer = findpeerbyassoc(res_associd);
3027 ctl_error(CERR_BADASSOC);
3030 rpkt.status = htons(ctlpeerstatus(peer));
3032 rpkt.status = htons(ctlsysstatus());
3038 * read_status - return either a list of associd's, or a particular
3044 struct recvbuf *rbufp,
3051 /* a_st holds association ID, status pairs alternating */
3052 u_short a_st[CTL_MAX_DATA_LEN / sizeof(u_short)];
3056 printf("read_status: ID %d\n", res_associd);
3059 * Two choices here. If the specified association ID is
3060 * zero we return all known assocation ID's. Otherwise
3061 * we return a bunch of stuff about the particular peer.
3064 peer = findpeerbyassoc(res_associd);
3066 ctl_error(CERR_BADASSOC);
3069 rpkt.status = htons(ctlpeerstatus(peer));
3071 peer->num_events = 0;
3073 * For now, output everything we know about the
3074 * peer. May be more selective later.
3076 for (cp = def_peer_var; *cp != 0; cp++)
3077 ctl_putpeer((int)*cp, peer);
3082 rpkt.status = htons(ctlsysstatus());
3083 for (peer = peer_list; peer != NULL; peer = peer->p_link) {
3084 a_st[n++] = htons(peer->associd);
3085 a_st[n++] = htons(ctlpeerstatus(peer));
3086 /* two entries each loop iteration, so n + 1 */
3087 if (n + 1 >= COUNTOF(a_st)) {
3088 ctl_putdata((void *)a_st, n * sizeof(a_st[0]),
3094 ctl_putdata((void *)a_st, n * sizeof(a_st[0]), 1);
3100 * read_peervars - half of read_variables() implementation
3105 const struct ctl_var *v;
3110 u_char wants[CP_MAXCODE + 1];
3114 * Wants info for a particular peer. See if we know
3117 peer = findpeerbyassoc(res_associd);
3119 ctl_error(CERR_BADASSOC);
3122 rpkt.status = htons(ctlpeerstatus(peer));
3124 peer->num_events = 0;
3127 while (NULL != (v = ctl_getitem(peer_var, &valuep))) {
3128 if (v->flags & EOV) {
3129 ctl_error(CERR_UNKNOWNVAR);
3132 INSIST(v->code < COUNTOF(wants));
3137 for (i = 1; i < COUNTOF(wants); i++)
3139 ctl_putpeer(i, peer);
3141 for (cp = def_peer_var; *cp != 0; cp++)
3142 ctl_putpeer((int)*cp, peer);
3148 * read_sysvars - half of read_variables() implementation
3153 const struct ctl_var *v;
3164 * Wants system variables. Figure out which he wants
3165 * and give them to him.
3167 rpkt.status = htons(ctlsysstatus());
3169 ctl_sys_num_events = 0;
3170 wants_count = CS_MAXCODE + 1 + count_var(ext_sys_var);
3171 wants = emalloc_zero(wants_count);
3173 while (NULL != (v = ctl_getitem(sys_var, &valuep))) {
3174 if (!(EOV & v->flags)) {
3175 INSIST(v->code < wants_count);
3179 v = ctl_getitem(ext_sys_var, &valuep);
3181 if (EOV & v->flags) {
3182 ctl_error(CERR_UNKNOWNVAR);
3186 n = v->code + CS_MAXCODE + 1;
3187 INSIST(n < wants_count);
3193 for (n = 1; n <= CS_MAXCODE; n++)
3196 for (n = 0; n + CS_MAXCODE + 1 < wants_count; n++)
3197 if (wants[n + CS_MAXCODE + 1]) {
3198 pch = ext_sys_var[n].text;
3199 ctl_putdata(pch, strlen(pch), 0);
3202 for (cs = def_sys_var; *cs != 0; cs++)
3203 ctl_putsys((int)*cs);
3204 for (kv = ext_sys_var; kv && !(EOV & kv->flags); kv++)
3205 if (DEF & kv->flags)
3206 ctl_putdata(kv->text, strlen(kv->text),
3215 * read_variables - return the variables the caller asks for
3220 struct recvbuf *rbufp,
3232 * write_variables - write into variables. We only allow leap bit
3238 struct recvbuf *rbufp,
3242 const struct ctl_var *v;
3253 * If he's trying to write into a peer tell him no way
3255 if (res_associd != 0) {
3256 ctl_error(CERR_PERMISSION);
3263 rpkt.status = htons(ctlsysstatus());
3266 * Look through the variables. Dump out at the first sign of
3269 while ((v = ctl_getitem(sys_var, &valuep)) != 0) {
3271 if (v->flags & EOV) {
3272 if ((v = ctl_getitem(ext_sys_var, &valuep)) !=
3274 if (v->flags & EOV) {
3275 ctl_error(CERR_UNKNOWNVAR);
3283 if (!(v->flags & CAN_WRITE)) {
3284 ctl_error(CERR_PERMISSION);
3287 if (!ext_var && (*valuep == '\0' || !atoint(valuep,
3289 ctl_error(CERR_BADFMT);
3292 if (!ext_var && (val & ~LEAP_NOTINSYNC) != 0) {
3293 ctl_error(CERR_BADVALUE);
3298 octets = strlen(v->text) + strlen(valuep) + 2;
3299 vareqv = emalloc(octets);
3302 while (*t && *t != '=')
3305 memcpy(tt, valuep, 1 + strlen(valuep));
3306 set_sys_var(vareqv, 1 + strlen(vareqv), v->flags);
3309 ctl_error(CERR_UNSPEC); /* really */
3315 * If we got anything, do it. xxx nothing to do ***
3318 if (leapind != ~0 || leapwarn != ~0) {
3319 if (!leap_setleap((int)leapind, (int)leapwarn)) {
3320 ctl_error(CERR_PERMISSION);
3330 * configure() processes ntpq :config/config-from-file, allowing
3331 * generic runtime reconfiguration.
3333 static void configure(
3334 struct recvbuf *rbufp,
3341 /* I haven't yet implemented changes to an existing association.
3342 * Hence check if the association id is 0
3344 if (res_associd != 0) {
3345 ctl_error(CERR_BADVALUE);
3349 if (RES_NOMODIFY & restrict_mask) {
3350 snprintf(remote_config.err_msg,
3351 sizeof(remote_config.err_msg),
3352 "runtime configuration prohibited by restrict ... nomodify");
3353 ctl_putdata(remote_config.err_msg,
3354 strlen(remote_config.err_msg), 0);
3358 "runtime config from %s rejected due to nomodify restriction",
3359 stoa(&rbufp->recv_srcadr));
3364 /* Initialize the remote config buffer */
3365 data_count = remoteconfig_cmdlength(reqpt, reqend);
3367 if (data_count > sizeof(remote_config.buffer) - 2) {
3368 snprintf(remote_config.err_msg,
3369 sizeof(remote_config.err_msg),
3370 "runtime configuration failed: request too long");
3371 ctl_putdata(remote_config.err_msg,
3372 strlen(remote_config.err_msg), 0);
3375 "runtime config from %s rejected: request too long",
3376 stoa(&rbufp->recv_srcadr));
3379 /* Bug 2853 -- check if all characters were acceptable */
3380 if (data_count != (size_t)(reqend - reqpt)) {
3381 snprintf(remote_config.err_msg,
3382 sizeof(remote_config.err_msg),
3383 "runtime configuration failed: request contains an unprintable character");
3384 ctl_putdata(remote_config.err_msg,
3385 strlen(remote_config.err_msg), 0);
3388 "runtime config from %s rejected: request contains an unprintable character: %0x",
3389 stoa(&rbufp->recv_srcadr),
3394 memcpy(remote_config.buffer, reqpt, data_count);
3395 /* The buffer has no trailing linefeed or NUL right now. For
3396 * logging, we do not want a newline, so we do that first after
3397 * adding the necessary NUL byte.
3399 remote_config.buffer[data_count] = '\0';
3400 DPRINTF(1, ("Got Remote Configuration Command: %s\n",
3401 remote_config.buffer));
3402 msyslog(LOG_NOTICE, "%s config: %s",
3403 stoa(&rbufp->recv_srcadr),
3404 remote_config.buffer);
3406 /* Now we have to make sure there is a NL/NUL sequence at the
3407 * end of the buffer before we parse it.
3409 remote_config.buffer[data_count++] = '\n';
3410 remote_config.buffer[data_count] = '\0';
3411 remote_config.pos = 0;
3412 remote_config.err_pos = 0;
3413 remote_config.no_errors = 0;
3414 config_remotely(&rbufp->recv_srcadr);
3417 * Check if errors were reported. If not, output 'Config
3418 * Succeeded'. Else output the error count. It would be nice
3419 * to output any parser error messages.
3421 if (0 == remote_config.no_errors) {
3422 retval = snprintf(remote_config.err_msg,
3423 sizeof(remote_config.err_msg),
3424 "Config Succeeded");
3426 remote_config.err_pos += retval;
3429 ctl_putdata(remote_config.err_msg, remote_config.err_pos, 0);
3432 DPRINTF(1, ("Reply: %s\n", remote_config.err_msg));
3434 if (remote_config.no_errors > 0)
3435 msyslog(LOG_NOTICE, "%d error in %s config",
3436 remote_config.no_errors,
3437 stoa(&rbufp->recv_srcadr));
3442 * derive_nonce - generate client-address-specific nonce value
3443 * associated with a given timestamp.
3445 static u_int32 derive_nonce(
3451 static u_int32 salt[4];
3452 static u_long last_salt_update;
3454 u_char digest[EVP_MAX_MD_SIZE];
3460 while (!salt[0] || current_time - last_salt_update >= 3600) {
3461 salt[0] = ntp_random();
3462 salt[1] = ntp_random();
3463 salt[2] = ntp_random();
3464 salt[3] = ntp_random();
3465 last_salt_update = current_time;
3468 EVP_DigestInit(&ctx, EVP_get_digestbynid(NID_md5));
3469 EVP_DigestUpdate(&ctx, salt, sizeof(salt));
3470 EVP_DigestUpdate(&ctx, &ts_i, sizeof(ts_i));
3471 EVP_DigestUpdate(&ctx, &ts_f, sizeof(ts_f));
3473 EVP_DigestUpdate(&ctx, &SOCK_ADDR4(addr),
3474 sizeof(SOCK_ADDR4(addr)));
3476 EVP_DigestUpdate(&ctx, &SOCK_ADDR6(addr),
3477 sizeof(SOCK_ADDR6(addr)));
3478 EVP_DigestUpdate(&ctx, &NSRCPORT(addr), sizeof(NSRCPORT(addr)));
3479 EVP_DigestUpdate(&ctx, salt, sizeof(salt));
3480 EVP_DigestFinal(&ctx, d.digest, &len);
3487 * generate_nonce - generate client-address-specific nonce string.
3489 static void generate_nonce(
3490 struct recvbuf * rbufp,
3497 derived = derive_nonce(&rbufp->recv_srcadr,
3498 rbufp->recv_time.l_ui,
3499 rbufp->recv_time.l_uf);
3500 snprintf(nonce, nonce_octets, "%08x%08x%08x",
3501 rbufp->recv_time.l_ui, rbufp->recv_time.l_uf, derived);
3506 * validate_nonce - validate client-address-specific nonce string.
3508 * Returns TRUE if the local calculation of the nonce matches the
3509 * client-provided value and the timestamp is recent enough.
3511 static int validate_nonce(
3512 const char * pnonce,
3513 struct recvbuf * rbufp
3523 if (3 != sscanf(pnonce, "%08x%08x%08x", &ts_i, &ts_f, &supposed))
3526 ts.l_ui = (u_int32)ts_i;
3527 ts.l_uf = (u_int32)ts_f;
3528 derived = derive_nonce(&rbufp->recv_srcadr, ts.l_ui, ts.l_uf);
3529 get_systime(&now_delta);
3530 L_SUB(&now_delta, &ts);
3532 return (supposed == derived && now_delta.l_ui < 16);
3537 * send_random_tag_value - send a randomly-generated three character
3538 * tag prefix, a '.', an index, a '=' and a
3539 * random integer value.
3541 * To try to force clients to ignore unrecognized tags in mrulist,
3542 * reslist, and ifstats responses, the first and last rows are spiced
3543 * with randomly-generated tag names with correct .# index. Make it
3544 * three characters knowing that none of the currently-used subscripted
3545 * tags have that length, avoiding the need to test for
3549 send_random_tag_value(
3556 noise = rand() ^ (rand() << 16);
3557 buf[0] = 'a' + noise % 26;
3559 buf[1] = 'a' + noise % 26;
3561 buf[2] = 'a' + noise % 26;
3564 snprintf(&buf[4], sizeof(buf) - 4, "%d", indx);
3565 ctl_putuint(buf, noise);
3570 * Send a MRU list entry in response to a "ntpq -c mrulist" operation.
3572 * To keep clients honest about not depending on the order of values,
3573 * and thereby avoid being locked into ugly workarounds to maintain
3574 * backward compatibility later as new fields are added to the response,
3575 * the order is random.
3583 const char first_fmt[] = "first.%d";
3584 const char ct_fmt[] = "ct.%d";
3585 const char mv_fmt[] = "mv.%d";
3586 const char rs_fmt[] = "rs.%d";
3588 u_char sent[6]; /* 6 tag=value pairs */
3594 remaining = COUNTOF(sent);
3596 noise = (u_int32)(rand() ^ (rand() << 16));
3597 while (remaining > 0) {
3598 which = (noise & 7) % COUNTOF(sent);
3601 which = (which + 1) % COUNTOF(sent);
3606 snprintf(tag, sizeof(tag), addr_fmt, count);
3607 pch = sptoa(&mon->rmtadr);
3608 ctl_putunqstr(tag, pch, strlen(pch));
3612 snprintf(tag, sizeof(tag), last_fmt, count);
3613 ctl_putts(tag, &mon->last);
3617 snprintf(tag, sizeof(tag), first_fmt, count);
3618 ctl_putts(tag, &mon->first);
3622 snprintf(tag, sizeof(tag), ct_fmt, count);
3623 ctl_putint(tag, mon->count);
3627 snprintf(tag, sizeof(tag), mv_fmt, count);
3628 ctl_putuint(tag, mon->vn_mode);
3632 snprintf(tag, sizeof(tag), rs_fmt, count);
3633 ctl_puthex(tag, mon->flags);
3643 * read_mru_list - supports ntpq's mrulist command.
3645 * The challenge here is to match ntpdc's monlist functionality without
3646 * being limited to hundreds of entries returned total, and without
3647 * requiring state on the server. If state were required, ntpq's
3648 * mrulist command would require authentication.
3650 * The approach was suggested by Ry Jones. A finite and variable number
3651 * of entries are retrieved per request, to avoid having responses with
3652 * such large numbers of packets that socket buffers are overflowed and
3653 * packets lost. The entries are retrieved oldest-first, taking into
3654 * account that the MRU list will be changing between each request. We
3655 * can expect to see duplicate entries for addresses updated in the MRU
3656 * list during the fetch operation. In the end, the client can assemble
3657 * a close approximation of the MRU list at the point in time the last
3658 * response was sent by ntpd. The only difference is it may be longer,
3659 * containing some number of oldest entries which have since been
3660 * reclaimed. If necessary, the protocol could be extended to zap those
3661 * from the client snapshot at the end, but so far that doesn't seem
3664 * To accomodate the changing MRU list, the starting point for requests
3665 * after the first request is supplied as a series of last seen
3666 * timestamps and associated addresses, the newest ones the client has
3667 * received. As long as at least one of those entries hasn't been
3668 * bumped to the head of the MRU list, ntpd can pick up at that point.
3669 * Otherwise, the request is failed and it is up to ntpq to back up and
3670 * provide the next newest entry's timestamps and addresses, conceivably
3671 * backing up all the way to the starting point.
3674 * nonce= Regurgitated nonce retrieved by the client
3675 * previously using CTL_OP_REQ_NONCE, demonstrating
3676 * ability to receive traffic sent to its address.
3677 * frags= Limit on datagrams (fragments) in response. Used
3678 * by newer ntpq versions instead of limit= when
3679 * retrieving multiple entries.
3680 * limit= Limit on MRU entries returned. One of frags= or
3681 * limit= must be provided.
3682 * limit=1 is a special case: Instead of fetching
3683 * beginning with the supplied starting point's
3684 * newer neighbor, fetch the supplied entry, and
3685 * in that case the #.last timestamp can be zero.
3686 * This enables fetching a single entry by IP
3687 * address. When limit is not one and frags= is
3688 * provided, the fragment limit controls.
3689 * mincount= (decimal) Return entries with count >= mincount.
3690 * laddr= Return entries associated with the server's IP
3691 * address given. No port specification is needed,
3692 * and any supplied is ignored.
3693 * resall= 0x-prefixed hex restrict bits which must all be
3694 * lit for an MRU entry to be included.
3695 * Has precedence over any resany=.
3696 * resany= 0x-prefixed hex restrict bits, at least one of
3697 * which must be list for an MRU entry to be
3699 * last.0= 0x-prefixed hex l_fp timestamp of newest entry
3700 * which client previously received.
3701 * addr.0= text of newest entry's IP address and port,
3702 * IPv6 addresses in bracketed form: [::]:123
3703 * last.1= timestamp of 2nd newest entry client has.
3704 * addr.1= address of 2nd newest entry.
3707 * ntpq provides as many last/addr pairs as will fit in a single request
3708 * packet, except for the first request in a MRU fetch operation.
3710 * The response begins with a new nonce value to be used for any
3711 * followup request. Following the nonce is the next newer entry than
3712 * referred to by last.0 and addr.0, if the "0" entry has not been
3713 * bumped to the front. If it has, the first entry returned will be the
3714 * next entry newer than referred to by last.1 and addr.1, and so on.
3715 * If none of the referenced entries remain unchanged, the request fails
3716 * and ntpq backs up to the next earlier set of entries to resync.
3718 * Except for the first response, the response begins with confirmation
3719 * of the entry that precedes the first additional entry provided:
3721 * last.older= hex l_fp timestamp matching one of the input
3722 * .last timestamps, which entry now precedes the
3723 * response 0. entry in the MRU list.
3724 * addr.older= text of address corresponding to older.last.
3726 * And in any case, a successful response contains sets of values
3727 * comprising entries, with the oldest numbered 0 and incrementing from
3730 * addr.# text of IPv4 or IPv6 address and port
3731 * last.# hex l_fp timestamp of last receipt
3732 * first.# hex l_fp timestamp of first receipt
3733 * ct.# count of packets received
3734 * mv.# mode and version
3735 * rs.# restriction mask (RES_* bits)
3737 * Note the code currently assumes there are no valid three letter
3738 * tags sent with each row, and needs to be adjusted if that changes.
3740 * The client should accept the values in any order, and ignore .#
3741 * values which it does not understand, to allow a smooth path to
3742 * future changes without requiring a new opcode. Clients can rely
3743 * on all *.0 values preceding any *.1 values, that is all values for
3744 * a given index number are together in the response.
3746 * The end of the response list is noted with one or two tag=value
3747 * pairs. Unconditionally:
3749 * now= 0x-prefixed l_fp timestamp at the server marking
3750 * the end of the operation.
3752 * If any entries were returned, now= is followed by:
3754 * last.newest= hex l_fp identical to last.# of the prior
3757 static void read_mru_list(
3758 struct recvbuf *rbufp,
3762 const char nonce_text[] = "nonce";
3763 const char frags_text[] = "frags";
3764 const char limit_text[] = "limit";
3765 const char mincount_text[] = "mincount";
3766 const char resall_text[] = "resall";
3767 const char resany_text[] = "resany";
3768 const char maxlstint_text[] = "maxlstint";
3769 const char laddr_text[] = "laddr";
3770 const char resaxx_fmt[] = "0x%hx";
3778 struct interface * lcladr;
3783 sockaddr_u addr[COUNTOF(last)];
3785 struct ctl_var * in_parms;
3786 const struct ctl_var * v;
3795 mon_entry * prior_mon;
3798 if (RES_NOMRULIST & restrict_mask) {
3799 ctl_error(CERR_PERMISSION);
3802 "mrulist from %s rejected due to nomrulist restriction",
3803 stoa(&rbufp->recv_srcadr));
3808 * fill in_parms var list with all possible input parameters.
3811 set_var(&in_parms, nonce_text, sizeof(nonce_text), 0);
3812 set_var(&in_parms, frags_text, sizeof(frags_text), 0);
3813 set_var(&in_parms, limit_text, sizeof(limit_text), 0);
3814 set_var(&in_parms, mincount_text, sizeof(mincount_text), 0);
3815 set_var(&in_parms, resall_text, sizeof(resall_text), 0);
3816 set_var(&in_parms, resany_text, sizeof(resany_text), 0);
3817 set_var(&in_parms, maxlstint_text, sizeof(maxlstint_text), 0);
3818 set_var(&in_parms, laddr_text, sizeof(laddr_text), 0);
3819 for (i = 0; i < COUNTOF(last); i++) {
3820 snprintf(buf, sizeof(buf), last_fmt, (int)i);
3821 set_var(&in_parms, buf, strlen(buf) + 1, 0);
3822 snprintf(buf, sizeof(buf), addr_fmt, (int)i);
3823 set_var(&in_parms, buf, strlen(buf) + 1, 0);
3826 /* decode input parms */
3839 while (NULL != (v = ctl_getitem(in_parms, &val)) &&
3840 !(EOV & v->flags)) {
3843 if (!strcmp(nonce_text, v->text)) {
3846 pnonce = estrdup(val);
3847 } else if (!strcmp(frags_text, v->text)) {
3848 sscanf(val, "%hu", &frags);
3849 } else if (!strcmp(limit_text, v->text)) {
3850 sscanf(val, "%u", &limit);
3851 } else if (!strcmp(mincount_text, v->text)) {
3852 if (1 != sscanf(val, "%d", &mincount) ||
3855 } else if (!strcmp(resall_text, v->text)) {
3856 sscanf(val, resaxx_fmt, &resall);
3857 } else if (!strcmp(resany_text, v->text)) {
3858 sscanf(val, resaxx_fmt, &resany);
3859 } else if (!strcmp(maxlstint_text, v->text)) {
3860 sscanf(val, "%u", &maxlstint);
3861 } else if (!strcmp(laddr_text, v->text)) {
3862 if (decodenetnum(val, &laddr))
3863 lcladr = getinterface(&laddr, 0);
3864 } else if (1 == sscanf(v->text, last_fmt, &si) &&
3865 (size_t)si < COUNTOF(last)) {
3866 if (2 == sscanf(val, "0x%08x.%08x", &ui, &uf)) {
3869 if (!SOCK_UNSPEC(&addr[si]) &&
3873 } else if (1 == sscanf(v->text, addr_fmt, &si) &&
3874 (size_t)si < COUNTOF(addr)) {
3875 if (decodenetnum(val, &addr[si])
3876 && last[si].l_ui && last[si].l_uf &&
3881 free_varlist(in_parms);
3884 /* return no responses until the nonce is validated */
3888 nonce_valid = validate_nonce(pnonce, rbufp);
3893 if ((0 == frags && !(0 < limit && limit <= MRU_ROW_LIMIT)) ||
3894 frags > MRU_FRAGS_LIMIT) {
3895 ctl_error(CERR_BADVALUE);
3900 * If either frags or limit is not given, use the max.
3902 if (0 != frags && 0 == limit)
3904 else if (0 != limit && 0 == frags)
3905 frags = MRU_FRAGS_LIMIT;
3908 * Find the starting point if one was provided.
3911 for (i = 0; i < (size_t)priors; i++) {
3912 hash = MON_HASH(&addr[i]);
3913 for (mon = mon_hash[hash];
3915 mon = mon->hash_next)
3916 if (ADDR_PORT_EQ(&mon->rmtadr, &addr[i]))
3919 if (L_ISEQU(&mon->last, &last[i]))
3925 /* If a starting point was provided... */
3927 /* and none could be found unmodified... */
3929 /* tell ntpq to try again with older entries */
3930 ctl_error(CERR_UNKNOWNVAR);
3933 /* confirm the prior entry used as starting point */
3934 ctl_putts("last.older", &mon->last);
3935 pch = sptoa(&mon->rmtadr);
3936 ctl_putunqstr("addr.older", pch, strlen(pch));
3939 * Move on to the first entry the client doesn't have,
3940 * except in the special case of a limit of one. In
3941 * that case return the starting point entry.
3944 mon = PREV_DLIST(mon_mru_list, mon, mru);
3945 } else { /* start with the oldest */
3946 mon = TAIL_DLIST(mon_mru_list, mru);
3950 * send up to limit= entries in up to frags= datagrams
3953 generate_nonce(rbufp, buf, sizeof(buf));
3954 ctl_putunqstr("nonce", buf, strlen(buf));
3957 mon != NULL && res_frags < frags && count < limit;
3958 mon = PREV_DLIST(mon_mru_list, mon, mru)) {
3960 if (mon->count < mincount)
3962 if (resall && resall != (resall & mon->flags))
3964 if (resany && !(resany & mon->flags))
3966 if (maxlstint > 0 && now.l_ui - mon->last.l_ui >
3969 if (lcladr != NULL && mon->lcladr != lcladr)
3972 send_mru_entry(mon, count);
3974 send_random_tag_value(0);
3980 * If this batch completes the MRU list, say so explicitly with
3981 * a now= l_fp timestamp.
3985 send_random_tag_value(count - 1);
3986 ctl_putts("now", &now);
3987 /* if any entries were returned confirm the last */
3988 if (prior_mon != NULL)
3989 ctl_putts("last.newest", &prior_mon->last);
3996 * Send a ifstats entry in response to a "ntpq -c ifstats" request.
3998 * To keep clients honest about not depending on the order of values,
3999 * and thereby avoid being locked into ugly workarounds to maintain
4000 * backward compatibility later as new fields are added to the response,
4001 * the order is random.
4009 const char addr_fmtu[] = "addr.%u";
4010 const char bcast_fmt[] = "bcast.%u";
4011 const char en_fmt[] = "en.%u"; /* enabled */
4012 const char name_fmt[] = "name.%u";
4013 const char flags_fmt[] = "flags.%u";
4014 const char tl_fmt[] = "tl.%u"; /* ttl */
4015 const char mc_fmt[] = "mc.%u"; /* mcast count */
4016 const char rx_fmt[] = "rx.%u";
4017 const char tx_fmt[] = "tx.%u";
4018 const char txerr_fmt[] = "txerr.%u";
4019 const char pc_fmt[] = "pc.%u"; /* peer count */
4020 const char up_fmt[] = "up.%u"; /* uptime */
4022 u_char sent[IFSTATS_FIELDS]; /* 12 tag=value pairs */
4029 remaining = COUNTOF(sent);
4033 while (remaining > 0) {
4034 if (noisebits < 4) {
4035 noise = rand() ^ (rand() << 16);
4038 which = (noise & 0xf) % COUNTOF(sent);
4043 which = (which + 1) % COUNTOF(sent);
4048 snprintf(tag, sizeof(tag), addr_fmtu, ifnum);
4049 pch = sptoa(&la->sin);
4050 ctl_putunqstr(tag, pch, strlen(pch));
4054 snprintf(tag, sizeof(tag), bcast_fmt, ifnum);
4055 if (INT_BCASTOPEN & la->flags)
4056 pch = sptoa(&la->bcast);
4059 ctl_putunqstr(tag, pch, strlen(pch));
4063 snprintf(tag, sizeof(tag), en_fmt, ifnum);
4064 ctl_putint(tag, !la->ignore_packets);
4068 snprintf(tag, sizeof(tag), name_fmt, ifnum);
4069 ctl_putstr(tag, la->name, strlen(la->name));
4073 snprintf(tag, sizeof(tag), flags_fmt, ifnum);
4074 ctl_puthex(tag, (u_int)la->flags);
4078 snprintf(tag, sizeof(tag), tl_fmt, ifnum);
4079 ctl_putint(tag, la->last_ttl);
4083 snprintf(tag, sizeof(tag), mc_fmt, ifnum);
4084 ctl_putint(tag, la->num_mcast);
4088 snprintf(tag, sizeof(tag), rx_fmt, ifnum);
4089 ctl_putint(tag, la->received);
4093 snprintf(tag, sizeof(tag), tx_fmt, ifnum);
4094 ctl_putint(tag, la->sent);
4098 snprintf(tag, sizeof(tag), txerr_fmt, ifnum);
4099 ctl_putint(tag, la->notsent);
4103 snprintf(tag, sizeof(tag), pc_fmt, ifnum);
4104 ctl_putuint(tag, la->peercnt);
4108 snprintf(tag, sizeof(tag), up_fmt, ifnum);
4109 ctl_putuint(tag, current_time - la->starttime);
4115 send_random_tag_value((int)ifnum);
4120 * read_ifstats - send statistics for each local address, exposed by
4125 struct recvbuf * rbufp
4132 * loop over [0..sys_ifnum] searching ep_list for each
4135 for (ifidx = 0; ifidx < sys_ifnum; ifidx++) {
4136 for (la = ep_list; la != NULL; la = la->elink)
4137 if (ifidx == la->ifnum)
4141 /* return stats for one local address */
4142 send_ifstats_entry(la, ifidx);
4148 sockaddrs_from_restrict_u(
4158 psaA->sa.sa_family = AF_INET;
4159 psaA->sa4.sin_addr.s_addr = htonl(pres->u.v4.addr);
4160 psaM->sa.sa_family = AF_INET;
4161 psaM->sa4.sin_addr.s_addr = htonl(pres->u.v4.mask);
4163 psaA->sa.sa_family = AF_INET6;
4164 memcpy(&psaA->sa6.sin6_addr, &pres->u.v6.addr,
4165 sizeof(psaA->sa6.sin6_addr));
4166 psaM->sa.sa_family = AF_INET6;
4167 memcpy(&psaM->sa6.sin6_addr, &pres->u.v6.mask,
4168 sizeof(psaA->sa6.sin6_addr));
4174 * Send a restrict entry in response to a "ntpq -c reslist" request.
4176 * To keep clients honest about not depending on the order of values,
4177 * and thereby avoid being locked into ugly workarounds to maintain
4178 * backward compatibility later as new fields are added to the response,
4179 * the order is random.
4182 send_restrict_entry(
4188 const char addr_fmtu[] = "addr.%u";
4189 const char mask_fmtu[] = "mask.%u";
4190 const char hits_fmt[] = "hits.%u";
4191 const char flags_fmt[] = "flags.%u";
4193 u_char sent[RESLIST_FIELDS]; /* 4 tag=value pairs */
4202 const char * match_str;
4203 const char * access_str;
4205 sockaddrs_from_restrict_u(&addr, &mask, pres, ipv6);
4206 remaining = COUNTOF(sent);
4210 while (remaining > 0) {
4211 if (noisebits < 2) {
4212 noise = rand() ^ (rand() << 16);
4215 which = (noise & 0x3) % COUNTOF(sent);
4220 which = (which + 1) % COUNTOF(sent);
4225 snprintf(tag, sizeof(tag), addr_fmtu, idx);
4227 ctl_putunqstr(tag, pch, strlen(pch));
4231 snprintf(tag, sizeof(tag), mask_fmtu, idx);
4233 ctl_putunqstr(tag, pch, strlen(pch));
4237 snprintf(tag, sizeof(tag), hits_fmt, idx);
4238 ctl_putuint(tag, pres->count);
4242 snprintf(tag, sizeof(tag), flags_fmt, idx);
4243 match_str = res_match_flags(pres->mflags);
4244 access_str = res_access_flags(pres->flags);
4245 if ('\0' == match_str[0]) {
4249 snprintf(buf, LIB_BUFLENGTH, "%s %s",
4250 match_str, access_str);
4253 ctl_putunqstr(tag, pch, strlen(pch));
4259 send_random_tag_value((int)idx);
4270 for ( ; pres != NULL; pres = pres->link) {
4271 send_restrict_entry(pres, ipv6, *pidx);
4278 * read_addr_restrictions - returns IPv4 and IPv6 access control lists
4281 read_addr_restrictions(
4282 struct recvbuf * rbufp
4288 send_restrict_list(restrictlist4, FALSE, &idx);
4289 send_restrict_list(restrictlist6, TRUE, &idx);
4295 * read_ordlist - CTL_OP_READ_ORDLIST_A for ntpq -c ifstats & reslist
4299 struct recvbuf * rbufp,
4303 const char ifstats_s[] = "ifstats";
4304 const size_t ifstats_chars = COUNTOF(ifstats_s) - 1;
4305 const char addr_rst_s[] = "addr_restrictions";
4306 const size_t a_r_chars = COUNTOF(addr_rst_s) - 1;
4307 struct ntp_control * cpkt;
4308 u_short qdata_octets;
4311 * CTL_OP_READ_ORDLIST_A was first named CTL_OP_READ_IFSTATS and
4312 * used only for ntpq -c ifstats. With the addition of reslist
4313 * the same opcode was generalized to retrieve ordered lists
4314 * which require authentication. The request data is empty or
4315 * contains "ifstats" (not null terminated) to retrieve local
4316 * addresses and associated stats. It is "addr_restrictions"
4317 * to retrieve the IPv4 then IPv6 remote address restrictions,
4318 * which are access control lists. Other request data return
4321 cpkt = (struct ntp_control *)&rbufp->recv_pkt;
4322 qdata_octets = ntohs(cpkt->count);
4323 if (0 == qdata_octets || (ifstats_chars == qdata_octets &&
4324 !memcmp(ifstats_s, cpkt->u.data, ifstats_chars))) {
4325 read_ifstats(rbufp);
4328 if (a_r_chars == qdata_octets &&
4329 !memcmp(addr_rst_s, cpkt->u.data, a_r_chars)) {
4330 read_addr_restrictions(rbufp);
4333 ctl_error(CERR_UNKNOWNVAR);
4338 * req_nonce - CTL_OP_REQ_NONCE for ntpq -c mrulist prerequisite.
4340 static void req_nonce(
4341 struct recvbuf * rbufp,
4347 generate_nonce(rbufp, buf, sizeof(buf));
4348 ctl_putunqstr("nonce", buf, strlen(buf));
4354 * read_clockstatus - return clock radio status
4359 struct recvbuf *rbufp,
4365 * If no refclock support, no data to return
4367 ctl_error(CERR_BADASSOC);
4369 const struct ctl_var * v;
4377 struct ctl_var * kv;
4378 struct refclockstat cs;
4380 if (res_associd != 0) {
4381 peer = findpeerbyassoc(res_associd);
4384 * Find a clock for this jerk. If the system peer
4385 * is a clock use it, else search peer_list for one.
4387 if (sys_peer != NULL && (FLAG_REFCLOCK &
4391 for (peer = peer_list;
4393 peer = peer->p_link)
4394 if (FLAG_REFCLOCK & peer->flags)
4397 if (NULL == peer || !(FLAG_REFCLOCK & peer->flags)) {
4398 ctl_error(CERR_BADASSOC);
4402 * If we got here we have a peer which is a clock. Get his
4406 refclock_control(&peer->srcadr, NULL, &cs);
4409 * Look for variables in the packet.
4411 rpkt.status = htons(ctlclkstatus(&cs));
4412 wants_alloc = CC_MAXCODE + 1 + count_var(kv);
4413 wants = emalloc_zero(wants_alloc);
4415 while (NULL != (v = ctl_getitem(clock_var, &valuep))) {
4416 if (!(EOV & v->flags)) {
4417 wants[v->code] = TRUE;
4420 v = ctl_getitem(kv, &valuep);
4422 if (EOV & v->flags) {
4423 ctl_error(CERR_UNKNOWNVAR);
4425 free_varlist(cs.kv_list);
4428 wants[CC_MAXCODE + 1 + v->code] = TRUE;
4434 for (i = 1; i <= CC_MAXCODE; i++)
4436 ctl_putclock(i, &cs, TRUE);
4438 for (i = 0; !(EOV & kv[i].flags); i++)
4439 if (wants[i + CC_MAXCODE + 1])
4440 ctl_putdata(kv[i].text,
4444 for (cc = def_clock_var; *cc != 0; cc++)
4445 ctl_putclock((int)*cc, &cs, FALSE);
4446 for ( ; kv != NULL && !(EOV & kv->flags); kv++)
4447 if (DEF & kv->flags)
4448 ctl_putdata(kv->text, strlen(kv->text),
4453 free_varlist(cs.kv_list);
4461 * write_clockstatus - we don't do this
4466 struct recvbuf *rbufp,
4470 ctl_error(CERR_PERMISSION);
4474 * Trap support from here on down. We send async trap messages when the
4475 * upper levels report trouble. Traps can by set either by control
4476 * messages or by configuration.
4479 * set_trap - set a trap in response to a control message
4483 struct recvbuf *rbufp,
4490 * See if this guy is allowed
4492 if (restrict_mask & RES_NOTRAP) {
4493 ctl_error(CERR_PERMISSION);
4498 * Determine his allowed trap type.
4500 traptype = TRAP_TYPE_PRIO;
4501 if (restrict_mask & RES_LPTRAP)
4502 traptype = TRAP_TYPE_NONPRIO;
4505 * Call ctlsettrap() to do the work. Return
4506 * an error if it can't assign the trap.
4508 if (!ctlsettrap(&rbufp->recv_srcadr, rbufp->dstadr, traptype,
4510 ctl_error(CERR_NORESOURCE);
4516 * unset_trap - unset a trap in response to a control message
4520 struct recvbuf *rbufp,
4527 * We don't prevent anyone from removing his own trap unless the
4528 * trap is configured. Note we also must be aware of the
4529 * possibility that restriction flags were changed since this
4530 * guy last set his trap. Set the trap type based on this.
4532 traptype = TRAP_TYPE_PRIO;
4533 if (restrict_mask & RES_LPTRAP)
4534 traptype = TRAP_TYPE_NONPRIO;
4537 * Call ctlclrtrap() to clear this out.
4539 if (!ctlclrtrap(&rbufp->recv_srcadr, rbufp->dstadr, traptype))
4540 ctl_error(CERR_BADASSOC);
4546 * ctlsettrap - called to set a trap
4551 struct interface *linter,
4557 struct ctl_trap *tp;
4558 struct ctl_trap *tptouse;
4561 * See if we can find this trap. If so, we only need update
4562 * the flags and the time.
4564 if ((tp = ctlfindtrap(raddr, linter)) != NULL) {
4567 case TRAP_TYPE_CONFIG:
4568 tp->tr_flags = TRAP_INUSE|TRAP_CONFIGURED;
4571 case TRAP_TYPE_PRIO:
4572 if (tp->tr_flags & TRAP_CONFIGURED)
4573 return (1); /* don't change anything */
4574 tp->tr_flags = TRAP_INUSE;
4577 case TRAP_TYPE_NONPRIO:
4578 if (tp->tr_flags & TRAP_CONFIGURED)
4579 return (1); /* don't change anything */
4580 tp->tr_flags = TRAP_INUSE|TRAP_NONPRIO;
4583 tp->tr_settime = current_time;
4589 * First we heard of this guy. Try to find a trap structure
4590 * for him to use, clearing out lesser priority guys if we
4591 * have to. Clear out anyone who's expired while we're at it.
4594 for (n = 0; n < COUNTOF(ctl_traps); n++) {
4596 if ((TRAP_INUSE & tp->tr_flags) &&
4597 !(TRAP_CONFIGURED & tp->tr_flags) &&
4598 ((tp->tr_settime + CTL_TRAPTIME) > current_time)) {
4602 if (!(TRAP_INUSE & tp->tr_flags)) {
4604 } else if (!(TRAP_CONFIGURED & tp->tr_flags)) {
4607 case TRAP_TYPE_CONFIG:
4608 if (tptouse == NULL) {
4612 if ((TRAP_NONPRIO & tptouse->tr_flags) &&
4613 !(TRAP_NONPRIO & tp->tr_flags))
4616 if (!(TRAP_NONPRIO & tptouse->tr_flags)
4617 && (TRAP_NONPRIO & tp->tr_flags)) {
4621 if (tptouse->tr_origtime <
4626 case TRAP_TYPE_PRIO:
4627 if ( TRAP_NONPRIO & tp->tr_flags) {
4628 if (tptouse == NULL ||
4630 tptouse->tr_flags) &&
4631 tptouse->tr_origtime <
4637 case TRAP_TYPE_NONPRIO:
4644 * If we don't have room for him return an error.
4646 if (tptouse == NULL)
4650 * Set up this structure for him.
4652 tptouse->tr_settime = tptouse->tr_origtime = current_time;
4653 tptouse->tr_count = tptouse->tr_resets = 0;
4654 tptouse->tr_sequence = 1;
4655 tptouse->tr_addr = *raddr;
4656 tptouse->tr_localaddr = linter;
4657 tptouse->tr_version = (u_char) version;
4658 tptouse->tr_flags = TRAP_INUSE;
4659 if (traptype == TRAP_TYPE_CONFIG)
4660 tptouse->tr_flags |= TRAP_CONFIGURED;
4661 else if (traptype == TRAP_TYPE_NONPRIO)
4662 tptouse->tr_flags |= TRAP_NONPRIO;
4669 * ctlclrtrap - called to clear a trap
4674 struct interface *linter,
4678 register struct ctl_trap *tp;
4680 if ((tp = ctlfindtrap(raddr, linter)) == NULL)
4683 if (tp->tr_flags & TRAP_CONFIGURED
4684 && traptype != TRAP_TYPE_CONFIG)
4694 * ctlfindtrap - find a trap given the remote and local addresses
4696 static struct ctl_trap *
4699 struct interface *linter
4704 for (n = 0; n < COUNTOF(ctl_traps); n++)
4705 if ((ctl_traps[n].tr_flags & TRAP_INUSE)
4706 && ADDR_PORT_EQ(raddr, &ctl_traps[n].tr_addr)
4707 && (linter == ctl_traps[n].tr_localaddr))
4708 return &ctl_traps[n];
4715 * report_event - report an event to the trappers
4719 int err, /* error code */
4720 struct peer *peer, /* peer structure pointer */
4721 const char *str /* protostats string */
4724 char statstr[NTP_MAXSTRLEN];
4729 * Report the error to the protostats file, system log and
4735 * Discard a system report if the number of reports of
4736 * the same type exceeds the maximum.
4738 if (ctl_sys_last_event != (u_char)err)
4739 ctl_sys_num_events= 0;
4740 if (ctl_sys_num_events >= CTL_SYS_MAXEVENTS)
4743 ctl_sys_last_event = (u_char)err;
4744 ctl_sys_num_events++;
4745 snprintf(statstr, sizeof(statstr),
4746 "0.0.0.0 %04x %02x %s",
4747 ctlsysstatus(), err, eventstr(err));
4749 len = strlen(statstr);
4750 snprintf(statstr + len, sizeof(statstr) - len,
4754 msyslog(LOG_INFO, "%s", statstr);
4758 * Discard a peer report if the number of reports of
4759 * the same type exceeds the maximum for that peer.
4764 errlast = (u_char)err & ~PEER_EVENT;
4765 if (peer->last_event == errlast)
4766 peer->num_events = 0;
4767 if (peer->num_events >= CTL_PEER_MAXEVENTS)
4770 peer->last_event = errlast;
4772 if (ISREFCLOCKADR(&peer->srcadr))
4773 src = refnumtoa(&peer->srcadr);
4775 src = stoa(&peer->srcadr);
4777 snprintf(statstr, sizeof(statstr),
4778 "%s %04x %02x %s", src,
4779 ctlpeerstatus(peer), err, eventstr(err));
4781 len = strlen(statstr);
4782 snprintf(statstr + len, sizeof(statstr) - len,
4785 NLOG(NLOG_PEEREVENT)
4786 msyslog(LOG_INFO, "%s", statstr);
4788 record_proto_stats(statstr);
4791 printf("event at %lu %s\n", current_time, statstr);
4795 * If no trappers, return.
4797 if (num_ctl_traps <= 0)
4801 * Set up the outgoing packet variables
4803 res_opcode = CTL_OP_ASYNCMSG;
4806 res_authenticate = FALSE;
4807 datapt = rpkt.u.data;
4808 dataend = &rpkt.u.data[CTL_MAX_DATA_LEN];
4809 if (!(err & PEER_EVENT)) {
4811 rpkt.status = htons(ctlsysstatus());
4813 /* Include the core system variables and the list. */
4814 for (i = 1; i <= CS_VARLIST; i++)
4817 INSIST(peer != NULL);
4818 rpkt.associd = htons(peer->associd);
4819 rpkt.status = htons(ctlpeerstatus(peer));
4821 /* Dump it all. Later, maybe less. */
4822 for (i = 1; i <= CP_MAX_NOAUTOKEY; i++)
4823 ctl_putpeer(i, peer);
4826 * for clock exception events: add clock variables to
4827 * reflect info on exception
4829 if (err == PEVNT_CLOCK) {
4830 struct refclockstat cs;
4834 refclock_control(&peer->srcadr, NULL, &cs);
4836 ctl_puthex("refclockstatus",
4839 for (i = 1; i <= CC_MAXCODE; i++)
4840 ctl_putclock(i, &cs, FALSE);
4841 for (kv = cs.kv_list;
4842 kv != NULL && !(EOV & kv->flags);
4844 if (DEF & kv->flags)
4845 ctl_putdata(kv->text,
4848 free_varlist(cs.kv_list);
4850 #endif /* REFCLOCK */
4854 * We're done, return.
4861 * mprintf_event - printf-style varargs variant of report_event()
4865 int evcode, /* event code */
4866 struct peer * p, /* may be NULL */
4867 const char * fmt, /* msnprintf format */
4876 rc = mvsnprintf(msg, sizeof(msg), fmt, ap);
4878 report_event(evcode, p, msg);
4885 * ctl_clr_stats - clear stat counters
4890 ctltimereset = current_time;
4893 numctlresponses = 0;
4898 numctlinputresp = 0;
4899 numctlinputfrag = 0;
4901 numctlbadoffset = 0;
4902 numctlbadversion = 0;
4903 numctldatatooshort = 0;
4910 const struct ctl_var *k
4919 while (!(EOV & (k++)->flags))
4922 ENSURE(c <= USHRT_MAX);
4929 struct ctl_var **kv,
4939 *kv = erealloc(*kv, (c + 2) * sizeof(**kv));
4941 buf = emalloc(size);
4946 k[c + 1].text = NULL;
4947 k[c + 1].flags = EOV;
4955 struct ctl_var **kv,
4966 if (NULL == data || !size)
4971 while (!(EOV & k->flags)) {
4972 if (NULL == k->text) {
4974 memcpy(td, data, size);
4981 while (*t != '=' && *s == *t) {
4985 if (*s == *t && ((*t == '=') || !*t)) {
4986 td = erealloc((void *)(intptr_t)k->text, size);
4987 memcpy(td, data, size);
4996 td = add_var(kv, size, def);
4997 memcpy(td, data, size);
5008 set_var(&ext_sys_var, data, size, def);
5013 * get_ext_sys_var() retrieves the value of a user-defined variable or
5014 * NULL if the variable has not been setvar'd.
5017 get_ext_sys_var(const char *tag)
5025 for (v = ext_sys_var; !(EOV & v->flags); v++) {
5026 if (NULL != v->text && !memcmp(tag, v->text, c)) {
5027 if ('=' == v->text[c]) {
5028 val = v->text + c + 1;
5030 } else if ('\0' == v->text[c]) {
5048 for (k = kv; !(k->flags & EOV); k++)
5049 free((void *)(intptr_t)k->text);