-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-patch_unhardcode-conf.patch
1972 lines (1889 loc) · 60.8 KB
/
6-patch_unhardcode-conf.patch
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
From: Pekka Helenius <[email protected]>
Date: Tue, 04 Aug 2020 01:52:15 +0300
Subject: Unhardcode majority of configuration settings, update manual
--- a/src/sensors.c 2020-07-31 23:58:46.000000000 +0300
+++ b/src/sensors.c 2020-08-03 23:12:53.431633678 +0300
@@ -145,7 +145,7 @@ sensor_add(int sensordev, char *dxname)
s->sensordevid = sensordev;
if (cs->refstr == NULL)
- memcpy(&s->refid, SENSOR_DEFAULT_REFID, sizeof(s->refid));
+ memcpy(&s->refid, conf->sensor_default_refid, sizeof(s->refid));
else {
s->refid = 0;
strncpy((char *)&s->refid, cs->refstr, sizeof(s->refid));
@@ -174,12 +174,12 @@ sensor_query(struct ntp_sensor *s)
double sens_time;
if (conf->settime)
- s->next = getmonotime() + SENSOR_QUERY_INTERVAL_SETTIME;
+ s->next = getmonotime() + conf->sensor_query_interval_settime;
else
- s->next = getmonotime() + SENSOR_QUERY_INTERVAL;
+ s->next = getmonotime() + conf->sensor_query_interval;
/* rcvd is walltime here, monotime in client.c. not used elsewhere */
- if (s->update.rcvd < time(NULL) - SENSOR_DATA_MAXAGE)
+ if (s->update.rcvd < time(NULL) - conf->sensor_data_maxage)
s->update.good = 0;
if (!sensor_probe(s->sensordevid, dxname, &sensor)) {
--- a/src/ntpd.c 2020-08-03 23:29:45.150837701 +0300
+++ b/src/ntpd.c 2020-08-03 23:48:03.062564686 +0300
@@ -58,10 +58,10 @@ void ntpd_adjfreq(double, int);
void ntpd_settime(double);
void readfreq(void);
int writefreq(double);
-void ctl_main(int, char*[]);
+void ctl_main(int, char*[], const struct ntpd_conf *);
const char *ctl_lookup_option(char *, const char **);
void show_status_msg(struct imsg *);
-void show_peer_msg(struct imsg *, int);
+void show_peer_msg(struct imsg *, int, const struct ntpd_conf *);
void show_sensor_msg(struct imsg *, int);
void update_time_sync_status(int);
@@ -135,7 +135,7 @@ auto_preconditions(const struct ntpd_con
#endif
constraints = !TAILQ_EMPTY(&cnf->constraints);
return !cnf->settime && (constraints || cnf->trusted_peers ||
- conf->trusted_sensors) && securelevel == 0;
+ cnf->trusted_sensors) && securelevel == 0;
}
#define POLL_MAX 8
@@ -171,15 +171,16 @@ main(int argc, char *argv[])
__progname = get_progname(argv[0]);
- if (strcmp(__progname, "ntpctl") == 0) {
- ctl_main(argc, argv);
- /* NOTREACHED */
- }
-
conffile = CONFFILE;
memset(&lconf, 0, sizeof(lconf));
+ if (strcmp(__progname, "ntpctl") == 0) {
+ parse_config(conffile, &lconf);
+ ctl_main(argc, argv, &lconf);
+ /* NOTREACHED */
+ }
+
#ifndef HAVE_SETPROCTITLE
/* Prepare for later setproctitle emulation */
saved_argv = calloc(argc + 1, sizeof(*saved_argv));
@@ -253,8 +254,8 @@ main(int argc, char *argv[])
if (geteuid())
errx(1, "main process: need root privileges");
- if ((pw = getpwnam(NTPD_USER)) == NULL)
- errx(1, "main process: unknown user %s", NTPD_USER);
+ if ((pw = getpwnam(conf->ntpd_user)) == NULL)
+ errx(1, "main process: unknown user %s", conf->ntpd_user);
lconf.automatic = auto_preconditions(&lconf);
if (lconf.automatic)
@@ -277,7 +278,7 @@ main(int argc, char *argv[])
fatalx("main process: process '%s' failed (%s)", pname, __func__);
} else {
- if ((control_check(CTLSOCKET)) == -1)
+ if ((control_check(conf->ctlsocket)) == -1)
fatalx("OpenNTPD is already running");
}
@@ -295,7 +296,7 @@ main(int argc, char *argv[])
}
} else {
settime_deadline = getmonotime();
- timeout = 100;
+ timeout = conf->settime_timeout;
}
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNSPEC,
@@ -311,6 +312,10 @@ main(int argc, char *argv[])
start_child(NTP_PROC_NAME, pipe_chld[1], argc0, argv0);
log_procinit("[priv]");
+
+ if (lconf.debug)
+ print_conf(&lconf);
+
readfreq();
signal(SIGTERM, sighdlr);
@@ -370,7 +375,7 @@ main(int argc, char *argv[])
}
if (nfds == 0 && lconf.settime &&
- getmonotime() > settime_deadline + SETTIME_TIMEOUT) {
+ getmonotime() > settime_deadline + conf->settime_timeout) {
lconf.settime = 0;
timeout = INFTIM;
log_init(logdest, lconf.verbose, LOG_DAEMON);
@@ -520,7 +525,7 @@ ntpd_adjtime(double d)
{
int synced = 0;
static int firstadj = 1;
- double threshold = (double)LOG_NEGLIGIBLE_ADJTIME / 1000;
+ double threshold = (double)conf->log_negligible_adjtime / 1000;
d += getoffset();
if (d >= threshold || d <= -1 * threshold)
@@ -581,8 +586,8 @@ ntpd_adjfreq(double relfreq, int wrlog)
r = writefreq(curfreq / 1e9 / (1LL << 32));
ppmfreq = relfreq * 1e6;
if (wrlog) {
- if (ppmfreq >= LOG_NEGLIGIBLE_ADJFREQ ||
- ppmfreq <= -LOG_NEGLIGIBLE_ADJFREQ)
+ if (ppmfreq >= conf->log_negligible_adjfreq ||
+ ppmfreq <= -(conf->log_negligible_adjfreq))
log_info("main process: adjusting clock frequency by %f to %f ppm%s",
ppmfreq, curfreq / 1e3 / (1LL << 32),
r ? "" : " (no drift file)");
@@ -634,13 +639,13 @@ readfreq(void)
int fd;
double d;
- fd = open(DRIFTFILE, O_RDWR);
+ fd = open(conf->driftfile, O_RDWR);
if (fd == -1) {
- log_warnx("main process: creating new drift file %s", DRIFTFILE);
+ log_warnx("main process: creating new drift file %s", conf->driftfile);
current = 0;
if (adjfreq(¤t, NULL) == -1)
log_warn("main process: frequency reset failed");
- freqfp = fopen(DRIFTFILE, "w");
+ freqfp = fopen(conf->driftfile, "w");
return;
}
@@ -654,7 +659,7 @@ readfreq(void)
d /= 1e6; /* scale from ppm */
ntpd_adjfreq(d, 0);
} else
- log_warnx("main process: drift file %s is empty", DRIFTFILE);
+ log_warnx("main process: drift file %s is empty", conf->driftfile);
}
}
@@ -671,7 +676,7 @@ writefreq(double d)
r = fprintf(freqfp, "%.3f\n", d * 1e6); /* scale to ppm */
if (r < 0 || fflush(freqfp) != 0) {
if (warnonce) {
- log_warnx("main process: can't write drift file %s", DRIFTFILE);
+ log_warnx("main process: can't write drift file %s", conf->driftfile);
warnonce = 0;
}
clearerr(freqfp);
@@ -679,13 +684,13 @@ writefreq(double d)
}
off = ftello(freqfp);
if (off == -1 || ftruncate(fileno(freqfp), off) == -1)
- log_warnx("main process: can't truncate drift file %s", DRIFTFILE);
+ log_warnx("main process: can't truncate drift file %s", conf->driftfile);
fsync(fileno(freqfp));
return 1;
}
void
-ctl_main(int argc, char *argv[])
+ctl_main(int argc, char *argv[], const struct ntpd_conf *cconf)
{
struct sockaddr_un sa;
struct imsg imsg;
@@ -693,7 +698,7 @@ ctl_main(int argc, char *argv[])
int fd, n, done, ch, action;
char *sockname;
- sockname = CTLSOCKET;
+ sockname = cconf->ctlsocket;
if (argc < 2) {
usage();
@@ -741,6 +746,7 @@ ctl_main(int argc, char *argv[])
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
+
if (strlcpy(sa.sun_path, sockname, sizeof(sa.sun_path)) >=
sizeof(sa.sun_path))
errx(1, "ntpctl: control socket name is too long");
@@ -799,7 +805,7 @@ ctl_main(int argc, char *argv[])
done = 1;
break;
case CTL_SHOW_PEERS:
- show_peer_msg(&imsg, 0);
+ show_peer_msg(&imsg, 0, cconf);
if (imsg.hdr.type ==
IMSG_CTL_SHOW_PEERS_END)
done = 1;
@@ -816,7 +822,7 @@ ctl_main(int argc, char *argv[])
show_status_msg(&imsg);
break;
case IMSG_CTL_SHOW_PEERS:
- show_peer_msg(&imsg, 1);
+ show_peer_msg(&imsg, 1, cconf);
break;
case IMSG_CTL_SHOW_SENSORS:
show_sensor_msg(&imsg, 1);
@@ -911,7 +917,7 @@ show_status_msg(struct imsg *imsg)
}
void
-show_peer_msg(struct imsg *imsg, int calledfromshowall)
+show_peer_msg(struct imsg *imsg, int calledfromshowall, const struct ntpd_conf *pconf)
{
struct ctl_show_peer *cpeer;
int cnt;
@@ -953,7 +959,7 @@ show_peer_msg(struct imsg *imsg, int cal
cpeer->weight, cpeer->trustlevel, stratum,
(long long)cpeer->next, (long long)cpeer->poll);
- if (cpeer->trustlevel >= TRUSTLEVEL_BADPEER)
+ if (cpeer->trustlevel >= pconf->trustlevel_badpeer)
printf(" %12.3fms %9.3fms %8.3fms\n", cpeer->offset,
cpeer->delay, cpeer->jitter);
else
--- a/src/ntp.c 2020-08-01 15:22:42.000000000 +0300
+++ b/src/ntp.c 2020-08-03 23:08:32.397143675 +0300
@@ -55,7 +55,7 @@ int ntp_dispatch_imsg_dns(void);
void peer_add(struct ntp_peer *);
void peer_remove(struct ntp_peer *);
int inpool(struct sockaddr_storage *,
- struct sockaddr_storage[MAX_SERVERS_DNS], size_t);
+ struct sockaddr_storage[conf->max_servers_dns], size_t);
void
ntp_sighdlr(int sig)
@@ -258,12 +258,12 @@ ntp_main(struct ntpd_conf *nconf, struct
sent_cnt++;
}
if (p->deadline > 0 && p->deadline <= getmonotime()) {
- timeout = 300;
+ timeout = conf->interval_query_timeout;
log_debug("NTP client: NTP peer %s - no reply received in time, "
"next query in %ds", log_sockaddr(
(struct sockaddr *)&p->addr->ss), timeout);
- if (p->trustlevel >= TRUSTLEVEL_BADPEER &&
- (p->trustlevel /= 2) < TRUSTLEVEL_BADPEER)
+ if (p->trustlevel >= conf->trustlevel_badpeer &&
+ (p->trustlevel /= 2) < conf->trustlevel_badpeer)
log_info("NTP client: NTP peer %s is invalid now",
log_sockaddr(
(struct sockaddr *)&p->addr->ss));
@@ -273,17 +273,17 @@ ntp_main(struct ntpd_conf *nconf, struct
}
set_next(p, timeout);
}
- if (p->senderrors > MAX_SEND_ERRORS) {
+ if (p->senderrors > conf->max_send_errors) {
log_debug("NTP client: NTP peer %s - failed to send query, "
"next query in %ds", log_sockaddr(
(struct sockaddr *)&p->addr->ss),
- INTERVAL_QUERY_PATHETIC);
+ conf->interval_query_pathetic);
p->senderrors = 0;
if (client_nextaddr(p) == 1) {
peer_addr_head_clear(p);
client_nextaddr(p);
}
- set_next(p, INTERVAL_QUERY_PATHETIC);
+ set_next(p, conf->interval_query_pathetic);
}
if (p->next > 0 && p->next < nextaction)
nextaction = p->next;
@@ -304,13 +304,13 @@ ntp_main(struct ntpd_conf *nconf, struct
(conf->trusted_sensors || constraint_cnt == 0 ||
conf->constraint_median != 0)) {
if (last_sensor_scan == 0 ||
- last_sensor_scan + SENSOR_SCAN_INTERVAL <= getmonotime()) {
+ last_sensor_scan + conf->sensor_scan_interval <= getmonotime()) {
sensors_cnt = sensor_scan();
last_sensor_scan = getmonotime();
}
if (sensors_cnt == 0 &&
- nextaction > last_sensor_scan + SENSOR_SCAN_INTERVAL)
- nextaction = last_sensor_scan + SENSOR_SCAN_INTERVAL;
+ nextaction > last_sensor_scan + conf->sensor_scan_interval)
+ nextaction = last_sensor_scan + conf->sensor_scan_interval;
sensors_cnt = 0;
TAILQ_FOREACH(s, &conf->ntp_sensors, entry) {
if (conf->settime && s->offsets[0].offset)
@@ -482,7 +482,7 @@ ntp_dispatch_imsg(void)
int
inpool(struct sockaddr_storage *a,
- struct sockaddr_storage old[MAX_SERVERS_DNS], size_t n)
+ struct sockaddr_storage old[conf->max_servers_dns], size_t n)
{
size_t i;
@@ -506,7 +506,7 @@ int
ntp_dispatch_imsg_dns(void)
{
struct imsg imsg;
- struct sockaddr_storage existing[MAX_SERVERS_DNS];
+ struct sockaddr_storage existing[conf->max_servers_dns];
struct ntp_peer *peer, *npeer, *tmp;
u_int16_t dlen;
u_char *p;
@@ -558,7 +558,7 @@ ntp_dispatch_imsg_dns(void)
if (dlen == 0) { /* no data -> temp error */
log_warnx("DNS lookup temporary failed");
peer->state = STATE_DNS_TEMPFAIL;
- if (conf->tmpfail++ == TRIES_AUTO_DNSFAIL)
+ if (conf->tmpfail++ == conf->tries_auto_dnsfail)
priv_settime(0, "of DNS failures");
break;
}
@@ -690,7 +690,7 @@ priv_adjfreq(double offset)
conf->freq.y += offset;
conf->freq.xx += curtime * curtime;
- if (conf->freq.samples % FREQUENCY_SAMPLES != 0)
+ if (conf->freq.samples % conf->frequency_samples != 0)
return;
freq =
@@ -698,10 +698,10 @@ priv_adjfreq(double offset)
/
(conf->freq.xx - conf->freq.x * conf->freq.x / conf->freq.samples);
- if (freq > MAX_FREQUENCY_ADJUST)
- freq = MAX_FREQUENCY_ADJUST;
- else if (freq < -MAX_FREQUENCY_ADJUST)
- freq = -MAX_FREQUENCY_ADJUST;
+ if (freq > conf->max_frequency_adjust)
+ freq = conf->max_frequency_adjust;
+ else if (freq < -(conf->max_frequency_adjust))
+ freq = -(conf->max_frequency_adjust);
imsg_compose(ibuf_main, IMSG_ADJFREQ, 0, 0, -1, &freq, sizeof(freq));
conf->filters |= FILTER_ADJFREQ;
@@ -724,7 +724,7 @@ priv_adjtime(void)
double offset_median;
TAILQ_FOREACH(p, &conf->ntp_peers, entry) {
- if (p->trustlevel < TRUSTLEVEL_BADPEER)
+ if (p->trustlevel < conf->trustlevel_badpeer)
continue;
if (!p->update.good)
return (1);
@@ -744,7 +744,7 @@ priv_adjtime(void)
fatal("main process: can't allocate memory for time adjustment");
TAILQ_FOREACH(p, &conf->ntp_peers, entry) {
- if (p->trustlevel < TRUSTLEVEL_BADPEER)
+ if (p->trustlevel < conf->trustlevel_badpeer)
continue;
for (j = 0; j < p->weight; j++)
offsets[i++] = &p->update;
@@ -841,13 +841,13 @@ update_scale(double offset)
if (offset < 0)
offset = -offset;
- if (offset > QSCALE_OFF_MAX || !conf->status.synced ||
+ if (offset > conf->qscale_off_max || !conf->status.synced ||
conf->freq.num < 3)
conf->scale = 1;
- else if (offset < QSCALE_OFF_MIN)
- conf->scale = QSCALE_OFF_MAX / QSCALE_OFF_MIN;
+ else if (offset < conf->qscale_off_min)
+ conf->scale = conf->qscale_off_max / conf->qscale_off_min;
else
- conf->scale = QSCALE_OFF_MAX / offset;
+ conf->scale = conf->qscale_off_max / offset;
}
time_t
@@ -865,7 +865,7 @@ error_interval(void)
{
time_t interval, r;
- interval = INTERVAL_QUERY_PATHETIC * QSCALE_OFF_MAX / QSCALE_OFF_MIN;
+ interval = conf->interval_query_pathetic * conf->qscale_off_max / conf->qscale_off_min;
r = arc4random_uniform(interval / 10);
return (interval + r);
}
--- a/src/control.c 2020-07-31 23:23:56.000000000 +0300
+++ b/src/control.c 2020-08-03 23:06:05.136249122 +0300
@@ -317,7 +317,7 @@ build_show_status(struct ctl_show_status
TAILQ_FOREACH(p, &conf->ntp_peers, entry) {
cs->peercnt++;
- if (p->trustlevel >= TRUSTLEVEL_BADPEER)
+ if (p->trustlevel >= conf->trustlevel_badpeer)
cs->valid_peers++;
}
TAILQ_FOREACH(s, &conf->ntp_sensors, entry) {
@@ -424,7 +424,7 @@ build_show_sensor(struct ctl_show_sensor
now = getmonotime();
- memcpy(&refid, SENSOR_DEFAULT_REFID, sizeof(refid));
+ memcpy(&refid, conf->sensor_default_refid, sizeof(refid));
refid = refid == s->refid ? 0 : s->refid;
snprintf(cs->sensor_desc, sizeof(cs->sensor_desc),
@@ -445,7 +445,7 @@ build_show_sensor(struct ctl_show_sensor
cs->good = s->update.good;
cs->stratum = s->offsets[shift].status.stratum;
cs->next = s->next - now < 0 ? 0 : s->next - now;
- cs->poll = SENSOR_QUERY_INTERVAL;
+ cs->poll = conf->sensor_query_interval;
cs->offset = s->offsets[shift].offset * 1000.0;
cs->correction = (double)s->correction / 1000.0;
}
--- a/src/constraint.c 2020-08-02 01:57:36.430286127 +0300
+++ b/src/constraint.c 2020-08-02 01:57:57.020286149 +0300
@@ -165,8 +165,8 @@ constraint_query(struct constraint *cstr
/* Proceed and query the time */
break;
case STATE_DNS_TEMPFAIL:
- if (now > cstr->last + (cstr->dnstries >= TRIES_AUTO_DNSFAIL ?
- CONSTRAINT_RETRY_INTERVAL : INTERVAL_AUIO_DNSFAIL)) {
+ if (now > cstr->last + (cstr->dnstries >= conf->tries_auto_dnsfail ?
+ conf->constraint_retry_interval : conf->interval_auto_dnsfail)) {
cstr->dnstries++;
/* Retry resolving the address */
constraint_init(cstr);
@@ -174,7 +174,7 @@ constraint_query(struct constraint *cstr
}
return (-1);
case STATE_QUERY_SENT:
- if (cstr->last + CONSTRAINT_SCAN_TIMEOUT > now) {
+ if (cstr->last + conf->constraint_scan_timeout > now) {
/* The caller should expect a reply */
return (0);
}
@@ -186,7 +186,7 @@ constraint_query(struct constraint *cstr
cstr->state = STATE_TIMEOUT;
return (-1);
case STATE_INVALID:
- if (cstr->last + CONSTRAINT_SCAN_INTERVAL > now) {
+ if (cstr->last + conf->constraint_scan_interval > now) {
/* Nothing to do */
return (-1);
}
@@ -745,7 +745,7 @@ constraint_msg_close(u_int32_t id, u_int
log_debug("constraint %s: no reply"
" received in time, next query in %ds",
log_sockaddr((struct sockaddr *)
- &cstr->addr->ss), CONSTRAINT_SCAN_INTERVAL);
+ &cstr->addr->ss), conf->constraint_scan_interval);
cnt = 0;
TAILQ_FOREACH(tmp, &conf->constraints, entry)
@@ -920,9 +920,9 @@ constraint_check(double val)
tv.tv_usec = 0;
diff = fabs(val - gettime_from_timeval(&tv));
- if (diff > CONSTRAINT_MARGIN) {
+ if (diff > conf->constraint_margin) {
if (conf->constraint_errors++ >
- (CONSTRAINT_ERROR_MARGIN * peer_cnt)) {
+ (conf->constraint_error_margin * peer_cnt)) {
constraint_reset();
}
@@ -999,7 +999,7 @@ int
httpsdate_request(struct httpsdate *httpsdate, struct timeval *when)
{
char timebuf1[32], timebuf2[32];
- size_t outlen = 0, maxlength = CONSTRAINT_MAXHEADERLENGTH, len;
+ size_t outlen = 0, maxlength = conf->constraint_maxheaderlength, len;
char *line, *p, *buf;
time_t httptime, notbefore, notafter;
struct tm *tm;
--- a/src/config.c 2020-08-01 01:02:14.000000000 +0300
+++ b/src/config.c 2020-08-01 11:35:05.758097319 +0300
@@ -115,7 +115,7 @@ host_dns1(const char *s, struct ntp_addr
return (-1);
}
- for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) {
+ for (res = res0; res && cnt < conf->max_servers_dns; res = res->ai_next) {
if (res->ai_family != AF_INET &&
res->ai_family != AF_INET6)
continue;
--- a/src/client.c 2020-08-02 02:04:55.666953258 +0300
+++ b/src/client.c 2020-08-03 23:12:14.368300303 +0300
@@ -57,7 +57,7 @@ client_peer_init(struct ntp_peer *p)
p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3);
p->state = STATE_NONE;
p->shift = 0;
- p->trustlevel = TRUSTLEVEL_PATHETIC;
+ p->trustlevel = conf->trustlevel_pathetic;
p->lasterror = 0;
p->senderrors = 0;
@@ -120,7 +120,7 @@ client_nextaddr(struct ntp_peer *p)
}
p->shift = 0;
- p->trustlevel = TRUSTLEVEL_PATHETIC;
+ p->trustlevel = conf->trustlevel_pathetic;
if (p->addr == NULL) {
p->addr = p->addr_head.a;
@@ -148,10 +148,10 @@ client_query(struct ntp_peer *p)
if (p->addr == NULL && client_nextaddr(p) == -1) {
if (conf->settime)
- set_next(p, INTERVAL_AUIO_DNSFAIL);
+ set_next(p, conf->interval_auto_dnsfail);
else
- set_next(p, MAXIMUM(SETTIME_TIMEOUT,
- scale_interval(INTERVAL_QUERY_AGGRESSIVE)));
+ set_next(p, MAXIMUM(conf->settime_timeout,
+ scale_interval(conf->interval_query_aggressive)));
return (0);
}
@@ -200,8 +200,8 @@ client_query(struct ntp_peer *p)
client_nextaddr(p);
if (p->addr == NULL)
p->addr = p->addr_head.a;
- set_next(p, MAXIMUM(SETTIME_TIMEOUT,
- scale_interval(INTERVAL_QUERY_AGGRESSIVE)));
+ set_next(p, MAXIMUM(conf->settime_timeout,
+ scale_interval(conf->interval_query_aggressive)));
p->senderrors++;
return (-1);
} else
@@ -239,14 +239,14 @@ client_query(struct ntp_peer *p)
if (ntp_sendmsg(p->query->fd, NULL, &p->query->msg) == -1) {
p->senderrors++;
- set_next(p, INTERVAL_QUERY_PATHETIC);
- p->trustlevel = TRUSTLEVEL_PATHETIC;
+ set_next(p, conf->interval_query_pathetic);
+ p->trustlevel = conf->trustlevel_pathetic;
return (-1);
}
p->senderrors = 0;
p->state = STATE_QUERY_SENT;
- set_deadline(p, QUERYTIME_MAX);
+ set_deadline(p, conf->querytime_max);
return (0);
}
@@ -263,7 +263,7 @@ void
handle_auto(uint8_t trusted, double offset)
{
static int count;
- static double v[AUTO_REPLIES];
+ double v[conf->auto_replies];
/*
* It happens the (constraint) resolves initially fail, don't give up
@@ -272,7 +272,7 @@ handle_auto(uint8_t trusted, double offs
if (!trusted && conf->constraint_median == 0)
return;
- if (offset < AUTO_THRESHOLD) {
+ if (offset < conf->auto_threshold) {
/* don't bother */
priv_settime(0, "NTP client: NTP peer offset is negative or close enough");
return;
@@ -281,13 +281,13 @@ handle_auto(uint8_t trusted, double offs
v[count++] = offset;
if (count < AUTO_REPLIES)
return;
-
+
/* we have enough */
qsort(v, count, sizeof(double), auto_cmp);
- if (AUTO_REPLIES % 2 == 0)
- offset = (v[AUTO_REPLIES / 2 - 1] + v[AUTO_REPLIES / 2]) / 2;
+ if (conf->auto_replies % 2 == 0)
+ offset = (v[conf->auto_replies / 2 - 1] + v[conf->auto_replies / 2]) / 2;
else
- offset = v[AUTO_REPLIES / 2];
+ offset = v[conf->auto_replies / 2];
priv_settime(offset, "");
}
@@ -451,22 +451,22 @@ client_dispatch(struct ntp_peer *p, u_in
} else
p->reply[p->shift].status.send_refid = msg.xmttime.fractionl;
- if (p->trustlevel < TRUSTLEVEL_PATHETIC)
- interval = scale_interval(INTERVAL_QUERY_PATHETIC);
- else if (p->trustlevel < TRUSTLEVEL_AGGRESSIVE)
+ if (p->trustlevel < conf->trustlevel_pathetic)
+ interval = scale_interval(conf->interval_query_pathetic);
+ else if (p->trustlevel < conf->trustlevel_aggressive)
interval = (conf->settime && conf->automatic) ?
- INTERVAL_QUERY_ULTRA_VIOLENCE :
- scale_interval(INTERVAL_QUERY_AGGRESSIVE);
+ conf->interval_query_ultra_violence :
+ scale_interval(conf->interval_query_aggressive);
else
- interval = scale_interval(INTERVAL_QUERY_NORMAL);
+ interval = scale_interval(conf->interval_query_normal);
set_next(p, interval);
p->state = STATE_REPLY_RECEIVED;
/* every received reply which we do not discard increases trust */
- if (p->trustlevel < TRUSTLEVEL_MAX) {
- if (p->trustlevel < TRUSTLEVEL_BADPEER &&
- p->trustlevel + 1 >= TRUSTLEVEL_BADPEER)
+ if (p->trustlevel < conf->trustlevel_max) {
+ if (p->trustlevel < conf->trustlevel_badpeer &&
+ p->trustlevel + 1 >= conf->trustlevel_badpeer)
log_info("NTP client: NTP peer %s is valid now",
log_sockaddr((struct sockaddr *)&p->addr->ss));
p->trustlevel++;
--- a/src/ntpd.h 2020-08-01 01:27:06.000000000 +0300
+++ b/src/ntpd.h 2020-08-03 23:10:00.839597442 +0300
@@ -56,6 +56,8 @@
#define INTERVAL_QUERY_AGGRESSIVE 5
#define INTERVAL_QUERY_ULTRA_VIOLENCE 1 /* used at startup for auto */
+#define INTERVAL_QUERY_TIMEOUT 300
+
#define TRUSTLEVEL_BADPEER 6
#define TRUSTLEVEL_PATHETIC 2
#define TRUSTLEVEL_AGGRESSIVE 8
@@ -69,7 +71,7 @@
#define QUERYTIME_MAX 15 /* single query might take n secs max */
#define OFFSET_ARRAY_SIZE 8
#define SENSOR_OFFSETS 6
-#define SETTIME_TIMEOUT 15 /* max seconds to wait with -s */
+#define SETTIME_TIMEOUT 100 /* max seconds to wait when settime == 1 */
#define LOG_NEGLIGIBLE_ADJTIME 32 /* negligible drift to not log (ms) */
#define LOG_NEGLIGIBLE_ADJFREQ 0.05 /* negligible rate to not log (ppm) */
#define FREQUENCY_SAMPLES 8 /* samples for est. of permanent drift */
@@ -80,7 +82,7 @@
#define FILTER_ADJFREQ 0x01 /* set after doing adjfreq */
#define AUTO_REPLIES 4 /* # of ntp replies we want for auto */
#define AUTO_THRESHOLD 60 /* dont bother auto setting < this */
-#define INTERVAL_AUIO_DNSFAIL 1 /* DNS tmpfail interval for auto */
+#define INTERVAL_AUTO_DNSFAIL 1 /* DNS tmpfail interval for auto */
#define TRIES_AUTO_DNSFAIL 4 /* DNS tmpfail quick retries */
@@ -268,6 +270,59 @@ struct ntpd_conf {
size_t ca_len;
int tmpfail;
char *pid_file;
+
+ char *ntpd_user;
+ char *driftfile;
+ char *ctlsocket;
+
+ int interval_query_normal;
+ int interval_query_pathetic;
+ int interval_query_aggressive;
+ int interval_query_ultra_violence;
+
+ int interval_query_timeout;
+
+ int trustlevel_badpeer;
+ int trustlevel_pathetic;
+ int trustlevel_aggressive;
+ int trustlevel_max;
+
+ int max_servers_dns;
+
+ double qscale_off_min;
+ double qscale_off_max;
+
+ int querytime_max;
+ int settime_timeout;
+
+ int log_negligible_adjtime;
+ double log_negligible_adjfreq;
+
+ int frequency_samples;
+ double max_frequency_adjust;
+
+ int max_send_errors;
+
+ u_int8_t filter_adjfreq;
+
+ int auto_replies;
+ int auto_threshold;
+ int interval_auto_dnsfail;
+ int tries_auto_dnsfail;
+
+ int sensor_query_interval_settime;
+ int sensor_data_maxage;
+ int sensor_query_interval;
+ int sensor_scan_interval;
+ char *sensor_default_refid;
+
+ double constraint_error_margin;
+ int constraint_retry_interval;
+ int constraint_scan_interval;
+ int constraint_scan_timeout;
+ double constraint_margin;
+
+ int constraint_maxheaderlength;
};
struct ctl_show_status {
@@ -363,6 +418,7 @@ extern struct ctl_conns ctl_conns;
/* parse.y */
int parse_config(const char *, struct ntpd_conf *);
+void print_conf(struct ntpd_conf *);
/* config.c */
void host(const char *, struct ntp_addr **);
--- a/src/parse.y 2020-08-01 01:51:28.000000000 +0300
+++ b/src/parse.y 2020-08-03 23:11:12.796264187 +0300
@@ -52,7 +52,6 @@ int yyerror(const char *, ...)
__attribute__((__format__ (printf, 1, 2)))
__attribute__((__nonnull__ (1)));
int kw_cmp(const void *, const void *);
-int lookup(char *);
int lgetc(int);
int lungetc(int);
int findeol(void);
@@ -70,12 +69,15 @@ struct opts {
int trusted;
char *refstr;
int port;
+ int pos_num;
+ double pos_decimal;
} opts;
void opts_default(void);
typedef struct {
union {
int64_t number;
+ double decimal;
char *string;
struct ntp_addr_wrap *addr;
struct opts opts;
@@ -89,8 +91,64 @@ typedef struct {
%token SERVER SERVERS SENSOR CORRECTION RTABLE REFID STRATUM WEIGHT
%token ERROR
%token PORT
+
+%token _NTPD_USER
+%token _DRIFTFILE
+%token _CTLSOCKET
+
+%token _INTERVAL_QUERY_NORMAL
+%token _INTERVAL_QUERY_PATHETIC
+%token _INTERVAL_QUERY_AGGRESSIVE
+%token _INTERVAL_QUERY_ULTRA_VIOLENCE
+
+%token _INTERVAL_QUERY_TIMEOUT
+
+%token _TRUSTLEVEL_BADPEER
+%token _TRUSTLEVEL_PATHETIC
+%token _TRUSTLEVEL_AGGRESSIVE
+%token _TRUSTLEVEL_MAX
+
+%token _MAX_SERVERS_DNS
+
+%token _QSCALE_OFF_MIN
+%token _QSCALE_OFF_MAX
+
+%token _QUERYTIME_MAX
+%token _SETTIME_TIMEOUT
+
+%token _LOG_NEGLIGIBLE_ADJTIME
+%token _LOG_NEGLIGIBLE_ADJFREQ
+
+%token _FREQUENCY_SAMPLES
+%token _MAX_FREQUENCY_ADJUST
+
+%token _MAX_SEND_ERRORS
+
+%token _FILTER_ADJFREQ
+
+%token _AUTO_REPLIES
+%token _AUTO_THRESHOLD
+
+%token _INTERVAL_AUTO_DNSFAIL
+%token _TRIES_AUTO_DNSFAIL
+
+%token _SENSOR_DATA_MAXAGE
+%token _SENSOR_QUERY_INTERVAL
+%token _SENSOR_SCAN_INTERVAL
+
+%token _SENSOR_DEFAULT_REFID
+
+%token _CONSTRAINT_ERROR_MARGIN
+%token _CONSTRAINT_RETRY_INTERVAL
+%token _CONSTRAINT_SCAN_INTERVAL
+%token _CONSTRAINT_SCAN_TIMEOUT
+%token _CONSTRAINT_MARGIN
+
+%token _CONSTRAINT_MAXHEADERLENGTH
+
%token <v.string> STRING
%token <v.number> NUMBER
+%token <v.decimal> NUMBER_DOUBLE
%type <v.addr> address url urllist
%type <v.opts> listen_opts listen_opts_l listen_opt
%type <v.opts> server_opts server_opts_l server_opt
@@ -103,6 +161,9 @@ typedef struct {
%type <v.opts> weight
%type <v.opts> trusted
%type <v.opts> port
+
+%type <v.opts> pos_num
+%type <v.opts> pos_decimal
%%
grammar : /* empty */
@@ -385,6 +446,161 @@ main : LISTEN ON address listen_opts {
free($2);
TAILQ_INSERT_TAIL(&conf->ntp_conf_sensors, s, entry);
}
+
+ | _NTPD_USER STRING {
+ conf->ntpd_user = $2;
+ }
+ | _DRIFTFILE STRING {
+ conf->driftfile = $2;
+ }
+ | _CTLSOCKET STRING {
+ conf->ctlsocket = $2;
+ }
+
+ | _INTERVAL_QUERY_NORMAL pos_num {
+ conf->interval_query_normal = $2.pos_num;
+ }
+ | _INTERVAL_QUERY_PATHETIC pos_num {
+ conf->interval_query_pathetic = $2.pos_num;
+ }
+ | _INTERVAL_QUERY_AGGRESSIVE pos_num {
+ conf->interval_query_aggressive = $2.pos_num;
+ }
+ | _INTERVAL_QUERY_ULTRA_VIOLENCE pos_num {
+ conf->interval_query_ultra_violence = $2.pos_num;
+ }
+
+ | _INTERVAL_QUERY_TIMEOUT pos_num {
+ conf->interval_query_timeout = $2.pos_num;
+ }
+
+ | _TRUSTLEVEL_BADPEER pos_num {
+ conf->trustlevel_badpeer = $2.pos_num;
+ }
+ | _TRUSTLEVEL_PATHETIC pos_num {
+ conf->trustlevel_pathetic = $2.pos_num;
+ }
+ | _TRUSTLEVEL_AGGRESSIVE pos_num {
+ conf->trustlevel_aggressive = $2.pos_num;
+ }
+ | _TRUSTLEVEL_MAX pos_num {
+ conf->trustlevel_max = $2.pos_num;
+ }
+
+ | _MAX_SERVERS_DNS pos_num {
+ conf->max_servers_dns = $2.pos_num;
+ }
+
+ | _QSCALE_OFF_MIN pos_decimal {
+ conf->qscale_off_min = $2.pos_decimal;
+ }
+ | _QSCALE_OFF_MAX pos_decimal {
+ conf->qscale_off_max = $2.pos_decimal;
+ }
+
+ | _QUERYTIME_MAX pos_num {
+ conf->querytime_max = $2.pos_num;
+ }
+ | _SETTIME_TIMEOUT pos_num {
+ conf->settime_timeout = $2.pos_num;
+ }
+
+ | _LOG_NEGLIGIBLE_ADJTIME pos_num {
+ conf->log_negligible_adjtime = $2.pos_num;
+ }
+ | _LOG_NEGLIGIBLE_ADJFREQ pos_decimal {
+ conf->log_negligible_adjfreq = $2.pos_decimal;
+ }
+
+ | _FREQUENCY_SAMPLES pos_num {
+ conf->frequency_samples = $2.pos_num;
+ }
+ | _MAX_FREQUENCY_ADJUST pos_decimal {
+ conf->max_frequency_adjust = $2.pos_decimal;
+ }
+
+ | _MAX_SEND_ERRORS pos_num {
+ conf->max_send_errors = $2.pos_num;
+ }
+
+ | _AUTO_REPLIES pos_num {
+ conf->auto_replies = $2.pos_num;
+ }
+ | _AUTO_THRESHOLD pos_num {
+ conf->auto_threshold = $2.pos_num;
+ }
+ | _INTERVAL_AUTO_DNSFAIL pos_num {
+ conf->interval_auto_dnsfail = $2.pos_num;
+ }
+ | _TRIES_AUTO_DNSFAIL pos_num {
+ conf->tries_auto_dnsfail = $2.pos_num;
+ }
+
+ | _FILTER_ADJFREQ STRING {
+ u_int8_t val;
+
+ if (strcmp("true", $2) == 0) {
+ val = 0x01;
+ } else if (strcmp("false", $2) == 0) {
+ val = 0x00;
+ } else {
+ yyerror("option filter_adjfreq expects either 'true' or 'false'");
+ YYERROR;
+ }
+
+ conf->filter_adjfreq = val;
+ }
+
+ | _SENSOR_DATA_MAXAGE pos_num {
+ conf->sensor_data_maxage = $2.pos_num;
+ }
+ | _SENSOR_QUERY_INTERVAL pos_num {
+ conf->sensor_query_interval = $2.pos_num;
+ }
+ | _SENSOR_SCAN_INTERVAL pos_num {
+ conf->sensor_scan_interval = $2.pos_num;
+ }
+
+ | _SENSOR_DEFAULT_REFID STRING {
+ conf->sensor_default_refid = $2;
+ }
+
+ | _CONSTRAINT_ERROR_MARGIN pos_num {
+ conf->constraint_error_margin = $2.pos_num;
+ }
+ | _CONSTRAINT_RETRY_INTERVAL pos_num {
+ conf->constraint_retry_interval = $2.pos_num;
+ }
+ | _CONSTRAINT_SCAN_INTERVAL pos_num {
+ conf->constraint_scan_interval = $2.pos_num;
+ }
+ | _CONSTRAINT_SCAN_TIMEOUT pos_num {
+ conf->constraint_scan_timeout = $2.pos_num;
+ }
+ | _CONSTRAINT_MARGIN pos_num {
+ conf->constraint_margin = (double)$2.pos_num;
+ }
+ | _CONSTRAINT_MAXHEADERLENGTH pos_num {
+ conf->constraint_maxheaderlength = $2.pos_num;
+ }
+ ;
+
+pos_num : NUMBER {
+ if ($1 < 0) {
+ yyerror("must be a positive number");
+ YYERROR;
+ }
+ $$.pos_num = $1;
+ }
+ ;
+
+pos_decimal : NUMBER_DOUBLE {
+ if ($1 < 0) {
+ yyerror("must be a positive decimal number");
+ YYERROR;
+ }
+ $$.pos_decimal = $1;