Annotation of src/usr.bin/telnet/commands.c, Revision 1.15
1.15 ! art 1: /* $OpenBSD: commands.c,v 1.14 1998/05/15 01:26:09 art 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) {
73: printf("%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.1 deraadt 1670: char hbuf[256+1];
1.3 niklas 1671: char *cp2 = strchr((char *)ep->value, ':');
1.1 deraadt 1672:
1673: gethostname(hbuf, 256);
1674: hbuf[256] = '\0';
1.10 art 1675:
1676: /* If this is not the full name, try to get it via DNS */
1677: if (strchr(hbuf, '.') == 0) {
1678: struct hostent *he = gethostbyname(hbuf);
1679: if (he != 0)
1680: strncpy(hbuf, he->h_name, 256);
1681: hbuf[256] = '\0';
1682: }
1683:
1684: asprintf (&cp, "%s%s", hbuf, cp2);
1.14 art 1685: if (cp == NULL)
1686: err(1, "asprintf");
1.10 art 1687:
1.1 deraadt 1688: free(ep->value);
1689: ep->value = (unsigned char *)cp;
1690: }
1691: /*
1692: * If USER is not defined, but LOGNAME is, then add
1693: * USER with the value from LOGNAME. By default, we
1694: * don't export the USER variable.
1695: */
1696: if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1697: env_define((unsigned char *)"USER", ep->value);
1698: env_unexport((unsigned char *)"USER");
1699: }
1700: env_export((unsigned char *)"DISPLAY");
1701: env_export((unsigned char *)"PRINTER");
1.10 art 1702: env_export((unsigned char *)"XAUTHORITY");
1.1 deraadt 1703: }
1704:
1705: struct env_lst *
1706: env_define(var, value)
1707: unsigned char *var, *value;
1708: {
1709: register struct env_lst *ep;
1710:
1.10 art 1711: if ((ep = env_find(var))) {
1.1 deraadt 1712: if (ep->var)
1713: free(ep->var);
1714: if (ep->value)
1715: free(ep->value);
1716: } else {
1.14 art 1717: if ((ep = malloc(sizeof(struct env_lst))) == NULL)
1718: err(1, "malloc");
1.1 deraadt 1719: ep->next = envlisthead.next;
1720: envlisthead.next = ep;
1721: ep->prev = &envlisthead;
1722: if (ep->next)
1723: ep->next->prev = ep;
1724: }
1.10 art 1725: ep->welldefined = opt_welldefined((char *)var);
1.1 deraadt 1726: ep->export = 1;
1.14 art 1727: if ((ep->var = strdup((char *)var)) == NULL)
1728: err(1, "strdup");
1729: if ((ep->value = strdup((char *)value)) == NULL)
1730: err(1, "strdup");
1.1 deraadt 1731: return(ep);
1732: }
1733:
1734: void
1735: env_undefine(var)
1736: unsigned char *var;
1737: {
1738: register struct env_lst *ep;
1739:
1.10 art 1740: if ((ep = env_find(var))) {
1.1 deraadt 1741: ep->prev->next = ep->next;
1742: if (ep->next)
1743: ep->next->prev = ep->prev;
1744: if (ep->var)
1745: free(ep->var);
1746: if (ep->value)
1747: free(ep->value);
1748: free(ep);
1749: }
1750: }
1751:
1752: void
1753: env_export(var)
1754: unsigned char *var;
1755: {
1756: register struct env_lst *ep;
1757:
1.10 art 1758: if ((ep = env_find(var)))
1.1 deraadt 1759: ep->export = 1;
1760: }
1761:
1762: void
1763: env_unexport(var)
1764: unsigned char *var;
1765: {
1766: register struct env_lst *ep;
1767:
1.15 ! art 1768: if ((ep = env_find(var)) != NULL)
1.1 deraadt 1769: ep->export = 0;
1770: }
1771:
1772: void
1773: env_send(var)
1774: unsigned char *var;
1775: {
1776: register struct env_lst *ep;
1777:
1.3 niklas 1778: if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1.1 deraadt 1779: #ifdef OLD_ENVIRON
1780: && my_state_is_wont(TELOPT_OLD_ENVIRON)
1781: #endif
1782: ) {
1783: fprintf(stderr,
1.10 art 1784: "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1.1 deraadt 1785: var);
1786: return;
1787: }
1788: ep = env_find(var);
1789: if (ep == 0) {
1.10 art 1790: fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1.1 deraadt 1791: var);
1792: return;
1793: }
1794: env_opt_start_info();
1795: env_opt_add(ep->var);
1796: env_opt_end(0);
1797: }
1798:
1799: void
1800: env_list()
1801: {
1802: register struct env_lst *ep;
1803:
1804: for (ep = envlisthead.next; ep; ep = ep->next) {
1.10 art 1805: printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1.1 deraadt 1806: ep->var, ep->value);
1807: }
1808: }
1809:
1810: unsigned char *
1811: env_default(init, welldefined)
1812: int init;
1813: {
1814: static struct env_lst *nep = NULL;
1815:
1816: if (init) {
1817: nep = &envlisthead;
1.10 art 1818: return NULL;
1.1 deraadt 1819: }
1820: if (nep) {
1.10 art 1821: while ((nep = nep->next)) {
1.1 deraadt 1822: if (nep->export && (nep->welldefined == welldefined))
1823: return(nep->var);
1824: }
1825: }
1826: return(NULL);
1827: }
1828:
1829: unsigned char *
1830: env_getvalue(var)
1831: unsigned char *var;
1832: {
1833: register struct env_lst *ep;
1834:
1.10 art 1835: if ((ep = env_find(var)))
1.1 deraadt 1836: return(ep->value);
1837: return(NULL);
1838: }
1839:
1840: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1841: void
1842: env_varval(what)
1843: unsigned char *what;
1844: {
1845: extern int old_env_var, old_env_value, env_auto;
1846: int len = strlen((char *)what);
1847:
1848: if (len == 0)
1849: goto unknown;
1850:
1851: if (strncasecmp((char *)what, "status", len) == 0) {
1852: if (env_auto)
1853: printf("%s%s", "VAR and VALUE are/will be ",
1.10 art 1854: "determined automatically\r\n");
1.1 deraadt 1855: if (old_env_var == OLD_ENV_VAR)
1.10 art 1856: printf("VAR and VALUE set to correct definitions\r\n");
1.1 deraadt 1857: else
1.10 art 1858: printf("VAR and VALUE definitions are reversed\r\n");
1.1 deraadt 1859: } else if (strncasecmp((char *)what, "auto", len) == 0) {
1860: env_auto = 1;
1861: old_env_var = OLD_ENV_VALUE;
1862: old_env_value = OLD_ENV_VAR;
1863: } else if (strncasecmp((char *)what, "right", len) == 0) {
1864: env_auto = 0;
1865: old_env_var = OLD_ENV_VAR;
1866: old_env_value = OLD_ENV_VALUE;
1867: } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1868: env_auto = 0;
1869: old_env_var = OLD_ENV_VALUE;
1870: old_env_value = OLD_ENV_VAR;
1871: } else {
1872: unknown:
1.10 art 1873: printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\r\n");
1.1 deraadt 1874: }
1875: }
1876: #endif
1877:
1878: #if defined(AUTHENTICATION)
1879: /*
1880: * The AUTHENTICATE command.
1881: */
1882:
1883: struct authlist {
1884: char *name;
1885: char *help;
1886: int (*handler)();
1887: int narg;
1888: };
1889:
1890: static int
1891: auth_help P((void));
1892:
1893: struct authlist AuthList[] = {
1894: { "status", "Display current status of authentication information",
1895: auth_status, 0 },
1896: { "disable", "Disable an authentication type ('auth disable ?' for more)",
1897: auth_disable, 1 },
1898: { "enable", "Enable an authentication type ('auth enable ?' for more)",
1899: auth_enable, 1 },
1900: { "help", 0, auth_help, 0 },
1901: { "?", "Print help information", auth_help, 0 },
1902: { 0 },
1903: };
1904:
1905: static int
1906: auth_help()
1907: {
1908: struct authlist *c;
1909:
1910: for (c = AuthList; c->name; c++) {
1911: if (c->help) {
1912: if (*c->help)
1.10 art 1913: printf("%-15s %s\r\n", c->name, c->help);
1.1 deraadt 1914: else
1.10 art 1915: printf("\r\n");
1.1 deraadt 1916: }
1917: }
1918: return 0;
1919: }
1920:
1.15 ! art 1921: int
1.1 deraadt 1922: auth_cmd(argc, argv)
1923: int argc;
1924: char *argv[];
1925: {
1926: struct authlist *c;
1927:
1.3 niklas 1928: if (argc < 2) {
1929: fprintf(stderr,
1.10 art 1930: "Need an argument to 'auth' command. 'auth ?' for help.\r\n");
1.3 niklas 1931: return 0;
1932: }
1933:
1.1 deraadt 1934: c = (struct authlist *)
1935: genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1936: if (c == 0) {
1.10 art 1937: fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1.1 deraadt 1938: argv[1]);
1.3 niklas 1939: return 0;
1.1 deraadt 1940: }
1941: if (Ambiguous(c)) {
1.10 art 1942: fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1.1 deraadt 1943: argv[1]);
1.3 niklas 1944: return 0;
1.1 deraadt 1945: }
1946: if (c->narg + 2 != argc) {
1947: fprintf(stderr,
1.10 art 1948: "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\r\n",
1.1 deraadt 1949: c->narg < argc + 2 ? "only " : "",
1950: c->narg, c->narg == 1 ? "" : "s", c->name);
1951: return 0;
1952: }
1953: return((*c->handler)(argv[2], argv[3]));
1954: }
1955: #endif
1956:
1.10 art 1957: #if defined(ENCRYPTION)
1958: /*
1959: * The ENCRYPT command.
1960: */
1961:
1962: struct encryptlist {
1963: char *name;
1964: char *help;
1965: int (*handler)();
1966: int needconnect;
1967: int minarg;
1968: int maxarg;
1969: };
1970:
1971: static int
1972: EncryptHelp (void);
1973:
1974: struct encryptlist EncryptList[] = {
1975: { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1976: EncryptEnable, 1, 1, 2 },
1977: { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1978: EncryptDisable, 0, 1, 2 },
1979: { "type", "Set encryptiong type. ('encrypt type ?' for more)",
1980: EncryptType, 0, 1, 1 },
1981: { "start", "Start encryption. ('encrypt start ?' for more)",
1982: EncryptStart, 1, 0, 1 },
1983: { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1984: EncryptStop, 1, 0, 1 },
1985: { "input", "Start encrypting the input stream",
1986: EncryptStartInput, 1, 0, 0 },
1987: { "-input", "Stop encrypting the input stream",
1988: EncryptStopInput, 1, 0, 0 },
1989: { "output", "Start encrypting the output stream",
1990: EncryptStartOutput, 1, 0, 0 },
1991: { "-output", "Stop encrypting the output stream",
1992: EncryptStopOutput, 1, 0, 0 },
1993:
1994: { "status", "Display current status of authentication information",
1995: EncryptStatus, 0, 0, 0 },
1996: { "help", 0, EncryptHelp, 0, 0, 0 },
1997: { "?", "Print help information", EncryptHelp, 0, 0, 0 },
1998: { 0 },
1999: };
2000:
2001: static int
2002: EncryptHelp()
2003: {
2004: struct encryptlist *c;
2005:
2006: for (c = EncryptList; c->name; c++) {
2007: if (c->help) {
2008: if (*c->help)
2009: printf("%-15s %s\r\n", c->name, c->help);
2010: else
2011: printf("\r\n");
2012: }
2013: }
2014: return 0;
2015: }
2016:
2017: static int
2018: encrypt_cmd(int argc, char **argv)
2019: {
2020: struct encryptlist *c;
1.13 art 2021:
2022: if (argc < 2) {
2023: fprintf(stderr, "Need at least one argument for 'encrypt' command.\n");
2024: fprintf(stderr, "('encrypt ?' for help)\n");
2025: return 0;
2026: }
2027:
1.10 art 2028: c = (struct encryptlist *)
2029: genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2030: if (c == 0) {
2031: fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
2032: argv[1]);
2033: return 0;
2034: }
2035: if (Ambiguous(c)) {
2036: fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
2037: argv[1]);
2038: return 0;
2039: }
2040: argc -= 2;
2041: if (argc < c->minarg || argc > c->maxarg) {
2042: if (c->minarg == c->maxarg) {
2043: fprintf(stderr, "Need %s%d argument%s ",
2044: c->minarg < argc ? "only " : "", c->minarg,
2045: c->minarg == 1 ? "" : "s");
2046: } else {
2047: fprintf(stderr, "Need %s%d-%d arguments ",
2048: c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2049: }
2050: fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\r\n",
2051: c->name);
2052: return 0;
2053: }
2054: if (c->needconnect && !connected) {
2055: if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2056: printf("?Need to be connected first.\r\n");
2057: return 0;
2058: }
2059: }
2060: return ((*c->handler)(argc > 0 ? argv[2] : 0,
2061: argc > 1 ? argv[3] : 0,
2062: argc > 2 ? argv[4] : 0));
2063: }
2064: #endif
1.1 deraadt 2065:
2066: #if defined(unix) && defined(TN3270)
2067: static void
2068: filestuff(fd)
2069: int fd;
2070: {
2071: int res;
2072:
2073: #ifdef F_GETOWN
2074: setconnmode(0);
2075: res = fcntl(fd, F_GETOWN, 0);
2076: setcommandmode();
2077:
2078: if (res == -1) {
2079: perror("fcntl");
2080: return;
2081: }
1.10 art 2082: printf("\tOwner is %d.\r\n", res);
1.1 deraadt 2083: #endif
2084:
2085: setconnmode(0);
2086: res = fcntl(fd, F_GETFL, 0);
2087: setcommandmode();
2088:
2089: if (res == -1) {
2090: perror("fcntl");
2091: return;
2092: }
2093: #ifdef notdef
1.10 art 2094: printf("\tFlags are 0x%x: %s\r\n", res, decodeflags(res));
1.1 deraadt 2095: #endif
2096: }
2097: #endif /* defined(unix) && defined(TN3270) */
2098:
2099: /*
2100: * Print status about the connection.
2101: */
2102: /*ARGSUSED*/
1.15 ! art 2103: static int
1.1 deraadt 2104: status(argc, argv)
2105: int argc;
2106: char *argv[];
2107: {
2108: if (connected) {
1.10 art 2109: printf("Connected to %s.\r\n", hostname);
1.1 deraadt 2110: if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2111: int mode = getconnmode();
2112:
2113: if (my_want_state_is_will(TELOPT_LINEMODE)) {
1.10 art 2114: printf("Operating with LINEMODE option\r\n");
2115: printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
2116: printf("%s catching of signals\r\n",
1.1 deraadt 2117: (mode&MODE_TRAPSIG) ? "Local" : "No");
2118: slcstate();
2119: #ifdef KLUDGELINEMODE
2120: } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1.10 art 2121: printf("Operating in obsolete linemode\r\n");
1.1 deraadt 2122: #endif
2123: } else {
1.10 art 2124: printf("Operating in single character mode\r\n");
1.1 deraadt 2125: if (localchars)
1.10 art 2126: printf("Catching signals locally\r\n");
1.1 deraadt 2127: }
1.10 art 2128: printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1.1 deraadt 2129: if (my_want_state_is_will(TELOPT_LFLOW))
1.10 art 2130: printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
2131: #if defined(ENCRYPTION)
2132: encrypt_display();
2133: #endif
1.1 deraadt 2134: }
2135: } else {
1.10 art 2136: printf("No connection.\r\n");
1.1 deraadt 2137: }
2138: # if !defined(TN3270)
1.10 art 2139: printf("Escape character is '%s'.\r\n", control(escape));
1.1 deraadt 2140: (void) fflush(stdout);
2141: # else /* !defined(TN3270) */
2142: if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
1.10 art 2143: printf("Escape character is '%s'.\r\n", control(escape));
1.1 deraadt 2144: }
2145: # if defined(unix)
2146: if ((argc >= 2) && !strcmp(argv[1], "everything")) {
1.10 art 2147: printf("SIGIO received %d time%s.\r\n",
1.1 deraadt 2148: sigiocount, (sigiocount == 1)? "":"s");
2149: if (In3270) {
1.10 art 2150: printf("Process ID %d, process group %d.\r\n",
1.11 deraadt 2151: getpid(), getpgrp());
1.10 art 2152: printf("Terminal input:\r\n");
1.1 deraadt 2153: filestuff(tin);
1.10 art 2154: printf("Terminal output:\r\n");
1.1 deraadt 2155: filestuff(tout);
1.10 art 2156: printf("Network socket:\r\n");
1.1 deraadt 2157: filestuff(net);
2158: }
2159: }
2160: if (In3270 && transcom) {
1.10 art 2161: printf("Transparent mode command is '%s'.\r\n", transcom);
1.1 deraadt 2162: }
2163: # endif /* defined(unix) */
2164: (void) fflush(stdout);
2165: if (In3270) {
2166: return 0;
2167: }
2168: # endif /* defined(TN3270) */
1.10 art 2169: fflush(stdout);
1.1 deraadt 2170: return 1;
2171: }
2172:
2173: #ifdef SIGINFO
2174: /*
2175: * Function that gets called when SIGINFO is received.
2176: */
1.10 art 2177: void
1.1 deraadt 2178: ayt_status()
2179: {
2180: (void) call(status, "status", "notmuch", 0);
2181: }
2182: #endif
2183:
1.10 art 2184: static Command *getcmd(char *name);
2185:
2186: static void
2187: cmdrc(char *m1, char *m2)
2188: {
2189: static char rcname[128];
2190: Command *c;
2191: FILE *rcfile;
2192: int gotmachine = 0;
2193: int l1 = strlen(m1);
2194: int l2 = strlen(m2);
2195: char m1save[64];
2196:
2197: if (skiprc)
2198: return;
2199:
2200: strcpy(m1save, m1);
2201: m1 = m1save;
2202:
2203: if (rcname[0] == 0) {
2204: char *home = getenv("HOME");
2205:
2206: snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
2207: home ? home : "");
2208: }
2209:
2210: if ((rcfile = fopen(rcname, "r")) == 0) {
2211: return;
2212: }
2213:
2214: for (;;) {
2215: if (fgets(line, sizeof(line), rcfile) == NULL)
2216: break;
2217: if (line[0] == 0)
2218: break;
2219: if (line[0] == '#')
2220: continue;
2221: if (gotmachine) {
2222: if (!isspace(line[0]))
2223: gotmachine = 0;
2224: }
2225: if (gotmachine == 0) {
2226: if (isspace(line[0]))
2227: continue;
2228: if (strncasecmp(line, m1, l1) == 0)
2229: strncpy(line, &line[l1], sizeof(line) - l1);
2230: else if (strncasecmp(line, m2, l2) == 0)
2231: strncpy(line, &line[l2], sizeof(line) - l2);
2232: else if (strncasecmp(line, "DEFAULT", 7) == 0)
2233: strncpy(line, &line[7], sizeof(line) - 7);
2234: else
2235: continue;
2236: if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2237: continue;
2238: gotmachine = 1;
2239: }
2240: makeargv();
2241: if (margv[0] == 0)
2242: continue;
2243: c = getcmd(margv[0]);
2244: if (Ambiguous(c)) {
2245: printf("?Ambiguous command: %s\r\n", margv[0]);
2246: continue;
2247: }
2248: if (c == 0) {
2249: printf("?Invalid command: %s\r\n", margv[0]);
2250: continue;
2251: }
2252: /*
2253: * This should never happen...
2254: */
2255: if (c->needconnect && !connected) {
2256: printf("?Need to be connected first for %s.\r\n", margv[0]);
2257: continue;
2258: }
2259: (*c->handler)(margc, margv);
2260: }
2261: fclose(rcfile);
2262: }
2263:
1.1 deraadt 2264:
2265: int
2266: tn(argc, argv)
2267: int argc;
2268: char *argv[];
2269: {
1.5 niklas 2270: register struct hostent *host = 0, *alias = 0;
1.10 art 2271: #if defined(AF_INET6)
2272: struct sockaddr_in6 sin6;
2273: #endif
1.1 deraadt 2274: struct sockaddr_in sin;
1.5 niklas 2275: struct sockaddr_in ladr;
1.10 art 2276: struct sockaddr *sa;
2277: int sa_size;
1.1 deraadt 2278: struct servent *sp = 0;
2279: unsigned long temp;
2280: extern char *inet_ntoa();
2281: #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
1.10 art 2282: char *srp = 0;
2283: int srlen;
1.1 deraadt 2284: #endif
1.5 niklas 2285: char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
1.10 art 2286: int family, port;
1.1 deraadt 2287:
2288: /* clear the socket address prior to use */
1.3 niklas 2289: memset((char *)&sin, 0, sizeof(sin));
1.1 deraadt 2290:
2291: if (connected) {
1.10 art 2292: printf("?Already connected to %s\r\n", hostname);
1.9 tholo 2293: seteuid(getuid());
1.1 deraadt 2294: setuid(getuid());
2295: return 0;
2296: }
2297: if (argc < 2) {
2298: (void) strcpy(line, "open ");
2299: printf("(to) ");
2300: (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2301: makeargv();
2302: argc = margc;
2303: argv = margv;
2304: }
2305: cmd = *argv;
2306: --argc; ++argv;
2307: while (argc) {
1.3 niklas 2308: if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1.1 deraadt 2309: goto usage;
2310: if (strcmp(*argv, "-l") == 0) {
2311: --argc; ++argv;
2312: if (argc == 0)
2313: goto usage;
1.14 art 2314: if ((user = strdup(*argv++)) == NULL)
2315: err(1, "strdup");
1.1 deraadt 2316: --argc;
2317: continue;
2318: }
1.5 niklas 2319: if (strcmp(*argv, "-b") == 0) {
2320: --argc; ++argv;
2321: if (argc == 0)
2322: goto usage;
2323: aliasp = *argv++;
2324: --argc;
2325: continue;
2326: }
1.1 deraadt 2327: if (strcmp(*argv, "-a") == 0) {
2328: --argc; ++argv;
2329: autologin = 1;
2330: continue;
2331: }
2332: if (hostp == 0) {
2333: hostp = *argv++;
2334: --argc;
2335: continue;
2336: }
2337: if (portp == 0) {
2338: portp = *argv++;
2339: --argc;
2340: continue;
2341: }
2342: usage:
1.10 art 2343: printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
1.9 tholo 2344: seteuid(getuid());
1.1 deraadt 2345: setuid(getuid());
2346: return 0;
2347: }
2348: if (hostp == 0)
2349: goto usage;
2350:
2351: #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2352: if (hostp[0] == '@' || hostp[0] == '!') {
2353: if ((hostname = strrchr(hostp, ':')) == NULL)
2354: hostname = strrchr(hostp, '@');
2355: hostname++;
2356: srp = 0;
2357: temp = sourceroute(hostp, &srp, &srlen);
2358: if (temp == 0) {
2359: herror(srp);
1.9 tholo 2360: seteuid(getuid());
1.1 deraadt 2361: setuid(getuid());
2362: return 0;
2363: } else if (temp == -1) {
1.10 art 2364: printf("Bad source route option: %s\r\n", hostp);
1.9 tholo 2365: seteuid(getuid());
1.1 deraadt 2366: setuid(getuid());
2367: return 0;
2368: } else {
1.10 art 2369: abort();
1.1 deraadt 2370: }
2371: } else {
2372: #endif
1.10 art 2373: memset (&sin, 0, sizeof(sin));
2374: #if defined(HAVE_INET_PTON) && defined(AF_INET6)
2375: memset (&sin6, 0, sizeof(sin6));
2376:
2377: if(inet_pton(AF_INET6, hostp, &sin6.sin6_addr)) {
2378: sin6.sin6_family = family = AF_INET6;
1.12 art 2379: sa = (struct sockaddr *)&sin6;
2380: sa_size = sizeof(sin6);
1.10 art 2381: strcpy(_hostname, hostp);
2382: hostname =_hostname;
2383: } else
2384: #endif
2385: if(inet_aton(hostp, &sin.sin_addr)){
2386: sin.sin_family = family = AF_INET;
1.12 art 2387: sa = (struct sockaddr *)&sin;
2388: sa_size = sizeof(sin);
1.10 art 2389: strcpy(_hostname, hostp);
1.1 deraadt 2390: hostname = _hostname;
2391: } else {
1.10 art 2392: #ifdef HAVE_GETHOSTBYNAME2
2393: host = gethostbyname2(hostp, AF_INET6);
2394: if(host == NULL)
2395: host = gethostbyname2(hostp, AF_INET);
2396: #else
2397: host = gethostbyname(hostp);
2398: #endif
2399: if (host) {
2400: strncpy(_hostname, host->h_name, sizeof(_hostname));
2401: family = host->h_addrtype;
2402:
2403: switch(family) {
2404: case AF_INET:
2405: memset(&sin, 0, sizeof(sin));
2406: sa_size = sizeof(sin);
2407: sa = (struct sockaddr *)&sin;
2408: sin.sin_family = family;
2409: sin.sin_addr = *((struct in_addr *)(*host->h_addr_list));
2410: break;
2411: #if defined(AF_INET6) && defined(HAVE_STRUCT_SOCKADDR_IN6)
2412: case AF_INET6:
2413: memset(&sin6, 0, sizeof(sin6));
2414: sa_size = sizeof(sin6);
2415: sa = (struct sockaddr *)&sin6;
2416: sin6.sin6_family = family;
2417: sin6.sin6_addr = *((struct in6_addr *)(*host->h_addr_list));
2418: break;
2419: #endif
2420: default:
2421: fprintf(stderr, "Bad address family: %d\n", family);
2422: return 0;
2423: }
2424:
2425: _hostname[sizeof(_hostname)-1] = '\0';
2426: hostname = _hostname;
2427: } else {
2428: herror(hostp);
2429: seteuid(getuid());
2430: setuid(getuid());
2431: fprintf (stderr, "%s: %s\r\n", hostp ? hostp : "",
2432: "unknown error");
2433: return 0;
2434: }
1.1 deraadt 2435: }
2436: #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2437: }
2438: #endif
2439: if (portp) {
2440: if (*portp == '-') {
2441: portp++;
2442: telnetport = 1;
2443: } else
2444: telnetport = 0;
1.10 art 2445: port = atoi(portp);
2446: if (port == 0) {
1.1 deraadt 2447: sp = getservbyname(portp, "tcp");
2448: if (sp)
1.10 art 2449: port = sp->s_port;
1.1 deraadt 2450: else {
1.10 art 2451: printf("%s: bad port number\r\n", portp);
1.9 tholo 2452: seteuid(getuid());
1.3 niklas 2453: setuid(getuid());
1.1 deraadt 2454: return 0;
2455: }
2456: } else {
1.10 art 2457: port = htons(port);
1.1 deraadt 2458: }
2459: } else {
2460: if (sp == 0) {
2461: sp = getservbyname("telnet", "tcp");
2462: if (sp == 0) {
1.10 art 2463: fprintf(stderr, "telnet: tcp/telnet: unknown service\r\n");
1.9 tholo 2464: seteuid(getuid());
1.3 niklas 2465: setuid(getuid());
1.1 deraadt 2466: return 0;
2467: }
1.10 art 2468: port = sp->s_port;
1.1 deraadt 2469: }
2470: telnetport = 1;
2471: }
1.10 art 2472: switch(family) {
2473: case AF_INET:
2474: sin.sin_port = port;
2475: printf("Trying %s...\r\n", inet_ntoa(sin.sin_addr));
2476: break;
2477: #if defined(AF_INET6) && defined(HAVE_STRUCT_SOCKADDR_IN6)
2478: case AF_INET6: {
2479: #ifndef INET6_ADDRSTRLEN
2480: #define INET6_ADDRSTRLEN 46
2481: #endif
2482:
2483: char buf[INET6_ADDRSTRLEN];
2484:
2485: sin6.sin6_port = port;
2486: #ifdef HAVE_INET_NTOP
2487: printf("Trying %s...\r\n", inet_ntop(AF_INET6,
2488: &sin6.sin6_addr,
2489: buf,
2490: sizeof(buf)));
2491: #endif
2492: break;
2493: }
2494: #endif
2495: default:
2496: abort();
2497: }
2498:
1.1 deraadt 2499: do {
1.10 art 2500: net = socket(family, SOCK_STREAM, 0);
1.9 tholo 2501: seteuid(getuid());
1.1 deraadt 2502: setuid(getuid());
2503: if (net < 0) {
2504: perror("telnet: socket");
2505: return 0;
2506: }
1.5 niklas 2507: if (aliasp) {
2508: memset ((caddr_t)&ladr, 0, sizeof (ladr));
2509: temp = inet_addr(aliasp);
2510: if (temp != INADDR_NONE) {
2511: ladr.sin_addr.s_addr = temp;
2512: ladr.sin_family = AF_INET;
2513: alias = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
2514: } else {
2515: alias = gethostbyname(aliasp);
2516: if (alias) {
2517: ladr.sin_family = alias->h_addrtype;
2518: #if defined(h_addr) /* In 4.3, this is a #define */
2519: memmove((caddr_t)&ladr.sin_addr,
2520: alias->h_addr_list[0], alias->h_length);
2521: #else /* defined(h_addr) */
2522: memmove((caddr_t)&ladr.sin_addr, alias->h_addr,
2523: alias->h_length);
2524: #endif /* defined(h_addr) */
2525: } else {
2526: herror(aliasp);
2527: return 0;
2528: }
2529: }
2530: ladr.sin_port = htons(0);
2531:
2532: if (bind (net, (struct sockaddr *)&ladr, sizeof(ladr)) < 0) {
2533: perror(aliasp);;
2534: (void) close(net); /* dump descriptor */
2535: return 0;
2536: }
2537: }
2538: #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
1.1 deraadt 2539: if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2540: perror("setsockopt (IP_OPTIONS)");
2541: #endif
2542: #if defined(IPPROTO_IP) && defined(IP_TOS)
2543: {
2544: # if defined(HAS_GETTOS)
2545: struct tosent *tp;
2546: if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2547: tos = tp->t_tos;
2548: # endif
2549: if (tos < 0)
1.3 niklas 2550: tos = IPTOS_LOWDELAY; /* Low Delay bit */
1.1 deraadt 2551: if (tos
2552: && (setsockopt(net, IPPROTO_IP, IP_TOS,
1.10 art 2553: (void *)&tos, sizeof(int)) < 0)
1.1 deraadt 2554: && (errno != ENOPROTOOPT))
2555: perror("telnet: setsockopt (IP_TOS) (ignored)");
2556: }
2557: #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2558:
2559: if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2560: perror("setsockopt (SO_DEBUG)");
2561: }
2562:
1.10 art 2563: if (connect(net, sa, sa_size) < 0) {
1.1 deraadt 2564: if (host && host->h_addr_list[1]) {
2565: int oerrno = errno;
2566:
1.10 art 2567: switch(family) {
2568: case AF_INET :
2569: fprintf(stderr, "telnet: connect to address %s: ",
2570: inet_ntoa(sin.sin_addr));
2571: sin.sin_addr = *((struct in_addr *)(*++host->h_addr_list));
2572: break;
2573: #if defined(AF_INET6) && defined(HAVE_STRUCT_SOCKADDR_IN6)
2574: case AF_INET6: {
2575: char buf[INET6_ADDRSTRLEN];
2576:
2577: fprintf(stderr, "telnet: connect to address %s: ",
2578: inet_ntop(AF_INET6, &sin6.sin6_addr, buf,
2579: sizeof(buf)));
2580: sin6.sin6_addr = *((struct in6_addr *)(*++host->h_addr_list));
2581: break;
2582: }
2583: #endif
2584: default:
2585: abort();
2586: }
2587:
1.1 deraadt 2588: errno = oerrno;
1.10 art 2589: perror(NULL);
1.1 deraadt 2590: host->h_addr_list++;
1.3 niklas 2591: memmove((caddr_t)&sin.sin_addr,
1.1 deraadt 2592: host->h_addr_list[0], host->h_length);
2593: (void) NetClose(net);
2594: continue;
2595: }
2596: perror("telnet: Unable to connect to remote host");
2597: return 0;
2598: }
2599: connected++;
1.10 art 2600: #if defined(AUTHENTICATION) || defined(ENCRYPTION)
1.1 deraadt 2601: auth_encrypt_connect(connected);
2602: #endif /* defined(AUTHENTICATION) */
2603: } while (connected == 0);
2604: cmdrc(hostp, hostname);
2605: if (autologin && user == NULL) {
2606: struct passwd *pw;
2607:
2608: user = getenv("USER");
2609: if (user == NULL ||
1.15 ! art 2610: ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
! 2611: if ((pw = getpwuid(getuid())) != NULL)
1.1 deraadt 2612: user = pw->pw_name;
2613: else
2614: user = NULL;
2615: }
2616: }
2617: if (user) {
2618: env_define((unsigned char *)"USER", (unsigned char *)user);
2619: env_export((unsigned char *)"USER");
2620: }
2621: (void) call(status, "status", "notmuch", 0);
2622: if (setjmp(peerdied) == 0)
2623: telnet(user);
2624: (void) NetClose(net);
1.10 art 2625: ExitString("Connection closed by foreign host.\r\n",1);
1.1 deraadt 2626: /*NOTREACHED*/
1.10 art 2627: return 0;
1.1 deraadt 2628: }
2629:
2630: #define HELPINDENT (sizeof ("connect"))
2631:
2632: static char
2633: openhelp[] = "connect to a site",
2634: closehelp[] = "close current connection",
2635: logouthelp[] = "forcibly logout remote user and close the connection",
2636: quithelp[] = "exit telnet",
2637: statushelp[] = "print status information",
2638: helphelp[] = "print help information",
2639: sendhelp[] = "transmit special characters ('send ?' for more)",
2640: sethelp[] = "set operating parameters ('set ?' for more)",
2641: unsethelp[] = "unset operating parameters ('unset ?' for more)",
2642: togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2643: slchelp[] = "change state of special charaters ('slc ?' for more)",
2644: displayhelp[] = "display operating parameters",
2645: #if defined(TN3270) && defined(unix)
2646: transcomhelp[] = "specify Unix command for transparent mode pipe",
2647: #endif /* defined(TN3270) && defined(unix) */
2648: #if defined(AUTHENTICATION)
2649: authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2650: #endif
1.10 art 2651: #if defined(ENCRYPTION)
2652: encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2653: #endif
1.1 deraadt 2654: zhelp[] = "suspend telnet",
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)
2693: { "skey", NULL, skey_calc, 0 },
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