summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/ntpd/cmd_args.c
blob: 3ed9b666fcf9543ef682c657b725ed7e76ec5a34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * cmd_args.c = command-line argument processing
 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ntpd.h"
#include "ntp_stdlib.h"
#include "ntp_cmdargs.h"

#ifdef SIM
#include "ntpsim.h"
#endif /* SIM */

/*
 * Definitions of things either imported from or exported to outside
 */
extern char const *progname;
int	listen_to_virtual_ips = 1;

#ifdef SYS_WINNT
extern BOOL NoWinService;
#endif

static const char *ntp_options = "aAbB:c:C:dD:f:gi:k:l:LmnNO:p:P:qr:s:S:t:T:W:u:v:V:xY:Z:-:";

#ifdef HAVE_NETINFO
extern int	check_netinfo;
#endif


/*
 * getstartup - search through the options looking for a debugging flag
 */
void
getstartup(
	int argc,
	char *argv[]
	)
{
	int errflg;
	extern int priority_done;
	int c;

#ifdef DEBUG
	debug = 0;		/* no debugging by default */
#endif

	/*
	 * This is a big hack.	We don't really want to read command line
	 * configuration until everything else is initialized, since
	 * the ability to configure the system may depend on storage
	 * and the like having been initialized.  Except that we also
	 * don't want to initialize anything until after detaching from
	 * the terminal, but we won't know to do that until we've
	 * parsed the command line.  Do that now, crudely, and do it
	 * again later.  Our ntp_getopt() is explicitly reusable, by the
	 * way.  Your own mileage may vary.
	 *
	 * This hack is even called twice (to allow complete logging to file)
	 */
	errflg = 0;
	progname = argv[0];

	/*
	 * Decode argument list
	 */
	while ((c = ntp_getopt(argc, argv, ntp_options)) != EOF)
	    switch (c) {
#ifdef DEBUG
		case 'd':
		    ++debug;
		    break;
		case 'D':
		    debug = (int)atol(ntp_optarg);
		    printf("Debug1: %s -> %x = %d\n", ntp_optarg, debug, debug);
		    break;
#else
		case 'd':
		case 'D':
		    msyslog(LOG_ERR, "ntpd not compiled with -DDEBUG option - no DEBUG support");
		    fprintf(stderr, "ntpd not compiled with -DDEBUG option - no DEBUG support\n");
		    ++errflg;
		    break;
#endif
		case 'L':
		    listen_to_virtual_ips = 0;
		    break;
		case 'l':
			{
				FILE *new_file;

				if(strcmp(ntp_optarg, "stderr") == 0)
					new_file = stderr;
				else if(strcmp(ntp_optarg, "stdout") == 0)
					new_file = stdout;
				else
					new_file = fopen(ntp_optarg, "a");
				if (new_file != NULL) {
					NLOG(NLOG_SYSINFO)
						msyslog(LOG_NOTICE, "logging to file %s", ntp_optarg);
					if (syslog_file != NULL &&
						fileno(syslog_file) != fileno(new_file))
						(void)fclose(syslog_file);

					syslog_file = new_file;
					syslogit = 0;
				}
				else
					msyslog(LOG_ERR,
						"Cannot open log file %s",
						ntp_optarg);
			}
			break;

		case 'n':
		case 'q':
		    ++nofork;
#ifdef SYS_WINNT
		    NoWinService = TRUE;	 
#endif
		    break;

		case 'N':
		    priority_done = 0;
		    break;
			
		case '?':
		    ++errflg;
		    break;

	    case '-':
	      if ( ! strcmp(ntp_optarg, "version") ) {
		printf("%.80s: %.80s\n", progname, Version);
		exit(0);
	      } else if ( ! strcmp(ntp_optarg, "help") ) {
		/* usage(); */
		/* exit(0); */
		++errflg;
	      } else if ( ! strcmp(ntp_optarg, "copyright") ) {
		printf("unknown\n");
		exit(0);
	      } else {
		fprintf(stderr, "%.80s: Error unknown argument '--%.80s'\n",
			progname,
			ntp_optarg);
		exit(12);
	      }
	      break;

		default:
			break;
		}

	if (errflg || ntp_optind != argc) {
		(void) fprintf(stderr, "usage: %s [ -abdgmnqx ] [ -c config_file ] [ -e e_delay ]\n", progname);
		(void) fprintf(stderr, "\t\t[ -f freq_file ] [ -k key_file ] [ -l log_file ]\n");
		(void) fprintf(stderr, "\t\t[ -p pid_file ] [ -r broad_delay ] [ -s statdir ]\n");
		(void) fprintf(stderr, "\t\t[ -t trust_key ] [ -v sys_var ] [ -V default_sysvar ]\n");
#if defined(HAVE_SCHED_SETSCHEDULER)
		(void) fprintf(stderr, "\t\t[ -P fixed_process_priority ]\n");
#endif
#ifdef HAVE_CLOCKCTL
		(void) fprintf(stderr, "\t\t[ -u user[:group] ] [ -i chrootdir ]\n");
#endif
		exit(2);
	}
	ntp_optind = 0;	/* reset ntp_optind to restart ntp_getopt */

#ifdef DEBUG
	if (debug) {
#ifdef HAVE_SETVBUF
		static char buf[BUFSIZ];
		setvbuf(stdout, buf, _IOLBF, BUFSIZ);
#else
		setlinebuf(stdout);
#endif
	}
#endif
}

