1 /* 2 * Copyright (c) 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)commands.c 8.4 (Berkeley) 5/30/95 34 * $FreeBSD: src/crypto/telnet/telnet/commands.c,v 1.12.2.7 2003/04/23 07:16:32 ru Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/un.h> 39 #include <sys/file.h> 40 #include <sys/socket.h> 41 #include <netinet/in.h> 42 43 #include <ctype.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <netdb.h> 47 #include <pwd.h> 48 #include <signal.h> 49 #include <stdarg.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include <arpa/telnet.h> 55 #include <arpa/inet.h> 56 57 #include "general.h" 58 59 #include "ring.h" 60 61 #include "externs.h" 62 #include "defines.h" 63 #include "types.h" 64 #include "misc.h" 65 66 #ifdef AUTHENTICATION 67 #include <libtelnet/auth.h> 68 #endif 69 #ifdef ENCRYPTION 70 #include <libtelnet/encrypt.h> 71 #endif 72 73 #include <netinet/in_systm.h> 74 #include <netinet/ip.h> 75 #include <netinet/ip6.h> 76 77 #ifndef MAXHOSTNAMELEN 78 #define MAXHOSTNAMELEN 256 79 #endif 80 81 typedef int (*intrtn_t)(int, char **); 82 83 #ifdef AUTHENTICATION 84 extern int auth_togdebug(int); 85 #endif 86 #ifdef ENCRYPTION 87 extern int EncryptAutoEnc(int); 88 extern int EncryptAutoDec(int); 89 extern int EncryptDebug(int); 90 extern int EncryptVerbose(int); 91 #endif /* ENCRYPTION */ 92 #if defined(IPPROTO_IP) && defined(IP_TOS) 93 int tos = -1; 94 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ 95 96 char *hostname; 97 static char _hostname[MAXHOSTNAMELEN]; 98 99 static int help(int, char **); 100 static int call(intrtn_t, ...); 101 static void cmdrc(char *, char *); 102 #ifdef INET6 103 static int switch_af(struct addrinfo **); 104 #endif 105 static int togglehelp(void); 106 static int send_tncmd(void (*)(int, int), const char *, char *); 107 static int setmod(int); 108 static int clearmode(int); 109 static int modehelp(void); 110 static int sourceroute(struct addrinfo *, char *, char **, int *, int *, int *); 111 112 typedef struct { 113 const char *name; /* command name */ 114 const char *help; /* help string (NULL for no help) */ 115 int (*handler)(int, char **); /* routine which executes command */ 116 int needconnect; /* Do we need to be connected to execute? */ 117 } Command; 118 119 static char line[256]; 120 static char saveline[256]; 121 static int margc; 122 static char *margv[20]; 123 124 #ifdef OPIE 125 #include <sys/wait.h> 126 #define PATH_OPIEKEY "/usr/bin/opiekey" 127 static int 128 opie_calc(int argc, char *argv[]) 129 { 130 int status; 131 132 if(argc != 3) { 133 printf("%s sequence challenge\n", argv[0]); 134 return (0); 135 } 136 137 switch(fork()) { 138 case 0: 139 execv(PATH_OPIEKEY, argv); 140 exit (1); 141 case -1: 142 perror("fork"); 143 break; 144 default: 145 (void) wait(&status); 146 if (WIFEXITED(status)) 147 return (WEXITSTATUS(status)); 148 } 149 return (0); 150 } 151 #endif 152 153 static void 154 makeargv(void) 155 { 156 char *cp, *cp2, c; 157 char **argp = margv; 158 159 margc = 0; 160 cp = line; 161 if (*cp == '!') { /* Special case shell escape */ 162 strcpy(saveline, line); /* save for shell command */ 163 *argp++ = strdup("!"); /* No room in string to get this */ 164 margc++; 165 cp++; 166 } 167 while ((c = *cp)) { 168 int inquote = 0; 169 while (isspace(c)) 170 c = *++cp; 171 if (c == '\0') 172 break; 173 *argp++ = cp; 174 margc += 1; 175 for (cp2 = cp; c != '\0'; c = *++cp) { 176 if (inquote) { 177 if (c == inquote) { 178 inquote = 0; 179 continue; 180 } 181 } else { 182 if (c == '\\') { 183 if ((c = *++cp) == '\0') 184 break; 185 } else if (c == '"') { 186 inquote = '"'; 187 continue; 188 } else if (c == '\'') { 189 inquote = '\''; 190 continue; 191 } else if (isspace(c)) 192 break; 193 } 194 *cp2++ = c; 195 } 196 *cp2 = '\0'; 197 if (c == '\0') 198 break; 199 cp++; 200 } 201 *argp++ = NULL; 202 } 203 204 /* 205 * Make a character string into a number. 206 * 207 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1). 208 */ 209 210 static int 211 special(char *s) 212 { 213 char c; 214 char b; 215 216 switch (*s) { 217 case '^': 218 b = *++s; 219 if (b == '?') { 220 c = b | 0x40; /* DEL */ 221 } else { 222 c = b & 0x1f; 223 } 224 break; 225 default: 226 c = *s; 227 break; 228 } 229 return c; 230 } 231 232 /* 233 * Construct a control character sequence 234 * for a special character. 235 */ 236 static const char * 237 control(cc_t c) 238 { 239 static char buf[5]; 240 /* 241 * The only way I could get the Sun 3.5 compiler 242 * to shut up about 243 * if ((unsigned int)c >= 0x80) 244 * was to assign "c" to an unsigned int variable... 245 * Arggg.... 246 */ 247 unsigned int uic = (unsigned int)c; 248 249 if (uic == 0x7f) 250 return ("^?"); 251 if (c == (cc_t)_POSIX_VDISABLE) { 252 return "off"; 253 } 254 if (uic >= 0x80) { 255 buf[0] = '\\'; 256 buf[1] = ((c>>6)&07) + '0'; 257 buf[2] = ((c>>3)&07) + '0'; 258 buf[3] = (c&07) + '0'; 259 buf[4] = 0; 260 } else if (uic >= 0x20) { 261 buf[0] = c; 262 buf[1] = 0; 263 } else { 264 buf[0] = '^'; 265 buf[1] = '@'+c; 266 buf[2] = 0; 267 } 268 return (buf); 269 } 270 271 /* 272 * The following are data structures and routines for 273 * the "send" command. 274 * 275 */ 276 277 struct sendlist { 278 const char *name; /* How user refers to it (case independent) */ 279 const char *help; /* Help information (0 ==> no help) */ 280 int needconnect; /* Need to be connected */ 281 int narg; /* Number of arguments */ 282 int (*handler)(char *, ...); /* Routine to perform (for special ops) */ 283 int nbyte; /* Number of bytes to send this command */ 284 int what; /* Character to be sent (<0 ==> special) */ 285 }; 286 287 288 static int 289 send_esc(void), 290 send_help(void), 291 send_docmd(char *), 292 send_dontcmd(char *), 293 send_willcmd(char *), 294 send_wontcmd(char *); 295 296 static struct sendlist Sendlist[] = { 297 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO }, 298 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT }, 299 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK }, 300 { "break", NULL, 1, 0, NULL, 2, BREAK }, 301 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC }, 302 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL }, 303 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 }, 304 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA }, 305 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP }, 306 { "intp", NULL, 1, 0, NULL, 2, IP }, 307 { "interrupt", NULL, 1, 0, NULL, 2, IP }, 308 { "intr", NULL, 1, 0, NULL, 2, IP }, 309 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP }, 310 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR }, 311 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT }, 312 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP }, 313 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF }, 314 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 }, 315 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 }, 316 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 }, 317 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 }, 318 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 }, 319 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 }, 320 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 }, 321 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 }, 322 { NULL, NULL, 0, 0, NULL, 0, 0 } 323 }; 324 325 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \ 326 sizeof(struct sendlist))) 327 328 static int 329 sendcmd(int argc, char *argv[]) 330 { 331 int count; /* how many bytes we are going to need to send */ 332 int i; 333 struct sendlist *s; /* pointer to current command */ 334 int success = 0; 335 int needconnect = 0; 336 337 if (argc < 2) { 338 printf("need at least one argument for 'send' command\n"); 339 printf("'send ?' for help\n"); 340 return 0; 341 } 342 /* 343 * First, validate all the send arguments. 344 * In addition, we see how much space we are going to need, and 345 * whether or not we will be doing a "SYNCH" operation (which 346 * flushes the network queue). 347 */ 348 count = 0; 349 for (i = 1; i < argc; i++) { 350 s = GETSEND(argv[i]); 351 if (s == NULL) { 352 printf("Unknown send argument '%s'\n'send ?' for help.\n", 353 argv[i]); 354 return 0; 355 } else if (Ambiguous((void *)s)) { 356 printf("Ambiguous send argument '%s'\n'send ?' for help.\n", 357 argv[i]); 358 return 0; 359 } 360 if (i + s->narg >= argc) { 361 fprintf(stderr, 362 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n", 363 s->narg, s->narg == 1 ? "" : "s", s->name, s->name); 364 return 0; 365 } 366 count += s->nbyte; 367 if ((void *)s->handler == (void *)send_help) { 368 send_help(); 369 return 0; 370 } 371 372 i += s->narg; 373 needconnect += s->needconnect; 374 } 375 if (!connected && needconnect) { 376 printf("?Need to be connected first.\n"); 377 printf("'send ?' for help\n"); 378 return 0; 379 } 380 /* Now, do we have enough room? */ 381 if (NETROOM() < count) { 382 printf("There is not enough room in the buffer TO the network\n"); 383 printf("to process your request. Nothing will be done.\n"); 384 printf("('send synch' will throw away most data in the network\n"); 385 printf("buffer, if this might help.)\n"); 386 return 0; 387 } 388 /* OK, they are all OK, now go through again and actually send */ 389 count = 0; 390 for (i = 1; i < argc; i++) { 391 if ((s = GETSEND(argv[i])) == NULL) { 392 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n"); 393 quit(); 394 /*NOTREACHED*/ 395 } 396 if (s->handler) { 397 count++; 398 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0, 399 (s->narg > 1) ? argv[i+2] : 0); 400 i += s->narg; 401 } else { 402 NET2ADD(IAC, s->what); 403 printoption("SENT", IAC, s->what); 404 } 405 } 406 return (count == success); 407 } 408 409 static int 410 send_esc(void) 411 { 412 NETADD(escape); 413 return 1; 414 } 415 416 static int 417 send_docmd(char *name) 418 { 419 return(send_tncmd(send_do, "do", name)); 420 } 421 422 static int 423 send_dontcmd(char *name) 424 { 425 return(send_tncmd(send_dont, "dont", name)); 426 } 427 428 static int 429 send_willcmd(char *name) 430 { 431 return(send_tncmd(send_will, "will", name)); 432 } 433 434 static int 435 send_wontcmd(char *name) 436 { 437 return(send_tncmd(send_wont, "wont", name)); 438 } 439 440 static int 441 send_tncmd(void (*func)(int, int), const char *cmd, char *name) 442 { 443 char **cpp; 444 extern char *telopts[]; 445 int val = 0; 446 447 if (isprefix(name, "help") || isprefix(name, "?")) { 448 int col, len; 449 450 printf("Usage: send %s <value|option>\n", cmd); 451 printf("\"value\" must be from 0 to 255\n"); 452 printf("Valid options are:\n\t"); 453 454 col = 8; 455 for (cpp = telopts; *cpp; cpp++) { 456 len = strlen(*cpp) + 3; 457 if (col + len > 65) { 458 printf("\n\t"); 459 col = 8; 460 } 461 printf(" \"%s\"", *cpp); 462 col += len; 463 } 464 printf("\n"); 465 return 0; 466 } 467 cpp = (char **)genget(name, telopts, sizeof(char *)); 468 if (Ambiguous(cpp)) { 469 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n", 470 name, cmd); 471 return 0; 472 } 473 if (cpp) { 474 val = cpp - telopts; 475 } else { 476 char *cp = name; 477 478 while (*cp >= '0' && *cp <= '9') { 479 val *= 10; 480 val += *cp - '0'; 481 cp++; 482 } 483 if (*cp != 0) { 484 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n", 485 name, cmd); 486 return 0; 487 } else if (val < 0 || val > 255) { 488 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n", 489 name, cmd); 490 return 0; 491 } 492 } 493 if (!connected) { 494 printf("?Need to be connected first.\n"); 495 return 0; 496 } 497 (*func)(val, 1); 498 return 1; 499 } 500 501 static int 502 send_help(void) 503 { 504 struct sendlist *s; /* pointer to current command */ 505 for (s = Sendlist; s->name; s++) { 506 if (s->help) 507 printf("%-15s %s\n", s->name, s->help); 508 } 509 return(0); 510 } 511 512 /* 513 * The following are the routines and data structures referred 514 * to by the arguments to the "toggle" command. 515 */ 516 517 static int 518 lclchars(void) 519 { 520 donelclchars = 1; 521 return 1; 522 } 523 524 static int 525 togdebug(void) 526 { 527 #ifndef NOT43 528 if (net > 0 && 529 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) { 530 perror("setsockopt (SO_DEBUG)"); 531 } 532 #else /* NOT43 */ 533 if (debug) { 534 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) 535 perror("setsockopt (SO_DEBUG)"); 536 } else 537 printf("Cannot turn off socket debugging\n"); 538 #endif /* NOT43 */ 539 return 1; 540 } 541 542 543 static int 544 togcrlf(void) 545 { 546 if (crlf) { 547 printf("Will send carriage returns as telnet <CR><LF>.\n"); 548 } else { 549 printf("Will send carriage returns as telnet <CR><NUL>.\n"); 550 } 551 return 1; 552 } 553 554 int binmode; 555 556 static int 557 togbinary(int val) 558 { 559 donebinarytoggle = 1; 560 561 if (val >= 0) { 562 binmode = val; 563 } else { 564 if (my_want_state_is_will(TELOPT_BINARY) && 565 my_want_state_is_do(TELOPT_BINARY)) { 566 binmode = 1; 567 } else if (my_want_state_is_wont(TELOPT_BINARY) && 568 my_want_state_is_dont(TELOPT_BINARY)) { 569 binmode = 0; 570 } 571 val = binmode ? 0 : 1; 572 } 573 574 if (val == 1) { 575 if (my_want_state_is_will(TELOPT_BINARY) && 576 my_want_state_is_do(TELOPT_BINARY)) { 577 printf("Already operating in binary mode with remote host.\n"); 578 } else { 579 printf("Negotiating binary mode with remote host.\n"); 580 tel_enter_binary(3); 581 } 582 } else { 583 if (my_want_state_is_wont(TELOPT_BINARY) && 584 my_want_state_is_dont(TELOPT_BINARY)) { 585 printf("Already in network ascii mode with remote host.\n"); 586 } else { 587 printf("Negotiating network ascii mode with remote host.\n"); 588 tel_leave_binary(3); 589 } 590 } 591 return 1; 592 } 593 594 static int 595 togrbinary(int val) 596 { 597 donebinarytoggle = 1; 598 599 if (val == -1) 600 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1; 601 602 if (val == 1) { 603 if (my_want_state_is_do(TELOPT_BINARY)) { 604 printf("Already receiving in binary mode.\n"); 605 } else { 606 printf("Negotiating binary mode on input.\n"); 607 tel_enter_binary(1); 608 } 609 } else { 610 if (my_want_state_is_dont(TELOPT_BINARY)) { 611 printf("Already receiving in network ascii mode.\n"); 612 } else { 613 printf("Negotiating network ascii mode on input.\n"); 614 tel_leave_binary(1); 615 } 616 } 617 return 1; 618 } 619 620 static int 621 togxbinary(int val) 622 { 623 donebinarytoggle = 1; 624 625 if (val == -1) 626 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1; 627 628 if (val == 1) { 629 if (my_want_state_is_will(TELOPT_BINARY)) { 630 printf("Already transmitting in binary mode.\n"); 631 } else { 632 printf("Negotiating binary mode on output.\n"); 633 tel_enter_binary(2); 634 } 635 } else { 636 if (my_want_state_is_wont(TELOPT_BINARY)) { 637 printf("Already transmitting in network ascii mode.\n"); 638 } else { 639 printf("Negotiating network ascii mode on output.\n"); 640 tel_leave_binary(2); 641 } 642 } 643 return 1; 644 } 645 646 struct togglelist { 647 const char *name; /* name of toggle */ 648 const char *help; /* help message */ 649 int (*handler)(int); /* routine to do actual setting */ 650 int *variable; 651 const char *actionexplanation; 652 }; 653 654 static struct togglelist Togglelist[] = { 655 { "autoflush", 656 "flushing of output when sending interrupt characters", 657 0, 658 &autoflush, 659 "flush output when sending interrupt characters" }, 660 { "autosynch", 661 "automatic sending of interrupt characters in urgent mode", 662 0, 663 &autosynch, 664 "send interrupt characters in urgent mode" }, 665 #ifdef AUTHENTICATION 666 { "autologin", 667 "automatic sending of login and/or authentication info", 668 0, 669 &autologin, 670 "send login name and/or authentication information" }, 671 { "authdebug", 672 "Toggle authentication debugging", 673 auth_togdebug, 674 0, 675 "print authentication debugging information" }, 676 #endif 677 #ifdef ENCRYPTION 678 { "autoencrypt", 679 "automatic encryption of data stream", 680 EncryptAutoEnc, 681 0, 682 "automatically encrypt output" }, 683 { "autodecrypt", 684 "automatic decryption of data stream", 685 EncryptAutoDec, 686 0, 687 "automatically decrypt input" }, 688 { "verbose_encrypt", 689 "Toggle verbose encryption output", 690 EncryptVerbose, 691 0, 692 "print verbose encryption output" }, 693 { "encdebug", 694 "Toggle encryption debugging", 695 EncryptDebug, 696 0, 697 "print encryption debugging information" }, 698 #endif /* ENCRYPTION */ 699 { "skiprc", 700 "don't read ~/.telnetrc file", 701 0, 702 &skiprc, 703 "skip reading of ~/.telnetrc file" }, 704 { "binary", 705 "sending and receiving of binary data", 706 togbinary, 707 0, 708 0 }, 709 { "inbinary", 710 "receiving of binary data", 711 togrbinary, 712 0, 713 0 }, 714 { "outbinary", 715 "sending of binary data", 716 togxbinary, 717 0, 718 0 }, 719 { "crlf", 720 "sending carriage returns as telnet <CR><LF>", 721 (int (*)(int))togcrlf, 722 &crlf, 723 0 }, 724 { "crmod", 725 "mapping of received carriage returns", 726 0, 727 &crmod, 728 "map carriage return on output" }, 729 { "localchars", 730 "local recognition of certain control characters", 731 (int (*)(int))lclchars, 732 &localchars, 733 "recognize certain control characters" }, 734 { " ", "", NULL, NULL, NULL }, /* empty line */ 735 { "debug", 736 "debugging", 737 (int (*)(int))togdebug, 738 &debug, 739 "turn on socket level debugging" }, 740 { "netdata", 741 "printing of hexadecimal network data (debugging)", 742 0, 743 &netdata, 744 "print hexadecimal representation of network traffic" }, 745 { "prettydump", 746 "output of \"netdata\" to user readable format (debugging)", 747 0, 748 &prettydump, 749 "print user readable output for \"netdata\"" }, 750 { "options", 751 "viewing of options processing (debugging)", 752 0, 753 &showoptions, 754 "show option processing" }, 755 { "termdata", 756 "(debugging) toggle printing of hexadecimal terminal data", 757 0, 758 &termdata, 759 "print hexadecimal representation of terminal traffic" }, 760 { "?", 761 NULL, 762 (int (*)(int))togglehelp, 763 NULL, 764 NULL }, 765 { NULL, NULL, NULL, NULL, NULL }, 766 { "help", 767 NULL, 768 (int (*)(int))togglehelp, 769 NULL, 770 NULL }, 771 { NULL, NULL, NULL, NULL, NULL } 772 }; 773 774 static int 775 togglehelp(void) 776 { 777 struct togglelist *c; 778 779 for (c = Togglelist; c->name; c++) { 780 if (c->help) { 781 if (*c->help) 782 printf("%-15s toggle %s\n", c->name, c->help); 783 else 784 printf("\n"); 785 } 786 } 787 printf("\n"); 788 printf("%-15s %s\n", "?", "display help information"); 789 return 0; 790 } 791 792 static void 793 settogglehelp(int set) 794 { 795 struct togglelist *c; 796 797 for (c = Togglelist; c->name; c++) { 798 if (c->help) { 799 if (*c->help) 800 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable", 801 c->help); 802 else 803 printf("\n"); 804 } 805 } 806 } 807 808 #define GETTOGGLE(name) (struct togglelist *) \ 809 genget(name, (char **) Togglelist, sizeof(struct togglelist)) 810 811 static int 812 toggle(int argc, char *argv[]) 813 { 814 int retval = 1; 815 char *name; 816 struct togglelist *c; 817 818 if (argc < 2) { 819 fprintf(stderr, 820 "Need an argument to 'toggle' command. 'toggle ?' for help.\n"); 821 return 0; 822 } 823 argc--; 824 argv++; 825 while (argc--) { 826 name = *argv++; 827 c = GETTOGGLE(name); 828 if (Ambiguous((void *)c)) { 829 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n", 830 name); 831 return 0; 832 } else if (c == NULL) { 833 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n", 834 name); 835 return 0; 836 } else { 837 if (c->variable) { 838 *c->variable = !*c->variable; /* invert it */ 839 if (c->actionexplanation) { 840 printf("%s %s.\n", *c->variable? "Will" : "Won't", 841 c->actionexplanation); 842 } 843 } 844 if (c->handler) { 845 retval &= (*c->handler)(-1); 846 } 847 } 848 } 849 return retval; 850 } 851 852 /* 853 * The following perform the "set" command. 854 */ 855 856 #ifdef USE_TERMIO 857 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 }; 858 #endif 859 860 struct setlist { 861 const char *name; /* name */ 862 const char *help; /* help information */ 863 void (*handler)(char *); 864 cc_t *charp; /* where it is located at */ 865 }; 866 867 static struct setlist Setlist[] = { 868 #ifdef KLUDGELINEMODE 869 { "echo", "character to toggle local echoing on/off", NULL, &echoc }, 870 #endif 871 { "escape", "character to escape back to telnet command mode", NULL, &escape }, 872 { "rlogin", "rlogin escape character", 0, &rlogin }, 873 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile}, 874 { " ", "", NULL, NULL }, 875 { " ", "The following need 'localchars' to be toggled true", NULL, NULL }, 876 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp }, 877 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp }, 878 { "quit", "character to cause an Abort process", NULL, termQuitCharp }, 879 { "eof", "character to cause an EOF ", NULL, termEofCharp }, 880 { " ", "", NULL, NULL }, 881 { " ", "The following are for local editing in linemode", NULL, NULL }, 882 { "erase", "character to use to erase a character", NULL, termEraseCharp }, 883 { "kill", "character to use to erase a line", NULL, termKillCharp }, 884 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp }, 885 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp }, 886 { "reprint", "character to use for line reprint", NULL, termRprntCharp }, 887 { "worderase", "character to use to erase a word", NULL, termWerasCharp }, 888 { "start", "character to use for XON", NULL, termStartCharp }, 889 { "stop", "character to use for XOFF", NULL, termStopCharp }, 890 { "forw1", "alternate end of line character", NULL, termForw1Charp }, 891 { "forw2", "alternate end of line character", NULL, termForw2Charp }, 892 { "ayt", "alternate AYT character", NULL, termAytCharp }, 893 { NULL, NULL, NULL, NULL } 894 }; 895 896 static struct setlist * 897 getset(char *name) 898 { 899 return (struct setlist *) 900 genget(name, (char **) Setlist, sizeof(struct setlist)); 901 } 902 903 void 904 set_escape_char(char *s) 905 { 906 if (rlogin != _POSIX_VDISABLE) { 907 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE; 908 printf("Telnet rlogin escape character is '%s'.\n", 909 control(rlogin)); 910 } else { 911 escape = (s && *s) ? special(s) : _POSIX_VDISABLE; 912 printf("Telnet escape character is '%s'.\n", control(escape)); 913 } 914 } 915 916 static int 917 setcmd(int argc, char *argv[]) 918 { 919 int value; 920 struct setlist *ct; 921 struct togglelist *c; 922 923 if (argc < 2 || argc > 3) { 924 printf("Format is 'set Name Value'\n'set ?' for help.\n"); 925 return 0; 926 } 927 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) { 928 for (ct = Setlist; ct->name; ct++) 929 printf("%-15s %s\n", ct->name, ct->help); 930 printf("\n"); 931 settogglehelp(1); 932 printf("%-15s %s\n", "?", "display help information"); 933 return 0; 934 } 935 936 ct = getset(argv[1]); 937 if (ct == NULL) { 938 c = GETTOGGLE(argv[1]); 939 if (c == NULL) { 940 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n", 941 argv[1]); 942 return 0; 943 } else if (Ambiguous((void *)c)) { 944 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n", 945 argv[1]); 946 return 0; 947 } 948 if (c->variable) { 949 if ((argc == 2) || (strcmp("on", argv[2]) == 0)) 950 *c->variable = 1; 951 else if (strcmp("off", argv[2]) == 0) 952 *c->variable = 0; 953 else { 954 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n"); 955 return 0; 956 } 957 if (c->actionexplanation) { 958 printf("%s %s.\n", *c->variable? "Will" : "Won't", 959 c->actionexplanation); 960 } 961 } 962 if (c->handler) 963 (*c->handler)(1); 964 } else if (argc != 3) { 965 printf("Format is 'set Name Value'\n'set ?' for help.\n"); 966 return 0; 967 } else if (Ambiguous((void *)ct)) { 968 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n", 969 argv[1]); 970 return 0; 971 } else if (ct->handler) { 972 (*ct->handler)(argv[2]); 973 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp); 974 } else { 975 if (strcmp("off", argv[2])) { 976 value = special(argv[2]); 977 } else { 978 value = _POSIX_VDISABLE; 979 } 980 *(ct->charp) = (cc_t)value; 981 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp))); 982 } 983 slc_check(); 984 return 1; 985 } 986 987 static int 988 unsetcmd(int argc, char *argv[]) 989 { 990 struct setlist *ct; 991 struct togglelist *c; 992 char *name; 993 994 if (argc < 2) { 995 fprintf(stderr, 996 "Need an argument to 'unset' command. 'unset ?' for help.\n"); 997 return 0; 998 } 999 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) { 1000 for (ct = Setlist; ct->name; ct++) 1001 printf("%-15s %s\n", ct->name, ct->help); 1002 printf("\n"); 1003 settogglehelp(0); 1004 printf("%-15s %s\n", "?", "display help information"); 1005 return 0; 1006 } 1007 1008 argc--; 1009 argv++; 1010 while (argc--) { 1011 name = *argv++; 1012 ct = getset(name); 1013 if (ct == NULL) { 1014 c = GETTOGGLE(name); 1015 if (c == NULL) { 1016 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n", 1017 name); 1018 return 0; 1019 } else if (Ambiguous((void *)c)) { 1020 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n", 1021 name); 1022 return 0; 1023 } 1024 if (c->variable) { 1025 *c->variable = 0; 1026 if (c->actionexplanation) { 1027 printf("%s %s.\n", *c->variable? "Will" : "Won't", 1028 c->actionexplanation); 1029 } 1030 } 1031 if (c->handler) 1032 (*c->handler)(0); 1033 } else if (Ambiguous((void *)ct)) { 1034 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n", 1035 name); 1036 return 0; 1037 } else if (ct->handler) { 1038 (*ct->handler)(0); 1039 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp); 1040 } else { 1041 *(ct->charp) = _POSIX_VDISABLE; 1042 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp))); 1043 } 1044 } 1045 return 1; 1046 } 1047 1048 /* 1049 * The following are the data structures and routines for the 1050 * 'mode' command. 1051 */ 1052 #ifdef KLUDGELINEMODE 1053 extern int kludgelinemode; 1054 1055 static int 1056 dokludgemode(void) 1057 { 1058 kludgelinemode = 1; 1059 send_wont(TELOPT_LINEMODE, 1); 1060 send_dont(TELOPT_SGA, 1); 1061 send_dont(TELOPT_ECHO, 1); 1062 return 1; 1063 } 1064 #endif 1065 1066 static int 1067 dolinemode(void) 1068 { 1069 #ifdef KLUDGELINEMODE 1070 if (kludgelinemode) 1071 send_dont(TELOPT_SGA, 1); 1072 #endif 1073 send_will(TELOPT_LINEMODE, 1); 1074 send_dont(TELOPT_ECHO, 1); 1075 return 1; 1076 } 1077 1078 static int 1079 docharmode(void) 1080 { 1081 #ifdef KLUDGELINEMODE 1082 if (kludgelinemode) 1083 send_do(TELOPT_SGA, 1); 1084 else 1085 #endif 1086 send_wont(TELOPT_LINEMODE, 1); 1087 send_do(TELOPT_ECHO, 1); 1088 return 1; 1089 } 1090 1091 static int 1092 dolmmode(int bit, int on) 1093 { 1094 unsigned char c; 1095 extern int linemode; 1096 1097 if (my_want_state_is_wont(TELOPT_LINEMODE)) { 1098 printf("?Need to have LINEMODE option enabled first.\n"); 1099 printf("'mode ?' for help.\n"); 1100 return 0; 1101 } 1102 1103 if (on) 1104 c = (linemode | bit); 1105 else 1106 c = (linemode & ~bit); 1107 lm_mode(&c, 1, 1); 1108 return 1; 1109 } 1110 1111 static int 1112 setmod(int bit) 1113 { 1114 return dolmmode(bit, 1); 1115 } 1116 1117 static int 1118 clearmode(int bit) 1119 { 1120 return dolmmode(bit, 0); 1121 } 1122 1123 struct modelist { 1124 const char *name; /* command name */ 1125 const char *help; /* help string */ 1126 int (*handler)(int);/* routine which executes command */ 1127 int needconnect; /* Do we need to be connected to execute? */ 1128 int arg1; 1129 }; 1130 1131 static struct modelist ModeList[] = { 1132 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 }, 1133 #ifdef KLUDGELINEMODE 1134 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 }, 1135 #endif 1136 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 }, 1137 #ifdef KLUDGELINEMODE 1138 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 }, 1139 #endif 1140 { "", "", NULL, 0, 0 }, 1141 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 }, 1142 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG }, 1143 { "+isig", 0, setmod, 1, MODE_TRAPSIG }, 1144 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG }, 1145 { "edit", "Enable character editing", setmod, 1, MODE_EDIT }, 1146 { "+edit", 0, setmod, 1, MODE_EDIT }, 1147 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT }, 1148 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB }, 1149 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB }, 1150 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB }, 1151 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO }, 1152 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO }, 1153 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO }, 1154 { "help", 0, (int (*)(int))modehelp, 0, 0 }, 1155 #ifdef KLUDGELINEMODE 1156 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 }, 1157 #endif 1158 { "", "", NULL, 0, 0 }, 1159 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 }, 1160 { NULL, NULL, NULL, 0, 0 }, 1161 }; 1162 1163 1164 static int 1165 modehelp(void) 1166 { 1167 struct modelist *mt; 1168 1169 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n"); 1170 for (mt = ModeList; mt->name; mt++) { 1171 if (mt->help) { 1172 if (*mt->help) 1173 printf("%-15s %s\n", mt->name, mt->help); 1174 else 1175 printf("\n"); 1176 } 1177 } 1178 return 0; 1179 } 1180 1181 #define GETMODECMD(name) (struct modelist *) \ 1182 genget(name, (char **) ModeList, sizeof(struct modelist)) 1183 1184 static int 1185 modecmd(int argc, char *argv[]) 1186 { 1187 struct modelist *mt; 1188 1189 if (argc != 2) { 1190 printf("'mode' command requires an argument\n"); 1191 printf("'mode ?' for help.\n"); 1192 } else if ((mt = GETMODECMD(argv[1])) == NULL) { 1193 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]); 1194 } else if (Ambiguous((void *)mt)) { 1195 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]); 1196 } else if (mt->needconnect && !connected) { 1197 printf("?Need to be connected first.\n"); 1198 printf("'mode ?' for help.\n"); 1199 } else if (mt->handler) { 1200 return (*mt->handler)(mt->arg1); 1201 } 1202 return 0; 1203 } 1204 1205 /* 1206 * The following data structures and routines implement the 1207 * "display" command. 1208 */ 1209 1210 static int 1211 display(int argc, char *argv[]) 1212 { 1213 struct togglelist *tl; 1214 struct setlist *sl; 1215 1216 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \ 1217 if (*tl->variable) { \ 1218 printf("will"); \ 1219 } else { \ 1220 printf("won't"); \ 1221 } \ 1222 printf(" %s.\n", tl->actionexplanation); \ 1223 } 1224 1225 #define doset(sl) if (sl->name && *sl->name != ' ') { \ 1226 if (sl->handler == 0) \ 1227 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \ 1228 else \ 1229 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \ 1230 } 1231 1232 if (argc == 1) { 1233 for (tl = Togglelist; tl->name; tl++) { 1234 dotog(tl); 1235 } 1236 printf("\n"); 1237 for (sl = Setlist; sl->name; sl++) { 1238 doset(sl); 1239 } 1240 } else { 1241 int i; 1242 1243 for (i = 1; i < argc; i++) { 1244 sl = getset(argv[i]); 1245 tl = GETTOGGLE(argv[i]); 1246 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) { 1247 printf("?Ambiguous argument '%s'.\n", argv[i]); 1248 return 0; 1249 } else if (!sl && !tl) { 1250 printf("?Unknown argument '%s'.\n", argv[i]); 1251 return 0; 1252 } else { 1253 if (tl) { 1254 dotog(tl); 1255 } 1256 if (sl) { 1257 doset(sl); 1258 } 1259 } 1260 } 1261 } 1262 /*@*/optionstatus(); 1263 #ifdef ENCRYPTION 1264 EncryptStatus(); 1265 #endif /* ENCRYPTION */ 1266 return 1; 1267 #undef doset 1268 #undef dotog 1269 } 1270 1271 /* 1272 * The following are the data structures, and many of the routines, 1273 * relating to command processing. 1274 */ 1275 1276 /* 1277 * Set the escape character. 1278 */ 1279 static int 1280 setescape(int argc, char *argv[]) 1281 { 1282 char *arg; 1283 char buf[50]; 1284 1285 printf( 1286 "Deprecated usage - please use 'set escape%s%s' in the future.\n", 1287 (argc > 2)? " ":"", (argc > 2)? argv[1]: ""); 1288 if (argc > 2) 1289 arg = argv[1]; 1290 else { 1291 printf("new escape character: "); 1292 (void) fgets(buf, sizeof(buf), stdin); 1293 arg = buf; 1294 } 1295 if (arg[0] != '\0') 1296 escape = arg[0]; 1297 (void) fflush(stdout); 1298 return 1; 1299 } 1300 1301 static int 1302 togcrmod(void) 1303 { 1304 crmod = !crmod; 1305 printf("Deprecated usage - please use 'toggle crmod' in the future.\n"); 1306 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't"); 1307 (void) fflush(stdout); 1308 return 1; 1309 } 1310 1311 static int 1312 suspend(void) 1313 { 1314 #ifdef SIGTSTP 1315 setcommandmode(); 1316 { 1317 long oldrows, oldcols, newrows, newcols, err_; 1318 1319 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0; 1320 (void) kill(0, SIGTSTP); 1321 /* 1322 * If we didn't get the window size before the SUSPEND, but we 1323 * can get them now (?), then send the NAWS to make sure that 1324 * we are set up for the right window size. 1325 */ 1326 if (TerminalWindowSize(&newrows, &newcols) && connected && 1327 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) { 1328 sendnaws(); 1329 } 1330 } 1331 /* reget parameters in case they were changed */ 1332 TerminalSaveState(); 1333 setconnmode(0); 1334 #else 1335 printf("Suspend is not supported. Try the '!' command instead\n"); 1336 #endif 1337 return 1; 1338 } 1339 1340 static int 1341 shell(int argc, char *argv[] __unused) 1342 { 1343 long oldrows, oldcols, newrows, newcols, err_; 1344 1345 setcommandmode(); 1346 1347 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0; 1348 switch(vfork()) { 1349 case -1: 1350 perror("Fork failed\n"); 1351 break; 1352 1353 case 0: 1354 { 1355 /* 1356 * Fire up the shell in the child. 1357 */ 1358 const char *shellp, *shellname; 1359 1360 shellp = getenv("SHELL"); 1361 if (shellp == NULL) 1362 shellp = "/bin/sh"; 1363 if ((shellname = strrchr(shellp, '/')) == NULL) 1364 shellname = shellp; 1365 else 1366 shellname++; 1367 if (argc > 1) 1368 execl(shellp, shellname, "-c", &saveline[1], NULL); 1369 else 1370 execl(shellp, shellname, NULL); 1371 perror("Execl"); 1372 _exit(1); 1373 } 1374 default: 1375 (void)wait(NULL); /* Wait for the shell to complete */ 1376 1377 if (TerminalWindowSize(&newrows, &newcols) && connected && 1378 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) { 1379 sendnaws(); 1380 } 1381 break; 1382 } 1383 return 1; 1384 } 1385 1386 static int 1387 bye(int argc, char *argv[]) 1388 { 1389 extern int resettermname; 1390 1391 if (connected) { 1392 (void) shutdown(net, SHUT_RDWR); 1393 printf("Connection closed.\n"); 1394 (void) NetClose(net); 1395 connected = 0; 1396 resettermname = 1; 1397 #ifdef AUTHENTICATION 1398 #ifdef ENCRYPTION 1399 auth_encrypt_connect(connected); 1400 #endif 1401 #endif 1402 /* reset options */ 1403 tninit(); 1404 } 1405 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) { 1406 longjmp(toplevel, 1); 1407 /* NOTREACHED */ 1408 } 1409 return 1; /* Keep lint, etc., happy */ 1410 } 1411 1412 void 1413 quit(void) 1414 { 1415 (void) call(bye, "bye", "fromquit", 0); 1416 Exit(0); 1417 } 1418 1419 static int 1420 logout(void) 1421 { 1422 send_do(TELOPT_LOGOUT, 1); 1423 (void) netflush(); 1424 return 1; 1425 } 1426 1427 1428 /* 1429 * The SLC command. 1430 */ 1431 1432 struct slclist { 1433 const char *name; 1434 const char *help; 1435 void (*handler)(int); 1436 int arg; 1437 }; 1438 1439 static void slc_help(void); 1440 1441 struct slclist SlcList[] = { 1442 { "export", "Use local special character definitions", 1443 (void (*)(int))slc_mode_export, 0 }, 1444 { "import", "Use remote special character definitions", 1445 slc_mode_import, 1 }, 1446 { "check", "Verify remote special character definitions", 1447 slc_mode_import, 0 }, 1448 { "help", NULL, (void (*)(int))slc_help, 0 }, 1449 { "?", "Print help information", (void (*)(int))slc_help, 0 }, 1450 { NULL, NULL, NULL, 0 }, 1451 }; 1452 1453 static void 1454 slc_help(void) 1455 { 1456 struct slclist *c; 1457 1458 for (c = SlcList; c->name; c++) { 1459 if (c->help) { 1460 if (*c->help) 1461 printf("%-15s %s\n", c->name, c->help); 1462 else 1463 printf("\n"); 1464 } 1465 } 1466 } 1467 1468 static struct slclist * 1469 getslc(char *name) 1470 { 1471 return (struct slclist *) 1472 genget(name, (char **) SlcList, sizeof(struct slclist)); 1473 } 1474 1475 static int 1476 slccmd(int argc, char *argv[]) 1477 { 1478 struct slclist *c; 1479 1480 if (argc != 2) { 1481 fprintf(stderr, 1482 "Need an argument to 'slc' command. 'slc ?' for help.\n"); 1483 return 0; 1484 } 1485 c = getslc(argv[1]); 1486 if (c == NULL) { 1487 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n", 1488 argv[1]); 1489 return 0; 1490 } 1491 if (Ambiguous((void *)c)) { 1492 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n", 1493 argv[1]); 1494 return 0; 1495 } 1496 (*c->handler)(c->arg); 1497 slcstate(); 1498 return 1; 1499 } 1500 1501 /* 1502 * The ENVIRON command. 1503 */ 1504 1505 struct envlist { 1506 const char *name; 1507 const char *help; 1508 void (*handler)(unsigned char *, unsigned char *); 1509 int narg; 1510 }; 1511 1512 extern struct env_lst * 1513 env_define(const unsigned char *, unsigned char *); 1514 extern void 1515 env_undefine(unsigned char *), 1516 env_export(const unsigned char *), 1517 env_unexport(const unsigned char *), 1518 env_send(unsigned char *), 1519 #if defined(OLD_ENVIRON) && defined(ENV_HACK) 1520 env_varval(unsigned char *), 1521 #endif 1522 env_list(void); 1523 static void 1524 env_help(void); 1525 1526 struct envlist EnvList[] = { 1527 { "define", "Define an environment variable", 1528 (void (*)(unsigned char *, unsigned char *))env_define, 2 }, 1529 { "undefine", "Undefine an environment variable", 1530 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 }, 1531 { "export", "Mark an environment variable for automatic export", 1532 (void (*)(unsigned char *, unsigned char *))env_export, 1 }, 1533 { "unexport", "Don't mark an environment variable for automatic export", 1534 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 }, 1535 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 }, 1536 { "list", "List the current environment variables", 1537 (void (*)(unsigned char *, unsigned char *))env_list, 0 }, 1538 #if defined(OLD_ENVIRON) && defined(ENV_HACK) 1539 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)", 1540 (void (*)(unsigned char *, unsigned char *))env_varval, 1 }, 1541 #endif 1542 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 }, 1543 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 }, 1544 { NULL, NULL, NULL, 0 }, 1545 }; 1546 1547 static void 1548 env_help(void) 1549 { 1550 struct envlist *c; 1551 1552 for (c = EnvList; c->name; c++) { 1553 if (c->help) { 1554 if (*c->help) 1555 printf("%-15s %s\n", c->name, c->help); 1556 else 1557 printf("\n"); 1558 } 1559 } 1560 } 1561 1562 static struct envlist * 1563 getenvcmd(char *name) 1564 { 1565 return (struct envlist *) 1566 genget(name, (char **) EnvList, sizeof(struct envlist)); 1567 } 1568 1569 static int 1570 env_cmd(int argc, char *argv[]) 1571 { 1572 struct envlist *c; 1573 1574 if (argc < 2) { 1575 fprintf(stderr, 1576 "Need an argument to 'environ' command. 'environ ?' for help.\n"); 1577 return 0; 1578 } 1579 c = getenvcmd(argv[1]); 1580 if (c == NULL) { 1581 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n", 1582 argv[1]); 1583 return 0; 1584 } 1585 if (Ambiguous((void *)c)) { 1586 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n", 1587 argv[1]); 1588 return 0; 1589 } 1590 if (c->narg + 2 != argc) { 1591 fprintf(stderr, 1592 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n", 1593 c->narg < argc + 2 ? "only " : "", 1594 c->narg, c->narg == 1 ? "" : "s", c->name); 1595 return 0; 1596 } 1597 (*c->handler)(argv[2], argv[3]); 1598 return 1; 1599 } 1600 1601 struct env_lst { 1602 struct env_lst *next; /* pointer to next structure */ 1603 struct env_lst *prev; /* pointer to previous structure */ 1604 unsigned char *var; /* pointer to variable name */ 1605 unsigned char *value; /* pointer to variable value */ 1606 int export; /* 1 -> export with default list of variables */ 1607 int welldefined; /* A well defined variable */ 1608 }; 1609 1610 struct env_lst envlisthead; 1611 1612 static struct env_lst * 1613 env_find(const unsigned char *var) 1614 { 1615 struct env_lst *ep; 1616 1617 for (ep = envlisthead.next; ep; ep = ep->next) { 1618 if (strcmp(ep->var, var) == 0) 1619 return(ep); 1620 } 1621 return(NULL); 1622 } 1623 1624 void 1625 env_init(void) 1626 { 1627 extern char **environ; 1628 char **epp, *cp; 1629 struct env_lst *ep; 1630 1631 for (epp = environ; *epp; epp++) { 1632 if ((cp = strchr(*epp, '='))) { 1633 *cp = '\0'; 1634 ep = env_define((unsigned char *)*epp, 1635 (unsigned char *)cp+1); 1636 ep->export = 0; 1637 *cp = '='; 1638 } 1639 } 1640 /* 1641 * Special case for DISPLAY variable. If it is ":0.0" or 1642 * "unix:0.0", we have to get rid of "unix" and insert our 1643 * hostname. 1644 */ 1645 if ((ep = env_find("DISPLAY")) 1646 && ((*ep->value == ':') 1647 || (strncmp((char *)ep->value, "unix:", 5) == 0))) { 1648 char hbuf[256+1]; 1649 char *cp2 = strchr((char *)ep->value, ':'); 1650 1651 gethostname(hbuf, 256); 1652 hbuf[256] = '\0'; 1653 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1); 1654 sprintf((char *)cp, "%s%s", hbuf, cp2); 1655 free(ep->value); 1656 ep->value = (unsigned char *)cp; 1657 } 1658 /* 1659 * If USER is not defined, but LOGNAME is, then add 1660 * USER with the value from LOGNAME. By default, we 1661 * don't export the USER variable. 1662 */ 1663 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) { 1664 env_define("USER", ep->value); 1665 env_unexport("USER"); 1666 } 1667 env_export("DISPLAY"); 1668 env_export("PRINTER"); 1669 } 1670 1671 struct env_lst * 1672 env_define(const unsigned char *var, unsigned char *value) 1673 { 1674 struct env_lst *ep; 1675 1676 if ((ep = env_find(var))) { 1677 if (ep->var) 1678 free(ep->var); 1679 if (ep->value) 1680 free(ep->value); 1681 } else { 1682 ep = (struct env_lst *)malloc(sizeof(struct env_lst)); 1683 ep->next = envlisthead.next; 1684 envlisthead.next = ep; 1685 ep->prev = &envlisthead; 1686 if (ep->next) 1687 ep->next->prev = ep; 1688 } 1689 ep->welldefined = opt_welldefined(var); 1690 ep->export = 1; 1691 ep->var = strdup(var); 1692 ep->value = strdup(value); 1693 return(ep); 1694 } 1695 1696 void 1697 env_undefine(unsigned char *var) 1698 { 1699 struct env_lst *ep; 1700 1701 if ((ep = env_find(var))) { 1702 ep->prev->next = ep->next; 1703 if (ep->next) 1704 ep->next->prev = ep->prev; 1705 if (ep->var) 1706 free(ep->var); 1707 if (ep->value) 1708 free(ep->value); 1709 free(ep); 1710 } 1711 } 1712 1713 void 1714 env_export(const unsigned char *var) 1715 { 1716 struct env_lst *ep; 1717 1718 if ((ep = env_find(var))) 1719 ep->export = 1; 1720 } 1721 1722 void 1723 env_unexport(const unsigned char *var) 1724 { 1725 struct env_lst *ep; 1726 1727 if ((ep = env_find(var))) 1728 ep->export = 0; 1729 } 1730 1731 void 1732 env_send(unsigned char *var) 1733 { 1734 struct env_lst *ep; 1735 1736 if (my_state_is_wont(TELOPT_NEW_ENVIRON) 1737 #ifdef OLD_ENVIRON 1738 && my_state_is_wont(TELOPT_OLD_ENVIRON) 1739 #endif 1740 ) { 1741 fprintf(stderr, 1742 "Cannot send '%s': Telnet ENVIRON option not enabled\n", 1743 var); 1744 return; 1745 } 1746 ep = env_find(var); 1747 if (ep == NULL) { 1748 fprintf(stderr, "Cannot send '%s': variable not defined\n", 1749 var); 1750 return; 1751 } 1752 env_opt_start_info(); 1753 env_opt_add(ep->var); 1754 env_opt_end(0); 1755 } 1756 1757 void 1758 env_list(void) 1759 { 1760 struct env_lst *ep; 1761 1762 for (ep = envlisthead.next; ep; ep = ep->next) { 1763 printf("%c %-20s %s\n", ep->export ? '*' : ' ', 1764 ep->var, ep->value); 1765 } 1766 } 1767 1768 unsigned char * 1769 env_default(int init, int welldefined) 1770 { 1771 static struct env_lst *nep = NULL; 1772 1773 if (init) { 1774 nep = &envlisthead; 1775 return(NULL); 1776 } 1777 if (nep) { 1778 while ((nep = nep->next)) { 1779 if (nep->export && (nep->welldefined == welldefined)) 1780 return(nep->var); 1781 } 1782 } 1783 return(NULL); 1784 } 1785 1786 unsigned char * 1787 env_getvalue(const unsigned char *var) 1788 { 1789 struct env_lst *ep; 1790 1791 if ((ep = env_find(var))) 1792 return(ep->value); 1793 return(NULL); 1794 } 1795 1796 #if defined(OLD_ENVIRON) && defined(ENV_HACK) 1797 void 1798 env_varval(unsigned char *what) 1799 { 1800 extern int old_env_var, old_env_value, env_auto; 1801 int len = strlen((char *)what); 1802 1803 if (len == 0) 1804 goto unknown; 1805 1806 if (strncasecmp((char *)what, "status", len) == 0) { 1807 if (env_auto) 1808 printf("%s%s", "VAR and VALUE are/will be ", 1809 "determined automatically\n"); 1810 if (old_env_var == OLD_ENV_VAR) 1811 printf("VAR and VALUE set to correct definitions\n"); 1812 else 1813 printf("VAR and VALUE definitions are reversed\n"); 1814 } else if (strncasecmp((char *)what, "auto", len) == 0) { 1815 env_auto = 1; 1816 old_env_var = OLD_ENV_VALUE; 1817 old_env_value = OLD_ENV_VAR; 1818 } else if (strncasecmp((char *)what, "right", len) == 0) { 1819 env_auto = 0; 1820 old_env_var = OLD_ENV_VAR; 1821 old_env_value = OLD_ENV_VALUE; 1822 } else if (strncasecmp((char *)what, "wrong", len) == 0) { 1823 env_auto = 0; 1824 old_env_var = OLD_ENV_VALUE; 1825 old_env_value = OLD_ENV_VAR; 1826 } else { 1827 unknown: 1828 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n"); 1829 } 1830 } 1831 #endif 1832 1833 #ifdef AUTHENTICATION 1834 /* 1835 * The AUTHENTICATE command. 1836 */ 1837 1838 struct authlist { 1839 const char *name; 1840 const char *help; 1841 int (*handler)(char *); 1842 int narg; 1843 }; 1844 1845 extern int 1846 auth_enable(char *), 1847 auth_disable(char *), 1848 auth_status(void); 1849 static int 1850 auth_help(void); 1851 1852 struct authlist AuthList[] = { 1853 { "status", "Display current status of authentication information", 1854 (int (*)(char *))auth_status, 0 }, 1855 { "disable", "Disable an authentication type ('auth disable ?' for more)", 1856 auth_disable, 1 }, 1857 { "enable", "Enable an authentication type ('auth enable ?' for more)", 1858 auth_enable, 1 }, 1859 { "help", NULL, (int (*)(char *))auth_help, 0 }, 1860 { "?", "Print help information", (int (*)(char *))auth_help, 0 }, 1861 { NULL, NULL, NULL, 0 }, 1862 }; 1863 1864 static int 1865 auth_help(void) 1866 { 1867 struct authlist *c; 1868 1869 for (c = AuthList; c->name; c++) { 1870 if (c->help) { 1871 if (*c->help) 1872 printf("%-15s %s\n", c->name, c->help); 1873 else 1874 printf("\n"); 1875 } 1876 } 1877 return 0; 1878 } 1879 1880 int 1881 auth_cmd(int argc, char *argv[]) 1882 { 1883 struct authlist *c; 1884 1885 if (argc < 2) { 1886 fprintf(stderr, 1887 "Need an argument to 'auth' command. 'auth ?' for help.\n"); 1888 return 0; 1889 } 1890 1891 c = (struct authlist *) 1892 genget(argv[1], (char **) AuthList, sizeof(struct authlist)); 1893 if (c == NULL) { 1894 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n", 1895 argv[1]); 1896 return 0; 1897 } 1898 if (Ambiguous((void *)c)) { 1899 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n", 1900 argv[1]); 1901 return 0; 1902 } 1903 if (c->narg + 2 != argc) { 1904 fprintf(stderr, 1905 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n", 1906 c->narg < argc + 2 ? "only " : "", 1907 c->narg, c->narg == 1 ? "" : "s", c->name); 1908 return 0; 1909 } 1910 return((*c->handler)(argv[2])); 1911 } 1912 #endif 1913 1914 #ifdef ENCRYPTION 1915 /* 1916 * The ENCRYPT command. 1917 */ 1918 1919 struct encryptlist { 1920 const char *name; 1921 const char *help; 1922 int (*handler)(char *, char *); 1923 int needconnect; 1924 int minarg; 1925 int maxarg; 1926 }; 1927 1928 extern int 1929 EncryptEnable(char *, char *), 1930 EncryptDisable(char *, char *), 1931 EncryptType(char *, char *), 1932 EncryptStart(char *), 1933 EncryptStartInput(void), 1934 EncryptStartOutput(void), 1935 EncryptStop(char *), 1936 EncryptStopInput(void), 1937 EncryptStopOutput(void), 1938 EncryptStatus(void); 1939 static int 1940 EncryptHelp(void); 1941 1942 struct encryptlist EncryptList[] = { 1943 { "enable", "Enable encryption. ('encrypt enable ?' for more)", 1944 EncryptEnable, 1, 1, 2 }, 1945 { "disable", "Disable encryption. ('encrypt enable ?' for more)", 1946 EncryptDisable, 0, 1, 2 }, 1947 { "type", "Set encryption type. ('encrypt type ?' for more)", 1948 EncryptType, 0, 1, 1 }, 1949 { "start", "Start encryption. ('encrypt start ?' for more)", 1950 (int (*)(char *, char *))EncryptStart, 1, 0, 1 }, 1951 { "stop", "Stop encryption. ('encrypt stop ?' for more)", 1952 (int (*)(char *, char *))EncryptStop, 1, 0, 1 }, 1953 { "input", "Start encrypting the input stream", 1954 (int (*)(char *, char *))EncryptStartInput, 1, 0, 0 }, 1955 { "-input", "Stop encrypting the input stream", 1956 (int (*)(char *, char *))EncryptStopInput, 1, 0, 0 }, 1957 { "output", "Start encrypting the output stream", 1958 (int (*)(char *, char *))EncryptStartOutput, 1, 0, 0 }, 1959 { "-output", "Stop encrypting the output stream", 1960 (int (*)(char *, char *))EncryptStopOutput, 1, 0, 0 }, 1961 1962 { "status", "Display current status of authentication information", 1963 (int (*)(char *, char *))EncryptStatus, 0, 0, 0 }, 1964 { "help", NULL, (int (*)(char *, char *))EncryptHelp, 0, 0, 0 }, 1965 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp, 0, 0, 0 }, 1966 { NULL, NULL, NULL, 0, 0, 0 }, 1967 }; 1968 1969 static int 1970 EncryptHelp(void) 1971 { 1972 struct encryptlist *c; 1973 1974 for (c = EncryptList; c->name; c++) { 1975 if (c->help) { 1976 if (*c->help) 1977 printf("%-15s %s\n", c->name, c->help); 1978 else 1979 printf("\n"); 1980 } 1981 } 1982 return 0; 1983 } 1984 1985 static int 1986 encrypt_cmd(int argc, char *argv[]) 1987 { 1988 struct encryptlist *c; 1989 1990 if (argc < 2) { 1991 fprintf(stderr, 1992 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n"); 1993 return 0; 1994 } 1995 1996 c = (struct encryptlist *) 1997 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist)); 1998 if (c == NULL) { 1999 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n", 2000 argv[1]); 2001 return 0; 2002 } 2003 if (Ambiguous((void *)c)) { 2004 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n", 2005 argv[1]); 2006 return 0; 2007 } 2008 argc -= 2; 2009 if (argc < c->minarg || argc > c->maxarg) { 2010 if (c->minarg == c->maxarg) { 2011 fprintf(stderr, "Need %s%d argument%s ", 2012 c->minarg < argc ? "only " : "", c->minarg, 2013 c->minarg == 1 ? "" : "s"); 2014 } else { 2015 fprintf(stderr, "Need %s%d-%d arguments ", 2016 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg); 2017 } 2018 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n", 2019 c->name); 2020 return 0; 2021 } 2022 if (c->needconnect && !connected) { 2023 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) { 2024 printf("?Need to be connected first.\n"); 2025 return 0; 2026 } 2027 } 2028 return ((*c->handler)(argc > 0 ? argv[2] : 0, 2029 argc > 1 ? argv[3] : 0)); 2030 } 2031 #endif /* ENCRYPTION */ 2032 2033 /* 2034 * Print status about the connection. 2035 */ 2036 /*ARGSUSED*/ 2037 static int 2038 status(int argc, char *argv[]) 2039 { 2040 if (connected) { 2041 printf("Connected to %s.\n", hostname); 2042 if ((argc < 2) || strcmp(argv[1], "notmuch")) { 2043 int mode = getconnmode(); 2044 2045 if (my_want_state_is_will(TELOPT_LINEMODE)) { 2046 printf("Operating with LINEMODE option\n"); 2047 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No"); 2048 printf("%s catching of signals\n", 2049 (mode&MODE_TRAPSIG) ? "Local" : "No"); 2050 slcstate(); 2051 #ifdef KLUDGELINEMODE 2052 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) { 2053 printf("Operating in obsolete linemode\n"); 2054 #endif 2055 } else { 2056 printf("Operating in single character mode\n"); 2057 if (localchars) 2058 printf("Catching signals locally\n"); 2059 } 2060 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote"); 2061 if (my_want_state_is_will(TELOPT_LFLOW)) 2062 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No"); 2063 #ifdef ENCRYPTION 2064 encrypt_display(); 2065 #endif /* ENCRYPTION */ 2066 } 2067 } else { 2068 printf("No connection.\n"); 2069 } 2070 printf("Escape character is '%s'.\n", control(escape)); 2071 (void) fflush(stdout); 2072 return 1; 2073 } 2074 2075 #ifdef SIGINFO 2076 /* 2077 * Function that gets called when SIGINFO is received. 2078 */ 2079 void 2080 ayt_status(void) 2081 { 2082 (void) call(status, "status", "notmuch", 0); 2083 } 2084 #endif 2085 2086 static const char * 2087 sockaddr_ntop(struct sockaddr *sa) 2088 { 2089 void *addr; 2090 static char addrbuf[INET6_ADDRSTRLEN]; 2091 2092 switch (sa->sa_family) { 2093 case AF_INET: 2094 addr = &((struct sockaddr_in *)sa)->sin_addr; 2095 break; 2096 case AF_UNIX: 2097 addr = &((struct sockaddr_un *)sa)->sun_path; 2098 break; 2099 #ifdef INET6 2100 case AF_INET6: 2101 addr = &((struct sockaddr_in6 *)sa)->sin6_addr; 2102 break; 2103 #endif 2104 default: 2105 return NULL; 2106 } 2107 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf)); 2108 return addrbuf; 2109 } 2110 2111 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 2112 static int 2113 setpolicy(int lnet, struct addrinfo *res, char *policy) 2114 { 2115 char *buf; 2116 int level; 2117 int optname; 2118 2119 if (policy == NULL) 2120 return 0; 2121 2122 buf = ipsec_set_policy(policy, strlen(policy)); 2123 if (buf == NULL) { 2124 printf("%s\n", ipsec_strerror()); 2125 return -1; 2126 } 2127 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 2128 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY; 2129 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){ 2130 perror("setsockopt"); 2131 return -1; 2132 } 2133 2134 free(buf); 2135 return 0; 2136 } 2137 #endif 2138 2139 #ifdef INET6 2140 /* 2141 * When an Address Family related error happend, check if retry with 2142 * another AF is possible or not. 2143 * Return 1, if retry with another af is OK. Else, return 0. 2144 */ 2145 static int 2146 switch_af(struct addrinfo **aip) 2147 { 2148 int nextaf; 2149 struct addrinfo *ai; 2150 2151 ai = *aip; 2152 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET; 2153 do 2154 ai=ai->ai_next; 2155 while (ai != NULL && ai->ai_family != nextaf); 2156 *aip = ai; 2157 if (*aip != NULL) { 2158 return 1; 2159 } 2160 return 0; 2161 } 2162 #endif 2163 2164 int 2165 tn(int argc, char *argv[]) 2166 { 2167 char *srp = NULL; 2168 int proto, opt; 2169 int srlen; 2170 int srcroute = 0, result; 2171 char *cmd, *hostp = NULL, *portp = NULL, *user = NULL; 2172 char *src_addr = NULL; 2173 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL; 2174 int error = 0, af_error = 0; 2175 2176 if (connected) { 2177 printf("?Already connected to %s\n", hostname); 2178 setuid(getuid()); 2179 return 0; 2180 } 2181 if (argc < 2) { 2182 (void) strcpy(line, "open "); 2183 printf("(to) "); 2184 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin); 2185 makeargv(); 2186 argc = margc; 2187 argv = margv; 2188 } 2189 cmd = *argv; 2190 --argc; ++argv; 2191 while (argc) { 2192 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?")) 2193 goto usage; 2194 if (strcmp(*argv, "-l") == 0) { 2195 --argc; ++argv; 2196 if (argc == 0) 2197 goto usage; 2198 user = *argv++; 2199 --argc; 2200 continue; 2201 } 2202 if (strcmp(*argv, "-a") == 0) { 2203 --argc; ++argv; 2204 autologin = 1; 2205 continue; 2206 } 2207 if (strcmp(*argv, "-s") == 0) { 2208 --argc; ++argv; 2209 if (argc == 0) 2210 goto usage; 2211 src_addr = *argv++; 2212 --argc; 2213 continue; 2214 } 2215 if (hostp == NULL) { 2216 hostp = *argv++; 2217 --argc; 2218 continue; 2219 } 2220 if (portp == NULL) { 2221 portp = *argv++; 2222 --argc; 2223 continue; 2224 } 2225 usage: 2226 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd); 2227 setuid(getuid()); 2228 return 0; 2229 } 2230 if (hostp == NULL) 2231 goto usage; 2232 2233 if (src_addr != NULL) { 2234 memset(&hints, 0, sizeof(hints)); 2235 hints.ai_flags = AI_NUMERICHOST; 2236 hints.ai_family = family; 2237 hints.ai_socktype = SOCK_STREAM; 2238 error = getaddrinfo(src_addr, 0, &hints, &src_res); 2239 if (error == EAI_NODATA) { 2240 hints.ai_flags = 0; 2241 error = getaddrinfo(src_addr, 0, &hints, &src_res); 2242 } 2243 if (error != 0) { 2244 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error)); 2245 if (error == EAI_SYSTEM) 2246 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno)); 2247 setuid(getuid()); 2248 return 0; 2249 } 2250 src_res0 = src_res; 2251 } 2252 if (hostp[0] == '/') { 2253 struct sockaddr_un su; 2254 2255 if (strlen(hostp) >= sizeof(su.sun_path)) { 2256 fprintf(stderr, "hostname too long for unix domain socket: %s", 2257 hostp); 2258 goto fail; 2259 } 2260 memset(&su, 0, sizeof su); 2261 su.sun_family = AF_UNIX; 2262 strncpy(su.sun_path, hostp, sizeof su.sun_path); 2263 printf("Trying %s...\n", hostp); 2264 net = socket(PF_UNIX, SOCK_STREAM, 0); 2265 if ( net < 0) { 2266 perror("socket"); 2267 goto fail; 2268 } 2269 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) { 2270 perror(su.sun_path); 2271 (void) NetClose(net); 2272 goto fail; 2273 } 2274 goto af_unix; 2275 } else if (hostp[0] == '@' || hostp[0] == '!') { 2276 if ( 2277 #ifdef INET6 2278 family == AF_INET6 || 2279 #endif 2280 (hostname = strrchr(hostp, ':')) == NULL) 2281 hostname = strrchr(hostp, '@'); 2282 if (hostname == NULL) { 2283 hostname = hostp; 2284 } else { 2285 hostname++; 2286 srcroute = 1; 2287 } 2288 } else 2289 hostname = hostp; 2290 if (!portp) { 2291 telnetport = 1; 2292 portp = strdup("telnet"); 2293 } else if (*portp == '-') { 2294 portp++; 2295 telnetport = 1; 2296 } else 2297 telnetport = 0; 2298 2299 memset(&hints, 0, sizeof(hints)); 2300 hints.ai_flags = AI_NUMERICHOST; 2301 hints.ai_family = family; 2302 hints.ai_socktype = SOCK_STREAM; 2303 error = getaddrinfo(hostname, portp, &hints, &res); 2304 if (error) { 2305 hints.ai_flags = AI_CANONNAME; 2306 error = getaddrinfo(hostname, portp, &hints, &res); 2307 } 2308 if (error != 0) { 2309 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error)); 2310 if (error == EAI_SYSTEM) 2311 fprintf(stderr, "%s: %s\n", hostname, strerror(errno)); 2312 setuid(getuid()); 2313 goto fail; 2314 } 2315 if (hints.ai_flags == AI_NUMERICHOST) { 2316 /* hostname has numeric */ 2317 int gni_err = 1; 2318 2319 if (doaddrlookup) 2320 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len, 2321 _hostname, sizeof(_hostname) - 1, NULL, 0, 2322 NI_NAMEREQD); 2323 if (gni_err != 0) 2324 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1); 2325 _hostname[sizeof(_hostname)-1] = '\0'; 2326 hostname = _hostname; 2327 } else { 2328 /* hostname has FQDN */ 2329 if (srcroute != 0) 2330 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1); 2331 else if (res->ai_canonname != NULL) 2332 strcpy(_hostname, res->ai_canonname); 2333 else 2334 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1); 2335 _hostname[sizeof(_hostname)-1] = '\0'; 2336 hostname = _hostname; 2337 } 2338 res0 = res; 2339 #ifdef INET6 2340 af_again: 2341 #endif 2342 if (srcroute != 0) { 2343 static char hostbuf[BUFSIZ]; 2344 2345 if (af_error == 0) { /* save intermediate hostnames for retry */ 2346 strncpy(hostbuf, hostp, BUFSIZ - 1); 2347 hostbuf[BUFSIZ - 1] = '\0'; 2348 } else 2349 hostp = hostbuf; 2350 srp = NULL; 2351 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt); 2352 if (result == 0) { 2353 #ifdef INET6 2354 if (family == AF_UNSPEC && af_error == 0 && 2355 switch_af(&res) == 1) { 2356 af_error = 1; 2357 goto af_again; 2358 } 2359 #endif 2360 setuid(getuid()); 2361 goto fail; 2362 } else if (result == -1) { 2363 printf("Bad source route option: %s\n", hostp); 2364 setuid(getuid()); 2365 goto fail; 2366 } 2367 } 2368 do { 2369 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr)); 2370 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 2371 setuid(getuid()); 2372 if (net < 0) { 2373 #ifdef INET6 2374 if (family == AF_UNSPEC && af_error == 0 && 2375 switch_af(&res) == 1) { 2376 af_error = 1; 2377 goto af_again; 2378 } 2379 #endif 2380 perror("telnet: socket"); 2381 goto fail; 2382 } 2383 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0) 2384 perror("setsockopt (source route)"); 2385 #if defined(IPPROTO_IP) && defined(IP_TOS) 2386 if (res->ai_family == PF_INET) { 2387 # if defined(HAS_GETTOS) 2388 struct tosent *tp; 2389 if (tos < 0 && (tp = gettosbyname("telnet", "tcp"))) 2390 tos = tp->t_tos; 2391 # endif 2392 if (tos < 0) 2393 tos = IPTOS_LOWDELAY; 2394 if (tos 2395 && (setsockopt(net, IPPROTO_IP, IP_TOS, 2396 (char *)&tos, sizeof(int)) < 0) 2397 && (errno != ENOPROTOOPT)) 2398 perror("telnet: setsockopt (IP_TOS) (ignored)"); 2399 } 2400 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ 2401 2402 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) { 2403 perror("setsockopt (SO_DEBUG)"); 2404 } 2405 2406 if (src_addr != NULL) { 2407 for (src_res = src_res0; src_res != NULL; src_res = src_res->ai_next) 2408 if (src_res->ai_family == res->ai_family) 2409 break; 2410 if (src_res == NULL) 2411 src_res = src_res0; 2412 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) { 2413 #ifdef INET6 2414 if (family == AF_UNSPEC && af_error == 0 && 2415 switch_af(&res) == 1) { 2416 af_error = 1; 2417 (void) NetClose(net); 2418 goto af_again; 2419 } 2420 #endif 2421 perror("bind"); 2422 (void) NetClose(net); 2423 goto fail; 2424 } 2425 } 2426 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 2427 if (setpolicy(net, res, ipsec_policy_in) < 0) { 2428 (void) NetClose(net); 2429 goto fail; 2430 } 2431 if (setpolicy(net, res, ipsec_policy_out) < 0) { 2432 (void) NetClose(net); 2433 goto fail; 2434 } 2435 #endif 2436 2437 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) { 2438 struct addrinfo *next; 2439 2440 next = res->ai_next; 2441 /* If already an af failed, only try same af. */ 2442 if (af_error != 0) 2443 while (next != NULL && next->ai_family != res->ai_family) 2444 next = next->ai_next; 2445 warn("connect to address %s", sockaddr_ntop(res->ai_addr)); 2446 if (next != NULL) { 2447 res = next; 2448 (void) NetClose(net); 2449 continue; 2450 } 2451 warnx("Unable to connect to remote host"); 2452 (void) NetClose(net); 2453 goto fail; 2454 } 2455 connected++; 2456 #ifdef AUTHENTICATION 2457 #ifdef ENCRYPTION 2458 auth_encrypt_connect(connected); 2459 #endif 2460 #endif 2461 } while (connected == 0); 2462 freeaddrinfo(res0); 2463 if (src_res0 != NULL) 2464 freeaddrinfo(src_res0); 2465 cmdrc(hostp, hostname); 2466 af_unix: 2467 if (autologin && user == NULL) { 2468 struct passwd *pw; 2469 2470 user = getenv("USER"); 2471 if (user == NULL || 2472 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) { 2473 if ((pw = getpwuid(getuid()))) 2474 user = pw->pw_name; 2475 else 2476 user = NULL; 2477 } 2478 } 2479 if (user) { 2480 env_define("USER", user); 2481 env_export("USER"); 2482 } 2483 (void) call(status, "status", "notmuch", 0); 2484 if (setjmp(peerdied) == 0) 2485 telnet(user); 2486 (void) NetClose(net); 2487 ExitString("Connection closed by foreign host.\n",1); 2488 /*NOTREACHED*/ 2489 fail: 2490 if (res0 != NULL) 2491 freeaddrinfo(res0); 2492 if (src_res0 != NULL) 2493 freeaddrinfo(src_res0); 2494 return 0; 2495 } 2496 2497 #define HELPINDENT (sizeof ("connect")) 2498 2499 static char 2500 openhelp[] = "connect to a site", 2501 closehelp[] = "close current connection", 2502 logouthelp[] = "forcibly logout remote user and close the connection", 2503 quithelp[] = "exit telnet", 2504 statushelp[] = "print status information", 2505 helphelp[] = "print help information", 2506 sendhelp[] = "transmit special characters ('send ?' for more)", 2507 sethelp[] = "set operating parameters ('set ?' for more)", 2508 unsethelp[] = "unset operating parameters ('unset ?' for more)", 2509 togglestring[] ="toggle operating parameters ('toggle ?' for more)", 2510 slchelp[] = "change state of special characters ('slc ?' for more)", 2511 displayhelp[] = "display operating parameters", 2512 #ifdef AUTHENTICATION 2513 authhelp[] = "turn on (off) authentication ('auth ?' for more)", 2514 #endif 2515 #ifdef ENCRYPTION 2516 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)", 2517 #endif /* ENCRYPTION */ 2518 zhelp[] = "suspend telnet", 2519 #ifdef OPIE 2520 opiehelp[] = "compute response to OPIE challenge", 2521 #endif 2522 shellhelp[] = "invoke a subshell", 2523 envhelp[] = "change environment variables ('environ ?' for more)", 2524 modestring[] = "try to enter line or character mode ('mode ?' for more)"; 2525 2526 static Command cmdtab[] = { 2527 { "close", closehelp, bye, 1 }, 2528 { "logout", logouthelp, (int (*)(int, char **))logout, 1 }, 2529 { "display", displayhelp, display, 0 }, 2530 { "mode", modestring, modecmd, 0 }, 2531 { "telnet", openhelp, tn, 0 }, 2532 { "open", openhelp, tn, 0 }, 2533 { "quit", quithelp, (int (*)(int, char **))quit, 0 }, 2534 { "send", sendhelp, sendcmd, 0 }, 2535 { "set", sethelp, setcmd, 0 }, 2536 { "unset", unsethelp, unsetcmd, 0 }, 2537 { "status", statushelp, status, 0 }, 2538 { "toggle", togglestring, toggle, 0 }, 2539 { "slc", slchelp, slccmd, 0 }, 2540 #ifdef AUTHENTICATION 2541 { "auth", authhelp, auth_cmd, 0 }, 2542 #endif 2543 #ifdef ENCRYPTION 2544 { "encrypt", encrypthelp, encrypt_cmd, 0 }, 2545 #endif /* ENCRYPTION */ 2546 { "z", zhelp, (int (*)(int, char **))suspend, 0 }, 2547 { "!", shellhelp, shell, 1 }, 2548 { "environ", envhelp, env_cmd, 0 }, 2549 { "?", helphelp, help, 0 }, 2550 #ifdef OPIE 2551 { "opie", opiehelp, opie_calc, 0 }, 2552 #endif 2553 { NULL, NULL, NULL, 0 } 2554 }; 2555 2556 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead"; 2557 static char escapehelp[] = "deprecated command -- use 'set escape' instead"; 2558 2559 static Command cmdtab2[] = { 2560 { "help", 0, help, 0 }, 2561 { "escape", escapehelp, setescape, 0 }, 2562 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 }, 2563 { NULL, NULL, NULL, 0 } 2564 }; 2565 2566 2567 /* 2568 * Call routine with argc, argv set from args (terminated by 0). 2569 */ 2570 2571 static int 2572 call(intrtn_t routine, ...) 2573 { 2574 va_list ap; 2575 char *args[100]; 2576 int argno = 0; 2577 2578 va_start(ap, routine); 2579 while ((args[argno++] = va_arg(ap, char *)) != NULL); 2580 va_end(ap); 2581 return (*routine)(argno-1, args); 2582 } 2583 2584 2585 static Command * 2586 getcmd(char *name) 2587 { 2588 Command *cm; 2589 2590 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))) 2591 return cm; 2592 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command)); 2593 } 2594 2595 void 2596 command(int top, const char *tbuf, int cnt) 2597 { 2598 Command *c; 2599 2600 setcommandmode(); 2601 if (!top) { 2602 putchar('\n'); 2603 } else { 2604 (void) signal(SIGINT, SIG_DFL); 2605 (void) signal(SIGQUIT, SIG_DFL); 2606 } 2607 for (;;) { 2608 if (rlogin == _POSIX_VDISABLE) 2609 printf("%s> ", prompt); 2610 if (tbuf) { 2611 char *cp; 2612 cp = line; 2613 while (cnt > 0 && (*cp++ = *tbuf++) != '\n') 2614 cnt--; 2615 tbuf = NULL; 2616 if (cp == line || *--cp != '\n' || cp == line) 2617 goto getline; 2618 *cp = '\0'; 2619 if (rlogin == _POSIX_VDISABLE) 2620 printf("%s\n", line); 2621 } else { 2622 getline: 2623 if (rlogin != _POSIX_VDISABLE) 2624 printf("%s> ", prompt); 2625 if (fgets(line, sizeof(line), stdin) == NULL) { 2626 if (feof(stdin) || ferror(stdin)) { 2627 (void) quit(); 2628 /*NOTREACHED*/ 2629 } 2630 break; 2631 } 2632 } 2633 if (line[0] == 0) 2634 break; 2635 makeargv(); 2636 if (margv[0] == NULL) { 2637 break; 2638 } 2639 c = getcmd(margv[0]); 2640 if (Ambiguous((void *)c)) { 2641 printf("?Ambiguous command\n"); 2642 continue; 2643 } 2644 if (c == NULL) { 2645 printf("?Invalid command\n"); 2646 continue; 2647 } 2648 if (c->needconnect && !connected) { 2649 printf("?Need to be connected first.\n"); 2650 continue; 2651 } 2652 if ((*c->handler)(margc, margv)) { 2653 break; 2654 } 2655 } 2656 if (!top) { 2657 if (!connected) { 2658 longjmp(toplevel, 1); 2659 /*NOTREACHED*/ 2660 } 2661 setconnmode(0); 2662 } 2663 } 2664 2665 /* 2666 * Help command. 2667 */ 2668 static int 2669 help(int argc, char *argv[]) 2670 { 2671 Command *c; 2672 2673 if (argc == 1) { 2674 printf("Commands may be abbreviated. Commands are:\n\n"); 2675 for (c = cmdtab; c->name; c++) 2676 if (c->help) { 2677 printf("%-*s\t%s\n", (int)HELPINDENT, c->name, 2678 c->help); 2679 } 2680 return 0; 2681 } 2682 else while (--argc > 0) { 2683 char *arg; 2684 arg = *++argv; 2685 c = getcmd(arg); 2686 if (Ambiguous((void *)c)) 2687 printf("?Ambiguous help command %s\n", arg); 2688 else if (c == NULL) 2689 printf("?Invalid help command %s\n", arg); 2690 else 2691 printf("%s\n", c->help); 2692 } 2693 return 0; 2694 } 2695 2696 static char *rcname = NULL; 2697 static char rcbuf[128]; 2698 2699 void 2700 cmdrc(char *m1, char *m2) 2701 { 2702 Command *c; 2703 FILE *rcfile; 2704 int gotmachine = 0; 2705 int l1 = strlen(m1); 2706 int l2 = strlen(m2); 2707 char m1save[MAXHOSTNAMELEN]; 2708 2709 if (skiprc) 2710 return; 2711 2712 strlcpy(m1save, m1, sizeof(m1save)); 2713 m1 = m1save; 2714 2715 if (rcname == NULL) { 2716 rcname = getenv("HOME"); 2717 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf)) 2718 strcpy(rcbuf, rcname); 2719 else 2720 rcbuf[0] = '\0'; 2721 strcat(rcbuf, "/.telnetrc"); 2722 rcname = rcbuf; 2723 } 2724 2725 if ((rcfile = fopen(rcname, "r")) == NULL) { 2726 return; 2727 } 2728 2729 for (;;) { 2730 if (fgets(line, sizeof(line), rcfile) == NULL) 2731 break; 2732 if (line[0] == 0) 2733 break; 2734 if (line[0] == '#') 2735 continue; 2736 if (gotmachine) { 2737 if (!isspace(line[0])) 2738 gotmachine = 0; 2739 } 2740 if (gotmachine == 0) { 2741 if (isspace(line[0])) 2742 continue; 2743 if (strncasecmp(line, m1, l1) == 0) 2744 strncpy(line, &line[l1], sizeof(line) - l1); 2745 else if (strncasecmp(line, m2, l2) == 0) 2746 strncpy(line, &line[l2], sizeof(line) - l2); 2747 else if (strncasecmp(line, "DEFAULT", 7) == 0) 2748 strncpy(line, &line[7], sizeof(line) - 7); 2749 else 2750 continue; 2751 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n') 2752 continue; 2753 gotmachine = 1; 2754 } 2755 makeargv(); 2756 if (margv[0] == NULL) 2757 continue; 2758 c = getcmd(margv[0]); 2759 if (Ambiguous((void *)c)) { 2760 printf("?Ambiguous command: %s\n", margv[0]); 2761 continue; 2762 } 2763 if (c == NULL) { 2764 printf("?Invalid command: %s\n", margv[0]); 2765 continue; 2766 } 2767 /* 2768 * This should never happen... 2769 */ 2770 if (c->needconnect && !connected) { 2771 printf("?Need to be connected first for %s.\n", margv[0]); 2772 continue; 2773 } 2774 (*c->handler)(margc, margv); 2775 } 2776 fclose(rcfile); 2777 } 2778 2779 /* 2780 * Source route is handed in as 2781 * [!]@hop1@hop2...[@|:]dst 2782 * If the leading ! is present, it is a 2783 * strict source route, otherwise it is 2784 * assmed to be a loose source route. 2785 * 2786 * We fill in the source route option as 2787 * hop1,hop2,hop3...dest 2788 * and return a pointer to hop1, which will 2789 * be the address to connect() to. 2790 * 2791 * Arguments: 2792 * 2793 * res: ponter to addrinfo structure which contains sockaddr to 2794 * the host to connect to. 2795 * 2796 * arg: pointer to route list to decipher 2797 * 2798 * cpp: If *cpp is not equal to NULL, this is a 2799 * pointer to a pointer to a character array 2800 * that should be filled in with the option. 2801 * 2802 * lenp: pointer to an integer that contains the 2803 * length of *cpp if *cpp != NULL. 2804 * 2805 * protop: pointer to an integer that should be filled in with 2806 * appropriate protocol for setsockopt, as socket 2807 * protocol family. 2808 * 2809 * optp: pointer to an integer that should be filled in with 2810 * appropriate option for setsockopt, as socket protocol 2811 * family. 2812 * 2813 * Return values: 2814 * 2815 * If the return value is 1, then all operations are 2816 * successful. If the 2817 * return value is -1, there was a syntax error in the 2818 * option, either unknown characters, or too many hosts. 2819 * If the return value is 0, one of the hostnames in the 2820 * path is unknown, and *cpp is set to point to the bad 2821 * hostname. 2822 * 2823 * *cpp: If *cpp was equal to NULL, it will be filled 2824 * in with a pointer to our static area that has 2825 * the option filled in. This will be 32bit aligned. 2826 * 2827 * *lenp: This will be filled in with how long the option 2828 * pointed to by *cpp is. 2829 * 2830 * *protop: This will be filled in with appropriate protocol for 2831 * setsockopt, as socket protocol family. 2832 * 2833 * *optp: This will be filled in with appropriate option for 2834 * setsockopt, as socket protocol family. 2835 */ 2836 static int 2837 sourceroute(struct addrinfo *ai, char *arg, char **cpp, int *lenp, int *protop, int *optp) 2838 { 2839 static char buf[1024 + ALIGNBYTES]; /*XXX*/ 2840 char *cp, *cp2, *lsrp, *ep; 2841 struct sockaddr_in *_sin; 2842 #ifdef INET6 2843 struct sockaddr_in6 *sin6; 2844 struct cmsghdr *cmsg; 2845 struct ip6_rthdr *rth; 2846 #endif 2847 struct addrinfo hints, *res; 2848 int error; 2849 char c; 2850 2851 /* 2852 * Verify the arguments, and make sure we have 2853 * at least 7 bytes for the option. 2854 */ 2855 if (cpp == NULL || lenp == NULL) 2856 return -1; 2857 if (*cpp != NULL) { 2858 switch (res->ai_family) { 2859 case AF_INET: 2860 if (*lenp < 7) 2861 return -1; 2862 break; 2863 #ifdef INET6 2864 case AF_INET6: 2865 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) + 2866 sizeof(struct in6_addr))) 2867 return -1; 2868 break; 2869 #endif 2870 } 2871 } 2872 /* 2873 * Decide whether we have a buffer passed to us, 2874 * or if we need to use our own static buffer. 2875 */ 2876 if (*cpp) { 2877 lsrp = *cpp; 2878 ep = lsrp + *lenp; 2879 } else { 2880 *cpp = lsrp = (char *)ALIGN(buf); 2881 ep = lsrp + 1024; 2882 } 2883 2884 cp = arg; 2885 2886 #ifdef INET6 2887 if (ai->ai_family == AF_INET6) { 2888 /* 2889 * RFC3542 has obsoleted IPV6_PKTOPTIONS socket option. 2890 */ 2891 #ifdef COMPAT_RFC1883 /* XXX */ 2892 cmsg = NULL; 2893 if (*cp != '@') 2894 return -1; 2895 *protop = IPPROTO_IPV6; 2896 *optp = IPV6_PKTOPTIONS; 2897 #else 2898 return -1; 2899 #endif /* COMPAT_RFC1883 */ 2900 } else 2901 #endif 2902 { 2903 /* 2904 * Next, decide whether we have a loose source 2905 * route or a strict source route, and fill in 2906 * the begining of the option. 2907 */ 2908 if (*cp == '!') { 2909 cp++; 2910 *lsrp++ = IPOPT_SSRR; 2911 } else 2912 *lsrp++ = IPOPT_LSRR; 2913 2914 if (*cp != '@') 2915 return -1; 2916 2917 lsrp++; /* skip over length, we'll fill it in later */ 2918 *lsrp++ = 4; 2919 *protop = IPPROTO_IP; 2920 *optp = IP_OPTIONS; 2921 } 2922 2923 cp++; 2924 memset(&hints, 0, sizeof(hints)); 2925 hints.ai_family = ai->ai_family; 2926 hints.ai_socktype = SOCK_STREAM; 2927 for (c = 0;;) { 2928 if ( 2929 #ifdef INET6 2930 ai->ai_family != AF_INET6 && 2931 #endif 2932 c == ':') 2933 cp2 = NULL; 2934 else for (cp2 = cp; (c = *cp2); cp2++) { 2935 if (c == ',') { 2936 *cp2++ = '\0'; 2937 if (*cp2 == '@') 2938 cp2++; 2939 } else if (c == '@') { 2940 *cp2++ = '\0'; 2941 } else if ( 2942 #ifdef INET6 2943 ai->ai_family != AF_INET6 && 2944 #endif 2945 c == ':') { 2946 *cp2++ = '\0'; 2947 } else 2948 continue; 2949 break; 2950 } 2951 if (!c) 2952 cp2 = NULL; 2953 2954 hints.ai_flags = AI_NUMERICHOST; 2955 error = getaddrinfo(cp, NULL, &hints, &res); 2956 if (error == EAI_NODATA) { 2957 hints.ai_flags = 0; 2958 error = getaddrinfo(cp, NULL, &hints, &res); 2959 } 2960 if (error != 0) { 2961 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error)); 2962 if (error == EAI_SYSTEM) 2963 fprintf(stderr, "%s: %s\n", cp, 2964 strerror(errno)); 2965 *cpp = cp; 2966 return(0); 2967 } 2968 #ifdef INET6 2969 if (res->ai_family == AF_INET6) { 2970 return(0); 2971 } else 2972 #endif 2973 { 2974 _sin = (struct sockaddr_in *)res->ai_addr; 2975 memcpy(lsrp, (char *)&_sin->sin_addr, 4); 2976 lsrp += 4; 2977 } 2978 if (cp2) 2979 cp = cp2; 2980 else 2981 break; 2982 /* 2983 * Check to make sure there is space for next address 2984 */ 2985 #ifdef INET6 2986 #ifdef COMPAT_RFC1883 /* XXX */ 2987 if (res->ai_family == AF_INET6) { 2988 if (((char *)CMSG_DATA(cmsg) + 2989 sizeof(struct ip6_rthdr)) > ep) 2990 return -1; 2991 } else 2992 #endif /* COMPAT_RFC1883 */ 2993 #endif 2994 if (lsrp + 4 > ep) 2995 return -1; 2996 freeaddrinfo(res); 2997 } 2998 #ifdef INET6 2999 if (res->ai_family == AF_INET6) { 3000 #ifdef COMPAT_RFC1883 /* XXX */ 3001 *lenp = 0; 3002 #endif /* COMPAT_RFC1883 */ 3003 } else 3004 #endif 3005 { 3006 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) { 3007 *cpp = NULL; 3008 *lenp = 0; 3009 return -1; 3010 } 3011 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */ 3012 *lenp = lsrp - *cpp; 3013 } 3014 freeaddrinfo(res); 3015 return 1; 3016 } 3017