xref: /freebsd/contrib/ntp/ntpd/cmd_args.c (revision 5b9c547c)
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 #ifdef HAVE_DROPROOT
80 	if (HAVE_OPT( JAILDIR )) {
81 		droproot = 1;
82 		chrootdir = OPT_ARG( JAILDIR );
83 	}
84 #endif
85 
86 	if (HAVE_OPT( KEYFILE ))
87 		getauthkeys(OPT_ARG( KEYFILE ));
88 
89 	if (HAVE_OPT( PIDFILE ))
90 		stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ));
91 
92 	if (HAVE_OPT( QUIT ))
93 		mode_ntpdate = TRUE;
94 
95 	if (HAVE_OPT( PROPAGATIONDELAY ))
96 		do {
97 			double tmp;
98 			const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
99 
100 			if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
101 				msyslog(LOG_ERR,
102 					"command line broadcast delay value %s undecodable",
103 					my_ntp_optarg);
104 			} else {
105 				proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
106 			}
107 		} while (0);
108 
109 	if (HAVE_OPT( STATSDIR ))
110 		stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ));
111 
112 	if (HAVE_OPT( TRUSTEDKEY )) {
113 		int		ct = STACKCT_OPT(  TRUSTEDKEY );
114 		const char**	pp = STACKLST_OPT( TRUSTEDKEY );
115 
116 		do  {
117 			u_long tkey;
118 			const char* p = *pp++;
119 
120 			tkey = (int)atol(p);
121 			if (tkey == 0 || tkey > NTP_MAXKEY) {
122 				msyslog(LOG_ERR,
123 				    "command line trusted key %s is invalid",
124 				    p);
125 			} else {
126 				authtrust(tkey, 1);
127 			}
128 		} while (--ct > 0);
129 	}
130 
131 #ifdef HAVE_DROPROOT
132 	if (HAVE_OPT( USER )) {
133 		droproot = 1;
134 		user = estrdup(OPT_ARG( USER ));
135 		group = strrchr(user, ':');
136 		if (group != NULL) {
137 			size_t	len;
138 
139 			*group++ = '\0'; /* get rid of the ':' */
140 			len = group - user;
141 			group = estrdup(group);
142 			user = erealloc(user, len);
143 		}
144 	}
145 #endif
146 
147 	if (HAVE_OPT( VAR )) {
148 		int		ct;
149 		const char **	pp;
150 		const char *	v_assign;
151 
152 		ct = STACKCT_OPT(  VAR );
153 		pp = STACKLST_OPT( VAR );
154 
155 		do  {
156 			v_assign = *pp++;
157 			set_sys_var(v_assign, strlen(v_assign) + 1, RW);
158 		} while (--ct > 0);
159 	}
160 
161 	if (HAVE_OPT( DVAR )) {
162 		int		ct = STACKCT_OPT(  DVAR );
163 		const char**	pp = STACKLST_OPT( DVAR );
164 
165 		do  {
166 			const char* my_ntp_optarg = *pp++;
167 
168 			set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
169 			    (u_short) (RW | DEF));
170 		} while (--ct > 0);
171 	}
172 
173 	if (HAVE_OPT( SLEW ))
174 		loop_config(LOOP_MAX, 600);
175 
176 	if (HAVE_OPT( UPDATEINTERVAL )) {
177 		long val = OPT_VALUE_UPDATEINTERVAL;
178 
179 		if (val >= 0)
180 			interface_interval = val;
181 		else {
182 			fprintf(stderr,
183 				"command line interface update interval %ld must not be negative\n",
184 				val);
185 			msyslog(LOG_ERR,
186 				"command line interface update interval %ld must not be negative",
187 				val);
188 			errflg++;
189 		}
190 	}
191 
192 
193 	/* save list of servers from cmd line for config_peers() use */
194 	if (argc > 0) {
195 		cmdline_server_count = argc;
196 		cmdline_servers = argv;
197 	}
198 
199 	/* display usage & exit with any option processing errors */
200 	if (errflg)
201 		optionUsage(&ntpdOptions, 2);	/* does not return */
202 }
203