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