Home | History | Annotate | Line # | Download | only in ftp
util.c revision 1.111
      1  1.111     lukem /*	$NetBSD: util.c,v 1.111 2003/02/28 09:54:51 lukem Exp $	*/
      2   1.27   thorpej 
      3   1.27   thorpej /*-
      4  1.111     lukem  * Copyright (c) 1997-2003 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  * 3. All advertising materials mentioning features or use of this software
     23   1.27   thorpej  *    must display the following acknowledgement:
     24   1.27   thorpej  *	This product includes software developed by the NetBSD
     25   1.27   thorpej  *	Foundation, Inc. and its contributors.
     26   1.27   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     27   1.27   thorpej  *    contributors may be used to endorse or promote products derived
     28   1.27   thorpej  *    from this software without specific prior written permission.
     29   1.27   thorpej  *
     30   1.27   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     31   1.27   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32   1.27   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33   1.27   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     34   1.27   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35   1.27   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36   1.27   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37   1.27   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38   1.27   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39   1.27   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40   1.27   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     41   1.27   thorpej  */
     42    1.1     lukem 
     43    1.1     lukem /*
     44    1.1     lukem  * Copyright (c) 1985, 1989, 1993, 1994
     45    1.1     lukem  *	The Regents of the University of California.  All rights reserved.
     46    1.1     lukem  *
     47    1.1     lukem  * Redistribution and use in source and binary forms, with or without
     48    1.1     lukem  * modification, are permitted provided that the following conditions
     49    1.1     lukem  * are met:
     50    1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     51    1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     52    1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     53    1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     54    1.1     lukem  *    documentation and/or other materials provided with the distribution.
     55    1.1     lukem  * 3. All advertising materials mentioning features or use of this software
     56    1.1     lukem  *    must display the following acknowledgement:
     57    1.1     lukem  *	This product includes software developed by the University of
     58    1.1     lukem  *	California, Berkeley and its contributors.
     59    1.1     lukem  * 4. Neither the name of the University nor the names of its contributors
     60    1.1     lukem  *    may be used to endorse or promote products derived from this software
     61    1.1     lukem  *    without specific prior written permission.
     62    1.1     lukem  *
     63    1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64    1.1     lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65    1.1     lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66    1.1     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67    1.1     lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68    1.1     lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69    1.1     lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70    1.1     lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71    1.1     lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72    1.1     lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73    1.1     lukem  * SUCH DAMAGE.
     74    1.1     lukem  */
     75    1.1     lukem 
     76   1.10     lukem #include <sys/cdefs.h>
     77    1.1     lukem #ifndef lint
     78  1.111     lukem __RCSID("$NetBSD: util.c,v 1.111 2003/02/28 09:54:51 lukem Exp $");
     79    1.1     lukem #endif /* not lint */
     80    1.1     lukem 
     81    1.1     lukem /*
     82    1.1     lukem  * FTP User Program -- Misc support routines
     83    1.1     lukem  */
     84   1.27   thorpej #include <sys/types.h>
     85   1.27   thorpej #include <sys/socket.h>
     86    1.1     lukem #include <sys/ioctl.h>
     87    1.1     lukem #include <sys/time.h>
     88   1.78     lukem #include <netinet/in.h>
     89    1.1     lukem #include <arpa/ftp.h>
     90    1.1     lukem 
     91    1.1     lukem #include <ctype.h>
     92    1.1     lukem #include <err.h>
     93   1.53     lukem #include <errno.h>
     94    1.1     lukem #include <fcntl.h>
     95    1.1     lukem #include <glob.h>
     96   1.23  christos #include <signal.h>
     97   1.10     lukem #include <limits.h>
     98  1.102     lukem #include <netdb.h>
     99    1.1     lukem #include <stdio.h>
    100   1.10     lukem #include <stdlib.h>
    101    1.1     lukem #include <string.h>
    102   1.68     lukem #include <termios.h>
    103    1.1     lukem #include <time.h>
    104   1.17     lukem #include <tzfile.h>
    105    1.1     lukem #include <unistd.h>
    106    1.1     lukem 
    107    1.1     lukem #include "ftp_var.h"
    108    1.1     lukem 
    109    1.1     lukem /*
    110   1.96     lukem  * Connect to peer server and auto-login, if possible.
    111    1.1     lukem  */
    112    1.1     lukem void
    113   1.95     lukem setpeer(int argc, char *argv[])
    114    1.1     lukem {
    115    1.1     lukem 	char *host;
    116   1.54    itojun 	char *port;
    117    1.1     lukem 
    118   1.79     lukem 	if (argc == 0)
    119   1.79     lukem 		goto usage;
    120    1.1     lukem 	if (connected) {
    121   1.24     lukem 		fprintf(ttyout, "Already connected to %s, use close first.\n",
    122    1.5     lukem 		    hostname);
    123    1.1     lukem 		code = -1;
    124    1.1     lukem 		return;
    125    1.1     lukem 	}
    126    1.1     lukem 	if (argc < 2)
    127    1.5     lukem 		(void)another(&argc, &argv, "to");
    128    1.1     lukem 	if (argc < 2 || argc > 3) {
    129   1.79     lukem  usage:
    130   1.24     lukem 		fprintf(ttyout, "usage: %s host-name [port]\n", argv[0]);
    131    1.1     lukem 		code = -1;
    132    1.1     lukem 		return;
    133    1.1     lukem 	}
    134   1.12     lukem 	if (gatemode)
    135   1.12     lukem 		port = gateport;
    136   1.12     lukem 	else
    137   1.12     lukem 		port = ftpport;
    138   1.54    itojun 	if (argc > 2)
    139   1.80     lukem 		port = argv[2];
    140   1.12     lukem 
    141   1.12     lukem 	if (gatemode) {
    142   1.12     lukem 		if (gateserver == NULL || *gateserver == '\0')
    143   1.12     lukem 			errx(1, "gateserver not defined (shouldn't happen)");
    144   1.12     lukem 		host = hookup(gateserver, port);
    145   1.12     lukem 	} else
    146   1.12     lukem 		host = hookup(argv[1], port);
    147   1.12     lukem 
    148    1.1     lukem 	if (host) {
    149   1.49     lukem 		if (gatemode && verbose) {
    150   1.49     lukem 			fprintf(ttyout,
    151   1.49     lukem 			    "Connecting via pass-through server %s\n",
    152   1.49     lukem 			    gateserver);
    153   1.12     lukem 		}
    154   1.12     lukem 
    155    1.1     lukem 		connected = 1;
    156    1.1     lukem 		/*
    157    1.1     lukem 		 * Set up defaults for FTP.
    158    1.1     lukem 		 */
    159   1.65     lukem 		(void)strlcpy(typename, "ascii", sizeof(typename));
    160   1.65     lukem 		type = TYPE_A;
    161    1.1     lukem 		curtype = TYPE_A;
    162   1.65     lukem 		(void)strlcpy(formname, "non-print", sizeof(formname));
    163   1.65     lukem 		form = FORM_N;
    164   1.65     lukem 		(void)strlcpy(modename, "stream", sizeof(modename));
    165   1.65     lukem 		mode = MODE_S;
    166   1.65     lukem 		(void)strlcpy(structname, "file", sizeof(structname));
    167   1.65     lukem 		stru = STRU_F;
    168   1.65     lukem 		(void)strlcpy(bytename, "8", sizeof(bytename));
    169   1.65     lukem 		bytesize = 8;
    170    1.1     lukem 		if (autologin)
    171   1.28     lukem 			(void)ftp_login(argv[1], NULL, NULL);
    172   1.96     lukem 	}
    173   1.96     lukem }
    174   1.96     lukem 
    175   1.97     lukem static void
    176   1.97     lukem parse_feat(const char *line)
    177   1.97     lukem {
    178   1.97     lukem 
    179   1.97     lukem 	if (strcasecmp(line, " MDTM") == 0)
    180   1.97     lukem 		features[FEAT_MDTM] = 1;
    181   1.97     lukem 	else if (strncasecmp(line, " MLST", sizeof(" MLST") - 1) == 0) {
    182   1.97     lukem 		features[FEAT_MLST] = 1;
    183   1.97     lukem 	} else if (strcasecmp(line, " REST STREAM") == 0)
    184   1.97     lukem 		features[FEAT_REST_STREAM] = 1;
    185   1.97     lukem 	else if (strcasecmp(line, " SIZE") == 0)
    186   1.97     lukem 		features[FEAT_SIZE] = 1;
    187   1.97     lukem 	else if (strcasecmp(line, " TVFS") == 0)
    188   1.97     lukem 		features[FEAT_TVFS] = 1;
    189   1.97     lukem }
    190   1.97     lukem 
    191   1.96     lukem /*
    192   1.97     lukem  * Determine the remote system type (SYST) and features (FEAT).
    193   1.96     lukem  * Call after a successful login (i.e, connected = -1)
    194   1.96     lukem  */
    195   1.96     lukem void
    196   1.97     lukem getremoteinfo(void)
    197   1.96     lukem {
    198   1.97     lukem 	int overbose, i;
    199    1.1     lukem 
    200   1.96     lukem 	overbose = verbose;
    201   1.96     lukem 	if (debug == 0)
    202   1.96     lukem 		verbose = -1;
    203   1.97     lukem 
    204   1.97     lukem 			/* determine remote system type */
    205   1.97     lukem 	if (command("SYST") == COMPLETE) {
    206   1.97     lukem 		if (overbose) {
    207   1.97     lukem 			char *cp, c;
    208   1.97     lukem 
    209   1.97     lukem 			c = 0;
    210   1.97     lukem 			cp = strchr(reply_string + 4, ' ');
    211   1.97     lukem 			if (cp == NULL)
    212   1.97     lukem 				cp = strchr(reply_string + 4, '\r');
    213   1.97     lukem 			if (cp) {
    214   1.97     lukem 				if (cp[-1] == '.')
    215   1.97     lukem 					cp--;
    216   1.97     lukem 				c = *cp;
    217   1.97     lukem 				*cp = '\0';
    218   1.97     lukem 			}
    219   1.97     lukem 
    220   1.97     lukem 			fprintf(ttyout, "Remote system type is %s.\n",
    221   1.97     lukem 			    reply_string + 4);
    222   1.97     lukem 			if (cp)
    223   1.97     lukem 				*cp = c;
    224   1.97     lukem 		}
    225   1.97     lukem 		if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
    226   1.97     lukem 			if (proxy)
    227   1.97     lukem 				unix_proxy = 1;
    228   1.97     lukem 			else
    229   1.97     lukem 				unix_server = 1;
    230   1.97     lukem 			/*
    231   1.97     lukem 			 * Set type to 0 (not specified by user),
    232   1.97     lukem 			 * meaning binary by default, but don't bother
    233   1.97     lukem 			 * telling server.  We can use binary
    234   1.97     lukem 			 * for text files unless changed by the user.
    235   1.97     lukem 			 */
    236   1.97     lukem 			type = 0;
    237   1.97     lukem 			(void)strlcpy(typename, "binary", sizeof(typename));
    238   1.97     lukem 			if (overbose)
    239   1.97     lukem 			    fprintf(ttyout,
    240   1.97     lukem 				"Using %s mode to transfer files.\n",
    241   1.97     lukem 				typename);
    242   1.97     lukem 		} else {
    243   1.97     lukem 			if (proxy)
    244   1.97     lukem 				unix_proxy = 0;
    245   1.97     lukem 			else
    246   1.97     lukem 				unix_server = 0;
    247   1.97     lukem 			if (overbose &&
    248   1.97     lukem 			    !strncmp(reply_string, "215 TOPS20", 10))
    249   1.97     lukem 				fputs(
    250   1.24     lukem "Remember to set tenex mode when transferring binary files from this machine.\n",
    251   1.97     lukem 				    ttyout);
    252   1.97     lukem 		}
    253    1.1     lukem 	}
    254   1.97     lukem 
    255   1.97     lukem 			/* determine features (if any) */
    256   1.97     lukem 	for (i = 0; i < FEAT_max; i++)
    257   1.97     lukem 		features[i] = -1;
    258   1.97     lukem 	reply_callback = parse_feat;
    259   1.97     lukem 	if (command("FEAT") == COMPLETE) {
    260   1.97     lukem 		for (i = 0; i < FEAT_max; i++) {
    261   1.97     lukem 			if (features[i] == -1)
    262   1.97     lukem 				features[i] = 0;
    263   1.97     lukem 		}
    264   1.97     lukem 		features[FEAT_FEAT] = 1;
    265   1.97     lukem 	} else
    266   1.97     lukem 		features[FEAT_FEAT] = 0;
    267   1.97     lukem 	if (debug)
    268   1.97     lukem 		for (i = 0; i < FEAT_max; i++)
    269   1.97     lukem 			printf("features[%d] = %d\n", i, features[i]);
    270   1.97     lukem 	reply_callback = NULL;
    271   1.97     lukem 
    272   1.96     lukem 	verbose = overbose;
    273    1.7     lukem }
    274    1.7     lukem 
    275    1.7     lukem /*
    276   1.79     lukem  * Reset the various variables that indicate connection state back to
    277   1.79     lukem  * disconnected settings.
    278   1.79     lukem  * The caller is responsible for issuing any commands to the remote server
    279   1.79     lukem  * to perform a clean shutdown before this is invoked.
    280   1.79     lukem  */
    281   1.79     lukem void
    282   1.96     lukem cleanuppeer(void)
    283   1.79     lukem {
    284   1.79     lukem 
    285   1.79     lukem 	if (cout)
    286   1.79     lukem 		(void)fclose(cout);
    287   1.79     lukem 	cout = NULL;
    288   1.79     lukem 	connected = 0;
    289   1.96     lukem 	unix_server = 0;
    290   1.96     lukem 	unix_proxy = 0;
    291   1.79     lukem 			/*
    292   1.79     lukem 			 * determine if anonftp was specifically set with -a
    293   1.79     lukem 			 * (1), or implicitly set by auto_fetch() (2). in the
    294   1.79     lukem 			 * latter case, disable after the current xfer
    295   1.79     lukem 			 */
    296   1.79     lukem 	if (anonftp == 2)
    297   1.79     lukem 		anonftp = 0;
    298   1.79     lukem 	data = -1;
    299   1.79     lukem 	epsv4bad = 0;
    300   1.81     lukem 	if (username)
    301   1.81     lukem 		free(username);
    302   1.84     lukem 	username = NULL;
    303   1.79     lukem 	if (!proxy)
    304   1.79     lukem 		macnum = 0;
    305   1.79     lukem }
    306   1.79     lukem 
    307   1.79     lukem /*
    308   1.79     lukem  * Top-level signal handler for interrupted commands.
    309   1.79     lukem  */
    310   1.79     lukem void
    311   1.95     lukem intr(int dummy)
    312   1.79     lukem {
    313   1.79     lukem 
    314   1.79     lukem 	alarmtimer(0);
    315   1.79     lukem 	if (fromatty)
    316   1.79     lukem 		write(fileno(ttyout), "\n", 1);
    317   1.79     lukem 	siglongjmp(toplevel, 1);
    318   1.79     lukem }
    319   1.79     lukem 
    320   1.79     lukem /*
    321   1.79     lukem  * Signal handler for lost connections; cleanup various elements of
    322   1.79     lukem  * the connection state, and call cleanuppeer() to finish it off.
    323   1.79     lukem  */
    324   1.79     lukem void
    325   1.95     lukem lostpeer(int dummy)
    326   1.79     lukem {
    327   1.79     lukem 	int oerrno = errno;
    328   1.79     lukem 
    329   1.79     lukem 	alarmtimer(0);
    330   1.79     lukem 	if (connected) {
    331   1.79     lukem 		if (cout != NULL) {
    332   1.79     lukem 			(void)shutdown(fileno(cout), 1+1);
    333   1.79     lukem 			(void)fclose(cout);
    334   1.79     lukem 			cout = NULL;
    335   1.79     lukem 		}
    336   1.79     lukem 		if (data >= 0) {
    337   1.79     lukem 			(void)shutdown(data, 1+1);
    338   1.79     lukem 			(void)close(data);
    339   1.79     lukem 			data = -1;
    340   1.79     lukem 		}
    341   1.79     lukem 		connected = 0;
    342   1.79     lukem 	}
    343   1.79     lukem 	pswitch(1);
    344   1.79     lukem 	if (connected) {
    345   1.79     lukem 		if (cout != NULL) {
    346   1.79     lukem 			(void)shutdown(fileno(cout), 1+1);
    347   1.79     lukem 			(void)fclose(cout);
    348   1.79     lukem 			cout = NULL;
    349   1.79     lukem 		}
    350   1.79     lukem 		connected = 0;
    351   1.79     lukem 	}
    352   1.79     lukem 	proxflag = 0;
    353   1.79     lukem 	pswitch(0);
    354   1.79     lukem 	cleanuppeer();
    355   1.79     lukem 	errno = oerrno;
    356   1.79     lukem }
    357   1.79     lukem 
    358   1.79     lukem 
    359   1.79     lukem /*
    360   1.96     lukem  * Login to remote host, using given username & password if supplied.
    361   1.96     lukem  * Return non-zero if successful.
    362    1.7     lukem  */
    363    1.7     lukem int
    364   1.95     lukem ftp_login(const char *host, const char *user, const char *pass)
    365    1.7     lukem {
    366    1.7     lukem 	char tmp[80];
    367   1.29   mycroft 	const char *acct;
    368   1.79     lukem 	int n, aflag, rval, freeuser, freepass, freeacct;
    369    1.7     lukem 
    370    1.7     lukem 	acct = NULL;
    371   1.47     lukem 	aflag = rval = freeuser = freepass = freeacct = 0;
    372    1.7     lukem 
    373   1.83     lukem 	if (debug)
    374   1.83     lukem 		fprintf(ttyout, "ftp_login: user `%s' pass `%s' host `%s'\n",
    375   1.83     lukem 		    user ? user : "<null>", pass ? pass : "<null>",
    376   1.83     lukem 		    host ? host : "<null>");
    377   1.83     lukem 
    378   1.83     lukem 
    379    1.7     lukem 	/*
    380    1.7     lukem 	 * Set up arguments for an anonymous FTP session, if necessary.
    381    1.7     lukem 	 */
    382   1.47     lukem 	if (anonftp) {
    383    1.9     lukem 		user = "anonymous";	/* as per RFC 1635 */
    384   1.79     lukem 		pass = getoptionvalue("anonpass");
    385    1.7     lukem 	}
    386    1.7     lukem 
    387   1.53     lukem 	if (user == NULL)
    388   1.47     lukem 		freeuser = 1;
    389   1.53     lukem 	if (pass == NULL)
    390   1.53     lukem 		freepass = 1;
    391   1.53     lukem 	freeacct = 1;
    392   1.53     lukem 	if (ruserpass(host, &user, &pass, &acct) < 0) {
    393   1.53     lukem 		code = -1;
    394   1.53     lukem 		goto cleanup_ftp_login;
    395   1.47     lukem 	}
    396   1.47     lukem 
    397    1.7     lukem 	while (user == NULL) {
    398  1.103     lukem 		if (localname)
    399  1.103     lukem 			fprintf(ttyout, "Name (%s:%s): ", host, localname);
    400    1.7     lukem 		else
    401   1.24     lukem 			fprintf(ttyout, "Name (%s): ", host);
    402   1.19     lukem 		*tmp = '\0';
    403   1.49     lukem 		if (fgets(tmp, sizeof(tmp) - 1, stdin) == NULL) {
    404   1.49     lukem 			fprintf(ttyout, "\nEOF received; login aborted.\n");
    405   1.77     lukem 			clearerr(stdin);
    406   1.49     lukem 			code = -1;
    407   1.49     lukem 			goto cleanup_ftp_login;
    408   1.49     lukem 		}
    409    1.7     lukem 		tmp[strlen(tmp) - 1] = '\0';
    410   1.51  christos 		freeuser = 0;
    411    1.7     lukem 		if (*tmp == '\0')
    412  1.103     lukem 			user = localname;
    413    1.7     lukem 		else
    414    1.7     lukem 			user = tmp;
    415    1.7     lukem 	}
    416   1.49     lukem 
    417   1.49     lukem 	if (gatemode) {
    418   1.49     lukem 		char *nuser;
    419   1.49     lukem 		int len;
    420   1.49     lukem 
    421   1.49     lukem 		len = strlen(user) + 1 + strlen(host) + 1;
    422   1.49     lukem 		nuser = xmalloc(len);
    423   1.65     lukem 		(void)strlcpy(nuser, user, len);
    424   1.65     lukem 		(void)strlcat(nuser, "@",  len);
    425   1.65     lukem 		(void)strlcat(nuser, host, len);
    426   1.51  christos 		freeuser = 1;
    427   1.49     lukem 		user = nuser;
    428   1.49     lukem 	}
    429   1.49     lukem 
    430    1.7     lukem 	n = command("USER %s", user);
    431    1.7     lukem 	if (n == CONTINUE) {
    432   1.51  christos 		if (pass == NULL) {
    433   1.51  christos 			freepass = 0;
    434    1.7     lukem 			pass = getpass("Password:");
    435   1.51  christos 		}
    436    1.7     lukem 		n = command("PASS %s", pass);
    437    1.7     lukem 	}
    438    1.7     lukem 	if (n == CONTINUE) {
    439    1.7     lukem 		aflag++;
    440   1.47     lukem 		if (acct == NULL) {
    441   1.51  christos 			freeacct = 0;
    442    1.7     lukem 			acct = getpass("Account:");
    443   1.49     lukem 		}
    444   1.49     lukem 		if (acct[0] == '\0') {
    445   1.49     lukem 			warnx("Login failed.");
    446   1.49     lukem 			goto cleanup_ftp_login;
    447   1.47     lukem 		}
    448    1.7     lukem 		n = command("ACCT %s", acct);
    449    1.7     lukem 	}
    450    1.7     lukem 	if ((n != COMPLETE) ||
    451    1.7     lukem 	    (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
    452    1.7     lukem 		warnx("Login failed.");
    453   1.47     lukem 		goto cleanup_ftp_login;
    454   1.47     lukem 	}
    455   1.48  matthias 	rval = 1;
    456   1.81     lukem 	username = xstrdup(user);
    457   1.48  matthias 	if (proxy)
    458   1.47     lukem 		goto cleanup_ftp_login;
    459   1.48  matthias 
    460    1.7     lukem 	connected = -1;
    461   1.97     lukem 	getremoteinfo();
    462    1.7     lukem 	for (n = 0; n < macnum; ++n) {
    463    1.7     lukem 		if (!strcmp("init", macros[n].mac_name)) {
    464   1.65     lukem 			(void)strlcpy(line, "$init", sizeof(line));
    465    1.7     lukem 			makeargv();
    466    1.7     lukem 			domacro(margc, margv);
    467    1.7     lukem 			break;
    468    1.7     lukem 		}
    469    1.7     lukem 	}
    470   1.83     lukem 	updateremotepwd();
    471   1.83     lukem 
    472   1.97     lukem  cleanup_ftp_login:
    473   1.47     lukem 	if (user != NULL && freeuser)
    474   1.47     lukem 		free((char *)user);
    475   1.47     lukem 	if (pass != NULL && freepass)
    476   1.47     lukem 		free((char *)pass);
    477   1.47     lukem 	if (acct != NULL && freeacct)
    478   1.47     lukem 		free((char *)acct);
    479   1.47     lukem 	return (rval);
    480    1.1     lukem }
    481    1.1     lukem 
    482    1.1     lukem /*
    483    1.5     lukem  * `another' gets another argument, and stores the new argc and argv.
    484   1.79     lukem  * It reverts to the top level (via intr()) on EOF/error.
    485    1.1     lukem  *
    486    1.1     lukem  * Returns false if no new arguments have been added.
    487    1.1     lukem  */
    488    1.1     lukem int
    489   1.95     lukem another(int *pargc, char ***pargv, const char *prompt)
    490    1.1     lukem {
    491    1.1     lukem 	int len = strlen(line), ret;
    492    1.1     lukem 
    493    1.1     lukem 	if (len >= sizeof(line) - 3) {
    494   1.24     lukem 		fputs("sorry, arguments too long.\n", ttyout);
    495   1.75     lukem 		intr(0);
    496    1.1     lukem 	}
    497   1.24     lukem 	fprintf(ttyout, "(%s) ", prompt);
    498    1.1     lukem 	line[len++] = ' ';
    499   1.77     lukem 	if (fgets(&line[len], sizeof(line) - len, stdin) == NULL) {
    500   1.77     lukem 		clearerr(stdin);
    501   1.75     lukem 		intr(0);
    502   1.77     lukem 	}
    503    1.1     lukem 	len += strlen(&line[len]);
    504    1.1     lukem 	if (len > 0 && line[len - 1] == '\n')
    505    1.1     lukem 		line[len - 1] = '\0';
    506    1.1     lukem 	makeargv();
    507    1.1     lukem 	ret = margc > *pargc;
    508    1.1     lukem 	*pargc = margc;
    509    1.1     lukem 	*pargv = margv;
    510    1.1     lukem 	return (ret);
    511    1.1     lukem }
    512    1.1     lukem 
    513    1.5     lukem /*
    514    1.5     lukem  * glob files given in argv[] from the remote server.
    515    1.5     lukem  * if errbuf isn't NULL, store error messages there instead
    516    1.5     lukem  * of writing to the screen.
    517    1.5     lukem  */
    518    1.1     lukem char *
    519   1.95     lukem remglob(char *argv[], int doswitch, char **errbuf)
    520    1.1     lukem {
    521    1.1     lukem         char temp[MAXPATHLEN];
    522    1.1     lukem         static char buf[MAXPATHLEN];
    523    1.1     lukem         static FILE *ftemp = NULL;
    524    1.1     lukem         static char **args;
    525  1.104     lukem         int oldverbose, oldhash, oldprogress, fd, len;
    526    1.1     lukem         char *cp, *mode;
    527    1.1     lukem 
    528   1.79     lukem         if (!mflag || !connected) {
    529    1.5     lukem                 if (!doglob)
    530    1.1     lukem                         args = NULL;
    531    1.1     lukem                 else {
    532    1.1     lukem                         if (ftemp) {
    533    1.5     lukem                                 (void)fclose(ftemp);
    534    1.1     lukem                                 ftemp = NULL;
    535    1.1     lukem                         }
    536    1.1     lukem                 }
    537    1.1     lukem                 return (NULL);
    538    1.1     lukem         }
    539    1.1     lukem         if (!doglob) {
    540    1.1     lukem                 if (args == NULL)
    541    1.1     lukem                         args = argv;
    542    1.1     lukem                 if ((cp = *++args) == NULL)
    543    1.1     lukem                         args = NULL;
    544    1.1     lukem                 return (cp);
    545    1.1     lukem         }
    546    1.1     lukem         if (ftemp == NULL) {
    547   1.75     lukem 		len = strlcpy(temp, tmpdir, sizeof(temp));
    548   1.75     lukem 		if (temp[len - 1] != '/')
    549   1.74    simonb 			(void)strlcat(temp, "/", sizeof(temp));
    550   1.74    simonb 		(void)strlcat(temp, TMPFILE, sizeof(temp));
    551    1.5     lukem                 if ((fd = mkstemp(temp)) < 0) {
    552    1.1     lukem                         warn("unable to create temporary file %s", temp);
    553    1.1     lukem                         return (NULL);
    554    1.1     lukem                 }
    555    1.1     lukem                 close(fd);
    556    1.5     lukem                 oldverbose = verbose;
    557    1.5     lukem 		verbose = (errbuf != NULL) ? -1 : 0;
    558    1.5     lukem                 oldhash = hash;
    559  1.104     lukem 		oldprogress = progress;
    560    1.5     lukem                 hash = 0;
    561  1.104     lukem 		progress = 0;
    562    1.5     lukem                 if (doswitch)
    563    1.1     lukem                         pswitch(!proxy);
    564    1.1     lukem                 for (mode = "w"; *++argv != NULL; mode = "a")
    565   1.12     lukem                         recvrequest("NLST", temp, *argv, mode, 0, 0);
    566    1.5     lukem 		if ((code / 100) != COMPLETE) {
    567    1.5     lukem 			if (errbuf != NULL)
    568    1.5     lukem 				*errbuf = reply_string;
    569    1.5     lukem 		}
    570    1.5     lukem                 if (doswitch)
    571    1.1     lukem                         pswitch(!proxy);
    572    1.5     lukem                 verbose = oldverbose;
    573    1.5     lukem 		hash = oldhash;
    574  1.104     lukem 		progress = oldprogress;
    575    1.1     lukem                 ftemp = fopen(temp, "r");
    576    1.5     lukem                 (void)unlink(temp);
    577    1.1     lukem                 if (ftemp == NULL) {
    578    1.5     lukem 			if (errbuf == NULL)
    579   1.24     lukem 				fputs(
    580   1.24     lukem 				    "can't find list of remote files, oops.\n",
    581   1.24     lukem 				    ttyout);
    582    1.5     lukem 			else
    583    1.5     lukem 				*errbuf =
    584    1.5     lukem 				    "can't find list of remote files, oops.";
    585    1.1     lukem                         return (NULL);
    586    1.1     lukem                 }
    587    1.1     lukem         }
    588    1.5     lukem         if (fgets(buf, sizeof(buf), ftemp) == NULL) {
    589    1.5     lukem                 (void)fclose(ftemp);
    590    1.5     lukem 		ftemp = NULL;
    591    1.1     lukem                 return (NULL);
    592    1.1     lukem         }
    593    1.1     lukem         if ((cp = strchr(buf, '\n')) != NULL)
    594    1.1     lukem                 *cp = '\0';
    595    1.1     lukem         return (buf);
    596    1.1     lukem }
    597    1.1     lukem 
    598    1.1     lukem /*
    599   1.65     lukem  * Glob a local file name specification with the expectation of a single
    600   1.65     lukem  * return value. Can't control multiple values being expanded from the
    601   1.65     lukem  * expression, we return only the first.
    602   1.65     lukem  * Returns NULL on error, or a pointer to a buffer containing the filename
    603   1.65     lukem  * that's the caller's responsiblity to free(3) when finished with.
    604    1.1     lukem  */
    605   1.65     lukem char *
    606   1.95     lukem globulize(const char *pattern)
    607    1.1     lukem {
    608    1.1     lukem 	glob_t gl;
    609    1.1     lukem 	int flags;
    610   1.65     lukem 	char *p;
    611    1.1     lukem 
    612    1.1     lukem 	if (!doglob)
    613   1.65     lukem 		return (xstrdup(pattern));
    614    1.1     lukem 
    615   1.25    kleink 	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
    616    1.1     lukem 	memset(&gl, 0, sizeof(gl));
    617   1.65     lukem 	if (glob(pattern, flags, NULL, &gl) || gl.gl_pathc == 0) {
    618   1.65     lukem 		warnx("%s: not found", pattern);
    619    1.1     lukem 		globfree(&gl);
    620   1.65     lukem 		return (NULL);
    621    1.1     lukem 	}
    622   1.65     lukem 	p = xstrdup(gl.gl_pathv[0]);
    623    1.1     lukem 	globfree(&gl);
    624   1.65     lukem 	return (p);
    625    1.1     lukem }
    626    1.1     lukem 
    627    1.1     lukem /*
    628    1.1     lukem  * determine size of remote file
    629    1.1     lukem  */
    630    1.1     lukem off_t
    631   1.95     lukem remotesize(const char *file, int noisy)
    632    1.1     lukem {
    633   1.97     lukem 	int overbose, r;
    634    1.1     lukem 	off_t size;
    635    1.1     lukem 
    636    1.1     lukem 	overbose = verbose;
    637    1.1     lukem 	size = -1;
    638    1.1     lukem 	if (debug == 0)
    639    1.1     lukem 		verbose = -1;
    640   1.97     lukem 	if (! features[FEAT_SIZE]) {
    641   1.97     lukem 		if (noisy)
    642   1.97     lukem 			fprintf(ttyout,
    643   1.97     lukem 			    "SIZE is not supported by remote server.\n");
    644   1.97     lukem 		goto cleanup_remotesize;
    645   1.97     lukem 	}
    646   1.97     lukem 	r = command("SIZE %s", file);
    647   1.97     lukem 	if (r == COMPLETE) {
    648   1.11     lukem 		char *cp, *ep;
    649   1.11     lukem 
    650   1.11     lukem 		cp = strchr(reply_string, ' ');
    651   1.11     lukem 		if (cp != NULL) {
    652   1.11     lukem 			cp++;
    653   1.98     lukem 			size = STRTOLL(cp, &ep, 10);
    654   1.23  christos 			if (*ep != '\0' && !isspace((unsigned char)*ep))
    655   1.11     lukem 				size = -1;
    656   1.11     lukem 		}
    657   1.97     lukem 	} else {
    658   1.97     lukem 		if (r == ERROR && code == 500 && features[FEAT_SIZE] == -1)
    659   1.97     lukem 			features[FEAT_SIZE] = 0;
    660   1.97     lukem 		if (noisy && debug == 0) {
    661   1.97     lukem 			fputs(reply_string, ttyout);
    662   1.97     lukem 			putc('\n', ttyout);
    663   1.97     lukem 		}
    664   1.24     lukem 	}
    665   1.97     lukem  cleanup_remotesize:
    666    1.1     lukem 	verbose = overbose;
    667    1.1     lukem 	return (size);
    668    1.1     lukem }
    669    1.1     lukem 
    670    1.1     lukem /*
    671    1.1     lukem  * determine last modification time (in GMT) of remote file
    672    1.1     lukem  */
    673    1.1     lukem time_t
    674   1.95     lukem remotemodtime(const char *file, int noisy)
    675    1.1     lukem {
    676   1.97     lukem 	int	overbose, ocode, r;
    677   1.97     lukem 	time_t	rtime;
    678    1.1     lukem 
    679    1.1     lukem 	overbose = verbose;
    680   1.14     lukem 	ocode = code;
    681    1.1     lukem 	rtime = -1;
    682    1.1     lukem 	if (debug == 0)
    683    1.1     lukem 		verbose = -1;
    684   1.97     lukem 	if (! features[FEAT_MDTM]) {
    685   1.97     lukem 		if (noisy)
    686   1.97     lukem 			fprintf(ttyout,
    687   1.97     lukem 			    "MDTM is not supported by remote server.\n");
    688   1.97     lukem 		goto cleanup_parse_time;
    689   1.97     lukem 	}
    690   1.97     lukem 	r = command("MDTM %s", file);
    691   1.97     lukem 	if (r == COMPLETE) {
    692    1.1     lukem 		struct tm timebuf;
    693   1.87     lukem 		char *timestr, *frac;
    694   1.90     lukem 		int yy, mo, day, hour, min, sec;
    695   1.87     lukem 
    696   1.87     lukem 		/*
    697   1.87     lukem 		 * time-val = 14DIGIT [ "." 1*DIGIT ]
    698   1.87     lukem 		 *		YYYYMMDDHHMMSS[.sss]
    699   1.87     lukem 		 * mdtm-response = "213" SP time-val CRLF / error-response
    700   1.87     lukem 		 */
    701   1.87     lukem 		timestr = reply_string + 4;
    702   1.87     lukem 
    703   1.87     lukem 					/*
    704   1.87     lukem 					 * parse fraction.
    705   1.87     lukem 					 * XXX: ignored for now
    706   1.87     lukem 					 */
    707   1.87     lukem 		frac = strchr(timestr, '\r');
    708   1.87     lukem 		if (frac != NULL)
    709   1.87     lukem 			*frac = '\0';
    710   1.87     lukem 		frac = strchr(timestr, '.');
    711   1.87     lukem 		if (frac != NULL)
    712   1.87     lukem 			*frac++ = '\0';
    713   1.87     lukem 		if (strlen(timestr) == 15 && strncmp(timestr, "191", 3) == 0) {
    714   1.87     lukem 			/*
    715   1.88     lukem 			 * XXX:	Workaround for lame ftpd's that return
    716   1.88     lukem 			 *	`19100' instead of `2000'
    717   1.87     lukem 			 */
    718   1.87     lukem 			fprintf(ttyout,
    719   1.87     lukem 	    "Y2K warning! Incorrect time-val `%s' received from server.\n",
    720   1.87     lukem 			    timestr);
    721   1.87     lukem 			timestr++;
    722   1.87     lukem 			timestr[0] = '2';
    723   1.87     lukem 			timestr[1] = '0';
    724   1.87     lukem 			fprintf(ttyout, "Converted to `%s'\n", timestr);
    725   1.87     lukem 		}
    726   1.87     lukem 		if (strlen(timestr) != 14 ||
    727   1.90     lukem 		    sscanf(timestr, "%04d%02d%02d%02d%02d%02d",
    728   1.90     lukem 			&yy, &mo, &day, &hour, &min, &sec) != 6) {
    729   1.87     lukem  bad_parse_time:
    730   1.90     lukem 			fprintf(ttyout, "Can't parse time `%s'.\n", timestr);
    731   1.87     lukem 			goto cleanup_parse_time;
    732   1.87     lukem 		}
    733   1.90     lukem 		memset(&timebuf, 0, sizeof(timebuf));
    734   1.90     lukem 		timebuf.tm_sec = sec;
    735   1.90     lukem 		timebuf.tm_min = min;
    736   1.90     lukem 		timebuf.tm_hour = hour;
    737   1.90     lukem 		timebuf.tm_mday = day;
    738   1.90     lukem 		timebuf.tm_mon = mo - 1;
    739   1.90     lukem 		timebuf.tm_year = yy - TM_YEAR_BASE;
    740   1.90     lukem 		timebuf.tm_isdst = -1;
    741   1.61     lukem 		rtime = timegm(&timebuf);
    742   1.89     lukem 		if (rtime == -1) {
    743   1.89     lukem 			if (noisy || debug != 0)
    744   1.89     lukem 				goto bad_parse_time;
    745   1.89     lukem 			else
    746   1.89     lukem 				goto cleanup_parse_time;
    747   1.89     lukem 		} else if (debug)
    748   1.89     lukem 			fprintf(ttyout, "parsed date as: %s", ctime(&rtime));
    749   1.97     lukem 	} else {
    750   1.97     lukem 		if (r == ERROR && code == 500 && features[FEAT_MDTM] == -1)
    751   1.97     lukem 			features[FEAT_MDTM] = 0;
    752   1.97     lukem 		if (noisy && debug == 0) {
    753   1.97     lukem 			fputs(reply_string, ttyout);
    754   1.97     lukem 			putc('\n', ttyout);
    755   1.97     lukem 		}
    756   1.24     lukem 	}
    757   1.87     lukem  cleanup_parse_time:
    758    1.1     lukem 	verbose = overbose;
    759   1.14     lukem 	if (rtime == -1)
    760   1.14     lukem 		code = ocode;
    761    1.1     lukem 	return (rtime);
    762   1.81     lukem }
    763   1.81     lukem 
    764   1.81     lukem /*
    765   1.81     lukem  * update global `remotepwd', which contains the state of the remote cwd
    766   1.81     lukem  */
    767   1.81     lukem void
    768   1.95     lukem updateremotepwd(void)
    769   1.81     lukem {
    770   1.81     lukem 	int	 overbose, ocode, i;
    771   1.81     lukem 	char	*cp;
    772   1.81     lukem 
    773   1.81     lukem 	overbose = verbose;
    774   1.81     lukem 	ocode = code;
    775   1.81     lukem 	if (debug == 0)
    776   1.81     lukem 		verbose = -1;
    777   1.81     lukem 	if (command("PWD") != COMPLETE)
    778   1.81     lukem 		goto badremotepwd;
    779   1.81     lukem 	cp = strchr(reply_string, ' ');
    780   1.81     lukem 	if (cp == NULL || cp[0] == '\0' || cp[1] != '"')
    781   1.81     lukem 		goto badremotepwd;
    782   1.81     lukem 	cp += 2;
    783   1.81     lukem 	for (i = 0; *cp && i < sizeof(remotepwd) - 1; i++, cp++) {
    784   1.81     lukem 		if (cp[0] == '"') {
    785   1.81     lukem 			if (cp[1] == '"')
    786   1.81     lukem 				cp++;
    787   1.81     lukem 			else
    788   1.81     lukem 				break;
    789   1.81     lukem 		}
    790   1.81     lukem 		remotepwd[i] = *cp;
    791   1.81     lukem 	}
    792   1.81     lukem 	remotepwd[i] = '\0';
    793   1.81     lukem 	if (debug)
    794   1.81     lukem 		fprintf(ttyout, "got remotepwd as `%s'\n", remotepwd);
    795   1.81     lukem 	goto cleanupremotepwd;
    796   1.81     lukem  badremotepwd:
    797   1.81     lukem 	remotepwd[0]='\0';
    798   1.81     lukem  cleanupremotepwd:
    799   1.81     lukem 	verbose = overbose;
    800   1.81     lukem 	code = ocode;
    801    1.1     lukem }
    802    1.1     lukem 
    803   1.73     lukem 
    804   1.73     lukem /*
    805    1.1     lukem  * List words in stringlist, vertically arranged
    806    1.1     lukem  */
    807    1.1     lukem void
    808   1.95     lukem list_vertical(StringList *sl)
    809    1.1     lukem {
    810    1.1     lukem 	int i, j, w;
    811  1.101     lukem 	int columns, width, lines;
    812    1.1     lukem 	char *p;
    813    1.1     lukem 
    814  1.101     lukem 	width = 0;
    815    1.1     lukem 
    816    1.1     lukem 	for (i = 0 ; i < sl->sl_cur ; i++) {
    817    1.1     lukem 		w = strlen(sl->sl_str[i]);
    818    1.1     lukem 		if (w > width)
    819    1.1     lukem 			width = w;
    820    1.1     lukem 	}
    821    1.1     lukem 	width = (width + 8) &~ 7;
    822    1.1     lukem 
    823    1.1     lukem 	columns = ttywidth / width;
    824    1.1     lukem 	if (columns == 0)
    825    1.1     lukem 		columns = 1;
    826    1.1     lukem 	lines = (sl->sl_cur + columns - 1) / columns;
    827    1.1     lukem 	for (i = 0; i < lines; i++) {
    828    1.1     lukem 		for (j = 0; j < columns; j++) {
    829    1.1     lukem 			p = sl->sl_str[j * lines + i];
    830    1.1     lukem 			if (p)
    831   1.24     lukem 				fputs(p, ttyout);
    832    1.1     lukem 			if (j * lines + i + lines >= sl->sl_cur) {
    833   1.24     lukem 				putc('\n', ttyout);
    834    1.1     lukem 				break;
    835    1.1     lukem 			}
    836    1.1     lukem 			w = strlen(p);
    837    1.1     lukem 			while (w < width) {
    838    1.1     lukem 				w = (w + 8) &~ 7;
    839   1.24     lukem 				(void)putc('\t', ttyout);
    840    1.1     lukem 			}
    841    1.1     lukem 		}
    842    1.1     lukem 	}
    843    1.3     lukem }
    844    1.3     lukem 
    845    1.3     lukem /*
    846    1.3     lukem  * Update the global ttywidth value, using TIOCGWINSZ.
    847    1.3     lukem  */
    848    1.3     lukem void
    849   1.95     lukem setttywidth(int a)
    850    1.3     lukem {
    851    1.3     lukem 	struct winsize winsize;
    852   1.75     lukem 	int oerrno = errno;
    853    1.3     lukem 
    854   1.34     lukem 	if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1 &&
    855   1.34     lukem 	    winsize.ws_col != 0)
    856    1.3     lukem 		ttywidth = winsize.ws_col;
    857    1.3     lukem 	else
    858    1.3     lukem 		ttywidth = 80;
    859   1.53     lukem 	errno = oerrno;
    860    1.3     lukem }
    861    1.3     lukem 
    862   1.73     lukem /*
    863   1.73     lukem  * Change the rate limit up (SIGUSR1) or down (SIGUSR2)
    864   1.73     lukem  */
    865   1.53     lukem void
    866   1.95     lukem crankrate(int sig)
    867   1.53     lukem {
    868   1.53     lukem 
    869   1.53     lukem 	switch (sig) {
    870   1.53     lukem 	case SIGUSR1:
    871   1.53     lukem 		if (rate_get)
    872   1.53     lukem 			rate_get += rate_get_incr;
    873   1.53     lukem 		if (rate_put)
    874   1.53     lukem 			rate_put += rate_put_incr;
    875   1.53     lukem 		break;
    876   1.53     lukem 	case SIGUSR2:
    877   1.53     lukem 		if (rate_get && rate_get > rate_get_incr)
    878   1.53     lukem 			rate_get -= rate_get_incr;
    879   1.53     lukem 		if (rate_put && rate_put > rate_put_incr)
    880   1.53     lukem 			rate_put -= rate_put_incr;
    881   1.53     lukem 		break;
    882   1.53     lukem 	default:
    883   1.53     lukem 		err(1, "crankrate invoked with unknown signal: %d", sig);
    884   1.53     lukem 	}
    885   1.53     lukem }
    886   1.53     lukem 
    887   1.53     lukem 
    888    1.3     lukem /*
    889    1.6     lukem  * Setup or cleanup EditLine structures
    890    1.6     lukem  */
    891   1.50       cgd #ifndef NO_EDITCOMPLETE
    892    1.6     lukem void
    893   1.95     lukem controlediting(void)
    894    1.6     lukem {
    895    1.6     lukem 	if (editing && el == NULL && hist == NULL) {
    896   1.16  christos 		HistEvent ev;
    897   1.30     lukem 		int editmode;
    898   1.16  christos 
    899  1.105       cgd 		el = el_init(getprogname(), stdin, ttyout, stderr);
    900   1.23  christos 		/* init editline */
    901    1.6     lukem 		hist = history_init();		/* init the builtin history */
    902   1.23  christos 		history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
    903    1.6     lukem 		el_set(el, EL_HIST, history, hist);	/* use history */
    904    1.6     lukem 
    905    1.6     lukem 		el_set(el, EL_EDITOR, "emacs");	/* default editor is emacs */
    906   1.82     lukem 		el_set(el, EL_PROMPT, prompt);	/* set the prompt functions */
    907   1.82     lukem 		el_set(el, EL_RPROMPT, rprompt);
    908    1.6     lukem 
    909    1.6     lukem 		/* add local file completion, bind to TAB */
    910    1.6     lukem 		el_set(el, EL_ADDFN, "ftp-complete",
    911    1.6     lukem 		    "Context sensitive argument completion",
    912    1.6     lukem 		    complete);
    913    1.6     lukem 		el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
    914    1.6     lukem 		el_source(el, NULL);	/* read ~/.editrc */
    915   1.30     lukem 		if ((el_get(el, EL_EDITMODE, &editmode) != -1) && editmode == 0)
    916   1.30     lukem 			editing = 0;	/* the user doesn't want editing,
    917   1.30     lukem 					 * so disable, and let statement
    918   1.30     lukem 					 * below cleanup */
    919   1.30     lukem 		else
    920   1.30     lukem 			el_set(el, EL_SIGNAL, 1);
    921   1.30     lukem 	}
    922   1.30     lukem 	if (!editing) {
    923    1.6     lukem 		if (hist) {
    924    1.6     lukem 			history_end(hist);
    925    1.6     lukem 			hist = NULL;
    926    1.6     lukem 		}
    927    1.6     lukem 		if (el) {
    928    1.6     lukem 			el_end(el);
    929    1.6     lukem 			el = NULL;
    930    1.6     lukem 		}
    931    1.6     lukem 	}
    932    1.6     lukem }
    933   1.50       cgd #endif /* !NO_EDITCOMPLETE */
    934   1.27   thorpej 
    935   1.27   thorpej /*
    936   1.53     lukem  * Convert the string `arg' to an int, which may have an optional SI suffix
    937   1.53     lukem  * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
    938   1.27   thorpej  */
    939   1.27   thorpej int
    940   1.95     lukem strsuftoi(const char *arg)
    941   1.27   thorpej {
    942   1.27   thorpej 	char *cp;
    943   1.53     lukem 	long val;
    944   1.27   thorpej 
    945   1.35  christos 	if (!isdigit((unsigned char)arg[0]))
    946   1.27   thorpej 		return (-1);
    947   1.27   thorpej 
    948   1.27   thorpej 	val = strtol(arg, &cp, 10);
    949   1.27   thorpej 	if (cp != NULL) {
    950   1.53     lukem 		if (cp[0] != '\0' && cp[1] != '\0')
    951   1.27   thorpej 			 return (-1);
    952   1.53     lukem 		switch (tolower((unsigned char)cp[0])) {
    953   1.53     lukem 		case '\0':
    954   1.53     lukem 		case 'b':
    955   1.53     lukem 			break;
    956   1.53     lukem 		case 'k':
    957   1.53     lukem 			val <<= 10;
    958   1.53     lukem 			break;
    959   1.53     lukem 		case 'm':
    960   1.53     lukem 			val <<= 20;
    961   1.53     lukem 			break;
    962   1.53     lukem 		case 'g':
    963   1.53     lukem 			val <<= 30;
    964   1.53     lukem 			break;
    965   1.53     lukem 		default:
    966   1.53     lukem 			return (-1);
    967   1.53     lukem 		}
    968   1.27   thorpej 	}
    969   1.53     lukem 	if (val < 0 || val > INT_MAX)
    970   1.27   thorpej 		return (-1);
    971   1.27   thorpej 
    972   1.27   thorpej 	return (val);
    973   1.27   thorpej }
    974   1.27   thorpej 
    975   1.27   thorpej /*
    976   1.27   thorpej  * Set up socket buffer sizes before a connection is made.
    977   1.27   thorpej  */
    978   1.27   thorpej void
    979   1.95     lukem setupsockbufsize(int sock)
    980   1.27   thorpej {
    981   1.27   thorpej 
    982   1.58     lukem 	if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size,
    983   1.58     lukem 	    sizeof(rcvbuf_size)) < 0)
    984   1.58     lukem 		warn("unable to set sndbuf size %d", sndbuf_size);
    985   1.58     lukem 
    986   1.58     lukem 	if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf_size,
    987   1.58     lukem 	    sizeof(rcvbuf_size)) < 0)
    988   1.58     lukem 		warn("unable to set rcvbuf size %d", rcvbuf_size);
    989   1.43     lukem }
    990   1.43     lukem 
    991   1.78     lukem /*
    992   1.78     lukem  * Copy characters from src into dst, \ quoting characters that require it
    993   1.78     lukem  */
    994   1.43     lukem void
    995   1.95     lukem ftpvis(char *dst, size_t dstlen, const char *src, size_t srclen)
    996   1.43     lukem {
    997   1.43     lukem 	int	di, si;
    998   1.43     lukem 
    999   1.43     lukem 	for (di = si = 0;
   1000   1.43     lukem 	    src[si] != '\0' && di < dstlen && si < srclen;
   1001   1.43     lukem 	    di++, si++) {
   1002   1.43     lukem 		switch (src[si]) {
   1003   1.43     lukem 		case '\\':
   1004   1.43     lukem 		case ' ':
   1005   1.43     lukem 		case '\t':
   1006   1.43     lukem 		case '\r':
   1007   1.44     lukem 		case '\n':
   1008   1.43     lukem 		case '"':
   1009   1.43     lukem 			dst[di++] = '\\';
   1010   1.43     lukem 			if (di >= dstlen)
   1011   1.43     lukem 				break;
   1012   1.43     lukem 			/* FALLTHROUGH */
   1013   1.43     lukem 		default:
   1014   1.43     lukem 			dst[di] = src[si];
   1015   1.43     lukem 		}
   1016   1.43     lukem 	}
   1017   1.43     lukem 	dst[di] = '\0';
   1018   1.82     lukem }
   1019   1.82     lukem 
   1020   1.82     lukem /*
   1021   1.82     lukem  * Copy src into buf (which is len bytes long), expanding % sequences.
   1022   1.82     lukem  */
   1023   1.82     lukem void
   1024   1.95     lukem formatbuf(char *buf, size_t len, const char *src)
   1025   1.82     lukem {
   1026   1.82     lukem 	const char	*p;
   1027   1.82     lukem 	char		*p2, *q;
   1028   1.82     lukem 	int		 i, op, updirs, pdirs;
   1029   1.82     lukem 
   1030   1.82     lukem #define ADDBUF(x) do { \
   1031   1.82     lukem 		if (i >= len - 1) \
   1032   1.82     lukem 			goto endbuf; \
   1033   1.82     lukem 		buf[i++] = (x); \
   1034   1.82     lukem 	} while (0)
   1035   1.82     lukem 
   1036   1.82     lukem 	p = src;
   1037   1.82     lukem 	for (i = 0; *p; p++) {
   1038   1.82     lukem 		if (*p != '%') {
   1039   1.82     lukem 			ADDBUF(*p);
   1040   1.82     lukem 			continue;
   1041   1.82     lukem 		}
   1042   1.82     lukem 		p++;
   1043   1.82     lukem 
   1044   1.82     lukem 		switch (op = *p) {
   1045   1.82     lukem 
   1046   1.82     lukem 		case '/':
   1047   1.82     lukem 		case '.':
   1048   1.82     lukem 		case 'c':
   1049   1.82     lukem 			p2 = connected ? remotepwd : "";
   1050   1.82     lukem 			updirs = pdirs = 0;
   1051   1.82     lukem 
   1052   1.82     lukem 			/* option to determine fixed # of dirs from path */
   1053   1.82     lukem 			if (op == '.' || op == 'c') {
   1054   1.82     lukem 				int skip;
   1055   1.82     lukem 
   1056   1.82     lukem 				q = p2;
   1057   1.82     lukem 				while (*p2)		/* calc # of /'s */
   1058   1.82     lukem 					if (*p2++ == '/')
   1059   1.82     lukem 						updirs++;
   1060   1.82     lukem 				if (p[1] == '0') {	/* print <x> or ... */
   1061   1.82     lukem 					pdirs = 1;
   1062   1.82     lukem 					p++;
   1063   1.82     lukem 				}
   1064   1.82     lukem 				if (p[1] >= '1' && p[1] <= '9') {
   1065   1.82     lukem 							/* calc # to skip  */
   1066   1.82     lukem 					skip = p[1] - '0';
   1067   1.82     lukem 					p++;
   1068   1.82     lukem 				} else
   1069   1.82     lukem 					skip = 1;
   1070   1.82     lukem 
   1071   1.82     lukem 				updirs -= skip;
   1072   1.82     lukem 				while (skip-- > 0) {
   1073   1.82     lukem 					while ((p2 > q) && (*p2 != '/'))
   1074   1.82     lukem 						p2--;	/* back up */
   1075   1.82     lukem 					if (skip && p2 > q)
   1076   1.82     lukem 						p2--;
   1077   1.82     lukem 				}
   1078   1.82     lukem 				if (*p2 == '/' && p2 != q)
   1079   1.82     lukem 					p2++;
   1080   1.82     lukem 			}
   1081   1.82     lukem 
   1082   1.82     lukem 			if (updirs > 0 && pdirs) {
   1083   1.82     lukem 				if (i >= len - 5)
   1084   1.82     lukem 					break;
   1085   1.82     lukem 				if (op == '.') {
   1086   1.82     lukem 					ADDBUF('.');
   1087   1.82     lukem 					ADDBUF('.');
   1088   1.82     lukem 					ADDBUF('.');
   1089   1.82     lukem 				} else {
   1090   1.82     lukem 					ADDBUF('/');
   1091   1.82     lukem 					ADDBUF('<');
   1092   1.82     lukem 					if (updirs > 9) {
   1093   1.82     lukem 						ADDBUF('9');
   1094   1.82     lukem 						ADDBUF('+');
   1095   1.82     lukem 					} else
   1096   1.82     lukem 						ADDBUF('0' + updirs);
   1097   1.82     lukem 					ADDBUF('>');
   1098   1.82     lukem 				}
   1099   1.82     lukem 			}
   1100   1.82     lukem 			for (; *p2; p2++)
   1101   1.82     lukem 				ADDBUF(*p2);
   1102   1.82     lukem 			break;
   1103   1.82     lukem 
   1104   1.82     lukem 		case 'M':
   1105   1.82     lukem 		case 'm':
   1106   1.82     lukem 			for (p2 = connected ? hostname : "-"; *p2; p2++) {
   1107   1.82     lukem 				if (op == 'm' && *p2 == '.')
   1108   1.82     lukem 					break;
   1109   1.82     lukem 				ADDBUF(*p2);
   1110   1.82     lukem 			}
   1111   1.82     lukem 			break;
   1112   1.82     lukem 
   1113   1.82     lukem 		case 'n':
   1114   1.82     lukem 			for (p2 = connected ? username : "-"; *p2 ; p2++)
   1115   1.82     lukem 				ADDBUF(*p2);
   1116   1.82     lukem 			break;
   1117   1.82     lukem 
   1118   1.82     lukem 		case '%':
   1119   1.82     lukem 			ADDBUF('%');
   1120   1.82     lukem 			break;
   1121   1.82     lukem 
   1122   1.82     lukem 		default:		/* display unknown codes literally */
   1123   1.82     lukem 			ADDBUF('%');
   1124   1.82     lukem 			ADDBUF(op);
   1125   1.82     lukem 			break;
   1126   1.82     lukem 
   1127   1.82     lukem 		}
   1128   1.82     lukem 	}
   1129   1.82     lukem  endbuf:
   1130   1.82     lukem 	buf[i] = '\0';
   1131  1.100     lukem }
   1132  1.100     lukem 
   1133  1.100     lukem /*
   1134  1.100     lukem  * Parse `port' into a TCP port number, defaulting to `defport' if `port' is
   1135  1.100     lukem  * an unknown service name. If defport != -1, print a warning upon bad parse.
   1136  1.100     lukem  */
   1137  1.100     lukem int
   1138  1.100     lukem parseport(const char *port, int defport)
   1139  1.100     lukem {
   1140  1.100     lukem 	int	 rv;
   1141  1.100     lukem 	long	 nport;
   1142  1.100     lukem 	char	*p, *ep;
   1143  1.100     lukem 
   1144  1.100     lukem 	p = xstrdup(port);
   1145  1.100     lukem 	nport = strtol(p, &ep, 10);
   1146  1.100     lukem 	if (*ep != '\0' && ep == p) {
   1147  1.100     lukem 		struct servent	*svp;
   1148  1.100     lukem 
   1149  1.100     lukem 		svp = getservbyname(port, "tcp");
   1150  1.100     lukem 		if (svp == NULL) {
   1151  1.100     lukem  badparseport:
   1152  1.100     lukem 			if (defport != -1)
   1153  1.100     lukem 				warnx("Unknown port `%s', using port %d",
   1154  1.100     lukem 				    port, defport);
   1155  1.100     lukem 			rv = defport;
   1156  1.100     lukem 		} else
   1157  1.100     lukem 			rv = ntohs(svp->s_port);
   1158  1.100     lukem 	} else if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0')
   1159  1.100     lukem 		goto badparseport;
   1160  1.100     lukem 	else
   1161  1.100     lukem 		rv = nport;
   1162  1.100     lukem 	free(p);
   1163  1.100     lukem 	return (rv);
   1164   1.27   thorpej }
   1165   1.55     lukem 
   1166   1.55     lukem /*
   1167   1.55     lukem  * Determine if given string is an IPv6 address or not.
   1168   1.55     lukem  * Return 1 for yes, 0 for no
   1169   1.55     lukem  */
   1170   1.55     lukem int
   1171   1.95     lukem isipv6addr(const char *addr)
   1172   1.55     lukem {
   1173   1.55     lukem 	int rv = 0;
   1174   1.55     lukem #ifdef INET6
   1175   1.93    itojun 	struct addrinfo hints, *res;
   1176   1.55     lukem 
   1177   1.93    itojun 	memset(&hints, 0, sizeof(hints));
   1178   1.93    itojun 	hints.ai_family = PF_INET6;
   1179   1.93    itojun 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
   1180   1.93    itojun 	hints.ai_flags = AI_NUMERICHOST;
   1181   1.93    itojun 	if (getaddrinfo(addr, "0", &hints, &res) != 0)
   1182   1.93    itojun 		rv = 0;
   1183   1.93    itojun 	else {
   1184   1.93    itojun 		rv = 1;
   1185   1.93    itojun 		freeaddrinfo(res);
   1186   1.93    itojun 	}
   1187   1.55     lukem 	if (debug)
   1188   1.55     lukem 		fprintf(ttyout, "isipv6addr: got %d for %s\n", rv, addr);
   1189   1.55     lukem #endif
   1190   1.86    itojun 	return (rv == 1) ? 1 : 0;
   1191   1.55     lukem }
   1192   1.55     lukem 
   1193   1.27   thorpej 
   1194   1.27   thorpej /*
   1195   1.27   thorpej  * Internal version of connect(2); sets socket buffer sizes first.
   1196   1.27   thorpej  */
   1197   1.27   thorpej int
   1198   1.95     lukem xconnect(int sock, const struct sockaddr *name, int namelen)
   1199   1.27   thorpej {
   1200   1.27   thorpej 
   1201   1.27   thorpej 	setupsockbufsize(sock);
   1202   1.27   thorpej 	return (connect(sock, name, namelen));
   1203   1.27   thorpej }
   1204   1.27   thorpej 
   1205   1.27   thorpej /*
   1206   1.27   thorpej  * Internal version of listen(2); sets socket buffer sizes first.
   1207   1.27   thorpej  */
   1208   1.27   thorpej int
   1209   1.95     lukem xlisten(int sock, int backlog)
   1210   1.27   thorpej {
   1211   1.27   thorpej 
   1212   1.27   thorpej 	setupsockbufsize(sock);
   1213   1.27   thorpej 	return (listen(sock, backlog));
   1214   1.31     lukem }
   1215   1.31     lukem 
   1216   1.78     lukem /*
   1217   1.78     lukem  * malloc() with inbuilt error checking
   1218   1.78     lukem  */
   1219   1.31     lukem void *
   1220   1.95     lukem xmalloc(size_t size)
   1221   1.31     lukem {
   1222   1.31     lukem 	void *p;
   1223   1.31     lukem 
   1224   1.31     lukem 	p = malloc(size);
   1225   1.31     lukem 	if (p == NULL)
   1226   1.31     lukem 		err(1, "Unable to allocate %ld bytes of memory", (long)size);
   1227   1.31     lukem 	return (p);
   1228   1.85     lukem }
   1229   1.85     lukem 
   1230   1.85     lukem /*
   1231   1.85     lukem  * sl_init() with inbuilt error checking
   1232   1.85     lukem  */
   1233   1.85     lukem StringList *
   1234   1.95     lukem xsl_init(void)
   1235   1.85     lukem {
   1236   1.85     lukem 	StringList *p;
   1237   1.85     lukem 
   1238   1.85     lukem 	p = sl_init();
   1239   1.85     lukem 	if (p == NULL)
   1240   1.85     lukem 		err(1, "Unable to allocate memory for stringlist");
   1241   1.85     lukem 	return (p);
   1242   1.85     lukem }
   1243   1.85     lukem 
   1244   1.85     lukem /*
   1245   1.85     lukem  * sl_add() with inbuilt error checking
   1246   1.85     lukem  */
   1247   1.85     lukem void
   1248   1.95     lukem xsl_add(StringList *sl, char *i)
   1249   1.85     lukem {
   1250   1.85     lukem 
   1251   1.85     lukem 	if (sl_add(sl, i) == -1)
   1252   1.85     lukem 		err(1, "Unable to add `%s' to stringlist", i);
   1253   1.31     lukem }
   1254   1.31     lukem 
   1255   1.78     lukem /*
   1256   1.78     lukem  * strdup() with inbuilt error checking
   1257   1.78     lukem  */
   1258   1.31     lukem char *
   1259   1.95     lukem xstrdup(const char *str)
   1260   1.31     lukem {
   1261   1.31     lukem 	char *s;
   1262   1.31     lukem 
   1263   1.31     lukem 	if (str == NULL)
   1264   1.31     lukem 		errx(1, "xstrdup() called with NULL argument");
   1265   1.31     lukem 	s = strdup(str);
   1266   1.31     lukem 	if (s == NULL)
   1267   1.31     lukem 		err(1, "Unable to allocate memory for string copy");
   1268   1.31     lukem 	return (s);
   1269   1.27   thorpej }
   1270