/*
 * getCmdOpts - get command line options
 */
void
getCmdOpts(
	int argc,
	char *argv[]
	)
{
	extern char *config_file;
	struct sockaddr_in inaddrntp;
	int errflg;
	int c;

	/*
	 * Initialize, initialize
	 */
	errflg = 0;
#ifdef DEBUG
	debug = 0;
#endif	/* DEBUG */

	progname = argv[0];

	/*
	 * Decode argument list
	 */
	while ((c = ntp_getopt(argc, argv, ntp_options)) != EOF) {
		switch (c) {
		    case 'a':
			proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
			break;

		    case 'A':
			proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
			break;

		    case 'b':
			proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
			break;

		    case 'c':
			config_file = ntp_optarg;
#ifdef HAVE_NETINFO
			check_netinfo = 0;
#endif
			break;

		    case 'd':
#ifdef DEBUG
			debug++;
#else
			errflg++;
#endif	/* DEBUG */
			break;

		    case 'D':
#ifdef DEBUG
			debug = (int)atol(ntp_optarg);
			printf("Debug2: %s -> %x = %d\n", ntp_optarg, debug, debug);
#else
			errflg++;
#endif	/* DEBUG */
			break;

		    case 'f':
			stats_config(STATS_FREQ_FILE, ntp_optarg);
			break;

		    case 'g':
			allow_panic = TRUE;
			break;

		    case 'i':
#ifdef HAVE_CLOCKCTL
			if (!ntp_optarg)
				errflg++;
			else
				chrootdir = ntp_optarg;
			break;
#else
			errflg++;
#endif
		    case 'k':
			getauthkeys(ntp_optarg);
			break;

		    case 'L':   /* already done at pre-scan */
		    case 'l':   /* already done at pre-scan */
			break;

		    case 'm':
			inaddrntp.sin_family = AF_INET;
			inaddrntp.sin_port = htons(NTP_PORT);
			inaddrntp.sin_addr.s_addr = htonl(INADDR_NTP);
			proto_config(PROTO_MULTICAST_ADD, 0, 0., (struct sockaddr_storage*)&inaddrntp);
			sys_bclient = 1;
			break;

		    case 'n':	/* already done at pre-scan */
			break;

		    case 'N':	/* already done at pre-scan */
			break;

		    case 'p':
			stats_config(STATS_PID_FILE, ntp_optarg);
			break;

		    case 'P':
#if defined(HAVE_SCHED_SETSCHEDULER)
			config_priority = (int)atol(ntp_optarg);
			config_priority_override = 1;
#else
			errflg++;
#endif
			break;

		    case 'q':
			mode_ntpdate = TRUE;
			break;

		    case 'r':
			do {
				double tmp;

				if (sscanf(ntp_optarg, "%lf", &tmp) != 1) {
					msyslog(LOG_ERR,
						"command line broadcast delay value %s undecodable",
						ntp_optarg);
				} else {
					proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
				}
			} while (0);
			break;
			
		    case 'u':
#ifdef HAVE_CLOCKCTL
			user = malloc(strlen(ntp_optarg) + 1);
			if ((user == NULL) || (ntp_optarg == NULL))
				errflg++;
			(void)strncpy(user, ntp_optarg, strlen(ntp_optarg) + 1);
			group = rindex(user, ':');
			if (group)
				*group++ = '\0'; /* get rid of the ':' */
#else
			errflg++;
#endif
			break;
		    case 's':
			stats_config(STATS_STATSDIR, ntp_optarg);
			break;
			
		    case 't':
			do {
				u_long tkey;
				
				tkey = (int)atol(ntp_optarg);
				if (tkey <= 0 || tkey > NTP_MAXKEY) {
					msyslog(LOG_ERR,
					    "command line trusted key %s is invalid",
					    ntp_optarg);
				} else {
					authtrust(tkey, 1);
				}
			} while (0);
			break;

		    case 'v':
		    case 'V':
			set_sys_var(ntp_optarg, strlen(ntp_optarg)+1,
			    (u_short) (RW | ((c == 'V') ? DEF : 0)));
			break;

		    case 'x':
			clock_max = 600;
			break;
#ifdef SIM
		case 'B':
			sscanf(ntp_optarg, "%lf", &ntp_node.bdly);
                        break;

		case 'C':
			sscanf(ntp_optarg, "%lf", &ntp_node.snse);
                        break;

		case 'H':
			sscanf(ntp_optarg, "%lf", &ntp_node.slew);
                        break;

		case 'O':
			sscanf(ntp_optarg, "%lf", &ntp_node.clk_time);
                        break;

		case 'S':
			sscanf(ntp_optarg, "%lf", &ntp_node.sim_time);
                        break;

		case 'T':
			sscanf(ntp_optarg, "%lf", &ntp_node.ferr);
                        break;

		case 'W':
			sscanf(ntp_optarg, "%lf", &ntp_node.fnse);
                        break;

		case 'Y':
			sscanf(ntp_optarg, "%lf", &ntp_node.ndly);
                        break;

		case 'Z': 
			sscanf(ntp_optarg, "%lf", &ntp_node.pdly);
                        break;

#endif /* SIM */
		    default:
			errflg++;
			break;
		}
	}

	if (errflg || ntp_optind != argc) {
		(void) fprintf(stderr, "usage: %s [ -abdgmnx ] [ -c config_file ] [ -e e_delay ]\n", progname);
		(void) fprintf(stderr, "\t\t[ -f freq_file ] [ -k key_file ] [ -l log_file ]\n");
		(void) fprintf(stderr, "\t\t[ -p pid_file ] [ -r broad_delay ] [ -s statdir ]\n");
		(void) fprintf(stderr, "\t\t[ -t trust_key ] [ -v sys_var ] [ -V default_sysvar ]\n");
#if defined(HAVE_SCHED_SETSCHEDULER)
		(void) fprintf(stderr, "\t\t[ -P fixed_process_priority ]\n");
#endif
#ifdef HAVE_CLOCKCTL
		(void) fprintf(stderr, "\t\t[ -u user[:group] ] [ -i chrootdir ]\n");
#endif
		exit(2);
	}
	return;
}
OpenPOWER on IntegriCloud