annotate lib/rfc959.c @ 1:8b1883341c6f

Initial revision
author masneyb
date Mon, 05 Aug 2002 19:46:57 +0000
parents
children 5551ab2301fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1 /*****************************************************************************/
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
2 /* rfc959.c - General purpose routines for the FTP protocol (RFC 959) */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
3 /* Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org> */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
4 /* */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
5 /* This program is free software; you can redistribute it and/or modify */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
6 /* it under the terms of the GNU General Public License as published by */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
7 /* the Free Software Foundation; either version 2 of the License, or */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
8 /* (at your option) any later version. */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
9 /* */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
10 /* This program is distributed in the hope that it will be useful, */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
13 /* GNU General Public License for more details. */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
14 /* */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
15 /* You should have received a copy of the GNU General Public License */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
16 /* along with this program; if not, write to the Free Software */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
18 /*****************************************************************************/
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
19
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
20 #include "gftp.h"
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
21
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
22 static int rfc959_connect ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
23 static void rfc959_disconnect ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
24 static long rfc959_get_file ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
25 const char *filename,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
26 FILE * fd,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
27 off_t startsize );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
28 static int rfc959_put_file ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
29 const char *filename,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
30 FILE * fd,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
31 off_t startsize,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
32 off_t totalsize );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
33 static long rfc959_transfer_file ( gftp_request *fromreq,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
34 const char *fromfile,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
35 off_t fromsize,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
36 gftp_request *toreq,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
37 const char *tofile,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
38 off_t tosize );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
39 static int rfc959_end_transfer ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
40 static int rfc959_list_files ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
41 static int rfc959_set_data_type ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
42 int data_type );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
43 static off_t rfc959_get_file_size ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
44 const char *filename );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
45 static int rfc959_data_connection_new ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
46 static int rfc959_accept_active_connection ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
47 static int rfc959_send_command ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
48 const char *command );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
49 static int write_to_socket ( gftp_request *request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
50 const char *command );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
51 static int rfc959_read_response ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
52 static int rfc959_chdir ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
53 const char *directory );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
54 static int rfc959_rmdir ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
55 const char *directory );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
56 static int rfc959_rmfile ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
57 const char *file );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
58 static int rfc959_mkdir ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
59 const char *directory );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
60 static int rfc959_rename ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
61 const char *oldname,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
62 const char *newname );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
63 static int rfc959_chmod ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
64 const char *file,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
65 int mode );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
66 static int rfc959_site ( gftp_request * request,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
67 const char *command );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
68 static char *parse_ftp_proxy_string ( gftp_request * request );
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
69
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
70 void
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
71 rfc959_init (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
72 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
73 g_return_if_fail (request != NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
74
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
75 request->protonum = GFTP_FTP_NUM;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
76 request->init = rfc959_init;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
77 request->destroy = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
78 request->connect = rfc959_connect;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
79 request->disconnect = rfc959_disconnect;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
80 request->get_file = rfc959_get_file;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
81 request->put_file = rfc959_put_file;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
82 request->transfer_file = rfc959_transfer_file;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
83 request->get_next_file_chunk = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
84 request->put_next_file_chunk = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
85 request->end_transfer = rfc959_end_transfer;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
86 request->list_files = rfc959_list_files;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
87 request->get_next_file = rfc959_get_next_file;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
88 request->set_data_type = rfc959_set_data_type;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
89 request->get_file_size = rfc959_get_file_size;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
90 request->chdir = rfc959_chdir;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
91 request->rmdir = rfc959_rmdir;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
92 request->rmfile = rfc959_rmfile;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
93 request->mkdir = rfc959_mkdir;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
94 request->rename = rfc959_rename;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
95 request->chmod = rfc959_chmod;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
96 request->set_file_time = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
97 request->site = rfc959_site;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
98 request->parse_url = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
99 request->url_prefix = "ftp";
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
100 request->protocol_name = "FTP";
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
101 request->need_hostport = 1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
102 request->need_userpass = 1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
103 request->use_cache = 1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
104 request->use_threads = 1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
105 request->always_connected = 0;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
106 gftp_set_config_options (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
107 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
108
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
109
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
110 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
111 rfc959_connect (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
112 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
113 char tempchar, *startpos, *endpos, *tempstr, *dir;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
114 int sock, ret, resp;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
115
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
116 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
117 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
118 g_return_val_if_fail (request->hostname != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
119 g_return_val_if_fail (request->username != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
120
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
121 if (request->sockfd != NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
122 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
123
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
124 if ((sock = gftp_connect_server (request, "ftp")) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
125 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
126
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
127 if ((request->sockfd = fdopen (sock, "rb")) == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
128 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
129 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
130 _("Cannot fdopen() socket: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
131 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
132 close (sock);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
133 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
134 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
135
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
136 if ((request->sockfd_write = fdopen (dup (sock), "wb")) == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
137 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
138 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
139 _("Cannot fdopen() socket: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
140 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
141 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
142 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
143 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
144
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
145 /* Get the banner */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
146 if (rfc959_read_response (request) != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
147 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
148 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
149 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
150 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
151
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
152 /* Login the proxy server if available */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
153 if (request->use_proxy)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
154 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
155 resp = '3';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
156 startpos = endpos = tempstr = parse_ftp_proxy_string (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
157 while ((resp == '3' || resp == '2') && *startpos != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
158 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
159 if (*endpos == '\n' || *endpos == '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
160 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
161 tempchar = *(endpos + 1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
162 if (*endpos != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
163 *(endpos + 1) = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
164 if ((resp = rfc959_send_command (request, startpos)) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
165 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
166 if (*endpos != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
167 *(endpos + 1) = tempchar;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
168 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
169 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
170 startpos = endpos + 1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
171 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
172 endpos++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
173 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
174 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
175 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
176 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
177 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
178 tempstr = g_strconcat ("USER ", request->username, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
179 resp = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
180 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
181 if (resp < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
182 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
183 if (resp == '3')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
184 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
185 tempstr = g_strconcat ("PASS ", request->password, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
186 resp = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
187 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
188 if (resp < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
189 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
190 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
191 if (resp == '3' && request->account)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
192 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
193 tempstr = g_strconcat ("ACCT ", request->account, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
194 resp = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
195 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
196 if (resp < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
197 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
198 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
199 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
200
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
201 if (resp != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
202 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
203 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
204 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
205 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
206
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
207 if (request->data_type == GFTP_TYPE_BINARY)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
208 tempstr = "TYPE I\r\n";
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
209 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
210 tempstr = "TYPE A\r\n";
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
211
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
212 if (rfc959_send_command (request, tempstr) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
213 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
214
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
215 ret = -1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
216 if (request->directory != NULL && *request->directory != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
217 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
218 ret = rfc959_chdir (request, request->directory);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
219 if (request->sockfd == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
220 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
221 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
222
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
223 if (ret != 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
224 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
225 if (rfc959_send_command (request, "PWD\r\n") != '2' ||
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
226 request->sockfd == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
227 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
228 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
229 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
230 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
231
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
232 if ((tempstr = strchr (request->last_ftp_response, '"')) == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
233 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
234 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
235 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
236 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
237 dir = tempstr + 1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
238
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
239 if ((tempstr = strchr (dir, '"')) == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
240 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
241 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
242 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
243 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
244 if (tempstr != NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
245 *tempstr = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
246
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
247 request->directory = g_malloc (strlen (dir) + 1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
248 strcpy (request->directory, dir);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
249 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
250
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
251 if (request->sockfd == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
252 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
253
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
254 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
255 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
256
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
257
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
258 static void
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
259 rfc959_disconnect (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
260 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
261 g_return_if_fail (request != NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
262 g_return_if_fail (request->protonum == GFTP_FTP_NUM);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
263
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
264 if (request->sockfd != NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
265 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
266 request->logging_function (gftp_logging_misc, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
267 _("Disconnecting from site %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
268 request->hostname);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
269 fclose (request->sockfd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
270 fclose (request->sockfd_write);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
271 request->sockfd = request->sockfd_write = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
272 if (request->datafd)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
273 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
274 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
275 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
276 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
277 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
278 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
279
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
280
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
281 static long
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
282 rfc959_get_file (gftp_request * request, const char *filename, FILE * fd,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
283 off_t startsize)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
284 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
285 char *command, *tempstr, resp;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
286 int ret, flags;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
287
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
288 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
289 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
290 g_return_val_if_fail (filename != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
291 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
292
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
293 if (fd != NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
294 request->datafd = fd;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
295
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
296 if (request->datafd == NULL &&
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
297 (ret = rfc959_data_connection_new (request)) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
298 return (ret);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
299
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
300 flags = fcntl (fileno (request->datafd), F_GETFL, 0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
301 if (fcntl (fileno (request->datafd), F_SETFL, flags | O_NONBLOCK) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
302 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
303 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
304 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
305 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
306 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
307
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
308 if (startsize > 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
309 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
310 command = g_strdup_printf ("REST %ld\r\n", startsize);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
311 resp = rfc959_send_command (request, command);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
312 g_free (command);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
313
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
314 if (resp != '3')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
315 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
316 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
317 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
318 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
319 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
320 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
321
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
322 tempstr = g_strconcat ("RETR ", filename, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
323 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
324 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
325
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
326 if (ret != '1')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
327 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
328 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
329 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
330 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
331 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
332
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
333 if (request->transfer_type == gftp_transfer_active &&
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
334 (ret = rfc959_accept_active_connection (request)) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
335 return (ret);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
336
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
337 if ((tempstr = strrchr (request->last_ftp_response, '(')) == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
338 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
339 tempstr = request->last_ftp_response + 4;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
340 while (!isdigit ((int) *tempstr) && *tempstr != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
341 tempstr++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
342 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
343 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
344 tempstr++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
345
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
346 return (strtol (tempstr, NULL, 10) + startsize);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
347 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
348
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
349
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
350 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
351 rfc959_put_file (gftp_request * request, const char *filename, FILE * fd,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
352 off_t startsize, off_t totalsize)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
353 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
354 char *command, *tempstr, resp;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
355 int ret, flags;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
356
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
357 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
358 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
359 g_return_val_if_fail (filename != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
360 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
361
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
362 if (fd != NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
363 fd = request->datafd;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
364
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
365 if (request->datafd == NULL &&
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
366 (ret = rfc959_data_connection_new (request)) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
367 return (ret);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
368
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
369 flags = fcntl (fileno (request->datafd), F_GETFL, 0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
370 if (fcntl (fileno (request->datafd), F_SETFL, flags | O_NONBLOCK) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
371 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
372 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
373 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
374 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
375 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
376
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
377 if (startsize > 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
378 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
379 command = g_strdup_printf ("REST %ld\r\n", startsize);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
380 resp = rfc959_send_command (request, command);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
381 g_free (command);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
382 if (resp != '3')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
383 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
384 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
385 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
386 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
387 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
388 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
389
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
390 tempstr = g_strconcat ("STOR ", filename, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
391 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
392 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
393 if (ret != '1')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
394 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
395 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
396 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
397 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
398 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
399
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
400 if (request->transfer_type == gftp_transfer_active &&
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
401 (ret = rfc959_accept_active_connection (request)) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
402 return (ret);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
403
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
404 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
405 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
406
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
407 static long
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
408 rfc959_transfer_file (gftp_request *fromreq, const char *fromfile,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
409 off_t fromsize, gftp_request *toreq,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
410 const char *tofile, off_t tosize)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
411 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
412 char *tempstr, *pos, *endpos;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
413
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
414 g_return_val_if_fail (fromreq != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
415 g_return_val_if_fail (fromfile != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
416 g_return_val_if_fail (toreq != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
417 g_return_val_if_fail (tofile != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
418 g_return_val_if_fail (fromreq->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
419 g_return_val_if_fail (toreq->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
420
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
421 fromreq->transfer_type = gftp_transfer_passive;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
422 toreq->transfer_type = gftp_transfer_active;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
423
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
424 if (rfc959_send_command (fromreq, "PASV\r\n") != '2' ||
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
425 fromreq->sockfd == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
426 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
427
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
428 pos = fromreq->last_ftp_response + 4;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
429 while (!isdigit ((int) *pos) && *pos != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
430 pos++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
431 if (*pos == '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
432 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
433
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
434 endpos = pos;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
435 while (*endpos != ')' && *endpos != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
436 endpos++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
437 if (*endpos == ')')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
438 *endpos = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
439
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
440 tempstr = g_strconcat ("PORT ", pos, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
441 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
442 if (rfc959_send_command (toreq, tempstr) != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
443 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
444
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
445 tempstr = g_strconcat ("RETR ", fromfile, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
446 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
447 if (write_to_socket (fromreq, tempstr) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
448 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
449
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
450 tempstr = g_strconcat ("STOR ", tofile, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
451 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
452 if (write_to_socket (toreq, tempstr) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
453 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
454
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
455 if (rfc959_read_response (fromreq) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
456 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
457 if (rfc959_read_response (toreq) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
458 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
459
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
460 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
461 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
462
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
463
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
464 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
465 rfc959_end_transfer (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
466 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
467 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
468 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
469 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
470
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
471 if (request->datafd)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
472 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
473 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
474 request->datafd = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
475 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
476 return (rfc959_read_response (request) == '2' ? 0 : -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
477 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
478
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
479
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
480 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
481 rfc959_list_files (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
482 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
483 char *tempstr, parms[3];
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
484 int ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
485
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
486 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
487 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
488 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
489
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
490 if ((ret = rfc959_data_connection_new (request)) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
491 return (ret);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
492
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
493 *parms = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
494 strcat (parms, show_hidden_files ? "a" : "");
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
495 strcat (parms, resolve_symlinks ? "L" : "");
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
496 tempstr = g_strconcat ("LIST", *parms != '\0' ? " -" : "", parms, "\r\n",
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
497 NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
498
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
499 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
500 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
501
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
502 if (ret != '1')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
503 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
504
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
505 ret = 0;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
506 if (request->transfer_type == gftp_transfer_active)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
507 ret = rfc959_accept_active_connection (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
508
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
509 return (ret);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
510 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
511
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
512
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
513 int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
514 rfc959_get_next_file (gftp_request * request, gftp_file * fle, FILE * fd)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
515 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
516 char tempstr[255];
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
517 size_t len;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
518
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
519 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
520 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
521 g_return_val_if_fail (fle != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
522 g_return_val_if_fail (fd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
523
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
524 if (request->last_dir_entry)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
525 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
526 g_free (request->last_dir_entry);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
527 request->last_dir_entry = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
528 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
529 do
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
530 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
531 /* I don't run select() here because select could return
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
532 successfully saying there is data, but the fgets call could block if
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
533 there is no carriage return */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
534 if (!fgets (tempstr, sizeof (tempstr), fd))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
535 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
536 gftp_file_destroy (fle);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
537 if (ferror (fd))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
538 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
539 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
540 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
541 tempstr[sizeof (tempstr) - 1] = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
542
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
543 if (gftp_parse_ls (tempstr, fle) != 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
544 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
545 if (strncmp (tempstr, "total", strlen("total")) != 0 &&
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
546 strncmp (tempstr, _("total"), strlen(_("total"))) != 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
547 request->logging_function (gftp_logging_misc, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
548 _("Warning: Cannot parse listing %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
549 tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
550 gftp_file_destroy (fle);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
551 continue;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
552 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
553 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
554 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
555 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
556 while (1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
557
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
558 len = strlen (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
559 if (!request->cached)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
560 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
561 request->last_dir_entry = g_malloc (len + 1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
562 strcpy (request->last_dir_entry, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
563 request->last_dir_entry_len = len;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
564 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
565 return (len);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
566 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
567
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
568
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
569 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
570 rfc959_set_data_type (gftp_request * request, int data_type)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
571 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
572 char *tempstr;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
573
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
574 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
575 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
576
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
577 if (request->sockfd != NULL && request->data_type != data_type)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
578 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
579 if (data_type == GFTP_TYPE_BINARY)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
580 tempstr = "TYPE I\r\n";
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
581 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
582 tempstr = "TYPE A\r\n";
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
583
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
584 if (rfc959_send_command (request, tempstr) != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
585 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
586 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
587 request->data_type = data_type;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
588 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
589 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
590
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
591
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
592 static off_t
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
593 rfc959_get_file_size (gftp_request * request, const char *filename)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
594 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
595 char *tempstr;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
596 int ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
597
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
598 g_return_val_if_fail (request != NULL, 0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
599 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
600 g_return_val_if_fail (filename != NULL, 0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
601 g_return_val_if_fail (request->sockfd != NULL, 0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
602
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
603 tempstr = g_strconcat ("SIZE ", filename, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
604 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
605 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
606 if (ret < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
607 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
608
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
609 if (*request->last_ftp_response != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
610 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
611 return (strtol (request->last_ftp_response + 4, NULL, 10));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
612 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
613
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
614
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
615 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
616 rfc959_data_connection_new (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
617 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
618 char *pos, *pos1, resp, *command;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
619 struct sockaddr_in data_addr;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
620 size_t data_addr_len;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
621 unsigned int temp[6];
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
622 unsigned char ad[6];
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
623 int i, sock;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
624
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
625 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
626 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
627 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
628
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
629 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
630 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
631 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
632 _("Failed to create a socket: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
633 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
634 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
635 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
636 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
637
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
638 data_addr_len = sizeof (data_addr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
639 memset (&data_addr, 0, data_addr_len);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
640 data_addr.sin_family = AF_INET;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
641
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
642 if (request->transfer_type == gftp_transfer_passive)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
643 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
644 if ((resp = rfc959_send_command (request, "PASV\r\n")) != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
645 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
646 if (request->sockfd == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
647 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
648
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
649 request->transfer_type = gftp_transfer_active;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
650 return (rfc959_data_connection_new (request));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
651 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
652 pos = request->last_ftp_response + 4;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
653 while (!isdigit ((int) *pos) && *pos != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
654 pos++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
655 if (*pos == '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
656 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
657 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
658 close (sock);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
659 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
660 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
661 if (sscanf (pos, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
662 &temp[3], &temp[4], &temp[5]) != 6)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
663 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
664 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
665 close (sock);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
666 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
667 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
668 for (i = 0; i < 6; i++)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
669 ad[i] = (unsigned char) (temp[i] & 0xff);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
670
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
671 memcpy (&data_addr.sin_addr, &ad[0], 4);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
672 memcpy (&data_addr.sin_port, &ad[4], 2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
673 if (connect (sock, (struct sockaddr *) &data_addr, data_addr_len) == -1)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
674 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
675 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
676 _("Cannot create a data connection: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
677 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
678 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
679 close (sock);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
680 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
681 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
682 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
683 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
684 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
685 getsockname (fileno (request->sockfd), (struct sockaddr *) &data_addr,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
686 &data_addr_len);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
687 data_addr.sin_port = 0;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
688 if (bind (sock, (struct sockaddr *) &data_addr, data_addr_len) == -1)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
689 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
690 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
691 _("Cannot bind a port: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
692 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
693 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
694 close (sock);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
695 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
696 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
697
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
698 getsockname (sock, (struct sockaddr *) &data_addr, &data_addr_len);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
699 if (listen (sock, 1) == -1)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
700 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
701 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
702 _("Cannot listen on port %d: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
703 ntohs (data_addr.sin_port),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
704 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
705 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
706 close (sock);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
707 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
708 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
709 pos = (char *) &data_addr.sin_addr;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
710 pos1 = (char *) &data_addr.sin_port;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
711 command = g_strdup_printf ("PORT %u,%u,%u,%u,%u,%u\r\n",
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
712 pos[0] & 0xff, pos[1] & 0xff, pos[2] & 0xff,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
713 pos[3] & 0xff, pos1[0] & 0xff,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
714 pos1[1] & 0xff);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
715 resp = rfc959_send_command (request, command);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
716 g_free (command);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
717 if (resp != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
718 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
719 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
720 close (sock);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
721 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
722 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
723 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
724
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
725 if ((request->datafd = fdopen (sock, "rb+")) == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
726 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
727 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
728 _("Cannot fdopen() socket: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
729 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
730 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
731 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
732 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
733
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
734 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
735 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
736
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
737
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
738 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
739 rfc959_accept_active_connection (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
740 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
741 struct sockaddr_in cli_addr;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
742 size_t cli_addr_len;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
743 int infd;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
744
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
745 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
746 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
747 g_return_val_if_fail (request->datafd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
748 g_return_val_if_fail (request->transfer_type == gftp_transfer_active, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
749
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
750 cli_addr_len = sizeof (cli_addr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
751 if ((infd = accept (fileno (request->datafd), (struct sockaddr *) &cli_addr,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
752 &cli_addr_len)) == -1)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
753 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
754 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
755 _("Cannot accept connection from server: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
756 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
757 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
758 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
759 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
760
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
761 fclose (request->datafd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
762
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
763 if ((request->datafd = fdopen (infd, "rb+")) == NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
764 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
765 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
766 _("Cannot fdopen() socket: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
767 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
768 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
769 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
770 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
771 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
772 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
773
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
774
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
775 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
776 rfc959_send_command (gftp_request * request, const char *command)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
777 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
778 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
779 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
780 g_return_val_if_fail (command != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
781 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
782
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
783 if (strncmp (command, "PASS", 4) == 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
784 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
785 request->logging_function (gftp_logging_send, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
786 "PASS xxxx\n");
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
787 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
788 else if (strncmp (command, "ACCT", 4) == 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
789 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
790 request->logging_function (gftp_logging_send, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
791 "ACCT xxxx\n");
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
792 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
793 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
794 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
795 request->logging_function (gftp_logging_send, request->user_data, "%s",
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
796 command);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
797 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
798
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
799 if (write_to_socket (request, command) < 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
800 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
801 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
802 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
803 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
804
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
805 return (rfc959_read_response (request));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
806 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
807
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
808 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
809 write_to_socket (gftp_request *request, const char *command)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
810 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
811 struct timeval tv;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
812 fd_set fset;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
813
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
814 FD_ZERO (&fset);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
815 FD_SET (fileno (request->sockfd_write), &fset);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
816 tv.tv_sec = request->network_timeout;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
817 tv.tv_usec = 0;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
818 if (!select (fileno (request->sockfd_write) + 1, NULL, &fset, NULL, &tv))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
819 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
820 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
821 _("Connection to %s timed out\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
822 request->hostname);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
823 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
824 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
825 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
826
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
827 fwrite (command, strlen (command), 1, request->sockfd_write);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
828 if (ferror (request->sockfd_write))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
829 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
830 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
831 _("Error: Could not write to socket: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
832 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
833 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
834 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
835 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
836
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
837 fflush (request->sockfd_write);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
838 if (ferror (request->sockfd_write))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
839 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
840 request->logging_function (gftp_logging_error, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
841 _("Error: Could not write to socket: %s\n"),
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
842 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
843 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
844 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
845 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
846
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
847 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
848 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
849
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
850
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
851
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
852 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
853 rfc959_read_response (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
854 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
855 char tempstr[255], code[4];
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
856
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
857 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
858 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
859 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
860
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
861 *code = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
862 if (request->last_ftp_response)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
863 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
864 g_free (request->last_ftp_response);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
865 request->last_ftp_response = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
866 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
867
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
868 do
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
869 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
870 /* I don't run select() here because select could return
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
871 successfully saying there is data, but the fgets call could block if
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
872 there is no carriage return */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
873 if (!fgets (tempstr, sizeof (tempstr), request->sockfd))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
874 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
875 tempstr[strlen (tempstr) - 1] = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
876 if (tempstr[strlen (tempstr) - 1] == '\r')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
877 tempstr[strlen (tempstr) - 1] = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
878 if (isdigit ((int) *tempstr) && isdigit ((int) *(tempstr + 1))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
879 && isdigit ((int) *(tempstr + 2)))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
880 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
881 strncpy (code, tempstr, 3);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
882 code[3] = ' ';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
883 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
884 request->logging_function (gftp_logging_recv, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
885 "%s\n", tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
886 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
887 while (strncmp (code, tempstr, 4) != 0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
888
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
889 if (ferror (request->sockfd))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
890 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
891 request->logging_function (gftp_logging_send, request->user_data,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
892 "Error reading from socket: %s\n",
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
893 g_strerror (errno));
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
894 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
895 return (-1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
896 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
897
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
898 request->last_ftp_response = g_malloc (strlen (tempstr) + 1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
899 strcpy (request->last_ftp_response, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
900
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
901 if (*request->last_ftp_response == '4')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
902 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
903
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
904 return (*request->last_ftp_response);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
905 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
906
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
907
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
908 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
909 rfc959_chdir (gftp_request * request, const char *directory)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
910 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
911 char ret, *tempstr, *dir;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
912
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
913 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
914 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
915 g_return_val_if_fail (directory != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
916
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
917 if (strcmp (directory, "..") == 0)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
918 ret = rfc959_send_command (request, "CDUP\r\n");
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
919 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
920 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
921 tempstr = g_strconcat ("CWD ", directory, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
922 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
923 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
924 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
925
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
926 if (ret != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
927 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
928
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
929 if (directory != request->directory)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
930 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
931 if (request->directory)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
932 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
933 g_free (request->directory);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
934 request->directory = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
935 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
936
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
937 if (rfc959_send_command (request, "PWD\r\n") != '2')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
938 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
939
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
940 tempstr = strchr (request->last_ftp_response, '"');
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
941 if (tempstr != NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
942 dir = tempstr + 1;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
943 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
944 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
945
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
946 tempstr = strchr (dir, '"');
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
947 if (tempstr != NULL)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
948 *tempstr = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
949 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
950 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
951 gftp_disconnect (request);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
952 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
953 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
954
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
955 request->directory = g_malloc (strlen (dir) + 1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
956 strcpy (request->directory, dir);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
957 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
958
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
959 return (0);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
960 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
961
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
962
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
963 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
964 rfc959_rmdir (gftp_request * request, const char *directory)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
965 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
966 char *tempstr, ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
967
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
968 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
969 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
970 g_return_val_if_fail (directory != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
971 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
972
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
973 tempstr = g_strconcat ("RMD ", directory, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
974 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
975 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
976 return (ret == '2' ? 0 : -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
977 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
978
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
979
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
980 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
981 rfc959_rmfile (gftp_request * request, const char *file)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
982 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
983 char *tempstr, ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
984
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
985 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
986 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
987 g_return_val_if_fail (file != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
988 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
989
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
990 tempstr = g_strconcat ("DELE ", file, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
991 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
992 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
993 return (ret == '2' ? 0 : -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
994 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
995
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
996
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
997 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
998 rfc959_mkdir (gftp_request * request, const char *directory)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
999 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1000 char *tempstr, ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1001
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1002 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1003 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1004 g_return_val_if_fail (directory != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1005 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1006
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1007 tempstr = g_strconcat ("MKD ", directory, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1008 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1009 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1010 return (ret == '2' ? 0 : -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1011 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1012
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1013
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1014 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1015 rfc959_rename (gftp_request * request, const char *oldname,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1016 const char *newname)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1017 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1018 char *tempstr, ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1019
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1020 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1021 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1022 g_return_val_if_fail (oldname != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1023 g_return_val_if_fail (newname != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1024 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1025
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1026 tempstr = g_strconcat ("RNFR ", oldname, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1027 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1028 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1029 if (ret != '3')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1030 return (-2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1031
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1032 tempstr = g_strconcat ("RNTO ", newname, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1033 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1034 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1035 return (ret == '2' ? 0 : -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1036 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1037
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1038
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1039 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1040 rfc959_chmod (gftp_request * request, const char *file, int mode)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1041 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1042 char *tempstr, ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1043
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1044 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1045 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1046 g_return_val_if_fail (file != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1047 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1048
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1049 tempstr = g_malloc (strlen (file) + (mode / 10) + 16);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1050 sprintf (tempstr, "SITE CHMOD %d %s\r\n", mode, file);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1051 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1052 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1053 return (ret == '2' ? 0 : -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1054 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1055
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1056
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1057 static int
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1058 rfc959_site (gftp_request * request, const char *command)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1059 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1060 char *tempstr, ret;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1061
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1062 g_return_val_if_fail (request != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1063 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1064 g_return_val_if_fail (command != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1065 g_return_val_if_fail (request->sockfd != NULL, -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1066
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1067 tempstr = g_strconcat ("SITE ", command, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1068 ret = rfc959_send_command (request, tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1069 g_free (tempstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1070 return (request->sockfd != NULL ? ret : -2);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1071 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1072
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1073
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1074 static char *
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1075 parse_ftp_proxy_string (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1076 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1077 char *startpos, *endpos, *oldstr, *newstr, *newval, *tempport;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1078
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1079 g_return_val_if_fail (request != NULL, NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1080 g_return_val_if_fail (request->protonum == GFTP_FTP_NUM, NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1081
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1082 newstr = g_malloc (1);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1083 *newstr = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1084 startpos = endpos = request->proxy_config;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1085 while (*endpos != '\0')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1086 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1087 tempport = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1088 if (*endpos == '%' && tolower ((int) *(endpos + 1)) == 'p')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1089 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1090 switch (tolower ((int) *(endpos + 2)))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1091 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1092 case 'u':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1093 newval = request->proxy_username;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1094 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1095 case 'p':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1096 newval = request->proxy_password;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1097 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1098 case 'h':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1099 newval = request->proxy_hostname;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1100 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1101 case 'o':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1102 tempport = g_strdup_printf ("%d", request->proxy_port);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1103 newval = tempport;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1104 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1105 case 'a':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1106 newval = request->proxy_account;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1107 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1108 default:
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1109 endpos++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1110 continue;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1111 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1112 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1113 else if (*endpos == '%' && tolower ((int) *(endpos + 1)) == 'h')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1114 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1115 switch (tolower ((int) *(endpos + 2)))
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1116 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1117 case 'u':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1118 newval = request->username;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1119 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1120 case 'p':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1121 newval = request->password;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1122 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1123 case 'h':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1124 newval = request->hostname;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1125 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1126 case 'o':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1127 tempport = g_strdup_printf ("%d", request->port);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1128 newval = tempport;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1129 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1130 case 'a':
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1131 newval = request->account;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1132 break;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1133 default:
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1134 endpos++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1135 continue;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1136 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1137 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1138 else if (*endpos == '%' && tolower ((int) *(endpos + 1)) == 'n')
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1139 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1140 *endpos = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1141 oldstr = newstr;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1142 newstr = g_strconcat (oldstr, startpos, "\r\n", NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1143 g_free (oldstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1144 endpos += 2;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1145 startpos = endpos;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1146 continue;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1147 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1148 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1149 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1150 endpos++;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1151 continue;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1152 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1153
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1154 *endpos = '\0';
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1155 oldstr = newstr;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1156 if (!newval)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1157 newstr = g_strconcat (oldstr, startpos, NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1158 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1159 newstr = g_strconcat (oldstr, startpos, newval, NULL);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1160 if (tempport)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1161 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1162 g_free (tempport);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1163 tempport = NULL;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1164 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1165 g_free (oldstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1166 endpos += 3;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1167 startpos = endpos;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1168 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1169 return (newstr);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1170 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1171