netinet{,6}: Assert in{,6}_inithead() are only used for system routing tables.
[dragonfly.git] / contrib / tnftp / cmds.c
CommitLineData
a83a5d06 1/* $NetBSD: cmds.c,v 1.135 2012/12/22 16:57:09 christos Exp $ */
eedc536d
PA
2
3/*-
20e59a13 4 * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
eedc536d
PA
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12 * NASA Ames Research Center.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
eedc536d
PA
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/*
37 * Copyright (c) 1985, 1989, 1993, 1994
38 * The Regents of the University of California. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65/*
66 * Copyright (C) 1997 and 1998 WIDE Project.
67 * All rights reserved.
68 *
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
71 * are met:
72 * 1. Redistributions of source code must retain the above copyright
73 * notice, this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright
75 * notice, this list of conditions and the following disclaimer in the
76 * documentation and/or other materials provided with the distribution.
77 * 3. Neither the name of the project nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
80 *
81 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * SUCH DAMAGE.
92 */
93
94#include <sys/cdefs.h>
95#ifndef lint
96#if 0
97static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
98#else
a83a5d06 99__RCSID("$NetBSD: cmds.c,v 1.135 2012/12/22 16:57:09 christos Exp $");
eedc536d
PA
100#endif
101#endif /* not lint */
102
103/*
104 * FTP User Program -- Command Routines.
105 */
106#include <sys/types.h>
107#include <sys/socket.h>
108#include <sys/stat.h>
109#include <sys/wait.h>
110#include <arpa/ftp.h>
111
112#include <ctype.h>
113#include <err.h>
d2b51479 114#include <errno.h>
eedc536d
PA
115#include <glob.h>
116#include <limits.h>
117#include <netdb.h>
118#include <paths.h>
20e59a13 119#include <stddef.h>
eedc536d 120#include <stdio.h>
3aa3fd83 121#include <libutil.h>
eedc536d
PA
122#include <stdlib.h>
123#include <string.h>
124#include <time.h>
125#include <unistd.h>
126
127#include "ftp_var.h"
128#include "version.h"
129
0511850f 130static struct types {
20e59a13
PA
131 const char *t_name;
132 const char *t_mode;
133 int t_type;
134 const char *t_arg;
eedc536d
PA
135} types[] = {
136 { "ascii", "A", TYPE_A, 0 },
137 { "binary", "I", TYPE_I, 0 },
138 { "image", "I", TYPE_I, 0 },
139 { "ebcdic", "E", TYPE_E, 0 },
140 { "tenex", "L", TYPE_L, bytename },
20e59a13 141 { NULL, NULL, 0, NULL }
eedc536d
PA
142};
143
0511850f 144static sigjmp_buf jabort;
eedc536d
PA
145
146static int confirm(const char *, const char *);
c81ae005 147__dead static void mintr(int);
0511850f 148static void mabort(const char *);
20e59a13 149static void set_type(const char *);
eedc536d
PA
150
151static const char *doprocess(char *, size_t, const char *, int, int, int);
152static const char *domap(char *, size_t, const char *);
153static const char *docase(char *, size_t, const char *);
154static const char *dotrans(char *, size_t, const char *);
155
0511850f
PA
156/*
157 * Confirm if "cmd" is to be performed upon "file".
158 * If "file" is NULL, generate a "Continue with" prompt instead.
159 */
eedc536d
PA
160static int
161confirm(const char *cmd, const char *file)
162{
163 const char *errormsg;
20e59a13 164 char cline[BUFSIZ];
0511850f 165 const char *promptleft, *promptright;
eedc536d
PA
166
167 if (!interactive || confirmrest)
168 return (1);
0511850f
PA
169 if (file == NULL) {
170 promptleft = "Continue with";
171 promptright = cmd;
172 } else {
173 promptleft = cmd;
174 promptright = file;
175 }
eedc536d 176 while (1) {
0511850f 177 fprintf(ttyout, "%s %s [anpqy?]? ", promptleft, promptright);
eedc536d 178 (void)fflush(ttyout);
20e59a13 179 if (get_line(stdin, cline, sizeof(cline), &errormsg) < 0) {
eedc536d 180 mflag = 0;
0511850f 181 fprintf(ttyout, "%s; %s aborted\n", errormsg, cmd);
eedc536d
PA
182 return (0);
183 }
20e59a13 184 switch (tolower((unsigned char)*cline)) {
eedc536d
PA
185 case 'a':
186 confirmrest = 1;
187 fprintf(ttyout,
188 "Prompting off for duration of %s.\n", cmd);
189 break;
190 case 'p':
191 interactive = 0;
192 fputs("Interactive mode: off.\n", ttyout);
193 break;
194 case 'q':
195 mflag = 0;
0511850f 196 fprintf(ttyout, "%s aborted.\n", cmd);
eedc536d
PA
197 /* FALLTHROUGH */
198 case 'n':
199 return (0);
200 case '?':
201 fprintf(ttyout,
202 " confirmation options:\n"
203 "\ta answer `yes' for the duration of %s\n"
204 "\tn answer `no' for this file\n"
205 "\tp turn off `prompt' mode\n"
206 "\tq stop the current %s\n"
207 "\ty answer `yes' for this file\n"
208 "\t? this help list\n",
209 cmd, cmd);
210 continue; /* back to while(1) */
211 }
212 return (1);
213 }
214 /* NOTREACHED */
215}
216
217/*
218 * Set transfer type.
219 */
220void
221settype(int argc, char *argv[])
222{
223 struct types *p;
eedc536d
PA
224
225 if (argc == 0 || argc > 2) {
20e59a13 226 const char *sep;
eedc536d
PA
227
228 UPRINTF("usage: %s [", argv[0]);
229 sep = " ";
230 for (p = types; p->t_name; p++) {
231 fprintf(ttyout, "%s%s", sep, p->t_name);
232 sep = " | ";
233 }
234 fputs(" ]\n", ttyout);
235 code = -1;
236 return;
237 }
238 if (argc < 2) {
239 fprintf(ttyout, "Using %s mode to transfer files.\n", typename);
240 code = 0;
241 return;
242 }
20e59a13
PA
243 set_type(argv[1]);
244}
245
246void
247set_type(const char *ttype)
248{
249 struct types *p;
250 int comret;
251
eedc536d 252 for (p = types; p->t_name; p++)
20e59a13 253 if (strcmp(ttype, p->t_name) == 0)
eedc536d
PA
254 break;
255 if (p->t_name == 0) {
20e59a13 256 fprintf(ttyout, "%s: unknown mode.\n", ttype);
eedc536d
PA
257 code = -1;
258 return;
259 }
260 if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
261 comret = command("TYPE %s %s", p->t_mode, p->t_arg);
262 else
263 comret = command("TYPE %s", p->t_mode);
264 if (comret == COMPLETE) {
265 (void)strlcpy(typename, p->t_name, sizeof(typename));
266 curtype = type = p->t_type;
267 }
268}
269
270/*
271 * Internal form of settype; changes current type in use with server
272 * without changing our notion of the type for data transfers.
273 * Used to change to and from ascii for listings.
274 */
275void
276changetype(int newtype, int show)
277{
278 struct types *p;
279 int comret, oldverbose = verbose;
280
281 if (newtype == 0)
282 newtype = TYPE_I;
283 if (newtype == curtype)
284 return;
285 if (ftp_debug == 0 && show == 0)
286 verbose = 0;
287 for (p = types; p->t_name; p++)
288 if (newtype == p->t_type)
289 break;
290 if (p->t_name == 0) {
0511850f 291 errx(1, "changetype: unknown type %d", newtype);
eedc536d
PA
292 }
293 if (newtype == TYPE_L && bytename[0] != '\0')
294 comret = command("TYPE %s %s", p->t_mode, bytename);
295 else
296 comret = command("TYPE %s", p->t_mode);
297 if (comret == COMPLETE)
298 curtype = newtype;
299 verbose = oldverbose;
300}
301
eedc536d
PA
302/*
303 * Set binary transfer type.
304 */
305/*VARARGS*/
306void
307setbinary(int argc, char *argv[])
308{
309
310 if (argc == 0) {
311 UPRINTF("usage: %s\n", argv[0]);
312 code = -1;
313 return;
314 }
20e59a13 315 set_type("binary");
eedc536d
PA
316}
317
318/*
319 * Set ascii transfer type.
320 */
321/*VARARGS*/
322void
323setascii(int argc, char *argv[])
324{
325
326 if (argc == 0) {
327 UPRINTF("usage: %s\n", argv[0]);
328 code = -1;
329 return;
330 }
20e59a13 331 set_type("ascii");
eedc536d
PA
332}
333
334/*
335 * Set tenex transfer type.
336 */
337/*VARARGS*/
338void
339settenex(int argc, char *argv[])
340{
341
342 if (argc == 0) {
343 UPRINTF("usage: %s\n", argv[0]);
344 code = -1;
345 return;
346 }
20e59a13 347 set_type("tenex");
eedc536d
PA
348}
349
350/*
351 * Set file transfer mode.
352 */
353/*ARGSUSED*/
354void
355setftmode(int argc, char *argv[])
356{
357
358 if (argc != 2) {
359 UPRINTF("usage: %s mode-name\n", argv[0]);
360 code = -1;
361 return;
362 }
363 fprintf(ttyout, "We only support %s mode, sorry.\n", modename);
364 code = -1;
365}
366
367/*
368 * Set file transfer format.
369 */
370/*ARGSUSED*/
371void
372setform(int argc, char *argv[])
373{
374
375 if (argc != 2) {
376 UPRINTF("usage: %s format\n", argv[0]);
377 code = -1;
378 return;
379 }
380 fprintf(ttyout, "We only support %s format, sorry.\n", formname);
381 code = -1;
382}
383
384/*
385 * Set file transfer structure.
386 */
387/*ARGSUSED*/
388void
389setstruct(int argc, char *argv[])
390{
391
392 if (argc != 2) {
393 UPRINTF("usage: %s struct-mode\n", argv[0]);
394 code = -1;
395 return;
396 }
397 fprintf(ttyout, "We only support %s structure, sorry.\n", structname);
398 code = -1;
399}
400
401/*
402 * Send a single file.
403 */
404void
405put(int argc, char *argv[])
406{
407 char buf[MAXPATHLEN];
20e59a13 408 const char *cmd;
eedc536d
PA
409 int loc = 0;
410 char *locfile;
411 const char *remfile;
412
413 if (argc == 2) {
414 argc++;
415 argv[2] = argv[1];
416 loc++;
417 }
418 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "local-file")))
419 goto usage;
420 if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) {
421 usage:
422 UPRINTF("usage: %s local-file [remote-file]\n", argv[0]);
423 code = -1;
424 return;
425 }
426 if ((locfile = globulize(argv[1])) == NULL) {
427 code = -1;
428 return;
429 }
430 remfile = argv[2];
431 if (loc) /* If argv[2] is a copy of the old argv[1], update it */
432 remfile = locfile;
433 cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
434 remfile = doprocess(buf, sizeof(buf), remfile,
435 0, loc && ntflag, loc && mapflag);
436 sendrequest(cmd, locfile, remfile,
437 locfile != argv[1] || remfile != argv[2]);
438 free(locfile);
439}
440
441static const char *
442doprocess(char *dst, size_t dlen, const char *src,
443 int casef, int transf, int mapf)
444{
445 if (casef)
446 src = docase(dst, dlen, src);
447 if (transf)
448 src = dotrans(dst, dlen, src);
449 if (mapf)
450 src = domap(dst, dlen, src);
451 return src;
452}
453
454/*
455 * Send multiple files.
456 */
457void
458mput(int argc, char *argv[])
459{
460 int i;
461 sigfunc oldintr;
462 int ointer;
463 const char *tp;
464
465 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "local-files"))) {
466 UPRINTF("usage: %s local-files\n", argv[0]);
467 code = -1;
468 return;
469 }
eedc536d
PA
470 mflag = 1;
471 oldintr = xsignal(SIGINT, mintr);
472 if (sigsetjmp(jabort, 1))
0511850f 473 mabort(argv[0]);
eedc536d
PA
474 if (proxy) {
475 char *cp;
476
477 while ((cp = remglob(argv, 0, NULL)) != NULL) {
478 if (*cp == '\0' || !connected) {
479 mflag = 0;
480 continue;
481 }
482 if (mflag && confirm(argv[0], cp)) {
483 char buf[MAXPATHLEN];
484 tp = doprocess(buf, sizeof(buf), cp,
485 mcase, ntflag, mapflag);
486 sendrequest((sunique) ? "STOU" : "STOR",
487 cp, tp, cp != tp || !interactive);
488 if (!mflag && fromatty) {
489 ointer = interactive;
490 interactive = 1;
0511850f 491 if (confirm(argv[0], NULL)) {
eedc536d
PA
492 mflag++;
493 }
494 interactive = ointer;
495 }
496 }
497 }
498 goto cleanupmput;
499 }
500 for (i = 1; i < argc && connected; i++) {
501 char **cpp;
502 glob_t gl;
503 int flags;
504
505 if (!doglob) {
506 if (mflag && confirm(argv[0], argv[i])) {
507 char buf[MAXPATHLEN];
508 tp = doprocess(buf, sizeof(buf), argv[i],
509 0, ntflag, mapflag);
510 sendrequest((sunique) ? "STOU" : "STOR",
511 argv[i], tp, tp != argv[i] || !interactive);
512 if (!mflag && fromatty) {
513 ointer = interactive;
514 interactive = 1;
0511850f 515 if (confirm(argv[0], NULL)) {
eedc536d
PA
516 mflag++;
517 }
518 interactive = ointer;
519 }
520 }
521 continue;
522 }
523
524 memset(&gl, 0, sizeof(gl));
525 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
526 if (glob(argv[i], flags, NULL, &gl) || gl.gl_pathc == 0) {
0511850f 527 warnx("Glob pattern `%s' not found", argv[i]);
eedc536d
PA
528 globfree(&gl);
529 continue;
530 }
531 for (cpp = gl.gl_pathv; cpp && *cpp != NULL && connected;
532 cpp++) {
533 if (mflag && confirm(argv[0], *cpp)) {
534 char buf[MAXPATHLEN];
535 tp = *cpp;
536 tp = doprocess(buf, sizeof(buf), *cpp,
537 0, ntflag, mapflag);
538 sendrequest((sunique) ? "STOU" : "STOR",
539 *cpp, tp, *cpp != tp || !interactive);
540 if (!mflag && fromatty) {
541 ointer = interactive;
542 interactive = 1;
0511850f 543 if (confirm(argv[0], NULL)) {
eedc536d
PA
544 mflag++;
545 }
546 interactive = ointer;
547 }
548 }
549 }
550 globfree(&gl);
551 }
552 cleanupmput:
553 (void)xsignal(SIGINT, oldintr);
554 mflag = 0;
555}
556
557void
558reget(int argc, char *argv[])
559{
560
d2b51479 561 (void)getit(argc, argv, 1, restart_point ? "r+" : "a");
eedc536d
PA
562}
563
564void
565get(int argc, char *argv[])
566{
567
d2b51479 568 (void)getit(argc, argv, 0, restart_point ? "r+" : "w");
eedc536d
PA
569}
570
571/*
572 * Receive one file.
573 * If restartit is 1, restart the xfer always.
574 * If restartit is -1, restart the xfer only if the remote file is newer.
575 */
576int
20e59a13 577getit(int argc, char *argv[], int restartit, const char *gmode)
eedc536d
PA
578{
579 int loc, rval;
580 char *remfile, *olocfile;
581 const char *locfile;
582 char buf[MAXPATHLEN];
583
584 loc = rval = 0;
585 if (argc == 2) {
586 argc++;
587 argv[2] = argv[1];
588 loc++;
589 }
590 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "remote-file")))
591 goto usage;
592 if ((argc < 3 && !another(&argc, &argv, "local-file")) || argc > 3) {
593 usage:
594 UPRINTF("usage: %s remote-file [local-file]\n", argv[0]);
595 code = -1;
596 return (0);
597 }
598 remfile = argv[1];
599 if ((olocfile = globulize(argv[2])) == NULL) {
600 code = -1;
601 return (0);
602 }
603 locfile = doprocess(buf, sizeof(buf), olocfile,
604 loc && mcase, loc && ntflag, loc && mapflag);
605 if (restartit) {
606 struct stat stbuf;
607 int ret;
608
609 if (! features[FEAT_REST_STREAM]) {
610 fprintf(ttyout,
611 "Restart is not supported by the remote server.\n");
612 return (0);
613 }
614 ret = stat(locfile, &stbuf);
615 if (restartit == 1) {
616 if (ret < 0) {
d2b51479
PA
617 if (errno != ENOENT) {
618 warn("Can't stat `%s'", locfile);
619 goto freegetit;
620 }
621 restart_point = 0;
eedc536d 622 }
d2b51479
PA
623 else
624 restart_point = stbuf.st_size;
eedc536d
PA
625 } else {
626 if (ret == 0) {
627 time_t mtime;
628
629 mtime = remotemodtime(argv[1], 0);
630 if (mtime == -1)
631 goto freegetit;
632 if (stbuf.st_mtime >= mtime) {
633 rval = 1;
634 goto freegetit;
635 }
636 }
637 }
638 }
639
20e59a13 640 recvrequest("RETR", locfile, remfile, gmode,
eedc536d
PA
641 remfile != argv[1] || locfile != argv[2], loc);
642 restart_point = 0;
643 freegetit:
644 (void)free(olocfile);
645 return (rval);
646}
647
648/* ARGSUSED */
0511850f 649static void
eedc536d
PA
650mintr(int signo)
651{
652
653 alarmtimer(0);
654 if (fromatty)
655 write(fileno(ttyout), "\n", 1);
656 siglongjmp(jabort, 1);
657}
658
0511850f
PA
659static void
660mabort(const char *cmd)
eedc536d
PA
661{
662 int ointer, oconf;
663
664 if (mflag && fromatty) {
665 ointer = interactive;
666 oconf = confirmrest;
667 interactive = 1;
668 confirmrest = 0;
0511850f 669 if (confirm(cmd, NULL)) {
eedc536d
PA
670 interactive = ointer;
671 confirmrest = oconf;
672 return;
673 }
674 interactive = ointer;
675 confirmrest = oconf;
676 }
677 mflag = 0;
678}
679
680/*
681 * Get multiple files.
682 */
683void
684mget(int argc, char *argv[])
685{
686 sigfunc oldintr;
687 int ointer;
688 char *cp;
689 const char *tp;
20e59a13 690 int volatile restartit;
eedc536d
PA
691
692 if (argc == 0 ||
693 (argc == 1 && !another(&argc, &argv, "remote-files"))) {
694 UPRINTF("usage: %s remote-files\n", argv[0]);
695 code = -1;
696 return;
697 }
eedc536d
PA
698 mflag = 1;
699 restart_point = 0;
700 restartit = 0;
701 if (strcmp(argv[0], "mreget") == 0) {
702 if (! features[FEAT_REST_STREAM]) {
703 fprintf(ttyout,
704 "Restart is not supported by the remote server.\n");
705 return;
706 }
707 restartit = 1;
708 }
709 oldintr = xsignal(SIGINT, mintr);
710 if (sigsetjmp(jabort, 1))
0511850f 711 mabort(argv[0]);
eedc536d
PA
712 while ((cp = remglob(argv, proxy, NULL)) != NULL) {
713 char buf[MAXPATHLEN];
714 if (*cp == '\0' || !connected) {
715 mflag = 0;
716 continue;
717 }
718 if (! mflag)
719 continue;
720 if (! fileindir(cp, localcwd)) {
721 fprintf(ttyout, "Skipping non-relative filename `%s'\n",
722 cp);
723 continue;
724 }
725 if (!confirm(argv[0], cp))
726 continue;
727 tp = doprocess(buf, sizeof(buf), cp, mcase, ntflag, mapflag);
728 if (restartit) {
729 struct stat stbuf;
730
731 if (stat(tp, &stbuf) == 0)
732 restart_point = stbuf.st_size;
733 else
0511850f 734 warn("Can't stat `%s'", tp);
eedc536d
PA
735 }
736 recvrequest("RETR", tp, cp, restart_point ? "r+" : "w",
737 tp != cp || !interactive, 1);
738 restart_point = 0;
739 if (!mflag && fromatty) {
740 ointer = interactive;
741 interactive = 1;
0511850f 742 if (confirm(argv[0], NULL))
eedc536d
PA
743 mflag++;
744 interactive = ointer;
745 }
746 }
747 (void)xsignal(SIGINT, oldintr);
748 mflag = 0;
749}
750
751/*
752 * Read list of filenames from a local file and get those
753 */
754void
755fget(int argc, char *argv[])
756{
20e59a13 757 const char *gmode;
eedc536d 758 FILE *fp;
20e59a13 759 char buf[MAXPATHLEN], cmdbuf[MAX_C_NAME];
eedc536d
PA
760
761 if (argc != 2) {
762 UPRINTF("usage: %s localfile\n", argv[0]);
763 code = -1;
764 return;
765 }
766
767 fp = fopen(argv[1], "r");
768 if (fp == NULL) {
0511850f 769 fprintf(ttyout, "Can't open source file %s\n", argv[1]);
eedc536d
PA
770 code = -1;
771 return;
772 }
773
20e59a13
PA
774 (void)strlcpy(cmdbuf, "get", sizeof(cmdbuf));
775 argv[0] = cmdbuf;
776 gmode = restart_point ? "r+" : "w";
eedc536d 777
20e59a13 778 while (get_line(fp, buf, sizeof(buf), NULL) >= 0) {
eedc536d
PA
779 if (buf[0] == '\0')
780 continue;
781 argv[1] = buf;
20e59a13 782 (void)getit(argc, argv, 0, gmode);
eedc536d
PA
783 }
784 fclose(fp);
785}
786
787const char *
20e59a13 788onoff(int val)
eedc536d
PA
789{
790
20e59a13 791 return (val ? "on" : "off");
eedc536d
PA
792}
793
794/*
795 * Show status.
796 */
797/*ARGSUSED*/
798void
799status(int argc, char *argv[])
800{
801
802 if (argc == 0) {
803 UPRINTF("usage: %s\n", argv[0]);
804 code = -1;
805 return;
806 }
807#ifndef NO_STATUS
808 if (connected)
809 fprintf(ttyout, "Connected %sto %s.\n",
810 connected == -1 ? "and logged in" : "", hostname);
811 else
812 fputs("Not connected.\n", ttyout);
813 if (!proxy) {
814 pswitch(1);
815 if (connected) {
816 fprintf(ttyout, "Connected for proxy commands to %s.\n",
817 hostname);
818 }
819 else {
820 fputs("No proxy connection.\n", ttyout);
821 }
822 pswitch(0);
823 }
824 fprintf(ttyout, "Gate ftp: %s, server %s, port %s.\n", onoff(gatemode),
825 *gateserver ? gateserver : "(none)", gateport);
826 fprintf(ttyout, "Passive mode: %s; fallback to active mode: %s.\n",
827 onoff(passivemode), onoff(activefallback));
828 fprintf(ttyout, "Mode: %s; Type: %s; Form: %s; Structure: %s.\n",
829 modename, typename, formname, structname);
830 fprintf(ttyout, "Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s.\n",
831 onoff(verbose), onoff(bell), onoff(interactive), onoff(doglob));
832 fprintf(ttyout, "Store unique: %s; Receive unique: %s.\n",
833 onoff(sunique), onoff(runique));
834 fprintf(ttyout, "Preserve modification times: %s.\n", onoff(preserve));
835 fprintf(ttyout, "Case: %s; CR stripping: %s.\n", onoff(mcase),
836 onoff(crflag));
837 if (ntflag) {
838 fprintf(ttyout, "Ntrans: (in) %s (out) %s\n", ntin, ntout);
839 }
840 else {
841 fputs("Ntrans: off.\n", ttyout);
842 }
843 if (mapflag) {
844 fprintf(ttyout, "Nmap: (in) %s (out) %s\n", mapin, mapout);
845 }
846 else {
847 fputs("Nmap: off.\n", ttyout);
848 }
849 fprintf(ttyout,
850 "Hash mark printing: %s; Mark count: %d; Progress bar: %s.\n",
851 onoff(hash), mark, onoff(progress));
852 fprintf(ttyout,
853 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
854 onoff(rate_get), rate_get, rate_get_incr);
855 fprintf(ttyout,
856 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
857 onoff(rate_put), rate_put, rate_put_incr);
858 fprintf(ttyout,
859 "Socket buffer sizes: send %d, receive %d.\n",
860 sndbuf_size, rcvbuf_size);
861 fprintf(ttyout, "Use of PORT cmds: %s.\n", onoff(sendport));
862 fprintf(ttyout, "Use of EPSV/EPRT cmds for IPv4: %s%s.\n", onoff(epsv4),
863 epsv4bad ? " (disabled for this connection)" : "");
fa70ef36
PA
864 fprintf(ttyout, "Use of EPSV/EPRT cmds for IPv6: %s%s.\n", onoff(epsv6),
865 epsv6bad ? " (disabled for this connection)" : "");
eedc536d
PA
866 fprintf(ttyout, "Command line editing: %s.\n",
867#ifdef NO_EDITCOMPLETE
868 "support not compiled in"
869#else /* !def NO_EDITCOMPLETE */
870 onoff(editing)
871#endif /* !def NO_EDITCOMPLETE */
872 );
873 if (macnum > 0) {
874 int i;
875
876 fputs("Macros:\n", ttyout);
877 for (i=0; i<macnum; i++) {
878 fprintf(ttyout, "\t%s\n", macros[i].mac_name);
879 }
880 }
881#endif /* !def NO_STATUS */
882 fprintf(ttyout, "Version: %s %s\n", FTP_PRODUCT, FTP_VERSION);
883 code = 0;
884}
885
886/*
887 * Toggle a variable
888 */
889int
890togglevar(int argc, char *argv[], int *var, const char *mesg)
891{
892 if (argc == 1) {
893 *var = !*var;
894 } else if (argc == 2 && strcasecmp(argv[1], "on") == 0) {
895 *var = 1;
896 } else if (argc == 2 && strcasecmp(argv[1], "off") == 0) {
897 *var = 0;
898 } else {
899 UPRINTF("usage: %s [ on | off ]\n", argv[0]);
900 return (-1);
901 }
902 if (mesg)
903 fprintf(ttyout, "%s %s.\n", mesg, onoff(*var));
904 return (*var);
905}
906
907/*
908 * Set beep on cmd completed mode.
909 */
910/*VARARGS*/
911void
912setbell(int argc, char *argv[])
913{
914
915 code = togglevar(argc, argv, &bell, "Bell mode");
916}
917
918/*
919 * Set command line editing
920 */
921/*VARARGS*/
922void
923setedit(int argc, char *argv[])
924{
925
926#ifdef NO_EDITCOMPLETE
927 if (argc == 0) {
928 UPRINTF("usage: %s\n", argv[0]);
929 code = -1;
930 return;
931 }
932 if (verbose)
933 fputs("Editing support not compiled in; ignoring command.\n",
934 ttyout);
935#else /* !def NO_EDITCOMPLETE */
936 code = togglevar(argc, argv, &editing, "Editing mode");
937 controlediting();
938#endif /* !def NO_EDITCOMPLETE */
939}
940
941/*
942 * Turn on packet tracing.
943 */
944/*VARARGS*/
945void
946settrace(int argc, char *argv[])
947{
948
949 code = togglevar(argc, argv, &trace, "Packet tracing");
950}
951
952/*
953 * Toggle hash mark printing during transfers, or set hash mark bytecount.
954 */
955/*VARARGS*/
956void
957sethash(int argc, char *argv[])
958{
959 if (argc == 1)
960 hash = !hash;
961 else if (argc != 2) {
962 UPRINTF("usage: %s [ on | off | bytecount ]\n",
963 argv[0]);
964 code = -1;
965 return;
966 } else if (strcasecmp(argv[1], "on") == 0)
967 hash = 1;
968 else if (strcasecmp(argv[1], "off") == 0)
969 hash = 0;
970 else {
971 int nmark;
972
973 nmark = strsuftoi(argv[1]);
974 if (nmark < 1) {
975 fprintf(ttyout, "mark: bad bytecount value `%s'.\n",
976 argv[1]);
977 code = -1;
978 return;
979 }
980 mark = nmark;
981 hash = 1;
982 }
983 fprintf(ttyout, "Hash mark printing %s", onoff(hash));
984 if (hash)
985 fprintf(ttyout, " (%d bytes/hash mark)", mark);
986 fputs(".\n", ttyout);
987 if (hash)
988 progress = 0;
989 code = hash;
990}
991
992/*
993 * Turn on printing of server echo's.
994 */
995/*VARARGS*/
996void
997setverbose(int argc, char *argv[])
998{
999
1000 code = togglevar(argc, argv, &verbose, "Verbose mode");
1001}
1002
1003/*
1004 * Toggle PORT/LPRT cmd use before each data connection.
1005 */
1006/*VARARGS*/
1007void
1008setport(int argc, char *argv[])
1009{
1010
1011 code = togglevar(argc, argv, &sendport, "Use of PORT/LPRT cmds");
1012}
1013
1014/*
1015 * Toggle transfer progress bar.
1016 */
1017/*VARARGS*/
1018void
1019setprogress(int argc, char *argv[])
1020{
1021
1022 code = togglevar(argc, argv, &progress, "Progress bar");
1023 if (progress)
1024 hash = 0;
1025}
1026
1027/*
1028 * Turn on interactive prompting during mget, mput, and mdelete.
1029 */
1030/*VARARGS*/
1031void
1032setprompt(int argc, char *argv[])
1033{
1034
1035 code = togglevar(argc, argv, &interactive, "Interactive mode");
1036}
1037
1038/*
1039 * Toggle gate-ftp mode, or set gate-ftp server
1040 */
1041/*VARARGS*/
1042void
1043setgate(int argc, char *argv[])
1044{
1045 static char gsbuf[MAXHOSTNAMELEN];
1046
1047 if (argc == 0 || argc > 3) {
1048 UPRINTF(
1049 "usage: %s [ on | off | gateserver [port] ]\n", argv[0]);
1050 code = -1;
1051 return;
1052 } else if (argc < 2) {
1053 gatemode = !gatemode;
1054 } else {
1055 if (argc == 2 && strcasecmp(argv[1], "on") == 0)
1056 gatemode = 1;
1057 else if (argc == 2 && strcasecmp(argv[1], "off") == 0)
1058 gatemode = 0;
1059 else {
1060 if (argc == 3)
1061 gateport = ftp_strdup(argv[2]);
1062 (void)strlcpy(gsbuf, argv[1], sizeof(gsbuf));
1063 gateserver = gsbuf;
1064 gatemode = 1;
1065 }
1066 }
1067 if (gatemode && (gateserver == NULL || *gateserver == '\0')) {
1068 fprintf(ttyout,
1069 "Disabling gate-ftp mode - no gate-ftp server defined.\n");
1070 gatemode = 0;
1071 } else {
1072 fprintf(ttyout, "Gate ftp: %s, server %s, port %s.\n",
1073 onoff(gatemode), *gateserver ? gateserver : "(none)",
1074 gateport);
1075 }
1076 code = gatemode;
1077}
1078
1079/*
1080 * Toggle metacharacter interpretation on local file names.
1081 */
1082/*VARARGS*/
1083void
1084setglob(int argc, char *argv[])
1085{
1086
1087 code = togglevar(argc, argv, &doglob, "Globbing");
1088}
1089
1090/*
1091 * Toggle preserving modification times on retrieved files.
1092 */
1093/*VARARGS*/
1094void
1095setpreserve(int argc, char *argv[])
1096{
1097
1098 code = togglevar(argc, argv, &preserve, "Preserve modification times");
1099}
1100
1101/*
1102 * Set debugging mode on/off and/or set level of debugging.
1103 */
1104/*VARARGS*/
1105void
1106setdebug(int argc, char *argv[])
1107{
1108 if (argc == 0 || argc > 2) {
1109 UPRINTF("usage: %s [ on | off | debuglevel ]\n", argv[0]);
1110 code = -1;
1111 return;
1112 } else if (argc == 2) {
1113 if (strcasecmp(argv[1], "on") == 0)
1114 ftp_debug = 1;
1115 else if (strcasecmp(argv[1], "off") == 0)
1116 ftp_debug = 0;
1117 else {
1118 int val;
1119
1120 val = strsuftoi(argv[1]);
1121 if (val < 0) {
1122 fprintf(ttyout, "%s: bad debugging value.\n",
1123 argv[1]);
1124 code = -1;
1125 return;
1126 }
1127 ftp_debug = val;
1128 }
1129 } else
1130 ftp_debug = !ftp_debug;
1131 if (ftp_debug)
1132 options |= SO_DEBUG;
1133 else
1134 options &= ~SO_DEBUG;
1135 fprintf(ttyout, "Debugging %s (ftp_debug=%d).\n", onoff(ftp_debug), ftp_debug);
1136 code = ftp_debug > 0;
1137}
1138
1139/*
1140 * Set current working directory on remote machine.
1141 */
1142void
1143cd(int argc, char *argv[])
1144{
1145 int r;
1146
1147 if (argc == 0 || argc > 2 ||
1148 (argc == 1 && !another(&argc, &argv, "remote-directory"))) {
1149 UPRINTF("usage: %s remote-directory\n", argv[0]);
1150 code = -1;
1151 return;
1152 }
1153 r = command("CWD %s", argv[1]);
1154 if (r == ERROR && code == 500) {
1155 if (verbose)
1156 fputs("CWD command not recognized, trying XCWD.\n",
1157 ttyout);
1158 r = command("XCWD %s", argv[1]);
1159 }
1160 if (r == COMPLETE) {
1161 dirchange = 1;
1162 updateremotecwd();
1163 }
1164}
1165
1166/*
1167 * Set current working directory on local machine.
1168 */
1169void
1170lcd(int argc, char *argv[])
1171{
1172 char *locdir;
1173
1174 code = -1;
1175 if (argc == 1) {
1176 argc++;
1177 argv[1] = localhome;
1178 }
1179 if (argc != 2) {
1180 UPRINTF("usage: %s [local-directory]\n", argv[0]);
1181 return;
1182 }
1183 if ((locdir = globulize(argv[1])) == NULL)
1184 return;
1185 if (chdir(locdir) == -1)
0511850f 1186 warn("Can't chdir `%s'", locdir);
eedc536d
PA
1187 else {
1188 updatelocalcwd();
1189 if (localcwd[0]) {
1190 fprintf(ttyout, "Local directory now: %s\n", localcwd);
1191 code = 0;
1192 } else {
1193 fprintf(ttyout, "Unable to determine local directory\n");
1194 }
1195 }
1196 (void)free(locdir);
1197}
1198
1199/*
1200 * Delete a single file.
1201 */
1202void
1203delete(int argc, char *argv[])
1204{
1205
1206 if (argc == 0 || argc > 2 ||
1207 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
1208 UPRINTF("usage: %s remote-file\n", argv[0]);
1209 code = -1;
1210 return;
1211 }
1212 if (command("DELE %s", argv[1]) == COMPLETE)
1213 dirchange = 1;
1214}
1215
1216/*
1217 * Delete multiple files.
1218 */
1219void
1220mdelete(int argc, char *argv[])
1221{
1222 sigfunc oldintr;
1223 int ointer;
1224 char *cp;
1225
1226 if (argc == 0 ||
1227 (argc == 1 && !another(&argc, &argv, "remote-files"))) {
1228 UPRINTF("usage: %s [remote-files]\n", argv[0]);
1229 code = -1;
1230 return;
1231 }
eedc536d
PA
1232 mflag = 1;
1233 oldintr = xsignal(SIGINT, mintr);
1234 if (sigsetjmp(jabort, 1))
0511850f 1235 mabort(argv[0]);
eedc536d
PA
1236 while ((cp = remglob(argv, 0, NULL)) != NULL) {
1237 if (*cp == '\0') {
1238 mflag = 0;
1239 continue;
1240 }
1241 if (mflag && confirm(argv[0], cp)) {
1242 if (command("DELE %s", cp) == COMPLETE)
1243 dirchange = 1;
1244 if (!mflag && fromatty) {
1245 ointer = interactive;
1246 interactive = 1;
0511850f 1247 if (confirm(argv[0], NULL)) {
eedc536d
PA
1248 mflag++;
1249 }
1250 interactive = ointer;
1251 }
1252 }
1253 }
1254 (void)xsignal(SIGINT, oldintr);
1255 mflag = 0;
1256}
1257
1258/*
1259 * Rename a remote file.
1260 */
1261void
1262renamefile(int argc, char *argv[])
1263{
1264
1265 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "from-name")))
1266 goto usage;
1267 if ((argc < 3 && !another(&argc, &argv, "to-name")) || argc > 3) {
1268 usage:
1269 UPRINTF("usage: %s from-name to-name\n", argv[0]);
1270 code = -1;
1271 return;
1272 }
1273 if (command("RNFR %s", argv[1]) == CONTINUE &&
1274 command("RNTO %s", argv[2]) == COMPLETE)
1275 dirchange = 1;
1276}
1277
1278/*
1279 * Get a directory listing of remote files.
1280 * Supports being invoked as:
1281 * cmd runs
1282 * --- ----
1283 * dir, ls LIST
1284 * mlsd MLSD
1285 * nlist NLST
1286 * pdir, pls LIST |$PAGER
20e59a13 1287 * pmlsd MLSD |$PAGER
eedc536d
PA
1288 */
1289void
1290ls(int argc, char *argv[])
1291{
1292 const char *cmd;
20e59a13
PA
1293 char *remdir, *locbuf;
1294 const char *locfile;
1295 int pagecmd, mlsdcmd;
eedc536d
PA
1296
1297 remdir = NULL;
20e59a13 1298 locbuf = NULL;
eedc536d 1299 locfile = "-";
20e59a13 1300 pagecmd = mlsdcmd = 0;
eedc536d
PA
1301 /*
1302 * the only commands that start with `p' are
1303 * the `pager' versions.
1304 */
1305 if (argv[0][0] == 'p')
1306 pagecmd = 1;
1307 if (strcmp(argv[0] + pagecmd , "mlsd") == 0) {
1308 if (! features[FEAT_MLST]) {
1309 fprintf(ttyout,
1310 "MLSD is not supported by the remote server.\n");
1311 return;
1312 }
1313 mlsdcmd = 1;
1314 }
1315 if (argc == 0)
1316 goto usage;
1317
1318 if (mlsdcmd)
1319 cmd = "MLSD";
1320 else if (strcmp(argv[0] + pagecmd, "nlist") == 0)
1321 cmd = "NLST";
1322 else
1323 cmd = "LIST";
1324
1325 if (argc > 1)
1326 remdir = argv[1];
1327 if (argc > 2)
1328 locfile = argv[2];
1329 if (argc > 3 || ((pagecmd | mlsdcmd) && argc > 2)) {
1330 usage:
1331 if (pagecmd || mlsdcmd)
1332 UPRINTF("usage: %s [remote-path]\n", argv[0]);
1333 else
1334 UPRINTF("usage: %s [remote-path [local-file]]\n",
1335 argv[0]);
1336 code = -1;
1337 goto freels;
1338 }
1339
1340 if (pagecmd) {
20e59a13 1341 const char *p;
eedc536d
PA
1342 size_t len;
1343
1344 p = getoptionvalue("pager");
1345 if (EMPTYSTRING(p))
1346 p = DEFAULTPAGER;
1347 len = strlen(p) + 2;
20e59a13
PA
1348 locbuf = ftp_malloc(len);
1349 locbuf[0] = '|';
1350 (void)strlcpy(locbuf + 1, p, len - 1);
1351 locfile = locbuf;
eedc536d 1352 } else if ((strcmp(locfile, "-") != 0) && *locfile != '|') {
20e59a13
PA
1353 if ((locbuf = globulize(locfile)) == NULL ||
1354 !confirm("output to local-file:", locbuf)) {
eedc536d
PA
1355 code = -1;
1356 goto freels;
1357 }
20e59a13 1358 locfile = locbuf;
eedc536d
PA
1359 }
1360 recvrequest(cmd, locfile, remdir, "w", 0, 0);
1361 freels:
20e59a13
PA
1362 if (locbuf)
1363 (void)free(locbuf);
eedc536d
PA
1364}
1365
1366/*
1367 * Get a directory listing of multiple remote files.
1368 */
1369void
1370mls(int argc, char *argv[])
1371{
1372 sigfunc oldintr;
1373 int ointer, i;
20e59a13
PA
1374 int volatile dolist;
1375 char * volatile dest, *odest;
1376 const char *lmode;
eedc536d
PA
1377
1378 if (argc == 0)
1379 goto usage;
1380 if (argc < 2 && !another(&argc, &argv, "remote-files"))
1381 goto usage;
1382 if (argc < 3 && !another(&argc, &argv, "local-file")) {
1383 usage:
1384 UPRINTF("usage: %s remote-files local-file\n", argv[0]);
1385 code = -1;
1386 return;
1387 }
1388 odest = dest = argv[argc - 1];
1389 argv[argc - 1] = NULL;
eedc536d
PA
1390 if (strcmp(dest, "-") && *dest != '|')
1391 if (((dest = globulize(dest)) == NULL) ||
1392 !confirm("output to local-file:", dest)) {
1393 code = -1;
1394 return;
1395 }
1396 dolist = strcmp(argv[0], "mls");
1397 mflag = 1;
1398 oldintr = xsignal(SIGINT, mintr);
1399 if (sigsetjmp(jabort, 1))
0511850f 1400 mabort(argv[0]);
eedc536d 1401 for (i = 1; mflag && i < argc-1 && connected; i++) {
20e59a13
PA
1402 lmode = (i == 1) ? "w" : "a";
1403 recvrequest(dolist ? "LIST" : "NLST", dest, argv[i], lmode,
eedc536d
PA
1404 0, 0);
1405 if (!mflag && fromatty) {
1406 ointer = interactive;
1407 interactive = 1;
0511850f 1408 if (confirm(argv[0], NULL)) {
eedc536d
PA
1409 mflag++;
1410 }
1411 interactive = ointer;
1412 }
1413 }
1414 (void)xsignal(SIGINT, oldintr);
1415 mflag = 0;
1416 if (dest != odest) /* free up after globulize() */
1417 free(dest);
1418}
1419
1420/*
1421 * Do a shell escape
1422 */
1423/*ARGSUSED*/
1424void
1425shell(int argc, char *argv[])
1426{
1427 pid_t pid;
1428 sigfunc oldintr;
20e59a13
PA
1429 char shellnam[MAXPATHLEN];
1430 const char *shellp, *namep;
eedc536d
PA
1431 int wait_status;
1432
1433 if (argc == 0) {
1434 UPRINTF("usage: %s [command [args]]\n", argv[0]);
1435 code = -1;
1436 return;
1437 }
1438 oldintr = xsignal(SIGINT, SIG_IGN);
1439 if ((pid = fork()) == 0) {
1440 for (pid = 3; pid < 20; pid++)
1441 (void)close(pid);
1442 (void)xsignal(SIGINT, SIG_DFL);
20e59a13
PA
1443 shellp = getenv("SHELL");
1444 if (shellp == NULL)
1445 shellp = _PATH_BSHELL;
1446 namep = strrchr(shellp, '/');
eedc536d 1447 if (namep == NULL)
20e59a13 1448 namep = shellp;
eedc536d
PA
1449 else
1450 namep++;
1451 (void)strlcpy(shellnam, namep, sizeof(shellnam));
1452 if (ftp_debug) {
20e59a13 1453 fputs(shellp, ttyout);
eedc536d
PA
1454 putc('\n', ttyout);
1455 }
1456 if (argc > 1) {
20e59a13 1457 execl(shellp, shellnam, "-c", altarg, (char *)0);
eedc536d
PA
1458 }
1459 else {
20e59a13 1460 execl(shellp, shellnam, (char *)0);
eedc536d 1461 }
20e59a13 1462 warn("Can't execute `%s'", shellp);
eedc536d
PA
1463 code = -1;
1464 exit(1);
1465 }
1466 if (pid > 0)
1467 while (wait(&wait_status) != pid)
1468 ;
1469 (void)xsignal(SIGINT, oldintr);
1470 if (pid == -1) {
0511850f 1471 warn("Can't fork a subshell; try again later");
eedc536d
PA
1472 code = -1;
1473 } else
1474 code = 0;
1475}
1476
1477/*
1478 * Send new user information (re-login)
1479 */
1480void
1481user(int argc, char *argv[])
1482{
1483 char *password;
0511850f 1484 char emptypass[] = "";
eedc536d
PA
1485 int n, aflag = 0;
1486
1487 if (argc == 0)
1488 goto usage;
1489 if (argc < 2)
1490 (void)another(&argc, &argv, "username");
1491 if (argc < 2 || argc > 4) {
1492 usage:
1493 UPRINTF("usage: %s username [password [account]]\n",
1494 argv[0]);
1495 code = -1;
1496 return;
1497 }
1498 n = command("USER %s", argv[1]);
1499 if (n == CONTINUE) {
1500 if (argc < 3) {
1501 password = getpass("Password: ");
0511850f
PA
1502 if (password == NULL)
1503 password = emptypass;
eedc536d
PA
1504 } else {
1505 password = argv[2];
1506 }
1507 n = command("PASS %s", password);
1508 memset(password, 0, strlen(password));
1509 }
1510 if (n == CONTINUE) {
1511 aflag++;
1512 if (argc < 4) {
1513 password = getpass("Account: ");
0511850f
PA
1514 if (password == NULL)
1515 password = emptypass;
eedc536d
PA
1516 } else {
1517 password = argv[3];
1518 }
1519 n = command("ACCT %s", password);
1520 memset(password, 0, strlen(password));
1521 }
1522 if (n != COMPLETE) {
1523 fputs("Login failed.\n", ttyout);
1524 return;
1525 }
1526 if (!aflag && argc == 4) {
1527 password = argv[3];
1528 (void)command("ACCT %s", password);
1529 memset(password, 0, strlen(password));
1530 }
1531 connected = -1;
1532 getremoteinfo();
1533}
1534
1535/*
1536 * Print working directory on remote machine.
1537 */
1538/*VARARGS*/
1539void
1540pwd(int argc, char *argv[])
1541{
1542
1543 code = -1;
1544 if (argc != 1) {
1545 UPRINTF("usage: %s\n", argv[0]);
1546 return;
1547 }
1548 if (! remotecwd[0])
1549 updateremotecwd();
1550 if (! remotecwd[0])
1551 fprintf(ttyout, "Unable to determine remote directory\n");
1552 else {
1553 fprintf(ttyout, "Remote directory: %s\n", remotecwd);
1554 code = 0;
1555 }
1556}
1557
1558/*
1559 * Print working directory on local machine.
1560 */
1561void
1562lpwd(int argc, char *argv[])
1563{
1564
1565 code = -1;
1566 if (argc != 1) {
1567 UPRINTF("usage: %s\n", argv[0]);
1568 return;
1569 }
1570 if (! localcwd[0])
1571 updatelocalcwd();
1572 if (! localcwd[0])
1573 fprintf(ttyout, "Unable to determine local directory\n");
1574 else {
1575 fprintf(ttyout, "Local directory: %s\n", localcwd);
1576 code = 0;
1577 }
1578}
1579
1580/*
1581 * Make a directory.
1582 */
1583void
1584makedir(int argc, char *argv[])
1585{
1586 int r;
1587
1588 if (argc == 0 || argc > 2 ||
1589 (argc == 1 && !another(&argc, &argv, "directory-name"))) {
1590 UPRINTF("usage: %s directory-name\n", argv[0]);
1591 code = -1;
1592 return;
1593 }
1594 r = command("MKD %s", argv[1]);
1595 if (r == ERROR && code == 500) {
1596 if (verbose)
1597 fputs("MKD command not recognized, trying XMKD.\n",
1598 ttyout);
1599 r = command("XMKD %s", argv[1]);
1600 }
1601 if (r == COMPLETE)
1602 dirchange = 1;
1603}
1604
1605/*
1606 * Remove a directory.
1607 */
1608void
1609removedir(int argc, char *argv[])
1610{
1611 int r;
1612
1613 if (argc == 0 || argc > 2 ||
1614 (argc == 1 && !another(&argc, &argv, "directory-name"))) {
1615 UPRINTF("usage: %s directory-name\n", argv[0]);
1616 code = -1;
1617 return;
1618 }
1619 r = command("RMD %s", argv[1]);
1620 if (r == ERROR && code == 500) {
1621 if (verbose)
1622 fputs("RMD command not recognized, trying XRMD.\n",
1623 ttyout);
1624 r = command("XRMD %s", argv[1]);
1625 }
1626 if (r == COMPLETE)
1627 dirchange = 1;
1628}
1629
1630/*
1631 * Send a line, verbatim, to the remote machine.
1632 */
1633void
1634quote(int argc, char *argv[])
1635{
1636
1637 if (argc == 0 ||
1638 (argc == 1 && !another(&argc, &argv, "command line to send"))) {
1639 UPRINTF("usage: %s line-to-send\n", argv[0]);
1640 code = -1;
1641 return;
1642 }
1643 quote1("", argc, argv);
1644}
1645
1646/*
1647 * Send a SITE command to the remote machine. The line
1648 * is sent verbatim to the remote machine, except that the
1649 * word "SITE" is added at the front.
1650 */
1651void
1652site(int argc, char *argv[])
1653{
1654
1655 if (argc == 0 ||
1656 (argc == 1 && !another(&argc, &argv, "arguments to SITE command"))){
1657 UPRINTF("usage: %s line-to-send\n", argv[0]);
1658 code = -1;
1659 return;
1660 }
1661 quote1("SITE ", argc, argv);
1662}
1663
1664/*
1665 * Turn argv[1..argc) into a space-separated string, then prepend initial text.
1666 * Send the result as a one-line command and get response.
1667 */
1668void
1669quote1(const char *initial, int argc, char *argv[])
1670{
1671 int i;
1672 char buf[BUFSIZ]; /* must be >= sizeof(line) */
1673
1674 (void)strlcpy(buf, initial, sizeof(buf));
1675 for (i = 1; i < argc; i++) {
1676 (void)strlcat(buf, argv[i], sizeof(buf));
1677 if (i < (argc - 1))
1678 (void)strlcat(buf, " ", sizeof(buf));
1679 }
1680 if (command("%s", buf) == PRELIM) {
1681 while (getreply(0) == PRELIM)
1682 continue;
1683 }
1684 dirchange = 1;
1685}
1686
1687void
1688do_chmod(int argc, char *argv[])
1689{
1690
1691 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "mode")))
1692 goto usage;
1693 if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) {
1694 usage:
1695 UPRINTF("usage: %s mode remote-file\n", argv[0]);
1696 code = -1;
1697 return;
1698 }
1699 (void)command("SITE CHMOD %s %s", argv[1], argv[2]);
1700}
1701
1702#define COMMAND_1ARG(argc, argv, cmd) \
1703 if (argc == 1) \
1704 command(cmd); \
1705 else \
1706 command(cmd " %s", argv[1])
1707
1708void
1709do_umask(int argc, char *argv[])
1710{
1711 int oldverbose = verbose;
1712
1713 if (argc == 0) {
1714 UPRINTF("usage: %s [umask]\n", argv[0]);
1715 code = -1;
1716 return;
1717 }
1718 verbose = 1;
1719 COMMAND_1ARG(argc, argv, "SITE UMASK");
1720 verbose = oldverbose;
1721}
1722
1723void
1724idlecmd(int argc, char *argv[])
1725{
1726 int oldverbose = verbose;
1727
1728 if (argc < 1 || argc > 2) {
1729 UPRINTF("usage: %s [seconds]\n", argv[0]);
1730 code = -1;
1731 return;
1732 }
1733 verbose = 1;
1734 COMMAND_1ARG(argc, argv, "SITE IDLE");
1735 verbose = oldverbose;
1736}
1737
1738/*
1739 * Ask the other side for help.
1740 */
1741void
1742rmthelp(int argc, char *argv[])
1743{
1744 int oldverbose = verbose;
1745
1746 if (argc == 0) {
1747 UPRINTF("usage: %s\n", argv[0]);
1748 code = -1;
1749 return;
1750 }
1751 verbose = 1;
1752 COMMAND_1ARG(argc, argv, "HELP");
1753 verbose = oldverbose;
1754}
1755
1756/*
1757 * Terminate session and exit.
1758 * May be called with 0, NULL.
1759 */
1760/*VARARGS*/
1761void
1762quit(int argc, char *argv[])
1763{
1764
1765 /* this may be called with argc == 0, argv == NULL */
1766 if (argc == 0 && argv != NULL) {
1767 UPRINTF("usage: %s\n", argv[0]);
1768 code = -1;
1769 return;
1770 }
1771 if (connected)
1772 disconnect(0, NULL);
1773 pswitch(1);
1774 if (connected)
1775 disconnect(0, NULL);
1776 exit(0);
1777}
1778
1779/*
1780 * Terminate session, but don't exit.
1781 * May be called with 0, NULL.
1782 */
1783void
1784disconnect(int argc, char *argv[])
1785{
1786
1787 /* this may be called with argc == 0, argv == NULL */
1788 if (argc == 0 && argv != NULL) {
1789 UPRINTF("usage: %s\n", argv[0]);
1790 code = -1;
1791 return;
1792 }
1793 if (!connected)
1794 return;
1795 (void)command("QUIT");
1796 cleanuppeer();
1797}
1798
1799void
1800account(int argc, char *argv[])
1801{
1802 char *ap;
0511850f 1803 char emptypass[] = "";
eedc536d
PA
1804
1805 if (argc == 0 || argc > 2) {
1806 UPRINTF("usage: %s [password]\n", argv[0]);
1807 code = -1;
1808 return;
1809 }
1810 else if (argc == 2)
1811 ap = argv[1];
0511850f 1812 else {
eedc536d 1813 ap = getpass("Account:");
0511850f
PA
1814 if (ap == NULL)
1815 ap = emptypass;
1816 }
eedc536d 1817 (void)command("ACCT %s", ap);
0511850f 1818 memset(ap, 0, strlen(ap));
eedc536d
PA
1819}
1820
1821sigjmp_buf abortprox;
1822
1823void
1824proxabort(int notused)
1825{
1826
1827 sigint_raised = 1;
1828 alarmtimer(0);
1829 if (!proxy) {
1830 pswitch(1);
1831 }
1832 if (connected) {
1833 proxflag = 1;
1834 }
1835 else {
1836 proxflag = 0;
1837 }
1838 pswitch(0);
1839 siglongjmp(abortprox, 1);
1840}
1841
1842void
1843doproxy(int argc, char *argv[])
1844{
1845 struct cmd *c;
1846 int cmdpos;
1847 sigfunc oldintr;
20e59a13 1848 char cmdbuf[MAX_C_NAME];
eedc536d
PA
1849
1850 if (argc == 0 || (argc == 1 && !another(&argc, &argv, "command"))) {
1851 UPRINTF("usage: %s command\n", argv[0]);
1852 code = -1;
1853 return;
1854 }
1855 c = getcmd(argv[1]);
1856 if (c == (struct cmd *) -1) {
1857 fputs("?Ambiguous command.\n", ttyout);
1858 code = -1;
1859 return;
1860 }
1861 if (c == 0) {
1862 fputs("?Invalid command.\n", ttyout);
1863 code = -1;
1864 return;
1865 }
1866 if (!c->c_proxy) {
1867 fputs("?Invalid proxy command.\n", ttyout);
1868 code = -1;
1869 return;
1870 }
1871 if (sigsetjmp(abortprox, 1)) {
1872 code = -1;
1873 return;
1874 }
1875 oldintr = xsignal(SIGINT, proxabort);
1876 pswitch(1);
1877 if (c->c_conn && !connected) {
1878 fputs("Not connected.\n", ttyout);
1879 pswitch(0);
1880 (void)xsignal(SIGINT, oldintr);
1881 code = -1;
1882 return;
1883 }
1884 cmdpos = strcspn(line, " \t");
1885 if (cmdpos > 0) /* remove leading "proxy " from input buffer */
1886 memmove(line, line + cmdpos + 1, strlen(line) - cmdpos + 1);
20e59a13
PA
1887 (void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
1888 argv[1] = cmdbuf;
eedc536d
PA
1889 (*c->c_handler)(argc-1, argv+1);
1890 if (connected) {
1891 proxflag = 1;
1892 }
1893 else {
1894 proxflag = 0;
1895 }
1896 pswitch(0);
1897 (void)xsignal(SIGINT, oldintr);
1898}
1899
1900void
1901setcase(int argc, char *argv[])
1902{
1903
1904 code = togglevar(argc, argv, &mcase, "Case mapping");
1905}
1906
1907/*
1908 * convert the given name to lower case if it's all upper case, into
1909 * a static buffer which is returned to the caller
1910 */
1911static const char *
1912docase(char *dst, size_t dlen, const char *src)
1913{
1914 size_t i;
1915 int dochange = 1;
1916
1917 for (i = 0; src[i] != '\0' && i < dlen - 1; i++) {
1918 dst[i] = src[i];
1919 if (islower((unsigned char)dst[i]))
1920 dochange = 0;
1921 }
1922 dst[i] = '\0';
1923
1924 if (dochange) {
1925 for (i = 0; dst[i] != '\0'; i++)
1926 if (isupper((unsigned char)dst[i]))
1927 dst[i] = tolower((unsigned char)dst[i]);
1928 }
1929 return dst;
1930}
1931
1932void
1933setcr(int argc, char *argv[])
1934{
1935
1936 code = togglevar(argc, argv, &crflag, "Carriage Return stripping");
1937}
1938
1939void
1940setntrans(int argc, char *argv[])
1941{
1942
1943 if (argc == 0 || argc > 3) {
1944 UPRINTF("usage: %s [inchars [outchars]]\n", argv[0]);
1945 code = -1;
1946 return;
1947 }
1948 if (argc == 1) {
1949 ntflag = 0;
1950 fputs("Ntrans off.\n", ttyout);
1951 code = ntflag;
1952 return;
1953 }
1954 ntflag++;
1955 code = ntflag;
1956 (void)strlcpy(ntin, argv[1], sizeof(ntin));
1957 if (argc == 2) {
1958 ntout[0] = '\0';
1959 return;
1960 }
1961 (void)strlcpy(ntout, argv[2], sizeof(ntout));
1962}
1963
1964static const char *
1965dotrans(char *dst, size_t dlen, const char *src)
1966{
1967 const char *cp1;
1968 char *cp2 = dst;
1969 size_t i, ostop;
1970
1971 for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
1972 continue;
1973 for (cp1 = src; *cp1; cp1++) {
1974 int found = 0;
1975 for (i = 0; *(ntin + i) && i < 16; i++) {
1976 if (*cp1 == *(ntin + i)) {
1977 found++;
1978 if (i < ostop) {
1979 *cp2++ = *(ntout + i);
20e59a13 1980 if (cp2 - dst >= (ptrdiff_t)(dlen - 1))
eedc536d
PA
1981 goto out;
1982 }
1983 break;
1984 }
1985 }
1986 if (!found) {
1987 *cp2++ = *cp1;
1988 }
1989 }
1990out:
1991 *cp2 = '\0';
1992 return dst;
1993}
1994
1995void
1996setnmap(int argc, char *argv[])
1997{
1998 char *cp;
1999
2000 if (argc == 1) {
2001 mapflag = 0;
2002 fputs("Nmap off.\n", ttyout);
2003 code = mapflag;
2004 return;
2005 }
2006 if (argc == 0 ||
2007 (argc < 3 && !another(&argc, &argv, "mapout")) || argc > 3) {
2008 UPRINTF("usage: %s [mapin mapout]\n", argv[0]);
2009 code = -1;
2010 return;
2011 }
2012 mapflag = 1;
2013 code = 1;
2014 cp = strchr(altarg, ' ');
2015 if (proxy) {
2016 while(*++cp == ' ')
2017 continue;
2018 altarg = cp;
2019 cp = strchr(altarg, ' ');
2020 }
2021 *cp = '\0';
2022 (void)strlcpy(mapin, altarg, MAXPATHLEN);
2023 while (*++cp == ' ')
2024 continue;
2025 (void)strlcpy(mapout, cp, MAXPATHLEN);
2026}
2027
2028static const char *
2029domap(char *dst, size_t dlen, const char *src)
2030{
2031 const char *cp1 = src;
2032 char *cp2 = mapin;
2033 const char *tp[9], *te[9];
2034 int i, toks[9], toknum = 0, match = 1;
2035
2036 for (i=0; i < 9; ++i) {
2037 toks[i] = 0;
2038 }
2039 while (match && *cp1 && *cp2) {
2040 switch (*cp2) {
2041 case '\\':
2042 if (*++cp2 != *cp1) {
2043 match = 0;
2044 }
2045 break;
2046 case '$':
2047 if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {
2048 if (*cp1 != *(++cp2+1)) {
2049 toks[toknum = *cp2 - '1']++;
2050 tp[toknum] = cp1;
2051 while (*++cp1 && *(cp2+1)
2052 != *cp1);
2053 te[toknum] = cp1;
2054 }
2055 cp2++;
2056 break;
2057 }
2058 /* FALLTHROUGH */
2059 default:
2060 if (*cp2 != *cp1) {
2061 match = 0;
2062 }
2063 break;
2064 }
2065 if (match && *cp1) {
2066 cp1++;
2067 }
2068 if (match && *cp2) {
2069 cp2++;
2070 }
2071 }
2072 if (!match && *cp1) /* last token mismatch */
2073 {
2074 toks[toknum] = 0;
2075 }
2076 cp2 = dst;
2077 *cp2 = '\0';
2078 cp1 = mapout;
2079 while (*cp1) {
2080 match = 0;
2081 switch (*cp1) {
2082 case '\\':
2083 if (*(cp1 + 1)) {
2084 *cp2++ = *++cp1;
2085 }
2086 break;
2087 case '[':
2088LOOP:
2089 if (*++cp1 == '$' &&
2090 isdigit((unsigned char)*(cp1+1))) {
2091 if (*++cp1 == '0') {
2092 const char *cp3 = src;
2093
2094 while (*cp3) {
2095 *cp2++ = *cp3++;
2096 }
2097 match = 1;
2098 }
2099 else if (toks[toknum = *cp1 - '1']) {
2100 const char *cp3 = tp[toknum];
2101
2102 while (cp3 != te[toknum]) {
2103 *cp2++ = *cp3++;
2104 }
2105 match = 1;
2106 }
2107 }
2108 else {
2109 while (*cp1 && *cp1 != ',' &&
2110 *cp1 != ']') {
2111 if (*cp1 == '\\') {
2112 cp1++;
2113 }
2114 else if (*cp1 == '$' &&
2115 isdigit((unsigned char)*(cp1+1))) {
2116 if (*++cp1 == '0') {
2117 const char *cp3 = src;
2118
2119 while (*cp3) {
2120 *cp2++ = *cp3++;
2121 }
2122 }
2123 else if (toks[toknum =
2124 *cp1 - '1']) {
2125 const char *cp3=tp[toknum];
2126
2127 while (cp3 !=
2128 te[toknum]) {
2129 *cp2++ = *cp3++;
2130 }
2131 }
2132 }
2133 else if (*cp1) {
2134 *cp2++ = *cp1++;
2135 }
2136 }
2137 if (!*cp1) {
2138 fputs(
2139 "nmap: unbalanced brackets.\n",
2140 ttyout);
2141 return (src);
2142 }
2143 match = 1;
2144 cp1--;
2145 }
2146 if (match) {
2147 while (*++cp1 && *cp1 != ']') {
2148 if (*cp1 == '\\' && *(cp1 + 1)) {
2149 cp1++;
2150 }
2151 }
2152 if (!*cp1) {
2153 fputs(
2154 "nmap: unbalanced brackets.\n",
2155 ttyout);
2156 return (src);
2157 }
2158 break;
2159 }
2160 switch (*++cp1) {
2161 case ',':
2162 goto LOOP;
2163 case ']':
2164 break;
2165 default:
2166 cp1--;
2167 goto LOOP;
2168 }
2169 break;
2170 case '$':
2171 if (isdigit((unsigned char)*(cp1 + 1))) {
2172 if (*++cp1 == '0') {
2173 const char *cp3 = src;
2174
2175 while (*cp3) {
2176 *cp2++ = *cp3++;
2177 }
2178 }
2179 else if (toks[toknum = *cp1 - '1']) {
2180 const char *cp3 = tp[toknum];
2181
2182 while (cp3 != te[toknum]) {
2183 *cp2++ = *cp3++;
2184 }
2185 }
2186 break;
2187 }
2188 /* intentional drop through */
2189 default:
2190 *cp2++ = *cp1;
2191 break;
2192 }
2193 cp1++;
2194 }
2195 *cp2 = '\0';
2196 return *dst ? dst : src;
2197}
2198
2199void
2200setpassive(int argc, char *argv[])
2201{
2202
2203 if (argc == 1) {
2204 passivemode = !passivemode;
2205 activefallback = passivemode;
2206 } else if (argc != 2) {
2207 passiveusage:
2208 UPRINTF("usage: %s [ on | off | auto ]\n", argv[0]);
2209 code = -1;
2210 return;
2211 } else if (strcasecmp(argv[1], "on") == 0) {
2212 passivemode = 1;
2213 activefallback = 0;
2214 } else if (strcasecmp(argv[1], "off") == 0) {
2215 passivemode = 0;
2216 activefallback = 0;
2217 } else if (strcasecmp(argv[1], "auto") == 0) {
2218 passivemode = 1;
2219 activefallback = 1;
2220 } else
2221 goto passiveusage;
2222 fprintf(ttyout, "Passive mode: %s; fallback to active mode: %s.\n",
2223 onoff(passivemode), onoff(activefallback));
2224 code = passivemode;
2225}
2226
fa70ef36 2227
eedc536d
PA
2228void
2229setepsv4(int argc, char *argv[])
2230{
eedc536d
PA
2231 code = togglevar(argc, argv, &epsv4,
2232 verbose ? "EPSV/EPRT on IPv4" : NULL);
2233 epsv4bad = 0;
2234}
2235
fa70ef36
PA
2236void
2237setepsv6(int argc, char *argv[])
2238{
2239 code = togglevar(argc, argv, &epsv6,
2240 verbose ? "EPSV/EPRT on IPv6" : NULL);
2241 epsv6bad = 0;
2242}
2243
2244void
2245setepsv(int argc, char*argv[])
2246{
2247 setepsv4(argc,argv);
2248 setepsv6(argc,argv);
2249}
2250
eedc536d
PA
2251void
2252setsunique(int argc, char *argv[])
2253{
2254
2255 code = togglevar(argc, argv, &sunique, "Store unique");
2256}
2257
2258void
2259setrunique(int argc, char *argv[])
2260{
2261
2262 code = togglevar(argc, argv, &runique, "Receive unique");
2263}
2264
2265int
2266parserate(int argc, char *argv[], int cmdlineopt)
2267{
2268 int dir, max, incr, showonly;
2269 sigfunc oldusr1, oldusr2;
2270
2271 if (argc > 4 || (argc < (cmdlineopt ? 3 : 2))) {
2272 usage:
2273 if (cmdlineopt)
2274 UPRINTF(
2275 "usage: %s (all|get|put),maximum-bytes[,increment-bytes]]\n",
2276 argv[0]);
2277 else
2278 UPRINTF(
2279 "usage: %s (all|get|put) [maximum-bytes [increment-bytes]]\n",
2280 argv[0]);
2281 return -1;
2282 }
2283 dir = max = incr = showonly = 0;
2284#define RATE_GET 1
2285#define RATE_PUT 2
2286#define RATE_ALL (RATE_GET | RATE_PUT)
2287
2288 if (strcasecmp(argv[1], "all") == 0)
2289 dir = RATE_ALL;
2290 else if (strcasecmp(argv[1], "get") == 0)
2291 dir = RATE_GET;
2292 else if (strcasecmp(argv[1], "put") == 0)
2293 dir = RATE_PUT;
2294 else
2295 goto usage;
2296
2297 if (argc >= 3) {
2298 if ((max = strsuftoi(argv[2])) < 0)
2299 goto usage;
2300 } else
2301 showonly = 1;
2302
2303 if (argc == 4) {
2304 if ((incr = strsuftoi(argv[3])) <= 0)
2305 goto usage;
2306 } else
2307 incr = DEFAULTINCR;
2308
2309 oldusr1 = xsignal(SIGUSR1, SIG_IGN);
2310 oldusr2 = xsignal(SIGUSR2, SIG_IGN);
2311 if (dir & RATE_GET) {
2312 if (!showonly) {
2313 rate_get = max;
2314 rate_get_incr = incr;
2315 }
2316 if (!cmdlineopt || verbose)
2317 fprintf(ttyout,
2318 "Get transfer rate throttle: %s; maximum: %d; increment %d.\n",
2319 onoff(rate_get), rate_get, rate_get_incr);
2320 }
2321 if (dir & RATE_PUT) {
2322 if (!showonly) {
2323 rate_put = max;
2324 rate_put_incr = incr;
2325 }
2326 if (!cmdlineopt || verbose)
2327 fprintf(ttyout,
2328 "Put transfer rate throttle: %s; maximum: %d; increment %d.\n",
2329 onoff(rate_put), rate_put, rate_put_incr);
2330 }
2331 (void)xsignal(SIGUSR1, oldusr1);
2332 (void)xsignal(SIGUSR2, oldusr2);
2333 return 0;
2334}
2335
2336void
2337setrate(int argc, char *argv[])
2338{
2339
2340 code = parserate(argc, argv, 0);
2341}
2342
2343/* change directory to parent directory */
2344void
2345cdup(int argc, char *argv[])
2346{
2347 int r;
2348
2349 if (argc == 0) {
2350 UPRINTF("usage: %s\n", argv[0]);
2351 code = -1;
2352 return;
2353 }
2354 r = command("CDUP");
2355 if (r == ERROR && code == 500) {
2356 if (verbose)
2357 fputs("CDUP command not recognized, trying XCUP.\n",
2358 ttyout);
2359 r = command("XCUP");
2360 }
2361 if (r == COMPLETE) {
2362 dirchange = 1;
2363 updateremotecwd();
2364 }
2365}
2366
2367/*
2368 * Restart transfer at specific point
2369 */
2370void
2371restart(int argc, char *argv[])
2372{
2373
2374 if (argc == 0 || argc > 2) {
2375 UPRINTF("usage: %s [restart-point]\n", argv[0]);
2376 code = -1;
2377 return;
2378 }
2379 if (! features[FEAT_REST_STREAM]) {
2380 fprintf(ttyout,
2381 "Restart is not supported by the remote server.\n");
2382 return;
2383 }
2384 if (argc == 2) {
2385 off_t rp;
2386 char *ep;
2387
2388 rp = STRTOLL(argv[1], &ep, 10);
2389 if (rp < 0 || *ep != '\0')
2390 fprintf(ttyout, "restart: Invalid offset `%s'\n",
2391 argv[1]);
2392 else
2393 restart_point = rp;
2394 }
2395 if (restart_point == 0)
2396 fputs("No restart point defined.\n", ttyout);
2397 else
2398 fprintf(ttyout,
2399 "Restarting at " LLF " for next get, put or append\n",
2400 (LLT)restart_point);
2401}
2402
2403/*
2404 * Show remote system type
2405 */
2406void
2407syst(int argc, char *argv[])
2408{
2409 int oldverbose = verbose;
2410
2411 if (argc == 0) {
2412 UPRINTF("usage: %s\n", argv[0]);
2413 code = -1;
2414 return;
2415 }
2416 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2417 (void)command("SYST");
2418 verbose = oldverbose;
2419}
2420
2421void
2422macdef(int argc, char *argv[])
2423{
2424 char *tmp;
2425 int c;
2426
2427 if (argc == 0)
2428 goto usage;
2429 if (macnum == 16) {
2430 fputs("Limit of 16 macros have already been defined.\n",
2431 ttyout);
2432 code = -1;
2433 return;
2434 }
2435 if ((argc < 2 && !another(&argc, &argv, "macro name")) || argc > 2) {
2436 usage:
2437 UPRINTF("usage: %s macro_name\n", argv[0]);
2438 code = -1;
2439 return;
2440 }
2441 if (interactive)
2442 fputs(
2443 "Enter macro line by line, terminating it with a null line.\n",
2444 ttyout);
2445 (void)strlcpy(macros[macnum].mac_name, argv[1],
2446 sizeof(macros[macnum].mac_name));
2447 if (macnum == 0)
2448 macros[macnum].mac_start = macbuf;
2449 else
2450 macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
2451 tmp = macros[macnum].mac_start;
2452 while (tmp != macbuf+4096) {
2453 if ((c = getchar()) == EOF) {
2454 fputs("macdef: end of file encountered.\n", ttyout);
2455 code = -1;
2456 return;
2457 }
2458 if ((*tmp = c) == '\n') {
2459 if (tmp == macros[macnum].mac_start) {
2460 macros[macnum++].mac_end = tmp;
2461 code = 0;
2462 return;
2463 }
2464 if (*(tmp-1) == '\0') {
2465 macros[macnum++].mac_end = tmp - 1;
2466 code = 0;
2467 return;
2468 }
2469 *tmp = '\0';
2470 }
2471 tmp++;
2472 }
2473 while (1) {
2474 while ((c = getchar()) != '\n' && c != EOF)
2475 /* LOOP */;
2476 if (c == EOF || getchar() == '\n') {
2477 fputs("Macro not defined - 4K buffer exceeded.\n",
2478 ttyout);
2479 code = -1;
2480 return;
2481 }
2482 }
2483}
2484
2485/*
2486 * Get size of file on remote machine
2487 */
2488void
2489sizecmd(int argc, char *argv[])
2490{
2491 off_t size;
2492
2493 if (argc == 0 || argc > 2 ||
2494 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
2495 UPRINTF("usage: %s remote-file\n", argv[0]);
2496 code = -1;
2497 return;
2498 }
2499 size = remotesize(argv[1], 1);
2500 if (size != -1)
2501 fprintf(ttyout,
2502 "%s\t" LLF "\n", argv[1], (LLT)size);
2503 code = (size > 0);
2504}
2505
2506/*
2507 * Get last modification time of file on remote machine
2508 */
2509void
2510modtime(int argc, char *argv[])
2511{
2512 time_t mtime;
2513
2514 if (argc == 0 || argc > 2 ||
2515 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
2516 UPRINTF("usage: %s remote-file\n", argv[0]);
2517 code = -1;
2518 return;
2519 }
2520 mtime = remotemodtime(argv[1], 1);
2521 if (mtime != -1)
c106ca54
PA
2522 fprintf(ttyout, "%s\t%s", argv[1],
2523 rfc2822time(localtime(&mtime)));
eedc536d
PA
2524 code = (mtime > 0);
2525}
2526
2527/*
2528 * Show status on remote machine
2529 */
2530void
2531rmtstatus(int argc, char *argv[])
2532{
2533
2534 if (argc == 0) {
2535 UPRINTF("usage: %s [remote-file]\n", argv[0]);
2536 code = -1;
2537 return;
2538 }
2539 COMMAND_1ARG(argc, argv, "STAT");
2540}
2541
2542/*
2543 * Get file if modtime is more recent than current file
2544 */
2545void
2546newer(int argc, char *argv[])
2547{
2548
2549 if (getit(argc, argv, -1, "w"))
2550 fprintf(ttyout,
2551 "Local file \"%s\" is newer than remote file \"%s\".\n",
2552 argv[2], argv[1]);
2553}
2554
2555/*
2556 * Display one local file through $PAGER.
2557 */
2558void
2559lpage(int argc, char *argv[])
2560{
2561 size_t len;
20e59a13
PA
2562 const char *p;
2563 char *pager, *locfile;
eedc536d
PA
2564
2565 if (argc == 0 || argc > 2 ||
2566 (argc == 1 && !another(&argc, &argv, "local-file"))) {
2567 UPRINTF("usage: %s local-file\n", argv[0]);
2568 code = -1;
2569 return;
2570 }
2571 if ((locfile = globulize(argv[1])) == NULL) {
2572 code = -1;
2573 return;
2574 }
2575 p = getoptionvalue("pager");
2576 if (EMPTYSTRING(p))
2577 p = DEFAULTPAGER;
2578 len = strlen(p) + strlen(locfile) + 2;
2579 pager = ftp_malloc(len);
2580 (void)strlcpy(pager, p, len);
2581 (void)strlcat(pager, " ", len);
2582 (void)strlcat(pager, locfile, len);
2583 system(pager);
2584 code = 0;
2585 (void)free(pager);
2586 (void)free(locfile);
2587}
2588
2589/*
2590 * Display one remote file through $PAGER.
2591 */
2592void
2593page(int argc, char *argv[])
2594{
2595 int ohash, orestart_point, overbose;
2596 size_t len;
20e59a13
PA
2597 const char *p;
2598 char *pager;
eedc536d
PA
2599
2600 if (argc == 0 || argc > 2 ||
2601 (argc == 1 && !another(&argc, &argv, "remote-file"))) {
2602 UPRINTF("usage: %s remote-file\n", argv[0]);
2603 code = -1;
2604 return;
2605 }
2606 p = getoptionvalue("pager");
2607 if (EMPTYSTRING(p))
2608 p = DEFAULTPAGER;
2609 len = strlen(p) + 2;
2610 pager = ftp_malloc(len);
2611 pager[0] = '|';
2612 (void)strlcpy(pager + 1, p, len - 1);
2613
2614 ohash = hash;
2615 orestart_point = restart_point;
2616 overbose = verbose;
2617 hash = restart_point = verbose = 0;
2618 recvrequest("RETR", pager, argv[1], "r+", 1, 0);
2619 hash = ohash;
2620 restart_point = orestart_point;
2621 verbose = overbose;
2622 (void)free(pager);
2623}
2624
2625/*
2626 * Set the socket send or receive buffer size.
2627 */
2628void
2629setxferbuf(int argc, char *argv[])
2630{
2631 int size, dir;
2632
2633 if (argc != 2) {
2634 usage:
2635 UPRINTF("usage: %s size\n", argv[0]);
2636 code = -1;
2637 return;
2638 }
2639 if (strcasecmp(argv[0], "sndbuf") == 0)
2640 dir = RATE_PUT;
2641 else if (strcasecmp(argv[0], "rcvbuf") == 0)
2642 dir = RATE_GET;
2643 else if (strcasecmp(argv[0], "xferbuf") == 0)
2644 dir = RATE_ALL;
2645 else
2646 goto usage;
2647
2648 if ((size = strsuftoi(argv[1])) == -1)
2649 goto usage;
2650
2651 if (size == 0) {
2652 fprintf(ttyout, "%s: size must be positive.\n", argv[0]);
2653 goto usage;
2654 }
2655
2656 if (dir & RATE_PUT)
2657 sndbuf_size = size;
2658 if (dir & RATE_GET)
2659 rcvbuf_size = size;
2660 fprintf(ttyout, "Socket buffer sizes: send %d, receive %d.\n",
2661 sndbuf_size, rcvbuf_size);
2662 code = 0;
2663}
2664
2665/*
2666 * Set or display options (defaults are provided by various env vars)
2667 */
2668void
2669setoption(int argc, char *argv[])
2670{
2671 struct option *o;
2672
2673 code = -1;
2674 if (argc == 0 || (argc != 1 && argc != 3)) {
2675 UPRINTF("usage: %s [option value]\n", argv[0]);
2676 return;
2677 }
2678
a83a5d06 2679#define OPTIONINDENT ((int) sizeof("https_proxy"))
eedc536d
PA
2680 if (argc == 1) {
2681 for (o = optiontab; o->name != NULL; o++) {
2682 fprintf(ttyout, "%-*s\t%s\n", OPTIONINDENT,
2683 o->name, o->value ? o->value : "");
2684 }
2685 } else {
20e59a13 2686 set_option(argv[1], argv[2], 1);
eedc536d
PA
2687 }
2688 code = 0;
2689}
2690
20e59a13
PA
2691void
2692set_option(const char * option, const char * value, int doverbose)
2693{
2694 struct option *o;
2695
2696 o = getoption(option);
2697 if (o == NULL) {
2698 fprintf(ttyout, "No such option `%s'.\n", option);
2699 return;
2700 }
2701 FREEPTR(o->value);
2702 o->value = ftp_strdup(value);
2703 if (verbose && doverbose)
2704 fprintf(ttyout, "Setting `%s' to `%s'.\n",
2705 o->name, o->value);
2706}
2707
eedc536d
PA
2708/*
2709 * Unset an option
2710 */
2711void
2712unsetoption(int argc, char *argv[])
2713{
2714 struct option *o;
2715
2716 code = -1;
2717 if (argc == 0 || argc != 2) {
2718 UPRINTF("usage: %s option\n", argv[0]);
2719 return;
2720 }
2721
2722 o = getoption(argv[1]);
2723 if (o == NULL) {
2724 fprintf(ttyout, "No such option `%s'.\n", argv[1]);
2725 return;
2726 }
2727 FREEPTR(o->value);
2728 fprintf(ttyout, "Unsetting `%s'.\n", o->name);
2729 code = 0;
2730}
2731
2732/*
2733 * Display features supported by the remote host.
2734 */
2735void
2736feat(int argc, char *argv[])
2737{
2738 int oldverbose = verbose;
2739
2740 if (argc == 0) {
2741 UPRINTF("usage: %s\n", argv[0]);
2742 code = -1;
2743 return;
2744 }
2745 if (! features[FEAT_FEAT]) {
2746 fprintf(ttyout,
2747 "FEAT is not supported by the remote server.\n");
2748 return;
2749 }
2750 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2751 (void)command("FEAT");
2752 verbose = oldverbose;
2753}
2754
2755void
2756mlst(int argc, char *argv[])
2757{
2758 int oldverbose = verbose;
2759
2760 if (argc < 1 || argc > 2) {
2761 UPRINTF("usage: %s [remote-path]\n", argv[0]);
2762 code = -1;
2763 return;
2764 }
2765 if (! features[FEAT_MLST]) {
2766 fprintf(ttyout,
2767 "MLST is not supported by the remote server.\n");
2768 return;
2769 }
2770 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2771 COMMAND_1ARG(argc, argv, "MLST");
2772 verbose = oldverbose;
2773}
2774
2775void
2776opts(int argc, char *argv[])
2777{
2778 int oldverbose = verbose;
2779
2780 if (argc < 2 || argc > 3) {
2781 UPRINTF("usage: %s command [options]\n", argv[0]);
2782 code = -1;
2783 return;
2784 }
2785 if (! features[FEAT_FEAT]) {
2786 fprintf(ttyout,
2787 "OPTS is not supported by the remote server.\n");
2788 return;
2789 }
2790 verbose = 1; /* If we aren't verbose, this doesn't do anything! */
2791 if (argc == 2)
2792 command("OPTS %s", argv[1]);
2793 else
2794 command("OPTS %s %s", argv[1], argv[2]);
2795 verbose = oldverbose;
2796}