1 /* 2 * ntpdc - control and monitor your ntpd daemon 3 */ 4 #include <stdio.h> 5 #include <ctype.h> 6 #include <signal.h> 7 #include <setjmp.h> 8 #include <sys/types.h> 9 #include <sys/time.h> 10 #include <netdb.h> 11 12 #ifdef SYS_WINNT 13 #include <io.h> 14 #else 15 #define closesocket close 16 #endif /* SYS_WINNT */ 17 18 19 #include "ntpdc.h" 20 #include "ntp_select.h" 21 #include "ntp_io.h" 22 #include "ntp_stdlib.h" 23 24 #ifdef SYS_VXWORKS 25 /* vxWorks needs mode flag -casey*/ 26 #define open(name, flags) open(name, flags, 0777) 27 #define SERVER_PORT_NUM 123 28 #endif 29 30 /* 31 * Because we now potentially understand a lot of commands (and 32 * it requires a lot of commands to talk to ntpd) we will run 33 * interactive if connected to a terminal. 34 */ 35 static int interactive = 0; /* set to 1 when we should prompt */ 36 static const char * prompt = "ntpdc> "; /* prompt to ask him about */ 37 38 /* 39 * Keyid used for authenticated requests. Obtained on the fly. 40 */ 41 static u_long info_auth_keyid; 42 43 /* 44 * Type of key md5 or des 45 */ 46 #define KEY_TYPE_DES 3 47 #define KEY_TYPE_MD5 4 48 49 static int info_auth_keytype = KEY_TYPE_MD5; /* MD5*/ 50 u_long current_time; /* needed by authkeys; not used */ 51 52 int ntpdcmain P((int, char **)); 53 /* 54 * Built in command handler declarations 55 */ 56 static int openhost P((const char *)); 57 static int sendpkt P((char *, int)); 58 static void growpktdata P((void)); 59 static int getresponse P((int, int, int *, int *, char **)); 60 static int sendrequest P((int, int, int, int, int, char *)); 61 static void getcmds P((void)); 62 static RETSIGTYPE abortcmd P((int)); 63 static void docmd P((const char *)); 64 static void tokenize P((const char *, char **, int *)); 65 static int findcmd P((char *, struct xcmd *, struct xcmd *, struct xcmd **)); 66 static int getarg P((char *, int, arg_v *)); 67 static int getnetnum P((const char *, u_int32 *, char *)); 68 static void help P((struct parse *, FILE *)); 69 #ifdef QSORT_USES_VOID_P 70 static int helpsort P((const void *, const void *)); 71 #else 72 static int helpsort P((char **, char **)); 73 #endif 74 static void printusage P((struct xcmd *, FILE *)); 75 static void timeout P((struct parse *, FILE *)); 76 static void my_delay P((struct parse *, FILE *)); 77 static void host P((struct parse *, FILE *)); 78 static void keyid P((struct parse *, FILE *)); 79 static void keytype P((struct parse *, FILE *)); 80 static void passwd P((struct parse *, FILE *)); 81 static void hostnames P((struct parse *, FILE *)); 82 static void setdebug P((struct parse *, FILE *)); 83 static void quit P((struct parse *, FILE *)); 84 static void version P((struct parse *, FILE *)); 85 static void warning P((const char *, const char *, const char *)); 86 static void error P((const char *, const char *, const char *)); 87 static u_long getkeyid P((const char *)); 88 89 90 91 /* 92 * Built-in commands we understand 93 */ 94 static struct xcmd builtins[] = { 95 { "?", help, { OPT|NTP_STR, NO, NO, NO }, 96 { "command", "", "", "" }, 97 "tell the use and syntax of commands" }, 98 { "help", help, { OPT|NTP_STR, NO, NO, NO }, 99 { "command", "", "", "" }, 100 "tell the use and syntax of commands" }, 101 { "timeout", timeout, { OPT|UINT, NO, NO, NO }, 102 { "msec", "", "", "" }, 103 "set the primary receive time out" }, 104 { "delay", my_delay, { OPT|INT, NO, NO, NO }, 105 { "msec", "", "", "" }, 106 "set the delay added to encryption time stamps" }, 107 { "host", host, { OPT|NTP_STR, NO, NO, NO }, 108 { "hostname", "", "", "" }, 109 "specify the host whose NTP server we talk to" }, 110 { "passwd", passwd, { OPT|NTP_STR, NO, NO, NO }, 111 { "", "", "", "" }, 112 "specify a password to use for authenticated requests"}, 113 { "hostnames", hostnames, { OPT|NTP_STR, NO, NO, NO }, 114 { "yes|no", "", "", "" }, 115 "specify whether hostnames or net numbers are printed"}, 116 { "debug", setdebug, { OPT|NTP_STR, NO, NO, NO }, 117 { "no|more|less", "", "", "" }, 118 "set/change debugging level" }, 119 { "quit", quit, { NO, NO, NO, NO }, 120 { "", "", "", "" }, 121 "exit ntpdc" }, 122 { "exit", quit, { NO, NO, NO, NO }, 123 { "", "", "", "" }, 124 "exit ntpdc" }, 125 { "keyid", keyid, { OPT|UINT, NO, NO, NO }, 126 { "key#", "", "", "" }, 127 "set/show keyid to use for authenticated requests" }, 128 { "keytype", keytype, { OPT|NTP_STR, NO, NO, NO }, 129 { "(md5|des)", "", "", "" }, 130 "set/show key authentication type for authenticated requests (des|md5)" }, 131 { "version", version, { NO, NO, NO, NO }, 132 { "", "", "", "" }, 133 "print version number" }, 134 { 0, 0, { NO, NO, NO, NO }, 135 { "", "", "", "" }, "" } 136 }; 137 138 139 /* 140 * Default values we use. 141 */ 142 #define DEFTIMEOUT (5) /* 5 second time out */ 143 #define DEFSTIMEOUT (2) /* 2 second time out after first */ 144 #define DEFDELAY 0x51EB852 /* 20 milliseconds, l_fp fraction */ 145 #define DEFHOST "localhost" /* default host name */ 146 #define LENHOSTNAME 256 /* host name is 256 characters long */ 147 #define MAXCMDS 100 /* maximum commands on cmd line */ 148 #define MAXHOSTS 200 /* maximum hosts on cmd line */ 149 #define MAXLINE 512 /* maximum line length */ 150 #define MAXTOKENS (1+MAXARGS+2) /* maximum number of usable tokens */ 151 152 /* 153 * Some variables used and manipulated locally 154 */ 155 static struct timeval tvout = { DEFTIMEOUT, 0 }; /* time out for reads */ 156 static struct timeval tvsout = { DEFSTIMEOUT, 0 }; /* secondary time out */ 157 static l_fp delay_time; /* delay time */ 158 static char currenthost[LENHOSTNAME]; /* current host name */ 159 static struct sockaddr_in hostaddr = { 0 }; /* host address */ 160 static int showhostnames = 1; /* show host names by default */ 161 162 static int sockfd; /* fd socket is openned on */ 163 static int havehost = 0; /* set to 1 when host open */ 164 struct servent *server_entry = NULL; /* server entry for ntp */ 165 166 #if defined (SYS_WINNT) || defined (SYS_VXWORKS) 167 char password[9]; 168 #endif /* SYS_WINNT || SYS_VXWORKS */ 169 170 #ifdef SYS_WINNT 171 WORD wVersionRequested; 172 WSADATA wsaData; 173 DWORD NumberOfBytesWritten; 174 175 HANDLE TimerThreadHandle = NULL; /* 1998/06/03 - Used in ntplib/machines.c */ 176 void timer(void) { ; }; /* 1998/06/03 - Used in ntplib/machines.c */ 177 178 #endif /* SYS_WINNT */ 179 180 /* 181 * Holds data returned from queries. We allocate INITDATASIZE 182 * octets to begin with, increasing this as we need to. 183 */ 184 #define INITDATASIZE (sizeof(struct resp_pkt) * 16) 185 #define INCDATASIZE (sizeof(struct resp_pkt) * 8) 186 187 static char *pktdata; 188 static int pktdatasize; 189 190 /* 191 * For commands typed on the command line (with the -c option) 192 */ 193 static int numcmds = 0; 194 static const char *ccmds[MAXCMDS]; 195 #define ADDCMD(cp) if (numcmds < MAXCMDS) ccmds[numcmds++] = (cp) 196 197 /* 198 * When multiple hosts are specified. 199 */ 200 static int numhosts = 0; 201 static const char *chosts[MAXHOSTS]; 202 #define ADDHOST(cp) if (numhosts < MAXHOSTS) chosts[numhosts++] = (cp) 203 204 /* 205 * Error codes for internal use 206 */ 207 #define ERR_INCOMPLETE 16 208 #define ERR_TIMEOUT 17 209 210 /* 211 * Macro definitions we use 212 */ 213 #define ISSPACE(c) ((c) == ' ' || (c) == '\t') 214 #define ISEOL(c) ((c) == '\n' || (c) == '\r' || (c) == '\0') 215 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) 216 217 /* 218 * For converting time stamps to dates 219 */ 220 #define JAN_1970 2208988800 /* 1970 - 1900 in seconds */ 221 222 /* 223 * Jump buffer for longjumping back to the command level 224 */ 225 static jmp_buf interrupt_buf; 226 static volatile int jump = 0; 227 228 /* 229 * Pointer to current output unit 230 */ 231 static FILE *current_output; 232 233 /* 234 * Command table imported from ntpdc_ops.c 235 */ 236 extern struct xcmd opcmds[]; 237 238 char *progname; 239 volatile int debug; 240 241 #ifdef NO_MAIN_ALLOWED 242 CALL(ntpdc,"ntpdc",ntpdcmain); 243 #else 244 int 245 main( 246 int argc, 247 char *argv[] 248 ) 249 { 250 return ntpdcmain(argc, argv); 251 } 252 #endif 253 254 #ifdef SYS_VXWORKS 255 void clear_globals(void) 256 { 257 extern int ntp_optind; 258 extern char *ntp_optarg; 259 showhostnames = 0; /* show host names by default */ 260 ntp_optind = 0; 261 ntp_optarg = 0; 262 server_entry = NULL; /* server entry for ntp */ 263 havehost = 0; /* set to 1 when host open */ 264 numcmds = 0; 265 numhosts = 0; 266 } 267 #endif 268 269 /* 270 * main - parse arguments and handle options 271 */ 272 int 273 ntpdcmain( 274 int argc, 275 char *argv[] 276 ) 277 { 278 int c; 279 int errflg = 0; 280 extern int ntp_optind; 281 extern char *ntp_optarg; 282 283 delay_time.l_ui = 0; 284 delay_time.l_uf = DEFDELAY; 285 286 #ifdef SYS_VXWORKS 287 clear_globals(); 288 taskPrioritySet(taskIdSelf(), 100 ); 289 #endif 290 291 progname = argv[0]; 292 while ((c = ntp_getopt(argc, argv, "c:dilnps")) != EOF) 293 switch (c) { 294 case 'c': 295 ADDCMD(ntp_optarg); 296 break; 297 case 'd': 298 ++debug; 299 break; 300 case 'i': 301 interactive = 1; 302 break; 303 case 'l': 304 ADDCMD("listpeers"); 305 break; 306 case 'n': 307 showhostnames = 0; 308 break; 309 case 'p': 310 ADDCMD("peers"); 311 break; 312 case 's': 313 ADDCMD("dmpeers"); 314 break; 315 default: 316 errflg++; 317 break; 318 } 319 if (errflg) { 320 (void) fprintf(stderr, 321 "usage: %s [-dilnps] [-c cmd] host ...\n", 322 progname); 323 exit(2); 324 } 325 if (ntp_optind == argc) { 326 ADDHOST(DEFHOST); 327 } else { 328 for (; ntp_optind < argc; ntp_optind++) 329 ADDHOST(argv[ntp_optind]); 330 } 331 332 if (numcmds == 0 && interactive == 0 333 && isatty(fileno(stdin)) && isatty(fileno(stderr))) { 334 interactive = 1; 335 } 336 337 #ifndef SYS_WINNT /* Under NT cannot handle SIGINT, WIN32 spawns a handler */ 338 if (interactive) 339 (void) signal_no_reset(SIGINT, abortcmd); 340 #endif /* SYS_WINNT */ 341 342 /* 343 * Initialize the packet data buffer 344 */ 345 pktdata = (char *)malloc(INITDATASIZE); 346 if (pktdata == NULL) { 347 (void) fprintf(stderr, "%s: malloc() failed!\n", progname); 348 exit(1); 349 } 350 pktdatasize = INITDATASIZE; 351 352 #ifdef SYS_WINNT 353 wVersionRequested = MAKEWORD(1,1); 354 if (WSAStartup(wVersionRequested, &wsaData)) { 355 fprintf(stderr, "No useable winsock.dll"); 356 exit(1); 357 } 358 #endif /* SYS_WINNT */ 359 360 if (numcmds == 0) { 361 (void) openhost(chosts[0]); 362 getcmds(); 363 } else { 364 int ihost; 365 int icmd; 366 367 for (ihost = 0; ihost < numhosts; ihost++) { 368 if (openhost(chosts[ihost])) 369 for (icmd = 0; icmd < numcmds; icmd++) { 370 if (numhosts > 1) 371 printf ("--- %s ---\n",chosts[ihost]); 372 docmd(ccmds[icmd]); 373 } 374 } 375 } 376 #ifdef SYS_WINNT 377 WSACleanup(); 378 #endif 379 return(0); 380 } /* main end */ 381 382 383 /* 384 * openhost - open a socket to a host 385 */ 386 static int 387 openhost( 388 const char *hname 389 ) 390 { 391 u_int32 netnum; 392 char temphost[LENHOSTNAME]; 393 394 if (server_entry == NULL) { 395 server_entry = getservbyname("ntp", "udp"); 396 if (server_entry == NULL) { 397 #ifdef VMS /* UCX getservbyname() doesn't work [yet], but we do know better */ 398 server_entry = (struct servent *) 399 malloc(sizeof(struct servent)); 400 server_entry->s_port = htons(NTP_PORT); 401 #else 402 (void) fprintf(stderr, "%s: ntp/udp: unknown service\n", 403 progname); 404 exit(1); 405 #endif /* VMS & UCX */ 406 } 407 if (debug > 2) 408 printf("Got ntp/udp service entry\n"); 409 } 410 411 if (!getnetnum(hname, &netnum, temphost)) 412 return 0; 413 414 if (debug > 2) 415 printf("Opening host %s\n", temphost); 416 417 if (havehost == 1) { 418 if (debug > 2) 419 printf("Closing old host %s\n", currenthost); 420 (void) closesocket(sockfd); 421 havehost = 0; 422 } 423 (void) strcpy(currenthost, temphost); 424 425 hostaddr.sin_family = AF_INET; 426 #ifndef SYS_VXWORKS 427 hostaddr.sin_port = server_entry->s_port; 428 #else 429 hostaddr.sin_port = htons(SERVER_PORT_NUM); 430 #endif 431 hostaddr.sin_addr.s_addr = netnum; 432 433 #ifdef SYS_WINNT 434 { 435 int optionValue = SO_SYNCHRONOUS_NONALERT; 436 int err; 437 err = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&optionValue, sizeof(optionValue)); 438 if (err != NO_ERROR) { 439 (void) fprintf(stderr, "cannot open nonoverlapped sockets\n"); 440 exit(1); 441 } 442 } 443 444 sockfd = socket(AF_INET, SOCK_DGRAM, 0); 445 if (sockfd == INVALID_SOCKET) { 446 error("socket", "", ""); 447 exit(-1); 448 } 449 #else 450 sockfd = socket(AF_INET, SOCK_DGRAM, 0); 451 if (sockfd == -1) 452 error("socket", "", ""); 453 #endif /* SYS_WINNT */ 454 455 456 #ifdef NEED_RCVBUF_SLOP 457 # ifdef SO_RCVBUF 458 { 459 int rbufsize = INITDATASIZE + 2048; /* 2K for slop */ 460 461 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, 462 &rbufsize, sizeof(int)) == -1) 463 error("setsockopt", "", ""); 464 } 465 # endif 466 #endif 467 468 if (connect(sockfd, (struct sockaddr *)&hostaddr, 469 sizeof(hostaddr)) == -1) 470 error("connect", "", ""); 471 472 havehost = 1; 473 return 1; 474 } 475 476 477 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */ 478 /* 479 * sendpkt - send a packet to the remote host 480 */ 481 static int 482 sendpkt( 483 char *xdata, 484 int xdatalen 485 ) 486 { 487 if (send(sockfd, xdata, xdatalen, 0) == -1) { 488 warning("write to %s failed", currenthost, ""); 489 return -1; 490 } 491 492 return 0; 493 } 494 495 496 /* 497 * growpktdata - grow the packet data area 498 */ 499 static void 500 growpktdata(void) 501 { 502 pktdatasize += INCDATASIZE; 503 pktdata = (char *)realloc(pktdata, (unsigned)pktdatasize); 504 if (pktdata == 0) { 505 (void) fprintf(stderr, "%s: realloc() failed!\n", progname); 506 exit(1); 507 } 508 } 509 510 511 /* 512 * getresponse - get a (series of) response packet(s) and return the data 513 */ 514 static int 515 getresponse( 516 int implcode, 517 int reqcode, 518 int *ritems, 519 int *rsize, 520 char **rdata 521 ) 522 { 523 struct resp_pkt rpkt; 524 struct timeval tvo; 525 int items; 526 int size; 527 int datasize; 528 char *datap; 529 char haveseq[MAXSEQ+1]; 530 int firstpkt; 531 int lastseq; 532 int numrecv; 533 int seq; 534 fd_set fds; 535 int n; 536 537 /* 538 * This is pretty tricky. We may get between 1 and many packets 539 * back in response to the request. We peel the data out of 540 * each packet and collect it in one long block. When the last 541 * packet in the sequence is received we'll know how many we 542 * should have had. Note we use one long time out, should reconsider. 543 */ 544 *ritems = 0; 545 *rsize = 0; 546 firstpkt = 1; 547 numrecv = 0; 548 *rdata = datap = pktdata; 549 lastseq = 999; /* too big to be a sequence number */ 550 memset(haveseq, 0, sizeof(haveseq)); 551 FD_ZERO(&fds); 552 553 again: 554 if (firstpkt) 555 tvo = tvout; 556 else 557 tvo = tvsout; 558 559 FD_SET(sockfd, &fds); 560 n = select(sockfd+1, &fds, (fd_set *)0, (fd_set *)0, &tvo); 561 562 if (n == -1) { 563 warning("select fails", "", ""); 564 return -1; 565 } 566 if (n == 0) { 567 /* 568 * Timed out. Return what we have 569 */ 570 if (firstpkt) { 571 (void) fprintf(stderr, 572 "%s: timed out, nothing received\n", currenthost); 573 return ERR_TIMEOUT; 574 } else { 575 (void) fprintf(stderr, 576 "%s: timed out with incomplete data\n", 577 currenthost); 578 if (debug) { 579 printf("Received sequence numbers"); 580 for (n = 0; n <= MAXSEQ; n++) 581 if (haveseq[n]) 582 printf(" %d,", n); 583 if (lastseq != 999) 584 printf(" last frame received\n"); 585 else 586 printf(" last frame not received\n"); 587 } 588 return ERR_INCOMPLETE; 589 } 590 } 591 592 n = recv(sockfd, (char *)&rpkt, sizeof(rpkt), 0); 593 if (n == -1) { 594 warning("read", "", ""); 595 return -1; 596 } 597 598 599 /* 600 * Check for format errors. Bug proofing. 601 */ 602 if (n < RESP_HEADER_SIZE) { 603 if (debug) 604 printf("Short (%d byte) packet received\n", n); 605 goto again; 606 } 607 if (INFO_VERSION(rpkt.rm_vn_mode) > NTP_VERSION || 608 INFO_VERSION(rpkt.rm_vn_mode) < NTP_OLDVERSION) { 609 if (debug) 610 printf("Packet received with version %d\n", 611 INFO_VERSION(rpkt.rm_vn_mode)); 612 goto again; 613 } 614 if (INFO_MODE(rpkt.rm_vn_mode) != MODE_PRIVATE) { 615 if (debug) 616 printf("Packet received with mode %d\n", 617 INFO_MODE(rpkt.rm_vn_mode)); 618 goto again; 619 } 620 if (INFO_IS_AUTH(rpkt.auth_seq)) { 621 if (debug) 622 printf("Encrypted packet received\n"); 623 goto again; 624 } 625 if (!ISRESPONSE(rpkt.rm_vn_mode)) { 626 if (debug) 627 printf("Received request packet, wanted response\n"); 628 goto again; 629 } 630 if (INFO_MBZ(rpkt.mbz_itemsize) != 0) { 631 if (debug) 632 printf("Received packet with nonzero MBZ field!\n"); 633 goto again; 634 } 635 636 /* 637 * Check implementation/request. Could be old data getting to us. 638 */ 639 if (rpkt.implementation != implcode || rpkt.request != reqcode) { 640 if (debug) 641 printf( 642 "Received implementation/request of %d/%d, wanted %d/%d", 643 rpkt.implementation, rpkt.request, 644 implcode, reqcode); 645 goto again; 646 } 647 648 /* 649 * Check the error code. If non-zero, return it. 650 */ 651 if (INFO_ERR(rpkt.err_nitems) != INFO_OKAY) { 652 if (debug && ISMORE(rpkt.rm_vn_mode)) { 653 printf("Error code %d received on not-final packet\n", 654 INFO_ERR(rpkt.err_nitems)); 655 } 656 return (int)INFO_ERR(rpkt.err_nitems); 657 } 658 659 660 /* 661 * Collect items and size. Make sure they make sense. 662 */ 663 items = INFO_NITEMS(rpkt.err_nitems); 664 size = INFO_ITEMSIZE(rpkt.mbz_itemsize); 665 666 if ((datasize = items*size) > (n-RESP_HEADER_SIZE)) { 667 if (debug) 668 printf( 669 "Received items %d, size %d (total %d), data in packet is %d\n", 670 items, size, datasize, n-RESP_HEADER_SIZE); 671 goto again; 672 } 673 674 /* 675 * If this isn't our first packet, make sure the size matches 676 * the other ones. 677 */ 678 if (!firstpkt && size != *rsize) { 679 if (debug) 680 printf("Received itemsize %d, previous %d\n", 681 size, *rsize); 682 goto again; 683 } 684 685 /* 686 * If we've received this before, toss it 687 */ 688 seq = INFO_SEQ(rpkt.auth_seq); 689 if (haveseq[seq]) { 690 if (debug) 691 printf("Received duplicate sequence number %d\n", seq); 692 goto again; 693 } 694 haveseq[seq] = 1; 695 696 /* 697 * If this is the last in the sequence, record that. 698 */ 699 if (!ISMORE(rpkt.rm_vn_mode)) { 700 if (lastseq != 999) { 701 printf("Received second end sequence packet\n"); 702 goto again; 703 } 704 lastseq = seq; 705 } 706 707 /* 708 * So far, so good. Copy this data into the output array. 709 */ 710 if ((datap + datasize) > (pktdata + pktdatasize)) { 711 int offset = datap - pktdata; 712 growpktdata(); 713 *rdata = pktdata; /* might have been realloced ! */ 714 datap = pktdata + offset; 715 } 716 memmove(datap, (char *)rpkt.data, (unsigned)datasize); 717 datap += datasize; 718 if (firstpkt) { 719 firstpkt = 0; 720 *rsize = size; 721 } 722 *ritems += items; 723 724 /* 725 * Finally, check the count of received packets. If we've got them 726 * all, return 727 */ 728 ++numrecv; 729 if (numrecv <= lastseq) 730 goto again; 731 return INFO_OKAY; 732 } 733 734 735 /* 736 * sendrequest - format and send a request packet 737 */ 738 static int 739 sendrequest( 740 int implcode, 741 int reqcode, 742 int auth, 743 int qitems, 744 int qsize, 745 char *qdata 746 ) 747 { 748 struct req_pkt qpkt; 749 int datasize; 750 751 memset((char *)&qpkt, 0, sizeof qpkt); 752 753 qpkt.rm_vn_mode = RM_VN_MODE(0, 0, 0); 754 qpkt.implementation = (u_char)implcode; 755 qpkt.request = (u_char)reqcode; 756 757 datasize = qitems * qsize; 758 if (datasize != 0 && qdata != NULL) { 759 memmove((char *)qpkt.data, qdata, (unsigned)datasize); 760 qpkt.err_nitems = ERR_NITEMS(0, qitems); 761 qpkt.mbz_itemsize = MBZ_ITEMSIZE(qsize); 762 } else { 763 qpkt.err_nitems = ERR_NITEMS(0, 0); 764 qpkt.mbz_itemsize = MBZ_ITEMSIZE(0); 765 } 766 767 if (!auth) { 768 qpkt.auth_seq = AUTH_SEQ(0, 0); 769 return sendpkt((char *)&qpkt, REQ_LEN_NOMAC); 770 } else { 771 l_fp ts; 772 int maclen = 0; 773 const char *pass = "\0"; 774 775 if (info_auth_keyid == 0) { 776 maclen = getkeyid("Keyid: "); 777 if (maclen == 0) { 778 (void) fprintf(stderr, 779 "Invalid key identifier\n"); 780 return 1; 781 } 782 info_auth_keyid = maclen; 783 } 784 if (!authistrusted(info_auth_keyid)) { 785 pass = getpass((info_auth_keytype == KEY_TYPE_DES) 786 ? "DES Password: " : "MD5 Password: "); 787 if (*pass == '\0') { 788 (void) fprintf(stderr, 789 "Invalid password\n"); 790 return (1); 791 } 792 } 793 authusekey(info_auth_keyid, info_auth_keytype, (const u_char *)pass); 794 authtrust(info_auth_keyid, 1); 795 qpkt.auth_seq = AUTH_SEQ(1, 0); 796 qpkt.keyid = htonl(info_auth_keyid); 797 get_systime(&ts); 798 L_ADD(&ts, &delay_time); 799 HTONL_FP(&ts, &qpkt.tstamp); 800 maclen = authencrypt(info_auth_keyid, (u_int32 *)&qpkt, 801 REQ_LEN_NOMAC); 802 if (maclen == 0) { 803 (void) fprintf(stderr, "Key not found\n"); 804 return (1); 805 } 806 return sendpkt((char *)&qpkt, (int)(REQ_LEN_NOMAC + maclen)); 807 } 808 /*NOTREACHED*/ 809 } 810 811 812 /* 813 * doquery - send a request and process the response 814 */ 815 int 816 doquery( 817 int implcode, 818 int reqcode, 819 int auth, 820 int qitems, 821 int qsize, 822 char *qdata, 823 int *ritems, 824 int *rsize, 825 char **rdata, 826 int quiet_mask 827 ) 828 { 829 int res; 830 char junk[512]; 831 fd_set fds; 832 struct timeval tvzero; 833 834 /* 835 * Check to make sure host is open 836 */ 837 if (!havehost) { 838 (void) fprintf(stderr, "***No host open, use `host' command\n"); 839 return -1; 840 } 841 842 /* 843 * Poll the socket and clear out any pending data 844 */ 845 do { 846 tvzero.tv_sec = tvzero.tv_usec = 0; 847 FD_ZERO(&fds); 848 FD_SET(sockfd, &fds); 849 res = select(sockfd+1, &fds, (fd_set *)0, (fd_set *)0, &tvzero); 850 851 if (res == -1) { 852 warning("polling select", "", ""); 853 return -1; 854 } else if (res > 0) 855 856 (void) recv(sockfd, junk, sizeof junk, 0); 857 } while (res > 0); 858 859 860 /* 861 * send a request 862 */ 863 res = sendrequest(implcode, reqcode, auth, qitems, qsize, qdata); 864 if (res != 0) 865 return res; 866 867 /* 868 * Get the response. If we got a standard error, print a message 869 */ 870 res = getresponse(implcode, reqcode, ritems, rsize, rdata); 871 872 /* log error message if not told to be quiet */ 873 if ((res > 0) && (((1 << res) & quiet_mask) == 0)) { 874 switch(res) { 875 case INFO_ERR_IMPL: 876 (void) fprintf(stderr, 877 "***Server implementation incompatable with our own\n"); 878 break; 879 case INFO_ERR_REQ: 880 (void) fprintf(stderr, 881 "***Server doesn't implement this request\n"); 882 break; 883 case INFO_ERR_FMT: 884 (void) fprintf(stderr, 885 "***Server reports a format error in the received packet (shouldn't happen)\n"); 886 break; 887 case INFO_ERR_NODATA: 888 (void) fprintf(stderr, 889 "***Server reports data not found\n"); 890 break; 891 case INFO_ERR_AUTH: 892 (void) fprintf(stderr, "***Permission denied\n"); 893 break; 894 case ERR_TIMEOUT: 895 (void) fprintf(stderr, "***Request timed out\n"); 896 break; 897 case ERR_INCOMPLETE: 898 (void) fprintf(stderr, 899 "***Response from server was incomplete\n"); 900 break; 901 default: 902 (void) fprintf(stderr, 903 "***Server returns unknown error code %d\n", res); 904 break; 905 } 906 } 907 return res; 908 } 909 910 911 /* 912 * getcmds - read commands from the standard input and execute them 913 */ 914 static void 915 getcmds(void) 916 { 917 char line[MAXLINE]; 918 919 for (;;) { 920 if (interactive) { 921 #ifdef VMS /* work around a problem with mixing stdout & stderr */ 922 fputs("",stdout); 923 #endif 924 (void) fputs(prompt, stderr); 925 (void) fflush(stderr); 926 } 927 928 if (fgets(line, sizeof line, stdin) == NULL) 929 return; 930 931 docmd(line); 932 } 933 } 934 935 936 /* 937 * abortcmd - catch interrupts and abort the current command 938 */ 939 static RETSIGTYPE 940 abortcmd( 941 int sig 942 ) 943 { 944 945 if (current_output == stdout) 946 (void) fflush(stdout); 947 putc('\n', stderr); 948 (void) fflush(stderr); 949 if (jump) longjmp(interrupt_buf, 1); 950 } 951 952 953 /* 954 * docmd - decode the command line and execute a command 955 */ 956 static void 957 docmd( 958 const char *cmdline 959 ) 960 { 961 char *tokens[1+MAXARGS+2]; 962 struct parse pcmd; 963 int ntok; 964 static int i; 965 struct xcmd *xcmd; 966 967 /* 968 * Tokenize the command line. If nothing on it, return. 969 */ 970 tokenize(cmdline, tokens, &ntok); 971 if (ntok == 0) 972 return; 973 974 /* 975 * Find the appropriate command description. 976 */ 977 i = findcmd(tokens[0], builtins, opcmds, &xcmd); 978 if (i == 0) { 979 (void) fprintf(stderr, "***Command `%s' unknown\n", 980 tokens[0]); 981 return; 982 } else if (i >= 2) { 983 (void) fprintf(stderr, "***Command `%s' ambiguous\n", 984 tokens[0]); 985 return; 986 } 987 988 /* 989 * Save the keyword, then walk through the arguments, interpreting 990 * as we go. 991 */ 992 pcmd.keyword = tokens[0]; 993 pcmd.nargs = 0; 994 for (i = 0; i < MAXARGS && xcmd->arg[i] != NO; i++) { 995 if ((i+1) >= ntok) { 996 if (!(xcmd->arg[i] & OPT)) { 997 printusage(xcmd, stderr); 998 return; 999 } 1000 break; 1001 } 1002 if ((xcmd->arg[i] & OPT) && (*tokens[i+1] == '>')) 1003 break; 1004 if (!getarg(tokens[i+1], (int)xcmd->arg[i], &pcmd.argval[i])) 1005 return; 1006 pcmd.nargs++; 1007 } 1008 1009 i++; 1010 if (i < ntok && *tokens[i] == '>') { 1011 char *fname; 1012 1013 if (*(tokens[i]+1) != '\0') 1014 fname = tokens[i]+1; 1015 else if ((i+1) < ntok) 1016 fname = tokens[i+1]; 1017 else { 1018 (void) fprintf(stderr, "***No file for redirect\n"); 1019 return; 1020 } 1021 1022 current_output = fopen(fname, "w"); 1023 if (current_output == NULL) { 1024 (void) fprintf(stderr, "***Error opening %s: ", fname); 1025 perror(""); 1026 return; 1027 } 1028 i = 1; /* flag we need a close */ 1029 } else { 1030 current_output = stdout; 1031 i = 0; /* flag no close */ 1032 } 1033 1034 if (interactive && setjmp(interrupt_buf)) { 1035 return; 1036 } else { 1037 jump = 1; 1038 (xcmd->handler)(&pcmd, current_output); 1039 jump = 0; 1040 if (i) (void) fclose(current_output); 1041 } 1042 } 1043 1044 1045 /* 1046 * tokenize - turn a command line into tokens 1047 */ 1048 static void 1049 tokenize( 1050 const char *line, 1051 char **tokens, 1052 int *ntok 1053 ) 1054 { 1055 register const char *cp; 1056 register char *sp; 1057 static char tspace[MAXLINE]; 1058 1059 sp = tspace; 1060 cp = line; 1061 for (*ntok = 0; *ntok < MAXTOKENS; (*ntok)++) { 1062 tokens[*ntok] = sp; 1063 while (ISSPACE(*cp)) 1064 cp++; 1065 if (ISEOL(*cp)) 1066 break; 1067 do { 1068 *sp++ = *cp++; 1069 } while (!ISSPACE(*cp) && !ISEOL(*cp)); 1070 1071 *sp++ = '\0'; 1072 } 1073 } 1074 1075 1076 1077 /* 1078 * findcmd - find a command in a command description table 1079 */ 1080 static int 1081 findcmd( 1082 register char *str, 1083 struct xcmd *clist1, 1084 struct xcmd *clist2, 1085 struct xcmd **cmd 1086 ) 1087 { 1088 register struct xcmd *cl; 1089 register int clen; 1090 int nmatch; 1091 struct xcmd *nearmatch = NULL; 1092 struct xcmd *clist; 1093 1094 clen = strlen(str); 1095 nmatch = 0; 1096 if (clist1 != 0) 1097 clist = clist1; 1098 else if (clist2 != 0) 1099 clist = clist2; 1100 else 1101 return 0; 1102 1103 again: 1104 for (cl = clist; cl->keyword != 0; cl++) { 1105 /* do a first character check, for efficiency */ 1106 if (*str != *(cl->keyword)) 1107 continue; 1108 if (strncmp(str, cl->keyword, (unsigned)clen) == 0) { 1109 /* 1110 * Could be extact match, could be approximate. 1111 * Is exact if the length of the keyword is the 1112 * same as the str. 1113 */ 1114 if (*((cl->keyword) + clen) == '\0') { 1115 *cmd = cl; 1116 return 1; 1117 } 1118 nmatch++; 1119 nearmatch = cl; 1120 } 1121 } 1122 1123 /* 1124 * See if there is more to do. If so, go again. Sorry about the 1125 * goto, too much looking at BSD sources... 1126 */ 1127 if (clist == clist1 && clist2 != 0) { 1128 clist = clist2; 1129 goto again; 1130 } 1131 1132 /* 1133 * If we got extactly 1 near match, use it, else return number 1134 * of matches. 1135 */ 1136 if (nmatch == 1) { 1137 *cmd = nearmatch; 1138 return 1; 1139 } 1140 return nmatch; 1141 } 1142 1143 1144 /* 1145 * getarg - interpret an argument token 1146 */ 1147 static int 1148 getarg( 1149 char *str, 1150 int code, 1151 arg_v *argp 1152 ) 1153 { 1154 int isneg; 1155 char *cp, *np; 1156 static const char *digits = "0123456789"; 1157 1158 switch (code & ~OPT) { 1159 case NTP_STR: 1160 argp->string = str; 1161 break; 1162 case ADD: 1163 if (!getnetnum(str, &(argp->netnum), (char *)0)) { 1164 return 0; 1165 } 1166 break; 1167 case INT: 1168 case UINT: 1169 isneg = 0; 1170 np = str; 1171 if (*np == '-') { 1172 np++; 1173 isneg = 1; 1174 } 1175 1176 argp->uval = 0; 1177 do { 1178 cp = strchr(digits, *np); 1179 if (cp == NULL) { 1180 (void) fprintf(stderr, 1181 "***Illegal integer value %s\n", str); 1182 return 0; 1183 } 1184 argp->uval *= 10; 1185 argp->uval += (cp - digits); 1186 } while (*(++np) != '\0'); 1187 1188 if (isneg) { 1189 if ((code & ~OPT) == UINT) { 1190 (void) fprintf(stderr, 1191 "***Value %s should be unsigned\n", str); 1192 return 0; 1193 } 1194 argp->ival = -argp->ival; 1195 } 1196 break; 1197 } 1198 1199 return 1; 1200 } 1201 1202 1203 /* 1204 * getnetnum - given a host name, return its net number 1205 * and (optional) full name 1206 */ 1207 static int 1208 getnetnum( 1209 const char *hname, 1210 u_int32 *num, 1211 char *fullhost 1212 ) 1213 { 1214 struct hostent *hp; 1215 1216 if (decodenetnum(hname, num)) { 1217 if (fullhost != 0) { 1218 (void) sprintf(fullhost, 1219 "%u.%u.%u.%u", (u_int)((htonl(*num)>>24)&0xff), 1220 (u_int)((htonl(*num)>>16)&0xff), (u_int)((htonl(*num)>>8)&0xff), 1221 (u_int)(htonl(*num)&0xff)); 1222 } 1223 return 1; 1224 } else if ((hp = gethostbyname(hname)) != 0) { 1225 memmove((char *)num, hp->h_addr, sizeof(u_int32)); 1226 if (fullhost != 0) 1227 (void) strcpy(fullhost, hp->h_name); 1228 return 1; 1229 } else { 1230 (void) fprintf(stderr, "***Can't find host %s\n", hname); 1231 return 0; 1232 } 1233 /*NOTREACHED*/ 1234 } 1235 1236 /* 1237 * nntohost - convert network number to host name. This routine enforces 1238 * the showhostnames setting. 1239 */ 1240 char * 1241 nntohost( 1242 u_int32 netnum 1243 ) 1244 { 1245 if (!showhostnames) 1246 return numtoa(netnum); 1247 if ((ntohl(netnum) & REFCLOCK_MASK) == REFCLOCK_ADDR) 1248 return refnumtoa(netnum); 1249 return numtohost(netnum); 1250 } 1251 1252 1253 /* 1254 * Finally, the built in command handlers 1255 */ 1256 1257 /* 1258 * help - tell about commands, or details of a particular command 1259 */ 1260 static void 1261 help( 1262 struct parse *pcmd, 1263 FILE *fp 1264 ) 1265 { 1266 int i; 1267 int n; 1268 struct xcmd *xcp; 1269 char *cmd; 1270 const char *cmdsort[100]; 1271 int length[100]; 1272 int maxlength; 1273 int numperline; 1274 static const char *spaces = " "; /* 20 spaces */ 1275 1276 if (pcmd->nargs == 0) { 1277 n = 0; 1278 for (xcp = builtins; xcp->keyword != 0; xcp++) { 1279 if (*(xcp->keyword) != '?') 1280 cmdsort[n++] = xcp->keyword; 1281 } 1282 for (xcp = opcmds; xcp->keyword != 0; xcp++) 1283 cmdsort[n++] = xcp->keyword; 1284 1285 #ifdef QSORT_USES_VOID_P 1286 qsort(cmdsort, n, sizeof(char *), helpsort); 1287 #else 1288 qsort((char *)cmdsort, n, sizeof(char *), helpsort); 1289 #endif 1290 1291 maxlength = 0; 1292 for (i = 0; i < n; i++) { 1293 length[i] = strlen(cmdsort[i]); 1294 if (length[i] > maxlength) 1295 maxlength = length[i]; 1296 } 1297 maxlength++; 1298 numperline = 76 / maxlength; 1299 1300 (void) fprintf(fp, "Commands available:\n"); 1301 for (i = 0; i < n; i++) { 1302 if ((i % numperline) == (numperline-1) 1303 || i == (n-1)) 1304 (void) fprintf(fp, "%s\n", cmdsort[i]); 1305 else 1306 (void) fprintf(fp, "%s%s", cmdsort[i], 1307 spaces+20-maxlength+length[i]); 1308 } 1309 } else { 1310 cmd = pcmd->argval[0].string; 1311 n = findcmd(cmd, builtins, opcmds, &xcp); 1312 if (n == 0) { 1313 (void) fprintf(stderr, 1314 "Command `%s' is unknown\n", cmd); 1315 return; 1316 } else if (n >= 2) { 1317 (void) fprintf(stderr, 1318 "Command `%s' is ambiguous\n", cmd); 1319 return; 1320 } 1321 (void) fprintf(fp, "function: %s\n", xcp->comment); 1322 printusage(xcp, fp); 1323 } 1324 } 1325 1326 1327 /* 1328 * helpsort - do hostname qsort comparisons 1329 */ 1330 #ifdef QSORT_USES_VOID_P 1331 static int 1332 helpsort( 1333 const void *t1, 1334 const void *t2 1335 ) 1336 { 1337 const char **name1 = (const char **)t1; 1338 const char **name2 = (const char **)t2; 1339 1340 return strcmp(*name1, *name2); 1341 } 1342 #else 1343 static int 1344 helpsort( 1345 char **name1, 1346 char **name2 1347 ) 1348 { 1349 return strcmp(*name1, *name2); 1350 } 1351 #endif 1352 1353 1354 /* 1355 * printusage - print usage information for a command 1356 */ 1357 static void 1358 printusage( 1359 struct xcmd *xcp, 1360 FILE *fp 1361 ) 1362 { 1363 register int i; 1364 1365 (void) fprintf(fp, "usage: %s", xcp->keyword); 1366 for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) { 1367 if (xcp->arg[i] & OPT) 1368 (void) fprintf(fp, " [ %s ]", xcp->desc[i]); 1369 else 1370 (void) fprintf(fp, " %s", xcp->desc[i]); 1371 } 1372 (void) fprintf(fp, "\n"); 1373 } 1374 1375 1376 /* 1377 * timeout - set time out time 1378 */ 1379 static void 1380 timeout( 1381 struct parse *pcmd, 1382 FILE *fp 1383 ) 1384 { 1385 int val; 1386 1387 if (pcmd->nargs == 0) { 1388 val = tvout.tv_sec * 1000 + tvout.tv_usec / 1000; 1389 (void) fprintf(fp, "primary timeout %d ms\n", val); 1390 } else { 1391 tvout.tv_sec = pcmd->argval[0].uval / 1000; 1392 tvout.tv_usec = (pcmd->argval[0].uval - (tvout.tv_sec * 1000)) 1393 * 1000; 1394 } 1395 } 1396 1397 1398 /* 1399 * my_delay - set delay for auth requests 1400 */ 1401 static void 1402 my_delay( 1403 struct parse *pcmd, 1404 FILE *fp 1405 ) 1406 { 1407 int isneg; 1408 u_long val; 1409 1410 if (pcmd->nargs == 0) { 1411 val = delay_time.l_ui * 1000 + delay_time.l_uf / 4294967; 1412 (void) fprintf(fp, "delay %lu ms\n", val); 1413 } else { 1414 if (pcmd->argval[0].ival < 0) { 1415 isneg = 1; 1416 val = (u_long)(-pcmd->argval[0].ival); 1417 } else { 1418 isneg = 0; 1419 val = (u_long)pcmd->argval[0].ival; 1420 } 1421 1422 delay_time.l_ui = val / 1000; 1423 val %= 1000; 1424 delay_time.l_uf = val * 4294967; /* 2**32/1000 */ 1425 1426 if (isneg) 1427 L_NEG(&delay_time); 1428 } 1429 } 1430 1431 1432 /* 1433 * host - set the host we are dealing with. 1434 */ 1435 static void 1436 host( 1437 struct parse *pcmd, 1438 FILE *fp 1439 ) 1440 { 1441 if (pcmd->nargs == 0) { 1442 if (havehost) 1443 (void) fprintf(fp, "current host is %s\n", currenthost); 1444 else 1445 (void) fprintf(fp, "no current host\n"); 1446 } else if (openhost(pcmd->argval[0].string)) { 1447 (void) fprintf(fp, "current host set to %s\n", currenthost); 1448 } else { 1449 if (havehost) 1450 (void) fprintf(fp, 1451 "current host remains %s\n", currenthost); 1452 else 1453 (void) fprintf(fp, "still no current host\n"); 1454 } 1455 } 1456 1457 1458 /* 1459 * keyid - get a keyid to use for authenticating requests 1460 */ 1461 static void 1462 keyid( 1463 struct parse *pcmd, 1464 FILE *fp 1465 ) 1466 { 1467 if (pcmd->nargs == 0) { 1468 if (info_auth_keyid == 0) 1469 (void) fprintf(fp, "no keyid defined\n"); 1470 else 1471 (void) fprintf(fp, "keyid is %lu\n", (u_long)info_auth_keyid); 1472 } else { 1473 info_auth_keyid = pcmd->argval[0].uval; 1474 } 1475 } 1476 1477 1478 /* 1479 * keytype - get type of key to use for authenticating requests 1480 */ 1481 static void 1482 keytype( 1483 struct parse *pcmd, 1484 FILE *fp 1485 ) 1486 { 1487 if (pcmd->nargs == 0) 1488 fprintf(fp, "keytype is %s\n", 1489 (info_auth_keytype == KEY_TYPE_MD5) ? "MD5" : "DES"); 1490 else 1491 switch (*(pcmd->argval[0].string)) { 1492 case 'm': 1493 case 'M': 1494 info_auth_keytype = KEY_TYPE_MD5; 1495 break; 1496 1497 case 'd': 1498 case 'D': 1499 info_auth_keytype = KEY_TYPE_DES; 1500 break; 1501 1502 default: 1503 fprintf(fp, "keytype must be 'md5' or 'des'\n"); 1504 } 1505 } 1506 1507 1508 1509 /* 1510 * passwd - get an authentication key 1511 */ 1512 /*ARGSUSED*/ 1513 static void 1514 passwd( 1515 struct parse *pcmd, 1516 FILE *fp 1517 ) 1518 { 1519 char *pass; 1520 1521 if (info_auth_keyid == 0) { 1522 info_auth_keyid = getkeyid("Keyid: "); 1523 if (info_auth_keyid == 0) { 1524 (void)fprintf(fp, "Keyid must be defined\n"); 1525 return; 1526 } 1527 } 1528 if (!interactive) { 1529 authusekey(info_auth_keyid, info_auth_keytype, 1530 (u_char *)pcmd->argval[0].string); 1531 } else { 1532 pass = getpass((info_auth_keytype == KEY_TYPE_DES) 1533 ? "DES Password: " 1534 : "MD5 Password: " 1535 ); 1536 if (*pass == '\0') 1537 (void) fprintf(fp, "Password unchanged\n"); 1538 else 1539 authusekey(info_auth_keyid, info_auth_keytype, 1540 (u_char *)pass); 1541 } 1542 } 1543 1544 1545 /* 1546 * hostnames - set the showhostnames flag 1547 */ 1548 static void 1549 hostnames( 1550 struct parse *pcmd, 1551 FILE *fp 1552 ) 1553 { 1554 if (pcmd->nargs == 0) { 1555 if (showhostnames) 1556 (void) fprintf(fp, "hostnames being shown\n"); 1557 else 1558 (void) fprintf(fp, "hostnames not being shown\n"); 1559 } else { 1560 if (STREQ(pcmd->argval[0].string, "yes")) 1561 showhostnames = 1; 1562 else if (STREQ(pcmd->argval[0].string, "no")) 1563 showhostnames = 0; 1564 else 1565 (void)fprintf(stderr, "What?\n"); 1566 } 1567 } 1568 1569 1570 /* 1571 * setdebug - set/change debugging level 1572 */ 1573 static void 1574 setdebug( 1575 struct parse *pcmd, 1576 FILE *fp 1577 ) 1578 { 1579 if (pcmd->nargs == 0) { 1580 (void) fprintf(fp, "debug level is %d\n", debug); 1581 return; 1582 } else if (STREQ(pcmd->argval[0].string, "no")) { 1583 debug = 0; 1584 } else if (STREQ(pcmd->argval[0].string, "more")) { 1585 debug++; 1586 } else if (STREQ(pcmd->argval[0].string, "less")) { 1587 debug--; 1588 } else { 1589 (void) fprintf(fp, "What?\n"); 1590 return; 1591 } 1592 (void) fprintf(fp, "debug level set to %d\n", debug); 1593 } 1594 1595 1596 /* 1597 * quit - stop this nonsense 1598 */ 1599 /*ARGSUSED*/ 1600 static void 1601 quit( 1602 struct parse *pcmd, 1603 FILE *fp 1604 ) 1605 { 1606 if (havehost) 1607 closesocket(sockfd); 1608 exit(0); 1609 } 1610 1611 1612 /* 1613 * version - print the current version number 1614 */ 1615 /*ARGSUSED*/ 1616 static void 1617 version( 1618 struct parse *pcmd, 1619 FILE *fp 1620 ) 1621 { 1622 1623 (void) fprintf(fp, "%s\n", Version); 1624 return; 1625 } 1626 1627 1628 /* 1629 * warning - print a warning message 1630 */ 1631 static void 1632 warning( 1633 const char *fmt, 1634 const char *st1, 1635 const char *st2 1636 ) 1637 { 1638 (void) fprintf(stderr, "%s: ", progname); 1639 (void) fprintf(stderr, fmt, st1, st2); 1640 (void) fprintf(stderr, ": "); 1641 perror(""); 1642 } 1643 1644 1645 /* 1646 * error - print a message and exit 1647 */ 1648 static void 1649 error( 1650 const char *fmt, 1651 const char *st1, 1652 const char *st2 1653 ) 1654 { 1655 warning(fmt, st1, st2); 1656 exit(1); 1657 } 1658 1659 /* 1660 * getkeyid - prompt the user for a keyid to use 1661 */ 1662 static u_long 1663 getkeyid( 1664 const char *keyprompt 1665 ) 1666 { 1667 register char *p; 1668 register int c; 1669 FILE *fi; 1670 char pbuf[20]; 1671 1672 #ifndef SYS_WINNT 1673 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL) 1674 #else 1675 if ((fi = _fdopen((int)GetStdHandle(STD_INPUT_HANDLE), "r")) == NULL) 1676 #endif /* SYS_WINNT */ 1677 fi = stdin; 1678 else 1679 setbuf(fi, (char *)NULL); 1680 fprintf(stderr, "%s", keyprompt); fflush(stderr); 1681 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) { 1682 if (p < &pbuf[18]) 1683 *p++ = c; 1684 } 1685 *p = '\0'; 1686 if (fi != stdin) 1687 fclose(fi); 1688 return (u_int32)atoi(pbuf); 1689 } 1690