1 /* 2 * ntp_control.c - respond to mode 6 control messages and send async 3 * traps. Provides service to ntpq and others. 4 */ 5 6 #ifdef HAVE_CONFIG_H 7 # include <config.h> 8 #endif 9 10 #include <stdio.h> 11 #include <ctype.h> 12 #include <signal.h> 13 #include <sys/stat.h> 14 #ifdef HAVE_NETINET_IN_H 15 # include <netinet/in.h> 16 #endif 17 #include <arpa/inet.h> 18 19 #include "ntpd.h" 20 #include "ntp_io.h" 21 #include "ntp_refclock.h" 22 #include "ntp_control.h" 23 #include "ntp_unixtime.h" 24 #include "ntp_stdlib.h" 25 #include "ntp_config.h" 26 #include "ntp_crypto.h" 27 #include "ntp_assert.h" 28 #include "ntp_leapsec.h" 29 #include "timexsup.h" 30 31 #include <rc_cmdlength.h> 32 #ifdef KERNEL_PLL 33 # include "ntp_syscall.h" 34 #endif 35 36 /* 37 * Structure to hold request procedure information 38 */ 39 40 struct ctl_proc { 41 short control_code; /* defined request code */ 42 #define NO_REQUEST (-1) 43 u_short flags; /* flags word */ 44 /* Only one flag. Authentication required or not. */ 45 #define NOAUTH 0 46 #define AUTH 1 47 void (*handler) (struct recvbuf *, int); /* handle request */ 48 }; 49 50 51 /* 52 * Request processing routines 53 */ 54 static void ctl_error (u_char); 55 #ifdef REFCLOCK 56 static u_short ctlclkstatus (struct refclockstat *); 57 #endif 58 static void ctl_flushpkt (u_char); 59 static void ctl_putdata (const char *, unsigned int, int); 60 static void ctl_putstr (const char *, const char *, size_t); 61 static void ctl_putdblf (const char *, int, int, double); 62 #define ctl_putdbl(tag, d) ctl_putdblf(tag, 1, 3, d) 63 #define ctl_putdbl6(tag, d) ctl_putdblf(tag, 1, 6, d) 64 #define ctl_putsfp(tag, sfp) ctl_putdblf(tag, 0, -1, \ 65 FPTOD(sfp)) 66 static void ctl_putuint (const char *, u_long); 67 static void ctl_puthex (const char *, u_long); 68 static void ctl_putint (const char *, long); 69 static void ctl_putts (const char *, l_fp *); 70 static void ctl_putadr (const char *, u_int32, 71 sockaddr_u *); 72 static void ctl_putrefid (const char *, u_int32); 73 static void ctl_putarray (const char *, double *, int); 74 static void ctl_putsys (int); 75 static void ctl_putpeer (int, struct peer *); 76 static void ctl_putfs (const char *, tstamp_t); 77 static void ctl_printf (const char *, ...) NTP_PRINTF(1, 2); 78 #ifdef REFCLOCK 79 static void ctl_putclock (int, struct refclockstat *, int); 80 #endif /* REFCLOCK */ 81 static const struct ctl_var *ctl_getitem(const struct ctl_var *, 82 char **); 83 static u_short count_var (const struct ctl_var *); 84 static void control_unspec (struct recvbuf *, int); 85 static void read_status (struct recvbuf *, int); 86 static void read_sysvars (void); 87 static void read_peervars (void); 88 static void read_variables (struct recvbuf *, int); 89 static void write_variables (struct recvbuf *, int); 90 static void read_clockstatus(struct recvbuf *, int); 91 static void write_clockstatus(struct recvbuf *, int); 92 static void set_trap (struct recvbuf *, int); 93 static void save_config (struct recvbuf *, int); 94 static void configure (struct recvbuf *, int); 95 static void send_mru_entry (mon_entry *, int); 96 static void send_random_tag_value(int); 97 static void read_mru_list (struct recvbuf *, int); 98 static void send_ifstats_entry(endpt *, u_int); 99 static void read_ifstats (struct recvbuf *); 100 static void sockaddrs_from_restrict_u(sockaddr_u *, sockaddr_u *, 101 restrict_u *, int); 102 static void send_restrict_entry(restrict_u *, int, u_int); 103 static void send_restrict_list(restrict_u *, int, u_int *); 104 static void read_addr_restrictions(struct recvbuf *); 105 static void read_ordlist (struct recvbuf *, int); 106 static u_int32 derive_nonce (sockaddr_u *, u_int32, u_int32); 107 static void generate_nonce (struct recvbuf *, char *, size_t); 108 static int validate_nonce (const char *, struct recvbuf *); 109 static void req_nonce (struct recvbuf *, int); 110 static void unset_trap (struct recvbuf *, int); 111 static struct ctl_trap *ctlfindtrap(sockaddr_u *, 112 endpt *); 113 114 int/*BOOL*/ is_safe_filename(const char * name); 115 116 static const struct ctl_proc control_codes[] = { 117 { CTL_OP_UNSPEC, NOAUTH, control_unspec }, 118 { CTL_OP_READSTAT, NOAUTH, read_status }, 119 { CTL_OP_READVAR, NOAUTH, read_variables }, 120 { CTL_OP_WRITEVAR, AUTH, write_variables }, 121 { CTL_OP_READCLOCK, NOAUTH, read_clockstatus }, 122 { CTL_OP_WRITECLOCK, AUTH, write_clockstatus }, 123 { CTL_OP_SETTRAP, AUTH, set_trap }, 124 { CTL_OP_CONFIGURE, AUTH, configure }, 125 { CTL_OP_SAVECONFIG, AUTH, save_config }, 126 { CTL_OP_READ_MRU, NOAUTH, read_mru_list }, 127 { CTL_OP_READ_ORDLIST_A, AUTH, read_ordlist }, 128 { CTL_OP_REQ_NONCE, NOAUTH, req_nonce }, 129 { CTL_OP_UNSETTRAP, AUTH, unset_trap }, 130 { NO_REQUEST, 0, NULL } 131 }; 132 133 /* 134 * System variables we understand 135 */ 136 #define CS_LEAP 1 137 #define CS_STRATUM 2 138 #define CS_PRECISION 3 139 #define CS_ROOTDELAY 4 140 #define CS_ROOTDISPERSION 5 141 #define CS_REFID 6 142 #define CS_REFTIME 7 143 #define CS_POLL 8 144 #define CS_PEERID 9 145 #define CS_OFFSET 10 146 #define CS_DRIFT 11 147 #define CS_JITTER 12 148 #define CS_ERROR 13 149 #define CS_CLOCK 14 150 #define CS_PROCESSOR 15 151 #define CS_SYSTEM 16 152 #define CS_VERSION 17 153 #define CS_STABIL 18 154 #define CS_VARLIST 19 155 #define CS_TAI 20 156 #define CS_LEAPTAB 21 157 #define CS_LEAPEND 22 158 #define CS_RATE 23 159 #define CS_MRU_ENABLED 24 160 #define CS_MRU_DEPTH 25 161 #define CS_MRU_DEEPEST 26 162 #define CS_MRU_MINDEPTH 27 163 #define CS_MRU_MAXAGE 28 164 #define CS_MRU_MAXDEPTH 29 165 #define CS_MRU_MEM 30 166 #define CS_MRU_MAXMEM 31 167 #define CS_SS_UPTIME 32 168 #define CS_SS_RESET 33 169 #define CS_SS_RECEIVED 34 170 #define CS_SS_THISVER 35 171 #define CS_SS_OLDVER 36 172 #define CS_SS_BADFORMAT 37 173 #define CS_SS_BADAUTH 38 174 #define CS_SS_DECLINED 39 175 #define CS_SS_RESTRICTED 40 176 #define CS_SS_LIMITED 41 177 #define CS_SS_KODSENT 42 178 #define CS_SS_PROCESSED 43 179 #define CS_SS_LAMPORT 44 180 #define CS_SS_TSROUNDING 45 181 #define CS_PEERADR 46 182 #define CS_PEERMODE 47 183 #define CS_BCASTDELAY 48 184 #define CS_AUTHDELAY 49 185 #define CS_AUTHKEYS 50 186 #define CS_AUTHFREEK 51 187 #define CS_AUTHKLOOKUPS 52 188 #define CS_AUTHKNOTFOUND 53 189 #define CS_AUTHKUNCACHED 54 190 #define CS_AUTHKEXPIRED 55 191 #define CS_AUTHENCRYPTS 56 192 #define CS_AUTHDECRYPTS 57 193 #define CS_AUTHRESET 58 194 #define CS_K_OFFSET 59 195 #define CS_K_FREQ 60 196 #define CS_K_MAXERR 61 197 #define CS_K_ESTERR 62 198 #define CS_K_STFLAGS 63 199 #define CS_K_TIMECONST 64 200 #define CS_K_PRECISION 65 201 #define CS_K_FREQTOL 66 202 #define CS_K_PPS_FREQ 67 203 #define CS_K_PPS_STABIL 68 204 #define CS_K_PPS_JITTER 69 205 #define CS_K_PPS_CALIBDUR 70 206 #define CS_K_PPS_CALIBS 71 207 #define CS_K_PPS_CALIBERRS 72 208 #define CS_K_PPS_JITEXC 73 209 #define CS_K_PPS_STBEXC 74 210 #define CS_KERN_FIRST CS_K_OFFSET 211 #define CS_KERN_LAST CS_K_PPS_STBEXC 212 #define CS_IOSTATS_RESET 75 213 #define CS_TOTAL_RBUF 76 214 #define CS_FREE_RBUF 77 215 #define CS_USED_RBUF 78 216 #define CS_RBUF_LOWATER 79 217 #define CS_IO_DROPPED 80 218 #define CS_IO_IGNORED 81 219 #define CS_IO_RECEIVED 82 220 #define CS_IO_SENT 83 221 #define CS_IO_SENDFAILED 84 222 #define CS_IO_WAKEUPS 85 223 #define CS_IO_GOODWAKEUPS 86 224 #define CS_TIMERSTATS_RESET 87 225 #define CS_TIMER_OVERRUNS 88 226 #define CS_TIMER_XMTS 89 227 #define CS_FUZZ 90 228 #define CS_WANDER_THRESH 91 229 #define CS_LEAPSMEARINTV 92 230 #define CS_LEAPSMEAROFFS 93 231 #define CS_MAX_NOAUTOKEY CS_LEAPSMEAROFFS 232 #ifdef AUTOKEY 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 */ 245 246 /* 247 * Peer variables we understand 248 */ 249 #define CP_CONFIG 1 250 #define CP_AUTHENABLE 2 251 #define CP_AUTHENTIC 3 252 #define CP_SRCADR 4 253 #define CP_SRCPORT 5 254 #define CP_DSTADR 6 255 #define CP_DSTPORT 7 256 #define CP_LEAP 8 257 #define CP_HMODE 9 258 #define CP_STRATUM 10 259 #define CP_PPOLL 11 260 #define CP_HPOLL 12 261 #define CP_PRECISION 13 262 #define CP_ROOTDELAY 14 263 #define CP_ROOTDISPERSION 15 264 #define CP_REFID 16 265 #define CP_REFTIME 17 266 #define CP_ORG 18 267 #define CP_REC 19 268 #define CP_XMT 20 269 #define CP_REACH 21 270 #define CP_UNREACH 22 271 #define CP_TIMER 23 272 #define CP_DELAY 24 273 #define CP_OFFSET 25 274 #define CP_JITTER 26 275 #define CP_DISPERSION 27 276 #define CP_KEYID 28 277 #define CP_FILTDELAY 29 278 #define CP_FILTOFFSET 30 279 #define CP_PMODE 31 280 #define CP_RECEIVED 32 281 #define CP_SENT 33 282 #define CP_FILTERROR 34 283 #define CP_FLASH 35 284 #define CP_TTL 36 285 #define CP_VARLIST 37 286 #define CP_IN 38 287 #define CP_OUT 39 288 #define CP_RATE 40 289 #define CP_BIAS 41 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 295 #define CP_OLDPKT 47 296 #define CP_SELDISP 48 297 #define CP_SELBROKEN 49 298 #define CP_CANDIDATE 50 299 #define CP_MAX_NOAUTOKEY CP_CANDIDATE 300 #ifdef AUTOKEY 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 */ 313 314 /* 315 * Clock variables we understand 316 */ 317 #define CC_TYPE 1 318 #define CC_TIMECODE 2 319 #define CC_POLL 3 320 #define CC_NOREPLY 4 321 #define CC_BADFORMAT 5 322 #define CC_BADDATA 6 323 #define CC_FUDGETIME1 7 324 #define CC_FUDGETIME2 8 325 #define CC_FUDGEVAL1 9 326 #define CC_FUDGEVAL2 10 327 #define CC_FLAGS 11 328 #define CC_DEVICE 12 329 #define CC_VARLIST 13 330 #define CC_FUDGEMINJIT 14 331 #define CC_MAXCODE CC_FUDGEMINJIT 332 333 /* 334 * System variable values. The array can be indexed by the variable 335 * index to find the textual name. 336 */ 337 static const struct ctl_var sys_var[] = { 338 { 0, PADDING, "" }, /* 0 */ 339 { CS_LEAP, RW, "leap" }, /* 1 */ 340 { CS_STRATUM, RO, "stratum" }, /* 2 */ 341 { CS_PRECISION, RO, "precision" }, /* 3 */ 342 { CS_ROOTDELAY, RO, "rootdelay" }, /* 4 */ 343 { CS_ROOTDISPERSION, RO, "rootdisp" }, /* 5 */ 344 { CS_REFID, RO, "refid" }, /* 6 */ 345 { CS_REFTIME, RO, "reftime" }, /* 7 */ 346 { CS_POLL, RO, "tc" }, /* 8 */ 347 { CS_PEERID, RO, "peer" }, /* 9 */ 348 { CS_OFFSET, RO, "offset" }, /* 10 */ 349 { CS_DRIFT, RO, "frequency" }, /* 11 */ 350 { CS_JITTER, RO, "sys_jitter" }, /* 12 */ 351 { CS_ERROR, RO, "clk_jitter" }, /* 13 */ 352 { CS_CLOCK, RO, "clock" }, /* 14 */ 353 { CS_PROCESSOR, RO, "processor" }, /* 15 */ 354 { CS_SYSTEM, RO, "system" }, /* 16 */ 355 { CS_VERSION, RO, "version" }, /* 17 */ 356 { CS_STABIL, RO, "clk_wander" }, /* 18 */ 357 { CS_VARLIST, RO, "sys_var_list" }, /* 19 */ 358 { CS_TAI, RO, "tai" }, /* 20 */ 359 { CS_LEAPTAB, RO, "leapsec" }, /* 21 */ 360 { CS_LEAPEND, RO, "expire" }, /* 22 */ 361 { CS_RATE, RO, "mintc" }, /* 23 */ 362 { CS_MRU_ENABLED, RO, "mru_enabled" }, /* 24 */ 363 { CS_MRU_DEPTH, RO, "mru_depth" }, /* 25 */ 364 { CS_MRU_DEEPEST, RO, "mru_deepest" }, /* 26 */ 365 { CS_MRU_MINDEPTH, RO, "mru_mindepth" }, /* 27 */ 366 { CS_MRU_MAXAGE, RO, "mru_maxage" }, /* 28 */ 367 { CS_MRU_MAXDEPTH, RO, "mru_maxdepth" }, /* 29 */ 368 { CS_MRU_MEM, RO, "mru_mem" }, /* 30 */ 369 { CS_MRU_MAXMEM, RO, "mru_maxmem" }, /* 31 */ 370 { CS_SS_UPTIME, RO, "ss_uptime" }, /* 32 */ 371 { CS_SS_RESET, RO, "ss_reset" }, /* 33 */ 372 { CS_SS_RECEIVED, RO, "ss_received" }, /* 34 */ 373 { CS_SS_THISVER, RO, "ss_thisver" }, /* 35 */ 374 { CS_SS_OLDVER, RO, "ss_oldver" }, /* 36 */ 375 { CS_SS_BADFORMAT, RO, "ss_badformat" }, /* 37 */ 376 { CS_SS_BADAUTH, RO, "ss_badauth" }, /* 38 */ 377 { CS_SS_DECLINED, RO, "ss_declined" }, /* 39 */ 378 { CS_SS_RESTRICTED, RO, "ss_restricted" }, /* 40 */ 379 { CS_SS_LIMITED, RO, "ss_limited" }, /* 41 */ 380 { CS_SS_KODSENT, RO, "ss_kodsent" }, /* 42 */ 381 { CS_SS_PROCESSED, RO, "ss_processed" }, /* 43 */ 382 { CS_SS_LAMPORT, RO, "ss_lamport" }, /* 44 */ 383 { CS_SS_TSROUNDING, RO, "ss_tsrounding" }, /* 45 */ 384 { CS_PEERADR, RO, "peeradr" }, /* 46 */ 385 { CS_PEERMODE, RO, "peermode" }, /* 47 */ 386 { CS_BCASTDELAY, RO, "bcastdelay" }, /* 48 */ 387 { CS_AUTHDELAY, RO, "authdelay" }, /* 49 */ 388 { CS_AUTHKEYS, RO, "authkeys" }, /* 50 */ 389 { CS_AUTHFREEK, RO, "authfreek" }, /* 51 */ 390 { CS_AUTHKLOOKUPS, RO, "authklookups" }, /* 52 */ 391 { CS_AUTHKNOTFOUND, RO, "authknotfound" }, /* 53 */ 392 { CS_AUTHKUNCACHED, RO, "authkuncached" }, /* 54 */ 393 { CS_AUTHKEXPIRED, RO, "authkexpired" }, /* 55 */ 394 { CS_AUTHENCRYPTS, RO, "authencrypts" }, /* 56 */ 395 { CS_AUTHDECRYPTS, RO, "authdecrypts" }, /* 57 */ 396 { CS_AUTHRESET, RO, "authreset" }, /* 58 */ 397 { CS_K_OFFSET, RO, "koffset" }, /* 59 */ 398 { CS_K_FREQ, RO, "kfreq" }, /* 60 */ 399 { CS_K_MAXERR, RO, "kmaxerr" }, /* 61 */ 400 { CS_K_ESTERR, RO, "kesterr" }, /* 62 */ 401 { CS_K_STFLAGS, RO, "kstflags" }, /* 63 */ 402 { CS_K_TIMECONST, RO, "ktimeconst" }, /* 64 */ 403 { CS_K_PRECISION, RO, "kprecis" }, /* 65 */ 404 { CS_K_FREQTOL, RO, "kfreqtol" }, /* 66 */ 405 { CS_K_PPS_FREQ, RO, "kppsfreq" }, /* 67 */ 406 { CS_K_PPS_STABIL, RO, "kppsstab" }, /* 68 */ 407 { CS_K_PPS_JITTER, RO, "kppsjitter" }, /* 69 */ 408 { CS_K_PPS_CALIBDUR, RO, "kppscalibdur" }, /* 70 */ 409 { CS_K_PPS_CALIBS, RO, "kppscalibs" }, /* 71 */ 410 { CS_K_PPS_CALIBERRS, RO, "kppscaliberrs" }, /* 72 */ 411 { CS_K_PPS_JITEXC, RO, "kppsjitexc" }, /* 73 */ 412 { CS_K_PPS_STBEXC, RO, "kppsstbexc" }, /* 74 */ 413 { CS_IOSTATS_RESET, RO, "iostats_reset" }, /* 75 */ 414 { CS_TOTAL_RBUF, RO, "total_rbuf" }, /* 76 */ 415 { CS_FREE_RBUF, RO, "free_rbuf" }, /* 77 */ 416 { CS_USED_RBUF, RO, "used_rbuf" }, /* 78 */ 417 { CS_RBUF_LOWATER, RO, "rbuf_lowater" }, /* 79 */ 418 { CS_IO_DROPPED, RO, "io_dropped" }, /* 80 */ 419 { CS_IO_IGNORED, RO, "io_ignored" }, /* 81 */ 420 { CS_IO_RECEIVED, RO, "io_received" }, /* 82 */ 421 { CS_IO_SENT, RO, "io_sent" }, /* 83 */ 422 { CS_IO_SENDFAILED, RO, "io_sendfailed" }, /* 84 */ 423 { CS_IO_WAKEUPS, RO, "io_wakeups" }, /* 85 */ 424 { CS_IO_GOODWAKEUPS, RO, "io_goodwakeups" }, /* 86 */ 425 { CS_TIMERSTATS_RESET, RO, "timerstats_reset" },/* 87 */ 426 { CS_TIMER_OVERRUNS, RO, "timer_overruns" }, /* 88 */ 427 { CS_TIMER_XMTS, RO, "timer_xmts" }, /* 89 */ 428 { CS_FUZZ, RO, "fuzz" }, /* 90 */ 429 { CS_WANDER_THRESH, RO, "clk_wander_threshold" }, /* 91 */ 430 431 { CS_LEAPSMEARINTV, RO, "leapsmearinterval" }, /* 92 */ 432 { CS_LEAPSMEAROFFS, RO, "leapsmearoffset" }, /* 93 */ 433 434 #ifdef AUTOKEY 435 { CS_FLAGS, RO, "flags" }, /* 1 + CS_MAX_NOAUTOKEY */ 436 { CS_HOST, RO, "host" }, /* 2 + CS_MAX_NOAUTOKEY */ 437 { CS_PUBLIC, RO, "update" }, /* 3 + CS_MAX_NOAUTOKEY */ 438 { CS_CERTIF, RO, "cert" }, /* 4 + CS_MAX_NOAUTOKEY */ 439 { CS_SIGNATURE, RO, "signature" }, /* 5 + CS_MAX_NOAUTOKEY */ 440 { CS_REVTIME, RO, "until" }, /* 6 + CS_MAX_NOAUTOKEY */ 441 { CS_IDENT, RO, "ident" }, /* 7 + CS_MAX_NOAUTOKEY */ 442 { CS_DIGEST, RO, "digest" }, /* 8 + CS_MAX_NOAUTOKEY */ 443 #endif /* AUTOKEY */ 444 { 0, EOV, "" } /* 94/102 */ 445 }; 446 447 static struct ctl_var *ext_sys_var = NULL; 448 449 /* 450 * System variables we print by default (in fuzzball order, 451 * more-or-less) 452 */ 453 static const u_char def_sys_var[] = { 454 CS_VERSION, 455 CS_PROCESSOR, 456 CS_SYSTEM, 457 CS_LEAP, 458 CS_STRATUM, 459 CS_PRECISION, 460 CS_ROOTDELAY, 461 CS_ROOTDISPERSION, 462 CS_REFID, 463 CS_REFTIME, 464 CS_CLOCK, 465 CS_PEERID, 466 CS_POLL, 467 CS_RATE, 468 CS_OFFSET, 469 CS_DRIFT, 470 CS_JITTER, 471 CS_ERROR, 472 CS_STABIL, 473 CS_TAI, 474 CS_LEAPTAB, 475 CS_LEAPEND, 476 CS_LEAPSMEARINTV, 477 CS_LEAPSMEAROFFS, 478 #ifdef AUTOKEY 479 CS_HOST, 480 CS_IDENT, 481 CS_FLAGS, 482 CS_DIGEST, 483 CS_SIGNATURE, 484 CS_PUBLIC, 485 CS_CERTIF, 486 #endif /* AUTOKEY */ 487 0 488 }; 489 490 491 /* 492 * Peer variable list 493 */ 494 static const struct ctl_var peer_var[] = { 495 { 0, PADDING, "" }, /* 0 */ 496 { CP_CONFIG, RO, "config" }, /* 1 */ 497 { CP_AUTHENABLE, RO, "authenable" }, /* 2 */ 498 { CP_AUTHENTIC, RO, "authentic" }, /* 3 */ 499 { CP_SRCADR, RO, "srcadr" }, /* 4 */ 500 { CP_SRCPORT, RO, "srcport" }, /* 5 */ 501 { CP_DSTADR, RO, "dstadr" }, /* 6 */ 502 { CP_DSTPORT, RO, "dstport" }, /* 7 */ 503 { CP_LEAP, RO, "leap" }, /* 8 */ 504 { CP_HMODE, RO, "hmode" }, /* 9 */ 505 { CP_STRATUM, RO, "stratum" }, /* 10 */ 506 { CP_PPOLL, RO, "ppoll" }, /* 11 */ 507 { CP_HPOLL, RO, "hpoll" }, /* 12 */ 508 { CP_PRECISION, RO, "precision" }, /* 13 */ 509 { CP_ROOTDELAY, RO, "rootdelay" }, /* 14 */ 510 { CP_ROOTDISPERSION, RO, "rootdisp" }, /* 15 */ 511 { CP_REFID, RO, "refid" }, /* 16 */ 512 { CP_REFTIME, RO, "reftime" }, /* 17 */ 513 { CP_ORG, RO, "org" }, /* 18 */ 514 { CP_REC, RO, "rec" }, /* 19 */ 515 { CP_XMT, RO, "xleave" }, /* 20 */ 516 { CP_REACH, RO, "reach" }, /* 21 */ 517 { CP_UNREACH, RO, "unreach" }, /* 22 */ 518 { CP_TIMER, RO, "timer" }, /* 23 */ 519 { CP_DELAY, RO, "delay" }, /* 24 */ 520 { CP_OFFSET, RO, "offset" }, /* 25 */ 521 { CP_JITTER, RO, "jitter" }, /* 26 */ 522 { CP_DISPERSION, RO, "dispersion" }, /* 27 */ 523 { CP_KEYID, RO, "keyid" }, /* 28 */ 524 { CP_FILTDELAY, RO, "filtdelay" }, /* 29 */ 525 { CP_FILTOFFSET, RO, "filtoffset" }, /* 30 */ 526 { CP_PMODE, RO, "pmode" }, /* 31 */ 527 { CP_RECEIVED, RO, "received"}, /* 32 */ 528 { CP_SENT, RO, "sent" }, /* 33 */ 529 { CP_FILTERROR, RO, "filtdisp" }, /* 34 */ 530 { CP_FLASH, RO, "flash" }, /* 35 */ 531 { CP_TTL, RO, "ttl" }, /* 36 */ 532 { CP_VARLIST, RO, "peer_var_list" }, /* 37 */ 533 { CP_IN, RO, "in" }, /* 38 */ 534 { CP_OUT, RO, "out" }, /* 39 */ 535 { CP_RATE, RO, "headway" }, /* 40 */ 536 { CP_BIAS, RO, "bias" }, /* 41 */ 537 { CP_SRCHOST, RO, "srchost" }, /* 42 */ 538 { CP_TIMEREC, RO, "timerec" }, /* 43 */ 539 { CP_TIMEREACH, RO, "timereach" }, /* 44 */ 540 { CP_BADAUTH, RO, "badauth" }, /* 45 */ 541 { CP_BOGUSORG, RO, "bogusorg" }, /* 46 */ 542 { CP_OLDPKT, RO, "oldpkt" }, /* 47 */ 543 { CP_SELDISP, RO, "seldisp" }, /* 48 */ 544 { CP_SELBROKEN, RO, "selbroken" }, /* 49 */ 545 { CP_CANDIDATE, RO, "candidate" }, /* 50 */ 546 #ifdef AUTOKEY 547 { CP_FLAGS, RO, "flags" }, /* 1 + CP_MAX_NOAUTOKEY */ 548 { CP_HOST, RO, "host" }, /* 2 + CP_MAX_NOAUTOKEY */ 549 { CP_VALID, RO, "valid" }, /* 3 + CP_MAX_NOAUTOKEY */ 550 { CP_INITSEQ, RO, "initsequence" }, /* 4 + CP_MAX_NOAUTOKEY */ 551 { CP_INITKEY, RO, "initkey" }, /* 5 + CP_MAX_NOAUTOKEY */ 552 { CP_INITTSP, RO, "timestamp" }, /* 6 + CP_MAX_NOAUTOKEY */ 553 { CP_SIGNATURE, RO, "signature" }, /* 7 + CP_MAX_NOAUTOKEY */ 554 { CP_IDENT, RO, "ident" }, /* 8 + CP_MAX_NOAUTOKEY */ 555 #endif /* AUTOKEY */ 556 { 0, EOV, "" } /* 50/58 */ 557 }; 558 559 560 /* 561 * Peer variables we print by default 562 */ 563 static const u_char def_peer_var[] = { 564 CP_SRCADR, 565 CP_SRCPORT, 566 CP_SRCHOST, 567 CP_DSTADR, 568 CP_DSTPORT, 569 CP_OUT, 570 CP_IN, 571 CP_LEAP, 572 CP_STRATUM, 573 CP_PRECISION, 574 CP_ROOTDELAY, 575 CP_ROOTDISPERSION, 576 CP_REFID, 577 CP_REFTIME, 578 CP_REC, 579 CP_REACH, 580 CP_UNREACH, 581 CP_HMODE, 582 CP_PMODE, 583 CP_HPOLL, 584 CP_PPOLL, 585 CP_RATE, 586 CP_FLASH, 587 CP_KEYID, 588 CP_TTL, 589 CP_OFFSET, 590 CP_DELAY, 591 CP_DISPERSION, 592 CP_JITTER, 593 CP_XMT, 594 CP_BIAS, 595 CP_FILTDELAY, 596 CP_FILTOFFSET, 597 CP_FILTERROR, 598 #ifdef AUTOKEY 599 CP_HOST, 600 CP_FLAGS, 601 CP_SIGNATURE, 602 CP_VALID, 603 CP_INITSEQ, 604 CP_IDENT, 605 #endif /* AUTOKEY */ 606 0 607 }; 608 609 610 #ifdef REFCLOCK 611 /* 612 * Clock variable list 613 */ 614 static const struct ctl_var clock_var[] = { 615 { 0, PADDING, "" }, /* 0 */ 616 { CC_TYPE, RO, "type" }, /* 1 */ 617 { CC_TIMECODE, RO, "timecode" }, /* 2 */ 618 { CC_POLL, RO, "poll" }, /* 3 */ 619 { CC_NOREPLY, RO, "noreply" }, /* 4 */ 620 { CC_BADFORMAT, RO, "badformat" }, /* 5 */ 621 { CC_BADDATA, RO, "baddata" }, /* 6 */ 622 { CC_FUDGETIME1, RO, "fudgetime1" }, /* 7 */ 623 { CC_FUDGETIME2, RO, "fudgetime2" }, /* 8 */ 624 { CC_FUDGEVAL1, RO, "stratum" }, /* 9 */ 625 { CC_FUDGEVAL2, RO, "refid" }, /* 10 */ 626 { CC_FLAGS, RO, "flags" }, /* 11 */ 627 { CC_DEVICE, RO, "device" }, /* 12 */ 628 { CC_VARLIST, RO, "clock_var_list" }, /* 13 */ 629 { CC_FUDGEMINJIT, RO, "minjitter" }, /* 14 */ 630 { 0, EOV, "" } /* 15 */ 631 }; 632 633 634 /* 635 * Clock variables printed by default 636 */ 637 static const u_char def_clock_var[] = { 638 CC_DEVICE, 639 CC_TYPE, /* won't be output if device = known */ 640 CC_TIMECODE, 641 CC_POLL, 642 CC_NOREPLY, 643 CC_BADFORMAT, 644 CC_BADDATA, 645 CC_FUDGEMINJIT, 646 CC_FUDGETIME1, 647 CC_FUDGETIME2, 648 CC_FUDGEVAL1, 649 CC_FUDGEVAL2, 650 CC_FLAGS, 651 0 652 }; 653 #endif 654 655 /* 656 * MRU string constants shared by send_mru_entry() and read_mru_list(). 657 */ 658 static const char addr_fmt[] = "addr.%d"; 659 static const char last_fmt[] = "last.%d"; 660 661 /* 662 * System and processor definitions. 663 */ 664 #ifndef HAVE_UNAME 665 # ifndef STR_SYSTEM 666 # define STR_SYSTEM "UNIX" 667 # endif 668 # ifndef STR_PROCESSOR 669 # define STR_PROCESSOR "unknown" 670 # endif 671 672 static const char str_system[] = STR_SYSTEM; 673 static const char str_processor[] = STR_PROCESSOR; 674 #else 675 # include <sys/utsname.h> 676 static struct utsname utsnamebuf; 677 #endif /* HAVE_UNAME */ 678 679 /* 680 * Trap structures. We only allow a few of these, and send a copy of 681 * each async message to each live one. Traps time out after an hour, it 682 * is up to the trap receipient to keep resetting it to avoid being 683 * timed out. 684 */ 685 /* ntp_request.c */ 686 struct ctl_trap ctl_traps[CTL_MAXTRAPS]; 687 int num_ctl_traps; 688 689 /* 690 * Type bits, for ctlsettrap() call. 691 */ 692 #define TRAP_TYPE_CONFIG 0 /* used by configuration code */ 693 #define TRAP_TYPE_PRIO 1 /* priority trap */ 694 #define TRAP_TYPE_NONPRIO 2 /* nonpriority trap */ 695 696 697 /* 698 * List relating reference clock types to control message time sources. 699 * Index by the reference clock type. This list will only be used iff 700 * the reference clock driver doesn't set peer->sstclktype to something 701 * different than CTL_SST_TS_UNSPEC. 702 */ 703 #ifdef REFCLOCK 704 static const u_char clocktypes[] = { 705 CTL_SST_TS_NTP, /* REFCLK_NONE (0) */ 706 CTL_SST_TS_LOCAL, /* REFCLK_LOCALCLOCK (1) */ 707 CTL_SST_TS_UHF, /* deprecated REFCLK_GPS_TRAK (2) */ 708 CTL_SST_TS_HF, /* REFCLK_WWV_PST (3) */ 709 CTL_SST_TS_LF, /* REFCLK_WWVB_SPECTRACOM (4) */ 710 CTL_SST_TS_UHF, /* REFCLK_TRUETIME (5) */ 711 CTL_SST_TS_UHF, /* REFCLK_IRIG_AUDIO (6) */ 712 CTL_SST_TS_HF, /* REFCLK_CHU (7) */ 713 CTL_SST_TS_LF, /* REFCLOCK_PARSE (default) (8) */ 714 CTL_SST_TS_LF, /* REFCLK_GPS_MX4200 (9) */ 715 CTL_SST_TS_UHF, /* REFCLK_GPS_AS2201 (10) */ 716 CTL_SST_TS_UHF, /* REFCLK_GPS_ARBITER (11) */ 717 CTL_SST_TS_UHF, /* REFCLK_IRIG_TPRO (12) */ 718 CTL_SST_TS_ATOM, /* REFCLK_ATOM_LEITCH (13) */ 719 CTL_SST_TS_LF, /* deprecated REFCLK_MSF_EES (14) */ 720 CTL_SST_TS_NTP, /* not used (15) */ 721 CTL_SST_TS_UHF, /* REFCLK_IRIG_BANCOMM (16) */ 722 CTL_SST_TS_UHF, /* REFCLK_GPS_DATU (17) */ 723 CTL_SST_TS_TELEPHONE, /* REFCLK_NIST_ACTS (18) */ 724 CTL_SST_TS_HF, /* REFCLK_WWV_HEATH (19) */ 725 CTL_SST_TS_UHF, /* REFCLK_GPS_NMEA (20) */ 726 CTL_SST_TS_UHF, /* REFCLK_GPS_VME (21) */ 727 CTL_SST_TS_ATOM, /* REFCLK_ATOM_PPS (22) */ 728 CTL_SST_TS_NTP, /* not used (23) */ 729 CTL_SST_TS_NTP, /* not used (24) */ 730 CTL_SST_TS_NTP, /* not used (25) */ 731 CTL_SST_TS_UHF, /* REFCLK_GPS_HP (26) */ 732 CTL_SST_TS_LF, /* REFCLK_ARCRON_MSF (27) */ 733 CTL_SST_TS_UHF, /* REFCLK_SHM (28) */ 734 CTL_SST_TS_UHF, /* REFCLK_PALISADE (29) */ 735 CTL_SST_TS_UHF, /* REFCLK_ONCORE (30) */ 736 CTL_SST_TS_UHF, /* REFCLK_JUPITER (31) */ 737 CTL_SST_TS_LF, /* REFCLK_CHRONOLOG (32) */ 738 CTL_SST_TS_LF, /* REFCLK_DUMBCLOCK (33) */ 739 CTL_SST_TS_LF, /* REFCLK_ULINK (34) */ 740 CTL_SST_TS_LF, /* REFCLK_PCF (35) */ 741 CTL_SST_TS_HF, /* REFCLK_WWV (36) */ 742 CTL_SST_TS_LF, /* REFCLK_FG (37) */ 743 CTL_SST_TS_UHF, /* REFCLK_HOPF_SERIAL (38) */ 744 CTL_SST_TS_UHF, /* REFCLK_HOPF_PCI (39) */ 745 CTL_SST_TS_LF, /* REFCLK_JJY (40) */ 746 CTL_SST_TS_UHF, /* REFCLK_TT560 (41) */ 747 CTL_SST_TS_UHF, /* REFCLK_ZYFER (42) */ 748 CTL_SST_TS_UHF, /* REFCLK_RIPENCC (43) */ 749 CTL_SST_TS_UHF, /* REFCLK_NEOCLOCK4X (44) */ 750 CTL_SST_TS_UHF, /* REFCLK_TSYNCPCI (45) */ 751 CTL_SST_TS_UHF /* REFCLK_GPSDJSON (46) */ 752 }; 753 #endif /* REFCLOCK */ 754 755 756 /* 757 * Keyid used for authenticating write requests. 758 */ 759 keyid_t ctl_auth_keyid; 760 761 /* 762 * We keep track of the last error reported by the system internally 763 */ 764 static u_char ctl_sys_last_event; 765 static u_char ctl_sys_num_events; 766 767 768 /* 769 * Statistic counters to keep track of requests and responses. 770 */ 771 u_long ctltimereset; /* time stats reset */ 772 u_long numctlreq; /* number of requests we've received */ 773 u_long numctlbadpkts; /* number of bad control packets */ 774 u_long numctlresponses; /* number of resp packets sent with data */ 775 u_long numctlfrags; /* number of fragments sent */ 776 u_long numctlerrors; /* number of error responses sent */ 777 u_long numctltooshort; /* number of too short input packets */ 778 u_long numctlinputresp; /* number of responses on input */ 779 u_long numctlinputfrag; /* number of fragments on input */ 780 u_long numctlinputerr; /* number of input pkts with err bit set */ 781 u_long numctlbadoffset; /* number of input pkts with nonzero offset */ 782 u_long numctlbadversion; /* number of input pkts with unknown version */ 783 u_long numctldatatooshort; /* data too short for count */ 784 u_long numctlbadop; /* bad op code found in packet */ 785 u_long numasyncmsgs; /* number of async messages we've sent */ 786 787 /* 788 * Response packet used by these routines. Also some state information 789 * so that we can handle packet formatting within a common set of 790 * subroutines. Note we try to enter data in place whenever possible, 791 * but the need to set the more bit correctly means we occasionally 792 * use the extra buffer and copy. 793 */ 794 static struct ntp_control rpkt; 795 static u_char res_version; 796 static u_char res_opcode; 797 static associd_t res_associd; 798 static u_short res_frags; /* datagrams in this response */ 799 static int res_offset; /* offset of payload in response */ 800 static u_char * datapt; 801 static u_char * dataend; 802 static int datalinelen; 803 static int datasent; /* flag to avoid initial ", " */ 804 static int datanotbinflag; 805 static sockaddr_u *rmt_addr; 806 static endpt *lcl_inter; 807 808 static u_char res_authenticate; 809 static u_char res_authokay; 810 static keyid_t res_keyid; 811 812 #define MAXDATALINELEN (72) 813 814 static u_char res_async; /* sending async trap response? */ 815 816 /* 817 * Pointers for saving state when decoding request packets 818 */ 819 static char *reqpt; 820 static char *reqend; 821 822 /* 823 * init_control - initialize request data 824 */ 825 void 826 init_control(void) 827 { 828 size_t i; 829 830 #ifdef HAVE_UNAME 831 uname(&utsnamebuf); 832 #endif /* HAVE_UNAME */ 833 834 ctl_clr_stats(); 835 836 ctl_auth_keyid = 0; 837 ctl_sys_last_event = EVNT_UNSPEC; 838 ctl_sys_num_events = 0; 839 840 num_ctl_traps = 0; 841 for (i = 0; i < COUNTOF(ctl_traps); i++) 842 ctl_traps[i].tr_flags = 0; 843 } 844 845 846 /* 847 * ctl_error - send an error response for the current request 848 */ 849 static void 850 ctl_error( 851 u_char errcode 852 ) 853 { 854 size_t maclen; 855 856 numctlerrors++; 857 DPRINTF(3, ("sending control error %u\n", errcode)); 858 859 /* 860 * Fill in the fields. We assume rpkt.sequence and rpkt.associd 861 * have already been filled in. 862 */ 863 rpkt.r_m_e_op = (u_char)CTL_RESPONSE | CTL_ERROR | 864 (res_opcode & CTL_OP_MASK); 865 rpkt.status = htons((u_short)(errcode << 8) & 0xff00); 866 rpkt.count = 0; 867 868 /* 869 * send packet and bump counters 870 */ 871 if (res_authenticate && sys_authenticate) { 872 maclen = authencrypt(res_keyid, (u_int32 *)&rpkt, 873 CTL_HEADER_LEN); 874 sendpkt(rmt_addr, lcl_inter, -2, (void *)&rpkt, 875 CTL_HEADER_LEN + maclen); 876 } else 877 sendpkt(rmt_addr, lcl_inter, -3, (void *)&rpkt, 878 CTL_HEADER_LEN); 879 } 880 881 int/*BOOL*/ 882 is_safe_filename(const char * name) 883 { 884 /* We need a strict validation of filenames we should write: The 885 * daemon might run with special permissions and is remote 886 * controllable, so we better take care what we allow as file 887 * name! 888 * 889 * The first character must be digit or a letter from the ASCII 890 * base plane or a '_' ([_A-Za-z0-9]), the following characters 891 * must be from [-._+A-Za-z0-9]. 892 * 893 * We do not trust the character classification much here: Since 894 * the NTP protocol makes no provisions for UTF-8 or local code 895 * pages, we strictly require the 7bit ASCII code page. 896 * 897 * The following table is a packed bit field of 128 two-bit 898 * groups. The LSB in each group tells us if a character is 899 * acceptable at the first position, the MSB if the character is 900 * accepted at any other position. 901 * 902 * This does not ensure that the file name is syntactically 903 * correct (multiple dots will not work with VMS...) but it will 904 * exclude potential globbing bombs and directory traversal. It 905 * also rules out drive selection. (For systems that have this 906 * notion, like Windows or VMS.) 907 */ 908 static const uint32_t chclass[8] = { 909 0x00000000, 0x00000000, 910 0x28800000, 0x000FFFFF, 911 0xFFFFFFFC, 0xC03FFFFF, 912 0xFFFFFFFC, 0x003FFFFF 913 }; 914 915 u_int widx, bidx, mask; 916 if ( ! (name && *name)) 917 return FALSE; 918 919 mask = 1u; 920 while (0 != (widx = (u_char)*name++)) { 921 bidx = (widx & 15) << 1; 922 widx = widx >> 4; 923 if (widx >= sizeof(chclass)/sizeof(chclass[0])) 924 return FALSE; 925 if (0 == ((chclass[widx] >> bidx) & mask)) 926 return FALSE; 927 mask = 2u; 928 } 929 return TRUE; 930 } 931 932 933 /* 934 * save_config - Implements ntpq -c "saveconfig <filename>" 935 * Writes current configuration including any runtime 936 * changes by ntpq's :config or config-from-file 937 * 938 * Note: There should be no buffer overflow or truncation in the 939 * processing of file names -- both cause security problems. This is bit 940 * painful to code but essential here. 941 */ 942 void 943 save_config( 944 struct recvbuf *rbufp, 945 int restrict_mask 946 ) 947 { 948 /* block directory traversal by searching for characters that 949 * indicate directory components in a file path. 950 * 951 * Conceptually we should be searching for DIRSEP in filename, 952 * however Windows actually recognizes both forward and 953 * backslashes as equivalent directory separators at the API 954 * level. On POSIX systems we could allow '\\' but such 955 * filenames are tricky to manipulate from a shell, so just 956 * reject both types of slashes on all platforms. 957 */ 958 /* TALOS-CAN-0062: block directory traversal for VMS, too */ 959 static const char * illegal_in_filename = 960 #if defined(VMS) 961 ":[]" /* do not allow drive and path components here */ 962 #elif defined(SYS_WINNT) 963 ":\\/" /* path and drive separators */ 964 #else 965 "\\/" /* separator and critical char for POSIX */ 966 #endif 967 ; 968 char reply[128]; 969 #ifdef SAVECONFIG 970 static const char savedconfig_eq[] = "savedconfig="; 971 972 /* Build a safe open mode from the available mode flags. We want 973 * to create a new file and write it in text mode (when 974 * applicable -- only Windows does this...) 975 */ 976 static const int openmode = O_CREAT | O_TRUNC | O_WRONLY 977 # if defined(O_EXCL) /* posix, vms */ 978 | O_EXCL 979 # elif defined(_O_EXCL) /* windows is alway very special... */ 980 | _O_EXCL 981 # endif 982 # if defined(_O_TEXT) /* windows, again */ 983 | _O_TEXT 984 #endif 985 ; 986 987 char filespec[128]; 988 char filename[128]; 989 char fullpath[512]; 990 char savedconfig[sizeof(savedconfig_eq) + sizeof(filename)]; 991 time_t now; 992 int fd; 993 FILE *fptr; 994 int prc; 995 size_t reqlen; 996 #endif 997 998 if (RES_NOMODIFY & restrict_mask) { 999 ctl_printf("%s", "saveconfig prohibited by restrict ... nomodify"); 1000 ctl_flushpkt(0); 1001 NLOG(NLOG_SYSINFO) 1002 msyslog(LOG_NOTICE, 1003 "saveconfig from %s rejected due to nomodify restriction", 1004 stoa(&rbufp->recv_srcadr)); 1005 sys_restricted++; 1006 return; 1007 } 1008 1009 #ifdef SAVECONFIG 1010 if (NULL == saveconfigdir) { 1011 ctl_printf("%s", "saveconfig prohibited, no saveconfigdir configured"); 1012 ctl_flushpkt(0); 1013 NLOG(NLOG_SYSINFO) 1014 msyslog(LOG_NOTICE, 1015 "saveconfig from %s rejected, no saveconfigdir", 1016 stoa(&rbufp->recv_srcadr)); 1017 return; 1018 } 1019 1020 /* The length checking stuff gets serious. Do not assume a NUL 1021 * byte can be found, but if so, use it to calculate the needed 1022 * buffer size. If the available buffer is too short, bail out; 1023 * likewise if there is no file spec. (The latter will not 1024 * happen when using NTPQ, but there are other ways to craft a 1025 * network packet!) 1026 */ 1027 reqlen = (size_t)(reqend - reqpt); 1028 if (0 != reqlen) { 1029 char * nulpos = (char*)memchr(reqpt, 0, reqlen); 1030 if (NULL != nulpos) 1031 reqlen = (size_t)(nulpos - reqpt); 1032 } 1033 if (0 == reqlen) 1034 return; 1035 if (reqlen >= sizeof(filespec)) { 1036 ctl_printf("saveconfig exceeded maximum raw name length (%u)", 1037 (u_int)sizeof(filespec)); 1038 ctl_flushpkt(0); 1039 msyslog(LOG_NOTICE, 1040 "saveconfig exceeded maximum raw name length from %s", 1041 stoa(&rbufp->recv_srcadr)); 1042 return; 1043 } 1044 1045 /* copy data directly as we exactly know the size */ 1046 memcpy(filespec, reqpt, reqlen); 1047 filespec[reqlen] = '\0'; 1048 1049 /* 1050 * allow timestamping of the saved config filename with 1051 * strftime() format such as: 1052 * ntpq -c "saveconfig ntp-%Y%m%d-%H%M%S.conf" 1053 * XXX: Nice feature, but not too safe. 1054 * YYY: The check for permitted characters in file names should 1055 * weed out the worst. Let's hope 'strftime()' does not 1056 * develop pathological problems. 1057 */ 1058 time(&now); 1059 if (0 == strftime(filename, sizeof(filename), filespec, 1060 localtime(&now))) 1061 { 1062 /* 1063 * If we arrive here, 'strftime()' balked; most likely 1064 * the buffer was too short. (Or it encounterd an empty 1065 * format, or just a format that expands to an empty 1066 * string.) We try to use the original name, though this 1067 * is very likely to fail later if there are format 1068 * specs in the string. Note that truncation cannot 1069 * happen here as long as both buffers have the same 1070 * size! 1071 */ 1072 strlcpy(filename, filespec, sizeof(filename)); 1073 } 1074 1075 /* 1076 * Check the file name for sanity. This might/will rule out file 1077 * names that would be legal but problematic, and it blocks 1078 * directory traversal. 1079 */ 1080 if (!is_safe_filename(filename)) { 1081 ctl_printf("saveconfig rejects unsafe file name '%s'", 1082 filename); 1083 ctl_flushpkt(0); 1084 msyslog(LOG_NOTICE, 1085 "saveconfig rejects unsafe file name from %s", 1086 stoa(&rbufp->recv_srcadr)); 1087 return; 1088 } 1089 1090 /* 1091 * XXX: This next test may not be needed with is_safe_filename() 1092 */ 1093 1094 /* block directory/drive traversal */ 1095 /* TALOS-CAN-0062: block directory traversal for VMS, too */ 1096 if (NULL != strpbrk(filename, illegal_in_filename)) { 1097 snprintf(reply, sizeof(reply), 1098 "saveconfig does not allow directory in filename"); 1099 ctl_putdata(reply, strlen(reply), 0); 1100 ctl_flushpkt(0); 1101 msyslog(LOG_NOTICE, 1102 "saveconfig rejects unsafe file name from %s", 1103 stoa(&rbufp->recv_srcadr)); 1104 return; 1105 } 1106 1107 /* concatenation of directory and path can cause another 1108 * truncation... 1109 */ 1110 prc = snprintf(fullpath, sizeof(fullpath), "%s%s", 1111 saveconfigdir, filename); 1112 if (prc < 0 || (size_t)prc >= sizeof(fullpath)) { 1113 ctl_printf("saveconfig exceeded maximum path length (%u)", 1114 (u_int)sizeof(fullpath)); 1115 ctl_flushpkt(0); 1116 msyslog(LOG_NOTICE, 1117 "saveconfig exceeded maximum path length from %s", 1118 stoa(&rbufp->recv_srcadr)); 1119 return; 1120 } 1121 1122 fd = open(fullpath, openmode, S_IRUSR | S_IWUSR); 1123 if (-1 == fd) 1124 fptr = NULL; 1125 else 1126 fptr = fdopen(fd, "w"); 1127 1128 if (NULL == fptr || -1 == dump_all_config_trees(fptr, 1)) { 1129 ctl_printf("Unable to save configuration to file '%s': %s", 1130 filename, strerror(errno)); 1131 msyslog(LOG_ERR, 1132 "saveconfig %s from %s failed", filename, 1133 stoa(&rbufp->recv_srcadr)); 1134 } else { 1135 ctl_printf("Configuration saved to '%s'", filename); 1136 msyslog(LOG_NOTICE, 1137 "Configuration saved to '%s' (requested by %s)", 1138 fullpath, stoa(&rbufp->recv_srcadr)); 1139 /* 1140 * save the output filename in system variable 1141 * savedconfig, retrieved with: 1142 * ntpq -c "rv 0 savedconfig" 1143 * Note: the way 'savedconfig' is defined makes overflow 1144 * checks unnecessary here. 1145 */ 1146 snprintf(savedconfig, sizeof(savedconfig), "%s%s", 1147 savedconfig_eq, filename); 1148 set_sys_var(savedconfig, strlen(savedconfig) + 1, RO); 1149 } 1150 1151 if (NULL != fptr) 1152 fclose(fptr); 1153 #else /* !SAVECONFIG follows */ 1154 ctl_printf("%s", 1155 "saveconfig unavailable, configured with --disable-saveconfig"); 1156 #endif 1157 ctl_flushpkt(0); 1158 } 1159 1160 1161 /* 1162 * process_control - process an incoming control message 1163 */ 1164 void 1165 process_control( 1166 struct recvbuf *rbufp, 1167 int restrict_mask 1168 ) 1169 { 1170 struct ntp_control *pkt; 1171 int req_count; 1172 int req_data; 1173 const struct ctl_proc *cc; 1174 keyid_t *pkid; 1175 int properlen; 1176 size_t maclen; 1177 1178 DPRINTF(3, ("in process_control()\n")); 1179 1180 /* 1181 * Save the addresses for error responses 1182 */ 1183 numctlreq++; 1184 rmt_addr = &rbufp->recv_srcadr; 1185 lcl_inter = rbufp->dstadr; 1186 pkt = (struct ntp_control *)&rbufp->recv_pkt; 1187 1188 /* 1189 * If the length is less than required for the header, 1190 * ignore it. 1191 */ 1192 if (rbufp->recv_length < (int)CTL_HEADER_LEN) { 1193 DPRINTF(1, ("Short control packet\n")); 1194 numctltooshort++; 1195 return; 1196 } 1197 1198 /* 1199 * If this packet is a response or a fragment, ignore it. 1200 */ 1201 if ( (CTL_RESPONSE | CTL_MORE | CTL_ERROR) & pkt->r_m_e_op 1202 || pkt->offset != 0) { 1203 DPRINTF(1, ("invalid format in control packet\n")); 1204 if (CTL_RESPONSE & pkt->r_m_e_op) 1205 numctlinputresp++; 1206 if (CTL_MORE & pkt->r_m_e_op) 1207 numctlinputfrag++; 1208 if (CTL_ERROR & pkt->r_m_e_op) 1209 numctlinputerr++; 1210 if (pkt->offset != 0) 1211 numctlbadoffset++; 1212 return; 1213 } 1214 1215 res_version = PKT_VERSION(pkt->li_vn_mode); 1216 if (res_version > NTP_VERSION || res_version < NTP_OLDVERSION) { 1217 DPRINTF(1, ("unknown version %d in control packet\n", 1218 res_version)); 1219 numctlbadversion++; 1220 return; 1221 } 1222 1223 /* 1224 * Pull enough data from the packet to make intelligent 1225 * responses 1226 */ 1227 rpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, res_version, 1228 MODE_CONTROL); 1229 res_opcode = pkt->r_m_e_op; 1230 rpkt.sequence = pkt->sequence; 1231 rpkt.associd = pkt->associd; 1232 rpkt.status = 0; 1233 res_frags = 1; 1234 res_offset = 0; 1235 res_associd = htons(pkt->associd); 1236 res_async = FALSE; 1237 res_authenticate = FALSE; 1238 res_keyid = 0; 1239 res_authokay = FALSE; 1240 req_count = (int)ntohs(pkt->count); 1241 datanotbinflag = FALSE; 1242 datalinelen = 0; 1243 datasent = 0; 1244 datapt = rpkt.u.data; 1245 dataend = &rpkt.u.data[CTL_MAX_DATA_LEN]; 1246 1247 if ((rbufp->recv_length & 0x3) != 0) 1248 DPRINTF(3, ("Control packet length %d unrounded\n", 1249 rbufp->recv_length)); 1250 1251 /* 1252 * We're set up now. Make sure we've got at least enough 1253 * incoming data space to match the count. 1254 */ 1255 req_data = rbufp->recv_length - CTL_HEADER_LEN; 1256 if (req_data < req_count || rbufp->recv_length & 0x3) { 1257 ctl_error(CERR_BADFMT); 1258 numctldatatooshort++; 1259 return; 1260 } 1261 1262 properlen = req_count + CTL_HEADER_LEN; 1263 /* round up proper len to a 8 octet boundary */ 1264 1265 properlen = (properlen + 7) & ~7; 1266 maclen = rbufp->recv_length - properlen; 1267 if ((rbufp->recv_length & 3) == 0 && 1268 maclen >= MIN_MAC_LEN && maclen <= MAX_MAC_LEN && 1269 sys_authenticate) { 1270 res_authenticate = TRUE; 1271 pkid = (void *)((char *)pkt + properlen); 1272 res_keyid = ntohl(*pkid); 1273 DPRINTF(3, ("recv_len %d, properlen %d, wants auth with keyid %08x, MAC length=%zu\n", 1274 rbufp->recv_length, properlen, res_keyid, 1275 maclen)); 1276 1277 if (!authistrustedip(res_keyid, &rbufp->recv_srcadr)) 1278 DPRINTF(3, ("invalid keyid %08x\n", res_keyid)); 1279 else if (authdecrypt(res_keyid, (u_int32 *)pkt, 1280 rbufp->recv_length - maclen, 1281 maclen)) { 1282 res_authokay = TRUE; 1283 DPRINTF(3, ("authenticated okay\n")); 1284 } else { 1285 res_keyid = 0; 1286 DPRINTF(3, ("authentication failed\n")); 1287 } 1288 } 1289 1290 /* 1291 * Set up translate pointers 1292 */ 1293 reqpt = (char *)pkt->u.data; 1294 reqend = reqpt + req_count; 1295 1296 /* 1297 * Look for the opcode processor 1298 */ 1299 for (cc = control_codes; cc->control_code != NO_REQUEST; cc++) { 1300 if (cc->control_code == res_opcode) { 1301 DPRINTF(3, ("opcode %d, found command handler\n", 1302 res_opcode)); 1303 if (cc->flags == AUTH 1304 && (!res_authokay 1305 || res_keyid != ctl_auth_keyid)) { 1306 ctl_error(CERR_PERMISSION); 1307 return; 1308 } 1309 (cc->handler)(rbufp, restrict_mask); 1310 return; 1311 } 1312 } 1313 1314 /* 1315 * Can't find this one, return an error. 1316 */ 1317 numctlbadop++; 1318 ctl_error(CERR_BADOP); 1319 return; 1320 } 1321 1322 1323 /* 1324 * ctlpeerstatus - return a status word for this peer 1325 */ 1326 u_short 1327 ctlpeerstatus( 1328 register struct peer *p 1329 ) 1330 { 1331 u_short status; 1332 1333 status = p->status; 1334 if (FLAG_CONFIG & p->flags) 1335 status |= CTL_PST_CONFIG; 1336 if (p->keyid) 1337 status |= CTL_PST_AUTHENABLE; 1338 if (FLAG_AUTHENTIC & p->flags) 1339 status |= CTL_PST_AUTHENTIC; 1340 if (p->reach) 1341 status |= CTL_PST_REACH; 1342 if (MDF_TXONLY_MASK & p->cast_flags) 1343 status |= CTL_PST_BCAST; 1344 1345 return CTL_PEER_STATUS(status, p->num_events, p->last_event); 1346 } 1347 1348 1349 /* 1350 * ctlclkstatus - return a status word for this clock 1351 */ 1352 #ifdef REFCLOCK 1353 static u_short 1354 ctlclkstatus( 1355 struct refclockstat *pcs 1356 ) 1357 { 1358 return CTL_PEER_STATUS(0, pcs->lastevent, pcs->currentstatus); 1359 } 1360 #endif 1361 1362 1363 /* 1364 * ctlsysstatus - return the system status word 1365 */ 1366 u_short 1367 ctlsysstatus(void) 1368 { 1369 register u_char this_clock; 1370 1371 this_clock = CTL_SST_TS_UNSPEC; 1372 #ifdef REFCLOCK 1373 if (sys_peer != NULL) { 1374 if (CTL_SST_TS_UNSPEC != sys_peer->sstclktype) 1375 this_clock = sys_peer->sstclktype; 1376 else if (sys_peer->refclktype < COUNTOF(clocktypes)) 1377 this_clock = clocktypes[sys_peer->refclktype]; 1378 } 1379 #else /* REFCLOCK */ 1380 if (sys_peer != 0) 1381 this_clock = CTL_SST_TS_NTP; 1382 #endif /* REFCLOCK */ 1383 return CTL_SYS_STATUS(sys_leap, this_clock, ctl_sys_num_events, 1384 ctl_sys_last_event); 1385 } 1386 1387 1388 /* 1389 * ctl_flushpkt - write out the current packet and prepare 1390 * another if necessary. 1391 */ 1392 static void 1393 ctl_flushpkt( 1394 u_char more 1395 ) 1396 { 1397 size_t i; 1398 size_t dlen; 1399 size_t sendlen; 1400 size_t maclen; 1401 size_t totlen; 1402 keyid_t keyid; 1403 1404 dlen = datapt - rpkt.u.data; 1405 if (!more && datanotbinflag && dlen + 2 < CTL_MAX_DATA_LEN) { 1406 /* 1407 * Big hack, output a trailing \r\n 1408 */ 1409 *datapt++ = '\r'; 1410 *datapt++ = '\n'; 1411 dlen += 2; 1412 } 1413 sendlen = dlen + CTL_HEADER_LEN; 1414 1415 /* 1416 * Pad to a multiple of 32 bits 1417 */ 1418 while (sendlen & 0x3) { 1419 *datapt++ = '\0'; 1420 sendlen++; 1421 } 1422 1423 /* 1424 * Fill in the packet with the current info 1425 */ 1426 rpkt.r_m_e_op = CTL_RESPONSE | more | 1427 (res_opcode & CTL_OP_MASK); 1428 rpkt.count = htons((u_short)dlen); 1429 rpkt.offset = htons((u_short)res_offset); 1430 if (res_async) { 1431 for (i = 0; i < COUNTOF(ctl_traps); i++) { 1432 if (TRAP_INUSE & ctl_traps[i].tr_flags) { 1433 rpkt.li_vn_mode = 1434 PKT_LI_VN_MODE( 1435 sys_leap, 1436 ctl_traps[i].tr_version, 1437 MODE_CONTROL); 1438 rpkt.sequence = 1439 htons(ctl_traps[i].tr_sequence); 1440 sendpkt(&ctl_traps[i].tr_addr, 1441 ctl_traps[i].tr_localaddr, -4, 1442 (struct pkt *)&rpkt, sendlen); 1443 if (!more) 1444 ctl_traps[i].tr_sequence++; 1445 numasyncmsgs++; 1446 } 1447 } 1448 } else { 1449 if (res_authenticate && sys_authenticate) { 1450 totlen = sendlen; 1451 /* 1452 * If we are going to authenticate, then there 1453 * is an additional requirement that the MAC 1454 * begin on a 64 bit boundary. 1455 */ 1456 while (totlen & 7) { 1457 *datapt++ = '\0'; 1458 totlen++; 1459 } 1460 keyid = htonl(res_keyid); 1461 memcpy(datapt, &keyid, sizeof(keyid)); 1462 maclen = authencrypt(res_keyid, 1463 (u_int32 *)&rpkt, totlen); 1464 sendpkt(rmt_addr, lcl_inter, -5, 1465 (struct pkt *)&rpkt, totlen + maclen); 1466 } else { 1467 sendpkt(rmt_addr, lcl_inter, -6, 1468 (struct pkt *)&rpkt, sendlen); 1469 } 1470 if (more) 1471 numctlfrags++; 1472 else 1473 numctlresponses++; 1474 } 1475 1476 /* 1477 * Set us up for another go around. 1478 */ 1479 res_frags++; 1480 res_offset += dlen; 1481 datapt = rpkt.u.data; 1482 } 1483 1484 1485 /* -------------------------------------------------------------------- 1486 * block transfer API -- stream string/data fragments into xmit buffer 1487 * without additional copying 1488 */ 1489 1490 /* buffer descriptor: address & size of fragment 1491 * 'buf' may only be NULL when 'len' is zero! 1492 */ 1493 typedef struct { 1494 const void *buf; 1495 size_t len; 1496 } CtlMemBufT; 1497 1498 /* put ctl data in a gather-style operation */ 1499 static void 1500 ctl_putdata_ex( 1501 const CtlMemBufT * argv, 1502 size_t argc, 1503 int/*BOOL*/ bin /* set to 1 when data is binary */ 1504 ) 1505 { 1506 const char * src_ptr; 1507 size_t src_len, cur_len, add_len, argi; 1508 1509 /* text / binary preprocessing, possibly create new linefeed */ 1510 if (bin) { 1511 add_len = 0; 1512 } else { 1513 datanotbinflag = TRUE; 1514 add_len = 3; 1515 1516 if (datasent) { 1517 *datapt++ = ','; 1518 datalinelen++; 1519 1520 /* sum up total length */ 1521 for (argi = 0, src_len = 0; argi < argc; ++argi) 1522 src_len += argv[argi].len; 1523 /* possibly start a new line, assume no size_t overflow */ 1524 if ((src_len + datalinelen + 1) >= MAXDATALINELEN) { 1525 *datapt++ = '\r'; 1526 *datapt++ = '\n'; 1527 datalinelen = 0; 1528 } else { 1529 *datapt++ = ' '; 1530 datalinelen++; 1531 } 1532 } 1533 } 1534 1535 /* now stream out all buffers */ 1536 for (argi = 0; argi < argc; ++argi) { 1537 src_ptr = argv[argi].buf; 1538 src_len = argv[argi].len; 1539 1540 if ( ! (src_ptr && src_len)) 1541 continue; 1542 1543 cur_len = (size_t)(dataend - datapt); 1544 while ((src_len + add_len) > cur_len) { 1545 /* Not enough room in this one, flush it out. */ 1546 if (src_len < cur_len) 1547 cur_len = src_len; 1548 1549 memcpy(datapt, src_ptr, cur_len); 1550 datapt += cur_len; 1551 datalinelen += cur_len; 1552 1553 src_ptr += cur_len; 1554 src_len -= cur_len; 1555 1556 ctl_flushpkt(CTL_MORE); 1557 cur_len = (size_t)(dataend - datapt); 1558 } 1559 1560 memcpy(datapt, src_ptr, src_len); 1561 datapt += src_len; 1562 datalinelen += src_len; 1563 1564 datasent = TRUE; 1565 } 1566 } 1567 1568 /* 1569 * ctl_putdata - write data into the packet, fragmenting and starting 1570 * another if this one is full. 1571 */ 1572 static void 1573 ctl_putdata( 1574 const char *dp, 1575 unsigned int dlen, 1576 int bin /* set to 1 when data is binary */ 1577 ) 1578 { 1579 CtlMemBufT args[1]; 1580 1581 args[0].buf = dp; 1582 args[0].len = dlen; 1583 ctl_putdata_ex(args, 1, bin); 1584 } 1585 1586 /* 1587 * ctl_putstr - write a tagged string into the response packet 1588 * in the form: 1589 * 1590 * tag="data" 1591 * 1592 * len is the data length excluding the NUL terminator, 1593 * as in ctl_putstr("var", "value", strlen("value")); 1594 */ 1595 static void 1596 ctl_putstr( 1597 const char * tag, 1598 const char * data, 1599 size_t len 1600 ) 1601 { 1602 CtlMemBufT args[4]; 1603 1604 args[0].buf = tag; 1605 args[0].len = strlen(tag); 1606 if (data && len) { 1607 args[1].buf = "=\""; 1608 args[1].len = 2; 1609 args[2].buf = data; 1610 args[2].len = len; 1611 args[3].buf = "\""; 1612 args[3].len = 1; 1613 ctl_putdata_ex(args, 4, FALSE); 1614 } else { 1615 args[1].buf = "=\"\""; 1616 args[1].len = 3; 1617 ctl_putdata_ex(args, 2, FALSE); 1618 } 1619 } 1620 1621 1622 /* 1623 * ctl_putunqstr - write a tagged string into the response packet 1624 * in the form: 1625 * 1626 * tag=data 1627 * 1628 * len is the data length excluding the NUL terminator. 1629 * data must not contain a comma or whitespace. 1630 */ 1631 static void 1632 ctl_putunqstr( 1633 const char * tag, 1634 const char * data, 1635 size_t len 1636 ) 1637 { 1638 CtlMemBufT args[3]; 1639 1640 args[0].buf = tag; 1641 args[0].len = strlen(tag); 1642 args[1].buf = "="; 1643 args[1].len = 1; 1644 if (data && len) { 1645 args[2].buf = data; 1646 args[2].len = len; 1647 ctl_putdata_ex(args, 3, FALSE); 1648 } else { 1649 ctl_putdata_ex(args, 2, FALSE); 1650 } 1651 } 1652 1653 1654 /* 1655 * ctl_putdblf - write a tagged, signed double into the response packet 1656 */ 1657 static void 1658 ctl_putdblf( 1659 const char * tag, 1660 int use_f, 1661 int precision, 1662 double d 1663 ) 1664 { 1665 char buffer[40]; 1666 int rc; 1667 1668 rc = snprintf(buffer, sizeof(buffer), 1669 (use_f ? "%.*f" : "%.*g"), 1670 precision, d); 1671 INSIST(rc >= 0 && (size_t)rc < sizeof(buffer)); 1672 ctl_putunqstr(tag, buffer, rc); 1673 } 1674 1675 /* 1676 * ctl_putuint - write a tagged unsigned integer into the response 1677 */ 1678 static void 1679 ctl_putuint( 1680 const char *tag, 1681 u_long uval 1682 ) 1683 { 1684 char buffer[24]; /* needs to fit for 64 bits! */ 1685 int rc; 1686 1687 rc = snprintf(buffer, sizeof(buffer), "%lu", uval); 1688 INSIST(rc >= 0 && (size_t)rc < sizeof(buffer)); 1689 ctl_putunqstr(tag, buffer, rc); 1690 } 1691 1692 /* 1693 * ctl_putcal - write a decoded calendar data into the response. 1694 * only used with AUTOKEY currently, so compiled conditional 1695 */ 1696 #ifdef AUTOKEY 1697 static void 1698 ctl_putcal( 1699 const char *tag, 1700 const struct calendar *pcal 1701 ) 1702 { 1703 char buffer[16]; 1704 int rc; 1705 1706 rc = snprintf(buffer, sizeof(buffer), 1707 "%04d%02d%02d%02d%02d", 1708 pcal->year, pcal->month, pcal->monthday, 1709 pcal->hour, pcal->minute 1710 ); 1711 INSIST(rc >= 0 && (size_t)rc < sizeof(buffer)); 1712 ctl_putunqstr(tag, buffer, rc); 1713 } 1714 #endif 1715 1716 /* 1717 * ctl_putfs - write a decoded filestamp into the response 1718 */ 1719 static void 1720 ctl_putfs( 1721 const char *tag, 1722 tstamp_t uval 1723 ) 1724 { 1725 char buffer[16]; 1726 int rc; 1727 1728 time_t fstamp = (time_t)uval - JAN_1970; 1729 struct tm *tm = gmtime(&fstamp); 1730 1731 if (NULL == tm) 1732 return; 1733 1734 rc = snprintf(buffer, sizeof(buffer), 1735 "%04d%02d%02d%02d%02d", 1736 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 1737 tm->tm_hour, tm->tm_min); 1738 INSIST(rc >= 0 && (size_t)rc < sizeof(buffer)); 1739 ctl_putunqstr(tag, buffer, rc); 1740 } 1741 1742 1743 /* 1744 * ctl_puthex - write a tagged unsigned integer, in hex, into the 1745 * response 1746 */ 1747 static void 1748 ctl_puthex( 1749 const char *tag, 1750 u_long uval 1751 ) 1752 { 1753 char buffer[24]; /* must fit 64bit int! */ 1754 int rc; 1755 1756 rc = snprintf(buffer, sizeof(buffer), "0x%lx", uval); 1757 INSIST(rc >= 0 && (size_t)rc < sizeof(buffer)); 1758 ctl_putunqstr(tag, buffer, rc); 1759 } 1760 1761 1762 /* 1763 * ctl_putint - write a tagged signed integer into the response 1764 */ 1765 static void 1766 ctl_putint( 1767 const char *tag, 1768 long ival 1769 ) 1770 { 1771 char buffer[24]; /*must fit 64bit int */ 1772 int rc; 1773 1774 rc = snprintf(buffer, sizeof(buffer), "%ld", ival); 1775 INSIST(rc >= 0 && (size_t)rc < sizeof(buffer)); 1776 ctl_putunqstr(tag, buffer, rc); 1777 } 1778 1779 1780 /* 1781 * ctl_putts - write a tagged timestamp, in hex, into the response 1782 */ 1783 static void 1784 ctl_putts( 1785 const char *tag, 1786 l_fp *ts 1787 ) 1788 { 1789 char buffer[24]; 1790 int rc; 1791 1792 rc = snprintf(buffer, sizeof(buffer), 1793 "0x%08lx.%08lx", 1794 (u_long)ts->l_ui, (u_long)ts->l_uf); 1795 INSIST(rc >= 0 && (size_t)rc < sizeof(buffer)); 1796 ctl_putunqstr(tag, buffer, rc); 1797 } 1798 1799 1800 /* 1801 * ctl_putadr - write an IP address into the response 1802 */ 1803 static void 1804 ctl_putadr( 1805 const char *tag, 1806 u_int32 addr32, 1807 sockaddr_u *addr 1808 ) 1809 { 1810 const char *cq; 1811 1812 if (NULL == addr) 1813 cq = numtoa(addr32); 1814 else 1815 cq = stoa(addr); 1816 ctl_putunqstr(tag, cq, strlen(cq)); 1817 } 1818 1819 1820 /* 1821 * ctl_putrefid - send a u_int32 refid as printable text 1822 */ 1823 static void 1824 ctl_putrefid( 1825 const char * tag, 1826 u_int32 refid 1827 ) 1828 { 1829 size_t nc; 1830 1831 union { 1832 uint32_t w; 1833 uint8_t b[sizeof(uint32_t)]; 1834 } bytes; 1835 1836 bytes.w = refid; 1837 for (nc = 0; nc < sizeof(bytes.b) && bytes.b[nc]; ++nc) 1838 if ( !isprint(bytes.b[nc]) 1839 || isspace(bytes.b[nc]) 1840 || bytes.b[nc] == ',' ) 1841 bytes.b[nc] = '.'; 1842 ctl_putunqstr(tag, (const char*)bytes.b, nc); 1843 } 1844 1845 1846 /* 1847 * ctl_putarray - write a tagged eight element double array into the response 1848 */ 1849 static void 1850 ctl_putarray( 1851 const char *tag, 1852 double *arr, 1853 int start 1854 ) 1855 { 1856 char *cp, *ep; 1857 char buffer[200]; 1858 int i, rc; 1859 1860 cp = buffer; 1861 ep = buffer + sizeof(buffer); 1862 i = start; 1863 do { 1864 if (i == 0) 1865 i = NTP_SHIFT; 1866 i--; 1867 rc = snprintf(cp, (size_t)(ep - cp), " %.2f", arr[i] * 1e3); 1868 INSIST(rc >= 0 && (size_t)rc < (size_t)(ep - cp)); 1869 cp += rc; 1870 } while (i != start); 1871 ctl_putunqstr(tag, buffer, (size_t)(cp - buffer)); 1872 } 1873 1874 /* 1875 * ctl_printf - put a formatted string into the data buffer 1876 */ 1877 static void 1878 ctl_printf( 1879 const char * fmt, 1880 ... 1881 ) 1882 { 1883 static const char * ellipsis = "[...]"; 1884 va_list va; 1885 char fmtbuf[128]; 1886 int rc; 1887 1888 va_start(va, fmt); 1889 rc = vsnprintf(fmtbuf, sizeof(fmtbuf), fmt, va); 1890 va_end(va); 1891 if (rc < 0 || (size_t)rc >= sizeof(fmtbuf)) 1892 strcpy(fmtbuf + sizeof(fmtbuf) - strlen(ellipsis) - 1, 1893 ellipsis); 1894 ctl_putdata(fmtbuf, strlen(fmtbuf), 0); 1895 } 1896 1897 1898 /* 1899 * ctl_putsys - output a system variable 1900 */ 1901 static void 1902 ctl_putsys( 1903 int varid 1904 ) 1905 { 1906 l_fp tmp; 1907 char str[256]; 1908 u_int u; 1909 double kb; 1910 double dtemp; 1911 const char *ss; 1912 #ifdef AUTOKEY 1913 struct cert_info *cp; 1914 #endif /* AUTOKEY */ 1915 #ifdef KERNEL_PLL 1916 static struct timex ntx; 1917 static u_long ntp_adjtime_time; 1918 1919 /* 1920 * CS_K_* variables depend on up-to-date output of ntp_adjtime() 1921 */ 1922 if (CS_KERN_FIRST <= varid && varid <= CS_KERN_LAST && 1923 current_time != ntp_adjtime_time) { 1924 ZERO(ntx); 1925 if (ntp_adjtime(&ntx) < 0) 1926 msyslog(LOG_ERR, "ntp_adjtime() for mode 6 query failed: %m"); 1927 else 1928 ntp_adjtime_time = current_time; 1929 } 1930 #endif /* KERNEL_PLL */ 1931 1932 switch (varid) { 1933 1934 case CS_LEAP: 1935 ctl_putuint(sys_var[CS_LEAP].text, sys_leap); 1936 break; 1937 1938 case CS_STRATUM: 1939 ctl_putuint(sys_var[CS_STRATUM].text, sys_stratum); 1940 break; 1941 1942 case CS_PRECISION: 1943 ctl_putint(sys_var[CS_PRECISION].text, sys_precision); 1944 break; 1945 1946 case CS_ROOTDELAY: 1947 ctl_putdbl(sys_var[CS_ROOTDELAY].text, sys_rootdelay * 1948 1e3); 1949 break; 1950 1951 case CS_ROOTDISPERSION: 1952 ctl_putdbl(sys_var[CS_ROOTDISPERSION].text, 1953 sys_rootdisp * 1e3); 1954 break; 1955 1956 case CS_REFID: 1957 if (REFID_ISTEXT(sys_stratum)) 1958 ctl_putrefid(sys_var[varid].text, sys_refid); 1959 else 1960 ctl_putadr(sys_var[varid].text, sys_refid, NULL); 1961 break; 1962 1963 case CS_REFTIME: 1964 ctl_putts(sys_var[CS_REFTIME].text, &sys_reftime); 1965 break; 1966 1967 case CS_POLL: 1968 ctl_putuint(sys_var[CS_POLL].text, sys_poll); 1969 break; 1970 1971 case CS_PEERID: 1972 if (sys_peer == NULL) 1973 ctl_putuint(sys_var[CS_PEERID].text, 0); 1974 else 1975 ctl_putuint(sys_var[CS_PEERID].text, 1976 sys_peer->associd); 1977 break; 1978 1979 case CS_PEERADR: 1980 if (sys_peer != NULL && sys_peer->dstadr != NULL) 1981 ss = sptoa(&sys_peer->srcadr); 1982 else 1983 ss = "0.0.0.0:0"; 1984 ctl_putunqstr(sys_var[CS_PEERADR].text, ss, strlen(ss)); 1985 break; 1986 1987 case CS_PEERMODE: 1988 u = (sys_peer != NULL) 1989 ? sys_peer->hmode 1990 : MODE_UNSPEC; 1991 ctl_putuint(sys_var[CS_PEERMODE].text, u); 1992 break; 1993 1994 case CS_OFFSET: 1995 ctl_putdbl6(sys_var[CS_OFFSET].text, last_offset * 1e3); 1996 break; 1997 1998 case CS_DRIFT: 1999 ctl_putdbl(sys_var[CS_DRIFT].text, drift_comp * 1e6); 2000 break; 2001 2002 case CS_JITTER: 2003 ctl_putdbl6(sys_var[CS_JITTER].text, sys_jitter * 1e3); 2004 break; 2005 2006 case CS_ERROR: 2007 ctl_putdbl(sys_var[CS_ERROR].text, clock_jitter * 1e3); 2008 break; 2009 2010 case CS_CLOCK: 2011 get_systime(&tmp); 2012 ctl_putts(sys_var[CS_CLOCK].text, &tmp); 2013 break; 2014 2015 case CS_PROCESSOR: 2016 #ifndef HAVE_UNAME 2017 ctl_putstr(sys_var[CS_PROCESSOR].text, str_processor, 2018 sizeof(str_processor) - 1); 2019 #else 2020 ctl_putstr(sys_var[CS_PROCESSOR].text, 2021 utsnamebuf.machine, strlen(utsnamebuf.machine)); 2022 #endif /* HAVE_UNAME */ 2023 break; 2024 2025 case CS_SYSTEM: 2026 #ifndef HAVE_UNAME 2027 ctl_putstr(sys_var[CS_SYSTEM].text, str_system, 2028 sizeof(str_system) - 1); 2029 #else 2030 snprintf(str, sizeof(str), "%s/%s", utsnamebuf.sysname, 2031 utsnamebuf.release); 2032 ctl_putstr(sys_var[CS_SYSTEM].text, str, strlen(str)); 2033 #endif /* HAVE_UNAME */ 2034 break; 2035 2036 case CS_VERSION: 2037 ctl_putstr(sys_var[CS_VERSION].text, Version, 2038 strlen(Version)); 2039 break; 2040 2041 case CS_STABIL: 2042 ctl_putdbl(sys_var[CS_STABIL].text, clock_stability * 2043 1e6); 2044 break; 2045 2046 case CS_VARLIST: 2047 { 2048 char buf[CTL_MAX_DATA_LEN]; 2049 //buffPointer, firstElementPointer, buffEndPointer 2050 char *buffp, *buffend; 2051 int firstVarName; 2052 const char *ss1; 2053 int len; 2054 const struct ctl_var *k; 2055 2056 buffp = buf; 2057 buffend = buf + sizeof(buf); 2058 if (strlen(sys_var[CS_VARLIST].text) > (sizeof(buf) - 4)) 2059 break; /* really long var name */ 2060 2061 snprintf(buffp, sizeof(buf), "%s=\"",sys_var[CS_VARLIST].text); 2062 buffp += strlen(buffp); 2063 firstVarName = TRUE; 2064 for (k = sys_var; !(k->flags & EOV); k++) { 2065 if (k->flags & PADDING) 2066 continue; 2067 len = strlen(k->text); 2068 if (len + 1 >= buffend - buffp) 2069 break; 2070 if (!firstVarName) 2071 *buffp++ = ','; 2072 else 2073 firstVarName = FALSE; 2074 memcpy(buffp, k->text, len); 2075 buffp += len; 2076 } 2077 2078 for (k = ext_sys_var; k && !(k->flags & EOV); k++) { 2079 if (k->flags & PADDING) 2080 continue; 2081 if (NULL == k->text) 2082 continue; 2083 ss1 = strchr(k->text, '='); 2084 if (NULL == ss1) 2085 len = strlen(k->text); 2086 else 2087 len = ss1 - k->text; 2088 if (len + 1 >= buffend - buffp) 2089 break; 2090 if (firstVarName) { 2091 *buffp++ = ','; 2092 firstVarName = FALSE; 2093 } 2094 memcpy(buffp, k->text,(unsigned)len); 2095 buffp += len; 2096 } 2097 if (2 >= buffend - buffp) 2098 break; 2099 2100 *buffp++ = '"'; 2101 *buffp = '\0'; 2102 2103 ctl_putdata(buf, (unsigned)( buffp - buf ), 0); 2104 break; 2105 } 2106 2107 case CS_TAI: 2108 if (sys_tai > 0) 2109 ctl_putuint(sys_var[CS_TAI].text, sys_tai); 2110 break; 2111 2112 case CS_LEAPTAB: 2113 { 2114 leap_signature_t lsig; 2115 leapsec_getsig(&lsig); 2116 if (lsig.ttime > 0) 2117 ctl_putfs(sys_var[CS_LEAPTAB].text, lsig.ttime); 2118 break; 2119 } 2120 2121 case CS_LEAPEND: 2122 { 2123 leap_signature_t lsig; 2124 leapsec_getsig(&lsig); 2125 if (lsig.etime > 0) 2126 ctl_putfs(sys_var[CS_LEAPEND].text, lsig.etime); 2127 break; 2128 } 2129 2130 #ifdef LEAP_SMEAR 2131 case CS_LEAPSMEARINTV: 2132 if (leap_smear_intv > 0) 2133 ctl_putuint(sys_var[CS_LEAPSMEARINTV].text, leap_smear_intv); 2134 break; 2135 2136 case CS_LEAPSMEAROFFS: 2137 if (leap_smear_intv > 0) 2138 ctl_putdbl(sys_var[CS_LEAPSMEAROFFS].text, 2139 leap_smear.doffset * 1e3); 2140 break; 2141 #endif /* LEAP_SMEAR */ 2142 2143 case CS_RATE: 2144 ctl_putuint(sys_var[CS_RATE].text, ntp_minpoll); 2145 break; 2146 2147 case CS_MRU_ENABLED: 2148 ctl_puthex(sys_var[varid].text, mon_enabled); 2149 break; 2150 2151 case CS_MRU_DEPTH: 2152 ctl_putuint(sys_var[varid].text, mru_entries); 2153 break; 2154 2155 case CS_MRU_MEM: 2156 kb = mru_entries * (sizeof(mon_entry) / 1024.); 2157 u = (u_int)kb; 2158 if (kb - u >= 0.5) 2159 u++; 2160 ctl_putuint(sys_var[varid].text, u); 2161 break; 2162 2163 case CS_MRU_DEEPEST: 2164 ctl_putuint(sys_var[varid].text, mru_peakentries); 2165 break; 2166 2167 case CS_MRU_MINDEPTH: 2168 ctl_putuint(sys_var[varid].text, mru_mindepth); 2169 break; 2170 2171 case CS_MRU_MAXAGE: 2172 ctl_putint(sys_var[varid].text, mru_maxage); 2173 break; 2174 2175 case CS_MRU_MAXDEPTH: 2176 ctl_putuint(sys_var[varid].text, mru_maxdepth); 2177 break; 2178 2179 case CS_MRU_MAXMEM: 2180 kb = mru_maxdepth * (sizeof(mon_entry) / 1024.); 2181 u = (u_int)kb; 2182 if (kb - u >= 0.5) 2183 u++; 2184 ctl_putuint(sys_var[varid].text, u); 2185 break; 2186 2187 case CS_SS_UPTIME: 2188 ctl_putuint(sys_var[varid].text, current_time); 2189 break; 2190 2191 case CS_SS_RESET: 2192 ctl_putuint(sys_var[varid].text, 2193 current_time - sys_stattime); 2194 break; 2195 2196 case CS_SS_RECEIVED: 2197 ctl_putuint(sys_var[varid].text, sys_received); 2198 break; 2199 2200 case CS_SS_THISVER: 2201 ctl_putuint(sys_var[varid].text, sys_newversion); 2202 break; 2203 2204 case CS_SS_OLDVER: 2205 ctl_putuint(sys_var[varid].text, sys_oldversion); 2206 break; 2207 2208 case CS_SS_BADFORMAT: 2209 ctl_putuint(sys_var[varid].text, sys_badlength); 2210 break; 2211 2212 case CS_SS_BADAUTH: 2213 ctl_putuint(sys_var[varid].text, sys_badauth); 2214 break; 2215 2216 case CS_SS_DECLINED: 2217 ctl_putuint(sys_var[varid].text, sys_declined); 2218 break; 2219 2220 case CS_SS_RESTRICTED: 2221 ctl_putuint(sys_var[varid].text, sys_restricted); 2222 break; 2223 2224 case CS_SS_LIMITED: 2225 ctl_putuint(sys_var[varid].text, sys_limitrejected); 2226 break; 2227 2228 case CS_SS_LAMPORT: 2229 ctl_putuint(sys_var[varid].text, sys_lamport); 2230 break; 2231 2232 case CS_SS_TSROUNDING: 2233 ctl_putuint(sys_var[varid].text, sys_tsrounding); 2234 break; 2235 2236 case CS_SS_KODSENT: 2237 ctl_putuint(sys_var[varid].text, sys_kodsent); 2238 break; 2239 2240 case CS_SS_PROCESSED: 2241 ctl_putuint(sys_var[varid].text, sys_processed); 2242 break; 2243 2244 case CS_BCASTDELAY: 2245 ctl_putdbl(sys_var[varid].text, sys_bdelay * 1e3); 2246 break; 2247 2248 case CS_AUTHDELAY: 2249 LFPTOD(&sys_authdelay, dtemp); 2250 ctl_putdbl(sys_var[varid].text, dtemp * 1e3); 2251 break; 2252 2253 case CS_AUTHKEYS: 2254 ctl_putuint(sys_var[varid].text, authnumkeys); 2255 break; 2256 2257 case CS_AUTHFREEK: 2258 ctl_putuint(sys_var[varid].text, authnumfreekeys); 2259 break; 2260 2261 case CS_AUTHKLOOKUPS: 2262 ctl_putuint(sys_var[varid].text, authkeylookups); 2263 break; 2264 2265 case CS_AUTHKNOTFOUND: 2266 ctl_putuint(sys_var[varid].text, authkeynotfound); 2267 break; 2268 2269 case CS_AUTHKUNCACHED: 2270 ctl_putuint(sys_var[varid].text, authkeyuncached); 2271 break; 2272 2273 case CS_AUTHKEXPIRED: 2274 ctl_putuint(sys_var[varid].text, authkeyexpired); 2275 break; 2276 2277 case CS_AUTHENCRYPTS: 2278 ctl_putuint(sys_var[varid].text, authencryptions); 2279 break; 2280 2281 case CS_AUTHDECRYPTS: 2282 ctl_putuint(sys_var[varid].text, authdecryptions); 2283 break; 2284 2285 case CS_AUTHRESET: 2286 ctl_putuint(sys_var[varid].text, 2287 current_time - auth_timereset); 2288 break; 2289 2290 /* 2291 * CTL_IF_KERNLOOP() puts a zero if the kernel loop is 2292 * unavailable, otherwise calls putfunc with args. 2293 */ 2294 #ifndef KERNEL_PLL 2295 # define CTL_IF_KERNLOOP(putfunc, args) \ 2296 ctl_putint(sys_var[varid].text, 0) 2297 #else 2298 # define CTL_IF_KERNLOOP(putfunc, args) \ 2299 putfunc args 2300 #endif 2301 2302 /* 2303 * CTL_IF_KERNPPS() puts a zero if either the kernel 2304 * loop is unavailable, or kernel hard PPS is not 2305 * active, otherwise calls putfunc with args. 2306 */ 2307 #ifndef KERNEL_PLL 2308 # define CTL_IF_KERNPPS(putfunc, args) \ 2309 ctl_putint(sys_var[varid].text, 0) 2310 #else 2311 # define CTL_IF_KERNPPS(putfunc, args) \ 2312 if (0 == ntx.shift) \ 2313 ctl_putint(sys_var[varid].text, 0); \ 2314 else \ 2315 putfunc args /* no trailing ; */ 2316 #endif 2317 2318 case CS_K_OFFSET: 2319 CTL_IF_KERNLOOP( 2320 ctl_putdblf, 2321 (sys_var[varid].text, 0, -1, 2322 1000 * dbl_from_var_long(ntx.offset, ntx.status)) 2323 ); 2324 break; 2325 2326 case CS_K_FREQ: 2327 CTL_IF_KERNLOOP( 2328 ctl_putsfp, 2329 (sys_var[varid].text, ntx.freq) 2330 ); 2331 break; 2332 2333 case CS_K_MAXERR: 2334 CTL_IF_KERNLOOP( 2335 ctl_putdblf, 2336 (sys_var[varid].text, 0, 6, 2337 1000 * dbl_from_usec_long(ntx.maxerror)) 2338 ); 2339 break; 2340 2341 case CS_K_ESTERR: 2342 CTL_IF_KERNLOOP( 2343 ctl_putdblf, 2344 (sys_var[varid].text, 0, 6, 2345 1000 * dbl_from_usec_long(ntx.esterror)) 2346 ); 2347 break; 2348 2349 case CS_K_STFLAGS: 2350 #ifndef KERNEL_PLL 2351 ss = ""; 2352 #else 2353 ss = k_st_flags(ntx.status); 2354 #endif 2355 ctl_putstr(sys_var[varid].text, ss, strlen(ss)); 2356 break; 2357 2358 case CS_K_TIMECONST: 2359 CTL_IF_KERNLOOP( 2360 ctl_putint, 2361 (sys_var[varid].text, ntx.constant) 2362 ); 2363 break; 2364 2365 case CS_K_PRECISION: 2366 CTL_IF_KERNLOOP( 2367 ctl_putdblf, 2368 (sys_var[varid].text, 0, 6, 2369 1000 * dbl_from_var_long(ntx.precision, ntx.status)) 2370 ); 2371 break; 2372 2373 case CS_K_FREQTOL: 2374 CTL_IF_KERNLOOP( 2375 ctl_putsfp, 2376 (sys_var[varid].text, ntx.tolerance) 2377 ); 2378 break; 2379 2380 case CS_K_PPS_FREQ: 2381 CTL_IF_KERNPPS( 2382 ctl_putsfp, 2383 (sys_var[varid].text, ntx.ppsfreq) 2384 ); 2385 break; 2386 2387 case CS_K_PPS_STABIL: 2388 CTL_IF_KERNPPS( 2389 ctl_putsfp, 2390 (sys_var[varid].text, ntx.stabil) 2391 ); 2392 break; 2393 2394 case CS_K_PPS_JITTER: 2395 CTL_IF_KERNPPS( 2396 ctl_putdbl, 2397 (sys_var[varid].text, 2398 1000 * dbl_from_var_long(ntx.jitter, ntx.status)) 2399 ); 2400 break; 2401 2402 case CS_K_PPS_CALIBDUR: 2403 CTL_IF_KERNPPS( 2404 ctl_putint, 2405 (sys_var[varid].text, 1 << ntx.shift) 2406 ); 2407 break; 2408 2409 case CS_K_PPS_CALIBS: 2410 CTL_IF_KERNPPS( 2411 ctl_putint, 2412 (sys_var[varid].text, ntx.calcnt) 2413 ); 2414 break; 2415 2416 case CS_K_PPS_CALIBERRS: 2417 CTL_IF_KERNPPS( 2418 ctl_putint, 2419 (sys_var[varid].text, ntx.errcnt) 2420 ); 2421 break; 2422 2423 case CS_K_PPS_JITEXC: 2424 CTL_IF_KERNPPS( 2425 ctl_putint, 2426 (sys_var[varid].text, ntx.jitcnt) 2427 ); 2428 break; 2429 2430 case CS_K_PPS_STBEXC: 2431 CTL_IF_KERNPPS( 2432 ctl_putint, 2433 (sys_var[varid].text, ntx.stbcnt) 2434 ); 2435 break; 2436 2437 case CS_IOSTATS_RESET: 2438 ctl_putuint(sys_var[varid].text, 2439 current_time - io_timereset); 2440 break; 2441 2442 case CS_TOTAL_RBUF: 2443 ctl_putuint(sys_var[varid].text, total_recvbuffs()); 2444 break; 2445 2446 case CS_FREE_RBUF: 2447 ctl_putuint(sys_var[varid].text, free_recvbuffs()); 2448 break; 2449 2450 case CS_USED_RBUF: 2451 ctl_putuint(sys_var[varid].text, full_recvbuffs()); 2452 break; 2453 2454 case CS_RBUF_LOWATER: 2455 ctl_putuint(sys_var[varid].text, lowater_additions()); 2456 break; 2457 2458 case CS_IO_DROPPED: 2459 ctl_putuint(sys_var[varid].text, packets_dropped); 2460 break; 2461 2462 case CS_IO_IGNORED: 2463 ctl_putuint(sys_var[varid].text, packets_ignored); 2464 break; 2465 2466 case CS_IO_RECEIVED: 2467 ctl_putuint(sys_var[varid].text, packets_received); 2468 break; 2469 2470 case CS_IO_SENT: 2471 ctl_putuint(sys_var[varid].text, packets_sent); 2472 break; 2473 2474 case CS_IO_SENDFAILED: 2475 ctl_putuint(sys_var[varid].text, packets_notsent); 2476 break; 2477 2478 case CS_IO_WAKEUPS: 2479 ctl_putuint(sys_var[varid].text, handler_calls); 2480 break; 2481 2482 case CS_IO_GOODWAKEUPS: 2483 ctl_putuint(sys_var[varid].text, handler_pkts); 2484 break; 2485 2486 case CS_TIMERSTATS_RESET: 2487 ctl_putuint(sys_var[varid].text, 2488 current_time - timer_timereset); 2489 break; 2490 2491 case CS_TIMER_OVERRUNS: 2492 ctl_putuint(sys_var[varid].text, alarm_overflow); 2493 break; 2494 2495 case CS_TIMER_XMTS: 2496 ctl_putuint(sys_var[varid].text, timer_xmtcalls); 2497 break; 2498 2499 case CS_FUZZ: 2500 ctl_putdbl(sys_var[varid].text, sys_fuzz * 1e3); 2501 break; 2502 case CS_WANDER_THRESH: 2503 ctl_putdbl(sys_var[varid].text, wander_threshold * 1e6); 2504 break; 2505 #ifdef AUTOKEY 2506 case CS_FLAGS: 2507 if (crypto_flags) 2508 ctl_puthex(sys_var[CS_FLAGS].text, 2509 crypto_flags); 2510 break; 2511 2512 case CS_DIGEST: 2513 if (crypto_flags) { 2514 strlcpy(str, OBJ_nid2ln(crypto_nid), 2515 COUNTOF(str)); 2516 ctl_putstr(sys_var[CS_DIGEST].text, str, 2517 strlen(str)); 2518 } 2519 break; 2520 2521 case CS_SIGNATURE: 2522 if (crypto_flags) { 2523 const EVP_MD *dp; 2524 2525 dp = EVP_get_digestbynid(crypto_flags >> 16); 2526 strlcpy(str, OBJ_nid2ln(EVP_MD_pkey_type(dp)), 2527 COUNTOF(str)); 2528 ctl_putstr(sys_var[CS_SIGNATURE].text, str, 2529 strlen(str)); 2530 } 2531 break; 2532 2533 case CS_HOST: 2534 if (hostval.ptr != NULL) 2535 ctl_putstr(sys_var[CS_HOST].text, hostval.ptr, 2536 strlen(hostval.ptr)); 2537 break; 2538 2539 case CS_IDENT: 2540 if (sys_ident != NULL) 2541 ctl_putstr(sys_var[CS_IDENT].text, sys_ident, 2542 strlen(sys_ident)); 2543 break; 2544 2545 case CS_CERTIF: 2546 for (cp = cinfo; cp != NULL; cp = cp->link) { 2547 snprintf(str, sizeof(str), "%s %s 0x%x", 2548 cp->subject, cp->issuer, cp->flags); 2549 ctl_putstr(sys_var[CS_CERTIF].text, str, 2550 strlen(str)); 2551 ctl_putcal(sys_var[CS_REVTIME].text, &(cp->last)); 2552 } 2553 break; 2554 2555 case CS_PUBLIC: 2556 if (hostval.tstamp != 0) 2557 ctl_putfs(sys_var[CS_PUBLIC].text, 2558 ntohl(hostval.tstamp)); 2559 break; 2560 #endif /* AUTOKEY */ 2561 2562 default: 2563 break; 2564 } 2565 } 2566 2567 2568 /* 2569 * ctl_putpeer - output a peer variable 2570 */ 2571 static void 2572 ctl_putpeer( 2573 int id, 2574 struct peer *p 2575 ) 2576 { 2577 char buf[CTL_MAX_DATA_LEN]; 2578 char *s; 2579 char *t; 2580 char *be; 2581 int i; 2582 const struct ctl_var *k; 2583 #ifdef AUTOKEY 2584 struct autokey *ap; 2585 const EVP_MD *dp; 2586 const char *str; 2587 #endif /* AUTOKEY */ 2588 2589 switch (id) { 2590 2591 case CP_CONFIG: 2592 ctl_putuint(peer_var[id].text, 2593 !(FLAG_PREEMPT & p->flags)); 2594 break; 2595 2596 case CP_AUTHENABLE: 2597 ctl_putuint(peer_var[id].text, !(p->keyid)); 2598 break; 2599 2600 case CP_AUTHENTIC: 2601 ctl_putuint(peer_var[id].text, 2602 !!(FLAG_AUTHENTIC & p->flags)); 2603 break; 2604 2605 case CP_SRCADR: 2606 ctl_putadr(peer_var[id].text, 0, &p->srcadr); 2607 break; 2608 2609 case CP_SRCPORT: 2610 ctl_putuint(peer_var[id].text, SRCPORT(&p->srcadr)); 2611 break; 2612 2613 case CP_SRCHOST: 2614 if (p->hostname != NULL) 2615 ctl_putstr(peer_var[id].text, p->hostname, 2616 strlen(p->hostname)); 2617 break; 2618 2619 case CP_DSTADR: 2620 ctl_putadr(peer_var[id].text, 0, 2621 (p->dstadr != NULL) 2622 ? &p->dstadr->sin 2623 : NULL); 2624 break; 2625 2626 case CP_DSTPORT: 2627 ctl_putuint(peer_var[id].text, 2628 (p->dstadr != NULL) 2629 ? SRCPORT(&p->dstadr->sin) 2630 : 0); 2631 break; 2632 2633 case CP_IN: 2634 if (p->r21 > 0.) 2635 ctl_putdbl(peer_var[id].text, p->r21 / 1e3); 2636 break; 2637 2638 case CP_OUT: 2639 if (p->r34 > 0.) 2640 ctl_putdbl(peer_var[id].text, p->r34 / 1e3); 2641 break; 2642 2643 case CP_RATE: 2644 ctl_putuint(peer_var[id].text, p->throttle); 2645 break; 2646 2647 case CP_LEAP: 2648 ctl_putuint(peer_var[id].text, p->leap); 2649 break; 2650 2651 case CP_HMODE: 2652 ctl_putuint(peer_var[id].text, p->hmode); 2653 break; 2654 2655 case CP_STRATUM: 2656 ctl_putuint(peer_var[id].text, p->stratum); 2657 break; 2658 2659 case CP_PPOLL: 2660 ctl_putuint(peer_var[id].text, p->ppoll); 2661 break; 2662 2663 case CP_HPOLL: 2664 ctl_putuint(peer_var[id].text, p->hpoll); 2665 break; 2666 2667 case CP_PRECISION: 2668 ctl_putint(peer_var[id].text, p->precision); 2669 break; 2670 2671 case CP_ROOTDELAY: 2672 ctl_putdbl(peer_var[id].text, p->rootdelay * 1e3); 2673 break; 2674 2675 case CP_ROOTDISPERSION: 2676 ctl_putdbl(peer_var[id].text, p->rootdisp * 1e3); 2677 break; 2678 2679 case CP_REFID: 2680 #ifdef REFCLOCK 2681 if (p->flags & FLAG_REFCLOCK) { 2682 ctl_putrefid(peer_var[id].text, p->refid); 2683 break; 2684 } 2685 #endif 2686 if (REFID_ISTEXT(p->stratum)) 2687 ctl_putrefid(peer_var[id].text, p->refid); 2688 else 2689 ctl_putadr(peer_var[id].text, p->refid, NULL); 2690 break; 2691 2692 case CP_REFTIME: 2693 ctl_putts(peer_var[id].text, &p->reftime); 2694 break; 2695 2696 case CP_ORG: 2697 ctl_putts(peer_var[id].text, &p->aorg); 2698 break; 2699 2700 case CP_REC: 2701 ctl_putts(peer_var[id].text, &p->dst); 2702 break; 2703 2704 case CP_XMT: 2705 if (p->xleave) 2706 ctl_putdbl(peer_var[id].text, p->xleave * 1e3); 2707 break; 2708 2709 case CP_BIAS: 2710 if (p->bias != 0.) 2711 ctl_putdbl(peer_var[id].text, p->bias * 1e3); 2712 break; 2713 2714 case CP_REACH: 2715 ctl_puthex(peer_var[id].text, p->reach); 2716 break; 2717 2718 case CP_FLASH: 2719 ctl_puthex(peer_var[id].text, p->flash); 2720 break; 2721 2722 case CP_TTL: 2723 #ifdef REFCLOCK 2724 if (p->flags & FLAG_REFCLOCK) { 2725 ctl_putuint(peer_var[id].text, p->ttl); 2726 break; 2727 } 2728 #endif 2729 if (p->ttl > 0 && p->ttl < COUNTOF(sys_ttl)) 2730 ctl_putint(peer_var[id].text, 2731 sys_ttl[p->ttl]); 2732 break; 2733 2734 case CP_UNREACH: 2735 ctl_putuint(peer_var[id].text, p->unreach); 2736 break; 2737 2738 case CP_TIMER: 2739 ctl_putuint(peer_var[id].text, 2740 p->nextdate - current_time); 2741 break; 2742 2743 case CP_DELAY: 2744 ctl_putdbl(peer_var[id].text, p->delay * 1e3); 2745 break; 2746 2747 case CP_OFFSET: 2748 ctl_putdbl(peer_var[id].text, p->offset * 1e3); 2749 break; 2750 2751 case CP_JITTER: 2752 ctl_putdbl(peer_var[id].text, p->jitter * 1e3); 2753 break; 2754 2755 case CP_DISPERSION: 2756 ctl_putdbl(peer_var[id].text, p->disp * 1e3); 2757 break; 2758 2759 case CP_KEYID: 2760 if (p->keyid > NTP_MAXKEY) 2761 ctl_puthex(peer_var[id].text, p->keyid); 2762 else 2763 ctl_putuint(peer_var[id].text, p->keyid); 2764 break; 2765 2766 case CP_FILTDELAY: 2767 ctl_putarray(peer_var[id].text, p->filter_delay, 2768 p->filter_nextpt); 2769 break; 2770 2771 case CP_FILTOFFSET: 2772 ctl_putarray(peer_var[id].text, p->filter_offset, 2773 p->filter_nextpt); 2774 break; 2775 2776 case CP_FILTERROR: 2777 ctl_putarray(peer_var[id].text, p->filter_disp, 2778 p->filter_nextpt); 2779 break; 2780 2781 case CP_PMODE: 2782 ctl_putuint(peer_var[id].text, p->pmode); 2783 break; 2784 2785 case CP_RECEIVED: 2786 ctl_putuint(peer_var[id].text, p->received); 2787 break; 2788 2789 case CP_SENT: 2790 ctl_putuint(peer_var[id].text, p->sent); 2791 break; 2792 2793 case CP_VARLIST: 2794 s = buf; 2795 be = buf + sizeof(buf); 2796 if (strlen(peer_var[id].text) + 4 > sizeof(buf)) 2797 break; /* really long var name */ 2798 2799 snprintf(s, sizeof(buf), "%s=\"", peer_var[id].text); 2800 s += strlen(s); 2801 t = s; 2802 for (k = peer_var; !(EOV & k->flags); k++) { 2803 if (PADDING & k->flags) 2804 continue; 2805 i = strlen(k->text); 2806 if (s + i + 1 >= be) 2807 break; 2808 if (s != t) 2809 *s++ = ','; 2810 memcpy(s, k->text, i); 2811 s += i; 2812 } 2813 if (s + 2 < be) { 2814 *s++ = '"'; 2815 *s = '\0'; 2816 ctl_putdata(buf, (u_int)(s - buf), 0); 2817 } 2818 break; 2819 2820 case CP_TIMEREC: 2821 ctl_putuint(peer_var[id].text, 2822 current_time - p->timereceived); 2823 break; 2824 2825 case CP_TIMEREACH: 2826 ctl_putuint(peer_var[id].text, 2827 current_time - p->timereachable); 2828 break; 2829 2830 case CP_BADAUTH: 2831 ctl_putuint(peer_var[id].text, p->badauth); 2832 break; 2833 2834 case CP_BOGUSORG: 2835 ctl_putuint(peer_var[id].text, p->bogusorg); 2836 break; 2837 2838 case CP_OLDPKT: 2839 ctl_putuint(peer_var[id].text, p->oldpkt); 2840 break; 2841 2842 case CP_SELDISP: 2843 ctl_putuint(peer_var[id].text, p->seldisptoolarge); 2844 break; 2845 2846 case CP_SELBROKEN: 2847 ctl_putuint(peer_var[id].text, p->selbroken); 2848 break; 2849 2850 case CP_CANDIDATE: 2851 ctl_putuint(peer_var[id].text, p->status); 2852 break; 2853 #ifdef AUTOKEY 2854 case CP_FLAGS: 2855 if (p->crypto) 2856 ctl_puthex(peer_var[id].text, p->crypto); 2857 break; 2858 2859 case CP_SIGNATURE: 2860 if (p->crypto) { 2861 dp = EVP_get_digestbynid(p->crypto >> 16); 2862 str = OBJ_nid2ln(EVP_MD_pkey_type(dp)); 2863 ctl_putstr(peer_var[id].text, str, strlen(str)); 2864 } 2865 break; 2866 2867 case CP_HOST: 2868 if (p->subject != NULL) 2869 ctl_putstr(peer_var[id].text, p->subject, 2870 strlen(p->subject)); 2871 break; 2872 2873 case CP_VALID: /* not used */ 2874 break; 2875 2876 case CP_INITSEQ: 2877 if (NULL == (ap = p->recval.ptr)) 2878 break; 2879 2880 ctl_putint(peer_var[CP_INITSEQ].text, ap->seq); 2881 ctl_puthex(peer_var[CP_INITKEY].text, ap->key); 2882 ctl_putfs(peer_var[CP_INITTSP].text, 2883 ntohl(p->recval.tstamp)); 2884 break; 2885 2886 case CP_IDENT: 2887 if (p->ident != NULL) 2888 ctl_putstr(peer_var[id].text, p->ident, 2889 strlen(p->ident)); 2890 break; 2891 2892 2893 #endif /* AUTOKEY */ 2894 } 2895 } 2896 2897 2898 #ifdef REFCLOCK 2899 /* 2900 * ctl_putclock - output clock variables 2901 */ 2902 static void 2903 ctl_putclock( 2904 int id, 2905 struct refclockstat *pcs, 2906 int mustput 2907 ) 2908 { 2909 char buf[CTL_MAX_DATA_LEN]; 2910 char *s, *t, *be; 2911 const char *ss; 2912 int i; 2913 const struct ctl_var *k; 2914 2915 switch (id) { 2916 2917 case CC_TYPE: 2918 if (mustput || pcs->clockdesc == NULL 2919 || *(pcs->clockdesc) == '\0') { 2920 ctl_putuint(clock_var[id].text, pcs->type); 2921 } 2922 break; 2923 case CC_TIMECODE: 2924 ctl_putstr(clock_var[id].text, 2925 pcs->p_lastcode, 2926 (unsigned)pcs->lencode); 2927 break; 2928 2929 case CC_POLL: 2930 ctl_putuint(clock_var[id].text, pcs->polls); 2931 break; 2932 2933 case CC_NOREPLY: 2934 ctl_putuint(clock_var[id].text, 2935 pcs->noresponse); 2936 break; 2937 2938 case CC_BADFORMAT: 2939 ctl_putuint(clock_var[id].text, 2940 pcs->badformat); 2941 break; 2942 2943 case CC_BADDATA: 2944 ctl_putuint(clock_var[id].text, 2945 pcs->baddata); 2946 break; 2947 2948 case CC_FUDGETIME1: 2949 if (mustput || (pcs->haveflags & CLK_HAVETIME1)) 2950 ctl_putdbl(clock_var[id].text, 2951 pcs->fudgetime1 * 1e3); 2952 break; 2953 2954 case CC_FUDGETIME2: 2955 if (mustput || (pcs->haveflags & CLK_HAVETIME2)) 2956 ctl_putdbl(clock_var[id].text, 2957 pcs->fudgetime2 * 1e3); 2958 break; 2959 2960 case CC_FUDGEVAL1: 2961 if (mustput || (pcs->haveflags & CLK_HAVEVAL1)) 2962 ctl_putint(clock_var[id].text, 2963 pcs->fudgeval1); 2964 break; 2965 2966 case CC_FUDGEVAL2: 2967 /* RefID of clocks are always text even if stratum is fudged */ 2968 if (mustput || (pcs->haveflags & CLK_HAVEVAL2)) 2969 ctl_putrefid(clock_var[id].text, pcs->fudgeval2); 2970 break; 2971 2972 case CC_FLAGS: 2973 ctl_putuint(clock_var[id].text, pcs->flags); 2974 break; 2975 2976 case CC_DEVICE: 2977 if (pcs->clockdesc == NULL || 2978 *(pcs->clockdesc) == '\0') { 2979 if (mustput) 2980 ctl_putstr(clock_var[id].text, 2981 "", 0); 2982 } else { 2983 ctl_putstr(clock_var[id].text, 2984 pcs->clockdesc, 2985 strlen(pcs->clockdesc)); 2986 } 2987 break; 2988 2989 case CC_VARLIST: 2990 s = buf; 2991 be = buf + sizeof(buf); 2992 if (strlen(clock_var[CC_VARLIST].text) + 4 > 2993 sizeof(buf)) 2994 break; /* really long var name */ 2995 2996 snprintf(s, sizeof(buf), "%s=\"", 2997 clock_var[CC_VARLIST].text); 2998 s += strlen(s); 2999 t = s; 3000 3001 for (k = clock_var; !(EOV & k->flags); k++) { 3002 if (PADDING & k->flags) 3003 continue; 3004 3005 i = strlen(k->text); 3006 if (s + i + 1 >= be) 3007 break; 3008 3009 if (s != t) 3010 *s++ = ','; 3011 memcpy(s, k->text, i); 3012 s += i; 3013 } 3014 3015 for (k = pcs->kv_list; k && !(EOV & k->flags); k++) { 3016 if (PADDING & k->flags) 3017 continue; 3018 3019 ss = k->text; 3020 if (NULL == ss) 3021 continue; 3022 3023 while (*ss && *ss != '=') 3024 ss++; 3025 i = ss - k->text; 3026 if (s + i + 1 >= be) 3027 break; 3028 3029 if (s != t) 3030 *s++ = ','; 3031 memcpy(s, k->text, (unsigned)i); 3032 s += i; 3033 *s = '\0'; 3034 } 3035 if (s + 2 >= be) 3036 break; 3037 3038 *s++ = '"'; 3039 *s = '\0'; 3040 ctl_putdata(buf, (unsigned)(s - buf), 0); 3041 break; 3042 3043 case CC_FUDGEMINJIT: 3044 if (mustput || (pcs->haveflags & CLK_HAVEMINJIT)) 3045 ctl_putdbl(clock_var[id].text, 3046 pcs->fudgeminjitter * 1e3); 3047 break; 3048 3049 default: 3050 break; 3051 3052 } 3053 } 3054 #endif 3055 3056 3057 3058 /* 3059 * ctl_getitem - get the next data item from the incoming packet 3060 */ 3061 static const struct ctl_var * 3062 ctl_getitem( 3063 const struct ctl_var *var_list, 3064 char **data 3065 ) 3066 { 3067 /* [Bug 3008] First check the packet data sanity, then search 3068 * the key. This improves the consistency of result values: If 3069 * the result is NULL once, it will never be EOV again for this 3070 * packet; If it's EOV, it will never be NULL again until the 3071 * variable is found and processed in a given 'var_list'. (That 3072 * is, a result is returned that is neither NULL nor EOV). 3073 */ 3074 static const struct ctl_var eol = { 0, EOV, NULL }; 3075 static char buf[128]; 3076 static u_long quiet_until; 3077 const struct ctl_var *v; 3078 char *cp; 3079 char *tp; 3080 3081 /* 3082 * Part One: Validate the packet state 3083 */ 3084 3085 /* Delete leading commas and white space */ 3086 while (reqpt < reqend && (*reqpt == ',' || 3087 isspace((unsigned char)*reqpt))) 3088 reqpt++; 3089 if (reqpt >= reqend) 3090 return NULL; 3091 3092 /* Scan the string in the packet until we hit comma or 3093 * EoB. Register position of first '=' on the fly. */ 3094 for (tp = NULL, cp = reqpt; cp != reqend; ++cp) { 3095 if (*cp == '=' && tp == NULL) 3096 tp = cp; 3097 if (*cp == ',') 3098 break; 3099 } 3100 3101 /* Process payload, if any. */ 3102 *data = NULL; 3103 if (NULL != tp) { 3104 /* eventually strip white space from argument. */ 3105 const char *plhead = tp + 1; /* skip the '=' */ 3106 const char *pltail = cp; 3107 size_t plsize; 3108 3109 while (plhead != pltail && isspace((u_char)plhead[0])) 3110 ++plhead; 3111 while (plhead != pltail && isspace((u_char)pltail[-1])) 3112 --pltail; 3113 3114 /* check payload size, terminate packet on overflow */ 3115 plsize = (size_t)(pltail - plhead); 3116 if (plsize >= sizeof(buf)) 3117 goto badpacket; 3118 3119 /* copy data, NUL terminate, and set result data ptr */ 3120 memcpy(buf, plhead, plsize); 3121 buf[plsize] = '\0'; 3122 *data = buf; 3123 } else { 3124 /* no payload, current end --> current name termination */ 3125 tp = cp; 3126 } 3127 3128 /* Part Two 3129 * 3130 * Now we're sure that the packet data itself is sane. Scan the 3131 * list now. Make sure a NULL list is properly treated by 3132 * returning a synthetic End-Of-Values record. We must not 3133 * return NULL pointers after this point, or the behaviour would 3134 * become inconsistent if called several times with different 3135 * variable lists after an EoV was returned. (Such a behavior 3136 * actually caused Bug 3008.) 3137 */ 3138 3139 if (NULL == var_list) 3140 return &eol; 3141 3142 for (v = var_list; !(EOV & v->flags); ++v) 3143 if (!(PADDING & v->flags)) { 3144 /* Check if the var name matches the buffer. The 3145 * name is bracketed by [reqpt..tp] and not NUL 3146 * terminated, and it contains no '=' char. The 3147 * lookup value IS NUL-terminated but might 3148 * include a '='... We have to look out for 3149 * that! 3150 */ 3151 const char *sp1 = reqpt; 3152 const char *sp2 = v->text; 3153 3154 /* [Bug 3412] do not compare past NUL byte in name */ 3155 while ( (sp1 != tp) 3156 && ('\0' != *sp2) && (*sp1 == *sp2)) { 3157 ++sp1; 3158 ++sp2; 3159 } 3160 if (sp1 == tp && (*sp2 == '\0' || *sp2 == '=')) 3161 break; 3162 } 3163 3164 /* See if we have found a valid entry or not. If found, advance 3165 * the request pointer for the next round; if not, clear the 3166 * data pointer so we have no dangling garbage here. 3167 */ 3168 if (EOV & v->flags) 3169 *data = NULL; 3170 else 3171 reqpt = cp + (cp != reqend); 3172 return v; 3173 3174 badpacket: 3175 /*TODO? somehow indicate this packet was bad, apart from syslog? */ 3176 numctlbadpkts++; 3177 NLOG(NLOG_SYSEVENT) 3178 if (quiet_until <= current_time) { 3179 quiet_until = current_time + 300; 3180 msyslog(LOG_WARNING, 3181 "Possible 'ntpdx' exploit from %s (possibly spoofed)", 3182 sptoa(rmt_addr)); 3183 } 3184 reqpt = reqend; /* never again for this packet! */ 3185 return NULL; 3186 } 3187 3188 3189 /* 3190 * control_unspec - response to an unspecified op-code 3191 */ 3192 /*ARGSUSED*/ 3193 static void 3194 control_unspec( 3195 struct recvbuf *rbufp, 3196 int restrict_mask 3197 ) 3198 { 3199 struct peer *peer; 3200 3201 /* 3202 * What is an appropriate response to an unspecified op-code? 3203 * I return no errors and no data, unless a specified assocation 3204 * doesn't exist. 3205 */ 3206 if (res_associd) { 3207 peer = findpeerbyassoc(res_associd); 3208 if (NULL == peer) { 3209 ctl_error(CERR_BADASSOC); 3210 return; 3211 } 3212 rpkt.status = htons(ctlpeerstatus(peer)); 3213 } else 3214 rpkt.status = htons(ctlsysstatus()); 3215 ctl_flushpkt(0); 3216 } 3217 3218 3219 /* 3220 * read_status - return either a list of associd's, or a particular 3221 * peer's status. 3222 */ 3223 /*ARGSUSED*/ 3224 static void 3225 read_status( 3226 struct recvbuf *rbufp, 3227 int restrict_mask 3228 ) 3229 { 3230 struct peer *peer; 3231 const u_char *cp; 3232 size_t n; 3233 /* a_st holds association ID, status pairs alternating */ 3234 u_short a_st[CTL_MAX_DATA_LEN / sizeof(u_short)]; 3235 3236 #ifdef DEBUG 3237 if (debug > 2) 3238 printf("read_status: ID %d\n", res_associd); 3239 #endif 3240 /* 3241 * Two choices here. If the specified association ID is 3242 * zero we return all known assocation ID's. Otherwise 3243 * we return a bunch of stuff about the particular peer. 3244 */ 3245 if (res_associd) { 3246 peer = findpeerbyassoc(res_associd); 3247 if (NULL == peer) { 3248 ctl_error(CERR_BADASSOC); 3249 return; 3250 } 3251 rpkt.status = htons(ctlpeerstatus(peer)); 3252 if (res_authokay) 3253 peer->num_events = 0; 3254 /* 3255 * For now, output everything we know about the 3256 * peer. May be more selective later. 3257 */ 3258 for (cp = def_peer_var; *cp != 0; cp++) 3259 ctl_putpeer((int)*cp, peer); 3260 ctl_flushpkt(0); 3261 return; 3262 } 3263 n = 0; 3264 rpkt.status = htons(ctlsysstatus()); 3265 for (peer = peer_list; peer != NULL; peer = peer->p_link) { 3266 a_st[n++] = htons(peer->associd); 3267 a_st[n++] = htons(ctlpeerstatus(peer)); 3268 /* two entries each loop iteration, so n + 1 */ 3269 if (n + 1 >= COUNTOF(a_st)) { 3270 ctl_putdata((void *)a_st, n * sizeof(a_st[0]), 3271 1); 3272 n = 0; 3273 } 3274 } 3275 if (n) 3276 ctl_putdata((void *)a_st, n * sizeof(a_st[0]), 1); 3277 ctl_flushpkt(0); 3278 } 3279 3280 3281 /* 3282 * read_peervars - half of read_variables() implementation 3283 */ 3284 static void 3285 read_peervars(void) 3286 { 3287 const struct ctl_var *v; 3288 struct peer *peer; 3289 const u_char *cp; 3290 size_t i; 3291 char * valuep; 3292 u_char wants[CP_MAXCODE + 1]; 3293 u_int gotvar; 3294 3295 /* 3296 * Wants info for a particular peer. See if we know 3297 * the guy. 3298 */ 3299 peer = findpeerbyassoc(res_associd); 3300 if (NULL == peer) { 3301 ctl_error(CERR_BADASSOC); 3302 return; 3303 } 3304 rpkt.status = htons(ctlpeerstatus(peer)); 3305 if (res_authokay) 3306 peer->num_events = 0; 3307 ZERO(wants); 3308 gotvar = 0; 3309 while (NULL != (v = ctl_getitem(peer_var, &valuep))) { 3310 if (v->flags & EOV) { 3311 ctl_error(CERR_UNKNOWNVAR); 3312 return; 3313 } 3314 INSIST(v->code < COUNTOF(wants)); 3315 wants[v->code] = 1; 3316 gotvar = 1; 3317 } 3318 if (gotvar) { 3319 for (i = 1; i < COUNTOF(wants); i++) 3320 if (wants[i]) 3321 ctl_putpeer(i, peer); 3322 } else 3323 for (cp = def_peer_var; *cp != 0; cp++) 3324 ctl_putpeer((int)*cp, peer); 3325 ctl_flushpkt(0); 3326 } 3327 3328 3329 /* 3330 * read_sysvars - half of read_variables() implementation 3331 */ 3332 static void 3333 read_sysvars(void) 3334 { 3335 const struct ctl_var *v; 3336 struct ctl_var *kv; 3337 u_int n; 3338 u_int gotvar; 3339 const u_char *cs; 3340 char * valuep; 3341 const char * pch; 3342 u_char *wants; 3343 size_t wants_count; 3344 3345 /* 3346 * Wants system variables. Figure out which he wants 3347 * and give them to him. 3348 */ 3349 rpkt.status = htons(ctlsysstatus()); 3350 if (res_authokay) 3351 ctl_sys_num_events = 0; 3352 wants_count = CS_MAXCODE + 1 + count_var(ext_sys_var); 3353 wants = emalloc_zero(wants_count); 3354 gotvar = 0; 3355 while (NULL != (v = ctl_getitem(sys_var, &valuep))) { 3356 if (!(EOV & v->flags)) { 3357 INSIST(v->code < wants_count); 3358 wants[v->code] = 1; 3359 gotvar = 1; 3360 } else { 3361 v = ctl_getitem(ext_sys_var, &valuep); 3362 if (NULL == v) { 3363 ctl_error(CERR_BADVALUE); 3364 free(wants); 3365 return; 3366 } 3367 if (EOV & v->flags) { 3368 ctl_error(CERR_UNKNOWNVAR); 3369 free(wants); 3370 return; 3371 } 3372 n = v->code + CS_MAXCODE + 1; 3373 INSIST(n < wants_count); 3374 wants[n] = 1; 3375 gotvar = 1; 3376 } 3377 } 3378 if (gotvar) { 3379 for (n = 1; n <= CS_MAXCODE; n++) 3380 if (wants[n]) 3381 ctl_putsys(n); 3382 for (n = 0; n + CS_MAXCODE + 1 < wants_count; n++) 3383 if (wants[n + CS_MAXCODE + 1]) { 3384 pch = ext_sys_var[n].text; 3385 ctl_putdata(pch, strlen(pch), 0); 3386 } 3387 } else { 3388 for (cs = def_sys_var; *cs != 0; cs++) 3389 ctl_putsys((int)*cs); 3390 for (kv = ext_sys_var; kv && !(EOV & kv->flags); kv++) 3391 if (DEF & kv->flags) 3392 ctl_putdata(kv->text, strlen(kv->text), 3393 0); 3394 } 3395 free(wants); 3396 ctl_flushpkt(0); 3397 } 3398 3399 3400 /* 3401 * read_variables - return the variables the caller asks for 3402 */ 3403 /*ARGSUSED*/ 3404 static void 3405 read_variables( 3406 struct recvbuf *rbufp, 3407 int restrict_mask 3408 ) 3409 { 3410 if (res_associd) 3411 read_peervars(); 3412 else 3413 read_sysvars(); 3414 } 3415 3416 3417 /* 3418 * write_variables - write into variables. We only allow leap bit 3419 * writing this way. 3420 */ 3421 /*ARGSUSED*/ 3422 static void 3423 write_variables( 3424 struct recvbuf *rbufp, 3425 int restrict_mask 3426 ) 3427 { 3428 const struct ctl_var *v; 3429 int ext_var; 3430 char *valuep; 3431 long val; 3432 size_t octets; 3433 char *vareqv; 3434 const char *t; 3435 char *tt; 3436 3437 val = 0; 3438 /* 3439 * If he's trying to write into a peer tell him no way 3440 */ 3441 if (res_associd != 0) { 3442 ctl_error(CERR_PERMISSION); 3443 return; 3444 } 3445 3446 /* 3447 * Set status 3448 */ 3449 rpkt.status = htons(ctlsysstatus()); 3450 3451 /* 3452 * Look through the variables. Dump out at the first sign of 3453 * trouble. 3454 */ 3455 while ((v = ctl_getitem(sys_var, &valuep)) != NULL) { 3456 ext_var = 0; 3457 if (v->flags & EOV) { 3458 v = ctl_getitem(ext_sys_var, &valuep); 3459 if (v != NULL) { 3460 if (v->flags & EOV) { 3461 ctl_error(CERR_UNKNOWNVAR); 3462 return; 3463 } 3464 ext_var = 1; 3465 } else { 3466 break; 3467 } 3468 } 3469 if (!(v->flags & CAN_WRITE)) { 3470 ctl_error(CERR_PERMISSION); 3471 return; 3472 } 3473 /* [bug 3565] writing makes sense only if we *have* a 3474 * value in the packet! 3475 */ 3476 if (valuep == NULL) { 3477 ctl_error(CERR_BADFMT); 3478 return; 3479 } 3480 if (!ext_var) { 3481 if ( !(*valuep && atoint(valuep, &val))) { 3482 ctl_error(CERR_BADFMT); 3483 return; 3484 } 3485 if ((val & ~LEAP_NOTINSYNC) != 0) { 3486 ctl_error(CERR_BADVALUE); 3487 return; 3488 } 3489 } 3490 3491 if (ext_var) { 3492 octets = strlen(v->text) + strlen(valuep) + 2; 3493 vareqv = emalloc(octets); 3494 tt = vareqv; 3495 t = v->text; 3496 while (*t && *t != '=') 3497 *tt++ = *t++; 3498 *tt++ = '='; 3499 memcpy(tt, valuep, 1 + strlen(valuep)); 3500 set_sys_var(vareqv, 1 + strlen(vareqv), v->flags); 3501 free(vareqv); 3502 } else { 3503 ctl_error(CERR_UNSPEC); /* really */ 3504 return; 3505 } 3506 } 3507 3508 /* 3509 * If we got anything, do it. xxx nothing to do *** 3510 */ 3511 /* 3512 if (leapind != ~0 || leapwarn != ~0) { 3513 if (!leap_setleap((int)leapind, (int)leapwarn)) { 3514 ctl_error(CERR_PERMISSION); 3515 return; 3516 } 3517 } 3518 */ 3519 ctl_flushpkt(0); 3520 } 3521 3522 3523 /* 3524 * configure() processes ntpq :config/config-from-file, allowing 3525 * generic runtime reconfiguration. 3526 */ 3527 static void configure( 3528 struct recvbuf *rbufp, 3529 int restrict_mask 3530 ) 3531 { 3532 size_t data_count; 3533 int retval; 3534 3535 /* I haven't yet implemented changes to an existing association. 3536 * Hence check if the association id is 0 3537 */ 3538 if (res_associd != 0) { 3539 ctl_error(CERR_BADVALUE); 3540 return; 3541 } 3542 3543 if (RES_NOMODIFY & restrict_mask) { 3544 snprintf(remote_config.err_msg, 3545 sizeof(remote_config.err_msg), 3546 "runtime configuration prohibited by restrict ... nomodify"); 3547 ctl_putdata(remote_config.err_msg, 3548 strlen(remote_config.err_msg), 0); 3549 ctl_flushpkt(0); 3550 NLOG(NLOG_SYSINFO) 3551 msyslog(LOG_NOTICE, 3552 "runtime config from %s rejected due to nomodify restriction", 3553 stoa(&rbufp->recv_srcadr)); 3554 sys_restricted++; 3555 return; 3556 } 3557 3558 /* Initialize the remote config buffer */ 3559 data_count = remoteconfig_cmdlength(reqpt, reqend); 3560 3561 if (data_count > sizeof(remote_config.buffer) - 2) { 3562 snprintf(remote_config.err_msg, 3563 sizeof(remote_config.err_msg), 3564 "runtime configuration failed: request too long"); 3565 ctl_putdata(remote_config.err_msg, 3566 strlen(remote_config.err_msg), 0); 3567 ctl_flushpkt(0); 3568 msyslog(LOG_NOTICE, 3569 "runtime config from %s rejected: request too long", 3570 stoa(&rbufp->recv_srcadr)); 3571 return; 3572 } 3573 /* Bug 2853 -- check if all characters were acceptable */ 3574 if (data_count != (size_t)(reqend - reqpt)) { 3575 snprintf(remote_config.err_msg, 3576 sizeof(remote_config.err_msg), 3577 "runtime configuration failed: request contains an unprintable character"); 3578 ctl_putdata(remote_config.err_msg, 3579 strlen(remote_config.err_msg), 0); 3580 ctl_flushpkt(0); 3581 msyslog(LOG_NOTICE, 3582 "runtime config from %s rejected: request contains an unprintable character: %0x", 3583 stoa(&rbufp->recv_srcadr), 3584 reqpt[data_count]); 3585 return; 3586 } 3587 3588 memcpy(remote_config.buffer, reqpt, data_count); 3589 /* The buffer has no trailing linefeed or NUL right now. For 3590 * logging, we do not want a newline, so we do that first after 3591 * adding the necessary NUL byte. 3592 */ 3593 remote_config.buffer[data_count] = '\0'; 3594 DPRINTF(1, ("Got Remote Configuration Command: %s\n", 3595 remote_config.buffer)); 3596 msyslog(LOG_NOTICE, "%s config: %s", 3597 stoa(&rbufp->recv_srcadr), 3598 remote_config.buffer); 3599 3600 /* Now we have to make sure there is a NL/NUL sequence at the 3601 * end of the buffer before we parse it. 3602 */ 3603 remote_config.buffer[data_count++] = '\n'; 3604 remote_config.buffer[data_count] = '\0'; 3605 remote_config.pos = 0; 3606 remote_config.err_pos = 0; 3607 remote_config.no_errors = 0; 3608 config_remotely(&rbufp->recv_srcadr); 3609 3610 /* 3611 * Check if errors were reported. If not, output 'Config 3612 * Succeeded'. Else output the error count. It would be nice 3613 * to output any parser error messages. 3614 */ 3615 if (0 == remote_config.no_errors) { 3616 retval = snprintf(remote_config.err_msg, 3617 sizeof(remote_config.err_msg), 3618 "Config Succeeded"); 3619 if (retval > 0) 3620 remote_config.err_pos += retval; 3621 } 3622 3623 ctl_putdata(remote_config.err_msg, remote_config.err_pos, 0); 3624 ctl_flushpkt(0); 3625 3626 DPRINTF(1, ("Reply: %s\n", remote_config.err_msg)); 3627 3628 if (remote_config.no_errors > 0) 3629 msyslog(LOG_NOTICE, "%d error in %s config", 3630 remote_config.no_errors, 3631 stoa(&rbufp->recv_srcadr)); 3632 } 3633 3634 3635 /* 3636 * derive_nonce - generate 32-bit nonce value derived from the client 3637 * address and a request-specific timestamp. 3638 * 3639 * This uses MD5 for a non-authentication purpose -- the nonce is used 3640 * analogous to the TCP 3-way handshake to confirm the UDP client can 3641 * receive traffic from which it claims to originate, that is, to 3642 * prevent spoofed requests leading to reflected amplification. 3643 */ 3644 static u_int32 derive_nonce( 3645 sockaddr_u * addr, 3646 u_int32 ts_i, 3647 u_int32 ts_f 3648 ) 3649 { 3650 static u_int32 salt[4]; 3651 static u_long last_salt_update; 3652 MD5_CTX ctx; 3653 union d_tag { 3654 u_char digest[MD5_DIGEST_LENGTH]; 3655 u_int32 extract; 3656 } d; 3657 3658 while (!salt[0] || current_time - last_salt_update >= 3600) { 3659 salt[0] = ntp_random(); 3660 salt[1] = ntp_random(); 3661 salt[2] = ntp_random(); 3662 salt[3] = ntp_random(); 3663 last_salt_update = current_time; 3664 } 3665 3666 MD5Init(&ctx); 3667 MD5Update(&ctx, salt, sizeof(salt)); 3668 MD5Update(&ctx, &ts_i, sizeof(ts_i)); 3669 MD5Update(&ctx, &ts_f, sizeof(ts_f)); 3670 if (IS_IPV4(addr)) { 3671 MD5Update(&ctx, &SOCK_ADDR4(addr), sizeof(SOCK_ADDR4(addr))); 3672 } else { 3673 MD5Update(&ctx, &SOCK_ADDR6(addr), sizeof(SOCK_ADDR6(addr))); 3674 } 3675 MD5Update(&ctx, &NSRCPORT(addr), sizeof(NSRCPORT(addr))); 3676 MD5Update(&ctx, salt, sizeof(salt)); 3677 MD5Final(d.digest, &ctx); 3678 3679 return d.extract; 3680 } 3681 3682 3683 /* 3684 * generate_nonce - generate client-address-specific nonce string. 3685 */ 3686 static void generate_nonce( 3687 struct recvbuf * rbufp, 3688 char * nonce, 3689 size_t nonce_octets 3690 ) 3691 { 3692 u_int32 derived; 3693 3694 derived = derive_nonce(&rbufp->recv_srcadr, 3695 rbufp->recv_time.l_ui, 3696 rbufp->recv_time.l_uf); 3697 snprintf(nonce, nonce_octets, "%08x%08x%08x", 3698 rbufp->recv_time.l_ui, rbufp->recv_time.l_uf, derived); 3699 } 3700 3701 3702 /* 3703 * validate_nonce - validate client-address-specific nonce string. 3704 * 3705 * Returns TRUE if the local calculation of the nonce matches the 3706 * client-provided value and the timestamp is recent enough. 3707 */ 3708 static int validate_nonce( 3709 const char * pnonce, 3710 struct recvbuf * rbufp 3711 ) 3712 { 3713 u_int ts_i; 3714 u_int ts_f; 3715 l_fp ts; 3716 l_fp now_delta; 3717 u_int supposed; 3718 u_int derived; 3719 3720 if (3 != sscanf(pnonce, "%08x%08x%08x", &ts_i, &ts_f, &supposed)) 3721 return FALSE; 3722 3723 ts.l_ui = (u_int32)ts_i; 3724 ts.l_uf = (u_int32)ts_f; 3725 derived = derive_nonce(&rbufp->recv_srcadr, ts.l_ui, ts.l_uf); 3726 get_systime(&now_delta); 3727 L_SUB(&now_delta, &ts); 3728 3729 return (supposed == derived && now_delta.l_ui < 16); 3730 } 3731 3732 3733 /* 3734 * send_random_tag_value - send a randomly-generated three character 3735 * tag prefix, a '.', an index, a '=' and a 3736 * random integer value. 3737 * 3738 * To try to force clients to ignore unrecognized tags in mrulist, 3739 * reslist, and ifstats responses, the first and last rows are spiced 3740 * with randomly-generated tag names with correct .# index. Make it 3741 * three characters knowing that none of the currently-used subscripted 3742 * tags have that length, avoiding the need to test for 3743 * tag collision. 3744 */ 3745 static void 3746 send_random_tag_value( 3747 int indx 3748 ) 3749 { 3750 int noise; 3751 char buf[32]; 3752 3753 noise = rand() ^ (rand() << 16); 3754 buf[0] = 'a' + noise % 26; 3755 noise >>= 5; 3756 buf[1] = 'a' + noise % 26; 3757 noise >>= 5; 3758 buf[2] = 'a' + noise % 26; 3759 noise >>= 5; 3760 buf[3] = '.'; 3761 snprintf(&buf[4], sizeof(buf) - 4, "%d", indx); 3762 ctl_putuint(buf, noise); 3763 } 3764 3765 3766 /* 3767 * Send a MRU list entry in response to a "ntpq -c mrulist" operation. 3768 * 3769 * To keep clients honest about not depending on the order of values, 3770 * and thereby avoid being locked into ugly workarounds to maintain 3771 * backward compatibility later as new fields are added to the response, 3772 * the order is random. 3773 */ 3774 static void 3775 send_mru_entry( 3776 mon_entry * mon, 3777 int count 3778 ) 3779 { 3780 const char first_fmt[] = "first.%d"; 3781 const char ct_fmt[] = "ct.%d"; 3782 const char mv_fmt[] = "mv.%d"; 3783 const char rs_fmt[] = "rs.%d"; 3784 char tag[32]; 3785 u_char sent[6]; /* 6 tag=value pairs */ 3786 u_int32 noise; 3787 u_int which; 3788 u_int remaining; 3789 const char * pch; 3790 3791 remaining = COUNTOF(sent); 3792 ZERO(sent); 3793 noise = (u_int32)(rand() ^ (rand() << 16)); 3794 while (remaining > 0) { 3795 which = (noise & 7) % COUNTOF(sent); 3796 noise >>= 3; 3797 while (sent[which]) 3798 which = (which + 1) % COUNTOF(sent); 3799 3800 switch (which) { 3801 3802 case 0: 3803 snprintf(tag, sizeof(tag), addr_fmt, count); 3804 pch = sptoa(&mon->rmtadr); 3805 ctl_putunqstr(tag, pch, strlen(pch)); 3806 break; 3807 3808 case 1: 3809 snprintf(tag, sizeof(tag), last_fmt, count); 3810 ctl_putts(tag, &mon->last); 3811 break; 3812 3813 case 2: 3814 snprintf(tag, sizeof(tag), first_fmt, count); 3815 ctl_putts(tag, &mon->first); 3816 break; 3817 3818 case 3: 3819 snprintf(tag, sizeof(tag), ct_fmt, count); 3820 ctl_putint(tag, mon->count); 3821 break; 3822 3823 case 4: 3824 snprintf(tag, sizeof(tag), mv_fmt, count); 3825 ctl_putuint(tag, mon->vn_mode); 3826 break; 3827 3828 case 5: 3829 snprintf(tag, sizeof(tag), rs_fmt, count); 3830 ctl_puthex(tag, mon->flags); 3831 break; 3832 } 3833 sent[which] = TRUE; 3834 remaining--; 3835 } 3836 } 3837 3838 3839 /* 3840 * read_mru_list - supports ntpq's mrulist command. 3841 * 3842 * The challenge here is to match ntpdc's monlist functionality without 3843 * being limited to hundreds of entries returned total, and without 3844 * requiring state on the server. If state were required, ntpq's 3845 * mrulist command would require authentication. 3846 * 3847 * The approach was suggested by Ry Jones. A finite and variable number 3848 * of entries are retrieved per request, to avoid having responses with 3849 * such large numbers of packets that socket buffers are overflowed and 3850 * packets lost. The entries are retrieved oldest-first, taking into 3851 * account that the MRU list will be changing between each request. We 3852 * can expect to see duplicate entries for addresses updated in the MRU 3853 * list during the fetch operation. In the end, the client can assemble 3854 * a close approximation of the MRU list at the point in time the last 3855 * response was sent by ntpd. The only difference is it may be longer, 3856 * containing some number of oldest entries which have since been 3857 * reclaimed. If necessary, the protocol could be extended to zap those 3858 * from the client snapshot at the end, but so far that doesn't seem 3859 * useful. 3860 * 3861 * To accomodate the changing MRU list, the starting point for requests 3862 * after the first request is supplied as a series of last seen 3863 * timestamps and associated addresses, the newest ones the client has 3864 * received. As long as at least one of those entries hasn't been 3865 * bumped to the head of the MRU list, ntpd can pick up at that point. 3866 * Otherwise, the request is failed and it is up to ntpq to back up and 3867 * provide the next newest entry's timestamps and addresses, conceivably 3868 * backing up all the way to the starting point. 3869 * 3870 * input parameters: 3871 * nonce= Regurgitated nonce retrieved by the client 3872 * previously using CTL_OP_REQ_NONCE, demonstrating 3873 * ability to receive traffic sent to its address. 3874 * frags= Limit on datagrams (fragments) in response. Used 3875 * by newer ntpq versions instead of limit= when 3876 * retrieving multiple entries. 3877 * limit= Limit on MRU entries returned. One of frags= or 3878 * limit= must be provided. 3879 * limit=1 is a special case: Instead of fetching 3880 * beginning with the supplied starting point's 3881 * newer neighbor, fetch the supplied entry, and 3882 * in that case the #.last timestamp can be zero. 3883 * This enables fetching a single entry by IP 3884 * address. When limit is not one and frags= is 3885 * provided, the fragment limit controls. 3886 * mincount= (decimal) Return entries with count >= mincount. 3887 * laddr= Return entries associated with the server's IP 3888 * address given. No port specification is needed, 3889 * and any supplied is ignored. 3890 * resall= 0x-prefixed hex restrict bits which must all be 3891 * lit for an MRU entry to be included. 3892 * Has precedence over any resany=. 3893 * resany= 0x-prefixed hex restrict bits, at least one of 3894 * which must be list for an MRU entry to be 3895 * included. 3896 * last.0= 0x-prefixed hex l_fp timestamp of newest entry 3897 * which client previously received. 3898 * addr.0= text of newest entry's IP address and port, 3899 * IPv6 addresses in bracketed form: [::]:123 3900 * last.1= timestamp of 2nd newest entry client has. 3901 * addr.1= address of 2nd newest entry. 3902 * [...] 3903 * 3904 * ntpq provides as many last/addr pairs as will fit in a single request 3905 * packet, except for the first request in a MRU fetch operation. 3906 * 3907 * The response begins with a new nonce value to be used for any 3908 * followup request. Following the nonce is the next newer entry than 3909 * referred to by last.0 and addr.0, if the "0" entry has not been 3910 * bumped to the front. If it has, the first entry returned will be the 3911 * next entry newer than referred to by last.1 and addr.1, and so on. 3912 * If none of the referenced entries remain unchanged, the request fails 3913 * and ntpq backs up to the next earlier set of entries to resync. 3914 * 3915 * Except for the first response, the response begins with confirmation 3916 * of the entry that precedes the first additional entry provided: 3917 * 3918 * last.older= hex l_fp timestamp matching one of the input 3919 * .last timestamps, which entry now precedes the 3920 * response 0. entry in the MRU list. 3921 * addr.older= text of address corresponding to older.last. 3922 * 3923 * And in any case, a successful response contains sets of values 3924 * comprising entries, with the oldest numbered 0 and incrementing from 3925 * there: 3926 * 3927 * addr.# text of IPv4 or IPv6 address and port 3928 * last.# hex l_fp timestamp of last receipt 3929 * first.# hex l_fp timestamp of first receipt 3930 * ct.# count of packets received 3931 * mv.# mode and version 3932 * rs.# restriction mask (RES_* bits) 3933 * 3934 * Note the code currently assumes there are no valid three letter 3935 * tags sent with each row, and needs to be adjusted if that changes. 3936 * 3937 * The client should accept the values in any order, and ignore .# 3938 * values which it does not understand, to allow a smooth path to 3939 * future changes without requiring a new opcode. Clients can rely 3940 * on all *.0 values preceding any *.1 values, that is all values for 3941 * a given index number are together in the response. 3942 * 3943 * The end of the response list is noted with one or two tag=value 3944 * pairs. Unconditionally: 3945 * 3946 * now= 0x-prefixed l_fp timestamp at the server marking 3947 * the end of the operation. 3948 * 3949 * If any entries were returned, now= is followed by: 3950 * 3951 * last.newest= hex l_fp identical to last.# of the prior 3952 * entry. 3953 */ 3954 static void read_mru_list( 3955 struct recvbuf *rbufp, 3956 int restrict_mask 3957 ) 3958 { 3959 static const char nulltxt[1] = { '\0' }; 3960 static const char nonce_text[] = "nonce"; 3961 static const char frags_text[] = "frags"; 3962 static const char limit_text[] = "limit"; 3963 static const char mincount_text[] = "mincount"; 3964 static const char resall_text[] = "resall"; 3965 static const char resany_text[] = "resany"; 3966 static const char maxlstint_text[] = "maxlstint"; 3967 static const char laddr_text[] = "laddr"; 3968 static const char resaxx_fmt[] = "0x%hx"; 3969 3970 u_int limit; 3971 u_short frags; 3972 u_short resall; 3973 u_short resany; 3974 int mincount; 3975 u_int maxlstint; 3976 sockaddr_u laddr; 3977 endpt * lcladr; 3978 u_int count; 3979 u_int ui; 3980 u_int uf; 3981 l_fp last[16]; 3982 sockaddr_u addr[COUNTOF(last)]; 3983 char buf[128]; 3984 struct ctl_var * in_parms; 3985 const struct ctl_var * v; 3986 const char * val; 3987 const char * pch; 3988 char * pnonce; 3989 int nonce_valid; 3990 size_t i; 3991 int priors; 3992 u_short hash; 3993 mon_entry * mon; 3994 mon_entry * prior_mon; 3995 l_fp now; 3996 3997 if (RES_NOMRULIST & restrict_mask) { 3998 ctl_error(CERR_PERMISSION); 3999 NLOG(NLOG_SYSINFO) 4000 msyslog(LOG_NOTICE, 4001 "mrulist from %s rejected due to nomrulist restriction", 4002 stoa(&rbufp->recv_srcadr)); 4003 sys_restricted++; 4004 return; 4005 } 4006 /* 4007 * fill in_parms var list with all possible input parameters. 4008 */ 4009 in_parms = NULL; 4010 set_var(&in_parms, nonce_text, sizeof(nonce_text), 0); 4011 set_var(&in_parms, frags_text, sizeof(frags_text), 0); 4012 set_var(&in_parms, limit_text, sizeof(limit_text), 0); 4013 set_var(&in_parms, mincount_text, sizeof(mincount_text), 0); 4014 set_var(&in_parms, resall_text, sizeof(resall_text), 0); 4015 set_var(&in_parms, resany_text, sizeof(resany_text), 0); 4016 set_var(&in_parms, maxlstint_text, sizeof(maxlstint_text), 0); 4017 set_var(&in_parms, laddr_text, sizeof(laddr_text), 0); 4018 for (i = 0; i < COUNTOF(last); i++) { 4019 snprintf(buf, sizeof(buf), last_fmt, (int)i); 4020 set_var(&in_parms, buf, strlen(buf) + 1, 0); 4021 snprintf(buf, sizeof(buf), addr_fmt, (int)i); 4022 set_var(&in_parms, buf, strlen(buf) + 1, 0); 4023 } 4024 4025 /* decode input parms */ 4026 pnonce = NULL; 4027 frags = 0; 4028 limit = 0; 4029 mincount = 0; 4030 resall = 0; 4031 resany = 0; 4032 maxlstint = 0; 4033 lcladr = NULL; 4034 priors = 0; 4035 ZERO(last); 4036 ZERO(addr); 4037 4038 /* have to go through '(void*)' to drop 'const' property from pointer. 4039 * ctl_getitem()' needs some cleanup, too.... perlinger@ntp.org 4040 */ 4041 while (NULL != (v = ctl_getitem(in_parms, (void*)&val)) && 4042 !(EOV & v->flags)) { 4043 int si; 4044 4045 if (NULL == val) 4046 val = nulltxt; 4047 4048 if (!strcmp(nonce_text, v->text)) { 4049 free(pnonce); 4050 pnonce = (*val) ? estrdup(val) : NULL; 4051 } else if (!strcmp(frags_text, v->text)) { 4052 if (1 != sscanf(val, "%hu", &frags)) 4053 goto blooper; 4054 } else if (!strcmp(limit_text, v->text)) { 4055 if (1 != sscanf(val, "%u", &limit)) 4056 goto blooper; 4057 } else if (!strcmp(mincount_text, v->text)) { 4058 if (1 != sscanf(val, "%d", &mincount)) 4059 goto blooper; 4060 if (mincount < 0) 4061 mincount = 0; 4062 } else if (!strcmp(resall_text, v->text)) { 4063 if (1 != sscanf(val, resaxx_fmt, &resall)) 4064 goto blooper; 4065 } else if (!strcmp(resany_text, v->text)) { 4066 if (1 != sscanf(val, resaxx_fmt, &resany)) 4067 goto blooper; 4068 } else if (!strcmp(maxlstint_text, v->text)) { 4069 if (1 != sscanf(val, "%u", &maxlstint)) 4070 goto blooper; 4071 } else if (!strcmp(laddr_text, v->text)) { 4072 if (!decodenetnum(val, &laddr)) 4073 goto blooper; 4074 lcladr = getinterface(&laddr, 0); 4075 } else if (1 == sscanf(v->text, last_fmt, &si) && 4076 (size_t)si < COUNTOF(last)) { 4077 if (2 != sscanf(val, "0x%08x.%08x", &ui, &uf)) 4078 goto blooper; 4079 last[si].l_ui = ui; 4080 last[si].l_uf = uf; 4081 if (!SOCK_UNSPEC(&addr[si]) && si == priors) 4082 priors++; 4083 } else if (1 == sscanf(v->text, addr_fmt, &si) && 4084 (size_t)si < COUNTOF(addr)) { 4085 if (!decodenetnum(val, &addr[si])) 4086 goto blooper; 4087 if (last[si].l_ui && last[si].l_uf && si == priors) 4088 priors++; 4089 } else { 4090 DPRINTF(1, ("read_mru_list: invalid key item: '%s' (ignored)\n", 4091 v->text)); 4092 continue; 4093 4094 blooper: 4095 DPRINTF(1, ("read_mru_list: invalid param for '%s': '%s' (bailing)\n", 4096 v->text, val)); 4097 free(pnonce); 4098 pnonce = NULL; 4099 break; 4100 } 4101 } 4102 free_varlist(in_parms); 4103 in_parms = NULL; 4104 4105 /* return no responses until the nonce is validated */ 4106 if (NULL == pnonce) 4107 return; 4108 4109 nonce_valid = validate_nonce(pnonce, rbufp); 4110 free(pnonce); 4111 if (!nonce_valid) 4112 return; 4113 4114 if ((0 == frags && !(0 < limit && limit <= MRU_ROW_LIMIT)) || 4115 frags > MRU_FRAGS_LIMIT) { 4116 ctl_error(CERR_BADVALUE); 4117 return; 4118 } 4119 4120 /* 4121 * If either frags or limit is not given, use the max. 4122 */ 4123 if (0 != frags && 0 == limit) 4124 limit = UINT_MAX; 4125 else if (0 != limit && 0 == frags) 4126 frags = MRU_FRAGS_LIMIT; 4127 4128 /* 4129 * Find the starting point if one was provided. 4130 */ 4131 mon = NULL; 4132 for (i = 0; i < (size_t)priors; i++) { 4133 hash = MON_HASH(&addr[i]); 4134 for (mon = mon_hash[hash]; 4135 mon != NULL; 4136 mon = mon->hash_next) 4137 if (ADDR_PORT_EQ(&mon->rmtadr, &addr[i])) 4138 break; 4139 if (mon != NULL) { 4140 if (L_ISEQU(&mon->last, &last[i])) 4141 break; 4142 mon = NULL; 4143 } 4144 } 4145 4146 /* If a starting point was provided... */ 4147 if (priors) { 4148 /* and none could be found unmodified... */ 4149 if (NULL == mon) { 4150 /* tell ntpq to try again with older entries */ 4151 ctl_error(CERR_UNKNOWNVAR); 4152 return; 4153 } 4154 /* confirm the prior entry used as starting point */ 4155 ctl_putts("last.older", &mon->last); 4156 pch = sptoa(&mon->rmtadr); 4157 ctl_putunqstr("addr.older", pch, strlen(pch)); 4158 4159 /* 4160 * Move on to the first entry the client doesn't have, 4161 * except in the special case of a limit of one. In 4162 * that case return the starting point entry. 4163 */ 4164 if (limit > 1) 4165 mon = PREV_DLIST(mon_mru_list, mon, mru); 4166 } else { /* start with the oldest */ 4167 mon = TAIL_DLIST(mon_mru_list, mru); 4168 } 4169 4170 /* 4171 * send up to limit= entries in up to frags= datagrams 4172 */ 4173 get_systime(&now); 4174 generate_nonce(rbufp, buf, sizeof(buf)); 4175 ctl_putunqstr("nonce", buf, strlen(buf)); 4176 prior_mon = NULL; 4177 for (count = 0; 4178 mon != NULL && res_frags < frags && count < limit; 4179 mon = PREV_DLIST(mon_mru_list, mon, mru)) { 4180 4181 if (mon->count < mincount) 4182 continue; 4183 if (resall && resall != (resall & mon->flags)) 4184 continue; 4185 if (resany && !(resany & mon->flags)) 4186 continue; 4187 if (maxlstint > 0 && now.l_ui - mon->last.l_ui > 4188 maxlstint) 4189 continue; 4190 if (lcladr != NULL && mon->lcladr != lcladr) 4191 continue; 4192 4193 send_mru_entry(mon, count); 4194 if (!count) 4195 send_random_tag_value(0); 4196 count++; 4197 prior_mon = mon; 4198 } 4199 4200 /* 4201 * If this batch completes the MRU list, say so explicitly with 4202 * a now= l_fp timestamp. 4203 */ 4204 if (NULL == mon) { 4205 if (count > 1) 4206 send_random_tag_value(count - 1); 4207 ctl_putts("now", &now); 4208 /* if any entries were returned confirm the last */ 4209 if (prior_mon != NULL) 4210 ctl_putts("last.newest", &prior_mon->last); 4211 } 4212 ctl_flushpkt(0); 4213 } 4214 4215 4216 /* 4217 * Send a ifstats entry in response to a "ntpq -c ifstats" request. 4218 * 4219 * To keep clients honest about not depending on the order of values, 4220 * and thereby avoid being locked into ugly workarounds to maintain 4221 * backward compatibility later as new fields are added to the response, 4222 * the order is random. 4223 */ 4224 static void 4225 send_ifstats_entry( 4226 endpt * la, 4227 u_int ifnum 4228 ) 4229 { 4230 const char addr_fmtu[] = "addr.%u"; 4231 const char bcast_fmt[] = "bcast.%u"; 4232 const char en_fmt[] = "en.%u"; /* enabled */ 4233 const char name_fmt[] = "name.%u"; 4234 const char flags_fmt[] = "flags.%u"; 4235 const char tl_fmt[] = "tl.%u"; /* ttl */ 4236 const char mc_fmt[] = "mc.%u"; /* mcast count */ 4237 const char rx_fmt[] = "rx.%u"; 4238 const char tx_fmt[] = "tx.%u"; 4239 const char txerr_fmt[] = "txerr.%u"; 4240 const char pc_fmt[] = "pc.%u"; /* peer count */ 4241 const char up_fmt[] = "up.%u"; /* uptime */ 4242 char tag[32]; 4243 u_char sent[IFSTATS_FIELDS]; /* 12 tag=value pairs */ 4244 int noisebits; 4245 u_int32 noise; 4246 u_int which; 4247 u_int remaining; 4248 const char *pch; 4249 4250 remaining = COUNTOF(sent); 4251 ZERO(sent); 4252 noise = 0; 4253 noisebits = 0; 4254 while (remaining > 0) { 4255 if (noisebits < 4) { 4256 noise = rand() ^ (rand() << 16); 4257 noisebits = 31; 4258 } 4259 which = (noise & 0xf) % COUNTOF(sent); 4260 noise >>= 4; 4261 noisebits -= 4; 4262 4263 while (sent[which]) 4264 which = (which + 1) % COUNTOF(sent); 4265 4266 switch (which) { 4267 4268 case 0: 4269 snprintf(tag, sizeof(tag), addr_fmtu, ifnum); 4270 pch = sptoa(&la->sin); 4271 ctl_putunqstr(tag, pch, strlen(pch)); 4272 break; 4273 4274 case 1: 4275 snprintf(tag, sizeof(tag), bcast_fmt, ifnum); 4276 if (INT_BCASTOPEN & la->flags) 4277 pch = sptoa(&la->bcast); 4278 else 4279 pch = ""; 4280 ctl_putunqstr(tag, pch, strlen(pch)); 4281 break; 4282 4283 case 2: 4284 snprintf(tag, sizeof(tag), en_fmt, ifnum); 4285 ctl_putint(tag, !la->ignore_packets); 4286 break; 4287 4288 case 3: 4289 snprintf(tag, sizeof(tag), name_fmt, ifnum); 4290 ctl_putstr(tag, la->name, strlen(la->name)); 4291 break; 4292 4293 case 4: 4294 snprintf(tag, sizeof(tag), flags_fmt, ifnum); 4295 ctl_puthex(tag, (u_int)la->flags); 4296 break; 4297 4298 case 5: 4299 snprintf(tag, sizeof(tag), tl_fmt, ifnum); 4300 ctl_putint(tag, la->last_ttl); 4301 break; 4302 4303 case 6: 4304 snprintf(tag, sizeof(tag), mc_fmt, ifnum); 4305 ctl_putint(tag, la->num_mcast); 4306 break; 4307 4308 case 7: 4309 snprintf(tag, sizeof(tag), rx_fmt, ifnum); 4310 ctl_putint(tag, la->received); 4311 break; 4312 4313 case 8: 4314 snprintf(tag, sizeof(tag), tx_fmt, ifnum); 4315 ctl_putint(tag, la->sent); 4316 break; 4317 4318 case 9: 4319 snprintf(tag, sizeof(tag), txerr_fmt, ifnum); 4320 ctl_putint(tag, la->notsent); 4321 break; 4322 4323 case 10: 4324 snprintf(tag, sizeof(tag), pc_fmt, ifnum); 4325 ctl_putuint(tag, la->peercnt); 4326 break; 4327 4328 case 11: 4329 snprintf(tag, sizeof(tag), up_fmt, ifnum); 4330 ctl_putuint(tag, current_time - la->starttime); 4331 break; 4332 } 4333 sent[which] = TRUE; 4334 remaining--; 4335 } 4336 send_random_tag_value((int)ifnum); 4337 } 4338 4339 4340 /* 4341 * read_ifstats - send statistics for each local address, exposed by 4342 * ntpq -c ifstats 4343 */ 4344 static void 4345 read_ifstats( 4346 struct recvbuf * rbufp 4347 ) 4348 { 4349 u_int ifidx; 4350 endpt * la; 4351 4352 /* 4353 * loop over [0..sys_ifnum] searching ep_list for each 4354 * ifnum in turn. 4355 */ 4356 for (ifidx = 0; ifidx < sys_ifnum; ifidx++) { 4357 for (la = ep_list; la != NULL; la = la->elink) 4358 if (ifidx == la->ifnum) 4359 break; 4360 if (NULL == la) 4361 continue; 4362 /* return stats for one local address */ 4363 send_ifstats_entry(la, ifidx); 4364 } 4365 ctl_flushpkt(0); 4366 } 4367 4368 static void 4369 sockaddrs_from_restrict_u( 4370 sockaddr_u * psaA, 4371 sockaddr_u * psaM, 4372 restrict_u * pres, 4373 int ipv6 4374 ) 4375 { 4376 ZERO(*psaA); 4377 ZERO(*psaM); 4378 if (!ipv6) { 4379 psaA->sa.sa_family = AF_INET; 4380 psaA->sa4.sin_addr.s_addr = htonl(pres->u.v4.addr); 4381 psaM->sa.sa_family = AF_INET; 4382 psaM->sa4.sin_addr.s_addr = htonl(pres->u.v4.mask); 4383 } else { 4384 psaA->sa.sa_family = AF_INET6; 4385 memcpy(&psaA->sa6.sin6_addr, &pres->u.v6.addr, 4386 sizeof(psaA->sa6.sin6_addr)); 4387 psaM->sa.sa_family = AF_INET6; 4388 memcpy(&psaM->sa6.sin6_addr, &pres->u.v6.mask, 4389 sizeof(psaA->sa6.sin6_addr)); 4390 } 4391 } 4392 4393 4394 /* 4395 * Send a restrict entry in response to a "ntpq -c reslist" request. 4396 * 4397 * To keep clients honest about not depending on the order of values, 4398 * and thereby avoid being locked into ugly workarounds to maintain 4399 * backward compatibility later as new fields are added to the response, 4400 * the order is random. 4401 */ 4402 static void 4403 send_restrict_entry( 4404 restrict_u * pres, 4405 int ipv6, 4406 u_int idx 4407 ) 4408 { 4409 const char addr_fmtu[] = "addr.%u"; 4410 const char mask_fmtu[] = "mask.%u"; 4411 const char hits_fmt[] = "hits.%u"; 4412 const char flags_fmt[] = "flags.%u"; 4413 char tag[32]; 4414 u_char sent[RESLIST_FIELDS]; /* 4 tag=value pairs */ 4415 int noisebits; 4416 u_int32 noise; 4417 u_int which; 4418 u_int remaining; 4419 sockaddr_u addr; 4420 sockaddr_u mask; 4421 const char * pch; 4422 char * buf; 4423 const char * match_str; 4424 const char * access_str; 4425 4426 sockaddrs_from_restrict_u(&addr, &mask, pres, ipv6); 4427 remaining = COUNTOF(sent); 4428 ZERO(sent); 4429 noise = 0; 4430 noisebits = 0; 4431 while (remaining > 0) { 4432 if (noisebits < 2) { 4433 noise = rand() ^ (rand() << 16); 4434 noisebits = 31; 4435 } 4436 which = (noise & 0x3) % COUNTOF(sent); 4437 noise >>= 2; 4438 noisebits -= 2; 4439 4440 while (sent[which]) 4441 which = (which + 1) % COUNTOF(sent); 4442 4443 /* XXX: Numbers? Really? */ 4444 switch (which) { 4445 4446 case 0: 4447 snprintf(tag, sizeof(tag), addr_fmtu, idx); 4448 pch = stoa(&addr); 4449 ctl_putunqstr(tag, pch, strlen(pch)); 4450 break; 4451 4452 case 1: 4453 snprintf(tag, sizeof(tag), mask_fmtu, idx); 4454 pch = stoa(&mask); 4455 ctl_putunqstr(tag, pch, strlen(pch)); 4456 break; 4457 4458 case 2: 4459 snprintf(tag, sizeof(tag), hits_fmt, idx); 4460 ctl_putuint(tag, pres->count); 4461 break; 4462 4463 case 3: 4464 snprintf(tag, sizeof(tag), flags_fmt, idx); 4465 match_str = res_match_flags(pres->mflags); 4466 access_str = res_access_flags(pres->rflags); 4467 if ('\0' == match_str[0]) { 4468 pch = access_str; 4469 } else { 4470 LIB_GETBUF(buf); 4471 snprintf(buf, LIB_BUFLENGTH, "%s %s", 4472 match_str, access_str); 4473 pch = buf; 4474 } 4475 ctl_putunqstr(tag, pch, strlen(pch)); 4476 break; 4477 } 4478 sent[which] = TRUE; 4479 remaining--; 4480 } 4481 send_random_tag_value((int)idx); 4482 } 4483 4484 4485 static void 4486 send_restrict_list( 4487 restrict_u * pres, 4488 int ipv6, 4489 u_int * pidx 4490 ) 4491 { 4492 for ( ; pres != NULL; pres = pres->link) { 4493 send_restrict_entry(pres, ipv6, *pidx); 4494 (*pidx)++; 4495 } 4496 } 4497 4498 4499 /* 4500 * read_addr_restrictions - returns IPv4 and IPv6 access control lists 4501 */ 4502 static void 4503 read_addr_restrictions( 4504 struct recvbuf * rbufp 4505 ) 4506 { 4507 u_int idx; 4508 4509 idx = 0; 4510 send_restrict_list(restrictlist4, FALSE, &idx); 4511 send_restrict_list(restrictlist6, TRUE, &idx); 4512 ctl_flushpkt(0); 4513 } 4514 4515 4516 /* 4517 * read_ordlist - CTL_OP_READ_ORDLIST_A for ntpq -c ifstats & reslist 4518 */ 4519 static void 4520 read_ordlist( 4521 struct recvbuf * rbufp, 4522 int restrict_mask 4523 ) 4524 { 4525 const char ifstats_s[] = "ifstats"; 4526 const size_t ifstats_chars = COUNTOF(ifstats_s) - 1; 4527 const char addr_rst_s[] = "addr_restrictions"; 4528 const size_t a_r_chars = COUNTOF(addr_rst_s) - 1; 4529 struct ntp_control * cpkt; 4530 u_short qdata_octets; 4531 4532 /* 4533 * CTL_OP_READ_ORDLIST_A was first named CTL_OP_READ_IFSTATS and 4534 * used only for ntpq -c ifstats. With the addition of reslist 4535 * the same opcode was generalized to retrieve ordered lists 4536 * which require authentication. The request data is empty or 4537 * contains "ifstats" (not null terminated) to retrieve local 4538 * addresses and associated stats. It is "addr_restrictions" 4539 * to retrieve the IPv4 then IPv6 remote address restrictions, 4540 * which are access control lists. Other request data return 4541 * CERR_UNKNOWNVAR. 4542 */ 4543 cpkt = (struct ntp_control *)&rbufp->recv_pkt; 4544 qdata_octets = ntohs(cpkt->count); 4545 if (0 == qdata_octets || (ifstats_chars == qdata_octets && 4546 !memcmp(ifstats_s, cpkt->u.data, ifstats_chars))) { 4547 read_ifstats(rbufp); 4548 return; 4549 } 4550 if (a_r_chars == qdata_octets && 4551 !memcmp(addr_rst_s, cpkt->u.data, a_r_chars)) { 4552 read_addr_restrictions(rbufp); 4553 return; 4554 } 4555 ctl_error(CERR_UNKNOWNVAR); 4556 } 4557 4558 4559 /* 4560 * req_nonce - CTL_OP_REQ_NONCE for ntpq -c mrulist prerequisite. 4561 */ 4562 static void req_nonce( 4563 struct recvbuf * rbufp, 4564 int restrict_mask 4565 ) 4566 { 4567 char buf[64]; 4568 4569 generate_nonce(rbufp, buf, sizeof(buf)); 4570 ctl_putunqstr("nonce", buf, strlen(buf)); 4571 ctl_flushpkt(0); 4572 } 4573 4574 4575 /* 4576 * read_clockstatus - return clock radio status 4577 */ 4578 /*ARGSUSED*/ 4579 static void 4580 read_clockstatus( 4581 struct recvbuf *rbufp, 4582 int restrict_mask 4583 ) 4584 { 4585 #ifndef REFCLOCK 4586 /* 4587 * If no refclock support, no data to return 4588 */ 4589 ctl_error(CERR_BADASSOC); 4590 #else 4591 const struct ctl_var * v; 4592 int i; 4593 struct peer * peer; 4594 char * valuep; 4595 u_char * wants; 4596 size_t wants_alloc; 4597 int gotvar; 4598 const u_char * cc; 4599 struct ctl_var * kv; 4600 struct refclockstat cs; 4601 4602 if (res_associd != 0) { 4603 peer = findpeerbyassoc(res_associd); 4604 } else { 4605 /* 4606 * Find a clock for this jerk. If the system peer 4607 * is a clock use it, else search peer_list for one. 4608 */ 4609 if (sys_peer != NULL && (FLAG_REFCLOCK & 4610 sys_peer->flags)) 4611 peer = sys_peer; 4612 else 4613 for (peer = peer_list; 4614 peer != NULL; 4615 peer = peer->p_link) 4616 if (FLAG_REFCLOCK & peer->flags) 4617 break; 4618 } 4619 if (NULL == peer || !(FLAG_REFCLOCK & peer->flags)) { 4620 ctl_error(CERR_BADASSOC); 4621 return; 4622 } 4623 /* 4624 * If we got here we have a peer which is a clock. Get his 4625 * status. 4626 */ 4627 cs.kv_list = NULL; 4628 refclock_control(&peer->srcadr, NULL, &cs); 4629 kv = cs.kv_list; 4630 /* 4631 * Look for variables in the packet. 4632 */ 4633 rpkt.status = htons(ctlclkstatus(&cs)); 4634 wants_alloc = CC_MAXCODE + 1 + count_var(kv); 4635 wants = emalloc_zero(wants_alloc); 4636 gotvar = FALSE; 4637 while (NULL != (v = ctl_getitem(clock_var, &valuep))) { 4638 if (!(EOV & v->flags)) { 4639 wants[v->code] = TRUE; 4640 gotvar = TRUE; 4641 } else { 4642 v = ctl_getitem(kv, &valuep); 4643 if (NULL == v) { 4644 ctl_error(CERR_BADVALUE); 4645 free(wants); 4646 free_varlist(cs.kv_list); 4647 return; 4648 } 4649 if (EOV & v->flags) { 4650 ctl_error(CERR_UNKNOWNVAR); 4651 free(wants); 4652 free_varlist(cs.kv_list); 4653 return; 4654 } 4655 wants[CC_MAXCODE + 1 + v->code] = TRUE; 4656 gotvar = TRUE; 4657 } 4658 } 4659 4660 if (gotvar) { 4661 for (i = 1; i <= CC_MAXCODE; i++) 4662 if (wants[i]) 4663 ctl_putclock(i, &cs, TRUE); 4664 if (kv != NULL) 4665 for (i = 0; !(EOV & kv[i].flags); i++) 4666 if (wants[i + CC_MAXCODE + 1]) 4667 ctl_putdata(kv[i].text, 4668 strlen(kv[i].text), 4669 FALSE); 4670 } else { 4671 for (cc = def_clock_var; *cc != 0; cc++) 4672 ctl_putclock((int)*cc, &cs, FALSE); 4673 for ( ; kv != NULL && !(EOV & kv->flags); kv++) 4674 if (DEF & kv->flags) 4675 ctl_putdata(kv->text, strlen(kv->text), 4676 FALSE); 4677 } 4678 4679 free(wants); 4680 free_varlist(cs.kv_list); 4681 4682 ctl_flushpkt(0); 4683 #endif 4684 } 4685 4686 4687 /* 4688 * write_clockstatus - we don't do this 4689 */ 4690 /*ARGSUSED*/ 4691 static void 4692 write_clockstatus( 4693 struct recvbuf *rbufp, 4694 int restrict_mask 4695 ) 4696 { 4697 ctl_error(CERR_PERMISSION); 4698 } 4699 4700 /* 4701 * Trap support from here on down. We send async trap messages when the 4702 * upper levels report trouble. Traps can by set either by control 4703 * messages or by configuration. 4704 */ 4705 /* 4706 * set_trap - set a trap in response to a control message 4707 */ 4708 static void 4709 set_trap( 4710 struct recvbuf *rbufp, 4711 int restrict_mask 4712 ) 4713 { 4714 int traptype; 4715 4716 /* 4717 * See if this guy is allowed 4718 */ 4719 if (restrict_mask & RES_NOTRAP) { 4720 ctl_error(CERR_PERMISSION); 4721 return; 4722 } 4723 4724 /* 4725 * Determine his allowed trap type. 4726 */ 4727 traptype = TRAP_TYPE_PRIO; 4728 if (restrict_mask & RES_LPTRAP) 4729 traptype = TRAP_TYPE_NONPRIO; 4730 4731 /* 4732 * Call ctlsettrap() to do the work. Return 4733 * an error if it can't assign the trap. 4734 */ 4735 if (!ctlsettrap(&rbufp->recv_srcadr, rbufp->dstadr, traptype, 4736 (int)res_version)) 4737 ctl_error(CERR_NORESOURCE); 4738 ctl_flushpkt(0); 4739 } 4740 4741 4742 /* 4743 * unset_trap - unset a trap in response to a control message 4744 */ 4745 static void 4746 unset_trap( 4747 struct recvbuf *rbufp, 4748 int restrict_mask 4749 ) 4750 { 4751 int traptype; 4752 4753 /* 4754 * We don't prevent anyone from removing his own trap unless the 4755 * trap is configured. Note we also must be aware of the 4756 * possibility that restriction flags were changed since this 4757 * guy last set his trap. Set the trap type based on this. 4758 */ 4759 traptype = TRAP_TYPE_PRIO; 4760 if (restrict_mask & RES_LPTRAP) 4761 traptype = TRAP_TYPE_NONPRIO; 4762 4763 /* 4764 * Call ctlclrtrap() to clear this out. 4765 */ 4766 if (!ctlclrtrap(&rbufp->recv_srcadr, rbufp->dstadr, traptype)) 4767 ctl_error(CERR_BADASSOC); 4768 ctl_flushpkt(0); 4769 } 4770 4771 4772 /* 4773 * ctlsettrap - called to set a trap 4774 */ 4775 int 4776 ctlsettrap( 4777 sockaddr_u *raddr, 4778 endpt *linter, 4779 int traptype, 4780 int version 4781 ) 4782 { 4783 size_t n; 4784 struct ctl_trap *tp; 4785 struct ctl_trap *tptouse; 4786 4787 /* 4788 * See if we can find this trap. If so, we only need update 4789 * the flags and the time. 4790 */ 4791 if ((tp = ctlfindtrap(raddr, linter)) != NULL) { 4792 switch (traptype) { 4793 4794 case TRAP_TYPE_CONFIG: 4795 tp->tr_flags = TRAP_INUSE|TRAP_CONFIGURED; 4796 break; 4797 4798 case TRAP_TYPE_PRIO: 4799 if (tp->tr_flags & TRAP_CONFIGURED) 4800 return (1); /* don't change anything */ 4801 tp->tr_flags = TRAP_INUSE; 4802 break; 4803 4804 case TRAP_TYPE_NONPRIO: 4805 if (tp->tr_flags & TRAP_CONFIGURED) 4806 return (1); /* don't change anything */ 4807 tp->tr_flags = TRAP_INUSE|TRAP_NONPRIO; 4808 break; 4809 } 4810 tp->tr_settime = current_time; 4811 tp->tr_resets++; 4812 return (1); 4813 } 4814 4815 /* 4816 * First we heard of this guy. Try to find a trap structure 4817 * for him to use, clearing out lesser priority guys if we 4818 * have to. Clear out anyone who's expired while we're at it. 4819 */ 4820 tptouse = NULL; 4821 for (n = 0; n < COUNTOF(ctl_traps); n++) { 4822 tp = &ctl_traps[n]; 4823 if ((TRAP_INUSE & tp->tr_flags) && 4824 !(TRAP_CONFIGURED & tp->tr_flags) && 4825 ((tp->tr_settime + CTL_TRAPTIME) > current_time)) { 4826 tp->tr_flags = 0; 4827 num_ctl_traps--; 4828 } 4829 if (!(TRAP_INUSE & tp->tr_flags)) { 4830 tptouse = tp; 4831 } else if (!(TRAP_CONFIGURED & tp->tr_flags)) { 4832 switch (traptype) { 4833 4834 case TRAP_TYPE_CONFIG: 4835 if (tptouse == NULL) { 4836 tptouse = tp; 4837 break; 4838 } 4839 if ((TRAP_NONPRIO & tptouse->tr_flags) && 4840 !(TRAP_NONPRIO & tp->tr_flags)) 4841 break; 4842 4843 if (!(TRAP_NONPRIO & tptouse->tr_flags) 4844 && (TRAP_NONPRIO & tp->tr_flags)) { 4845 tptouse = tp; 4846 break; 4847 } 4848 if (tptouse->tr_origtime < 4849 tp->tr_origtime) 4850 tptouse = tp; 4851 break; 4852 4853 case TRAP_TYPE_PRIO: 4854 if ( TRAP_NONPRIO & tp->tr_flags) { 4855 if (tptouse == NULL || 4856 ((TRAP_INUSE & 4857 tptouse->tr_flags) && 4858 tptouse->tr_origtime < 4859 tp->tr_origtime)) 4860 tptouse = tp; 4861 } 4862 break; 4863 4864 case TRAP_TYPE_NONPRIO: 4865 break; 4866 } 4867 } 4868 } 4869 4870 /* 4871 * If we don't have room for him return an error. 4872 */ 4873 if (tptouse == NULL) 4874 return (0); 4875 4876 /* 4877 * Set up this structure for him. 4878 */ 4879 tptouse->tr_settime = tptouse->tr_origtime = current_time; 4880 tptouse->tr_count = tptouse->tr_resets = 0; 4881 tptouse->tr_sequence = 1; 4882 tptouse->tr_addr = *raddr; 4883 tptouse->tr_localaddr = linter; 4884 tptouse->tr_version = (u_char) version; 4885 tptouse->tr_flags = TRAP_INUSE; 4886 if (traptype == TRAP_TYPE_CONFIG) 4887 tptouse->tr_flags |= TRAP_CONFIGURED; 4888 else if (traptype == TRAP_TYPE_NONPRIO) 4889 tptouse->tr_flags |= TRAP_NONPRIO; 4890 num_ctl_traps++; 4891 return (1); 4892 } 4893 4894 4895 /* 4896 * ctlclrtrap - called to clear a trap 4897 */ 4898 int 4899 ctlclrtrap( 4900 sockaddr_u *raddr, 4901 endpt *linter, 4902 int traptype 4903 ) 4904 { 4905 register struct ctl_trap *tp; 4906 4907 if ((tp = ctlfindtrap(raddr, linter)) == NULL) 4908 return (0); 4909 4910 if (tp->tr_flags & TRAP_CONFIGURED 4911 && traptype != TRAP_TYPE_CONFIG) 4912 return (0); 4913 4914 tp->tr_flags = 0; 4915 num_ctl_traps--; 4916 return (1); 4917 } 4918 4919 4920 /* 4921 * ctlfindtrap - find a trap given the remote and local addresses 4922 */ 4923 static struct ctl_trap * 4924 ctlfindtrap( 4925 sockaddr_u *raddr, 4926 endpt *linter 4927 ) 4928 { 4929 size_t n; 4930 4931 for (n = 0; n < COUNTOF(ctl_traps); n++) 4932 if ((ctl_traps[n].tr_flags & TRAP_INUSE) 4933 && ADDR_PORT_EQ(raddr, &ctl_traps[n].tr_addr) 4934 && (linter == ctl_traps[n].tr_localaddr)) 4935 return &ctl_traps[n]; 4936 4937 return NULL; 4938 } 4939 4940 4941 /* 4942 * report_event - report an event to the trappers 4943 */ 4944 void 4945 report_event( 4946 int err, /* error code */ 4947 struct peer *peer, /* peer structure pointer */ 4948 const char *str /* protostats string */ 4949 ) 4950 { 4951 char statstr[NTP_MAXSTRLEN]; 4952 int i; 4953 size_t len; 4954 4955 /* 4956 * Report the error to the protostats file, system log and 4957 * trappers. 4958 */ 4959 if (peer == NULL) { 4960 4961 /* 4962 * Discard a system report if the number of reports of 4963 * the same type exceeds the maximum. 4964 */ 4965 if (ctl_sys_last_event != (u_char)err) 4966 ctl_sys_num_events= 0; 4967 if (ctl_sys_num_events >= CTL_SYS_MAXEVENTS) 4968 return; 4969 4970 ctl_sys_last_event = (u_char)err; 4971 ctl_sys_num_events++; 4972 snprintf(statstr, sizeof(statstr), 4973 "0.0.0.0 %04x %02x %s", 4974 ctlsysstatus(), err, eventstr(err)); 4975 if (str != NULL) { 4976 len = strlen(statstr); 4977 snprintf(statstr + len, sizeof(statstr) - len, 4978 " %s", str); 4979 } 4980 NLOG(NLOG_SYSEVENT) 4981 msyslog(LOG_INFO, "%s", statstr); 4982 } else { 4983 4984 /* 4985 * Discard a peer report if the number of reports of 4986 * the same type exceeds the maximum for that peer. 4987 */ 4988 const char * src; 4989 u_char errlast; 4990 4991 errlast = (u_char)err & ~PEER_EVENT; 4992 if (peer->last_event != errlast) 4993 peer->num_events = 0; 4994 if (peer->num_events >= CTL_PEER_MAXEVENTS) 4995 return; 4996 4997 peer->last_event = errlast; 4998 peer->num_events++; 4999 if (ISREFCLOCKADR(&peer->srcadr)) 5000 src = refnumtoa(&peer->srcadr); 5001 else 5002 src = stoa(&peer->srcadr); 5003 5004 snprintf(statstr, sizeof(statstr), 5005 "%s %04x %02x %s", src, 5006 ctlpeerstatus(peer), err, eventstr(err)); 5007 if (str != NULL) { 5008 len = strlen(statstr); 5009 snprintf(statstr + len, sizeof(statstr) - len, 5010 " %s", str); 5011 } 5012 NLOG(NLOG_PEEREVENT) 5013 msyslog(LOG_INFO, "%s", statstr); 5014 } 5015 record_proto_stats(statstr); 5016 #if DEBUG 5017 if (debug) 5018 printf("event at %lu %s\n", current_time, statstr); 5019 #endif 5020 5021 /* 5022 * If no trappers, return. 5023 */ 5024 if (num_ctl_traps <= 0) 5025 return; 5026 5027 /* [Bug 3119] 5028 * Peer Events should be associated with a peer -- hence the 5029 * name. But there are instances where this function is called 5030 * *without* a valid peer. This happens e.g. with an unsolicited 5031 * CryptoNAK, or when a leap second alarm is going off while 5032 * currently without a system peer. 5033 * 5034 * The most sensible approach to this seems to bail out here if 5035 * this happens. Avoiding to call this function would also 5036 * bypass the log reporting in the first part of this function, 5037 * and this is probably not the best of all options. 5038 * -*-perlinger@ntp.org-*- 5039 */ 5040 if ((err & PEER_EVENT) && !peer) 5041 return; 5042 5043 /* 5044 * Set up the outgoing packet variables 5045 */ 5046 res_opcode = CTL_OP_ASYNCMSG; 5047 res_offset = 0; 5048 res_async = TRUE; 5049 res_authenticate = FALSE; 5050 datapt = rpkt.u.data; 5051 dataend = &rpkt.u.data[CTL_MAX_DATA_LEN]; 5052 if (!(err & PEER_EVENT)) { 5053 rpkt.associd = 0; 5054 rpkt.status = htons(ctlsysstatus()); 5055 5056 /* Include the core system variables and the list. */ 5057 for (i = 1; i <= CS_VARLIST; i++) 5058 ctl_putsys(i); 5059 } else if (NULL != peer) { /* paranoia -- skip output */ 5060 rpkt.associd = htons(peer->associd); 5061 rpkt.status = htons(ctlpeerstatus(peer)); 5062 5063 /* Dump it all. Later, maybe less. */ 5064 for (i = 1; i <= CP_MAX_NOAUTOKEY; i++) 5065 ctl_putpeer(i, peer); 5066 # ifdef REFCLOCK 5067 /* 5068 * for clock exception events: add clock variables to 5069 * reflect info on exception 5070 */ 5071 if (err == PEVNT_CLOCK) { 5072 struct refclockstat cs; 5073 struct ctl_var *kv; 5074 5075 cs.kv_list = NULL; 5076 refclock_control(&peer->srcadr, NULL, &cs); 5077 5078 ctl_puthex("refclockstatus", 5079 ctlclkstatus(&cs)); 5080 5081 for (i = 1; i <= CC_MAXCODE; i++) 5082 ctl_putclock(i, &cs, FALSE); 5083 for (kv = cs.kv_list; 5084 kv != NULL && !(EOV & kv->flags); 5085 kv++) 5086 if (DEF & kv->flags) 5087 ctl_putdata(kv->text, 5088 strlen(kv->text), 5089 FALSE); 5090 free_varlist(cs.kv_list); 5091 } 5092 # endif /* REFCLOCK */ 5093 } 5094 5095 /* 5096 * We're done, return. 5097 */ 5098 ctl_flushpkt(0); 5099 } 5100 5101 5102 /* 5103 * mprintf_event - printf-style varargs variant of report_event() 5104 */ 5105 int 5106 mprintf_event( 5107 int evcode, /* event code */ 5108 struct peer * p, /* may be NULL */ 5109 const char * fmt, /* msnprintf format */ 5110 ... 5111 ) 5112 { 5113 va_list ap; 5114 int rc; 5115 char msg[512]; 5116 5117 va_start(ap, fmt); 5118 rc = mvsnprintf(msg, sizeof(msg), fmt, ap); 5119 va_end(ap); 5120 report_event(evcode, p, msg); 5121 5122 return rc; 5123 } 5124 5125 5126 /* 5127 * ctl_clr_stats - clear stat counters 5128 */ 5129 void 5130 ctl_clr_stats(void) 5131 { 5132 ctltimereset = current_time; 5133 numctlreq = 0; 5134 numctlbadpkts = 0; 5135 numctlresponses = 0; 5136 numctlfrags = 0; 5137 numctlerrors = 0; 5138 numctlfrags = 0; 5139 numctltooshort = 0; 5140 numctlinputresp = 0; 5141 numctlinputfrag = 0; 5142 numctlinputerr = 0; 5143 numctlbadoffset = 0; 5144 numctlbadversion = 0; 5145 numctldatatooshort = 0; 5146 numctlbadop = 0; 5147 numasyncmsgs = 0; 5148 } 5149 5150 static u_short 5151 count_var( 5152 const struct ctl_var *k 5153 ) 5154 { 5155 u_int c; 5156 5157 if (NULL == k) 5158 return 0; 5159 5160 c = 0; 5161 while (!(EOV & (k++)->flags)) 5162 c++; 5163 5164 ENSURE(c <= USHRT_MAX); 5165 return (u_short)c; 5166 } 5167 5168 5169 char * 5170 add_var( 5171 struct ctl_var **kv, 5172 u_long size, 5173 u_short def 5174 ) 5175 { 5176 u_short c; 5177 struct ctl_var *k; 5178 char * buf; 5179 5180 c = count_var(*kv); 5181 *kv = erealloc(*kv, (c + 2) * sizeof(**kv)); 5182 k = *kv; 5183 buf = emalloc(size); 5184 k[c].code = c; 5185 k[c].text = buf; 5186 k[c].flags = def; 5187 k[c + 1].code = 0; 5188 k[c + 1].text = NULL; 5189 k[c + 1].flags = EOV; 5190 5191 return buf; 5192 } 5193 5194 5195 void 5196 set_var( 5197 struct ctl_var **kv, 5198 const char *data, 5199 u_long size, 5200 u_short def 5201 ) 5202 { 5203 struct ctl_var *k; 5204 const char *s; 5205 const char *t; 5206 char *td; 5207 5208 if (NULL == data || !size) 5209 return; 5210 5211 k = *kv; 5212 if (k != NULL) { 5213 while (!(EOV & k->flags)) { 5214 if (NULL == k->text) { 5215 td = emalloc(size); 5216 memcpy(td, data, size); 5217 k->text = td; 5218 k->flags = def; 5219 return; 5220 } else { 5221 s = data; 5222 t = k->text; 5223 while (*t != '=' && *s == *t) { 5224 s++; 5225 t++; 5226 } 5227 if (*s == *t && ((*t == '=') || !*t)) { 5228 td = erealloc((void *)(intptr_t)k->text, size); 5229 memcpy(td, data, size); 5230 k->text = td; 5231 k->flags = def; 5232 return; 5233 } 5234 } 5235 k++; 5236 } 5237 } 5238 td = add_var(kv, size, def); 5239 memcpy(td, data, size); 5240 } 5241 5242 5243 void 5244 set_sys_var( 5245 const char *data, 5246 u_long size, 5247 u_short def 5248 ) 5249 { 5250 set_var(&ext_sys_var, data, size, def); 5251 } 5252 5253 5254 /* 5255 * get_ext_sys_var() retrieves the value of a user-defined variable or 5256 * NULL if the variable has not been setvar'd. 5257 */ 5258 const char * 5259 get_ext_sys_var(const char *tag) 5260 { 5261 struct ctl_var * v; 5262 size_t c; 5263 const char * val; 5264 5265 val = NULL; 5266 c = strlen(tag); 5267 for (v = ext_sys_var; !(EOV & v->flags); v++) { 5268 if (NULL != v->text && !memcmp(tag, v->text, c)) { 5269 if ('=' == v->text[c]) { 5270 val = v->text + c + 1; 5271 break; 5272 } else if ('\0' == v->text[c]) { 5273 val = ""; 5274 break; 5275 } 5276 } 5277 } 5278 5279 return val; 5280 } 5281 5282 5283 void 5284 free_varlist( 5285 struct ctl_var *kv 5286 ) 5287 { 5288 struct ctl_var *k; 5289 if (kv) { 5290 for (k = kv; !(k->flags & EOV); k++) 5291 free((void *)(intptr_t)k->text); 5292 free((void *)kv); 5293 } 5294 } 5295