xref: /netbsd-src/external/bsd/ntp/dist/ntpdc/ntpdc.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: ntpdc.c,v 1.7 2013/12/28 03:20:14 christos Exp $	*/
2 
3 /*
4  * ntpdc - control and monitor your ntpd daemon
5  */
6 #include <config.h>
7 #include <stdio.h>
8 #include <stddef.h>
9 #include <ctype.h>
10 #include <signal.h>
11 #include <setjmp.h>
12 #ifdef HAVE_UNISTD_H
13 # include <unistd.h>
14 #endif
15 #ifdef HAVE_FCNTL_H
16 # include <fcntl.h>
17 #endif
18 #ifdef SYS_WINNT
19 # include <mswsock.h>
20 #endif
21 #include <isc/net.h>
22 #include <isc/result.h>
23 
24 #include "ntpdc.h"
25 #include "ntp_select.h"
26 #include "ntp_stdlib.h"
27 #include "ntp_assert.h"
28 #include "ntp_lineedit.h"
29 #ifdef OPENSSL
30 #include "openssl/evp.h"
31 #include "openssl/objects.h"
32 #endif
33 #include <ssl_applink.c>
34 
35 #include "ntp_libopts.h"
36 #include "ntpdc-opts.h"
37 
38 #ifdef SYS_VXWORKS
39 				/* vxWorks needs mode flag -casey*/
40 # define open(name, flags)   open(name, flags, 0777)
41 # define SERVER_PORT_NUM     123
42 #endif
43 
44 /* We use COMMAND as an autogen keyword */
45 #ifdef COMMAND
46 # undef COMMAND
47 #endif
48 
49 /*
50  * Because we now potentially understand a lot of commands (and
51  * it requires a lot of commands to talk to ntpd) we will run
52  * interactive if connected to a terminal.
53  */
54 static	int	interactive = 0;	/* set to 1 when we should prompt */
55 static	const char *	prompt = "ntpdc> ";	/* prompt to ask him about */
56 
57 /*
58  * Keyid used for authenticated requests.  Obtained on the fly.
59  */
60 static	u_long	info_auth_keyid;
61 static int keyid_entered = 0;
62 
63 static	int	info_auth_keytype = NID_md5;	/* MD5 */
64 static	size_t	info_auth_hashlen = 16;		/* MD5 */
65 u_long	current_time;		/* needed by authkeys; not used */
66 
67 /*
68  * for get_systime()
69  */
70 s_char	sys_precision;		/* local clock precision (log2 s) */
71 
72 int		ntpdcmain	(int,	char **);
73 /*
74  * Built in command handler declarations
75  */
76 static	int	openhost	(const char *);
77 static	int	sendpkt		(void *, size_t);
78 static	void	growpktdata	(void);
79 static	int	getresponse	(int, int, int *, int *, char **, int);
80 static	int	sendrequest	(int, int, int, u_int, size_t, char *);
81 static	void	getcmds		(void);
82 static	RETSIGTYPE abortcmd	(int);
83 static	void	docmd		(const char *);
84 static	void	tokenize	(const char *, char **, int *);
85 static	int	findcmd		(char *, struct xcmd *, struct xcmd *, struct xcmd **);
86 static	int	getarg		(char *, int, arg_v *);
87 static	int	getnetnum	(const char *, sockaddr_u *, char *, int);
88 static	void	help		(struct parse *, FILE *);
89 static	int	helpsort	(const void *, const void *);
90 static	void	printusage	(struct xcmd *, FILE *);
91 static	void	timeout		(struct parse *, FILE *);
92 static	void	my_delay	(struct parse *, FILE *);
93 static	void	host		(struct parse *, FILE *);
94 static	void	keyid		(struct parse *, FILE *);
95 static	void	keytype		(struct parse *, FILE *);
96 static	void	passwd		(struct parse *, FILE *);
97 static	void	hostnames	(struct parse *, FILE *);
98 static	void	setdebug	(struct parse *, FILE *);
99 static	void	quit		(struct parse *, FILE *);
100 static	void	version		(struct parse *, FILE *);
101 static	void	warning		(const char *, ...)
102     __attribute__((__format__(__printf__, 1, 2)));
103 static	void	error		(const char *, ...)
104     __attribute__((__format__(__printf__, 1, 2)));
105 static	u_long	getkeyid	(const char *);
106 
107 
108 
109 /*
110  * Built-in commands we understand
111  */
112 static	struct xcmd builtins[] = {
113 	{ "?",		help,		{  OPT|NTP_STR, NO, NO, NO },
114 	  { "command", "", "", "" },
115 	  "tell the use and syntax of commands" },
116 	{ "help",	help,		{  OPT|NTP_STR, NO, NO, NO },
117 	  { "command", "", "", "" },
118 	  "tell the use and syntax of commands" },
119 	{ "timeout",	timeout,	{ OPT|NTP_UINT, NO, NO, NO },
120 	  { "msec", "", "", "" },
121 	  "set the primary receive time out" },
122 	{ "delay",	my_delay,	{ OPT|NTP_INT, NO, NO, NO },
123 	  { "msec", "", "", "" },
124 	  "set the delay added to encryption time stamps" },
125 	{ "host",	host,		{ OPT|NTP_STR, OPT|NTP_STR, NO, NO },
126 	  { "-4|-6", "hostname", "", "" },
127 	  "specify the host whose NTP server we talk to" },
128 	{ "passwd",	passwd,		{ OPT|NTP_STR, NO, NO, NO },
129 	  { "", "", "", "" },
130 	  "specify a password to use for authenticated requests"},
131 	{ "hostnames",	hostnames,	{ OPT|NTP_STR, NO, NO, NO },
132 	  { "yes|no", "", "", "" },
133 	  "specify whether hostnames or net numbers are printed"},
134 	{ "debug",	setdebug,	{ OPT|NTP_STR, NO, NO, NO },
135 	  { "no|more|less", "", "", "" },
136 	  "set/change debugging level" },
137 	{ "quit",	quit,		{ NO, NO, NO, NO },
138 	  { "", "", "", "" },
139 	  "exit ntpdc" },
140 	{ "exit",	quit,		{ NO, NO, NO, NO },
141 	  { "", "", "", "" },
142 	  "exit ntpdc" },
143 	{ "keyid",	keyid,		{ OPT|NTP_UINT, NO, NO, NO },
144 	  { "key#", "", "", "" },
145 	  "set/show keyid to use for authenticated requests" },
146 	{ "keytype",	keytype,	{ OPT|NTP_STR, NO, NO, NO },
147 	  { "(md5|des)", "", "", "" },
148 	  "set/show key authentication type for authenticated requests (des|md5)" },
149 	{ "version",	version,	{ NO, NO, NO, NO },
150 	  { "", "", "", "" },
151 	  "print version number" },
152 	{ 0,		0,		{ NO, NO, NO, NO },
153 	  { "", "", "", "" }, "" }
154 };
155 
156 
157 /*
158  * Default values we use.
159  */
160 #define	DEFHOST		"localhost"	/* default host name */
161 #define	DEFTIMEOUT	(5)		/* 5 second time out */
162 #define	DEFSTIMEOUT	(2)		/* 2 second time out after first */
163 #define	DEFDELAY	0x51EB852	/* 20 milliseconds, l_fp fraction */
164 #define	LENHOSTNAME	256		/* host name is 256 characters long */
165 #define	MAXCMDS		100		/* maximum commands on cmd line */
166 #define	MAXHOSTS	200		/* maximum hosts on cmd line */
167 #define	MAXLINE		512		/* maximum line length */
168 #define	MAXTOKENS	(1+1+MAXARGS+MOREARGS+2)	/* maximum number of usable tokens */
169 #define	SCREENWIDTH  	78		/* nominal screen width in columns */
170 
171 /*
172  * Some variables used and manipulated locally
173  */
174 static	struct sock_timeval tvout = { DEFTIMEOUT, 0 };	/* time out for reads */
175 static	struct sock_timeval tvsout = { DEFSTIMEOUT, 0 };/* secondary time out */
176 static	l_fp delay_time;				/* delay time */
177 static	char currenthost[LENHOSTNAME];			/* current host name */
178 int showhostnames = 1;					/* show host names by default */
179 
180 static	int ai_fam_templ;				/* address family */
181 static	int ai_fam_default;				/* default address family */
182 static	SOCKET sockfd;					/* fd socket is opened on */
183 static	int havehost = 0;				/* set to 1 when host open */
184 int s_port = 0;
185 
186 /*
187  * Holds data returned from queries.  We allocate INITDATASIZE
188  * octets to begin with, increasing this as we need to.
189  */
190 #define	INITDATASIZE	(sizeof(struct resp_pkt) * 16)
191 #define	INCDATASIZE	(sizeof(struct resp_pkt) * 8)
192 
193 static	char *pktdata;
194 static	int pktdatasize;
195 
196 /*
197  * These are used to help the magic with old and new versions of ntpd.
198  */
199 int impl_ver = IMPL_XNTPD;
200 static int req_pkt_size = REQ_LEN_NOMAC;
201 
202 /*
203  * For commands typed on the command line (with the -c option)
204  */
205 static	int numcmds = 0;
206 static	const char *ccmds[MAXCMDS];
207 #define	ADDCMD(cp)	if (numcmds < MAXCMDS) ccmds[numcmds++] = (cp)
208 
209 /*
210  * When multiple hosts are specified.
211  */
212 static	int numhosts = 0;
213 static	const char *chosts[MAXHOSTS];
214 #define	ADDHOST(cp)	if (numhosts < MAXHOSTS) chosts[numhosts++] = (cp)
215 
216 /*
217  * Error codes for internal use
218  */
219 #define	ERR_INCOMPLETE		16
220 #define	ERR_TIMEOUT		17
221 
222 /*
223  * Macro definitions we use
224  */
225 #define	ISSPACE(c)	((c) == ' ' || (c) == '\t')
226 #define	ISEOL(c)	((c) == '\n' || (c) == '\r' || (c) == '\0')
227 #define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)
228 
229 /*
230  * Jump buffer for longjumping back to the command level
231  */
232 static	jmp_buf interrupt_buf;
233 static  volatile int jump = 0;
234 
235 /*
236  * Pointer to current output unit
237  */
238 static	FILE *current_output;
239 
240 /*
241  * Command table imported from ntpdc_ops.c
242  */
243 extern struct xcmd opcmds[];
244 
245 char *progname;
246 
247 #ifdef NO_MAIN_ALLOWED
248 CALL(ntpdc,"ntpdc",ntpdcmain);
249 #else
250 int
251 main(
252 	int argc,
253 	char *argv[]
254 	)
255 {
256 	return ntpdcmain(argc, argv);
257 }
258 #endif
259 
260 #ifdef SYS_VXWORKS
261 void clear_globals(void)
262 {
263     showhostnames = 0;              /* show host names by default */
264     havehost = 0;                   /* set to 1 when host open */
265     numcmds = 0;
266     numhosts = 0;
267 }
268 #endif
269 
270 /*
271  * main - parse arguments and handle options
272  */
273 int
274 ntpdcmain(
275 	int argc,
276 	char *argv[]
277 	)
278 {
279 
280 	delay_time.l_ui = 0;
281 	delay_time.l_uf = DEFDELAY;
282 
283 #ifdef SYS_VXWORKS
284 	clear_globals();
285 	taskPrioritySet(taskIdSelf(), 100 );
286 #endif
287 
288 	init_lib();	/* sets up ipv4_works, ipv6_works */
289 	ssl_applink();
290 	init_auth();
291 
292 	/* Check to see if we have IPv6. Otherwise default to IPv4 */
293 	if (!ipv6_works)
294 		ai_fam_default = AF_INET;
295 
296 	progname = argv[0];
297 
298 	{
299 		int optct = ntpOptionProcess(&ntpdcOptions, argc, argv);
300 		argc -= optct;
301 		argv += optct;
302 	}
303 
304 	if (HAVE_OPT(IPV4))
305 		ai_fam_templ = AF_INET;
306 	else if (HAVE_OPT(IPV6))
307 		ai_fam_templ = AF_INET6;
308 	else
309 		ai_fam_templ = ai_fam_default;
310 
311 	if (HAVE_OPT(COMMAND)) {
312 		int		cmdct = STACKCT_OPT( COMMAND );
313 		const char**	cmds  = STACKLST_OPT( COMMAND );
314 
315 		while (cmdct-- > 0) {
316 			ADDCMD(*cmds++);
317 		}
318 	}
319 
320 	debug = OPT_VALUE_SET_DEBUG_LEVEL;
321 
322 	if (HAVE_OPT(INTERACTIVE)) {
323 		interactive = 1;
324 	}
325 
326 	if (HAVE_OPT(NUMERIC)) {
327 		showhostnames = 0;
328 	}
329 
330 	if (HAVE_OPT(LISTPEERS)) {
331 		ADDCMD("listpeers");
332 	}
333 
334 	if (HAVE_OPT(PEERS)) {
335 		ADDCMD("peers");
336 	}
337 
338 	if (HAVE_OPT(SHOWPEERS)) {
339 		ADDCMD("dmpeers");
340 	}
341 
342 	if (ntp_optind == argc) {
343 		ADDHOST(DEFHOST);
344 	} else {
345 		for (; ntp_optind < argc; ntp_optind++)
346 		    ADDHOST(argv[ntp_optind]);
347 	}
348 
349 	if (numcmds == 0 && interactive == 0
350 	    && isatty(fileno(stdin)) && isatty(fileno(stderr))) {
351 		interactive = 1;
352 	}
353 
354 #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */
355 	if (interactive)
356 	    (void) signal_no_reset(SIGINT, abortcmd);
357 #endif /* SYS_WINNT */
358 
359 	/*
360 	 * Initialize the packet data buffer
361 	 */
362 	pktdatasize = INITDATASIZE;
363 	pktdata = emalloc(INITDATASIZE);
364 
365 	if (numcmds == 0) {
366 		(void) openhost(chosts[0]);
367 		getcmds();
368 	} else {
369 		int ihost;
370 		int icmd;
371 
372 		for (ihost = 0; ihost < numhosts; ihost++) {
373 			if (openhost(chosts[ihost]))
374 			    for (icmd = 0; icmd < numcmds; icmd++) {
375 				    if (numhosts > 1)
376 					printf ("--- %s ---\n",chosts[ihost]);
377 				    docmd(ccmds[icmd]);
378 			    }
379 		}
380 	}
381 #ifdef SYS_WINNT
382 	WSACleanup();
383 #endif
384 	return(0);
385 } /* main end */
386 
387 
388 /*
389  * openhost - open a socket to a host
390  */
391 static int
392 openhost(
393 	const char *hname
394 	)
395 {
396 	char temphost[LENHOSTNAME];
397 	int a_info, i;
398 	struct addrinfo hints, *ai = NULL;
399 	sockaddr_u addr;
400 	size_t octets;
401 	register const char *cp;
402 	char name[LENHOSTNAME];
403 	char service[5];
404 
405 	/*
406 	 * We need to get by the [] if they were entered
407 	 */
408 
409 	cp = hname;
410 
411 	if (*cp == '[') {
412 		cp++;
413 		for (i = 0; *cp && *cp != ']'; cp++, i++)
414 			name[i] = *cp;
415 		if (*cp == ']') {
416 			name[i] = '\0';
417 			hname = name;
418 		} else {
419 			return 0;
420 		}
421 	}
422 
423 	/*
424 	 * First try to resolve it as an ip address and if that fails,
425 	 * do a fullblown (dns) lookup. That way we only use the dns
426 	 * when it is needed and work around some implementations that
427 	 * will return an "IPv4-mapped IPv6 address" address if you
428 	 * give it an IPv4 address to lookup.
429 	 */
430 	strlcpy(service, "ntp", sizeof(service));
431 	ZERO(hints);
432 	hints.ai_family = ai_fam_templ;
433 	hints.ai_protocol = IPPROTO_UDP;
434 	hints.ai_socktype = SOCK_DGRAM;
435 	hints.ai_flags = Z_AI_NUMERICHOST;
436 
437 	a_info = getaddrinfo(hname, service, &hints, &ai);
438 	if (a_info == EAI_NONAME
439 #ifdef EAI_NODATA
440 	    || a_info == EAI_NODATA
441 #endif
442 	   ) {
443 		hints.ai_flags = AI_CANONNAME;
444 #ifdef AI_ADDRCONFIG
445 		hints.ai_flags |= AI_ADDRCONFIG;
446 #endif
447 		a_info = getaddrinfo(hname, service, &hints, &ai);
448 	}
449 	/* Some older implementations don't like AI_ADDRCONFIG. */
450 	if (a_info == EAI_BADFLAGS) {
451 		hints.ai_flags = AI_CANONNAME;
452 		a_info = getaddrinfo(hname, service, &hints, &ai);
453 	}
454 	if (a_info != 0) {
455 		fprintf(stderr, "%s\n", gai_strerror(a_info));
456 		if (ai != NULL)
457 			freeaddrinfo(ai);
458 		return 0;
459 	}
460 
461 	/*
462 	 * getaddrinfo() has returned without error so ai should not
463 	 * be NULL.
464 	 */
465 	INSIST(ai != NULL);
466 	ZERO(addr);
467 	octets = min(sizeof(addr), ai->ai_addrlen);
468 	memcpy(&addr, ai->ai_addr, octets);
469 
470 	if (ai->ai_canonname == NULL)
471 		strlcpy(temphost, stoa(&addr), sizeof(temphost));
472 	else
473 		strlcpy(temphost, ai->ai_canonname, sizeof(temphost));
474 
475 	if (debug > 2)
476 		printf("Opening host %s\n", temphost);
477 
478 	if (havehost == 1) {
479 		if (debug > 2)
480 			printf("Closing old host %s\n", currenthost);
481 		closesocket(sockfd);
482 		havehost = 0;
483 	}
484 	strlcpy(currenthost, temphost, sizeof(currenthost));
485 
486 	/* port maps to the same in both families */
487 	s_port = NSRCPORT(&addr);;
488 #ifdef SYS_VXWORKS
489 	((struct sockaddr_in6 *)&hostaddr)->sin6_port = htons(SERVER_PORT_NUM);
490 	if (ai->ai_family == AF_INET)
491 		*(struct sockaddr_in *)&hostaddr=
492 			*((struct sockaddr_in *)ai->ai_addr);
493 	else
494 		*(struct sockaddr_in6 *)&hostaddr=
495 			*((struct sockaddr_in6 *)ai->ai_addr);
496 #endif /* SYS_VXWORKS */
497 
498 #ifdef SYS_WINNT
499 	{
500 		int optionValue = SO_SYNCHRONOUS_NONALERT;
501 		int err;
502 
503 		err = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&optionValue, sizeof(optionValue));
504 		if (err != NO_ERROR) {
505 			(void) fprintf(stderr, "cannot open nonoverlapped sockets\n");
506 			exit(1);
507 		}
508 	}
509 #endif /* SYS_WINNT */
510 
511 	sockfd = socket(ai->ai_family, SOCK_DGRAM, 0);
512 	if (sockfd == INVALID_SOCKET) {
513 		error("socket");
514 		exit(-1);
515 	}
516 
517 #ifdef NEED_RCVBUF_SLOP
518 # ifdef SO_RCVBUF
519 	{
520 		int rbufsize = INITDATASIZE + 2048; /* 2K for slop */
521 
522 		if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF,
523 			       &rbufsize, sizeof(int)) == -1)
524 		    error("setsockopt");
525 	}
526 # endif
527 #endif
528 
529 #ifdef SYS_VXWORKS
530 	if (connect(sockfd, (struct sockaddr *)&hostaddr,
531 		    sizeof(hostaddr)) == -1) {
532 #else
533 	if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) == -1) {
534 #endif /* SYS_VXWORKS */
535 		error("connect");
536 		exit(-1);
537 	}
538 
539 	freeaddrinfo(ai);
540 	havehost = 1;
541 	req_pkt_size = REQ_LEN_NOMAC;
542 	impl_ver = IMPL_XNTPD;
543 	return 1;
544 }
545 
546 
547 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
548 /*
549  * sendpkt - send a packet to the remote host
550  */
551 static int
552 sendpkt(
553 	void *	xdata,
554 	size_t	xdatalen
555 	)
556 {
557 	if (send(sockfd, xdata, xdatalen, 0) == -1) {
558 		warning("write to %s failed", currenthost);
559 		return -1;
560 	}
561 
562 	return 0;
563 }
564 
565 
566 /*
567  * growpktdata - grow the packet data area
568  */
569 static void
570 growpktdata(void)
571 {
572 	size_t priorsz;
573 
574 	priorsz = (size_t)pktdatasize;
575 	pktdatasize += INCDATASIZE;
576 	pktdata = erealloc_zero(pktdata, (size_t)pktdatasize, priorsz);
577 }
578 
579 
580 /*
581  * getresponse - get a (series of) response packet(s) and return the data
582  */
583 static int
584 getresponse(
585 	int implcode,
586 	int reqcode,
587 	int *ritems,
588 	int *rsize,
589 	char **rdata,
590 	int esize
591 	)
592 {
593 	struct resp_pkt rpkt;
594 	struct sock_timeval tvo;
595 	int items;
596 	int i;
597 	int size;
598 	int datasize;
599 	char *datap;
600 	char *tmp_data;
601 	char haveseq[MAXSEQ+1];
602 	int firstpkt;
603 	int lastseq;
604 	int numrecv;
605 	int seq;
606 	fd_set fds;
607 	ssize_t n;
608 	int pad;
609 
610 	/*
611 	 * This is pretty tricky.  We may get between 1 and many packets
612 	 * back in response to the request.  We peel the data out of
613 	 * each packet and collect it in one long block.  When the last
614 	 * packet in the sequence is received we'll know how many we
615 	 * should have had.  Note we use one long time out, should reconsider.
616 	 */
617 	*ritems = 0;
618 	*rsize = 0;
619 	firstpkt = 1;
620 	numrecv = 0;
621 	*rdata = datap = pktdata;
622 	lastseq = 999;	/* too big to be a sequence number */
623 	ZERO(haveseq);
624 	FD_ZERO(&fds);
625 
626     again:
627 	if (firstpkt)
628 		tvo = tvout;
629 	else
630 		tvo = tvsout;
631 
632 	FD_SET(sockfd, &fds);
633 	n = select(sockfd+1, &fds, (fd_set *)0, (fd_set *)0, &tvo);
634 
635 	if (n == -1) {
636 		warning("select fails");
637 		return -1;
638 	}
639 	if (n == 0) {
640 		/*
641 		 * Timed out.  Return what we have
642 		 */
643 		if (firstpkt) {
644 			(void) fprintf(stderr,
645 				       "%s: timed out, nothing received\n", currenthost);
646 			return ERR_TIMEOUT;
647 		} else {
648 			(void) fprintf(stderr,
649 				       "%s: timed out with incomplete data\n",
650 				       currenthost);
651 			if (debug) {
652 				printf("Received sequence numbers");
653 				for (n = 0; n <= MAXSEQ; n++)
654 				    if (haveseq[n])
655 					printf(" %zd,", n);
656 				if (lastseq != 999)
657 				    printf(" last frame received\n");
658 				else
659 				    printf(" last frame not received\n");
660 			}
661 			return ERR_INCOMPLETE;
662 		}
663 	}
664 
665 	n = recv(sockfd, (char *)&rpkt, sizeof(rpkt), 0);
666 	if (n == -1) {
667 		warning("read");
668 		return -1;
669 	}
670 
671 
672 	/*
673 	 * Check for format errors.  Bug proofing.
674 	 */
675 	if (n < (ssize_t)RESP_HEADER_SIZE) {
676 		if (debug)
677 			printf("Short (%zd byte) packet received\n", n);
678 		goto again;
679 	}
680 	if (INFO_VERSION(rpkt.rm_vn_mode) > NTP_VERSION ||
681 	    INFO_VERSION(rpkt.rm_vn_mode) < NTP_OLDVERSION) {
682 		if (debug)
683 			printf("Packet received with version %d\n",
684 			       INFO_VERSION(rpkt.rm_vn_mode));
685 		goto again;
686 	}
687 	if (INFO_MODE(rpkt.rm_vn_mode) != MODE_PRIVATE) {
688 		if (debug)
689 			printf("Packet received with mode %d\n",
690 			       INFO_MODE(rpkt.rm_vn_mode));
691 		goto again;
692 	}
693 	if (INFO_IS_AUTH(rpkt.auth_seq)) {
694 		if (debug)
695 			printf("Encrypted packet received\n");
696 		goto again;
697 	}
698 	if (!ISRESPONSE(rpkt.rm_vn_mode)) {
699 		if (debug)
700 			printf("Received request packet, wanted response\n");
701 		goto again;
702 	}
703 	if (INFO_MBZ(rpkt.mbz_itemsize) != 0) {
704 		if (debug)
705 			printf("Received packet with nonzero MBZ field!\n");
706 		goto again;
707 	}
708 
709 	/*
710 	 * Check implementation/request.  Could be old data getting to us.
711 	 */
712 	if (rpkt.implementation != implcode || rpkt.request != reqcode) {
713 		if (debug)
714 			printf(
715 			    "Received implementation/request of %d/%d, wanted %d/%d",
716 			    rpkt.implementation, rpkt.request,
717 			    implcode, reqcode);
718 		goto again;
719 	}
720 
721 	/*
722 	 * Check the error code.  If non-zero, return it.
723 	 */
724 	if (INFO_ERR(rpkt.err_nitems) != INFO_OKAY) {
725 		if (debug && ISMORE(rpkt.rm_vn_mode)) {
726 			printf("Error code %d received on not-final packet\n",
727 			       INFO_ERR(rpkt.err_nitems));
728 		}
729 		return (int)INFO_ERR(rpkt.err_nitems);
730 	}
731 
732 	/*
733 	 * Collect items and size.  Make sure they make sense.
734 	 */
735 	items = INFO_NITEMS(rpkt.err_nitems);
736 	size = INFO_ITEMSIZE(rpkt.mbz_itemsize);
737 	if (esize > size)
738 		pad = esize - size;
739 	else
740 		pad = 0;
741 	datasize = items * size;
742 	if ((size_t)datasize > (n-RESP_HEADER_SIZE)) {
743 		if (debug)
744 		    printf(
745 			    "Received items %d, size %d (total %d), data in packet is %zu\n",
746 			    items, size, datasize, n-RESP_HEADER_SIZE);
747 		goto again;
748 	}
749 
750 	/*
751 	 * If this isn't our first packet, make sure the size matches
752 	 * the other ones.
753 	 */
754 	if (!firstpkt && size != *rsize) {
755 		if (debug)
756 		    printf("Received itemsize %d, previous %d\n",
757 			   size, *rsize);
758 		goto again;
759 	}
760 	/*
761 	 * If we've received this before, +toss it
762 	 */
763 	seq = INFO_SEQ(rpkt.auth_seq);
764 	if (haveseq[seq]) {
765 		if (debug)
766 		    printf("Received duplicate sequence number %d\n", seq);
767 		goto again;
768 	}
769 	haveseq[seq] = 1;
770 
771 	/*
772 	 * If this is the last in the sequence, record that.
773 	 */
774 	if (!ISMORE(rpkt.rm_vn_mode)) {
775 		if (lastseq != 999) {
776 			printf("Received second end sequence packet\n");
777 			goto again;
778 		}
779 		lastseq = seq;
780 	}
781 
782 	/*
783 	 * So far, so good.  Copy this data into the output array.
784 	 */
785 	if ((datap + datasize + (pad * items)) > (pktdata + pktdatasize)) {
786 		int offset = datap - pktdata;
787 		growpktdata();
788 		*rdata = pktdata; /* might have been realloced ! */
789 		datap = pktdata + offset;
790 	}
791 	/*
792 	 * We now move the pointer along according to size and number of
793 	 * items.  This is so we can play nice with older implementations
794 	 */
795 
796 	tmp_data = rpkt.u.data;
797 	for (i = 0; i < items; i++) {
798 		memcpy(datap, tmp_data, (unsigned)size);
799 		tmp_data += size;
800 		zero_mem(datap + size, pad);
801 		datap += size + pad;
802 	}
803 
804 	if (firstpkt) {
805 		firstpkt = 0;
806 		*rsize = size + pad;
807 	}
808 	*ritems += items;
809 
810 	/*
811 	 * Finally, check the count of received packets.  If we've got them
812 	 * all, return
813 	 */
814 	++numrecv;
815 	if (numrecv <= lastseq)
816 		goto again;
817 	return INFO_OKAY;
818 }
819 
820 
821 /*
822  * sendrequest - format and send a request packet
823  *
824  * Historically, ntpdc has used a fixed-size request packet regardless
825  * of the actual payload size.  When authenticating, the timestamp, key
826  * ID, and digest have been placed just before the end of the packet.
827  * With the introduction in late 2009 of support for authenticated
828  * ntpdc requests using larger 20-octet digests (vs. 16 for MD5), we
829  * come up four bytes short.
830  *
831  * To maintain interop while allowing for larger digests, the behavior
832  * is unchanged when using 16-octet digests.  For larger digests, the
833  * timestamp, key ID, and digest are placed immediately following the
834  * request payload, with the overall packet size variable.  ntpd can
835  * distinguish 16-octet digests by the overall request size being
836  * REQ_LEN_NOMAC + 4 + 16 with the auth bit enabled.  When using a
837  * longer digest, that request size should be avoided.
838  *
839  * With the form used with 20-octet and larger digests, the timestamp,
840  * key ID, and digest are located by ntpd relative to the start of the
841  * packet, and the size of the digest is then implied by the packet
842  * size.
843  */
844 static int
845 sendrequest(
846 	int implcode,
847 	int reqcode,
848 	int auth,
849 	u_int qitems,
850 	size_t qsize,
851 	char *qdata
852 	)
853 {
854 	struct req_pkt qpkt;
855 	size_t	datasize;
856 	size_t	reqsize;
857 	u_long	key_id;
858 	l_fp	ts;
859 	l_fp *	ptstamp;
860 	int	maclen;
861 	char *	pass;
862 
863 	ZERO(qpkt);
864 	qpkt.rm_vn_mode = RM_VN_MODE(0, 0, 0);
865 	qpkt.implementation = (u_char)implcode;
866 	qpkt.request = (u_char)reqcode;
867 
868 	datasize = qitems * qsize;
869 	if (datasize && qdata != NULL) {
870 		memcpy(qpkt.u.data, qdata, datasize);
871 		qpkt.err_nitems = ERR_NITEMS(0, qitems);
872 		qpkt.mbz_itemsize = MBZ_ITEMSIZE(qsize);
873 	} else {
874 		qpkt.err_nitems = ERR_NITEMS(0, 0);
875 		qpkt.mbz_itemsize = MBZ_ITEMSIZE(qsize);  /* allow for optional first item */
876 	}
877 
878 	if (!auth || (keyid_entered && info_auth_keyid == 0)) {
879 		qpkt.auth_seq = AUTH_SEQ(0, 0);
880 		return sendpkt(&qpkt, req_pkt_size);
881 	}
882 
883 	if (info_auth_keyid == 0) {
884 		key_id = getkeyid("Keyid: ");
885 		if (!key_id) {
886 			fprintf(stderr, "Invalid key identifier\n");
887 			return 1;
888 		}
889 		info_auth_keyid = key_id;
890 	}
891 	if (!authistrusted(info_auth_keyid)) {
892 		pass = getpass_keytype(info_auth_keytype);
893 		if ('\0' == pass[0]) {
894 			fprintf(stderr, "Invalid password\n");
895 			return 1;
896 		}
897 		authusekey(info_auth_keyid, info_auth_keytype,
898 			   (u_char *)pass);
899 		authtrust(info_auth_keyid, 1);
900 	}
901 	qpkt.auth_seq = AUTH_SEQ(1, 0);
902 	if (info_auth_hashlen > 16) {
903 		/*
904 		 * Only ntpd which expects REQ_LEN_NOMAC plus maclen
905 		 * octets in an authenticated request using a 16 octet
906 		 * digest (that is, a newer ntpd) will handle digests
907 		 * larger than 16 octets, so for longer digests, do
908 		 * not attempt to shorten the requests for downlevel
909 		 * ntpd compatibility.
910 		 */
911 		if (REQ_LEN_NOMAC != req_pkt_size)
912 			return 1;
913 		reqsize = REQ_LEN_HDR + datasize + sizeof(*ptstamp);
914 		/* align to 32 bits */
915 		reqsize = (reqsize + 3) & ~3;
916 	} else
917 		reqsize = req_pkt_size;
918 	ptstamp = (void *)((char *)&qpkt + reqsize);
919 	ptstamp--;
920 	get_systime(&ts);
921 	L_ADD(&ts, &delay_time);
922 	HTONL_FP(&ts, ptstamp);
923 	maclen = authencrypt(info_auth_keyid, (void *)&qpkt, reqsize);
924 	if (!maclen) {
925 		fprintf(stderr, "Key not found\n");
926 		return 1;
927 	} else if (maclen != (int)(info_auth_hashlen + sizeof(keyid_t))) {
928 		fprintf(stderr,
929 			"%d octet MAC, %zu expected with %zu octet digest\n",
930 			maclen, (info_auth_hashlen + sizeof(keyid_t)),
931 			info_auth_hashlen);
932 		return 1;
933 	}
934 	return sendpkt(&qpkt, reqsize + maclen);
935 }
936 
937 
938 /*
939  * doquery - send a request and process the response
940  */
941 int
942 doquery(
943 	int implcode,
944 	int reqcode,
945 	int auth,
946 	int qitems,
947 	int qsize,
948 	char *qdata,
949 	int *ritems,
950 	int *rsize,
951 	char **rdata,
952  	int quiet_mask,
953 	int esize
954 	)
955 {
956 	int res;
957 	char junk[512];
958 	fd_set fds;
959 	struct sock_timeval tvzero;
960 
961 	/*
962 	 * Check to make sure host is open
963 	 */
964 	if (!havehost) {
965 		(void) fprintf(stderr, "***No host open, use `host' command\n");
966 		return -1;
967 	}
968 
969 	/*
970 	 * Poll the socket and clear out any pending data
971 	 */
972 again:
973 	do {
974 		tvzero.tv_sec = tvzero.tv_usec = 0;
975 		FD_ZERO(&fds);
976 		FD_SET(sockfd, &fds);
977 		res = select(sockfd+1, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
978 
979 		if (res == -1) {
980 			warning("polling select");
981 			return -1;
982 		} else if (res > 0)
983 
984 		    (void) recv(sockfd, junk, sizeof junk, 0);
985 	} while (res > 0);
986 
987 
988 	/*
989 	 * send a request
990 	 */
991 	res = sendrequest(implcode, reqcode, auth, qitems, qsize, qdata);
992 	if (res != 0)
993 		return res;
994 
995 	/*
996 	 * Get the response.  If we got a standard error, print a message
997 	 */
998 	res = getresponse(implcode, reqcode, ritems, rsize, rdata, esize);
999 
1000 	/*
1001 	 * Try to be compatible with older implementations of ntpd.
1002 	 */
1003 	if (res == INFO_ERR_FMT && req_pkt_size != 48) {
1004 		int oldsize;
1005 
1006 		oldsize = req_pkt_size;
1007 
1008 		switch(req_pkt_size) {
1009 		case REQ_LEN_NOMAC:
1010 			req_pkt_size = 160;
1011 			break;
1012 		case 160:
1013 			req_pkt_size = 48;
1014 			break;
1015 		}
1016 		if (impl_ver == IMPL_XNTPD) {
1017 			fprintf(stderr,
1018 			    "***Warning changing to older implementation\n");
1019 			return INFO_ERR_IMPL;
1020 		}
1021 
1022 		fprintf(stderr,
1023 		    "***Warning changing the request packet size from %d to %d\n",
1024 		    oldsize, req_pkt_size);
1025 		goto again;
1026 	}
1027 
1028  	/* log error message if not told to be quiet */
1029  	if ((res > 0) && (((1 << res) & quiet_mask) == 0)) {
1030 		switch(res) {
1031 		case INFO_ERR_IMPL:
1032 			/* Give us a chance to try the older implementation. */
1033 			if (implcode == IMPL_XNTPD)
1034 				break;
1035 			(void) fprintf(stderr,
1036 				       "***Server implementation incompatible with our own\n");
1037 			break;
1038 		case INFO_ERR_REQ:
1039 			(void) fprintf(stderr,
1040 				       "***Server doesn't implement this request\n");
1041 			break;
1042 		case INFO_ERR_FMT:
1043 			(void) fprintf(stderr,
1044 				       "***Server reports a format error in the received packet (shouldn't happen)\n");
1045 			break;
1046 		case INFO_ERR_NODATA:
1047 			(void) fprintf(stderr,
1048 				       "***Server reports data not found\n");
1049 			break;
1050 		case INFO_ERR_AUTH:
1051 			(void) fprintf(stderr, "***Permission denied\n");
1052 			break;
1053 		case ERR_TIMEOUT:
1054 			(void) fprintf(stderr, "***Request timed out\n");
1055 			break;
1056 		case ERR_INCOMPLETE:
1057 			(void) fprintf(stderr,
1058 				       "***Response from server was incomplete\n");
1059 			break;
1060 		default:
1061 			(void) fprintf(stderr,
1062 				       "***Server returns unknown error code %d\n", res);
1063 			break;
1064 		}
1065 	}
1066 	return res;
1067 }
1068 
1069 
1070 /*
1071  * getcmds - read commands from the standard input and execute them
1072  */
1073 static void
1074 getcmds(void)
1075 {
1076 	char *	line;
1077 	int	count;
1078 
1079 	ntp_readline_init(interactive ? prompt : NULL);
1080 
1081 	for (;;) {
1082 		line = ntp_readline(&count);
1083 		if (NULL == line)
1084 			break;
1085 		docmd(line);
1086 		free(line);
1087 	}
1088 
1089 	ntp_readline_uninit();
1090 }
1091 
1092 
1093 #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */
1094 /*
1095  * abortcmd - catch interrupts and abort the current command
1096  */
1097 static RETSIGTYPE
1098 abortcmd(
1099 	int sig
1100 	)
1101 {
1102 
1103 	if (current_output == stdout)
1104 	    (void) fflush(stdout);
1105 	putc('\n', stderr);
1106 	(void) fflush(stderr);
1107 	if (jump) longjmp(interrupt_buf, 1);
1108 }
1109 #endif /* SYS_WINNT */
1110 
1111 /*
1112  * docmd - decode the command line and execute a command
1113  */
1114 static void
1115 docmd(
1116 	const char *cmdline
1117 	)
1118 {
1119 	char *tokens[1+MAXARGS+MOREARGS+2];
1120 	struct parse pcmd;
1121 	int ntok;
1122 	int i, ti;
1123 	int rval;
1124 	struct xcmd *xcmd;
1125 
1126 	ai_fam_templ = ai_fam_default;
1127 	/*
1128 	 * Tokenize the command line.  If nothing on it, return.
1129 	 */
1130 	if (strlen(cmdline) >= MAXLINE) {
1131 		fprintf(stderr, "***Command ignored, more than %d characters:\n%s\n",
1132 			MAXLINE - 1, cmdline);
1133 		return;
1134 	}
1135 	tokenize(cmdline, tokens, &ntok);
1136 	if (ntok == 0)
1137 	    return;
1138 
1139 	/*
1140 	 * Find the appropriate command description.
1141 	 */
1142 	i = findcmd(tokens[0], builtins, opcmds, &xcmd);
1143 	if (i == 0) {
1144 		(void) fprintf(stderr, "***Command `%s' unknown\n",
1145 			       tokens[0]);
1146 		return;
1147 	} else if (i >= 2) {
1148 		(void) fprintf(stderr, "***Command `%s' ambiguous\n",
1149 			       tokens[0]);
1150 		return;
1151 	}
1152 
1153 	/*
1154 	 * Save the keyword, then walk through the arguments, interpreting
1155 	 * as we go.
1156 	 */
1157 	pcmd.keyword = tokens[0];
1158 	pcmd.nargs = 0;
1159 	ti = 1;
1160 	for (i = 0; i < MAXARGS && xcmd->arg[i] != NO;) {
1161 		if ((i+ti) >= ntok) {
1162 			if (!(xcmd->arg[i] & OPT)) {
1163 				printusage(xcmd, stderr);
1164 				return;
1165 			}
1166 			break;
1167 		}
1168 		if ((xcmd->arg[i] & OPT) && (*tokens[i+ti] == '>'))
1169 			break;
1170 		rval = getarg(tokens[i+ti], (int)xcmd->arg[i], &pcmd.argval[i]);
1171 		if (rval == -1) {
1172 			ti++;
1173 			continue;
1174 		}
1175 		if (rval == 0)
1176 			return;
1177 		pcmd.nargs++;
1178 		i++;
1179 	}
1180 
1181 	/* Any extra args are assumed to be "OPT|NTP_STR". */
1182 	for ( ; i < MAXARGS + MOREARGS;) {
1183 	     if ((i+ti) >= ntok)
1184 		  break;
1185 		rval = getarg(tokens[i+ti], (int)(OPT|NTP_STR), &pcmd.argval[i]);
1186 		if (rval == -1) {
1187 			ti++;
1188 			continue;
1189 		}
1190 		if (rval == 0)
1191 			return;
1192 		pcmd.nargs++;
1193 		i++;
1194 	}
1195 
1196 	i += ti;
1197 	if (i < ntok && *tokens[i] == '>') {
1198 		char *fname;
1199 
1200 		if (*(tokens[i]+1) != '\0')
1201 		    fname = tokens[i]+1;
1202 		else if ((i+1) < ntok)
1203 		    fname = tokens[i+1];
1204 		else {
1205 			(void) fprintf(stderr, "***No file for redirect\n");
1206 			return;
1207 		}
1208 
1209 		current_output = fopen(fname, "w");
1210 		if (current_output == NULL) {
1211 			(void) fprintf(stderr, "***Error opening %s: ", fname);
1212 			perror("");
1213 			return;
1214 		}
1215 	} else {
1216 		current_output = stdout;
1217 	}
1218 
1219 	if (interactive && setjmp(interrupt_buf)) {
1220 		return;
1221 	} else {
1222 		jump = 1;
1223 		(xcmd->handler)(&pcmd, current_output);
1224 		jump = 0;
1225 		if (current_output != stdout)
1226 			(void) fclose(current_output);
1227 		current_output = NULL;
1228 	}
1229 }
1230 
1231 
1232 /*
1233  * tokenize - turn a command line into tokens
1234  */
1235 static void
1236 tokenize(
1237 	const char *line,
1238 	char **tokens,
1239 	int *ntok
1240 	)
1241 {
1242 	register const char *cp;
1243 	register char *sp;
1244 	static char tspace[MAXLINE];
1245 
1246 	sp = tspace;
1247 	cp = line;
1248 	for (*ntok = 0; *ntok < MAXTOKENS; (*ntok)++) {
1249 		tokens[*ntok] = sp;
1250 		while (ISSPACE(*cp))
1251 		    cp++;
1252 		if (ISEOL(*cp))
1253 		    break;
1254 		do {
1255 			*sp++ = *cp++;
1256 		} while (!ISSPACE(*cp) && !ISEOL(*cp));
1257 
1258 		*sp++ = '\0';
1259 	}
1260 }
1261 
1262 
1263 
1264 /*
1265  * findcmd - find a command in a command description table
1266  */
1267 static int
1268 findcmd(
1269 	register char *str,
1270 	struct xcmd *clist1,
1271 	struct xcmd *clist2,
1272 	struct xcmd **cmd
1273 	)
1274 {
1275 	register struct xcmd *cl;
1276 	register int clen;
1277 	int nmatch;
1278 	struct xcmd *nearmatch = NULL;
1279 	struct xcmd *clist;
1280 
1281 	clen = strlen(str);
1282 	nmatch = 0;
1283 	if (clist1 != 0)
1284 	    clist = clist1;
1285 	else if (clist2 != 0)
1286 	    clist = clist2;
1287 	else
1288 	    return 0;
1289 
1290     again:
1291 	for (cl = clist; cl->keyword != 0; cl++) {
1292 		/* do a first character check, for efficiency */
1293 		if (*str != *(cl->keyword))
1294 		    continue;
1295 		if (strncmp(str, cl->keyword, (unsigned)clen) == 0) {
1296 			/*
1297 			 * Could be extact match, could be approximate.
1298 			 * Is exact if the length of the keyword is the
1299 			 * same as the str.
1300 			 */
1301 			if (*((cl->keyword) + clen) == '\0') {
1302 				*cmd = cl;
1303 				return 1;
1304 			}
1305 			nmatch++;
1306 			nearmatch = cl;
1307 		}
1308 	}
1309 
1310 				/*
1311 				 * See if there is more to do.  If so, go again.  Sorry about the
1312 				 * goto, too much looking at BSD sources...
1313 				 */
1314 	if (clist == clist1 && clist2 != 0) {
1315 		clist = clist2;
1316 		goto again;
1317 	}
1318 
1319 				/*
1320 				 * If we got extactly 1 near match, use it, else return number
1321 				 * of matches.
1322 				 */
1323 	if (nmatch == 1) {
1324 		*cmd = nearmatch;
1325 		return 1;
1326 	}
1327 	return nmatch;
1328 }
1329 
1330 
1331 /*
1332  * getarg - interpret an argument token
1333  *
1334  * string is always set.
1335  * type is set to the decoded type.
1336  *
1337  * return:	 0 - failure
1338  *		 1 - success
1339  *		-1 - skip to next token
1340  */
1341 static int
1342 getarg(
1343 	char *str,
1344 	int code,
1345 	arg_v *argp
1346 	)
1347 {
1348 	int isneg;
1349 	char *cp, *np;
1350 	static const char *digits = "0123456789";
1351 
1352 	ZERO(*argp);
1353 	argp->string = str;
1354 	argp->type   = code & ~OPT;
1355 
1356 	switch (argp->type) {
1357 	    case NTP_STR:
1358 		break;
1359 	    case NTP_ADD:
1360 		if (!strcmp("-6", str)) {
1361 			ai_fam_templ = AF_INET6;
1362 			return -1;
1363 		} else if (!strcmp("-4", str)) {
1364 			ai_fam_templ = AF_INET;
1365 			return -1;
1366 		}
1367 		if (!getnetnum(str, &(argp->netnum), (char *)0, 0)) {
1368 			return 0;
1369 		}
1370 		break;
1371 	    case NTP_INT:
1372 	    case NTP_UINT:
1373 		isneg = 0;
1374 		np = str;
1375 		if (*np == '-') {
1376 			np++;
1377 			isneg = 1;
1378 		}
1379 
1380 		argp->uval = 0;
1381 		do {
1382 			cp = strchr(digits, *np);
1383 			if (cp == NULL) {
1384 				(void) fprintf(stderr,
1385 					       "***Illegal integer value %s\n", str);
1386 				return 0;
1387 			}
1388 			argp->uval *= 10;
1389 			argp->uval += (cp - digits);
1390 		} while (*(++np) != '\0');
1391 
1392 		if (isneg) {
1393 			if ((code & ~OPT) == NTP_UINT) {
1394 				(void) fprintf(stderr,
1395 					       "***Value %s should be unsigned\n", str);
1396 				return 0;
1397 			}
1398 			argp->ival = -argp->ival;
1399 		}
1400 		break;
1401 	    case IP_VERSION:
1402 		if (!strcmp("-6", str))
1403 			argp->ival = 6 ;
1404 		else if (!strcmp("-4", str))
1405 			argp->ival = 4 ;
1406 		else {
1407 			(void) fprintf(stderr,
1408 			    "***Version must be either 4 or 6\n");
1409 			return 0;
1410 		}
1411 		break;
1412 	}
1413 
1414 	return 1;
1415 }
1416 
1417 
1418 /*
1419  * getnetnum - given a host name, return its net number
1420  *	       and (optional) full name
1421  */
1422 static int
1423 getnetnum(
1424 	const char *hname,
1425 	sockaddr_u *num,
1426 	char *fullhost,
1427 	int af
1428 	)
1429 {
1430 	struct addrinfo hints, *ai = NULL;
1431 
1432 	ZERO(hints);
1433 	hints.ai_flags = AI_CANONNAME;
1434 #ifdef AI_ADDRCONFIG
1435 	hints.ai_flags |= AI_ADDRCONFIG;
1436 #endif
1437 
1438 	/*
1439 	 * decodenetnum only works with addresses, but handles syntax
1440 	 * that getaddrinfo doesn't:  [2001::1]:1234
1441 	 */
1442 	if (decodenetnum(hname, num)) {
1443 		if (fullhost != NULL)
1444 			getnameinfo(&num->sa, SOCKLEN(num), fullhost,
1445 				    LENHOSTNAME, NULL, 0, 0);
1446 		return 1;
1447 	} else if (getaddrinfo(hname, "ntp", &hints, &ai) == 0) {
1448 		NTP_INSIST(sizeof(*num) >= ai->ai_addrlen);
1449 		memcpy(num, ai->ai_addr, ai->ai_addrlen);
1450 		if (fullhost != NULL) {
1451 			if (ai->ai_canonname != NULL)
1452 				strlcpy(fullhost, ai->ai_canonname,
1453 					LENHOSTNAME);
1454 			else
1455 				getnameinfo(&num->sa, SOCKLEN(num),
1456 					    fullhost, LENHOSTNAME, NULL,
1457 					    0, 0);
1458 		}
1459 		return 1;
1460 	}
1461 	fprintf(stderr, "***Can't find host %s\n", hname);
1462 
1463 	return 0;
1464 }
1465 
1466 
1467 /*
1468  * nntohost - convert network number to host name.  This routine enforces
1469  *	       the showhostnames setting.
1470  */
1471 const char *
1472 nntohost(
1473 	sockaddr_u *netnum
1474 	)
1475 {
1476 	if (!showhostnames || SOCK_UNSPEC(netnum))
1477 		return stoa(netnum);
1478 	else if (ISREFCLOCKADR(netnum))
1479 		return refnumtoa(netnum);
1480 	else
1481 		return socktohost(netnum);
1482 }
1483 
1484 
1485 /*
1486  * Finally, the built in command handlers
1487  */
1488 
1489 /*
1490  * help - tell about commands, or details of a particular command
1491  */
1492 static void
1493 help(
1494 	struct parse *pcmd,
1495 	FILE *fp
1496 	)
1497 {
1498 	struct xcmd *xcp;
1499 	char *cmd;
1500 	const char *list[100];
1501 	size_t word, words;
1502 	size_t row, rows;
1503 	size_t col, cols;
1504 	size_t length;
1505 
1506 	if (pcmd->nargs == 0) {
1507 		words = 0;
1508 		for (xcp = builtins; xcp->keyword != 0; xcp++) {
1509 			if (*(xcp->keyword) != '?')
1510 				list[words++] = xcp->keyword;
1511 		}
1512 		for (xcp = opcmds; xcp->keyword != 0; xcp++)
1513 			list[words++] = xcp->keyword;
1514 
1515 		qsort((void *)list, words, sizeof(list[0]), helpsort);
1516 		col = 0;
1517 		for (word = 0; word < words; word++) {
1518 			length = strlen(list[word]);
1519 			col = max(col, length);
1520 		}
1521 
1522 		cols = SCREENWIDTH / ++col;
1523 		rows = (words + cols - 1) / cols;
1524 
1525 		fprintf(fp, "ntpdc commands:\n");
1526 
1527 		for (row = 0; row < rows; row++) {
1528 			for (word = row; word < words; word += rows)
1529 				fprintf(fp, "%-*.*s", (int)col,
1530 					(int)col - 1, list[word]);
1531 			fprintf(fp, "\n");
1532 		}
1533 	} else {
1534 		cmd = pcmd->argval[0].string;
1535 		words = findcmd(cmd, builtins, opcmds, &xcp);
1536 		if (words == 0) {
1537 			fprintf(stderr,
1538 				"Command `%s' is unknown\n", cmd);
1539 			return;
1540 		} else if (words >= 2) {
1541 			fprintf(stderr,
1542 				"Command `%s' is ambiguous\n", cmd);
1543 			return;
1544 		}
1545 		fprintf(fp, "function: %s\n", xcp->comment);
1546 		printusage(xcp, fp);
1547 	}
1548 }
1549 
1550 
1551 /*
1552  * helpsort - do hostname qsort comparisons
1553  */
1554 static int
1555 helpsort(
1556 	const void *t1,
1557 	const void *t2
1558 	)
1559 {
1560 	const char * const *	name1 = t1;
1561 	const char * const *	name2 = t2;
1562 
1563 	return strcmp(*name1, *name2);
1564 }
1565 
1566 
1567 /*
1568  * printusage - print usage information for a command
1569  */
1570 static void
1571 printusage(
1572 	struct xcmd *xcp,
1573 	FILE *fp
1574 	)
1575 {
1576 	int i, opt46;
1577 
1578 	opt46 = 0;
1579 	(void) fprintf(fp, "usage: %s", xcp->keyword);
1580 	for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) {
1581 		if (opt46 == 0 && (xcp->arg[i] & ~OPT) == NTP_ADD) {
1582 			(void) fprintf(fp, " [ -4|-6 ]");
1583 			opt46 = 1;
1584 		}
1585 		if (xcp->arg[i] & OPT)
1586 		    (void) fprintf(fp, " [ %s ]", xcp->desc[i]);
1587 		else
1588 		    (void) fprintf(fp, " %s", xcp->desc[i]);
1589 	}
1590 	(void) fprintf(fp, "\n");
1591 }
1592 
1593 
1594 /*
1595  * timeout - set time out time
1596  */
1597 static void
1598 timeout(
1599 	struct parse *pcmd,
1600 	FILE *fp
1601 	)
1602 {
1603 	int val;
1604 
1605 	if (pcmd->nargs == 0) {
1606 		val = tvout.tv_sec * 1000 + tvout.tv_usec / 1000;
1607 		(void) fprintf(fp, "primary timeout %d ms\n", val);
1608 	} else {
1609 		tvout.tv_sec = pcmd->argval[0].uval / 1000;
1610 		tvout.tv_usec = (pcmd->argval[0].uval - (tvout.tv_sec * 1000))
1611 			* 1000;
1612 	}
1613 }
1614 
1615 
1616 /*
1617  * my_delay - set delay for auth requests
1618  */
1619 static void
1620 my_delay(
1621 	struct parse *pcmd,
1622 	FILE *fp
1623 	)
1624 {
1625 	int isneg;
1626 	u_long val;
1627 
1628 	if (pcmd->nargs == 0) {
1629 		val = delay_time.l_ui * 1000 + delay_time.l_uf / 4294967;
1630 		(void) fprintf(fp, "delay %lu ms\n", val);
1631 	} else {
1632 		if (pcmd->argval[0].ival < 0) {
1633 			isneg = 1;
1634 			val = (u_long)(-pcmd->argval[0].ival);
1635 		} else {
1636 			isneg = 0;
1637 			val = (u_long)pcmd->argval[0].ival;
1638 		}
1639 
1640 		delay_time.l_ui = val / 1000;
1641 		val %= 1000;
1642 		delay_time.l_uf = val * 4294967;	/* 2**32/1000 */
1643 
1644 		if (isneg)
1645 		    L_NEG(&delay_time);
1646 	}
1647 }
1648 
1649 
1650 /*
1651  * host - set the host we are dealing with.
1652  */
1653 static void
1654 host(
1655 	struct parse *pcmd,
1656 	FILE *fp
1657 	)
1658 {
1659 	int i;
1660 
1661 	if (pcmd->nargs == 0) {
1662 		if (havehost)
1663 		    (void) fprintf(fp, "current host is %s\n", currenthost);
1664 		else
1665 		    (void) fprintf(fp, "no current host\n");
1666 		return;
1667 	}
1668 
1669 	i = 0;
1670 	if (pcmd->nargs == 2) {
1671 		if (!strcmp("-4", pcmd->argval[i].string))
1672 			ai_fam_templ = AF_INET;
1673 		else if (!strcmp("-6", pcmd->argval[i].string))
1674 			ai_fam_templ = AF_INET6;
1675 		else {
1676 			if (havehost)
1677 				(void) fprintf(fp,
1678 				    "current host remains %s\n", currenthost);
1679 			else
1680 				(void) fprintf(fp, "still no current host\n");
1681 			return;
1682 		}
1683 		i = 1;
1684 	}
1685 	if (openhost(pcmd->argval[i].string)) {
1686 		(void) fprintf(fp, "current host set to %s\n", currenthost);
1687 	} else {
1688 		if (havehost)
1689 		    (void) fprintf(fp,
1690 				   "current host remains %s\n", currenthost);
1691 		else
1692 		    (void) fprintf(fp, "still no current host\n");
1693 	}
1694 }
1695 
1696 
1697 /*
1698  * keyid - get a keyid to use for authenticating requests
1699  */
1700 static void
1701 keyid(
1702 	struct parse *pcmd,
1703 	FILE *fp
1704 	)
1705 {
1706 	if (pcmd->nargs == 0) {
1707 		if (info_auth_keyid == 0 && !keyid_entered)
1708 		    (void) fprintf(fp, "no keyid defined\n");
1709 		else if (info_auth_keyid == 0 && keyid_entered)
1710 		    (void) fprintf(fp, "no keyid will be sent\n");
1711 		else
1712 		    (void) fprintf(fp, "keyid is %lu\n", (u_long)info_auth_keyid);
1713 	} else {
1714 		info_auth_keyid = pcmd->argval[0].uval;
1715 		keyid_entered = 1;
1716 	}
1717 }
1718 
1719 
1720 /*
1721  * keytype - get type of key to use for authenticating requests
1722  */
1723 static void
1724 keytype(
1725 	struct parse *pcmd,
1726 	FILE *fp
1727 	)
1728 {
1729 	const char *	digest_name;
1730 	size_t		digest_len;
1731 	int		key_type;
1732 
1733 	if (!pcmd->nargs) {
1734 		fprintf(fp, "keytype is %s with %lu octet digests\n",
1735 			keytype_name(info_auth_keytype),
1736 			(u_long)info_auth_hashlen);
1737 		return;
1738 	}
1739 
1740 	digest_name = pcmd->argval[0].string;
1741 	digest_len = 0;
1742 	key_type = keytype_from_text(digest_name, &digest_len);
1743 
1744 	if (!key_type) {
1745 		fprintf(fp, "keytype must be 'md5'%s\n",
1746 #ifdef OPENSSL
1747 			" or a digest type provided by OpenSSL");
1748 #else
1749 			"");
1750 #endif
1751 		return;
1752 	}
1753 
1754 	info_auth_keytype = key_type;
1755 	info_auth_hashlen = digest_len;
1756 }
1757 
1758 
1759 /*
1760  * passwd - get an authentication key
1761  */
1762 /*ARGSUSED*/
1763 static void
1764 passwd(
1765 	struct parse *pcmd,
1766 	FILE *fp
1767 	)
1768 {
1769 	char *pass;
1770 
1771 	if (info_auth_keyid == 0) {
1772 		info_auth_keyid = getkeyid("Keyid: ");
1773 		if (info_auth_keyid == 0) {
1774 			(void)fprintf(fp, "Keyid must be defined\n");
1775 			return;
1776 		}
1777 	}
1778 	if (pcmd->nargs >= 1)
1779 		pass = pcmd->argval[0].string;
1780 	else {
1781 		pass = getpass_keytype(info_auth_keytype);
1782 		if ('\0' == *pass) {
1783 			fprintf(fp, "Password unchanged\n");
1784 			return;
1785 		}
1786 	}
1787 	authusekey(info_auth_keyid, info_auth_keytype, (u_char *)pass);
1788 	authtrust(info_auth_keyid, 1);
1789 }
1790 
1791 
1792 /*
1793  * hostnames - set the showhostnames flag
1794  */
1795 static void
1796 hostnames(
1797 	struct parse *pcmd,
1798 	FILE *fp
1799 	)
1800 {
1801 	if (pcmd->nargs == 0) {
1802 		if (showhostnames)
1803 		    (void) fprintf(fp, "hostnames being shown\n");
1804 		else
1805 		    (void) fprintf(fp, "hostnames not being shown\n");
1806 	} else {
1807 		if (STREQ(pcmd->argval[0].string, "yes"))
1808 		    showhostnames = 1;
1809 		else if (STREQ(pcmd->argval[0].string, "no"))
1810 		    showhostnames = 0;
1811 		else
1812 		    (void)fprintf(stderr, "What?\n");
1813 	}
1814 }
1815 
1816 
1817 /*
1818  * setdebug - set/change debugging level
1819  */
1820 static void
1821 setdebug(
1822 	struct parse *pcmd,
1823 	FILE *fp
1824 	)
1825 {
1826 	if (pcmd->nargs == 0) {
1827 		(void) fprintf(fp, "debug level is %d\n", debug);
1828 		return;
1829 	} else if (STREQ(pcmd->argval[0].string, "no")) {
1830 		debug = 0;
1831 	} else if (STREQ(pcmd->argval[0].string, "more")) {
1832 		debug++;
1833 	} else if (STREQ(pcmd->argval[0].string, "less")) {
1834 		debug--;
1835 	} else {
1836 		(void) fprintf(fp, "What?\n");
1837 		return;
1838 	}
1839 	(void) fprintf(fp, "debug level set to %d\n", debug);
1840 }
1841 
1842 
1843 /*
1844  * quit - stop this nonsense
1845  */
1846 /*ARGSUSED*/
1847 static void
1848 quit(
1849 	struct parse *pcmd,
1850 	FILE *fp
1851 	)
1852 {
1853 	if (havehost)
1854 	    closesocket(sockfd);
1855 	exit(0);
1856 }
1857 
1858 
1859 /*
1860  * version - print the current version number
1861  */
1862 /*ARGSUSED*/
1863 static void
1864 version(
1865 	struct parse *pcmd,
1866 	FILE *fp
1867 	)
1868 {
1869 
1870 	(void) fprintf(fp, "%s\n", Version);
1871 	return;
1872 }
1873 
1874 
1875 static void __attribute__((__format__(__printf__, 1, 0)))
1876 vwarning(const char *fmt, va_list ap)
1877 {
1878 	int serrno = errno;
1879 	(void) fprintf(stderr, "%s: ", progname);
1880 	vfprintf(stderr, fmt, ap);
1881 	(void) fprintf(stderr, ": %s\n", strerror(serrno));
1882 }
1883 
1884 /*
1885  * warning - print a warning message
1886  */
1887 static void __attribute__((__format__(__printf__, 1, 2)))
1888 warning(
1889 	const char *fmt,
1890 	...
1891 	)
1892 {
1893 	va_list ap;
1894 	va_start(ap, fmt);
1895 	vwarning(fmt, ap);
1896 	va_end(ap);
1897 }
1898 
1899 
1900 /*
1901  * error - print a message and exit
1902  */
1903 static void __attribute__((__format__(__printf__, 1, 2)))
1904 error(
1905 	const char *fmt,
1906 	...
1907 	)
1908 {
1909 	va_list ap;
1910 	va_start(ap, fmt);
1911 	vwarning(fmt, ap);
1912 	va_end(ap);
1913 	exit(1);
1914 }
1915 
1916 /*
1917  * getkeyid - prompt the user for a keyid to use
1918  */
1919 static u_long
1920 getkeyid(
1921 	const char *keyprompt
1922 	)
1923 {
1924 	int c;
1925 	FILE *fi;
1926 	char pbuf[20];
1927 	size_t i;
1928 	size_t ilim;
1929 
1930 #ifndef SYS_WINNT
1931 	if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
1932 #else
1933 	if ((fi = _fdopen(open("CONIN$", _O_TEXT), "r")) == NULL)
1934 #endif /* SYS_WINNT */
1935 		fi = stdin;
1936 	else
1937 		setbuf(fi, (char *)NULL);
1938 	fprintf(stderr, "%s", keyprompt); fflush(stderr);
1939 	for (i = 0, ilim = COUNTOF(pbuf) - 1;
1940 	     i < ilim && (c = getc(fi)) != '\n' && c != EOF;
1941 	     )
1942 		pbuf[i++] = (char)c;
1943 	pbuf[i] = '\0';
1944 	if (fi != stdin)
1945 		fclose(fi);
1946 
1947 	return (u_long) atoi(pbuf);
1948 }
1949