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