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