xref: /netbsd-src/external/bsd/ntp/dist/ntpd/cmd_args.c (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: cmd_args.c,v 1.1.1.2 2012/01/31 21:26:01 kardel 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_cmdargs.h"
13 
14 #include "ntpd-opts.h"
15 
16 /*
17  * Definitions of things either imported from or exported to outside
18  */
19 extern char const *progname;
20 extern const char *specific_interface;
21 
22 #ifdef HAVE_NETINFO
23 extern int	check_netinfo;
24 #endif
25 
26 
27 /*
28  * getCmdOpts - get command line options
29  */
30 void
31 getCmdOpts(
32 	int argc,
33 	char *argv[]
34 	)
35 {
36 	extern const char *config_file;
37 	int errflg;
38 	tOptions *myOptions = &ntpdOptions;
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 = rindex(user, ':');
136 		if (group)
137 			*group++ = '\0'; /* get rid of the ':' */
138 	}
139 #endif
140 
141 	if (HAVE_OPT( VAR )) {
142 		int		ct = STACKCT_OPT(  VAR );
143 		const char**	pp = STACKLST_OPT( VAR );
144 
145 		do  {
146 			const char* my_ntp_optarg = *pp++;
147 
148 			set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
149 			    (u_short) (RW));
150 		} while (--ct > 0);
151 	}
152 
153 	if (HAVE_OPT( DVAR )) {
154 		int		ct = STACKCT_OPT(  DVAR );
155 		const char**	pp = STACKLST_OPT( DVAR );
156 
157 		do  {
158 			const char* my_ntp_optarg = *pp++;
159 
160 			set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
161 			    (u_short) (RW | DEF));
162 		} while (--ct > 0);
163 	}
164 
165 	if (HAVE_OPT( SLEW )) {
166 		clock_max = 600;
167 		kern_enable = 0;
168 	}
169 	if (HAVE_OPT( UPDATEINTERVAL )) {
170 		long val = OPT_VALUE_UPDATEINTERVAL;
171 
172 		if (val >= 0)
173 			interface_interval = val;
174 		else {
175 			fprintf(stderr,
176 				"command line interface update interval %ld must not be negative\n",
177 				val);
178 			msyslog(LOG_ERR,
179 				"command line interface update interval %ld must not be negative",
180 				val);
181 			errflg++;
182 		}
183 	}
184 #ifdef SIM
185 
186 	/* SK:
187 	 * The simulator no longer takes any command line arguments. Hence,
188 	 * all the code that was here has been removed.
189 	 */
190 
191 #endif /* SIM */
192 
193 	if (errflg || argc) {
194 		if (argc)
195 			fprintf(stderr, "argc after processing is <%d>\n", argc);
196 		optionUsage(myOptions, 2);
197 	}
198 	return;
199 }
200