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