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