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