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