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