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