1/*
2 * cmd_args.c = command-line argument processing
3 */
4#ifdef HAVE_CONFIG_H
5# include <config.h>
6#endif
7
8#include "ntpd.h"
9#include "ntp_stdlib.h"
10#include "ntp_cmdargs.h"
11
12#include "ntpd-opts.h"
13
14/*
15 * Definitions of things either imported from or exported to outside
16 */
17extern char const *progname;
18extern const char *specific_interface;
19extern short default_ai_family;
20
21#ifdef HAVE_NETINFO
22extern int	check_netinfo;
23#endif
24
25
26/*
27 * getCmdOpts - get command line options
28 */
29void
30getCmdOpts(
31	int argc,
32	char *argv[]
33	)
34{
35	extern const char *config_file;
36	int errflg;
37	tOptions *myOptions = &ntpdOptions;
38
39	/*
40	 * Initialize, initialize
41	 */
42	errflg = 0;
43
44	if (HAVE_OPT( IPV4 ))
45		default_ai_family = AF_INET;
46	else if (HAVE_OPT( IPV6 ))
47		default_ai_family = AF_INET6;
48
49	if (HAVE_OPT( AUTHREQ ))
50		proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
51	else if (HAVE_OPT( AUTHNOREQ ))
52		proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
53
54	if (HAVE_OPT( BCASTSYNC ))
55		proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
56
57	if (HAVE_OPT( CONFIGFILE )) {
58		config_file = OPT_ARG( CONFIGFILE );
59#ifdef HAVE_NETINFO
60		check_netinfo = 0;
61#endif
62	}
63
64	if (HAVE_OPT( DRIFTFILE ))
65		stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ));
66
67	if (HAVE_OPT( PANICGATE ))
68		allow_panic = TRUE;
69
70#ifdef HAVE_DROPROOT
71	if (HAVE_OPT( JAILDIR )) {
72		droproot = 1;
73		chrootdir = OPT_ARG( JAILDIR );
74	}
75#endif
76
77	if (HAVE_OPT( KEYFILE ))
78		getauthkeys(OPT_ARG( KEYFILE ));
79
80	if (HAVE_OPT( PIDFILE ))
81		stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ));
82
83	if (HAVE_OPT( QUIT ))
84		mode_ntpdate = TRUE;
85
86	if (HAVE_OPT( PROPAGATIONDELAY ))
87		do {
88			double tmp;
89			const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
90
91			if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
92				msyslog(LOG_ERR,
93					"command line broadcast delay value %s undecodable",
94					my_ntp_optarg);
95			} else {
96				proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
97			}
98		} while (0);
99
100	if (HAVE_OPT( STATSDIR ))
101		stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ));
102
103	if (HAVE_OPT( TRUSTEDKEY )) {
104		int		ct = STACKCT_OPT(  TRUSTEDKEY );
105		const char**	pp = STACKLST_OPT( TRUSTEDKEY );
106
107		do  {
108			u_long tkey;
109			const char* p = *pp++;
110
111			tkey = (int)atol(p);
112			if (tkey == 0 || tkey > NTP_MAXKEY) {
113				msyslog(LOG_ERR,
114				    "command line trusted key %s is invalid",
115				    p);
116			} else {
117				authtrust(tkey, 1);
118			}
119		} while (--ct > 0);
120	}
121
122#ifdef HAVE_DROPROOT
123	if (HAVE_OPT( USER )) {
124		droproot = 1;
125		user = estrdup(OPT_ARG( USER ));
126		group = rindex(user, ':');
127		if (group)
128			*group++ = '\0'; /* get rid of the ':' */
129	}
130#endif
131
132	if (HAVE_OPT( VAR )) {
133		int		ct = STACKCT_OPT(  VAR );
134		const char**	pp = STACKLST_OPT( VAR );
135
136		do  {
137			const char* my_ntp_optarg = *pp++;
138
139			set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
140			    (u_short) (RW));
141		} while (--ct > 0);
142	}
143
144	if (HAVE_OPT( DVAR )) {
145		int		ct = STACKCT_OPT(  DVAR );
146		const char**	pp = STACKLST_OPT( DVAR );
147
148		do  {
149			const char* my_ntp_optarg = *pp++;
150
151			set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
152			    (u_short) (RW | DEF));
153		} while (--ct > 0);
154	}
155
156	if (HAVE_OPT( SLEW )) {
157		clock_max = 600;
158		kern_enable = 0;
159	}
160	if (HAVE_OPT( UPDATEINTERVAL )) {
161		long val = OPT_VALUE_UPDATEINTERVAL;
162
163		if (val >= 0)
164			interface_interval = val;
165		else {
166			fprintf(stderr,
167				"command line interface update interval %ld must not be negative\n",
168				val);
169			msyslog(LOG_ERR,
170				"command line interface update interval %ld must not be negative",
171				val);
172			errflg++;
173		}
174	}
175#ifdef SIM
176
177	/* SK:
178	 * The simulator no longer takes any command line arguments. Hence,
179	 * all the code that was here has been removed.
180	 */
181
182#endif /* SIM */
183
184	if (errflg || argc) {
185		if (argc)
186			fprintf(stderr, "argc after processing is <%d>\n", argc);
187		optionUsage(myOptions, 2);
188	}
189	return;
190}
191