Annotation of src/usr.bin/telnet/commands.c, Revision 1.69
1.69 ! jsg 1: /* $OpenBSD: commands.c,v 1.68 2014/07/20 12:08:55 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.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.63 guenther 775: void (*handler)(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.67 guenther 1423: static void env_undefine(unsigned char *);
1424: static void env_export(unsigned char *);
1425: static void env_unexport(unsigned char *);
1426: static void env_send(unsigned char *);
1427: static void env_list(void);
1428: static struct env_lst *env_find(unsigned 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 */
1504: unsigned char *var; /* pointer to variable name */
1505: unsigned char *value; /* pointer to variable value */
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.69 ! jsg 1513: env_find(unsigned 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) {
1518: if (strcmp((char *)ep->var, (char *)var) == 0)
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';
1534: ep = env_define((unsigned char *)*epp,
1535: (unsigned char *)cp+1);
1536: ep->export = 0;
1537: *cp = '=';
1538: }
1539: }
1540: /*
1541: * Special case for DISPLAY variable. If it is ":0.0" or
1542: * "unix:0.0", we have to get rid of "unix" and insert our
1543: * hostname.
1544: */
1545: if ((ep = env_find("DISPLAY"))
1546: && ((*ep->value == ':')
1.3 niklas 1547: || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1.18 deraadt 1548: char hbuf[MAXHOSTNAMELEN];
1.3 niklas 1549: char *cp2 = strchr((char *)ep->value, ':');
1.1 deraadt 1550:
1.18 deraadt 1551: gethostname(hbuf, sizeof hbuf);
1.10 art 1552:
1553: /* If this is not the full name, try to get it via DNS */
1554: if (strchr(hbuf, '.') == 0) {
1555: struct hostent *he = gethostbyname(hbuf);
1556: if (he != 0)
1.19 deraadt 1557: strncpy(hbuf, he->h_name, sizeof hbuf-1);
1558: hbuf[sizeof hbuf-1] = '\0';
1.10 art 1559: }
1560:
1.44 pvalchev 1561: if (asprintf (&cp, "%s%s", hbuf, cp2) == -1)
1.14 art 1562: err(1, "asprintf");
1.10 art 1563:
1.1 deraadt 1564: free(ep->value);
1565: ep->value = (unsigned char *)cp;
1566: }
1567: /*
1568: * If USER is not defined, but LOGNAME is, then add
1569: * USER with the value from LOGNAME. By default, we
1570: * don't export the USER variable.
1571: */
1572: if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1573: env_define((unsigned char *)"USER", ep->value);
1574: env_unexport((unsigned char *)"USER");
1575: }
1576: env_export((unsigned char *)"DISPLAY");
1577: env_export((unsigned char *)"PRINTER");
1.10 art 1578: env_export((unsigned char *)"XAUTHORITY");
1.1 deraadt 1579: }
1580:
1.69 ! jsg 1581: struct env_lst *
! 1582: env_define(unsigned char *var, unsigned char *value)
1.1 deraadt 1583: {
1.39 mpech 1584: struct env_lst *ep;
1.1 deraadt 1585:
1.10 art 1586: if ((ep = env_find(var))) {
1.1 deraadt 1587: if (ep->var)
1588: free(ep->var);
1589: if (ep->value)
1590: free(ep->value);
1591: } else {
1.14 art 1592: if ((ep = malloc(sizeof(struct env_lst))) == NULL)
1593: err(1, "malloc");
1.1 deraadt 1594: ep->next = envlisthead.next;
1595: envlisthead.next = ep;
1596: ep->prev = &envlisthead;
1597: if (ep->next)
1598: ep->next->prev = ep;
1599: }
1.10 art 1600: ep->welldefined = opt_welldefined((char *)var);
1.1 deraadt 1601: ep->export = 1;
1.14 art 1602: if ((ep->var = strdup((char *)var)) == NULL)
1603: err(1, "strdup");
1604: if ((ep->value = strdup((char *)value)) == NULL)
1605: err(1, "strdup");
1.1 deraadt 1606: return(ep);
1607: }
1608:
1.67 guenther 1609: static void
1.69 ! jsg 1610: env_undefine(unsigned char *var)
1.1 deraadt 1611: {
1.39 mpech 1612: struct env_lst *ep;
1.1 deraadt 1613:
1.10 art 1614: if ((ep = env_find(var))) {
1.1 deraadt 1615: ep->prev->next = ep->next;
1616: if (ep->next)
1617: ep->next->prev = ep->prev;
1618: if (ep->var)
1619: free(ep->var);
1620: if (ep->value)
1621: free(ep->value);
1622: free(ep);
1623: }
1624: }
1625:
1.67 guenther 1626: static void
1.69 ! jsg 1627: env_export(unsigned char *var)
1.1 deraadt 1628: {
1.39 mpech 1629: struct env_lst *ep;
1.1 deraadt 1630:
1.10 art 1631: if ((ep = env_find(var)))
1.1 deraadt 1632: ep->export = 1;
1633: }
1634:
1.67 guenther 1635: static void
1.69 ! jsg 1636: env_unexport(unsigned char *var)
1.1 deraadt 1637: {
1.39 mpech 1638: struct env_lst *ep;
1.1 deraadt 1639:
1.15 art 1640: if ((ep = env_find(var)) != NULL)
1.1 deraadt 1641: ep->export = 0;
1642: }
1643:
1.67 guenther 1644: static void
1.69 ! jsg 1645: env_send(unsigned char *var)
1.1 deraadt 1646: {
1.39 mpech 1647: struct env_lst *ep;
1.1 deraadt 1648:
1.3 niklas 1649: if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1.1 deraadt 1650: ) {
1651: fprintf(stderr,
1.10 art 1652: "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1.1 deraadt 1653: var);
1654: return;
1655: }
1656: ep = env_find(var);
1657: if (ep == 0) {
1.10 art 1658: fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1.1 deraadt 1659: var);
1660: return;
1661: }
1662: env_opt_start_info();
1663: env_opt_add(ep->var);
1664: env_opt_end(0);
1665: }
1666:
1.67 guenther 1667: static void
1.69 ! jsg 1668: env_list(void)
1.1 deraadt 1669: {
1.39 mpech 1670: struct env_lst *ep;
1.1 deraadt 1671:
1672: for (ep = envlisthead.next; ep; ep = ep->next) {
1.10 art 1673: printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1.1 deraadt 1674: ep->var, ep->value);
1675: }
1676: }
1677:
1.69 ! jsg 1678: unsigned char *
! 1679: env_default(int init, int welldefined)
1.1 deraadt 1680: {
1681: static struct env_lst *nep = NULL;
1682:
1683: if (init) {
1684: nep = &envlisthead;
1.10 art 1685: return NULL;
1.1 deraadt 1686: }
1687: if (nep) {
1.10 art 1688: while ((nep = nep->next)) {
1.1 deraadt 1689: if (nep->export && (nep->welldefined == welldefined))
1690: return(nep->var);
1691: }
1692: }
1693: return(NULL);
1694: }
1695:
1.69 ! jsg 1696: unsigned char *
! 1697: env_getvalue(unsigned char *var, int exported_only)
1.1 deraadt 1698: {
1.39 mpech 1699: struct env_lst *ep;
1.1 deraadt 1700:
1.48 otto 1701: if ((ep = env_find(var)) && (!exported_only || ep->export))
1.1 deraadt 1702: return(ep->value);
1703: return(NULL);
1704: }
1705:
1.68 guenther 1706: static void
1707: connection_status(int local_only)
1708: {
1709: if (!connected)
1710: printf("No connection.\r\n");
1711: else {
1712: printf("Connected to %s.\r\n", hostname);
1713: if (!local_only) {
1714: int mode = getconnmode();
1715:
1716: printf("Operating ");
1717: if (my_want_state_is_will(TELOPT_LINEMODE)) {
1718: printf("with LINEMODE option\r\n"
1719: "%s line editing\r\n"
1720: "%s catching of signals\r\n",
1721: (mode & MODE_EDIT) ? "Local" : "No",
1722: (mode & MODE_TRAPSIG) ? "Local" : "No");
1723: slcstate();
1724: #ifdef KLUDGELINEMODE
1725: } else if (kludgelinemode &&
1726: my_want_state_is_dont(TELOPT_SGA)) {
1727: printf("in obsolete linemode\r\n");
1728: #endif
1729: } else {
1730: printf("in single character mode\r\n");
1731: if (localchars)
1732: printf("Catching signals locally\r\n");
1733: }
1734:
1735: printf("%s character echo\r\n",
1736: (mode & MODE_ECHO) ? "Local" : "Remote");
1737: if (my_want_state_is_will(TELOPT_LFLOW))
1738: printf("%s flow control\r\n",
1739: (mode & MODE_FLOW) ? "Local" : "No");
1740: }
1741: }
1742: printf("Escape character is '%s'.\r\n", control(escape));
1743: (void) fflush(stdout);
1744: }
1745:
1.1 deraadt 1746: /*
1747: * Print status about the connection.
1748: */
1.68 guenther 1749: static int
1750: status(int argc, char *argv[])
1.1 deraadt 1751: {
1.68 guenther 1752: connection_status(0);
1753: return 1;
1.1 deraadt 1754: }
1755:
1756: #ifdef SIGINFO
1757: /*
1758: * Function that gets called when SIGINFO is received.
1759: */
1.10 art 1760: void
1.68 guenther 1761: ayt_status(int sig)
1.1 deraadt 1762: {
1.68 guenther 1763: connection_status(1);
1.1 deraadt 1764: }
1765: #endif
1766:
1.10 art 1767: static Command *getcmd(char *name);
1768:
1769: static void
1770: cmdrc(char *m1, char *m2)
1771: {
1772: static char rcname[128];
1773: Command *c;
1774: FILE *rcfile;
1775: int gotmachine = 0;
1776: int l1 = strlen(m1);
1777: int l2 = strlen(m2);
1.34 aaron 1778: char m1save[MAXHOSTNAMELEN];
1.10 art 1779:
1780: if (skiprc)
1781: return;
1782:
1.34 aaron 1783: strlcpy(m1save, m1, sizeof(m1save));
1.10 art 1784: m1 = m1save;
1785:
1786: if (rcname[0] == 0) {
1787: char *home = getenv("HOME");
1788:
1.29 millert 1789: if (home == NULL || *home == '\0')
1790: return;
1.10 art 1791: snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
1792: home ? home : "");
1793: }
1794:
1795: if ((rcfile = fopen(rcname, "r")) == 0) {
1796: return;
1797: }
1798:
1799: for (;;) {
1800: if (fgets(line, sizeof(line), rcfile) == NULL)
1801: break;
1802: if (line[0] == 0)
1803: break;
1804: if (line[0] == '#')
1805: continue;
1806: if (gotmachine) {
1.60 guenther 1807: if (!isspace((unsigned char)line[0]))
1.10 art 1808: gotmachine = 0;
1809: }
1810: if (gotmachine == 0) {
1.60 guenther 1811: if (isspace((unsigned char)line[0]))
1.10 art 1812: continue;
1813: if (strncasecmp(line, m1, l1) == 0)
1814: strncpy(line, &line[l1], sizeof(line) - l1);
1815: else if (strncasecmp(line, m2, l2) == 0)
1816: strncpy(line, &line[l2], sizeof(line) - l2);
1817: else if (strncasecmp(line, "DEFAULT", 7) == 0)
1818: strncpy(line, &line[7], sizeof(line) - 7);
1819: else
1820: continue;
1821: if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
1822: continue;
1823: gotmachine = 1;
1824: }
1825: makeargv();
1826: if (margv[0] == 0)
1827: continue;
1828: c = getcmd(margv[0]);
1829: if (Ambiguous(c)) {
1830: printf("?Ambiguous command: %s\r\n", margv[0]);
1831: continue;
1832: }
1833: if (c == 0) {
1834: printf("?Invalid command: %s\r\n", margv[0]);
1835: continue;
1836: }
1837: /*
1838: * This should never happen...
1839: */
1840: if (c->needconnect && !connected) {
1841: printf("?Need to be connected first for %s.\r\n", margv[0]);
1842: continue;
1843: }
1844: (*c->handler)(margc, margv);
1845: }
1846: fclose(rcfile);
1847: }
1848:
1.69 ! jsg 1849: int
! 1850: tn(int argc, char *argv[])
1.1 deraadt 1851: {
1.23 itojun 1852: struct addrinfo hints, *res, *res0;
1853: int error;
1.1 deraadt 1854: struct sockaddr_in sin;
1855: unsigned long temp;
1.10 art 1856: char *srp = 0;
1857: int srlen;
1.5 niklas 1858: char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
1.31 itojun 1859: int retry;
1860: const int niflags = NI_NUMERICHOST;
1.1 deraadt 1861:
1862: /* clear the socket address prior to use */
1.3 niklas 1863: memset((char *)&sin, 0, sizeof(sin));
1.1 deraadt 1864:
1865: if (connected) {
1.10 art 1866: printf("?Already connected to %s\r\n", hostname);
1.1 deraadt 1867: return 0;
1868: }
1869: if (argc < 2) {
1.43 hin 1870: strlcpy(line, "open ", sizeof(line));
1.1 deraadt 1871: printf("(to) ");
1872: (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
1873: makeargv();
1874: argc = margc;
1875: argv = margv;
1876: }
1877: cmd = *argv;
1878: --argc; ++argv;
1879: while (argc) {
1.3 niklas 1880: if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1.1 deraadt 1881: goto usage;
1882: if (strcmp(*argv, "-l") == 0) {
1883: --argc; ++argv;
1884: if (argc == 0)
1885: goto usage;
1.14 art 1886: if ((user = strdup(*argv++)) == NULL)
1887: err(1, "strdup");
1.1 deraadt 1888: --argc;
1889: continue;
1890: }
1.5 niklas 1891: if (strcmp(*argv, "-b") == 0) {
1892: --argc; ++argv;
1893: if (argc == 0)
1894: goto usage;
1895: aliasp = *argv++;
1896: --argc;
1897: continue;
1898: }
1.1 deraadt 1899: if (strcmp(*argv, "-a") == 0) {
1900: --argc; ++argv;
1901: autologin = 1;
1902: continue;
1903: }
1904: if (hostp == 0) {
1905: hostp = *argv++;
1906: --argc;
1907: continue;
1908: }
1909: if (portp == 0) {
1910: portp = *argv++;
1911: --argc;
1912: continue;
1913: }
1914: usage:
1.56 guenther 1915: printf("usage: %s [-a] [-b hostalias] [-l user] host-name [port]\r\n", cmd);
1.1 deraadt 1916: return 0;
1917: }
1918: if (hostp == 0)
1919: goto usage;
1920:
1921: if (hostp[0] == '@' || hostp[0] == '!') {
1922: if ((hostname = strrchr(hostp, ':')) == NULL)
1923: hostname = strrchr(hostp, '@');
1924: hostname++;
1925: srp = 0;
1926: temp = sourceroute(hostp, &srp, &srlen);
1927: if (temp == 0) {
1928: herror(srp);
1929: return 0;
1930: } else if (temp == -1) {
1.10 art 1931: printf("Bad source route option: %s\r\n", hostp);
1.1 deraadt 1932: return 0;
1933: } else {
1.10 art 1934: abort();
1.1 deraadt 1935: }
1.23 itojun 1936: } else
1937: {
1938: hostname = hostp;
1939: memset(&hints, 0, sizeof(hints));
1.46 otto 1940: hints.ai_family = family;
1.23 itojun 1941: hints.ai_socktype = SOCK_STREAM;
1942: hints.ai_flags = AI_CANONNAME;
1943: if (portp == NULL) {
1944: portp = "telnet";
1.38 itojun 1945: telnetport = 1;
1.23 itojun 1946: } else if (*portp == '-') {
1947: portp++;
1948: telnetport = 1;
1.38 itojun 1949: } else
1950: telnetport = 0;
1.28 millert 1951: h_errno = 0;
1.23 itojun 1952: error = getaddrinfo(hostp, portp, &hints, &res0);
1953: if (error) {
1.28 millert 1954: if (error == EAI_SERVICE)
1955: warnx("%s: bad port", portp);
1956: else
1957: warnx("%s: %s", hostp, gai_strerror(error));
1958: if (h_errno)
1959: herror(hostp);
1.23 itojun 1960: return 0;
1961: }
1.1 deraadt 1962: }
1.10 art 1963:
1.23 itojun 1964: net = -1;
1965: retry = 0;
1966: for (res = res0; res; res = res->ai_next) {
1967: if (1 /* retry */) {
1.31 itojun 1968: char hbuf[NI_MAXHOST];
1.23 itojun 1969:
1.31 itojun 1970: if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
1971: NULL, 0, niflags) != 0) {
1.43 hin 1972: strlcpy(hbuf, "(invalid)", sizeof(hbuf));
1.31 itojun 1973: }
1.23 itojun 1974: printf("Trying %s...\r\n", hbuf);
1975: }
1976: net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1977: if (net < 0)
1978: continue;
1.51 claudio 1979:
1.55 sthen 1980: if (rtableid >= 0 && (setsockopt(net, SOL_SOCKET, SO_RTABLE, &rtableid,
1981: sizeof(rtableid)) == -1))
1.54 phessler 1982: perror("setsockopt (SO_RTABLE)");
1.10 art 1983:
1.5 niklas 1984: if (aliasp) {
1.23 itojun 1985: struct addrinfo ahints, *ares;
1986:
1987: memset(&ahints, 0, sizeof(ahints));
1.46 otto 1988: ahints.ai_family = family;
1.23 itojun 1989: ahints.ai_socktype = SOCK_STREAM;
1990: ahints.ai_flags = AI_PASSIVE;
1991: error = getaddrinfo(aliasp, "0", &ahints, &ares);
1992: if (error) {
1993: warn("%s: %s", aliasp, gai_strerror(error));
1994: close(net);
1.49 otto 1995: net = -1;
1.23 itojun 1996: continue;
1.5 niklas 1997: }
1.32 itojun 1998: if (bind(net, ares->ai_addr, ares->ai_addrlen) < 0) {
1.31 itojun 1999: perror(aliasp);
2000: (void) close(net); /* dump descriptor */
1.49 otto 2001: net = -1;
1.23 itojun 2002: freeaddrinfo(ares);
2003: continue;
1.5 niklas 2004: }
1.23 itojun 2005: freeaddrinfo(ares);
2006: }
2007: if (srp && res->ai_family == AF_INET
1.56 guenther 2008: && setsockopt(net, IPPROTO_IP, IP_OPTIONS, srp, srlen) < 0)
1.1 deraadt 2009: perror("setsockopt (IP_OPTIONS)");
1.23 itojun 2010: if (res->ai_family == AF_INET) {
1.1 deraadt 2011: if (tos < 0)
1.3 niklas 2012: tos = IPTOS_LOWDELAY; /* Low Delay bit */
1.1 deraadt 2013: if (tos
1.56 guenther 2014: && (setsockopt(net, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1.1 deraadt 2015: && (errno != ENOPROTOOPT))
2016: perror("telnet: setsockopt (IP_TOS) (ignored)");
2017: }
2018:
1.56 guenther 2019: if (debug) {
2020: int one = 1;
2021:
2022: if (setsockopt(net, SOL_SOCKET, SO_DEBUG, &one,
2023: sizeof(one)) < 0)
2024: perror("setsockopt (SO_DEBUG)");
1.1 deraadt 2025: }
2026:
1.23 itojun 2027: if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
1.31 itojun 2028: char hbuf[NI_MAXHOST];
1.16 deraadt 2029:
1.31 itojun 2030: if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
2031: NULL, 0, niflags) != 0) {
1.43 hin 2032: strlcpy(hbuf, "(invalid)", sizeof(hbuf));
1.31 itojun 2033: }
1.25 itojun 2034: fprintf(stderr, "telnet: connect to address %s: %s\n", hbuf,
2035: strerror(errno));
1.23 itojun 2036:
2037: close(net);
2038: net = -1;
2039: retry++;
2040: continue;
1.1 deraadt 2041: }
1.23 itojun 2042:
1.1 deraadt 2043: connected++;
1.23 itojun 2044: break;
2045: }
2046: freeaddrinfo(res0);
2047: if (net < 0) {
2048: return 0;
2049: }
1.1 deraadt 2050: cmdrc(hostp, hostname);
2051: if (autologin && user == NULL) {
2052: struct passwd *pw;
2053:
1.58 guenther 2054: user = getlogin();
1.1 deraadt 2055: if (user == NULL ||
1.58 guenther 2056: (pw = getpwnam(user)) == NULL || pw->pw_uid != getuid()) {
1.15 art 2057: if ((pw = getpwuid(getuid())) != NULL)
1.1 deraadt 2058: user = pw->pw_name;
2059: else
2060: user = NULL;
2061: }
2062: }
2063: if (user) {
2064: env_define((unsigned char *)"USER", (unsigned char *)user);
2065: env_export((unsigned char *)"USER");
2066: }
1.68 guenther 2067: connection_status(1);
1.1 deraadt 2068: if (setjmp(peerdied) == 0)
2069: telnet(user);
1.67 guenther 2070: (void)close(net);
1.10 art 2071: ExitString("Connection closed by foreign host.\r\n",1);
1.1 deraadt 2072: }
2073:
2074: #define HELPINDENT (sizeof ("connect"))
2075:
2076: static char
2077: openhelp[] = "connect to a site",
2078: closehelp[] = "close current connection",
2079: logouthelp[] = "forcibly logout remote user and close the connection",
2080: quithelp[] = "exit telnet",
2081: statushelp[] = "print status information",
2082: helphelp[] = "print help information",
2083: sendhelp[] = "transmit special characters ('send ?' for more)",
2084: sethelp[] = "set operating parameters ('set ?' for more)",
2085: unsethelp[] = "unset operating parameters ('unset ?' for more)",
2086: togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2087: slchelp[] = "change state of special charaters ('slc ?' for more)",
2088: displayhelp[] = "display operating parameters",
2089: zhelp[] = "suspend telnet",
1.21 deraadt 2090: #ifdef SKEY
2091: skeyhelp[] = "compute response to s/key challenge",
2092: #endif
1.1 deraadt 2093: shellhelp[] = "invoke a subshell",
2094: envhelp[] = "change environment variables ('environ ?' for more)",
2095: modestring[] = "try to enter line or character mode ('mode ?' for more)";
2096:
1.40 millert 2097: static int help(int, char**);
1.1 deraadt 2098:
2099: static Command cmdtab[] = {
2100: { "close", closehelp, bye, 1 },
2101: { "logout", logouthelp, logout, 1 },
2102: { "display", displayhelp, display, 0 },
2103: { "mode", modestring, modecmd, 0 },
2104: { "open", openhelp, tn, 0 },
1.63 guenther 2105: { "quit", quithelp, quitcmd, 0 },
1.1 deraadt 2106: { "send", sendhelp, sendcmd, 0 },
2107: { "set", sethelp, setcmd, 0 },
2108: { "unset", unsethelp, unsetcmd, 0 },
2109: { "status", statushelp, status, 0 },
2110: { "toggle", togglestring, toggle, 0 },
2111: { "slc", slchelp, slccmd, 0 },
1.10 art 2112:
2113: { "z", zhelp, telnetsuspend, 0 },
1.1 deraadt 2114: { "!", shellhelp, shell, 0 },
2115: { "environ", envhelp, env_cmd, 0 },
2116: { "?", helphelp, help, 0 },
1.68 guenther 2117: #ifdef SKEY
1.21 deraadt 2118: { "skey", skeyhelp, skey_calc, 0 },
1.6 deraadt 2119: #endif
1.10 art 2120: { 0, 0, 0, 0 }
1.1 deraadt 2121: };
2122:
2123: static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2124: static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2125:
2126: static Command cmdtab2[] = {
2127: { "help", 0, help, 0 },
2128: { "escape", escapehelp, setescape, 0 },
2129: { "crmod", crmodhelp, togcrmod, 0 },
1.10 art 2130: { 0, 0, 0, 0 }
1.1 deraadt 2131: };
2132:
2133:
1.69 ! jsg 2134: static Command *
! 2135: getcmd(char *name)
1.1 deraadt 2136: {
2137: Command *cm;
2138:
1.10 art 2139: if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
1.1 deraadt 2140: return cm;
2141: return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2142: }
2143:
1.69 ! jsg 2144: void
! 2145: command(int top, char *tbuf, int cnt)
1.1 deraadt 2146: {
1.39 mpech 2147: Command *c;
1.1 deraadt 2148:
2149: setcommandmode();
2150: if (!top) {
2151: putchar('\n');
2152: } else {
2153: (void) signal(SIGINT, SIG_DFL);
2154: (void) signal(SIGQUIT, SIG_DFL);
2155: }
2156: for (;;) {
2157: if (rlogin == _POSIX_VDISABLE)
2158: printf("%s> ", prompt);
2159: if (tbuf) {
1.39 mpech 2160: char *cp;
1.1 deraadt 2161: cp = line;
2162: while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2163: cnt--;
2164: tbuf = 0;
2165: if (cp == line || *--cp != '\n' || cp == line)
2166: goto getline;
2167: *cp = '\0';
2168: if (rlogin == _POSIX_VDISABLE)
1.10 art 2169: printf("%s\r\n", line);
1.1 deraadt 2170: } else {
2171: getline:
2172: if (rlogin != _POSIX_VDISABLE)
2173: printf("%s> ", prompt);
2174: if (fgets(line, sizeof(line), stdin) == NULL) {
1.66 guenther 2175: if (feof(stdin) || ferror(stdin))
1.63 guenther 2176: quit();
1.1 deraadt 2177: break;
2178: }
2179: }
2180: if (line[0] == 0)
2181: break;
2182: makeargv();
2183: if (margv[0] == 0) {
2184: break;
2185: }
2186: c = getcmd(margv[0]);
2187: if (Ambiguous(c)) {
1.10 art 2188: printf("?Ambiguous command\r\n");
1.1 deraadt 2189: continue;
2190: }
2191: if (c == 0) {
1.10 art 2192: printf("?Invalid command\r\n");
1.1 deraadt 2193: continue;
2194: }
2195: if (c->needconnect && !connected) {
1.10 art 2196: printf("?Need to be connected first.\r\n");
1.1 deraadt 2197: continue;
2198: }
2199: if ((*c->handler)(margc, margv)) {
2200: break;
2201: }
2202: }
2203: if (!top) {
1.66 guenther 2204: if (!connected)
1.1 deraadt 2205: longjmp(toplevel, 1);
2206: setconnmode(0);
2207: }
2208: }
2209:
2210: /*
2211: * Help command.
2212: */
1.69 ! jsg 2213: static int
! 2214: help(int argc, char *argv[])
1.1 deraadt 2215: {
1.39 mpech 2216: Command *c;
1.1 deraadt 2217:
2218: if (argc == 1) {
1.10 art 2219: printf("Commands may be abbreviated. Commands are:\r\n\r\n");
1.1 deraadt 2220: for (c = cmdtab; c->name; c++)
2221: if (c->help) {
1.37 deraadt 2222: printf("%-*s\t%s\r\n", (int)HELPINDENT, c->name,
1.1 deraadt 2223: c->help);
2224: }
2225: return 0;
2226: }
2227: while (--argc > 0) {
1.39 mpech 2228: char *arg;
1.1 deraadt 2229: arg = *++argv;
2230: c = getcmd(arg);
2231: if (Ambiguous(c))
1.10 art 2232: printf("?Ambiguous help command %s\r\n", arg);
1.1 deraadt 2233: else if (c == (Command *)0)
1.10 art 2234: printf("?Invalid help command %s\r\n", arg);
1.1 deraadt 2235: else
1.10 art 2236: printf("%s\r\n", c->help);
1.1 deraadt 2237: }
2238: return 0;
2239: }
2240:
2241: /*
2242: * Source route is handed in as
2243: * [!]@hop1@hop2...[@|:]dst
2244: * If the leading ! is present, it is a
2245: * strict source route, otherwise it is
2246: * assmed to be a loose source route.
2247: *
2248: * We fill in the source route option as
2249: * hop1,hop2,hop3...dest
2250: * and return a pointer to hop1, which will
2251: * be the address to connect() to.
2252: *
2253: * Arguments:
2254: * arg: pointer to route list to decipher
2255: *
2256: * cpp: If *cpp is not equal to NULL, this is a
2257: * pointer to a pointer to a character array
2258: * that should be filled in with the option.
2259: *
2260: * lenp: pointer to an integer that contains the
2261: * length of *cpp if *cpp != NULL.
2262: *
2263: * Return values:
2264: *
2265: * Returns the address of the host to connect to. If the
2266: * return value is -1, there was a syntax error in the
2267: * option, either unknown characters, or too many hosts.
2268: * If the return value is 0, one of the hostnames in the
2269: * path is unknown, and *cpp is set to point to the bad
2270: * hostname.
2271: *
2272: * *cpp: If *cpp was equal to NULL, it will be filled
2273: * in with a pointer to our static area that has
2274: * the option filled in. This will be 32bit aligned.
1.3 niklas 2275: *
1.1 deraadt 2276: * *lenp: This will be filled in with how long the option
2277: * pointed to by *cpp is.
1.3 niklas 2278: *
1.1 deraadt 2279: */
1.67 guenther 2280:
2281: static unsigned long
1.69 ! jsg 2282: sourceroute(char *arg, char **cpp, int *lenp)
1.1 deraadt 2283: {
2284: static char lsr[44];
2285: char *cp, *cp2, *lsrp, *lsrep;
1.65 guenther 2286: struct in_addr addr;
1.39 mpech 2287: struct hostent *host = 0;
2288: char c;
1.1 deraadt 2289:
2290: /*
2291: * Verify the arguments, and make sure we have
2292: * at least 7 bytes for the option.
2293: */
2294: if (cpp == NULL || lenp == NULL)
2295: return((unsigned long)-1);
2296: if (*cpp != NULL && *lenp < 7)
2297: return((unsigned long)-1);
2298: /*
2299: * Decide whether we have a buffer passed to us,
2300: * or if we need to use our own static buffer.
2301: */
2302: if (*cpp) {
2303: lsrp = *cpp;
2304: lsrep = lsrp + *lenp;
2305: } else {
2306: *cpp = lsrp = lsr;
2307: lsrep = lsrp + 44;
2308: }
2309:
2310: cp = arg;
2311:
2312: /*
2313: * Next, decide whether we have a loose source
2314: * route or a strict source route, and fill in
2315: * the begining of the option.
2316: */
2317: if (*cp == '!') {
2318: cp++;
2319: *lsrp++ = IPOPT_SSRR;
2320: } else
2321: *lsrp++ = IPOPT_LSRR;
2322:
2323: if (*cp != '@')
2324: return((unsigned long)-1);
2325:
2326: lsrp++; /* skip over length, we'll fill it in later */
2327: *lsrp++ = 4;
2328:
2329: cp++;
2330:
1.65 guenther 2331: addr.s_addr = 0;
1.1 deraadt 2332:
2333: for (c = 0;;) {
2334: if (c == ':')
2335: cp2 = 0;
1.10 art 2336: else for (cp2 = cp; (c = *cp2); cp2++) {
1.1 deraadt 2337: if (c == ',') {
2338: *cp2++ = '\0';
2339: if (*cp2 == '@')
2340: cp2++;
2341: } else if (c == '@') {
2342: *cp2++ = '\0';
2343: } else if (c == ':') {
2344: *cp2++ = '\0';
2345: } else
2346: continue;
2347: break;
2348: }
2349: if (!c)
2350: cp2 = 0;
2351:
1.65 guenther 2352: if ((addr.s_addr = inet_addr(cp)) == INADDR_NONE) {
2353: if ((host = gethostbyname(cp)) == NULL) {
2354: *cpp = cp;
2355: return(0);
2356: }
2357: memcpy(&addr, host->h_addr_list[0], sizeof addr);
1.1 deraadt 2358: }
1.65 guenther 2359: memcpy(lsrp, &addr, 4);
1.1 deraadt 2360: lsrp += 4;
2361: if (cp2)
2362: cp = cp2;
2363: else
2364: break;
2365: /*
2366: * Check to make sure there is space for next address
2367: */
2368: if (lsrp + 4 > lsrep)
2369: return((unsigned long)-1);
2370: }
2371: if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2372: *cpp = 0;
2373: *lenp = 0;
2374: return((unsigned long)-1);
2375: }
2376: *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2377: *lenp = lsrp - *cpp;
1.65 guenther 2378: return(addr.s_addr);
1.1 deraadt 2379: }