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