ntpq.c revision 1.20 1 1.20 christos /* $NetBSD: ntpq.c,v 1.20 2018/09/29 21:52:34 christos Exp $ */
2 1.1 kardel
3 1.1 kardel /*
4 1.1 kardel * ntpq - query an NTP server using mode 6 commands
5 1.1 kardel */
6 1.9 christos #include <config.h>
7 1.1 kardel #include <ctype.h>
8 1.1 kardel #include <signal.h>
9 1.1 kardel #include <setjmp.h>
10 1.19 christos #include <stddef.h>
11 1.19 christos #include <stdio.h>
12 1.1 kardel #include <sys/types.h>
13 1.1 kardel #include <sys/time.h>
14 1.9 christos #ifdef HAVE_UNISTD_H
15 1.9 christos # include <unistd.h>
16 1.9 christos #endif
17 1.9 christos #ifdef HAVE_FCNTL_H
18 1.9 christos # include <fcntl.h>
19 1.9 christos #endif
20 1.9 christos #ifdef SYS_WINNT
21 1.9 christos # include <mswsock.h>
22 1.9 christos #endif
23 1.9 christos #include <isc/net.h>
24 1.9 christos #include <isc/result.h>
25 1.1 kardel
26 1.1 kardel #include "ntpq.h"
27 1.12 christos #include "ntp_assert.h"
28 1.9 christos #include "ntp_stdlib.h"
29 1.1 kardel #include "ntp_unixtime.h"
30 1.1 kardel #include "ntp_calendar.h"
31 1.1 kardel #include "ntp_select.h"
32 1.1 kardel #include "ntp_assert.h"
33 1.9 christos #include "lib_strbuf.h"
34 1.1 kardel #include "ntp_lineedit.h"
35 1.1 kardel #include "ntp_debug.h"
36 1.9 christos #ifdef OPENSSL
37 1.20 christos # include "openssl/evp.h"
38 1.20 christos # include "openssl/objects.h"
39 1.20 christos # include "openssl/err.h"
40 1.20 christos # ifdef SYS_WINNT
41 1.20 christos # include "openssl/opensslv.h"
42 1.20 christos # if !defined(HAVE_EVP_MD_DO_ALL_SORTED) && OPENSSL_VERSION_NUMBER > 0x10000000L
43 1.20 christos # define HAVE_EVP_MD_DO_ALL_SORTED 1
44 1.20 christos # endif
45 1.20 christos # endif
46 1.20 christos # include "libssl_compat.h"
47 1.20 christos # ifdef HAVE_OPENSSL_CMAC_H
48 1.20 christos # include <openssl/cmac.h>
49 1.20 christos # define CMAC "AES128CMAC"
50 1.19 christos # endif
51 1.19 christos #endif
52 1.1 kardel #include <ssl_applink.c>
53 1.1 kardel
54 1.4 kardel #include "ntp_libopts.h"
55 1.14 christos #include "safecast.h"
56 1.1 kardel
57 1.9 christos #ifdef SYS_VXWORKS /* vxWorks needs mode flag -casey*/
58 1.1 kardel # define open(name, flags) open(name, flags, 0777)
59 1.1 kardel # define SERVER_PORT_NUM 123
60 1.1 kardel #endif
61 1.1 kardel
62 1.1 kardel /* we use COMMAND as an autogen keyword */
63 1.1 kardel #ifdef COMMAND
64 1.1 kardel # undef COMMAND
65 1.1 kardel #endif
66 1.1 kardel
67 1.1 kardel /*
68 1.1 kardel * Because we potentially understand a lot of commands we will run
69 1.1 kardel * interactive if connected to a terminal.
70 1.1 kardel */
71 1.1 kardel int interactive = 0; /* set to 1 when we should prompt */
72 1.1 kardel const char *prompt = "ntpq> "; /* prompt to ask him about */
73 1.1 kardel
74 1.1 kardel /*
75 1.1 kardel * use old readvars behavior? --old-rv processing in ntpq resets
76 1.1 kardel * this value based on the presence or absence of --old-rv. It is
77 1.1 kardel * initialized to 1 here to maintain backward compatibility with
78 1.1 kardel * libntpq clients such as ntpsnmpd, which are free to reset it as
79 1.1 kardel * desired.
80 1.1 kardel */
81 1.1 kardel int old_rv = 1;
82 1.1 kardel
83 1.15 christos /*
84 1.15 christos * How should we display the refid?
85 1.15 christos * REFID_HASH, REFID_IPV4
86 1.15 christos */
87 1.15 christos te_Refid drefid = -1;
88 1.1 kardel
89 1.1 kardel /*
90 1.1 kardel * for get_systime()
91 1.1 kardel */
92 1.1 kardel s_char sys_precision; /* local clock precision (log2 s) */
93 1.1 kardel
94 1.1 kardel /*
95 1.1 kardel * Keyid used for authenticated requests. Obtained on the fly.
96 1.1 kardel */
97 1.1 kardel u_long info_auth_keyid = 0;
98 1.1 kardel
99 1.1 kardel static int info_auth_keytype = NID_md5; /* MD5 */
100 1.1 kardel static size_t info_auth_hashlen = 16; /* MD5 */
101 1.1 kardel u_long current_time; /* needed by authkeys; not used */
102 1.1 kardel
103 1.1 kardel /*
104 1.1 kardel * Flag which indicates we should always send authenticated requests
105 1.1 kardel */
106 1.1 kardel int always_auth = 0;
107 1.1 kardel
108 1.1 kardel /*
109 1.1 kardel * Flag which indicates raw mode output.
110 1.1 kardel */
111 1.1 kardel int rawmode = 0;
112 1.1 kardel
113 1.1 kardel /*
114 1.1 kardel * Packet version number we use
115 1.1 kardel */
116 1.1 kardel u_char pktversion = NTP_OLDVERSION + 1;
117 1.1 kardel
118 1.1 kardel
119 1.1 kardel /*
120 1.1 kardel * Format values
121 1.1 kardel */
122 1.1 kardel #define PADDING 0
123 1.9 christos #define HA 1 /* host address */
124 1.9 christos #define NA 2 /* network address */
125 1.9 christos #define LP 3 /* leap (print in binary) */
126 1.9 christos #define RF 4 /* refid (sometimes string, sometimes not) */
127 1.9 christos #define AR 5 /* array of times */
128 1.9 christos #define FX 6 /* test flags */
129 1.9 christos #define TS 7 /* l_fp timestamp in hex */
130 1.9 christos #define OC 8 /* integer, print in octal */
131 1.1 kardel #define EOV 255 /* end of table */
132 1.1 kardel
133 1.1 kardel /*
134 1.9 christos * For the most part ntpq simply displays what ntpd provides in the
135 1.9 christos * mostly plain-text mode 6 responses. A few variable names are by
136 1.9 christos * default "cooked" to provide more human-friendly output.
137 1.9 christos */
138 1.9 christos const var_format cookedvars[] = {
139 1.9 christos { "leap", LP },
140 1.9 christos { "reach", OC },
141 1.9 christos { "refid", RF },
142 1.9 christos { "reftime", TS },
143 1.9 christos { "clock", TS },
144 1.9 christos { "org", TS },
145 1.9 christos { "rec", TS },
146 1.9 christos { "xmt", TS },
147 1.9 christos { "flash", FX },
148 1.9 christos { "srcadr", HA },
149 1.9 christos { "peeradr", HA }, /* compat with others */
150 1.9 christos { "dstadr", NA },
151 1.9 christos { "filtdelay", AR },
152 1.9 christos { "filtoffset", AR },
153 1.9 christos { "filtdisp", AR },
154 1.9 christos { "filterror", AR }, /* compat with others */
155 1.1 kardel };
156 1.1 kardel
157 1.1 kardel
158 1.1 kardel
159 1.1 kardel /*
160 1.1 kardel * flasher bits
161 1.1 kardel */
162 1.1 kardel static const char *tstflagnames[] = {
163 1.1 kardel "pkt_dup", /* TEST1 */
164 1.1 kardel "pkt_bogus", /* TEST2 */
165 1.1 kardel "pkt_unsync", /* TEST3 */
166 1.1 kardel "pkt_denied", /* TEST4 */
167 1.1 kardel "pkt_auth", /* TEST5 */
168 1.1 kardel "pkt_stratum", /* TEST6 */
169 1.1 kardel "pkt_header", /* TEST7 */
170 1.1 kardel "pkt_autokey", /* TEST8 */
171 1.1 kardel "pkt_crypto", /* TEST9 */
172 1.1 kardel "peer_stratum", /* TEST10 */
173 1.1 kardel "peer_dist", /* TEST11 */
174 1.1 kardel "peer_loop", /* TEST12 */
175 1.1 kardel "peer_unreach" /* TEST13 */
176 1.1 kardel };
177 1.1 kardel
178 1.1 kardel
179 1.1 kardel int ntpqmain (int, char **);
180 1.1 kardel /*
181 1.1 kardel * Built in command handler declarations
182 1.1 kardel */
183 1.9 christos static int openhost (const char *, int);
184 1.9 christos static void dump_hex_printable(const void *, size_t);
185 1.1 kardel static int sendpkt (void *, size_t);
186 1.14 christos static int getresponse (int, int, u_short *, size_t *, const char **, int);
187 1.14 christos static int sendrequest (int, associd_t, int, size_t, const char *);
188 1.1 kardel static char * tstflags (u_long);
189 1.1 kardel #ifndef BUILD_AS_LIB
190 1.1 kardel static void getcmds (void);
191 1.1 kardel #ifndef SYS_WINNT
192 1.14 christos static int abortcmd (void);
193 1.1 kardel #endif /* SYS_WINNT */
194 1.1 kardel static void docmd (const char *);
195 1.1 kardel static void tokenize (const char *, char **, int *);
196 1.9 christos static int getarg (const char *, int, arg_v *);
197 1.1 kardel #endif /* BUILD_AS_LIB */
198 1.9 christos static int findcmd (const char *, struct xcmd *,
199 1.9 christos struct xcmd *, struct xcmd **);
200 1.1 kardel static int rtdatetolfp (char *, l_fp *);
201 1.19 christos static int decodearr (char *, int *, l_fp *, int);
202 1.1 kardel static void help (struct parse *, FILE *);
203 1.1 kardel static int helpsort (const void *, const void *);
204 1.1 kardel static void printusage (struct xcmd *, FILE *);
205 1.1 kardel static void timeout (struct parse *, FILE *);
206 1.1 kardel static void auth_delay (struct parse *, FILE *);
207 1.1 kardel static void host (struct parse *, FILE *);
208 1.1 kardel static void ntp_poll (struct parse *, FILE *);
209 1.1 kardel static void keyid (struct parse *, FILE *);
210 1.1 kardel static void keytype (struct parse *, FILE *);
211 1.1 kardel static void passwd (struct parse *, FILE *);
212 1.1 kardel static void hostnames (struct parse *, FILE *);
213 1.1 kardel static void setdebug (struct parse *, FILE *);
214 1.1 kardel static void quit (struct parse *, FILE *);
215 1.15 christos static void showdrefid (struct parse *, FILE *);
216 1.1 kardel static void version (struct parse *, FILE *);
217 1.1 kardel static void raw (struct parse *, FILE *);
218 1.1 kardel static void cooked (struct parse *, FILE *);
219 1.1 kardel static void authenticate (struct parse *, FILE *);
220 1.1 kardel static void ntpversion (struct parse *, FILE *);
221 1.20 christos static void warning (const char *, ...) NTP_PRINTF(1, 2);
222 1.20 christos static void error (const char *, ...) NTP_PRINTF(1, 2);
223 1.1 kardel static u_long getkeyid (const char *);
224 1.1 kardel static void atoascii (const char *, size_t, char *, size_t);
225 1.14 christos static void cookedprint (int, size_t, const char *, int, int, FILE *);
226 1.14 christos static void rawprint (int, size_t, const char *, int, int, FILE *);
227 1.1 kardel static void startoutput (void);
228 1.9 christos static void output (FILE *, const char *, const char *);
229 1.1 kardel static void endoutput (FILE *);
230 1.1 kardel static void outputarr (FILE *, char *, int, l_fp *);
231 1.1 kardel static int assoccmp (const void *, const void *);
232 1.20 christos u_short varfmt (const char *);
233 1.20 christos void ntpq_custom_opt_handler(tOptions *, tOptDesc *);
234 1.20 christos
235 1.20 christos #ifndef BUILD_AS_LIB
236 1.20 christos static char *list_digest_names(void);
237 1.20 christos static char *insert_cmac (char *list);
238 1.14 christos static void on_ctrlc (void);
239 1.15 christos static int my_easprintf (char**, const char *, ...) NTP_PRINTF(2, 3);
240 1.20 christos # if defined(OPENSSL) && defined(HAVE_EVP_MD_DO_ALL_SORTED)
241 1.20 christos static void list_md_fn (const EVP_MD *m, const char *from,
242 1.20 christos const char *to, void *arg);
243 1.20 christos # endif /* defined(OPENSSL) && defined(HAVE_EVP_MD_DO_ALL_SORTED) */
244 1.20 christos #endif /* !defined(BUILD_AS_LIB) */
245 1.20 christos
246 1.1 kardel
247 1.19 christos /* read a character from memory and expand to integer */
248 1.19 christos static inline int
249 1.19 christos pgetc(
250 1.19 christos const char *cp
251 1.19 christos )
252 1.19 christos {
253 1.19 christos return (int)*(const unsigned char*)cp;
254 1.19 christos }
255 1.19 christos
256 1.19 christos
257 1.1 kardel
258 1.1 kardel /*
259 1.1 kardel * Built-in commands we understand
260 1.1 kardel */
261 1.1 kardel struct xcmd builtins[] = {
262 1.1 kardel { "?", help, { OPT|NTP_STR, NO, NO, NO },
263 1.1 kardel { "command", "", "", "" },
264 1.1 kardel "tell the use and syntax of commands" },
265 1.1 kardel { "help", help, { OPT|NTP_STR, NO, NO, NO },
266 1.1 kardel { "command", "", "", "" },
267 1.1 kardel "tell the use and syntax of commands" },
268 1.1 kardel { "timeout", timeout, { OPT|NTP_UINT, NO, NO, NO },
269 1.1 kardel { "msec", "", "", "" },
270 1.1 kardel "set the primary receive time out" },
271 1.1 kardel { "delay", auth_delay, { OPT|NTP_INT, NO, NO, NO },
272 1.1 kardel { "msec", "", "", "" },
273 1.1 kardel "set the delay added to encryption time stamps" },
274 1.1 kardel { "host", host, { OPT|NTP_STR, OPT|NTP_STR, NO, NO },
275 1.1 kardel { "-4|-6", "hostname", "", "" },
276 1.1 kardel "specify the host whose NTP server we talk to" },
277 1.1 kardel { "poll", ntp_poll, { OPT|NTP_UINT, OPT|NTP_STR, NO, NO },
278 1.1 kardel { "n", "verbose", "", "" },
279 1.1 kardel "poll an NTP server in client mode `n' times" },
280 1.9 christos { "passwd", passwd, { OPT|NTP_STR, NO, NO, NO },
281 1.1 kardel { "", "", "", "" },
282 1.1 kardel "specify a password to use for authenticated requests"},
283 1.1 kardel { "hostnames", hostnames, { OPT|NTP_STR, NO, NO, NO },
284 1.1 kardel { "yes|no", "", "", "" },
285 1.1 kardel "specify whether hostnames or net numbers are printed"},
286 1.1 kardel { "debug", setdebug, { OPT|NTP_STR, NO, NO, NO },
287 1.1 kardel { "no|more|less", "", "", "" },
288 1.1 kardel "set/change debugging level" },
289 1.1 kardel { "quit", quit, { NO, NO, NO, NO },
290 1.1 kardel { "", "", "", "" },
291 1.1 kardel "exit ntpq" },
292 1.1 kardel { "exit", quit, { NO, NO, NO, NO },
293 1.1 kardel { "", "", "", "" },
294 1.1 kardel "exit ntpq" },
295 1.1 kardel { "keyid", keyid, { OPT|NTP_UINT, NO, NO, NO },
296 1.1 kardel { "key#", "", "", "" },
297 1.1 kardel "set keyid to use for authenticated requests" },
298 1.15 christos { "drefid", showdrefid, { OPT|NTP_STR, NO, NO, NO },
299 1.15 christos { "hash|ipv4", "", "", "" },
300 1.15 christos "display refid's as IPv4 or hash" },
301 1.1 kardel { "version", version, { NO, NO, NO, NO },
302 1.1 kardel { "", "", "", "" },
303 1.1 kardel "print version number" },
304 1.1 kardel { "raw", raw, { NO, NO, NO, NO },
305 1.1 kardel { "", "", "", "" },
306 1.1 kardel "do raw mode variable output" },
307 1.1 kardel { "cooked", cooked, { NO, NO, NO, NO },
308 1.1 kardel { "", "", "", "" },
309 1.1 kardel "do cooked mode variable output" },
310 1.1 kardel { "authenticate", authenticate, { OPT|NTP_STR, NO, NO, NO },
311 1.1 kardel { "yes|no", "", "", "" },
312 1.1 kardel "always authenticate requests to this server" },
313 1.1 kardel { "ntpversion", ntpversion, { OPT|NTP_UINT, NO, NO, NO },
314 1.1 kardel { "version number", "", "", "" },
315 1.1 kardel "set the NTP version number to use for requests" },
316 1.1 kardel { "keytype", keytype, { OPT|NTP_STR, NO, NO, NO },
317 1.12 christos { "key type %s", "", "", "" },
318 1.12 christos NULL },
319 1.1 kardel { 0, 0, { NO, NO, NO, NO },
320 1.1 kardel { "", "", "", "" }, "" }
321 1.1 kardel };
322 1.1 kardel
323 1.1 kardel
324 1.1 kardel /*
325 1.1 kardel * Default values we use.
326 1.1 kardel */
327 1.1 kardel #define DEFHOST "localhost" /* default host name */
328 1.9 christos #define DEFTIMEOUT 5 /* wait 5 seconds for 1st pkt */
329 1.9 christos #define DEFSTIMEOUT 3 /* and 3 more for each additional */
330 1.9 christos /*
331 1.9 christos * Requests are automatically retried once, so total timeout with no
332 1.9 christos * response is a bit over 2 * DEFTIMEOUT, or 10 seconds. At the other
333 1.9 christos * extreme, a request eliciting 32 packets of responses each for some
334 1.9 christos * reason nearly DEFSTIMEOUT seconds after the prior in that series,
335 1.9 christos * with a single packet dropped, would take around 32 * DEFSTIMEOUT, or
336 1.9 christos * 93 seconds to fail each of two times, or 186 seconds.
337 1.9 christos * Some commands involve a series of requests, such as "peers" and
338 1.9 christos * "mrulist", so the cumulative timeouts are even longer for those.
339 1.9 christos */
340 1.1 kardel #define DEFDELAY 0x51EB852 /* 20 milliseconds, l_fp fraction */
341 1.1 kardel #define LENHOSTNAME 256 /* host name is 256 characters long */
342 1.1 kardel #define MAXCMDS 100 /* maximum commands on cmd line */
343 1.1 kardel #define MAXHOSTS 200 /* maximum hosts on cmd line */
344 1.1 kardel #define MAXLINE 512 /* maximum line length */
345 1.1 kardel #define MAXTOKENS (1+MAXARGS+2) /* maximum number of usable tokens */
346 1.1 kardel #define MAXVARLEN 256 /* maximum length of a variable name */
347 1.9 christos #define MAXVALLEN 2048 /* maximum length of a variable value */
348 1.1 kardel #define MAXOUTLINE 72 /* maximum length of an output line */
349 1.1 kardel #define SCREENWIDTH 76 /* nominal screen width in columns */
350 1.1 kardel
351 1.1 kardel /*
352 1.1 kardel * Some variables used and manipulated locally
353 1.1 kardel */
354 1.1 kardel struct sock_timeval tvout = { DEFTIMEOUT, 0 }; /* time out for reads */
355 1.1 kardel struct sock_timeval tvsout = { DEFSTIMEOUT, 0 };/* secondary time out */
356 1.1 kardel l_fp delay_time; /* delay time */
357 1.1 kardel char currenthost[LENHOSTNAME]; /* current host name */
358 1.4 kardel int currenthostisnum; /* is prior text from IP? */
359 1.5 kardel struct sockaddr_in hostaddr; /* host address */
360 1.1 kardel int showhostnames = 1; /* show host names by default */
361 1.10 christos int wideremote = 0; /* show wide remote names? */
362 1.1 kardel
363 1.1 kardel int ai_fam_templ; /* address family */
364 1.1 kardel int ai_fam_default; /* default address family */
365 1.1 kardel SOCKET sockfd; /* fd socket is opened on */
366 1.1 kardel int havehost = 0; /* set to 1 when host open */
367 1.1 kardel int s_port = 0;
368 1.1 kardel struct servent *server_entry = NULL; /* server entry for ntp */
369 1.1 kardel
370 1.1 kardel
371 1.1 kardel /*
372 1.1 kardel * Sequence number used for requests. It is incremented before
373 1.1 kardel * it is used.
374 1.1 kardel */
375 1.1 kardel u_short sequence;
376 1.1 kardel
377 1.1 kardel /*
378 1.1 kardel * Holds data returned from queries. Declare buffer long to be sure of
379 1.1 kardel * alignment.
380 1.1 kardel */
381 1.1 kardel #define DATASIZE (MAXFRAGS*480) /* maximum amount of data */
382 1.1 kardel long pktdata[DATASIZE/sizeof(long)];
383 1.1 kardel
384 1.1 kardel /*
385 1.9 christos * assoc_cache[] is a dynamic array which allows references to
386 1.9 christos * associations using &1 ... &N for n associations, avoiding manual
387 1.9 christos * lookup of the current association IDs for a given ntpd. It also
388 1.9 christos * caches the status word for each association, retrieved incidentally.
389 1.9 christos */
390 1.9 christos struct association * assoc_cache;
391 1.9 christos u_int assoc_cache_slots;/* count of allocated array entries */
392 1.9 christos u_int numassoc; /* number of cached associations */
393 1.1 kardel
394 1.1 kardel /*
395 1.1 kardel * For commands typed on the command line (with the -c option)
396 1.1 kardel */
397 1.12 christos size_t numcmds = 0;
398 1.1 kardel const char *ccmds[MAXCMDS];
399 1.1 kardel #define ADDCMD(cp) if (numcmds < MAXCMDS) ccmds[numcmds++] = (cp)
400 1.1 kardel
401 1.1 kardel /*
402 1.1 kardel * When multiple hosts are specified.
403 1.1 kardel */
404 1.1 kardel
405 1.9 christos u_int numhosts;
406 1.9 christos
407 1.9 christos chost chosts[MAXHOSTS];
408 1.9 christos #define ADDHOST(cp) \
409 1.9 christos do { \
410 1.9 christos if (numhosts < MAXHOSTS) { \
411 1.9 christos chosts[numhosts].name = (cp); \
412 1.9 christos chosts[numhosts].fam = ai_fam_templ; \
413 1.9 christos numhosts++; \
414 1.9 christos } \
415 1.9 christos } while (0)
416 1.1 kardel
417 1.1 kardel /*
418 1.1 kardel * Macro definitions we use
419 1.1 kardel */
420 1.1 kardel #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
421 1.1 kardel #define ISEOL(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
422 1.1 kardel #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
423 1.1 kardel
424 1.1 kardel /*
425 1.20 christos * Jump buffer for longjumping back to the command level.
426 1.20 christos *
427 1.20 christos * Since we do this from a signal handler, we use 'sig{set,long}jmp()'
428 1.20 christos * if available. The signal is blocked by default during the excution of
429 1.20 christos * a signal handler, and it is unspecified if '{set,long}jmp()' save and
430 1.20 christos * restore the signal mask. They do on BSD, it depends on the GLIBC
431 1.20 christos * version on Linux, and the gods know what happens on other OSes...
432 1.20 christos *
433 1.20 christos * So we use the 'sig{set,long}jmp()' functions where available, because
434 1.20 christos * for them the semantics are well-defined. If we have to fall back to
435 1.20 christos * '{set,long}jmp()', the CTRL-C handling might be a bit erratic.
436 1.20 christos */
437 1.20 christos #if HAVE_DECL_SIGSETJMP && HAVE_DECL_SIGLONGJMP
438 1.20 christos # define JMP_BUF sigjmp_buf
439 1.20 christos # define SETJMP(x) sigsetjmp((x), 1)
440 1.20 christos # define LONGJMP(x, v) siglongjmp((x),(v))
441 1.20 christos #else
442 1.20 christos # define JMP_BUF jmp_buf
443 1.20 christos # define SETJMP(x) setjmp((x))
444 1.20 christos # define LONGJMP(x, v) longjmp((x),(v))
445 1.20 christos #endif
446 1.20 christos static JMP_BUF interrupt_buf;
447 1.20 christos static volatile int jump = 0;
448 1.1 kardel
449 1.1 kardel /*
450 1.1 kardel * Points at file being currently printed into
451 1.1 kardel */
452 1.20 christos FILE *current_output = NULL;
453 1.1 kardel
454 1.1 kardel /*
455 1.1 kardel * Command table imported from ntpdc_ops.c
456 1.1 kardel */
457 1.1 kardel extern struct xcmd opcmds[];
458 1.1 kardel
459 1.13 christos char const *progname;
460 1.1 kardel
461 1.1 kardel #ifdef NO_MAIN_ALLOWED
462 1.1 kardel #ifndef BUILD_AS_LIB
463 1.1 kardel CALL(ntpq,"ntpq",ntpqmain);
464 1.1 kardel
465 1.1 kardel void clear_globals(void)
466 1.1 kardel {
467 1.1 kardel extern int ntp_optind;
468 1.1 kardel showhostnames = 0; /* don'tshow host names by default */
469 1.1 kardel ntp_optind = 0;
470 1.1 kardel server_entry = NULL; /* server entry for ntp */
471 1.1 kardel havehost = 0; /* set to 1 when host open */
472 1.1 kardel numassoc = 0; /* number of cached associations */
473 1.1 kardel numcmds = 0;
474 1.1 kardel numhosts = 0;
475 1.1 kardel }
476 1.1 kardel #endif /* !BUILD_AS_LIB */
477 1.1 kardel #endif /* NO_MAIN_ALLOWED */
478 1.1 kardel
479 1.1 kardel /*
480 1.1 kardel * main - parse arguments and handle options
481 1.1 kardel */
482 1.1 kardel #ifndef NO_MAIN_ALLOWED
483 1.1 kardel int
484 1.1 kardel main(
485 1.1 kardel int argc,
486 1.1 kardel char *argv[]
487 1.1 kardel )
488 1.1 kardel {
489 1.1 kardel return ntpqmain(argc, argv);
490 1.1 kardel }
491 1.1 kardel #endif
492 1.1 kardel
493 1.19 christos
494 1.1 kardel #ifndef BUILD_AS_LIB
495 1.1 kardel int
496 1.1 kardel ntpqmain(
497 1.1 kardel int argc,
498 1.1 kardel char *argv[]
499 1.1 kardel )
500 1.1 kardel {
501 1.9 christos u_int ihost;
502 1.12 christos size_t icmd;
503 1.9 christos
504 1.1 kardel
505 1.1 kardel #ifdef SYS_VXWORKS
506 1.1 kardel clear_globals();
507 1.1 kardel taskPrioritySet(taskIdSelf(), 100 );
508 1.1 kardel #endif
509 1.1 kardel
510 1.1 kardel delay_time.l_ui = 0;
511 1.1 kardel delay_time.l_uf = DEFDELAY;
512 1.1 kardel
513 1.1 kardel init_lib(); /* sets up ipv4_works, ipv6_works */
514 1.1 kardel ssl_applink();
515 1.9 christos init_auth();
516 1.1 kardel
517 1.1 kardel /* Check to see if we have IPv6. Otherwise default to IPv4 */
518 1.1 kardel if (!ipv6_works)
519 1.1 kardel ai_fam_default = AF_INET;
520 1.1 kardel
521 1.12 christos /* Fixup keytype's help based on available digest names */
522 1.12 christos
523 1.12 christos {
524 1.12 christos char *list;
525 1.12 christos char *msg;
526 1.12 christos
527 1.12 christos list = list_digest_names();
528 1.19 christos
529 1.19 christos for (icmd = 0; icmd < sizeof(builtins)/sizeof(*builtins); icmd++) {
530 1.19 christos if (strcmp("keytype", builtins[icmd].keyword) == 0) {
531 1.12 christos break;
532 1.19 christos }
533 1.12 christos }
534 1.12 christos
535 1.12 christos /* CID: 1295478 */
536 1.12 christos /* This should only "trip" if "keytype" is removed from builtins */
537 1.19 christos INSIST(icmd < sizeof(builtins)/sizeof(*builtins));
538 1.12 christos
539 1.12 christos #ifdef OPENSSL
540 1.12 christos builtins[icmd].desc[0] = "digest-name";
541 1.15 christos my_easprintf(&msg,
542 1.15 christos "set key type to use for authenticated requests, one of:%s",
543 1.15 christos list);
544 1.12 christos #else
545 1.12 christos builtins[icmd].desc[0] = "md5";
546 1.15 christos my_easprintf(&msg,
547 1.15 christos "set key type to use for authenticated requests (%s)",
548 1.15 christos list);
549 1.12 christos #endif
550 1.12 christos builtins[icmd].comment = msg;
551 1.12 christos free(list);
552 1.12 christos }
553 1.12 christos
554 1.1 kardel progname = argv[0];
555 1.1 kardel
556 1.1 kardel {
557 1.4 kardel int optct = ntpOptionProcess(&ntpqOptions, argc, argv);
558 1.1 kardel argc -= optct;
559 1.1 kardel argv += optct;
560 1.1 kardel }
561 1.1 kardel
562 1.1 kardel /*
563 1.1 kardel * Process options other than -c and -p, which are specially
564 1.1 kardel * handled by ntpq_custom_opt_handler().
565 1.1 kardel */
566 1.1 kardel
567 1.9 christos debug = OPT_VALUE_SET_DEBUG_LEVEL;
568 1.1 kardel
569 1.1 kardel if (HAVE_OPT(IPV4))
570 1.1 kardel ai_fam_templ = AF_INET;
571 1.1 kardel else if (HAVE_OPT(IPV6))
572 1.1 kardel ai_fam_templ = AF_INET6;
573 1.1 kardel else
574 1.1 kardel ai_fam_templ = ai_fam_default;
575 1.1 kardel
576 1.1 kardel if (HAVE_OPT(INTERACTIVE))
577 1.1 kardel interactive = 1;
578 1.1 kardel
579 1.1 kardel if (HAVE_OPT(NUMERIC))
580 1.1 kardel showhostnames = 0;
581 1.1 kardel
582 1.10 christos if (HAVE_OPT(WIDE))
583 1.10 christos wideremote = 1;
584 1.10 christos
585 1.1 kardel old_rv = HAVE_OPT(OLD_RV);
586 1.1 kardel
587 1.15 christos drefid = OPT_VALUE_REFID;
588 1.15 christos
589 1.9 christos if (0 == argc) {
590 1.1 kardel ADDHOST(DEFHOST);
591 1.1 kardel } else {
592 1.9 christos for (ihost = 0; ihost < (u_int)argc; ihost++) {
593 1.9 christos if ('-' == *argv[ihost]) {
594 1.9 christos //
595 1.9 christos // If I really cared I'd also check:
596 1.9 christos // 0 == argv[ihost][2]
597 1.9 christos //
598 1.9 christos // and there are other cases as well...
599 1.9 christos //
600 1.9 christos if ('4' == argv[ihost][1]) {
601 1.9 christos ai_fam_templ = AF_INET;
602 1.9 christos continue;
603 1.9 christos } else if ('6' == argv[ihost][1]) {
604 1.9 christos ai_fam_templ = AF_INET6;
605 1.9 christos continue;
606 1.9 christos } else {
607 1.9 christos // XXX Throw a usage error
608 1.9 christos }
609 1.9 christos }
610 1.9 christos ADDHOST(argv[ihost]);
611 1.9 christos }
612 1.1 kardel }
613 1.1 kardel
614 1.1 kardel if (numcmds == 0 && interactive == 0
615 1.1 kardel && isatty(fileno(stdin)) && isatty(fileno(stderr))) {
616 1.1 kardel interactive = 1;
617 1.1 kardel }
618 1.1 kardel
619 1.14 christos set_ctrl_c_hook(on_ctrlc);
620 1.1 kardel #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */
621 1.1 kardel if (interactive)
622 1.14 christos push_ctrl_c_handler(abortcmd);
623 1.1 kardel #endif /* SYS_WINNT */
624 1.1 kardel
625 1.1 kardel if (numcmds == 0) {
626 1.9 christos (void) openhost(chosts[0].name, chosts[0].fam);
627 1.1 kardel getcmds();
628 1.1 kardel } else {
629 1.1 kardel for (ihost = 0; ihost < numhosts; ihost++) {
630 1.19 christos if (openhost(chosts[ihost].name, chosts[ihost].fam)) {
631 1.20 christos if (ihost && current_output)
632 1.19 christos fputc('\n', current_output);
633 1.19 christos for (icmd = 0; icmd < numcmds; icmd++) {
634 1.20 christos if (icmd && current_output)
635 1.19 christos fputc('\n', current_output);
636 1.1 kardel docmd(ccmds[icmd]);
637 1.19 christos }
638 1.19 christos }
639 1.1 kardel }
640 1.1 kardel }
641 1.1 kardel #ifdef SYS_WINNT
642 1.1 kardel WSACleanup();
643 1.1 kardel #endif /* SYS_WINNT */
644 1.1 kardel return 0;
645 1.1 kardel }
646 1.1 kardel #endif /* !BUILD_AS_LIB */
647 1.1 kardel
648 1.1 kardel /*
649 1.1 kardel * openhost - open a socket to a host
650 1.1 kardel */
651 1.1 kardel static int
652 1.1 kardel openhost(
653 1.9 christos const char *hname,
654 1.9 christos int fam
655 1.1 kardel )
656 1.1 kardel {
657 1.9 christos const char svc[] = "ntp";
658 1.1 kardel char temphost[LENHOSTNAME];
659 1.20 christos int a_info;
660 1.9 christos struct addrinfo hints, *ai;
661 1.9 christos sockaddr_u addr;
662 1.9 christos size_t octets;
663 1.20 christos const char *cp;
664 1.1 kardel char name[LENHOSTNAME];
665 1.1 kardel
666 1.1 kardel /*
667 1.1 kardel * We need to get by the [] if they were entered
668 1.1 kardel */
669 1.20 christos if (*hname == '[') {
670 1.20 christos cp = strchr(hname + 1, ']');
671 1.20 christos if (!cp || (octets = (size_t)(cp - hname) - 1) >= sizeof(name)) {
672 1.20 christos errno = EINVAL;
673 1.20 christos warning("%s", "bad hostname/address");
674 1.1 kardel return 0;
675 1.1 kardel }
676 1.20 christos memcpy(name, hname + 1, octets);
677 1.20 christos name[octets] = '\0';
678 1.20 christos hname = name;
679 1.1 kardel }
680 1.1 kardel
681 1.1 kardel /*
682 1.1 kardel * First try to resolve it as an ip address and if that fails,
683 1.1 kardel * do a fullblown (dns) lookup. That way we only use the dns
684 1.1 kardel * when it is needed and work around some implementations that
685 1.1 kardel * will return an "IPv4-mapped IPv6 address" address if you
686 1.1 kardel * give it an IPv4 address to lookup.
687 1.1 kardel */
688 1.4 kardel ZERO(hints);
689 1.9 christos hints.ai_family = fam;
690 1.1 kardel hints.ai_protocol = IPPROTO_UDP;
691 1.1 kardel hints.ai_socktype = SOCK_DGRAM;
692 1.4 kardel hints.ai_flags = Z_AI_NUMERICHOST;
693 1.9 christos ai = NULL;
694 1.1 kardel
695 1.9 christos a_info = getaddrinfo(hname, svc, &hints, &ai);
696 1.1 kardel if (a_info == EAI_NONAME
697 1.1 kardel #ifdef EAI_NODATA
698 1.1 kardel || a_info == EAI_NODATA
699 1.1 kardel #endif
700 1.1 kardel ) {
701 1.1 kardel hints.ai_flags = AI_CANONNAME;
702 1.1 kardel #ifdef AI_ADDRCONFIG
703 1.1 kardel hints.ai_flags |= AI_ADDRCONFIG;
704 1.1 kardel #endif
705 1.9 christos a_info = getaddrinfo(hname, svc, &hints, &ai);
706 1.1 kardel }
707 1.1 kardel #ifdef AI_ADDRCONFIG
708 1.1 kardel /* Some older implementations don't like AI_ADDRCONFIG. */
709 1.1 kardel if (a_info == EAI_BADFLAGS) {
710 1.9 christos hints.ai_flags &= ~AI_ADDRCONFIG;
711 1.9 christos a_info = getaddrinfo(hname, svc, &hints, &ai);
712 1.1 kardel }
713 1.1 kardel #endif
714 1.1 kardel if (a_info != 0) {
715 1.9 christos fprintf(stderr, "%s\n", gai_strerror(a_info));
716 1.1 kardel return 0;
717 1.1 kardel }
718 1.1 kardel
719 1.9 christos INSIST(ai != NULL);
720 1.9 christos ZERO(addr);
721 1.9 christos octets = min(sizeof(addr), ai->ai_addrlen);
722 1.9 christos memcpy(&addr, ai->ai_addr, octets);
723 1.9 christos
724 1.9 christos if (ai->ai_canonname == NULL) {
725 1.9 christos strlcpy(temphost, stoa(&addr), sizeof(temphost));
726 1.4 kardel currenthostisnum = TRUE;
727 1.1 kardel } else {
728 1.9 christos strlcpy(temphost, ai->ai_canonname, sizeof(temphost));
729 1.4 kardel currenthostisnum = FALSE;
730 1.1 kardel }
731 1.1 kardel
732 1.1 kardel if (debug > 2)
733 1.9 christos printf("Opening host %s (%s)\n",
734 1.9 christos temphost,
735 1.9 christos (ai->ai_family == AF_INET)
736 1.9 christos ? "AF_INET"
737 1.9 christos : (ai->ai_family == AF_INET6)
738 1.9 christos ? "AF_INET6"
739 1.9 christos : "AF-???"
740 1.9 christos );
741 1.1 kardel
742 1.1 kardel if (havehost == 1) {
743 1.1 kardel if (debug > 2)
744 1.1 kardel printf("Closing old host %s\n", currenthost);
745 1.9 christos closesocket(sockfd);
746 1.1 kardel havehost = 0;
747 1.1 kardel }
748 1.9 christos strlcpy(currenthost, temphost, sizeof(currenthost));
749 1.1 kardel
750 1.1 kardel /* port maps to the same location in both families */
751 1.9 christos s_port = NSRCPORT(&addr);
752 1.1 kardel #ifdef SYS_VXWORKS
753 1.1 kardel ((struct sockaddr_in6 *)&hostaddr)->sin6_port = htons(SERVER_PORT_NUM);
754 1.1 kardel if (ai->ai_family == AF_INET)
755 1.1 kardel *(struct sockaddr_in *)&hostaddr=
756 1.1 kardel *((struct sockaddr_in *)ai->ai_addr);
757 1.1 kardel else
758 1.1 kardel *(struct sockaddr_in6 *)&hostaddr=
759 1.1 kardel *((struct sockaddr_in6 *)ai->ai_addr);
760 1.1 kardel #endif /* SYS_VXWORKS */
761 1.1 kardel
762 1.1 kardel #ifdef SYS_WINNT
763 1.1 kardel {
764 1.1 kardel int optionValue = SO_SYNCHRONOUS_NONALERT;
765 1.1 kardel int err;
766 1.1 kardel
767 1.1 kardel err = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
768 1.19 christos (void *)&optionValue, sizeof(optionValue));
769 1.1 kardel if (err) {
770 1.9 christos mfprintf(stderr,
771 1.9 christos "setsockopt(SO_SYNCHRONOUS_NONALERT)"
772 1.9 christos " error: %m\n");
773 1.9 christos freeaddrinfo(ai);
774 1.1 kardel exit(1);
775 1.1 kardel }
776 1.1 kardel }
777 1.1 kardel #endif /* SYS_WINNT */
778 1.1 kardel
779 1.9 christos sockfd = socket(ai->ai_family, ai->ai_socktype,
780 1.9 christos ai->ai_protocol);
781 1.1 kardel if (sockfd == INVALID_SOCKET) {
782 1.3 christos error("socket");
783 1.9 christos freeaddrinfo(ai);
784 1.9 christos return 0;
785 1.1 kardel }
786 1.1 kardel
787 1.9 christos
788 1.1 kardel #ifdef NEED_RCVBUF_SLOP
789 1.1 kardel # ifdef SO_RCVBUF
790 1.1 kardel { int rbufsize = DATASIZE + 2048; /* 2K for slop */
791 1.1 kardel if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF,
792 1.19 christos (void *)&rbufsize, sizeof(int)) == -1)
793 1.9 christos error("setsockopt");
794 1.1 kardel }
795 1.1 kardel # endif
796 1.1 kardel #endif
797 1.1 kardel
798 1.9 christos if
799 1.1 kardel #ifdef SYS_VXWORKS
800 1.9 christos (connect(sockfd, (struct sockaddr *)&hostaddr,
801 1.1 kardel sizeof(hostaddr)) == -1)
802 1.1 kardel #else
803 1.9 christos (connect(sockfd, (struct sockaddr *)ai->ai_addr,
804 1.14 christos ai->ai_addrlen) == -1)
805 1.1 kardel #endif /* SYS_VXWORKS */
806 1.14 christos {
807 1.9 christos error("connect");
808 1.1 kardel freeaddrinfo(ai);
809 1.9 christos return 0;
810 1.9 christos }
811 1.9 christos freeaddrinfo(ai);
812 1.1 kardel havehost = 1;
813 1.9 christos numassoc = 0;
814 1.9 christos
815 1.1 kardel return 1;
816 1.1 kardel }
817 1.1 kardel
818 1.1 kardel
819 1.9 christos static void
820 1.9 christos dump_hex_printable(
821 1.9 christos const void * data,
822 1.9 christos size_t len
823 1.9 christos )
824 1.9 christos {
825 1.18 christos /* every line shows at most 16 bytes, so we need a buffer of
826 1.18 christos * 4 * 16 (2 xdigits, 1 char, one sep for xdigits)
827 1.18 christos * + 2 * 1 (block separators)
828 1.18 christos * + <LF> + <NUL>
829 1.18 christos *---------------
830 1.18 christos * 68 bytes
831 1.18 christos */
832 1.18 christos static const char s_xdig[16] = "0123456789ABCDEF";
833 1.18 christos
834 1.18 christos char lbuf[68];
835 1.18 christos int ch, rowlen;
836 1.18 christos const u_char * cdata = data;
837 1.18 christos char *xptr, *pptr;
838 1.18 christos
839 1.18 christos while (len) {
840 1.18 christos memset(lbuf, ' ', sizeof(lbuf));
841 1.18 christos xptr = lbuf;
842 1.18 christos pptr = lbuf + 3*16 + 2;
843 1.18 christos
844 1.18 christos rowlen = (len > 16) ? 16 : (int)len;
845 1.9 christos len -= rowlen;
846 1.18 christos
847 1.18 christos do {
848 1.18 christos ch = *cdata++;
849 1.18 christos
850 1.18 christos *xptr++ = s_xdig[ch >> 4 ];
851 1.18 christos *xptr++ = s_xdig[ch & 0x0F];
852 1.18 christos if (++xptr == lbuf + 3*8)
853 1.18 christos ++xptr;
854 1.18 christos
855 1.18 christos *pptr++ = isprint(ch) ? (char)ch : '.';
856 1.18 christos } while (--rowlen);
857 1.18 christos
858 1.18 christos *pptr++ = '\n';
859 1.18 christos *pptr = '\0';
860 1.18 christos fputs(lbuf, stdout);
861 1.9 christos }
862 1.9 christos }
863 1.9 christos
864 1.9 christos
865 1.1 kardel /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
866 1.1 kardel /*
867 1.1 kardel * sendpkt - send a packet to the remote host
868 1.1 kardel */
869 1.1 kardel static int
870 1.1 kardel sendpkt(
871 1.1 kardel void * xdata,
872 1.1 kardel size_t xdatalen
873 1.1 kardel )
874 1.1 kardel {
875 1.1 kardel if (debug >= 3)
876 1.2 christos printf("Sending %zu octets\n", xdatalen);
877 1.1 kardel
878 1.14 christos if (send(sockfd, xdata, xdatalen, 0) == -1) {
879 1.3 christos warning("write to %s failed", currenthost);
880 1.1 kardel return -1;
881 1.1 kardel }
882 1.1 kardel
883 1.1 kardel if (debug >= 4) {
884 1.9 christos printf("Request packet:\n");
885 1.9 christos dump_hex_printable(xdata, xdatalen);
886 1.1 kardel }
887 1.1 kardel return 0;
888 1.1 kardel }
889 1.1 kardel
890 1.1 kardel /*
891 1.1 kardel * getresponse - get a (series of) response packet(s) and return the data
892 1.1 kardel */
893 1.1 kardel static int
894 1.1 kardel getresponse(
895 1.1 kardel int opcode,
896 1.1 kardel int associd,
897 1.1 kardel u_short *rstatus,
898 1.14 christos size_t *rsize,
899 1.4 kardel const char **rdata,
900 1.1 kardel int timeo
901 1.1 kardel )
902 1.1 kardel {
903 1.1 kardel struct ntp_control rpkt;
904 1.1 kardel struct sock_timeval tvo;
905 1.1 kardel u_short offsets[MAXFRAGS+1];
906 1.1 kardel u_short counts[MAXFRAGS+1];
907 1.1 kardel u_short offset;
908 1.1 kardel u_short count;
909 1.4 kardel size_t numfrags;
910 1.4 kardel size_t f;
911 1.4 kardel size_t ff;
912 1.1 kardel int seenlastfrag;
913 1.1 kardel int shouldbesize;
914 1.1 kardel fd_set fds;
915 1.1 kardel int n;
916 1.9 christos int errcode;
917 1.15 christos /* absolute timeout checks. Not 'time_t' by intention! */
918 1.15 christos uint32_t tobase; /* base value for timeout */
919 1.15 christos uint32_t tospan; /* timeout span (max delay) */
920 1.15 christos uint32_t todiff; /* current delay */
921 1.1 kardel
922 1.18 christos memset(offsets, 0, sizeof(offsets));
923 1.18 christos memset(counts , 0, sizeof(counts ));
924 1.18 christos
925 1.1 kardel /*
926 1.1 kardel * This is pretty tricky. We may get between 1 and MAXFRAG packets
927 1.1 kardel * back in response to the request. We peel the data out of
928 1.1 kardel * each packet and collect it in one long block. When the last
929 1.1 kardel * packet in the sequence is received we'll know how much data we
930 1.1 kardel * should have had. Note we use one long time out, should reconsider.
931 1.1 kardel */
932 1.1 kardel *rsize = 0;
933 1.1 kardel if (rstatus)
934 1.4 kardel *rstatus = 0;
935 1.1 kardel *rdata = (char *)pktdata;
936 1.1 kardel
937 1.1 kardel numfrags = 0;
938 1.1 kardel seenlastfrag = 0;
939 1.1 kardel
940 1.15 christos tobase = (uint32_t)time(NULL);
941 1.15 christos
942 1.1 kardel FD_ZERO(&fds);
943 1.1 kardel
944 1.1 kardel /*
945 1.1 kardel * Loop until we have an error or a complete response. Nearly all
946 1.4 kardel * code paths to loop again use continue.
947 1.1 kardel */
948 1.1 kardel for (;;) {
949 1.1 kardel
950 1.1 kardel if (numfrags == 0)
951 1.4 kardel tvo = tvout;
952 1.1 kardel else
953 1.4 kardel tvo = tvsout;
954 1.15 christos tospan = (uint32_t)tvo.tv_sec + (tvo.tv_usec != 0);
955 1.9 christos
956 1.1 kardel FD_SET(sockfd, &fds);
957 1.14 christos n = select(sockfd+1, &fds, NULL, NULL, &tvo);
958 1.1 kardel if (n == -1) {
959 1.15 christos #if !defined(SYS_WINNT) && defined(EINTR)
960 1.15 christos /* Windows does not know about EINTR (until very
961 1.15 christos * recently) and the handling of console events
962 1.15 christos * is *very* different from POSIX/UNIX signal
963 1.15 christos * handling anyway.
964 1.15 christos *
965 1.15 christos * Under non-windows targets we map EINTR as
966 1.15 christos * 'last packet was received' and try to exit
967 1.15 christos * the receive sequence.
968 1.15 christos */
969 1.15 christos if (errno == EINTR) {
970 1.15 christos seenlastfrag = 1;
971 1.15 christos goto maybe_final;
972 1.15 christos }
973 1.15 christos #endif
974 1.3 christos warning("select fails");
975 1.1 kardel return -1;
976 1.1 kardel }
977 1.15 christos
978 1.15 christos /*
979 1.15 christos * Check if this is already too late. Trash the data and
980 1.15 christos * fake a timeout if this is so.
981 1.15 christos */
982 1.15 christos todiff = (((uint32_t)time(NULL)) - tobase) & 0x7FFFFFFFu;
983 1.15 christos if ((n > 0) && (todiff > tospan)) {
984 1.15 christos n = recv(sockfd, (char *)&rpkt, sizeof(rpkt), 0);
985 1.18 christos n -= n; /* faked timeout return from 'select()',
986 1.18 christos * execute RMW cycle on 'n'
987 1.18 christos */
988 1.15 christos }
989 1.15 christos
990 1.18 christos if (n <= 0) {
991 1.1 kardel /*
992 1.1 kardel * Timed out. Return what we have
993 1.1 kardel */
994 1.1 kardel if (numfrags == 0) {
995 1.1 kardel if (timeo)
996 1.4 kardel fprintf(stderr,
997 1.4 kardel "%s: timed out, nothing received\n",
998 1.4 kardel currenthost);
999 1.1 kardel return ERR_TIMEOUT;
1000 1.1 kardel }
1001 1.4 kardel if (timeo)
1002 1.4 kardel fprintf(stderr,
1003 1.4 kardel "%s: timed out with incomplete data\n",
1004 1.4 kardel currenthost);
1005 1.4 kardel if (debug) {
1006 1.4 kardel fprintf(stderr,
1007 1.4 kardel "ERR_INCOMPLETE: Received fragments:\n");
1008 1.4 kardel for (f = 0; f < numfrags; f++)
1009 1.4 kardel fprintf(stderr,
1010 1.9 christos "%2u: %5d %5d\t%3d octets\n",
1011 1.9 christos (u_int)f, offsets[f],
1012 1.4 kardel offsets[f] +
1013 1.4 kardel counts[f],
1014 1.4 kardel counts[f]);
1015 1.4 kardel fprintf(stderr,
1016 1.4 kardel "last fragment %sreceived\n",
1017 1.4 kardel (seenlastfrag)
1018 1.4 kardel ? ""
1019 1.4 kardel : "not ");
1020 1.4 kardel }
1021 1.4 kardel return ERR_INCOMPLETE;
1022 1.1 kardel }
1023 1.1 kardel
1024 1.1 kardel n = recv(sockfd, (char *)&rpkt, sizeof(rpkt), 0);
1025 1.18 christos if (n < 0) {
1026 1.3 christos warning("read");
1027 1.1 kardel return -1;
1028 1.1 kardel }
1029 1.1 kardel
1030 1.1 kardel if (debug >= 4) {
1031 1.9 christos printf("Response packet:\n");
1032 1.9 christos dump_hex_printable(&rpkt, n);
1033 1.1 kardel }
1034 1.1 kardel
1035 1.1 kardel /*
1036 1.1 kardel * Check for format errors. Bug proofing.
1037 1.1 kardel */
1038 1.5 kardel if (n < (int)CTL_HEADER_LEN) {
1039 1.1 kardel if (debug)
1040 1.4 kardel printf("Short (%d byte) packet received\n", n);
1041 1.1 kardel continue;
1042 1.1 kardel }
1043 1.1 kardel if (PKT_VERSION(rpkt.li_vn_mode) > NTP_VERSION
1044 1.1 kardel || PKT_VERSION(rpkt.li_vn_mode) < NTP_OLDVERSION) {
1045 1.1 kardel if (debug)
1046 1.4 kardel printf("Packet received with version %d\n",
1047 1.4 kardel PKT_VERSION(rpkt.li_vn_mode));
1048 1.1 kardel continue;
1049 1.1 kardel }
1050 1.1 kardel if (PKT_MODE(rpkt.li_vn_mode) != MODE_CONTROL) {
1051 1.1 kardel if (debug)
1052 1.4 kardel printf("Packet received with mode %d\n",
1053 1.4 kardel PKT_MODE(rpkt.li_vn_mode));
1054 1.1 kardel continue;
1055 1.1 kardel }
1056 1.1 kardel if (!CTL_ISRESPONSE(rpkt.r_m_e_op)) {
1057 1.1 kardel if (debug)
1058 1.4 kardel printf("Received request packet, wanted response\n");
1059 1.1 kardel continue;
1060 1.1 kardel }
1061 1.1 kardel
1062 1.1 kardel /*
1063 1.1 kardel * Check opcode and sequence number for a match.
1064 1.1 kardel * Could be old data getting to us.
1065 1.1 kardel */
1066 1.1 kardel if (ntohs(rpkt.sequence) != sequence) {
1067 1.1 kardel if (debug)
1068 1.4 kardel printf("Received sequnce number %d, wanted %d\n",
1069 1.4 kardel ntohs(rpkt.sequence), sequence);
1070 1.1 kardel continue;
1071 1.1 kardel }
1072 1.1 kardel if (CTL_OP(rpkt.r_m_e_op) != opcode) {
1073 1.1 kardel if (debug)
1074 1.1 kardel printf(
1075 1.1 kardel "Received opcode %d, wanted %d (sequence number okay)\n",
1076 1.1 kardel CTL_OP(rpkt.r_m_e_op), opcode);
1077 1.1 kardel continue;
1078 1.1 kardel }
1079 1.1 kardel
1080 1.1 kardel /*
1081 1.1 kardel * Check the error code. If non-zero, return it.
1082 1.1 kardel */
1083 1.1 kardel if (CTL_ISERROR(rpkt.r_m_e_op)) {
1084 1.1 kardel errcode = (ntohs(rpkt.status) >> 8) & 0xff;
1085 1.9 christos if (CTL_ISMORE(rpkt.r_m_e_op))
1086 1.9 christos TRACE(1, ("Error code %d received on not-final packet\n",
1087 1.9 christos errcode));
1088 1.1 kardel if (errcode == CERR_UNSPEC)
1089 1.9 christos return ERR_UNSPEC;
1090 1.1 kardel return errcode;
1091 1.1 kardel }
1092 1.1 kardel
1093 1.1 kardel /*
1094 1.1 kardel * Check the association ID to make sure it matches what
1095 1.1 kardel * we sent.
1096 1.1 kardel */
1097 1.1 kardel if (ntohs(rpkt.associd) != associd) {
1098 1.9 christos TRACE(1, ("Association ID %d doesn't match expected %d\n",
1099 1.9 christos ntohs(rpkt.associd), associd));
1100 1.1 kardel /*
1101 1.1 kardel * Hack for silly fuzzballs which, at the time of writing,
1102 1.1 kardel * return an assID of sys.peer when queried for system variables.
1103 1.1 kardel */
1104 1.1 kardel #ifdef notdef
1105 1.1 kardel continue;
1106 1.1 kardel #endif
1107 1.1 kardel }
1108 1.1 kardel
1109 1.1 kardel /*
1110 1.1 kardel * Collect offset and count. Make sure they make sense.
1111 1.1 kardel */
1112 1.1 kardel offset = ntohs(rpkt.offset);
1113 1.1 kardel count = ntohs(rpkt.count);
1114 1.1 kardel
1115 1.1 kardel /*
1116 1.1 kardel * validate received payload size is padded to next 32-bit
1117 1.1 kardel * boundary and no smaller than claimed by rpkt.count
1118 1.1 kardel */
1119 1.1 kardel if (n & 0x3) {
1120 1.9 christos TRACE(1, ("Response packet not padded, size = %d\n",
1121 1.9 christos n));
1122 1.1 kardel continue;
1123 1.1 kardel }
1124 1.1 kardel
1125 1.1 kardel shouldbesize = (CTL_HEADER_LEN + count + 3) & ~3;
1126 1.1 kardel
1127 1.1 kardel if (n < shouldbesize) {
1128 1.9 christos printf("Response packet claims %u octets payload, above %ld received\n",
1129 1.16 christos count, (long)(n - CTL_HEADER_LEN));
1130 1.1 kardel return ERR_INCOMPLETE;
1131 1.1 kardel }
1132 1.1 kardel
1133 1.1 kardel if (debug >= 3 && shouldbesize > n) {
1134 1.1 kardel u_int32 key;
1135 1.1 kardel u_int32 *lpkt;
1136 1.1 kardel int maclen;
1137 1.1 kardel
1138 1.1 kardel /*
1139 1.1 kardel * Usually we ignore authentication, but for debugging purposes
1140 1.1 kardel * we watch it here.
1141 1.1 kardel */
1142 1.1 kardel /* round to 8 octet boundary */
1143 1.1 kardel shouldbesize = (shouldbesize + 7) & ~7;
1144 1.1 kardel
1145 1.1 kardel maclen = n - shouldbesize;
1146 1.2 christos if (maclen >= (int)MIN_MAC_LEN) {
1147 1.1 kardel printf(
1148 1.1 kardel "Packet shows signs of authentication (total %d, data %d, mac %d)\n",
1149 1.1 kardel n, shouldbesize, maclen);
1150 1.1 kardel lpkt = (u_int32 *)&rpkt;
1151 1.1 kardel printf("%08lx %08lx %08lx %08lx %08lx %08lx\n",
1152 1.1 kardel (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_int32) - 3]),
1153 1.1 kardel (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_int32) - 2]),
1154 1.1 kardel (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_int32) - 1]),
1155 1.1 kardel (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_int32)]),
1156 1.1 kardel (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_int32) + 1]),
1157 1.1 kardel (u_long)ntohl(lpkt[(n - maclen)/sizeof(u_int32) + 2]));
1158 1.1 kardel key = ntohl(lpkt[(n - maclen) / sizeof(u_int32)]);
1159 1.1 kardel printf("Authenticated with keyid %lu\n", (u_long)key);
1160 1.1 kardel if (key != 0 && key != info_auth_keyid) {
1161 1.1 kardel printf("We don't know that key\n");
1162 1.1 kardel } else {
1163 1.1 kardel if (authdecrypt(key, (u_int32 *)&rpkt,
1164 1.1 kardel n - maclen, maclen)) {
1165 1.1 kardel printf("Auth okay!\n");
1166 1.1 kardel } else {
1167 1.1 kardel printf("Auth failed!\n");
1168 1.1 kardel }
1169 1.1 kardel }
1170 1.1 kardel }
1171 1.1 kardel }
1172 1.1 kardel
1173 1.9 christos TRACE(2, ("Got packet, size = %d\n", n));
1174 1.9 christos if (count > (n - CTL_HEADER_LEN)) {
1175 1.9 christos TRACE(1, ("Received count of %u octets, data in packet is %ld\n",
1176 1.9 christos count, (long)n - CTL_HEADER_LEN));
1177 1.1 kardel continue;
1178 1.1 kardel }
1179 1.1 kardel if (count == 0 && CTL_ISMORE(rpkt.r_m_e_op)) {
1180 1.9 christos TRACE(1, ("Received count of 0 in non-final fragment\n"));
1181 1.1 kardel continue;
1182 1.1 kardel }
1183 1.1 kardel if (offset + count > sizeof(pktdata)) {
1184 1.9 christos TRACE(1, ("Offset %u, count %u, too big for buffer\n",
1185 1.9 christos offset, count));
1186 1.1 kardel return ERR_TOOMUCH;
1187 1.1 kardel }
1188 1.1 kardel if (seenlastfrag && !CTL_ISMORE(rpkt.r_m_e_op)) {
1189 1.9 christos TRACE(1, ("Received second last fragment packet\n"));
1190 1.1 kardel continue;
1191 1.1 kardel }
1192 1.1 kardel
1193 1.1 kardel /*
1194 1.1 kardel * So far, so good. Record this fragment, making sure it doesn't
1195 1.1 kardel * overlap anything.
1196 1.1 kardel */
1197 1.9 christos TRACE(2, ("Packet okay\n"));
1198 1.1 kardel
1199 1.1 kardel if (numfrags > (MAXFRAGS - 1)) {
1200 1.9 christos TRACE(2, ("Number of fragments exceeds maximum %d\n",
1201 1.9 christos MAXFRAGS - 1));
1202 1.1 kardel return ERR_TOOMUCH;
1203 1.1 kardel }
1204 1.1 kardel
1205 1.1 kardel /*
1206 1.1 kardel * Find the position for the fragment relative to any
1207 1.1 kardel * previously received.
1208 1.1 kardel */
1209 1.9 christos for (f = 0;
1210 1.9 christos f < numfrags && offsets[f] < offset;
1211 1.4 kardel f++) {
1212 1.1 kardel /* empty body */ ;
1213 1.1 kardel }
1214 1.1 kardel
1215 1.4 kardel if (f < numfrags && offset == offsets[f]) {
1216 1.9 christos TRACE(1, ("duplicate %u octets at %u ignored, prior %u at %u\n",
1217 1.9 christos count, offset, counts[f], offsets[f]));
1218 1.1 kardel continue;
1219 1.1 kardel }
1220 1.1 kardel
1221 1.4 kardel if (f > 0 && (offsets[f-1] + counts[f-1]) > offset) {
1222 1.9 christos TRACE(1, ("received frag at %u overlaps with %u octet frag at %u\n",
1223 1.9 christos offset, counts[f-1], offsets[f-1]));
1224 1.1 kardel continue;
1225 1.1 kardel }
1226 1.1 kardel
1227 1.4 kardel if (f < numfrags && (offset + count) > offsets[f]) {
1228 1.9 christos TRACE(1, ("received %u octet frag at %u overlaps with frag at %u\n",
1229 1.9 christos count, offset, offsets[f]));
1230 1.1 kardel continue;
1231 1.1 kardel }
1232 1.1 kardel
1233 1.4 kardel for (ff = numfrags; ff > f; ff--) {
1234 1.4 kardel offsets[ff] = offsets[ff-1];
1235 1.4 kardel counts[ff] = counts[ff-1];
1236 1.1 kardel }
1237 1.4 kardel offsets[f] = offset;
1238 1.4 kardel counts[f] = count;
1239 1.1 kardel numfrags++;
1240 1.1 kardel
1241 1.1 kardel /*
1242 1.1 kardel * Got that stuffed in right. Figure out if this was the last.
1243 1.1 kardel * Record status info out of the last packet.
1244 1.1 kardel */
1245 1.1 kardel if (!CTL_ISMORE(rpkt.r_m_e_op)) {
1246 1.1 kardel seenlastfrag = 1;
1247 1.1 kardel if (rstatus != 0)
1248 1.4 kardel *rstatus = ntohs(rpkt.status);
1249 1.1 kardel }
1250 1.1 kardel
1251 1.1 kardel /*
1252 1.15 christos * Copy the data into the data buffer, and bump the
1253 1.15 christos * timout base in case we need more.
1254 1.1 kardel */
1255 1.9 christos memcpy((char *)pktdata + offset, &rpkt.u, count);
1256 1.15 christos tobase = (uint32_t)time(NULL);
1257 1.15 christos
1258 1.1 kardel /*
1259 1.1 kardel * If we've seen the last fragment, look for holes in the sequence.
1260 1.1 kardel * If there aren't any, we're done.
1261 1.1 kardel */
1262 1.16 christos #if !defined(SYS_WINNT) && defined(EINTR)
1263 1.16 christos maybe_final:
1264 1.16 christos #endif
1265 1.16 christos
1266 1.1 kardel if (seenlastfrag && offsets[0] == 0) {
1267 1.4 kardel for (f = 1; f < numfrags; f++)
1268 1.4 kardel if (offsets[f-1] + counts[f-1] !=
1269 1.4 kardel offsets[f])
1270 1.1 kardel break;
1271 1.4 kardel if (f == numfrags) {
1272 1.4 kardel *rsize = offsets[f-1] + counts[f-1];
1273 1.9 christos TRACE(1, ("%lu packets reassembled into response\n",
1274 1.9 christos (u_long)numfrags));
1275 1.1 kardel return 0;
1276 1.1 kardel }
1277 1.1 kardel }
1278 1.1 kardel } /* giant for (;;) collecting response packets */
1279 1.1 kardel } /* getresponse() */
1280 1.1 kardel
1281 1.1 kardel
1282 1.1 kardel /*
1283 1.1 kardel * sendrequest - format and send a request packet
1284 1.1 kardel */
1285 1.1 kardel static int
1286 1.1 kardel sendrequest(
1287 1.1 kardel int opcode,
1288 1.9 christos associd_t associd,
1289 1.1 kardel int auth,
1290 1.14 christos size_t qsize,
1291 1.9 christos const char *qdata
1292 1.1 kardel )
1293 1.1 kardel {
1294 1.1 kardel struct ntp_control qpkt;
1295 1.14 christos size_t pktsize;
1296 1.1 kardel u_long key_id;
1297 1.1 kardel char * pass;
1298 1.14 christos size_t maclen;
1299 1.1 kardel
1300 1.1 kardel /*
1301 1.1 kardel * Check to make sure the data will fit in one packet
1302 1.1 kardel */
1303 1.1 kardel if (qsize > CTL_MAX_DATA_LEN) {
1304 1.1 kardel fprintf(stderr,
1305 1.14 christos "***Internal error! qsize (%zu) too large\n",
1306 1.1 kardel qsize);
1307 1.1 kardel return 1;
1308 1.1 kardel }
1309 1.1 kardel
1310 1.1 kardel /*
1311 1.1 kardel * Fill in the packet
1312 1.1 kardel */
1313 1.1 kardel qpkt.li_vn_mode = PKT_LI_VN_MODE(0, pktversion, MODE_CONTROL);
1314 1.1 kardel qpkt.r_m_e_op = (u_char)(opcode & CTL_OP_MASK);
1315 1.1 kardel qpkt.sequence = htons(sequence);
1316 1.1 kardel qpkt.status = 0;
1317 1.1 kardel qpkt.associd = htons((u_short)associd);
1318 1.1 kardel qpkt.offset = 0;
1319 1.1 kardel qpkt.count = htons((u_short)qsize);
1320 1.1 kardel
1321 1.1 kardel pktsize = CTL_HEADER_LEN;
1322 1.1 kardel
1323 1.1 kardel /*
1324 1.1 kardel * If we have data, copy and pad it out to a 32-bit boundary.
1325 1.1 kardel */
1326 1.1 kardel if (qsize > 0) {
1327 1.9 christos memcpy(&qpkt.u, qdata, (size_t)qsize);
1328 1.1 kardel pktsize += qsize;
1329 1.1 kardel while (pktsize & (sizeof(u_int32) - 1)) {
1330 1.9 christos qpkt.u.data[qsize++] = 0;
1331 1.1 kardel pktsize++;
1332 1.1 kardel }
1333 1.1 kardel }
1334 1.1 kardel
1335 1.1 kardel /*
1336 1.1 kardel * If it isn't authenticated we can just send it. Otherwise
1337 1.1 kardel * we're going to have to think about it a little.
1338 1.1 kardel */
1339 1.1 kardel if (!auth && !always_auth) {
1340 1.1 kardel return sendpkt(&qpkt, pktsize);
1341 1.9 christos }
1342 1.1 kardel
1343 1.1 kardel /*
1344 1.1 kardel * Pad out packet to a multiple of 8 octets to be sure
1345 1.1 kardel * receiver can handle it.
1346 1.1 kardel */
1347 1.1 kardel while (pktsize & 7) {
1348 1.9 christos qpkt.u.data[qsize++] = 0;
1349 1.1 kardel pktsize++;
1350 1.1 kardel }
1351 1.1 kardel
1352 1.1 kardel /*
1353 1.1 kardel * Get the keyid and the password if we don't have one.
1354 1.1 kardel */
1355 1.1 kardel if (info_auth_keyid == 0) {
1356 1.1 kardel key_id = getkeyid("Keyid: ");
1357 1.1 kardel if (key_id == 0 || key_id > NTP_MAXKEY) {
1358 1.9 christos fprintf(stderr,
1359 1.1 kardel "Invalid key identifier\n");
1360 1.1 kardel return 1;
1361 1.1 kardel }
1362 1.1 kardel info_auth_keyid = key_id;
1363 1.1 kardel }
1364 1.1 kardel if (!authistrusted(info_auth_keyid)) {
1365 1.4 kardel pass = getpass_keytype(info_auth_keytype);
1366 1.1 kardel if ('\0' == pass[0]) {
1367 1.1 kardel fprintf(stderr, "Invalid password\n");
1368 1.1 kardel return 1;
1369 1.1 kardel }
1370 1.1 kardel authusekey(info_auth_keyid, info_auth_keytype,
1371 1.1 kardel (u_char *)pass);
1372 1.1 kardel authtrust(info_auth_keyid, 1);
1373 1.1 kardel }
1374 1.1 kardel
1375 1.1 kardel /*
1376 1.1 kardel * Do the encryption.
1377 1.1 kardel */
1378 1.1 kardel maclen = authencrypt(info_auth_keyid, (void *)&qpkt, pktsize);
1379 1.9 christos if (!maclen) {
1380 1.1 kardel fprintf(stderr, "Key not found\n");
1381 1.1 kardel return 1;
1382 1.1 kardel } else if ((size_t)maclen != (info_auth_hashlen + sizeof(keyid_t))) {
1383 1.1 kardel fprintf(stderr,
1384 1.14 christos "%zu octet MAC, %zu expected with %zu octet digest\n",
1385 1.1 kardel maclen, (info_auth_hashlen + sizeof(keyid_t)),
1386 1.1 kardel info_auth_hashlen);
1387 1.1 kardel return 1;
1388 1.1 kardel }
1389 1.9 christos
1390 1.1 kardel return sendpkt((char *)&qpkt, pktsize + maclen);
1391 1.1 kardel }
1392 1.1 kardel
1393 1.1 kardel
1394 1.1 kardel /*
1395 1.4 kardel * show_error_msg - display the error text for a mode 6 error response.
1396 1.4 kardel */
1397 1.4 kardel void
1398 1.4 kardel show_error_msg(
1399 1.4 kardel int m6resp,
1400 1.4 kardel associd_t associd
1401 1.4 kardel )
1402 1.4 kardel {
1403 1.4 kardel if (numhosts > 1)
1404 1.4 kardel fprintf(stderr, "server=%s ", currenthost);
1405 1.4 kardel
1406 1.15 christos switch (m6resp) {
1407 1.4 kardel
1408 1.4 kardel case CERR_BADFMT:
1409 1.4 kardel fprintf(stderr,
1410 1.4 kardel "***Server reports a bad format request packet\n");
1411 1.4 kardel break;
1412 1.4 kardel
1413 1.4 kardel case CERR_PERMISSION:
1414 1.4 kardel fprintf(stderr,
1415 1.4 kardel "***Server disallowed request (authentication?)\n");
1416 1.4 kardel break;
1417 1.4 kardel
1418 1.4 kardel case CERR_BADOP:
1419 1.4 kardel fprintf(stderr,
1420 1.4 kardel "***Server reports a bad opcode in request\n");
1421 1.4 kardel break;
1422 1.4 kardel
1423 1.4 kardel case CERR_BADASSOC:
1424 1.4 kardel fprintf(stderr,
1425 1.4 kardel "***Association ID %d unknown to server\n",
1426 1.4 kardel associd);
1427 1.4 kardel break;
1428 1.4 kardel
1429 1.4 kardel case CERR_UNKNOWNVAR:
1430 1.4 kardel fprintf(stderr,
1431 1.4 kardel "***A request variable unknown to the server\n");
1432 1.4 kardel break;
1433 1.4 kardel
1434 1.4 kardel case CERR_BADVALUE:
1435 1.4 kardel fprintf(stderr,
1436 1.4 kardel "***Server indicates a request variable was bad\n");
1437 1.4 kardel break;
1438 1.4 kardel
1439 1.4 kardel case ERR_UNSPEC:
1440 1.4 kardel fprintf(stderr,
1441 1.4 kardel "***Server returned an unspecified error\n");
1442 1.4 kardel break;
1443 1.4 kardel
1444 1.4 kardel case ERR_TIMEOUT:
1445 1.4 kardel fprintf(stderr, "***Request timed out\n");
1446 1.4 kardel break;
1447 1.4 kardel
1448 1.4 kardel case ERR_INCOMPLETE:
1449 1.4 kardel fprintf(stderr,
1450 1.4 kardel "***Response from server was incomplete\n");
1451 1.4 kardel break;
1452 1.4 kardel
1453 1.4 kardel case ERR_TOOMUCH:
1454 1.4 kardel fprintf(stderr,
1455 1.4 kardel "***Buffer size exceeded for returned data\n");
1456 1.4 kardel break;
1457 1.4 kardel
1458 1.4 kardel default:
1459 1.4 kardel fprintf(stderr,
1460 1.4 kardel "***Server returns unknown error code %d\n",
1461 1.4 kardel m6resp);
1462 1.4 kardel }
1463 1.4 kardel }
1464 1.4 kardel
1465 1.4 kardel /*
1466 1.4 kardel * doquery - send a request and process the response, displaying
1467 1.4 kardel * error messages for any error responses.
1468 1.1 kardel */
1469 1.1 kardel int
1470 1.1 kardel doquery(
1471 1.1 kardel int opcode,
1472 1.4 kardel associd_t associd,
1473 1.4 kardel int auth,
1474 1.14 christos size_t qsize,
1475 1.9 christos const char *qdata,
1476 1.4 kardel u_short *rstatus,
1477 1.14 christos size_t *rsize,
1478 1.4 kardel const char **rdata
1479 1.4 kardel )
1480 1.4 kardel {
1481 1.4 kardel return doqueryex(opcode, associd, auth, qsize, qdata, rstatus,
1482 1.4 kardel rsize, rdata, FALSE);
1483 1.4 kardel }
1484 1.4 kardel
1485 1.4 kardel
1486 1.4 kardel /*
1487 1.4 kardel * doqueryex - send a request and process the response, optionally
1488 1.4 kardel * displaying error messages for any error responses.
1489 1.4 kardel */
1490 1.4 kardel int
1491 1.4 kardel doqueryex(
1492 1.4 kardel int opcode,
1493 1.4 kardel associd_t associd,
1494 1.1 kardel int auth,
1495 1.14 christos size_t qsize,
1496 1.9 christos const char *qdata,
1497 1.1 kardel u_short *rstatus,
1498 1.14 christos size_t *rsize,
1499 1.4 kardel const char **rdata,
1500 1.4 kardel int quiet
1501 1.1 kardel )
1502 1.1 kardel {
1503 1.1 kardel int res;
1504 1.1 kardel int done;
1505 1.1 kardel
1506 1.1 kardel /*
1507 1.1 kardel * Check to make sure host is open
1508 1.1 kardel */
1509 1.1 kardel if (!havehost) {
1510 1.4 kardel fprintf(stderr, "***No host open, use `host' command\n");
1511 1.1 kardel return -1;
1512 1.1 kardel }
1513 1.1 kardel
1514 1.1 kardel done = 0;
1515 1.1 kardel sequence++;
1516 1.1 kardel
1517 1.1 kardel again:
1518 1.1 kardel /*
1519 1.1 kardel * send a request
1520 1.1 kardel */
1521 1.1 kardel res = sendrequest(opcode, associd, auth, qsize, qdata);
1522 1.1 kardel if (res != 0)
1523 1.4 kardel return res;
1524 1.9 christos
1525 1.1 kardel /*
1526 1.1 kardel * Get the response. If we got a standard error, print a message
1527 1.1 kardel */
1528 1.1 kardel res = getresponse(opcode, associd, rstatus, rsize, rdata, done);
1529 1.1 kardel
1530 1.1 kardel if (res > 0) {
1531 1.1 kardel if (!done && (res == ERR_TIMEOUT || res == ERR_INCOMPLETE)) {
1532 1.1 kardel if (res == ERR_INCOMPLETE) {
1533 1.1 kardel /*
1534 1.1 kardel * better bump the sequence so we don't
1535 1.1 kardel * get confused about differing fragments.
1536 1.1 kardel */
1537 1.1 kardel sequence++;
1538 1.1 kardel }
1539 1.1 kardel done = 1;
1540 1.1 kardel goto again;
1541 1.1 kardel }
1542 1.4 kardel if (!quiet)
1543 1.4 kardel show_error_msg(res, associd);
1544 1.4 kardel
1545 1.1 kardel }
1546 1.1 kardel return res;
1547 1.1 kardel }
1548 1.1 kardel
1549 1.1 kardel
1550 1.1 kardel #ifndef BUILD_AS_LIB
1551 1.1 kardel /*
1552 1.1 kardel * getcmds - read commands from the standard input and execute them
1553 1.1 kardel */
1554 1.1 kardel static void
1555 1.1 kardel getcmds(void)
1556 1.1 kardel {
1557 1.1 kardel char * line;
1558 1.1 kardel int count;
1559 1.1 kardel
1560 1.1 kardel ntp_readline_init(interactive ? prompt : NULL);
1561 1.1 kardel
1562 1.1 kardel for (;;) {
1563 1.1 kardel line = ntp_readline(&count);
1564 1.1 kardel if (NULL == line)
1565 1.1 kardel break;
1566 1.1 kardel docmd(line);
1567 1.1 kardel free(line);
1568 1.1 kardel }
1569 1.1 kardel
1570 1.1 kardel ntp_readline_uninit();
1571 1.1 kardel }
1572 1.1 kardel #endif /* !BUILD_AS_LIB */
1573 1.1 kardel
1574 1.1 kardel
1575 1.1 kardel #if !defined(SYS_WINNT) && !defined(BUILD_AS_LIB)
1576 1.1 kardel /*
1577 1.1 kardel * abortcmd - catch interrupts and abort the current command
1578 1.1 kardel */
1579 1.14 christos static int
1580 1.14 christos abortcmd(void)
1581 1.1 kardel {
1582 1.1 kardel if (current_output == stdout)
1583 1.14 christos (void) fflush(stdout);
1584 1.1 kardel putc('\n', stderr);
1585 1.1 kardel (void) fflush(stderr);
1586 1.14 christos if (jump) {
1587 1.14 christos jump = 0;
1588 1.20 christos LONGJMP(interrupt_buf, 1);
1589 1.14 christos }
1590 1.14 christos return TRUE;
1591 1.1 kardel }
1592 1.1 kardel #endif /* !SYS_WINNT && !BUILD_AS_LIB */
1593 1.1 kardel
1594 1.1 kardel
1595 1.1 kardel #ifndef BUILD_AS_LIB
1596 1.1 kardel /*
1597 1.1 kardel * docmd - decode the command line and execute a command
1598 1.1 kardel */
1599 1.1 kardel static void
1600 1.1 kardel docmd(
1601 1.1 kardel const char *cmdline
1602 1.1 kardel )
1603 1.1 kardel {
1604 1.1 kardel char *tokens[1+MAXARGS+2];
1605 1.1 kardel struct parse pcmd;
1606 1.1 kardel int ntok;
1607 1.1 kardel static int i;
1608 1.1 kardel struct xcmd *xcmd;
1609 1.1 kardel
1610 1.1 kardel /*
1611 1.1 kardel * Tokenize the command line. If nothing on it, return.
1612 1.1 kardel */
1613 1.1 kardel tokenize(cmdline, tokens, &ntok);
1614 1.1 kardel if (ntok == 0)
1615 1.1 kardel return;
1616 1.9 christos
1617 1.1 kardel /*
1618 1.1 kardel * Find the appropriate command description.
1619 1.1 kardel */
1620 1.1 kardel i = findcmd(tokens[0], builtins, opcmds, &xcmd);
1621 1.1 kardel if (i == 0) {
1622 1.1 kardel (void) fprintf(stderr, "***Command `%s' unknown\n",
1623 1.1 kardel tokens[0]);
1624 1.1 kardel return;
1625 1.1 kardel } else if (i >= 2) {
1626 1.1 kardel (void) fprintf(stderr, "***Command `%s' ambiguous\n",
1627 1.1 kardel tokens[0]);
1628 1.1 kardel return;
1629 1.1 kardel }
1630 1.9 christos
1631 1.9 christos /* Warn about ignored extra args */
1632 1.9 christos for (i = MAXARGS + 1; i < ntok ; ++i) {
1633 1.9 christos fprintf(stderr, "***Extra arg `%s' ignored\n", tokens[i]);
1634 1.9 christos }
1635 1.9 christos
1636 1.1 kardel /*
1637 1.1 kardel * Save the keyword, then walk through the arguments, interpreting
1638 1.1 kardel * as we go.
1639 1.1 kardel */
1640 1.1 kardel pcmd.keyword = tokens[0];
1641 1.1 kardel pcmd.nargs = 0;
1642 1.1 kardel for (i = 0; i < MAXARGS && xcmd->arg[i] != NO; i++) {
1643 1.1 kardel if ((i+1) >= ntok) {
1644 1.1 kardel if (!(xcmd->arg[i] & OPT)) {
1645 1.1 kardel printusage(xcmd, stderr);
1646 1.1 kardel return;
1647 1.1 kardel }
1648 1.1 kardel break;
1649 1.1 kardel }
1650 1.1 kardel if ((xcmd->arg[i] & OPT) && (*tokens[i+1] == '>'))
1651 1.1 kardel break;
1652 1.1 kardel if (!getarg(tokens[i+1], (int)xcmd->arg[i], &pcmd.argval[i]))
1653 1.1 kardel return;
1654 1.1 kardel pcmd.nargs++;
1655 1.1 kardel }
1656 1.1 kardel
1657 1.1 kardel i++;
1658 1.1 kardel if (i < ntok && *tokens[i] == '>') {
1659 1.1 kardel char *fname;
1660 1.1 kardel
1661 1.1 kardel if (*(tokens[i]+1) != '\0')
1662 1.1 kardel fname = tokens[i]+1;
1663 1.1 kardel else if ((i+1) < ntok)
1664 1.1 kardel fname = tokens[i+1];
1665 1.1 kardel else {
1666 1.1 kardel (void) fprintf(stderr, "***No file for redirect\n");
1667 1.1 kardel return;
1668 1.1 kardel }
1669 1.1 kardel
1670 1.1 kardel current_output = fopen(fname, "w");
1671 1.1 kardel if (current_output == NULL) {
1672 1.1 kardel (void) fprintf(stderr, "***Error opening %s: ", fname);
1673 1.1 kardel perror("");
1674 1.1 kardel return;
1675 1.1 kardel }
1676 1.1 kardel } else {
1677 1.1 kardel current_output = stdout;
1678 1.1 kardel }
1679 1.1 kardel
1680 1.20 christos if (interactive) {
1681 1.20 christos if ( ! SETJMP(interrupt_buf)) {
1682 1.20 christos jump = 1;
1683 1.20 christos (xcmd->handler)(&pcmd, current_output);
1684 1.20 christos jump = 0;
1685 1.20 christos } else {
1686 1.20 christos fflush(current_output);
1687 1.20 christos fputs("\n >>> command aborted <<<\n", stderr);
1688 1.20 christos fflush(stderr);
1689 1.20 christos }
1690 1.20 christos
1691 1.20 christos } else {
1692 1.1 kardel jump = 0;
1693 1.1 kardel (xcmd->handler)(&pcmd, current_output);
1694 1.1 kardel }
1695 1.20 christos if ((NULL != current_output) && (stdout != current_output)) {
1696 1.20 christos (void)fclose(current_output);
1697 1.20 christos current_output = NULL;
1698 1.20 christos }
1699 1.1 kardel }
1700 1.1 kardel
1701 1.1 kardel
1702 1.1 kardel /*
1703 1.1 kardel * tokenize - turn a command line into tokens
1704 1.1 kardel *
1705 1.9 christos * SK: Modified to allow a quoted string
1706 1.1 kardel *
1707 1.1 kardel * HMS: If the first character of the first token is a ':' then (after
1708 1.1 kardel * eating inter-token whitespace) the 2nd token is the rest of the line.
1709 1.1 kardel */
1710 1.1 kardel
1711 1.1 kardel static void
1712 1.1 kardel tokenize(
1713 1.1 kardel const char *line,
1714 1.1 kardel char **tokens,
1715 1.1 kardel int *ntok
1716 1.1 kardel )
1717 1.1 kardel {
1718 1.1 kardel register const char *cp;
1719 1.1 kardel register char *sp;
1720 1.1 kardel static char tspace[MAXLINE];
1721 1.1 kardel
1722 1.1 kardel sp = tspace;
1723 1.1 kardel cp = line;
1724 1.1 kardel for (*ntok = 0; *ntok < MAXTOKENS; (*ntok)++) {
1725 1.1 kardel tokens[*ntok] = sp;
1726 1.1 kardel
1727 1.1 kardel /* Skip inter-token whitespace */
1728 1.1 kardel while (ISSPACE(*cp))
1729 1.1 kardel cp++;
1730 1.1 kardel
1731 1.1 kardel /* If we're at EOL we're done */
1732 1.1 kardel if (ISEOL(*cp))
1733 1.1 kardel break;
1734 1.1 kardel
1735 1.1 kardel /* If this is the 2nd token and the first token begins
1736 1.1 kardel * with a ':', then just grab to EOL.
1737 1.1 kardel */
1738 1.1 kardel
1739 1.1 kardel if (*ntok == 1 && tokens[0][0] == ':') {
1740 1.1 kardel do {
1741 1.10 christos if (sp - tspace >= MAXLINE)
1742 1.10 christos goto toobig;
1743 1.1 kardel *sp++ = *cp++;
1744 1.1 kardel } while (!ISEOL(*cp));
1745 1.1 kardel }
1746 1.1 kardel
1747 1.1 kardel /* Check if this token begins with a double quote.
1748 1.1 kardel * If yes, continue reading till the next double quote
1749 1.1 kardel */
1750 1.1 kardel else if (*cp == '\"') {
1751 1.1 kardel ++cp;
1752 1.1 kardel do {
1753 1.10 christos if (sp - tspace >= MAXLINE)
1754 1.10 christos goto toobig;
1755 1.1 kardel *sp++ = *cp++;
1756 1.1 kardel } while ((*cp != '\"') && !ISEOL(*cp));
1757 1.1 kardel /* HMS: a missing closing " should be an error */
1758 1.1 kardel }
1759 1.1 kardel else {
1760 1.1 kardel do {
1761 1.10 christos if (sp - tspace >= MAXLINE)
1762 1.10 christos goto toobig;
1763 1.1 kardel *sp++ = *cp++;
1764 1.1 kardel } while ((*cp != '\"') && !ISSPACE(*cp) && !ISEOL(*cp));
1765 1.1 kardel /* HMS: Why check for a " in the previous line? */
1766 1.1 kardel }
1767 1.1 kardel
1768 1.10 christos if (sp - tspace >= MAXLINE)
1769 1.10 christos goto toobig;
1770 1.1 kardel *sp++ = '\0';
1771 1.1 kardel }
1772 1.10 christos return;
1773 1.10 christos
1774 1.10 christos toobig:
1775 1.10 christos *ntok = 0;
1776 1.10 christos fprintf(stderr,
1777 1.10 christos "***Line `%s' is too big\n",
1778 1.10 christos line);
1779 1.10 christos return;
1780 1.1 kardel }
1781 1.1 kardel
1782 1.1 kardel
1783 1.1 kardel /*
1784 1.1 kardel * getarg - interpret an argument token
1785 1.1 kardel */
1786 1.1 kardel static int
1787 1.1 kardel getarg(
1788 1.9 christos const char *str,
1789 1.1 kardel int code,
1790 1.1 kardel arg_v *argp
1791 1.1 kardel )
1792 1.1 kardel {
1793 1.9 christos u_long ul;
1794 1.1 kardel
1795 1.1 kardel switch (code & ~OPT) {
1796 1.9 christos case NTP_STR:
1797 1.1 kardel argp->string = str;
1798 1.1 kardel break;
1799 1.9 christos
1800 1.9 christos case NTP_ADD:
1801 1.9 christos if (!getnetnum(str, &argp->netnum, NULL, 0))
1802 1.1 kardel return 0;
1803 1.1 kardel break;
1804 1.9 christos
1805 1.9 christos case NTP_UINT:
1806 1.9 christos if ('&' == str[0]) {
1807 1.9 christos if (!atouint(&str[1], &ul)) {
1808 1.9 christos fprintf(stderr,
1809 1.9 christos "***Association index `%s' invalid/undecodable\n",
1810 1.9 christos str);
1811 1.1 kardel return 0;
1812 1.1 kardel }
1813 1.9 christos if (0 == numassoc) {
1814 1.9 christos dogetassoc(stdout);
1815 1.9 christos if (0 == numassoc) {
1816 1.9 christos fprintf(stderr,
1817 1.9 christos "***No associations found, `%s' unknown\n",
1818 1.9 christos str);
1819 1.1 kardel return 0;
1820 1.1 kardel }
1821 1.1 kardel }
1822 1.9 christos ul = min(ul, numassoc);
1823 1.9 christos argp->uval = assoc_cache[ul - 1].assid;
1824 1.1 kardel break;
1825 1.1 kardel }
1826 1.9 christos if (!atouint(str, &argp->uval)) {
1827 1.9 christos fprintf(stderr, "***Illegal unsigned value %s\n",
1828 1.9 christos str);
1829 1.9 christos return 0;
1830 1.1 kardel }
1831 1.9 christos break;
1832 1.1 kardel
1833 1.9 christos case NTP_INT:
1834 1.9 christos if (!atoint(str, &argp->ival)) {
1835 1.9 christos fprintf(stderr, "***Illegal integer value %s\n",
1836 1.9 christos str);
1837 1.9 christos return 0;
1838 1.1 kardel }
1839 1.1 kardel break;
1840 1.9 christos
1841 1.9 christos case IP_VERSION:
1842 1.9 christos if (!strcmp("-6", str)) {
1843 1.9 christos argp->ival = 6;
1844 1.9 christos } else if (!strcmp("-4", str)) {
1845 1.9 christos argp->ival = 4;
1846 1.9 christos } else {
1847 1.9 christos fprintf(stderr, "***Version must be either 4 or 6\n");
1848 1.1 kardel return 0;
1849 1.1 kardel }
1850 1.1 kardel break;
1851 1.1 kardel }
1852 1.1 kardel
1853 1.1 kardel return 1;
1854 1.1 kardel }
1855 1.1 kardel #endif /* !BUILD_AS_LIB */
1856 1.1 kardel
1857 1.1 kardel
1858 1.1 kardel /*
1859 1.1 kardel * findcmd - find a command in a command description table
1860 1.1 kardel */
1861 1.1 kardel static int
1862 1.1 kardel findcmd(
1863 1.9 christos const char * str,
1864 1.9 christos struct xcmd * clist1,
1865 1.9 christos struct xcmd * clist2,
1866 1.9 christos struct xcmd ** cmd
1867 1.1 kardel )
1868 1.1 kardel {
1869 1.9 christos struct xcmd *cl;
1870 1.14 christos size_t clen;
1871 1.1 kardel int nmatch;
1872 1.1 kardel struct xcmd *nearmatch = NULL;
1873 1.1 kardel struct xcmd *clist;
1874 1.1 kardel
1875 1.1 kardel clen = strlen(str);
1876 1.1 kardel nmatch = 0;
1877 1.1 kardel if (clist1 != 0)
1878 1.1 kardel clist = clist1;
1879 1.1 kardel else if (clist2 != 0)
1880 1.1 kardel clist = clist2;
1881 1.1 kardel else
1882 1.1 kardel return 0;
1883 1.1 kardel
1884 1.1 kardel again:
1885 1.1 kardel for (cl = clist; cl->keyword != 0; cl++) {
1886 1.1 kardel /* do a first character check, for efficiency */
1887 1.1 kardel if (*str != *(cl->keyword))
1888 1.1 kardel continue;
1889 1.1 kardel if (strncmp(str, cl->keyword, (unsigned)clen) == 0) {
1890 1.1 kardel /*
1891 1.1 kardel * Could be extact match, could be approximate.
1892 1.1 kardel * Is exact if the length of the keyword is the
1893 1.1 kardel * same as the str.
1894 1.1 kardel */
1895 1.1 kardel if (*((cl->keyword) + clen) == '\0') {
1896 1.1 kardel *cmd = cl;
1897 1.1 kardel return 1;
1898 1.1 kardel }
1899 1.1 kardel nmatch++;
1900 1.1 kardel nearmatch = cl;
1901 1.1 kardel }
1902 1.1 kardel }
1903 1.1 kardel
1904 1.1 kardel /*
1905 1.1 kardel * See if there is more to do. If so, go again. Sorry about the
1906 1.1 kardel * goto, too much looking at BSD sources...
1907 1.1 kardel */
1908 1.1 kardel if (clist == clist1 && clist2 != 0) {
1909 1.1 kardel clist = clist2;
1910 1.1 kardel goto again;
1911 1.1 kardel }
1912 1.1 kardel
1913 1.1 kardel /*
1914 1.1 kardel * If we got extactly 1 near match, use it, else return number
1915 1.1 kardel * of matches.
1916 1.1 kardel */
1917 1.1 kardel if (nmatch == 1) {
1918 1.1 kardel *cmd = nearmatch;
1919 1.1 kardel return 1;
1920 1.1 kardel }
1921 1.1 kardel return nmatch;
1922 1.1 kardel }
1923 1.1 kardel
1924 1.1 kardel
1925 1.1 kardel /*
1926 1.1 kardel * getnetnum - given a host name, return its net number
1927 1.1 kardel * and (optional) full name
1928 1.1 kardel */
1929 1.1 kardel int
1930 1.1 kardel getnetnum(
1931 1.1 kardel const char *hname,
1932 1.1 kardel sockaddr_u *num,
1933 1.1 kardel char *fullhost,
1934 1.1 kardel int af
1935 1.1 kardel )
1936 1.1 kardel {
1937 1.1 kardel struct addrinfo hints, *ai = NULL;
1938 1.1 kardel
1939 1.4 kardel ZERO(hints);
1940 1.1 kardel hints.ai_flags = AI_CANONNAME;
1941 1.1 kardel #ifdef AI_ADDRCONFIG
1942 1.1 kardel hints.ai_flags |= AI_ADDRCONFIG;
1943 1.1 kardel #endif
1944 1.9 christos
1945 1.4 kardel /*
1946 1.4 kardel * decodenetnum only works with addresses, but handles syntax
1947 1.4 kardel * that getaddrinfo doesn't: [2001::1]:1234
1948 1.4 kardel */
1949 1.1 kardel if (decodenetnum(hname, num)) {
1950 1.4 kardel if (fullhost != NULL)
1951 1.4 kardel getnameinfo(&num->sa, SOCKLEN(num), fullhost,
1952 1.4 kardel LENHOSTNAME, NULL, 0, 0);
1953 1.1 kardel return 1;
1954 1.1 kardel } else if (getaddrinfo(hname, "ntp", &hints, &ai) == 0) {
1955 1.9 christos INSIST(sizeof(*num) >= ai->ai_addrlen);
1956 1.4 kardel memcpy(num, ai->ai_addr, ai->ai_addrlen);
1957 1.4 kardel if (fullhost != NULL) {
1958 1.9 christos if (ai->ai_canonname != NULL)
1959 1.9 christos strlcpy(fullhost, ai->ai_canonname,
1960 1.4 kardel LENHOSTNAME);
1961 1.9 christos else
1962 1.4 kardel getnameinfo(&num->sa, SOCKLEN(num),
1963 1.4 kardel fullhost, LENHOSTNAME, NULL,
1964 1.4 kardel 0, 0);
1965 1.4 kardel }
1966 1.9 christos freeaddrinfo(ai);
1967 1.1 kardel return 1;
1968 1.1 kardel }
1969 1.4 kardel fprintf(stderr, "***Can't find host %s\n", hname);
1970 1.4 kardel
1971 1.4 kardel return 0;
1972 1.1 kardel }
1973 1.1 kardel
1974 1.9 christos
1975 1.1 kardel /*
1976 1.1 kardel * nntohost - convert network number to host name. This routine enforces
1977 1.1 kardel * the showhostnames setting.
1978 1.1 kardel */
1979 1.4 kardel const char *
1980 1.1 kardel nntohost(
1981 1.1 kardel sockaddr_u *netnum
1982 1.1 kardel )
1983 1.1 kardel {
1984 1.4 kardel return nntohost_col(netnum, LIB_BUFLENGTH - 1, FALSE);
1985 1.4 kardel }
1986 1.4 kardel
1987 1.4 kardel
1988 1.4 kardel /*
1989 1.4 kardel * nntohost_col - convert network number to host name in fixed width.
1990 1.4 kardel * This routine enforces the showhostnames setting.
1991 1.4 kardel * When displaying hostnames longer than the width,
1992 1.4 kardel * the first part of the hostname is displayed. When
1993 1.4 kardel * displaying numeric addresses longer than the width,
1994 1.4 kardel * Such as IPv6 addresses, the caller decides whether
1995 1.4 kardel * the first or last of the numeric address is used.
1996 1.4 kardel */
1997 1.4 kardel const char *
1998 1.4 kardel nntohost_col(
1999 1.4 kardel sockaddr_u * addr,
2000 1.4 kardel size_t width,
2001 1.4 kardel int preserve_lowaddrbits
2002 1.4 kardel )
2003 1.4 kardel {
2004 1.4 kardel const char * out;
2005 1.4 kardel
2006 1.9 christos if (!showhostnames || SOCK_UNSPEC(addr)) {
2007 1.4 kardel if (preserve_lowaddrbits)
2008 1.4 kardel out = trunc_left(stoa(addr), width);
2009 1.4 kardel else
2010 1.4 kardel out = trunc_right(stoa(addr), width);
2011 1.4 kardel } else if (ISREFCLOCKADR(addr)) {
2012 1.4 kardel out = refnumtoa(addr);
2013 1.4 kardel } else {
2014 1.4 kardel out = trunc_right(socktohost(addr), width);
2015 1.4 kardel }
2016 1.4 kardel return out;
2017 1.1 kardel }
2018 1.1 kardel
2019 1.1 kardel
2020 1.1 kardel /*
2021 1.9 christos * nntohostp() is the same as nntohost() plus a :port suffix
2022 1.9 christos */
2023 1.9 christos const char *
2024 1.9 christos nntohostp(
2025 1.9 christos sockaddr_u *netnum
2026 1.9 christos )
2027 1.9 christos {
2028 1.9 christos const char * hostn;
2029 1.9 christos char * buf;
2030 1.9 christos
2031 1.9 christos if (!showhostnames || SOCK_UNSPEC(netnum))
2032 1.9 christos return sptoa(netnum);
2033 1.9 christos else if (ISREFCLOCKADR(netnum))
2034 1.9 christos return refnumtoa(netnum);
2035 1.9 christos
2036 1.9 christos hostn = socktohost(netnum);
2037 1.9 christos LIB_GETBUF(buf);
2038 1.9 christos snprintf(buf, LIB_BUFLENGTH, "%s:%u", hostn, SRCPORT(netnum));
2039 1.9 christos
2040 1.9 christos return buf;
2041 1.9 christos }
2042 1.9 christos
2043 1.9 christos /*
2044 1.1 kardel * rtdatetolfp - decode an RT-11 date into an l_fp
2045 1.1 kardel */
2046 1.1 kardel static int
2047 1.1 kardel rtdatetolfp(
2048 1.1 kardel char *str,
2049 1.1 kardel l_fp *lfp
2050 1.1 kardel )
2051 1.1 kardel {
2052 1.1 kardel register char *cp;
2053 1.1 kardel register int i;
2054 1.1 kardel struct calendar cal;
2055 1.1 kardel char buf[4];
2056 1.1 kardel
2057 1.1 kardel cal.yearday = 0;
2058 1.1 kardel
2059 1.1 kardel /*
2060 1.1 kardel * An RT-11 date looks like:
2061 1.1 kardel *
2062 1.1 kardel * d[d]-Mth-y[y] hh:mm:ss
2063 1.1 kardel *
2064 1.1 kardel * (No docs, but assume 4-digit years are also legal...)
2065 1.1 kardel *
2066 1.1 kardel * d[d]-Mth-y[y[y[y]]] hh:mm:ss
2067 1.1 kardel */
2068 1.1 kardel cp = str;
2069 1.19 christos if (!isdigit(pgetc(cp))) {
2070 1.1 kardel if (*cp == '-') {
2071 1.1 kardel /*
2072 1.1 kardel * Catch special case
2073 1.1 kardel */
2074 1.1 kardel L_CLR(lfp);
2075 1.1 kardel return 1;
2076 1.1 kardel }
2077 1.1 kardel return 0;
2078 1.1 kardel }
2079 1.1 kardel
2080 1.1 kardel cal.monthday = (u_char) (*cp++ - '0'); /* ascii dependent */
2081 1.19 christos if (isdigit(pgetc(cp))) {
2082 1.1 kardel cal.monthday = (u_char)((cal.monthday << 3) + (cal.monthday << 1));
2083 1.1 kardel cal.monthday = (u_char)(cal.monthday + *cp++ - '0');
2084 1.1 kardel }
2085 1.1 kardel
2086 1.1 kardel if (*cp++ != '-')
2087 1.1 kardel return 0;
2088 1.9 christos
2089 1.1 kardel for (i = 0; i < 3; i++)
2090 1.1 kardel buf[i] = *cp++;
2091 1.1 kardel buf[3] = '\0';
2092 1.1 kardel
2093 1.1 kardel for (i = 0; i < 12; i++)
2094 1.1 kardel if (STREQ(buf, months[i]))
2095 1.1 kardel break;
2096 1.1 kardel if (i == 12)
2097 1.1 kardel return 0;
2098 1.1 kardel cal.month = (u_char)(i + 1);
2099 1.1 kardel
2100 1.1 kardel if (*cp++ != '-')
2101 1.1 kardel return 0;
2102 1.9 christos
2103 1.19 christos if (!isdigit(pgetc(cp)))
2104 1.1 kardel return 0;
2105 1.1 kardel cal.year = (u_short)(*cp++ - '0');
2106 1.19 christos if (isdigit(pgetc(cp))) {
2107 1.1 kardel cal.year = (u_short)((cal.year << 3) + (cal.year << 1));
2108 1.1 kardel cal.year = (u_short)(*cp++ - '0');
2109 1.1 kardel }
2110 1.19 christos if (isdigit(pgetc(cp))) {
2111 1.1 kardel cal.year = (u_short)((cal.year << 3) + (cal.year << 1));
2112 1.1 kardel cal.year = (u_short)(cal.year + *cp++ - '0');
2113 1.1 kardel }
2114 1.19 christos if (isdigit(pgetc(cp))) {
2115 1.1 kardel cal.year = (u_short)((cal.year << 3) + (cal.year << 1));
2116 1.1 kardel cal.year = (u_short)(cal.year + *cp++ - '0');
2117 1.1 kardel }
2118 1.1 kardel
2119 1.1 kardel /*
2120 1.1 kardel * Catch special case. If cal.year == 0 this is a zero timestamp.
2121 1.1 kardel */
2122 1.1 kardel if (cal.year == 0) {
2123 1.1 kardel L_CLR(lfp);
2124 1.1 kardel return 1;
2125 1.1 kardel }
2126 1.1 kardel
2127 1.19 christos if (*cp++ != ' ' || !isdigit(pgetc(cp)))
2128 1.1 kardel return 0;
2129 1.1 kardel cal.hour = (u_char)(*cp++ - '0');
2130 1.19 christos if (isdigit(pgetc(cp))) {
2131 1.1 kardel cal.hour = (u_char)((cal.hour << 3) + (cal.hour << 1));
2132 1.1 kardel cal.hour = (u_char)(cal.hour + *cp++ - '0');
2133 1.1 kardel }
2134 1.1 kardel
2135 1.19 christos if (*cp++ != ':' || !isdigit(pgetc(cp)))
2136 1.1 kardel return 0;
2137 1.1 kardel cal.minute = (u_char)(*cp++ - '0');
2138 1.19 christos if (isdigit(pgetc(cp))) {
2139 1.1 kardel cal.minute = (u_char)((cal.minute << 3) + (cal.minute << 1));
2140 1.1 kardel cal.minute = (u_char)(cal.minute + *cp++ - '0');
2141 1.1 kardel }
2142 1.1 kardel
2143 1.19 christos if (*cp++ != ':' || !isdigit(pgetc(cp)))
2144 1.1 kardel return 0;
2145 1.1 kardel cal.second = (u_char)(*cp++ - '0');
2146 1.19 christos if (isdigit(pgetc(cp))) {
2147 1.1 kardel cal.second = (u_char)((cal.second << 3) + (cal.second << 1));
2148 1.1 kardel cal.second = (u_char)(cal.second + *cp++ - '0');
2149 1.1 kardel }
2150 1.1 kardel
2151 1.1 kardel /*
2152 1.1 kardel * For RT-11, 1972 seems to be the pivot year
2153 1.1 kardel */
2154 1.1 kardel if (cal.year < 72)
2155 1.1 kardel cal.year += 2000;
2156 1.1 kardel if (cal.year < 100)
2157 1.1 kardel cal.year += 1900;
2158 1.1 kardel
2159 1.1 kardel lfp->l_ui = caltontp(&cal);
2160 1.1 kardel lfp->l_uf = 0;
2161 1.1 kardel return 1;
2162 1.1 kardel }
2163 1.1 kardel
2164 1.1 kardel
2165 1.1 kardel /*
2166 1.1 kardel * decodets - decode a timestamp into an l_fp format number, with
2167 1.1 kardel * consideration of fuzzball formats.
2168 1.1 kardel */
2169 1.1 kardel int
2170 1.1 kardel decodets(
2171 1.1 kardel char *str,
2172 1.1 kardel l_fp *lfp
2173 1.1 kardel )
2174 1.1 kardel {
2175 1.4 kardel char *cp;
2176 1.4 kardel char buf[30];
2177 1.4 kardel size_t b;
2178 1.4 kardel
2179 1.1 kardel /*
2180 1.1 kardel * If it starts with a 0x, decode as hex.
2181 1.1 kardel */
2182 1.1 kardel if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
2183 1.1 kardel return hextolfp(str+2, lfp);
2184 1.1 kardel
2185 1.1 kardel /*
2186 1.1 kardel * If it starts with a '"', try it as an RT-11 date.
2187 1.1 kardel */
2188 1.1 kardel if (*str == '"') {
2189 1.4 kardel cp = str + 1;
2190 1.4 kardel b = 0;
2191 1.4 kardel while ('"' != *cp && '\0' != *cp &&
2192 1.4 kardel b < COUNTOF(buf) - 1)
2193 1.4 kardel buf[b++] = *cp++;
2194 1.4 kardel buf[b] = '\0';
2195 1.1 kardel return rtdatetolfp(buf, lfp);
2196 1.1 kardel }
2197 1.1 kardel
2198 1.1 kardel /*
2199 1.1 kardel * Might still be hex. Check out the first character. Talk
2200 1.1 kardel * about heuristics!
2201 1.1 kardel */
2202 1.1 kardel if ((*str >= 'A' && *str <= 'F') || (*str >= 'a' && *str <= 'f'))
2203 1.1 kardel return hextolfp(str, lfp);
2204 1.1 kardel
2205 1.1 kardel /*
2206 1.1 kardel * Try it as a decimal. If this fails, try as an unquoted
2207 1.1 kardel * RT-11 date. This code should go away eventually.
2208 1.1 kardel */
2209 1.1 kardel if (atolfp(str, lfp))
2210 1.1 kardel return 1;
2211 1.1 kardel
2212 1.1 kardel return rtdatetolfp(str, lfp);
2213 1.1 kardel }
2214 1.1 kardel
2215 1.1 kardel
2216 1.1 kardel /*
2217 1.1 kardel * decodetime - decode a time value. It should be in milliseconds
2218 1.1 kardel */
2219 1.1 kardel int
2220 1.1 kardel decodetime(
2221 1.1 kardel char *str,
2222 1.1 kardel l_fp *lfp
2223 1.1 kardel )
2224 1.1 kardel {
2225 1.1 kardel return mstolfp(str, lfp);
2226 1.1 kardel }
2227 1.1 kardel
2228 1.1 kardel
2229 1.1 kardel /*
2230 1.1 kardel * decodeint - decode an integer
2231 1.1 kardel */
2232 1.1 kardel int
2233 1.1 kardel decodeint(
2234 1.1 kardel char *str,
2235 1.1 kardel long *val
2236 1.1 kardel )
2237 1.1 kardel {
2238 1.1 kardel if (*str == '0') {
2239 1.1 kardel if (*(str+1) == 'x' || *(str+1) == 'X')
2240 1.1 kardel return hextoint(str+2, (u_long *)val);
2241 1.1 kardel return octtoint(str, (u_long *)val);
2242 1.1 kardel }
2243 1.1 kardel return atoint(str, val);
2244 1.1 kardel }
2245 1.1 kardel
2246 1.1 kardel
2247 1.1 kardel /*
2248 1.1 kardel * decodeuint - decode an unsigned integer
2249 1.1 kardel */
2250 1.1 kardel int
2251 1.1 kardel decodeuint(
2252 1.1 kardel char *str,
2253 1.1 kardel u_long *val
2254 1.1 kardel )
2255 1.1 kardel {
2256 1.1 kardel if (*str == '0') {
2257 1.1 kardel if (*(str + 1) == 'x' || *(str + 1) == 'X')
2258 1.1 kardel return (hextoint(str + 2, val));
2259 1.1 kardel return (octtoint(str, val));
2260 1.1 kardel }
2261 1.1 kardel return (atouint(str, val));
2262 1.1 kardel }
2263 1.1 kardel
2264 1.1 kardel
2265 1.1 kardel /*
2266 1.1 kardel * decodearr - decode an array of time values
2267 1.1 kardel */
2268 1.1 kardel static int
2269 1.1 kardel decodearr(
2270 1.19 christos char *cp,
2271 1.19 christos int *narr,
2272 1.19 christos l_fp *lfpa,
2273 1.19 christos int amax
2274 1.1 kardel )
2275 1.1 kardel {
2276 1.19 christos char *bp;
2277 1.1 kardel char buf[60];
2278 1.1 kardel
2279 1.1 kardel *narr = 0;
2280 1.1 kardel
2281 1.19 christos while (*narr < amax && *cp) {
2282 1.19 christos if (isspace(pgetc(cp))) {
2283 1.19 christos do
2284 1.19 christos ++cp;
2285 1.19 christos while (*cp && isspace(pgetc(cp)));
2286 1.19 christos } else {
2287 1.19 christos bp = buf;
2288 1.19 christos do {
2289 1.19 christos if (bp != (buf + sizeof(buf) - 1))
2290 1.19 christos *bp++ = *cp;
2291 1.19 christos ++cp;
2292 1.19 christos } while (*cp && !isspace(pgetc(cp)));
2293 1.19 christos *bp = '\0';
2294 1.1 kardel
2295 1.19 christos if (!decodetime(buf, lfpa))
2296 1.19 christos return 0;
2297 1.19 christos ++(*narr);
2298 1.19 christos ++lfpa;
2299 1.19 christos }
2300 1.1 kardel }
2301 1.1 kardel return 1;
2302 1.1 kardel }
2303 1.1 kardel
2304 1.1 kardel
2305 1.1 kardel /*
2306 1.1 kardel * Finally, the built in command handlers
2307 1.1 kardel */
2308 1.1 kardel
2309 1.1 kardel /*
2310 1.1 kardel * help - tell about commands, or details of a particular command
2311 1.1 kardel */
2312 1.1 kardel static void
2313 1.1 kardel help(
2314 1.1 kardel struct parse *pcmd,
2315 1.1 kardel FILE *fp
2316 1.1 kardel )
2317 1.1 kardel {
2318 1.1 kardel struct xcmd *xcp = NULL; /* quiet warning */
2319 1.9 christos const char *cmd;
2320 1.1 kardel const char *list[100];
2321 1.4 kardel size_t word, words;
2322 1.4 kardel size_t row, rows;
2323 1.4 kardel size_t col, cols;
2324 1.4 kardel size_t length;
2325 1.1 kardel
2326 1.1 kardel if (pcmd->nargs == 0) {
2327 1.1 kardel words = 0;
2328 1.4 kardel for (xcp = builtins; xcp->keyword != NULL; xcp++) {
2329 1.9 christos if (*(xcp->keyword) != '?' &&
2330 1.9 christos words < COUNTOF(list))
2331 1.1 kardel list[words++] = xcp->keyword;
2332 1.1 kardel }
2333 1.4 kardel for (xcp = opcmds; xcp->keyword != NULL; xcp++)
2334 1.9 christos if (words < COUNTOF(list))
2335 1.9 christos list[words++] = xcp->keyword;
2336 1.1 kardel
2337 1.9 christos qsort((void *)list, words, sizeof(list[0]), helpsort);
2338 1.1 kardel col = 0;
2339 1.1 kardel for (word = 0; word < words; word++) {
2340 1.9 christos length = strlen(list[word]);
2341 1.4 kardel col = max(col, length);
2342 1.1 kardel }
2343 1.1 kardel
2344 1.1 kardel cols = SCREENWIDTH / ++col;
2345 1.1 kardel rows = (words + cols - 1) / cols;
2346 1.1 kardel
2347 1.4 kardel fprintf(fp, "ntpq commands:\n");
2348 1.1 kardel
2349 1.1 kardel for (row = 0; row < rows; row++) {
2350 1.4 kardel for (word = row; word < words; word += rows)
2351 1.9 christos fprintf(fp, "%-*.*s", (int)col,
2352 1.9 christos (int)col - 1, list[word]);
2353 1.4 kardel fprintf(fp, "\n");
2354 1.1 kardel }
2355 1.1 kardel } else {
2356 1.1 kardel cmd = pcmd->argval[0].string;
2357 1.1 kardel words = findcmd(cmd, builtins, opcmds, &xcp);
2358 1.1 kardel if (words == 0) {
2359 1.4 kardel fprintf(stderr,
2360 1.4 kardel "Command `%s' is unknown\n", cmd);
2361 1.1 kardel return;
2362 1.1 kardel } else if (words >= 2) {
2363 1.4 kardel fprintf(stderr,
2364 1.4 kardel "Command `%s' is ambiguous\n", cmd);
2365 1.1 kardel return;
2366 1.1 kardel }
2367 1.4 kardel fprintf(fp, "function: %s\n", xcp->comment);
2368 1.1 kardel printusage(xcp, fp);
2369 1.1 kardel }
2370 1.1 kardel }
2371 1.1 kardel
2372 1.1 kardel
2373 1.1 kardel /*
2374 1.1 kardel * helpsort - do hostname qsort comparisons
2375 1.1 kardel */
2376 1.1 kardel static int
2377 1.1 kardel helpsort(
2378 1.1 kardel const void *t1,
2379 1.1 kardel const void *t2
2380 1.1 kardel )
2381 1.1 kardel {
2382 1.4 kardel const char * const * name1 = t1;
2383 1.4 kardel const char * const * name2 = t2;
2384 1.1 kardel
2385 1.1 kardel return strcmp(*name1, *name2);
2386 1.1 kardel }
2387 1.1 kardel
2388 1.1 kardel
2389 1.1 kardel /*
2390 1.1 kardel * printusage - print usage information for a command
2391 1.1 kardel */
2392 1.1 kardel static void
2393 1.1 kardel printusage(
2394 1.1 kardel struct xcmd *xcp,
2395 1.1 kardel FILE *fp
2396 1.1 kardel )
2397 1.1 kardel {
2398 1.1 kardel register int i;
2399 1.1 kardel
2400 1.9 christos /* XXX: Do we need to warn about extra args here too? */
2401 1.9 christos
2402 1.1 kardel (void) fprintf(fp, "usage: %s", xcp->keyword);
2403 1.1 kardel for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) {
2404 1.1 kardel if (xcp->arg[i] & OPT)
2405 1.1 kardel (void) fprintf(fp, " [ %s ]", xcp->desc[i]);
2406 1.1 kardel else
2407 1.1 kardel (void) fprintf(fp, " %s", xcp->desc[i]);
2408 1.1 kardel }
2409 1.1 kardel (void) fprintf(fp, "\n");
2410 1.1 kardel }
2411 1.1 kardel
2412 1.1 kardel
2413 1.1 kardel /*
2414 1.1 kardel * timeout - set time out time
2415 1.1 kardel */
2416 1.1 kardel static void
2417 1.1 kardel timeout(
2418 1.1 kardel struct parse *pcmd,
2419 1.1 kardel FILE *fp
2420 1.1 kardel )
2421 1.1 kardel {
2422 1.1 kardel int val;
2423 1.1 kardel
2424 1.1 kardel if (pcmd->nargs == 0) {
2425 1.1 kardel val = (int)tvout.tv_sec * 1000 + tvout.tv_usec / 1000;
2426 1.1 kardel (void) fprintf(fp, "primary timeout %d ms\n", val);
2427 1.1 kardel } else {
2428 1.1 kardel tvout.tv_sec = pcmd->argval[0].uval / 1000;
2429 1.1 kardel tvout.tv_usec = (pcmd->argval[0].uval - ((long)tvout.tv_sec * 1000))
2430 1.1 kardel * 1000;
2431 1.1 kardel }
2432 1.1 kardel }
2433 1.1 kardel
2434 1.1 kardel
2435 1.1 kardel /*
2436 1.1 kardel * auth_delay - set delay for auth requests
2437 1.1 kardel */
2438 1.1 kardel static void
2439 1.1 kardel auth_delay(
2440 1.1 kardel struct parse *pcmd,
2441 1.1 kardel FILE *fp
2442 1.1 kardel )
2443 1.1 kardel {
2444 1.1 kardel int isneg;
2445 1.1 kardel u_long val;
2446 1.1 kardel
2447 1.1 kardel if (pcmd->nargs == 0) {
2448 1.1 kardel val = delay_time.l_ui * 1000 + delay_time.l_uf / 4294967;
2449 1.1 kardel (void) fprintf(fp, "delay %lu ms\n", val);
2450 1.1 kardel } else {
2451 1.1 kardel if (pcmd->argval[0].ival < 0) {
2452 1.1 kardel isneg = 1;
2453 1.1 kardel val = (u_long)(-pcmd->argval[0].ival);
2454 1.1 kardel } else {
2455 1.1 kardel isneg = 0;
2456 1.1 kardel val = (u_long)pcmd->argval[0].ival;
2457 1.1 kardel }
2458 1.1 kardel
2459 1.1 kardel delay_time.l_ui = val / 1000;
2460 1.1 kardel val %= 1000;
2461 1.1 kardel delay_time.l_uf = val * 4294967; /* 2**32/1000 */
2462 1.1 kardel
2463 1.1 kardel if (isneg)
2464 1.1 kardel L_NEG(&delay_time);
2465 1.1 kardel }
2466 1.1 kardel }
2467 1.1 kardel
2468 1.1 kardel
2469 1.1 kardel /*
2470 1.1 kardel * host - set the host we are dealing with.
2471 1.1 kardel */
2472 1.1 kardel static void
2473 1.1 kardel host(
2474 1.1 kardel struct parse *pcmd,
2475 1.1 kardel FILE *fp
2476 1.1 kardel )
2477 1.1 kardel {
2478 1.1 kardel int i;
2479 1.1 kardel
2480 1.1 kardel if (pcmd->nargs == 0) {
2481 1.1 kardel if (havehost)
2482 1.1 kardel (void) fprintf(fp, "current host is %s\n",
2483 1.1 kardel currenthost);
2484 1.1 kardel else
2485 1.1 kardel (void) fprintf(fp, "no current host\n");
2486 1.1 kardel return;
2487 1.1 kardel }
2488 1.1 kardel
2489 1.1 kardel i = 0;
2490 1.1 kardel ai_fam_templ = ai_fam_default;
2491 1.1 kardel if (pcmd->nargs == 2) {
2492 1.1 kardel if (!strcmp("-4", pcmd->argval[i].string))
2493 1.1 kardel ai_fam_templ = AF_INET;
2494 1.1 kardel else if (!strcmp("-6", pcmd->argval[i].string))
2495 1.1 kardel ai_fam_templ = AF_INET6;
2496 1.9 christos else
2497 1.9 christos goto no_change;
2498 1.1 kardel i = 1;
2499 1.1 kardel }
2500 1.9 christos if (openhost(pcmd->argval[i].string, ai_fam_templ)) {
2501 1.9 christos fprintf(fp, "current host set to %s\n", currenthost);
2502 1.1 kardel } else {
2503 1.9 christos no_change:
2504 1.1 kardel if (havehost)
2505 1.9 christos fprintf(fp, "current host remains %s\n",
2506 1.9 christos currenthost);
2507 1.1 kardel else
2508 1.9 christos fprintf(fp, "still no current host\n");
2509 1.1 kardel }
2510 1.1 kardel }
2511 1.1 kardel
2512 1.1 kardel
2513 1.1 kardel /*
2514 1.1 kardel * poll - do one (or more) polls of the host via NTP
2515 1.1 kardel */
2516 1.1 kardel /*ARGSUSED*/
2517 1.1 kardel static void
2518 1.1 kardel ntp_poll(
2519 1.1 kardel struct parse *pcmd,
2520 1.1 kardel FILE *fp
2521 1.1 kardel )
2522 1.1 kardel {
2523 1.1 kardel (void) fprintf(fp, "poll not implemented yet\n");
2524 1.1 kardel }
2525 1.1 kardel
2526 1.1 kardel
2527 1.1 kardel /*
2528 1.15 christos * showdrefid2str - return a string explanation of the value of drefid
2529 1.15 christos */
2530 1.15 christos static const char *
2531 1.15 christos showdrefid2str(void)
2532 1.15 christos {
2533 1.15 christos switch (drefid) {
2534 1.15 christos case REFID_HASH:
2535 1.15 christos return "hash";
2536 1.15 christos case REFID_IPV4:
2537 1.15 christos return "ipv4";
2538 1.15 christos default:
2539 1.15 christos return "Unknown";
2540 1.15 christos }
2541 1.15 christos }
2542 1.15 christos
2543 1.15 christos
2544 1.15 christos /*
2545 1.15 christos * drefid - display/change "display hash"
2546 1.15 christos */
2547 1.15 christos static void
2548 1.15 christos showdrefid(
2549 1.15 christos struct parse *pcmd,
2550 1.15 christos FILE *fp
2551 1.15 christos )
2552 1.15 christos {
2553 1.15 christos if (pcmd->nargs == 0) {
2554 1.15 christos (void) fprintf(fp, "drefid value is %s\n", showdrefid2str());
2555 1.15 christos return;
2556 1.15 christos } else if (STREQ(pcmd->argval[0].string, "hash")) {
2557 1.15 christos drefid = REFID_HASH;
2558 1.15 christos } else if (STREQ(pcmd->argval[0].string, "ipv4")) {
2559 1.15 christos drefid = REFID_IPV4;
2560 1.15 christos } else {
2561 1.15 christos (void) fprintf(fp, "What?\n");
2562 1.15 christos return;
2563 1.15 christos }
2564 1.15 christos (void) fprintf(fp, "drefid value set to %s\n", showdrefid2str());
2565 1.15 christos }
2566 1.15 christos
2567 1.15 christos
2568 1.15 christos /*
2569 1.1 kardel * keyid - get a keyid to use for authenticating requests
2570 1.1 kardel */
2571 1.1 kardel static void
2572 1.1 kardel keyid(
2573 1.1 kardel struct parse *pcmd,
2574 1.1 kardel FILE *fp
2575 1.1 kardel )
2576 1.1 kardel {
2577 1.1 kardel if (pcmd->nargs == 0) {
2578 1.1 kardel if (info_auth_keyid == 0)
2579 1.1 kardel (void) fprintf(fp, "no keyid defined\n");
2580 1.1 kardel else
2581 1.1 kardel (void) fprintf(fp, "keyid is %lu\n", (u_long)info_auth_keyid);
2582 1.1 kardel } else {
2583 1.1 kardel /* allow zero so that keyid can be cleared. */
2584 1.1 kardel if(pcmd->argval[0].uval > NTP_MAXKEY)
2585 1.1 kardel (void) fprintf(fp, "Invalid key identifier\n");
2586 1.1 kardel info_auth_keyid = pcmd->argval[0].uval;
2587 1.1 kardel }
2588 1.1 kardel }
2589 1.1 kardel
2590 1.1 kardel /*
2591 1.1 kardel * keytype - get type of key to use for authenticating requests
2592 1.1 kardel */
2593 1.1 kardel static void
2594 1.1 kardel keytype(
2595 1.1 kardel struct parse *pcmd,
2596 1.1 kardel FILE *fp
2597 1.1 kardel )
2598 1.1 kardel {
2599 1.1 kardel const char * digest_name;
2600 1.1 kardel size_t digest_len;
2601 1.1 kardel int key_type;
2602 1.1 kardel
2603 1.1 kardel if (!pcmd->nargs) {
2604 1.6 kardel fprintf(fp, "keytype is %s with %lu octet digests\n",
2605 1.1 kardel keytype_name(info_auth_keytype),
2606 1.4 kardel (u_long)info_auth_hashlen);
2607 1.1 kardel return;
2608 1.1 kardel }
2609 1.1 kardel
2610 1.1 kardel digest_name = pcmd->argval[0].string;
2611 1.1 kardel digest_len = 0;
2612 1.1 kardel key_type = keytype_from_text(digest_name, &digest_len);
2613 1.1 kardel
2614 1.1 kardel if (!key_type) {
2615 1.12 christos fprintf(fp, "keytype is not valid. "
2616 1.1 kardel #ifdef OPENSSL
2617 1.12 christos "Type \"help keytype\" for the available digest types.\n");
2618 1.1 kardel #else
2619 1.12 christos "Only \"md5\" is available.\n");
2620 1.1 kardel #endif
2621 1.1 kardel return;
2622 1.1 kardel }
2623 1.1 kardel
2624 1.1 kardel info_auth_keytype = key_type;
2625 1.1 kardel info_auth_hashlen = digest_len;
2626 1.1 kardel }
2627 1.1 kardel
2628 1.1 kardel
2629 1.1 kardel /*
2630 1.1 kardel * passwd - get an authentication key
2631 1.1 kardel */
2632 1.1 kardel /*ARGSUSED*/
2633 1.1 kardel static void
2634 1.1 kardel passwd(
2635 1.1 kardel struct parse *pcmd,
2636 1.1 kardel FILE *fp
2637 1.1 kardel )
2638 1.1 kardel {
2639 1.9 christos const char *pass;
2640 1.1 kardel
2641 1.1 kardel if (info_auth_keyid == 0) {
2642 1.9 christos info_auth_keyid = getkeyid("Keyid: ");
2643 1.9 christos if (info_auth_keyid == 0) {
2644 1.9 christos (void)fprintf(fp, "Keyid must be defined\n");
2645 1.1 kardel return;
2646 1.1 kardel }
2647 1.1 kardel }
2648 1.4 kardel if (pcmd->nargs >= 1)
2649 1.4 kardel pass = pcmd->argval[0].string;
2650 1.1 kardel else {
2651 1.4 kardel pass = getpass_keytype(info_auth_keytype);
2652 1.4 kardel if ('\0' == pass[0]) {
2653 1.4 kardel fprintf(fp, "Password unchanged\n");
2654 1.4 kardel return;
2655 1.4 kardel }
2656 1.1 kardel }
2657 1.9 christos authusekey(info_auth_keyid, info_auth_keytype,
2658 1.9 christos (const u_char *)pass);
2659 1.4 kardel authtrust(info_auth_keyid, 1);
2660 1.1 kardel }
2661 1.1 kardel
2662 1.1 kardel
2663 1.1 kardel /*
2664 1.1 kardel * hostnames - set the showhostnames flag
2665 1.1 kardel */
2666 1.1 kardel static void
2667 1.1 kardel hostnames(
2668 1.1 kardel struct parse *pcmd,
2669 1.1 kardel FILE *fp
2670 1.1 kardel )
2671 1.1 kardel {
2672 1.1 kardel if (pcmd->nargs == 0) {
2673 1.1 kardel if (showhostnames)
2674 1.1 kardel (void) fprintf(fp, "hostnames being shown\n");
2675 1.1 kardel else
2676 1.1 kardel (void) fprintf(fp, "hostnames not being shown\n");
2677 1.1 kardel } else {
2678 1.1 kardel if (STREQ(pcmd->argval[0].string, "yes"))
2679 1.1 kardel showhostnames = 1;
2680 1.1 kardel else if (STREQ(pcmd->argval[0].string, "no"))
2681 1.1 kardel showhostnames = 0;
2682 1.1 kardel else
2683 1.1 kardel (void)fprintf(stderr, "What?\n");
2684 1.1 kardel }
2685 1.1 kardel }
2686 1.1 kardel
2687 1.1 kardel
2688 1.1 kardel
2689 1.1 kardel /*
2690 1.1 kardel * setdebug - set/change debugging level
2691 1.1 kardel */
2692 1.1 kardel static void
2693 1.1 kardel setdebug(
2694 1.1 kardel struct parse *pcmd,
2695 1.1 kardel FILE *fp
2696 1.1 kardel )
2697 1.1 kardel {
2698 1.1 kardel if (pcmd->nargs == 0) {
2699 1.1 kardel (void) fprintf(fp, "debug level is %d\n", debug);
2700 1.1 kardel return;
2701 1.1 kardel } else if (STREQ(pcmd->argval[0].string, "no")) {
2702 1.1 kardel debug = 0;
2703 1.1 kardel } else if (STREQ(pcmd->argval[0].string, "more")) {
2704 1.1 kardel debug++;
2705 1.1 kardel } else if (STREQ(pcmd->argval[0].string, "less")) {
2706 1.1 kardel debug--;
2707 1.1 kardel } else {
2708 1.1 kardel (void) fprintf(fp, "What?\n");
2709 1.1 kardel return;
2710 1.1 kardel }
2711 1.1 kardel (void) fprintf(fp, "debug level set to %d\n", debug);
2712 1.1 kardel }
2713 1.1 kardel
2714 1.1 kardel
2715 1.1 kardel /*
2716 1.1 kardel * quit - stop this nonsense
2717 1.1 kardel */
2718 1.1 kardel /*ARGSUSED*/
2719 1.1 kardel static void
2720 1.1 kardel quit(
2721 1.1 kardel struct parse *pcmd,
2722 1.1 kardel FILE *fp
2723 1.1 kardel )
2724 1.1 kardel {
2725 1.1 kardel if (havehost)
2726 1.1 kardel closesocket(sockfd); /* cleanliness next to godliness */
2727 1.1 kardel exit(0);
2728 1.1 kardel }
2729 1.1 kardel
2730 1.1 kardel
2731 1.1 kardel /*
2732 1.1 kardel * version - print the current version number
2733 1.1 kardel */
2734 1.1 kardel /*ARGSUSED*/
2735 1.1 kardel static void
2736 1.1 kardel version(
2737 1.1 kardel struct parse *pcmd,
2738 1.1 kardel FILE *fp
2739 1.1 kardel )
2740 1.1 kardel {
2741 1.1 kardel
2742 1.1 kardel (void) fprintf(fp, "%s\n", Version);
2743 1.1 kardel return;
2744 1.1 kardel }
2745 1.1 kardel
2746 1.1 kardel
2747 1.1 kardel /*
2748 1.1 kardel * raw - set raw mode output
2749 1.1 kardel */
2750 1.1 kardel /*ARGSUSED*/
2751 1.1 kardel static void
2752 1.1 kardel raw(
2753 1.1 kardel struct parse *pcmd,
2754 1.1 kardel FILE *fp
2755 1.1 kardel )
2756 1.1 kardel {
2757 1.1 kardel rawmode = 1;
2758 1.1 kardel (void) fprintf(fp, "Output set to raw\n");
2759 1.1 kardel }
2760 1.1 kardel
2761 1.1 kardel
2762 1.1 kardel /*
2763 1.1 kardel * cooked - set cooked mode output
2764 1.1 kardel */
2765 1.1 kardel /*ARGSUSED*/
2766 1.1 kardel static void
2767 1.1 kardel cooked(
2768 1.1 kardel struct parse *pcmd,
2769 1.1 kardel FILE *fp
2770 1.1 kardel )
2771 1.1 kardel {
2772 1.1 kardel rawmode = 0;
2773 1.1 kardel (void) fprintf(fp, "Output set to cooked\n");
2774 1.1 kardel return;
2775 1.1 kardel }
2776 1.1 kardel
2777 1.1 kardel
2778 1.1 kardel /*
2779 1.1 kardel * authenticate - always authenticate requests to this host
2780 1.1 kardel */
2781 1.1 kardel static void
2782 1.1 kardel authenticate(
2783 1.1 kardel struct parse *pcmd,
2784 1.1 kardel FILE *fp
2785 1.1 kardel )
2786 1.1 kardel {
2787 1.1 kardel if (pcmd->nargs == 0) {
2788 1.1 kardel if (always_auth) {
2789 1.1 kardel (void) fprintf(fp,
2790 1.1 kardel "authenticated requests being sent\n");
2791 1.1 kardel } else
2792 1.1 kardel (void) fprintf(fp,
2793 1.1 kardel "unauthenticated requests being sent\n");
2794 1.1 kardel } else {
2795 1.1 kardel if (STREQ(pcmd->argval[0].string, "yes")) {
2796 1.1 kardel always_auth = 1;
2797 1.1 kardel } else if (STREQ(pcmd->argval[0].string, "no")) {
2798 1.1 kardel always_auth = 0;
2799 1.1 kardel } else
2800 1.1 kardel (void)fprintf(stderr, "What?\n");
2801 1.1 kardel }
2802 1.1 kardel }
2803 1.1 kardel
2804 1.1 kardel
2805 1.1 kardel /*
2806 1.1 kardel * ntpversion - choose the NTP version to use
2807 1.1 kardel */
2808 1.1 kardel static void
2809 1.1 kardel ntpversion(
2810 1.1 kardel struct parse *pcmd,
2811 1.1 kardel FILE *fp
2812 1.1 kardel )
2813 1.1 kardel {
2814 1.1 kardel if (pcmd->nargs == 0) {
2815 1.1 kardel (void) fprintf(fp,
2816 1.1 kardel "NTP version being claimed is %d\n", pktversion);
2817 1.1 kardel } else {
2818 1.1 kardel if (pcmd->argval[0].uval < NTP_OLDVERSION
2819 1.1 kardel || pcmd->argval[0].uval > NTP_VERSION) {
2820 1.1 kardel (void) fprintf(stderr, "versions %d to %d, please\n",
2821 1.1 kardel NTP_OLDVERSION, NTP_VERSION);
2822 1.1 kardel } else {
2823 1.1 kardel pktversion = (u_char) pcmd->argval[0].uval;
2824 1.1 kardel }
2825 1.1 kardel }
2826 1.1 kardel }
2827 1.1 kardel
2828 1.1 kardel
2829 1.3 christos static void __attribute__((__format__(__printf__, 1, 0)))
2830 1.3 christos vwarning(const char *fmt, va_list ap)
2831 1.3 christos {
2832 1.3 christos int serrno = errno;
2833 1.3 christos (void) fprintf(stderr, "%s: ", progname);
2834 1.3 christos vfprintf(stderr, fmt, ap);
2835 1.14 christos (void) fprintf(stderr, ": %s\n", strerror(serrno));
2836 1.3 christos }
2837 1.3 christos
2838 1.1 kardel /*
2839 1.1 kardel * warning - print a warning message
2840 1.1 kardel */
2841 1.3 christos static void __attribute__((__format__(__printf__, 1, 2)))
2842 1.1 kardel warning(
2843 1.1 kardel const char *fmt,
2844 1.3 christos ...
2845 1.1 kardel )
2846 1.1 kardel {
2847 1.3 christos va_list ap;
2848 1.3 christos va_start(ap, fmt);
2849 1.3 christos vwarning(fmt, ap);
2850 1.3 christos va_end(ap);
2851 1.1 kardel }
2852 1.1 kardel
2853 1.1 kardel
2854 1.1 kardel /*
2855 1.1 kardel * error - print a message and exit
2856 1.1 kardel */
2857 1.3 christos static void __attribute__((__format__(__printf__, 1, 2)))
2858 1.1 kardel error(
2859 1.1 kardel const char *fmt,
2860 1.3 christos ...
2861 1.1 kardel )
2862 1.1 kardel {
2863 1.3 christos va_list ap;
2864 1.3 christos va_start(ap, fmt);
2865 1.3 christos vwarning(fmt, ap);
2866 1.3 christos va_end(ap);
2867 1.1 kardel exit(1);
2868 1.1 kardel }
2869 1.1 kardel /*
2870 1.1 kardel * getkeyid - prompt the user for a keyid to use
2871 1.1 kardel */
2872 1.1 kardel static u_long
2873 1.1 kardel getkeyid(
2874 1.1 kardel const char *keyprompt
2875 1.1 kardel )
2876 1.1 kardel {
2877 1.4 kardel int c;
2878 1.1 kardel FILE *fi;
2879 1.1 kardel char pbuf[20];
2880 1.4 kardel size_t i;
2881 1.4 kardel size_t ilim;
2882 1.1 kardel
2883 1.1 kardel #ifndef SYS_WINNT
2884 1.1 kardel if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
2885 1.1 kardel #else
2886 1.1 kardel if ((fi = _fdopen(open("CONIN$", _O_TEXT), "r")) == NULL)
2887 1.1 kardel #endif /* SYS_WINNT */
2888 1.1 kardel fi = stdin;
2889 1.4 kardel else
2890 1.1 kardel setbuf(fi, (char *)NULL);
2891 1.1 kardel fprintf(stderr, "%s", keyprompt); fflush(stderr);
2892 1.4 kardel for (i = 0, ilim = COUNTOF(pbuf) - 1;
2893 1.4 kardel i < ilim && (c = getc(fi)) != '\n' && c != EOF;
2894 1.4 kardel )
2895 1.4 kardel pbuf[i++] = (char)c;
2896 1.4 kardel pbuf[i] = '\0';
2897 1.1 kardel if (fi != stdin)
2898 1.4 kardel fclose(fi);
2899 1.1 kardel
2900 1.1 kardel return (u_long) atoi(pbuf);
2901 1.1 kardel }
2902 1.1 kardel
2903 1.1 kardel
2904 1.1 kardel /*
2905 1.1 kardel * atoascii - printable-ize possibly ascii data using the character
2906 1.1 kardel * transformations cat -v uses.
2907 1.1 kardel */
2908 1.1 kardel static void
2909 1.1 kardel atoascii(
2910 1.1 kardel const char *in,
2911 1.1 kardel size_t in_octets,
2912 1.1 kardel char *out,
2913 1.1 kardel size_t out_octets
2914 1.1 kardel )
2915 1.1 kardel {
2916 1.9 christos const u_char * pchIn;
2917 1.9 christos const u_char * pchInLimit;
2918 1.9 christos u_char * pchOut;
2919 1.9 christos u_char c;
2920 1.1 kardel
2921 1.1 kardel pchIn = (const u_char *)in;
2922 1.1 kardel pchInLimit = pchIn + in_octets;
2923 1.1 kardel pchOut = (u_char *)out;
2924 1.1 kardel
2925 1.1 kardel if (NULL == pchIn) {
2926 1.1 kardel if (0 < out_octets)
2927 1.1 kardel *pchOut = '\0';
2928 1.1 kardel return;
2929 1.1 kardel }
2930 1.1 kardel
2931 1.1 kardel #define ONEOUT(c) \
2932 1.1 kardel do { \
2933 1.1 kardel if (0 == --out_octets) { \
2934 1.1 kardel *pchOut = '\0'; \
2935 1.1 kardel return; \
2936 1.1 kardel } \
2937 1.1 kardel *pchOut++ = (c); \
2938 1.1 kardel } while (0)
2939 1.1 kardel
2940 1.1 kardel for ( ; pchIn < pchInLimit; pchIn++) {
2941 1.1 kardel c = *pchIn;
2942 1.1 kardel if ('\0' == c)
2943 1.1 kardel break;
2944 1.1 kardel if (c & 0x80) {
2945 1.1 kardel ONEOUT('M');
2946 1.1 kardel ONEOUT('-');
2947 1.1 kardel c &= 0x7f;
2948 1.1 kardel }
2949 1.1 kardel if (c < ' ') {
2950 1.1 kardel ONEOUT('^');
2951 1.1 kardel ONEOUT((u_char)(c + '@'));
2952 1.1 kardel } else if (0x7f == c) {
2953 1.1 kardel ONEOUT('^');
2954 1.1 kardel ONEOUT('?');
2955 1.1 kardel } else
2956 1.1 kardel ONEOUT(c);
2957 1.1 kardel }
2958 1.1 kardel ONEOUT('\0');
2959 1.1 kardel
2960 1.1 kardel #undef ONEOUT
2961 1.1 kardel }
2962 1.1 kardel
2963 1.1 kardel
2964 1.1 kardel /*
2965 1.1 kardel * makeascii - print possibly ascii data using the character
2966 1.1 kardel * transformations that cat -v uses.
2967 1.1 kardel */
2968 1.4 kardel void
2969 1.1 kardel makeascii(
2970 1.14 christos size_t length,
2971 1.4 kardel const char *data,
2972 1.1 kardel FILE *fp
2973 1.1 kardel )
2974 1.1 kardel {
2975 1.4 kardel const u_char *data_u_char;
2976 1.4 kardel const u_char *cp;
2977 1.4 kardel int c;
2978 1.4 kardel
2979 1.4 kardel data_u_char = (const u_char *)data;
2980 1.1 kardel
2981 1.4 kardel for (cp = data_u_char; cp < data_u_char + length; cp++) {
2982 1.1 kardel c = (int)*cp;
2983 1.1 kardel if (c & 0x80) {
2984 1.1 kardel putc('M', fp);
2985 1.1 kardel putc('-', fp);
2986 1.1 kardel c &= 0x7f;
2987 1.1 kardel }
2988 1.1 kardel
2989 1.1 kardel if (c < ' ') {
2990 1.1 kardel putc('^', fp);
2991 1.1 kardel putc(c + '@', fp);
2992 1.1 kardel } else if (0x7f == c) {
2993 1.1 kardel putc('^', fp);
2994 1.1 kardel putc('?', fp);
2995 1.1 kardel } else
2996 1.1 kardel putc(c, fp);
2997 1.1 kardel }
2998 1.1 kardel }
2999 1.1 kardel
3000 1.1 kardel
3001 1.1 kardel /*
3002 1.1 kardel * asciize - same thing as makeascii except add a newline
3003 1.1 kardel */
3004 1.1 kardel void
3005 1.1 kardel asciize(
3006 1.1 kardel int length,
3007 1.1 kardel char *data,
3008 1.1 kardel FILE *fp
3009 1.1 kardel )
3010 1.1 kardel {
3011 1.1 kardel makeascii(length, data, fp);
3012 1.1 kardel putc('\n', fp);
3013 1.1 kardel }
3014 1.1 kardel
3015 1.1 kardel
3016 1.1 kardel /*
3017 1.4 kardel * truncate string to fit clipping excess at end.
3018 1.4 kardel * "too long" -> "too l"
3019 1.4 kardel * Used for hostnames.
3020 1.4 kardel */
3021 1.4 kardel const char *
3022 1.4 kardel trunc_right(
3023 1.4 kardel const char * src,
3024 1.4 kardel size_t width
3025 1.4 kardel )
3026 1.4 kardel {
3027 1.4 kardel size_t sl;
3028 1.4 kardel char * out;
3029 1.4 kardel
3030 1.9 christos
3031 1.4 kardel sl = strlen(src);
3032 1.4 kardel if (sl > width && LIB_BUFLENGTH - 1 > width && width > 0) {
3033 1.4 kardel LIB_GETBUF(out);
3034 1.4 kardel memcpy(out, src, width);
3035 1.4 kardel out[width] = '\0';
3036 1.4 kardel
3037 1.4 kardel return out;
3038 1.4 kardel }
3039 1.4 kardel
3040 1.4 kardel return src;
3041 1.4 kardel }
3042 1.4 kardel
3043 1.4 kardel
3044 1.4 kardel /*
3045 1.4 kardel * truncate string to fit by preserving right side and using '_' to hint
3046 1.4 kardel * "too long" -> "_long"
3047 1.4 kardel * Used for local IPv6 addresses, where low bits differentiate.
3048 1.4 kardel */
3049 1.4 kardel const char *
3050 1.4 kardel trunc_left(
3051 1.4 kardel const char * src,
3052 1.4 kardel size_t width
3053 1.4 kardel )
3054 1.4 kardel {
3055 1.4 kardel size_t sl;
3056 1.4 kardel char * out;
3057 1.4 kardel
3058 1.4 kardel
3059 1.4 kardel sl = strlen(src);
3060 1.4 kardel if (sl > width && LIB_BUFLENGTH - 1 > width && width > 1) {
3061 1.4 kardel LIB_GETBUF(out);
3062 1.4 kardel out[0] = '_';
3063 1.4 kardel memcpy(&out[1], &src[sl + 1 - width], width);
3064 1.4 kardel
3065 1.4 kardel return out;
3066 1.4 kardel }
3067 1.4 kardel
3068 1.4 kardel return src;
3069 1.4 kardel }
3070 1.4 kardel
3071 1.4 kardel
3072 1.4 kardel /*
3073 1.1 kardel * Some circular buffer space
3074 1.1 kardel */
3075 1.1 kardel #define CBLEN 80
3076 1.1 kardel #define NUMCB 6
3077 1.1 kardel
3078 1.1 kardel char circ_buf[NUMCB][CBLEN];
3079 1.1 kardel int nextcb = 0;
3080 1.1 kardel
3081 1.20 christos /* --------------------------------------------------------------------
3082 1.20 christos * Parsing a response value list
3083 1.20 christos *
3084 1.20 christos * This sounds simple (and it actually is not really hard) but it has
3085 1.20 christos * some pitfalls.
3086 1.20 christos *
3087 1.20 christos * Rule1: CR/LF is never embedded in an item
3088 1.20 christos * Rule2: An item is a name, optionally followed by a value
3089 1.20 christos * Rule3: The value is separated from the name by a '='
3090 1.20 christos * Rule4: Items are separated by a ','
3091 1.20 christos * Rule5: values can be quoted by '"', in which case they can contain
3092 1.20 christos * arbitrary characters but *not* '"', CR and LF
3093 1.20 christos *
3094 1.20 christos * There are a few implementations out there that require a somewhat
3095 1.20 christos * relaxed attitude when parsing a value list, especially since we want
3096 1.20 christos * to copy names and values into local buffers. If these would overflow,
3097 1.20 christos * the item should be skipped without terminating the parsing sequence.
3098 1.20 christos *
3099 1.20 christos * Also, for empty values, there might be a '=' after the name or not;
3100 1.20 christos * we treat that equivalent.
3101 1.20 christos *
3102 1.20 christos * Parsing an item definitely breaks on a CR/LF. If an item is not
3103 1.20 christos * followed by a comma (','), parsing stops. In the middle of a quoted
3104 1.20 christos * character sequence CR/LF terminates the parsing finally without
3105 1.20 christos * returning a value.
3106 1.20 christos *
3107 1.20 christos * White space and other noise is ignored when parsing the data buffer;
3108 1.20 christos * only CR, LF, ',', '=' and '"' are characters with a special meaning.
3109 1.20 christos * White space is stripped from the names and values *after* working
3110 1.20 christos * through the buffer, before making the local copies. If whitespace
3111 1.20 christos * stripping results in an empty name, parsing resumes.
3112 1.20 christos */
3113 1.20 christos
3114 1.20 christos /*
3115 1.20 christos * nextvar parsing helpers
3116 1.20 christos */
3117 1.20 christos
3118 1.20 christos /* predicate: allowed chars inside a quoted string */
3119 1.20 christos static int/*BOOL*/ cp_qschar(int ch)
3120 1.20 christos {
3121 1.20 christos return ch && (ch != '"' && ch != '\r' && ch != '\n');
3122 1.20 christos }
3123 1.20 christos
3124 1.20 christos /* predicate: allowed chars inside an unquoted string */
3125 1.20 christos static int/*BOOL*/ cp_uqchar(int ch)
3126 1.20 christos {
3127 1.20 christos return ch && (ch != ',' && ch != '"' && ch != '\r' && ch != '\n');
3128 1.20 christos }
3129 1.20 christos
3130 1.20 christos /* predicate: allowed chars inside a value name */
3131 1.20 christos static int/*BOOL*/ cp_namechar(int ch)
3132 1.20 christos {
3133 1.20 christos return ch && (ch != ',' && ch != '=' && ch != '\r' && ch != '\n');
3134 1.20 christos }
3135 1.20 christos
3136 1.20 christos /* predicate: characters *between* list items. We're relaxed here. */
3137 1.20 christos static int/*BOOL*/ cp_ivspace(int ch)
3138 1.20 christos {
3139 1.20 christos return (ch == ',' || (ch > 0 && ch <= ' '));
3140 1.20 christos }
3141 1.20 christos
3142 1.20 christos /* get current character (or NUL when on end) */
3143 1.20 christos static inline int
3144 1.20 christos pf_getch(
3145 1.20 christos const char ** datap,
3146 1.20 christos const char * endp
3147 1.20 christos )
3148 1.20 christos {
3149 1.20 christos return (*datap != endp)
3150 1.20 christos ? *(const unsigned char*)*datap
3151 1.20 christos : '\0';
3152 1.20 christos }
3153 1.20 christos
3154 1.20 christos /* get next character (or NUL when on end) */
3155 1.20 christos static inline int
3156 1.20 christos pf_nextch(
3157 1.20 christos const char ** datap,
3158 1.20 christos const char * endp
3159 1.20 christos )
3160 1.20 christos {
3161 1.20 christos return (*datap != endp && ++(*datap) != endp)
3162 1.20 christos ? *(const unsigned char*)*datap
3163 1.20 christos : '\0';
3164 1.20 christos }
3165 1.20 christos
3166 1.20 christos static size_t
3167 1.20 christos str_strip(
3168 1.20 christos const char ** datap,
3169 1.20 christos size_t len
3170 1.20 christos )
3171 1.20 christos {
3172 1.20 christos static const char empty[] = "";
3173 1.20 christos
3174 1.20 christos if (*datap && len) {
3175 1.20 christos const char * cpl = *datap;
3176 1.20 christos const char * cpr = cpl + len;
3177 1.20 christos
3178 1.20 christos while (cpl != cpr && *(const unsigned char*)cpl <= ' ')
3179 1.20 christos ++cpl;
3180 1.20 christos while (cpl != cpr && *(const unsigned char*)(cpr - 1) <= ' ')
3181 1.20 christos --cpr;
3182 1.20 christos *datap = cpl;
3183 1.20 christos len = (size_t)(cpr - cpl);
3184 1.20 christos } else {
3185 1.20 christos *datap = empty;
3186 1.20 christos len = 0;
3187 1.20 christos }
3188 1.20 christos return len;
3189 1.20 christos }
3190 1.20 christos
3191 1.20 christos static void
3192 1.20 christos pf_error(
3193 1.20 christos const char * what,
3194 1.20 christos const char * where,
3195 1.20 christos const char * whend
3196 1.20 christos )
3197 1.20 christos {
3198 1.20 christos # ifndef BUILD_AS_LIB
3199 1.20 christos
3200 1.20 christos FILE * ofp = (debug > 0) ? stdout : stderr;
3201 1.20 christos size_t len = (size_t)(whend - where);
3202 1.20 christos
3203 1.20 christos if (len > 50) /* *must* fit into an 'int'! */
3204 1.20 christos len = 50;
3205 1.20 christos fprintf(ofp, "nextvar: %s: '%.*s'\n",
3206 1.20 christos what, (int)len, where);
3207 1.20 christos
3208 1.20 christos # else /*defined(BUILD_AS_LIB)*/
3209 1.20 christos
3210 1.20 christos UNUSED_ARG(what);
3211 1.20 christos UNUSED_ARG(where);
3212 1.20 christos UNUSED_ARG(whend);
3213 1.20 christos
3214 1.20 christos # endif /*defined(BUILD_AS_LIB)*/
3215 1.20 christos }
3216 1.20 christos
3217 1.1 kardel /*
3218 1.1 kardel * nextvar - find the next variable in the buffer
3219 1.1 kardel */
3220 1.20 christos int/*BOOL*/
3221 1.1 kardel nextvar(
3222 1.14 christos size_t *datalen,
3223 1.4 kardel const char **datap,
3224 1.1 kardel char **vname,
3225 1.1 kardel char **vvalue
3226 1.1 kardel )
3227 1.1 kardel {
3228 1.20 christos enum PState { sDone, sInit, sName, sValU, sValQ };
3229 1.20 christos
3230 1.20 christos static char name[MAXVARLEN], value[MAXVALLEN];
3231 1.1 kardel
3232 1.20 christos const char *cp, *cpend;
3233 1.20 christos const char *np, *vp;
3234 1.20 christos size_t nlen, vlen;
3235 1.20 christos int ch;
3236 1.20 christos enum PState st;
3237 1.20 christos
3238 1.20 christos cpend = *datap + *datalen;
3239 1.1 kardel
3240 1.20 christos again:
3241 1.20 christos np = vp = NULL;
3242 1.20 christos nlen = vlen = 0;
3243 1.20 christos
3244 1.20 christos st = sInit;
3245 1.20 christos ch = pf_getch(datap, cpend);
3246 1.9 christos
3247 1.20 christos while (st != sDone) {
3248 1.20 christos switch (st)
3249 1.20 christos {
3250 1.20 christos case sInit: /* handle inter-item chars */
3251 1.20 christos while (cp_ivspace(ch))
3252 1.20 christos ch = pf_nextch(datap, cpend);
3253 1.20 christos if (cp_namechar(ch)) {
3254 1.20 christos np = *datap;
3255 1.20 christos cp = np;
3256 1.20 christos st = sName;
3257 1.20 christos ch = pf_nextch(datap, cpend);
3258 1.20 christos } else {
3259 1.20 christos goto final_done;
3260 1.20 christos }
3261 1.20 christos break;
3262 1.20 christos
3263 1.20 christos case sName: /* collect name */
3264 1.20 christos while (cp_namechar(ch))
3265 1.20 christos ch = pf_nextch(datap, cpend);
3266 1.20 christos nlen = (size_t)(*datap - np);
3267 1.20 christos if (ch == '=') {
3268 1.20 christos ch = pf_nextch(datap, cpend);
3269 1.20 christos vp = *datap;
3270 1.20 christos st = sValU;
3271 1.20 christos } else {
3272 1.20 christos if (ch != ',')
3273 1.20 christos *datap = cpend;
3274 1.20 christos st = sDone;
3275 1.20 christos }
3276 1.20 christos break;
3277 1.20 christos
3278 1.20 christos case sValU: /* collect unquoted part(s) of value */
3279 1.20 christos while (cp_uqchar(ch))
3280 1.20 christos ch = pf_nextch(datap, cpend);
3281 1.20 christos if (ch == '"') {
3282 1.20 christos ch = pf_nextch(datap, cpend);
3283 1.20 christos st = sValQ;
3284 1.20 christos } else {
3285 1.20 christos vlen = (size_t)(*datap - vp);
3286 1.20 christos if (ch != ',')
3287 1.20 christos *datap = cpend;
3288 1.20 christos st = sDone;
3289 1.20 christos }
3290 1.20 christos break;
3291 1.20 christos
3292 1.20 christos case sValQ: /* collect quoted part(s) of value */
3293 1.20 christos while (cp_qschar(ch))
3294 1.20 christos ch = pf_nextch(datap, cpend);
3295 1.20 christos if (ch == '"') {
3296 1.20 christos ch = pf_nextch(datap, cpend);
3297 1.20 christos st = sValU;
3298 1.20 christos } else {
3299 1.20 christos pf_error("no closing quote, stop", cp, cpend);
3300 1.20 christos goto final_done;
3301 1.20 christos }
3302 1.20 christos break;
3303 1.20 christos
3304 1.20 christos default:
3305 1.20 christos pf_error("state machine error, stop", *datap, cpend);
3306 1.20 christos goto final_done;
3307 1.20 christos }
3308 1.20 christos }
3309 1.1 kardel
3310 1.20 christos /* If name or value do not fit their buffer, croak and start
3311 1.20 christos * over. If there's no name at all after whitespace stripping,
3312 1.20 christos * redo silently.
3313 1.1 kardel */
3314 1.20 christos nlen = str_strip(&np, nlen);
3315 1.20 christos vlen = str_strip(&vp, vlen);
3316 1.20 christos
3317 1.20 christos if (nlen == 0) {
3318 1.20 christos goto again;
3319 1.20 christos }
3320 1.20 christos if (nlen >= sizeof(name)) {
3321 1.20 christos pf_error("runaway name", np, cpend);
3322 1.20 christos goto again;
3323 1.1 kardel }
3324 1.20 christos if (vlen >= sizeof(value)) {
3325 1.20 christos pf_error("runaway value", vp, cpend);
3326 1.20 christos goto again;
3327 1.1 kardel }
3328 1.1 kardel
3329 1.20 christos /* copy name and value into NUL-terminated buffers */
3330 1.20 christos memcpy(name, np, nlen);
3331 1.20 christos name[nlen] = '\0';
3332 1.20 christos *vname = name;
3333 1.20 christos
3334 1.20 christos memcpy(value, vp, vlen);
3335 1.20 christos value[vlen] = '\0';
3336 1.1 kardel *vvalue = value;
3337 1.20 christos
3338 1.20 christos /* check if there's more to do or if we are finshed */
3339 1.20 christos *datalen = (size_t)(cpend - *datap);
3340 1.20 christos return TRUE;
3341 1.20 christos
3342 1.20 christos final_done:
3343 1.20 christos *datap = cpend;
3344 1.20 christos *datalen = 0;
3345 1.20 christos return FALSE;
3346 1.1 kardel }
3347 1.1 kardel
3348 1.1 kardel
3349 1.9 christos u_short
3350 1.9 christos varfmt(const char * varname)
3351 1.9 christos {
3352 1.9 christos u_int n;
3353 1.9 christos
3354 1.9 christos for (n = 0; n < COUNTOF(cookedvars); n++)
3355 1.9 christos if (!strcmp(varname, cookedvars[n].varname))
3356 1.9 christos return cookedvars[n].fmt;
3357 1.9 christos
3358 1.9 christos return PADDING;
3359 1.1 kardel }
3360 1.1 kardel
3361 1.1 kardel
3362 1.1 kardel /*
3363 1.1 kardel * printvars - print variables returned in response packet
3364 1.1 kardel */
3365 1.1 kardel void
3366 1.1 kardel printvars(
3367 1.14 christos size_t length,
3368 1.4 kardel const char *data,
3369 1.1 kardel int status,
3370 1.1 kardel int sttype,
3371 1.1 kardel int quiet,
3372 1.1 kardel FILE *fp
3373 1.1 kardel )
3374 1.1 kardel {
3375 1.1 kardel if (rawmode)
3376 1.1 kardel rawprint(sttype, length, data, status, quiet, fp);
3377 1.1 kardel else
3378 1.1 kardel cookedprint(sttype, length, data, status, quiet, fp);
3379 1.1 kardel }
3380 1.1 kardel
3381 1.1 kardel
3382 1.1 kardel /*
3383 1.1 kardel * rawprint - do a printout of the data in raw mode
3384 1.1 kardel */
3385 1.1 kardel static void
3386 1.1 kardel rawprint(
3387 1.1 kardel int datatype,
3388 1.14 christos size_t length,
3389 1.4 kardel const char *data,
3390 1.1 kardel int status,
3391 1.1 kardel int quiet,
3392 1.1 kardel FILE *fp
3393 1.1 kardel )
3394 1.1 kardel {
3395 1.4 kardel const char *cp;
3396 1.4 kardel const char *cpend;
3397 1.1 kardel
3398 1.1 kardel /*
3399 1.1 kardel * Essentially print the data as is. We reformat unprintables, though.
3400 1.1 kardel */
3401 1.1 kardel cp = data;
3402 1.1 kardel cpend = data + length;
3403 1.1 kardel
3404 1.1 kardel if (!quiet)
3405 1.1 kardel (void) fprintf(fp, "status=0x%04x,\n", status);
3406 1.1 kardel
3407 1.1 kardel while (cp < cpend) {
3408 1.1 kardel if (*cp == '\r') {
3409 1.1 kardel /*
3410 1.1 kardel * If this is a \r and the next character is a
3411 1.1 kardel * \n, supress this, else pretty print it. Otherwise
3412 1.1 kardel * just output the character.
3413 1.1 kardel */
3414 1.1 kardel if (cp == (cpend - 1) || *(cp + 1) != '\n')
3415 1.1 kardel makeascii(1, cp, fp);
3416 1.19 christos } else if (isspace(pgetc(cp)) || isprint(pgetc(cp)))
3417 1.1 kardel putc(*cp, fp);
3418 1.1 kardel else
3419 1.1 kardel makeascii(1, cp, fp);
3420 1.1 kardel cp++;
3421 1.1 kardel }
3422 1.1 kardel }
3423 1.1 kardel
3424 1.1 kardel
3425 1.1 kardel /*
3426 1.1 kardel * Global data used by the cooked output routines
3427 1.1 kardel */
3428 1.1 kardel int out_chars; /* number of characters output */
3429 1.1 kardel int out_linecount; /* number of characters output on this line */
3430 1.1 kardel
3431 1.1 kardel
3432 1.1 kardel /*
3433 1.1 kardel * startoutput - get ready to do cooked output
3434 1.1 kardel */
3435 1.1 kardel static void
3436 1.1 kardel startoutput(void)
3437 1.1 kardel {
3438 1.1 kardel out_chars = 0;
3439 1.1 kardel out_linecount = 0;
3440 1.1 kardel }
3441 1.1 kardel
3442 1.1 kardel
3443 1.1 kardel /*
3444 1.1 kardel * output - output a variable=value combination
3445 1.1 kardel */
3446 1.1 kardel static void
3447 1.1 kardel output(
3448 1.1 kardel FILE *fp,
3449 1.9 christos const char *name,
3450 1.4 kardel const char *value
3451 1.1 kardel )
3452 1.1 kardel {
3453 1.14 christos int len;
3454 1.1 kardel
3455 1.1 kardel /* strlen of "name=value" */
3456 1.14 christos len = size2int_sat(strlen(name) + 1 + strlen(value));
3457 1.1 kardel
3458 1.1 kardel if (out_chars != 0) {
3459 1.1 kardel out_chars += 2;
3460 1.1 kardel if ((out_linecount + len + 2) > MAXOUTLINE) {
3461 1.1 kardel fputs(",\n", fp);
3462 1.1 kardel out_linecount = 0;
3463 1.1 kardel } else {
3464 1.1 kardel fputs(", ", fp);
3465 1.1 kardel out_linecount += 2;
3466 1.1 kardel }
3467 1.1 kardel }
3468 1.1 kardel
3469 1.1 kardel fputs(name, fp);
3470 1.1 kardel putc('=', fp);
3471 1.1 kardel fputs(value, fp);
3472 1.1 kardel out_chars += len;
3473 1.1 kardel out_linecount += len;
3474 1.1 kardel }
3475 1.1 kardel
3476 1.1 kardel
3477 1.1 kardel /*
3478 1.1 kardel * endoutput - terminate a block of cooked output
3479 1.1 kardel */
3480 1.1 kardel static void
3481 1.1 kardel endoutput(
3482 1.1 kardel FILE *fp
3483 1.1 kardel )
3484 1.1 kardel {
3485 1.1 kardel if (out_chars != 0)
3486 1.1 kardel putc('\n', fp);
3487 1.1 kardel }
3488 1.1 kardel
3489 1.1 kardel
3490 1.1 kardel /*
3491 1.1 kardel * outputarr - output an array of values
3492 1.1 kardel */
3493 1.1 kardel static void
3494 1.1 kardel outputarr(
3495 1.1 kardel FILE *fp,
3496 1.1 kardel char *name,
3497 1.1 kardel int narr,
3498 1.1 kardel l_fp *lfp
3499 1.1 kardel )
3500 1.1 kardel {
3501 1.14 christos char *bp;
3502 1.14 christos char *cp;
3503 1.14 christos size_t i;
3504 1.14 christos size_t len;
3505 1.1 kardel char buf[256];
3506 1.1 kardel
3507 1.1 kardel bp = buf;
3508 1.1 kardel /*
3509 1.1 kardel * Hack to align delay and offset values
3510 1.1 kardel */
3511 1.1 kardel for (i = (int)strlen(name); i < 11; i++)
3512 1.20 christos *bp++ = ' ';
3513 1.9 christos
3514 1.1 kardel for (i = narr; i > 0; i--) {
3515 1.14 christos if (i != (size_t)narr)
3516 1.20 christos *bp++ = ' ';
3517 1.1 kardel cp = lfptoms(lfp, 2);
3518 1.1 kardel len = strlen(cp);
3519 1.1 kardel if (len > 7) {
3520 1.1 kardel cp[7] = '\0';
3521 1.1 kardel len = 7;
3522 1.1 kardel }
3523 1.1 kardel while (len < 7) {
3524 1.1 kardel *bp++ = ' ';
3525 1.1 kardel len++;
3526 1.1 kardel }
3527 1.1 kardel while (*cp != '\0')
3528 1.1 kardel *bp++ = *cp++;
3529 1.1 kardel lfp++;
3530 1.1 kardel }
3531 1.1 kardel *bp = '\0';
3532 1.1 kardel output(fp, name, buf);
3533 1.1 kardel }
3534 1.1 kardel
3535 1.1 kardel static char *
3536 1.1 kardel tstflags(
3537 1.1 kardel u_long val
3538 1.1 kardel )
3539 1.1 kardel {
3540 1.20 christos # if CBLEN < 10
3541 1.20 christos # error BLEN is too small -- increase!
3542 1.20 christos # endif
3543 1.20 christos
3544 1.20 christos char *cp, *s;
3545 1.20 christos size_t cb, i;
3546 1.20 christos int l;
3547 1.1 kardel
3548 1.4 kardel s = cp = circ_buf[nextcb];
3549 1.1 kardel if (++nextcb >= NUMCB)
3550 1.4 kardel nextcb = 0;
3551 1.4 kardel cb = sizeof(circ_buf[0]);
3552 1.1 kardel
3553 1.20 christos l = snprintf(cp, cb, "%02lx", val);
3554 1.20 christos if (l < 0 || (size_t)l >= cb)
3555 1.20 christos goto fail;
3556 1.20 christos cp += l;
3557 1.20 christos cb -= l;
3558 1.1 kardel if (!val) {
3559 1.20 christos l = strlcat(cp, " ok", cb);
3560 1.20 christos if ((size_t)l >= cb)
3561 1.20 christos goto fail;
3562 1.20 christos cp += l;
3563 1.20 christos cb -= l;
3564 1.1 kardel } else {
3565 1.20 christos const char *sep;
3566 1.20 christos
3567 1.20 christos sep = " ";
3568 1.20 christos for (i = 0; i < COUNTOF(tstflagnames); i++) {
3569 1.1 kardel if (val & 0x1) {
3570 1.20 christos l = snprintf(cp, cb, "%s%s", sep,
3571 1.20 christos tstflagnames[i]);
3572 1.20 christos if (l < 0)
3573 1.20 christos goto fail;
3574 1.20 christos if ((size_t)l >= cb) {
3575 1.20 christos cp += cb - 4;
3576 1.20 christos cb = 4;
3577 1.20 christos l = strlcpy (cp, "...", cb);
3578 1.20 christos cp += l;
3579 1.20 christos cb -= l;
3580 1.20 christos break;
3581 1.20 christos }
3582 1.1 kardel sep = ", ";
3583 1.20 christos cp += l;
3584 1.20 christos cb -= l;
3585 1.1 kardel }
3586 1.1 kardel val >>= 1;
3587 1.1 kardel }
3588 1.1 kardel }
3589 1.4 kardel
3590 1.1 kardel return s;
3591 1.20 christos
3592 1.20 christos fail:
3593 1.20 christos *cp = '\0';
3594 1.20 christos return s;
3595 1.1 kardel }
3596 1.1 kardel
3597 1.1 kardel /*
3598 1.1 kardel * cookedprint - output variables in cooked mode
3599 1.1 kardel */
3600 1.1 kardel static void
3601 1.1 kardel cookedprint(
3602 1.1 kardel int datatype,
3603 1.14 christos size_t length,
3604 1.4 kardel const char *data,
3605 1.1 kardel int status,
3606 1.1 kardel int quiet,
3607 1.1 kardel FILE *fp
3608 1.1 kardel )
3609 1.1 kardel {
3610 1.1 kardel char *name;
3611 1.1 kardel char *value;
3612 1.1 kardel char output_raw;
3613 1.1 kardel int fmt;
3614 1.1 kardel l_fp lfp;
3615 1.1 kardel sockaddr_u hval;
3616 1.1 kardel u_long uval;
3617 1.9 christos int narr;
3618 1.9 christos size_t len;
3619 1.1 kardel l_fp lfparr[8];
3620 1.9 christos char b[12];
3621 1.9 christos char bn[2 * MAXVARLEN];
3622 1.9 christos char bv[2 * MAXVALLEN];
3623 1.1 kardel
3624 1.9 christos UNUSED_ARG(datatype);
3625 1.1 kardel
3626 1.1 kardel if (!quiet)
3627 1.1 kardel fprintf(fp, "status=%04x %s,\n", status,
3628 1.1 kardel statustoa(datatype, status));
3629 1.1 kardel
3630 1.1 kardel startoutput();
3631 1.1 kardel while (nextvar(&length, &data, &name, &value)) {
3632 1.9 christos fmt = varfmt(name);
3633 1.9 christos output_raw = 0;
3634 1.9 christos switch (fmt) {
3635 1.9 christos
3636 1.9 christos case PADDING:
3637 1.1 kardel output_raw = '*';
3638 1.9 christos break;
3639 1.9 christos
3640 1.9 christos case TS:
3641 1.19 christos if (!value || !decodets(value, &lfp))
3642 1.9 christos output_raw = '?';
3643 1.9 christos else
3644 1.9 christos output(fp, name, prettydate(&lfp));
3645 1.9 christos break;
3646 1.9 christos
3647 1.9 christos case HA: /* fallthru */
3648 1.9 christos case NA:
3649 1.19 christos if (!value || !decodenetnum(value, &hval)) {
3650 1.9 christos output_raw = '?';
3651 1.9 christos } else if (fmt == HA){
3652 1.9 christos output(fp, name, nntohost(&hval));
3653 1.9 christos } else {
3654 1.9 christos output(fp, name, stoa(&hval));
3655 1.9 christos }
3656 1.9 christos break;
3657 1.1 kardel
3658 1.9 christos case RF:
3659 1.19 christos if (!value) {
3660 1.19 christos output_raw = '?';
3661 1.19 christos } else if (decodenetnum(value, &hval)) {
3662 1.9 christos if (ISREFCLOCKADR(&hval))
3663 1.9 christos output(fp, name,
3664 1.9 christos refnumtoa(&hval));
3665 1.1 kardel else
3666 1.9 christos output(fp, name, stoa(&hval));
3667 1.9 christos } else if (strlen(value) <= 4) {
3668 1.9 christos output(fp, name, value);
3669 1.9 christos } else {
3670 1.9 christos output_raw = '?';
3671 1.9 christos }
3672 1.9 christos break;
3673 1.1 kardel
3674 1.9 christos case LP:
3675 1.19 christos if (!value || !decodeuint(value, &uval) || uval > 3) {
3676 1.9 christos output_raw = '?';
3677 1.9 christos } else {
3678 1.9 christos b[0] = (0x2 & uval)
3679 1.9 christos ? '1'
3680 1.9 christos : '0';
3681 1.9 christos b[1] = (0x1 & uval)
3682 1.9 christos ? '1'
3683 1.9 christos : '0';
3684 1.9 christos b[2] = '\0';
3685 1.9 christos output(fp, name, b);
3686 1.9 christos }
3687 1.9 christos break;
3688 1.1 kardel
3689 1.9 christos case OC:
3690 1.19 christos if (!value || !decodeuint(value, &uval)) {
3691 1.9 christos output_raw = '?';
3692 1.9 christos } else {
3693 1.9 christos snprintf(b, sizeof(b), "%03lo", uval);
3694 1.9 christos output(fp, name, b);
3695 1.9 christos }
3696 1.9 christos break;
3697 1.1 kardel
3698 1.9 christos case AR:
3699 1.19 christos if (!value || !decodearr(value, &narr, lfparr, 8))
3700 1.9 christos output_raw = '?';
3701 1.9 christos else
3702 1.9 christos outputarr(fp, name, narr, lfparr);
3703 1.9 christos break;
3704 1.1 kardel
3705 1.9 christos case FX:
3706 1.19 christos if (!value || !decodeuint(value, &uval))
3707 1.9 christos output_raw = '?';
3708 1.9 christos else
3709 1.9 christos output(fp, name, tstflags(uval));
3710 1.9 christos break;
3711 1.1 kardel
3712 1.9 christos default:
3713 1.9 christos fprintf(stderr, "Internal error in cookedprint, %s=%s, fmt %d\n",
3714 1.9 christos name, value, fmt);
3715 1.9 christos output_raw = '?';
3716 1.9 christos break;
3717 1.1 kardel }
3718 1.9 christos
3719 1.1 kardel if (output_raw != 0) {
3720 1.13 christos /* TALOS-CAN-0063: avoid buffer overrun */
3721 1.1 kardel atoascii(name, MAXVARLEN, bn, sizeof(bn));
3722 1.1 kardel if (output_raw != '*') {
3723 1.13 christos atoascii(value, MAXVALLEN,
3724 1.13 christos bv, sizeof(bv) - 1);
3725 1.1 kardel len = strlen(bv);
3726 1.1 kardel bv[len] = output_raw;
3727 1.1 kardel bv[len+1] = '\0';
3728 1.13 christos } else {
3729 1.13 christos atoascii(value, MAXVALLEN,
3730 1.13 christos bv, sizeof(bv));
3731 1.1 kardel }
3732 1.1 kardel output(fp, bn, bv);
3733 1.1 kardel }
3734 1.1 kardel }
3735 1.1 kardel endoutput(fp);
3736 1.1 kardel }
3737 1.1 kardel
3738 1.1 kardel
3739 1.1 kardel /*
3740 1.1 kardel * sortassoc - sort associations in the cache into ascending order
3741 1.1 kardel */
3742 1.1 kardel void
3743 1.1 kardel sortassoc(void)
3744 1.1 kardel {
3745 1.1 kardel if (numassoc > 1)
3746 1.9 christos qsort(assoc_cache, (size_t)numassoc,
3747 1.9 christos sizeof(assoc_cache[0]), &assoccmp);
3748 1.1 kardel }
3749 1.1 kardel
3750 1.1 kardel
3751 1.1 kardel /*
3752 1.1 kardel * assoccmp - compare two associations
3753 1.1 kardel */
3754 1.1 kardel static int
3755 1.1 kardel assoccmp(
3756 1.1 kardel const void *t1,
3757 1.1 kardel const void *t2
3758 1.1 kardel )
3759 1.1 kardel {
3760 1.4 kardel const struct association *ass1 = t1;
3761 1.4 kardel const struct association *ass2 = t2;
3762 1.1 kardel
3763 1.1 kardel if (ass1->assid < ass2->assid)
3764 1.1 kardel return -1;
3765 1.1 kardel if (ass1->assid > ass2->assid)
3766 1.1 kardel return 1;
3767 1.1 kardel return 0;
3768 1.1 kardel }
3769 1.4 kardel
3770 1.1 kardel
3771 1.1 kardel /*
3772 1.9 christos * grow_assoc_cache() - enlarge dynamic assoc_cache array
3773 1.9 christos *
3774 1.9 christos * The strategy is to add an assumed 4k page size at a time, leaving
3775 1.9 christos * room for malloc() bookkeeping overhead equivalent to 4 pointers.
3776 1.9 christos */
3777 1.9 christos void
3778 1.9 christos grow_assoc_cache(void)
3779 1.9 christos {
3780 1.9 christos static size_t prior_sz;
3781 1.9 christos size_t new_sz;
3782 1.9 christos
3783 1.9 christos new_sz = prior_sz + 4 * 1024;
3784 1.9 christos if (0 == prior_sz) {
3785 1.9 christos new_sz -= 4 * sizeof(void *);
3786 1.9 christos }
3787 1.9 christos assoc_cache = erealloc_zero(assoc_cache, new_sz, prior_sz);
3788 1.9 christos prior_sz = new_sz;
3789 1.14 christos assoc_cache_slots = (u_int)(new_sz / sizeof(assoc_cache[0]));
3790 1.9 christos }
3791 1.9 christos
3792 1.9 christos
3793 1.9 christos /*
3794 1.1 kardel * ntpq_custom_opt_handler - autoopts handler for -c and -p
3795 1.1 kardel *
3796 1.1 kardel * By default, autoopts loses the relative order of -c and -p options
3797 1.1 kardel * on the command line. This routine replaces the default handler for
3798 1.1 kardel * those routines and builds a list of commands to execute preserving
3799 1.1 kardel * the order.
3800 1.1 kardel */
3801 1.1 kardel void
3802 1.1 kardel ntpq_custom_opt_handler(
3803 1.1 kardel tOptions *pOptions,
3804 1.1 kardel tOptDesc *pOptDesc
3805 1.1 kardel )
3806 1.1 kardel {
3807 1.1 kardel switch (pOptDesc->optValue) {
3808 1.9 christos
3809 1.1 kardel default:
3810 1.9 christos fprintf(stderr,
3811 1.1 kardel "ntpq_custom_opt_handler unexpected option '%c' (%d)\n",
3812 1.1 kardel pOptDesc->optValue, pOptDesc->optValue);
3813 1.9 christos exit(1);
3814 1.1 kardel
3815 1.1 kardel case 'c':
3816 1.1 kardel ADDCMD(pOptDesc->pzLastArg);
3817 1.1 kardel break;
3818 1.1 kardel
3819 1.1 kardel case 'p':
3820 1.1 kardel ADDCMD("peers");
3821 1.1 kardel break;
3822 1.1 kardel }
3823 1.1 kardel }
3824 1.12 christos /*
3825 1.12 christos * Obtain list of digest names
3826 1.12 christos */
3827 1.12 christos
3828 1.19 christos #if defined(OPENSSL) && !defined(HAVE_EVP_MD_DO_ALL_SORTED)
3829 1.19 christos # if defined(_MSC_VER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
3830 1.19 christos # define HAVE_EVP_MD_DO_ALL_SORTED
3831 1.19 christos # endif
3832 1.19 christos #endif
3833 1.19 christos
3834 1.12 christos #ifdef OPENSSL
3835 1.12 christos # ifdef HAVE_EVP_MD_DO_ALL_SORTED
3836 1.19 christos # define K_PER_LINE 8
3837 1.19 christos # define K_NL_PFX_STR "\n "
3838 1.19 christos # define K_DELIM_STR ", "
3839 1.19 christos
3840 1.12 christos struct hstate {
3841 1.20 christos char *list;
3842 1.20 christos const char **seen;
3843 1.20 christos int idx;
3844 1.12 christos };
3845 1.19 christos
3846 1.19 christos
3847 1.20 christos # ifndef BUILD_AS_LIB
3848 1.19 christos static void
3849 1.19 christos list_md_fn(const EVP_MD *m, const char *from, const char *to, void *arg)
3850 1.12 christos {
3851 1.20 christos size_t len, n;
3852 1.20 christos const char *name, **seen;
3853 1.20 christos struct hstate *hstate = arg;
3854 1.20 christos const char *cp;
3855 1.20 christos
3856 1.20 christos /* m is MD obj, from is name or alias, to is base name for alias */
3857 1.20 christos if (!m || !from || to)
3858 1.20 christos return; /* Ignore aliases */
3859 1.20 christos
3860 1.20 christos /* Discard MACs that NTP won't accept. */
3861 1.20 christos /* Keep this consistent with keytype_from_text() in ssl_init.c. */
3862 1.20 christos if (EVP_MD_size(m) > (int)(MAX_MAC_LEN - sizeof(keyid_t)))
3863 1.20 christos return;
3864 1.20 christos
3865 1.20 christos name = EVP_MD_name(m);
3866 1.20 christos
3867 1.20 christos /* Lowercase names aren't accepted by keytype_from_text in ssl_init.c */
3868 1.20 christos
3869 1.20 christos for (cp = name; *cp; cp++)
3870 1.20 christos if (islower((unsigned char)*cp))
3871 1.20 christos return;
3872 1.12 christos
3873 1.20 christos len = (cp - name) + 1;
3874 1.20 christos
3875 1.20 christos /* There are duplicates. Discard if name has been seen. */
3876 1.20 christos
3877 1.20 christos for (seen = hstate->seen; *seen; seen++)
3878 1.20 christos if (!strcmp(*seen, name))
3879 1.20 christos return;
3880 1.12 christos
3881 1.20 christos n = (seen - hstate->seen) + 2;
3882 1.20 christos hstate->seen = erealloc(hstate->seen, n * sizeof(*seen));
3883 1.20 christos hstate->seen[n-2] = name;
3884 1.20 christos hstate->seen[n-1] = NULL;
3885 1.20 christos
3886 1.20 christos if (hstate->list != NULL)
3887 1.20 christos len += strlen(hstate->list);
3888 1.12 christos
3889 1.20 christos len += (hstate->idx >= K_PER_LINE)
3890 1.20 christos ? strlen(K_NL_PFX_STR)
3891 1.20 christos : strlen(K_DELIM_STR);
3892 1.20 christos
3893 1.20 christos if (hstate->list == NULL) {
3894 1.20 christos hstate->list = (char *)emalloc(len);
3895 1.20 christos hstate->list[0] = '\0';
3896 1.20 christos } else {
3897 1.20 christos hstate->list = (char *)erealloc(hstate->list, len);
3898 1.19 christos }
3899 1.20 christos
3900 1.20 christos sprintf(hstate->list + strlen(hstate->list), "%s%s",
3901 1.20 christos ((hstate->idx >= K_PER_LINE) ? K_NL_PFX_STR : K_DELIM_STR),
3902 1.20 christos name);
3903 1.20 christos
3904 1.20 christos if (hstate->idx >= K_PER_LINE)
3905 1.20 christos hstate->idx = 1;
3906 1.20 christos else
3907 1.20 christos hstate->idx++;
3908 1.19 christos }
3909 1.20 christos # endif /* !defined(BUILD_AS_LIB) */
3910 1.19 christos
3911 1.20 christos # ifndef BUILD_AS_LIB
3912 1.19 christos /* Insert CMAC into SSL digests list */
3913 1.19 christos static char *
3914 1.19 christos insert_cmac(char *list)
3915 1.19 christos {
3916 1.20 christos #ifdef ENABLE_CMAC
3917 1.20 christos int insert;
3918 1.20 christos size_t len;
3919 1.19 christos
3920 1.19 christos
3921 1.20 christos /* If list empty, we need to insert CMAC on new line */
3922 1.20 christos insert = (!list || !*list);
3923 1.20 christos
3924 1.19 christos if (insert) {
3925 1.20 christos len = strlen(K_NL_PFX_STR) + strlen(CMAC);
3926 1.19 christos list = (char *)erealloc(list, len + 1);
3927 1.20 christos sprintf(list, "%s%s", K_NL_PFX_STR, CMAC);
3928 1.20 christos } else { /* List not empty */
3929 1.20 christos /* Check if CMAC already in list - future proofing */
3930 1.20 christos const char *cmac_sn;
3931 1.20 christos char *cmac_p;
3932 1.20 christos
3933 1.20 christos cmac_sn = OBJ_nid2sn(NID_cmac);
3934 1.20 christos cmac_p = list;
3935 1.20 christos insert = cmac_sn != NULL && *cmac_sn != '\0';
3936 1.20 christos
3937 1.20 christos /* CMAC in list if found, followed by nul char or ',' */
3938 1.20 christos while (insert && NULL != (cmac_p = strstr(cmac_p, cmac_sn))) {
3939 1.20 christos cmac_p += strlen(cmac_sn);
3940 1.20 christos /* Still need to insert if not nul and not ',' */
3941 1.20 christos insert = *cmac_p && ',' != *cmac_p;
3942 1.20 christos }
3943 1.20 christos
3944 1.20 christos /* Find proper insertion point */
3945 1.20 christos if (insert) {
3946 1.20 christos char *last_nl;
3947 1.20 christos char *point;
3948 1.20 christos char *delim;
3949 1.20 christos int found;
3950 1.20 christos
3951 1.20 christos /* Default to start if list empty */
3952 1.20 christos found = 0;
3953 1.20 christos delim = list;
3954 1.20 christos len = strlen(list);
3955 1.20 christos
3956 1.20 christos /* While new lines */
3957 1.20 christos while (delim < list + len && *delim &&
3958 1.20 christos !strncmp(K_NL_PFX_STR, delim, strlen(K_NL_PFX_STR))) {
3959 1.20 christos point = delim + strlen(K_NL_PFX_STR);
3960 1.20 christos
3961 1.20 christos /* While digest names on line */
3962 1.20 christos while (point < list + len && *point) {
3963 1.20 christos /* Another digest after on same or next line? */
3964 1.20 christos delim = strstr( point, K_DELIM_STR);
3965 1.20 christos last_nl = strstr( point, K_NL_PFX_STR);
3966 1.20 christos
3967 1.20 christos /* No - end of list */
3968 1.20 christos if (!delim && !last_nl) {
3969 1.20 christos delim = list + len;
3970 1.20 christos } else
3971 1.20 christos /* New line and no delim or before delim? */
3972 1.20 christos if (last_nl && (!delim || last_nl < delim)) {
3973 1.20 christos delim = last_nl;
3974 1.20 christos }
3975 1.20 christos
3976 1.20 christos /* Found insertion point where CMAC before entry? */
3977 1.20 christos if (strncmp(CMAC, point, delim - point) < 0) {
3978 1.20 christos found = 1;
3979 1.20 christos break;
3980 1.20 christos }
3981 1.20 christos
3982 1.20 christos if (delim < list + len && *delim &&
3983 1.20 christos !strncmp(K_DELIM_STR, delim, strlen(K_DELIM_STR))) {
3984 1.20 christos point += strlen(K_DELIM_STR);
3985 1.20 christos } else {
3986 1.20 christos break;
3987 1.20 christos }
3988 1.20 christos } /* While digest names on line */
3989 1.20 christos } /* While new lines */
3990 1.20 christos
3991 1.20 christos /* If found in list */
3992 1.20 christos if (found) {
3993 1.20 christos /* insert cmac and delim */
3994 1.20 christos /* Space for list could move - save offset */
3995 1.20 christos ptrdiff_t p_offset = point - list;
3996 1.20 christos len += strlen(CMAC) + strlen(K_DELIM_STR);
3997 1.20 christos list = (char *)erealloc(list, len + 1);
3998 1.20 christos point = list + p_offset;
3999 1.20 christos /* move to handle src/dest overlap */
4000 1.20 christos memmove(point + strlen(CMAC) + strlen(K_DELIM_STR),
4001 1.19 christos point, strlen(point) + 1);
4002 1.20 christos strncpy(point, CMAC, strlen(CMAC));
4003 1.20 christos strncpy(point + strlen(CMAC), K_DELIM_STR, strlen(K_DELIM_STR));
4004 1.20 christos } else { /* End of list */
4005 1.20 christos /* append delim and cmac */
4006 1.20 christos len += strlen(K_DELIM_STR) + strlen(CMAC);
4007 1.20 christos list = (char *)erealloc(list, len + 1);
4008 1.20 christos strcpy(list + strlen(list), K_DELIM_STR);
4009 1.20 christos strcpy(list + strlen(list), CMAC);
4010 1.20 christos }
4011 1.20 christos } /* insert */
4012 1.20 christos } /* List not empty */
4013 1.20 christos #endif /*ENABLE_CMAC*/
4014 1.20 christos return list;
4015 1.12 christos }
4016 1.20 christos # endif /* !defined(BUILD_AS_LIB) */
4017 1.12 christos # endif
4018 1.12 christos #endif
4019 1.12 christos
4020 1.19 christos
4021 1.20 christos #ifndef BUILD_AS_LIB
4022 1.19 christos static char *
4023 1.19 christos list_digest_names(void)
4024 1.12 christos {
4025 1.20 christos char *list = NULL;
4026 1.20 christos
4027 1.12 christos #ifdef OPENSSL
4028 1.12 christos # ifdef HAVE_EVP_MD_DO_ALL_SORTED
4029 1.20 christos struct hstate hstate = { NULL, NULL, K_PER_LINE+1 };
4030 1.20 christos
4031 1.20 christos /* replace calloc(1, sizeof(const char *)) */
4032 1.20 christos hstate.seen = (const char **)emalloc_zero(sizeof(const char *));
4033 1.20 christos
4034 1.20 christos INIT_SSL();
4035 1.20 christos EVP_MD_do_all_sorted(list_md_fn, &hstate);
4036 1.20 christos list = hstate.list;
4037 1.20 christos free(hstate.seen);
4038 1.20 christos
4039 1.20 christos list = insert_cmac(list); /* Insert CMAC into SSL digests list */
4040 1.20 christos
4041 1.12 christos # else
4042 1.20 christos list = (char *)emalloc(sizeof("md5, others (upgrade to OpenSSL-1.0 for full list)"));
4043 1.20 christos strcpy(list, "md5, others (upgrade to OpenSSL-1.0 for full list)");
4044 1.12 christos # endif
4045 1.12 christos #else
4046 1.20 christos list = (char *)emalloc(sizeof("md5"));
4047 1.20 christos strcpy(list, "md5");
4048 1.12 christos #endif
4049 1.20 christos
4050 1.20 christos return list;
4051 1.12 christos }
4052 1.20 christos #endif /* !defined(BUILD_AS_LIB) */
4053 1.14 christos
4054 1.14 christos #define CTRLC_STACK_MAX 4
4055 1.14 christos static volatile size_t ctrlc_stack_len = 0;
4056 1.14 christos static volatile Ctrl_C_Handler ctrlc_stack[CTRLC_STACK_MAX];
4057 1.14 christos
4058 1.14 christos
4059 1.14 christos
4060 1.14 christos int/*BOOL*/
4061 1.14 christos push_ctrl_c_handler(
4062 1.14 christos Ctrl_C_Handler func
4063 1.14 christos )
4064 1.14 christos {
4065 1.14 christos size_t size = ctrlc_stack_len;
4066 1.14 christos if (func && (size < CTRLC_STACK_MAX)) {
4067 1.14 christos ctrlc_stack[size] = func;
4068 1.14 christos ctrlc_stack_len = size + 1;
4069 1.14 christos return TRUE;
4070 1.14 christos }
4071 1.14 christos return FALSE;
4072 1.14 christos }
4073 1.14 christos
4074 1.14 christos int/*BOOL*/
4075 1.14 christos pop_ctrl_c_handler(
4076 1.14 christos Ctrl_C_Handler func
4077 1.14 christos )
4078 1.14 christos {
4079 1.14 christos size_t size = ctrlc_stack_len;
4080 1.14 christos if (size) {
4081 1.14 christos --size;
4082 1.14 christos if (func == NULL || func == ctrlc_stack[size]) {
4083 1.14 christos ctrlc_stack_len = size;
4084 1.14 christos return TRUE;
4085 1.14 christos }
4086 1.14 christos }
4087 1.14 christos return FALSE;
4088 1.14 christos }
4089 1.14 christos
4090 1.20 christos #ifndef BUILD_AS_LIB
4091 1.14 christos static void
4092 1.14 christos on_ctrlc(void)
4093 1.14 christos {
4094 1.14 christos size_t size = ctrlc_stack_len;
4095 1.14 christos while (size)
4096 1.14 christos if ((*ctrlc_stack[--size])())
4097 1.14 christos break;
4098 1.14 christos }
4099 1.20 christos #endif /* !defined(BUILD_AS_LIB) */
4100 1.15 christos
4101 1.20 christos #ifndef BUILD_AS_LIB
4102 1.15 christos static int
4103 1.15 christos my_easprintf(
4104 1.15 christos char ** ppinto,
4105 1.15 christos const char * fmt ,
4106 1.15 christos ...
4107 1.15 christos )
4108 1.15 christos {
4109 1.15 christos va_list va;
4110 1.15 christos int prc;
4111 1.15 christos size_t len = 128;
4112 1.15 christos char * buf = emalloc(len);
4113 1.15 christos
4114 1.15 christos again:
4115 1.15 christos /* Note: we expect the memory allocation to fail long before the
4116 1.15 christos * increment in buffer size actually overflows.
4117 1.15 christos */
4118 1.15 christos buf = (buf) ? erealloc(buf, len) : emalloc(len);
4119 1.15 christos
4120 1.15 christos va_start(va, fmt);
4121 1.15 christos prc = vsnprintf(buf, len, fmt, va);
4122 1.15 christos va_end(va);
4123 1.15 christos
4124 1.15 christos if (prc < 0) {
4125 1.15 christos /* might be very old vsnprintf. Or actually MSVC... */
4126 1.15 christos len += len >> 1;
4127 1.15 christos goto again;
4128 1.15 christos }
4129 1.15 christos if ((size_t)prc >= len) {
4130 1.15 christos /* at least we have the proper size now... */
4131 1.15 christos len = (size_t)prc + 1;
4132 1.15 christos goto again;
4133 1.15 christos }
4134 1.15 christos if ((size_t)prc < (len - 32))
4135 1.15 christos buf = erealloc(buf, (size_t)prc + 1);
4136 1.15 christos *ppinto = buf;
4137 1.15 christos return prc;
4138 1.15 christos }
4139 1.20 christos #endif /* !defined(BUILD_AS_LIB) */
4140