xref: /freebsd/contrib/ntp/ntpdc/ntpdc.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * ntpdc - control and monitor your ntpd daemon
3c0b746e5SOllivier Robert  */
42b15cb3dSCy Schubert #include <config.h>
5c0b746e5SOllivier Robert #include <stdio.h>
62b15cb3dSCy Schubert #include <stddef.h>
7ea906c41SOllivier Robert #include <ctype.h>
8ea906c41SOllivier Robert #include <signal.h>
9ea906c41SOllivier Robert #include <setjmp.h>
102b15cb3dSCy Schubert #ifdef HAVE_UNISTD_H
112b15cb3dSCy Schubert # include <unistd.h>
122b15cb3dSCy Schubert #endif
132b15cb3dSCy Schubert #ifdef HAVE_FCNTL_H
142b15cb3dSCy Schubert # include <fcntl.h>
152b15cb3dSCy Schubert #endif
162b15cb3dSCy Schubert #ifdef SYS_WINNT
172b15cb3dSCy Schubert # include <mswsock.h>
182b15cb3dSCy Schubert #endif
192b15cb3dSCy Schubert #include <isc/net.h>
202b15cb3dSCy Schubert #include <isc/result.h>
21ea906c41SOllivier Robert 
22224ba2bdSOllivier Robert #include "ntpdc.h"
23224ba2bdSOllivier Robert #include "ntp_select.h"
24224ba2bdSOllivier Robert #include "ntp_stdlib.h"
252b15cb3dSCy Schubert #include "ntp_assert.h"
262b15cb3dSCy Schubert #include "ntp_lineedit.h"
272b15cb3dSCy Schubert #ifdef OPENSSL
282b15cb3dSCy Schubert #include "openssl/evp.h"
292b15cb3dSCy Schubert #include "openssl/objects.h"
302b15cb3dSCy Schubert #endif
312b15cb3dSCy Schubert #include <ssl_applink.c>
32224ba2bdSOllivier Robert 
332b15cb3dSCy Schubert #include "ntp_libopts.h"
34ea906c41SOllivier Robert #include "ntpdc-opts.h"
353311ff84SXin LI #include "safecast.h"
36c0b746e5SOllivier Robert 
37c0b746e5SOllivier Robert #ifdef SYS_VXWORKS
38c0b746e5SOllivier Robert 				/* vxWorks needs mode flag -casey*/
39c0b746e5SOllivier Robert # define open(name, flags)   open(name, flags, 0777)
40c0b746e5SOllivier Robert # define SERVER_PORT_NUM     123
41c0b746e5SOllivier Robert #endif
42c0b746e5SOllivier Robert 
43ea906c41SOllivier Robert /* We use COMMAND as an autogen keyword */
44ea906c41SOllivier Robert #ifdef COMMAND
45ea906c41SOllivier Robert # undef COMMAND
46ea906c41SOllivier Robert #endif
47ea906c41SOllivier Robert 
48c0b746e5SOllivier Robert /*
49c0b746e5SOllivier Robert  * Because we now potentially understand a lot of commands (and
50c0b746e5SOllivier Robert  * it requires a lot of commands to talk to ntpd) we will run
51c0b746e5SOllivier Robert  * interactive if connected to a terminal.
52c0b746e5SOllivier Robert  */
53c0b746e5SOllivier Robert static	int	interactive = 0;	/* set to 1 when we should prompt */
54c0b746e5SOllivier Robert static	const char *	prompt = "ntpdc> ";	/* prompt to ask him about */
55c0b746e5SOllivier Robert 
56c0b746e5SOllivier Robert /*
57c0b746e5SOllivier Robert  * Keyid used for authenticated requests.  Obtained on the fly.
58c0b746e5SOllivier Robert  */
59c0b746e5SOllivier Robert static	u_long	info_auth_keyid;
60ea906c41SOllivier Robert static int keyid_entered = 0;
61c0b746e5SOllivier Robert 
622b15cb3dSCy Schubert static	int	info_auth_keytype = NID_md5;	/* MD5 */
632b15cb3dSCy Schubert static	size_t	info_auth_hashlen = 16;		/* MD5 */
64c0b746e5SOllivier Robert u_long	current_time;		/* needed by authkeys; not used */
65c0b746e5SOllivier Robert 
66ea906c41SOllivier Robert /*
67ea906c41SOllivier Robert  * for get_systime()
68ea906c41SOllivier Robert  */
69ea906c41SOllivier Robert s_char	sys_precision;		/* local clock precision (log2 s) */
70ea906c41SOllivier Robert 
712b15cb3dSCy Schubert int		ntpdcmain	(int,	char **);
72c0b746e5SOllivier Robert /*
73c0b746e5SOllivier Robert  * Built in command handler declarations
74c0b746e5SOllivier Robert  */
752b15cb3dSCy Schubert static	int	openhost	(const char *);
762b15cb3dSCy Schubert static	int	sendpkt		(void *, size_t);
772b15cb3dSCy Schubert static	void	growpktdata	(void);
783311ff84SXin LI static	int	getresponse	(int, int, size_t *, size_t *, const char **, size_t);
793311ff84SXin LI static	int	sendrequest	(int, int, int, size_t, size_t, const char *);
802b15cb3dSCy Schubert static	void	getcmds		(void);
812b15cb3dSCy Schubert static	RETSIGTYPE abortcmd	(int);
822b15cb3dSCy Schubert static	void	docmd		(const char *);
832b15cb3dSCy Schubert static	void	tokenize	(const char *, char **, int *);
842b15cb3dSCy Schubert static	int	findcmd		(char *, struct xcmd *, struct xcmd *, struct xcmd **);
852b15cb3dSCy Schubert static	int	getarg		(char *, int, arg_v *);
862b15cb3dSCy Schubert static	int	getnetnum	(const char *, sockaddr_u *, char *, int);
872b15cb3dSCy Schubert static	void	help		(struct parse *, FILE *);
882b15cb3dSCy Schubert static	int	helpsort	(const void *, const void *);
892b15cb3dSCy Schubert static	void	printusage	(struct xcmd *, FILE *);
902b15cb3dSCy Schubert static	void	timeout		(struct parse *, FILE *);
912b15cb3dSCy Schubert static	void	my_delay	(struct parse *, FILE *);
922b15cb3dSCy Schubert static	void	host		(struct parse *, FILE *);
932b15cb3dSCy Schubert static	void	keyid		(struct parse *, FILE *);
942b15cb3dSCy Schubert static	void	keytype		(struct parse *, FILE *);
952b15cb3dSCy Schubert static	void	passwd		(struct parse *, FILE *);
962b15cb3dSCy Schubert static	void	hostnames	(struct parse *, FILE *);
972b15cb3dSCy Schubert static	void	setdebug	(struct parse *, FILE *);
982b15cb3dSCy Schubert static	void	quit		(struct parse *, FILE *);
992b15cb3dSCy Schubert static	void	version		(struct parse *, FILE *);
1002b15cb3dSCy Schubert static	void	warning		(const char *, ...)
1012b15cb3dSCy Schubert     __attribute__((__format__(__printf__, 1, 2)));
1022b15cb3dSCy Schubert static	void	error		(const char *, ...)
1032b15cb3dSCy Schubert     __attribute__((__format__(__printf__, 1, 2)));
1042b15cb3dSCy Schubert static	u_long	getkeyid	(const char *);
105c0b746e5SOllivier Robert 
106c0b746e5SOllivier Robert 
107c0b746e5SOllivier Robert 
108c0b746e5SOllivier Robert /*
109c0b746e5SOllivier Robert  * Built-in commands we understand
110c0b746e5SOllivier Robert  */
111c0b746e5SOllivier Robert static	struct xcmd builtins[] = {
112c0b746e5SOllivier Robert 	{ "?",		help,		{  OPT|NTP_STR, NO, NO, NO },
113c0b746e5SOllivier Robert 	  { "command", "", "", "" },
114c0b746e5SOllivier Robert 	  "tell the use and syntax of commands" },
115c0b746e5SOllivier Robert 	{ "help",	help,		{  OPT|NTP_STR, NO, NO, NO },
116c0b746e5SOllivier Robert 	  { "command", "", "", "" },
117c0b746e5SOllivier Robert 	  "tell the use and syntax of commands" },
118ea906c41SOllivier Robert 	{ "timeout",	timeout,	{ OPT|NTP_UINT, NO, NO, NO },
119c0b746e5SOllivier Robert 	  { "msec", "", "", "" },
120c0b746e5SOllivier Robert 	  "set the primary receive time out" },
121ea906c41SOllivier Robert 	{ "delay",	my_delay,	{ OPT|NTP_INT, NO, NO, NO },
122c0b746e5SOllivier Robert 	  { "msec", "", "", "" },
123c0b746e5SOllivier Robert 	  "set the delay added to encryption time stamps" },
1249c2daa00SOllivier Robert 	{ "host",	host,		{ OPT|NTP_STR, OPT|NTP_STR, NO, NO },
1259c2daa00SOllivier Robert 	  { "-4|-6", "hostname", "", "" },
126c0b746e5SOllivier Robert 	  "specify the host whose NTP server we talk to" },
127c0b746e5SOllivier Robert 	{ "passwd",	passwd,		{ OPT|NTP_STR, NO, NO, NO },
128c0b746e5SOllivier Robert 	  { "", "", "", "" },
129c0b746e5SOllivier Robert 	  "specify a password to use for authenticated requests"},
130c0b746e5SOllivier Robert 	{ "hostnames",	hostnames,	{ OPT|NTP_STR, NO, NO, NO },
131c0b746e5SOllivier Robert 	  { "yes|no", "", "", "" },
132c0b746e5SOllivier Robert 	  "specify whether hostnames or net numbers are printed"},
133c0b746e5SOllivier Robert 	{ "debug",	setdebug,	{ OPT|NTP_STR, NO, NO, NO },
134c0b746e5SOllivier Robert 	  { "no|more|less", "", "", "" },
135c0b746e5SOllivier Robert 	  "set/change debugging level" },
136c0b746e5SOllivier Robert 	{ "quit",	quit,		{ NO, NO, NO, NO },
137c0b746e5SOllivier Robert 	  { "", "", "", "" },
138c0b746e5SOllivier Robert 	  "exit ntpdc" },
139c0b746e5SOllivier Robert 	{ "exit",	quit,		{ NO, NO, NO, NO },
140c0b746e5SOllivier Robert 	  { "", "", "", "" },
141c0b746e5SOllivier Robert 	  "exit ntpdc" },
142ea906c41SOllivier Robert 	{ "keyid",	keyid,		{ OPT|NTP_UINT, NO, NO, NO },
143c0b746e5SOllivier Robert 	  { "key#", "", "", "" },
144c0b746e5SOllivier Robert 	  "set/show keyid to use for authenticated requests" },
145c0b746e5SOllivier Robert 	{ "keytype",	keytype,	{ OPT|NTP_STR, NO, NO, NO },
146c0b746e5SOllivier Robert 	  { "(md5|des)", "", "", "" },
147c0b746e5SOllivier Robert 	  "set/show key authentication type for authenticated requests (des|md5)" },
148c0b746e5SOllivier Robert 	{ "version",	version,	{ NO, NO, NO, NO },
149c0b746e5SOllivier Robert 	  { "", "", "", "" },
150c0b746e5SOllivier Robert 	  "print version number" },
151c0b746e5SOllivier Robert 	{ 0,		0,		{ NO, NO, NO, NO },
152c0b746e5SOllivier Robert 	  { "", "", "", "" }, "" }
153c0b746e5SOllivier Robert };
154c0b746e5SOllivier Robert 
155c0b746e5SOllivier Robert 
156c0b746e5SOllivier Robert /*
157c0b746e5SOllivier Robert  * Default values we use.
158c0b746e5SOllivier Robert  */
1592b15cb3dSCy Schubert #define	DEFHOST		"localhost"	/* default host name */
160c0b746e5SOllivier Robert #define	DEFTIMEOUT	(5)		/* 5 second time out */
161c0b746e5SOllivier Robert #define	DEFSTIMEOUT	(2)		/* 2 second time out after first */
162c0b746e5SOllivier Robert #define	DEFDELAY	0x51EB852	/* 20 milliseconds, l_fp fraction */
163c0b746e5SOllivier Robert #define	LENHOSTNAME	256		/* host name is 256 characters long */
164c0b746e5SOllivier Robert #define	MAXCMDS		100		/* maximum commands on cmd line */
165c0b746e5SOllivier Robert #define	MAXHOSTS	200		/* maximum hosts on cmd line */
166c0b746e5SOllivier Robert #define	MAXLINE		512		/* maximum line length */
167ea906c41SOllivier Robert #define	MAXTOKENS	(1+1+MAXARGS+MOREARGS+2)	/* maximum number of usable tokens */
168ea906c41SOllivier Robert #define	SCREENWIDTH  	78		/* nominal screen width in columns */
169c0b746e5SOllivier Robert 
170c0b746e5SOllivier Robert /*
171c0b746e5SOllivier Robert  * Some variables used and manipulated locally
172c0b746e5SOllivier Robert  */
1732b15cb3dSCy Schubert static	struct sock_timeval tvout = { DEFTIMEOUT, 0 };	/* time out for reads */
1742b15cb3dSCy Schubert static	struct sock_timeval tvsout = { DEFSTIMEOUT, 0 };/* secondary time out */
175c0b746e5SOllivier Robert static	l_fp delay_time;				/* delay time */
176c0b746e5SOllivier Robert static	char currenthost[LENHOSTNAME];			/* current host name */
1779c2daa00SOllivier Robert int showhostnames = 1;					/* show host names by default */
178c0b746e5SOllivier Robert 
1792d4e511cSCy Schubert static	int ai_fam_templ = AF_UNSPEC;		/* address family */
1802d4e511cSCy Schubert static	int ai_fam_default = AF_UNSPEC;	/* default address family */
1819c2daa00SOllivier Robert static	SOCKET sockfd;					/* fd socket is opened on */
182c0b746e5SOllivier Robert static	int havehost = 0;				/* set to 1 when host open */
1839c2daa00SOllivier Robert int s_port = 0;
184c0b746e5SOllivier Robert 
185c0b746e5SOllivier Robert /*
186c0b746e5SOllivier Robert  * Holds data returned from queries.  We allocate INITDATASIZE
187c0b746e5SOllivier Robert  * octets to begin with, increasing this as we need to.
188c0b746e5SOllivier Robert  */
189c0b746e5SOllivier Robert #define	INITDATASIZE	(sizeof(struct resp_pkt) * 16)
190c0b746e5SOllivier Robert #define	INCDATASIZE	(sizeof(struct resp_pkt) * 8)
191c0b746e5SOllivier Robert 
192c0b746e5SOllivier Robert static	char *pktdata;
193c0b746e5SOllivier Robert static	int pktdatasize;
194c0b746e5SOllivier Robert 
195c0b746e5SOllivier Robert /*
196ce265a54SOllivier Robert  * These are used to help the magic with old and new versions of ntpd.
197ce265a54SOllivier Robert  */
1989c2daa00SOllivier Robert int impl_ver = IMPL_XNTPD;
199ce265a54SOllivier Robert static int req_pkt_size = REQ_LEN_NOMAC;
200ce265a54SOllivier Robert 
201ce265a54SOllivier Robert /*
202c0b746e5SOllivier Robert  * For commands typed on the command line (with the -c option)
203c0b746e5SOllivier Robert  */
204c0b746e5SOllivier Robert static	int numcmds = 0;
205c0b746e5SOllivier Robert static	const char *ccmds[MAXCMDS];
206c0b746e5SOllivier Robert #define	ADDCMD(cp)	if (numcmds < MAXCMDS) ccmds[numcmds++] = (cp)
207c0b746e5SOllivier Robert 
208c0b746e5SOllivier Robert /*
209c0b746e5SOllivier Robert  * When multiple hosts are specified.
210c0b746e5SOllivier Robert  */
211c0b746e5SOllivier Robert static	int numhosts = 0;
212c0b746e5SOllivier Robert static	const char *chosts[MAXHOSTS];
213c0b746e5SOllivier Robert #define	ADDHOST(cp)	if (numhosts < MAXHOSTS) chosts[numhosts++] = (cp)
214c0b746e5SOllivier Robert 
215c0b746e5SOllivier Robert /*
216c0b746e5SOllivier Robert  * Error codes for internal use
217c0b746e5SOllivier Robert  */
218c0b746e5SOllivier Robert #define	ERR_INCOMPLETE		16
219c0b746e5SOllivier Robert #define	ERR_TIMEOUT		17
220c0b746e5SOllivier Robert 
221c0b746e5SOllivier Robert /*
222c0b746e5SOllivier Robert  * Macro definitions we use
223c0b746e5SOllivier Robert  */
224c0b746e5SOllivier Robert #define	ISSPACE(c)	((c) == ' ' || (c) == '\t')
225c0b746e5SOllivier Robert #define	ISEOL(c)	((c) == '\n' || (c) == '\r' || (c) == '\0')
226c0b746e5SOllivier Robert #define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)
227c0b746e5SOllivier Robert 
228c0b746e5SOllivier Robert /*
2294e1ef62aSXin LI  * Jump buffer for longjumping back to the command level.
2304e1ef62aSXin LI  *
2314e1ef62aSXin LI  * See ntpq/ntpq.c for an explanation why 'sig{set,long}jmp()' is used
2324e1ef62aSXin LI  * when available.
233c0b746e5SOllivier Robert  */
2344e1ef62aSXin LI #if HAVE_DECL_SIGSETJMP && HAVE_DECL_SIGLONGJMP
2354e1ef62aSXin LI # define JMP_BUF	sigjmp_buf
2364e1ef62aSXin LI # define SETJMP(x)	sigsetjmp((x), 1)
2374e1ef62aSXin LI # define LONGJMP(x, v)	siglongjmp((x),(v))
2384e1ef62aSXin LI #else
2394e1ef62aSXin LI # define JMP_BUF	jmp_buf
2404e1ef62aSXin LI # define SETJMP(x)	setjmp((x))
2414e1ef62aSXin LI # define LONGJMP(x, v)	longjmp((x),(v))
2424e1ef62aSXin LI #endif
2434e1ef62aSXin LI static	JMP_BUF		interrupt_buf;
244c0b746e5SOllivier Robert static	volatile int	jump = 0;
245c0b746e5SOllivier Robert 
246c0b746e5SOllivier Robert /*
247c0b746e5SOllivier Robert  * Pointer to current output unit
248c0b746e5SOllivier Robert  */
2494e1ef62aSXin LI static	FILE *current_output = NULL;
250c0b746e5SOllivier Robert 
251c0b746e5SOllivier Robert /*
252c0b746e5SOllivier Robert  * Command table imported from ntpdc_ops.c
253c0b746e5SOllivier Robert  */
254c0b746e5SOllivier Robert extern struct xcmd opcmds[];
255c0b746e5SOllivier Robert 
2569034852cSGleb Smirnoff char const *progname;
257c0b746e5SOllivier Robert 
258c0b746e5SOllivier Robert #ifdef NO_MAIN_ALLOWED
259c0b746e5SOllivier Robert CALL(ntpdc,"ntpdc",ntpdcmain);
260c0b746e5SOllivier Robert #else
261c0b746e5SOllivier Robert int
main(int argc,char * argv[])262c0b746e5SOllivier Robert main(
263c0b746e5SOllivier Robert 	int argc,
264c0b746e5SOllivier Robert 	char *argv[]
265c0b746e5SOllivier Robert 	)
266c0b746e5SOllivier Robert {
267c0b746e5SOllivier Robert 	return ntpdcmain(argc, argv);
268c0b746e5SOllivier Robert }
269c0b746e5SOllivier Robert #endif
270c0b746e5SOllivier Robert 
271c0b746e5SOllivier Robert #ifdef SYS_VXWORKS
clear_globals(void)272c0b746e5SOllivier Robert void clear_globals(void)
273c0b746e5SOllivier Robert {
274c0b746e5SOllivier Robert     showhostnames = 0;              /* show host names by default */
275c0b746e5SOllivier Robert     havehost = 0;                   /* set to 1 when host open */
276c0b746e5SOllivier Robert     numcmds = 0;
277c0b746e5SOllivier Robert     numhosts = 0;
278c0b746e5SOllivier Robert }
279c0b746e5SOllivier Robert #endif
280c0b746e5SOllivier Robert 
281c0b746e5SOllivier Robert /*
282c0b746e5SOllivier Robert  * main - parse arguments and handle options
283c0b746e5SOllivier Robert  */
284c0b746e5SOllivier Robert int
ntpdcmain(int argc,char * argv[])285c0b746e5SOllivier Robert ntpdcmain(
286c0b746e5SOllivier Robert 	int argc,
287c0b746e5SOllivier Robert 	char *argv[]
288c0b746e5SOllivier Robert 	)
289c0b746e5SOllivier Robert {
290c0b746e5SOllivier Robert 	delay_time.l_ui = 0;
291c0b746e5SOllivier Robert 	delay_time.l_uf = DEFDELAY;
292c0b746e5SOllivier Robert 
293c0b746e5SOllivier Robert #ifdef SYS_VXWORKS
294c0b746e5SOllivier Robert 	clear_globals();
295c0b746e5SOllivier Robert 	taskPrioritySet(taskIdSelf(), 100 );
296c0b746e5SOllivier Robert #endif
297c0b746e5SOllivier Robert 
2982b15cb3dSCy Schubert 	init_lib();	/* sets up ipv4_works, ipv6_works */
2992b15cb3dSCy Schubert 	ssl_applink();
3002b15cb3dSCy Schubert 	init_auth();
3019c2daa00SOllivier Robert 
3022b15cb3dSCy Schubert 	/* Check to see if we have IPv6. Otherwise default to IPv4 */
3032b15cb3dSCy Schubert 	if (!ipv6_works)
3049c2daa00SOllivier Robert 		ai_fam_default = AF_INET;
3059c2daa00SOllivier Robert 
306c0b746e5SOllivier Robert 	progname = argv[0];
307ea906c41SOllivier Robert 
308ea906c41SOllivier Robert 	{
3092b15cb3dSCy Schubert 		int optct = ntpOptionProcess(&ntpdcOptions, argc, argv);
310ea906c41SOllivier Robert 		argc -= optct;
311ea906c41SOllivier Robert 		argv += optct;
312ea906c41SOllivier Robert 	}
313ea906c41SOllivier Robert 
3142b15cb3dSCy Schubert 	if (HAVE_OPT(IPV4))
3152d4e511cSCy Schubert 		ai_fam_default = AF_INET;
3162b15cb3dSCy Schubert 	else if (HAVE_OPT(IPV6))
3172d4e511cSCy Schubert 		ai_fam_default = AF_INET6;
3182d4e511cSCy Schubert 
319ea906c41SOllivier Robert 	ai_fam_templ = ai_fam_default;
320ea906c41SOllivier Robert 
321ea906c41SOllivier Robert 	if (HAVE_OPT(COMMAND)) {
322ea906c41SOllivier Robert 		int		cmdct = STACKCT_OPT( COMMAND );
323ea906c41SOllivier Robert 		const char**	cmds  = STACKLST_OPT( COMMAND );
324ea906c41SOllivier Robert 
325ea906c41SOllivier Robert 		while (cmdct-- > 0) {
326ea906c41SOllivier Robert 			ADDCMD(*cmds++);
327ea906c41SOllivier Robert 		}
328ea906c41SOllivier Robert 	}
329ea906c41SOllivier Robert 
3302b15cb3dSCy Schubert 	debug = OPT_VALUE_SET_DEBUG_LEVEL;
331ea906c41SOllivier Robert 
332ea906c41SOllivier Robert 	if (HAVE_OPT(INTERACTIVE)) {
333ea906c41SOllivier Robert 		interactive = 1;
334ea906c41SOllivier Robert 	}
335ea906c41SOllivier Robert 
336ea906c41SOllivier Robert 	if (HAVE_OPT(NUMERIC)) {
337ea906c41SOllivier Robert 		showhostnames = 0;
338ea906c41SOllivier Robert 	}
339ea906c41SOllivier Robert 
340ea906c41SOllivier Robert 	if (HAVE_OPT(LISTPEERS)) {
341ea906c41SOllivier Robert 		ADDCMD("listpeers");
342ea906c41SOllivier Robert 	}
343ea906c41SOllivier Robert 
344ea906c41SOllivier Robert 	if (HAVE_OPT(PEERS)) {
345ea906c41SOllivier Robert 		ADDCMD("peers");
346ea906c41SOllivier Robert 	}
347ea906c41SOllivier Robert 
348ea906c41SOllivier Robert 	if (HAVE_OPT(SHOWPEERS)) {
349ea906c41SOllivier Robert 		ADDCMD("dmpeers");
350ea906c41SOllivier Robert 	}
351ea906c41SOllivier Robert 
352ea906c41SOllivier Robert 	if (ntp_optind == argc) {
353ea906c41SOllivier Robert 		ADDHOST(DEFHOST);
354ea906c41SOllivier Robert 	} else {
355ea906c41SOllivier Robert 		for (; ntp_optind < argc; ntp_optind++)
356ea906c41SOllivier Robert 		    ADDHOST(argv[ntp_optind]);
357ea906c41SOllivier Robert 	}
358ea906c41SOllivier Robert 
359ea906c41SOllivier Robert 	if (numcmds == 0 && interactive == 0
360ea906c41SOllivier Robert 	    && isatty(fileno(stdin)) && isatty(fileno(stderr))) {
361ea906c41SOllivier Robert 		interactive = 1;
362ea906c41SOllivier Robert 	}
363ea906c41SOllivier Robert 
364c0b746e5SOllivier Robert #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */
365c0b746e5SOllivier Robert 	if (interactive)
366c0b746e5SOllivier Robert 		(void) signal_no_reset(SIGINT, abortcmd);
367c0b746e5SOllivier Robert #endif /* SYS_WINNT */
368c0b746e5SOllivier Robert 
369c0b746e5SOllivier Robert 	/*
370c0b746e5SOllivier Robert 	 * Initialize the packet data buffer
371c0b746e5SOllivier Robert 	 */
372c0b746e5SOllivier Robert 	pktdatasize = INITDATASIZE;
3732b15cb3dSCy Schubert 	pktdata = emalloc(INITDATASIZE);
374c0b746e5SOllivier Robert 
375c0b746e5SOllivier Robert 	if (numcmds == 0) {
376c0b746e5SOllivier Robert 		(void) openhost(chosts[0]);
377c0b746e5SOllivier Robert 		getcmds();
378c0b746e5SOllivier Robert 	} else {
379c0b746e5SOllivier Robert 		int ihost;
380c0b746e5SOllivier Robert 		int icmd;
381c0b746e5SOllivier Robert 
382c0b746e5SOllivier Robert 		for (ihost = 0; ihost < numhosts; ihost++) {
383c0b746e5SOllivier Robert 			if (openhost(chosts[ihost]))
384c0b746e5SOllivier Robert 			    for (icmd = 0; icmd < numcmds; icmd++) {
385c0b746e5SOllivier Robert 				    if (numhosts > 1)
386c0b746e5SOllivier Robert 					printf ("--- %s ---\n",chosts[ihost]);
387c0b746e5SOllivier Robert 				    docmd(ccmds[icmd]);
388c0b746e5SOllivier Robert 			    }
389c0b746e5SOllivier Robert 		}
390c0b746e5SOllivier Robert 	}
391c0b746e5SOllivier Robert #ifdef SYS_WINNT
392c0b746e5SOllivier Robert 	WSACleanup();
393c0b746e5SOllivier Robert #endif
394c0b746e5SOllivier Robert 	return(0);
395c0b746e5SOllivier Robert } /* main end */
396c0b746e5SOllivier Robert 
397c0b746e5SOllivier Robert 
398c0b746e5SOllivier Robert /*
399c0b746e5SOllivier Robert  * openhost - open a socket to a host
400c0b746e5SOllivier Robert  */
401c0b746e5SOllivier Robert static int
openhost(const char * hname)402c0b746e5SOllivier Robert openhost(
403c0b746e5SOllivier Robert 	const char *hname
404c0b746e5SOllivier Robert 	)
405c0b746e5SOllivier Robert {
406c0b746e5SOllivier Robert 	char temphost[LENHOSTNAME];
4074e1ef62aSXin LI 	int a_info;
4089c2daa00SOllivier Robert 	struct addrinfo hints, *ai = NULL;
4092b15cb3dSCy Schubert 	sockaddr_u addr;
4102b15cb3dSCy Schubert 	size_t octets;
4114e1ef62aSXin LI 	const char *cp;
4129c2daa00SOllivier Robert 	char name[LENHOSTNAME];
4139c2daa00SOllivier Robert 	char service[5];
414c0b746e5SOllivier Robert 
4159c2daa00SOllivier Robert 	/*
4169c2daa00SOllivier Robert 	 * We need to get by the [] if they were entered
4179c2daa00SOllivier Robert 	 */
4184e1ef62aSXin LI 	if (*hname == '[') {
4194e1ef62aSXin LI 		cp = strchr(hname + 1, ']');
4204e1ef62aSXin LI 		if (!cp || (octets = (size_t)(cp - hname) - 1) >= sizeof(name)) {
4214e1ef62aSXin LI 			errno = EINVAL;
4224e1ef62aSXin LI 			warning("%s", "bad hostname/address");
4232b15cb3dSCy Schubert 			return 0;
4242b15cb3dSCy Schubert 		}
4254e1ef62aSXin LI 		memcpy(name, hname + 1, octets);
4264e1ef62aSXin LI 		name[octets] = '\0';
4274e1ef62aSXin LI 		hname = name;
428c0b746e5SOllivier Robert 	}
429c0b746e5SOllivier Robert 
4309c2daa00SOllivier Robert 	/*
4319c2daa00SOllivier Robert 	 * First try to resolve it as an ip address and if that fails,
4329c2daa00SOllivier Robert 	 * do a fullblown (dns) lookup. That way we only use the dns
4339c2daa00SOllivier Robert 	 * when it is needed and work around some implementations that
4349c2daa00SOllivier Robert 	 * will return an "IPv4-mapped IPv6 address" address if you
4359c2daa00SOllivier Robert 	 * give it an IPv4 address to lookup.
4369c2daa00SOllivier Robert 	 */
4372b15cb3dSCy Schubert 	strlcpy(service, "ntp", sizeof(service));
4382b15cb3dSCy Schubert 	ZERO(hints);
4399c2daa00SOllivier Robert 	hints.ai_family = ai_fam_templ;
4409c2daa00SOllivier Robert 	hints.ai_protocol = IPPROTO_UDP;
4419c2daa00SOllivier Robert 	hints.ai_socktype = SOCK_DGRAM;
4422b15cb3dSCy Schubert 	hints.ai_flags = Z_AI_NUMERICHOST;
4439c2daa00SOllivier Robert 
4449c2daa00SOllivier Robert 	a_info = getaddrinfo(hname, service, &hints, &ai);
445ea906c41SOllivier Robert 	if (a_info == EAI_NONAME
446ea906c41SOllivier Robert #ifdef EAI_NODATA
447ea906c41SOllivier Robert 	    || a_info == EAI_NODATA
448ea906c41SOllivier Robert #endif
449ea906c41SOllivier Robert 	   ) {
4509c2daa00SOllivier Robert 		hints.ai_flags = AI_CANONNAME;
4519c2daa00SOllivier Robert #ifdef AI_ADDRCONFIG
4529c2daa00SOllivier Robert 		hints.ai_flags |= AI_ADDRCONFIG;
4539c2daa00SOllivier Robert #endif
4549c2daa00SOllivier Robert 		a_info = getaddrinfo(hname, service, &hints, &ai);
4559c2daa00SOllivier Robert 	}
456*a466cc55SCy Schubert 	/*
457*a466cc55SCy Schubert 	 * Some older implementations don't like AI_ADDRCONFIG.
458*a466cc55SCy Schubert 	 * Some versions of Windows return WSANO_DATA when there is no
459*a466cc55SCy Schubert 	 * global address and AI_ADDRCONFIG is used.  AI_ADDRCONFIG
460*a466cc55SCy Schubert 	 * is useful to short-circuit DNS lookups for IP protocols
461*a466cc55SCy Schubert 	 * for which the host has no local addresses.  Windows
462*a466cc55SCy Schubert 	 * unfortunately instead interprets AI_ADDRCONFIG to relate
463*a466cc55SCy Schubert 	 * to off-host connectivity and so fails lookup when
464*a466cc55SCy Schubert 	 * localhost works.
465*a466cc55SCy Schubert 	 * To further muddy matters, some versions of WS2tcpip.h
466*a466cc55SCy Schubert 	 * comment out #define EAI_NODATA WSANODATA claiming it
467*a466cc55SCy Schubert 	 * was removed from RFC 2553bis and mentioning a need to
468*a466cc55SCy Schubert 	 * contact the authors to find out why, but "helpfully"
469*a466cc55SCy Schubert 	 * #defines EAI_NODATA EAI_NONAME   (== WSAHOST_NOT_FOUND)
470*a466cc55SCy Schubert 	 * So we get more ugly platform-specific workarounds.
471*a466cc55SCy Schubert 	 */
472*a466cc55SCy Schubert 	if (
473*a466cc55SCy Schubert #if defined(WIN32)
474*a466cc55SCy Schubert 		WSANO_DATA == a_info || EAI_NONAME == a_info ||
475*a466cc55SCy Schubert #endif
476*a466cc55SCy Schubert 		EAI_BADFLAGS == a_info) {
4779c2daa00SOllivier Robert 		hints.ai_flags = AI_CANONNAME;
4789c2daa00SOllivier Robert 		a_info = getaddrinfo(hname, service, &hints, &ai);
4799c2daa00SOllivier Robert 	}
4809c2daa00SOllivier Robert 	if (a_info != 0) {
4812b15cb3dSCy Schubert 		fprintf(stderr, "%s\n", gai_strerror(a_info));
482ea906c41SOllivier Robert 		if (ai != NULL)
483ea906c41SOllivier Robert 			freeaddrinfo(ai);
484c0b746e5SOllivier Robert 		return 0;
4859c2daa00SOllivier Robert 	}
4869c2daa00SOllivier Robert 
4872b15cb3dSCy Schubert 	/*
4882b15cb3dSCy Schubert 	 * getaddrinfo() has returned without error so ai should not
4892b15cb3dSCy Schubert 	 * be NULL.
4902b15cb3dSCy Schubert 	 */
4912b15cb3dSCy Schubert 	INSIST(ai != NULL);
4922b15cb3dSCy Schubert 	ZERO(addr);
4932b15cb3dSCy Schubert 	octets = min(sizeof(addr), ai->ai_addrlen);
4942b15cb3dSCy Schubert 	memcpy(&addr, ai->ai_addr, octets);
4952b15cb3dSCy Schubert 
4962b15cb3dSCy Schubert 	if (ai->ai_canonname == NULL)
4972b15cb3dSCy Schubert 		strlcpy(temphost, stoa(&addr), sizeof(temphost));
4982b15cb3dSCy Schubert 	else
4992b15cb3dSCy Schubert 		strlcpy(temphost, ai->ai_canonname, sizeof(temphost));
500c0b746e5SOllivier Robert 
501c0b746e5SOllivier Robert 	if (debug > 2)
502c0b746e5SOllivier Robert 		printf("Opening host %s\n", temphost);
503c0b746e5SOllivier Robert 
504c0b746e5SOllivier Robert 	if (havehost == 1) {
505c0b746e5SOllivier Robert 		if (debug > 2)
506c0b746e5SOllivier Robert 			printf("Closing old host %s\n", currenthost);
5072b15cb3dSCy Schubert 		closesocket(sockfd);
508c0b746e5SOllivier Robert 		havehost = 0;
509c0b746e5SOllivier Robert 	}
5102b15cb3dSCy Schubert 	strlcpy(currenthost, temphost, sizeof(currenthost));
511c0b746e5SOllivier Robert 
5129c2daa00SOllivier Robert 	/* port maps to the same in both families */
5132b15cb3dSCy Schubert 	s_port = NSRCPORT(&addr);;
5149c2daa00SOllivier Robert #ifdef SYS_VXWORKS
5159c2daa00SOllivier Robert 	((struct sockaddr_in6 *)&hostaddr)->sin6_port = htons(SERVER_PORT_NUM);
5169c2daa00SOllivier Robert 	if (ai->ai_family == AF_INET)
5179c2daa00SOllivier Robert 		*(struct sockaddr_in *)&hostaddr=
5189c2daa00SOllivier Robert 			*((struct sockaddr_in *)ai->ai_addr);
5199c2daa00SOllivier Robert 	else
5209c2daa00SOllivier Robert 		*(struct sockaddr_in6 *)&hostaddr=
5219c2daa00SOllivier Robert 			*((struct sockaddr_in6 *)ai->ai_addr);
5229c2daa00SOllivier Robert #endif /* SYS_VXWORKS */
523c0b746e5SOllivier Robert 
524c0b746e5SOllivier Robert #ifdef SYS_WINNT
525c0b746e5SOllivier Robert 	{
526c0b746e5SOllivier Robert 		int optionValue = SO_SYNCHRONOUS_NONALERT;
527c0b746e5SOllivier Robert 		int err;
528ea906c41SOllivier Robert 
52909100258SXin LI 		err = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (void *)&optionValue, sizeof(optionValue));
530c0b746e5SOllivier Robert 		if (err != NO_ERROR) {
531c0b746e5SOllivier Robert 			(void) fprintf(stderr, "cannot open nonoverlapped sockets\n");
532c0b746e5SOllivier Robert 			exit(1);
533c0b746e5SOllivier Robert 		}
534c0b746e5SOllivier Robert 	}
5352b15cb3dSCy Schubert #endif /* SYS_WINNT */
536c0b746e5SOllivier Robert 
5379c2daa00SOllivier Robert 	sockfd = socket(ai->ai_family, SOCK_DGRAM, 0);
538c0b746e5SOllivier Robert 	if (sockfd == INVALID_SOCKET) {
5392b15cb3dSCy Schubert 		error("socket");
540c0b746e5SOllivier Robert 		exit(-1);
541c0b746e5SOllivier Robert 	}
542c0b746e5SOllivier Robert 
543c0b746e5SOllivier Robert #ifdef NEED_RCVBUF_SLOP
544c0b746e5SOllivier Robert # ifdef SO_RCVBUF
545c0b746e5SOllivier Robert 	{
546c0b746e5SOllivier Robert 		int rbufsize = INITDATASIZE + 2048; /* 2K for slop */
547c0b746e5SOllivier Robert 
548c0b746e5SOllivier Robert 		if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF,
54909100258SXin LI 			       (void *)&rbufsize, sizeof(int)) == -1)
5502b15cb3dSCy Schubert 		    error("setsockopt");
551c0b746e5SOllivier Robert 	}
552c0b746e5SOllivier Robert # endif
553c0b746e5SOllivier Robert #endif
554c0b746e5SOllivier Robert 
5559c2daa00SOllivier Robert #ifdef SYS_VXWORKS
556c0b746e5SOllivier Robert 	if (connect(sockfd, (struct sockaddr *)&hostaddr,
5573311ff84SXin LI 		    sizeof(hostaddr)) == -1)
5589c2daa00SOllivier Robert #else
5593311ff84SXin LI 	if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) == -1)
5609c2daa00SOllivier Robert #endif /* SYS_VXWORKS */
5613311ff84SXin LI 	{
5622b15cb3dSCy Schubert 		error("connect");
5632b15cb3dSCy Schubert 		exit(-1);
5642b15cb3dSCy Schubert 	}
5652b15cb3dSCy Schubert 
5669c2daa00SOllivier Robert 	freeaddrinfo(ai);
567c0b746e5SOllivier Robert 	havehost = 1;
568ce265a54SOllivier Robert 	req_pkt_size = REQ_LEN_NOMAC;
5699c2daa00SOllivier Robert 	impl_ver = IMPL_XNTPD;
570c0b746e5SOllivier Robert 	return 1;
571c0b746e5SOllivier Robert }
572c0b746e5SOllivier Robert 
573c0b746e5SOllivier Robert 
574c0b746e5SOllivier Robert /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
575c0b746e5SOllivier Robert /*
576c0b746e5SOllivier Robert  * sendpkt - send a packet to the remote host
577c0b746e5SOllivier Robert  */
578c0b746e5SOllivier Robert static int
sendpkt(void * xdata,size_t xdatalen)579c0b746e5SOllivier Robert sendpkt(
5802b15cb3dSCy Schubert 	void *	xdata,
5812b15cb3dSCy Schubert 	size_t	xdatalen
582c0b746e5SOllivier Robert 	)
583c0b746e5SOllivier Robert {
5842b15cb3dSCy Schubert 	if (send(sockfd, xdata, xdatalen, 0) == -1) {
5852b15cb3dSCy Schubert 		warning("write to %s failed", currenthost);
586c0b746e5SOllivier Robert 		return -1;
587c0b746e5SOllivier Robert 	}
588c0b746e5SOllivier Robert 
589c0b746e5SOllivier Robert 	return 0;
590c0b746e5SOllivier Robert }
591c0b746e5SOllivier Robert 
592c0b746e5SOllivier Robert 
593c0b746e5SOllivier Robert /*
594c0b746e5SOllivier Robert  * growpktdata - grow the packet data area
595c0b746e5SOllivier Robert  */
596c0b746e5SOllivier Robert static void
growpktdata(void)597c0b746e5SOllivier Robert growpktdata(void)
598c0b746e5SOllivier Robert {
5992b15cb3dSCy Schubert 	size_t priorsz;
6002b15cb3dSCy Schubert 
6012b15cb3dSCy Schubert 	priorsz = (size_t)pktdatasize;
602c0b746e5SOllivier Robert 	pktdatasize += INCDATASIZE;
6032b15cb3dSCy Schubert 	pktdata = erealloc_zero(pktdata, (size_t)pktdatasize, priorsz);
604c0b746e5SOllivier Robert }
605c0b746e5SOllivier Robert 
606c0b746e5SOllivier Robert 
607c0b746e5SOllivier Robert /*
608c0b746e5SOllivier Robert  * getresponse - get a (series of) response packet(s) and return the data
609c0b746e5SOllivier Robert  */
610c0b746e5SOllivier Robert static int
getresponse(int implcode,int reqcode,size_t * ritems,size_t * rsize,const char ** rdata,size_t esize)611c0b746e5SOllivier Robert getresponse(
612c0b746e5SOllivier Robert 	int implcode,
613c0b746e5SOllivier Robert 	int reqcode,
6143311ff84SXin LI 	size_t *ritems,
6153311ff84SXin LI 	size_t *rsize,
6163311ff84SXin LI 	const char **rdata,
6173311ff84SXin LI 	size_t esize
618c0b746e5SOllivier Robert 	)
619c0b746e5SOllivier Robert {
620c0b746e5SOllivier Robert 	struct resp_pkt rpkt;
6212b15cb3dSCy Schubert 	struct sock_timeval tvo;
6223311ff84SXin LI 	size_t items;
6233311ff84SXin LI 	size_t i;
6243311ff84SXin LI 	size_t size;
6253311ff84SXin LI 	size_t datasize;
626c0b746e5SOllivier Robert 	char *datap;
6279c2daa00SOllivier Robert 	char *tmp_data;
628c0b746e5SOllivier Robert 	char haveseq[MAXSEQ+1];
629c0b746e5SOllivier Robert 	int firstpkt;
630c0b746e5SOllivier Robert 	int lastseq;
631c0b746e5SOllivier Robert 	int numrecv;
632c0b746e5SOllivier Robert 	int seq;
633c0b746e5SOllivier Robert 	fd_set fds;
6342b15cb3dSCy Schubert 	ssize_t n;
63568ba7e87SXin LI 	int pad;
63668ba7e87SXin LI 	/* absolute timeout checks. Not 'time_t' by intention! */
63768ba7e87SXin LI 	uint32_t tobase;	/* base value for timeout */
63868ba7e87SXin LI 	uint32_t tospan;	/* timeout span (max delay) */
63968ba7e87SXin LI 	uint32_t todiff;	/* current delay */
640c0b746e5SOllivier Robert 
641c0b746e5SOllivier Robert 	/*
642c0b746e5SOllivier Robert 	 * This is pretty tricky.  We may get between 1 and many packets
643c0b746e5SOllivier Robert 	 * back in response to the request.  We peel the data out of
644c0b746e5SOllivier Robert 	 * each packet and collect it in one long block.  When the last
645c0b746e5SOllivier Robert 	 * packet in the sequence is received we'll know how many we
646c0b746e5SOllivier Robert 	 * should have had.  Note we use one long time out, should reconsider.
647c0b746e5SOllivier Robert 	 */
648c0b746e5SOllivier Robert 	*ritems = 0;
649c0b746e5SOllivier Robert 	*rsize = 0;
650c0b746e5SOllivier Robert 	firstpkt = 1;
651c0b746e5SOllivier Robert 	numrecv = 0;
652c0b746e5SOllivier Robert 	*rdata = datap = pktdata;
653c0b746e5SOllivier Robert 	lastseq = 999;	/* too big to be a sequence number */
6542b15cb3dSCy Schubert 	ZERO(haveseq);
655c0b746e5SOllivier Robert 	FD_ZERO(&fds);
65668ba7e87SXin LI 	tobase = (uint32_t)time(NULL);
657c0b746e5SOllivier Robert 
658c0b746e5SOllivier Robert     again:
659c0b746e5SOllivier Robert 	if (firstpkt)
660c0b746e5SOllivier Robert 		tvo = tvout;
661c0b746e5SOllivier Robert 	else
662c0b746e5SOllivier Robert 		tvo = tvsout;
66368ba7e87SXin LI 	tospan = (uint32_t)tvo.tv_sec + (tvo.tv_usec != 0);
664c0b746e5SOllivier Robert 
665c0b746e5SOllivier Robert 	FD_SET(sockfd, &fds);
6663311ff84SXin LI 	n = select(sockfd+1, &fds, NULL, NULL, &tvo);
667c0b746e5SOllivier Robert 	if (n == -1) {
6682b15cb3dSCy Schubert 		warning("select fails");
669c0b746e5SOllivier Robert 		return -1;
670c0b746e5SOllivier Robert 	}
67168ba7e87SXin LI 
67268ba7e87SXin LI 	/*
67368ba7e87SXin LI 	 * Check if this is already too late. Trash the data and fake a
67468ba7e87SXin LI 	 * timeout if this is so.
67568ba7e87SXin LI 	 */
67668ba7e87SXin LI 	todiff = (((uint32_t)time(NULL)) - tobase) & 0x7FFFFFFFu;
67768ba7e87SXin LI 	if ((n > 0) && (todiff > tospan)) {
67868ba7e87SXin LI 		n = recv(sockfd, (char *)&rpkt, sizeof(rpkt), 0);
679f0574f5cSXin LI 		n -= n; /* faked timeout return from 'select()'*/
68068ba7e87SXin LI 	}
68168ba7e87SXin LI 
682c0b746e5SOllivier Robert 	if (n == 0) {
683c0b746e5SOllivier Robert 		/*
684c0b746e5SOllivier Robert 		 * Timed out.  Return what we have
685c0b746e5SOllivier Robert 		 */
686c0b746e5SOllivier Robert 		if (firstpkt) {
687c0b746e5SOllivier Robert 			(void) fprintf(stderr,
6883311ff84SXin LI 				       "%s: timed out, nothing received\n",
6893311ff84SXin LI 				       currenthost);
690c0b746e5SOllivier Robert 			return ERR_TIMEOUT;
691c0b746e5SOllivier Robert 		} else {
692c0b746e5SOllivier Robert 			(void) fprintf(stderr,
693c0b746e5SOllivier Robert 				       "%s: timed out with incomplete data\n",
694c0b746e5SOllivier Robert 				       currenthost);
695c0b746e5SOllivier Robert 			if (debug) {
696c0b746e5SOllivier Robert 				printf("Received sequence numbers");
697c0b746e5SOllivier Robert 				for (n = 0; n <= MAXSEQ; n++)
698c0b746e5SOllivier Robert 				    if (haveseq[n])
699e27abb66SXin LI 					printf(" %zd,", (size_t)n);
700c0b746e5SOllivier Robert 				if (lastseq != 999)
701c0b746e5SOllivier Robert 				    printf(" last frame received\n");
702c0b746e5SOllivier Robert 				else
703c0b746e5SOllivier Robert 				    printf(" last frame not received\n");
704c0b746e5SOllivier Robert 			}
705c0b746e5SOllivier Robert 			return ERR_INCOMPLETE;
706c0b746e5SOllivier Robert 		}
707c0b746e5SOllivier Robert 	}
708c0b746e5SOllivier Robert 
709c0b746e5SOllivier Robert 	n = recv(sockfd, (char *)&rpkt, sizeof(rpkt), 0);
710c0b746e5SOllivier Robert 	if (n == -1) {
7112b15cb3dSCy Schubert 		warning("read");
712c0b746e5SOllivier Robert 		return -1;
713c0b746e5SOllivier Robert 	}
714c0b746e5SOllivier Robert 
715c0b746e5SOllivier Robert 
716c0b746e5SOllivier Robert 	/*
717c0b746e5SOllivier Robert 	 * Check for format errors.  Bug proofing.
718c0b746e5SOllivier Robert 	 */
7192b15cb3dSCy Schubert 	if (n < (ssize_t)RESP_HEADER_SIZE) {
720c0b746e5SOllivier Robert 		if (debug)
721e27abb66SXin LI 			printf("Short (%zd byte) packet received\n", (size_t)n);
722c0b746e5SOllivier Robert 		goto again;
723c0b746e5SOllivier Robert 	}
724c0b746e5SOllivier Robert 	if (INFO_VERSION(rpkt.rm_vn_mode) > NTP_VERSION ||
725c0b746e5SOllivier Robert 	    INFO_VERSION(rpkt.rm_vn_mode) < NTP_OLDVERSION) {
726c0b746e5SOllivier Robert 		if (debug)
727c0b746e5SOllivier Robert 			printf("Packet received with version %d\n",
728c0b746e5SOllivier Robert 			       INFO_VERSION(rpkt.rm_vn_mode));
729c0b746e5SOllivier Robert 		goto again;
730c0b746e5SOllivier Robert 	}
731c0b746e5SOllivier Robert 	if (INFO_MODE(rpkt.rm_vn_mode) != MODE_PRIVATE) {
732c0b746e5SOllivier Robert 		if (debug)
733c0b746e5SOllivier Robert 			printf("Packet received with mode %d\n",
734c0b746e5SOllivier Robert 			       INFO_MODE(rpkt.rm_vn_mode));
735c0b746e5SOllivier Robert 		goto again;
736c0b746e5SOllivier Robert 	}
737c0b746e5SOllivier Robert 	if (INFO_IS_AUTH(rpkt.auth_seq)) {
738c0b746e5SOllivier Robert 		if (debug)
739c0b746e5SOllivier Robert 			printf("Encrypted packet received\n");
740c0b746e5SOllivier Robert 		goto again;
741c0b746e5SOllivier Robert 	}
742c0b746e5SOllivier Robert 	if (!ISRESPONSE(rpkt.rm_vn_mode)) {
743c0b746e5SOllivier Robert 		if (debug)
744c0b746e5SOllivier Robert 			printf("Received request packet, wanted response\n");
745c0b746e5SOllivier Robert 		goto again;
746c0b746e5SOllivier Robert 	}
747c0b746e5SOllivier Robert 	if (INFO_MBZ(rpkt.mbz_itemsize) != 0) {
748c0b746e5SOllivier Robert 		if (debug)
749c0b746e5SOllivier Robert 			printf("Received packet with nonzero MBZ field!\n");
750c0b746e5SOllivier Robert 		goto again;
751c0b746e5SOllivier Robert 	}
752c0b746e5SOllivier Robert 
753c0b746e5SOllivier Robert 	/*
754c0b746e5SOllivier Robert 	 * Check implementation/request.  Could be old data getting to us.
755c0b746e5SOllivier Robert 	 */
756c0b746e5SOllivier Robert 	if (rpkt.implementation != implcode || rpkt.request != reqcode) {
757c0b746e5SOllivier Robert 		if (debug)
758c0b746e5SOllivier Robert 			printf(
759c0b746e5SOllivier Robert 			    "Received implementation/request of %d/%d, wanted %d/%d",
760c0b746e5SOllivier Robert 			    rpkt.implementation, rpkt.request,
761c0b746e5SOllivier Robert 			    implcode, reqcode);
762c0b746e5SOllivier Robert 		goto again;
763c0b746e5SOllivier Robert 	}
764c0b746e5SOllivier Robert 
765c0b746e5SOllivier Robert 	/*
766c0b746e5SOllivier Robert 	 * Check the error code.  If non-zero, return it.
767c0b746e5SOllivier Robert 	 */
768c0b746e5SOllivier Robert 	if (INFO_ERR(rpkt.err_nitems) != INFO_OKAY) {
769c0b746e5SOllivier Robert 		if (debug && ISMORE(rpkt.rm_vn_mode)) {
770c0b746e5SOllivier Robert 			printf("Error code %d received on not-final packet\n",
771c0b746e5SOllivier Robert 			       INFO_ERR(rpkt.err_nitems));
772c0b746e5SOllivier Robert 		}
773c0b746e5SOllivier Robert 		return (int)INFO_ERR(rpkt.err_nitems);
774c0b746e5SOllivier Robert 	}
775c0b746e5SOllivier Robert 
776c0b746e5SOllivier Robert 	/*
777c0b746e5SOllivier Robert 	 * Collect items and size.  Make sure they make sense.
778c0b746e5SOllivier Robert 	 */
779c0b746e5SOllivier Robert 	items = INFO_NITEMS(rpkt.err_nitems);
780c0b746e5SOllivier Robert 	size = INFO_ITEMSIZE(rpkt.mbz_itemsize);
7819c2daa00SOllivier Robert 	if (esize > size)
7829c2daa00SOllivier Robert 		pad = esize - size;
7839c2daa00SOllivier Robert 	else
7849c2daa00SOllivier Robert 		pad = 0;
7852b15cb3dSCy Schubert 	datasize = items * size;
7862b15cb3dSCy Schubert 	if ((size_t)datasize > (n-RESP_HEADER_SIZE)) {
787c0b746e5SOllivier Robert 		if (debug)
788c0b746e5SOllivier Robert 		    printf(
7893311ff84SXin LI 			    "Received items %zu, size %zu (total %zu), data in packet is %zu\n",
790c0b746e5SOllivier Robert 			    items, size, datasize, n-RESP_HEADER_SIZE);
791c0b746e5SOllivier Robert 		goto again;
792c0b746e5SOllivier Robert 	}
793c0b746e5SOllivier Robert 
794c0b746e5SOllivier Robert 	/*
795c0b746e5SOllivier Robert 	 * If this isn't our first packet, make sure the size matches
796c0b746e5SOllivier Robert 	 * the other ones.
797c0b746e5SOllivier Robert 	 */
7982b15cb3dSCy Schubert 	if (!firstpkt && size != *rsize) {
799c0b746e5SOllivier Robert 		if (debug)
8003311ff84SXin LI 		    printf("Received itemsize %zu, previous %zu\n",
801c0b746e5SOllivier Robert 			   size, *rsize);
802c0b746e5SOllivier Robert 		goto again;
803c0b746e5SOllivier Robert 	}
804c0b746e5SOllivier Robert 	/*
8059c2daa00SOllivier Robert 	 * If we've received this before, +toss it
806c0b746e5SOllivier Robert 	 */
807c0b746e5SOllivier Robert 	seq = INFO_SEQ(rpkt.auth_seq);
808c0b746e5SOllivier Robert 	if (haveseq[seq]) {
809c0b746e5SOllivier Robert 		if (debug)
810c0b746e5SOllivier Robert 		    printf("Received duplicate sequence number %d\n", seq);
811c0b746e5SOllivier Robert 		goto again;
812c0b746e5SOllivier Robert 	}
813c0b746e5SOllivier Robert 	haveseq[seq] = 1;
814c0b746e5SOllivier Robert 
815c0b746e5SOllivier Robert 	/*
816c0b746e5SOllivier Robert 	 * If this is the last in the sequence, record that.
817c0b746e5SOllivier Robert 	 */
818c0b746e5SOllivier Robert 	if (!ISMORE(rpkt.rm_vn_mode)) {
819c0b746e5SOllivier Robert 		if (lastseq != 999) {
820c0b746e5SOllivier Robert 			printf("Received second end sequence packet\n");
821c0b746e5SOllivier Robert 			goto again;
822c0b746e5SOllivier Robert 		}
823c0b746e5SOllivier Robert 		lastseq = seq;
824c0b746e5SOllivier Robert 	}
825c0b746e5SOllivier Robert 
826c0b746e5SOllivier Robert 	/*
82768ba7e87SXin LI 	 * So far, so good.  Copy this data into the output array. Bump
82868ba7e87SXin LI 	 * the timeout base, in case we expect more data.
829c0b746e5SOllivier Robert 	 */
83068ba7e87SXin LI 	tobase = (uint32_t)time(NULL);
8319c2daa00SOllivier Robert 	if ((datap + datasize + (pad * items)) > (pktdata + pktdatasize)) {
8323311ff84SXin LI 		size_t offset = datap - pktdata;
833c0b746e5SOllivier Robert 		growpktdata();
834c0b746e5SOllivier Robert 		*rdata = pktdata; /* might have been realloced ! */
835c0b746e5SOllivier Robert 		datap = pktdata + offset;
836c0b746e5SOllivier Robert 	}
8379c2daa00SOllivier Robert 	/*
8389c2daa00SOllivier Robert 	 * We now move the pointer along according to size and number of
8399c2daa00SOllivier Robert 	 * items.  This is so we can play nice with older implementations
8409c2daa00SOllivier Robert 	 */
8419c2daa00SOllivier Robert 
8422b15cb3dSCy Schubert 	tmp_data = rpkt.u.data;
8439c2daa00SOllivier Robert 	for (i = 0; i < items; i++) {
8442b15cb3dSCy Schubert 		memcpy(datap, tmp_data, (unsigned)size);
8459c2daa00SOllivier Robert 		tmp_data += size;
8462b15cb3dSCy Schubert 		zero_mem(datap + size, pad);
8479c2daa00SOllivier Robert 		datap += size + pad;
8489c2daa00SOllivier Robert 	}
8499c2daa00SOllivier Robert 
850c0b746e5SOllivier Robert 	if (firstpkt) {
851c0b746e5SOllivier Robert 		firstpkt = 0;
8529c2daa00SOllivier Robert 		*rsize = size + pad;
853c0b746e5SOllivier Robert 	}
854c0b746e5SOllivier Robert 	*ritems += items;
855c0b746e5SOllivier Robert 
856c0b746e5SOllivier Robert 	/*
857c0b746e5SOllivier Robert 	 * Finally, check the count of received packets.  If we've got them
858c0b746e5SOllivier Robert 	 * all, return
859c0b746e5SOllivier Robert 	 */
860c0b746e5SOllivier Robert 	++numrecv;
861c0b746e5SOllivier Robert 	if (numrecv <= lastseq)
862c0b746e5SOllivier Robert 		goto again;
863c0b746e5SOllivier Robert 	return INFO_OKAY;
864c0b746e5SOllivier Robert }
865c0b746e5SOllivier Robert 
866c0b746e5SOllivier Robert 
867c0b746e5SOllivier Robert /*
868c0b746e5SOllivier Robert  * sendrequest - format and send a request packet
8692b15cb3dSCy Schubert  *
8702b15cb3dSCy Schubert  * Historically, ntpdc has used a fixed-size request packet regardless
8712b15cb3dSCy Schubert  * of the actual payload size.  When authenticating, the timestamp, key
8722b15cb3dSCy Schubert  * ID, and digest have been placed just before the end of the packet.
8732b15cb3dSCy Schubert  * With the introduction in late 2009 of support for authenticated
8742b15cb3dSCy Schubert  * ntpdc requests using larger 20-octet digests (vs. 16 for MD5), we
8752b15cb3dSCy Schubert  * come up four bytes short.
8762b15cb3dSCy Schubert  *
8772b15cb3dSCy Schubert  * To maintain interop while allowing for larger digests, the behavior
8782b15cb3dSCy Schubert  * is unchanged when using 16-octet digests.  For larger digests, the
8792b15cb3dSCy Schubert  * timestamp, key ID, and digest are placed immediately following the
8802b15cb3dSCy Schubert  * request payload, with the overall packet size variable.  ntpd can
8812b15cb3dSCy Schubert  * distinguish 16-octet digests by the overall request size being
8822b15cb3dSCy Schubert  * REQ_LEN_NOMAC + 4 + 16 with the auth bit enabled.  When using a
8832b15cb3dSCy Schubert  * longer digest, that request size should be avoided.
8842b15cb3dSCy Schubert  *
8852b15cb3dSCy Schubert  * With the form used with 20-octet and larger digests, the timestamp,
8862b15cb3dSCy Schubert  * key ID, and digest are located by ntpd relative to the start of the
8872b15cb3dSCy Schubert  * packet, and the size of the digest is then implied by the packet
8882b15cb3dSCy Schubert  * size.
889c0b746e5SOllivier Robert  */
890c0b746e5SOllivier Robert static int
sendrequest(int implcode,int reqcode,int auth,size_t qitems,size_t qsize,const char * qdata)891c0b746e5SOllivier Robert sendrequest(
892c0b746e5SOllivier Robert 	int implcode,
893c0b746e5SOllivier Robert 	int reqcode,
894c0b746e5SOllivier Robert 	int auth,
8953311ff84SXin LI 	size_t qitems,
8962b15cb3dSCy Schubert 	size_t qsize,
8973311ff84SXin LI 	const char *qdata
898c0b746e5SOllivier Robert 	)
899c0b746e5SOllivier Robert {
900c0b746e5SOllivier Robert 	struct req_pkt qpkt;
9012b15cb3dSCy Schubert 	size_t	datasize;
9022b15cb3dSCy Schubert 	size_t	reqsize;
9032b15cb3dSCy Schubert 	u_long	key_id;
9042b15cb3dSCy Schubert 	l_fp	ts;
9052b15cb3dSCy Schubert 	l_fp *	ptstamp;
9063311ff84SXin LI 	size_t	maclen;
9072b15cb3dSCy Schubert 	char *	pass;
908c0b746e5SOllivier Robert 
9092b15cb3dSCy Schubert 	ZERO(qpkt);
910c0b746e5SOllivier Robert 	qpkt.rm_vn_mode = RM_VN_MODE(0, 0, 0);
911c0b746e5SOllivier Robert 	qpkt.implementation = (u_char)implcode;
912c0b746e5SOllivier Robert 	qpkt.request = (u_char)reqcode;
913c0b746e5SOllivier Robert 
914c0b746e5SOllivier Robert 	datasize = qitems * qsize;
9152b15cb3dSCy Schubert 	if (datasize && qdata != NULL) {
9162b15cb3dSCy Schubert 		memcpy(qpkt.u.data, qdata, datasize);
917c0b746e5SOllivier Robert 		qpkt.err_nitems = ERR_NITEMS(0, qitems);
918c0b746e5SOllivier Robert 		qpkt.mbz_itemsize = MBZ_ITEMSIZE(qsize);
919c0b746e5SOllivier Robert 	} else {
920c0b746e5SOllivier Robert 		qpkt.err_nitems = ERR_NITEMS(0, 0);
921ea906c41SOllivier Robert 		qpkt.mbz_itemsize = MBZ_ITEMSIZE(qsize);  /* allow for optional first item */
922c0b746e5SOllivier Robert 	}
923c0b746e5SOllivier Robert 
924ea906c41SOllivier Robert 	if (!auth || (keyid_entered && info_auth_keyid == 0)) {
925c0b746e5SOllivier Robert 		qpkt.auth_seq = AUTH_SEQ(0, 0);
9262b15cb3dSCy Schubert 		return sendpkt(&qpkt, req_pkt_size);
9272b15cb3dSCy Schubert 	}
928c0b746e5SOllivier Robert 
929c0b746e5SOllivier Robert 	if (info_auth_keyid == 0) {
9302b15cb3dSCy Schubert 		key_id = getkeyid("Keyid: ");
9312b15cb3dSCy Schubert 		if (!key_id) {
9322b15cb3dSCy Schubert 			fprintf(stderr, "Invalid key identifier\n");
933c0b746e5SOllivier Robert 			return 1;
934c0b746e5SOllivier Robert 		}
9352b15cb3dSCy Schubert 		info_auth_keyid = key_id;
936ea906c41SOllivier Robert 	}
937c0b746e5SOllivier Robert 	if (!authistrusted(info_auth_keyid)) {
9382b15cb3dSCy Schubert 		pass = getpass_keytype(info_auth_keytype);
9392b15cb3dSCy Schubert 		if ('\0' == pass[0]) {
9402b15cb3dSCy Schubert 			fprintf(stderr, "Invalid password\n");
9412b15cb3dSCy Schubert 			return 1;
942c0b746e5SOllivier Robert 		}
9432b15cb3dSCy Schubert 		authusekey(info_auth_keyid, info_auth_keytype,
9442b15cb3dSCy Schubert 			   (u_char *)pass);
945c0b746e5SOllivier Robert 		authtrust(info_auth_keyid, 1);
9462b15cb3dSCy Schubert 	}
947c0b746e5SOllivier Robert 	qpkt.auth_seq = AUTH_SEQ(1, 0);
9482b15cb3dSCy Schubert 	if (info_auth_hashlen > 16) {
9492b15cb3dSCy Schubert 		/*
9502b15cb3dSCy Schubert 		 * Only ntpd which expects REQ_LEN_NOMAC plus maclen
9512b15cb3dSCy Schubert 		 * octets in an authenticated request using a 16 octet
9522b15cb3dSCy Schubert 		 * digest (that is, a newer ntpd) will handle digests
9532b15cb3dSCy Schubert 		 * larger than 16 octets, so for longer digests, do
9542b15cb3dSCy Schubert 		 * not attempt to shorten the requests for downlevel
9552b15cb3dSCy Schubert 		 * ntpd compatibility.
9562b15cb3dSCy Schubert 		 */
9572b15cb3dSCy Schubert 		if (REQ_LEN_NOMAC != req_pkt_size)
9582b15cb3dSCy Schubert 			return 1;
9592b15cb3dSCy Schubert 		reqsize = REQ_LEN_HDR + datasize + sizeof(*ptstamp);
9602b15cb3dSCy Schubert 		/* align to 32 bits */
9612b15cb3dSCy Schubert 		reqsize = (reqsize + 3) & ~3;
9622b15cb3dSCy Schubert 	} else
9632b15cb3dSCy Schubert 		reqsize = req_pkt_size;
964*a466cc55SCy Schubert 	ptstamp = (void *)((char *)&qpkt + reqsize - sizeof *ptstamp);
965c0b746e5SOllivier Robert 	get_systime(&ts);
966c0b746e5SOllivier Robert 	L_ADD(&ts, &delay_time);
9672b15cb3dSCy Schubert 	HTONL_FP(&ts, ptstamp);
9683311ff84SXin LI 	maclen = authencrypt(
9693311ff84SXin LI 		info_auth_keyid, (void *)&qpkt, size2int_chk(reqsize));
9702b15cb3dSCy Schubert 	if (!maclen) {
9712b15cb3dSCy Schubert 		fprintf(stderr, "Key not found\n");
9722b15cb3dSCy Schubert 		return 1;
9734e1ef62aSXin LI 	} else if (maclen != (size_t)(info_auth_hashlen + sizeof(keyid_t))) {
9742b15cb3dSCy Schubert 		fprintf(stderr,
9753311ff84SXin LI 			"%zu octet MAC, %zu expected with %zu octet digest\n",
9762b15cb3dSCy Schubert 			maclen, (info_auth_hashlen + sizeof(keyid_t)),
9772b15cb3dSCy Schubert 			info_auth_hashlen);
9782b15cb3dSCy Schubert 		return 1;
979c0b746e5SOllivier Robert 	}
9802b15cb3dSCy Schubert 	return sendpkt(&qpkt, reqsize + maclen);
981c0b746e5SOllivier Robert }
982c0b746e5SOllivier Robert 
983c0b746e5SOllivier Robert 
984c0b746e5SOllivier Robert /*
985c0b746e5SOllivier Robert  * doquery - send a request and process the response
986c0b746e5SOllivier Robert  */
987c0b746e5SOllivier Robert int
doquery(int implcode,int reqcode,int auth,size_t qitems,size_t qsize,const char * qdata,size_t * ritems,size_t * rsize,const char ** rdata,int quiet_mask,int esize)988c0b746e5SOllivier Robert doquery(
989c0b746e5SOllivier Robert 	int implcode,
990c0b746e5SOllivier Robert 	int reqcode,
991c0b746e5SOllivier Robert 	int auth,
9923311ff84SXin LI 	size_t qitems,
9933311ff84SXin LI 	size_t qsize,
9943311ff84SXin LI 	const char *qdata,
9953311ff84SXin LI 	size_t *ritems,
9963311ff84SXin LI 	size_t *rsize,
9973311ff84SXin LI 	const char **rdata,
9989c2daa00SOllivier Robert  	int quiet_mask,
9999c2daa00SOllivier Robert 	int esize
1000c0b746e5SOllivier Robert 	)
1001c0b746e5SOllivier Robert {
1002c0b746e5SOllivier Robert 	int res;
1003c0b746e5SOllivier Robert 	char junk[512];
1004c0b746e5SOllivier Robert 	fd_set fds;
10052b15cb3dSCy Schubert 	struct sock_timeval tvzero;
1006c0b746e5SOllivier Robert 
1007c0b746e5SOllivier Robert 	/*
1008c0b746e5SOllivier Robert 	 * Check to make sure host is open
1009c0b746e5SOllivier Robert 	 */
1010c0b746e5SOllivier Robert 	if (!havehost) {
1011c0b746e5SOllivier Robert 		(void) fprintf(stderr, "***No host open, use `host' command\n");
1012c0b746e5SOllivier Robert 		return -1;
1013c0b746e5SOllivier Robert 	}
1014c0b746e5SOllivier Robert 
1015c0b746e5SOllivier Robert 	/*
1016c0b746e5SOllivier Robert 	 * Poll the socket and clear out any pending data
1017c0b746e5SOllivier Robert 	 */
1018ce265a54SOllivier Robert again:
1019c0b746e5SOllivier Robert 	do {
1020c0b746e5SOllivier Robert 		tvzero.tv_sec = tvzero.tv_usec = 0;
1021c0b746e5SOllivier Robert 		FD_ZERO(&fds);
1022c0b746e5SOllivier Robert 		FD_SET(sockfd, &fds);
10233311ff84SXin LI 		res = select(sockfd+1, &fds, NULL, NULL, &tvzero);
1024c0b746e5SOllivier Robert 		if (res == -1) {
10252b15cb3dSCy Schubert 			warning("polling select");
1026c0b746e5SOllivier Robert 			return -1;
1027c0b746e5SOllivier Robert 		} else if (res > 0)
1028c0b746e5SOllivier Robert 
1029c0b746e5SOllivier Robert 		    (void) recv(sockfd, junk, sizeof junk, 0);
1030c0b746e5SOllivier Robert 	} while (res > 0);
1031c0b746e5SOllivier Robert 
1032c0b746e5SOllivier Robert 
1033c0b746e5SOllivier Robert 	/*
1034c0b746e5SOllivier Robert 	 * send a request
1035c0b746e5SOllivier Robert 	 */
1036c0b746e5SOllivier Robert 	res = sendrequest(implcode, reqcode, auth, qitems, qsize, qdata);
1037c0b746e5SOllivier Robert 	if (res != 0)
1038c0b746e5SOllivier Robert 		return res;
1039c0b746e5SOllivier Robert 
1040c0b746e5SOllivier Robert 	/*
1041c0b746e5SOllivier Robert 	 * Get the response.  If we got a standard error, print a message
1042c0b746e5SOllivier Robert 	 */
10439c2daa00SOllivier Robert 	res = getresponse(implcode, reqcode, ritems, rsize, rdata, esize);
1044c0b746e5SOllivier Robert 
1045ce265a54SOllivier Robert 	/*
1046ce265a54SOllivier Robert 	 * Try to be compatible with older implementations of ntpd.
1047ce265a54SOllivier Robert 	 */
1048ce265a54SOllivier Robert 	if (res == INFO_ERR_FMT && req_pkt_size != 48) {
1049ce265a54SOllivier Robert 		int oldsize;
1050ce265a54SOllivier Robert 
1051ce265a54SOllivier Robert 		oldsize = req_pkt_size;
1052ce265a54SOllivier Robert 
1053ce265a54SOllivier Robert 		switch(req_pkt_size) {
1054ce265a54SOllivier Robert 		case REQ_LEN_NOMAC:
10559c2daa00SOllivier Robert 			req_pkt_size = 160;
10569c2daa00SOllivier Robert 			break;
10579c2daa00SOllivier Robert 		case 160:
1058ce265a54SOllivier Robert 			req_pkt_size = 48;
1059ce265a54SOllivier Robert 			break;
1060ce265a54SOllivier Robert 		}
10619c2daa00SOllivier Robert 		if (impl_ver == IMPL_XNTPD) {
10629c2daa00SOllivier Robert 			fprintf(stderr,
10639c2daa00SOllivier Robert 			    "***Warning changing to older implementation\n");
10649c2daa00SOllivier Robert 			return INFO_ERR_IMPL;
10659c2daa00SOllivier Robert 		}
1066ce265a54SOllivier Robert 
1067ce265a54SOllivier Robert 		fprintf(stderr,
1068ce265a54SOllivier Robert 		    "***Warning changing the request packet size from %d to %d\n",
1069ce265a54SOllivier Robert 		    oldsize, req_pkt_size);
1070ce265a54SOllivier Robert 		goto again;
1071ce265a54SOllivier Robert 	}
1072ce265a54SOllivier Robert 
1073c0b746e5SOllivier Robert  	/* log error message if not told to be quiet */
1074c0b746e5SOllivier Robert  	if ((res > 0) && (((1 << res) & quiet_mask) == 0)) {
1075c0b746e5SOllivier Robert 		switch(res) {
1076c0b746e5SOllivier Robert 		case INFO_ERR_IMPL:
10779c2daa00SOllivier Robert 			/* Give us a chance to try the older implementation. */
10789c2daa00SOllivier Robert 			if (implcode == IMPL_XNTPD)
10799c2daa00SOllivier Robert 				break;
1080c0b746e5SOllivier Robert 			(void) fprintf(stderr,
10812b15cb3dSCy Schubert 				       "***Server implementation incompatible with our own\n");
1082c0b746e5SOllivier Robert 			break;
1083c0b746e5SOllivier Robert 		case INFO_ERR_REQ:
1084c0b746e5SOllivier Robert 			(void) fprintf(stderr,
1085c0b746e5SOllivier Robert 				       "***Server doesn't implement this request\n");
1086c0b746e5SOllivier Robert 			break;
1087c0b746e5SOllivier Robert 		case INFO_ERR_FMT:
1088c0b746e5SOllivier Robert 			(void) fprintf(stderr,
1089c0b746e5SOllivier Robert 				       "***Server reports a format error in the received packet (shouldn't happen)\n");
1090c0b746e5SOllivier Robert 			break;
1091c0b746e5SOllivier Robert 		case INFO_ERR_NODATA:
1092c0b746e5SOllivier Robert 			(void) fprintf(stderr,
1093c0b746e5SOllivier Robert 				       "***Server reports data not found\n");
1094c0b746e5SOllivier Robert 			break;
1095c0b746e5SOllivier Robert 		case INFO_ERR_AUTH:
1096c0b746e5SOllivier Robert 			(void) fprintf(stderr, "***Permission denied\n");
1097c0b746e5SOllivier Robert 			break;
1098c0b746e5SOllivier Robert 		case ERR_TIMEOUT:
1099c0b746e5SOllivier Robert 			(void) fprintf(stderr, "***Request timed out\n");
1100c0b746e5SOllivier Robert 			break;
1101c0b746e5SOllivier Robert 		case ERR_INCOMPLETE:
1102c0b746e5SOllivier Robert 			(void) fprintf(stderr,
1103c0b746e5SOllivier Robert 				       "***Response from server was incomplete\n");
1104c0b746e5SOllivier Robert 			break;
1105c0b746e5SOllivier Robert 		default:
1106c0b746e5SOllivier Robert 			(void) fprintf(stderr,
1107c0b746e5SOllivier Robert 				       "***Server returns unknown error code %d\n", res);
1108c0b746e5SOllivier Robert 			break;
1109c0b746e5SOllivier Robert 		}
1110c0b746e5SOllivier Robert 	}
1111c0b746e5SOllivier Robert 	return res;
1112c0b746e5SOllivier Robert }
1113c0b746e5SOllivier Robert 
1114c0b746e5SOllivier Robert 
1115c0b746e5SOllivier Robert /*
1116c0b746e5SOllivier Robert  * getcmds - read commands from the standard input and execute them
1117c0b746e5SOllivier Robert  */
1118c0b746e5SOllivier Robert static void
getcmds(void)1119c0b746e5SOllivier Robert getcmds(void)
1120c0b746e5SOllivier Robert {
1121224ba2bdSOllivier Robert 	char *	line;
11222b15cb3dSCy Schubert 	int	count;
11232b15cb3dSCy Schubert 
11242b15cb3dSCy Schubert 	ntp_readline_init(interactive ? prompt : NULL);
1125224ba2bdSOllivier Robert 
1126224ba2bdSOllivier Robert 	for (;;) {
11272b15cb3dSCy Schubert 		line = ntp_readline(&count);
11282b15cb3dSCy Schubert 		if (NULL == line)
11292b15cb3dSCy Schubert 			break;
1130224ba2bdSOllivier Robert 		docmd(line);
1131224ba2bdSOllivier Robert 		free(line);
1132224ba2bdSOllivier Robert 	}
1133c0b746e5SOllivier Robert 
11342b15cb3dSCy Schubert 	ntp_readline_uninit();
1135c0b746e5SOllivier Robert }
1136c0b746e5SOllivier Robert 
1137c0b746e5SOllivier Robert 
11389c2daa00SOllivier Robert #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */
1139c0b746e5SOllivier Robert /*
1140c0b746e5SOllivier Robert  * abortcmd - catch interrupts and abort the current command
1141c0b746e5SOllivier Robert  */
1142c0b746e5SOllivier Robert static RETSIGTYPE
abortcmd(int sig)1143c0b746e5SOllivier Robert abortcmd(
1144c0b746e5SOllivier Robert 	int sig
1145c0b746e5SOllivier Robert 	)
1146c0b746e5SOllivier Robert {
1147c0b746e5SOllivier Robert 	if (current_output == stdout)
1148c0b746e5SOllivier Robert 		(void)fflush(stdout);
1149c0b746e5SOllivier Robert 	putc('\n', stderr);
1150c0b746e5SOllivier Robert 	(void)fflush(stderr);
11514e1ef62aSXin LI 	if (jump) {
11524e1ef62aSXin LI 		jump = 0;
11534e1ef62aSXin LI 		LONGJMP(interrupt_buf, 1);
11544e1ef62aSXin LI 	}
1155c0b746e5SOllivier Robert }
11569c2daa00SOllivier Robert #endif /* SYS_WINNT */
1157c0b746e5SOllivier Robert 
1158c0b746e5SOllivier Robert /*
1159c0b746e5SOllivier Robert  * docmd - decode the command line and execute a command
1160c0b746e5SOllivier Robert  */
1161c0b746e5SOllivier Robert static void
docmd(const char * cmdline)1162c0b746e5SOllivier Robert docmd(
1163c0b746e5SOllivier Robert 	const char *cmdline
1164c0b746e5SOllivier Robert 	)
1165c0b746e5SOllivier Robert {
1166ea906c41SOllivier Robert 	char *tokens[1+MAXARGS+MOREARGS+2];
1167c0b746e5SOllivier Robert 	struct parse pcmd;
1168c0b746e5SOllivier Robert 	int ntok;
11699c2daa00SOllivier Robert 	int i, ti;
11709c2daa00SOllivier Robert 	int rval;
1171c0b746e5SOllivier Robert 	struct xcmd *xcmd;
1172c0b746e5SOllivier Robert 
11739c2daa00SOllivier Robert 	ai_fam_templ = ai_fam_default;
1174c0b746e5SOllivier Robert 	/*
1175c0b746e5SOllivier Robert 	 * Tokenize the command line.  If nothing on it, return.
1176c0b746e5SOllivier Robert 	 */
11772b15cb3dSCy Schubert 	if (strlen(cmdline) >= MAXLINE) {
11782b15cb3dSCy Schubert 		fprintf(stderr, "***Command ignored, more than %d characters:\n%s\n",
11792b15cb3dSCy Schubert 			MAXLINE - 1, cmdline);
11802b15cb3dSCy Schubert 		return;
11812b15cb3dSCy Schubert 	}
1182c0b746e5SOllivier Robert 	tokenize(cmdline, tokens, &ntok);
1183c0b746e5SOllivier Robert 	if (ntok == 0)
1184c0b746e5SOllivier Robert 	    return;
1185c0b746e5SOllivier Robert 
1186c0b746e5SOllivier Robert 	/*
1187c0b746e5SOllivier Robert 	 * Find the appropriate command description.
1188c0b746e5SOllivier Robert 	 */
1189c0b746e5SOllivier Robert 	i = findcmd(tokens[0], builtins, opcmds, &xcmd);
1190c0b746e5SOllivier Robert 	if (i == 0) {
1191c0b746e5SOllivier Robert 		(void) fprintf(stderr, "***Command `%s' unknown\n",
1192c0b746e5SOllivier Robert 			       tokens[0]);
1193c0b746e5SOllivier Robert 		return;
1194c0b746e5SOllivier Robert 	} else if (i >= 2) {
1195c0b746e5SOllivier Robert 		(void) fprintf(stderr, "***Command `%s' ambiguous\n",
1196c0b746e5SOllivier Robert 			       tokens[0]);
1197c0b746e5SOllivier Robert 		return;
1198c0b746e5SOllivier Robert 	}
1199c0b746e5SOllivier Robert 
1200c0b746e5SOllivier Robert 	/*
1201c0b746e5SOllivier Robert 	 * Save the keyword, then walk through the arguments, interpreting
1202c0b746e5SOllivier Robert 	 * as we go.
1203c0b746e5SOllivier Robert 	 */
1204c0b746e5SOllivier Robert 	pcmd.keyword = tokens[0];
1205c0b746e5SOllivier Robert 	pcmd.nargs = 0;
12069c2daa00SOllivier Robert 	ti = 1;
12079c2daa00SOllivier Robert 	for (i = 0; i < MAXARGS && xcmd->arg[i] != NO;) {
12089c2daa00SOllivier Robert 		if ((i+ti) >= ntok) {
1209c0b746e5SOllivier Robert 			if (!(xcmd->arg[i] & OPT)) {
1210c0b746e5SOllivier Robert 				printusage(xcmd, stderr);
1211c0b746e5SOllivier Robert 				return;
1212c0b746e5SOllivier Robert 			}
1213c0b746e5SOllivier Robert 			break;
1214c0b746e5SOllivier Robert 		}
12159c2daa00SOllivier Robert 		if ((xcmd->arg[i] & OPT) && (*tokens[i+ti] == '>'))
1216c0b746e5SOllivier Robert 			break;
12179c2daa00SOllivier Robert 		rval = getarg(tokens[i+ti], (int)xcmd->arg[i], &pcmd.argval[i]);
12189c2daa00SOllivier Robert 		if (rval == -1) {
12199c2daa00SOllivier Robert 			ti++;
12209c2daa00SOllivier Robert 			continue;
12219c2daa00SOllivier Robert 		}
12229c2daa00SOllivier Robert 		if (rval == 0)
1223c0b746e5SOllivier Robert 			return;
1224c0b746e5SOllivier Robert 		pcmd.nargs++;
12259c2daa00SOllivier Robert 		i++;
1226c0b746e5SOllivier Robert 	}
1227c0b746e5SOllivier Robert 
1228ea906c41SOllivier Robert 	/* Any extra args are assumed to be "OPT|NTP_STR". */
1229ea906c41SOllivier Robert 	for ( ; i < MAXARGS + MOREARGS;) {
1230ea906c41SOllivier Robert 	     if ((i+ti) >= ntok)
1231ea906c41SOllivier Robert 		  break;
1232ea906c41SOllivier Robert 		rval = getarg(tokens[i+ti], (int)(OPT|NTP_STR), &pcmd.argval[i]);
1233ea906c41SOllivier Robert 		if (rval == -1) {
1234ea906c41SOllivier Robert 			ti++;
1235ea906c41SOllivier Robert 			continue;
1236ea906c41SOllivier Robert 		}
1237ea906c41SOllivier Robert 		if (rval == 0)
1238ea906c41SOllivier Robert 			return;
1239ea906c41SOllivier Robert 		pcmd.nargs++;
1240ea906c41SOllivier Robert 		i++;
1241ea906c41SOllivier Robert 	}
1242ea906c41SOllivier Robert 
12439c2daa00SOllivier Robert 	i += ti;
1244c0b746e5SOllivier Robert 	if (i < ntok && *tokens[i] == '>') {
1245c0b746e5SOllivier Robert 		char *fname;
1246c0b746e5SOllivier Robert 
1247c0b746e5SOllivier Robert 		if (*(tokens[i]+1) != '\0')
1248c0b746e5SOllivier Robert 		    fname = tokens[i]+1;
1249c0b746e5SOllivier Robert 		else if ((i+1) < ntok)
1250c0b746e5SOllivier Robert 		    fname = tokens[i+1];
1251c0b746e5SOllivier Robert 		else {
1252c0b746e5SOllivier Robert 			(void) fprintf(stderr, "***No file for redirect\n");
1253c0b746e5SOllivier Robert 			return;
1254c0b746e5SOllivier Robert 		}
1255c0b746e5SOllivier Robert 
1256c0b746e5SOllivier Robert 		current_output = fopen(fname, "w");
1257c0b746e5SOllivier Robert 		if (current_output == NULL) {
1258c0b746e5SOllivier Robert 			(void) fprintf(stderr, "***Error opening %s: ", fname);
1259c0b746e5SOllivier Robert 			perror("");
1260c0b746e5SOllivier Robert 			return;
1261c0b746e5SOllivier Robert 		}
1262c0b746e5SOllivier Robert 	} else {
1263c0b746e5SOllivier Robert 		current_output = stdout;
1264c0b746e5SOllivier Robert 	}
1265c0b746e5SOllivier Robert 
12664e1ef62aSXin LI 	if (interactive) {
12674e1ef62aSXin LI 		if ( ! SETJMP(interrupt_buf)) {
1268c0b746e5SOllivier Robert 			jump = 1;
1269c0b746e5SOllivier Robert 			(xcmd->handler)(&pcmd, current_output);
1270c0b746e5SOllivier Robert 			jump = 0;
12714e1ef62aSXin LI 		} else {
12724e1ef62aSXin LI 			fflush(current_output);
12734e1ef62aSXin LI 			fputs("\n >>> command aborted <<<\n", stderr);
12744e1ef62aSXin LI 			fflush(stderr);
12754e1ef62aSXin LI 		}
12764e1ef62aSXin LI 	} else {
12774e1ef62aSXin LI 		jump = 0;
12784e1ef62aSXin LI 		(xcmd->handler)(&pcmd, current_output);
12794e1ef62aSXin LI 	}
12804e1ef62aSXin LI 	if ((NULL != current_output) && (stdout != current_output)) {
12819c2daa00SOllivier Robert 		(void)fclose(current_output);
12829c2daa00SOllivier Robert 		current_output = NULL;
1283c0b746e5SOllivier Robert 	}
1284c0b746e5SOllivier Robert }
1285c0b746e5SOllivier Robert 
1286c0b746e5SOllivier Robert 
1287c0b746e5SOllivier Robert /*
1288c0b746e5SOllivier Robert  * tokenize - turn a command line into tokens
1289c0b746e5SOllivier Robert  */
1290c0b746e5SOllivier Robert static void
tokenize(const char * line,char ** tokens,int * ntok)1291c0b746e5SOllivier Robert tokenize(
1292c0b746e5SOllivier Robert 	const char *line,
1293c0b746e5SOllivier Robert 	char **tokens,
1294c0b746e5SOllivier Robert 	int *ntok
1295c0b746e5SOllivier Robert 	)
1296c0b746e5SOllivier Robert {
1297c0b746e5SOllivier Robert 	register const char *cp;
1298c0b746e5SOllivier Robert 	register char *sp;
1299c0b746e5SOllivier Robert 	static char tspace[MAXLINE];
1300c0b746e5SOllivier Robert 
1301c0b746e5SOllivier Robert 	sp = tspace;
1302c0b746e5SOllivier Robert 	cp = line;
1303c0b746e5SOllivier Robert 	for (*ntok = 0; *ntok < MAXTOKENS; (*ntok)++) {
1304c0b746e5SOllivier Robert 		tokens[*ntok] = sp;
1305c0b746e5SOllivier Robert 		while (ISSPACE(*cp))
1306c0b746e5SOllivier Robert 		    cp++;
1307c0b746e5SOllivier Robert 		if (ISEOL(*cp))
1308c0b746e5SOllivier Robert 		    break;
1309c0b746e5SOllivier Robert 		do {
1310c0b746e5SOllivier Robert 			*sp++ = *cp++;
1311c0b746e5SOllivier Robert 		} while (!ISSPACE(*cp) && !ISEOL(*cp));
1312c0b746e5SOllivier Robert 
1313c0b746e5SOllivier Robert 		*sp++ = '\0';
1314c0b746e5SOllivier Robert 	}
1315c0b746e5SOllivier Robert }
1316c0b746e5SOllivier Robert 
1317c0b746e5SOllivier Robert 
1318c0b746e5SOllivier Robert 
1319c0b746e5SOllivier Robert /*
1320c0b746e5SOllivier Robert  * findcmd - find a command in a command description table
1321c0b746e5SOllivier Robert  */
1322c0b746e5SOllivier Robert static int
findcmd(register char * str,struct xcmd * clist1,struct xcmd * clist2,struct xcmd ** cmd)1323c0b746e5SOllivier Robert findcmd(
1324c0b746e5SOllivier Robert 	register char *str,
1325c0b746e5SOllivier Robert 	struct xcmd *clist1,
1326c0b746e5SOllivier Robert 	struct xcmd *clist2,
1327c0b746e5SOllivier Robert 	struct xcmd **cmd
1328c0b746e5SOllivier Robert 	)
1329c0b746e5SOllivier Robert {
1330c0b746e5SOllivier Robert 	register struct xcmd *cl;
13313311ff84SXin LI 	size_t clen;
1332c0b746e5SOllivier Robert 	int nmatch;
1333c0b746e5SOllivier Robert 	struct xcmd *nearmatch = NULL;
1334c0b746e5SOllivier Robert 	struct xcmd *clist;
1335c0b746e5SOllivier Robert 
1336c0b746e5SOllivier Robert 	clen = strlen(str);
1337c0b746e5SOllivier Robert 	nmatch = 0;
1338c0b746e5SOllivier Robert 	if (clist1 != 0)
1339c0b746e5SOllivier Robert 	    clist = clist1;
1340c0b746e5SOllivier Robert 	else if (clist2 != 0)
1341c0b746e5SOllivier Robert 	    clist = clist2;
1342c0b746e5SOllivier Robert 	else
1343c0b746e5SOllivier Robert 	    return 0;
1344c0b746e5SOllivier Robert 
1345c0b746e5SOllivier Robert     again:
1346c0b746e5SOllivier Robert 	for (cl = clist; cl->keyword != 0; cl++) {
1347c0b746e5SOllivier Robert 		/* do a first character check, for efficiency */
1348c0b746e5SOllivier Robert 		if (*str != *(cl->keyword))
1349c0b746e5SOllivier Robert 		    continue;
1350c0b746e5SOllivier Robert 		if (strncmp(str, cl->keyword, (unsigned)clen) == 0) {
1351c0b746e5SOllivier Robert 			/*
1352c0b746e5SOllivier Robert 			 * Could be extact match, could be approximate.
1353c0b746e5SOllivier Robert 			 * Is exact if the length of the keyword is the
1354c0b746e5SOllivier Robert 			 * same as the str.
1355c0b746e5SOllivier Robert 			 */
1356c0b746e5SOllivier Robert 			if (*((cl->keyword) + clen) == '\0') {
1357c0b746e5SOllivier Robert 				*cmd = cl;
1358c0b746e5SOllivier Robert 				return 1;
1359c0b746e5SOllivier Robert 			}
1360c0b746e5SOllivier Robert 			nmatch++;
1361c0b746e5SOllivier Robert 			nearmatch = cl;
1362c0b746e5SOllivier Robert 		}
1363c0b746e5SOllivier Robert 	}
1364c0b746e5SOllivier Robert 
1365c0b746e5SOllivier Robert 				/*
1366c0b746e5SOllivier Robert 				 * See if there is more to do.  If so, go again.  Sorry about the
1367c0b746e5SOllivier Robert 				 * goto, too much looking at BSD sources...
1368c0b746e5SOllivier Robert 				 */
1369c0b746e5SOllivier Robert 	if (clist == clist1 && clist2 != 0) {
1370c0b746e5SOllivier Robert 		clist = clist2;
1371c0b746e5SOllivier Robert 		goto again;
1372c0b746e5SOllivier Robert 	}
1373c0b746e5SOllivier Robert 
1374c0b746e5SOllivier Robert 				/*
1375c0b746e5SOllivier Robert 				 * If we got extactly 1 near match, use it, else return number
1376c0b746e5SOllivier Robert 				 * of matches.
1377c0b746e5SOllivier Robert 				 */
1378c0b746e5SOllivier Robert 	if (nmatch == 1) {
1379c0b746e5SOllivier Robert 		*cmd = nearmatch;
1380c0b746e5SOllivier Robert 		return 1;
1381c0b746e5SOllivier Robert 	}
1382c0b746e5SOllivier Robert 	return nmatch;
1383c0b746e5SOllivier Robert }
1384c0b746e5SOllivier Robert 
1385c0b746e5SOllivier Robert 
1386c0b746e5SOllivier Robert /*
1387c0b746e5SOllivier Robert  * getarg - interpret an argument token
13889c2daa00SOllivier Robert  *
1389ea906c41SOllivier Robert  * string is always set.
1390ea906c41SOllivier Robert  * type is set to the decoded type.
1391ea906c41SOllivier Robert  *
13929c2daa00SOllivier Robert  * return:	 0 - failure
13939c2daa00SOllivier Robert  *		 1 - success
13949c2daa00SOllivier Robert  *		-1 - skip to next token
1395c0b746e5SOllivier Robert  */
1396c0b746e5SOllivier Robert static int
getarg(char * str,int code,arg_v * argp)1397c0b746e5SOllivier Robert getarg(
1398c0b746e5SOllivier Robert 	char *str,
1399c0b746e5SOllivier Robert 	int code,
1400c0b746e5SOllivier Robert 	arg_v *argp
1401c0b746e5SOllivier Robert 	)
1402c0b746e5SOllivier Robert {
14032b15cb3dSCy Schubert 	ZERO(*argp);
1404c0b746e5SOllivier Robert 	argp->string = str;
1405ea906c41SOllivier Robert 	argp->type   = code & ~OPT;
1406ea906c41SOllivier Robert 
1407ea906c41SOllivier Robert 	switch (argp->type) {
1408ea906c41SOllivier Robert 	case NTP_STR:
1409c0b746e5SOllivier Robert 		break;
1410ea906c41SOllivier Robert 	case NTP_ADD:
14119c2daa00SOllivier Robert 		if (!strcmp("-6", str)) {
14129c2daa00SOllivier Robert 			ai_fam_templ = AF_INET6;
14139c2daa00SOllivier Robert 			return -1;
14149c2daa00SOllivier Robert 		} else if (!strcmp("-4", str)) {
14159c2daa00SOllivier Robert 			ai_fam_templ = AF_INET;
14169c2daa00SOllivier Robert 			return -1;
14179c2daa00SOllivier Robert 		}
14182d4e511cSCy Schubert 		if (!getnetnum(str, &(argp->netnum), (char *)0, ai_fam_templ)) {
14192d4e511cSCy Schubert 			return 0;
14202d4e511cSCy Schubert 		}
14212d4e511cSCy Schubert 		break;
14222d4e511cSCy Schubert 	case NTP_UINT:
14232d4e511cSCy Schubert 		if (!atouint(str, &argp->uval)) {
14242d4e511cSCy Schubert 			fprintf(stderr, "***Illegal unsigned value %s\n",
14252d4e511cSCy Schubert 				str);
1426c0b746e5SOllivier Robert 			return 0;
1427c0b746e5SOllivier Robert 		}
1428c0b746e5SOllivier Robert 		break;
1429ea906c41SOllivier Robert 	case NTP_INT:
14302d4e511cSCy Schubert 		if (!atoint(str, &argp->ival)) {
14312d4e511cSCy Schubert 			fprintf(stderr, "***Illegal integer value %s\n",
14322d4e511cSCy Schubert 				str);
1433c0b746e5SOllivier Robert 			return 0;
1434c0b746e5SOllivier Robert 		}
1435c0b746e5SOllivier Robert 		break;
14369c2daa00SOllivier Robert 	case IP_VERSION:
14379c2daa00SOllivier Robert 		if (!strcmp("-6", str))
14389c2daa00SOllivier Robert 			argp->ival = 6 ;
14399c2daa00SOllivier Robert 		else if (!strcmp("-4", str))
14409c2daa00SOllivier Robert 			argp->ival = 4 ;
14419c2daa00SOllivier Robert 		else {
14429c2daa00SOllivier Robert 			(void) fprintf(stderr,
14439c2daa00SOllivier Robert 			    "***Version must be either 4 or 6\n");
14449c2daa00SOllivier Robert 			return 0;
14459c2daa00SOllivier Robert 		}
14469c2daa00SOllivier Robert 		break;
1447c0b746e5SOllivier Robert 	}
1448c0b746e5SOllivier Robert 
1449c0b746e5SOllivier Robert 	return 1;
1450c0b746e5SOllivier Robert }
1451c0b746e5SOllivier Robert 
1452c0b746e5SOllivier Robert 
1453c0b746e5SOllivier Robert /*
1454c0b746e5SOllivier Robert  * getnetnum - given a host name, return its net number
1455c0b746e5SOllivier Robert  *	       and (optional) full name
1456c0b746e5SOllivier Robert  */
1457c0b746e5SOllivier Robert static int
getnetnum(const char * hname,sockaddr_u * num,char * fullhost,int af)1458c0b746e5SOllivier Robert getnetnum(
1459c0b746e5SOllivier Robert 	const char *hname,
14602b15cb3dSCy Schubert 	sockaddr_u *num,
14619c2daa00SOllivier Robert 	char *fullhost,
14629c2daa00SOllivier Robert 	int af
1463c0b746e5SOllivier Robert 	)
1464c0b746e5SOllivier Robert {
14659c2daa00SOllivier Robert 	struct addrinfo hints, *ai = NULL;
1466c0b746e5SOllivier Robert 
14672b15cb3dSCy Schubert 	ZERO(hints);
14682d4e511cSCy Schubert 	hints.ai_family = af;
14699c2daa00SOllivier Robert 	hints.ai_flags = AI_CANONNAME;
14709c2daa00SOllivier Robert #ifdef AI_ADDRCONFIG
14719c2daa00SOllivier Robert 	hints.ai_flags |= AI_ADDRCONFIG;
14729c2daa00SOllivier Robert #endif
14739c2daa00SOllivier Robert 
14742b15cb3dSCy Schubert 	/*
14752b15cb3dSCy Schubert 	 * decodenetnum only works with addresses, but handles syntax
14762b15cb3dSCy Schubert 	 * that getaddrinfo doesn't:  [2001::1]:1234
14772b15cb3dSCy Schubert 	 */
1478c0b746e5SOllivier Robert 	if (decodenetnum(hname, num)) {
14792b15cb3dSCy Schubert 		if (fullhost != NULL)
14802b15cb3dSCy Schubert 			getnameinfo(&num->sa, SOCKLEN(num), fullhost,
14812b15cb3dSCy Schubert 				    LENHOSTNAME, NULL, 0, 0);
1482c0b746e5SOllivier Robert 		return 1;
1483ea906c41SOllivier Robert 	} else if (getaddrinfo(hname, "ntp", &hints, &ai) == 0) {
14849034852cSGleb Smirnoff 		INSIST(sizeof(*num) >= ai->ai_addrlen);
14852b15cb3dSCy Schubert 		memcpy(num, ai->ai_addr, ai->ai_addrlen);
14862b15cb3dSCy Schubert 		if (fullhost != NULL) {
14872b15cb3dSCy Schubert 			if (ai->ai_canonname != NULL)
14882b15cb3dSCy Schubert 				strlcpy(fullhost, ai->ai_canonname,
14892b15cb3dSCy Schubert 					LENHOSTNAME);
14902b15cb3dSCy Schubert 			else
14912b15cb3dSCy Schubert 				getnameinfo(&num->sa, SOCKLEN(num),
14922b15cb3dSCy Schubert 					    fullhost, LENHOSTNAME, NULL,
14932b15cb3dSCy Schubert 					    0, 0);
14942b15cb3dSCy Schubert 		}
1495c0b746e5SOllivier Robert 		return 1;
14962b15cb3dSCy Schubert 	}
14972b15cb3dSCy Schubert 	fprintf(stderr, "***Can't find host %s\n", hname);
14982b15cb3dSCy Schubert 
1499c0b746e5SOllivier Robert 	return 0;
1500c0b746e5SOllivier Robert }
15012b15cb3dSCy Schubert 
1502c0b746e5SOllivier Robert 
1503c0b746e5SOllivier Robert /*
1504c0b746e5SOllivier Robert  * nntohost - convert network number to host name.  This routine enforces
1505c0b746e5SOllivier Robert  *	       the showhostnames setting.
1506c0b746e5SOllivier Robert  */
15072b15cb3dSCy Schubert const char *
nntohost(sockaddr_u * netnum)1508c0b746e5SOllivier Robert nntohost(
15092b15cb3dSCy Schubert 	sockaddr_u *netnum
1510c0b746e5SOllivier Robert 	)
1511c0b746e5SOllivier Robert {
15122b15cb3dSCy Schubert 	if (!showhostnames || SOCK_UNSPEC(netnum))
15139c2daa00SOllivier Robert 		return stoa(netnum);
15142b15cb3dSCy Schubert 	else if (ISREFCLOCKADR(netnum))
1515c0b746e5SOllivier Robert 		return refnumtoa(netnum);
15162b15cb3dSCy Schubert 	else
15179c2daa00SOllivier Robert 		return socktohost(netnum);
1518c0b746e5SOllivier Robert }
1519c0b746e5SOllivier Robert 
1520c0b746e5SOllivier Robert 
1521c0b746e5SOllivier Robert /*
1522c0b746e5SOllivier Robert  * Finally, the built in command handlers
1523c0b746e5SOllivier Robert  */
1524c0b746e5SOllivier Robert 
1525c0b746e5SOllivier Robert /*
1526c0b746e5SOllivier Robert  * help - tell about commands, or details of a particular command
1527c0b746e5SOllivier Robert  */
1528c0b746e5SOllivier Robert static void
help(struct parse * pcmd,FILE * fp)1529c0b746e5SOllivier Robert help(
1530c0b746e5SOllivier Robert 	struct parse *pcmd,
1531c0b746e5SOllivier Robert 	FILE *fp
1532c0b746e5SOllivier Robert 	)
1533c0b746e5SOllivier Robert {
1534c0b746e5SOllivier Robert 	struct xcmd *xcp;
1535c0b746e5SOllivier Robert 	char *cmd;
1536ea906c41SOllivier Robert 	const char *list[100];
15372b15cb3dSCy Schubert 	size_t word, words;
15382b15cb3dSCy Schubert 	size_t row, rows;
15392b15cb3dSCy Schubert 	size_t col, cols;
15402b15cb3dSCy Schubert 	size_t length;
1541c0b746e5SOllivier Robert 
1542c0b746e5SOllivier Robert 	if (pcmd->nargs == 0) {
1543ea906c41SOllivier Robert 		words = 0;
1544c0b746e5SOllivier Robert 		for (xcp = builtins; xcp->keyword != 0; xcp++) {
1545c0b746e5SOllivier Robert 			if (*(xcp->keyword) != '?')
1546ea906c41SOllivier Robert 				list[words++] = xcp->keyword;
1547c0b746e5SOllivier Robert 		}
1548c0b746e5SOllivier Robert 		for (xcp = opcmds; xcp->keyword != 0; xcp++)
1549ea906c41SOllivier Robert 			list[words++] = xcp->keyword;
1550c0b746e5SOllivier Robert 
15512b15cb3dSCy Schubert 		qsort((void *)list, words, sizeof(list[0]), helpsort);
1552ea906c41SOllivier Robert 		col = 0;
1553ea906c41SOllivier Robert 		for (word = 0; word < words; word++) {
15542b15cb3dSCy Schubert 			length = strlen(list[word]);
15552b15cb3dSCy Schubert 			col = max(col, length);
1556ea906c41SOllivier Robert 		}
1557c0b746e5SOllivier Robert 
1558ea906c41SOllivier Robert 		cols = SCREENWIDTH / ++col;
1559ea906c41SOllivier Robert 		rows = (words + cols - 1) / cols;
1560ea906c41SOllivier Robert 
15612b15cb3dSCy Schubert 		fprintf(fp, "ntpdc commands:\n");
1562ea906c41SOllivier Robert 
1563ea906c41SOllivier Robert 		for (row = 0; row < rows; row++) {
15642b15cb3dSCy Schubert 			for (word = row; word < words; word += rows)
15652b15cb3dSCy Schubert 				fprintf(fp, "%-*.*s", (int)col,
15662b15cb3dSCy Schubert 					(int)col - 1, list[word]);
15672b15cb3dSCy Schubert 			fprintf(fp, "\n");
1568c0b746e5SOllivier Robert 		}
1569c0b746e5SOllivier Robert 	} else {
1570c0b746e5SOllivier Robert 		cmd = pcmd->argval[0].string;
1571ea906c41SOllivier Robert 		words = findcmd(cmd, builtins, opcmds, &xcp);
1572ea906c41SOllivier Robert 		if (words == 0) {
15732b15cb3dSCy Schubert 			fprintf(stderr,
1574c0b746e5SOllivier Robert 				"Command `%s' is unknown\n", cmd);
1575c0b746e5SOllivier Robert 			return;
1576ea906c41SOllivier Robert 		} else if (words >= 2) {
15772b15cb3dSCy Schubert 			fprintf(stderr,
1578c0b746e5SOllivier Robert 				"Command `%s' is ambiguous\n", cmd);
1579c0b746e5SOllivier Robert 			return;
1580c0b746e5SOllivier Robert 		}
15812b15cb3dSCy Schubert 		fprintf(fp, "function: %s\n", xcp->comment);
1582c0b746e5SOllivier Robert 		printusage(xcp, fp);
1583c0b746e5SOllivier Robert 	}
1584c0b746e5SOllivier Robert }
1585c0b746e5SOllivier Robert 
1586c0b746e5SOllivier Robert 
1587c0b746e5SOllivier Robert /*
1588c0b746e5SOllivier Robert  * helpsort - do hostname qsort comparisons
1589c0b746e5SOllivier Robert  */
1590c0b746e5SOllivier Robert static int
helpsort(const void * t1,const void * t2)1591c0b746e5SOllivier Robert helpsort(
1592c0b746e5SOllivier Robert 	const void *t1,
1593c0b746e5SOllivier Robert 	const void *t2
1594c0b746e5SOllivier Robert 	)
1595c0b746e5SOllivier Robert {
15962b15cb3dSCy Schubert 	const char * const *	name1 = t1;
15972b15cb3dSCy Schubert 	const char * const *	name2 = t2;
1598c0b746e5SOllivier Robert 
1599c0b746e5SOllivier Robert 	return strcmp(*name1, *name2);
1600c0b746e5SOllivier Robert }
1601c0b746e5SOllivier Robert 
1602c0b746e5SOllivier Robert 
1603c0b746e5SOllivier Robert /*
1604c0b746e5SOllivier Robert  * printusage - print usage information for a command
1605c0b746e5SOllivier Robert  */
1606c0b746e5SOllivier Robert static void
printusage(struct xcmd * xcp,FILE * fp)1607c0b746e5SOllivier Robert printusage(
1608c0b746e5SOllivier Robert 	struct xcmd *xcp,
1609c0b746e5SOllivier Robert 	FILE *fp
1610c0b746e5SOllivier Robert 	)
1611c0b746e5SOllivier Robert {
16129c2daa00SOllivier Robert 	int i, opt46;
1613c0b746e5SOllivier Robert 
16149c2daa00SOllivier Robert 	opt46 = 0;
1615c0b746e5SOllivier Robert 	(void) fprintf(fp, "usage: %s", xcp->keyword);
1616c0b746e5SOllivier Robert 	for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) {
1617ea906c41SOllivier Robert 		if (opt46 == 0 && (xcp->arg[i] & ~OPT) == NTP_ADD) {
16189c2daa00SOllivier Robert 			(void) fprintf(fp, " [ -4|-6 ]");
16199c2daa00SOllivier Robert 			opt46 = 1;
16209c2daa00SOllivier Robert 		}
1621c0b746e5SOllivier Robert 		if (xcp->arg[i] & OPT)
1622c0b746e5SOllivier Robert 		    (void) fprintf(fp, " [ %s ]", xcp->desc[i]);
1623c0b746e5SOllivier Robert 		else
1624c0b746e5SOllivier Robert 		    (void) fprintf(fp, " %s", xcp->desc[i]);
1625c0b746e5SOllivier Robert 	}
1626c0b746e5SOllivier Robert 	(void) fprintf(fp, "\n");
1627c0b746e5SOllivier Robert }
1628c0b746e5SOllivier Robert 
1629c0b746e5SOllivier Robert 
1630c0b746e5SOllivier Robert /*
1631c0b746e5SOllivier Robert  * timeout - set time out time
1632c0b746e5SOllivier Robert  */
1633c0b746e5SOllivier Robert static void
timeout(struct parse * pcmd,FILE * fp)1634c0b746e5SOllivier Robert timeout(
1635c0b746e5SOllivier Robert 	struct parse *pcmd,
1636c0b746e5SOllivier Robert 	FILE *fp
1637c0b746e5SOllivier Robert 	)
1638c0b746e5SOllivier Robert {
1639c0b746e5SOllivier Robert 	int val;
1640c0b746e5SOllivier Robert 
1641c0b746e5SOllivier Robert 	if (pcmd->nargs == 0) {
1642c0b746e5SOllivier Robert 		val = tvout.tv_sec * 1000 + tvout.tv_usec / 1000;
1643c0b746e5SOllivier Robert 		(void) fprintf(fp, "primary timeout %d ms\n", val);
1644c0b746e5SOllivier Robert 	} else {
1645c0b746e5SOllivier Robert 		tvout.tv_sec = pcmd->argval[0].uval / 1000;
1646c0b746e5SOllivier Robert 		tvout.tv_usec = (pcmd->argval[0].uval - (tvout.tv_sec * 1000))
1647c0b746e5SOllivier Robert 			* 1000;
1648c0b746e5SOllivier Robert 	}
1649c0b746e5SOllivier Robert }
1650c0b746e5SOllivier Robert 
1651c0b746e5SOllivier Robert 
1652c0b746e5SOllivier Robert /*
1653c0b746e5SOllivier Robert  * my_delay - set delay for auth requests
1654c0b746e5SOllivier Robert  */
1655c0b746e5SOllivier Robert static void
my_delay(struct parse * pcmd,FILE * fp)1656c0b746e5SOllivier Robert my_delay(
1657c0b746e5SOllivier Robert 	struct parse *pcmd,
1658c0b746e5SOllivier Robert 	FILE *fp
1659c0b746e5SOllivier Robert 	)
1660c0b746e5SOllivier Robert {
1661c0b746e5SOllivier Robert 	int isneg;
1662c0b746e5SOllivier Robert 	u_long val;
1663c0b746e5SOllivier Robert 
1664c0b746e5SOllivier Robert 	if (pcmd->nargs == 0) {
1665c0b746e5SOllivier Robert 		val = delay_time.l_ui * 1000 + delay_time.l_uf / 4294967;
1666c0b746e5SOllivier Robert 		(void) fprintf(fp, "delay %lu ms\n", val);
1667c0b746e5SOllivier Robert 	} else {
1668c0b746e5SOllivier Robert 		if (pcmd->argval[0].ival < 0) {
1669c0b746e5SOllivier Robert 			isneg = 1;
16702d4e511cSCy Schubert 			val = ~(u_long)(pcmd->argval[0].ival) + 1UL;
1671c0b746e5SOllivier Robert 		} else {
1672c0b746e5SOllivier Robert 			isneg = 0;
1673c0b746e5SOllivier Robert 			val = (u_long)pcmd->argval[0].ival;
1674c0b746e5SOllivier Robert 		}
1675c0b746e5SOllivier Robert 
1676c0b746e5SOllivier Robert 		delay_time.l_ui = val / 1000;
1677c0b746e5SOllivier Robert 		val %= 1000;
1678c0b746e5SOllivier Robert 		delay_time.l_uf = val * 4294967;	/* 2**32/1000 */
1679c0b746e5SOllivier Robert 
1680c0b746e5SOllivier Robert 		if (isneg)
1681c0b746e5SOllivier Robert 		    L_NEG(&delay_time);
1682c0b746e5SOllivier Robert 	}
1683c0b746e5SOllivier Robert }
1684c0b746e5SOllivier Robert 
1685c0b746e5SOllivier Robert 
1686c0b746e5SOllivier Robert /*
1687c0b746e5SOllivier Robert  * host - set the host we are dealing with.
1688c0b746e5SOllivier Robert  */
1689c0b746e5SOllivier Robert static void
host(struct parse * pcmd,FILE * fp)1690c0b746e5SOllivier Robert host(
1691c0b746e5SOllivier Robert 	struct parse *pcmd,
1692c0b746e5SOllivier Robert 	FILE *fp
1693c0b746e5SOllivier Robert 	)
1694c0b746e5SOllivier Robert {
16959c2daa00SOllivier Robert 	int i;
16969c2daa00SOllivier Robert 
1697c0b746e5SOllivier Robert 	if (pcmd->nargs == 0) {
1698c0b746e5SOllivier Robert 		if (havehost)
1699c0b746e5SOllivier Robert 		    (void) fprintf(fp, "current host is %s\n", currenthost);
1700c0b746e5SOllivier Robert 		else
1701c0b746e5SOllivier Robert 		    (void) fprintf(fp, "no current host\n");
17029c2daa00SOllivier Robert 		return;
17039c2daa00SOllivier Robert 	}
17049c2daa00SOllivier Robert 
17059c2daa00SOllivier Robert 	i = 0;
17069c2daa00SOllivier Robert 	if (pcmd->nargs == 2) {
17079c2daa00SOllivier Robert 		if (!strcmp("-4", pcmd->argval[i].string))
17089c2daa00SOllivier Robert 			ai_fam_templ = AF_INET;
17099c2daa00SOllivier Robert 		else if (!strcmp("-6", pcmd->argval[i].string))
17109c2daa00SOllivier Robert 			ai_fam_templ = AF_INET6;
17119c2daa00SOllivier Robert 		else {
17129c2daa00SOllivier Robert 			if (havehost)
17139c2daa00SOllivier Robert 				(void) fprintf(fp,
17149c2daa00SOllivier Robert 				    "current host remains %s\n", currenthost);
17159c2daa00SOllivier Robert 			else
17169c2daa00SOllivier Robert 				(void) fprintf(fp, "still no current host\n");
17179c2daa00SOllivier Robert 			return;
17189c2daa00SOllivier Robert 		}
17199c2daa00SOllivier Robert 		i = 1;
17209c2daa00SOllivier Robert 	}
17219c2daa00SOllivier Robert 	if (openhost(pcmd->argval[i].string)) {
1722c0b746e5SOllivier Robert 		(void) fprintf(fp, "current host set to %s\n", currenthost);
1723c0b746e5SOllivier Robert 	} else {
1724c0b746e5SOllivier Robert 		if (havehost)
1725c0b746e5SOllivier Robert 		    (void) fprintf(fp,
1726c0b746e5SOllivier Robert 				   "current host remains %s\n", currenthost);
1727c0b746e5SOllivier Robert 		else
1728c0b746e5SOllivier Robert 		    (void) fprintf(fp, "still no current host\n");
1729c0b746e5SOllivier Robert 	}
1730c0b746e5SOllivier Robert }
1731c0b746e5SOllivier Robert 
1732c0b746e5SOllivier Robert 
1733c0b746e5SOllivier Robert /*
1734c0b746e5SOllivier Robert  * keyid - get a keyid to use for authenticating requests
1735c0b746e5SOllivier Robert  */
1736c0b746e5SOllivier Robert static void
keyid(struct parse * pcmd,FILE * fp)1737c0b746e5SOllivier Robert keyid(
1738c0b746e5SOllivier Robert 	struct parse *pcmd,
1739c0b746e5SOllivier Robert 	FILE *fp
1740c0b746e5SOllivier Robert 	)
1741c0b746e5SOllivier Robert {
1742c0b746e5SOllivier Robert 	if (pcmd->nargs == 0) {
1743ea906c41SOllivier Robert 		if (info_auth_keyid == 0 && !keyid_entered)
1744c0b746e5SOllivier Robert 		    (void) fprintf(fp, "no keyid defined\n");
1745ea906c41SOllivier Robert 		else if (info_auth_keyid == 0 && keyid_entered)
1746ea906c41SOllivier Robert 		    (void) fprintf(fp, "no keyid will be sent\n");
1747c0b746e5SOllivier Robert 		else
1748c0b746e5SOllivier Robert 		    (void) fprintf(fp, "keyid is %lu\n", (u_long)info_auth_keyid);
1749c0b746e5SOllivier Robert 	} else {
1750c0b746e5SOllivier Robert 		info_auth_keyid = pcmd->argval[0].uval;
1751ea906c41SOllivier Robert 		keyid_entered = 1;
1752c0b746e5SOllivier Robert 	}
1753c0b746e5SOllivier Robert }
1754c0b746e5SOllivier Robert 
1755c0b746e5SOllivier Robert 
1756c0b746e5SOllivier Robert /*
1757c0b746e5SOllivier Robert  * keytype - get type of key to use for authenticating requests
1758c0b746e5SOllivier Robert  */
1759c0b746e5SOllivier Robert static void
keytype(struct parse * pcmd,FILE * fp)1760c0b746e5SOllivier Robert keytype(
1761c0b746e5SOllivier Robert 	struct parse *pcmd,
1762c0b746e5SOllivier Robert 	FILE *fp
1763c0b746e5SOllivier Robert 	)
1764c0b746e5SOllivier Robert {
17652b15cb3dSCy Schubert 	const char *	digest_name;
17662b15cb3dSCy Schubert 	size_t		digest_len;
17672b15cb3dSCy Schubert 	int		key_type;
1768c0b746e5SOllivier Robert 
17692b15cb3dSCy Schubert 	if (!pcmd->nargs) {
17702b15cb3dSCy Schubert 		fprintf(fp, "keytype is %s with %lu octet digests\n",
17712b15cb3dSCy Schubert 			keytype_name(info_auth_keytype),
17722b15cb3dSCy Schubert 			(u_long)info_auth_hashlen);
17732b15cb3dSCy Schubert 		return;
1774c0b746e5SOllivier Robert 	}
1775c0b746e5SOllivier Robert 
17762b15cb3dSCy Schubert 	digest_name = pcmd->argval[0].string;
17772b15cb3dSCy Schubert 	digest_len = 0;
17782b15cb3dSCy Schubert 	key_type = keytype_from_text(digest_name, &digest_len);
17792b15cb3dSCy Schubert 
17802b15cb3dSCy Schubert 	if (!key_type) {
17812b15cb3dSCy Schubert 		fprintf(fp, "keytype must be 'md5'%s\n",
17822b15cb3dSCy Schubert #ifdef OPENSSL
17832b15cb3dSCy Schubert 			" or a digest type provided by OpenSSL");
17842b15cb3dSCy Schubert #else
17852b15cb3dSCy Schubert 			"");
17862b15cb3dSCy Schubert #endif
17872b15cb3dSCy Schubert 		return;
17882b15cb3dSCy Schubert 	}
17892b15cb3dSCy Schubert 
17902b15cb3dSCy Schubert 	info_auth_keytype = key_type;
17912b15cb3dSCy Schubert 	info_auth_hashlen = digest_len;
17922b15cb3dSCy Schubert }
1793c0b746e5SOllivier Robert 
1794c0b746e5SOllivier Robert 
1795c0b746e5SOllivier Robert /*
1796c0b746e5SOllivier Robert  * passwd - get an authentication key
1797c0b746e5SOllivier Robert  */
1798c0b746e5SOllivier Robert /*ARGSUSED*/
1799c0b746e5SOllivier Robert static void
passwd(struct parse * pcmd,FILE * fp)1800c0b746e5SOllivier Robert passwd(
1801c0b746e5SOllivier Robert 	struct parse *pcmd,
1802c0b746e5SOllivier Robert 	FILE *fp
1803c0b746e5SOllivier Robert 	)
1804c0b746e5SOllivier Robert {
1805c0b746e5SOllivier Robert 	char *pass;
1806c0b746e5SOllivier Robert 
1807c0b746e5SOllivier Robert 	if (info_auth_keyid == 0) {
1808c0b746e5SOllivier Robert 		info_auth_keyid = getkeyid("Keyid: ");
1809c0b746e5SOllivier Robert 		if (info_auth_keyid == 0) {
1810c0b746e5SOllivier Robert 			(void)fprintf(fp, "Keyid must be defined\n");
1811c0b746e5SOllivier Robert 			return;
1812c0b746e5SOllivier Robert 		}
1813c0b746e5SOllivier Robert 	}
18142b15cb3dSCy Schubert 	if (pcmd->nargs >= 1)
18152b15cb3dSCy Schubert 		pass = pcmd->argval[0].string;
1816224ba2bdSOllivier Robert 	else {
18172b15cb3dSCy Schubert 		pass = getpass_keytype(info_auth_keytype);
18182b15cb3dSCy Schubert 		if ('\0' == *pass) {
18192b15cb3dSCy Schubert 			fprintf(fp, "Password unchanged\n");
18202b15cb3dSCy Schubert 			return;
18212b15cb3dSCy Schubert 		}
18222b15cb3dSCy Schubert 	}
18232b15cb3dSCy Schubert 	authusekey(info_auth_keyid, info_auth_keytype, (u_char *)pass);
1824224ba2bdSOllivier Robert 	authtrust(info_auth_keyid, 1);
1825224ba2bdSOllivier Robert }
1826c0b746e5SOllivier Robert 
1827c0b746e5SOllivier Robert 
1828c0b746e5SOllivier Robert /*
1829c0b746e5SOllivier Robert  * hostnames - set the showhostnames flag
1830c0b746e5SOllivier Robert  */
1831c0b746e5SOllivier Robert static void
hostnames(struct parse * pcmd,FILE * fp)1832c0b746e5SOllivier Robert hostnames(
1833c0b746e5SOllivier Robert 	struct parse *pcmd,
1834c0b746e5SOllivier Robert 	FILE *fp
1835c0b746e5SOllivier Robert 	)
1836c0b746e5SOllivier Robert {
1837c0b746e5SOllivier Robert 	if (pcmd->nargs == 0) {
1838c0b746e5SOllivier Robert 		if (showhostnames)
1839c0b746e5SOllivier Robert 		    (void) fprintf(fp, "hostnames being shown\n");
1840c0b746e5SOllivier Robert 		else
1841c0b746e5SOllivier Robert 		    (void) fprintf(fp, "hostnames not being shown\n");
1842c0b746e5SOllivier Robert 	} else {
1843c0b746e5SOllivier Robert 		if (STREQ(pcmd->argval[0].string, "yes"))
1844c0b746e5SOllivier Robert 		    showhostnames = 1;
1845c0b746e5SOllivier Robert 		else if (STREQ(pcmd->argval[0].string, "no"))
1846c0b746e5SOllivier Robert 		    showhostnames = 0;
1847c0b746e5SOllivier Robert 		else
1848c0b746e5SOllivier Robert 		    (void)fprintf(stderr, "What?\n");
1849c0b746e5SOllivier Robert 	}
1850c0b746e5SOllivier Robert }
1851c0b746e5SOllivier Robert 
1852c0b746e5SOllivier Robert 
1853c0b746e5SOllivier Robert /*
1854c0b746e5SOllivier Robert  * setdebug - set/change debugging level
1855c0b746e5SOllivier Robert  */
1856c0b746e5SOllivier Robert static void
setdebug(struct parse * pcmd,FILE * fp)1857c0b746e5SOllivier Robert setdebug(
1858c0b746e5SOllivier Robert 	struct parse *pcmd,
1859c0b746e5SOllivier Robert 	FILE *fp
1860c0b746e5SOllivier Robert 	)
1861c0b746e5SOllivier Robert {
1862c0b746e5SOllivier Robert 	if (pcmd->nargs == 0) {
1863c0b746e5SOllivier Robert 		(void) fprintf(fp, "debug level is %d\n", debug);
1864c0b746e5SOllivier Robert 		return;
1865c0b746e5SOllivier Robert 	} else if (STREQ(pcmd->argval[0].string, "no")) {
1866c0b746e5SOllivier Robert 		debug = 0;
1867c0b746e5SOllivier Robert 	} else if (STREQ(pcmd->argval[0].string, "more")) {
1868c0b746e5SOllivier Robert 		debug++;
1869c0b746e5SOllivier Robert 	} else if (STREQ(pcmd->argval[0].string, "less")) {
1870c0b746e5SOllivier Robert 		debug--;
1871c0b746e5SOllivier Robert 	} else {
1872c0b746e5SOllivier Robert 		(void) fprintf(fp, "What?\n");
1873c0b746e5SOllivier Robert 		return;
1874c0b746e5SOllivier Robert 	}
1875c0b746e5SOllivier Robert 	(void) fprintf(fp, "debug level set to %d\n", debug);
1876c0b746e5SOllivier Robert }
1877c0b746e5SOllivier Robert 
1878c0b746e5SOllivier Robert 
1879c0b746e5SOllivier Robert /*
1880c0b746e5SOllivier Robert  * quit - stop this nonsense
1881c0b746e5SOllivier Robert  */
1882c0b746e5SOllivier Robert /*ARGSUSED*/
1883c0b746e5SOllivier Robert static void
quit(struct parse * pcmd,FILE * fp)1884c0b746e5SOllivier Robert quit(
1885c0b746e5SOllivier Robert 	struct parse *pcmd,
1886c0b746e5SOllivier Robert 	FILE *fp
1887c0b746e5SOllivier Robert 	)
1888c0b746e5SOllivier Robert {
1889c0b746e5SOllivier Robert 	if (havehost)
1890c0b746e5SOllivier Robert 	    closesocket(sockfd);
1891c0b746e5SOllivier Robert 	exit(0);
1892c0b746e5SOllivier Robert }
1893c0b746e5SOllivier Robert 
1894c0b746e5SOllivier Robert 
1895c0b746e5SOllivier Robert /*
1896c0b746e5SOllivier Robert  * version - print the current version number
1897c0b746e5SOllivier Robert  */
1898c0b746e5SOllivier Robert /*ARGSUSED*/
1899c0b746e5SOllivier Robert static void
version(struct parse * pcmd,FILE * fp)1900c0b746e5SOllivier Robert version(
1901c0b746e5SOllivier Robert 	struct parse *pcmd,
1902c0b746e5SOllivier Robert 	FILE *fp
1903c0b746e5SOllivier Robert 	)
1904c0b746e5SOllivier Robert {
1905c0b746e5SOllivier Robert 
1906c0b746e5SOllivier Robert 	(void) fprintf(fp, "%s\n", Version);
1907c0b746e5SOllivier Robert 	return;
1908c0b746e5SOllivier Robert }
1909c0b746e5SOllivier Robert 
1910c0b746e5SOllivier Robert 
19112b15cb3dSCy Schubert static void __attribute__((__format__(__printf__, 1, 0)))
vwarning(const char * fmt,va_list ap)19122b15cb3dSCy Schubert vwarning(const char *fmt, va_list ap)
19132b15cb3dSCy Schubert {
19142b15cb3dSCy Schubert 	int serrno = errno;
19152b15cb3dSCy Schubert 	(void) fprintf(stderr, "%s: ", progname);
19162b15cb3dSCy Schubert 	vfprintf(stderr, fmt, ap);
19172b15cb3dSCy Schubert 	(void) fprintf(stderr, ": %s\n", strerror(serrno));
19182b15cb3dSCy Schubert }
19192b15cb3dSCy Schubert 
1920c0b746e5SOllivier Robert /*
1921c0b746e5SOllivier Robert  * warning - print a warning message
1922c0b746e5SOllivier Robert  */
19232b15cb3dSCy Schubert static void __attribute__((__format__(__printf__, 1, 2)))
warning(const char * fmt,...)1924c0b746e5SOllivier Robert warning(
1925c0b746e5SOllivier Robert 	const char *fmt,
19262b15cb3dSCy Schubert 	...
1927c0b746e5SOllivier Robert 	)
1928c0b746e5SOllivier Robert {
19292b15cb3dSCy Schubert 	va_list ap;
19302b15cb3dSCy Schubert 	va_start(ap, fmt);
19312b15cb3dSCy Schubert 	vwarning(fmt, ap);
19322b15cb3dSCy Schubert 	va_end(ap);
1933c0b746e5SOllivier Robert }
1934c0b746e5SOllivier Robert 
1935c0b746e5SOllivier Robert 
1936c0b746e5SOllivier Robert /*
1937c0b746e5SOllivier Robert  * error - print a message and exit
1938c0b746e5SOllivier Robert  */
19392b15cb3dSCy Schubert static void __attribute__((__format__(__printf__, 1, 2)))
error(const char * fmt,...)1940c0b746e5SOllivier Robert error(
1941c0b746e5SOllivier Robert 	const char *fmt,
19422b15cb3dSCy Schubert 	...
1943c0b746e5SOllivier Robert 	)
1944c0b746e5SOllivier Robert {
19452b15cb3dSCy Schubert 	va_list ap;
19462b15cb3dSCy Schubert 	va_start(ap, fmt);
19472b15cb3dSCy Schubert 	vwarning(fmt, ap);
19482b15cb3dSCy Schubert 	va_end(ap);
1949c0b746e5SOllivier Robert 	exit(1);
1950c0b746e5SOllivier Robert }
1951c0b746e5SOllivier Robert 
1952c0b746e5SOllivier Robert /*
1953c0b746e5SOllivier Robert  * getkeyid - prompt the user for a keyid to use
1954c0b746e5SOllivier Robert  */
1955c0b746e5SOllivier Robert static u_long
getkeyid(const char * keyprompt)1956c0b746e5SOllivier Robert getkeyid(
1957c0b746e5SOllivier Robert 	const char *keyprompt
1958c0b746e5SOllivier Robert 	)
1959c0b746e5SOllivier Robert {
19602b15cb3dSCy Schubert 	int c;
1961c0b746e5SOllivier Robert 	FILE *fi;
1962c0b746e5SOllivier Robert 	char pbuf[20];
19632b15cb3dSCy Schubert 	size_t i;
19642b15cb3dSCy Schubert 	size_t ilim;
1965c0b746e5SOllivier Robert 
1966c0b746e5SOllivier Robert #ifndef SYS_WINNT
1967c0b746e5SOllivier Robert 	if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
1968c0b746e5SOllivier Robert #else
19692b15cb3dSCy Schubert 	if ((fi = _fdopen(open("CONIN$", _O_TEXT), "r")) == NULL)
1970c0b746e5SOllivier Robert #endif /* SYS_WINNT */
1971c0b746e5SOllivier Robert 		fi = stdin;
1972c0b746e5SOllivier Robert 	else
1973c0b746e5SOllivier Robert 		setbuf(fi, (char *)NULL);
1974c0b746e5SOllivier Robert 	fprintf(stderr, "%s", keyprompt); fflush(stderr);
19752b15cb3dSCy Schubert 	for (i = 0, ilim = COUNTOF(pbuf) - 1;
19762b15cb3dSCy Schubert 	     i < ilim && (c = getc(fi)) != '\n' && c != EOF;
19772b15cb3dSCy Schubert 	     )
19782b15cb3dSCy Schubert 		pbuf[i++] = (char)c;
19792b15cb3dSCy Schubert 	pbuf[i] = '\0';
1980c0b746e5SOllivier Robert 	if (fi != stdin)
1981c0b746e5SOllivier Robert 		fclose(fi);
19822b15cb3dSCy Schubert 
19832b15cb3dSCy Schubert 	return (u_long) atoi(pbuf);
1984c0b746e5SOllivier Robert }
1985