util.c revision 1.153 1 /* $NetBSD: util.c,v 1.153 2010/03/04 21:40:53 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
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.
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 #include <sys/cdefs.h>
66 #ifndef lint
67 __RCSID("$NetBSD: util.c,v 1.153 2010/03/04 21:40:53 lukem Exp $");
68 #endif /* not lint */
69
70 /*
71 * FTP User Program -- Misc support routines
72 */
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/ioctl.h>
76 #include <sys/time.h>
77 #include <netinet/in.h>
78 #include <arpa/ftp.h>
79
80 #include <ctype.h>
81 #include <err.h>
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <glob.h>
85 #include <signal.h>
86 #include <libgen.h>
87 #include <limits.h>
88 #include <locale.h>
89 #include <netdb.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <termios.h>
94 #include <time.h>
95 #include <tzfile.h>
96 #include <unistd.h>
97
98 #include "ftp_var.h"
99
100 /*
101 * Connect to peer server and auto-login, if possible.
102 */
103 void
104 setpeer(int argc, char *argv[])
105 {
106 char *host;
107 const char *port;
108
109 if (argc == 0)
110 goto usage;
111 if (connected) {
112 fprintf(ttyout, "Already connected to %s, use close first.\n",
113 hostname);
114 code = -1;
115 return;
116 }
117 if (argc < 2)
118 (void)another(&argc, &argv, "to");
119 if (argc < 2 || argc > 3) {
120 usage:
121 UPRINTF("usage: %s host-name [port]\n", argv[0]);
122 code = -1;
123 return;
124 }
125 if (gatemode)
126 port = gateport;
127 else
128 port = ftpport;
129 if (argc > 2)
130 port = argv[2];
131
132 if (gatemode) {
133 if (gateserver == NULL || *gateserver == '\0')
134 errx(1, "main: gateserver not defined");
135 host = hookup(gateserver, port);
136 } else
137 host = hookup(argv[1], port);
138
139 if (host) {
140 if (gatemode && verbose) {
141 fprintf(ttyout,
142 "Connecting via pass-through server %s\n",
143 gateserver);
144 }
145
146 connected = 1;
147 /*
148 * Set up defaults for FTP.
149 */
150 (void)strlcpy(typename, "ascii", sizeof(typename));
151 type = TYPE_A;
152 curtype = TYPE_A;
153 (void)strlcpy(formname, "non-print", sizeof(formname));
154 form = FORM_N;
155 (void)strlcpy(modename, "stream", sizeof(modename));
156 mode = MODE_S;
157 (void)strlcpy(structname, "file", sizeof(structname));
158 stru = STRU_F;
159 (void)strlcpy(bytename, "8", sizeof(bytename));
160 bytesize = 8;
161 if (autologin)
162 (void)ftp_login(argv[1], NULL, NULL);
163 }
164 }
165
166 static void
167 parse_feat(const char *fline)
168 {
169
170 /*
171 * work-around broken ProFTPd servers that can't
172 * even obey RFC2389.
173 */
174 while (*fline && isspace((int)*fline))
175 fline++;
176
177 if (strcasecmp(fline, "MDTM") == 0)
178 features[FEAT_MDTM] = 1;
179 else if (strncasecmp(fline, "MLST", sizeof("MLST") - 1) == 0) {
180 features[FEAT_MLST] = 1;
181 } else if (strcasecmp(fline, "REST STREAM") == 0)
182 features[FEAT_REST_STREAM] = 1;
183 else if (strcasecmp(fline, "SIZE") == 0)
184 features[FEAT_SIZE] = 1;
185 else if (strcasecmp(fline, "TVFS") == 0)
186 features[FEAT_TVFS] = 1;
187 }
188
189 /*
190 * Determine the remote system type (SYST) and features (FEAT).
191 * Call after a successful login (i.e, connected = -1)
192 */
193 void
194 getremoteinfo(void)
195 {
196 int overbose, i;
197
198 overbose = verbose;
199 if (ftp_debug == 0)
200 verbose = -1;
201
202 /* determine remote system type */
203 if (command("SYST") == COMPLETE) {
204 if (overbose) {
205 char *cp, c;
206
207 c = 0;
208 cp = strchr(reply_string + 4, ' ');
209 if (cp == NULL)
210 cp = strchr(reply_string + 4, '\r');
211 if (cp) {
212 if (cp[-1] == '.')
213 cp--;
214 c = *cp;
215 *cp = '\0';
216 }
217
218 fprintf(ttyout, "Remote system type is %s.\n",
219 reply_string + 4);
220 if (cp)
221 *cp = c;
222 }
223 if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
224 if (proxy)
225 unix_proxy = 1;
226 else
227 unix_server = 1;
228 /*
229 * Set type to 0 (not specified by user),
230 * meaning binary by default, but don't bother
231 * telling server. We can use binary
232 * for text files unless changed by the user.
233 */
234 type = 0;
235 (void)strlcpy(typename, "binary", sizeof(typename));
236 if (overbose)
237 fprintf(ttyout,
238 "Using %s mode to transfer files.\n",
239 typename);
240 } else {
241 if (proxy)
242 unix_proxy = 0;
243 else
244 unix_server = 0;
245 if (overbose &&
246 !strncmp(reply_string, "215 TOPS20", 10))
247 fputs(
248 "Remember to set tenex mode when transferring binary files from this machine.\n",
249 ttyout);
250 }
251 }
252
253 /* determine features (if any) */
254 for (i = 0; i < FEAT_max; i++)
255 features[i] = -1;
256 reply_callback = parse_feat;
257 if (command("FEAT") == COMPLETE) {
258 for (i = 0; i < FEAT_max; i++) {
259 if (features[i] == -1)
260 features[i] = 0;
261 }
262 features[FEAT_FEAT] = 1;
263 } else
264 features[FEAT_FEAT] = 0;
265 #ifndef NO_DEBUG
266 if (ftp_debug) {
267 #define DEBUG_FEAT(x) fprintf(ttyout, "features[" #x "] = %d\n", features[(x)])
268 DEBUG_FEAT(FEAT_FEAT);
269 DEBUG_FEAT(FEAT_MDTM);
270 DEBUG_FEAT(FEAT_MLST);
271 DEBUG_FEAT(FEAT_REST_STREAM);
272 DEBUG_FEAT(FEAT_SIZE);
273 DEBUG_FEAT(FEAT_TVFS);
274 #undef DEBUG_FEAT
275 }
276 #endif
277 reply_callback = NULL;
278
279 verbose = overbose;
280 }
281
282 /*
283 * Reset the various variables that indicate connection state back to
284 * disconnected settings.
285 * The caller is responsible for issuing any commands to the remote server
286 * to perform a clean shutdown before this is invoked.
287 */
288 void
289 cleanuppeer(void)
290 {
291
292 if (cout)
293 (void)fclose(cout);
294 cout = NULL;
295 connected = 0;
296 unix_server = 0;
297 unix_proxy = 0;
298 /*
299 * determine if anonftp was specifically set with -a
300 * (1), or implicitly set by auto_fetch() (2). in the
301 * latter case, disable after the current xfer
302 */
303 if (anonftp == 2)
304 anonftp = 0;
305 data = -1;
306 epsv4bad = 0;
307 epsv6bad = 0;
308 if (username)
309 free(username);
310 username = NULL;
311 if (!proxy)
312 macnum = 0;
313 }
314
315 /*
316 * Top-level signal handler for interrupted commands.
317 */
318 void
319 intr(int signo)
320 {
321
322 sigint_raised = 1;
323 alarmtimer(0);
324 if (fromatty)
325 write(fileno(ttyout), "\n", 1);
326 siglongjmp(toplevel, 1);
327 }
328
329 /*
330 * Signal handler for lost connections; cleanup various elements of
331 * the connection state, and call cleanuppeer() to finish it off.
332 */
333 void
334 lostpeer(int dummy)
335 {
336 int oerrno = errno;
337
338 alarmtimer(0);
339 if (connected) {
340 if (cout != NULL) {
341 (void)shutdown(fileno(cout), 1+1);
342 (void)fclose(cout);
343 cout = NULL;
344 }
345 if (data >= 0) {
346 (void)shutdown(data, 1+1);
347 (void)close(data);
348 data = -1;
349 }
350 connected = 0;
351 }
352 pswitch(1);
353 if (connected) {
354 if (cout != NULL) {
355 (void)shutdown(fileno(cout), 1+1);
356 (void)fclose(cout);
357 cout = NULL;
358 }
359 connected = 0;
360 }
361 proxflag = 0;
362 pswitch(0);
363 cleanuppeer();
364 errno = oerrno;
365 }
366
367
368 /*
369 * Login to remote host, using given username & password if supplied.
370 * Return non-zero if successful.
371 */
372 int
373 ftp_login(const char *host, const char *luser, const char *lpass)
374 {
375 char tmp[80];
376 char *fuser, *pass, *facct, *p;
377 char emptypass[] = "";
378 const char *errormsg;
379 int n, aflag, rval, nlen;
380
381 aflag = rval = 0;
382 fuser = pass = facct = NULL;
383 if (luser)
384 fuser = ftp_strdup(luser);
385 if (lpass)
386 pass = ftp_strdup(lpass);
387
388 DPRINTF("ftp_login: user `%s' pass `%s' host `%s'\n",
389 STRorNULL(fuser), STRorNULL(pass), STRorNULL(host));
390
391 /*
392 * Set up arguments for an anonymous FTP session, if necessary.
393 */
394 if (anonftp) {
395 FREEPTR(fuser);
396 fuser = ftp_strdup("anonymous"); /* as per RFC1635 */
397 FREEPTR(pass);
398 pass = ftp_strdup(getoptionvalue("anonpass"));
399 }
400
401 if (ruserpass(host, &fuser, &pass, &facct) < 0) {
402 code = -1;
403 goto cleanup_ftp_login;
404 }
405
406 while (fuser == NULL) {
407 if (localname)
408 fprintf(ttyout, "Name (%s:%s): ", host, localname);
409 else
410 fprintf(ttyout, "Name (%s): ", host);
411 errormsg = NULL;
412 nlen = get_line(stdin, tmp, sizeof(tmp), &errormsg);
413 if (nlen < 0) {
414 fprintf(ttyout, "%s; %s aborted.\n", errormsg, "login");
415 code = -1;
416 goto cleanup_ftp_login;
417 } else if (nlen == 0) {
418 fuser = ftp_strdup(localname);
419 } else {
420 fuser = ftp_strdup(tmp);
421 }
422 }
423
424 if (gatemode) {
425 char *nuser;
426 size_t len;
427
428 len = strlen(fuser) + 1 + strlen(host) + 1;
429 nuser = ftp_malloc(len);
430 (void)strlcpy(nuser, fuser, len);
431 (void)strlcat(nuser, "@", len);
432 (void)strlcat(nuser, host, len);
433 FREEPTR(fuser);
434 fuser = nuser;
435 }
436
437 n = command("USER %s", fuser);
438 if (n == CONTINUE) {
439 if (pass == NULL) {
440 p = getpass("Password: ");
441 if (p == NULL)
442 p = emptypass;
443 pass = ftp_strdup(p);
444 memset(p, 0, strlen(p));
445 }
446 n = command("PASS %s", pass);
447 memset(pass, 0, strlen(pass));
448 }
449 if (n == CONTINUE) {
450 aflag++;
451 if (facct == NULL) {
452 p = getpass("Account: ");
453 if (p == NULL)
454 p = emptypass;
455 facct = ftp_strdup(p);
456 memset(p, 0, strlen(p));
457 }
458 if (facct[0] == '\0') {
459 warnx("Login failed");
460 goto cleanup_ftp_login;
461 }
462 n = command("ACCT %s", facct);
463 memset(facct, 0, strlen(facct));
464 }
465 if ((n != COMPLETE) ||
466 (!aflag && facct != NULL && command("ACCT %s", facct) != COMPLETE)) {
467 warnx("Login failed");
468 goto cleanup_ftp_login;
469 }
470 rval = 1;
471 username = ftp_strdup(fuser);
472 if (proxy)
473 goto cleanup_ftp_login;
474
475 connected = -1;
476 getremoteinfo();
477 for (n = 0; n < macnum; ++n) {
478 if (!strcmp("init", macros[n].mac_name)) {
479 (void)strlcpy(line, "$init", sizeof(line));
480 makeargv();
481 domacro(margc, margv);
482 break;
483 }
484 }
485 updatelocalcwd();
486 updateremotecwd();
487
488 cleanup_ftp_login:
489 FREEPTR(fuser);
490 if (pass != NULL)
491 memset(pass, 0, strlen(pass));
492 FREEPTR(pass);
493 if (facct != NULL)
494 memset(facct, 0, strlen(facct));
495 FREEPTR(facct);
496 return (rval);
497 }
498
499 /*
500 * `another' gets another argument, and stores the new argc and argv.
501 * It reverts to the top level (via intr()) on EOF/error.
502 *
503 * Returns false if no new arguments have been added.
504 */
505 int
506 another(int *pargc, char ***pargv, const char *aprompt)
507 {
508 const char *errormsg;
509 int ret, nlen;
510 size_t len;
511
512 len = strlen(line);
513 if (len >= sizeof(line) - 3) {
514 fputs("Sorry, arguments too long.\n", ttyout);
515 intr(0);
516 }
517 fprintf(ttyout, "(%s) ", aprompt);
518 line[len++] = ' ';
519 errormsg = NULL;
520 nlen = get_line(stdin, line + len, sizeof(line)-len, &errormsg);
521 if (nlen < 0) {
522 fprintf(ttyout, "%s; %s aborted.\n", errormsg, "operation");
523 intr(0);
524 }
525 len += nlen;
526 makeargv();
527 ret = margc > *pargc;
528 *pargc = margc;
529 *pargv = margv;
530 return (ret);
531 }
532
533 /*
534 * glob files given in argv[] from the remote server.
535 * if errbuf isn't NULL, store error messages there instead
536 * of writing to the screen.
537 */
538 char *
539 remglob(char *argv[], int doswitch, const char **errbuf)
540 {
541 static char buf[MAXPATHLEN];
542 static FILE *ftemp = NULL;
543 static char **args;
544 char temp[MAXPATHLEN];
545 int oldverbose, oldhash, oldprogress, fd;
546 char *cp;
547 const char *rmode;
548 size_t len;
549
550 if (!mflag || !connected) {
551 if (!doglob)
552 args = NULL;
553 else {
554 if (ftemp) {
555 (void)fclose(ftemp);
556 ftemp = NULL;
557 }
558 }
559 return (NULL);
560 }
561 if (!doglob) {
562 if (args == NULL)
563 args = argv;
564 if ((cp = *++args) == NULL)
565 args = NULL;
566 return (cp);
567 }
568 if (ftemp == NULL) {
569 len = strlcpy(temp, tmpdir, sizeof(temp));
570 if (temp[len - 1] != '/')
571 (void)strlcat(temp, "/", sizeof(temp));
572 (void)strlcat(temp, TMPFILE, sizeof(temp));
573 if ((fd = mkstemp(temp)) < 0) {
574 warn("Unable to create temporary file `%s'", temp);
575 return (NULL);
576 }
577 close(fd);
578 oldverbose = verbose;
579 verbose = (errbuf != NULL) ? -1 : 0;
580 oldhash = hash;
581 oldprogress = progress;
582 hash = 0;
583 progress = 0;
584 if (doswitch)
585 pswitch(!proxy);
586 for (rmode = "w"; *++argv != NULL; rmode = "a")
587 recvrequest("NLST", temp, *argv, rmode, 0, 0);
588 if ((code / 100) != COMPLETE) {
589 if (errbuf != NULL)
590 *errbuf = reply_string;
591 }
592 if (doswitch)
593 pswitch(!proxy);
594 verbose = oldverbose;
595 hash = oldhash;
596 progress = oldprogress;
597 ftemp = fopen(temp, "r");
598 (void)unlink(temp);
599 if (ftemp == NULL) {
600 if (errbuf == NULL)
601 warnx("Can't find list of remote files");
602 else
603 *errbuf =
604 "Can't find list of remote files";
605 return (NULL);
606 }
607 }
608 if (fgets(buf, sizeof(buf), ftemp) == NULL) {
609 (void)fclose(ftemp);
610 ftemp = NULL;
611 return (NULL);
612 }
613 if ((cp = strchr(buf, '\n')) != NULL)
614 *cp = '\0';
615 return (buf);
616 }
617
618 /*
619 * Glob a local file name specification with the expectation of a single
620 * return value. Can't control multiple values being expanded from the
621 * expression, we return only the first.
622 * Returns NULL on error, or a pointer to a buffer containing the filename
623 * that's the caller's responsiblity to free(3) when finished with.
624 */
625 char *
626 globulize(const char *pattern)
627 {
628 glob_t gl;
629 int flags;
630 char *p;
631
632 if (!doglob)
633 return (ftp_strdup(pattern));
634
635 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
636 memset(&gl, 0, sizeof(gl));
637 if (glob(pattern, flags, NULL, &gl) || gl.gl_pathc == 0) {
638 warnx("Glob pattern `%s' not found", pattern);
639 globfree(&gl);
640 return (NULL);
641 }
642 p = ftp_strdup(gl.gl_pathv[0]);
643 globfree(&gl);
644 return (p);
645 }
646
647 /*
648 * determine size of remote file
649 */
650 off_t
651 remotesize(const char *file, int noisy)
652 {
653 int overbose, r;
654 off_t size;
655
656 overbose = verbose;
657 size = -1;
658 if (ftp_debug == 0)
659 verbose = -1;
660 if (! features[FEAT_SIZE]) {
661 if (noisy)
662 fprintf(ttyout,
663 "SIZE is not supported by remote server.\n");
664 goto cleanup_remotesize;
665 }
666 r = command("SIZE %s", file);
667 if (r == COMPLETE) {
668 char *cp, *ep;
669
670 cp = strchr(reply_string, ' ');
671 if (cp != NULL) {
672 cp++;
673 size = STRTOLL(cp, &ep, 10);
674 if (*ep != '\0' && !isspace((unsigned char)*ep))
675 size = -1;
676 }
677 } else {
678 if (r == ERROR && code == 500 && features[FEAT_SIZE] == -1)
679 features[FEAT_SIZE] = 0;
680 if (noisy && ftp_debug == 0) {
681 fputs(reply_string, ttyout);
682 putc('\n', ttyout);
683 }
684 }
685 cleanup_remotesize:
686 verbose = overbose;
687 return (size);
688 }
689
690 /*
691 * determine last modification time (in GMT) of remote file
692 */
693 time_t
694 remotemodtime(const char *file, int noisy)
695 {
696 int overbose, ocode, r;
697 time_t rtime;
698
699 overbose = verbose;
700 ocode = code;
701 rtime = -1;
702 if (ftp_debug == 0)
703 verbose = -1;
704 if (! features[FEAT_MDTM]) {
705 if (noisy)
706 fprintf(ttyout,
707 "MDTM is not supported by remote server.\n");
708 goto cleanup_parse_time;
709 }
710 r = command("MDTM %s", file);
711 if (r == COMPLETE) {
712 struct tm timebuf;
713 char *timestr, *frac;
714
715 /*
716 * time-val = 14DIGIT [ "." 1*DIGIT ]
717 * YYYYMMDDHHMMSS[.sss]
718 * mdtm-response = "213" SP time-val CRLF / error-response
719 */
720 timestr = reply_string + 4;
721
722 /*
723 * parse fraction.
724 * XXX: ignored for now
725 */
726 frac = strchr(timestr, '\r');
727 if (frac != NULL)
728 *frac = '\0';
729 frac = strchr(timestr, '.');
730 if (frac != NULL)
731 *frac++ = '\0';
732 if (strlen(timestr) == 15 && strncmp(timestr, "191", 3) == 0) {
733 /*
734 * XXX: Workaround for lame ftpd's that return
735 * `19100' instead of `2000'
736 */
737 fprintf(ttyout,
738 "Y2K warning! Incorrect time-val `%s' received from server.\n",
739 timestr);
740 timestr++;
741 timestr[0] = '2';
742 timestr[1] = '0';
743 fprintf(ttyout, "Converted to `%s'\n", timestr);
744 }
745 memset(&timebuf, 0, sizeof(timebuf));
746 if (strlen(timestr) != 14 ||
747 (strptime(timestr, "%Y%m%d%H%M%S", &timebuf) == NULL)) {
748 bad_parse_time:
749 fprintf(ttyout, "Can't parse time `%s'.\n", timestr);
750 goto cleanup_parse_time;
751 }
752 timebuf.tm_isdst = -1;
753 rtime = timegm(&timebuf);
754 if (rtime == -1) {
755 if (noisy || ftp_debug != 0)
756 goto bad_parse_time;
757 else
758 goto cleanup_parse_time;
759 } else {
760 DPRINTF("remotemodtime: parsed time `%s' as " LLF
761 ", %s",
762 timestr, (LLT)rtime,
763 rfc2822time(localtime(&rtime)));
764 }
765 } else {
766 if (r == ERROR && code == 500 && features[FEAT_MDTM] == -1)
767 features[FEAT_MDTM] = 0;
768 if (noisy && ftp_debug == 0) {
769 fputs(reply_string, ttyout);
770 putc('\n', ttyout);
771 }
772 }
773 cleanup_parse_time:
774 verbose = overbose;
775 if (rtime == -1)
776 code = ocode;
777 return (rtime);
778 }
779
780 /*
781 * Format tm in an RFC2822 compatible manner, with a trailing \n.
782 * Returns a pointer to a static string containing the result.
783 */
784 const char *
785 rfc2822time(const struct tm *tm)
786 {
787 static char result[50];
788
789 if (strftime(result, sizeof(result),
790 "%a, %d %b %Y %H:%M:%S %z\n", tm) == 0)
791 errx(1, "Can't convert RFC2822 time: buffer too small");
792 return result;
793 }
794
795 /*
796 * Parse HTTP-date as per RFC 2616.
797 * Return a pointer to the next character of the consumed date string,
798 * or NULL if failed.
799 */
800 const char *
801 parse_rfc2616time(struct tm *parsed, const char *httpdate)
802 {
803 const char *t;
804 const char *curlocale;
805
806 /* The representation of %a depends on the current locale. */
807 curlocale = setlocale(LC_TIME, NULL);
808 (void)setlocale(LC_TIME, "C");
809 /* RFC1123 */
810 if ((t = strptime(httpdate, "%a, %d %b %Y %H:%M:%S GMT", parsed)) ||
811 /* RFC0850 */
812 (t = strptime(httpdate, "%a, %d-%b-%y %H:%M:%S GMT", parsed)) ||
813 /* asctime */
814 (t = strptime(httpdate, "%a, %b %d %H:%M:%S %Y", parsed))) {
815 ; /* do nothing */
816 }
817 (void)setlocale(LC_TIME, curlocale);
818 return t;
819 }
820
821 /*
822 * Update global `localcwd', which contains the state of the local cwd
823 */
824 void
825 updatelocalcwd(void)
826 {
827
828 if (getcwd(localcwd, sizeof(localcwd)) == NULL)
829 localcwd[0] = '\0';
830 DPRINTF("updatelocalcwd: got `%s'\n", localcwd);
831 }
832
833 /*
834 * Update global `remotecwd', which contains the state of the remote cwd
835 */
836 void
837 updateremotecwd(void)
838 {
839 int overbose, ocode;
840 size_t i;
841 char *cp;
842
843 overbose = verbose;
844 ocode = code;
845 if (ftp_debug == 0)
846 verbose = -1;
847 if (command("PWD") != COMPLETE)
848 goto badremotecwd;
849 cp = strchr(reply_string, ' ');
850 if (cp == NULL || cp[0] == '\0' || cp[1] != '"')
851 goto badremotecwd;
852 cp += 2;
853 for (i = 0; *cp && i < sizeof(remotecwd) - 1; i++, cp++) {
854 if (cp[0] == '"') {
855 if (cp[1] == '"')
856 cp++;
857 else
858 break;
859 }
860 remotecwd[i] = *cp;
861 }
862 remotecwd[i] = '\0';
863 DPRINTF("updateremotecwd: got `%s'\n", remotecwd);
864 goto cleanupremotecwd;
865 badremotecwd:
866 remotecwd[0]='\0';
867 cleanupremotecwd:
868 verbose = overbose;
869 code = ocode;
870 }
871
872 /*
873 * Ensure file is in or under dir.
874 * Returns 1 if so, 0 if not (or an error occurred).
875 */
876 int
877 fileindir(const char *file, const char *dir)
878 {
879 char parentdirbuf[PATH_MAX+1], *parentdir;
880 char realdir[PATH_MAX+1];
881 size_t dirlen;
882
883 /* determine parent directory of file */
884 (void)strlcpy(parentdirbuf, file, sizeof(parentdirbuf));
885 parentdir = dirname(parentdirbuf);
886 if (strcmp(parentdir, ".") == 0)
887 return 1; /* current directory is ok */
888
889 /* find the directory */
890 if (realpath(parentdir, realdir) == NULL) {
891 warn("Unable to determine real path of `%s'", parentdir);
892 return 0;
893 }
894 if (realdir[0] != '/') /* relative result is ok */
895 return 1;
896 dirlen = strlen(dir);
897 if (strncmp(realdir, dir, dirlen) == 0 &&
898 (realdir[dirlen] == '/' || realdir[dirlen] == '\0'))
899 return 1;
900 return 0;
901 }
902
903 /*
904 * List words in stringlist, vertically arranged
905 */
906 void
907 list_vertical(StringList *sl)
908 {
909 size_t i, j;
910 size_t columns, lines;
911 char *p;
912 size_t w, width;
913
914 width = 0;
915
916 for (i = 0 ; i < sl->sl_cur ; i++) {
917 w = strlen(sl->sl_str[i]);
918 if (w > width)
919 width = w;
920 }
921 width = (width + 8) &~ 7;
922
923 columns = ttywidth / width;
924 if (columns == 0)
925 columns = 1;
926 lines = (sl->sl_cur + columns - 1) / columns;
927 for (i = 0; i < lines; i++) {
928 for (j = 0; j < columns; j++) {
929 p = sl->sl_str[j * lines + i];
930 if (p)
931 fputs(p, ttyout);
932 if (j * lines + i + lines >= sl->sl_cur) {
933 putc('\n', ttyout);
934 break;
935 }
936 if (p) {
937 w = strlen(p);
938 while (w < width) {
939 w = (w + 8) &~ 7;
940 (void)putc('\t', ttyout);
941 }
942 }
943 }
944 }
945 }
946
947 /*
948 * Update the global ttywidth value, using TIOCGWINSZ.
949 */
950 void
951 setttywidth(int a)
952 {
953 struct winsize winsize;
954 int oerrno = errno;
955
956 if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1 &&
957 winsize.ws_col != 0)
958 ttywidth = winsize.ws_col;
959 else
960 ttywidth = 80;
961 errno = oerrno;
962 }
963
964 /*
965 * Change the rate limit up (SIGUSR1) or down (SIGUSR2)
966 */
967 void
968 crankrate(int sig)
969 {
970
971 switch (sig) {
972 case SIGUSR1:
973 if (rate_get)
974 rate_get += rate_get_incr;
975 if (rate_put)
976 rate_put += rate_put_incr;
977 break;
978 case SIGUSR2:
979 if (rate_get && rate_get > rate_get_incr)
980 rate_get -= rate_get_incr;
981 if (rate_put && rate_put > rate_put_incr)
982 rate_put -= rate_put_incr;
983 break;
984 default:
985 err(1, "crankrate invoked with unknown signal: %d", sig);
986 }
987 }
988
989
990 /*
991 * Setup or cleanup EditLine structures
992 */
993 #ifndef NO_EDITCOMPLETE
994 void
995 controlediting(void)
996 {
997 if (editing && el == NULL && hist == NULL) {
998 HistEvent ev;
999 int editmode;
1000
1001 el = el_init(getprogname(), stdin, ttyout, stderr);
1002 /* init editline */
1003 hist = history_init(); /* init the builtin history */
1004 history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
1005 el_set(el, EL_HIST, history, hist); /* use history */
1006
1007 el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
1008 el_set(el, EL_PROMPT, prompt); /* set the prompt functions */
1009 el_set(el, EL_RPROMPT, rprompt);
1010
1011 /* add local file completion, bind to TAB */
1012 el_set(el, EL_ADDFN, "ftp-complete",
1013 "Context sensitive argument completion",
1014 complete);
1015 el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
1016 el_source(el, NULL); /* read ~/.editrc */
1017 if ((el_get(el, EL_EDITMODE, &editmode) != -1) && editmode == 0)
1018 editing = 0; /* the user doesn't want editing,
1019 * so disable, and let statement
1020 * below cleanup */
1021 else
1022 el_set(el, EL_SIGNAL, 1);
1023 }
1024 if (!editing) {
1025 if (hist) {
1026 history_end(hist);
1027 hist = NULL;
1028 }
1029 if (el) {
1030 el_end(el);
1031 el = NULL;
1032 }
1033 }
1034 }
1035 #endif /* !NO_EDITCOMPLETE */
1036
1037 /*
1038 * Convert the string `arg' to an int, which may have an optional SI suffix
1039 * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
1040 */
1041 int
1042 strsuftoi(const char *arg)
1043 {
1044 char *cp;
1045 long val;
1046
1047 if (!isdigit((unsigned char)arg[0]))
1048 return (-1);
1049
1050 val = strtol(arg, &cp, 10);
1051 if (cp != NULL) {
1052 if (cp[0] != '\0' && cp[1] != '\0')
1053 return (-1);
1054 switch (tolower((unsigned char)cp[0])) {
1055 case '\0':
1056 case 'b':
1057 break;
1058 case 'k':
1059 val <<= 10;
1060 break;
1061 case 'm':
1062 val <<= 20;
1063 break;
1064 case 'g':
1065 val <<= 30;
1066 break;
1067 default:
1068 return (-1);
1069 }
1070 }
1071 if (val < 0 || val > INT_MAX)
1072 return (-1);
1073
1074 return (val);
1075 }
1076
1077 /*
1078 * Set up socket buffer sizes before a connection is made.
1079 */
1080 void
1081 setupsockbufsize(int sock)
1082 {
1083
1084 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
1085 (void *)&sndbuf_size, sizeof(sndbuf_size)) == -1)
1086 warn("Unable to set sndbuf size %d", sndbuf_size);
1087
1088 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1089 (void *)&rcvbuf_size, sizeof(rcvbuf_size)) == -1)
1090 warn("Unable to set rcvbuf size %d", rcvbuf_size);
1091 }
1092
1093 /*
1094 * Copy characters from src into dst, \ quoting characters that require it
1095 */
1096 void
1097 ftpvis(char *dst, size_t dstlen, const char *src, size_t srclen)
1098 {
1099 size_t di, si;
1100
1101 for (di = si = 0;
1102 src[si] != '\0' && di < dstlen && si < srclen;
1103 di++, si++) {
1104 switch (src[si]) {
1105 case '\\':
1106 case ' ':
1107 case '\t':
1108 case '\r':
1109 case '\n':
1110 case '"':
1111 dst[di++] = '\\';
1112 if (di >= dstlen)
1113 break;
1114 /* FALLTHROUGH */
1115 default:
1116 dst[di] = src[si];
1117 }
1118 }
1119 dst[di] = '\0';
1120 }
1121
1122 /*
1123 * Copy src into buf (which is len bytes long), expanding % sequences.
1124 */
1125 void
1126 formatbuf(char *buf, size_t len, const char *src)
1127 {
1128 const char *p, *p2, *q;
1129 size_t i;
1130 int op, updirs, pdirs;
1131
1132 #define ADDBUF(x) do { \
1133 if (i >= len - 1) \
1134 goto endbuf; \
1135 buf[i++] = (x); \
1136 } while (0)
1137
1138 p = src;
1139 for (i = 0; *p; p++) {
1140 if (*p != '%') {
1141 ADDBUF(*p);
1142 continue;
1143 }
1144 p++;
1145
1146 switch (op = *p) {
1147
1148 case '/':
1149 case '.':
1150 case 'c':
1151 p2 = connected ? remotecwd : "";
1152 updirs = pdirs = 0;
1153
1154 /* option to determine fixed # of dirs from path */
1155 if (op == '.' || op == 'c') {
1156 int skip;
1157
1158 q = p2;
1159 while (*p2) /* calc # of /'s */
1160 if (*p2++ == '/')
1161 updirs++;
1162 if (p[1] == '0') { /* print <x> or ... */
1163 pdirs = 1;
1164 p++;
1165 }
1166 if (p[1] >= '1' && p[1] <= '9') {
1167 /* calc # to skip */
1168 skip = p[1] - '0';
1169 p++;
1170 } else
1171 skip = 1;
1172
1173 updirs -= skip;
1174 while (skip-- > 0) {
1175 while ((p2 > q) && (*p2 != '/'))
1176 p2--; /* back up */
1177 if (skip && p2 > q)
1178 p2--;
1179 }
1180 if (*p2 == '/' && p2 != q)
1181 p2++;
1182 }
1183
1184 if (updirs > 0 && pdirs) {
1185 if (i >= len - 5)
1186 break;
1187 if (op == '.') {
1188 ADDBUF('.');
1189 ADDBUF('.');
1190 ADDBUF('.');
1191 } else {
1192 ADDBUF('/');
1193 ADDBUF('<');
1194 if (updirs > 9) {
1195 ADDBUF('9');
1196 ADDBUF('+');
1197 } else
1198 ADDBUF('0' + updirs);
1199 ADDBUF('>');
1200 }
1201 }
1202 for (; *p2; p2++)
1203 ADDBUF(*p2);
1204 break;
1205
1206 case 'M':
1207 case 'm':
1208 for (p2 = connected && hostname ? hostname : "-";
1209 *p2 ; p2++) {
1210 if (op == 'm' && *p2 == '.')
1211 break;
1212 ADDBUF(*p2);
1213 }
1214 break;
1215
1216 case 'n':
1217 for (p2 = connected ? username : "-"; *p2 ; p2++)
1218 ADDBUF(*p2);
1219 break;
1220
1221 case '%':
1222 ADDBUF('%');
1223 break;
1224
1225 default: /* display unknown codes literally */
1226 ADDBUF('%');
1227 ADDBUF(op);
1228 break;
1229
1230 }
1231 }
1232 endbuf:
1233 buf[i] = '\0';
1234 }
1235
1236 /*
1237 * Determine if given string is an IPv6 address or not.
1238 * Return 1 for yes, 0 for no
1239 */
1240 int
1241 isipv6addr(const char *addr)
1242 {
1243 int rv = 0;
1244 #ifdef INET6
1245 struct addrinfo hints, *res;
1246
1247 memset(&hints, 0, sizeof(hints));
1248 hints.ai_family = AF_INET6;
1249 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
1250 hints.ai_flags = AI_NUMERICHOST;
1251 if (getaddrinfo(addr, "0", &hints, &res) != 0)
1252 rv = 0;
1253 else {
1254 rv = 1;
1255 freeaddrinfo(res);
1256 }
1257 DPRINTF("isipv6addr: got %d for %s\n", rv, addr);
1258 #endif
1259 return (rv == 1) ? 1 : 0;
1260 }
1261
1262 /*
1263 * Read a line from the FILE stream into buf/buflen using fgets(), so up
1264 * to buflen-1 chars will be read and the result will be NUL terminated.
1265 * If the line has a trailing newline it will be removed.
1266 * If the line is too long, excess characters will be read until
1267 * newline/EOF/error.
1268 * If EOF/error occurs or a too-long line is encountered and errormsg
1269 * isn't NULL, it will be changed to a description of the problem.
1270 * (The EOF message has a leading \n for cosmetic purposes).
1271 * Returns:
1272 * >=0 length of line (excluding trailing newline) if all ok
1273 * -1 error occurred
1274 * -2 EOF encountered
1275 * -3 line was too long
1276 */
1277 int
1278 get_line(FILE *stream, char *buf, size_t buflen, const char **errormsg)
1279 {
1280 int rv, ch;
1281 size_t len;
1282
1283 if (fgets(buf, buflen, stream) == NULL) {
1284 if (feof(stream)) { /* EOF */
1285 rv = -2;
1286 if (errormsg)
1287 *errormsg = "\nEOF received";
1288 } else { /* error */
1289 rv = -1;
1290 if (errormsg)
1291 *errormsg = "Error encountered";
1292 }
1293 clearerr(stream);
1294 return rv;
1295 }
1296 len = strlen(buf);
1297 if (buf[len-1] == '\n') { /* clear any trailing newline */
1298 buf[--len] = '\0';
1299 } else if (len == buflen-1) { /* line too long */
1300 while ((ch = getchar()) != '\n' && ch != EOF)
1301 continue;
1302 if (errormsg)
1303 *errormsg = "Input line is too long";
1304 clearerr(stream);
1305 return -3;
1306 }
1307 if (errormsg)
1308 *errormsg = NULL;
1309 return len;
1310 }
1311
1312 /*
1313 * Internal version of connect(2); sets socket buffer sizes,
1314 * binds to a specific local address (if set), and
1315 * supports a connection timeout using a non-blocking connect(2) with
1316 * a poll(2).
1317 * Socket fcntl flags are temporarily updated to include O_NONBLOCK;
1318 * these will not be reverted on connection failure.
1319 * Returns 0 on success, or -1 upon failure (with an appropriate
1320 * error message displayed.)
1321 */
1322 int
1323 ftp_connect(int sock, const struct sockaddr *name, socklen_t namelen)
1324 {
1325 int flags, rv, timeout, error;
1326 socklen_t slen;
1327 struct timeval endtime, now, td;
1328 struct pollfd pfd[1];
1329 char hname[NI_MAXHOST];
1330 char sname[NI_MAXSERV];
1331
1332 setupsockbufsize(sock);
1333 if (getnameinfo(name, namelen,
1334 hname, sizeof(hname), sname, sizeof(sname),
1335 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
1336 strlcpy(hname, "?", sizeof(hname));
1337 strlcpy(sname, "?", sizeof(sname));
1338 }
1339
1340 if (bindai != NULL) { /* bind to specific addr */
1341 struct addrinfo *ai;
1342
1343 for (ai = bindai; ai != NULL; ai = ai->ai_next) {
1344 if (ai->ai_family == name->sa_family)
1345 break;
1346 }
1347 if (ai == NULL)
1348 ai = bindai;
1349 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1350 char bname[NI_MAXHOST];
1351 int saveerr;
1352
1353 saveerr = errno;
1354 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
1355 bname, sizeof(bname), NULL, 0, NI_NUMERICHOST) != 0)
1356 strlcpy(bname, "?", sizeof(bname));
1357 errno = saveerr;
1358 warn("Can't bind to `%s'", bname);
1359 return -1;
1360 }
1361 }
1362
1363 /* save current socket flags */
1364 if ((flags = fcntl(sock, F_GETFL, 0)) == -1) {
1365 warn("Can't %s socket flags for connect to `%s:%s'",
1366 "save", hname, sname);
1367 return -1;
1368 }
1369 /* set non-blocking connect */
1370 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) {
1371 warn("Can't set socket non-blocking for connect to `%s:%s'",
1372 hname, sname);
1373 return -1;
1374 }
1375
1376 /* NOTE: we now must restore socket flags on successful exit */
1377
1378 pfd[0].fd = sock;
1379 pfd[0].events = POLLIN|POLLOUT;
1380
1381 if (quit_time > 0) { /* want a non default timeout */
1382 (void)gettimeofday(&endtime, NULL);
1383 endtime.tv_sec += quit_time; /* determine end time */
1384 }
1385
1386 rv = connect(sock, name, namelen); /* inititate the connection */
1387 if (rv == -1) { /* connection error */
1388 if (errno != EINPROGRESS) { /* error isn't "please wait" */
1389 connecterror:
1390 warn("Can't connect to `%s:%s'", hname, sname);
1391 return -1;
1392 }
1393
1394 /* connect EINPROGRESS; wait */
1395 do {
1396 if (quit_time > 0) { /* determine timeout */
1397 (void)gettimeofday(&now, NULL);
1398 timersub(&endtime, &now, &td);
1399 timeout = td.tv_sec * 1000 + td.tv_usec/1000;
1400 if (timeout < 0)
1401 timeout = 0;
1402 } else {
1403 timeout = INFTIM;
1404 }
1405 pfd[0].revents = 0;
1406 rv = ftp_poll(pfd, 1, timeout);
1407 /* loop until poll ! EINTR */
1408 } while (rv == -1 && errno == EINTR);
1409
1410 if (rv == 0) { /* poll (connect) timed out */
1411 errno = ETIMEDOUT;
1412 goto connecterror;
1413 }
1414
1415 if (rv == -1) { /* poll error */
1416 goto connecterror;
1417 } else if (pfd[0].revents & (POLLIN|POLLOUT)) {
1418 slen = sizeof(error); /* OK, or pending error */
1419 if (getsockopt(sock, SOL_SOCKET, SO_ERROR,
1420 &error, &slen) == -1) {
1421 /* Solaris pending error */
1422 goto connecterror;
1423 } else if (error != 0) {
1424 errno = error; /* BSD pending error */
1425 goto connecterror;
1426 }
1427 } else {
1428 errno = EBADF; /* this shouldn't happen ... */
1429 goto connecterror;
1430 }
1431 }
1432
1433 if (fcntl(sock, F_SETFL, flags) == -1) {
1434 /* restore socket flags */
1435 warn("Can't %s socket flags for connect to `%s:%s'",
1436 "restore", hname, sname);
1437 return -1;
1438 }
1439 return 0;
1440 }
1441
1442 /*
1443 * Internal version of listen(2); sets socket buffer sizes first.
1444 */
1445 int
1446 ftp_listen(int sock, int backlog)
1447 {
1448
1449 setupsockbufsize(sock);
1450 return (listen(sock, backlog));
1451 }
1452
1453 /*
1454 * Internal version of poll(2), to allow reimplementation by select(2)
1455 * on platforms without the former.
1456 */
1457 int
1458 ftp_poll(struct pollfd *fds, int nfds, int timeout)
1459 {
1460 return poll(fds, nfds, timeout);
1461 }
1462
1463 /*
1464 * malloc() with inbuilt error checking
1465 */
1466 void *
1467 ftp_malloc(size_t size)
1468 {
1469 void *p;
1470
1471 p = malloc(size);
1472 if (p == NULL)
1473 err(1, "Unable to allocate %ld bytes of memory", (long)size);
1474 return (p);
1475 }
1476
1477 /*
1478 * sl_init() with inbuilt error checking
1479 */
1480 StringList *
1481 ftp_sl_init(void)
1482 {
1483 StringList *p;
1484
1485 p = sl_init();
1486 if (p == NULL)
1487 err(1, "Unable to allocate memory for stringlist");
1488 return (p);
1489 }
1490
1491 /*
1492 * sl_add() with inbuilt error checking
1493 */
1494 void
1495 ftp_sl_add(StringList *sl, char *i)
1496 {
1497
1498 if (sl_add(sl, i) == -1)
1499 err(1, "Unable to add `%s' to stringlist", i);
1500 }
1501
1502 /*
1503 * strdup() with inbuilt error checking
1504 */
1505 char *
1506 ftp_strdup(const char *str)
1507 {
1508 char *s;
1509
1510 if (str == NULL)
1511 errx(1, "ftp_strdup: called with NULL argument");
1512 s = strdup(str);
1513 if (s == NULL)
1514 err(1, "Unable to allocate memory for string copy");
1515 return (s);
1516 }
1517