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