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