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