1 1.7 roy /* $NetBSD: cmd_args.c,v 1.8 2024/08/18 20:47:17 christos Exp $ */ 2 1.1 kardel 3 1.1 kardel /* 4 1.1 kardel * cmd_args.c = command-line argument processing 5 1.1 kardel */ 6 1.1 kardel #ifdef HAVE_CONFIG_H 7 1.1 kardel # include <config.h> 8 1.1 kardel #endif 9 1.1 kardel 10 1.1 kardel #include "ntpd.h" 11 1.1 kardel #include "ntp_stdlib.h" 12 1.2 christos #include "ntp_config.h" 13 1.1 kardel #include "ntp_cmdargs.h" 14 1.1 kardel 15 1.1 kardel #include "ntpd-opts.h" 16 1.1 kardel 17 1.1 kardel /* 18 1.1 kardel * Definitions of things either imported from or exported to outside 19 1.1 kardel */ 20 1.1 kardel extern char const *progname; 21 1.1 kardel 22 1.1 kardel #ifdef HAVE_NETINFO 23 1.1 kardel extern int check_netinfo; 24 1.1 kardel #endif 25 1.1 kardel 26 1.1 kardel 27 1.1 kardel /* 28 1.2 christos * getCmdOpts - apply most command line options 29 1.2 christos * 30 1.2 christos * A few options are examined earlier in ntpd.c ntpdmain() and 31 1.2 christos * ports/winnt/ntpd/ntservice.c main(). 32 1.1 kardel */ 33 1.1 kardel void 34 1.1 kardel getCmdOpts( 35 1.2 christos int argc, 36 1.2 christos char ** argv 37 1.1 kardel ) 38 1.1 kardel { 39 1.1 kardel extern const char *config_file; 40 1.1 kardel int errflg; 41 1.1 kardel 42 1.1 kardel /* 43 1.1 kardel * Initialize, initialize 44 1.1 kardel */ 45 1.1 kardel errflg = 0; 46 1.1 kardel 47 1.2 christos if (ipv4_works && ipv6_works) { 48 1.2 christos if (HAVE_OPT( IPV4 )) 49 1.2 christos ipv6_works = 0; 50 1.2 christos else if (HAVE_OPT( IPV6 )) 51 1.2 christos ipv4_works = 0; 52 1.2 christos } else if (!ipv4_works && !ipv6_works) { 53 1.2 christos msyslog(LOG_ERR, "Neither IPv4 nor IPv6 networking detected, fatal."); 54 1.2 christos exit(1); 55 1.2 christos } else if (HAVE_OPT( IPV4 ) && !ipv4_works) 56 1.2 christos msyslog(LOG_WARNING, "-4/--ipv4 ignored, IPv4 networking not found."); 57 1.2 christos else if (HAVE_OPT( IPV6 ) && !ipv6_works) 58 1.2 christos msyslog(LOG_WARNING, "-6/--ipv6 ignored, IPv6 networking not found."); 59 1.1 kardel 60 1.1 kardel if (HAVE_OPT( AUTHREQ )) 61 1.1 kardel proto_config(PROTO_AUTHENTICATE, 1, 0., NULL); 62 1.1 kardel else if (HAVE_OPT( AUTHNOREQ )) 63 1.1 kardel proto_config(PROTO_AUTHENTICATE, 0, 0., NULL); 64 1.1 kardel 65 1.1 kardel if (HAVE_OPT( BCASTSYNC )) 66 1.1 kardel proto_config(PROTO_BROADCLIENT, 1, 0., NULL); 67 1.1 kardel 68 1.1 kardel if (HAVE_OPT( CONFIGFILE )) { 69 1.1 kardel config_file = OPT_ARG( CONFIGFILE ); 70 1.1 kardel #ifdef HAVE_NETINFO 71 1.1 kardel check_netinfo = 0; 72 1.1 kardel #endif 73 1.1 kardel } 74 1.1 kardel 75 1.1 kardel if (HAVE_OPT( DRIFTFILE )) 76 1.8 christos stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ), 1); 77 1.1 kardel 78 1.1 kardel if (HAVE_OPT( PANICGATE )) 79 1.1 kardel allow_panic = TRUE; 80 1.1 kardel 81 1.3 christos if (HAVE_OPT( FORCE_STEP_ONCE )) 82 1.3 christos force_step_once = TRUE; 83 1.3 christos 84 1.1 kardel #ifdef HAVE_DROPROOT 85 1.1 kardel if (HAVE_OPT( JAILDIR )) { 86 1.1 kardel droproot = 1; 87 1.1 kardel chrootdir = OPT_ARG( JAILDIR ); 88 1.1 kardel } 89 1.1 kardel #endif 90 1.1 kardel 91 1.1 kardel if (HAVE_OPT( KEYFILE )) 92 1.1 kardel getauthkeys(OPT_ARG( KEYFILE )); 93 1.1 kardel 94 1.1 kardel if (HAVE_OPT( PIDFILE )) 95 1.8 christos stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ), 1); 96 1.1 kardel 97 1.1 kardel if (HAVE_OPT( QUIT )) 98 1.1 kardel mode_ntpdate = TRUE; 99 1.1 kardel 100 1.1 kardel if (HAVE_OPT( PROPAGATIONDELAY )) 101 1.1 kardel do { 102 1.1 kardel double tmp; 103 1.1 kardel const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY ); 104 1.1 kardel 105 1.1 kardel if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) { 106 1.1 kardel msyslog(LOG_ERR, 107 1.1 kardel "command line broadcast delay value %s undecodable", 108 1.1 kardel my_ntp_optarg); 109 1.1 kardel } else { 110 1.1 kardel proto_config(PROTO_BROADDELAY, 0, tmp, NULL); 111 1.1 kardel } 112 1.1 kardel } while (0); 113 1.1 kardel 114 1.1 kardel if (HAVE_OPT( STATSDIR )) 115 1.8 christos stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ), 1); 116 1.1 kardel 117 1.1 kardel if (HAVE_OPT( TRUSTEDKEY )) { 118 1.1 kardel int ct = STACKCT_OPT( TRUSTEDKEY ); 119 1.1 kardel const char** pp = STACKLST_OPT( TRUSTEDKEY ); 120 1.1 kardel 121 1.1 kardel do { 122 1.1 kardel u_long tkey; 123 1.1 kardel const char* p = *pp++; 124 1.1 kardel 125 1.1 kardel tkey = (int)atol(p); 126 1.1 kardel if (tkey == 0 || tkey > NTP_MAXKEY) { 127 1.1 kardel msyslog(LOG_ERR, 128 1.1 kardel "command line trusted key %s is invalid", 129 1.1 kardel p); 130 1.1 kardel } else { 131 1.1 kardel authtrust(tkey, 1); 132 1.1 kardel } 133 1.1 kardel } while (--ct > 0); 134 1.1 kardel } 135 1.1 kardel 136 1.1 kardel #ifdef HAVE_DROPROOT 137 1.1 kardel if (HAVE_OPT( USER )) { 138 1.1 kardel droproot = 1; 139 1.1 kardel user = estrdup(OPT_ARG( USER )); 140 1.2 christos group = strrchr(user, ':'); 141 1.2 christos if (group != NULL) { 142 1.2 christos size_t len; 143 1.2 christos 144 1.1 kardel *group++ = '\0'; /* get rid of the ':' */ 145 1.2 christos len = group - user; 146 1.2 christos group = estrdup(group); 147 1.2 christos user = erealloc(user, len); 148 1.2 christos } 149 1.1 kardel } 150 1.1 kardel #endif 151 1.1 kardel 152 1.1 kardel if (HAVE_OPT( VAR )) { 153 1.2 christos int ct; 154 1.2 christos const char ** pp; 155 1.2 christos const char * v_assign; 156 1.2 christos 157 1.2 christos ct = STACKCT_OPT( VAR ); 158 1.2 christos pp = STACKLST_OPT( VAR ); 159 1.1 kardel 160 1.1 kardel do { 161 1.2 christos v_assign = *pp++; 162 1.2 christos set_sys_var(v_assign, strlen(v_assign) + 1, RW); 163 1.1 kardel } while (--ct > 0); 164 1.1 kardel } 165 1.1 kardel 166 1.1 kardel if (HAVE_OPT( DVAR )) { 167 1.1 kardel int ct = STACKCT_OPT( DVAR ); 168 1.1 kardel const char** pp = STACKLST_OPT( DVAR ); 169 1.1 kardel 170 1.1 kardel do { 171 1.1 kardel const char* my_ntp_optarg = *pp++; 172 1.1 kardel 173 1.1 kardel set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1, 174 1.1 kardel (u_short) (RW | DEF)); 175 1.1 kardel } while (--ct > 0); 176 1.1 kardel } 177 1.1 kardel 178 1.2 christos if (HAVE_OPT( SLEW )) 179 1.2 christos loop_config(LOOP_MAX, 600); 180 1.2 christos 181 1.1 kardel if (HAVE_OPT( UPDATEINTERVAL )) { 182 1.1 kardel long val = OPT_VALUE_UPDATEINTERVAL; 183 1.8 christos const char errfmt[] = 184 1.8 christos "-U/--updateinterval %ld must be >= 0\n"; 185 1.3 christos 186 1.7 roy if (val >= 0) { 187 1.8 christos endpt_scan_period = val; 188 1.7 roy } else { 189 1.8 christos fprintf(stderr, errfmt, val); 190 1.8 christos msyslog(LOG_ERR, errfmt, val); 191 1.1 kardel errflg++; 192 1.1 kardel } 193 1.1 kardel } 194 1.1 kardel 195 1.1 kardel 196 1.2 christos /* save list of servers from cmd line for config_peers() use */ 197 1.2 christos if (argc > 0) { 198 1.2 christos cmdline_server_count = argc; 199 1.2 christos cmdline_servers = argv; 200 1.2 christos } 201 1.1 kardel 202 1.2 christos /* display usage & exit with any option processing errors */ 203 1.2 christos if (errflg) 204 1.2 christos optionUsage(&ntpdOptions, 2); /* does not return */ 205 1.1 kardel } 206