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