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