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