1 /* $OpenBSD: cmds.c,v 1.26 2006/06/06 23:24:52 deraadt Exp $ */
2 /* $NetBSD: cmds.c,v 1.7 1997/02/11 09:24:03 mrg Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1983, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
41 static const char rcsid[] = "$OpenBSD: cmds.c,v 1.26 2006/06/06 23:24:52 deraadt Exp $";
46 #include "pathnames.h"
53 * miscellaneous commands
56 int quant[] = { 60, 60, 24 };
59 char *sep[] = { "second", "minute", "hour" };
60 static char *argv[10]; /* argument vector for take and put */
62 static void transfer(char *, int, char *);
63 static void stopsnd(int); /* SIGINT handler during file transfers */
64 static void intcopy(int); /* interrupt routine for file transfers */
65 static void transmit(FILE *, char *, char *);
66 static void send(int);
67 static void execute(char *);
68 static int args(char *, char **, int);
69 static void prtime(char *, time_t);
70 static void tandem(char *);
71 static void hardwareflow(char *);
72 void linedisc(char *);
73 static int anyof(char *, char *);
76 * FTP - remote ==> local
77 * get a file from the remote host
86 * get the UNIX receiving file's name
88 if (prompt("Local file name? ", copyname, sizeof(copyname)))
90 cp = expand(copyname);
91 if ((sfd = creat(cp, 0666)) < 0) {
92 printf("\r\n%s: cannot creat\r\n", copyname);
99 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
103 transfer(buf, sfd, value(EOFREAD));
107 * Cu-like take command
113 char line[BUFSIZ], *cp;
115 if (prompt("[take] ", copyname, sizeof(copyname)))
117 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 ||
119 printf("usage: <take> from [to]\r\n");
124 cp = expand(argv[1]);
125 if ((fd = creat(cp, 0666)) < 0) {
126 printf("\r\n%s: cannot create\r\n", argv[1]);
129 (void)snprintf(line, sizeof(line), "cat %s;echo ''|tr '\\012' '\\01'", argv[0]);
130 transfer(line, fd, "\01");
133 static jmp_buf intbuf;
136 * Bulk transfer routine --
137 * used by getfl(), cu_take(), and pipefile()
140 transfer(char *buf, int fd, char *eofchars)
143 char c, buffer[BUFSIZ];
150 if (number(value(FRAMESIZE)) > BUFSIZ || number(value(FRAMESIZE)) < 1) {
151 printf("framesize must be >= 1 and <= %d\r\n", BUFSIZ);
156 parwrite(FD, buf, size(buf));
158 kill(tipout_pid, SIGIOT);
159 read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
168 while ((c&STRIP_PAR) != '\n');
169 tcsetattr(0, TCSAFLUSH, &defchars);
171 (void) setjmp(intbuf);
172 f = signal(SIGINT, intcopy);
174 for (ct = 0; !quit;) {
175 eof = read(FD, &c, 1) <= 0;
179 if (eof || any(c, eofchars))
182 continue; /* ignore nulls */
187 if (c == '\n' && boolean(value(VERBOSE)))
188 printf("\r%d", ++ct);
189 if ((cnt = (p-buffer)) == (size_t)number(value(FRAMESIZE))) {
190 if ((size_t)write(fd, buffer, cnt) != cnt) {
191 printf("\r\nwrite error\r\n");
197 if ((cnt = (p-buffer)))
198 if ((size_t)write(fd, buffer, cnt) != cnt)
199 printf("\r\nwrite error\r\n");
201 if (boolean(value(VERBOSE)))
202 prtime(" lines transferred in ", time(0)-start);
203 tcsetattr(0, TCSAFLUSH, &term);
204 write(fildes[1], (char *)&ccc, 1);
210 * FTP - remote ==> local process
211 * send remote input to local process via pipe
222 if (prompt("Local command? ", buf, sizeof(buf)))
226 printf("can't establish pipe\r\n");
230 if ((cpid = fork()) < 0) {
231 printf("can't fork!\r\n");
234 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
235 close(pdes[0]), close(pdes[1]);
236 kill (cpid, SIGKILL);
239 signal(SIGPIPE, intcopy);
240 transfer(buf, pdes[1], value(EOFREAD));
241 signal(SIGPIPE, SIG_DFL);
242 while ((p = wait(&status)) > 0 && p != cpid)
250 for (f = 3; f < 20; f++)
253 printf("can't execl!\r\n");
259 * Interrupt service routine for FTP
266 signal(SIGINT, SIG_IGN);
270 * FTP - local ==> remote
271 * send local file to remote host
272 * terminate transmission with pseudo EOF sequence
284 if (prompt("Local file name? ", fname, sizeof(fname)))
290 fnamex = expand(fname);
291 if ((fp = fopen(fnamex, "r")) == NULL) {
292 printf("%s: cannot open\r\n", fname);
295 transmit(fp, value(EOFWRITE), NULL);
296 if (!boolean(value(ECHOCHECK)))
301 * Bulk transfer routine to remote host --
302 * used by sendfile() and cu_put()
305 transmit(FILE *fp, char *eofchars, char *command)
308 int c, ccount, lcount;
309 time_t start_t, stop_t;
312 kill(tipout_pid, SIGIOT); /* put TIPOUT into a wait state */
314 f = signal(SIGINT, stopsnd);
315 tcsetattr(0, TCSAFLUSH, &defchars);
316 read(repdes[0], (char *)&ccc, 1);
317 if (command != NULL) {
318 for (pc = command; *pc; pc++)
320 if (boolean(value(ECHOCHECK)))
321 read(FD, (char *)&c, 1); /* trailing \n */
324 sleep(5); /* wait for remote stty to take effect */
338 if (c == 0177 && !boolean(value(RAWFTP)))
343 if (!boolean(value(RAWFTP)))
345 } else if (c == '\t') {
346 if (!boolean(value(RAWFTP))) {
347 if (boolean(value(TABEXPAND))) {
349 while ((++ccount % 8) != 0)
355 if (!boolean(value(RAWFTP)))
359 } while (c != '\r' && !boolean(value(RAWFTP)));
360 if (boolean(value(VERBOSE)))
361 printf("\r%d", ++lcount);
362 if (boolean(value(ECHOCHECK))) {
364 alarm((unsigned int)lvalue(ETIMEOUT));
365 do { /* wait for prompt */
366 read(FD, (char *)&c, 1);
367 if (timedout || stop) {
369 printf("\r\ntimed out at eol\r\n");
373 } while ((c&STRIP_PAR) != character(value(PROMPT)));
378 if (lastc != '\n' && !boolean(value(RAWFTP)))
381 for (pc = eofchars; *pc; pc++)
387 if (boolean(value(VERBOSE))) {
388 if (boolean(value(RAWFTP)))
389 prtime(" chars transferred in ", stop_t-start_t);
391 prtime(" lines transferred in ", stop_t-start_t);
393 write(fildes[1], (char *)&ccc, 1);
394 tcsetattr(0, TCSAFLUSH, &term);
398 * Cu-like put command
409 if (prompt("[put] ", copyname, sizeof(copyname)))
411 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 ||
413 printf("usage: <put> from [to]\r\n");
418 copynamex = expand(argv[0]);
419 if ((fp = fopen(copynamex, "r")) == NULL) {
420 printf("%s: cannot open\r\n", copynamex);
423 if (boolean(value(ECHOCHECK)))
424 (void)snprintf(line, sizeof(line), "cat>%s\r", argv[1]);
426 (void)snprintf(line, sizeof(line),
427 "stty -echo;cat>%s;stty echo\r", argv[1]);
428 transmit(fp, "\04", line);
432 * FTP - send single character
433 * wait for echo & handle timeout
442 parwrite(FD, &cc, 1);
443 if (number(value(CDELAY)) > 0 && c != '\r')
444 usleep(number(value(CDELAY)));
445 if (!boolean(value(ECHOCHECK))) {
446 if (number(value(LDELAY)) > 0 && c == '\r')
447 usleep(number(value(LDELAY)));
452 alarm((unsigned int)lvalue(ETIMEOUT));
456 printf("\r\ntimeout error (%s)\r\n", ctrl(c));
459 parwrite(FD, &null, 1); /* poke it */
468 signal(SIGALRM, timeout);
473 * Stolen from consh() -- puts a remote file on the output of a local command.
474 * Identical to consh() except for where stdout goes.
482 time_t start = time(NULL);
485 if (prompt("Local command? ", buf, sizeof(buf)))
487 kill(tipout_pid, SIGIOT); /* put TIPOUT into a wait state */
488 signal(SIGINT, SIG_IGN);
489 signal(SIGQUIT, SIG_IGN);
490 tcsetattr(0, TCSAFLUSH, &defchars);
491 read(repdes[0], (char *)&ccc, 1);
493 * Set up file descriptors in the child and
496 if ((cpid = fork()) < 0)
497 printf("can't fork!\r\n");
500 while ((p = wait(&status)) > 0 && p != cpid)
506 for (i = 3; i < 20; i++)
508 signal(SIGINT, SIG_DFL);
509 signal(SIGQUIT, SIG_DFL);
511 printf("can't find `%s'\r\n", buf);
514 if (boolean(value(VERBOSE)))
515 prtime("away for ", time(0)-start);
516 write(fildes[1], (char *)&ccc, 1);
517 tcsetattr(0, TCSAFLUSH, &term);
518 signal(SIGINT, SIG_DFL);
519 signal(SIGQUIT, SIG_DFL);
524 * Fork a program with:
525 * 0 <-> remote tty in
526 * 1 <-> remote tty out
527 * 2 <-> local tty stderr
535 time_t start = time(NULL);
538 if (prompt("Local command? ", buf, sizeof(buf)))
540 kill(tipout_pid, SIGIOT); /* put TIPOUT into a wait state */
541 signal(SIGINT, SIG_IGN);
542 signal(SIGQUIT, SIG_IGN);
543 tcsetattr(0, TCSAFLUSH, &defchars);
544 read(repdes[0], (char *)&ccc, 1);
546 * Set up file descriptors in the child and
549 if ((cpid = fork()) < 0)
550 printf("can't fork!\r\n");
553 while ((p = wait(&status)) > 0 && p != cpid)
560 for (i = 3; i < 20; i++)
562 signal(SIGINT, SIG_DFL);
563 signal(SIGQUIT, SIG_DFL);
565 printf("can't find `%s'\r\n", buf);
568 if (boolean(value(VERBOSE)))
569 prtime("away for ", time(0)-start);
570 write(fildes[1], (char *)&ccc, 1);
571 tcsetattr(0, TCSAFLUSH, &term);
572 signal(SIGINT, SIG_DFL);
573 signal(SIGQUIT, SIG_DFL);
578 * Escape to local shell
589 signal(SIGINT, SIG_IGN);
590 signal(SIGQUIT, SIG_IGN);
592 if ((shpid = fork())) {
593 while (shpid != wait(&status));
596 signal(SIGINT, SIG_DFL);
597 signal(SIGQUIT, SIG_DFL);
600 signal(SIGQUIT, SIG_DFL);
601 signal(SIGINT, SIG_DFL);
602 if ((cp = strrchr(value(SHELL), '/')) == NULL)
607 execl(value(SHELL), cp, (char *)NULL);
608 printf("\r\ncan't execl!\r\n");
614 * TIPIN portion of scripting
615 * initiate the conversation with TIPOUT
623 * enable TIPOUT side for dialogue
625 kill(tipout_pid, SIGEMT);
626 if (boolean(value(SCRIPT)))
627 write(fildes[1], value(RECORD), size(value(RECORD)));
628 write(fildes[1], "\n", 1);
630 * wait for TIPOUT to finish
632 read(repdes[0], &c, 1);
634 printf("can't create %s\r\n", value(RECORD));
638 * Change current working directory of
639 * local portion of tip
645 char dirname[PATH_MAX];
648 if (prompt("[cd] ", dirname, sizeof(dirname))) {
654 printf("%s: bad directory\r\n", cp);
662 signal(SIGTERM, SIG_IGN);
663 kill(tipout_pid, SIGTERM);
666 printf("\r\n%s", msg);
667 printf("\r\n[EOT]\r\n");
669 (void)uu_unlock(uucplock);
681 if ((dismsg = value(DISCONNECT)) != NOSTR) {
682 write(FD, dismsg, strlen(dismsg));
702 if ((cp = strrchr(value(SHELL), '/')) == NULL)
707 execl(value(SHELL), cp, "-c", s, (char *)NULL);
711 args(char *buf, char *a[], int num)
713 char *p = buf, *start;
718 while (*p && (*p == ' ' || *p == '\t'))
723 while (*p && (*p != ' ' && *p != '\t'))
729 } while (*p && n < num);
735 prtime(char *s, time_t a)
740 for (i = 0; i < 3; i++) {
741 nums[i] = (int)(a % quant[i]);
746 if (nums[i] || (i == 0 && nums[1] == 0 && nums[2] == 0))
747 printf("%d %s%c ", nums[i], sep[i],
748 nums[i] == 1 ? '\0' : 's');
758 if (prompt("[set] ", buf, sizeof(buf)))
761 if (vtable[BEAUTIFY].v_access&CHANGED) {
762 vtable[BEAUTIFY].v_access &= ~CHANGED;
763 kill(tipout_pid, SIGSYS);
765 if (vtable[SCRIPT].v_access&CHANGED) {
766 vtable[SCRIPT].v_access &= ~CHANGED;
769 * So that "set record=blah script" doesn't
770 * cause two transactions to occur.
772 if (vtable[RECORD].v_access&CHANGED)
773 vtable[RECORD].v_access &= ~CHANGED;
775 if (vtable[RECORD].v_access&CHANGED) {
776 vtable[RECORD].v_access &= ~CHANGED;
777 if (boolean(value(SCRIPT)))
780 if (vtable[TAND].v_access&CHANGED) {
781 vtable[TAND].v_access &= ~CHANGED;
782 if (boolean(value(TAND)))
787 if (vtable[LECHO].v_access&CHANGED) {
788 vtable[LECHO].v_access &= ~CHANGED;
789 HD = boolean(value(LECHO));
791 if (vtable[PARITY].v_access&CHANGED) {
792 vtable[PARITY].v_access &= ~CHANGED;
795 if (vtable[HARDWAREFLOW].v_access&CHANGED) {
796 vtable[HARDWAREFLOW].v_access &= ~CHANGED;
797 if (boolean(value(HARDWAREFLOW)))
802 if (vtable[LINEDISC].v_access&CHANGED) {
803 vtable[LINEDISC].v_access &= ~CHANGED;
814 char charbuf[5]; /* for vis(3), 4 chars for encoding, plus nul */
817 for (p = vtable; p->v_name; p++) {
818 fputs(p->v_name, stdout);
819 switch (p->v_type&TMASK) {
822 buf = malloc(4*strlen(p->v_value) + 1);
824 fprintf(stderr, "Unable to malloc()\n");
827 strvis(buf, p->v_value, VIS_WHITE);
835 printf(" %ld\r\n", number(p->v_value));
839 !boolean(p->v_value) ? "false" : "true");
842 vis(charbuf, character(p->v_value), VIS_WHITE, 0);
843 printf(" %s\r\n", charbuf);
850 * Turn tandem mode on or off for remote tty.
855 struct termios rmtty;
857 tcgetattr(FD, &rmtty);
858 if (strcmp(option, "on") == 0) {
859 rmtty.c_iflag |= IXOFF;
860 term.c_iflag |= IXOFF;
862 rmtty.c_iflag &= ~IXOFF;
863 term.c_iflag &= ~IXOFF;
865 tcsetattr(FD, TCSADRAIN, &rmtty);
866 tcsetattr(0, TCSADRAIN, &term);
870 * Turn hardware flow control on or off for remote tty.
873 hardwareflow(char *option)
875 struct termios rmtty;
877 tcgetattr(FD, &rmtty);
878 if (strcmp(option, "on") == 0)
879 rmtty.c_iflag |= CRTSCTS;
881 rmtty.c_iflag &= ~CRTSCTS;
882 tcsetattr(FD, TCSADRAIN, &rmtty);
886 * Change line discipline to the specified one.
889 linedisc(char *option)
891 int ld = (int)(intptr_t)value(LINEDISC);
893 ioctl(FD, TIOCSETD, &ld);
903 ioctl(FD, TIOCSBRK, NULL);
905 ioctl(FD, TIOCCBRK, NULL);
915 kill(c == CTRL('y') ? getpid() : 0, SIGTSTP);
920 * expand a file name if it includes shell meta characters
925 static char xname[BUFSIZ];
932 if (!anyof(name, "~{[*?$`'\"\\"))
934 /* sigint = signal(SIGINT, SIG_IGN); */
935 if (pipe(pivec) < 0) {
937 /* signal(SIGINT, sigint) */
940 (void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
941 if ((pid = vfork()) == 0) {
942 Shell = value(SHELL);
944 Shell = _PATH_BSHELL;
951 execl(Shell, Shell, "-c", cmdbuf, (char *)NULL);
961 l = read(pivec[0], xname, BUFSIZ);
963 while (wait(&s) != pid);
966 if (s != 0 && s != SIGPIPE) {
967 fprintf(stderr, "\"Echo\" failed\n");
975 fprintf(stderr, "\"%s\": No match\n", name);
979 fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
983 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
990 * Are any of the characters in the two strings the same?
993 anyof(char *s1, char *s2)