ftp.c (77234) | ftp.c (77238) |
---|---|
1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * | 1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * |
28 * $FreeBSD: head/lib/libfetch/ftp.c 77234 2001-05-26 17:23:38Z des $ | 28 * $FreeBSD: head/lib/libfetch/ftp.c 77238 2001-05-26 19:37:15Z des $ |
29 */ 30 31/* 32 * Portions of this code were taken from or based on ftpio.c: 33 * 34 * ---------------------------------------------------------------------------- 35 * "THE BEER-WARE LICENSE" (Revision 42): 36 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you --- 670 unchanged lines hidden (view full) --- 707 if (e != -1) 708 _ftp_seterr(e); 709 if (sd >= 0) 710 close(sd); 711 return NULL; 712} 713 714/* | 29 */ 30 31/* 32 * Portions of this code were taken from or based on ftpio.c: 33 * 34 * ---------------------------------------------------------------------------- 35 * "THE BEER-WARE LICENSE" (Revision 42): 36 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you --- 670 unchanged lines hidden (view full) --- 707 if (e != -1) 708 _ftp_seterr(e); 709 if (sd >= 0) 710 close(sd); 711 return NULL; 712} 713 714/* |
715 * Authenticate 716 */ 717static int 718_ftp_authenticate(int cd, struct url *url, struct url *purl) 719{ 720 char *user, *pwd, *logname; 721 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1]; 722 int e, len; 723 724 /* XXX FTP_AUTH, and maybe .netrc */ 725 726 /* send user name and password */ 727 user = url->user; 728 if (!user || !*user) 729 user = getenv("FTP_LOGIN"); 730 if (!user || !*user) 731 user = FTP_ANONYMOUS_USER; 732 if (purl && url->port == _fetch_default_port(url->scheme)) 733 e = _ftp_cmd(cd, "USER %s@%s", user, url->host); 734 else if (purl) 735 e = _ftp_cmd(cd, "USER %s@%s@%d", user, url->host, url->port); 736 else 737 e = _ftp_cmd(cd, "USER %s", user); 738 739 /* did the server request a password? */ 740 if (e == FTP_NEED_PASSWORD) { 741 pwd = url->pwd; 742 if (!pwd || !*pwd) 743 pwd = getenv("FTP_PASSWORD"); 744 if (!pwd || !*pwd) { 745 if ((logname = getlogin()) == 0) 746 logname = FTP_ANONYMOUS_USER; 747 len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname); 748 gethostname(pbuf + len, sizeof pbuf - len); 749 pwd = pbuf; 750 } 751 e = _ftp_cmd(cd, "PASS %s", pwd); 752 } 753 754 return e; 755} 756 757/* |
|
715 * Log on to FTP server 716 */ 717static int 718_ftp_connect(struct url *url, struct url *purl, const char *flags) 719{ 720 int cd, e, direct, verbose; 721#ifdef INET6 722 int af = AF_UNSPEC; 723#else 724 int af = AF_INET; 725#endif | 758 * Log on to FTP server 759 */ 760static int 761_ftp_connect(struct url *url, struct url *purl, const char *flags) 762{ 763 int cd, e, direct, verbose; 764#ifdef INET6 765 int af = AF_UNSPEC; 766#else 767 int af = AF_INET; 768#endif |
726 const char *logname; 727 const char *user; 728 const char *pwd; 729 char localhost[MAXHOSTNAMELEN]; 730 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1]; | |
731 732 direct = CHECK_FLAG('d'); 733 verbose = CHECK_FLAG('v'); 734 if (CHECK_FLAG('4')) 735 af = AF_INET; 736 else if (CHECK_FLAG('6')) 737 af = AF_INET6; 738 --- 14 unchanged lines hidden (view full) --- 753 if (cd == -1) { 754 _fetch_syserr(); 755 return NULL; 756 } 757 758 /* expect welcome message */ 759 if ((e = _ftp_chkerr(cd)) != FTP_SERVICE_READY) 760 goto fouch; | 769 770 direct = CHECK_FLAG('d'); 771 verbose = CHECK_FLAG('v'); 772 if (CHECK_FLAG('4')) 773 af = AF_INET; 774 else if (CHECK_FLAG('6')) 775 af = AF_INET6; 776 --- 14 unchanged lines hidden (view full) --- 791 if (cd == -1) { 792 _fetch_syserr(); 793 return NULL; 794 } 795 796 /* expect welcome message */ 797 if ((e = _ftp_chkerr(cd)) != FTP_SERVICE_READY) 798 goto fouch; |
761 762 /* XXX FTP_AUTH, and maybe .netrc */ | |
763 | 799 |
764 /* send user name and password */ 765 user = url->user; 766 if (!user || !*user) 767 user = getenv("FTP_LOGIN"); 768 if (!user || !*user) 769 user = FTP_ANONYMOUS_USER; 770 if (purl && url->port == _fetch_default_port(url->scheme)) 771 e = _ftp_cmd(cd, "USER %s@%s", user, url->host); 772 else if (purl) 773 e = _ftp_cmd(cd, "USER %s@%s@%d", user, url->host, url->port); 774 else 775 e = _ftp_cmd(cd, "USER %s", user); 776 777 /* did the server request a password? */ 778 if (e == FTP_NEED_PASSWORD) { 779 pwd = url->pwd; 780 if (!pwd || !*pwd) 781 pwd = getenv("FTP_PASSWORD"); 782 if (!pwd || !*pwd) { 783 if ((logname = getlogin()) == 0) 784 logname = FTP_ANONYMOUS_USER; 785 gethostname(localhost, sizeof localhost); 786 snprintf(pbuf, sizeof pbuf, "%s@%s", logname, localhost); 787 pwd = pbuf; 788 } 789 e = _ftp_cmd(cd, "PASS %s", pwd); 790 } 791 792 /* did the server request an account? */ 793 if (e == FTP_NEED_ACCOUNT) | 800 /* authenticate */ 801 if ((e = _ftp_authenticate(cd, url, purl)) != FTP_LOGGED_IN) |
794 goto fouch; 795 | 802 goto fouch; 803 |
796 /* we should be done by now */ 797 if (e != FTP_LOGGED_IN) 798 goto fouch; 799 | |
800 /* might as well select mode and type at once */ 801#ifdef FTP_FORCE_STREAM_MODE 802 if ((e = _ftp_cmd(cd, "MODE S")) != FTP_OK) /* default is S */ 803 goto fouch; 804#endif 805 if ((e = _ftp_cmd(cd, "TYPE I")) != FTP_OK) /* default is A */ 806 goto fouch; 807 --- 220 unchanged lines hidden --- | 804 /* might as well select mode and type at once */ 805#ifdef FTP_FORCE_STREAM_MODE 806 if ((e = _ftp_cmd(cd, "MODE S")) != FTP_OK) /* default is S */ 807 goto fouch; 808#endif 809 if ((e = _ftp_cmd(cd, "TYPE I")) != FTP_OK) /* default is A */ 810 goto fouch; 811 --- 220 unchanged lines hidden --- |