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