2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
14 * Turned inside out. Now returns xfers as new file ids, not as a special
17 * $FreeBSD: src/lib/libftpio/ftpio.c,v 1.33.2.4 2002/07/25 15:25:32 ume Exp $
18 * $DragonFly: src/lib/libftpio/ftpio.c,v 1.7 2004/08/19 23:57:46 joerg Exp $
22 #include <sys/param.h>
23 #include <sys/socket.h>
25 #include <netinet/in.h>
27 #include <arpa/inet.h>
48 /* How to see by a given code whether or not the connection has timed out */
49 #define FTP_TIMEOUT(code) (FtpTimedOut || code == FTP_TIMED_OUT)
51 /* Internal routines - deal only with internal FTP_t type */
52 static FTP_t ftp_new(void);
53 static void check_passive(FILE *fp);
54 static int ftp_read_method(void *n, char *buf, int nbytes);
55 static int ftp_write_method(void *n, const char *buf, int nbytes);
56 static int ftp_close_method(void *n);
57 static int writes(int fd, const char *s);
58 static char *get_a_line(FTP_t ftp);
59 static int get_a_number(FTP_t ftp, char **q);
60 static int botch(const char *func, const char *botch_state);
61 static int cmd(FTP_t ftp, const char *fmt, ...);
62 static int ftp_login_session(FTP_t ftp, const char *host, int af,
63 const char *user, const char *passwd,
64 int port, int verbose);
65 static int ftp_file_op(FTP_t ftp, const char *operation, const char *file,
66 FILE **fp, const char *mode, off_t *seekto);
67 static int ftp_close(FTP_t ftp);
68 static int get_url_info(const char *url_in, char *host_ret,
69 size_t host_len, int *port_ret,
70 char *name_ret, size_t name_len);
71 static void ftp_timeout(int sig);
72 static void ftp_set_timeout(void);
73 static void ftp_clear_timeout(void);
74 static void ai_unmapped(struct addrinfo *);
76 int networkInit(void);
79 /* Global status variable - ick */
82 /* FTP happy status codes */
83 #define FTP_GENERALLY_HAPPY 200
84 #define FTP_ASCII_HAPPY FTP_GENERALLY_HAPPY
85 #define FTP_BINARY_HAPPY FTP_GENERALLY_HAPPY
86 #define FTP_PORT_HAPPY FTP_GENERALLY_HAPPY
87 #define FTP_HAPPY_COMMENT 220
88 #define FTP_QUIT_HAPPY 221
89 #define FTP_TRANSFER_HAPPY 226
90 #define FTP_PASSIVE_HAPPY 227
91 #define FTP_LPASSIVE_HAPPY 228
92 #define FTP_EPASSIVE_HAPPY 229
93 #define FTP_CHDIR_HAPPY 250
95 /* FTP unhappy status codes */
96 #define FTP_TIMED_OUT 421
100 * gross! evil! bad! We really need an access primitive for cookie in stdio
102 * it's too convenient a hook to bury and it's already exported through funopen
106 #define fcookie(fp) ((fp)->_cookie)
108 /* Placeholder in case we want to do any pre-init stuff at some point */
112 return SUCCESS; /* XXX dummy function for now XXX */
115 /* Check a return code with some lenience for back-dated garbage that might be in the buffer */
117 check_code(FTP_t ftp, int var, int preferred)
121 if (var == preferred)
123 else if (var == FTP_TRANSFER_HAPPY) /* last operation succeeded */
124 var = get_a_number(ftp, NULL);
125 else if (var == FTP_HAPPY_COMMENT) /* chit-chat */
126 var = get_a_number(ftp, NULL);
127 else if (var == FTP_GENERALLY_HAPPY) /* general success code */
128 var = get_a_number(ftp, NULL);
139 FTP_t ftp = fcookie(fp);
144 i = cmd(ftp, "TYPE A");
145 if (i < 0 || check_code(ftp, i, FTP_ASCII_HAPPY))
147 ftp->is_binary = FALSE;
154 FTP_t ftp = fcookie(fp);
159 i = cmd(ftp, "TYPE I");
160 if (i < 0 || check_code(ftp, i, FTP_BINARY_HAPPY))
162 ftp->is_binary = TRUE;
166 ftpVerbose(FILE *fp, int status)
168 FTP_t ftp = fcookie(fp);
169 ftp->is_verbose = status;
173 ftpChdir(FILE *fp, char *dir)
176 FTP_t ftp = fcookie(fp);
178 i = cmd(ftp, "CWD %s", dir);
179 if (i < 0 || check_code(ftp, i, FTP_CHDIR_HAPPY))
187 FTP_t ftp = fcookie(fp);
192 ftpErrString(int error)
197 return("connection in wrong state");
199 /* XXX soon UNIX errnos will catch up with FTP protocol errnos */
200 return strerror(error);
201 for (k = 0; k < ftpErrListLength; k++)
202 if (ftpErrList[k].num == error)
203 return(ftpErrList[k].string);
204 return("Unknown error");
208 ftpGetSize(FILE *fp, const char *name)
211 char p[BUFSIZ], *cp, *ep;
212 FTP_t ftp = fcookie(fp);
216 if ((size_t)snprintf(p, sizeof(p), "SIZE %s\r\n", name) >= sizeof(p))
219 fprintf(stderr, "Sending %s", p);
220 if (writes(ftp->fd_ctrl, p))
222 i = get_a_number(ftp, &cp);
223 if (check_code(ftp, i, 213))
226 errno = 0; /* to check for ERANGE */
227 size = (off_t)strtoq(cp, &ep, 10);
228 if (*ep != '\0' || errno == ERANGE)
234 ftpGetModtime(FILE *fp, const char *name)
238 time_t t0 = time (0);
239 FTP_t ftp = fcookie(fp);
243 if ((size_t)snprintf(p, sizeof(p), "MDTM %s\r\n", name) >= sizeof(p))
246 fprintf(stderr, "Sending %s", p);
247 if (writes(ftp->fd_ctrl, p))
249 i = get_a_number(ftp, &cp);
250 if (check_code(ftp, i, 213))
252 while (*cp && !isdigit(*cp))
256 t0 = localtime (&t0)->tm_gmtoff;
257 sscanf(cp, "%04d%02d%02d%02d%02d%02d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
267 ftpGet(FILE *fp, const char *file, off_t *seekto)
270 FTP_t ftp = fcookie(fp);
273 if (ftpBinary(fp) != SUCCESS)
276 if (ftp_file_op(ftp, "RETR", file, &fp2, "r", seekto) == SUCCESS)
281 /* Returns a standard FILE pointer type representing an open control connection */
283 ftpLogin(const char *host, const char *user, const char *passwd, int port,
284 int verbose, int *retcode)
287 return ftpLoginAf(host, AF_UNSPEC, user, passwd, port, verbose, retcode);
289 return ftpLoginAf(host, AF_INET, user, passwd, port, verbose, retcode);
294 ftpLoginAf(const char *host, int af, const char *user, const char *passwd,
295 int port, int verbose, int *retcode)
302 if (networkInit() != SUCCESS)
307 if (n && ftp_login_session(n, host, af, user, passwd, port, verbose) == SUCCESS) {
308 fp = funopen(n, ftp_read_method, ftp_write_method, NULL, ftp_close_method); /* BSD 4.4 function! */
309 fp->_file = n->fd_ctrl;
313 *retcode = (FtpTimedOut ? FTP_TIMED_OUT : -1);
314 /* Poor attempt at mapping real errnos to FTP error codes */
315 else switch(n->error) {
317 *retcode = FTP_TIMED_OUT; /* Actually no such host, but we have no way of saying that. :-( */
321 *retcode = FTP_TIMED_OUT;
333 ftpPut(FILE *fp, const char *file)
336 FTP_t ftp = fcookie(fp);
339 if (ftp_file_op(ftp, "STOR", file, &fp2, "w", NULL) == SUCCESS)
345 ftpPassive(FILE *fp, int st)
347 FTP_t ftp = fcookie(fp);
349 ftp->is_passive = !!st; /* normalize "st" to zero or one */
354 ftpGetURL(const char *url, const char *user, const char *passwd, int *retcode)
357 return ftpGetURLAf(url, AF_UNSPEC, user, passwd, retcode);
359 return ftpGetURLAf(url, AF_INET, user, passwd, retcode);
364 ftpGetURLAf(const char *url, int af, const char *user, const char *passwd,
367 char host[255], name[255];
370 static FILE *fp = NULL;
371 static char *prev_host;
375 if (get_url_info(url, host, sizeof(host), &port, name,
376 sizeof(name)) == SUCCESS) {
377 if (fp && prev_host) {
378 if (!strcmp(prev_host, host)) {
379 /* Try to use cached connection */
380 fp2 = ftpGet(fp, name, NULL);
382 /* Connection timed out or was no longer valid */
391 /* It's a different host now, flush old */
397 fp = ftpLoginAf(host, af, user, passwd, port, 0, retcode);
399 fp2 = ftpGet(fp, name, NULL);
401 /* Connection timed out or was no longer valid */
403 *retcode = ftpErrno(fp);
408 prev_host = strdup(host);
416 ftpPutURL(const char *url, const char *user, const char *passwd, int *retcode)
419 return ftpPutURLAf(url, AF_UNSPEC, user, passwd, retcode);
421 return ftpPutURLAf(url, AF_INET, user, passwd, retcode);
427 ftpPutURLAf(const char *url, int af, const char *user, const char *passwd,
430 char host[255], name[255];
432 static FILE *fp = NULL;
437 if (fp) { /* Close previous managed connection */
441 if (get_url_info(url, host, sizeof(host), &port,
442 name, sizeof(name)) == SUCCESS) {
443 fp = ftpLoginAf(host, af, user, passwd, port, 0, retcode);
445 fp2 = ftpPut(fp, name);
448 *retcode = ftpErrno(fp);
458 /* Internal workhorse function for dissecting URLs. Takes a URL as the first argument and returns the
459 result of such disection in the host, user, passwd, port and name variables. */
461 get_url_info(const char *url_in, char *host_ret, size_t host_len,
462 int *port_ret, char *name_ret, size_t name_len)
464 char *name, *host, *cp, url[BUFSIZ];
468 /* XXX add http:// here or somewhere reasonable at some point XXX */
469 if (strncmp("ftp://", url_in, 6) != 0)
471 /* We like to stomp a lot on the URL string in dissecting it, so copy it first */
472 strncpy(url, url_in, BUFSIZ);
474 if ((cp = index(host, ':')) != NULL) {
476 port = strtol(cp, 0, 0);
479 port = 0; /* use default */
483 if ((name = index(cp ? cp : host, '/')) != NULL)
486 if (strlen(host) >= host_len)
488 strcpy(host_ret, host);
490 if (name && name_ret) {
491 if (strlen(name) >= name_len)
493 strcpy(name_ret, name);
503 ftp = (FTP_t)malloc(sizeof *ftp);
506 memset(ftp, 0, sizeof *ftp);
508 ftp->con_state = init;
509 ftp->is_binary = FALSE;
510 ftp->is_passive = FALSE;
511 ftp->is_verbose = FALSE;
517 ftp_read_method(void *vp, char *buf, int nbytes)
523 i = (fd >= 0) ? read(fd, buf, nbytes) : EOF;
528 ftp_write_method(void *vp, const char *buf, int nbytes)
534 i = (fd >= 0) ? write(fd, buf, nbytes) : EOF;
539 ftp_close_method(void *n)
543 i = ftp_close((FTP_t)n);
549 * This function checks whether the FTP_PASSIVE_MODE environment
550 * variable is set, and, if so, enforces the desired mode.
553 check_passive(FILE *fp)
555 const char *cp = getenv("FTP_PASSIVE_MODE");
558 ftpPassive(fp, strncasecmp(cp, "no", 2));
562 ftp_timeout(int sig __unused)
565 /* Debug("ftp_pkg: ftp_timeout called - operation timed out"); */
569 ftp_set_timeout(void)
571 struct sigaction new;
576 sigemptyset(&new.sa_mask);
578 new.sa_handler = ftp_timeout;
579 sigaction(SIGALRM, &new, NULL);
580 cp = getenv("FTP_TIMEOUT");
581 if (!cp || !(ival = atoi(cp)))
587 ftp_clear_timeout(void)
589 struct sigaction new;
592 sigemptyset(&new.sa_mask);
594 new.sa_handler = SIG_DFL;
595 sigaction(SIGALRM, &new, NULL);
599 writes(int fd, const char *s)
601 int n, i = strlen(s);
606 if (FtpTimedOut || i != n)
612 get_a_line(FTP_t ftp)
614 static char buf[BUFSIZ];
617 /* Debug("ftp_pkg: trying to read a line from %d", ftp->fd_ctrl); */
618 for(i = 0; i < BUFSIZ;) {
620 j = read(ftp->fd_ctrl, buf + i, 1);
622 if (FtpTimedOut || j != 1)
624 if (buf[i] == '\r' || buf[i] == '\n') {
628 if (ftp->is_verbose == TRUE)
629 fprintf(stderr, "%s\n",buf+4);
634 /* Debug("ftp_pkg: read string \"%s\" from %d", buf, ftp->fd_ctrl); */
639 get_a_number(FTP_t ftp, char **q)
649 return FTP_TIMED_OUT;
652 if (!(isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])))
654 if (i == -1 && p[3] == '-') {
658 if (p[3] != ' ' && p[3] != '\t')
663 /* Debug("ftp_pkg: read reply %d from server (%s)", j, p); */
667 /* Debug("ftp_pkg: read reply %d from server (%s)", j, p); */
679 if (ftp->con_state == isopen) {
680 ftp->con_state = quit;
681 /* If last operation timed out, don't try to quit - just close */
682 if (ftp->error != FTP_TIMED_OUT)
683 i = cmd(ftp, "QUIT");
686 if (!check_code(ftp, i, FTP_QUIT_HAPPY))
691 else if (ftp->con_state == quit)
697 botch(const char *func __unused, const char *botch_state __unused)
699 /* Debug("ftp_pkg: botch: %s(%s)", func, botch_state); */
704 cmd(FTP_t ftp, const char *fmt, ...)
711 if ((size_t)vsnprintf(p, sizeof p - 2, fmt, ap) >= sizeof(p) - 2)
715 if (ftp->con_state == init)
716 return botch("cmd", "open");
720 fprintf(stderr, "Sending: %s", p);
721 if (writes(ftp->fd_ctrl, p)) {
723 return FTP_TIMED_OUT;
726 while ((i = get_a_number(ftp, NULL)) == FTP_HAPPY_COMMENT);
731 ftp_login_session(FTP_t ftp, const char *host, int af,
732 const char *user, const char *passwd, int port, int verbose)
735 struct addrinfo hints, *res, *res0;
740 if (networkInit() != SUCCESS)
743 if (ftp->con_state != init) {
758 if ((size_t)snprintf(pbuf, sizeof(pbuf), "%d", port) >= sizeof(pbuf))
760 memset(&hints, 0, sizeof(hints));
761 hints.ai_family = af;
762 hints.ai_socktype = SOCK_STREAM;
763 hints.ai_protocol = 0;
764 err = getaddrinfo(host, pbuf, &hints, &res0);
771 for (res = res0; res; res = res->ai_next) {
773 ftp->addrtype = res->ai_family;
775 if ((s = socket(res->ai_family, res->ai_socktype,
776 res->ai_protocol)) < 0)
779 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
794 ftp->con_state = isopen;
795 ftp->is_verbose = verbose;
797 i = cmd(ftp, "USER %s", user);
798 if (i >= 300 && i < 400)
799 i = cmd(ftp, "PASS %s", passwd);
800 if (i >= 299 || i < 0) {
810 ftp_file_op(FTP_t ftp, const char *operation, const char *file, FILE **fp,
811 const char *mode, off_t *seekto)
815 unsigned char addr[64];
817 struct sockaddr_in sin4;
818 struct sockaddr_in6 sin6;
826 if (ftp->con_state != isopen)
827 return botch("ftp_file_op", "open");
829 if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0) {
834 if (ftp->is_passive) {
835 if (ftp->addrtype == AF_INET) {
837 fprintf(stderr, "Sending PASV\n");
838 if (writes(ftp->fd_ctrl, "PASV\r\n")) {
841 ftp->error = FTP_TIMED_OUT;
842 return FTP_TIMED_OUT;
844 i = get_a_number(ftp, &q);
845 if (check_code(ftp, i, FTP_PASSIVE_HAPPY)) {
852 fprintf(stderr, "Sending EPSV\n");
853 if (writes(ftp->fd_ctrl, "EPSV\r\n")) {
856 ftp->error = FTP_TIMED_OUT;
857 return FTP_TIMED_OUT;
859 i = get_a_number(ftp, &q);
860 if (check_code(ftp, i, FTP_EPASSIVE_HAPPY)) {
862 fprintf(stderr, "Sending LPSV\n");
863 if (writes(ftp->fd_ctrl, "LPSV\r\n")) {
866 ftp->error = FTP_TIMED_OUT;
867 return FTP_TIMED_OUT;
869 i = get_a_number(ftp, &q);
870 if (check_code(ftp, i, FTP_LPASSIVE_HAPPY)) {
878 if (strcmp(cmdstr, "PASV") == 0 || strcmp(cmdstr, "LPSV") == 0) {
879 while (*q && !isdigit(*q))
886 l = (ftp->addrtype == AF_INET ? 6 : 21);
887 for (i = 0; i < l; i++) {
889 addr[i] = strtol(q, &q, 10);
892 sin.sin4.sin_family = ftp->addrtype;
893 if (ftp->addrtype == AF_INET6) {
894 sin.sin6.sin6_len = sizeof(struct sockaddr_in6);
895 bcopy(addr + 2, (char *)&sin.sin6.sin6_addr, 16);
896 bcopy(addr + 19, (char *)&sin.sin6.sin6_port, 2);
898 sin.sin4.sin_len = sizeof(struct sockaddr_in);
899 bcopy(addr, (char *)&sin.sin4.sin_addr, 4);
900 bcopy(addr + 4, (char *)&sin.sin4.sin_port, 2);
902 } else if (strcmp(cmdstr, "EPSV") == 0) {
905 while (*q && *q != '(') /* ) */
912 if (sscanf(q, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
913 &port, &addr[3]) != 5
914 || addr[0] != addr[1] || addr[0] != addr[2] || addr[0] != addr[3]) {
918 sinlen = sizeof(sin);
919 if (getpeername(ftp->fd_ctrl, (struct sockaddr *)&sin, &sinlen) < 0) {
923 switch (sin.sin4.sin_family) {
925 sin.sin4.sin_port = htons(port);
928 sin.sin6.sin6_port = htons(port);
936 if (connect(s, (struct sockaddr *)&sin, sin.sin4.sin_len) < 0) {
941 if (seekto && *seekto) {
942 i = cmd(ftp, "REST %d", *seekto);
943 if (i < 0 || FTP_TIMEOUT(i)) {
950 i = cmd(ftp, "%s %s", operation, file);
951 if (i < 0 || i > 299) {
956 *fp = fdopen(s, mode);
961 #ifdef IPV6_PORTRANGE
962 if (ftp->addrtype == AF_INET6) {
963 portrange = IPV6_PORTRANGE_HIGH;
964 if (setsockopt(s, IPPROTO_IPV6, IPV6_PORTRANGE, (char *)
965 &portrange, sizeof(portrange)) < 0) {
972 if (ftp->addrtype == AF_INET) {
973 portrange = IP_PORTRANGE_HIGH;
974 if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, (char *)
975 &portrange, sizeof(portrange)) < 0) {
983 getsockname(ftp->fd_ctrl, (struct sockaddr *)&sin, &i);
984 sin.sin4.sin_port = 0;
985 i = ((struct sockaddr *)&sin)->sa_len;
986 if (bind(s, (struct sockaddr *)&sin, i) < 0) {
991 getsockname(s,(struct sockaddr *)&sin,&i);
992 if (listen(s, 1) < 0) {
996 if (sin.sin4.sin_family == AF_INET) {
998 a = ntohl(sin.sin4.sin_addr.s_addr);
999 i = cmd(ftp, "PORT %d,%d,%d,%d,%d,%d",
1004 (ntohs(sin.sin4.sin_port) >> 8) & 0xff,
1005 ntohs(sin.sin4.sin_port) & 0xff);
1006 if (check_code(ftp, i, FTP_PORT_HAPPY)) {
1011 #define UC(b) (((int)b)&0xff)
1013 char hname[INET6_ADDRSTRLEN];
1015 sin.sin6.sin6_scope_id = 0;
1016 if (getnameinfo((struct sockaddr *)&sin, sin.sin6.sin6_len,
1017 hname, sizeof(hname),
1018 NULL, 0, NI_NUMERICHOST) != 0) {
1021 i = cmd(ftp, "EPRT |%d|%s|%d|", 2, hname,
1022 htons(sin.sin6.sin6_port));
1023 if (check_code(ftp, i, FTP_PORT_HAPPY)) {
1025 a = (char *)&sin.sin6.sin6_addr;
1027 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1029 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1030 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
1031 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
1032 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
1034 (ntohs(sin.sin4.sin_port) >> 8) & 0xff,
1035 ntohs(sin.sin4.sin_port) & 0xff);
1036 if (check_code(ftp, i, FTP_PORT_HAPPY)) {
1042 if (seekto && *seekto) {
1043 i = cmd(ftp, "REST %d", *seekto);
1044 if (i < 0 || FTP_TIMEOUT(i)) {
1052 i = cmd(ftp, "%s %s", operation, file);
1053 if (i < 0 || i > 299) {
1058 fd = accept(s, 0, 0);
1065 *fp = fdopen(fd, mode);
1074 ai_unmapped(struct addrinfo *ai)
1076 struct sockaddr_in6 *sin6;
1077 struct sockaddr_in sin;
1079 if (ai->ai_family != AF_INET6)
1081 if (ai->ai_addrlen != sizeof(struct sockaddr_in6) ||
1082 sizeof(sin) > ai->ai_addrlen)
1084 sin6 = (struct sockaddr_in6 *)ai->ai_addr;
1085 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1088 memset(&sin, 0, sizeof(sin));
1089 sin.sin_family = AF_INET;
1090 sin.sin_len = sizeof(struct sockaddr_in);
1091 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
1092 sizeof(sin.sin_addr));
1093 sin.sin_port = sin6->sin6_port;
1095 ai->ai_family = AF_INET;
1096 memcpy(ai->ai_addr, &sin, sin.sin_len);
1097 ai->ai_addrlen = sin.sin_len;