xref: /freebsd/contrib/ntp/ntpd/ntp_util.c (revision 7d8f797b725e3efc0a4256554654780df83c456c)
1 /*
2  * ntp_util.c - stuff I didn't have any other place for
3  */
4 #ifdef HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7 
8 #include "ntpd.h"
9 #include "ntp_unixtime.h"
10 #include "ntp_filegen.h"
11 #include "ntp_if.h"
12 #include "ntp_stdlib.h"
13 #include "ntp_assert.h"
14 #include "ntp_calendar.h"
15 #include "ntp_leapsec.h"
16 #include "lib_strbuf.h"
17 
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_IOCTL_H
22 # include <sys/ioctl.h>
23 #endif
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <sys/stat.h>
28 
29 #ifdef HAVE_IEEEFP_H
30 # include <ieeefp.h>
31 #endif
32 #ifdef HAVE_MATH_H
33 # include <math.h>
34 #endif
35 
36 #if defined(VMS)
37 # include <descrip.h>
38 #endif /* VMS */
39 
40 /*
41  * Defines used by the leapseconds stuff
42  */
43 #define	MAX_TAI	100			/* max TAI offset (s) */
44 #define	L_DAY	86400UL			/* seconds per day */
45 #define	L_YEAR	(L_DAY * 365)		/* days per year */
46 #define	L_LYEAR	(L_YEAR + L_DAY)	/* days per leap year */
47 #define	L_4YEAR	(L_LYEAR + 3 * L_YEAR)	/* days per leap cycle */
48 #define	L_CENT	(L_4YEAR * 25)		/* days per century */
49 
50 /*
51  * This contains odds and ends, including the hourly stats, various
52  * configuration items, leapseconds stuff, etc.
53  */
54 /*
55  * File names
56  */
57 static	char *key_file_name;		/* keys file name */
58 static char	  *leapfile_name;		/* leapseconds file name */
59 static struct stat leapfile_stat;	/* leapseconds file stat() buffer */
60 static int /*BOOL*/have_leapfile = FALSE;
61 char	*stats_drift_file;		/* frequency file name */
62 static	char *stats_temp_file;		/* temp frequency file name */
63 static double wander_resid;		/* last frequency update */
64 double	wander_threshold = 1e-7;	/* initial frequency threshold */
65 
66 /*
67  * Statistics file stuff
68  */
69 #ifndef NTP_VAR
70 # ifndef SYS_WINNT
71 #  define NTP_VAR "/var/NTP/"		/* NOTE the trailing '/' */
72 # else
73 #  define NTP_VAR "c:\\var\\ntp\\"	/* NOTE the trailing '\\' */
74 # endif /* SYS_WINNT */
75 #endif
76 
77 
78 char statsdir[MAXFILENAME] = NTP_VAR;
79 static FILEGEN peerstats;
80 static FILEGEN loopstats;
81 static FILEGEN clockstats;
82 static FILEGEN rawstats;
83 static FILEGEN sysstats;
84 static FILEGEN protostats;
85 static FILEGEN cryptostats;
86 static FILEGEN timingstats;
87 
88 /*
89  * This controls whether stats are written to the fileset. Provided
90  * so that ntpdc can turn off stats when the file system fills up.
91  */
92 int stats_control;
93 
94 /*
95  * Last frequency written to file.
96  */
97 static double prev_drift_comp;		/* last frequency update */
98 
99 /*
100  * Function prototypes
101  */
102 static	void	record_sys_stats(void);
103 	void	ntpd_time_stepped(void);
104 static  void	check_leap_expiration(int, uint32_t, const time_t*);
105 
106 /*
107  * Prototypes
108  */
109 #ifdef DEBUG
110 void	uninit_util(void);
111 #endif
112 
113 /*
114  * uninit_util - free memory allocated by init_util
115  */
116 #ifdef DEBUG
117 void
118 uninit_util(void)
119 {
120 #if defined(_MSC_VER) && defined (_DEBUG)
121 	_CrtCheckMemory();
122 #endif
123 	if (stats_drift_file) {
124 		free(stats_drift_file);
125 		free(stats_temp_file);
126 		stats_drift_file = NULL;
127 		stats_temp_file = NULL;
128 	}
129 	if (key_file_name) {
130 		free(key_file_name);
131 		key_file_name = NULL;
132 	}
133 	filegen_unregister("peerstats");
134 	filegen_unregister("loopstats");
135 	filegen_unregister("clockstats");
136 	filegen_unregister("rawstats");
137 	filegen_unregister("sysstats");
138 	filegen_unregister("protostats");
139 #ifdef AUTOKEY
140 	filegen_unregister("cryptostats");
141 #endif	/* AUTOKEY */
142 #ifdef DEBUG_TIMING
143 	filegen_unregister("timingstats");
144 #endif	/* DEBUG_TIMING */
145 
146 #if defined(_MSC_VER) && defined (_DEBUG)
147 	_CrtCheckMemory();
148 #endif
149 }
150 #endif /* DEBUG */
151 
152 
153 /*
154  * init_util - initialize the util module of ntpd
155  */
156 void
157 init_util(void)
158 {
159 	filegen_register(statsdir, "peerstats",	  &peerstats);
160 	filegen_register(statsdir, "loopstats",	  &loopstats);
161 	filegen_register(statsdir, "clockstats",  &clockstats);
162 	filegen_register(statsdir, "rawstats",	  &rawstats);
163 	filegen_register(statsdir, "sysstats",	  &sysstats);
164 	filegen_register(statsdir, "protostats",  &protostats);
165 	filegen_register(statsdir, "cryptostats", &cryptostats);
166 	filegen_register(statsdir, "timingstats", &timingstats);
167 	/*
168 	 * register with libntp ntp_set_tod() to call us back
169 	 * when time is stepped.
170 	 */
171 	step_callback = &ntpd_time_stepped;
172 #ifdef DEBUG
173 	atexit(&uninit_util);
174 #endif /* DEBUG */
175 }
176 
177 
178 /*
179  * hourly_stats - print some interesting stats
180  */
181 void
182 write_stats(void)
183 {
184 	FILE	*fp;
185 #ifdef DOSYNCTODR
186 	struct timeval tv;
187 #if !defined(VMS)
188 	int	prio_set;
189 #endif
190 #ifdef HAVE_GETCLOCK
191 	struct timespec ts;
192 #endif
193 	int	o_prio;
194 
195 	/*
196 	 * Sometimes having a Sun can be a drag.
197 	 *
198 	 * The kernel variable dosynctodr controls whether the system's
199 	 * soft clock is kept in sync with the battery clock. If it
200 	 * is zero, then the soft clock is not synced, and the battery
201 	 * clock is simply left to rot. That means that when the system
202 	 * reboots, the battery clock (which has probably gone wacky)
203 	 * sets the soft clock. That means ntpd starts off with a very
204 	 * confused idea of what time it is. It then takes a large
205 	 * amount of time to figure out just how wacky the battery clock
206 	 * has made things drift, etc, etc. The solution is to make the
207 	 * battery clock sync up to system time. The way to do THAT is
208 	 * to simply set the time of day to the current time of day, but
209 	 * as quickly as possible. This may, or may not be a sensible
210 	 * thing to do.
211 	 *
212 	 * CAVEAT: settimeofday() steps the sun clock by about 800 us,
213 	 *	   so setting DOSYNCTODR seems a bad idea in the
214 	 *	   case of us resolution
215 	 */
216 
217 #if !defined(VMS)
218 	/*
219 	 * (prr) getpriority returns -1 on error, but -1 is also a valid
220 	 * return value (!), so instead we have to zero errno before the
221 	 * call and check it for non-zero afterwards.
222 	 */
223 	errno = 0;
224 	prio_set = 0;
225 	o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
226 
227 	/*
228 	 * (prr) if getpriority succeeded, call setpriority to raise
229 	 * scheduling priority as high as possible.  If that succeeds
230 	 * as well, set the prio_set flag so we remember to reset
231 	 * priority to its previous value below.  Note that on Solaris
232 	 * 2.6 (and beyond?), both getpriority and setpriority will fail
233 	 * with ESRCH, because sched_setscheduler (called from main) put
234 	 * us in the real-time scheduling class which setpriority
235 	 * doesn't know about. Being in the real-time class is better
236 	 * than anything setpriority can do, anyhow, so this error is
237 	 * silently ignored.
238 	 */
239 	if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
240 		prio_set = 1;	/* overdrive */
241 #endif /* VMS */
242 #ifdef HAVE_GETCLOCK
243 	(void) getclock(TIMEOFDAY, &ts);
244 	tv.tv_sec = ts.tv_sec;
245 	tv.tv_usec = ts.tv_nsec / 1000;
246 #else /*  not HAVE_GETCLOCK */
247 	GETTIMEOFDAY(&tv,(struct timezone *)NULL);
248 #endif /* not HAVE_GETCLOCK */
249 	if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0)
250 		msyslog(LOG_ERR, "can't sync battery time: %m");
251 #if !defined(VMS)
252 	if (prio_set)
253 		setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
254 #endif /* VMS */
255 #endif /* DOSYNCTODR */
256 	record_sys_stats();
257 	if (stats_drift_file != 0) {
258 
259 		/*
260 		 * When the frequency file is written, initialize the
261 		 * prev_drift_comp and wander_resid. Thereafter,
262 		 * reduce the wander_resid by half each hour. When
263 		 * the difference between the prev_drift_comp and
264 		 * drift_comp is less than the wander_resid, update
265 		 * the frequncy file. This minimizes the file writes to
266 		 * nonvolaile storage.
267 		 */
268 #ifdef DEBUG
269 		if (debug)
270 			printf("write_stats: frequency %.6lf thresh %.6lf, freq %.6lf\n",
271 			    (prev_drift_comp - drift_comp) * 1e6, wander_resid *
272 			    1e6, drift_comp * 1e6);
273 #endif
274 		if (fabs(prev_drift_comp - drift_comp) < wander_resid) {
275 			wander_resid *= 0.5;
276 			return;
277 		}
278 		prev_drift_comp = drift_comp;
279 		wander_resid = wander_threshold;
280 		if ((fp = fopen(stats_temp_file, "w")) == NULL) {
281 			msyslog(LOG_ERR, "frequency file %s: %m",
282 			    stats_temp_file);
283 			return;
284 		}
285 		fprintf(fp, "%.3f\n", drift_comp * 1e6);
286 		(void)fclose(fp);
287 		/* atomic */
288 #ifdef SYS_WINNT
289 		if (_unlink(stats_drift_file)) /* rename semantics differ under NT */
290 			msyslog(LOG_WARNING,
291 				"Unable to remove prior drift file %s, %m",
292 				stats_drift_file);
293 #endif /* SYS_WINNT */
294 
295 #ifndef NO_RENAME
296 		if (rename(stats_temp_file, stats_drift_file))
297 			msyslog(LOG_WARNING,
298 				"Unable to rename temp drift file %s to %s, %m",
299 				stats_temp_file, stats_drift_file);
300 #else
301 		/* we have no rename NFS of ftp in use */
302 		if ((fp = fopen(stats_drift_file, "w")) ==
303 		    NULL) {
304 			msyslog(LOG_ERR,
305 			    "frequency file %s: %m",
306 			    stats_drift_file);
307 			return;
308 		}
309 #endif
310 
311 #if defined(VMS)
312 		/* PURGE */
313 		{
314 			$DESCRIPTOR(oldvers,";-1");
315 			struct dsc$descriptor driftdsc = {
316 				strlen(stats_drift_file), 0, 0,
317 				    stats_drift_file };
318 			while(lib$delete_file(&oldvers,
319 			    &driftdsc) & 1);
320 		}
321 #endif
322 	}
323 }
324 
325 
326 /*
327  * stats_config - configure the stats operation
328  */
329 void
330 stats_config(
331 	int item,
332 	const char *invalue	/* only one type so far */
333 	)
334 {
335 	FILE	*fp;
336 	const char *value;
337 	int	len;
338 	double	old_drift;
339 	l_fp	now;
340 	time_t  ttnow;
341 #ifndef VMS
342 	const char temp_ext[] = ".TEMP";
343 #else
344 	const char temp_ext[] = "-TEMP";
345 #endif
346 
347 	/*
348 	 * Expand environment strings under Windows NT, since the
349 	 * command interpreter doesn't do this, the program must.
350 	 */
351 #ifdef SYS_WINNT
352 	char newvalue[MAX_PATH], parameter[MAX_PATH];
353 
354 	if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
355 		switch (item) {
356 		case STATS_FREQ_FILE:
357 			strlcpy(parameter, "STATS_FREQ_FILE",
358 				sizeof(parameter));
359 			break;
360 
361 		case STATS_LEAP_FILE:
362 			strlcpy(parameter, "STATS_LEAP_FILE",
363 				sizeof(parameter));
364 			break;
365 
366 		case STATS_STATSDIR:
367 			strlcpy(parameter, "STATS_STATSDIR",
368 				sizeof(parameter));
369 			break;
370 
371 		case STATS_PID_FILE:
372 			strlcpy(parameter, "STATS_PID_FILE",
373 				sizeof(parameter));
374 			break;
375 
376 		default:
377 			strlcpy(parameter, "UNKNOWN",
378 				sizeof(parameter));
379 			break;
380 		}
381 		value = invalue;
382 		msyslog(LOG_ERR,
383 			"ExpandEnvironmentStrings(%s) failed: %m\n",
384 			parameter);
385 	} else {
386 		value = newvalue;
387 	}
388 #else
389 	value = invalue;
390 #endif /* SYS_WINNT */
391 
392 	switch (item) {
393 
394 	/*
395 	 * Open and read frequency file.
396 	 */
397 	case STATS_FREQ_FILE:
398 		if (!value || (len = strlen(value)) == 0)
399 			break;
400 
401 		stats_drift_file = erealloc(stats_drift_file, len + 1);
402 		stats_temp_file = erealloc(stats_temp_file,
403 		    len + sizeof(".TEMP"));
404 		memcpy(stats_drift_file, value, (size_t)(len+1));
405 		memcpy(stats_temp_file, value, (size_t)len);
406 		memcpy(stats_temp_file + len, temp_ext, sizeof(temp_ext));
407 
408 		/*
409 		 * Open drift file and read frequency. If the file is
410 		 * missing or contains errors, tell the loop to reset.
411 		 */
412 		if ((fp = fopen(stats_drift_file, "r")) == NULL)
413 			break;
414 
415 		if (fscanf(fp, "%lf", &old_drift) != 1) {
416 			msyslog(LOG_ERR,
417 				"format error frequency file %s",
418 				stats_drift_file);
419 			fclose(fp);
420 			break;
421 
422 		}
423 		fclose(fp);
424 		loop_config(LOOP_FREQ, old_drift);
425 		prev_drift_comp = drift_comp;
426 		break;
427 
428 	/*
429 	 * Specify statistics directory.
430 	 */
431 	case STATS_STATSDIR:
432 
433 		/* - 1 since value may be missing the DIR_SEP. */
434 		if (strlen(value) >= sizeof(statsdir) - 1) {
435 			msyslog(LOG_ERR,
436 			    "statsdir too long (>%d, sigh)",
437 			    (int)sizeof(statsdir) - 2);
438 		} else {
439 			int add_dir_sep;
440 			int value_l;
441 
442 			/* Add a DIR_SEP unless we already have one. */
443 			value_l = strlen(value);
444 			if (0 == value_l)
445 				add_dir_sep = FALSE;
446 			else
447 				add_dir_sep = (DIR_SEP !=
448 				    value[value_l - 1]);
449 
450 			if (add_dir_sep)
451 				snprintf(statsdir, sizeof(statsdir),
452 				    "%s%c", value, DIR_SEP);
453 			else
454 				snprintf(statsdir, sizeof(statsdir),
455 				    "%s", value);
456 			filegen_statsdir();
457 		}
458 		break;
459 
460 	/*
461 	 * Open pid file.
462 	 */
463 	case STATS_PID_FILE:
464 		if ((fp = fopen(value, "w")) == NULL) {
465 			msyslog(LOG_ERR, "pid file %s: %m",
466 			    value);
467 			break;
468 		}
469 		fprintf(fp, "%d", (int)getpid());
470 		fclose(fp);
471 		break;
472 
473 	/*
474 	 * Read leapseconds file.
475 	 *
476 	 * Note: Currently a leap file without SHA1 signature is
477 	 * accepted, but if there is a signature line, the signature
478 	 * must be valid or the file is rejected.
479 	 */
480 	case STATS_LEAP_FILE:
481 		if (!value || (len = strlen(value)) == 0)
482 			break;
483 
484 		leapfile_name = erealloc(leapfile_name, len + 1);
485 		memcpy(leapfile_name, value, len + 1);
486 
487 		if (leapsec_load_file(
488 			    leapfile_name, &leapfile_stat, TRUE, TRUE))
489 		{
490 			leap_signature_t lsig;
491 
492 			get_systime(&now);
493 			time(&ttnow);
494 			leapsec_getsig(&lsig);
495 			mprintf_event(EVNT_TAI, NULL,
496 				      "%d leap %s %s %s",
497 				      lsig.taiof,
498 				      fstostr(lsig.ttime),
499 				      leapsec_expired(now.l_ui, NULL)
500 					  ? "expired"
501 					  : "expires",
502 				      fstostr(lsig.etime));
503 
504 			have_leapfile = TRUE;
505 
506 			/* force an immediate daily expiration check of
507 			 * the leap seconds table
508 			 */
509 			check_leap_expiration(TRUE, now.l_ui, &ttnow);
510 		}
511 		break;
512 
513 	default:
514 		/* oh well */
515 		break;
516 	}
517 }
518 
519 
520 /*
521  * record_peer_stats - write peer statistics to file
522  *
523  * file format:
524  * day (MJD)
525  * time (s past UTC midnight)
526  * IP address
527  * status word (hex)
528  * offset
529  * delay
530  * dispersion
531  * jitter
532 */
533 void
534 record_peer_stats(
535 	sockaddr_u *addr,
536 	int	status,
537 	double	offset,		/* offset */
538 	double	delay,		/* delay */
539 	double	dispersion,	/* dispersion */
540 	double	jitter		/* jitter */
541 	)
542 {
543 	l_fp	now;
544 	u_long	day;
545 
546 	if (!stats_control)
547 		return;
548 
549 	get_systime(&now);
550 	filegen_setup(&peerstats, now.l_ui);
551 	day = now.l_ui / 86400 + MJD_1900;
552 	now.l_ui %= 86400;
553 	if (peerstats.fp != NULL) {
554 		fprintf(peerstats.fp,
555 		    "%lu %s %s %x %.9f %.9f %.9f %.9f\n", day,
556 		    ulfptoa(&now, 3), stoa(addr), status, offset,
557 		    delay, dispersion, jitter);
558 		fflush(peerstats.fp);
559 	}
560 }
561 
562 
563 /*
564  * record_loop_stats - write loop filter statistics to file
565  *
566  * file format:
567  * day (MJD)
568  * time (s past midnight)
569  * offset
570  * frequency (PPM)
571  * jitter
572  * wnder (PPM)
573  * time constant (log2)
574  */
575 void
576 record_loop_stats(
577 	double	offset,		/* offset */
578 	double	freq,		/* frequency (PPM) */
579 	double	jitter,		/* jitter */
580 	double	wander,		/* wander (PPM) */
581 	int spoll
582 	)
583 {
584 	l_fp	now;
585 	u_long	day;
586 
587 	if (!stats_control)
588 		return;
589 
590 	get_systime(&now);
591 	filegen_setup(&loopstats, now.l_ui);
592 	day = now.l_ui / 86400 + MJD_1900;
593 	now.l_ui %= 86400;
594 	if (loopstats.fp != NULL) {
595 		fprintf(loopstats.fp, "%lu %s %.9f %.3f %.9f %.6f %d\n",
596 		    day, ulfptoa(&now, 3), offset, freq * 1e6, jitter,
597 		    wander * 1e6, spoll);
598 		fflush(loopstats.fp);
599 	}
600 }
601 
602 
603 /*
604  * record_clock_stats - write clock statistics to file
605  *
606  * file format:
607  * day (MJD)
608  * time (s past midnight)
609  * IP address
610  * text message
611  */
612 void
613 record_clock_stats(
614 	sockaddr_u *addr,
615 	const char *text	/* timecode string */
616 	)
617 {
618 	l_fp	now;
619 	u_long	day;
620 
621 	if (!stats_control)
622 		return;
623 
624 	get_systime(&now);
625 	filegen_setup(&clockstats, now.l_ui);
626 	day = now.l_ui / 86400 + MJD_1900;
627 	now.l_ui %= 86400;
628 	if (clockstats.fp != NULL) {
629 		fprintf(clockstats.fp, "%lu %s %s %s\n", day,
630 		    ulfptoa(&now, 3), stoa(addr), text);
631 		fflush(clockstats.fp);
632 	}
633 }
634 
635 
636 /*
637  * mprintf_clock_stats - write clock statistics to file with
638  *			msnprintf-style formatting.
639  */
640 int
641 mprintf_clock_stats(
642 	sockaddr_u *addr,
643 	const char *fmt,
644 	...
645 	)
646 {
647 	va_list	ap;
648 	int	rc;
649 	char	msg[512];
650 
651 	va_start(ap, fmt);
652 	rc = mvsnprintf(msg, sizeof(msg), fmt, ap);
653 	va_end(ap);
654 	if (stats_control)
655 		record_clock_stats(addr, msg);
656 
657 	return rc;
658 }
659 
660 /*
661  * record_raw_stats - write raw timestamps to file
662  *
663  * file format
664  * day (MJD)
665  * time (s past midnight)
666  * peer ip address
667  * IP address
668  * t1 t2 t3 t4 timestamps
669  */
670 void
671 record_raw_stats(
672 	sockaddr_u *srcadr,
673 	sockaddr_u *dstadr,
674 	l_fp	*t1,		/* originate timestamp */
675 	l_fp	*t2,		/* receive timestamp */
676 	l_fp	*t3,		/* transmit timestamp */
677 	l_fp	*t4,		/* destination timestamp */
678 	int	leap,
679 	int	version,
680 	int	mode,
681 	int	stratum,
682 	int	ppoll,
683 	int	precision,
684 	double	root_delay,	/* seconds */
685 	double	root_dispersion,/* seconds */
686 	u_int32	refid
687 	)
688 {
689 	l_fp	now;
690 	u_long	day;
691 
692 	if (!stats_control)
693 		return;
694 
695 	get_systime(&now);
696 	filegen_setup(&rawstats, now.l_ui);
697 	day = now.l_ui / 86400 + MJD_1900;
698 	now.l_ui %= 86400;
699 	if (rawstats.fp != NULL) {
700 		fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s %d %d %d %d %d %d %.6f %.6f %s\n",
701 		    day, ulfptoa(&now, 3),
702 		    stoa(srcadr), dstadr ?  stoa(dstadr) : "-",
703 		    ulfptoa(t1, 9), ulfptoa(t2, 9),
704 		    ulfptoa(t3, 9), ulfptoa(t4, 9),
705 		    leap, version, mode, stratum, ppoll, precision,
706 		    root_delay, root_dispersion, refid_str(refid, stratum));
707 		fflush(rawstats.fp);
708 	}
709 }
710 
711 
712 /*
713  * record_sys_stats - write system statistics to file
714  *
715  * file format
716  * day (MJD)
717  * time (s past midnight)
718  * time since reset
719  * packets recieved
720  * packets for this host
721  * current version
722  * old version
723  * access denied
724  * bad length or format
725  * bad authentication
726  * declined
727  * rate exceeded
728  * KoD sent
729  */
730 void
731 record_sys_stats(void)
732 {
733 	l_fp	now;
734 	u_long	day;
735 
736 	if (!stats_control)
737 		return;
738 
739 	get_systime(&now);
740 	filegen_setup(&sysstats, now.l_ui);
741 	day = now.l_ui / 86400 + MJD_1900;
742 	now.l_ui %= 86400;
743 	if (sysstats.fp != NULL) {
744 		fprintf(sysstats.fp,
745 		    "%lu %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
746 		    day, ulfptoa(&now, 3), current_time - sys_stattime,
747 		    sys_received, sys_processed, sys_newversion,
748 		    sys_oldversion, sys_restricted, sys_badlength,
749 		    sys_badauth, sys_declined, sys_limitrejected,
750 		    sys_kodsent);
751 		fflush(sysstats.fp);
752 		proto_clr_stats();
753 	}
754 }
755 
756 
757 /*
758  * record_proto_stats - write system statistics to file
759  *
760  * file format
761  * day (MJD)
762  * time (s past midnight)
763  * text message
764  */
765 void
766 record_proto_stats(
767 	char	*str		/* text string */
768 	)
769 {
770 	l_fp	now;
771 	u_long	day;
772 
773 	if (!stats_control)
774 		return;
775 
776 	get_systime(&now);
777 	filegen_setup(&protostats, now.l_ui);
778 	day = now.l_ui / 86400 + MJD_1900;
779 	now.l_ui %= 86400;
780 	if (protostats.fp != NULL) {
781 		fprintf(protostats.fp, "%lu %s %s\n", day,
782 		    ulfptoa(&now, 3), str);
783 		fflush(protostats.fp);
784 	}
785 }
786 
787 
788 #ifdef AUTOKEY
789 /*
790  * record_crypto_stats - write crypto statistics to file
791  *
792  * file format:
793  * day (mjd)
794  * time (s past midnight)
795  * peer ip address
796  * text message
797  */
798 void
799 record_crypto_stats(
800 	sockaddr_u *addr,
801 	const char *text	/* text message */
802 	)
803 {
804 	l_fp	now;
805 	u_long	day;
806 
807 	if (!stats_control)
808 		return;
809 
810 	get_systime(&now);
811 	filegen_setup(&cryptostats, now.l_ui);
812 	day = now.l_ui / 86400 + MJD_1900;
813 	now.l_ui %= 86400;
814 	if (cryptostats.fp != NULL) {
815 		if (addr == NULL)
816 			fprintf(cryptostats.fp, "%lu %s 0.0.0.0 %s\n",
817 			    day, ulfptoa(&now, 3), text);
818 		else
819 			fprintf(cryptostats.fp, "%lu %s %s %s\n",
820 			    day, ulfptoa(&now, 3), stoa(addr), text);
821 		fflush(cryptostats.fp);
822 	}
823 }
824 #endif	/* AUTOKEY */
825 
826 
827 #ifdef DEBUG_TIMING
828 /*
829  * record_timing_stats - write timing statistics to file
830  *
831  * file format:
832  * day (mjd)
833  * time (s past midnight)
834  * text message
835  */
836 void
837 record_timing_stats(
838 	const char *text	/* text message */
839 	)
840 {
841 	static unsigned int flshcnt;
842 	l_fp	now;
843 	u_long	day;
844 
845 	if (!stats_control)
846 		return;
847 
848 	get_systime(&now);
849 	filegen_setup(&timingstats, now.l_ui);
850 	day = now.l_ui / 86400 + MJD_1900;
851 	now.l_ui %= 86400;
852 	if (timingstats.fp != NULL) {
853 		fprintf(timingstats.fp, "%lu %s %s\n", day, lfptoa(&now,
854 		    3), text);
855 		if (++flshcnt % 100 == 0)
856 			fflush(timingstats.fp);
857 	}
858 }
859 #endif
860 
861 
862 /*
863  * check_leap_file - See if the leapseconds file has been updated.
864  *
865  * Returns: n/a
866  *
867  * Note: This loads a new leapfile on the fly. Currently a leap file
868  * without SHA1 signature is accepted, but if there is a signature line,
869  * the signature must be valid or the file is rejected.
870  */
871 void
872 check_leap_file(
873 	int           is_daily_check,
874 	uint32_t      ntptime       ,
875 	const time_t *systime
876 	)
877 {
878 	/* just do nothing if there is no leap file */
879 	if ( ! (leapfile_name && *leapfile_name))
880 		return;
881 
882 	/* try to load leapfile, force it if no leapfile loaded yet */
883 	if (leapsec_load_file(
884 		    leapfile_name, &leapfile_stat,
885 		    !have_leapfile, is_daily_check))
886 		have_leapfile = TRUE;
887 	else if (!have_leapfile)
888 		return;
889 
890 	check_leap_expiration(is_daily_check, ntptime, systime);
891 }
892 
893 /*
894  * check expiration of a loaded leap table
895  */
896 static void
897 check_leap_expiration(
898 	int           is_daily_check,
899 	uint32_t      ntptime       ,
900 	const time_t *systime
901 	)
902 {
903 	static const char * const logPrefix = "leapsecond file";
904 	int  rc;
905 
906 	/* test the expiration of the leap data and log with proper
907 	 * level and frequency (once/hour or once/day, depending on the
908 	 * state.
909 	 */
910 	rc = leapsec_daystolive(ntptime, systime);
911 	if (rc == 0) {
912 		msyslog(LOG_WARNING,
913 			"%s ('%s'): will expire in less than one day",
914 			logPrefix, leapfile_name);
915 	} else if (is_daily_check && rc < 28) {
916 		if (rc < 0)
917 			msyslog(LOG_ERR,
918 				"%s ('%s'): expired less than %d day%s ago",
919 				logPrefix, leapfile_name, -rc, (rc == -1 ? "" : "s"));
920 		else
921 			msyslog(LOG_WARNING,
922 				"%s ('%s'): will expire in less than %d days",
923 				logPrefix, leapfile_name, 1+rc);
924 	}
925 }
926 
927 
928 /*
929  * getauthkeys - read the authentication keys from the specified file
930  */
931 void
932 getauthkeys(
933 	const char *keyfile
934 	)
935 {
936 	int len;
937 
938 	len = strlen(keyfile);
939 	if (!len)
940 		return;
941 
942 #ifndef SYS_WINNT
943 	key_file_name = erealloc(key_file_name, len + 1);
944 	memcpy(key_file_name, keyfile, len + 1);
945 #else
946 	key_file_name = erealloc(key_file_name, _MAX_PATH);
947 	if (len + 1 > _MAX_PATH)
948 		return;
949 	if (!ExpandEnvironmentStrings(keyfile, key_file_name,
950 				      _MAX_PATH)) {
951 		msyslog(LOG_ERR,
952 			"ExpandEnvironmentStrings(KEY_FILE) failed: %m");
953 		strlcpy(key_file_name, keyfile, _MAX_PATH);
954 	}
955 	key_file_name = erealloc(key_file_name,
956 				 1 + strlen(key_file_name));
957 #endif /* SYS_WINNT */
958 
959 	authreadkeys(key_file_name);
960 }
961 
962 
963 /*
964  * rereadkeys - read the authentication key file over again.
965  */
966 void
967 rereadkeys(void)
968 {
969 	if (NULL != key_file_name)
970 		authreadkeys(key_file_name);
971 }
972 
973 
974 #if notyet
975 /*
976  * ntp_exit - document explicitly that ntpd has exited
977  */
978 void
979 ntp_exit(int retval)
980 {
981 	msyslog(LOG_ERR, "EXITING with return code %d", retval);
982 	exit(retval);
983 }
984 #endif
985 
986 /*
987  * fstostr - prettyprint NTP seconds
988  */
989 char * fstostr(
990 	time_t	ntp_stamp
991 	)
992 {
993 	char *		buf;
994 	struct calendar tm;
995 
996 	LIB_GETBUF(buf);
997 	if (ntpcal_ntp_to_date(&tm, (u_int32)ntp_stamp, NULL) < 0)
998 		snprintf(buf, LIB_BUFLENGTH, "ntpcal_ntp_to_date: %ld: range error",
999 			 (long)ntp_stamp);
1000 	else
1001 		snprintf(buf, LIB_BUFLENGTH, "%04d%02d%02d%02d%02d",
1002 			 tm.year, tm.month, tm.monthday,
1003 			 tm.hour, tm.minute);
1004 	return buf;
1005 }
1006 
1007 
1008 /*
1009  * ntpd_time_stepped is called back by step_systime(), allowing ntpd
1010  * to do any one-time processing necessitated by the step.
1011  */
1012 void
1013 ntpd_time_stepped(void)
1014 {
1015 	u_int saved_mon_enabled;
1016 
1017 	/*
1018 	 * flush the monitor MRU list which contains l_fp timestamps
1019 	 * which should not be compared across the step.
1020 	 */
1021 	if (MON_OFF != mon_enabled) {
1022 		saved_mon_enabled = mon_enabled;
1023 		mon_stop(MON_OFF);
1024 		mon_start(saved_mon_enabled);
1025 	}
1026 
1027 	/* inform interpolating Windows code to allow time to go back */
1028 #ifdef SYS_WINNT
1029 	win_time_stepped();
1030 #endif
1031 }
1032