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