xref: /titanic_51/usr/src/cmd/cmd-inet/usr.bin/ftp/ftp.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  *	Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  *	University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  *	All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  *	University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  *	software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  *	contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "ftp_var.h"
43*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * WRITE() returns:
48*7c478bd9Sstevel@tonic-gate  * 	>0	no error
49*7c478bd9Sstevel@tonic-gate  *	-1	error, errorno is set
50*7c478bd9Sstevel@tonic-gate  *	-2	security error (secure_write() only)
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate #define	PUTC(x, y)	secure_putc(x, y)
53*7c478bd9Sstevel@tonic-gate #define	READ(x, y, z)	secure_read(x, y, z)
54*7c478bd9Sstevel@tonic-gate #define	WRITE(x, y, z)	secure_write(x, y, z)
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static struct	sockaddr_in6 data_addr;
57*7c478bd9Sstevel@tonic-gate int	data = -1;
58*7c478bd9Sstevel@tonic-gate static int	abrtflag = 0;
59*7c478bd9Sstevel@tonic-gate static int	ptflag = 0;
60*7c478bd9Sstevel@tonic-gate int		connected;
61*7c478bd9Sstevel@tonic-gate static jmp_buf	sendabort;
62*7c478bd9Sstevel@tonic-gate static jmp_buf	recvabort;
63*7c478bd9Sstevel@tonic-gate static jmp_buf 	ptabort;
64*7c478bd9Sstevel@tonic-gate static int ptabflg;
65*7c478bd9Sstevel@tonic-gate static boolean_t pasv_refused;
66*7c478bd9Sstevel@tonic-gate boolean_t	eport_supported = B_TRUE;
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * For IPv6 addresses, EPSV will be the default (rather than EPRT/LPRT).
69*7c478bd9Sstevel@tonic-gate  * The EPSV/ERPT ftp protocols are specified in RFC 2428.
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * Perform EPSV if passivemode is set and ipv6rem is TRUE.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate static boolean_t ipv6rem;
74*7c478bd9Sstevel@tonic-gate int	use_eprt = 0;	/* Testing option that specifies EPRT by default */
75*7c478bd9Sstevel@tonic-gate FILE	*ctrl_in, *ctrl_out;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static void abortsend(int sig);
78*7c478bd9Sstevel@tonic-gate static void abortpt(int sig);
79*7c478bd9Sstevel@tonic-gate static void proxtrans(char *cmd, char *local, char *remote);
80*7c478bd9Sstevel@tonic-gate static void cmdabort(int sig);
81*7c478bd9Sstevel@tonic-gate static int empty(struct fd_set *mask, int sec, int nfds);
82*7c478bd9Sstevel@tonic-gate static void abortrecv(int sig);
83*7c478bd9Sstevel@tonic-gate static int initconn(void);
84*7c478bd9Sstevel@tonic-gate static FILE *dataconn(char *mode);
85*7c478bd9Sstevel@tonic-gate static void ptransfer(char *direction, off_t bytes, hrtime_t t0,
86*7c478bd9Sstevel@tonic-gate     hrtime_t t1, char *local, char *remote);
87*7c478bd9Sstevel@tonic-gate static void psabort(int sig);
88*7c478bd9Sstevel@tonic-gate static char *gunique(char *local);
89*7c478bd9Sstevel@tonic-gate static const char *inet_ntop_native(int af, const void *src, char *dst,
90*7c478bd9Sstevel@tonic-gate     size_t size);
91*7c478bd9Sstevel@tonic-gate static ssize_t timedread(int fd, void *buf, size_t maxlen, int timeout);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate static int secure_command(char *);
94*7c478bd9Sstevel@tonic-gate static int decode_reply(uchar_t *, int, uchar_t *, int, boolean_t *);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static ssize_t	bufcnt;		/* number of bytes in buf[]	*/
97*7c478bd9Sstevel@tonic-gate static char	*bufp;		/* next character in buf	*/
98*7c478bd9Sstevel@tonic-gate static int	buferr;		/* last errno			*/
99*7c478bd9Sstevel@tonic-gate static size_t	bufsize;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate static void fdio_setbuf(char *buffer, size_t bufsize);
102*7c478bd9Sstevel@tonic-gate static int fdio_fillbuf(int fd);
103*7c478bd9Sstevel@tonic-gate static int fdio_error(int fd);
104*7c478bd9Sstevel@tonic-gate #define	fdio_getc(fd)	(--bufcnt < 0 ? fdio_fillbuf((fd)) : \
105*7c478bd9Sstevel@tonic-gate 			    ((unsigned char)*bufp++))
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate #define	MAX(a, b) ((a) > (b) ? (a) : (b))
108*7c478bd9Sstevel@tonic-gate #define	NONZERO(x)	((x) == 0 ? 1 : (x))
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static void
111*7c478bd9Sstevel@tonic-gate fdio_setbuf(char *buffer, size_t maxsize)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	buf = buffer;
114*7c478bd9Sstevel@tonic-gate 	bufp = buf;
115*7c478bd9Sstevel@tonic-gate 	bufcnt = 0;
116*7c478bd9Sstevel@tonic-gate 	buferr = 0;
117*7c478bd9Sstevel@tonic-gate 	bufsize = maxsize;
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate static int
121*7c478bd9Sstevel@tonic-gate fdio_fillbuf(int fd)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate 	bufcnt = timedread(fd, buf, bufsize, timeout);
124*7c478bd9Sstevel@tonic-gate 	if (bufcnt < 0)
125*7c478bd9Sstevel@tonic-gate 		buferr = errno;
126*7c478bd9Sstevel@tonic-gate 	if (bufcnt <= 0)
127*7c478bd9Sstevel@tonic-gate 		return (EOF);
128*7c478bd9Sstevel@tonic-gate 	bufp = buf;
129*7c478bd9Sstevel@tonic-gate 	bufcnt--;
130*7c478bd9Sstevel@tonic-gate 	return ((unsigned char)*bufp++);
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /*
134*7c478bd9Sstevel@tonic-gate  * fdio_error - used on a file descriptor instead of ferror()
135*7c478bd9Sstevel@tonic-gate  */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
138*7c478bd9Sstevel@tonic-gate static int
139*7c478bd9Sstevel@tonic-gate fdio_error(int fd)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	return (buferr);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate  * timedread - read buffer (like "read"), but with timeout (in seconds)
146*7c478bd9Sstevel@tonic-gate  */
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate static ssize_t
149*7c478bd9Sstevel@tonic-gate timedread(int fd, void *buf, size_t size, int timeout)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	struct fd_set mask;
152*7c478bd9Sstevel@tonic-gate 	struct timeval tv;
153*7c478bd9Sstevel@tonic-gate 	int err;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if (!timeout)
156*7c478bd9Sstevel@tonic-gate 		return (READ(fd, buf, size));
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	tv.tv_sec = (time_t)timeout;
159*7c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	FD_ZERO(&mask);
162*7c478bd9Sstevel@tonic-gate 	FD_SET(fd, &mask);
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	err = select(fd + 1, &mask, NULL, NULL, &tv);
165*7c478bd9Sstevel@tonic-gate 	if (err == 0)
166*7c478bd9Sstevel@tonic-gate 		errno = ETIMEDOUT;
167*7c478bd9Sstevel@tonic-gate 	if (err <= 0)
168*7c478bd9Sstevel@tonic-gate 		return (-1);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	return (READ(fd, buf, size));
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate char *
175*7c478bd9Sstevel@tonic-gate hookup(char *host, char *service)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *ai = NULL, *ai_head;
178*7c478bd9Sstevel@tonic-gate 	int s;
179*7c478bd9Sstevel@tonic-gate 	socklen_t len;
180*7c478bd9Sstevel@tonic-gate 	static char hostnamebuf[80];
181*7c478bd9Sstevel@tonic-gate 	struct in6_addr ipv6addr;
182*7c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
183*7c478bd9Sstevel@tonic-gate 	int error_num;
184*7c478bd9Sstevel@tonic-gate 	int on = 1;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	/*
187*7c478bd9Sstevel@tonic-gate 	 * There appears to be a bug in getaddrinfo() where, if the
188*7c478bd9Sstevel@tonic-gate 	 * ai_family is set to AF_INET6, and the host is a v4-only
189*7c478bd9Sstevel@tonic-gate 	 * host, getaddrinfo() returns an error instead of returning
190*7c478bd9Sstevel@tonic-gate 	 * an v4-mapped ipv6 address. Therefore the ai_family is
191*7c478bd9Sstevel@tonic-gate 	 * set to AF_UNSPEC and any returned v4 addresses are
192*7c478bd9Sstevel@tonic-gate 	 * explicitly mapped within ftp.
193*7c478bd9Sstevel@tonic-gate 	 */
194*7c478bd9Sstevel@tonic-gate 	bzero((char *)&remctladdr, sizeof (remctladdr));
195*7c478bd9Sstevel@tonic-gate 	bzero((char *)&hints, sizeof (hints));
196*7c478bd9Sstevel@tonic-gate 	hints.ai_flags = AI_CANONNAME;
197*7c478bd9Sstevel@tonic-gate 	hints.ai_family = AF_UNSPEC;
198*7c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	error_num = getaddrinfo(host, service, &hints, &ai);
201*7c478bd9Sstevel@tonic-gate 	if (error_num != 0) {
202*7c478bd9Sstevel@tonic-gate 		if (error_num == EAI_AGAIN) {
203*7c478bd9Sstevel@tonic-gate 			(void) printf(
204*7c478bd9Sstevel@tonic-gate 			    "%s: unknown host or invalid literal address "
205*7c478bd9Sstevel@tonic-gate 			    "(try again later)\n", host);
206*7c478bd9Sstevel@tonic-gate 		} else {
207*7c478bd9Sstevel@tonic-gate 			(void) printf(
208*7c478bd9Sstevel@tonic-gate 			    "%s: unknown host or invalid literal address\n",
209*7c478bd9Sstevel@tonic-gate 			    host);
210*7c478bd9Sstevel@tonic-gate 		}
211*7c478bd9Sstevel@tonic-gate 		code = -1;
212*7c478bd9Sstevel@tonic-gate 		return ((char *)0);
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 	ai_head = ai;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	/*
218*7c478bd9Sstevel@tonic-gate 	 * If ai_canonname is a IPv4-mapped IPv6 literal, we'll convert it to
219*7c478bd9Sstevel@tonic-gate 	 * IPv4 literal address.
220*7c478bd9Sstevel@tonic-gate 	 */
221*7c478bd9Sstevel@tonic-gate 	if (ai->ai_canonname != NULL &&
222*7c478bd9Sstevel@tonic-gate 	    (inet_pton(AF_INET6, ai->ai_canonname, &ipv6addr) > 0) &&
223*7c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&ipv6addr)) {
224*7c478bd9Sstevel@tonic-gate 		struct in_addr src4;
225*7c478bd9Sstevel@tonic-gate 		hostnamebuf[0] = '\0';
226*7c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&ipv6addr, &src4);
227*7c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &src4, hostnamebuf,
228*7c478bd9Sstevel@tonic-gate 		    sizeof (hostnamebuf));
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		/*
231*7c478bd9Sstevel@tonic-gate 		 * It can even be the case that the "host" supplied by the user
232*7c478bd9Sstevel@tonic-gate 		 * can be a IPv4-mapped IPv6 literal. So, let's fix that too.
233*7c478bd9Sstevel@tonic-gate 		 */
234*7c478bd9Sstevel@tonic-gate 		if ((inet_pton(AF_INET6, host, &ipv6addr) > 0) &&
235*7c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&ipv6addr) &&
236*7c478bd9Sstevel@tonic-gate 		    strlen(hostnamebuf) <= strlen(host)) {
237*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(host, hostnamebuf, strlen(host) + 1);
238*7c478bd9Sstevel@tonic-gate 		}
239*7c478bd9Sstevel@tonic-gate 	} else {
240*7c478bd9Sstevel@tonic-gate 		reset_timer();
241*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(hostnamebuf,
242*7c478bd9Sstevel@tonic-gate 		    (ai->ai_canonname ? ai->ai_canonname : host),
243*7c478bd9Sstevel@tonic-gate 		    sizeof (hostnamebuf));
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	hostname = hostnamebuf;
247*7c478bd9Sstevel@tonic-gate 	for (;;) {
248*7c478bd9Sstevel@tonic-gate 		int oerrno;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 		bcopy(ai->ai_addr, &remctladdr, ai->ai_addrlen);
251*7c478bd9Sstevel@tonic-gate 		if (ai->ai_addr->sa_family == AF_INET) {
252*7c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(
253*7c478bd9Sstevel@tonic-gate 			    &(((struct sockaddr_in *)ai->ai_addr)->sin_addr),
254*7c478bd9Sstevel@tonic-gate 			    &remctladdr.sin6_addr);
255*7c478bd9Sstevel@tonic-gate 			remctladdr.sin6_family = AF_INET6;
256*7c478bd9Sstevel@tonic-gate 		}
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 		s = socket(AF_INET6, SOCK_STREAM, 0);
259*7c478bd9Sstevel@tonic-gate 		if (s < 0) {
260*7c478bd9Sstevel@tonic-gate 			perror("ftp: socket");
261*7c478bd9Sstevel@tonic-gate 			code = -1;
262*7c478bd9Sstevel@tonic-gate 			freeaddrinfo(ai_head);
263*7c478bd9Sstevel@tonic-gate 			return (0);
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 		if (timeout && setsockopt(s, IPPROTO_TCP, TCP_ABORT_THRESHOLD,
266*7c478bd9Sstevel@tonic-gate 		    (char *)&timeoutms, sizeof (timeoutms)) < 0 && debug)
267*7c478bd9Sstevel@tonic-gate 			perror("ftp: setsockopt (TCP_ABORT_THRESHOLD)");
268*7c478bd9Sstevel@tonic-gate 		reset_timer();
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 		error_num = connect(s, (struct sockaddr *)&remctladdr,
271*7c478bd9Sstevel@tonic-gate 		    sizeof (remctladdr));
272*7c478bd9Sstevel@tonic-gate 		oerrno = errno;
273*7c478bd9Sstevel@tonic-gate 		if (error_num >= 0)
274*7c478bd9Sstevel@tonic-gate 			break;
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 		/*
277*7c478bd9Sstevel@tonic-gate 		 * Maintain message behavior: only include the address in
278*7c478bd9Sstevel@tonic-gate 		 * our error message if we have another one to try; if this
279*7c478bd9Sstevel@tonic-gate 		 * is the last address on our list, just print the error.
280*7c478bd9Sstevel@tonic-gate 		 */
281*7c478bd9Sstevel@tonic-gate 		if (ai->ai_next != NULL) {
282*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ftp: connect to address %s: ",
283*7c478bd9Sstevel@tonic-gate 			    inet_ntop_native(ai->ai_addr->sa_family,
284*7c478bd9Sstevel@tonic-gate 			    (void *)ai->ai_addr, abuf, sizeof (abuf)));
285*7c478bd9Sstevel@tonic-gate 			errno = oerrno;
286*7c478bd9Sstevel@tonic-gate 			perror((char *)0);
287*7c478bd9Sstevel@tonic-gate 		} else {
288*7c478bd9Sstevel@tonic-gate 			perror("ftp: connect");
289*7c478bd9Sstevel@tonic-gate 			code = -1;
290*7c478bd9Sstevel@tonic-gate 			freeaddrinfo(ai_head);
291*7c478bd9Sstevel@tonic-gate 			goto bad;
292*7c478bd9Sstevel@tonic-gate 		}
293*7c478bd9Sstevel@tonic-gate 		ai = ai->ai_next;
294*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Trying %s...\n",
295*7c478bd9Sstevel@tonic-gate 		    inet_ntop_native(ai->ai_addr->sa_family,
296*7c478bd9Sstevel@tonic-gate 		    (void *)ai->ai_addr, abuf, sizeof (abuf)));
297*7c478bd9Sstevel@tonic-gate 		(void) close(s);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	/* Set ipv6rem to TRUE if control connection is a native IPv6 address */
302*7c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&remctladdr.sin6_addr))
303*7c478bd9Sstevel@tonic-gate 		ipv6rem = B_FALSE;
304*7c478bd9Sstevel@tonic-gate 	else
305*7c478bd9Sstevel@tonic-gate 		ipv6rem = B_TRUE;
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	freeaddrinfo(ai_head);
309*7c478bd9Sstevel@tonic-gate 	ai = NULL;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	/*
312*7c478bd9Sstevel@tonic-gate 	 * Set passive mode flag on by default only if a native IPv6 address
313*7c478bd9Sstevel@tonic-gate 	 * is being used -and- the use_eprt is not set.
314*7c478bd9Sstevel@tonic-gate 	 */
315*7c478bd9Sstevel@tonic-gate 	if (ipv6rem == B_TRUE && use_eprt == 0)
316*7c478bd9Sstevel@tonic-gate 		passivemode = 1;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	len = sizeof (myctladdr);
319*7c478bd9Sstevel@tonic-gate 	if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
320*7c478bd9Sstevel@tonic-gate 		perror("ftp: getsockname");
321*7c478bd9Sstevel@tonic-gate 		code = -1;
322*7c478bd9Sstevel@tonic-gate 		goto bad;
323*7c478bd9Sstevel@tonic-gate 	}
324*7c478bd9Sstevel@tonic-gate 	ctrl_in = fdopen(s, "r");
325*7c478bd9Sstevel@tonic-gate 	ctrl_out = fdopen(s, "w");
326*7c478bd9Sstevel@tonic-gate 	if (ctrl_in == NULL || ctrl_out == NULL) {
327*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ftp: fdopen failed.\n");
328*7c478bd9Sstevel@tonic-gate 		if (ctrl_in)
329*7c478bd9Sstevel@tonic-gate 			(void) fclose(ctrl_in);
330*7c478bd9Sstevel@tonic-gate 		if (ctrl_out)
331*7c478bd9Sstevel@tonic-gate 			(void) fclose(ctrl_out);
332*7c478bd9Sstevel@tonic-gate 		code = -1;
333*7c478bd9Sstevel@tonic-gate 		goto bad;
334*7c478bd9Sstevel@tonic-gate 	}
335*7c478bd9Sstevel@tonic-gate 	if (verbose)
336*7c478bd9Sstevel@tonic-gate 		(void) printf("Connected to %s.\n", hostname);
337*7c478bd9Sstevel@tonic-gate 	if (getreply(0) > 2) {	/* read startup message from server */
338*7c478bd9Sstevel@tonic-gate 		if (ctrl_in)
339*7c478bd9Sstevel@tonic-gate 			(void) fclose(ctrl_in);
340*7c478bd9Sstevel@tonic-gate 		if (ctrl_out)
341*7c478bd9Sstevel@tonic-gate 			(void) fclose(ctrl_out);
342*7c478bd9Sstevel@tonic-gate 		ctrl_in = ctrl_out = NULL;
343*7c478bd9Sstevel@tonic-gate 		ctrl_in = ctrl_out = NULL;
344*7c478bd9Sstevel@tonic-gate 		code = -1;
345*7c478bd9Sstevel@tonic-gate 		goto bad;
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
348*7c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0 && debug)
349*7c478bd9Sstevel@tonic-gate 		perror("ftp: setsockopt (SO_OOBINLINE)");
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	return (hostname);
352*7c478bd9Sstevel@tonic-gate bad:
353*7c478bd9Sstevel@tonic-gate 	(void) close(s);
354*7c478bd9Sstevel@tonic-gate 	return ((char *)0);
355*7c478bd9Sstevel@tonic-gate }
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate int
358*7c478bd9Sstevel@tonic-gate login(char *host)
359*7c478bd9Sstevel@tonic-gate {
360*7c478bd9Sstevel@tonic-gate 	char tmp[80];
361*7c478bd9Sstevel@tonic-gate 	char *user, *pass, *acct;
362*7c478bd9Sstevel@tonic-gate 	int n, aflag = 0;
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	user = pass = acct = 0;
365*7c478bd9Sstevel@tonic-gate 	if (ruserpass(host, &user, &pass, &acct) < 0) {
366*7c478bd9Sstevel@tonic-gate 		disconnect(0, NULL);
367*7c478bd9Sstevel@tonic-gate 		code = -1;
368*7c478bd9Sstevel@tonic-gate 		return (0);
369*7c478bd9Sstevel@tonic-gate 	}
370*7c478bd9Sstevel@tonic-gate 	if (user == NULL) {
371*7c478bd9Sstevel@tonic-gate 		char *myname = getlogin();
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 		if (myname == NULL) {
374*7c478bd9Sstevel@tonic-gate 			struct passwd *pp = getpwuid(getuid());
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 			if (pp != NULL)
377*7c478bd9Sstevel@tonic-gate 				myname = pp->pw_name;
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 		stop_timer();
380*7c478bd9Sstevel@tonic-gate 		(void) printf("Name (%s:%s): ", host,
381*7c478bd9Sstevel@tonic-gate 			(myname == NULL) ? "" : myname);
382*7c478bd9Sstevel@tonic-gate 		*tmp = '\0';
383*7c478bd9Sstevel@tonic-gate 		if (fgets(tmp, sizeof (tmp) - 1, stdin) != NULL)
384*7c478bd9Sstevel@tonic-gate 			tmp[strlen(tmp) - 1] = '\0';
385*7c478bd9Sstevel@tonic-gate 		if (*tmp != '\0')
386*7c478bd9Sstevel@tonic-gate 			user = tmp;
387*7c478bd9Sstevel@tonic-gate 		else if (myname != NULL)
388*7c478bd9Sstevel@tonic-gate 			user = myname;
389*7c478bd9Sstevel@tonic-gate 		else
390*7c478bd9Sstevel@tonic-gate 			return (0);
391*7c478bd9Sstevel@tonic-gate 	}
392*7c478bd9Sstevel@tonic-gate 	n = command("USER %s", user);
393*7c478bd9Sstevel@tonic-gate 	if (n == CONTINUE) {
394*7c478bd9Sstevel@tonic-gate 		int oldclevel;
395*7c478bd9Sstevel@tonic-gate 		if (pass == NULL)
396*7c478bd9Sstevel@tonic-gate 			pass = mygetpass("Password:");
397*7c478bd9Sstevel@tonic-gate 		oldclevel = clevel;
398*7c478bd9Sstevel@tonic-gate 		clevel = PROT_P;
399*7c478bd9Sstevel@tonic-gate 		n = command("PASS %s", pass);
400*7c478bd9Sstevel@tonic-gate 		/* level may have changed */
401*7c478bd9Sstevel@tonic-gate 		if (clevel == PROT_P)
402*7c478bd9Sstevel@tonic-gate 			clevel = oldclevel;
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 	if (n == CONTINUE) {
405*7c478bd9Sstevel@tonic-gate 		aflag++;
406*7c478bd9Sstevel@tonic-gate 		if (acct == NULL)
407*7c478bd9Sstevel@tonic-gate 			acct = mygetpass("Account:");
408*7c478bd9Sstevel@tonic-gate 		n = command("ACCT %s", acct);
409*7c478bd9Sstevel@tonic-gate 	}
410*7c478bd9Sstevel@tonic-gate 	if (n != COMPLETE) {
411*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Login failed.\n");
412*7c478bd9Sstevel@tonic-gate 		return (0);
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 	if (!aflag && acct != NULL)
415*7c478bd9Sstevel@tonic-gate 		(void) command("ACCT %s", acct);
416*7c478bd9Sstevel@tonic-gate 	if (proxy)
417*7c478bd9Sstevel@tonic-gate 		return (1);
418*7c478bd9Sstevel@tonic-gate 	for (n = 0; n < macnum; ++n) {
419*7c478bd9Sstevel@tonic-gate 		if (strcmp("init", macros[n].mac_name) == 0) {
420*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(line, "$init", sizeof (line));
421*7c478bd9Sstevel@tonic-gate 			makeargv();
422*7c478bd9Sstevel@tonic-gate 			domacro(margc, margv);
423*7c478bd9Sstevel@tonic-gate 			break;
424*7c478bd9Sstevel@tonic-gate 		}
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 	return (1);
427*7c478bd9Sstevel@tonic-gate }
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
430*7c478bd9Sstevel@tonic-gate static void
431*7c478bd9Sstevel@tonic-gate cmdabort(int sig)
432*7c478bd9Sstevel@tonic-gate {
433*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
434*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
435*7c478bd9Sstevel@tonic-gate 	abrtflag++;
436*7c478bd9Sstevel@tonic-gate 	if (ptflag)
437*7c478bd9Sstevel@tonic-gate 		longjmp(ptabort, 1);
438*7c478bd9Sstevel@tonic-gate }
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate int
441*7c478bd9Sstevel@tonic-gate command(char *fmt, ...)
442*7c478bd9Sstevel@tonic-gate {
443*7c478bd9Sstevel@tonic-gate 	int r;
444*7c478bd9Sstevel@tonic-gate 	void (*oldintr)();
445*7c478bd9Sstevel@tonic-gate 	va_list ap;
446*7c478bd9Sstevel@tonic-gate 	char command_buf[FTPBUFSIZ];
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
449*7c478bd9Sstevel@tonic-gate 	abrtflag = 0;
450*7c478bd9Sstevel@tonic-gate 	if (debug) {
451*7c478bd9Sstevel@tonic-gate 		(void) printf("---> ");
452*7c478bd9Sstevel@tonic-gate 		if (strncmp("PASS ", fmt, 5) == 0)
453*7c478bd9Sstevel@tonic-gate 			(void) printf("PASS XXXX");
454*7c478bd9Sstevel@tonic-gate 		else if (strncmp("ACCT ", fmt, 5) == 0)
455*7c478bd9Sstevel@tonic-gate 			(void) printf("ACCT XXXX");
456*7c478bd9Sstevel@tonic-gate 		else
457*7c478bd9Sstevel@tonic-gate 			(void) vfprintf(stdout, fmt, ap);
458*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
459*7c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
460*7c478bd9Sstevel@tonic-gate 	}
461*7c478bd9Sstevel@tonic-gate 	if (ctrl_out == NULL) {
462*7c478bd9Sstevel@tonic-gate 		perror("No control connection for command");
463*7c478bd9Sstevel@tonic-gate 		code = -1;
464*7c478bd9Sstevel@tonic-gate 		return (0);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, cmdabort);
467*7c478bd9Sstevel@tonic-gate 	(void) vsnprintf(command_buf, FTPBUFSIZ, fmt, ap);
468*7c478bd9Sstevel@tonic-gate 	va_end(ap);
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate again:	if (secure_command(command_buf) == 0)
471*7c478bd9Sstevel@tonic-gate 		return (0);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	cpend = 1;
474*7c478bd9Sstevel@tonic-gate 	r = getreply(strcmp(fmt, "QUIT") == 0);
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	if (r == 533 && clevel == PROT_P) {
477*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ENC command not supported at server; "
478*7c478bd9Sstevel@tonic-gate 			"retrying under MIC...\n");
479*7c478bd9Sstevel@tonic-gate 		clevel = PROT_S;
480*7c478bd9Sstevel@tonic-gate 		goto again;
481*7c478bd9Sstevel@tonic-gate 	}
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	if (abrtflag && oldintr != SIG_IGN)
484*7c478bd9Sstevel@tonic-gate 		(*oldintr)();
485*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
486*7c478bd9Sstevel@tonic-gate 	return (r);
487*7c478bd9Sstevel@tonic-gate }
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate /* Need to save reply reponse from server for use in EPSV mode */
490*7c478bd9Sstevel@tonic-gate char reply_string[BUFSIZ];
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate int
493*7c478bd9Sstevel@tonic-gate getreply(int expecteof)
494*7c478bd9Sstevel@tonic-gate {
495*7c478bd9Sstevel@tonic-gate 	/*
496*7c478bd9Sstevel@tonic-gate 	 * 'code' is the 3 digit reply code, form xyz
497*7c478bd9Sstevel@tonic-gate 	 * 'dig'  counts the number of digits we are along in the code
498*7c478bd9Sstevel@tonic-gate 	 * 'n'	is the first digit of 'code'
499*7c478bd9Sstevel@tonic-gate 	 *	4yz: resource unavailable
500*7c478bd9Sstevel@tonic-gate 	 *	5yz: an error occurred, failure
501*7c478bd9Sstevel@tonic-gate 	 *	6yz: protected reply (is_base64 == TRUE)
502*7c478bd9Sstevel@tonic-gate 	 *		631 - base 64 encoded safe message
503*7c478bd9Sstevel@tonic-gate 	 * 		632 - base 64 encoded private message
504*7c478bd9Sstevel@tonic-gate 	 * 		633 - base 64 encoded confidential message
505*7c478bd9Sstevel@tonic-gate 	 * 'c'	is a wide char type, for international char sets
506*7c478bd9Sstevel@tonic-gate 	 */
507*7c478bd9Sstevel@tonic-gate 	wint_t c;
508*7c478bd9Sstevel@tonic-gate 	int i, n;
509*7c478bd9Sstevel@tonic-gate 	int dig;
510*7c478bd9Sstevel@tonic-gate 	int originalcode = 0, continuation = 0;
511*7c478bd9Sstevel@tonic-gate 	void (*oldintr)();
512*7c478bd9Sstevel@tonic-gate 	int pflag = 0;
513*7c478bd9Sstevel@tonic-gate 	char *pt = pasv;
514*7c478bd9Sstevel@tonic-gate 	/*
515*7c478bd9Sstevel@tonic-gate 	 * this is the input and output buffers needed for
516*7c478bd9Sstevel@tonic-gate 	 * radix_encode()
517*7c478bd9Sstevel@tonic-gate 	 */
518*7c478bd9Sstevel@tonic-gate 	unsigned char ibuf[FTPBUFSIZ];
519*7c478bd9Sstevel@tonic-gate 	unsigned char obuf[FTPBUFSIZ];
520*7c478bd9Sstevel@tonic-gate 	boolean_t is_base64;
521*7c478bd9Sstevel@tonic-gate 	int len;
522*7c478bd9Sstevel@tonic-gate 	char *cp;
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	if (!ctrl_in)
525*7c478bd9Sstevel@tonic-gate 		return (0);
526*7c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, cmdabort);
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 	ibuf[0] = '\0';
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	if (reply_parse)
531*7c478bd9Sstevel@tonic-gate 		reply_ptr = reply_buf;
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	for (;;) {
534*7c478bd9Sstevel@tonic-gate 		obuf[0] = '\0';
535*7c478bd9Sstevel@tonic-gate 		dig = n = code = 0;
536*7c478bd9Sstevel@tonic-gate 		i = is_base64 = 0;
537*7c478bd9Sstevel@tonic-gate 		cp = reply_string;
538*7c478bd9Sstevel@tonic-gate 		reset_timer();	/* once per line */
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 		while ((c = ibuf[0] ?
541*7c478bd9Sstevel@tonic-gate 		    (wint_t)ibuf[i++] : fgetwc(ctrl_in)) != '\n') {
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 		    if (i >= FTPBUFSIZ)
544*7c478bd9Sstevel@tonic-gate 			break;
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 		    if (c == IAC) {	/* handle telnet commands */
547*7c478bd9Sstevel@tonic-gate 			switch (c = fgetwc(ctrl_in)) {
548*7c478bd9Sstevel@tonic-gate 			    case WILL:
549*7c478bd9Sstevel@tonic-gate 			    case WONT:
550*7c478bd9Sstevel@tonic-gate 				c = fgetwc(ctrl_in);
551*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ctrl_out, "%c%c%wc", IAC,
552*7c478bd9Sstevel@tonic-gate 				    WONT, c);
553*7c478bd9Sstevel@tonic-gate 				(void) fflush(ctrl_out);
554*7c478bd9Sstevel@tonic-gate 				break;
555*7c478bd9Sstevel@tonic-gate 			    case DO:
556*7c478bd9Sstevel@tonic-gate 			    case DONT:
557*7c478bd9Sstevel@tonic-gate 				c = fgetwc(ctrl_in);
558*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ctrl_out, "%c%c%wc", IAC,
559*7c478bd9Sstevel@tonic-gate 				    DONT, c);
560*7c478bd9Sstevel@tonic-gate 				(void) fflush(ctrl_out);
561*7c478bd9Sstevel@tonic-gate 				break;
562*7c478bd9Sstevel@tonic-gate 			    default:
563*7c478bd9Sstevel@tonic-gate 				break;
564*7c478bd9Sstevel@tonic-gate 			}
565*7c478bd9Sstevel@tonic-gate 			continue;
566*7c478bd9Sstevel@tonic-gate 		    }
567*7c478bd9Sstevel@tonic-gate 		    dig++;
568*7c478bd9Sstevel@tonic-gate 		    if (c == EOF) {
569*7c478bd9Sstevel@tonic-gate 			if (expecteof) {
570*7c478bd9Sstevel@tonic-gate 				(void) signal(SIGINT, oldintr);
571*7c478bd9Sstevel@tonic-gate 				code = 221;
572*7c478bd9Sstevel@tonic-gate 				return (0);
573*7c478bd9Sstevel@tonic-gate 			}
574*7c478bd9Sstevel@tonic-gate 			lostpeer(0);
575*7c478bd9Sstevel@tonic-gate 			if (verbose) {
576*7c478bd9Sstevel@tonic-gate 				(void) printf(
577*7c478bd9Sstevel@tonic-gate 				    "421 Service not available, remote"
578*7c478bd9Sstevel@tonic-gate 				    " server has closed connection\n");
579*7c478bd9Sstevel@tonic-gate 			} else
580*7c478bd9Sstevel@tonic-gate 				(void) printf("Lost connection\n");
581*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
582*7c478bd9Sstevel@tonic-gate 			code = 421;
583*7c478bd9Sstevel@tonic-gate 			return (4);
584*7c478bd9Sstevel@tonic-gate 		    }
585*7c478bd9Sstevel@tonic-gate 		    if (n == 0)
586*7c478bd9Sstevel@tonic-gate 			n = c;
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate 		    if (n == '6')
589*7c478bd9Sstevel@tonic-gate 			is_base64 = 1;
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 		    if ((auth_type != AUTHTYPE_NONE) && !ibuf[0] &&
592*7c478bd9Sstevel@tonic-gate 			(is_base64 || continuation))  {
593*7c478bd9Sstevel@tonic-gate 			/* start storing chars in obuf */
594*7c478bd9Sstevel@tonic-gate 			if (c != '\r' && dig > 4)
595*7c478bd9Sstevel@tonic-gate 				obuf[i++] = (char)c;
596*7c478bd9Sstevel@tonic-gate 		    } else {
597*7c478bd9Sstevel@tonic-gate 			if ((auth_type != AUTHTYPE_NONE) && !ibuf[0] &&
598*7c478bd9Sstevel@tonic-gate 			    dig == 1 && verbose)
599*7c478bd9Sstevel@tonic-gate 			    (void) printf("Unauthenticated reply received "
600*7c478bd9Sstevel@tonic-gate 				"from server:\n");
601*7c478bd9Sstevel@tonic-gate 			if (reply_parse)
602*7c478bd9Sstevel@tonic-gate 				*reply_ptr++ = (char)c;
603*7c478bd9Sstevel@tonic-gate 			if (c != '\r' && (verbose > 0 ||
604*7c478bd9Sstevel@tonic-gate 			    (verbose > -1 && n == '5' && dig > 4))) {
605*7c478bd9Sstevel@tonic-gate 				if (proxflag &&
606*7c478bd9Sstevel@tonic-gate 				    (dig == 1 || dig == 5 && verbose == 0))
607*7c478bd9Sstevel@tonic-gate 					(void) printf("%s:", hostname);
608*7c478bd9Sstevel@tonic-gate 				(void) putwchar(c);
609*7c478bd9Sstevel@tonic-gate 			}
610*7c478bd9Sstevel@tonic-gate 		    } /* endif auth_type && !ibuf[0] ... */
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 		    if ((auth_type != AUTHTYPE_NONE) && !ibuf[0] && !is_base64)
613*7c478bd9Sstevel@tonic-gate 			continue;
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 		    /* we are still extracting the 3 digit code */
616*7c478bd9Sstevel@tonic-gate 		    if (dig < 4 && isascii(c) && isdigit(c))
617*7c478bd9Sstevel@tonic-gate 			code = code * 10 + (c - '0');
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 		    /* starting passive mode */
620*7c478bd9Sstevel@tonic-gate 		    if (!pflag && code == 227)
621*7c478bd9Sstevel@tonic-gate 			pflag = 1;
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 		    /* start to store characters, when dig > 4 */
624*7c478bd9Sstevel@tonic-gate 		    if (dig > 4 && pflag == 1 && isascii(c) && isdigit(c))
625*7c478bd9Sstevel@tonic-gate 			pflag = 2;
626*7c478bd9Sstevel@tonic-gate 		    if (pflag == 2) {
627*7c478bd9Sstevel@tonic-gate 			if (c != '\r' && c != ')') {
628*7c478bd9Sstevel@tonic-gate 				/* the mb array is to deal with the wchar_t */
629*7c478bd9Sstevel@tonic-gate 				char mb[MB_LEN_MAX];
630*7c478bd9Sstevel@tonic-gate 				int avail;
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate 				/*
633*7c478bd9Sstevel@tonic-gate 				 * space available in pasv[], accounting
634*7c478bd9Sstevel@tonic-gate 				 * for trailing NULL
635*7c478bd9Sstevel@tonic-gate 				 */
636*7c478bd9Sstevel@tonic-gate 				avail = &pasv[sizeof (pasv)] - pt - 1;
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate 				len = wctomb(mb, c);
639*7c478bd9Sstevel@tonic-gate 				if (len <= 0 && avail > 0) {
640*7c478bd9Sstevel@tonic-gate 					*pt++ = (unsigned char)c;
641*7c478bd9Sstevel@tonic-gate 				} else if (len > 0 && avail >= len) {
642*7c478bd9Sstevel@tonic-gate 					bcopy(mb, pt, (size_t)len);
643*7c478bd9Sstevel@tonic-gate 					pt += len;
644*7c478bd9Sstevel@tonic-gate 				} else {
645*7c478bd9Sstevel@tonic-gate 					/*
646*7c478bd9Sstevel@tonic-gate 					 * no room in pasv[];
647*7c478bd9Sstevel@tonic-gate 					 * close connection
648*7c478bd9Sstevel@tonic-gate 					 */
649*7c478bd9Sstevel@tonic-gate 					(void) printf("\nReply too long - "
650*7c478bd9Sstevel@tonic-gate 					    "closing connection\n");
651*7c478bd9Sstevel@tonic-gate 					lostpeer(0);
652*7c478bd9Sstevel@tonic-gate 					(void) fflush(stdout);
653*7c478bd9Sstevel@tonic-gate 					(void) signal(SIGINT, oldintr);
654*7c478bd9Sstevel@tonic-gate 					return (4);
655*7c478bd9Sstevel@tonic-gate 				}
656*7c478bd9Sstevel@tonic-gate 			} else {
657*7c478bd9Sstevel@tonic-gate 				*pt = '\0';
658*7c478bd9Sstevel@tonic-gate 				pflag = 3;
659*7c478bd9Sstevel@tonic-gate 			}
660*7c478bd9Sstevel@tonic-gate 		    } /* endif pflag == 2 */
661*7c478bd9Sstevel@tonic-gate 		    if (dig == 4 && c == '-' && !is_base64) {
662*7c478bd9Sstevel@tonic-gate 			if (continuation)
663*7c478bd9Sstevel@tonic-gate 				code = 0;
664*7c478bd9Sstevel@tonic-gate 			continuation++;
665*7c478bd9Sstevel@tonic-gate 		    }
666*7c478bd9Sstevel@tonic-gate 		    if (cp < &reply_string[sizeof (reply_string) - 1])
667*7c478bd9Sstevel@tonic-gate 			*cp++ = c;
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 		} /* end while */
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate 		if ((auth_type != AUTHTYPE_NONE) && !ibuf[0] && !is_base64)
672*7c478bd9Sstevel@tonic-gate 			return (getreply(expecteof));
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 		ibuf[0] = obuf[i] = '\0';
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 		if (code && is_base64) {
677*7c478bd9Sstevel@tonic-gate 		    boolean_t again = 0;
678*7c478bd9Sstevel@tonic-gate 		    n = decode_reply(ibuf, sizeof (ibuf), obuf, n, &again);
679*7c478bd9Sstevel@tonic-gate 		    if (again)
680*7c478bd9Sstevel@tonic-gate 			continue;
681*7c478bd9Sstevel@tonic-gate 		} else
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 		if (verbose > 0 || verbose > -1 && n == '5') {
684*7c478bd9Sstevel@tonic-gate 			(void) putwchar(c);
685*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
686*7c478bd9Sstevel@tonic-gate 		}
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate 		if (continuation && code != originalcode) {
689*7c478bd9Sstevel@tonic-gate 			ibuf[0] = obuf[i] = '\0';
690*7c478bd9Sstevel@tonic-gate 			if (originalcode == 0)
691*7c478bd9Sstevel@tonic-gate 				originalcode = code;
692*7c478bd9Sstevel@tonic-gate 			continue;
693*7c478bd9Sstevel@tonic-gate 		}
694*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
695*7c478bd9Sstevel@tonic-gate 		if (n != '1')
696*7c478bd9Sstevel@tonic-gate 			cpend = 0;
697*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
698*7c478bd9Sstevel@tonic-gate 		if (code == 421 || originalcode == 421)
699*7c478bd9Sstevel@tonic-gate 			lostpeer(0);
700*7c478bd9Sstevel@tonic-gate 		if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
701*7c478bd9Sstevel@tonic-gate 			(*oldintr)();
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 		if (reply_parse) {
704*7c478bd9Sstevel@tonic-gate 		    *reply_ptr = '\0';
705*7c478bd9Sstevel@tonic-gate 		    if (reply_ptr = strstr(reply_buf, reply_parse)) {
706*7c478bd9Sstevel@tonic-gate 			reply_parse = reply_ptr + strlen(reply_parse);
707*7c478bd9Sstevel@tonic-gate 			if (reply_ptr = strpbrk(reply_parse, " \r"))
708*7c478bd9Sstevel@tonic-gate 				*reply_ptr = '\0';
709*7c478bd9Sstevel@tonic-gate 		    } else
710*7c478bd9Sstevel@tonic-gate 			reply_parse = reply_ptr;
711*7c478bd9Sstevel@tonic-gate 		}
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 		return (n - '0');
714*7c478bd9Sstevel@tonic-gate 	} /* end for */
715*7c478bd9Sstevel@tonic-gate }
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate static int
718*7c478bd9Sstevel@tonic-gate empty(struct fd_set *mask, int sec, int nfds)
719*7c478bd9Sstevel@tonic-gate {
720*7c478bd9Sstevel@tonic-gate 	struct timeval t;
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 	reset_timer();
723*7c478bd9Sstevel@tonic-gate 	t.tv_sec = (time_t)sec;
724*7c478bd9Sstevel@tonic-gate 	t.tv_usec = 0;
725*7c478bd9Sstevel@tonic-gate 	return (select(nfds, mask, NULL, NULL, &t));
726*7c478bd9Sstevel@tonic-gate }
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
729*7c478bd9Sstevel@tonic-gate static void
730*7c478bd9Sstevel@tonic-gate abortsend(int sig)
731*7c478bd9Sstevel@tonic-gate {
732*7c478bd9Sstevel@tonic-gate 	mflag = 0;
733*7c478bd9Sstevel@tonic-gate 	abrtflag = 0;
734*7c478bd9Sstevel@tonic-gate 	(void) printf("\nsend aborted\n");
735*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
736*7c478bd9Sstevel@tonic-gate 	longjmp(sendabort, 1);
737*7c478bd9Sstevel@tonic-gate }
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate void
740*7c478bd9Sstevel@tonic-gate sendrequest(char *cmd, char *local, char *remote, int allowpipe)
741*7c478bd9Sstevel@tonic-gate {
742*7c478bd9Sstevel@tonic-gate 	FILE *fin, *dout = 0;
743*7c478bd9Sstevel@tonic-gate 	int (*closefunc)();
744*7c478bd9Sstevel@tonic-gate 	void (*oldintr)(), (*oldintp)();
745*7c478bd9Sstevel@tonic-gate 	off_t bytes = 0, hashbytes = HASHSIZ;
746*7c478bd9Sstevel@tonic-gate 	int c;
747*7c478bd9Sstevel@tonic-gate 	/*
748*7c478bd9Sstevel@tonic-gate 	 * d >=	 0 if there is no error
749*7c478bd9Sstevel@tonic-gate 	 *	-1 if there was a normal file i/o error
750*7c478bd9Sstevel@tonic-gate 	 *	-2 if there was a security error
751*7c478bd9Sstevel@tonic-gate 	 */
752*7c478bd9Sstevel@tonic-gate 	int d;
753*7c478bd9Sstevel@tonic-gate 	struct stat st;
754*7c478bd9Sstevel@tonic-gate 	hrtime_t start, stop;
755*7c478bd9Sstevel@tonic-gate 	char *dmode;
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 	if (proxy) {
758*7c478bd9Sstevel@tonic-gate 		proxtrans(cmd, local, remote);
759*7c478bd9Sstevel@tonic-gate 		return;
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate 	closefunc = NULL;
762*7c478bd9Sstevel@tonic-gate 	oldintr = NULL;
763*7c478bd9Sstevel@tonic-gate 	oldintp = NULL;
764*7c478bd9Sstevel@tonic-gate 	dmode = "w";
765*7c478bd9Sstevel@tonic-gate 	if (setjmp(sendabort)) {
766*7c478bd9Sstevel@tonic-gate 		while (cpend) {
767*7c478bd9Sstevel@tonic-gate 			(void) getreply(0);
768*7c478bd9Sstevel@tonic-gate 		}
769*7c478bd9Sstevel@tonic-gate 		if (data >= 0) {
770*7c478bd9Sstevel@tonic-gate 			(void) close(data);
771*7c478bd9Sstevel@tonic-gate 			data = -1;
772*7c478bd9Sstevel@tonic-gate 		}
773*7c478bd9Sstevel@tonic-gate 		if (oldintr)
774*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
775*7c478bd9Sstevel@tonic-gate 		if (oldintp)
776*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGPIPE, oldintp);
777*7c478bd9Sstevel@tonic-gate 		code = -1;
778*7c478bd9Sstevel@tonic-gate 		restart_point = 0;
779*7c478bd9Sstevel@tonic-gate 		return;
780*7c478bd9Sstevel@tonic-gate 	}
781*7c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, abortsend);
782*7c478bd9Sstevel@tonic-gate 	if (strcmp(local, "-") == 0)
783*7c478bd9Sstevel@tonic-gate 		fin = stdin;
784*7c478bd9Sstevel@tonic-gate 	else if (allowpipe && *local == '|') {
785*7c478bd9Sstevel@tonic-gate 		oldintp = signal(SIGPIPE, SIG_IGN);
786*7c478bd9Sstevel@tonic-gate 		fin = mypopen(local + 1, "r");
787*7c478bd9Sstevel@tonic-gate 		if (fin == NULL) {
788*7c478bd9Sstevel@tonic-gate 			perror(local + 1);
789*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
790*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGPIPE, oldintp);
791*7c478bd9Sstevel@tonic-gate 			code = -1;
792*7c478bd9Sstevel@tonic-gate 			restart_point = 0;
793*7c478bd9Sstevel@tonic-gate 			return;
794*7c478bd9Sstevel@tonic-gate 		}
795*7c478bd9Sstevel@tonic-gate 		closefunc = mypclose;
796*7c478bd9Sstevel@tonic-gate 	} else {
797*7c478bd9Sstevel@tonic-gate 		fin = fopen(local, "r");
798*7c478bd9Sstevel@tonic-gate 		if (fin == NULL) {
799*7c478bd9Sstevel@tonic-gate 			perror(local);
800*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
801*7c478bd9Sstevel@tonic-gate 			code = -1;
802*7c478bd9Sstevel@tonic-gate 			restart_point = 0;
803*7c478bd9Sstevel@tonic-gate 			return;
804*7c478bd9Sstevel@tonic-gate 		}
805*7c478bd9Sstevel@tonic-gate 		closefunc = fclose;
806*7c478bd9Sstevel@tonic-gate 		if (fstat(fileno(fin), &st) < 0 ||
807*7c478bd9Sstevel@tonic-gate 		    (st.st_mode&S_IFMT) != S_IFREG) {
808*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout,
809*7c478bd9Sstevel@tonic-gate 				"%s: not a plain file.\n", local);
810*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
811*7c478bd9Sstevel@tonic-gate 			code = -1;
812*7c478bd9Sstevel@tonic-gate 			(void) fclose(fin);
813*7c478bd9Sstevel@tonic-gate 			restart_point = 0;
814*7c478bd9Sstevel@tonic-gate 			return;
815*7c478bd9Sstevel@tonic-gate 		}
816*7c478bd9Sstevel@tonic-gate 	}
817*7c478bd9Sstevel@tonic-gate 	if (initconn()) {
818*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
819*7c478bd9Sstevel@tonic-gate 		if (oldintp)
820*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGPIPE, oldintp);
821*7c478bd9Sstevel@tonic-gate 		code = -1;
822*7c478bd9Sstevel@tonic-gate 		if (closefunc != NULL)
823*7c478bd9Sstevel@tonic-gate 			(*closefunc)(fin);
824*7c478bd9Sstevel@tonic-gate 		restart_point = 0;
825*7c478bd9Sstevel@tonic-gate 		return;
826*7c478bd9Sstevel@tonic-gate 	}
827*7c478bd9Sstevel@tonic-gate 	if (setjmp(sendabort))
828*7c478bd9Sstevel@tonic-gate 		goto abort;
829*7c478bd9Sstevel@tonic-gate 	if ((restart_point > 0) &&
830*7c478bd9Sstevel@tonic-gate 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
831*7c478bd9Sstevel@tonic-gate 		if (fseeko(fin, restart_point, SEEK_SET) < 0) {
832*7c478bd9Sstevel@tonic-gate 			perror(local);
833*7c478bd9Sstevel@tonic-gate 			if (closefunc != NULL)
834*7c478bd9Sstevel@tonic-gate 				(*closefunc)(fin);
835*7c478bd9Sstevel@tonic-gate 			restart_point = 0;
836*7c478bd9Sstevel@tonic-gate 			return;
837*7c478bd9Sstevel@tonic-gate 		}
838*7c478bd9Sstevel@tonic-gate 		if (command("REST %lld", (longlong_t)restart_point)
839*7c478bd9Sstevel@tonic-gate 			!= CONTINUE) {
840*7c478bd9Sstevel@tonic-gate 			if (closefunc != NULL)
841*7c478bd9Sstevel@tonic-gate 				(*closefunc)(fin);
842*7c478bd9Sstevel@tonic-gate 			restart_point = 0;
843*7c478bd9Sstevel@tonic-gate 			return;
844*7c478bd9Sstevel@tonic-gate 		}
845*7c478bd9Sstevel@tonic-gate 		dmode = "r+w";
846*7c478bd9Sstevel@tonic-gate 	}
847*7c478bd9Sstevel@tonic-gate 	restart_point = 0;
848*7c478bd9Sstevel@tonic-gate 	if (remote) {
849*7c478bd9Sstevel@tonic-gate 		if (command("%s %s", cmd, remote) != PRELIM) {
850*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
851*7c478bd9Sstevel@tonic-gate 			if (oldintp)
852*7c478bd9Sstevel@tonic-gate 				(void) signal(SIGPIPE, oldintp);
853*7c478bd9Sstevel@tonic-gate 			if (closefunc != NULL)
854*7c478bd9Sstevel@tonic-gate 				(*closefunc)(fin);
855*7c478bd9Sstevel@tonic-gate 			if (data >= 0) {
856*7c478bd9Sstevel@tonic-gate 				(void) close(data);
857*7c478bd9Sstevel@tonic-gate 				data = -1;
858*7c478bd9Sstevel@tonic-gate 			}
859*7c478bd9Sstevel@tonic-gate 			return;
860*7c478bd9Sstevel@tonic-gate 		}
861*7c478bd9Sstevel@tonic-gate 	} else
862*7c478bd9Sstevel@tonic-gate 		if (command("%s", cmd) != PRELIM) {
863*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
864*7c478bd9Sstevel@tonic-gate 			if (oldintp)
865*7c478bd9Sstevel@tonic-gate 				(void) signal(SIGPIPE, oldintp);
866*7c478bd9Sstevel@tonic-gate 			if (closefunc != NULL)
867*7c478bd9Sstevel@tonic-gate 				(*closefunc)(fin);
868*7c478bd9Sstevel@tonic-gate 			if (data >= 0) {
869*7c478bd9Sstevel@tonic-gate 				(void) close(data);
870*7c478bd9Sstevel@tonic-gate 				data = -1;
871*7c478bd9Sstevel@tonic-gate 			}
872*7c478bd9Sstevel@tonic-gate 			return;
873*7c478bd9Sstevel@tonic-gate 		}
874*7c478bd9Sstevel@tonic-gate 	dout = dataconn(dmode);
875*7c478bd9Sstevel@tonic-gate 	if (dout == NULL)
876*7c478bd9Sstevel@tonic-gate 		goto abort;
877*7c478bd9Sstevel@tonic-gate 	stop_timer();
878*7c478bd9Sstevel@tonic-gate 	oldintp = signal(SIGPIPE, SIG_IGN);
879*7c478bd9Sstevel@tonic-gate 	start = gethrtime();
880*7c478bd9Sstevel@tonic-gate 	switch (type) {
881*7c478bd9Sstevel@tonic-gate 
882*7c478bd9Sstevel@tonic-gate 	case TYPE_I:
883*7c478bd9Sstevel@tonic-gate 	case TYPE_L:
884*7c478bd9Sstevel@tonic-gate 		errno = d = 0;
885*7c478bd9Sstevel@tonic-gate 		while ((c = read(fileno(fin), buf, FTPBUFSIZ)) > 0) {
886*7c478bd9Sstevel@tonic-gate 			if ((d = WRITE(fileno(dout), buf, c)) < 0)
887*7c478bd9Sstevel@tonic-gate 				break;
888*7c478bd9Sstevel@tonic-gate 			bytes += c;
889*7c478bd9Sstevel@tonic-gate 			if (hash) {
890*7c478bd9Sstevel@tonic-gate 				while (bytes >= hashbytes) {
891*7c478bd9Sstevel@tonic-gate 					(void) putchar('#');
892*7c478bd9Sstevel@tonic-gate 					hashbytes += HASHSIZ;
893*7c478bd9Sstevel@tonic-gate 				}
894*7c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
895*7c478bd9Sstevel@tonic-gate 			}
896*7c478bd9Sstevel@tonic-gate 		}
897*7c478bd9Sstevel@tonic-gate 		if (hash && bytes > 0) {
898*7c478bd9Sstevel@tonic-gate 			if (bytes < hashbytes)
899*7c478bd9Sstevel@tonic-gate 				(void) putchar('#');
900*7c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
901*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
902*7c478bd9Sstevel@tonic-gate 		}
903*7c478bd9Sstevel@tonic-gate 		if (c < 0)
904*7c478bd9Sstevel@tonic-gate 			perror(local);
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate 		if (d >= 0)
907*7c478bd9Sstevel@tonic-gate 			d = secure_flush(fileno(dout));
908*7c478bd9Sstevel@tonic-gate 
909*7c478bd9Sstevel@tonic-gate 		if (d < 0) {
910*7c478bd9Sstevel@tonic-gate 			if ((d == -1) && (errno != EPIPE))
911*7c478bd9Sstevel@tonic-gate 				perror("netout");
912*7c478bd9Sstevel@tonic-gate 			bytes = -1;
913*7c478bd9Sstevel@tonic-gate 		}
914*7c478bd9Sstevel@tonic-gate 		break;
915*7c478bd9Sstevel@tonic-gate 
916*7c478bd9Sstevel@tonic-gate 	case TYPE_A:
917*7c478bd9Sstevel@tonic-gate 		while ((c = getc(fin)) != EOF) {
918*7c478bd9Sstevel@tonic-gate 			if (c == '\n') {
919*7c478bd9Sstevel@tonic-gate 				while (hash && (bytes >= hashbytes)) {
920*7c478bd9Sstevel@tonic-gate 					(void) putchar('#');
921*7c478bd9Sstevel@tonic-gate 					(void) fflush(stdout);
922*7c478bd9Sstevel@tonic-gate 					hashbytes += HASHSIZ;
923*7c478bd9Sstevel@tonic-gate 				}
924*7c478bd9Sstevel@tonic-gate 				if (ferror(dout) || PUTC('\r', dout) < 0)
925*7c478bd9Sstevel@tonic-gate 					break;
926*7c478bd9Sstevel@tonic-gate 				bytes++;
927*7c478bd9Sstevel@tonic-gate 			}
928*7c478bd9Sstevel@tonic-gate 
929*7c478bd9Sstevel@tonic-gate 			if (PUTC(c, dout) < 0)
930*7c478bd9Sstevel@tonic-gate 				break;
931*7c478bd9Sstevel@tonic-gate 			bytes++;
932*7c478bd9Sstevel@tonic-gate #ifdef notdef
933*7c478bd9Sstevel@tonic-gate 			if (c == '\r') {
934*7c478bd9Sstevel@tonic-gate 				/* this violates rfc */
935*7c478bd9Sstevel@tonic-gate 				(void) PUTC('\0', dout);
936*7c478bd9Sstevel@tonic-gate 				bytes++;
937*7c478bd9Sstevel@tonic-gate 			}
938*7c478bd9Sstevel@tonic-gate #endif
939*7c478bd9Sstevel@tonic-gate 		}
940*7c478bd9Sstevel@tonic-gate 		if (hash && bytes > 0) {
941*7c478bd9Sstevel@tonic-gate 			if (bytes < hashbytes)
942*7c478bd9Sstevel@tonic-gate 				(void) putchar('#');
943*7c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
944*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
945*7c478bd9Sstevel@tonic-gate 		}
946*7c478bd9Sstevel@tonic-gate 		if (ferror(fin))
947*7c478bd9Sstevel@tonic-gate 			perror(local);
948*7c478bd9Sstevel@tonic-gate 
949*7c478bd9Sstevel@tonic-gate 		d = ferror(dout) ? -1 : 0;
950*7c478bd9Sstevel@tonic-gate 		if (d == 0)
951*7c478bd9Sstevel@tonic-gate 			d = secure_flush(fileno(dout));
952*7c478bd9Sstevel@tonic-gate 
953*7c478bd9Sstevel@tonic-gate 		if (d < 0) {
954*7c478bd9Sstevel@tonic-gate 			if ((d == -1) && (errno != EPIPE))
955*7c478bd9Sstevel@tonic-gate 				perror("netout");
956*7c478bd9Sstevel@tonic-gate 			bytes = -1;
957*7c478bd9Sstevel@tonic-gate 		}
958*7c478bd9Sstevel@tonic-gate 		break;
959*7c478bd9Sstevel@tonic-gate 	}
960*7c478bd9Sstevel@tonic-gate 	reset_timer();
961*7c478bd9Sstevel@tonic-gate 	if (closefunc != NULL)
962*7c478bd9Sstevel@tonic-gate 		(*closefunc)(fin);
963*7c478bd9Sstevel@tonic-gate 	if (ctrl_in != NULL) {
964*7c478bd9Sstevel@tonic-gate 		int	dfn	= fileno(dout);
965*7c478bd9Sstevel@tonic-gate 		int	nfds	= fileno(ctrl_in);
966*7c478bd9Sstevel@tonic-gate 		fd_set  mask;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 		/*
969*7c478bd9Sstevel@tonic-gate 		 * There could be data not yet written to dout,
970*7c478bd9Sstevel@tonic-gate 		 * in the stdio buffer; so, before a shutdown()
971*7c478bd9Sstevel@tonic-gate 		 * on further sends, do fflush(dout)
972*7c478bd9Sstevel@tonic-gate 		 */
973*7c478bd9Sstevel@tonic-gate 		(void) fflush(dout);
974*7c478bd9Sstevel@tonic-gate 
975*7c478bd9Sstevel@tonic-gate 		/* sending over; shutdown sending on dfn */
976*7c478bd9Sstevel@tonic-gate 		(void) shutdown(dfn, SHUT_WR);
977*7c478bd9Sstevel@tonic-gate 		FD_ZERO(&mask);
978*7c478bd9Sstevel@tonic-gate 		FD_SET(dfn, &mask);
979*7c478bd9Sstevel@tonic-gate 		FD_SET(nfds, &mask);
980*7c478bd9Sstevel@tonic-gate 		nfds = MAX(dfn, nfds);
981*7c478bd9Sstevel@tonic-gate 
982*7c478bd9Sstevel@tonic-gate 		/*
983*7c478bd9Sstevel@tonic-gate 		 * Wait for remote end to either close data socket
984*7c478bd9Sstevel@tonic-gate 		 * or ack that we've closed our end; it doesn't
985*7c478bd9Sstevel@tonic-gate 		 * matter which happens first.
986*7c478bd9Sstevel@tonic-gate 		 */
987*7c478bd9Sstevel@tonic-gate 		(void) select(nfds + 1, &mask, NULL, NULL, NULL);
988*7c478bd9Sstevel@tonic-gate 	}
989*7c478bd9Sstevel@tonic-gate 	(void) fclose(dout); data = -1;
990*7c478bd9Sstevel@tonic-gate 	stop = gethrtime();
991*7c478bd9Sstevel@tonic-gate 	(void) getreply(0);
992*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
993*7c478bd9Sstevel@tonic-gate 	if (oldintp)
994*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, oldintp);
995*7c478bd9Sstevel@tonic-gate 
996*7c478bd9Sstevel@tonic-gate 	/*
997*7c478bd9Sstevel@tonic-gate 	 * Only print the transfer successful message if the code returned
998*7c478bd9Sstevel@tonic-gate 	 * from remote is 226 or 250. All other codes are error codes.
999*7c478bd9Sstevel@tonic-gate 	 */
1000*7c478bd9Sstevel@tonic-gate 	if ((bytes > 0) && verbose && ((code == 226) || (code == 250)))
1001*7c478bd9Sstevel@tonic-gate 		ptransfer("sent", bytes, start, stop, local, remote);
1002*7c478bd9Sstevel@tonic-gate 	if (!ctrl_in)
1003*7c478bd9Sstevel@tonic-gate 		(void) printf("Lost connection\n");
1004*7c478bd9Sstevel@tonic-gate 	return;
1005*7c478bd9Sstevel@tonic-gate abort:
1006*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
1007*7c478bd9Sstevel@tonic-gate 	if (oldintp)
1008*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, oldintp);
1009*7c478bd9Sstevel@tonic-gate 	if (!cpend) {
1010*7c478bd9Sstevel@tonic-gate 		code = -1;
1011*7c478bd9Sstevel@tonic-gate 		return;
1012*7c478bd9Sstevel@tonic-gate 	}
1013*7c478bd9Sstevel@tonic-gate 	if (data >= 0) {
1014*7c478bd9Sstevel@tonic-gate 		(void) close(data);
1015*7c478bd9Sstevel@tonic-gate 		data = -1;
1016*7c478bd9Sstevel@tonic-gate 	}
1017*7c478bd9Sstevel@tonic-gate 	if (dout) {
1018*7c478bd9Sstevel@tonic-gate 		(void) fclose(dout);
1019*7c478bd9Sstevel@tonic-gate 		data = -1;
1020*7c478bd9Sstevel@tonic-gate 	}
1021*7c478bd9Sstevel@tonic-gate 	(void) getreply(0);
1022*7c478bd9Sstevel@tonic-gate 	code = -1;
1023*7c478bd9Sstevel@tonic-gate 	if (closefunc != NULL && fin != NULL)
1024*7c478bd9Sstevel@tonic-gate 		(*closefunc)(fin);
1025*7c478bd9Sstevel@tonic-gate 	stop = gethrtime();
1026*7c478bd9Sstevel@tonic-gate 	/*
1027*7c478bd9Sstevel@tonic-gate 	 * Only print the transfer successful message if the code returned
1028*7c478bd9Sstevel@tonic-gate 	 * from remote is 226 or 250. All other codes are error codes.
1029*7c478bd9Sstevel@tonic-gate 	 */
1030*7c478bd9Sstevel@tonic-gate 	if ((bytes > 0) && verbose && ((code == 226) || (code == 250)))
1031*7c478bd9Sstevel@tonic-gate 		ptransfer("sent", bytes, start, stop, local, remote);
1032*7c478bd9Sstevel@tonic-gate 	if (!ctrl_in)
1033*7c478bd9Sstevel@tonic-gate 		(void) printf("Lost connection\n");
1034*7c478bd9Sstevel@tonic-gate 	restart_point = 0;
1035*7c478bd9Sstevel@tonic-gate }
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1038*7c478bd9Sstevel@tonic-gate static void
1039*7c478bd9Sstevel@tonic-gate abortrecv(int sig)
1040*7c478bd9Sstevel@tonic-gate {
1041*7c478bd9Sstevel@tonic-gate 	mflag = 0;
1042*7c478bd9Sstevel@tonic-gate 	abrtflag = 0;
1043*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
1044*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
1045*7c478bd9Sstevel@tonic-gate 	longjmp(recvabort, 1);
1046*7c478bd9Sstevel@tonic-gate }
1047*7c478bd9Sstevel@tonic-gate 
1048*7c478bd9Sstevel@tonic-gate void
1049*7c478bd9Sstevel@tonic-gate recvrequest(char *cmd, char *local, char *remote, char *mode, int allowpipe)
1050*7c478bd9Sstevel@tonic-gate {
1051*7c478bd9Sstevel@tonic-gate 	FILE *fout, *din = 0;
1052*7c478bd9Sstevel@tonic-gate 	int (*closefunc)();
1053*7c478bd9Sstevel@tonic-gate 	void (*oldintr)(), (*oldintp)();
1054*7c478bd9Sstevel@tonic-gate 	int oldverbose, oldtype = 0, tcrflag, nfnd;
1055*7c478bd9Sstevel@tonic-gate 	char msg;
1056*7c478bd9Sstevel@tonic-gate 	off_t bytes = 0, hashbytes = HASHSIZ;
1057*7c478bd9Sstevel@tonic-gate 	struct fd_set mask;
1058*7c478bd9Sstevel@tonic-gate 	int c, d, n;
1059*7c478bd9Sstevel@tonic-gate 	hrtime_t start, stop;
1060*7c478bd9Sstevel@tonic-gate 	int errflg = 0;
1061*7c478bd9Sstevel@tonic-gate 	int infd;
1062*7c478bd9Sstevel@tonic-gate 	int nfds;
1063*7c478bd9Sstevel@tonic-gate 	int retrcmd;
1064*7c478bd9Sstevel@tonic-gate 
1065*7c478bd9Sstevel@tonic-gate 	retrcmd = (strcmp(cmd, "RETR") == 0);
1066*7c478bd9Sstevel@tonic-gate 	if (proxy && retrcmd) {
1067*7c478bd9Sstevel@tonic-gate 		proxtrans(cmd, local, remote);
1068*7c478bd9Sstevel@tonic-gate 		return;
1069*7c478bd9Sstevel@tonic-gate 	}
1070*7c478bd9Sstevel@tonic-gate 	closefunc = NULL;
1071*7c478bd9Sstevel@tonic-gate 	oldintr = NULL;
1072*7c478bd9Sstevel@tonic-gate 	oldintp = NULL;
1073*7c478bd9Sstevel@tonic-gate 	tcrflag = !crflag && retrcmd;
1074*7c478bd9Sstevel@tonic-gate 	if (setjmp(recvabort)) {
1075*7c478bd9Sstevel@tonic-gate 		while (cpend) {
1076*7c478bd9Sstevel@tonic-gate 			(void) getreply(0);
1077*7c478bd9Sstevel@tonic-gate 		}
1078*7c478bd9Sstevel@tonic-gate 		if (data >= 0) {
1079*7c478bd9Sstevel@tonic-gate 			(void) close(data);
1080*7c478bd9Sstevel@tonic-gate 			data = -1;
1081*7c478bd9Sstevel@tonic-gate 		}
1082*7c478bd9Sstevel@tonic-gate 		if (oldintr)
1083*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
1084*7c478bd9Sstevel@tonic-gate 		code = -1;
1085*7c478bd9Sstevel@tonic-gate 		return;
1086*7c478bd9Sstevel@tonic-gate 	}
1087*7c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, abortrecv);
1088*7c478bd9Sstevel@tonic-gate 	if (local != NULL &&
1089*7c478bd9Sstevel@tonic-gate 	    strcmp(local, "-") != 0 &&
1090*7c478bd9Sstevel@tonic-gate 	    (*local != '|' || !allowpipe)) {
1091*7c478bd9Sstevel@tonic-gate 		if (access(local, W_OK) < 0) {
1092*7c478bd9Sstevel@tonic-gate 			char *dir = rindex(local, '/');
1093*7c478bd9Sstevel@tonic-gate 			int file_errno = errno;
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate 			if (file_errno != ENOENT && file_errno != EACCES) {
1096*7c478bd9Sstevel@tonic-gate 				perror(local);
1097*7c478bd9Sstevel@tonic-gate 				(void) signal(SIGINT, oldintr);
1098*7c478bd9Sstevel@tonic-gate 				code = -1;
1099*7c478bd9Sstevel@tonic-gate 				return;
1100*7c478bd9Sstevel@tonic-gate 			}
1101*7c478bd9Sstevel@tonic-gate 			if ((dir != NULL) && (dir != local))
1102*7c478bd9Sstevel@tonic-gate 				*dir = 0;
1103*7c478bd9Sstevel@tonic-gate 			if (dir == local)
1104*7c478bd9Sstevel@tonic-gate 				d = access("/", W_OK);
1105*7c478bd9Sstevel@tonic-gate 			else
1106*7c478bd9Sstevel@tonic-gate 				d = access(dir ? local : ".", W_OK);
1107*7c478bd9Sstevel@tonic-gate 			if ((dir != NULL) && (dir != local))
1108*7c478bd9Sstevel@tonic-gate 				*dir = '/';
1109*7c478bd9Sstevel@tonic-gate 			if (d < 0) {
1110*7c478bd9Sstevel@tonic-gate 				perror(local);
1111*7c478bd9Sstevel@tonic-gate 				(void) signal(SIGINT, oldintr);
1112*7c478bd9Sstevel@tonic-gate 				code = -1;
1113*7c478bd9Sstevel@tonic-gate 				return;
1114*7c478bd9Sstevel@tonic-gate 			}
1115*7c478bd9Sstevel@tonic-gate 			if (!runique && file_errno == EACCES) {
1116*7c478bd9Sstevel@tonic-gate 				errno = file_errno;
1117*7c478bd9Sstevel@tonic-gate 				perror(local);
1118*7c478bd9Sstevel@tonic-gate 				(void) signal(SIGINT, oldintr);
1119*7c478bd9Sstevel@tonic-gate 				code = -1;
1120*7c478bd9Sstevel@tonic-gate 				return;
1121*7c478bd9Sstevel@tonic-gate 			}
1122*7c478bd9Sstevel@tonic-gate 			if (runique && file_errno == EACCES &&
1123*7c478bd9Sstevel@tonic-gate 			    (local = gunique(local)) == NULL) {
1124*7c478bd9Sstevel@tonic-gate 				(void) signal(SIGINT, oldintr);
1125*7c478bd9Sstevel@tonic-gate 				code = -1;
1126*7c478bd9Sstevel@tonic-gate 				return;
1127*7c478bd9Sstevel@tonic-gate 			}
1128*7c478bd9Sstevel@tonic-gate 		} else if (runique && (local = gunique(local)) == NULL) {
1129*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
1130*7c478bd9Sstevel@tonic-gate 			code = -1;
1131*7c478bd9Sstevel@tonic-gate 			return;
1132*7c478bd9Sstevel@tonic-gate 		}
1133*7c478bd9Sstevel@tonic-gate 	}
1134*7c478bd9Sstevel@tonic-gate 	if (initconn()) {
1135*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
1136*7c478bd9Sstevel@tonic-gate 		code = -1;
1137*7c478bd9Sstevel@tonic-gate 		return;
1138*7c478bd9Sstevel@tonic-gate 	}
1139*7c478bd9Sstevel@tonic-gate 	if (setjmp(recvabort))
1140*7c478bd9Sstevel@tonic-gate 		goto abort;
1141*7c478bd9Sstevel@tonic-gate 	if (!retrcmd && type != TYPE_A) {
1142*7c478bd9Sstevel@tonic-gate 		oldtype = type;
1143*7c478bd9Sstevel@tonic-gate 		oldverbose = verbose;
1144*7c478bd9Sstevel@tonic-gate 		if (!debug)
1145*7c478bd9Sstevel@tonic-gate 			verbose = 0;
1146*7c478bd9Sstevel@tonic-gate 		setascii(0, NULL);
1147*7c478bd9Sstevel@tonic-gate 		verbose = oldverbose;
1148*7c478bd9Sstevel@tonic-gate 	}
1149*7c478bd9Sstevel@tonic-gate 	if ((restart_point > 0) && retrcmd &&
1150*7c478bd9Sstevel@tonic-gate 	    command("REST %lld", (longlong_t)restart_point) != CONTINUE) {
1151*7c478bd9Sstevel@tonic-gate 		return;
1152*7c478bd9Sstevel@tonic-gate 	}
1153*7c478bd9Sstevel@tonic-gate 	if (remote) {
1154*7c478bd9Sstevel@tonic-gate 		if (command("%s %s", cmd, remote) != PRELIM) {
1155*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
1156*7c478bd9Sstevel@tonic-gate 			if (oldtype) {
1157*7c478bd9Sstevel@tonic-gate 				if (!debug)
1158*7c478bd9Sstevel@tonic-gate 					verbose = 0;
1159*7c478bd9Sstevel@tonic-gate 				switch (oldtype) {
1160*7c478bd9Sstevel@tonic-gate 					case TYPE_I:
1161*7c478bd9Sstevel@tonic-gate 						setbinary(0, NULL);
1162*7c478bd9Sstevel@tonic-gate 						break;
1163*7c478bd9Sstevel@tonic-gate 					case TYPE_E:
1164*7c478bd9Sstevel@tonic-gate 						setebcdic(0, NULL);
1165*7c478bd9Sstevel@tonic-gate 						break;
1166*7c478bd9Sstevel@tonic-gate 					case TYPE_L:
1167*7c478bd9Sstevel@tonic-gate 						settenex(0, NULL);
1168*7c478bd9Sstevel@tonic-gate 						break;
1169*7c478bd9Sstevel@tonic-gate 				}
1170*7c478bd9Sstevel@tonic-gate 				verbose = oldverbose;
1171*7c478bd9Sstevel@tonic-gate 			}
1172*7c478bd9Sstevel@tonic-gate 			return;
1173*7c478bd9Sstevel@tonic-gate 		}
1174*7c478bd9Sstevel@tonic-gate 	} else {
1175*7c478bd9Sstevel@tonic-gate 		if (command("%s", cmd) != PRELIM) {
1176*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
1177*7c478bd9Sstevel@tonic-gate 			if (oldtype) {
1178*7c478bd9Sstevel@tonic-gate 				if (!debug)
1179*7c478bd9Sstevel@tonic-gate 					verbose = 0;
1180*7c478bd9Sstevel@tonic-gate 				switch (oldtype) {
1181*7c478bd9Sstevel@tonic-gate 					case TYPE_I:
1182*7c478bd9Sstevel@tonic-gate 						setbinary(0, NULL);
1183*7c478bd9Sstevel@tonic-gate 						break;
1184*7c478bd9Sstevel@tonic-gate 					case TYPE_E:
1185*7c478bd9Sstevel@tonic-gate 						setebcdic(0, NULL);
1186*7c478bd9Sstevel@tonic-gate 						break;
1187*7c478bd9Sstevel@tonic-gate 					case TYPE_L:
1188*7c478bd9Sstevel@tonic-gate 						settenex(0, NULL);
1189*7c478bd9Sstevel@tonic-gate 						break;
1190*7c478bd9Sstevel@tonic-gate 				}
1191*7c478bd9Sstevel@tonic-gate 				verbose = oldverbose;
1192*7c478bd9Sstevel@tonic-gate 			}
1193*7c478bd9Sstevel@tonic-gate 			return;
1194*7c478bd9Sstevel@tonic-gate 		}
1195*7c478bd9Sstevel@tonic-gate 	}
1196*7c478bd9Sstevel@tonic-gate 	din = dataconn("r");
1197*7c478bd9Sstevel@tonic-gate 	if (din == NULL)
1198*7c478bd9Sstevel@tonic-gate 		goto abort;
1199*7c478bd9Sstevel@tonic-gate 
1200*7c478bd9Sstevel@tonic-gate 	if (local == NULL) {
1201*7c478bd9Sstevel@tonic-gate 		fout = tmp_nlst;
1202*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(local, "-") == 0) {
1203*7c478bd9Sstevel@tonic-gate 		fout = stdout;
1204*7c478bd9Sstevel@tonic-gate 	} else if (allowpipe && *local == '|') {
1205*7c478bd9Sstevel@tonic-gate 		oldintp = signal(SIGPIPE, SIG_IGN);
1206*7c478bd9Sstevel@tonic-gate 		fout = mypopen(local + 1, "w");
1207*7c478bd9Sstevel@tonic-gate 		if (fout == NULL) {
1208*7c478bd9Sstevel@tonic-gate 			perror(local+1);
1209*7c478bd9Sstevel@tonic-gate 			goto abort;
1210*7c478bd9Sstevel@tonic-gate 		}
1211*7c478bd9Sstevel@tonic-gate 		closefunc = mypclose;
1212*7c478bd9Sstevel@tonic-gate 	} else {
1213*7c478bd9Sstevel@tonic-gate 		fout = fopen(local, mode);
1214*7c478bd9Sstevel@tonic-gate 		if (fout == NULL) {
1215*7c478bd9Sstevel@tonic-gate 			perror(local);
1216*7c478bd9Sstevel@tonic-gate 			goto abort;
1217*7c478bd9Sstevel@tonic-gate 		}
1218*7c478bd9Sstevel@tonic-gate 		closefunc = fclose;
1219*7c478bd9Sstevel@tonic-gate 	}
1220*7c478bd9Sstevel@tonic-gate 	start = gethrtime();
1221*7c478bd9Sstevel@tonic-gate 	stop_timer();
1222*7c478bd9Sstevel@tonic-gate 	switch (type) {
1223*7c478bd9Sstevel@tonic-gate 
1224*7c478bd9Sstevel@tonic-gate 	case TYPE_I:
1225*7c478bd9Sstevel@tonic-gate 	case TYPE_L:
1226*7c478bd9Sstevel@tonic-gate 		if ((restart_point > 0) && retrcmd &&
1227*7c478bd9Sstevel@tonic-gate 		    lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
1228*7c478bd9Sstevel@tonic-gate 			perror(local);
1229*7c478bd9Sstevel@tonic-gate 			goto abort;
1230*7c478bd9Sstevel@tonic-gate 		}
1231*7c478bd9Sstevel@tonic-gate 		errno = d = 0;
1232*7c478bd9Sstevel@tonic-gate 		infd = fileno(din);
1233*7c478bd9Sstevel@tonic-gate 		while ((c = timedread(infd, buf, FTPBUFSIZ, timeout)) > 0) {
1234*7c478bd9Sstevel@tonic-gate 			for (n = 0; n < c; n += d) {
1235*7c478bd9Sstevel@tonic-gate 				d = write(fileno(fout), &buf[n], c - n);
1236*7c478bd9Sstevel@tonic-gate 				if (d == -1)
1237*7c478bd9Sstevel@tonic-gate 					goto writeerr;
1238*7c478bd9Sstevel@tonic-gate 			}
1239*7c478bd9Sstevel@tonic-gate 			bytes += c;
1240*7c478bd9Sstevel@tonic-gate 			if (hash) {
1241*7c478bd9Sstevel@tonic-gate 				while (bytes >= hashbytes) {
1242*7c478bd9Sstevel@tonic-gate 					(void) putchar('#');
1243*7c478bd9Sstevel@tonic-gate 					hashbytes += HASHSIZ;
1244*7c478bd9Sstevel@tonic-gate 				}
1245*7c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
1246*7c478bd9Sstevel@tonic-gate 			}
1247*7c478bd9Sstevel@tonic-gate 		}
1248*7c478bd9Sstevel@tonic-gate 		if (hash && bytes > 0) {
1249*7c478bd9Sstevel@tonic-gate 			if (bytes < hashbytes)
1250*7c478bd9Sstevel@tonic-gate 				(void) putchar('#');
1251*7c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
1252*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
1253*7c478bd9Sstevel@tonic-gate 		}
1254*7c478bd9Sstevel@tonic-gate 		if (c < 0) {
1255*7c478bd9Sstevel@tonic-gate 			errflg = 1;
1256*7c478bd9Sstevel@tonic-gate 			perror("netin");
1257*7c478bd9Sstevel@tonic-gate 		}
1258*7c478bd9Sstevel@tonic-gate 		if ((d < 0) || ((c == 0) && (fsync(fileno(fout)) == -1))) {
1259*7c478bd9Sstevel@tonic-gate writeerr:
1260*7c478bd9Sstevel@tonic-gate 			errflg = 1;
1261*7c478bd9Sstevel@tonic-gate 			perror(local);
1262*7c478bd9Sstevel@tonic-gate 		}
1263*7c478bd9Sstevel@tonic-gate 		break;
1264*7c478bd9Sstevel@tonic-gate 
1265*7c478bd9Sstevel@tonic-gate 	case TYPE_A:
1266*7c478bd9Sstevel@tonic-gate 		if ((restart_point > 0) && retrcmd) {
1267*7c478bd9Sstevel@tonic-gate 			int c;
1268*7c478bd9Sstevel@tonic-gate 			off_t i = 0;
1269*7c478bd9Sstevel@tonic-gate 
1270*7c478bd9Sstevel@tonic-gate 			if (fseek(fout, 0L, SEEK_SET) < 0) {
1271*7c478bd9Sstevel@tonic-gate 				perror(local);
1272*7c478bd9Sstevel@tonic-gate 				goto abort;
1273*7c478bd9Sstevel@tonic-gate 			}
1274*7c478bd9Sstevel@tonic-gate 			while (i++ < restart_point) {
1275*7c478bd9Sstevel@tonic-gate 				if ((c = getc(fout)) == EOF) {
1276*7c478bd9Sstevel@tonic-gate 					if (ferror(fout))
1277*7c478bd9Sstevel@tonic-gate 						perror(local);
1278*7c478bd9Sstevel@tonic-gate 					else
1279*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
1280*7c478bd9Sstevel@tonic-gate 						"%s: Unexpected end of file\n",
1281*7c478bd9Sstevel@tonic-gate 							local);
1282*7c478bd9Sstevel@tonic-gate 					goto abort;
1283*7c478bd9Sstevel@tonic-gate 				}
1284*7c478bd9Sstevel@tonic-gate 				if (c == '\n')
1285*7c478bd9Sstevel@tonic-gate 					i++;
1286*7c478bd9Sstevel@tonic-gate 			}
1287*7c478bd9Sstevel@tonic-gate 			if (fseeko(fout, 0L, SEEK_CUR) < 0) {
1288*7c478bd9Sstevel@tonic-gate 				perror(local);
1289*7c478bd9Sstevel@tonic-gate 				goto abort;
1290*7c478bd9Sstevel@tonic-gate 			}
1291*7c478bd9Sstevel@tonic-gate 		}
1292*7c478bd9Sstevel@tonic-gate 		fdio_setbuf(buf, FTPBUFSIZ);
1293*7c478bd9Sstevel@tonic-gate 		infd = fileno(din);
1294*7c478bd9Sstevel@tonic-gate 		while ((c = fdio_getc(infd)) != EOF) {
1295*7c478bd9Sstevel@tonic-gate 			while (c == '\r') {
1296*7c478bd9Sstevel@tonic-gate 				while (hash && (bytes >= hashbytes)) {
1297*7c478bd9Sstevel@tonic-gate 					(void) putchar('#');
1298*7c478bd9Sstevel@tonic-gate 					(void) fflush(stdout);
1299*7c478bd9Sstevel@tonic-gate 					hashbytes += HASHSIZ;
1300*7c478bd9Sstevel@tonic-gate 				}
1301*7c478bd9Sstevel@tonic-gate 				bytes++;
1302*7c478bd9Sstevel@tonic-gate 
1303*7c478bd9Sstevel@tonic-gate 				if ((c = fdio_getc(infd)) != '\n' || tcrflag) {
1304*7c478bd9Sstevel@tonic-gate 					if (ferror(fout))
1305*7c478bd9Sstevel@tonic-gate 						break;
1306*7c478bd9Sstevel@tonic-gate 					if (putc('\r', fout) == EOF)
1307*7c478bd9Sstevel@tonic-gate 						goto writer_ascii_err;
1308*7c478bd9Sstevel@tonic-gate 				}
1309*7c478bd9Sstevel@tonic-gate #ifdef notdef
1310*7c478bd9Sstevel@tonic-gate 				if (c == '\0') {
1311*7c478bd9Sstevel@tonic-gate 					bytes++;
1312*7c478bd9Sstevel@tonic-gate 					continue;
1313*7c478bd9Sstevel@tonic-gate 				}
1314*7c478bd9Sstevel@tonic-gate #endif
1315*7c478bd9Sstevel@tonic-gate 				if (c == EOF)
1316*7c478bd9Sstevel@tonic-gate 					goto endread;
1317*7c478bd9Sstevel@tonic-gate 			}
1318*7c478bd9Sstevel@tonic-gate 			if (putc(c, fout) == EOF)
1319*7c478bd9Sstevel@tonic-gate 				goto writer_ascii_err;
1320*7c478bd9Sstevel@tonic-gate 			bytes++;
1321*7c478bd9Sstevel@tonic-gate 		}
1322*7c478bd9Sstevel@tonic-gate endread:
1323*7c478bd9Sstevel@tonic-gate 		if (hash && bytes > 0) {
1324*7c478bd9Sstevel@tonic-gate 			if (bytes < hashbytes)
1325*7c478bd9Sstevel@tonic-gate 				(void) putchar('#');
1326*7c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
1327*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
1328*7c478bd9Sstevel@tonic-gate 		}
1329*7c478bd9Sstevel@tonic-gate 		if (fdio_error(infd)) {
1330*7c478bd9Sstevel@tonic-gate 			errflg = 1;
1331*7c478bd9Sstevel@tonic-gate 			perror("netin");
1332*7c478bd9Sstevel@tonic-gate 		}
1333*7c478bd9Sstevel@tonic-gate 		if ((fflush(fout) == EOF) || ferror(fout) ||
1334*7c478bd9Sstevel@tonic-gate 			(fsync(fileno(fout)) == -1)) {
1335*7c478bd9Sstevel@tonic-gate writer_ascii_err:
1336*7c478bd9Sstevel@tonic-gate 			errflg = 1;
1337*7c478bd9Sstevel@tonic-gate 			perror(local);
1338*7c478bd9Sstevel@tonic-gate 		}
1339*7c478bd9Sstevel@tonic-gate 		break;
1340*7c478bd9Sstevel@tonic-gate 	}
1341*7c478bd9Sstevel@tonic-gate 	reset_timer();
1342*7c478bd9Sstevel@tonic-gate 	if (closefunc != NULL)
1343*7c478bd9Sstevel@tonic-gate 		(*closefunc)(fout);
1344*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
1345*7c478bd9Sstevel@tonic-gate 	if (oldintp)
1346*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, oldintp);
1347*7c478bd9Sstevel@tonic-gate 	(void) fclose(din); data = -1;
1348*7c478bd9Sstevel@tonic-gate 	stop = gethrtime();
1349*7c478bd9Sstevel@tonic-gate 	(void) getreply(0);
1350*7c478bd9Sstevel@tonic-gate 	if (bytes > 0 && verbose && !errflg)
1351*7c478bd9Sstevel@tonic-gate 		ptransfer("received", bytes, start, stop, local, remote);
1352*7c478bd9Sstevel@tonic-gate 	if (!ctrl_in)
1353*7c478bd9Sstevel@tonic-gate 		(void) printf("Lost connection\n");
1354*7c478bd9Sstevel@tonic-gate 	if (oldtype) {
1355*7c478bd9Sstevel@tonic-gate 		if (!debug)
1356*7c478bd9Sstevel@tonic-gate 			verbose = 0;
1357*7c478bd9Sstevel@tonic-gate 		switch (oldtype) {
1358*7c478bd9Sstevel@tonic-gate 			case TYPE_I:
1359*7c478bd9Sstevel@tonic-gate 				setbinary(0, NULL);
1360*7c478bd9Sstevel@tonic-gate 				break;
1361*7c478bd9Sstevel@tonic-gate 			case TYPE_E:
1362*7c478bd9Sstevel@tonic-gate 				setebcdic(0, NULL);
1363*7c478bd9Sstevel@tonic-gate 				break;
1364*7c478bd9Sstevel@tonic-gate 			case TYPE_L:
1365*7c478bd9Sstevel@tonic-gate 				settenex(0, NULL);
1366*7c478bd9Sstevel@tonic-gate 				break;
1367*7c478bd9Sstevel@tonic-gate 		}
1368*7c478bd9Sstevel@tonic-gate 		verbose = oldverbose;
1369*7c478bd9Sstevel@tonic-gate 	}
1370*7c478bd9Sstevel@tonic-gate 	return;
1371*7c478bd9Sstevel@tonic-gate abort:
1372*7c478bd9Sstevel@tonic-gate 
1373*7c478bd9Sstevel@tonic-gate /* abort using RFC959 recommended IP, SYNC sequence  */
1374*7c478bd9Sstevel@tonic-gate 
1375*7c478bd9Sstevel@tonic-gate 	stop = gethrtime();
1376*7c478bd9Sstevel@tonic-gate 	if (oldintp)
1377*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, oldintp);
1378*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
1379*7c478bd9Sstevel@tonic-gate 	if (!cpend) {
1380*7c478bd9Sstevel@tonic-gate 		code = -1;
1381*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
1382*7c478bd9Sstevel@tonic-gate 		return;
1383*7c478bd9Sstevel@tonic-gate 	}
1384*7c478bd9Sstevel@tonic-gate 
1385*7c478bd9Sstevel@tonic-gate 	(void) fprintf(ctrl_out, "%c%c", IAC, IP);
1386*7c478bd9Sstevel@tonic-gate 	(void) fflush(ctrl_out);
1387*7c478bd9Sstevel@tonic-gate 	msg = (char)IAC;
1388*7c478bd9Sstevel@tonic-gate 	/*
1389*7c478bd9Sstevel@tonic-gate 	 * send IAC in urgent mode instead of DM because UNIX places oob
1390*7c478bd9Sstevel@tonic-gate 	 * mark after urgent byte rather than before as now is protocol
1391*7c478bd9Sstevel@tonic-gate 	 */
1392*7c478bd9Sstevel@tonic-gate 	if (send(fileno(ctrl_out), &msg, 1, MSG_OOB) != 1) {
1393*7c478bd9Sstevel@tonic-gate 		perror("abort");
1394*7c478bd9Sstevel@tonic-gate 	}
1395*7c478bd9Sstevel@tonic-gate 	(void) fprintf(ctrl_out, "%cABOR\r\n", DM);
1396*7c478bd9Sstevel@tonic-gate 	(void) fflush(ctrl_out);
1397*7c478bd9Sstevel@tonic-gate 	nfds = fileno(ctrl_in) + 1;
1398*7c478bd9Sstevel@tonic-gate 	FD_ZERO(&mask);
1399*7c478bd9Sstevel@tonic-gate 	FD_SET(fileno(ctrl_in), &mask);
1400*7c478bd9Sstevel@tonic-gate 	if (din) {
1401*7c478bd9Sstevel@tonic-gate 		FD_SET(fileno(din), &mask);
1402*7c478bd9Sstevel@tonic-gate 		nfds = MAX(fileno(din) + 1, nfds);
1403*7c478bd9Sstevel@tonic-gate 	}
1404*7c478bd9Sstevel@tonic-gate 	if ((nfnd = empty(&mask, 10, nfds)) <= 0) {
1405*7c478bd9Sstevel@tonic-gate 		if (nfnd < 0) {
1406*7c478bd9Sstevel@tonic-gate 			perror("abort");
1407*7c478bd9Sstevel@tonic-gate 		}
1408*7c478bd9Sstevel@tonic-gate 		code = -1;
1409*7c478bd9Sstevel@tonic-gate 		lostpeer(0);
1410*7c478bd9Sstevel@tonic-gate 	}
1411*7c478bd9Sstevel@tonic-gate 	if (din && FD_ISSET(fileno(din), &mask)) {
1412*7c478bd9Sstevel@tonic-gate 		do {
1413*7c478bd9Sstevel@tonic-gate 			reset_timer();
1414*7c478bd9Sstevel@tonic-gate 		} while ((c = read(fileno(din), buf, FTPBUFSIZ)) > 0);
1415*7c478bd9Sstevel@tonic-gate 	}
1416*7c478bd9Sstevel@tonic-gate 	if ((c = getreply(0)) == ERROR && code == 552) {
1417*7c478bd9Sstevel@tonic-gate 		/* needed for nic style abort */
1418*7c478bd9Sstevel@tonic-gate 		if (data >= 0) {
1419*7c478bd9Sstevel@tonic-gate 			(void) close(data);
1420*7c478bd9Sstevel@tonic-gate 			data = -1;
1421*7c478bd9Sstevel@tonic-gate 		}
1422*7c478bd9Sstevel@tonic-gate 		(void) getreply(0);
1423*7c478bd9Sstevel@tonic-gate 	}
1424*7c478bd9Sstevel@tonic-gate 	if (oldtype) {
1425*7c478bd9Sstevel@tonic-gate 		if (!debug)
1426*7c478bd9Sstevel@tonic-gate 			verbose = 0;
1427*7c478bd9Sstevel@tonic-gate 		switch (oldtype) {
1428*7c478bd9Sstevel@tonic-gate 		case TYPE_I:
1429*7c478bd9Sstevel@tonic-gate 			setbinary(0, NULL);
1430*7c478bd9Sstevel@tonic-gate 			break;
1431*7c478bd9Sstevel@tonic-gate 		case TYPE_E:
1432*7c478bd9Sstevel@tonic-gate 			setebcdic(0, NULL);
1433*7c478bd9Sstevel@tonic-gate 			break;
1434*7c478bd9Sstevel@tonic-gate 		case TYPE_L:
1435*7c478bd9Sstevel@tonic-gate 			settenex(0, NULL);
1436*7c478bd9Sstevel@tonic-gate 			break;
1437*7c478bd9Sstevel@tonic-gate 		}
1438*7c478bd9Sstevel@tonic-gate 		verbose = oldverbose;
1439*7c478bd9Sstevel@tonic-gate 	}
1440*7c478bd9Sstevel@tonic-gate 	(void) getreply(0);
1441*7c478bd9Sstevel@tonic-gate 	code = -1;
1442*7c478bd9Sstevel@tonic-gate 	if (data >= 0) {
1443*7c478bd9Sstevel@tonic-gate 		(void) close(data);
1444*7c478bd9Sstevel@tonic-gate 		data = -1;
1445*7c478bd9Sstevel@tonic-gate 	}
1446*7c478bd9Sstevel@tonic-gate 	if (closefunc != NULL && fout != NULL)
1447*7c478bd9Sstevel@tonic-gate 		(*closefunc)(fout);
1448*7c478bd9Sstevel@tonic-gate 	if (din) {
1449*7c478bd9Sstevel@tonic-gate 		(void) fclose(din);
1450*7c478bd9Sstevel@tonic-gate 		data = -1;
1451*7c478bd9Sstevel@tonic-gate 	}
1452*7c478bd9Sstevel@tonic-gate 	if (bytes > 0 && verbose)
1453*7c478bd9Sstevel@tonic-gate 		ptransfer("received", bytes, start, stop, local, remote);
1454*7c478bd9Sstevel@tonic-gate 	if (!ctrl_in)
1455*7c478bd9Sstevel@tonic-gate 		(void) printf("Lost connection\n");
1456*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
1457*7c478bd9Sstevel@tonic-gate }
1458*7c478bd9Sstevel@tonic-gate 
1459*7c478bd9Sstevel@tonic-gate /*
1460*7c478bd9Sstevel@tonic-gate  * Need to start a listen on the data channel
1461*7c478bd9Sstevel@tonic-gate  * before we send the command, otherwise the
1462*7c478bd9Sstevel@tonic-gate  * server's connect may fail.
1463*7c478bd9Sstevel@tonic-gate  */
1464*7c478bd9Sstevel@tonic-gate 
1465*7c478bd9Sstevel@tonic-gate static int
1466*7c478bd9Sstevel@tonic-gate initconn(void)
1467*7c478bd9Sstevel@tonic-gate {
1468*7c478bd9Sstevel@tonic-gate 	unsigned char *p, *a;
1469*7c478bd9Sstevel@tonic-gate 	int result, tmpno = 0;
1470*7c478bd9Sstevel@tonic-gate 	int on = 1;
1471*7c478bd9Sstevel@tonic-gate 	socklen_t len;
1472*7c478bd9Sstevel@tonic-gate 	int v4_addr;
1473*7c478bd9Sstevel@tonic-gate 	char *c, *c2, delm;
1474*7c478bd9Sstevel@tonic-gate 	in_port_t ports;
1475*7c478bd9Sstevel@tonic-gate 
1476*7c478bd9Sstevel@tonic-gate 	pasv_refused = B_FALSE;
1477*7c478bd9Sstevel@tonic-gate 	if (passivemode) {
1478*7c478bd9Sstevel@tonic-gate 		data = socket(AF_INET6, SOCK_STREAM, 0);
1479*7c478bd9Sstevel@tonic-gate 		if (data < 0) {
1480*7c478bd9Sstevel@tonic-gate 			perror("socket");
1481*7c478bd9Sstevel@tonic-gate 			return (1);
1482*7c478bd9Sstevel@tonic-gate 		}
1483*7c478bd9Sstevel@tonic-gate 		if (timeout && setsockopt(data, IPPROTO_TCP,
1484*7c478bd9Sstevel@tonic-gate 		    TCP_ABORT_THRESHOLD, (char *)&timeoutms,
1485*7c478bd9Sstevel@tonic-gate 		    sizeof (timeoutms)) < 0 && debug)
1486*7c478bd9Sstevel@tonic-gate 			perror("ftp: setsockopt (TCP_ABORT_THRESHOLD)");
1487*7c478bd9Sstevel@tonic-gate 		if ((options & SO_DEBUG) &&
1488*7c478bd9Sstevel@tonic-gate 		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1489*7c478bd9Sstevel@tonic-gate 			    sizeof (on)) < 0)
1490*7c478bd9Sstevel@tonic-gate 			perror("setsockopt (ignored)");
1491*7c478bd9Sstevel@tonic-gate 		/*
1492*7c478bd9Sstevel@tonic-gate 		 * Use the system wide default send and receive buffer sizes
1493*7c478bd9Sstevel@tonic-gate 		 * unless one has been specified.
1494*7c478bd9Sstevel@tonic-gate 		 */
1495*7c478bd9Sstevel@tonic-gate 		if (tcpwindowsize) {
1496*7c478bd9Sstevel@tonic-gate 			if (setsockopt(data, SOL_SOCKET, SO_SNDBUF,
1497*7c478bd9Sstevel@tonic-gate 			    (char *)&tcpwindowsize, sizeof (tcpwindowsize)) < 0)
1498*7c478bd9Sstevel@tonic-gate 				perror("ftp: setsockopt (SO_SNDBUF - ignored)");
1499*7c478bd9Sstevel@tonic-gate 			if (setsockopt(data, SOL_SOCKET, SO_RCVBUF,
1500*7c478bd9Sstevel@tonic-gate 			    (char *)&tcpwindowsize, sizeof (tcpwindowsize)) < 0)
1501*7c478bd9Sstevel@tonic-gate 				perror("ftp: setsockopt (SO_RCVBUF - ignored)");
1502*7c478bd9Sstevel@tonic-gate 		}
1503*7c478bd9Sstevel@tonic-gate 
1504*7c478bd9Sstevel@tonic-gate 		data_addr = remctladdr;
1505*7c478bd9Sstevel@tonic-gate 
1506*7c478bd9Sstevel@tonic-gate 		if (ipv6rem == B_TRUE) {
1507*7c478bd9Sstevel@tonic-gate 			if (command("EPSV") != COMPLETE) {
1508*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1509*7c478bd9Sstevel@tonic-gate 					"Passive mode refused. Try EPRT\n");
1510*7c478bd9Sstevel@tonic-gate 				pasv_refused = B_TRUE;
1511*7c478bd9Sstevel@tonic-gate 				goto noport;
1512*7c478bd9Sstevel@tonic-gate 			}
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate 			/*
1515*7c478bd9Sstevel@tonic-gate 			 * Get the data port from reply string from the
1516*7c478bd9Sstevel@tonic-gate 			 * server.  The format of the reply string is:
1517*7c478bd9Sstevel@tonic-gate 			 * 229 Entering Extended Passive Mode (|||port|)
1518*7c478bd9Sstevel@tonic-gate 			 * where | is the delimiter being used.
1519*7c478bd9Sstevel@tonic-gate 			 */
1520*7c478bd9Sstevel@tonic-gate 			c = strchr(reply_string, '(');
1521*7c478bd9Sstevel@tonic-gate 			c2 = strchr(reply_string, ')');
1522*7c478bd9Sstevel@tonic-gate 			if (c == NULL || c2 == NULL) {
1523*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "Extended passive mode"
1524*7c478bd9Sstevel@tonic-gate 				    "parsing failure.\n");
1525*7c478bd9Sstevel@tonic-gate 				goto bad;
1526*7c478bd9Sstevel@tonic-gate 			}
1527*7c478bd9Sstevel@tonic-gate 			*(c2 - 1) = NULL;
1528*7c478bd9Sstevel@tonic-gate 			/* Delimiter is the next char in the reply string */
1529*7c478bd9Sstevel@tonic-gate 			delm = *(++c);
1530*7c478bd9Sstevel@tonic-gate 			while (*c == delm) {
1531*7c478bd9Sstevel@tonic-gate 				if (!*(c++)) {
1532*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1533*7c478bd9Sstevel@tonic-gate 					    "Extended passive mode"
1534*7c478bd9Sstevel@tonic-gate 					    "parsing failure.\n");
1535*7c478bd9Sstevel@tonic-gate 					goto bad;
1536*7c478bd9Sstevel@tonic-gate 				}
1537*7c478bd9Sstevel@tonic-gate 			}
1538*7c478bd9Sstevel@tonic-gate 			/* assign the port for data connection */
1539*7c478bd9Sstevel@tonic-gate 			ports = (in_port_t)atoi(c);
1540*7c478bd9Sstevel@tonic-gate 			data_addr.sin6_port =  htons(ports);
1541*7c478bd9Sstevel@tonic-gate 		} else {
1542*7c478bd9Sstevel@tonic-gate 			int a1, a2, a3, a4, p1, p2;
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate 			if (command("PASV") != COMPLETE) {
1545*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1546*7c478bd9Sstevel@tonic-gate 					"Passive mode refused. Try PORT\n");
1547*7c478bd9Sstevel@tonic-gate 				pasv_refused = B_TRUE;
1548*7c478bd9Sstevel@tonic-gate 				goto noport;
1549*7c478bd9Sstevel@tonic-gate 			}
1550*7c478bd9Sstevel@tonic-gate 
1551*7c478bd9Sstevel@tonic-gate 			/*
1552*7c478bd9Sstevel@tonic-gate 			 * Get the data port from reply string from the
1553*7c478bd9Sstevel@tonic-gate 			 * server.  The format of the reply string is:
1554*7c478bd9Sstevel@tonic-gate 			 * 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)
1555*7c478bd9Sstevel@tonic-gate 			 */
1556*7c478bd9Sstevel@tonic-gate 			if (sscanf(pasv, "%d,%d,%d,%d,%d,%d",
1557*7c478bd9Sstevel@tonic-gate 					&a1, &a2, &a3, &a4, &p1, &p2) != 6) {
1558*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1559*7c478bd9Sstevel@tonic-gate 					"Passive mode parsing failure.\n");
1560*7c478bd9Sstevel@tonic-gate 				goto bad;
1561*7c478bd9Sstevel@tonic-gate 			}
1562*7c478bd9Sstevel@tonic-gate 			/*
1563*7c478bd9Sstevel@tonic-gate 			 * Set the supplied address and port in an
1564*7c478bd9Sstevel@tonic-gate 			 * IPv4-mapped IPv6 address.
1565*7c478bd9Sstevel@tonic-gate 			 */
1566*7c478bd9Sstevel@tonic-gate 			a = (unsigned char *)&data_addr.sin6_addr +
1567*7c478bd9Sstevel@tonic-gate 				sizeof (struct in6_addr) -
1568*7c478bd9Sstevel@tonic-gate 				sizeof (struct in_addr);
1569*7c478bd9Sstevel@tonic-gate #define	UC(b)	((b)&0xff)
1570*7c478bd9Sstevel@tonic-gate 			a[0] = UC(a1);
1571*7c478bd9Sstevel@tonic-gate 			a[1] = UC(a2);
1572*7c478bd9Sstevel@tonic-gate 			a[2] = UC(a3);
1573*7c478bd9Sstevel@tonic-gate 			a[3] = UC(a4);
1574*7c478bd9Sstevel@tonic-gate 			p = (unsigned char *)&data_addr.sin6_port;
1575*7c478bd9Sstevel@tonic-gate 			p[0] = UC(p1);
1576*7c478bd9Sstevel@tonic-gate 			p[1] = UC(p2);
1577*7c478bd9Sstevel@tonic-gate 		}
1578*7c478bd9Sstevel@tonic-gate 
1579*7c478bd9Sstevel@tonic-gate 		if (connect(data, (struct sockaddr *)&data_addr,
1580*7c478bd9Sstevel@tonic-gate 		    sizeof (data_addr)) < 0) {
1581*7c478bd9Sstevel@tonic-gate 			perror("connect");
1582*7c478bd9Sstevel@tonic-gate 			goto bad;
1583*7c478bd9Sstevel@tonic-gate 		}
1584*7c478bd9Sstevel@tonic-gate 		return (0);
1585*7c478bd9Sstevel@tonic-gate 	}
1586*7c478bd9Sstevel@tonic-gate 
1587*7c478bd9Sstevel@tonic-gate noport:
1588*7c478bd9Sstevel@tonic-gate 	data_addr = myctladdr;
1589*7c478bd9Sstevel@tonic-gate 	if (sendport)
1590*7c478bd9Sstevel@tonic-gate 		data_addr.sin6_port = 0;	/* let system pick one */
1591*7c478bd9Sstevel@tonic-gate 
1592*7c478bd9Sstevel@tonic-gate 	if (data != -1)
1593*7c478bd9Sstevel@tonic-gate 		(void) close(data);
1594*7c478bd9Sstevel@tonic-gate 	data = socket(AF_INET6, SOCK_STREAM, 0);
1595*7c478bd9Sstevel@tonic-gate 	if (data < 0) {
1596*7c478bd9Sstevel@tonic-gate 		perror("ftp: socket");
1597*7c478bd9Sstevel@tonic-gate 		if (tmpno)
1598*7c478bd9Sstevel@tonic-gate 			sendport = 1;
1599*7c478bd9Sstevel@tonic-gate 		return (1);
1600*7c478bd9Sstevel@tonic-gate 	}
1601*7c478bd9Sstevel@tonic-gate 	if (!sendport)
1602*7c478bd9Sstevel@tonic-gate 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR,
1603*7c478bd9Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
1604*7c478bd9Sstevel@tonic-gate 			perror("ftp: setsockopt (SO_REUSEADDR)");
1605*7c478bd9Sstevel@tonic-gate 			goto bad;
1606*7c478bd9Sstevel@tonic-gate 		}
1607*7c478bd9Sstevel@tonic-gate 	if (bind(data,
1608*7c478bd9Sstevel@tonic-gate 	    (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
1609*7c478bd9Sstevel@tonic-gate 		perror("ftp: bind");
1610*7c478bd9Sstevel@tonic-gate 		goto bad;
1611*7c478bd9Sstevel@tonic-gate 	}
1612*7c478bd9Sstevel@tonic-gate 	if (timeout && setsockopt(data, IPPROTO_TCP, TCP_ABORT_THRESHOLD,
1613*7c478bd9Sstevel@tonic-gate 	    (char *)&timeoutms, sizeof (timeoutms)) < 0 && debug)
1614*7c478bd9Sstevel@tonic-gate 		perror("ftp: setsockopt (TCP_ABORT_THRESHOLD)");
1615*7c478bd9Sstevel@tonic-gate 	if (options & SO_DEBUG &&
1616*7c478bd9Sstevel@tonic-gate 	    setsockopt(data, SOL_SOCKET, SO_DEBUG,
1617*7c478bd9Sstevel@tonic-gate 	    (char *)&on, sizeof (on)) < 0)
1618*7c478bd9Sstevel@tonic-gate 		perror("ftp: setsockopt (SO_DEBUG - ignored)");
1619*7c478bd9Sstevel@tonic-gate 	/*
1620*7c478bd9Sstevel@tonic-gate 	 * Use the system wide default send and receive buffer sizes unless
1621*7c478bd9Sstevel@tonic-gate 	 * one has been specified.
1622*7c478bd9Sstevel@tonic-gate 	 */
1623*7c478bd9Sstevel@tonic-gate 	if (tcpwindowsize) {
1624*7c478bd9Sstevel@tonic-gate 		if (setsockopt(data, SOL_SOCKET, SO_SNDBUF,
1625*7c478bd9Sstevel@tonic-gate 		    (char *)&tcpwindowsize, sizeof (tcpwindowsize)) < 0)
1626*7c478bd9Sstevel@tonic-gate 			perror("ftp: setsockopt (SO_SNDBUF - ignored)");
1627*7c478bd9Sstevel@tonic-gate 		if (setsockopt(data, SOL_SOCKET, SO_RCVBUF,
1628*7c478bd9Sstevel@tonic-gate 		    (char *)&tcpwindowsize, sizeof (tcpwindowsize)) < 0)
1629*7c478bd9Sstevel@tonic-gate 			perror("ftp: setsockopt (SO_RCVBUF - ignored)");
1630*7c478bd9Sstevel@tonic-gate 	}
1631*7c478bd9Sstevel@tonic-gate 	len = sizeof (data_addr);
1632*7c478bd9Sstevel@tonic-gate 	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1633*7c478bd9Sstevel@tonic-gate 		perror("ftp: getsockname");
1634*7c478bd9Sstevel@tonic-gate 		goto bad;
1635*7c478bd9Sstevel@tonic-gate 	}
1636*7c478bd9Sstevel@tonic-gate 
1637*7c478bd9Sstevel@tonic-gate 	v4_addr = IN6_IS_ADDR_V4MAPPED(&data_addr.sin6_addr);
1638*7c478bd9Sstevel@tonic-gate 	if (listen(data, 1) < 0)
1639*7c478bd9Sstevel@tonic-gate 		perror("ftp: listen");
1640*7c478bd9Sstevel@tonic-gate 
1641*7c478bd9Sstevel@tonic-gate 	if (sendport) {
1642*7c478bd9Sstevel@tonic-gate 		a = (unsigned char *)&data_addr.sin6_addr;
1643*7c478bd9Sstevel@tonic-gate 		p = (unsigned char *)&data_addr.sin6_port;
1644*7c478bd9Sstevel@tonic-gate 		if (v4_addr) {
1645*7c478bd9Sstevel@tonic-gate 			result =
1646*7c478bd9Sstevel@tonic-gate 			    command("PORT %d,%d,%d,%d,%d,%d",
1647*7c478bd9Sstevel@tonic-gate 			    UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
1648*7c478bd9Sstevel@tonic-gate 			    UC(p[0]), UC(p[1]));
1649*7c478bd9Sstevel@tonic-gate 		} else {
1650*7c478bd9Sstevel@tonic-gate 			char hname[INET6_ADDRSTRLEN];
1651*7c478bd9Sstevel@tonic-gate 
1652*7c478bd9Sstevel@tonic-gate 			result = COMPLETE + 1;
1653*7c478bd9Sstevel@tonic-gate 			/*
1654*7c478bd9Sstevel@tonic-gate 			 * if on previous try to server, it was
1655*7c478bd9Sstevel@tonic-gate 			 * determined that the server doesn't support
1656*7c478bd9Sstevel@tonic-gate 			 * EPRT, don't bother trying again.  Just try
1657*7c478bd9Sstevel@tonic-gate 			 * LPRT.
1658*7c478bd9Sstevel@tonic-gate 			 */
1659*7c478bd9Sstevel@tonic-gate 			if (eport_supported == B_TRUE) {
1660*7c478bd9Sstevel@tonic-gate 				if (inet_ntop(AF_INET6, &data_addr.sin6_addr,
1661*7c478bd9Sstevel@tonic-gate 				    hname, sizeof (hname)) != NULL) {
1662*7c478bd9Sstevel@tonic-gate 					result = command("EPRT |%d|%s|%d|", 2,
1663*7c478bd9Sstevel@tonic-gate 					    hname, htons(data_addr.sin6_port));
1664*7c478bd9Sstevel@tonic-gate 					if (result != COMPLETE)
1665*7c478bd9Sstevel@tonic-gate 						eport_supported = B_FALSE;
1666*7c478bd9Sstevel@tonic-gate 				    }
1667*7c478bd9Sstevel@tonic-gate 			}
1668*7c478bd9Sstevel@tonic-gate 			/* Try LPRT */
1669*7c478bd9Sstevel@tonic-gate 			if (result != COMPLETE) {
1670*7c478bd9Sstevel@tonic-gate 				result = command(
1671*7c478bd9Sstevel@tonic-gate "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1672*7c478bd9Sstevel@tonic-gate 6, 16,
1673*7c478bd9Sstevel@tonic-gate UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1674*7c478bd9Sstevel@tonic-gate UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
1675*7c478bd9Sstevel@tonic-gate UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
1676*7c478bd9Sstevel@tonic-gate UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
1677*7c478bd9Sstevel@tonic-gate 2, UC(p[0]), UC(p[1]));
1678*7c478bd9Sstevel@tonic-gate 			}
1679*7c478bd9Sstevel@tonic-gate 		}
1680*7c478bd9Sstevel@tonic-gate 
1681*7c478bd9Sstevel@tonic-gate 		if (result == ERROR && sendport == -1) {
1682*7c478bd9Sstevel@tonic-gate 			sendport = 0;
1683*7c478bd9Sstevel@tonic-gate 			tmpno = 1;
1684*7c478bd9Sstevel@tonic-gate 			goto noport;
1685*7c478bd9Sstevel@tonic-gate 		}
1686*7c478bd9Sstevel@tonic-gate 		return (result != COMPLETE);
1687*7c478bd9Sstevel@tonic-gate 	}
1688*7c478bd9Sstevel@tonic-gate 	if (tmpno)
1689*7c478bd9Sstevel@tonic-gate 		sendport = 1;
1690*7c478bd9Sstevel@tonic-gate 	return (0);
1691*7c478bd9Sstevel@tonic-gate bad:
1692*7c478bd9Sstevel@tonic-gate 	(void) close(data), data = -1;
1693*7c478bd9Sstevel@tonic-gate 	if (tmpno)
1694*7c478bd9Sstevel@tonic-gate 		sendport = 1;
1695*7c478bd9Sstevel@tonic-gate 	return (1);
1696*7c478bd9Sstevel@tonic-gate }
1697*7c478bd9Sstevel@tonic-gate 
1698*7c478bd9Sstevel@tonic-gate static FILE *
1699*7c478bd9Sstevel@tonic-gate dataconn(char *mode)
1700*7c478bd9Sstevel@tonic-gate {
1701*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 from;
1702*7c478bd9Sstevel@tonic-gate 	int s;
1703*7c478bd9Sstevel@tonic-gate 	socklen_t fromlen = sizeof (from);
1704*7c478bd9Sstevel@tonic-gate 
1705*7c478bd9Sstevel@tonic-gate 	reset_timer();
1706*7c478bd9Sstevel@tonic-gate 	if (passivemode && !pasv_refused)
1707*7c478bd9Sstevel@tonic-gate 		return (fdopen(data, mode));
1708*7c478bd9Sstevel@tonic-gate 
1709*7c478bd9Sstevel@tonic-gate 	s = accept(data, (struct sockaddr *)&from, &fromlen);
1710*7c478bd9Sstevel@tonic-gate 	if (s < 0) {
1711*7c478bd9Sstevel@tonic-gate 		perror("ftp: accept");
1712*7c478bd9Sstevel@tonic-gate 		(void) close(data), data = -1;
1713*7c478bd9Sstevel@tonic-gate 		return (NULL);
1714*7c478bd9Sstevel@tonic-gate 	}
1715*7c478bd9Sstevel@tonic-gate 	(void) close(data);
1716*7c478bd9Sstevel@tonic-gate 	data = s;
1717*7c478bd9Sstevel@tonic-gate 	return (fdopen(data, mode));
1718*7c478bd9Sstevel@tonic-gate }
1719*7c478bd9Sstevel@tonic-gate 
1720*7c478bd9Sstevel@tonic-gate static void
1721*7c478bd9Sstevel@tonic-gate ptransfer(char *direction, off_t bytes, hrtime_t t0,
1722*7c478bd9Sstevel@tonic-gate     hrtime_t t1, char *local, char *remote)
1723*7c478bd9Sstevel@tonic-gate {
1724*7c478bd9Sstevel@tonic-gate 	hrtime_t td; /* nanoseconds in a 64 bit int */
1725*7c478bd9Sstevel@tonic-gate 	double s, bs;
1726*7c478bd9Sstevel@tonic-gate 
1727*7c478bd9Sstevel@tonic-gate 	td = t1 - t0;
1728*7c478bd9Sstevel@tonic-gate 	s = (double)td / 1000000000.0; /* seconds */
1729*7c478bd9Sstevel@tonic-gate 	bs = (double)bytes / NONZERO(s);
1730*7c478bd9Sstevel@tonic-gate 	if (local && *local != '-')
1731*7c478bd9Sstevel@tonic-gate 		(void) printf("local: %s ", local);
1732*7c478bd9Sstevel@tonic-gate 	if (remote)
1733*7c478bd9Sstevel@tonic-gate 		(void) printf("remote: %s\n", remote);
1734*7c478bd9Sstevel@tonic-gate 	(void) printf("%lld bytes %s in %.2g seconds (%.2f Kbytes/s)\n",
1735*7c478bd9Sstevel@tonic-gate 		(longlong_t)bytes, direction, s, bs / 1024.0);
1736*7c478bd9Sstevel@tonic-gate }
1737*7c478bd9Sstevel@tonic-gate 
1738*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1739*7c478bd9Sstevel@tonic-gate static void
1740*7c478bd9Sstevel@tonic-gate psabort(int sig)
1741*7c478bd9Sstevel@tonic-gate {
1742*7c478bd9Sstevel@tonic-gate 	abrtflag++;
1743*7c478bd9Sstevel@tonic-gate }
1744*7c478bd9Sstevel@tonic-gate 
1745*7c478bd9Sstevel@tonic-gate void
1746*7c478bd9Sstevel@tonic-gate pswitch(int flag)
1747*7c478bd9Sstevel@tonic-gate {
1748*7c478bd9Sstevel@tonic-gate 	void (*oldintr)();
1749*7c478bd9Sstevel@tonic-gate 	static struct comvars {
1750*7c478bd9Sstevel@tonic-gate 		int connect;
1751*7c478bd9Sstevel@tonic-gate 		char name[MAXHOSTNAMELEN];
1752*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 mctl;
1753*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 hctl;
1754*7c478bd9Sstevel@tonic-gate 		FILE *in;
1755*7c478bd9Sstevel@tonic-gate 		FILE *out;
1756*7c478bd9Sstevel@tonic-gate 		int tpe;
1757*7c478bd9Sstevel@tonic-gate 		int cpnd;
1758*7c478bd9Sstevel@tonic-gate 		int sunqe;
1759*7c478bd9Sstevel@tonic-gate 		int runqe;
1760*7c478bd9Sstevel@tonic-gate 		int mcse;
1761*7c478bd9Sstevel@tonic-gate 		int ntflg;
1762*7c478bd9Sstevel@tonic-gate 		char nti[17];
1763*7c478bd9Sstevel@tonic-gate 		char nto[17];
1764*7c478bd9Sstevel@tonic-gate 		int mapflg;
1765*7c478bd9Sstevel@tonic-gate 		char mi[MAXPATHLEN];
1766*7c478bd9Sstevel@tonic-gate 		char mo[MAXPATHLEN];
1767*7c478bd9Sstevel@tonic-gate 		int authtype;
1768*7c478bd9Sstevel@tonic-gate 		int clvl;
1769*7c478bd9Sstevel@tonic-gate 		int dlvl;
1770*7c478bd9Sstevel@tonic-gate 		} proxstruct, tmpstruct;
1771*7c478bd9Sstevel@tonic-gate 	struct comvars *ip, *op;
1772*7c478bd9Sstevel@tonic-gate 
1773*7c478bd9Sstevel@tonic-gate 	abrtflag = 0;
1774*7c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, psabort);
1775*7c478bd9Sstevel@tonic-gate 	if (flag) {
1776*7c478bd9Sstevel@tonic-gate 		if (proxy)
1777*7c478bd9Sstevel@tonic-gate 			return;
1778*7c478bd9Sstevel@tonic-gate 		ip = &tmpstruct;
1779*7c478bd9Sstevel@tonic-gate 		op = &proxstruct;
1780*7c478bd9Sstevel@tonic-gate 		proxy++;
1781*7c478bd9Sstevel@tonic-gate 	} else {
1782*7c478bd9Sstevel@tonic-gate 		if (!proxy)
1783*7c478bd9Sstevel@tonic-gate 			return;
1784*7c478bd9Sstevel@tonic-gate 		ip = &proxstruct;
1785*7c478bd9Sstevel@tonic-gate 		op = &tmpstruct;
1786*7c478bd9Sstevel@tonic-gate 		proxy = 0;
1787*7c478bd9Sstevel@tonic-gate 	}
1788*7c478bd9Sstevel@tonic-gate 	ip->connect = connected;
1789*7c478bd9Sstevel@tonic-gate 	connected = op->connect;
1790*7c478bd9Sstevel@tonic-gate 	if (hostname)
1791*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(ip->name, hostname, sizeof (ip->name));
1792*7c478bd9Sstevel@tonic-gate 	else
1793*7c478bd9Sstevel@tonic-gate 		ip->name[0] = 0;
1794*7c478bd9Sstevel@tonic-gate 	hostname = op->name;
1795*7c478bd9Sstevel@tonic-gate 	ip->hctl = remctladdr;
1796*7c478bd9Sstevel@tonic-gate 	remctladdr = op->hctl;
1797*7c478bd9Sstevel@tonic-gate 	ip->mctl = myctladdr;
1798*7c478bd9Sstevel@tonic-gate 	myctladdr = op->mctl;
1799*7c478bd9Sstevel@tonic-gate 	ip->in = ctrl_in;
1800*7c478bd9Sstevel@tonic-gate 	ctrl_in = op->in;
1801*7c478bd9Sstevel@tonic-gate 	ip->out = ctrl_out;
1802*7c478bd9Sstevel@tonic-gate 	ctrl_out = op->out;
1803*7c478bd9Sstevel@tonic-gate 	ip->tpe = type;
1804*7c478bd9Sstevel@tonic-gate 	type = op->tpe;
1805*7c478bd9Sstevel@tonic-gate 	if (!type)
1806*7c478bd9Sstevel@tonic-gate 		type = 1;
1807*7c478bd9Sstevel@tonic-gate 	ip->cpnd = cpend;
1808*7c478bd9Sstevel@tonic-gate 	cpend = op->cpnd;
1809*7c478bd9Sstevel@tonic-gate 	ip->sunqe = sunique;
1810*7c478bd9Sstevel@tonic-gate 	sunique = op->sunqe;
1811*7c478bd9Sstevel@tonic-gate 	ip->runqe = runique;
1812*7c478bd9Sstevel@tonic-gate 	runique = op->runqe;
1813*7c478bd9Sstevel@tonic-gate 	ip->mcse = mcase;
1814*7c478bd9Sstevel@tonic-gate 	mcase = op->mcse;
1815*7c478bd9Sstevel@tonic-gate 	ip->ntflg = ntflag;
1816*7c478bd9Sstevel@tonic-gate 	ntflag = op->ntflg;
1817*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(ip->nti, ntin, sizeof (ip->nti));
1818*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(ntin, op->nti, sizeof (ntin));
1819*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(ip->nto, ntout, sizeof (ip->nto));
1820*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(ntout, op->nto, sizeof (ntout));
1821*7c478bd9Sstevel@tonic-gate 	ip->mapflg = mapflag;
1822*7c478bd9Sstevel@tonic-gate 	mapflag = op->mapflg;
1823*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(ip->mi, mapin, sizeof (ip->mi));
1824*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(mapin, op->mi, sizeof (mapin));
1825*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(ip->mo, mapout, sizeof (ip->mo));
1826*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(mapout, op->mo, sizeof (mapout));
1827*7c478bd9Sstevel@tonic-gate 
1828*7c478bd9Sstevel@tonic-gate 	ip->authtype = auth_type;
1829*7c478bd9Sstevel@tonic-gate 	auth_type = op->authtype;
1830*7c478bd9Sstevel@tonic-gate 	ip->clvl = clevel;
1831*7c478bd9Sstevel@tonic-gate 	clevel = op->clvl;
1832*7c478bd9Sstevel@tonic-gate 	ip->dlvl = dlevel;
1833*7c478bd9Sstevel@tonic-gate 	dlevel = op->dlvl;
1834*7c478bd9Sstevel@tonic-gate 	if (!clevel)
1835*7c478bd9Sstevel@tonic-gate 		clevel = PROT_C;
1836*7c478bd9Sstevel@tonic-gate 	if (!dlevel)
1837*7c478bd9Sstevel@tonic-gate 		dlevel = PROT_C;
1838*7c478bd9Sstevel@tonic-gate 
1839*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
1840*7c478bd9Sstevel@tonic-gate 	if (abrtflag) {
1841*7c478bd9Sstevel@tonic-gate 		abrtflag = 0;
1842*7c478bd9Sstevel@tonic-gate 		(*oldintr)();
1843*7c478bd9Sstevel@tonic-gate 	}
1844*7c478bd9Sstevel@tonic-gate }
1845*7c478bd9Sstevel@tonic-gate 
1846*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1847*7c478bd9Sstevel@tonic-gate static void
1848*7c478bd9Sstevel@tonic-gate abortpt(int sig)
1849*7c478bd9Sstevel@tonic-gate {
1850*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
1851*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
1852*7c478bd9Sstevel@tonic-gate 	ptabflg++;
1853*7c478bd9Sstevel@tonic-gate 	mflag = 0;
1854*7c478bd9Sstevel@tonic-gate 	abrtflag = 0;
1855*7c478bd9Sstevel@tonic-gate 	longjmp(ptabort, 1);
1856*7c478bd9Sstevel@tonic-gate }
1857*7c478bd9Sstevel@tonic-gate 
1858*7c478bd9Sstevel@tonic-gate static void
1859*7c478bd9Sstevel@tonic-gate proxtrans(char *cmd, char *local, char *remote)
1860*7c478bd9Sstevel@tonic-gate {
1861*7c478bd9Sstevel@tonic-gate 	void (*oldintr)();
1862*7c478bd9Sstevel@tonic-gate 	int tmptype, oldtype = 0, secndflag = 0, nfnd;
1863*7c478bd9Sstevel@tonic-gate 	extern jmp_buf ptabort;
1864*7c478bd9Sstevel@tonic-gate 	char *cmd2;
1865*7c478bd9Sstevel@tonic-gate 	struct fd_set mask;
1866*7c478bd9Sstevel@tonic-gate 	int ipv4_addr = IN6_IS_ADDR_V4MAPPED(&remctladdr.sin6_addr);
1867*7c478bd9Sstevel@tonic-gate 
1868*7c478bd9Sstevel@tonic-gate 	if (strcmp(cmd, "RETR"))
1869*7c478bd9Sstevel@tonic-gate 		cmd2 = "RETR";
1870*7c478bd9Sstevel@tonic-gate 	else
1871*7c478bd9Sstevel@tonic-gate 		cmd2 = runique ? "STOU" : "STOR";
1872*7c478bd9Sstevel@tonic-gate 	if (command(ipv4_addr ? "PASV" : "EPSV") != COMPLETE) {
1873*7c478bd9Sstevel@tonic-gate 		(void) printf(
1874*7c478bd9Sstevel@tonic-gate 		    "proxy server does not support third part transfers.\n");
1875*7c478bd9Sstevel@tonic-gate 		return;
1876*7c478bd9Sstevel@tonic-gate 	}
1877*7c478bd9Sstevel@tonic-gate 	tmptype = type;
1878*7c478bd9Sstevel@tonic-gate 	pswitch(0);
1879*7c478bd9Sstevel@tonic-gate 	if (!connected) {
1880*7c478bd9Sstevel@tonic-gate 		(void) printf("No primary connection\n");
1881*7c478bd9Sstevel@tonic-gate 		pswitch(1);
1882*7c478bd9Sstevel@tonic-gate 		code = -1;
1883*7c478bd9Sstevel@tonic-gate 		return;
1884*7c478bd9Sstevel@tonic-gate 	}
1885*7c478bd9Sstevel@tonic-gate 	if (type != tmptype) {
1886*7c478bd9Sstevel@tonic-gate 		oldtype = type;
1887*7c478bd9Sstevel@tonic-gate 		switch (tmptype) {
1888*7c478bd9Sstevel@tonic-gate 			case TYPE_A:
1889*7c478bd9Sstevel@tonic-gate 				setascii(0, NULL);
1890*7c478bd9Sstevel@tonic-gate 				break;
1891*7c478bd9Sstevel@tonic-gate 			case TYPE_I:
1892*7c478bd9Sstevel@tonic-gate 				setbinary(0, NULL);
1893*7c478bd9Sstevel@tonic-gate 				break;
1894*7c478bd9Sstevel@tonic-gate 			case TYPE_E:
1895*7c478bd9Sstevel@tonic-gate 				setebcdic(0, NULL);
1896*7c478bd9Sstevel@tonic-gate 				break;
1897*7c478bd9Sstevel@tonic-gate 			case TYPE_L:
1898*7c478bd9Sstevel@tonic-gate 				settenex(0, NULL);
1899*7c478bd9Sstevel@tonic-gate 				break;
1900*7c478bd9Sstevel@tonic-gate 		}
1901*7c478bd9Sstevel@tonic-gate 	}
1902*7c478bd9Sstevel@tonic-gate 	if (command(ipv4_addr ? "PORT %s" : "EPRT %s", pasv) != COMPLETE) {
1903*7c478bd9Sstevel@tonic-gate 		switch (oldtype) {
1904*7c478bd9Sstevel@tonic-gate 			case 0:
1905*7c478bd9Sstevel@tonic-gate 				break;
1906*7c478bd9Sstevel@tonic-gate 			case TYPE_A:
1907*7c478bd9Sstevel@tonic-gate 				setascii(0, NULL);
1908*7c478bd9Sstevel@tonic-gate 				break;
1909*7c478bd9Sstevel@tonic-gate 			case TYPE_I:
1910*7c478bd9Sstevel@tonic-gate 				setbinary(0, NULL);
1911*7c478bd9Sstevel@tonic-gate 				break;
1912*7c478bd9Sstevel@tonic-gate 			case TYPE_E:
1913*7c478bd9Sstevel@tonic-gate 				setebcdic(0, NULL);
1914*7c478bd9Sstevel@tonic-gate 				break;
1915*7c478bd9Sstevel@tonic-gate 			case TYPE_L:
1916*7c478bd9Sstevel@tonic-gate 				settenex(0, NULL);
1917*7c478bd9Sstevel@tonic-gate 				break;
1918*7c478bd9Sstevel@tonic-gate 		}
1919*7c478bd9Sstevel@tonic-gate 		pswitch(1);
1920*7c478bd9Sstevel@tonic-gate 		return;
1921*7c478bd9Sstevel@tonic-gate 	}
1922*7c478bd9Sstevel@tonic-gate 	if (setjmp(ptabort))
1923*7c478bd9Sstevel@tonic-gate 		goto abort;
1924*7c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, (void (*)())abortpt);
1925*7c478bd9Sstevel@tonic-gate 	if (command("%s %s", cmd, remote) != PRELIM) {
1926*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
1927*7c478bd9Sstevel@tonic-gate 		switch (oldtype) {
1928*7c478bd9Sstevel@tonic-gate 			case 0:
1929*7c478bd9Sstevel@tonic-gate 				break;
1930*7c478bd9Sstevel@tonic-gate 			case TYPE_A:
1931*7c478bd9Sstevel@tonic-gate 				setascii(0, NULL);
1932*7c478bd9Sstevel@tonic-gate 				break;
1933*7c478bd9Sstevel@tonic-gate 			case TYPE_I:
1934*7c478bd9Sstevel@tonic-gate 				setbinary(0, NULL);
1935*7c478bd9Sstevel@tonic-gate 				break;
1936*7c478bd9Sstevel@tonic-gate 			case TYPE_E:
1937*7c478bd9Sstevel@tonic-gate 				setebcdic(0, NULL);
1938*7c478bd9Sstevel@tonic-gate 				break;
1939*7c478bd9Sstevel@tonic-gate 			case TYPE_L:
1940*7c478bd9Sstevel@tonic-gate 				settenex(0, NULL);
1941*7c478bd9Sstevel@tonic-gate 				break;
1942*7c478bd9Sstevel@tonic-gate 		}
1943*7c478bd9Sstevel@tonic-gate 		pswitch(1);
1944*7c478bd9Sstevel@tonic-gate 		return;
1945*7c478bd9Sstevel@tonic-gate 	}
1946*7c478bd9Sstevel@tonic-gate 	(void) sleep(2);
1947*7c478bd9Sstevel@tonic-gate 	pswitch(1);
1948*7c478bd9Sstevel@tonic-gate 	secndflag++;
1949*7c478bd9Sstevel@tonic-gate 	if (command("%s %s", cmd2, local) != PRELIM)
1950*7c478bd9Sstevel@tonic-gate 		goto abort;
1951*7c478bd9Sstevel@tonic-gate 	ptflag++;
1952*7c478bd9Sstevel@tonic-gate 	(void) getreply(0);
1953*7c478bd9Sstevel@tonic-gate 	pswitch(0);
1954*7c478bd9Sstevel@tonic-gate 	(void) getreply(0);
1955*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
1956*7c478bd9Sstevel@tonic-gate 	switch (oldtype) {
1957*7c478bd9Sstevel@tonic-gate 		case 0:
1958*7c478bd9Sstevel@tonic-gate 			break;
1959*7c478bd9Sstevel@tonic-gate 		case TYPE_A:
1960*7c478bd9Sstevel@tonic-gate 			setascii(0, NULL);
1961*7c478bd9Sstevel@tonic-gate 			break;
1962*7c478bd9Sstevel@tonic-gate 		case TYPE_I:
1963*7c478bd9Sstevel@tonic-gate 			setbinary(0, NULL);
1964*7c478bd9Sstevel@tonic-gate 			break;
1965*7c478bd9Sstevel@tonic-gate 		case TYPE_E:
1966*7c478bd9Sstevel@tonic-gate 			setebcdic(0, NULL);
1967*7c478bd9Sstevel@tonic-gate 			break;
1968*7c478bd9Sstevel@tonic-gate 		case TYPE_L:
1969*7c478bd9Sstevel@tonic-gate 			settenex(0, NULL);
1970*7c478bd9Sstevel@tonic-gate 			break;
1971*7c478bd9Sstevel@tonic-gate 	}
1972*7c478bd9Sstevel@tonic-gate 	pswitch(1);
1973*7c478bd9Sstevel@tonic-gate 	ptflag = 0;
1974*7c478bd9Sstevel@tonic-gate 	(void) printf("local: %s remote: %s\n", local, remote);
1975*7c478bd9Sstevel@tonic-gate 	return;
1976*7c478bd9Sstevel@tonic-gate abort:
1977*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
1978*7c478bd9Sstevel@tonic-gate 	ptflag = 0;
1979*7c478bd9Sstevel@tonic-gate 	if (strcmp(cmd, "RETR") && !proxy)
1980*7c478bd9Sstevel@tonic-gate 		pswitch(1);
1981*7c478bd9Sstevel@tonic-gate 	else if ((strcmp(cmd, "RETR") == 0) && proxy)
1982*7c478bd9Sstevel@tonic-gate 		pswitch(0);
1983*7c478bd9Sstevel@tonic-gate 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
1984*7c478bd9Sstevel@tonic-gate 		if (command("%s %s", cmd2, local) != PRELIM) {
1985*7c478bd9Sstevel@tonic-gate 			pswitch(0);
1986*7c478bd9Sstevel@tonic-gate 			switch (oldtype) {
1987*7c478bd9Sstevel@tonic-gate 				case 0:
1988*7c478bd9Sstevel@tonic-gate 					break;
1989*7c478bd9Sstevel@tonic-gate 				case TYPE_A:
1990*7c478bd9Sstevel@tonic-gate 					setascii(0, NULL);
1991*7c478bd9Sstevel@tonic-gate 					break;
1992*7c478bd9Sstevel@tonic-gate 				case TYPE_I:
1993*7c478bd9Sstevel@tonic-gate 					setbinary(0, NULL);
1994*7c478bd9Sstevel@tonic-gate 					break;
1995*7c478bd9Sstevel@tonic-gate 				case TYPE_E:
1996*7c478bd9Sstevel@tonic-gate 					setebcdic(0, NULL);
1997*7c478bd9Sstevel@tonic-gate 					break;
1998*7c478bd9Sstevel@tonic-gate 				case TYPE_L:
1999*7c478bd9Sstevel@tonic-gate 					settenex(0, NULL);
2000*7c478bd9Sstevel@tonic-gate 					break;
2001*7c478bd9Sstevel@tonic-gate 			}
2002*7c478bd9Sstevel@tonic-gate 			if (cpend) {
2003*7c478bd9Sstevel@tonic-gate 				char msg[2];
2004*7c478bd9Sstevel@tonic-gate 
2005*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ctrl_out, "%c%c", IAC, IP);
2006*7c478bd9Sstevel@tonic-gate 				(void) fflush(ctrl_out);
2007*7c478bd9Sstevel@tonic-gate 				*msg = (char)IAC;
2008*7c478bd9Sstevel@tonic-gate 				*(msg+1) = (char)DM;
2009*7c478bd9Sstevel@tonic-gate 				if (send(fileno(ctrl_out), msg, 2, MSG_OOB)
2010*7c478bd9Sstevel@tonic-gate 				    != 2)
2011*7c478bd9Sstevel@tonic-gate 					perror("abort");
2012*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ctrl_out, "ABOR\r\n");
2013*7c478bd9Sstevel@tonic-gate 				(void) fflush(ctrl_out);
2014*7c478bd9Sstevel@tonic-gate 				FD_ZERO(&mask);
2015*7c478bd9Sstevel@tonic-gate 				FD_SET(fileno(ctrl_in), &mask);
2016*7c478bd9Sstevel@tonic-gate 				if ((nfnd = empty(&mask, 10,
2017*7c478bd9Sstevel@tonic-gate 				    fileno(ctrl_in) + 1)) <= 0) {
2018*7c478bd9Sstevel@tonic-gate 					if (nfnd < 0) {
2019*7c478bd9Sstevel@tonic-gate 						perror("abort");
2020*7c478bd9Sstevel@tonic-gate 					}
2021*7c478bd9Sstevel@tonic-gate 					if (ptabflg)
2022*7c478bd9Sstevel@tonic-gate 						code = -1;
2023*7c478bd9Sstevel@tonic-gate 					lostpeer(0);
2024*7c478bd9Sstevel@tonic-gate 				}
2025*7c478bd9Sstevel@tonic-gate 				(void) getreply(0);
2026*7c478bd9Sstevel@tonic-gate 				(void) getreply(0);
2027*7c478bd9Sstevel@tonic-gate 			}
2028*7c478bd9Sstevel@tonic-gate 		}
2029*7c478bd9Sstevel@tonic-gate 		pswitch(1);
2030*7c478bd9Sstevel@tonic-gate 		if (ptabflg)
2031*7c478bd9Sstevel@tonic-gate 			code = -1;
2032*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldintr);
2033*7c478bd9Sstevel@tonic-gate 		return;
2034*7c478bd9Sstevel@tonic-gate 	}
2035*7c478bd9Sstevel@tonic-gate 	if (cpend) {
2036*7c478bd9Sstevel@tonic-gate 		char msg[2];
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate 		(void) fprintf(ctrl_out, "%c%c", IAC, IP);
2039*7c478bd9Sstevel@tonic-gate 		(void) fflush(ctrl_out);
2040*7c478bd9Sstevel@tonic-gate 		*msg = (char)IAC;
2041*7c478bd9Sstevel@tonic-gate 		*(msg+1) = (char)DM;
2042*7c478bd9Sstevel@tonic-gate 		if (send(fileno(ctrl_out), msg, 2, MSG_OOB) != 2)
2043*7c478bd9Sstevel@tonic-gate 			perror("abort");
2044*7c478bd9Sstevel@tonic-gate 		(void) fprintf(ctrl_out, "ABOR\r\n");
2045*7c478bd9Sstevel@tonic-gate 		(void) fflush(ctrl_out);
2046*7c478bd9Sstevel@tonic-gate 		FD_ZERO(&mask);
2047*7c478bd9Sstevel@tonic-gate 		FD_SET(fileno(ctrl_in), &mask);
2048*7c478bd9Sstevel@tonic-gate 		if ((nfnd = empty(&mask, 10, fileno(ctrl_in) + 1)) <= 0) {
2049*7c478bd9Sstevel@tonic-gate 			if (nfnd < 0) {
2050*7c478bd9Sstevel@tonic-gate 				perror("abort");
2051*7c478bd9Sstevel@tonic-gate 			}
2052*7c478bd9Sstevel@tonic-gate 			if (ptabflg)
2053*7c478bd9Sstevel@tonic-gate 				code = -1;
2054*7c478bd9Sstevel@tonic-gate 			lostpeer(0);
2055*7c478bd9Sstevel@tonic-gate 		}
2056*7c478bd9Sstevel@tonic-gate 		(void) getreply(0);
2057*7c478bd9Sstevel@tonic-gate 		(void) getreply(0);
2058*7c478bd9Sstevel@tonic-gate 	}
2059*7c478bd9Sstevel@tonic-gate 	pswitch(!proxy);
2060*7c478bd9Sstevel@tonic-gate 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
2061*7c478bd9Sstevel@tonic-gate 		if (command("%s %s", cmd2, local) != PRELIM) {
2062*7c478bd9Sstevel@tonic-gate 			pswitch(0);
2063*7c478bd9Sstevel@tonic-gate 			switch (oldtype) {
2064*7c478bd9Sstevel@tonic-gate 				case 0:
2065*7c478bd9Sstevel@tonic-gate 					break;
2066*7c478bd9Sstevel@tonic-gate 				case TYPE_A:
2067*7c478bd9Sstevel@tonic-gate 					setascii(0, NULL);
2068*7c478bd9Sstevel@tonic-gate 					break;
2069*7c478bd9Sstevel@tonic-gate 				case TYPE_I:
2070*7c478bd9Sstevel@tonic-gate 					setbinary(0, NULL);
2071*7c478bd9Sstevel@tonic-gate 					break;
2072*7c478bd9Sstevel@tonic-gate 				case TYPE_E:
2073*7c478bd9Sstevel@tonic-gate 					setebcdic(0, NULL);
2074*7c478bd9Sstevel@tonic-gate 					break;
2075*7c478bd9Sstevel@tonic-gate 				case TYPE_L:
2076*7c478bd9Sstevel@tonic-gate 					settenex(0, NULL);
2077*7c478bd9Sstevel@tonic-gate 					break;
2078*7c478bd9Sstevel@tonic-gate 			}
2079*7c478bd9Sstevel@tonic-gate 			if (cpend) {
2080*7c478bd9Sstevel@tonic-gate 				char msg[2];
2081*7c478bd9Sstevel@tonic-gate 
2082*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ctrl_out, "%c%c", IAC, IP);
2083*7c478bd9Sstevel@tonic-gate 				(void) fflush(ctrl_out);
2084*7c478bd9Sstevel@tonic-gate 				*msg = (char)IAC;
2085*7c478bd9Sstevel@tonic-gate 				*(msg+1) = (char)DM;
2086*7c478bd9Sstevel@tonic-gate 				if (send(fileno(ctrl_out), msg, 2, MSG_OOB)
2087*7c478bd9Sstevel@tonic-gate 				    != 2)
2088*7c478bd9Sstevel@tonic-gate 					perror("abort");
2089*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ctrl_out, "ABOR\r\n");
2090*7c478bd9Sstevel@tonic-gate 				(void) fflush(ctrl_out);
2091*7c478bd9Sstevel@tonic-gate 				FD_ZERO(&mask);
2092*7c478bd9Sstevel@tonic-gate 				FD_SET(fileno(ctrl_in), &mask);
2093*7c478bd9Sstevel@tonic-gate 				if ((nfnd = empty(&mask, 10,
2094*7c478bd9Sstevel@tonic-gate 				    fileno(ctrl_in) + 1)) <= 0) {
2095*7c478bd9Sstevel@tonic-gate 					if (nfnd < 0) {
2096*7c478bd9Sstevel@tonic-gate 						perror("abort");
2097*7c478bd9Sstevel@tonic-gate 					}
2098*7c478bd9Sstevel@tonic-gate 					if (ptabflg)
2099*7c478bd9Sstevel@tonic-gate 						code = -1;
2100*7c478bd9Sstevel@tonic-gate 					lostpeer(0);
2101*7c478bd9Sstevel@tonic-gate 				}
2102*7c478bd9Sstevel@tonic-gate 				(void) getreply(0);
2103*7c478bd9Sstevel@tonic-gate 				(void) getreply(0);
2104*7c478bd9Sstevel@tonic-gate 			}
2105*7c478bd9Sstevel@tonic-gate 			pswitch(1);
2106*7c478bd9Sstevel@tonic-gate 			if (ptabflg)
2107*7c478bd9Sstevel@tonic-gate 				code = -1;
2108*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, oldintr);
2109*7c478bd9Sstevel@tonic-gate 			return;
2110*7c478bd9Sstevel@tonic-gate 		}
2111*7c478bd9Sstevel@tonic-gate 	}
2112*7c478bd9Sstevel@tonic-gate 	if (cpend) {
2113*7c478bd9Sstevel@tonic-gate 		char msg[2];
2114*7c478bd9Sstevel@tonic-gate 
2115*7c478bd9Sstevel@tonic-gate 		(void) fprintf(ctrl_out, "%c%c", IAC, IP);
2116*7c478bd9Sstevel@tonic-gate 		(void) fflush(ctrl_out);
2117*7c478bd9Sstevel@tonic-gate 		*msg = (char)IAC;
2118*7c478bd9Sstevel@tonic-gate 		*(msg+1) = (char)DM;
2119*7c478bd9Sstevel@tonic-gate 		if (send(fileno(ctrl_out), msg, 2, MSG_OOB) != 2)
2120*7c478bd9Sstevel@tonic-gate 			perror("abort");
2121*7c478bd9Sstevel@tonic-gate 		(void) fprintf(ctrl_out, "ABOR\r\n");
2122*7c478bd9Sstevel@tonic-gate 		(void) fflush(ctrl_out);
2123*7c478bd9Sstevel@tonic-gate 		FD_ZERO(&mask);
2124*7c478bd9Sstevel@tonic-gate 		FD_SET(fileno(ctrl_in), &mask);
2125*7c478bd9Sstevel@tonic-gate 		if ((nfnd = empty(&mask, 10, fileno(ctrl_in) + 1)) <= 0) {
2126*7c478bd9Sstevel@tonic-gate 			if (nfnd < 0) {
2127*7c478bd9Sstevel@tonic-gate 				perror("abort");
2128*7c478bd9Sstevel@tonic-gate 			}
2129*7c478bd9Sstevel@tonic-gate 			if (ptabflg)
2130*7c478bd9Sstevel@tonic-gate 				code = -1;
2131*7c478bd9Sstevel@tonic-gate 			lostpeer(0);
2132*7c478bd9Sstevel@tonic-gate 		}
2133*7c478bd9Sstevel@tonic-gate 		(void) getreply(0);
2134*7c478bd9Sstevel@tonic-gate 		(void) getreply(0);
2135*7c478bd9Sstevel@tonic-gate 	}
2136*7c478bd9Sstevel@tonic-gate 	pswitch(!proxy);
2137*7c478bd9Sstevel@tonic-gate 	if (cpend) {
2138*7c478bd9Sstevel@tonic-gate 		FD_ZERO(&mask);
2139*7c478bd9Sstevel@tonic-gate 		FD_SET(fileno(ctrl_in), &mask);
2140*7c478bd9Sstevel@tonic-gate 		if ((nfnd = empty(&mask, 10, fileno(ctrl_in) + 1)) <= 0) {
2141*7c478bd9Sstevel@tonic-gate 			if (nfnd < 0) {
2142*7c478bd9Sstevel@tonic-gate 				perror("abort");
2143*7c478bd9Sstevel@tonic-gate 			}
2144*7c478bd9Sstevel@tonic-gate 			if (ptabflg)
2145*7c478bd9Sstevel@tonic-gate 				code = -1;
2146*7c478bd9Sstevel@tonic-gate 			lostpeer(0);
2147*7c478bd9Sstevel@tonic-gate 		}
2148*7c478bd9Sstevel@tonic-gate 		(void) getreply(0);
2149*7c478bd9Sstevel@tonic-gate 		(void) getreply(0);
2150*7c478bd9Sstevel@tonic-gate 	}
2151*7c478bd9Sstevel@tonic-gate 	if (proxy)
2152*7c478bd9Sstevel@tonic-gate 		pswitch(0);
2153*7c478bd9Sstevel@tonic-gate 	switch (oldtype) {
2154*7c478bd9Sstevel@tonic-gate 		case 0:
2155*7c478bd9Sstevel@tonic-gate 			break;
2156*7c478bd9Sstevel@tonic-gate 		case TYPE_A:
2157*7c478bd9Sstevel@tonic-gate 			setascii(0, NULL);
2158*7c478bd9Sstevel@tonic-gate 			break;
2159*7c478bd9Sstevel@tonic-gate 		case TYPE_I:
2160*7c478bd9Sstevel@tonic-gate 			setbinary(0, NULL);
2161*7c478bd9Sstevel@tonic-gate 			break;
2162*7c478bd9Sstevel@tonic-gate 		case TYPE_E:
2163*7c478bd9Sstevel@tonic-gate 			setebcdic(0, NULL);
2164*7c478bd9Sstevel@tonic-gate 			break;
2165*7c478bd9Sstevel@tonic-gate 		case TYPE_L:
2166*7c478bd9Sstevel@tonic-gate 			settenex(0, NULL);
2167*7c478bd9Sstevel@tonic-gate 			break;
2168*7c478bd9Sstevel@tonic-gate 	}
2169*7c478bd9Sstevel@tonic-gate 	pswitch(1);
2170*7c478bd9Sstevel@tonic-gate 	if (ptabflg)
2171*7c478bd9Sstevel@tonic-gate 		code = -1;
2172*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
2173*7c478bd9Sstevel@tonic-gate }
2174*7c478bd9Sstevel@tonic-gate 
2175*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2176*7c478bd9Sstevel@tonic-gate void
2177*7c478bd9Sstevel@tonic-gate reset(int argc, char *argv[])
2178*7c478bd9Sstevel@tonic-gate {
2179*7c478bd9Sstevel@tonic-gate 	struct fd_set mask;
2180*7c478bd9Sstevel@tonic-gate 	int nfnd = 1;
2181*7c478bd9Sstevel@tonic-gate 
2182*7c478bd9Sstevel@tonic-gate 	FD_ZERO(&mask);
2183*7c478bd9Sstevel@tonic-gate 	while (nfnd > 0) {
2184*7c478bd9Sstevel@tonic-gate 		FD_SET(fileno(ctrl_in), &mask);
2185*7c478bd9Sstevel@tonic-gate 		if ((nfnd = empty(&mask, 0, fileno(ctrl_in) + 1)) < 0) {
2186*7c478bd9Sstevel@tonic-gate 			perror("reset");
2187*7c478bd9Sstevel@tonic-gate 			code = -1;
2188*7c478bd9Sstevel@tonic-gate 			lostpeer(0);
2189*7c478bd9Sstevel@tonic-gate 		} else if (nfnd > 0) {
2190*7c478bd9Sstevel@tonic-gate 			(void) getreply(0);
2191*7c478bd9Sstevel@tonic-gate 		}
2192*7c478bd9Sstevel@tonic-gate 	}
2193*7c478bd9Sstevel@tonic-gate }
2194*7c478bd9Sstevel@tonic-gate 
2195*7c478bd9Sstevel@tonic-gate static char *
2196*7c478bd9Sstevel@tonic-gate gunique(char *local)
2197*7c478bd9Sstevel@tonic-gate {
2198*7c478bd9Sstevel@tonic-gate 	static char new[MAXPATHLEN];
2199*7c478bd9Sstevel@tonic-gate 	char *cp = rindex(local, '/');
2200*7c478bd9Sstevel@tonic-gate 	int d, count = 0;
2201*7c478bd9Sstevel@tonic-gate 	char ext = '1';
2202*7c478bd9Sstevel@tonic-gate 
2203*7c478bd9Sstevel@tonic-gate 	if (cp)
2204*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
2205*7c478bd9Sstevel@tonic-gate 	d = access(cp ? local : ".", 2);
2206*7c478bd9Sstevel@tonic-gate 	if (cp)
2207*7c478bd9Sstevel@tonic-gate 		*cp = '/';
2208*7c478bd9Sstevel@tonic-gate 	if (d < 0) {
2209*7c478bd9Sstevel@tonic-gate 		perror(local);
2210*7c478bd9Sstevel@tonic-gate 		return ((char *)0);
2211*7c478bd9Sstevel@tonic-gate 	}
2212*7c478bd9Sstevel@tonic-gate 	if (strlcpy(new, local, sizeof (new)) >= sizeof (new))
2213*7c478bd9Sstevel@tonic-gate 		(void) printf("gunique: too long: local %s, %d, new %d\n",
2214*7c478bd9Sstevel@tonic-gate 		    local, strlen(local), sizeof (new));
2215*7c478bd9Sstevel@tonic-gate 
2216*7c478bd9Sstevel@tonic-gate 	cp = new + strlen(new);
2217*7c478bd9Sstevel@tonic-gate 	*cp++ = '.';
2218*7c478bd9Sstevel@tonic-gate 	while (!d) {
2219*7c478bd9Sstevel@tonic-gate 		if (++count == 100) {
2220*7c478bd9Sstevel@tonic-gate 			(void) printf(
2221*7c478bd9Sstevel@tonic-gate 				"runique: can't find unique file name.\n");
2222*7c478bd9Sstevel@tonic-gate 			return ((char *)0);
2223*7c478bd9Sstevel@tonic-gate 		}
2224*7c478bd9Sstevel@tonic-gate 		*cp++ = ext;
2225*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
2226*7c478bd9Sstevel@tonic-gate 		if (ext == '9')
2227*7c478bd9Sstevel@tonic-gate 			ext = '0';
2228*7c478bd9Sstevel@tonic-gate 		else
2229*7c478bd9Sstevel@tonic-gate 			ext++;
2230*7c478bd9Sstevel@tonic-gate 		if ((d = access(new, 0)) < 0)
2231*7c478bd9Sstevel@tonic-gate 			break;
2232*7c478bd9Sstevel@tonic-gate 		if (ext != '0')
2233*7c478bd9Sstevel@tonic-gate 			cp--;
2234*7c478bd9Sstevel@tonic-gate 		else if (*(cp - 2) == '.')
2235*7c478bd9Sstevel@tonic-gate 			*(cp - 1) = '1';
2236*7c478bd9Sstevel@tonic-gate 		else {
2237*7c478bd9Sstevel@tonic-gate 			*(cp - 2) = *(cp - 2) + 1;
2238*7c478bd9Sstevel@tonic-gate 			cp--;
2239*7c478bd9Sstevel@tonic-gate 		}
2240*7c478bd9Sstevel@tonic-gate 	}
2241*7c478bd9Sstevel@tonic-gate 	return (new);
2242*7c478bd9Sstevel@tonic-gate }
2243*7c478bd9Sstevel@tonic-gate 
2244*7c478bd9Sstevel@tonic-gate /*
2245*7c478bd9Sstevel@tonic-gate  * This is a wrap-around function for inet_ntop(). In case the af is AF_INET6
2246*7c478bd9Sstevel@tonic-gate  * and the address pointed by src is a IPv4-mapped IPv6 address, it
2247*7c478bd9Sstevel@tonic-gate  * returns printable IPv4 address, not IPv4-mapped IPv6 address. In other cases
2248*7c478bd9Sstevel@tonic-gate  * it behaves just like inet_ntop().
2249*7c478bd9Sstevel@tonic-gate  */
2250*7c478bd9Sstevel@tonic-gate const char *
2251*7c478bd9Sstevel@tonic-gate inet_ntop_native(int af, const void *src, char *dst, size_t size)
2252*7c478bd9Sstevel@tonic-gate {
2253*7c478bd9Sstevel@tonic-gate 	struct in_addr src4;
2254*7c478bd9Sstevel@tonic-gate 	const char *result;
2255*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
2256*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
2257*7c478bd9Sstevel@tonic-gate 
2258*7c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
2259*7c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)src;
2260*7c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2261*7c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr, &src4);
2262*7c478bd9Sstevel@tonic-gate 			result = inet_ntop(AF_INET, &src4, dst, size);
2263*7c478bd9Sstevel@tonic-gate 		} else {
2264*7c478bd9Sstevel@tonic-gate 			result = inet_ntop(AF_INET6, &sin6->sin6_addr,
2265*7c478bd9Sstevel@tonic-gate 			    dst, size);
2266*7c478bd9Sstevel@tonic-gate 		}
2267*7c478bd9Sstevel@tonic-gate 	} else {
2268*7c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)src;
2269*7c478bd9Sstevel@tonic-gate 		result = inet_ntop(af, &sin->sin_addr, dst, size);
2270*7c478bd9Sstevel@tonic-gate 	}
2271*7c478bd9Sstevel@tonic-gate 
2272*7c478bd9Sstevel@tonic-gate 	return (result);
2273*7c478bd9Sstevel@tonic-gate }
2274*7c478bd9Sstevel@tonic-gate 
2275*7c478bd9Sstevel@tonic-gate int
2276*7c478bd9Sstevel@tonic-gate secure_command(char *cmd)
2277*7c478bd9Sstevel@tonic-gate {
2278*7c478bd9Sstevel@tonic-gate 	unsigned char *in = NULL, *out = NULL;
2279*7c478bd9Sstevel@tonic-gate 	int length = 0;
2280*7c478bd9Sstevel@tonic-gate 	size_t inlen;
2281*7c478bd9Sstevel@tonic-gate 
2282*7c478bd9Sstevel@tonic-gate 	if ((auth_type != AUTHTYPE_NONE) && clevel != PROT_C) {
2283*7c478bd9Sstevel@tonic-gate 		gss_buffer_desc in_buf, out_buf;
2284*7c478bd9Sstevel@tonic-gate 		OM_uint32 maj_stat, min_stat;
2285*7c478bd9Sstevel@tonic-gate 
2286*7c478bd9Sstevel@tonic-gate 		/* secure_command (based on level) */
2287*7c478bd9Sstevel@tonic-gate 		if (auth_type == AUTHTYPE_GSSAPI) {
2288*7c478bd9Sstevel@tonic-gate 			OM_uint32 expire_time;
2289*7c478bd9Sstevel@tonic-gate 			int conf_state;
2290*7c478bd9Sstevel@tonic-gate 			/* clevel = PROT_P; */
2291*7c478bd9Sstevel@tonic-gate 			in_buf.value = cmd;
2292*7c478bd9Sstevel@tonic-gate 			in_buf.length = strlen(cmd) + 1;
2293*7c478bd9Sstevel@tonic-gate 
2294*7c478bd9Sstevel@tonic-gate 			maj_stat = gss_context_time(&min_stat, gcontext,
2295*7c478bd9Sstevel@tonic-gate 				&expire_time);
2296*7c478bd9Sstevel@tonic-gate 			if (GSS_ERROR(maj_stat)) {
2297*7c478bd9Sstevel@tonic-gate 				user_gss_error(maj_stat, min_stat,
2298*7c478bd9Sstevel@tonic-gate 					"gss context has expired");
2299*7c478bd9Sstevel@tonic-gate 				fatal("Your gss credentials have expired.  "
2300*7c478bd9Sstevel@tonic-gate 					"Good-bye!");
2301*7c478bd9Sstevel@tonic-gate 			}
2302*7c478bd9Sstevel@tonic-gate 			maj_stat = gss_seal(&min_stat, gcontext,
2303*7c478bd9Sstevel@tonic-gate 					    (clevel == PROT_P), /* private */
2304*7c478bd9Sstevel@tonic-gate 					    GSS_C_QOP_DEFAULT,
2305*7c478bd9Sstevel@tonic-gate 					    &in_buf, &conf_state,
2306*7c478bd9Sstevel@tonic-gate 					    &out_buf);
2307*7c478bd9Sstevel@tonic-gate 			if (maj_stat != GSS_S_COMPLETE) {
2308*7c478bd9Sstevel@tonic-gate 				/* generally need to deal */
2309*7c478bd9Sstevel@tonic-gate 				user_gss_error(maj_stat, min_stat,
2310*7c478bd9Sstevel@tonic-gate 					(clevel == PROT_P) ?
2311*7c478bd9Sstevel@tonic-gate 					"gss_seal ENC didn't complete":
2312*7c478bd9Sstevel@tonic-gate 					"gss_seal MIC didn't complete");
2313*7c478bd9Sstevel@tonic-gate 			} else if ((clevel == PROT_P) && !conf_state) {
2314*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2315*7c478bd9Sstevel@tonic-gate 					"GSSAPI didn't encrypt message");
2316*7c478bd9Sstevel@tonic-gate 				out = out_buf.value;
2317*7c478bd9Sstevel@tonic-gate 			} else {
2318*7c478bd9Sstevel@tonic-gate 				if (debug)
2319*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2320*7c478bd9Sstevel@tonic-gate 					"sealed (%s) %d bytes\n",
2321*7c478bd9Sstevel@tonic-gate 					clevel == PROT_P ? "ENC" : "MIC",
2322*7c478bd9Sstevel@tonic-gate 					out_buf.length);
2323*7c478bd9Sstevel@tonic-gate 
2324*7c478bd9Sstevel@tonic-gate 				out = out_buf.value;
2325*7c478bd9Sstevel@tonic-gate 			}
2326*7c478bd9Sstevel@tonic-gate 		}
2327*7c478bd9Sstevel@tonic-gate 		/* Other auth types go here ... */
2328*7c478bd9Sstevel@tonic-gate 		inlen = ((4 * out_buf.length) / 3) + 4;
2329*7c478bd9Sstevel@tonic-gate 		in = (uchar_t *)malloc(inlen);
2330*7c478bd9Sstevel@tonic-gate 		if (in == NULL) {
2331*7c478bd9Sstevel@tonic-gate 			gss_release_buffer(&min_stat, &out_buf);
2332*7c478bd9Sstevel@tonic-gate 			fatal("Memory error allocating space for response.");
2333*7c478bd9Sstevel@tonic-gate 		}
2334*7c478bd9Sstevel@tonic-gate 		length = out_buf.length;
2335*7c478bd9Sstevel@tonic-gate 		if (auth_error = radix_encode(out, in, inlen, &length, 0)) {
2336*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2337*7c478bd9Sstevel@tonic-gate 				"Couldn't base 64 encode command (%s)\n",
2338*7c478bd9Sstevel@tonic-gate 				radix_error(auth_error));
2339*7c478bd9Sstevel@tonic-gate 			free(in);
2340*7c478bd9Sstevel@tonic-gate 			gss_release_buffer(&min_stat, &out_buf);
2341*7c478bd9Sstevel@tonic-gate 			return (0);
2342*7c478bd9Sstevel@tonic-gate 		}
2343*7c478bd9Sstevel@tonic-gate 
2344*7c478bd9Sstevel@tonic-gate 		(void) fprintf(ctrl_out, "%s %s",
2345*7c478bd9Sstevel@tonic-gate 			clevel == PROT_P ? "ENC" : "MIC", in);
2346*7c478bd9Sstevel@tonic-gate 
2347*7c478bd9Sstevel@tonic-gate 		free(in);
2348*7c478bd9Sstevel@tonic-gate 		gss_release_buffer(&min_stat, &out_buf);
2349*7c478bd9Sstevel@tonic-gate 
2350*7c478bd9Sstevel@tonic-gate 		if (debug)
2351*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2352*7c478bd9Sstevel@tonic-gate 			    "secure_command(%s)\nencoding %d bytes %s %s\n",
2353*7c478bd9Sstevel@tonic-gate 			    cmd, length,
2354*7c478bd9Sstevel@tonic-gate 			    (clevel == PROT_P) ? "ENC" : "MIC", in);
2355*7c478bd9Sstevel@tonic-gate 	} else {
2356*7c478bd9Sstevel@tonic-gate 		/*
2357*7c478bd9Sstevel@tonic-gate 		 * auth_type = AUTHTYPE_NONE or
2358*7c478bd9Sstevel@tonic-gate 		 * command channel is not protected
2359*7c478bd9Sstevel@tonic-gate 		 */
2360*7c478bd9Sstevel@tonic-gate 		fputs(cmd, ctrl_out);
2361*7c478bd9Sstevel@tonic-gate 	}
2362*7c478bd9Sstevel@tonic-gate 
2363*7c478bd9Sstevel@tonic-gate 	(void) fprintf(ctrl_out, "\r\n");
2364*7c478bd9Sstevel@tonic-gate 	(void) fflush(ctrl_out);
2365*7c478bd9Sstevel@tonic-gate 	return (1);
2366*7c478bd9Sstevel@tonic-gate }
2367*7c478bd9Sstevel@tonic-gate 
2368*7c478bd9Sstevel@tonic-gate unsigned int maxbuf;
2369*7c478bd9Sstevel@tonic-gate unsigned char *ucbuf;
2370*7c478bd9Sstevel@tonic-gate 
2371*7c478bd9Sstevel@tonic-gate void
2372*7c478bd9Sstevel@tonic-gate setpbsz(unsigned int size)
2373*7c478bd9Sstevel@tonic-gate {
2374*7c478bd9Sstevel@tonic-gate 	unsigned int actualbuf;
2375*7c478bd9Sstevel@tonic-gate 	int oldverbose;
2376*7c478bd9Sstevel@tonic-gate 
2377*7c478bd9Sstevel@tonic-gate 	if (ucbuf)
2378*7c478bd9Sstevel@tonic-gate 		(void) free(ucbuf);
2379*7c478bd9Sstevel@tonic-gate 	actualbuf = size;
2380*7c478bd9Sstevel@tonic-gate 	while ((ucbuf = (unsigned char *)malloc(actualbuf)) == NULL) {
2381*7c478bd9Sstevel@tonic-gate 		if (actualbuf)
2382*7c478bd9Sstevel@tonic-gate 			actualbuf >>= 2;
2383*7c478bd9Sstevel@tonic-gate 		else {
2384*7c478bd9Sstevel@tonic-gate 			perror("Error while trying to malloc PROT buffer:");
2385*7c478bd9Sstevel@tonic-gate 			exit(1);
2386*7c478bd9Sstevel@tonic-gate 		}
2387*7c478bd9Sstevel@tonic-gate 	}
2388*7c478bd9Sstevel@tonic-gate 	oldverbose = verbose;
2389*7c478bd9Sstevel@tonic-gate 	verbose = 0;
2390*7c478bd9Sstevel@tonic-gate 	reply_parse = "PBSZ=";
2391*7c478bd9Sstevel@tonic-gate 	if (command("PBSZ %u", actualbuf) != COMPLETE)
2392*7c478bd9Sstevel@tonic-gate 		fatal("Cannot set PROT buffer size");
2393*7c478bd9Sstevel@tonic-gate 	if (reply_parse) {
2394*7c478bd9Sstevel@tonic-gate 		if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
2395*7c478bd9Sstevel@tonic-gate 			maxbuf = actualbuf;
2396*7c478bd9Sstevel@tonic-gate 	} else
2397*7c478bd9Sstevel@tonic-gate 		maxbuf = actualbuf;
2398*7c478bd9Sstevel@tonic-gate 	reply_parse = NULL;
2399*7c478bd9Sstevel@tonic-gate 	verbose = oldverbose;
2400*7c478bd9Sstevel@tonic-gate }
2401*7c478bd9Sstevel@tonic-gate 
2402*7c478bd9Sstevel@tonic-gate /*
2403*7c478bd9Sstevel@tonic-gate  * Do the base 64 decoding of the raw input buffer, b64_buf.
2404*7c478bd9Sstevel@tonic-gate  * Also do the verification and decryption, if required.
2405*7c478bd9Sstevel@tonic-gate  * retval contains the current error code number
2406*7c478bd9Sstevel@tonic-gate  *
2407*7c478bd9Sstevel@tonic-gate  * returns:
2408*7c478bd9Sstevel@tonic-gate  *	(RFC 2228:  error returns are 3 digit numbers of the form 5xy)
2409*7c478bd9Sstevel@tonic-gate  *	5	if an error occurred
2410*7c478bd9Sstevel@tonic-gate  */
2411*7c478bd9Sstevel@tonic-gate static int
2412*7c478bd9Sstevel@tonic-gate decode_reply(uchar_t *plain_buf,
2413*7c478bd9Sstevel@tonic-gate 		int ilen,
2414*7c478bd9Sstevel@tonic-gate 		uchar_t *b64_buf,
2415*7c478bd9Sstevel@tonic-gate 		int retval,
2416*7c478bd9Sstevel@tonic-gate 		boolean_t *again)
2417*7c478bd9Sstevel@tonic-gate {
2418*7c478bd9Sstevel@tonic-gate 	int len;
2419*7c478bd9Sstevel@tonic-gate 	int safe = 0;
2420*7c478bd9Sstevel@tonic-gate 
2421*7c478bd9Sstevel@tonic-gate 	*again = 0;
2422*7c478bd9Sstevel@tonic-gate 
2423*7c478bd9Sstevel@tonic-gate 	if (!b64_buf[0])	/* if there is no string, no problem */
2424*7c478bd9Sstevel@tonic-gate 	    return (retval);
2425*7c478bd9Sstevel@tonic-gate 
2426*7c478bd9Sstevel@tonic-gate 	if ((auth_type == AUTHTYPE_NONE)) {
2427*7c478bd9Sstevel@tonic-gate 	    (void) printf("Cannot decode reply:\n%d %s\n", code, b64_buf);
2428*7c478bd9Sstevel@tonic-gate 	    return ('5');
2429*7c478bd9Sstevel@tonic-gate 	}
2430*7c478bd9Sstevel@tonic-gate 
2431*7c478bd9Sstevel@tonic-gate 	switch (code) {
2432*7c478bd9Sstevel@tonic-gate 
2433*7c478bd9Sstevel@tonic-gate 	    case 631:	/* 'safe' */
2434*7c478bd9Sstevel@tonic-gate 		safe = 1;
2435*7c478bd9Sstevel@tonic-gate 		break;
2436*7c478bd9Sstevel@tonic-gate 
2437*7c478bd9Sstevel@tonic-gate 	    case 632:	/* 'private' */
2438*7c478bd9Sstevel@tonic-gate 		break;
2439*7c478bd9Sstevel@tonic-gate 
2440*7c478bd9Sstevel@tonic-gate 	    case 633:	/* 'confidential' */
2441*7c478bd9Sstevel@tonic-gate 		break;
2442*7c478bd9Sstevel@tonic-gate 
2443*7c478bd9Sstevel@tonic-gate 	    default:
2444*7c478bd9Sstevel@tonic-gate 		(void) printf("Unknown reply: %d %s\n", code, b64_buf);
2445*7c478bd9Sstevel@tonic-gate 		return ('5');
2446*7c478bd9Sstevel@tonic-gate 	}
2447*7c478bd9Sstevel@tonic-gate 
2448*7c478bd9Sstevel@tonic-gate 	/* decode the base64 encoded message */
2449*7c478bd9Sstevel@tonic-gate 	auth_error = radix_encode(b64_buf, plain_buf, ilen, &len, 1);
2450*7c478bd9Sstevel@tonic-gate 
2451*7c478bd9Sstevel@tonic-gate 	if (auth_error) {
2452*7c478bd9Sstevel@tonic-gate 		(void) printf("Can't base 64 decode reply %d (%s)\n\"%s\"\n",
2453*7c478bd9Sstevel@tonic-gate 			code, radix_error(auth_error), b64_buf);
2454*7c478bd9Sstevel@tonic-gate 		return ('5');
2455*7c478bd9Sstevel@tonic-gate 	}
2456*7c478bd9Sstevel@tonic-gate 
2457*7c478bd9Sstevel@tonic-gate 	if (auth_type == AUTHTYPE_GSSAPI) {
2458*7c478bd9Sstevel@tonic-gate 		gss_buffer_desc xmit_buf, msg_buf;
2459*7c478bd9Sstevel@tonic-gate 		OM_uint32 maj_stat, min_stat;
2460*7c478bd9Sstevel@tonic-gate 		int conf_state = safe;
2461*7c478bd9Sstevel@tonic-gate 		xmit_buf.value = plain_buf;
2462*7c478bd9Sstevel@tonic-gate 		xmit_buf.length = len;
2463*7c478bd9Sstevel@tonic-gate 
2464*7c478bd9Sstevel@tonic-gate 		/* decrypt/verify the message */
2465*7c478bd9Sstevel@tonic-gate 		maj_stat = gss_unseal(&min_stat, gcontext,
2466*7c478bd9Sstevel@tonic-gate 			&xmit_buf, &msg_buf, &conf_state, NULL);
2467*7c478bd9Sstevel@tonic-gate 		if (maj_stat != GSS_S_COMPLETE) {
2468*7c478bd9Sstevel@tonic-gate 			user_gss_error(maj_stat, min_stat,
2469*7c478bd9Sstevel@tonic-gate 				"failed unsealing reply");
2470*7c478bd9Sstevel@tonic-gate 			return ('5');
2471*7c478bd9Sstevel@tonic-gate 		}
2472*7c478bd9Sstevel@tonic-gate 		if (msg_buf.length < ilen - 2 - 1) {
2473*7c478bd9Sstevel@tonic-gate 			memcpy(plain_buf, msg_buf.value, msg_buf.length);
2474*7c478bd9Sstevel@tonic-gate 			strcpy((char *)&plain_buf[msg_buf.length], "\r\n");
2475*7c478bd9Sstevel@tonic-gate 			gss_release_buffer(&min_stat, &msg_buf);
2476*7c478bd9Sstevel@tonic-gate 			*again = 1;
2477*7c478bd9Sstevel@tonic-gate 		} else {
2478*7c478bd9Sstevel@tonic-gate 			user_gss_error(maj_stat, min_stat,
2479*7c478bd9Sstevel@tonic-gate 				"reply was too long");
2480*7c478bd9Sstevel@tonic-gate 			return ('5');
2481*7c478bd9Sstevel@tonic-gate 		}
2482*7c478bd9Sstevel@tonic-gate 	} /* end if GSSAPI */
2483*7c478bd9Sstevel@tonic-gate 
2484*7c478bd9Sstevel@tonic-gate 	/* Other auth types go here... */
2485*7c478bd9Sstevel@tonic-gate 
2486*7c478bd9Sstevel@tonic-gate 	return (retval);
2487*7c478bd9Sstevel@tonic-gate }
2488