Debian Codegraph @

ftp/cmds.c

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