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