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