]> cygwin.com Git - cygwin-apps/setup.git/blob - nio-ftp.cc
* geturl.cc (get_url_to_file): Add parameter `allow_ftp_auth'.
[cygwin-apps/setup.git] / nio-ftp.cc
1 /*
2 * Copyright (c) 2000, 2001, Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
16 /* This file is responsible for implementing all direct FTP protocol
17 channels. It is intentionally simplistic. */
18
19 static char *cvsid = "\n%%% $Id$\n";
20
21 #include "win32.h"
22 #include "winsock.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26
27 #include "resource.h"
28 #include "state.h"
29 #include "simpsock.h"
30 #include "log.h"
31
32 #include "netio.h"
33 #include "nio-ftp.h"
34
35 static SimpleSocket *cmd = 0;
36 static char *cmd_host = 0;
37 static int cmd_port = 0;
38
39 static char *last_line;
40
41 static int
42 ftp_line (SimpleSocket *s)
43 {
44 do {
45 last_line = s->gets ();
46 log (LOG_BABBLE, "ftp > %s", last_line);
47 } while (last_line && (!isdigit (last_line[0]) || last_line[3] != ' '));
48 return atoi (last_line ?: "0");
49 }
50
51 NetIO_FTP::NetIO_FTP (char *Purl, BOOL allow_ftp_auth)
52 : NetIO (Purl, allow_ftp_auth)
53 {
54 s = 0;
55 int code;
56
57 if (port == 0)
58 port = 21;
59
60 if (cmd_host && strcmp (host, cmd_host) != 0 || port != cmd_port)
61 {
62 if (cmd)
63 cmd->printf ("QUIT\r\n");
64 delete cmd;
65 free (cmd_host);
66 cmd = 0;
67 cmd_host = 0;
68 }
69
70 if (cmd == 0)
71 {
72 SimpleSocket *c = new SimpleSocket (host, port);
73 int done = 0;
74 code = ftp_line (c);
75
76 auth_retry:
77 if (net_ftp_user)
78 c->printf ("USER %s\r\n", net_ftp_user);
79 else
80 c->printf ("USER anonymous\r\n");
81 code = ftp_line (c);
82 if (code == 331)
83 {
84 if (net_ftp_passwd)
85 c->printf ("PASS %s\r\n", net_ftp_passwd);
86 else
87 c->printf ("PASS cygwin-setup@\r\n");
88 code = ftp_line (c);
89 }
90 if (code == 530) /* Authentication failed, retry */
91 {
92 get_ftp_auth ();
93 if (net_ftp_user && net_ftp_passwd)
94 goto auth_retry;
95 }
96
97 if (code < 200 || code >= 300)
98 {
99 delete c;
100 return;
101 }
102
103 cmd = c;
104 cmd_host = _strdup (host);
105 cmd_port = port;
106
107 cmd->printf ("TYPE I\r\n");
108 code = ftp_line (cmd);
109 }
110
111 cmd->printf ("PASV\r\n");
112 do {
113 code = ftp_line (cmd);
114 } while (code == 226); /* previous RETR */
115 if (code != 227)
116 return;
117
118 char *paren = strchr (last_line, '(');
119 if (!paren)
120 return;
121
122 int i1, i2, i3, i4, p1, p2;
123 sscanf (paren+1, "%d,%d,%d,%d,%d,%d", &i1, &i2, &i3, &i4, &p1, &p2);
124 char tmp[20];
125 sprintf (tmp, "%d.%d.%d.%d", i1, i2, i3, i4);
126 s = new SimpleSocket (tmp, p1*256 + p2);
127
128 cmd->printf ("RETR %s\r\n", path);
129 code = ftp_line (cmd);
130 if (code != 150)
131 {
132 delete s;
133 s = 0;
134 return;
135 }
136 }
137
138 NetIO_FTP::~NetIO_FTP ()
139 {
140 if (s)
141 delete s;
142 }
143
144 int
145 NetIO_FTP::ok ()
146 {
147 if (s)
148 return 1;
149 return 0;
150 }
151
152 int
153 NetIO_FTP::read (char *buf, int nbytes)
154 {
155 if (!s)
156 return 0;
157 return s->read (buf, nbytes);
158 }
This page took 0.042398 seconds and 5 git commands to generate.