Repository navigation
Expand file tree
/
Copy pathntp_control.c
More file actions
5209 lines (4658 loc) · 119 KB
/
Copy pathntp_control.c
File metadata and controls
5209 lines (4658 loc) · 119 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ntp_control.c - respond to mode 6 control messages and send async
* traps. Provides service to ntpq and others.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <sys/stat.h>
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#include <arpa/inet.h>
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_refclock.h"
#include "ntp_control.h"
#include "ntp_unixtime.h"
#include "ntp_stdlib.h"
#include "ntp_config.h"
#include "ntp_crypto.h"
#include "ntp_assert.h"
#include "ntp_leapsec.h"
#include "ntp_md5.h" /* provides OpenSSL digest API */
#include "lib_strbuf.h"
#include <rc_cmdlength.h>
#ifdef KERNEL_PLL
# include "ntp_syscall.h"
#endif
/*
* Structure to hold request procedure information
*/
struct ctl_proc {
short control_code; /* defined request code */
#define NO_REQUEST (-1)
u_short flags; /* flags word */
/* Only one flag. Authentication required or not. */
#define NOAUTH 0
#define AUTH 1
void (*handler) (struct recvbuf *, int); /* handle request */
};
/*
* Request processing routines
*/
static void ctl_error (u_char);
#ifdef REFCLOCK
static u_short ctlclkstatus (struct refclockstat *);
#endif
static void ctl_flushpkt (u_char);
static void ctl_putdata (const char *, unsigned int, int);
static void ctl_putstr (const char *, const char *, size_t);
static void ctl_putdblf (const char *, int, int, double);
#define ctl_putdbl(tag, d) ctl_putdblf(tag, 1, 3, d)
#define ctl_putdbl6(tag, d) ctl_putdblf(tag, 1, 6, d)
#define ctl_putsfp(tag, sfp) ctl_putdblf(tag, 0, -1, \
FPTOD(sfp))
static void ctl_putuint (const char *, u_long);
static void ctl_puthex (const char *, u_long);
static void ctl_putint (const char *, long);
static void ctl_putts (const char *, l_fp *);
static void ctl_putadr (const char *, u_int32,
sockaddr_u *);
static void ctl_putrefid (const char *, u_int32);
static void ctl_putarray (const char *, double *, int);
static void ctl_putsys (int);
static void ctl_putpeer (int, struct peer *);
static void ctl_putfs (const char *, tstamp_t);
static void ctl_printf (const char *, ...) NTP_PRINTF(1, 2);
#ifdef REFCLOCK
static void ctl_putclock (int, struct refclockstat *, int);
#endif /* REFCLOCK */
static const struct ctl_var *ctl_getitem(const struct ctl_var *,
char **);
static u_short count_var (const struct ctl_var *);
static void control_unspec (struct recvbuf *, int);
static void read_status (struct recvbuf *, int);
static void read_sysvars (void);
static void read_peervars (void);
static void read_variables (struct recvbuf *, int);
static void write_variables (struct recvbuf *, int);
static void read_clockstatus(struct recvbuf *, int);
static void write_clockstatus(struct recvbuf *, int);
static void set_trap (struct recvbuf *, int);
static void save_config (struct recvbuf *, int);
static void configure (struct recvbuf *, int);
static void send_mru_entry (mon_entry *, int);
static void send_random_tag_value(int);
static void read_mru_list (struct recvbuf *, int);
static void send_ifstats_entry(endpt *, u_int);
static void read_ifstats (struct recvbuf *);
static void sockaddrs_from_restrict_u(sockaddr_u *, sockaddr_u *,
restrict_u *, int);
static void send_restrict_entry(restrict_u *, int, u_int);
static void send_restrict_list(restrict_u *, int, u_int *);
static void read_addr_restrictions(struct recvbuf *);
static void read_ordlist (struct recvbuf *, int);
static u_int32 derive_nonce (sockaddr_u *, u_int32, u_int32);
static void generate_nonce (struct recvbuf *, char *, size_t);
static int validate_nonce (const char *, struct recvbuf *);
static void req_nonce (struct recvbuf *, int);
static void unset_trap (struct recvbuf *, int);
static struct ctl_trap *ctlfindtrap(sockaddr_u *,
struct interface *);
int/*BOOL*/ is_safe_filename(const char * name);
static const struct ctl_proc control_codes[] = {
{ CTL_OP_UNSPEC, NOAUTH, control_unspec },
{ CTL_OP_READSTAT, NOAUTH, read_status },
{ CTL_OP_READVAR, NOAUTH, read_variables },
{ CTL_OP_WRITEVAR, AUTH, write_variables },
{ CTL_OP_READCLOCK, NOAUTH, read_clockstatus },
{ CTL_OP_WRITECLOCK, NOAUTH, write_clockstatus },
{ CTL_OP_SETTRAP, NOAUTH, set_trap },
{ CTL_OP_CONFIGURE, AUTH, configure },
{ CTL_OP_SAVECONFIG, AUTH, save_config },
{ CTL_OP_READ_MRU, NOAUTH, read_mru_list },
{ CTL_OP_READ_ORDLIST_A, AUTH, read_ordlist },
{ CTL_OP_REQ_NONCE, NOAUTH, req_nonce },
{ CTL_OP_UNSETTRAP, NOAUTH, unset_trap },
{ NO_REQUEST, 0, NULL }
};
/*
* System variables we understand
*/
#define CS_LEAP 1
#define CS_STRATUM 2
#define CS_PRECISION 3
#define CS_ROOTDELAY 4
#define CS_ROOTDISPERSION 5
#define CS_REFID 6
#define CS_REFTIME 7
#define CS_POLL 8
#define CS_PEERID 9
#define CS_OFFSET 10
#define CS_DRIFT 11
#define CS_JITTER 12
#define CS_ERROR 13
#define CS_CLOCK 14
#define CS_PROCESSOR 15
#define CS_SYSTEM 16
#define CS_VERSION 17
#define CS_STABIL 18
#define CS_VARLIST 19
#define CS_TAI 20
#define CS_LEAPTAB 21
#define CS_LEAPEND 22
#define CS_RATE 23
#define CS_MRU_ENABLED 24
#define CS_MRU_DEPTH 25
#define CS_MRU_DEEPEST 26
#define CS_MRU_MINDEPTH 27
#define CS_MRU_MAXAGE 28
#define CS_MRU_MAXDEPTH 29
#define CS_MRU_MEM 30
#define CS_MRU_MAXMEM 31
#define CS_SS_UPTIME 32
#define CS_SS_RESET 33
#define CS_SS_RECEIVED 34
#define CS_SS_THISVER 35
#define CS_SS_OLDVER 36
#define CS_SS_BADFORMAT 37
#define CS_SS_BADAUTH 38
#define CS_SS_DECLINED 39
#define CS_SS_RESTRICTED 40
#define CS_SS_LIMITED 41
#define CS_SS_KODSENT 42
#define CS_SS_PROCESSED 43
#define CS_PEERADR 44
#define CS_PEERMODE 45
#define CS_BCASTDELAY 46
#define CS_AUTHDELAY 47
#define CS_AUTHKEYS 48
#define CS_AUTHFREEK 49
#define CS_AUTHKLOOKUPS 50
#define CS_AUTHKNOTFOUND 51
#define CS_AUTHKUNCACHED 52
#define CS_AUTHKEXPIRED 53
#define CS_AUTHENCRYPTS 54
#define CS_AUTHDECRYPTS 55
#define CS_AUTHRESET 56
#define CS_K_OFFSET 57
#define CS_K_FREQ 58
#define CS_K_MAXERR 59
#define CS_K_ESTERR 60
#define CS_K_STFLAGS 61
#define CS_K_TIMECONST 62
#define CS_K_PRECISION 63
#define CS_K_FREQTOL 64
#define CS_K_PPS_FREQ 65
#define CS_K_PPS_STABIL 66
#define CS_K_PPS_JITTER 67
#define CS_K_PPS_CALIBDUR 68
#define CS_K_PPS_CALIBS 69
#define CS_K_PPS_CALIBERRS 70
#define CS_K_PPS_JITEXC 71
#define CS_K_PPS_STBEXC 72
#define CS_KERN_FIRST CS_K_OFFSET
#define CS_KERN_LAST CS_K_PPS_STBEXC
#define CS_IOSTATS_RESET 73
#define CS_TOTAL_RBUF 74
#define CS_FREE_RBUF 75
#define CS_USED_RBUF 76
#define CS_RBUF_LOWATER 77
#define CS_IO_DROPPED 78
#define CS_IO_IGNORED 79
#define CS_IO_RECEIVED 80
#define CS_IO_SENT 81
#define CS_IO_SENDFAILED 82
#define CS_IO_WAKEUPS 83
#define CS_IO_GOODWAKEUPS 84
#define CS_TIMERSTATS_RESET 85
#define CS_TIMER_OVERRUNS 86
#define CS_TIMER_XMTS 87
#define CS_FUZZ 88
#define CS_WANDER_THRESH 89
#define CS_LEAPSMEARINTV 90
#define CS_LEAPSMEAROFFS 91
#define CS_MAX_NOAUTOKEY CS_LEAPSMEAROFFS
#ifdef AUTOKEY
#define CS_FLAGS (1 + CS_MAX_NOAUTOKEY)
#define CS_HOST (2 + CS_MAX_NOAUTOKEY)
#define CS_PUBLIC (3 + CS_MAX_NOAUTOKEY)
#define CS_CERTIF (4 + CS_MAX_NOAUTOKEY)
#define CS_SIGNATURE (5 + CS_MAX_NOAUTOKEY)
#define CS_REVTIME (6 + CS_MAX_NOAUTOKEY)
#define CS_IDENT (7 + CS_MAX_NOAUTOKEY)
#define CS_DIGEST (8 + CS_MAX_NOAUTOKEY)
#define CS_MAXCODE CS_DIGEST
#else /* !AUTOKEY follows */
#define CS_MAXCODE CS_MAX_NOAUTOKEY
#endif /* !AUTOKEY */
/*
* Peer variables we understand
*/
#define CP_CONFIG 1
#define CP_AUTHENABLE 2
#define CP_AUTHENTIC 3
#define CP_SRCADR 4
#define CP_SRCPORT 5
#define CP_DSTADR 6
#define CP_DSTPORT 7
#define CP_LEAP 8
#define CP_HMODE 9
#define CP_STRATUM 10
#define CP_PPOLL 11
#define CP_HPOLL 12
#define CP_PRECISION 13
#define CP_ROOTDELAY 14
#define CP_ROOTDISPERSION 15
#define CP_REFID 16
#define CP_REFTIME 17
#define CP_ORG 18
#define CP_REC 19
#define CP_XMT 20
#define CP_REACH 21
#define CP_UNREACH 22
#define CP_TIMER 23
#define CP_DELAY 24
#define CP_OFFSET 25
#define CP_JITTER 26
#define CP_DISPERSION 27
#define CP_KEYID 28
#define CP_FILTDELAY 29
#define CP_FILTOFFSET 30
#define CP_PMODE 31
#define CP_RECEIVED 32
#define CP_SENT 33
#define CP_FILTERROR 34
#define CP_FLASH 35
#define CP_TTL 36
#define CP_VARLIST 37
#define CP_IN 38
#define CP_OUT 39
#define CP_RATE 40
#define CP_BIAS 41
#define CP_SRCHOST 42
#define CP_TIMEREC 43
#define CP_TIMEREACH 44
#define CP_BADAUTH 45
#define CP_BOGUSORG 46
#define CP_OLDPKT 47
#define CP_SELDISP 48
#define CP_SELBROKEN 49
#define CP_CANDIDATE 50
#define CP_MAX_NOAUTOKEY CP_CANDIDATE
#ifdef AUTOKEY
#define CP_FLAGS (1 + CP_MAX_NOAUTOKEY)
#define CP_HOST (2 + CP_MAX_NOAUTOKEY)
#define CP_VALID (3 + CP_MAX_NOAUTOKEY)
#define CP_INITSEQ (4 + CP_MAX_NOAUTOKEY)
#define CP_INITKEY (5 + CP_MAX_NOAUTOKEY)
#define CP_INITTSP (6 + CP_MAX_NOAUTOKEY)
#define CP_SIGNATURE (7 + CP_MAX_NOAUTOKEY)
#define CP_IDENT (8 + CP_MAX_NOAUTOKEY)
#define CP_MAXCODE CP_IDENT
#else /* !AUTOKEY follows */
#define CP_MAXCODE CP_MAX_NOAUTOKEY
#endif /* !AUTOKEY */
/*
* Clock variables we understand
*/
#define CC_TYPE 1
#define CC_TIMECODE 2
#define CC_POLL 3
#define CC_NOREPLY 4
#define CC_BADFORMAT 5
#define CC_BADDATA 6
#define CC_FUDGETIME1 7
#define CC_FUDGETIME2 8
#define CC_FUDGEVAL1 9
#define CC_FUDGEVAL2 10
#define CC_FLAGS 11
#define CC_DEVICE 12
#define CC_VARLIST 13
#define CC_MAXCODE CC_VARLIST
/*
* System variable values. The array can be indexed by the variable
* index to find the textual name.
*/
static const struct ctl_var sys_var[] = {
{ 0, PADDING, "" }, /* 0 */
{ CS_LEAP, RW, "leap" }, /* 1 */
{ CS_STRATUM, RO, "stratum" }, /* 2 */
{ CS_PRECISION, RO, "precision" }, /* 3 */
{ CS_ROOTDELAY, RO, "rootdelay" }, /* 4 */
{ CS_ROOTDISPERSION, RO, "rootdisp" }, /* 5 */
{ CS_REFID, RO, "refid" }, /* 6 */
{ CS_REFTIME, RO, "reftime" }, /* 7 */
{ CS_POLL, RO, "tc" }, /* 8 */
{ CS_PEERID, RO, "peer" }, /* 9 */
{ CS_OFFSET, RO, "offset" }, /* 10 */
{ CS_DRIFT, RO, "frequency" }, /* 11 */
{ CS_JITTER, RO, "sys_jitter" }, /* 12 */
{ CS_ERROR, RO, "clk_jitter" }, /* 13 */
{ CS_CLOCK, RO, "clock" }, /* 14 */
{ CS_PROCESSOR, RO, "processor" }, /* 15 */
{ CS_SYSTEM, RO, "system" }, /* 16 */
{ CS_VERSION, RO, "version" }, /* 17 */
{ CS_STABIL, RO, "clk_wander" }, /* 18 */
{ CS_VARLIST, RO, "sys_var_list" }, /* 19 */
{ CS_TAI, RO, "tai" }, /* 20 */
{ CS_LEAPTAB, RO, "leapsec" }, /* 21 */
{ CS_LEAPEND, RO, "expire" }, /* 22 */
{ CS_RATE, RO, "mintc" }, /* 23 */
{ CS_MRU_ENABLED, RO, "mru_enabled" }, /* 24 */
{ CS_MRU_DEPTH, RO, "mru_depth" }, /* 25 */
{ CS_MRU_DEEPEST, RO, "mru_deepest" }, /* 26 */
{ CS_MRU_MINDEPTH, RO, "mru_mindepth" }, /* 27 */
{ CS_MRU_MAXAGE, RO, "mru_maxage" }, /* 28 */
{ CS_MRU_MAXDEPTH, RO, "mru_maxdepth" }, /* 29 */
{ CS_MRU_MEM, RO, "mru_mem" }, /* 30 */
{ CS_MRU_MAXMEM, RO, "mru_maxmem" }, /* 31 */
{ CS_SS_UPTIME, RO, "ss_uptime" }, /* 32 */
{ CS_SS_RESET, RO, "ss_reset" }, /* 33 */
{ CS_SS_RECEIVED, RO, "ss_received" }, /* 34 */
{ CS_SS_THISVER, RO, "ss_thisver" }, /* 35 */
{ CS_SS_OLDVER, RO, "ss_oldver" }, /* 36 */
{ CS_SS_BADFORMAT, RO, "ss_badformat" }, /* 37 */
{ CS_SS_BADAUTH, RO, "ss_badauth" }, /* 38 */
{ CS_SS_DECLINED, RO, "ss_declined" }, /* 39 */
{ CS_SS_RESTRICTED, RO, "ss_restricted" }, /* 40 */
{ CS_SS_LIMITED, RO, "ss_limited" }, /* 41 */
{ CS_SS_KODSENT, RO, "ss_kodsent" }, /* 42 */
{ CS_SS_PROCESSED, RO, "ss_processed" }, /* 43 */
{ CS_PEERADR, RO, "peeradr" }, /* 44 */
{ CS_PEERMODE, RO, "peermode" }, /* 45 */
{ CS_BCASTDELAY, RO, "bcastdelay" }, /* 46 */
{ CS_AUTHDELAY, RO, "authdelay" }, /* 47 */
{ CS_AUTHKEYS, RO, "authkeys" }, /* 48 */
{ CS_AUTHFREEK, RO, "authfreek" }, /* 49 */
{ CS_AUTHKLOOKUPS, RO, "authklookups" }, /* 50 */
{ CS_AUTHKNOTFOUND, RO, "authknotfound" }, /* 51 */
{ CS_AUTHKUNCACHED, RO, "authkuncached" }, /* 52 */
{ CS_AUTHKEXPIRED, RO, "authkexpired" }, /* 53 */
{ CS_AUTHENCRYPTS, RO, "authencrypts" }, /* 54 */
{ CS_AUTHDECRYPTS, RO, "authdecrypts" }, /* 55 */
{ CS_AUTHRESET, RO, "authreset" }, /* 56 */
{ CS_K_OFFSET, RO, "koffset" }, /* 57 */
{ CS_K_FREQ, RO, "kfreq" }, /* 58 */
{ CS_K_MAXERR, RO, "kmaxerr" }, /* 59 */
{ CS_K_ESTERR, RO, "kesterr" }, /* 60 */
{ CS_K_STFLAGS, RO, "kstflags" }, /* 61 */
{ CS_K_TIMECONST, RO, "ktimeconst" }, /* 62 */
{ CS_K_PRECISION, RO, "kprecis" }, /* 63 */
{ CS_K_FREQTOL, RO, "kfreqtol" }, /* 64 */
{ CS_K_PPS_FREQ, RO, "kppsfreq" }, /* 65 */
{ CS_K_PPS_STABIL, RO, "kppsstab" }, /* 66 */
{ CS_K_PPS_JITTER, RO, "kppsjitter" }, /* 67 */
{ CS_K_PPS_CALIBDUR, RO, "kppscalibdur" }, /* 68 */
{ CS_K_PPS_CALIBS, RO, "kppscalibs" }, /* 69 */
{ CS_K_PPS_CALIBERRS, RO, "kppscaliberrs" }, /* 70 */
{ CS_K_PPS_JITEXC, RO, "kppsjitexc" }, /* 71 */
{ CS_K_PPS_STBEXC, RO, "kppsstbexc" }, /* 72 */
{ CS_IOSTATS_RESET, RO, "iostats_reset" }, /* 73 */
{ CS_TOTAL_RBUF, RO, "total_rbuf" }, /* 74 */
{ CS_FREE_RBUF, RO, "free_rbuf" }, /* 75 */
{ CS_USED_RBUF, RO, "used_rbuf" }, /* 76 */
{ CS_RBUF_LOWATER, RO, "rbuf_lowater" }, /* 77 */
{ CS_IO_DROPPED, RO, "io_dropped" }, /* 78 */
{ CS_IO_IGNORED, RO, "io_ignored" }, /* 79 */
{ CS_IO_RECEIVED, RO, "io_received" }, /* 80 */
{ CS_IO_SENT, RO, "io_sent" }, /* 81 */
{ CS_IO_SENDFAILED, RO, "io_sendfailed" }, /* 82 */
{ CS_IO_WAKEUPS, RO, "io_wakeups" }, /* 83 */
{ CS_IO_GOODWAKEUPS, RO, "io_goodwakeups" }, /* 84 */
{ CS_TIMERSTATS_RESET, RO, "timerstats_reset" },/* 85 */
{ CS_TIMER_OVERRUNS, RO, "timer_overruns" }, /* 86 */
{ CS_TIMER_XMTS, RO, "timer_xmts" }, /* 87 */
{ CS_FUZZ, RO, "fuzz" }, /* 88 */
{ CS_WANDER_THRESH, RO, "clk_wander_threshold" }, /* 89 */
{ CS_LEAPSMEARINTV, RO, "leapsmearinterval" }, /* 90 */
{ CS_LEAPSMEAROFFS, RO, "leapsmearoffset" }, /* 91 */
#ifdef AUTOKEY
{ CS_FLAGS, RO, "flags" }, /* 1 + CS_MAX_NOAUTOKEY */
{ CS_HOST, RO, "host" }, /* 2 + CS_MAX_NOAUTOKEY */
{ CS_PUBLIC, RO, "update" }, /* 3 + CS_MAX_NOAUTOKEY */
{ CS_CERTIF, RO, "cert" }, /* 4 + CS_MAX_NOAUTOKEY */
{ CS_SIGNATURE, RO, "signature" }, /* 5 + CS_MAX_NOAUTOKEY */
{ CS_REVTIME, RO, "until" }, /* 6 + CS_MAX_NOAUTOKEY */
{ CS_IDENT, RO, "ident" }, /* 7 + CS_MAX_NOAUTOKEY */
{ CS_DIGEST, RO, "digest" }, /* 8 + CS_MAX_NOAUTOKEY */
#endif /* AUTOKEY */
{ 0, EOV, "" } /* 87/95 */
};
static struct ctl_var *ext_sys_var = NULL;
/*
* System variables we print by default (in fuzzball order,
* more-or-less)
*/
static const u_char def_sys_var[] = {
CS_VERSION,
CS_PROCESSOR,
CS_SYSTEM,
CS_LEAP,
CS_STRATUM,
CS_PRECISION,
CS_ROOTDELAY,
CS_ROOTDISPERSION,
CS_REFID,
CS_REFTIME,
CS_CLOCK,
CS_PEERID,
CS_POLL,
CS_RATE,
CS_OFFSET,
CS_DRIFT,
CS_JITTER,
CS_ERROR,
CS_STABIL,
CS_TAI,
CS_LEAPTAB,
CS_LEAPEND,
CS_LEAPSMEARINTV,
CS_LEAPSMEAROFFS,
#ifdef AUTOKEY
CS_HOST,
CS_IDENT,
CS_FLAGS,
CS_DIGEST,
CS_SIGNATURE,
CS_PUBLIC,
CS_CERTIF,
#endif /* AUTOKEY */
0
};
/*
* Peer variable list
*/
static const struct ctl_var peer_var[] = {
{ 0, PADDING, "" }, /* 0 */
{ CP_CONFIG, RO, "config" }, /* 1 */
{ CP_AUTHENABLE, RO, "authenable" }, /* 2 */
{ CP_AUTHENTIC, RO, "authentic" }, /* 3 */
{ CP_SRCADR, RO, "srcadr" }, /* 4 */
{ CP_SRCPORT, RO, "srcport" }, /* 5 */
{ CP_DSTADR, RO, "dstadr" }, /* 6 */
{ CP_DSTPORT, RO, "dstport" }, /* 7 */
{ CP_LEAP, RO, "leap" }, /* 8 */
{ CP_HMODE, RO, "hmode" }, /* 9 */
{ CP_STRATUM, RO, "stratum" }, /* 10 */
{ CP_PPOLL, RO, "ppoll" }, /* 11 */
{ CP_HPOLL, RO, "hpoll" }, /* 12 */
{ CP_PRECISION, RO, "precision" }, /* 13 */
{ CP_ROOTDELAY, RO, "rootdelay" }, /* 14 */
{ CP_ROOTDISPERSION, RO, "rootdisp" }, /* 15 */
{ CP_REFID, RO, "refid" }, /* 16 */
{ CP_REFTIME, RO, "reftime" }, /* 17 */
{ CP_ORG, RO, "org" }, /* 18 */
{ CP_REC, RO, "rec" }, /* 19 */
{ CP_XMT, RO, "xleave" }, /* 20 */
{ CP_REACH, RO, "reach" }, /* 21 */
{ CP_UNREACH, RO, "unreach" }, /* 22 */
{ CP_TIMER, RO, "timer" }, /* 23 */
{ CP_DELAY, RO, "delay" }, /* 24 */
{ CP_OFFSET, RO, "offset" }, /* 25 */
{ CP_JITTER, RO, "jitter" }, /* 26 */
{ CP_DISPERSION, RO, "dispersion" }, /* 27 */
{ CP_KEYID, RO, "keyid" }, /* 28 */
{ CP_FILTDELAY, RO, "filtdelay" }, /* 29 */
{ CP_FILTOFFSET, RO, "filtoffset" }, /* 30 */
{ CP_PMODE, RO, "pmode" }, /* 31 */
{ CP_RECEIVED, RO, "received"}, /* 32 */
{ CP_SENT, RO, "sent" }, /* 33 */
{ CP_FILTERROR, RO, "filtdisp" }, /* 34 */
{ CP_FLASH, RO, "flash" }, /* 35 */
{ CP_TTL, RO, "ttl" }, /* 36 */
{ CP_VARLIST, RO, "peer_var_list" }, /* 37 */
{ CP_IN, RO, "in" }, /* 38 */
{ CP_OUT, RO, "out" }, /* 39 */
{ CP_RATE, RO, "headway" }, /* 40 */
{ CP_BIAS, RO, "bias" }, /* 41 */
{ CP_SRCHOST, RO, "srchost" }, /* 42 */
{ CP_TIMEREC, RO, "timerec" }, /* 43 */
{ CP_TIMEREACH, RO, "timereach" }, /* 44 */
{ CP_BADAUTH, RO, "badauth" }, /* 45 */
{ CP_BOGUSORG, RO, "bogusorg" }, /* 46 */
{ CP_OLDPKT, RO, "oldpkt" }, /* 47 */
{ CP_SELDISP, RO, "seldisp" }, /* 48 */
{ CP_SELBROKEN, RO, "selbroken" }, /* 49 */
{ CP_CANDIDATE, RO, "candidate" }, /* 50 */
#ifdef AUTOKEY
{ CP_FLAGS, RO, "flags" }, /* 1 + CP_MAX_NOAUTOKEY */
{ CP_HOST, RO, "host" }, /* 2 + CP_MAX_NOAUTOKEY */
{ CP_VALID, RO, "valid" }, /* 3 + CP_MAX_NOAUTOKEY */
{ CP_INITSEQ, RO, "initsequence" }, /* 4 + CP_MAX_NOAUTOKEY */
{ CP_INITKEY, RO, "initkey" }, /* 5 + CP_MAX_NOAUTOKEY */
{ CP_INITTSP, RO, "timestamp" }, /* 6 + CP_MAX_NOAUTOKEY */
{ CP_SIGNATURE, RO, "signature" }, /* 7 + CP_MAX_NOAUTOKEY */
{ CP_IDENT, RO, "ident" }, /* 8 + CP_MAX_NOAUTOKEY */
#endif /* AUTOKEY */
{ 0, EOV, "" } /* 50/58 */
};
/*
* Peer variables we print by default
*/
static const u_char def_peer_var[] = {
CP_SRCADR,
CP_SRCPORT,
CP_SRCHOST,
CP_DSTADR,
CP_DSTPORT,
CP_OUT,
CP_IN,
CP_LEAP,
CP_STRATUM,
CP_PRECISION,
CP_ROOTDELAY,
CP_ROOTDISPERSION,
CP_REFID,
CP_REFTIME,
CP_REC,
CP_REACH,
CP_UNREACH,
CP_HMODE,
CP_PMODE,
CP_HPOLL,
CP_PPOLL,
CP_RATE,
CP_FLASH,
CP_KEYID,
CP_TTL,
CP_OFFSET,
CP_DELAY,
CP_DISPERSION,
CP_JITTER,
CP_XMT,
CP_BIAS,
CP_FILTDELAY,
CP_FILTOFFSET,
CP_FILTERROR,
#ifdef AUTOKEY
CP_HOST,
CP_FLAGS,
CP_SIGNATURE,
CP_VALID,
CP_INITSEQ,
CP_IDENT,
#endif /* AUTOKEY */
0
};
#ifdef REFCLOCK
/*
* Clock variable list
*/
static const struct ctl_var clock_var[] = {
{ 0, PADDING, "" }, /* 0 */
{ CC_TYPE, RO, "type" }, /* 1 */
{ CC_TIMECODE, RO, "timecode" }, /* 2 */
{ CC_POLL, RO, "poll" }, /* 3 */
{ CC_NOREPLY, RO, "noreply" }, /* 4 */
{ CC_BADFORMAT, RO, "badformat" }, /* 5 */
{ CC_BADDATA, RO, "baddata" }, /* 6 */
{ CC_FUDGETIME1, RO, "fudgetime1" }, /* 7 */
{ CC_FUDGETIME2, RO, "fudgetime2" }, /* 8 */
{ CC_FUDGEVAL1, RO, "stratum" }, /* 9 */
{ CC_FUDGEVAL2, RO, "refid" }, /* 10 */
{ CC_FLAGS, RO, "flags" }, /* 11 */
{ CC_DEVICE, RO, "device" }, /* 12 */
{ CC_VARLIST, RO, "clock_var_list" }, /* 13 */
{ 0, EOV, "" } /* 14 */
};
/*
* Clock variables printed by default
*/
static const u_char def_clock_var[] = {
CC_DEVICE,
CC_TYPE, /* won't be output if device = known */
CC_TIMECODE,
CC_POLL,
CC_NOREPLY,
CC_BADFORMAT,
CC_BADDATA,
CC_FUDGETIME1,
CC_FUDGETIME2,
CC_FUDGEVAL1,
CC_FUDGEVAL2,
CC_FLAGS,
0
};
#endif
/*
* MRU string constants shared by send_mru_entry() and read_mru_list().
*/
static const char addr_fmt[] = "addr.%d";
static const char last_fmt[] = "last.%d";
/*
* System and processor definitions.
*/
#ifndef HAVE_UNAME
# ifndef STR_SYSTEM
# define STR_SYSTEM "UNIX"
# endif
# ifndef STR_PROCESSOR
# define STR_PROCESSOR "unknown"
# endif
static const char str_system[] = STR_SYSTEM;
static const char str_processor[] = STR_PROCESSOR;
#else
# include <sys/utsname.h>
static struct utsname utsnamebuf;
#endif /* HAVE_UNAME */
/*
* Trap structures. We only allow a few of these, and send a copy of
* each async message to each live one. Traps time out after an hour, it
* is up to the trap receipient to keep resetting it to avoid being
* timed out.
*/
/* ntp_request.c */
struct ctl_trap ctl_traps[CTL_MAXTRAPS];
int num_ctl_traps;
/*
* Type bits, for ctlsettrap() call.
*/
#define TRAP_TYPE_CONFIG 0 /* used by configuration code */
#define TRAP_TYPE_PRIO 1 /* priority trap */
#define TRAP_TYPE_NONPRIO 2 /* nonpriority trap */
/*
* List relating reference clock types to control message time sources.
* Index by the reference clock type. This list will only be used iff
* the reference clock driver doesn't set peer->sstclktype to something
* different than CTL_SST_TS_UNSPEC.
*/
#ifdef REFCLOCK
static const u_char clocktypes[] = {
CTL_SST_TS_NTP, /* REFCLK_NONE (0) */
CTL_SST_TS_LOCAL, /* REFCLK_LOCALCLOCK (1) */
CTL_SST_TS_UHF, /* deprecated REFCLK_GPS_TRAK (2) */
CTL_SST_TS_HF, /* REFCLK_WWV_PST (3) */
CTL_SST_TS_LF, /* REFCLK_WWVB_SPECTRACOM (4) */
CTL_SST_TS_UHF, /* REFCLK_TRUETIME (5) */
CTL_SST_TS_UHF, /* REFCLK_IRIG_AUDIO (6) */
CTL_SST_TS_HF, /* REFCLK_CHU (7) */
CTL_SST_TS_LF, /* REFCLOCK_PARSE (default) (8) */
CTL_SST_TS_LF, /* REFCLK_GPS_MX4200 (9) */
CTL_SST_TS_UHF, /* REFCLK_GPS_AS2201 (10) */
CTL_SST_TS_UHF, /* REFCLK_GPS_ARBITER (11) */
CTL_SST_TS_UHF, /* REFCLK_IRIG_TPRO (12) */
CTL_SST_TS_ATOM, /* REFCLK_ATOM_LEITCH (13) */
CTL_SST_TS_LF, /* deprecated REFCLK_MSF_EES (14) */
CTL_SST_TS_NTP, /* not used (15) */
CTL_SST_TS_UHF, /* REFCLK_IRIG_BANCOMM (16) */
CTL_SST_TS_UHF, /* REFCLK_GPS_DATU (17) */
CTL_SST_TS_TELEPHONE, /* REFCLK_NIST_ACTS (18) */
CTL_SST_TS_HF, /* REFCLK_WWV_HEATH (19) */
CTL_SST_TS_UHF, /* REFCLK_GPS_NMEA (20) */
CTL_SST_TS_UHF, /* REFCLK_GPS_VME (21) */
CTL_SST_TS_ATOM, /* REFCLK_ATOM_PPS (22) */
CTL_SST_TS_NTP, /* not used (23) */
CTL_SST_TS_NTP, /* not used (24) */
CTL_SST_TS_NTP, /* not used (25) */
CTL_SST_TS_UHF, /* REFCLK_GPS_HP (26) */
CTL_SST_TS_LF, /* REFCLK_ARCRON_MSF (27) */
CTL_SST_TS_UHF, /* REFCLK_SHM (28) */
CTL_SST_TS_UHF, /* REFCLK_PALISADE (29) */
CTL_SST_TS_UHF, /* REFCLK_ONCORE (30) */
CTL_SST_TS_UHF, /* REFCLK_JUPITER (31) */
CTL_SST_TS_LF, /* REFCLK_CHRONOLOG (32) */
CTL_SST_TS_LF, /* REFCLK_DUMBCLOCK (33) */
CTL_SST_TS_LF, /* REFCLK_ULINK (34) */
CTL_SST_TS_LF, /* REFCLK_PCF (35) */
CTL_SST_TS_HF, /* REFCLK_WWV (36) */
CTL_SST_TS_LF, /* REFCLK_FG (37) */
CTL_SST_TS_UHF, /* REFCLK_HOPF_SERIAL (38) */
CTL_SST_TS_UHF, /* REFCLK_HOPF_PCI (39) */
CTL_SST_TS_LF, /* REFCLK_JJY (40) */
CTL_SST_TS_UHF, /* REFCLK_TT560 (41) */
CTL_SST_TS_UHF, /* REFCLK_ZYFER (42) */
CTL_SST_TS_UHF, /* REFCLK_RIPENCC (43) */
CTL_SST_TS_UHF, /* REFCLK_NEOCLOCK4X (44) */
CTL_SST_TS_UHF, /* REFCLK_TSYNCPCI (45) */
CTL_SST_TS_UHF /* REFCLK_GPSDJSON (46) */
};
#endif /* REFCLOCK */
/*
* Keyid used for authenticating write requests.
*/
keyid_t ctl_auth_keyid;
/*
* We keep track of the last error reported by the system internally
*/
static u_char ctl_sys_last_event;
static u_char ctl_sys_num_events;
/*
* Statistic counters to keep track of requests and responses.
*/
u_long ctltimereset; /* time stats reset */
u_long numctlreq; /* number of requests we've received */
u_long numctlbadpkts; /* number of bad control packets */
u_long numctlresponses; /* number of resp packets sent with data */
u_long numctlfrags; /* number of fragments sent */
u_long numctlerrors; /* number of error responses sent */
u_long numctltooshort; /* number of too short input packets */
u_long numctlinputresp; /* number of responses on input */
u_long numctlinputfrag; /* number of fragments on input */
u_long numctlinputerr; /* number of input pkts with err bit set */
u_long numctlbadoffset; /* number of input pkts with nonzero offset */
u_long numctlbadversion; /* number of input pkts with unknown version */
u_long numctldatatooshort; /* data too short for count */
u_long numctlbadop; /* bad op code found in packet */
u_long numasyncmsgs; /* number of async messages we've sent */
/*
* Response packet used by these routines. Also some state information
* so that we can handle packet formatting within a common set of
* subroutines. Note we try to enter data in place whenever possible,
* but the need to set the more bit correctly means we occasionally
* use the extra buffer and copy.
*/
static struct ntp_control rpkt;
static u_char res_version;
static u_char res_opcode;
static associd_t res_associd;
static u_short res_frags; /* datagrams in this response */
static int res_offset; /* offset of payload in response */
static u_char * datapt;
static u_char * dataend;
static int datalinelen;
static int datasent; /* flag to avoid initial ", " */
static int datanotbinflag;
static sockaddr_u *rmt_addr;
static struct interface *lcl_inter;
static u_char res_authenticate;
static u_char res_authokay;
static keyid_t res_keyid;
#define MAXDATALINELEN (72)
static u_char res_async; /* sending async trap response? */
/*
* Pointers for saving state when decoding request packets
*/
static char *reqpt;
static char *reqend;
#ifndef MIN
#define MIN(a, b) (((a) <= (b)) ? (a) : (b))
#endif
/*
* init_control - initialize request data
*/
void
init_control(void)
{
size_t i;
#ifdef HAVE_UNAME
uname(&utsnamebuf);
#endif /* HAVE_UNAME */
ctl_clr_stats();
ctl_auth_keyid = 0;
ctl_sys_last_event = EVNT_UNSPEC;
ctl_sys_num_events = 0;
num_ctl_traps = 0;
for (i = 0; i < COUNTOF(ctl_traps); i++)
ctl_traps[i].tr_flags = 0;
}
/*
* ctl_error - send an error response for the current request
*/
static void
ctl_error(
u_char errcode
)
{
size_t maclen;
numctlerrors++;
DPRINTF(3, ("sending control error %u\n", errcode));
/*
* Fill in the fields. We assume rpkt.sequence and rpkt.associd
* have already been filled in.
*/
rpkt.r_m_e_op = (u_char)CTL_RESPONSE | CTL_ERROR |
(res_opcode & CTL_OP_MASK);
rpkt.status = htons((u_short)(errcode << 8) & 0xff00);
rpkt.count = 0;
/*
* send packet and bump counters
*/
if (res_authenticate && sys_authenticate) {
maclen = authencrypt(res_keyid, (u_int32 *)&rpkt,
CTL_HEADER_LEN);
sendpkt(rmt_addr, lcl_inter, -2, (void *)&rpkt,
CTL_HEADER_LEN + maclen);
} else
sendpkt(rmt_addr, lcl_inter, -3, (void *)&rpkt,
CTL_HEADER_LEN);
}
int/*BOOL*/
is_safe_filename(const char * name)
{
/* We need a strict validation of filenames we should write: The
* daemon might run with special permissions and is remote
* controllable, so we better take care what we allow as file
* name!
*
* The first character must be digit or a letter from the ASCII
* base plane or a '_' ([_A-Za-z0-9]), the following characters
* must be from [-._+A-Za-z0-9].
*
* We do not trust the character classification much here: Since
* the NTP protocol makes no provisions for UTF-8 or local code
* pages, we strictly require the 7bit ASCII code page.
*
* The following table is a packed bit field of 128 two-bit
* groups. The LSB in each group tells us if a character is
* acceptable at the first position, the MSB if the character is
* accepted at any other position.
*
* This does not ensure that the file name is syntactically
* correct (multiple dots will not work with VMS...) but it will
* exclude potential globbing bombs and directory traversal. It
* also rules out drive selection. (For systems that have this
* notion, like Windows or VMS.)
*/
static const uint32_t chclass[8] = {
0x00000000, 0x00000000,
0x28800000, 0x000FFFFF,
0xFFFFFFFC, 0xC03FFFFF,
0xFFFFFFFC, 0x003FFFFF
};
u_int widx, bidx, mask;
if (!*name)
return FALSE;
mask = 1u;
while (0 != (widx = (u_char)*name++)) {
bidx = (widx & 15) << 1;
widx = widx >> 4;
if (widx >= sizeof(chclass))
return FALSE;
if (0 == ((chclass[widx] >> bidx) & mask))
return FALSE;
mask |= 2u;
}
return TRUE;
}
/*
* save_config - Implements ntpq -c "saveconfig <filename>"
* Writes current configuration including any runtime
* changes by ntpq's :config or config-from-file
*
* Note: There should be no buffer overflow or truncation in the
* processing of file names -- both cause security problems. This is bit
* painful to code but essential here.
*/
void
save_config(
struct recvbuf *rbufp,
int restrict_mask
)
{
/* block directory traversal by searching for characters that
* indicate directory components in a file path.
*
* Conceptually we should be searching for DIRSEP in filename,
* however Windows actually recognizes both forward and
* backslashes as equivalent directory separators at the API
* level. On POSIX systems we could allow '\\' but such
* filenames are tricky to manipulate from a shell, so just
* reject both types of slashes on all platforms.
*/
/* TALOS-CAN-0062: block directory traversal for VMS, too */
static const char * illegal_in_filename =
#if defined(VMS)
":[]" /* do not allow drive and path components here */
#elif defined(SYS_WINNT)
":\\/" /* path and drive separators */
#else
"\\/" /* separator and critical char for POSIX */
#endif
;
char reply[128];
#ifdef SAVECONFIG
static const char savedconfig_eq[] = "savedconfig=";
/* Build a safe open mode from the available mode flags. We want
* to create a new file and write it in text mode (when
* applicable -- only Windows does this...)
*/
static const int openmode = O_CREAT | O_TRUNC | O_WRONLY
# if defined(O_EXCL) /* posix, vms */
| O_EXCL
# elif defined(_O_EXCL) /* windows is alway very special... */
| _O_EXCL
# endif
# if defined(_O_TEXT) /* windows, again */
| _O_TEXT
#endif
;
char filespec[128];
char filename[128];
char fullpath[512];
char savedconfig[sizeof(savedconfig_eq) + sizeof(filename)];
time_t now;
int fd;
FILE *fptr;
int prc;
size_t reqlen;
#endif
if (RES_NOMODIFY & restrict_mask) {
ctl_printf("%s", "saveconfig prohibited by restrict ... nomodify");
ctl_flushpkt(0);
NLOG(NLOG_SYSINFO)
msyslog(LOG_NOTICE,