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