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