182498Sroberto/*
282498Sroberto * cmd_args.c = command-line argument processing
382498Sroberto */
482498Sroberto#ifdef HAVE_CONFIG_H
582498Sroberto# include <config.h>
682498Sroberto#endif
782498Sroberto
882498Sroberto#include "ntpd.h"
982498Sroberto#include "ntp_stdlib.h"
10285612Sdelphij#include "ntp_config.h"
1182498Sroberto#include "ntp_cmdargs.h"
1282498Sroberto
13285612Sdelphij#include "ntpd-opts.h"
14132451Sroberto
1582498Sroberto/*
1682498Sroberto * Definitions of things either imported from or exported to outside
1782498Sroberto */
1882498Srobertoextern char const *progname;
1982498Sroberto
2082498Sroberto#ifdef HAVE_NETINFO
2182498Srobertoextern int	check_netinfo;
2282498Sroberto#endif
2382498Sroberto
2482498Sroberto
2582498Sroberto/*
26285612Sdelphij * getCmdOpts - apply most command line options
27285612Sdelphij *
28285612Sdelphij * A few options are examined earlier in ntpd.c ntpdmain() and
29285612Sdelphij * ports/winnt/ntpd/ntservice.c main().
3082498Sroberto */
3182498Srobertovoid
32182007SrobertogetCmdOpts(
33285612Sdelphij	int	argc,
34285612Sdelphij	char **	argv
3582498Sroberto	)
3682498Sroberto{
37182007Sroberto	extern const char *config_file;
3882498Sroberto	int errflg;
3982498Sroberto
4082498Sroberto	/*
41182007Sroberto	 * Initialize, initialize
4282498Sroberto	 */
4382498Sroberto	errflg = 0;
4482498Sroberto
45285612Sdelphij	if (ipv4_works && ipv6_works) {
46285612Sdelphij		if (HAVE_OPT( IPV4 ))
47285612Sdelphij			ipv6_works = 0;
48285612Sdelphij		else if (HAVE_OPT( IPV6 ))
49285612Sdelphij			ipv4_works = 0;
50285612Sdelphij	} else if (!ipv4_works && !ipv6_works) {
51285612Sdelphij		msyslog(LOG_ERR, "Neither IPv4 nor IPv6 networking detected, fatal.");
52285612Sdelphij		exit(1);
53285612Sdelphij	} else if (HAVE_OPT( IPV4 ) && !ipv4_works)
54285612Sdelphij		msyslog(LOG_WARNING, "-4/--ipv4 ignored, IPv4 networking not found.");
55285612Sdelphij	else if (HAVE_OPT( IPV6 ) && !ipv6_works)
56285612Sdelphij		msyslog(LOG_WARNING, "-6/--ipv6 ignored, IPv6 networking not found.");
5782498Sroberto
58182007Sroberto	if (HAVE_OPT( AUTHREQ ))
59182007Sroberto		proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
60285612Sdelphij	else if (HAVE_OPT( AUTHNOREQ ))
61182007Sroberto		proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
6282498Sroberto
63182007Sroberto	if (HAVE_OPT( BCASTSYNC ))
64182007Sroberto		proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
65182007Sroberto
66182007Sroberto	if (HAVE_OPT( CONFIGFILE )) {
67182007Sroberto		config_file = OPT_ARG( CONFIGFILE );
68182007Sroberto#ifdef HAVE_NETINFO
69182007Sroberto		check_netinfo = 0;
70132451Sroberto#endif
71182007Sroberto	}
7282498Sroberto
73182007Sroberto	if (HAVE_OPT( DRIFTFILE ))
74182007Sroberto		stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ));
7582498Sroberto
76182007Sroberto	if (HAVE_OPT( PANICGATE ))
77182007Sroberto		allow_panic = TRUE;
78132451Sroberto
79285612Sdelphij	if (HAVE_OPT( FORCE_STEP_ONCE ))
80285612Sdelphij		force_step_once = TRUE;
81285612Sdelphij
82285612Sdelphij#ifdef HAVE_DROPROOT
83182007Sroberto	if (HAVE_OPT( JAILDIR )) {
84285612Sdelphij		droproot = 1;
85285612Sdelphij		chrootdir = OPT_ARG( JAILDIR );
86285612Sdelphij	}
8782498Sroberto#endif
8882498Sroberto
89182007Sroberto	if (HAVE_OPT( KEYFILE ))
90182007Sroberto		getauthkeys(OPT_ARG( KEYFILE ));
9182498Sroberto
92182007Sroberto	if (HAVE_OPT( PIDFILE ))
93182007Sroberto		stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ));
9482498Sroberto
95182007Sroberto	if (HAVE_OPT( QUIT ))
96182007Sroberto		mode_ntpdate = TRUE;
9782498Sroberto
98182007Sroberto	if (HAVE_OPT( PROPAGATIONDELAY ))
99182007Sroberto		do {
100182007Sroberto			double tmp;
101182007Sroberto			const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
10282498Sroberto
103182007Sroberto			if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
104182007Sroberto				msyslog(LOG_ERR,
105182007Sroberto					"command line broadcast delay value %s undecodable",
106182007Sroberto					my_ntp_optarg);
107182007Sroberto			} else {
108182007Sroberto				proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
109182007Sroberto			}
110182007Sroberto		} while (0);
11182498Sroberto
112182007Sroberto	if (HAVE_OPT( STATSDIR ))
113182007Sroberto		stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ));
11482498Sroberto
115182007Sroberto	if (HAVE_OPT( TRUSTEDKEY )) {
116200576Sroberto		int		ct = STACKCT_OPT(  TRUSTEDKEY );
117200576Sroberto		const char**	pp = STACKLST_OPT( TRUSTEDKEY );
11882498Sroberto
119182007Sroberto		do  {
120182007Sroberto			u_long tkey;
121182007Sroberto			const char* p = *pp++;
12282498Sroberto
123182007Sroberto			tkey = (int)atol(p);
124182007Sroberto			if (tkey == 0 || tkey > NTP_MAXKEY) {
125182007Sroberto				msyslog(LOG_ERR,
126182007Sroberto				    "command line trusted key %s is invalid",
127182007Sroberto				    p);
128182007Sroberto			} else {
129182007Sroberto				authtrust(tkey, 1);
130182007Sroberto			}
131182007Sroberto		} while (--ct > 0);
132182007Sroberto	}
13382498Sroberto
134285612Sdelphij#ifdef HAVE_DROPROOT
135182007Sroberto	if (HAVE_OPT( USER )) {
136285612Sdelphij		droproot = 1;
137285612Sdelphij		user = estrdup(OPT_ARG( USER ));
138285612Sdelphij		group = strrchr(user, ':');
139285612Sdelphij		if (group != NULL) {
140285612Sdelphij			size_t	len;
14182498Sroberto
142200576Sroberto			*group++ = '\0'; /* get rid of the ':' */
143285612Sdelphij			len = group - user;
144285612Sdelphij			group = estrdup(group);
145285612Sdelphij			user = erealloc(user, len);
146285612Sdelphij		}
147285612Sdelphij	}
148132451Sroberto#endif
14982498Sroberto
150182007Sroberto	if (HAVE_OPT( VAR )) {
151285612Sdelphij		int		ct;
152285612Sdelphij		const char **	pp;
153285612Sdelphij		const char *	v_assign;
15482498Sroberto
155285612Sdelphij		ct = STACKCT_OPT(  VAR );
156285612Sdelphij		pp = STACKLST_OPT( VAR );
157285612Sdelphij
158182007Sroberto		do  {
159285612Sdelphij			v_assign = *pp++;
160285612Sdelphij			set_sys_var(v_assign, strlen(v_assign) + 1, RW);
161182007Sroberto		} while (--ct > 0);
162182007Sroberto	}
16382498Sroberto
164182007Sroberto	if (HAVE_OPT( DVAR )) {
165200576Sroberto		int		ct = STACKCT_OPT(  DVAR );
166200576Sroberto		const char**	pp = STACKLST_OPT( DVAR );
16782498Sroberto
168182007Sroberto		do  {
169182007Sroberto			const char* my_ntp_optarg = *pp++;
17082498Sroberto
171182007Sroberto			set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
172182007Sroberto			    (u_short) (RW | DEF));
173182007Sroberto		} while (--ct > 0);
174182007Sroberto	}
17582498Sroberto
176182007Sroberto	if (HAVE_OPT( SLEW ))
177285612Sdelphij		loop_config(LOOP_MAX, 600);
17882498Sroberto
179182007Sroberto	if (HAVE_OPT( UPDATEINTERVAL )) {
180182007Sroberto		long val = OPT_VALUE_UPDATEINTERVAL;
181285612Sdelphij
182182007Sroberto		if (val >= 0)
183182007Sroberto			interface_interval = val;
184182007Sroberto		else {
185285612Sdelphij			fprintf(stderr,
186200576Sroberto				"command line interface update interval %ld must not be negative\n",
187200576Sroberto				val);
188285612Sdelphij			msyslog(LOG_ERR,
189200576Sroberto				"command line interface update interval %ld must not be negative",
190200576Sroberto				val);
191132451Sroberto			errflg++;
192182007Sroberto		}
193182007Sroberto	}
19482498Sroberto
195132451Sroberto
196285612Sdelphij	/* save list of servers from cmd line for config_peers() use */
197285612Sdelphij	if (argc > 0) {
198285612Sdelphij		cmdline_server_count = argc;
199285612Sdelphij		cmdline_servers = argv;
200285612Sdelphij	}
201132451Sroberto
202285612Sdelphij	/* display usage & exit with any option processing errors */
203285612Sdelphij	if (errflg)
204285612Sdelphij		optionUsage(&ntpdOptions, 2);	/* does not return */
20582498Sroberto}
206