Deleted Added
full compact
cmd_args.c (182007) cmd_args.c (200576)
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#ifdef SIM
13# include "ntpsim.h"
14# include "ntpdsim-opts.h"
15# define OPTSTRUCT ntpdsimOptions
16#else
17# include "ntpd-opts.h"
18# define OPTSTRUCT ntpdOptions
19#endif /* SIM */
20
21/*
22 * Definitions of things either imported from or exported to outside
23 */
24extern char const *progname;
25extern const char *specific_interface;
26extern short default_ai_family;
27
28#ifdef HAVE_NETINFO
29extern int check_netinfo;
30#endif
31
32
33/*
34 * getCmdOpts - get command line options
35 */
36void
37getCmdOpts(
38 int argc,
39 char *argv[]
40 )
41{
42 extern const char *config_file;
43 int errflg;
44 tOptions *myOptions = &OPTSTRUCT;
45
46 /*
47 * Initialize, initialize
48 */
49 errflg = 0;
50
51 switch (WHICH_IDX_IPV4) {
52 case INDEX_OPT_IPV4:
53 default_ai_family = AF_INET;
54 break;
55 case INDEX_OPT_IPV6:
56 default_ai_family = AF_INET6;
57 break;
58 default:
59 /* ai_fam_templ = ai_fam_default; */
60 break;
61 }
62
63 if (HAVE_OPT( AUTHREQ ))
64 proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
65
66 if (HAVE_OPT( AUTHNOREQ ))
67 proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
68
69 if (HAVE_OPT( BCASTSYNC ))
70 proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
71
72 if (HAVE_OPT( CONFIGFILE )) {
73 config_file = OPT_ARG( CONFIGFILE );
74#ifdef HAVE_NETINFO
75 check_netinfo = 0;
76#endif
77 }
78
79 if (HAVE_OPT( DRIFTFILE ))
80 stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ));
81
82 if (HAVE_OPT( PANICGATE ))
83 allow_panic = TRUE;
84
85 if (HAVE_OPT( JAILDIR )) {
86#ifdef HAVE_DROPROOT
87 droproot = 1;
88 chrootdir = OPT_ARG( JAILDIR );
89#else
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#ifdef SIM
13# include "ntpsim.h"
14# include "ntpdsim-opts.h"
15# define OPTSTRUCT ntpdsimOptions
16#else
17# include "ntpd-opts.h"
18# define OPTSTRUCT ntpdOptions
19#endif /* SIM */
20
21/*
22 * Definitions of things either imported from or exported to outside
23 */
24extern char const *progname;
25extern const char *specific_interface;
26extern short default_ai_family;
27
28#ifdef HAVE_NETINFO
29extern int check_netinfo;
30#endif
31
32
33/*
34 * getCmdOpts - get command line options
35 */
36void
37getCmdOpts(
38 int argc,
39 char *argv[]
40 )
41{
42 extern const char *config_file;
43 int errflg;
44 tOptions *myOptions = &OPTSTRUCT;
45
46 /*
47 * Initialize, initialize
48 */
49 errflg = 0;
50
51 switch (WHICH_IDX_IPV4) {
52 case INDEX_OPT_IPV4:
53 default_ai_family = AF_INET;
54 break;
55 case INDEX_OPT_IPV6:
56 default_ai_family = AF_INET6;
57 break;
58 default:
59 /* ai_fam_templ = ai_fam_default; */
60 break;
61 }
62
63 if (HAVE_OPT( AUTHREQ ))
64 proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
65
66 if (HAVE_OPT( AUTHNOREQ ))
67 proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
68
69 if (HAVE_OPT( BCASTSYNC ))
70 proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
71
72 if (HAVE_OPT( CONFIGFILE )) {
73 config_file = OPT_ARG( CONFIGFILE );
74#ifdef HAVE_NETINFO
75 check_netinfo = 0;
76#endif
77 }
78
79 if (HAVE_OPT( DRIFTFILE ))
80 stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ));
81
82 if (HAVE_OPT( PANICGATE ))
83 allow_panic = TRUE;
84
85 if (HAVE_OPT( JAILDIR )) {
86#ifdef HAVE_DROPROOT
87 droproot = 1;
88 chrootdir = OPT_ARG( JAILDIR );
89#else
90 fprintf(stderr,
91 "command line -i option (jaildir) is not supported by this binary"
92# ifndef SYS_WINNT
93 ",\n" "can not drop root privileges. See configure options\n"
94 "--enable-clockctl and --enable-linuxcaps.\n");
95# else
96 ".\n");
97# endif
98 msyslog(LOG_ERR,
99 "command line -i option (jaildir) is not supported by this binary.");
90 errflg++;
91#endif
92 }
93
94 if (HAVE_OPT( KEYFILE ))
95 getauthkeys(OPT_ARG( KEYFILE ));
96
97 if (HAVE_OPT( PIDFILE ))
98 stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ));
99
100 if (HAVE_OPT( QUIT ))
101 mode_ntpdate = TRUE;
102
103 if (HAVE_OPT( PROPAGATIONDELAY ))
104 do {
105 double tmp;
106 const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
107
108 if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
109 msyslog(LOG_ERR,
110 "command line broadcast delay value %s undecodable",
111 my_ntp_optarg);
112 } else {
113 proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
114 }
115 } while (0);
116
117 if (HAVE_OPT( STATSDIR ))
118 stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ));
119
120 if (HAVE_OPT( TRUSTEDKEY )) {
100 errflg++;
101#endif
102 }
103
104 if (HAVE_OPT( KEYFILE ))
105 getauthkeys(OPT_ARG( KEYFILE ));
106
107 if (HAVE_OPT( PIDFILE ))
108 stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ));
109
110 if (HAVE_OPT( QUIT ))
111 mode_ntpdate = TRUE;
112
113 if (HAVE_OPT( PROPAGATIONDELAY ))
114 do {
115 double tmp;
116 const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
117
118 if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
119 msyslog(LOG_ERR,
120 "command line broadcast delay value %s undecodable",
121 my_ntp_optarg);
122 } else {
123 proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
124 }
125 } while (0);
126
127 if (HAVE_OPT( STATSDIR ))
128 stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ));
129
130 if (HAVE_OPT( TRUSTEDKEY )) {
121 int ct = STACKCT_OPT( TRUSTEDKEY );
122 const char** pp = STACKLST_OPT( TRUSTEDKEY );
131 int ct = STACKCT_OPT( TRUSTEDKEY );
132 const char** pp = STACKLST_OPT( TRUSTEDKEY );
123
124 do {
125 u_long tkey;
126 const char* p = *pp++;
127
128 tkey = (int)atol(p);
129 if (tkey == 0 || tkey > NTP_MAXKEY) {
130 msyslog(LOG_ERR,
131 "command line trusted key %s is invalid",
132 p);
133 } else {
134 authtrust(tkey, 1);
135 }
136 } while (--ct > 0);
137 }
138
139 if (HAVE_OPT( USER )) {
140#ifdef HAVE_DROPROOT
141 char *ntp_optarg = OPT_ARG( USER );
142
143 droproot = 1;
133
134 do {
135 u_long tkey;
136 const char* p = *pp++;
137
138 tkey = (int)atol(p);
139 if (tkey == 0 || tkey > NTP_MAXKEY) {
140 msyslog(LOG_ERR,
141 "command line trusted key %s is invalid",
142 p);
143 } else {
144 authtrust(tkey, 1);
145 }
146 } while (--ct > 0);
147 }
148
149 if (HAVE_OPT( USER )) {
150#ifdef HAVE_DROPROOT
151 char *ntp_optarg = OPT_ARG( USER );
152
153 droproot = 1;
144 user = malloc(strlen(ntp_optarg) + 1);
145 if (user == NULL) {
146 errflg++;
147 } else {
148 (void)strncpy(user, ntp_optarg, strlen(ntp_optarg) + 1);
149 group = rindex(user, ':');
150 if (group)
151 *group++ = '\0'; /* get rid of the ':' */
152 }
154 user = emalloc(strlen(ntp_optarg) + 1);
155 (void)strncpy(user, ntp_optarg, strlen(ntp_optarg) + 1);
156 group = rindex(user, ':');
157 if (group)
158 *group++ = '\0'; /* get rid of the ':' */
153#else
159#else
160 fprintf(stderr,
161 "command line -u/--user option is not supported by this binary"
162# ifndef SYS_WINNT
163 ",\n" "can not drop root privileges. See configure options\n"
164 "--enable-clockctl and --enable-linuxcaps.\n");
165# else
166 ".\n");
167# endif
168 msyslog(LOG_ERR,
169 "command line -u/--user option is not supported by this binary.");
154 errflg++;
155#endif
156 }
157
158 if (HAVE_OPT( VAR )) {
170 errflg++;
171#endif
172 }
173
174 if (HAVE_OPT( VAR )) {
159 int ct = STACKCT_OPT( VAR );
160 const char** pp = STACKLST_OPT( VAR );
175 int ct = STACKCT_OPT( VAR );
176 const char** pp = STACKLST_OPT( VAR );
161
162 do {
163 const char* my_ntp_optarg = *pp++;
164
165 set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
166 (u_short) (RW));
167 } while (--ct > 0);
168 }
169
170 if (HAVE_OPT( DVAR )) {
177
178 do {
179 const char* my_ntp_optarg = *pp++;
180
181 set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
182 (u_short) (RW));
183 } while (--ct > 0);
184 }
185
186 if (HAVE_OPT( DVAR )) {
171 int ct = STACKCT_OPT( DVAR );
172 const char** pp = STACKLST_OPT( DVAR );
187 int ct = STACKCT_OPT( DVAR );
188 const char** pp = STACKLST_OPT( DVAR );
173
174 do {
175 const char* my_ntp_optarg = *pp++;
176
177 set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
178 (u_short) (RW | DEF));
179 } while (--ct > 0);
180 }
181
182 if (HAVE_OPT( SLEW ))
183 clock_max = 600;
184
185 if (HAVE_OPT( UPDATEINTERVAL )) {
186 long val = OPT_VALUE_UPDATEINTERVAL;
187
188 if (val >= 0)
189 interface_interval = val;
190 else {
189
190 do {
191 const char* my_ntp_optarg = *pp++;
192
193 set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
194 (u_short) (RW | DEF));
195 } while (--ct > 0);
196 }
197
198 if (HAVE_OPT( SLEW ))
199 clock_max = 600;
200
201 if (HAVE_OPT( UPDATEINTERVAL )) {
202 long val = OPT_VALUE_UPDATEINTERVAL;
203
204 if (val >= 0)
205 interface_interval = val;
206 else {
191 msyslog(LOG_ERR,
192 "command line interface update interval %ld must be greater or equal to 0",
193 val);
207 fprintf(stderr,
208 "command line interface update interval %ld must not be negative\n",
209 val);
210 msyslog(LOG_ERR,
211 "command line interface update interval %ld must not be negative",
212 val);
194 errflg++;
195 }
196 }
197#ifdef SIM
198 if (HAVE_OPT( SIMBROADCASTDELAY ))
199 sscanf(OPT_ARG( SIMBROADCASTDELAY ), "%lf", &ntp_node.bdly);
200
201 if (HAVE_OPT( PHASENOISE ))
202 sscanf(OPT_ARG( PHASENOISE ), "%lf", &ntp_node.snse);
203
204 if (HAVE_OPT( SIMSLEW ))
205 sscanf(OPT_ARG( SIMSLEW ), "%lf", &ntp_node.slew);
206
207 if (HAVE_OPT( SERVERTIME ))
208 sscanf(OPT_ARG( SERVERTIME ), "%lf", &ntp_node.clk_time);
209
210 if (HAVE_OPT( ENDSIMTIME ))
211 sscanf(OPT_ARG( ENDSIMTIME ), "%lf", &ntp_node.sim_time);
212
213 if (HAVE_OPT( FREQERR ))
214 sscanf(OPT_ARG( FREQERR ), "%lf", &ntp_node.ferr);
215
216 if (HAVE_OPT( WALKNOISE ))
217 sscanf(OPT_ARG( WALKNOISE ), "%lf", &ntp_node.fnse);
218
219 if (HAVE_OPT( NDELAY ))
220 sscanf(OPT_ARG( NDELAY ), "%lf", &ntp_node.ndly);
221
222 if (HAVE_OPT( PDELAY ))
223 sscanf(OPT_ARG( PDELAY ), "%lf", &ntp_node.pdly);
224
225#endif /* SIM */
226
227 if (errflg || argc) {
213 errflg++;
214 }
215 }
216#ifdef SIM
217 if (HAVE_OPT( SIMBROADCASTDELAY ))
218 sscanf(OPT_ARG( SIMBROADCASTDELAY ), "%lf", &ntp_node.bdly);
219
220 if (HAVE_OPT( PHASENOISE ))
221 sscanf(OPT_ARG( PHASENOISE ), "%lf", &ntp_node.snse);
222
223 if (HAVE_OPT( SIMSLEW ))
224 sscanf(OPT_ARG( SIMSLEW ), "%lf", &ntp_node.slew);
225
226 if (HAVE_OPT( SERVERTIME ))
227 sscanf(OPT_ARG( SERVERTIME ), "%lf", &ntp_node.clk_time);
228
229 if (HAVE_OPT( ENDSIMTIME ))
230 sscanf(OPT_ARG( ENDSIMTIME ), "%lf", &ntp_node.sim_time);
231
232 if (HAVE_OPT( FREQERR ))
233 sscanf(OPT_ARG( FREQERR ), "%lf", &ntp_node.ferr);
234
235 if (HAVE_OPT( WALKNOISE ))
236 sscanf(OPT_ARG( WALKNOISE ), "%lf", &ntp_node.fnse);
237
238 if (HAVE_OPT( NDELAY ))
239 sscanf(OPT_ARG( NDELAY ), "%lf", &ntp_node.ndly);
240
241 if (HAVE_OPT( PDELAY ))
242 sscanf(OPT_ARG( PDELAY ), "%lf", &ntp_node.pdly);
243
244#endif /* SIM */
245
246 if (errflg || argc) {
228 printf("argc is <%d>\n", argc);
247 if (argc)
248 fprintf(stderr, "argc after processing is <%d>\n", argc);
229 optionUsage(myOptions, 2);
230 }
231 return;
232}
249 optionUsage(myOptions, 2);
250 }
251 return;
252}