]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ntp/ntpd/ntpd.c
Fix multiple denial of service in ntpd.
[FreeBSD/FreeBSD.git] / contrib / ntp / ntpd / ntpd.c
1 /*
2  * ntpd.c - main program for the fixed point NTP daemon
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include "ntp_machine.h"
10 #include "ntpd.h"
11 #include "ntp_io.h"
12 #include "ntp_stdlib.h"
13 #include <ntp_random.h>
14
15 #include "ntp_config.h"
16 #include "ntp_syslog.h"
17 #include "ntp_assert.h"
18 #include "isc/error.h"
19 #include "isc/strerror.h"
20 #include "isc/formatcheck.h"
21 #include "iosignal.h"
22
23 #ifdef SIM
24 # include "ntpsim.h"
25 #endif
26
27 #include "ntp_libopts.h"
28 #include "ntpd-opts.h"
29
30 /* there's a short treatise below what the thread stuff is for.
31  * [Bug 2954] enable the threading warm-up only for Linux.
32  */
33 #if defined(HAVE_PTHREADS) && HAVE_PTHREADS && !defined(NO_THREADS)
34 # ifdef HAVE_PTHREAD_H
35 #  include <pthread.h>
36 # endif
37 # if defined(linux)
38 #  define NEED_PTHREAD_WARMUP
39 # endif
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
47 #endif
48 #ifdef HAVE_SYS_WAIT_H
49 # include <sys/wait.h>
50 #endif
51 #include <stdio.h>
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
54 #endif
55 #ifdef HAVE_SYS_SIGNAL_H
56 # include <sys/signal.h>
57 #else
58 # include <signal.h>
59 #endif
60 #ifdef HAVE_SYS_IOCTL_H
61 # include <sys/ioctl.h>
62 #endif /* HAVE_SYS_IOCTL_H */
63 #if defined(HAVE_RTPRIO)
64 # ifdef HAVE_SYS_LOCK_H
65 #  include <sys/lock.h>
66 # endif
67 # include <sys/rtprio.h>
68 #else
69 # ifdef HAVE_PLOCK
70 #  ifdef HAVE_SYS_LOCK_H
71 #       include <sys/lock.h>
72 #  endif
73 # endif
74 #endif
75 #if defined(HAVE_SCHED_SETSCHEDULER)
76 # ifdef HAVE_SCHED_H
77 #  include <sched.h>
78 # else
79 #  ifdef HAVE_SYS_SCHED_H
80 #   include <sys/sched.h>
81 #  endif
82 # endif
83 #endif
84 #if defined(HAVE_SYS_MMAN_H)
85 # include <sys/mman.h>
86 #endif
87
88 #ifdef HAVE_SYSEXITS_H
89 # include <sysexits.h>
90 #endif
91
92 #ifdef HAVE_TERMIOS_H
93 # include <termios.h>
94 #endif
95
96 #ifdef SYS_DOMAINOS
97 # include <apollo/base.h>
98 #endif /* SYS_DOMAINOS */
99
100
101 #include "recvbuff.h"
102 #include "ntp_cmdargs.h"
103
104 #if 0                           /* HMS: I don't think we need this. 961223 */
105 #ifdef LOCK_PROCESS
106 # ifdef SYS_SOLARIS
107 #  include <sys/mman.h>
108 # else
109 #  include <sys/lock.h>
110 # endif
111 #endif
112 #endif
113
114 #ifdef SYS_WINNT
115 # include "ntservice.h"
116 #endif
117
118 #ifdef _AIX
119 # include <ulimit.h>
120 #endif /* _AIX */
121
122 #ifdef SCO5_CLOCK
123 # include <sys/ci/ciioctl.h>
124 #endif
125
126 #ifdef HAVE_DROPROOT
127 # include <ctype.h>
128 # include <grp.h>
129 # include <pwd.h>
130 #ifdef HAVE_LINUX_CAPABILITIES
131 # include <sys/capability.h>
132 # include <sys/prctl.h>
133 #endif /* HAVE_LINUX_CAPABILITIES */
134 #if defined(HAVE_PRIV_H) && defined(HAVE_SOLARIS_PRIVS)
135 # include <priv.h>
136 #endif /* HAVE_PRIV_H */
137 #endif /* HAVE_DROPROOT */
138
139 #if defined (LIBSECCOMP) && (KERN_SECCOMP)
140 /* # include <sys/types.h> */
141 # include <sys/resource.h>
142 # include <seccomp.h>
143 #endif /* LIBSECCOMP and KERN_SECCOMP */
144
145 #ifdef HAVE_DNSREGISTRATION
146 # include <dns_sd.h>
147 DNSServiceRef mdns;
148 #endif
149
150 /* In case 'sysexits.h' is unavailable, define some exit codes here: */
151 #ifndef EX_SOFTWARE
152 # define EX_SOFTWARE    70
153 #endif
154 #ifndef EX_OSERR
155 # define EX_OSERR       71
156 #endif
157 #ifndef EX_IOERR
158 # define EX_IOERR       74
159 #endif
160 #ifndef EX_PROTOCOL
161 #define EX_PROTOCOL     76
162 #endif
163
164
165 #ifdef HAVE_SETPGRP_0
166 # define ntp_setpgrp(x, y)      setpgrp()
167 #else
168 # define ntp_setpgrp(x, y)      setpgrp(x, y)
169 #endif
170
171 #ifdef HAVE_SOLARIS_PRIVS
172 # define LOWPRIVS "basic,sys_time,net_privaddr,proc_setid,!proc_info,!proc_session,!proc_exec"
173 static priv_set_t *lowprivs = NULL;
174 static priv_set_t *highprivs = NULL;
175 #endif /* HAVE_SOLARIS_PRIVS */
176 /*
177  * Scheduling priority we run at
178  */
179 #define NTPD_PRIO       (-12)
180
181 int priority_done = 2;          /* 0 - Set priority */
182                                 /* 1 - priority is OK where it is */
183                                 /* 2 - Don't set priority */
184                                 /* 1 and 2 are pretty much the same */
185
186 int listen_to_virtual_ips = TRUE;
187
188 /*
189  * No-fork flag.  If set, we do not become a background daemon.
190  */
191 int nofork;                     /* Fork by default */
192
193 #ifdef HAVE_DNSREGISTRATION
194 /*
195  * mDNS registration flag. If set, we attempt to register with the mDNS system, but only
196  * after we have synched the first time. If the attempt fails, then try again once per 
197  * minute for up to 5 times. After all, we may be starting before mDNS.
198  */
199 int mdnsreg = FALSE;
200 int mdnstries = 5;
201 #endif  /* HAVE_DNSREGISTRATION */
202
203 #ifdef HAVE_LINUX_CAPABILITIES
204 int have_caps;          /* runtime check whether capabilities work */
205 #endif /* HAVE_LINUX_CAPABILITIES */
206
207 #ifdef HAVE_DROPROOT
208 int droproot;
209 int root_dropped;
210 char *user;             /* User to switch to */
211 char *group;            /* group to switch to */
212 const char *chrootdir;  /* directory to chroot to */
213 uid_t sw_uid;
214 gid_t sw_gid;
215 struct group *gr;
216 struct passwd *pw;
217 #endif /* HAVE_DROPROOT */
218
219 #ifdef HAVE_WORKING_FORK
220 int     daemon_pipe[2] = { -1, -1 };
221 #endif
222
223 /*
224  * Version declaration
225  */
226 extern const char *Version;
227
228 char const *progname;
229
230 int was_alarmed;
231
232 #ifdef DECL_SYSCALL
233 /*
234  * We put this here, since the argument profile is syscall-specific
235  */
236 extern int syscall      (int, ...);
237 #endif /* DECL_SYSCALL */
238
239
240 #if !defined(SIM) && defined(SIGDIE1)
241 static volatile int signalled   = 0;
242 static volatile int signo       = 0;
243
244 /* In an ideal world, 'finish_safe()' would declared as noreturn... */
245 static  void            finish_safe     (int);
246 static  RETSIGTYPE      finish          (int);
247 #endif
248
249 #if !defined(SIM) && defined(HAVE_WORKING_FORK)
250 static int      wait_child_sync_if      (int, unsigned long);
251 static int      wait_child_exit_if      (pid_t, int);
252 #endif
253
254 #if !defined(SIM) && !defined(SYS_WINNT)
255 # ifdef DEBUG
256 static  RETSIGTYPE      moredebug       (int);
257 static  RETSIGTYPE      lessdebug       (int);
258 # else  /* !DEBUG follows */
259 static  RETSIGTYPE      no_debug        (int);
260 # endif /* !DEBUG */
261 #endif  /* !SIM && !SYS_WINNT */
262
263 #ifndef WORK_FORK
264 int     saved_argc;
265 char ** saved_argv;
266 #endif
267
268 #ifndef SIM
269 int             ntpdmain                (int, char **);
270 static void     set_process_priority    (void);
271 static void     assertion_failed        (const char *, int,
272                                          isc_assertiontype_t,
273                                          const char *)
274                         __attribute__   ((__noreturn__));
275 static void     library_fatal_error     (const char *, int, 
276                                          const char *, va_list)
277                                         ISC_FORMAT_PRINTF(3, 0);
278 static void     library_unexpected_error(const char *, int,
279                                          const char *, va_list)
280                                         ISC_FORMAT_PRINTF(3, 0);
281 #endif  /* !SIM */
282
283
284 /* Bug2332 unearthed a problem in the interaction of reduced user
285  * privileges, the limits on memory usage and some versions of the
286  * pthread library on Linux systems. The 'pthread_cancel()' function and
287  * likely some others need to track the stack of the thread involved,
288  * and uses a function that comes from GCC (--> libgcc_s.so) to do
289  * this. Unfortunately the developers of glibc decided to load the
290  * library on demand, which speeds up program start but can cause
291  * trouble here: Due to all the things NTPD does to limit its resource
292  * usage, this deferred load of libgcc_s does not always work once the
293  * restrictions are in effect.
294  *
295  * One way out of this was attempting a forced link against libgcc_s
296  * when possible because it makes the library available immediately
297  * without deferred load. (The symbol resolution would still be dynamic
298  * and on demand, but the code would already be in the process image.)
299  *
300  * This is a tricky thing to do, since it's not necessary everywhere,
301  * not possible everywhere, has shown to break the build of other
302  * programs in the NTP suite and is now generally frowned upon.
303  *
304  * So we take a different approach here: We creat a worker thread that does
305  * actually nothing except waiting for cancellation and cancel it. If
306  * this is done before all the limitations are put in place, the
307  * machinery is pre-heated and all the runtime stuff should be in place
308  * and useable when needed.
309  *
310  * This uses only the standard pthread API and should work with all
311  * implementations of pthreads. It is not necessary everywhere, but it's
312  * cheap enough to go on nearly unnoticed.
313  *
314  * Addendum: Bug 2954 showed that the assumption that this should work
315  * with all OS is wrong -- at least FreeBSD bombs heavily.
316  */
317 #ifdef NEED_PTHREAD_WARMUP
318
319 /* simple thread function: sleep until cancelled, just to exercise
320  * thread cancellation.
321  */
322 static void*
323 my_pthread_warmup_worker(
324         void *thread_args)
325 {
326         (void)thread_args;
327         for (;;)
328                 sleep(10);
329         return NULL;
330 }
331         
332 /* pre-heat threading: create a thread and cancel it, just to exercise
333  * thread cancellation.
334  */
335 static void
336 my_pthread_warmup(void)
337 {
338         pthread_t       thread;
339         pthread_attr_t  thr_attr;
340         int             rc;
341         
342         pthread_attr_init(&thr_attr);
343 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
344     defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && \
345     defined(PTHREAD_STACK_MIN)
346         {
347                 size_t ssmin = 32*1024; /* 32kB should be minimum */
348                 if (ssmin < PTHREAD_STACK_MIN)
349                         ssmin = PTHREAD_STACK_MIN;
350                 rc = pthread_attr_setstacksize(&thr_attr, ssmin);
351                 if (0 != rc)
352                         msyslog(LOG_ERR,
353                                 "my_pthread_warmup: pthread_attr_setstacksize() -> %s",
354                                 strerror(rc));
355         }
356 #endif
357         rc = pthread_create(
358                 &thread, &thr_attr, my_pthread_warmup_worker, NULL);
359         pthread_attr_destroy(&thr_attr);
360         if (0 != rc) {
361                 msyslog(LOG_ERR,
362                         "my_pthread_warmup: pthread_create() -> %s",
363                         strerror(rc));
364         } else {
365                 pthread_cancel(thread);
366                 pthread_join(thread, NULL);
367         }
368 }
369
370 #endif /*defined(NEED_PTHREAD_WARMUP)*/
371
372 #ifdef NEED_EARLY_FORK
373 static void
374 dummy_callback(void) { return; }
375
376 static void
377 fork_nonchroot_worker(void) {
378         getaddrinfo_sometime("localhost", "ntp", NULL, INITIAL_DNS_RETRY,
379                              (gai_sometime_callback)&dummy_callback, NULL);
380 }
381 #endif /* NEED_EARLY_FORK */
382
383 void
384 parse_cmdline_opts(
385         int *   pargc,
386         char ***pargv
387         )
388 {
389         static int      parsed;
390         static int      optct;
391
392         if (!parsed)
393                 optct = ntpOptionProcess(&ntpdOptions, *pargc, *pargv);
394
395         parsed = 1;
396         
397         *pargc -= optct;
398         *pargv += optct;
399 }
400
401
402 #ifdef SIM
403 int
404 main(
405         int argc,
406         char *argv[]
407         )
408 {
409         progname = argv[0];
410         parse_cmdline_opts(&argc, &argv);
411 #ifdef DEBUG
412         debug = OPT_VALUE_SET_DEBUG_LEVEL;
413         DPRINTF(1, ("%s\n", Version));
414 #endif
415
416         return ntpsim(argc, argv);
417 }
418 #elif defined(NO_MAIN_ALLOWED)
419 CALL(ntpd,"ntpd",ntpdmain);
420 #elif !defined(SYS_WINNT)
421 int
422 main(
423         int argc,
424         char *argv[]
425         )
426 {
427         return ntpdmain(argc, argv);
428 }
429 #endif /* !SYS_WINNT */
430
431 #ifdef _AIX
432 /*
433  * OK. AIX is different than solaris in how it implements plock().
434  * If you do NOT adjust the stack limit, you will get the MAXIMUM
435  * stack size allocated and PINNED with you program. To check the
436  * value, use ulimit -a.
437  *
438  * To fix this, we create an automatic variable and set our stack limit
439  * to that PLUS 32KB of extra space (we need some headroom).
440  *
441  * This subroutine gets the stack address.
442  *
443  * Grover Davidson and Matt Ladendorf
444  *
445  */
446 static char *
447 get_aix_stack(void)
448 {
449         char ch;
450         return (&ch);
451 }
452
453 /*
454  * Signal handler for SIGDANGER.
455  */
456 static void
457 catch_danger(int signo)
458 {
459         msyslog(LOG_INFO, "ntpd: setpgid(): %m");
460         /* Make the system believe we'll free something, but don't do it! */
461         return;
462 }
463 #endif /* _AIX */
464
465 /*
466  * Set the process priority
467  */
468 #ifndef SIM
469 static void
470 set_process_priority(void)
471 {
472
473 # ifdef DEBUG
474         if (debug > 1)
475                 msyslog(LOG_DEBUG, "set_process_priority: %s: priority_done is <%d>",
476                         ((priority_done)
477                          ? "Leave priority alone"
478                          : "Attempt to set priority"
479                                 ),
480                         priority_done);
481 # endif /* DEBUG */
482
483 # if defined(HAVE_SCHED_SETSCHEDULER)
484         if (!priority_done) {
485                 extern int config_priority_override, config_priority;
486                 int pmax, pmin;
487                 struct sched_param sched;
488
489                 pmax = sched_get_priority_max(SCHED_FIFO);
490                 sched.sched_priority = pmax;
491                 if ( config_priority_override ) {
492                         pmin = sched_get_priority_min(SCHED_FIFO);
493                         if ( config_priority > pmax )
494                                 sched.sched_priority = pmax;
495                         else if ( config_priority < pmin )
496                                 sched.sched_priority = pmin;
497                         else
498                                 sched.sched_priority = config_priority;
499                 }
500                 if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 )
501                         msyslog(LOG_ERR, "sched_setscheduler(): %m");
502                 else
503                         ++priority_done;
504         }
505 # endif /* HAVE_SCHED_SETSCHEDULER */
506 # ifdef HAVE_RTPRIO
507 #  ifdef RTP_SET
508         if (!priority_done) {
509                 struct rtprio srtp;
510
511                 srtp.type = RTP_PRIO_REALTIME;  /* was: RTP_PRIO_NORMAL */
512                 srtp.prio = 0;          /* 0 (hi) -> RTP_PRIO_MAX (31,lo) */
513
514                 if (rtprio(RTP_SET, getpid(), &srtp) < 0)
515                         msyslog(LOG_ERR, "rtprio() error: %m");
516                 else
517                         ++priority_done;
518         }
519 #  else /* !RTP_SET follows */
520         if (!priority_done) {
521                 if (rtprio(0, 120) < 0)
522                         msyslog(LOG_ERR, "rtprio() error: %m");
523                 else
524                         ++priority_done;
525         }
526 #  endif        /* !RTP_SET */
527 # endif /* HAVE_RTPRIO */
528 # if defined(NTPD_PRIO) && NTPD_PRIO != 0
529 #  ifdef HAVE_ATT_NICE
530         if (!priority_done) {
531                 errno = 0;
532                 if (-1 == nice (NTPD_PRIO) && errno != 0)
533                         msyslog(LOG_ERR, "nice() error: %m");
534                 else
535                         ++priority_done;
536         }
537 #  endif        /* HAVE_ATT_NICE */
538 #  ifdef HAVE_BSD_NICE
539         if (!priority_done) {
540                 if (-1 == setpriority(PRIO_PROCESS, 0, NTPD_PRIO))
541                         msyslog(LOG_ERR, "setpriority() error: %m");
542                 else
543                         ++priority_done;
544         }
545 #  endif        /* HAVE_BSD_NICE */
546 # endif /* NTPD_PRIO && NTPD_PRIO != 0 */
547         if (!priority_done)
548                 msyslog(LOG_ERR, "set_process_priority: No way found to improve our priority");
549 }
550 #endif  /* !SIM */
551
552 #if !defined(SIM) && !defined(SYS_WINNT)
553 /*
554  * Detach from terminal (much like daemon())
555  * Nothe that this function calls exit()
556  */
557 # ifdef HAVE_WORKING_FORK
558 static void
559 detach_from_terminal(
560         int pipe[2],
561         long wait_sync,
562         const char *logfilename
563         )
564 {
565         pid_t   cpid;
566         int     exit_code;
567 #  if !defined(HAVE_SETSID) && !defined (HAVE_SETPGID) && defined(TIOCNOTTY)
568         int             fid;
569 #  endif
570 #  ifdef _AIX
571         struct sigaction sa;
572 #  endif
573
574         cpid = fork();
575         if (0 != cpid) {
576                 /* parent */
577                 if (-1 == cpid) {
578                         msyslog(LOG_ERR, "fork: %m");
579                         exit_code = EX_OSERR;
580                 } else {
581                         close(pipe[1]);
582                         pipe[1] = -1;
583                         exit_code = wait_child_sync_if(
584                                         pipe[0], wait_sync);
585                         DPRINTF(1, ("sync_if: rc=%d\n", exit_code));
586                         if (exit_code <= 0) {
587                                 /* probe daemon exit code -- wait for
588                                  * child process if we have an unexpected
589                                  * EOF on the monitor pipe.
590                                  */
591                                 exit_code = wait_child_exit_if(
592                                                 cpid, (exit_code < 0));
593                                 DPRINTF(1, ("exit_if: rc=%d\n", exit_code));
594                         }
595                 }
596                 exit(exit_code);
597         }
598
599         /*
600          * child/daemon
601          * close all open files excepting waitsync_fd_to_close.
602          * msyslog() unreliable until after init_logging().
603          */
604         closelog();
605         if (syslog_file != NULL) {
606                 fclose(syslog_file);
607                 syslog_file = NULL;
608                 syslogit = TRUE;
609         }
610         close_all_except(pipe[1]);
611         pipe[0] = -1;
612         INSIST(0 == open("/dev/null", 0) && 1 == dup2(0, 1) \
613                 && 2 == dup2(0, 2));
614
615         init_logging(progname, 0, TRUE);
616         /* we lost our logfile (if any) daemonizing */
617         setup_logfile(logfilename);
618
619 #  ifdef SYS_DOMAINOS
620         {
621                 uid_$t puid;
622                 status_$t st;
623
624                 proc2_$who_am_i(&puid);
625                 proc2_$make_server(&puid, &st);
626         }
627 #  endif        /* SYS_DOMAINOS */
628 #  ifdef HAVE_SETSID
629         if (setsid() == (pid_t)-1)
630                 msyslog(LOG_ERR, "setsid(): %m");
631 #  elif defined(HAVE_SETPGID)
632         if (setpgid(0, 0) == -1)
633                 msyslog(LOG_ERR, "setpgid(): %m");
634 #  else         /* !HAVE_SETSID && !HAVE_SETPGID follows */
635 #   ifdef TIOCNOTTY
636         fid = open("/dev/tty", 2);
637         if (fid >= 0) {
638                 ioctl(fid, (u_long)TIOCNOTTY, NULL);
639                 close(fid);
640         }
641 #   endif       /* TIOCNOTTY */
642         ntp_setpgrp(0, getpid());
643 #  endif        /* !HAVE_SETSID && !HAVE_SETPGID */
644 #  ifdef _AIX
645         /* Don't get killed by low-on-memory signal. */
646         sa.sa_handler = catch_danger;
647         sigemptyset(&sa.sa_mask);
648         sa.sa_flags = SA_RESTART;
649         sigaction(SIGDANGER, &sa, NULL);
650 #  endif        /* _AIX */
651
652         return;
653 }
654 # endif /* HAVE_WORKING_FORK */
655
656 #ifdef HAVE_DROPROOT
657 /*
658  * Map user name/number to user ID
659 */
660 static int
661 map_user(
662         )
663 {
664         char *endp;
665
666         if (isdigit((unsigned char)*user)) {
667                 sw_uid = (uid_t)strtoul(user, &endp, 0);
668                 if (*endp != '\0')
669                         goto getuser;
670
671                 if ((pw = getpwuid(sw_uid)) != NULL) {
672                         free(user);
673                         user = estrdup(pw->pw_name);
674                         sw_gid = pw->pw_gid;
675                 } else {
676                         errno = 0;
677                         msyslog(LOG_ERR, "Cannot find user ID %s", user);
678                         return 0;
679                 }
680
681         } else {
682 getuser:
683                 errno = 0;
684                 if ((pw = getpwnam(user)) != NULL) {
685                         sw_uid = pw->pw_uid;
686                         sw_gid = pw->pw_gid;
687                 } else {
688                         if (errno)
689                                 msyslog(LOG_ERR, "getpwnam(%s) failed: %m", user);
690                         else
691                                 msyslog(LOG_ERR, "Cannot find user `%s'", user);
692                         return 0;
693                 }
694         }
695
696         return 1;
697 }
698
699 /*
700  * Map group name/number to group ID
701 */
702 static int
703 map_group(void)
704 {
705         char *endp;
706
707         if (isdigit((unsigned char)*group)) {
708                 sw_gid = (gid_t)strtoul(group, &endp, 0);
709                 if (*endp != '\0')
710                         goto getgroup;
711         } else {
712 getgroup:
713                 if ((gr = getgrnam(group)) != NULL) {
714                         sw_gid = gr->gr_gid;
715                 } else {
716                         errno = 0;
717                         msyslog(LOG_ERR, "Cannot find group `%s'", group);
718                         return 0;
719                 }
720         }
721
722         return 1;
723 }
724
725 static int
726 set_group_ids(void)
727 {
728         if (user && initgroups(user, sw_gid)) {
729                 msyslog(LOG_ERR, "Cannot initgroups() to user `%s': %m", user);
730                 return 0;
731         }
732         if (group && setgid(sw_gid)) {
733                 msyslog(LOG_ERR, "Cannot setgid() to group `%s': %m", group);
734                 return 0;
735         }
736         if (group && setegid(sw_gid)) {
737                 msyslog(LOG_ERR, "Cannot setegid() to group `%s': %m", group);
738                 return 0;
739         }
740         if (group) {
741                 if (0 != setgroups(1, &sw_gid)) {
742                         msyslog(LOG_ERR, "setgroups(1, %d) failed: %m", sw_gid);
743                         return 0;
744                 }
745         }
746         else if (pw)
747                 if (0 != initgroups(pw->pw_name, pw->pw_gid)) {
748                         msyslog(LOG_ERR, "initgroups(<%s>, %d) filed: %m", pw->pw_name, pw->pw_gid);
749                         return 0;
750                 }
751         return 1;
752 }
753
754 static int
755 set_user_ids(void)
756 {
757         if (user && setuid(sw_uid)) {
758                 msyslog(LOG_ERR, "Cannot setuid() to user `%s': %m", user);
759                 return 0;
760         }
761         if (user && seteuid(sw_uid)) {
762                 msyslog(LOG_ERR, "Cannot seteuid() to user `%s': %m", user);
763                 return 0;
764         }
765         return 1;
766 }
767
768 /*
769  * Change (effective) user and group IDs, also initialize the supplementary group access list
770  */
771 int set_user_group_ids(void);
772 int
773 set_user_group_ids(void)
774 {
775         /* If the the user was already mapped, no need to map it again */
776         if ((NULL != user) && (0 == sw_uid)) {
777                 if (0 == map_user())
778                         exit (-1);
779         }
780         /* same applies for the group */
781         if ((NULL != group) && (0 == sw_gid)) {
782                 if (0 == map_group())
783                         exit (-1);
784         }
785
786         if (getegid() != sw_gid && 0 == set_group_ids())
787                 return 0;
788         if (geteuid() != sw_uid && 0 == set_user_ids())
789                 return 0;
790
791         return 1;
792 }
793 #endif /* HAVE_DROPROOT */
794 #endif /* !SIM */
795
796 /*
797  * Main program.  Initialize us, disconnect us from the tty if necessary,
798  * and loop waiting for I/O and/or timer expiries.
799  */
800 #ifndef SIM
801 int
802 ntpdmain(
803         int argc,
804         char *argv[]
805         )
806 {
807         l_fp            now;
808         struct recvbuf *rbuf;
809         const char *    logfilename;
810 # ifdef HAVE_UMASK
811         mode_t          uv;
812 # endif
813 # if defined(HAVE_GETUID) && !defined(MPE) /* MPE lacks the concept of root */
814         uid_t           uid;
815 # endif
816 # if defined(HAVE_WORKING_FORK)
817         long            wait_sync = 0;
818 # endif /* HAVE_WORKING_FORK*/
819 # ifdef SCO5_CLOCK
820         int             fd;
821         int             zero;
822 # endif
823
824 # ifdef NEED_PTHREAD_WARMUP
825         my_pthread_warmup();
826 # endif
827         
828 # ifdef HAVE_UMASK
829         uv = umask(0);
830         if (uv)
831                 umask(uv);
832         else
833                 umask(022);
834 # endif
835         saved_argc = argc;
836         saved_argv = argv;
837         progname = argv[0];
838         initializing = TRUE;            /* mark that we are initializing */
839         parse_cmdline_opts(&argc, &argv);
840 # ifdef DEBUG
841         debug = OPT_VALUE_SET_DEBUG_LEVEL;
842 #  ifdef HAVE_SETLINEBUF
843         setlinebuf(stdout);
844 #  endif
845 # endif
846
847         if (HAVE_OPT(NOFORK) || HAVE_OPT(QUIT)
848 # ifdef DEBUG
849             || debug
850 # endif
851             || HAVE_OPT(SAVECONFIGQUIT))
852                 nofork = TRUE;
853
854         init_logging(progname, NLOG_SYNCMASK, TRUE);
855         /* honor -l/--logfile option to log to a file */
856         if (HAVE_OPT(LOGFILE)) {
857                 logfilename = OPT_ARG(LOGFILE);
858                 syslogit = FALSE;
859                 change_logfile(logfilename, FALSE);
860         } else {
861                 logfilename = NULL;
862                 if (nofork)
863                         msyslog_term = TRUE;
864                 if (HAVE_OPT(SAVECONFIGQUIT))
865                         syslogit = FALSE;
866         }
867         msyslog(LOG_NOTICE, "%s: Starting", Version);
868
869         {
870                 int i;
871                 char buf[1024]; /* Secret knowledge of msyslog buf length */
872                 char *cp = buf;
873
874                 /* Note that every arg has an initial space character */
875                 snprintf(cp, sizeof(buf), "Command line:");
876                 cp += strlen(cp);
877
878                 for (i = 0; i < saved_argc ; ++i) {
879                         snprintf(cp, sizeof(buf) - (cp - buf),
880                                 " %s", saved_argv[i]);
881                         cp += strlen(cp);
882                 }
883                 msyslog(LOG_NOTICE, "%s", buf);
884         }
885
886         msyslog(LOG_NOTICE, "----------------------------------------------------");
887         msyslog(LOG_NOTICE, "ntp-4 is maintained by Network Time Foundation,");
888         msyslog(LOG_NOTICE, "Inc. (NTF), a non-profit 501(c)(3) public-benefit");
889         msyslog(LOG_NOTICE, "corporation.  Support and training for ntp-4 are");
890         msyslog(LOG_NOTICE, "available at https://www.nwtime.org/support");
891         msyslog(LOG_NOTICE, "----------------------------------------------------");
892
893         /*
894          * Install trap handlers to log errors and assertion failures.
895          * Default handlers print to stderr which doesn't work if detached.
896          */
897         isc_assertion_setcallback(assertion_failed);
898         isc_error_setfatal(library_fatal_error);
899         isc_error_setunexpected(library_unexpected_error);
900
901         /* MPE lacks the concept of root */
902 # if defined(HAVE_GETUID) && !defined(MPE)
903         uid = getuid();
904         if (uid && !HAVE_OPT( SAVECONFIGQUIT )) {
905                 msyslog_term = TRUE;
906                 msyslog(LOG_ERR,
907                         "must be run as root, not uid %ld", (long)uid);
908                 exit(1);
909         }
910 # endif
911
912 /*
913  * Enable the Multi-Media Timer for Windows?
914  */
915 # ifdef SYS_WINNT
916         if (HAVE_OPT( MODIFYMMTIMER ))
917                 set_mm_timer(MM_TIMER_HIRES);
918 # endif
919
920 #ifdef HAVE_DNSREGISTRATION
921 /*
922  * Enable mDNS registrations?
923  */
924         if (HAVE_OPT( MDNS )) {
925                 mdnsreg = TRUE;
926         }
927 #endif  /* HAVE_DNSREGISTRATION */
928
929         if (HAVE_OPT( NOVIRTUALIPS ))
930                 listen_to_virtual_ips = 0;
931
932         /*
933          * --interface, listen on specified interfaces
934          */
935         if (HAVE_OPT( INTERFACE )) {
936                 int             ifacect = STACKCT_OPT( INTERFACE );
937                 const char**    ifaces  = STACKLST_OPT( INTERFACE );
938                 sockaddr_u      addr;
939
940                 while (ifacect-- > 0) {
941                         add_nic_rule(
942                                 is_ip_address(*ifaces, AF_UNSPEC, &addr)
943                                         ? MATCH_IFADDR
944                                         : MATCH_IFNAME,
945                                 *ifaces, -1, ACTION_LISTEN);
946                         ifaces++;
947                 }
948         }
949
950         if (HAVE_OPT( NICE ))
951                 priority_done = 0;
952
953 # ifdef HAVE_SCHED_SETSCHEDULER
954         if (HAVE_OPT( PRIORITY )) {
955                 config_priority = OPT_VALUE_PRIORITY;
956                 config_priority_override = 1;
957                 priority_done = 0;
958         }
959 # endif
960
961 # ifdef HAVE_WORKING_FORK
962         /* make sure the FDs are initialised
963          *
964          * note: if WAIT_SYNC is requested, we *have* to fork. This will
965          * overide any '-n' (nofork) or '-d' (debug) option presented on
966          * the command line!
967          */
968         if (HAVE_OPT(WAIT_SYNC)) {
969                 wait_sync = OPT_VALUE_WAIT_SYNC;
970                 if (wait_sync <= 0)
971                         wait_sync = 0;
972                 else
973                         nofork = FALSE;
974         }
975         if ( !nofork && pipe(daemon_pipe)) {
976                 msyslog(LOG_ERR,
977                         "Pipe creation failed for --wait-sync/daemon: %m");
978                 exit(EX_OSERR);
979         }
980 # endif /* HAVE_WORKING_FORK */
981
982         init_lib();
983 # ifdef SYS_WINNT
984         /*
985          * Make sure the service is initialized before we do anything else
986          */
987         ntservice_init();
988
989         /*
990          * Start interpolation thread, must occur before first
991          * get_systime()
992          */
993         init_winnt_time();
994 # endif
995         /*
996          * Initialize random generator and public key pair
997          */
998         get_systime(&now);
999
1000         ntp_srandom((int)(now.l_i * now.l_uf));
1001
1002         /*
1003          * Detach us from the terminal.  May need an #ifndef GIZMO.
1004          */
1005 # ifdef HAVE_WORKING_FORK
1006         if (!nofork) {
1007                 detach_from_terminal(daemon_pipe, wait_sync, logfilename);
1008         }
1009 # endif         /* HAVE_WORKING_FORK */
1010
1011 # ifdef SCO5_CLOCK
1012         /*
1013          * SCO OpenServer's system clock offers much more precise timekeeping
1014          * on the base CPU than the other CPUs (for multiprocessor systems),
1015          * so we must lock to the base CPU.
1016          */
1017         fd = open("/dev/at1", O_RDONLY);                
1018         if (fd >= 0) {
1019                 zero = 0;
1020                 if (ioctl(fd, ACPU_LOCK, &zero) < 0)
1021                         msyslog(LOG_ERR, "cannot lock to base CPU: %m");
1022                 close(fd);
1023         }
1024 # endif
1025
1026         /* Setup stack size in preparation for locking pages in memory. */
1027 # if defined(HAVE_MLOCKALL)
1028 #  ifdef HAVE_SETRLIMIT
1029         ntp_rlimit(RLIMIT_STACK, DFLT_RLIMIT_STACK * 4096, 4096, "4k");
1030 #   ifdef RLIMIT_MEMLOCK
1031         /*
1032          * The default RLIMIT_MEMLOCK is very low on Linux systems.
1033          * Unless we increase this limit malloc calls are likely to
1034          * fail if we drop root privilege.  To be useful the value
1035          * has to be larger than the largest ntpd resident set size.
1036          */
1037         ntp_rlimit(RLIMIT_MEMLOCK, DFLT_RLIMIT_MEMLOCK * 1024 * 1024, 1024 * 1024, "MB");
1038 #   endif       /* RLIMIT_MEMLOCK */
1039 #  endif        /* HAVE_SETRLIMIT */
1040 # else  /* !HAVE_MLOCKALL follows */
1041 #  ifdef HAVE_PLOCK
1042 #   ifdef PROCLOCK
1043 #    ifdef _AIX
1044         /*
1045          * set the stack limit for AIX for plock().
1046          * see get_aix_stack() for more info.
1047          */
1048         if (ulimit(SET_STACKLIM, (get_aix_stack() - 8 * 4096)) < 0)
1049                 msyslog(LOG_ERR,
1050                         "Cannot adjust stack limit for plock: %m");
1051 #    endif      /* _AIX */
1052 #   endif       /* PROCLOCK */
1053 #  endif        /* HAVE_PLOCK */
1054 # endif /* !HAVE_MLOCKALL */
1055
1056         /*
1057          * Set up signals we pay attention to locally.
1058          */
1059 # ifdef SIGDIE1
1060         signal_no_reset(SIGDIE1, finish);
1061         signal_no_reset(SIGDIE2, finish);
1062         signal_no_reset(SIGDIE3, finish);
1063         signal_no_reset(SIGDIE4, finish);
1064 # endif
1065 # ifdef SIGBUS
1066         signal_no_reset(SIGBUS, finish);
1067 # endif
1068
1069 # if !defined(SYS_WINNT) && !defined(VMS)
1070 #  ifdef DEBUG
1071         (void) signal_no_reset(MOREDEBUGSIG, moredebug);
1072         (void) signal_no_reset(LESSDEBUGSIG, lessdebug);
1073 #  else
1074         (void) signal_no_reset(MOREDEBUGSIG, no_debug);
1075         (void) signal_no_reset(LESSDEBUGSIG, no_debug);
1076 #  endif        /* DEBUG */
1077 # endif /* !SYS_WINNT && !VMS */
1078
1079         /*
1080          * Set up signals we should never pay attention to.
1081          */
1082 # ifdef SIGPIPE
1083         signal_no_reset(SIGPIPE, SIG_IGN);
1084 # endif
1085
1086         /*
1087          * Call the init_ routines to initialize the data structures.
1088          *
1089          * Exactly what command-line options are we expecting here?
1090          */
1091         INIT_SSL();
1092         init_auth();
1093         init_util();
1094         init_restrict();
1095         init_mon();
1096         init_timer();
1097         init_request();
1098         init_control();
1099         init_peer();
1100 # ifdef REFCLOCK
1101         init_refclock();
1102 # endif
1103         set_process_priority();
1104         init_proto();           /* Call at high priority */
1105         init_io();
1106         init_loopfilter();
1107         mon_start(MON_ON);      /* monitor on by default now      */
1108                                 /* turn off in config if unwanted */
1109
1110         /*
1111          * Get the configuration.  This is done in a separate module
1112          * since this will definitely be different for the gizmo board.
1113          */
1114         getconfig(argc, argv);
1115
1116         if (-1 == cur_memlock) {
1117 # if defined(HAVE_MLOCKALL)
1118                 /*
1119                  * lock the process into memory
1120                  */
1121                 if (   !HAVE_OPT(SAVECONFIGQUIT)
1122 #  ifdef RLIMIT_MEMLOCK
1123                     && -1 != DFLT_RLIMIT_MEMLOCK
1124 #  endif
1125                     && 0 != mlockall(MCL_CURRENT|MCL_FUTURE))
1126                         msyslog(LOG_ERR, "mlockall(): %m");
1127 # else  /* !HAVE_MLOCKALL follows */
1128 #  ifdef HAVE_PLOCK
1129 #   ifdef PROCLOCK
1130                 /*
1131                  * lock the process into memory
1132                  */
1133                 if (!HAVE_OPT(SAVECONFIGQUIT) && 0 != plock(PROCLOCK))
1134                         msyslog(LOG_ERR, "plock(PROCLOCK): %m");
1135 #   else        /* !PROCLOCK follows  */
1136 #    ifdef TXTLOCK
1137                 /*
1138                  * Lock text into ram
1139                  */
1140                 if (!HAVE_OPT(SAVECONFIGQUIT) && 0 != plock(TXTLOCK))
1141                         msyslog(LOG_ERR, "plock(TXTLOCK) error: %m");
1142 #    else       /* !TXTLOCK follows */
1143                 msyslog(LOG_ERR, "plock() - don't know what to lock!");
1144 #    endif      /* !TXTLOCK */
1145 #   endif       /* !PROCLOCK */
1146 #  endif        /* HAVE_PLOCK */
1147 # endif /* !HAVE_MLOCKALL */
1148         }
1149
1150         loop_config(LOOP_DRIFTINIT, 0);
1151         report_event(EVNT_SYSRESTART, NULL, NULL);
1152         initializing = FALSE;
1153
1154 # ifdef HAVE_LINUX_CAPABILITIES
1155         {
1156                 /*  Check that setting capabilities actually works; we might be
1157                  *  run on a kernel with disabled capabilities. We must not
1158                  *  drop privileges in this case.
1159                  */
1160                 cap_t caps;
1161                 caps = cap_from_text("cap_sys_time,cap_setuid,cap_setgid,cap_sys_chroot,cap_net_bind_service=pe");
1162                 if ( ! caps) {
1163                         msyslog( LOG_ERR, "cap_from_text() failed: %m" );
1164                         exit(-1);
1165                 }
1166                 have_caps = (cap_set_proc(caps) == 0);
1167                 cap_free(caps); /* caps not NULL here! */
1168         }
1169 # endif /* HAVE_LINUX_CAPABILITIES */
1170
1171 # ifdef HAVE_DROPROOT
1172 #  ifdef HAVE_LINUX_CAPABILITIES
1173         if (droproot && have_caps) {
1174 #  else
1175         if (droproot) {
1176 #  endif /*HAVE_LINUX_CAPABILITIES*/
1177
1178 #  ifdef NEED_EARLY_FORK
1179                 fork_nonchroot_worker();
1180 #  endif
1181
1182                 /* Drop super-user privileges and chroot now if the OS supports this */
1183
1184 #  ifdef HAVE_LINUX_CAPABILITIES
1185                 /* set flag: keep privileges accross setuid() call (we only really need cap_sys_time): */
1186                 if (prctl( PR_SET_KEEPCAPS, 1L, 0L, 0L, 0L ) == -1) {
1187                         msyslog( LOG_ERR, "prctl( PR_SET_KEEPCAPS, 1L ) failed: %m" );
1188                         exit(-1);
1189                 }
1190 #  elif HAVE_SOLARIS_PRIVS
1191                 /* Nothing to do here */
1192 #  else
1193                 /* we need a user to switch to */
1194                 if (user == NULL) {
1195                         msyslog(LOG_ERR, "Need user name to drop root privileges (see -u flag!)" );
1196                         exit(-1);
1197                 }
1198 #  endif        /* HAVE_LINUX_CAPABILITIES || HAVE_SOLARIS_PRIVS */
1199
1200                 if (user != NULL) {
1201                         if (0 == map_user())
1202                                 exit (-1);
1203                 }
1204                 if (group != NULL) {
1205                         if (0 == map_group())
1206                                 exit (-1);
1207                 }
1208
1209                 if (chrootdir ) {
1210                         /* make sure cwd is inside the jail: */
1211                         if (chdir(chrootdir)) {
1212                                 msyslog(LOG_ERR, "Cannot chdir() to `%s': %m", chrootdir);
1213                                 exit (-1);
1214                         }
1215                         if (chroot(chrootdir)) {
1216                                 msyslog(LOG_ERR, "Cannot chroot() to `%s': %m", chrootdir);
1217                                 exit (-1);
1218                         }
1219                         if (chdir("/")) {
1220                                 msyslog(LOG_ERR, "Cannot chdir() to`root after chroot(): %m");
1221                                 exit (-1);
1222                         }
1223                 }
1224 #  ifdef HAVE_SOLARIS_PRIVS
1225                 if ((lowprivs = priv_str_to_set(LOWPRIVS, ",", NULL)) == NULL) {
1226                         msyslog(LOG_ERR, "priv_str_to_set() failed:%m");
1227                         exit(-1);
1228                 }
1229                 if ((highprivs = priv_allocset()) == NULL) {
1230                         msyslog(LOG_ERR, "priv_allocset() failed:%m");
1231                         exit(-1);
1232                 }
1233                 (void) getppriv(PRIV_PERMITTED, highprivs);
1234                 (void) priv_intersect(highprivs, lowprivs);
1235                 if (setppriv(PRIV_SET, PRIV_PERMITTED, lowprivs) == -1) {
1236                         msyslog(LOG_ERR, "setppriv() failed:%m");
1237                         exit(-1);
1238                 }
1239 #  endif /* HAVE_SOLARIS_PRIVS */
1240                 if (0 == set_user_group_ids())
1241                         exit(-1);
1242
1243 #  if !defined(HAVE_LINUX_CAPABILITIES) && !defined(HAVE_SOLARIS_PRIVS)
1244                 /*
1245                  * for now assume that the privilege to bind to privileged ports
1246                  * is associated with running with uid 0 - should be refined on
1247                  * ports that allow binding to NTP_PORT with uid != 0
1248                  */
1249                 disable_dynamic_updates |= (sw_uid != 0);  /* also notifies routing message listener */
1250 #  endif /* !HAVE_LINUX_CAPABILITIES && !HAVE_SOLARIS_PRIVS */
1251
1252                 if (disable_dynamic_updates && interface_interval) {
1253                         interface_interval = 0;
1254                         msyslog(LOG_INFO, "running as non-root disables dynamic interface tracking");
1255                 }
1256
1257 #  ifdef HAVE_LINUX_CAPABILITIES
1258                 {
1259                         /*
1260                          *  We may be running under non-root uid now, but we still hold full root privileges!
1261                          *  We drop all of them, except for the crucial one or two: cap_sys_time and
1262                          *  cap_net_bind_service if doing dynamic interface tracking.
1263                          */
1264                         cap_t caps;
1265                         char *captext;
1266                         
1267                         captext = (0 != interface_interval)
1268                                       ? "cap_sys_time,cap_net_bind_service=pe"
1269                                       : "cap_sys_time=pe";
1270                         caps = cap_from_text(captext);
1271                         if (!caps) {
1272                                 msyslog(LOG_ERR,
1273                                         "cap_from_text(%s) failed: %m",
1274                                         captext);
1275                                 exit(-1);
1276                         }
1277                         if (-1 == cap_set_proc(caps)) {
1278                                 msyslog(LOG_ERR,
1279                                         "cap_set_proc() failed to drop root privs: %m");
1280                                 exit(-1);
1281                         }
1282                         cap_free(caps);
1283                 }
1284 #  endif        /* HAVE_LINUX_CAPABILITIES */
1285 #  ifdef HAVE_SOLARIS_PRIVS
1286                 if (priv_delset(lowprivs, "proc_setid") == -1) {
1287                         msyslog(LOG_ERR, "priv_delset() failed:%m");
1288                         exit(-1);
1289                 }
1290                 if (setppriv(PRIV_SET, PRIV_PERMITTED, lowprivs) == -1) {
1291                         msyslog(LOG_ERR, "setppriv() failed:%m");
1292                         exit(-1);
1293                 }
1294                 priv_freeset(lowprivs);
1295                 priv_freeset(highprivs);
1296 #  endif /* HAVE_SOLARIS_PRIVS */
1297                 root_dropped = TRUE;
1298                 fork_deferred_worker();
1299         }       /* if (droproot) */
1300 # endif /* HAVE_DROPROOT */
1301
1302 /* libssecomp sandboxing */
1303 #if defined (LIBSECCOMP) && (KERN_SECCOMP)
1304         scmp_filter_ctx ctx;
1305
1306         if ((ctx = seccomp_init(SCMP_ACT_KILL)) < 0)
1307                 msyslog(LOG_ERR, "%s: seccomp_init(SCMP_ACT_KILL) failed: %m", __func__);
1308         else {
1309                 msyslog(LOG_DEBUG, "%s: seccomp_init(SCMP_ACT_KILL) succeeded", __func__);
1310         }
1311
1312 #ifdef __x86_64__
1313 int scmp_sc[] = {
1314         SCMP_SYS(adjtimex),
1315         SCMP_SYS(bind),
1316         SCMP_SYS(brk),
1317         SCMP_SYS(chdir),
1318         SCMP_SYS(clock_gettime),
1319         SCMP_SYS(clock_settime),
1320         SCMP_SYS(close),
1321         SCMP_SYS(connect),
1322         SCMP_SYS(exit_group),
1323         SCMP_SYS(fstat),
1324         SCMP_SYS(fsync),
1325         SCMP_SYS(futex),
1326         SCMP_SYS(getitimer),
1327         SCMP_SYS(getsockname),
1328         SCMP_SYS(ioctl),
1329         SCMP_SYS(lseek),
1330         SCMP_SYS(madvise),
1331         SCMP_SYS(mmap),
1332         SCMP_SYS(munmap),
1333         SCMP_SYS(open),
1334         SCMP_SYS(poll),
1335         SCMP_SYS(read),
1336         SCMP_SYS(recvmsg),
1337         SCMP_SYS(rename),
1338         SCMP_SYS(rt_sigaction),
1339         SCMP_SYS(rt_sigprocmask),
1340         SCMP_SYS(rt_sigreturn),
1341         SCMP_SYS(select),
1342         SCMP_SYS(sendto),
1343         SCMP_SYS(setitimer),
1344         SCMP_SYS(setsid),
1345         SCMP_SYS(socket),
1346         SCMP_SYS(stat),
1347         SCMP_SYS(time),
1348         SCMP_SYS(write),
1349 };
1350 #endif
1351 #ifdef __i386__
1352 int scmp_sc[] = {
1353         SCMP_SYS(_newselect),
1354         SCMP_SYS(adjtimex),
1355         SCMP_SYS(brk),
1356         SCMP_SYS(chdir),
1357         SCMP_SYS(clock_gettime),
1358         SCMP_SYS(clock_settime),
1359         SCMP_SYS(close),
1360         SCMP_SYS(exit_group),
1361         SCMP_SYS(fsync),
1362         SCMP_SYS(futex),
1363         SCMP_SYS(getitimer),
1364         SCMP_SYS(madvise),
1365         SCMP_SYS(mmap),
1366         SCMP_SYS(mmap2),
1367         SCMP_SYS(munmap),
1368         SCMP_SYS(open),
1369         SCMP_SYS(poll),
1370         SCMP_SYS(read),
1371         SCMP_SYS(rename),
1372         SCMP_SYS(rt_sigaction),
1373         SCMP_SYS(rt_sigprocmask),
1374         SCMP_SYS(select),
1375         SCMP_SYS(setitimer),
1376         SCMP_SYS(setsid),
1377         SCMP_SYS(sigprocmask),
1378         SCMP_SYS(sigreturn),
1379         SCMP_SYS(socketcall),
1380         SCMP_SYS(stat64),
1381         SCMP_SYS(time),
1382         SCMP_SYS(write),
1383 };
1384 #endif
1385         {
1386                 int i;
1387
1388                 for (i = 0; i < COUNTOF(scmp_sc); i++) {
1389                         if (seccomp_rule_add(ctx,
1390                             SCMP_ACT_ALLOW, scmp_sc[i], 0) < 0) {
1391                                 msyslog(LOG_ERR,
1392                                     "%s: seccomp_rule_add() failed: %m",
1393                                     __func__);
1394                         }
1395                 }
1396         }
1397
1398         if (seccomp_load(ctx) < 0)
1399                 msyslog(LOG_ERR, "%s: seccomp_load() failed: %m",
1400                     __func__);  
1401         else {
1402                 msyslog(LOG_DEBUG, "%s: seccomp_load() succeeded", __func__);
1403         }
1404 #endif /* LIBSECCOMP and KERN_SECCOMP */
1405
1406 #if defined(SYS_WINNT)
1407         ntservice_isup();
1408 #elif defined(HAVE_WORKING_FORK)
1409         if (daemon_pipe[1] != -1) {
1410                 write(daemon_pipe[1], "R\n", 2);
1411         }
1412 #endif /* HAVE_WORKING_FORK */
1413
1414 # ifndef HAVE_IO_COMPLETION_PORT
1415         BLOCK_IO_AND_ALARM();
1416         was_alarmed = FALSE;
1417 # endif
1418
1419         for (;;) {
1420 #if !defined(SIM) && defined(SIGDIE1)
1421                 if (signalled)
1422                         finish_safe(signo);
1423 #endif
1424 # ifdef HAVE_IO_COMPLETION_PORT
1425                 GetReceivedBuffers();
1426
1427 # else /* normal I/O */
1428                 if (alarm_flag) {       /* alarmed? */
1429                         was_alarmed = TRUE;
1430                         alarm_flag = FALSE;
1431                 }
1432
1433                 /* collect async name/addr results */
1434                 if (!was_alarmed)
1435                     harvest_blocking_responses();
1436                 
1437                 if (!was_alarmed && !has_full_recv_buffer()) {
1438                         /*
1439                          * Nothing to do.  Wait for something.
1440                          */
1441                         io_handler();
1442                 }
1443
1444                 if (alarm_flag) {       /* alarmed? */
1445                         was_alarmed = TRUE;
1446                         alarm_flag = FALSE;
1447                 }
1448
1449                 if (was_alarmed) {
1450                         UNBLOCK_IO_AND_ALARM();
1451                         /*
1452                          * Out here, signals are unblocked.  Call timer routine
1453                          * to process expiry.
1454                          */
1455                         timer();
1456                         was_alarmed = FALSE;
1457                         BLOCK_IO_AND_ALARM();
1458                 }
1459
1460 # endif         /* !HAVE_IO_COMPLETION_PORT */
1461
1462 # ifdef DEBUG_TIMING
1463                 {
1464                         l_fp pts;
1465                         l_fp tsa, tsb;
1466                         int bufcount = 0;
1467
1468                         get_systime(&pts);
1469                         tsa = pts;
1470 # endif
1471                         rbuf = get_full_recv_buffer();
1472                         while (rbuf != NULL) {
1473                                 if (alarm_flag) {
1474                                         was_alarmed = TRUE;
1475                                         alarm_flag = FALSE;
1476                                 }
1477                                 UNBLOCK_IO_AND_ALARM();
1478
1479                                 if (was_alarmed) {
1480                                         /* avoid timer starvation during lengthy I/O handling */
1481                                         timer();
1482                                         was_alarmed = FALSE;
1483                                 }
1484
1485                                 /*
1486                                  * Call the data procedure to handle each received
1487                                  * packet.
1488                                  */
1489                                 if (rbuf->receiver != NULL) {
1490 # ifdef DEBUG_TIMING
1491                                         l_fp dts = pts;
1492
1493                                         L_SUB(&dts, &rbuf->recv_time);
1494                                         DPRINTF(2, ("processing timestamp delta %s (with prec. fuzz)\n", lfptoa(&dts, 9)));
1495                                         collect_timing(rbuf, "buffer processing delay", 1, &dts);
1496                                         bufcount++;
1497 # endif
1498                                         (*rbuf->receiver)(rbuf);
1499                                 } else {
1500                                         msyslog(LOG_ERR, "fatal: receive buffer callback NULL");
1501                                         abort();
1502                                 }
1503
1504                                 BLOCK_IO_AND_ALARM();
1505                                 freerecvbuf(rbuf);
1506                                 rbuf = get_full_recv_buffer();
1507                         }
1508 # ifdef DEBUG_TIMING
1509                         get_systime(&tsb);
1510                         L_SUB(&tsb, &tsa);
1511                         if (bufcount) {
1512                                 collect_timing(NULL, "processing", bufcount, &tsb);
1513                                 DPRINTF(2, ("processing time for %d buffers %s\n", bufcount, lfptoa(&tsb, 9)));
1514                         }
1515                 }
1516 # endif
1517
1518                 /*
1519                  * Go around again
1520                  */
1521
1522 # ifdef HAVE_DNSREGISTRATION
1523                 if (mdnsreg && (current_time - mdnsreg ) > 60 && mdnstries && sys_leap != LEAP_NOTINSYNC) {
1524                         mdnsreg = current_time;
1525                         msyslog(LOG_INFO, "Attempting to register mDNS");
1526                         if ( DNSServiceRegister (&mdns, 0, 0, NULL, "_ntp._udp", NULL, NULL, 
1527                             htons(NTP_PORT), 0, NULL, NULL, NULL) != kDNSServiceErr_NoError ) {
1528                                 if (!--mdnstries) {
1529                                         msyslog(LOG_ERR, "Unable to register mDNS, giving up.");
1530                                 } else {        
1531                                         msyslog(LOG_INFO, "Unable to register mDNS, will try later.");
1532                                 }
1533                         } else {
1534                                 msyslog(LOG_INFO, "mDNS service registered.");
1535                                 mdnsreg = FALSE;
1536                         }
1537                 }
1538 # endif /* HAVE_DNSREGISTRATION */
1539
1540         }
1541         UNBLOCK_IO_AND_ALARM();
1542         return 1;
1543 }
1544 #endif  /* !SIM */
1545
1546
1547 #if !defined(SIM) && defined(SIGDIE1)
1548 /*
1549  * finish - exit gracefully
1550  */
1551 static void
1552 finish_safe(
1553         int     sig
1554         )
1555 {
1556         const char *sig_desc;
1557
1558         sig_desc = NULL;
1559 #ifdef HAVE_STRSIGNAL
1560         sig_desc = strsignal(sig);
1561 #endif
1562         if (sig_desc == NULL)
1563                 sig_desc = "";
1564         msyslog(LOG_NOTICE, "%s exiting on signal %d (%s)", progname,
1565                 sig, sig_desc);
1566         /* See Bug 2513 and Bug 2522 re the unlink of PIDFILE */
1567 # ifdef HAVE_DNSREGISTRATION
1568         if (mdns != NULL)
1569                 DNSServiceRefDeallocate(mdns);
1570 # endif
1571         peer_cleanup();
1572         exit(0);
1573 }
1574
1575 static RETSIGTYPE
1576 finish(
1577         int     sig
1578         )
1579 {
1580         signalled = 1;
1581         signo = sig;
1582 }
1583
1584 #endif  /* !SIM && SIGDIE1 */
1585
1586
1587 #ifndef SIM
1588 /*
1589  * wait_child_sync_if - implements parent side of -w/--wait-sync
1590  */
1591 # ifdef HAVE_WORKING_FORK
1592
1593 static int
1594 wait_child_sync_if(
1595         int             pipe_read_fd,
1596         unsigned long   wait_sync
1597         )
1598 {
1599         int     rc;
1600         char    ch;
1601         time_t  wait_end_time;
1602         time_t  cur_time;
1603         time_t  wait_rem;
1604         fd_set  readset;
1605         struct timeval wtimeout;
1606
1607         /* we wait a bit for the child in *any* case, because on failure
1608          * of the child we have to get and inspect the exit code!
1609          */
1610         wait_end_time = time(NULL);
1611         if (wait_sync)
1612                 wait_end_time += wait_sync;
1613         else
1614                 wait_end_time += 30;
1615
1616         do {
1617                 cur_time = time(NULL);
1618                 wait_rem = (wait_end_time > cur_time)
1619                                 ? (wait_end_time - cur_time)
1620                                 : 0;
1621                 wtimeout.tv_sec = wait_rem;
1622                 wtimeout.tv_usec = 0;
1623                 FD_ZERO(&readset);
1624                 FD_SET(pipe_read_fd, &readset);
1625                 rc = select(pipe_read_fd + 1, &readset, NULL, NULL,
1626                             &wtimeout);
1627                 if (-1 == rc) {
1628                         if (EINTR == errno)
1629                                 continue;
1630                         msyslog(LOG_ERR,
1631                                 "daemon startup: select failed: %m");
1632                         return EX_IOERR;
1633                 }
1634                 if (0 == rc) {
1635                         /*
1636                          * select() indicated a timeout, but in case
1637                          * its timeouts are affected by a step of the
1638                          * system clock, select() again with a zero 
1639                          * timeout to confirm.
1640                          */
1641                         FD_ZERO(&readset);
1642                         FD_SET(pipe_read_fd, &readset);
1643                         wtimeout.tv_sec = 0;
1644                         wtimeout.tv_usec = 0;
1645                         rc = select(pipe_read_fd + 1, &readset, NULL,
1646                                     NULL, &wtimeout);
1647                         if (0 == rc)    /* select() timeout */
1648                                 break;
1649                 }
1650                 rc = read(pipe_read_fd, &ch, 1);
1651                 if (rc == 0) {
1652                         DPRINTF(2, ("daemon control: got EOF\n"));
1653                         return -1;      /* unexpected EOF, check daemon */
1654                 } else if (rc == 1) {
1655                         DPRINTF(2, ("daemon control: got '%c'\n",
1656                                     (ch >= ' ' ? ch : '.')));
1657                         if (ch == 'R' && !wait_sync)
1658                                 return 0;
1659                         if (ch == 'S' && wait_sync)
1660                                 return 0;
1661                 } else {
1662                         DPRINTF(2, ("daemon control: read 1 char failed: %s\n",
1663                                     strerror(errno)));
1664                         return EX_IOERR;
1665                 }
1666         } while (wait_rem > 0);
1667
1668         if (wait_sync) {
1669                 fprintf(stderr, "%s: -w/--wait-sync %ld timed out.\n",
1670                         progname, wait_sync);
1671                 return EX_PROTOCOL;
1672         } else {
1673                 fprintf(stderr, "%s: daemon startup monitoring timed out.\n",
1674                         progname);
1675                 return 0;
1676         }
1677 }
1678
1679
1680 static int
1681 wait_child_exit_if(
1682         pid_t   cpid,
1683         int     blocking
1684         )
1685 {
1686 #    ifdef HAVE_WAITPID
1687         int     rc = 0;
1688         int     wstatus;
1689         if (cpid == waitpid(cpid, &wstatus, (blocking ? 0 : WNOHANG))) {
1690                 DPRINTF(1, ("child (pid=%d) dead now\n", cpid));
1691                 if (WIFEXITED(wstatus)) {
1692                         rc = WEXITSTATUS(wstatus);
1693                         msyslog(LOG_ERR, "daemon child exited with code %d",
1694                                 rc);
1695                 } else if (WIFSIGNALED(wstatus)) {
1696                         rc = EX_SOFTWARE;
1697                         msyslog(LOG_ERR, "daemon child died with signal %d",
1698                                 WTERMSIG(wstatus));
1699                 } else {
1700                         rc = EX_SOFTWARE;
1701                         msyslog(LOG_ERR, "daemon child died with unknown cause");
1702                 }
1703         } else {
1704                 DPRINTF(1, ("child (pid=%d) still alive\n", cpid));
1705         }
1706         return rc;
1707 #    else
1708         UNUSED_ARG(cpid);
1709         return 0;
1710 #    endif
1711 }
1712
1713 # endif /* HAVE_WORKING_FORK */
1714
1715
1716 /*
1717  * assertion_failed - Redirect assertion failures to msyslog().
1718  */
1719 static void
1720 assertion_failed(
1721         const char *file,
1722         int line,
1723         isc_assertiontype_t type,
1724         const char *cond
1725         )
1726 {
1727         isc_assertion_setcallback(NULL);    /* Avoid recursion */
1728
1729         msyslog(LOG_ERR, "%s:%d: %s(%s) failed",
1730                 file, line, isc_assertion_typetotext(type), cond);
1731         msyslog(LOG_ERR, "exiting (due to assertion failure)");
1732
1733 #if defined(DEBUG) && defined(SYS_WINNT)
1734         if (debug)
1735                 DebugBreak();
1736 #endif
1737
1738         abort();
1739 }
1740
1741
1742 /*
1743  * library_fatal_error - Handle fatal errors from our libraries.
1744  */
1745 static void
1746 library_fatal_error(
1747         const char *file,
1748         int line,
1749         const char *format,
1750         va_list args
1751         )
1752 {
1753         char errbuf[256];
1754
1755         isc_error_setfatal(NULL);  /* Avoid recursion */
1756
1757         msyslog(LOG_ERR, "%s:%d: fatal error:", file, line);
1758         vsnprintf(errbuf, sizeof(errbuf), format, args);
1759         msyslog(LOG_ERR, "%s", errbuf);
1760         msyslog(LOG_ERR, "exiting (due to fatal error in library)");
1761
1762 #if defined(DEBUG) && defined(SYS_WINNT)
1763         if (debug)
1764                 DebugBreak();
1765 #endif
1766
1767         abort();
1768 }
1769
1770
1771 /*
1772  * library_unexpected_error - Handle non fatal errors from our libraries.
1773  */
1774 # define MAX_UNEXPECTED_ERRORS 100
1775 int unexpected_error_cnt = 0;
1776 static void
1777 library_unexpected_error(
1778         const char *file,
1779         int line,
1780         const char *format,
1781         va_list args
1782         )
1783 {
1784         char errbuf[256];
1785
1786         if (unexpected_error_cnt >= MAX_UNEXPECTED_ERRORS)
1787                 return; /* avoid clutter in log */
1788
1789         msyslog(LOG_ERR, "%s:%d: unexpected error:", file, line);
1790         vsnprintf(errbuf, sizeof(errbuf), format, args);
1791         msyslog(LOG_ERR, "%s", errbuf);
1792
1793         if (++unexpected_error_cnt == MAX_UNEXPECTED_ERRORS)
1794                 msyslog(LOG_ERR, "Too many errors.  Shutting up.");
1795
1796 }
1797 #endif  /* !SIM */
1798
1799 #if !defined(SIM) && !defined(SYS_WINNT)
1800 # ifdef DEBUG
1801
1802 /*
1803  * moredebug - increase debugging verbosity
1804  */
1805 static RETSIGTYPE
1806 moredebug(
1807         int sig
1808         )
1809 {
1810         int saved_errno = errno;
1811
1812         if (debug < 255)
1813         {
1814                 debug++;
1815                 msyslog(LOG_DEBUG, "debug raised to %d", debug);
1816         }
1817         errno = saved_errno;
1818 }
1819
1820
1821 /*
1822  * lessdebug - decrease debugging verbosity
1823  */
1824 static RETSIGTYPE
1825 lessdebug(
1826         int sig
1827         )
1828 {
1829         int saved_errno = errno;
1830
1831         if (debug > 0)
1832         {
1833                 debug--;
1834                 msyslog(LOG_DEBUG, "debug lowered to %d", debug);
1835         }
1836         errno = saved_errno;
1837 }
1838
1839 # else  /* !DEBUG follows */
1840
1841
1842 /*
1843  * no_debug - We don't do the debug here.
1844  */
1845 static RETSIGTYPE
1846 no_debug(
1847         int sig
1848         )
1849 {
1850         int saved_errno = errno;
1851
1852         msyslog(LOG_DEBUG, "ntpd not compiled for debugging (signal %d)", sig);
1853         errno = saved_errno;
1854 }
1855 # endif /* !DEBUG */
1856 #endif  /* !SIM && !SYS_WINNT */