Mercurial > gftp.yaz
annotate lib/protocols.c @ 518:c6a6e908dbaf
2004-7-27 Brian Masney <masneyb@gftp.org>
* lib/protocols.c lib/rfc2068.c lib/rfc959.c lib/sshv2.c
src/gtk/bookmarks.c src/gtk/dnd.c src/gtk/gftp-gtk.c src/gtk/gtui.c
src/gtk/menu-items.c src/misc-gtk.c src/gtk/options_dialog.c
src/gtk/transfer.c src/text/gftp-text.c src/uicommon/gftpui.c -
various fixes found through a static code analysis. (some
signed/unsigned fixes, removed unneeded casts, indentation fixes,
added static declaration to some functions)
author | masneyb |
---|---|
date | Wed, 28 Jul 2004 01:37:15 +0000 |
parents | fbb5a02beddb |
children | e67a52327b24 |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* protocols.c - Skeleton functions for the protocols gftp supports */ | |
122 | 3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program is distributed in the hope that it will be useful, */ | |
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
18 /*****************************************************************************/ | |
19 | |
20 #include "gftp.h" | |
33 | 21 static const char cvsid[] = "$Id$"; |
1 | 22 |
23 gftp_request * | |
24 gftp_request_new (void) | |
25 { | |
26 gftp_request *request; | |
27 | |
28 request = g_malloc0 (sizeof (*request)); | |
66 | 29 request->datafd = -1; |
30 request->cachefd = -1; | |
122 | 31 request->server_type = GFTP_DIRTYPE_OTHER; |
1 | 32 return (request); |
33 } | |
136 | 34 |
1 | 35 |
36 void | |
67 | 37 gftp_request_destroy (gftp_request * request, int free_request) |
1 | 38 { |
39 g_return_if_fail (request != NULL); | |
40 | |
41 gftp_disconnect (request); | |
42 | |
43 if (request->destroy != NULL) | |
44 request->destroy (request); | |
45 | |
46 if (request->hostname) | |
47 g_free (request->hostname); | |
48 if (request->username) | |
49 g_free (request->username); | |
50 if (request->password) | |
51 { | |
52 memset (request->password, 0, strlen (request->password)); | |
53 g_free (request->password); | |
54 } | |
55 if (request->account) | |
56 { | |
57 memset (request->account, 0, strlen (request->account)); | |
58 g_free (request->account); | |
59 } | |
60 if (request->directory) | |
61 g_free (request->directory); | |
62 if (request->last_ftp_response) | |
63 g_free (request->last_ftp_response); | |
64 if (request->protocol_data) | |
65 g_free (request->protocol_data); | |
67 | 66 |
198 | 67 if (request->local_options_vars != NULL) |
68 { | |
201 | 69 gftp_config_free_options (request->local_options_vars, |
70 request->local_options_hash, | |
71 request->num_local_options_vars); | |
198 | 72 } |
73 | |
1 | 74 memset (request, 0, sizeof (*request)); |
67 | 75 |
76 if (free_request) | |
77 g_free (request); | |
78 else | |
79 { | |
80 request->datafd = -1; | |
81 request->cachefd = -1; | |
82 } | |
1 | 83 } |
84 | |
85 | |
309 | 86 /* This function is called to copy protocol specific data from one request |
87 structure to another. This is typically called when a file transfer is | |
88 completed, state information can be copied back to the main window */ | |
89 void | |
90 gftp_copy_param_options (gftp_request * dest_request, | |
91 gftp_request * src_request) | |
92 { | |
93 g_return_if_fail (dest_request != NULL); | |
94 g_return_if_fail (src_request != NULL); | |
95 g_return_if_fail (dest_request->protonum == src_request->protonum); | |
96 | |
97 if (dest_request->copy_param_options) | |
98 dest_request->copy_param_options (dest_request, src_request); | |
99 } | |
100 | |
101 | |
1 | 102 void |
103 gftp_file_destroy (gftp_file * file) | |
104 { | |
105 g_return_if_fail (file != NULL); | |
106 | |
107 if (file->file) | |
108 g_free (file->file); | |
349 | 109 if (file->utf8_file) |
110 g_free (file->utf8_file); | |
1 | 111 if (file->user) |
112 g_free (file->user); | |
113 if (file->group) | |
114 g_free (file->group); | |
115 if (file->destfile) | |
116 g_free (file->destfile); | |
117 memset (file, 0, sizeof (*file)); | |
118 } | |
119 | |
120 | |
121 int | |
122 gftp_connect (gftp_request * request) | |
123 { | |
177 | 124 int ret; |
125 | |
84 | 126 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 127 |
128 if (request->connect == NULL) | |
84 | 129 return (GFTP_EFATAL); |
1 | 130 |
177 | 131 if ((ret = gftp_set_config_options (request)) < 0) |
132 return (ret); | |
1 | 133 |
134 return (request->connect (request)); | |
135 } | |
136 | |
137 | |
138 void | |
139 gftp_disconnect (gftp_request * request) | |
140 { | |
141 g_return_if_fail (request != NULL); | |
142 | |
143 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
151 | 144 if (request->free_hostp && request->hostp != NULL) |
1 | 145 freeaddrinfo (request->hostp); |
146 #endif | |
147 request->hostp = NULL; | |
148 | |
168 | 149 #ifdef USE_SSL |
150 if (request->ssl != NULL) | |
151 { | |
152 SSL_free (request->ssl); | |
153 request->ssl = NULL; | |
154 } | |
155 #endif | |
156 | |
187 | 157 #if GLIB_MAJOR_VERSION > 1 |
158 if (request->iconv_initialized) | |
159 { | |
160 g_iconv_close (request->iconv); | |
161 request->iconv_initialized = 0; | |
162 } | |
163 #endif | |
164 | |
1 | 165 request->cached = 0; |
166 if (request->disconnect == NULL) | |
167 return; | |
168 request->disconnect (request); | |
169 } | |
170 | |
171 | |
58 | 172 off_t |
173 gftp_get_file (gftp_request * request, const char *filename, int fd, | |
244 | 174 off_t startsize) |
1 | 175 { |
84 | 176 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 177 |
178 request->cached = 0; | |
179 if (request->get_file == NULL) | |
84 | 180 return (GFTP_EFATAL); |
244 | 181 |
1 | 182 return (request->get_file (request, filename, fd, startsize)); |
183 } | |
184 | |
185 | |
186 int | |
58 | 187 gftp_put_file (gftp_request * request, const char *filename, int fd, |
244 | 188 off_t startsize, off_t totalsize) |
1 | 189 { |
84 | 190 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 191 |
192 request->cached = 0; | |
193 if (request->put_file == NULL) | |
84 | 194 return (GFTP_EFATAL); |
1 | 195 return (request->put_file (request, filename, fd, startsize, totalsize)); |
196 } | |
197 | |
198 | |
261 | 199 off_t |
1 | 200 gftp_transfer_file (gftp_request * fromreq, const char *fromfile, |
244 | 201 int fromfd, off_t fromsize, |
1 | 202 gftp_request * toreq, const char *tofile, |
244 | 203 int tofd, off_t tosize) |
1 | 204 { |
469 | 205 /* Needed for systems that size(float) < size(void *) */ |
206 union { intptr_t i; float f; } maxkbs; | |
261 | 207 off_t size; |
84 | 208 int ret; |
1 | 209 |
84 | 210 g_return_val_if_fail (fromreq != NULL, GFTP_EFATAL); |
211 g_return_val_if_fail (fromfile != NULL, GFTP_EFATAL); | |
212 g_return_val_if_fail (toreq != NULL, GFTP_EFATAL); | |
213 g_return_val_if_fail (tofile != NULL, GFTP_EFATAL); | |
214 | |
469 | 215 gftp_lookup_request_option (toreq, "maxkbs", &maxkbs.f); |
216 | |
217 if (maxkbs.f > 0) | |
294 | 218 { |
219 toreq->logging_function (gftp_logging_misc, toreq, | |
220 _("File transfer will be throttled to %.2f KB/s\n"), | |
469 | 221 maxkbs.f); |
294 | 222 } |
223 | |
87 | 224 if (fromreq->protonum == toreq->protonum && |
84 | 225 fromreq->transfer_file != NULL) |
226 return (fromreq->transfer_file (fromreq, fromfile, fromsize, toreq, | |
227 tofile, tosize)); | |
1 | 228 |
229 fromreq->cached = 0; | |
230 toreq->cached = 0; | |
443 | 231 |
232 get_file: | |
233 size = gftp_get_file (fromreq, fromfile, fromfd, tosize); | |
234 if (size < 0) | |
1 | 235 { |
443 | 236 if (size == GFTP_ETIMEDOUT) |
237 { | |
238 ret = gftp_connect (fromreq); | |
239 if (ret < 0) | |
240 return (ret); | |
241 | |
242 goto get_file; | |
243 } | |
244 | |
245 return (size); | |
246 } | |
247 | |
248 put_file: | |
249 ret = gftp_put_file (toreq, tofile, tofd, tosize, size); | |
250 if (ret != 0) | |
251 { | |
252 if (size == GFTP_ETIMEDOUT) | |
253 { | |
254 ret = gftp_connect (fromreq); | |
255 if (ret < 0) | |
256 return (ret); | |
257 | |
258 goto put_file; | |
259 } | |
260 | |
40 | 261 if (gftp_abort_transfer (fromreq) != 0) |
262 gftp_end_transfer (fromreq); | |
263 | |
84 | 264 return (ret); |
1 | 265 } |
266 | |
267 return (size); | |
268 } | |
269 | |
270 | |
58 | 271 ssize_t |
1 | 272 gftp_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
273 { | |
84 | 274 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
275 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 276 |
277 if (request->get_next_file_chunk != NULL) | |
278 return (request->get_next_file_chunk (request, buf, size)); | |
279 | |
168 | 280 return (request->read_function (request, buf, size, request->datafd)); |
1 | 281 } |
282 | |
283 | |
58 | 284 ssize_t |
1 | 285 gftp_put_next_file_chunk (gftp_request * request, char *buf, size_t size) |
286 { | |
84 | 287 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
288 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 289 |
290 if (request->put_next_file_chunk != NULL) | |
291 return (request->put_next_file_chunk (request, buf, size)); | |
292 | |
293 if (size == 0) | |
294 return (0); | |
295 | |
168 | 296 return (request->write_function (request, buf, size, request->datafd)); |
1 | 297 } |
298 | |
299 | |
300 int | |
301 gftp_end_transfer (gftp_request * request) | |
302 { | |
303 int ret; | |
304 | |
84 | 305 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 306 |
40 | 307 if (!request->cached && |
308 request->end_transfer != NULL) | |
309 ret = request->end_transfer (request); | |
310 else | |
311 ret = 0; | |
1 | 312 |
58 | 313 if (request->cachefd > 0) |
1 | 314 { |
58 | 315 close (request->cachefd); |
316 request->cachefd = -1; | |
1 | 317 } |
318 | |
319 if (request->last_dir_entry) | |
320 { | |
321 g_free (request->last_dir_entry); | |
322 request->last_dir_entry = NULL; | |
323 request->last_dir_entry_len = 0; | |
324 } | |
325 | |
326 return (ret); | |
327 } | |
328 | |
329 | |
330 int | |
40 | 331 gftp_abort_transfer (gftp_request * request) |
332 { | |
84 | 333 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
40 | 334 |
335 if (request->abort_transfer == NULL) | |
84 | 336 return (GFTP_EFATAL); |
40 | 337 |
338 return (request->abort_transfer (request)); | |
339 } | |
340 | |
341 | |
518 | 342 mode_t /* FIXME - this is unsigned, can return negative */ |
500 | 343 gftp_stat_filename (gftp_request * request, const char *filename) |
344 { | |
345 mode_t ret; | |
346 | |
347 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
348 | |
349 if (request->stat_filename != NULL) | |
350 ret = request->stat_filename (request, filename); | |
351 else | |
352 ret = 0; | |
353 | |
354 return (ret); | |
355 } | |
356 | |
357 | |
40 | 358 int |
1 | 359 gftp_list_files (gftp_request * request) |
360 { | |
473 | 361 char *remote_lc_time, *locret; |
58 | 362 int fd; |
1 | 363 |
84 | 364 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 365 |
473 | 366 gftp_lookup_request_option (request, "remote_lc_time", &remote_lc_time); |
367 if (remote_lc_time != NULL && *remote_lc_time != '\0') | |
368 locret = setlocale (LC_TIME, remote_lc_time); | |
369 else | |
370 locret = setlocale (LC_TIME, NULL); | |
371 | |
372 if (locret == NULL) | |
373 { | |
374 locret = setlocale (LC_TIME, NULL); | |
375 request->logging_function (gftp_logging_misc, request, | |
376 _("Error setting LC_TIME to '%s'. Falling back to '%s'\n"), | |
377 remote_lc_time, locret); | |
378 } | |
379 | |
1 | 380 request->cached = 0; |
58 | 381 if (request->use_cache && (fd = gftp_find_cache_entry (request)) > 0) |
1 | 382 { |
186 | 383 request->logging_function (gftp_logging_misc, request, |
473 | 384 _("Loading directory listing %s from cache (LC_TIME=%s)\n"), |
385 request->directory, locret); | |
1 | 386 |
387 request->cachefd = fd; | |
388 request->cached = 1; | |
389 return (0); | |
390 } | |
391 else if (request->use_cache) | |
392 { | |
473 | 393 request->logging_function (gftp_logging_misc, request, |
394 _("Loading directory listing %s from server (LC_TIME=%s)\n"), | |
395 request->directory, locret); | |
396 | |
1 | 397 request->cachefd = gftp_new_cache_entry (request); |
398 request->cached = 0; | |
399 } | |
400 | |
401 if (request->list_files == NULL) | |
84 | 402 return (GFTP_EFATAL); |
473 | 403 |
1 | 404 return (request->list_files (request)); |
405 } | |
406 | |
407 | |
184 | 408 #if GLIB_MAJOR_VERSION > 1 |
291 | 409 |
184 | 410 static char * |
423 | 411 _gftp_get_next_charset (char **curpos) |
184 | 412 { |
413 char *ret, *endpos; | |
414 | |
415 if (**curpos == '\0') | |
416 return (NULL); | |
417 | |
418 ret = *curpos; | |
185 | 419 if ((endpos = strchr (*curpos, ',')) == NULL) |
184 | 420 *curpos += strlen (*curpos); |
421 else | |
422 { | |
423 *endpos = '\0'; | |
423 | 424 *curpos = endpos + 1; |
184 | 425 } |
426 | |
427 return (ret); | |
428 } | |
429 | |
430 | |
186 | 431 char * |
291 | 432 gftp_string_to_utf8 (gftp_request * request, const char *str) |
184 | 433 { |
423 | 434 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; |
184 | 435 gsize bread, bwrite; |
436 GError * error; | |
437 | |
188 | 438 if (request == NULL) |
439 return (NULL); | |
440 | |
184 | 441 if (request->iconv_initialized) |
442 return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
443 &error)); | |
291 | 444 else if (g_utf8_validate (str, -1, NULL)) |
445 return (NULL); | |
184 | 446 |
423 | 447 gftp_lookup_request_option (request, "remote_charsets", &tempstr); |
448 if (*tempstr == '\0') | |
184 | 449 { |
450 error = NULL; | |
451 if ((ret = g_locale_to_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) | |
452 return (ret); | |
453 | |
454 return (NULL); | |
455 } | |
456 | |
423 | 457 remote_charsets = g_strdup (tempstr); |
184 | 458 ret = NULL; |
459 stpos = remote_charsets; | |
423 | 460 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) |
184 | 461 { |
462 if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1) | |
463 continue; | |
464 | |
465 error = NULL; | |
466 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
467 &error)) == NULL) | |
468 { | |
469 g_iconv_close (request->iconv); | |
470 request->iconv = NULL; | |
471 continue; | |
472 } | |
473 else | |
474 { | |
475 request->iconv_initialized = 1; | |
476 break; | |
477 } | |
478 } | |
479 | |
423 | 480 g_free (remote_charsets); |
481 | |
184 | 482 return (ret); |
483 } | |
291 | 484 |
485 | |
486 char * | |
487 gftp_string_from_utf8 (gftp_request * request, const char *str) | |
488 { | |
423 | 489 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; |
291 | 490 gsize bread, bwrite; |
491 GError * error; | |
492 | |
493 if (request == NULL) | |
494 return (NULL); | |
495 | |
496 if (request->iconv_initialized) | |
497 return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
498 &error)); | |
499 else if (g_utf8_validate (str, -1, NULL)) | |
500 return (NULL); | |
501 | |
423 | 502 gftp_lookup_request_option (request, "remote_charsets", &tempstr); |
503 if (*tempstr == '\0') | |
291 | 504 { |
505 error = NULL; | |
506 if ((ret = g_locale_from_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) | |
507 return (ret); | |
508 | |
509 return (NULL); | |
510 } | |
511 | |
423 | 512 remote_charsets = g_strdup (tempstr); |
291 | 513 ret = NULL; |
514 stpos = remote_charsets; | |
423 | 515 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) |
291 | 516 { |
517 if ((request->iconv = g_iconv_open (cur_charset, "UTF-8")) == (GIConv) -1) | |
518 continue; | |
519 | |
520 error = NULL; | |
521 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
522 &error)) == NULL) | |
523 { | |
524 g_iconv_close (request->iconv); | |
525 request->iconv = NULL; | |
526 continue; | |
527 } | |
528 else | |
529 { | |
530 request->iconv_initialized = 1; | |
531 break; | |
532 } | |
533 } | |
534 | |
423 | 535 g_free (remote_charsets); |
536 | |
291 | 537 return (ret); |
538 } | |
539 | |
540 #else | |
541 | |
542 char * | |
543 gftp_string_to_utf8 (gftp_request * request, const char *str) | |
544 { | |
545 return (NULL); | |
546 } | |
547 | |
548 | |
549 char * | |
550 gftp_string_from_utf8 (gftp_request * request, const char *str) | |
551 { | |
552 return (NULL); | |
553 } | |
554 | |
184 | 555 #endif |
556 | |
557 | |
1 | 558 int |
377 | 559 gftp_get_next_file (gftp_request * request, const char *filespec, |
560 gftp_file * fle) | |
1 | 561 { |
58 | 562 int fd, ret; |
1 | 563 |
84 | 564 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 565 |
566 if (request->get_next_file == NULL) | |
84 | 567 return (GFTP_EFATAL); |
1 | 568 |
58 | 569 if (request->cached && request->cachefd > 0) |
1 | 570 fd = request->cachefd; |
571 else | |
572 fd = request->datafd; | |
573 | |
574 memset (fle, 0, sizeof (*fle)); | |
575 do | |
576 { | |
577 gftp_file_destroy (fle); | |
578 ret = request->get_next_file (request, fle, fd); | |
579 | |
291 | 580 if (ret >= 0 && fle->file != NULL) |
184 | 581 fle->utf8_file = gftp_string_to_utf8 (request, fle->file); |
45 | 582 |
60 | 583 if (ret >= 0 && !request->cached && request->cachefd > 0 && |
1 | 584 request->last_dir_entry != NULL) |
585 { | |
168 | 586 if (gftp_fd_write (request, request->last_dir_entry, |
60 | 587 request->last_dir_entry_len, request->cachefd) < 0) |
1 | 588 { |
186 | 589 request->logging_function (gftp_logging_error, request, |
1 | 590 _("Error: Cannot write to cache: %s\n"), |
591 g_strerror (errno)); | |
60 | 592 close (request->cachefd); |
593 request->cachefd = -1; | |
1 | 594 } |
595 } | |
596 } while (ret > 0 && !gftp_match_filespec (fle->file, filespec)); | |
597 | |
598 return (ret); | |
599 } | |
600 | |
601 | |
602 int | |
243 | 603 gftp_parse_bookmark (gftp_request * request, gftp_request * local_request, |
275 | 604 const char * bookmark, int *refresh_local) |
87 | 605 { |
606 gftp_logging_func logging_function; | |
122 | 607 gftp_bookmarks_var * tempentry; |
608 char *default_protocol; | |
173 | 609 int i, init_ret; |
87 | 610 |
611 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
612 g_return_val_if_fail (bookmark != NULL, GFTP_EFATAL); | |
613 | |
614 logging_function = request->logging_function; | |
615 gftp_request_destroy (request, 0); | |
616 request->logging_function = logging_function; | |
617 | |
122 | 618 if ((tempentry = g_hash_table_lookup (gftp_bookmarks_htable, |
619 bookmark)) == NULL) | |
87 | 620 { |
186 | 621 request->logging_function (gftp_logging_error, request, |
87 | 622 _("Error: Could not find bookmark %s\n"), |
623 bookmark); | |
624 return (GFTP_EFATAL); | |
625 } | |
626 else if (tempentry->hostname == NULL || *tempentry->hostname == '\0') | |
627 { | |
186 | 628 request->logging_function (gftp_logging_error, request, |
87 | 629 _("Bookmarks Error: The bookmark entry %s does not have a hostname\n"), bookmark); |
630 return (GFTP_EFATAL); | |
631 } | |
632 | |
633 if (tempentry->user != NULL) | |
634 gftp_set_username (request, tempentry->user); | |
635 | |
636 if (tempentry->pass != NULL) | |
122 | 637 gftp_set_password (request, tempentry->pass); |
87 | 638 |
639 if (tempentry->acct != NULL) | |
640 gftp_set_account (request, tempentry->acct); | |
641 | |
642 gftp_set_hostname (request, tempentry->hostname); | |
643 gftp_set_directory (request, tempentry->remote_dir); | |
644 gftp_set_port (request, tempentry->port); | |
645 | |
516 | 646 if (local_request != NULL && tempentry->local_dir != NULL && |
243 | 647 *tempentry->local_dir != '\0') |
275 | 648 { |
649 gftp_set_directory (local_request, tempentry->local_dir); | |
516 | 650 if (refresh_local != NULL) |
651 *refresh_local = 1; | |
275 | 652 } |
516 | 653 else if (refresh_local != NULL) |
275 | 654 *refresh_local = 0; |
243 | 655 |
87 | 656 for (i = 0; gftp_protocols[i].name; i++) |
657 { | |
658 if (strcmp (gftp_protocols[i].name, tempentry->protocol) == 0) | |
659 { | |
173 | 660 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
661 { | |
662 gftp_request_destroy (request, 0); | |
663 return (init_ret); | |
664 } | |
87 | 665 break; |
666 } | |
667 } | |
668 | |
122 | 669 if (gftp_protocols[i].name == NULL) |
87 | 670 { |
122 | 671 gftp_lookup_request_option (request, "default_protocol", |
672 &default_protocol); | |
673 | |
125 | 674 if (default_protocol != NULL && *default_protocol != '\0') |
87 | 675 { |
122 | 676 for (i = 0; gftp_protocols[i].url_prefix; i++) |
677 { | |
678 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
679 break; | |
680 } | |
87 | 681 } |
682 | |
683 if (gftp_protocols[i].url_prefix == NULL) | |
684 i = GFTP_FTP_NUM; | |
685 } | |
686 | |
199 | 687 gftp_copy_local_options (&request->local_options_vars, |
688 &request->local_options_hash, | |
429 | 689 &request->num_local_options_vars, |
199 | 690 tempentry->local_options_vars, |
691 tempentry->num_local_options_vars); | |
692 | |
173 | 693 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
694 { | |
695 gftp_request_destroy (request, 0); | |
696 return (init_ret); | |
697 } | |
698 | |
87 | 699 return (0); |
700 } | |
701 | |
702 | |
703 int | |
1 | 704 gftp_parse_url (gftp_request * request, const char *url) |
705 { | |
168 | 706 char *pos, *endpos, *endhostpos, tempchar, *default_protocol, *stpos; |
67 | 707 gftp_logging_func logging_function; |
168 | 708 const char *cpos; |
518 | 709 int i, init_ret; |
710 size_t len; | |
1 | 711 |
84 | 712 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
713 g_return_val_if_fail (url != NULL, GFTP_EFATAL); | |
1 | 714 |
67 | 715 logging_function = request->logging_function; |
716 gftp_request_destroy (request, 0); | |
717 request->logging_function = logging_function; | |
718 | |
168 | 719 for (cpos = url; *cpos == ' '; cpos++); |
720 stpos = g_strdup (cpos); | |
721 for (pos = stpos + strlen (stpos) - 1; *pos == ' '; pos--) | |
722 *pos = '\0'; | |
1 | 723 |
122 | 724 i = GFTP_FTP_NUM; |
725 | |
1 | 726 if ((pos = strstr (stpos, "://")) != NULL) |
727 { | |
728 *pos = '\0'; | |
729 | |
730 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
731 { | |
732 if (strcmp (gftp_protocols[i].url_prefix, stpos) == 0) | |
733 break; | |
734 } | |
735 | |
736 if (gftp_protocols[i].url_prefix == NULL) | |
168 | 737 { |
173 | 738 request->logging_function (gftp_logging_misc, NULL, |
739 _("The protocol '%s' is currently not supported.\n"), | |
740 stpos); | |
168 | 741 g_free (stpos); |
742 return (GFTP_EFATAL); | |
743 } | |
173 | 744 |
745 *pos = ':'; | |
1 | 746 } |
747 else | |
748 { | |
122 | 749 gftp_lookup_request_option (request, "default_protocol", |
750 &default_protocol); | |
751 | |
125 | 752 if (default_protocol != NULL && *default_protocol != '\0') |
1 | 753 { |
122 | 754 for (i = 0; gftp_protocols[i].url_prefix; i++) |
755 { | |
756 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
757 break; | |
758 } | |
1 | 759 } |
122 | 760 } |
1 | 761 |
122 | 762 if (gftp_protocols[i].url_prefix == NULL) |
763 i = GFTP_FTP_NUM; | |
1 | 764 |
173 | 765 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
766 { | |
767 gftp_request_destroy (request, 0); | |
768 return (init_ret); | |
769 } | |
1 | 770 |
771 if (request->parse_url != NULL) | |
168 | 772 { |
773 g_free (stpos); | |
774 return (request->parse_url (request, url)); | |
775 } | |
776 | |
777 pos = stpos; | |
778 len = strlen (request->url_prefix); | |
779 if (strncmp (pos, request->url_prefix, len) == 0 && | |
780 strncmp (pos + len, "://", 3) == 0) | |
781 pos += len + 3; | |
1 | 782 |
783 if (i == GFTP_LOCAL_NUM) | |
784 { | |
367 | 785 for (; *pos == ' ' || *pos == '\t'; pos++); |
168 | 786 request->directory = g_strdup (pos); |
787 g_free (stpos); | |
1 | 788 return (0); |
789 } | |
790 | |
182 | 791 if ((endhostpos = strrchr (pos, '@')) != NULL) |
1 | 792 { |
793 /* A user/password was entered */ | |
794 if ((endpos = strchr (pos, ':')) == NULL || endhostpos < endpos) | |
168 | 795 { |
796 /* No password was entered */ | |
797 gftp_set_password (request, ""); | |
798 endpos = endhostpos; | |
799 } | |
1 | 800 |
801 *endpos = '\0'; | |
802 gftp_set_username (request, pos); | |
803 | |
804 pos = endpos + 1; | |
805 if ((endpos = strchr (pos, '@')) != NULL) | |
168 | 806 { |
807 if (strchr (endpos + 1, '@') != NULL) | |
808 endpos = strchr (endpos + 1, '@'); | |
809 *endpos = '\0'; | |
810 gftp_set_password (request, pos); | |
1 | 811 |
168 | 812 pos = endpos + 1; |
813 } | |
1 | 814 } |
815 | |
816 /* Now get the hostname and an optional port and optional directory */ | |
817 endhostpos = pos + strlen (pos); | |
818 if ((endpos = strchr (pos, ':')) != NULL) | |
819 endhostpos = endpos; | |
820 else if ((endpos = strchr (pos, '/')) != NULL) | |
821 endhostpos = endpos; | |
822 tempchar = *endhostpos; | |
823 *endhostpos = '\0'; | |
824 gftp_set_hostname (request, pos); | |
825 *endhostpos = tempchar; | |
826 | |
827 if ((endpos = strchr (pos, ':')) != NULL) | |
828 { | |
829 /* A port was entered */ | |
830 pos = endpos + 1; | |
831 gftp_set_port (request, strtol (pos, NULL, 10)); | |
832 } | |
833 if ((endpos = strchr (pos, '/')) != NULL) | |
834 gftp_set_directory (request, endpos); | |
168 | 835 g_free (stpos); |
1 | 836 return (0); |
837 } | |
838 | |
839 | |
840 void | |
841 gftp_set_hostname (gftp_request * request, const char *hostname) | |
842 { | |
843 g_return_if_fail (request != NULL); | |
844 g_return_if_fail (hostname != NULL); | |
845 | |
846 if (request->hostname) | |
847 g_free (request->hostname); | |
124 | 848 request->hostname = g_strdup (hostname); |
1 | 849 } |
850 | |
851 | |
852 void | |
853 gftp_set_username (gftp_request * request, const char *username) | |
854 { | |
855 g_return_if_fail (request != NULL); | |
856 | |
857 if (request->username) | |
858 g_free (request->username); | |
169 | 859 |
860 if (username != NULL) | |
861 request->username = g_strdup (username); | |
862 else | |
863 request->username = NULL; | |
1 | 864 } |
865 | |
866 | |
867 void | |
868 gftp_set_password (gftp_request * request, const char *password) | |
869 { | |
870 g_return_if_fail (request != NULL); | |
871 | |
872 if (request->password) | |
873 g_free (request->password); | |
169 | 874 |
875 if (password != NULL) | |
876 request->password = g_strdup (password); | |
877 else | |
878 request->password = NULL; | |
1 | 879 } |
880 | |
881 | |
882 void | |
883 gftp_set_account (gftp_request * request, const char *account) | |
884 { | |
885 g_return_if_fail (request != NULL); | |
886 g_return_if_fail (account != NULL); | |
887 | |
888 if (request->account) | |
889 g_free (request->account); | |
124 | 890 request->account = g_strdup (account); |
1 | 891 } |
892 | |
893 | |
894 int | |
895 gftp_set_directory (gftp_request * request, const char *directory) | |
896 { | |
84 | 897 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
898 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
1 | 899 |
900 | |
169 | 901 if (request->datafd <= 0 && !request->always_connected) |
1 | 902 { |
903 if (directory != request->directory) | |
168 | 904 { |
905 if (request->directory) | |
906 g_free (request->directory); | |
907 request->directory = g_strdup (directory); | |
908 } | |
1 | 909 return (0); |
910 } | |
911 else if (request->chdir == NULL) | |
84 | 912 return (GFTP_EFATAL); |
1 | 913 return (request->chdir (request, directory)); |
914 } | |
915 | |
916 | |
917 void | |
918 gftp_set_port (gftp_request * request, unsigned int port) | |
919 { | |
920 g_return_if_fail (request != NULL); | |
921 | |
922 request->port = port; | |
923 } | |
924 | |
925 | |
926 int | |
927 gftp_remove_directory (gftp_request * request, const char *directory) | |
928 { | |
84 | 929 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 930 |
931 if (request->rmdir == NULL) | |
84 | 932 return (GFTP_EFATAL); |
1 | 933 return (request->rmdir (request, directory)); |
934 } | |
935 | |
936 | |
937 int | |
938 gftp_remove_file (gftp_request * request, const char *file) | |
939 { | |
84 | 940 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 941 |
942 if (request->rmfile == NULL) | |
84 | 943 return (GFTP_EFATAL); |
1 | 944 return (request->rmfile (request, file)); |
945 } | |
946 | |
947 | |
948 int | |
949 gftp_make_directory (gftp_request * request, const char *directory) | |
950 { | |
291 | 951 char *utf8; |
952 int ret; | |
953 | |
84 | 954 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 955 |
956 if (request->mkdir == NULL) | |
84 | 957 return (GFTP_EFATAL); |
291 | 958 |
959 utf8 = gftp_string_from_utf8 (request, directory); | |
960 if (utf8 != NULL) | |
961 { | |
962 ret = request->mkdir (request, utf8); | |
963 g_free (utf8); | |
964 } | |
965 else | |
966 ret = request->mkdir (request, directory); | |
967 | |
968 return (ret); | |
1 | 969 } |
970 | |
971 | |
972 int | |
973 gftp_rename_file (gftp_request * request, const char *oldname, | |
168 | 974 const char *newname) |
1 | 975 { |
291 | 976 char *utf8; |
977 int ret; | |
978 | |
84 | 979 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 980 |
981 if (request->rename == NULL) | |
84 | 982 return (GFTP_EFATAL); |
291 | 983 |
984 utf8 = gftp_string_from_utf8 (request, newname); | |
985 if (utf8 != NULL) | |
986 { | |
987 ret = request->rename (request, oldname, utf8); | |
988 g_free (utf8); | |
989 } | |
990 else | |
991 ret = request->rename (request, oldname, newname); | |
992 | |
993 return (ret); | |
1 | 994 } |
995 | |
996 | |
997 int | |
499 | 998 gftp_chmod (gftp_request * request, const char *file, mode_t mode) |
1 | 999 { |
84 | 1000 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 1001 |
1002 if (request->chmod == NULL) | |
84 | 1003 return (GFTP_EFATAL); |
504 | 1004 |
1005 mode &= S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX; | |
1 | 1006 return (request->chmod (request, file, mode)); |
1007 } | |
1008 | |
1009 | |
1010 int | |
1011 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
1012 { | |
84 | 1013 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 1014 |
1015 if (request->set_file_time == NULL) | |
84 | 1016 return (GFTP_EFATAL); |
1 | 1017 return (request->set_file_time (request, file, datetime)); |
1018 } | |
1019 | |
1020 | |
1021 char | |
478 | 1022 gftp_site_cmd (gftp_request * request, int specify_site, const char *command) |
1 | 1023 { |
84 | 1024 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 1025 |
1026 if (request->site == NULL) | |
84 | 1027 return (GFTP_EFATAL); |
478 | 1028 return (request->site (request, specify_site, command)); |
1 | 1029 } |
1030 | |
1031 | |
58 | 1032 off_t |
1 | 1033 gftp_get_file_size (gftp_request * request, const char *filename) |
1034 { | |
1035 g_return_val_if_fail (request != NULL, 0); | |
1036 | |
1037 if (request->get_file_size == NULL) | |
1038 return (0); | |
1039 return (request->get_file_size (request, filename)); | |
1040 } | |
1041 | |
1042 | |
122 | 1043 static int |
1044 gftp_need_proxy (gftp_request * request, char *service, char *proxy_hostname, | |
518 | 1045 unsigned int proxy_port) |
1 | 1046 { |
122 | 1047 gftp_config_list_vars * proxy_hosts; |
1 | 1048 gftp_proxy_hosts * hostname; |
460 | 1049 size_t hostlen, domlen; |
1 | 1050 unsigned char addy[4]; |
1051 struct sockaddr *addr; | |
1052 GList * templist; | |
1053 gint32 netaddr; | |
1054 char *pos; | |
1055 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
1056 struct addrinfo hints; | |
516 | 1057 unsigned int port; |
1058 int errnum; | |
1 | 1059 char serv[8]; |
122 | 1060 #endif |
1061 | |
218 | 1062 gftp_lookup_global_option ("dont_use_proxy", &proxy_hosts); |
122 | 1063 |
1064 if (proxy_hostname == NULL || *proxy_hostname == '\0') | |
1065 return (0); | |
1066 else if (proxy_hosts->list == NULL) | |
1067 return (proxy_hostname != NULL && | |
1068 *proxy_hostname != '\0'); | |
1 | 1069 |
1070 request->hostp = NULL; | |
122 | 1071 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
151 | 1072 request->free_hostp = 1; |
1 | 1073 memset (&hints, 0, sizeof (hints)); |
1074 hints.ai_flags = AI_CANONNAME; | |
146 | 1075 hints.ai_family = PF_UNSPEC; |
1 | 1076 hints.ai_socktype = SOCK_STREAM; |
1077 | |
122 | 1078 port = request->use_proxy ? proxy_port : request->port; |
1 | 1079 if (port == 0) |
1080 strcpy (serv, service); | |
1081 else | |
1082 snprintf (serv, sizeof (serv), "%d", port); | |
1083 | |
186 | 1084 request->logging_function (gftp_logging_misc, request, |
1 | 1085 _("Looking up %s\n"), request->hostname); |
1086 | |
1087 if ((errnum = getaddrinfo (request->hostname, serv, &hints, | |
1088 &request->hostp)) != 0) | |
1089 { | |
186 | 1090 request->logging_function (gftp_logging_error, request, |
1 | 1091 _("Cannot look up hostname %s: %s\n"), |
1092 request->hostname, gai_strerror (errnum)); | |
84 | 1093 return (GFTP_ERETRYABLE); |
1 | 1094 } |
1095 | |
1096 addr = request->hostp->ai_addr; | |
1097 | |
1098 #else /* !HAVE_GETADDRINFO */ | |
186 | 1099 request->logging_function (gftp_logging_misc, request, |
1 | 1100 _("Looking up %s\n"), request->hostname); |
1101 | |
1102 if (!(request->hostp = r_gethostbyname (request->hostname, &request->host, | |
1103 NULL))) | |
1104 { | |
186 | 1105 request->logging_function (gftp_logging_error, request, |
1 | 1106 _("Cannot look up hostname %s: %s\n"), |
1107 request->hostname, g_strerror (errno)); | |
84 | 1108 return (GFTP_ERETRYABLE); |
1 | 1109 } |
1110 | |
1111 addr = (struct sockaddr *) request->host.h_addr_list[0]; | |
1112 | |
1113 #endif /* HAVE_GETADDRINFO */ | |
1114 | |
122 | 1115 templist = proxy_hosts->list; |
1 | 1116 while (templist != NULL) |
1117 { | |
1118 hostname = templist->data; | |
460 | 1119 if (hostname->domain != NULL) |
168 | 1120 { |
460 | 1121 hostlen = strlen (request->hostname); |
1122 domlen = strlen (hostname->domain); | |
1123 if (hostlen > domlen) | |
1124 { | |
1125 pos = request->hostname + hostlen - domlen; | |
1126 if (strcmp (hostname->domain, pos) == 0) | |
1127 return (0); | |
1128 } | |
168 | 1129 } |
1 | 1130 |
1131 if (hostname->ipv4_network_address != 0) | |
168 | 1132 { |
516 | 1133 memcpy (addy, addr, sizeof (*addy)); |
168 | 1134 netaddr = |
1135 (((addy[0] & 0xff) << 24) | ((addy[1] & 0xff) << 16) | | |
1136 ((addy[2] & 0xff) << 8) | (addy[3] & 0xff)) & | |
1 | 1137 hostname->ipv4_netmask; |
168 | 1138 if (netaddr == hostname->ipv4_network_address) |
1139 return (0); | |
1140 } | |
1 | 1141 templist = templist->next; |
1142 } | |
1143 | |
122 | 1144 return (proxy_hostname != NULL && *proxy_hostname != '\0'); |
1 | 1145 } |
1146 | |
1147 | |
48 | 1148 static char * |
1149 copy_token (char **dest, char *source) | |
1 | 1150 { |
48 | 1151 /* This function is used internally by gftp_parse_ls () */ |
1152 char *endpos, savepos; | |
1 | 1153 |
48 | 1154 endpos = source; |
1155 while (*endpos != ' ' && *endpos != '\t' && *endpos != '\0') | |
1156 endpos++; | |
1157 if (*endpos == '\0') | |
1158 return (NULL); | |
1 | 1159 |
48 | 1160 savepos = *endpos; |
1161 *endpos = '\0'; | |
1162 *dest = g_malloc (endpos - source + 1); | |
1163 strcpy (*dest, source); | |
1164 *endpos = savepos; | |
1 | 1165 |
48 | 1166 /* Skip the blanks till we get to the next entry */ |
1167 source = endpos + 1; | |
1168 while ((*source == ' ' || *source == '\t') && *source != '\0') | |
1169 source++; | |
1170 return (source); | |
1171 } | |
1 | 1172 |
1173 | |
48 | 1174 static char * |
1175 goto_next_token (char *pos) | |
1176 { | |
1177 while (*pos != ' ' && *pos != '\t' && *pos != '\0') | |
1178 pos++; | |
1 | 1179 |
516 | 1180 while (*pos == ' ' || *pos == '\t') |
48 | 1181 pos++; |
1182 | |
1183 return (pos); | |
1 | 1184 } |
1185 | |
1186 | |
485 | 1187 static time_t |
1188 parse_vms_time (char *str, char **endpos) | |
1189 { | |
1190 struct tm curtime; | |
1191 time_t ret; | |
1192 | |
1193 /* 8-JUN-2004 13:04:14 */ | |
1194 memset (&curtime, 0, sizeof (curtime)); | |
1195 | |
1196 *endpos = strptime (str, "%d-%b-%Y %H:%M:%S", &curtime); | |
1197 if (*endpos == NULL) | |
1198 *endpos = strptime (str, "%d-%b-%Y %H:%M", &curtime); | |
1199 | |
1200 if (*endpos != NULL) | |
1201 { | |
1202 ret = mktime (&curtime); | |
1203 for (; **endpos == ' ' || **endpos == '\t'; (*endpos)++); | |
1204 } | |
1205 else | |
1206 { | |
1207 ret = 0; | |
1208 *endpos = goto_next_token (str); | |
1209 if (*endpos != NULL) | |
1210 *endpos = goto_next_token (*endpos); | |
1211 } | |
1212 | |
1213 return (ret); | |
1214 } | |
1215 | |
1216 | |
102 | 1217 time_t |
1218 parse_time (char *str, char **endpos) | |
1 | 1219 { |
102 | 1220 struct tm curtime, *loctime; |
105 | 1221 time_t t, ret; |
102 | 1222 char *tmppos; |
460 | 1223 size_t slen; |
260 | 1224 int i, num; |
1 | 1225 |
460 | 1226 slen = strlen (str); |
102 | 1227 memset (&curtime, 0, sizeof (curtime)); |
1228 curtime.tm_isdst = -1; | |
460 | 1229 |
1230 if (slen > 4 && isdigit ((int) str[0]) && str[2] == '-' && | |
260 | 1231 isdigit ((int) str[3])) |
1 | 1232 { |
1233 /* This is how DOS will return the date/time */ | |
1234 /* 07-06-99 12:57PM */ | |
1235 | |
102 | 1236 tmppos = strptime (str, "%m-%d-%y %I:%M%p", &curtime); |
1 | 1237 } |
460 | 1238 else if (slen > 4 && isdigit ((int) str[0]) && str[2] == '-' && |
260 | 1239 isalpha (str[3])) |
105 | 1240 { |
1241 /* 10-Jan-2003 09:14 */ | |
1242 tmppos = strptime (str, "%d-%h-%Y %H:%M", &curtime); | |
1243 } | |
460 | 1244 else if (slen > 4 && isdigit ((int) str[0]) && str[4] == '/') |
358 | 1245 { |
1246 /* 2003/12/25 */ | |
1247 tmppos = strptime (str, "%Y/%m/%d", &curtime); | |
1248 } | |
1 | 1249 else |
1250 { | |
1251 /* This is how most UNIX, Novell, and MacOS ftp servers send their time */ | |
102 | 1252 /* Jul 06 12:57 or Jul 6 1999 */ |
1 | 1253 |
102 | 1254 if (strchr (str, ':') != NULL) |
1255 { | |
1256 tmppos = strptime (str, "%h %d %H:%M", &curtime); | |
1257 t = time (NULL); | |
1258 loctime = localtime (&t); | |
359 | 1259 |
1260 if (curtime.tm_mon > loctime->tm_mon) | |
1261 curtime.tm_year = loctime->tm_year - 1; | |
1262 else | |
1263 curtime.tm_year = loctime->tm_year; | |
102 | 1264 } |
1265 else | |
1266 tmppos = strptime (str, "%h %d %Y", &curtime); | |
1267 } | |
1 | 1268 |
105 | 1269 if (tmppos != NULL) |
1270 ret = mktime (&curtime); | |
1271 else | |
1272 ret = 0; | |
1273 | |
102 | 1274 if (endpos != NULL) |
105 | 1275 { |
1276 if (tmppos == NULL) | |
1277 { | |
260 | 1278 /* We cannot parse this date format. So, just skip this date field |
1279 and continue to the next token. This is mainly for the HTTP | |
1280 support */ | |
1281 | |
1282 *endpos = str; | |
1283 for (num = 0; num < 2 && **endpos != '\0'; num++) | |
1284 { | |
1285 for (i=0; | |
1286 (*endpos)[i] != ' ' && (*endpos)[i] != '\t' && | |
1287 (*endpos)[i] != '\0'; | |
1288 i++); | |
1289 *endpos += i; | |
1290 | |
1291 for (i=0; (*endpos)[i] == ' ' || (*endpos)[i] == '\t'; i++); | |
1292 *endpos += i; | |
1293 } | |
105 | 1294 } |
1295 else | |
1296 *endpos = tmppos; | |
1297 } | |
1 | 1298 |
281 | 1299 return (ret); |
1 | 1300 } |
1301 | |
1302 | |
499 | 1303 static mode_t |
1304 gftp_parse_vms_attribs (char **src, mode_t mask) | |
107 | 1305 { |
1306 char *endpos; | |
499 | 1307 mode_t ret; |
107 | 1308 |
1309 if (*src == NULL) | |
499 | 1310 return (0); |
107 | 1311 |
1312 if ((endpos = strchr (*src, ',')) != NULL) | |
1313 *endpos = '\0'; | |
1314 | |
499 | 1315 ret = 0; |
107 | 1316 if (strchr (*src, 'R') != NULL) |
499 | 1317 ret |= S_IRUSR | S_IRGRP | S_IROTH; |
107 | 1318 if (strchr (*src, 'W') != NULL) |
499 | 1319 ret |= S_IWUSR | S_IWGRP | S_IWOTH; |
107 | 1320 if (strchr (*src, 'E') != NULL) |
499 | 1321 ret |= S_IXUSR | S_IXGRP | S_IXOTH; |
107 | 1322 |
1323 *src = endpos + 1; | |
499 | 1324 |
1325 return (ret & mask); | |
107 | 1326 } |
1327 | |
1328 | |
1329 static int | |
485 | 1330 gftp_parse_ls_vms (gftp_request * request, int fd, char *str, gftp_file * fle) |
107 | 1331 { |
485 | 1332 char *curpos, *endpos, tempstr[1024]; |
1333 int multiline; | |
1334 ssize_t len; | |
107 | 1335 |
1336 /* .PINE-DEBUG1;1 9 21-AUG-2002 20:06 [MYERSRG] (RWED,RWED,,) */ | |
1337 /* WWW.DIR;1 1 23-NOV-1999 05:47 [MYERSRG] (RWE,RWE,RE,E) */ | |
1338 | |
485 | 1339 /* Multiline VMS |
1340 $MAIN.TPU$JOURNAL;1 | |
1341 1/18 8-JUN-2004 13:04:14 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
1342 TCPIP$FTP_SERVER.LOG;29 | |
1343 0/18 8-JUN-2004 14:42:04 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
1344 TCPIP$FTP_SERVER.LOG;28 | |
1345 5/18 8-JUN-2004 13:05:11 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
1346 TCPIP$FTP_SERVER.LOG;27 | |
1347 5/18 8-JUN-2004 13:03:51 [NUCLEAR,FISSION] (RWED,RWED,RE,) */ | |
1348 | |
107 | 1349 if ((curpos = strchr (str, ';')) == NULL) |
1350 return (GFTP_EFATAL); | |
1351 | |
485 | 1352 multiline = strchr (str, ' ') == NULL; |
1353 | |
107 | 1354 *curpos = '\0'; |
1355 if (strlen (str) > 4 && strcmp (curpos - 4, ".DIR") == 0) | |
1356 { | |
499 | 1357 fle->st_mode |= S_IFDIR; |
107 | 1358 *(curpos - 4) = '\0'; |
1359 } | |
1360 | |
1361 fle->file = g_strdup (str); | |
1362 | |
485 | 1363 if (multiline) |
1364 { | |
1365 if (request->get_next_dirlist_line == NULL) | |
1366 return (GFTP_EFATAL); | |
1367 | |
1368 len = request->get_next_dirlist_line (request, fd, tempstr, | |
1369 sizeof (tempstr)); | |
1370 if (len <= 0) | |
1371 return ((int) len); | |
1372 | |
1373 for (curpos = tempstr; *curpos == ' ' || *curpos == '\t'; curpos++); | |
1374 } | |
1375 else | |
1376 curpos = goto_next_token (curpos + 1); | |
1377 | |
244 | 1378 fle->size = gftp_parse_file_size (curpos) * 512; /* Is this correct? */ |
107 | 1379 |
1380 curpos = goto_next_token (curpos); | |
1381 | |
485 | 1382 fle->datetime = parse_vms_time (curpos, &curpos); |
1383 | |
107 | 1384 if (*curpos != '[') |
1385 return (GFTP_EFATAL); | |
485 | 1386 |
107 | 1387 if ((endpos = strchr (curpos, ']')) == NULL) |
1388 return (GFTP_EFATAL); | |
1389 | |
1390 curpos = goto_next_token (endpos + 1); | |
1391 if ((curpos = strchr (curpos, ',')) == NULL) | |
1392 return (0); | |
1393 curpos++; | |
1394 | |
499 | 1395 fle->st_mode = gftp_parse_vms_attribs (&curpos, S_IRWXU); |
1396 fle->st_mode |= gftp_parse_vms_attribs (&curpos, S_IRWXG); | |
1397 fle->st_mode |= gftp_parse_vms_attribs (&curpos, S_IRWXO); | |
107 | 1398 |
485 | 1399 fle->user = g_strdup (""); |
1400 fle->group = g_strdup (""); | |
1401 | |
107 | 1402 return (0); |
1403 } | |
358 | 1404 |
1405 | |
1406 static int | |
1407 gftp_parse_ls_mvs (char *str, gftp_file * fle) | |
1408 { | |
1409 char *curpos; | |
1410 | |
1411 /* Volume Unit Referred Ext Used Recfm Lrecl BlkSz Dsorg Dsname */ | |
1412 /* SVI52A 3390 2003/12/10 8 216 FB 80 27920 PS CARDS.DELETES */ | |
1413 /* SVI528 3390 2003/12/12 1 5 FB 80 24000 PO CLIST */ | |
1414 | |
1415 curpos = goto_next_token (str + 1); | |
1416 if (curpos == NULL) | |
1417 return (GFTP_EFATAL); | |
1418 | |
1419 curpos = goto_next_token (curpos + 1); | |
1420 if (curpos == NULL) | |
1421 return (GFTP_EFATAL); | |
1422 | |
479 | 1423 fle->datetime = parse_time (curpos, &curpos); |
1424 | |
1425 curpos = goto_next_token (curpos); | |
1426 if (curpos == NULL) | |
358 | 1427 return (GFTP_EFATAL); |
1428 | |
1429 curpos = goto_next_token (curpos + 1); | |
1430 if (curpos == NULL) | |
1431 return (GFTP_EFATAL); | |
1432 | |
1433 fle->size = gftp_parse_file_size (curpos) * 55996; | |
1434 curpos = goto_next_token (curpos + 1); | |
1435 if (curpos == NULL) | |
1436 return (GFTP_EFATAL); | |
1437 | |
1438 curpos = goto_next_token (curpos + 1); | |
1439 if (curpos == NULL) | |
1440 return (GFTP_EFATAL); | |
1441 | |
1442 curpos = goto_next_token (curpos + 1); | |
1443 if (curpos == NULL) | |
1444 return (GFTP_EFATAL); | |
1445 | |
1446 curpos = goto_next_token (curpos + 1); | |
1447 if (curpos == NULL) | |
1448 return (GFTP_EFATAL); | |
1449 | |
1450 if (strncmp (curpos, "PS", 2) == 0) | |
499 | 1451 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
358 | 1452 else if (strncmp (curpos, "PO", 2) == 0) |
499 | 1453 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
358 | 1454 else |
1455 return (GFTP_EFATAL); | |
1456 | |
1457 curpos = goto_next_token (curpos + 1); | |
1458 | |
1459 fle->user = g_strdup (_("unknown")); | |
1460 fle->group = g_strdup (_("unknown")); | |
1461 fle->file = g_strdup (curpos); | |
1462 | |
1463 return (0); | |
1464 } | |
107 | 1465 |
1466 | |
1 | 1467 static int |
1468 gftp_parse_ls_eplf (char *str, gftp_file * fle) | |
1469 { | |
1470 char *startpos; | |
358 | 1471 int isdir = 0; |
1 | 1472 |
1473 startpos = str; | |
1474 while (startpos) | |
1475 { | |
1476 startpos++; | |
1477 switch (*startpos) | |
168 | 1478 { |
516 | 1479 case '/': |
1480 isdir = 1; | |
1481 break; | |
1482 case 's': | |
1483 fle->size = gftp_parse_file_size (startpos + 1); | |
1484 break; | |
1485 case 'm': | |
1486 fle->datetime = strtol (startpos + 1, NULL, 10); | |
1487 break; | |
168 | 1488 } |
1 | 1489 startpos = strchr (startpos, ','); |
1490 } | |
358 | 1491 |
1 | 1492 if ((startpos = strchr (str, 9)) == NULL) |
84 | 1493 return (GFTP_EFATAL); |
358 | 1494 |
1495 if (isdir) | |
499 | 1496 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
358 | 1497 else |
499 | 1498 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
358 | 1499 |
124 | 1500 fle->file = g_strdup (startpos + 1); |
1501 fle->user = g_strdup (_("unknown")); | |
1502 fle->group = g_strdup (_("unknown")); | |
1 | 1503 return (0); |
1504 } | |
1505 | |
1506 | |
1507 static int | |
460 | 1508 gftp_parse_ls_unix (gftp_request * request, char *str, size_t slen, |
1509 gftp_file * fle) | |
1 | 1510 { |
499 | 1511 char *endpos, *startpos, *pos, *attribs; |
91 | 1512 int cols; |
1513 | |
1514 /* If there is no space between the attribs and links field, just make one */ | |
460 | 1515 if (slen > 10) |
91 | 1516 str[10] = ' '; |
1517 | |
1518 /* Determine the number of columns */ | |
1519 cols = 0; | |
1520 pos = str; | |
1521 while (*pos != '\0') | |
1522 { | |
1523 while (*pos != '\0' && *pos != ' ' && *pos != '\t') | |
1524 { | |
1525 if (*pos == ':') | |
1526 break; | |
1527 pos++; | |
1528 } | |
1529 | |
1530 cols++; | |
1531 | |
1532 if (*pos == ':') | |
1533 { | |
1534 cols++; | |
1535 break; | |
1536 } | |
1537 | |
1538 while (*pos == ' ' || *pos == '\t') | |
1539 pos++; | |
1540 } | |
1 | 1541 |
1542 startpos = str; | |
1543 /* Copy file attributes */ | |
499 | 1544 if ((startpos = copy_token (&attribs, startpos)) == NULL) |
84 | 1545 return (GFTP_EFATAL); |
1 | 1546 |
499 | 1547 fle->st_mode = gftp_convert_attributes_to_mode_t (attribs); |
1548 g_free (attribs); | |
1549 | |
1 | 1550 if (cols >= 9) |
1551 { | |
1552 /* Skip the number of links */ | |
1553 startpos = goto_next_token (startpos); | |
1554 | |
1555 /* Copy the user that owns this file */ | |
1556 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
168 | 1557 return (GFTP_EFATAL); |
1 | 1558 |
1559 /* Copy the group that owns this file */ | |
1560 if ((startpos = copy_token (&fle->group, startpos)) == NULL) | |
168 | 1561 return (GFTP_EFATAL); |
1 | 1562 } |
1563 else | |
1564 { | |
124 | 1565 fle->group = g_strdup (_("unknown")); |
1 | 1566 if (cols == 8) |
168 | 1567 { |
1568 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
1569 return (GFTP_EFATAL); | |
1570 } | |
1 | 1571 else |
124 | 1572 fle->user = g_strdup (_("unknown")); |
1 | 1573 startpos = goto_next_token (startpos); |
1574 } | |
1575 | |
281 | 1576 if (request->server_type == GFTP_DIRTYPE_CRAY) |
1 | 1577 { |
91 | 1578 /* See if this is a Cray directory listing. It has the following format: |
1579 drwx------ 2 feiliu g913 DK common 4096 Sep 24 2001 wv */ | |
1580 if (cols == 11 && strstr (str, "->") == NULL) | |
1581 { | |
1582 startpos = goto_next_token (startpos); | |
1583 startpos = goto_next_token (startpos); | |
1584 } | |
1 | 1585 } |
1586 | |
1587 /* See if this is a block or character device. We will store the major number | |
1588 in the high word and the minor number in the low word. */ | |
499 | 1589 if (GFTP_IS_SPECIAL_DEVICE (fle->st_mode) && |
1590 (endpos = strchr (startpos, ',')) != NULL) | |
1 | 1591 { |
1592 fle->size = strtol (startpos, NULL, 10) << 16; | |
1593 | |
1594 startpos = endpos + 1; | |
1595 while (*startpos == ' ') | |
168 | 1596 startpos++; |
1 | 1597 |
1598 /* Get the minor number */ | |
1599 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1600 return (GFTP_EFATAL); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1601 fle->size |= strtol (startpos, NULL, 10) & 0xFF; |
1 | 1602 } |
1603 else | |
1604 { | |
1605 /* This is a regular file */ | |
1606 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1607 return (GFTP_EFATAL); |
244 | 1608 fle->size = gftp_parse_file_size (startpos); |
1 | 1609 } |
1610 | |
1611 /* Skip the blanks till we get to the next entry */ | |
1612 startpos = endpos + 1; | |
1613 while (*startpos == ' ') | |
1614 startpos++; | |
1615 | |
479 | 1616 fle->datetime = parse_time (startpos, &startpos); |
1 | 1617 |
1618 /* Skip the blanks till we get to the next entry */ | |
1619 startpos = goto_next_token (startpos); | |
1620 | |
1621 /* Parse the filename. If this file is a symbolic link, remove the -> part */ | |
499 | 1622 if (S_ISLNK (fle->st_mode) && ((endpos = strstr (startpos, "->")) != NULL)) |
1 | 1623 *(endpos - 1) = '\0'; |
1624 | |
124 | 1625 fle->file = g_strdup (startpos); |
1 | 1626 |
1627 /* Uncomment this if you want to strip the spaces off of the end of the file. | |
1628 I don't want to do this by default since there are valid filenames with | |
1629 spaces at the end of them. Some broken FTP servers like the Paradyne IPC | |
1630 DSLAMS append a bunch of spaces at the end of the file. | |
1631 for (endpos = fle->file + strlen (fle->file) - 1; | |
1632 *endpos == ' '; | |
1633 *endpos-- = '\0'); | |
1634 */ | |
1635 | |
1636 return (0); | |
1637 } | |
1638 | |
1639 | |
1640 static int | |
1641 gftp_parse_ls_nt (char *str, gftp_file * fle) | |
1642 { | |
1643 char *startpos; | |
1644 | |
1645 startpos = str; | |
479 | 1646 fle->datetime = parse_time (startpos, &startpos); |
1 | 1647 |
124 | 1648 fle->user = g_strdup (_("unknown")); |
1649 fle->group = g_strdup (_("unknown")); | |
1 | 1650 |
1651 startpos = goto_next_token (startpos); | |
499 | 1652 |
1 | 1653 if (startpos[0] == '<') |
499 | 1654 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
1 | 1655 else |
1656 { | |
499 | 1657 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
244 | 1658 fle->size = gftp_parse_file_size (startpos); |
1 | 1659 } |
124 | 1660 |
1 | 1661 startpos = goto_next_token (startpos); |
124 | 1662 fle->file = g_strdup (startpos); |
1 | 1663 return (0); |
1664 } | |
1665 | |
1666 | |
1667 static int | |
1668 gftp_parse_ls_novell (char *str, gftp_file * fle) | |
1669 { | |
1670 char *startpos; | |
1671 | |
1672 if (str[12] != ' ') | |
84 | 1673 return (GFTP_EFATAL); |
499 | 1674 |
1 | 1675 str[12] = '\0'; |
499 | 1676 fle->st_mode = gftp_convert_attributes_to_mode_t (str); |
1 | 1677 startpos = str + 13; |
1678 | |
1679 while ((*startpos == ' ' || *startpos == '\t') && *startpos != '\0') | |
1680 startpos++; | |
1681 | |
1682 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
84 | 1683 return (GFTP_EFATAL); |
1 | 1684 |
124 | 1685 fle->group = g_strdup (_("unknown")); |
1 | 1686 |
244 | 1687 fle->size = gftp_parse_file_size (startpos); |
1 | 1688 |
1689 startpos = goto_next_token (startpos); | |
479 | 1690 fle->datetime = parse_time (startpos, &startpos); |
1 | 1691 |
1692 startpos = goto_next_token (startpos); | |
124 | 1693 fle->file = g_strdup (startpos); |
1 | 1694 return (0); |
1695 } | |
1696 | |
1697 | |
48 | 1698 int |
485 | 1699 gftp_parse_ls (gftp_request * request, const char *lsoutput, gftp_file * fle, |
1700 int fd) | |
1 | 1701 { |
107 | 1702 char *str, *endpos, tmpchar; |
1703 int result, is_vms; | |
91 | 1704 size_t len; |
48 | 1705 |
84 | 1706 g_return_val_if_fail (lsoutput != NULL, GFTP_EFATAL); |
1707 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
48 | 1708 |
124 | 1709 str = g_strdup (lsoutput); |
48 | 1710 memset (fle, 0, sizeof (*fle)); |
1 | 1711 |
91 | 1712 len = strlen (str); |
107 | 1713 if (len > 0 && str[len - 1] == '\n') |
91 | 1714 str[--len] = '\0'; |
1715 if (len > 0 && str[len - 1] == '\r') | |
1716 str[--len] = '\0'; | |
39 | 1717 |
91 | 1718 switch (request->server_type) |
1719 { | |
122 | 1720 case GFTP_DIRTYPE_CRAY: |
1721 case GFTP_DIRTYPE_UNIX: | |
460 | 1722 result = gftp_parse_ls_unix (request, str, len, fle); |
91 | 1723 break; |
122 | 1724 case GFTP_DIRTYPE_EPLF: |
91 | 1725 result = gftp_parse_ls_eplf (str, fle); |
1726 break; | |
122 | 1727 case GFTP_DIRTYPE_NOVELL: |
91 | 1728 result = gftp_parse_ls_novell (str, fle); |
1729 break; | |
122 | 1730 case GFTP_DIRTYPE_DOS: |
91 | 1731 result = gftp_parse_ls_nt (str, fle); |
1732 break; | |
122 | 1733 case GFTP_DIRTYPE_VMS: |
485 | 1734 result = gftp_parse_ls_vms (request, fd, str, fle); |
107 | 1735 break; |
358 | 1736 case GFTP_DIRTYPE_MVS: |
1737 result = gftp_parse_ls_mvs (str, fle); | |
1738 break; | |
91 | 1739 default: /* autodetect */ |
1740 if (*lsoutput == '+') | |
1741 result = gftp_parse_ls_eplf (str, fle); | |
1742 else if (isdigit ((int) str[0]) && str[2] == '-') | |
1743 result = gftp_parse_ls_nt (str, fle); | |
1744 else if (str[1] == ' ' && str[2] == '[') | |
1745 result = gftp_parse_ls_novell (str, fle); | |
1746 else | |
107 | 1747 { |
1748 if ((endpos = strchr (str, ' ')) != NULL) | |
1749 { | |
1750 /* If the first token in the string has a ; in it, then */ | |
1751 /* we'll assume that this is a VMS directory listing */ | |
1752 tmpchar = *endpos; | |
1753 *endpos = '\0'; | |
1754 is_vms = strchr (str, ';') != NULL; | |
1755 *endpos = tmpchar; | |
1756 } | |
1757 else | |
1758 is_vms = 0; | |
48 | 1759 |
107 | 1760 if (is_vms) |
485 | 1761 result = gftp_parse_ls_vms (request, fd, str, fle); |
107 | 1762 else |
460 | 1763 result = gftp_parse_ls_unix (request, str, len, fle); |
107 | 1764 } |
91 | 1765 break; |
48 | 1766 } |
1767 g_free (str); | |
1768 | |
1769 return (result); | |
1 | 1770 } |
1771 | |
1772 | |
48 | 1773 static GHashTable * |
469 | 1774 gftp_gen_dir_hash (gftp_request * request, int *ret) |
1 | 1775 { |
48 | 1776 GHashTable * dirhash; |
1777 gftp_file * fle; | |
516 | 1778 off_t *newsize; |
48 | 1779 char * newname; |
1780 | |
1781 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); | |
469 | 1782 *ret = gftp_list_files (request); |
1783 if (*ret == 0) | |
48 | 1784 { |
1785 fle = g_malloc0 (sizeof (*fle)); | |
1786 while (gftp_get_next_file (request, NULL, fle) > 0) | |
1787 { | |
1788 newname = fle->file; | |
516 | 1789 newsize = g_malloc (sizeof (*newsize)); |
48 | 1790 *newsize = fle->size; |
1791 g_hash_table_insert (dirhash, newname, newsize); | |
1792 fle->file = NULL; | |
1793 gftp_file_destroy (fle); | |
1794 } | |
1795 gftp_end_transfer (request); | |
1796 g_free (fle); | |
1797 } | |
1798 else | |
1799 { | |
1800 g_hash_table_destroy (dirhash); | |
1801 dirhash = NULL; | |
1802 } | |
509 | 1803 |
48 | 1804 return (dirhash); |
1805 } | |
39 | 1806 |
48 | 1807 |
1808 static void | |
1809 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
1810 { | |
39 | 1811 |
48 | 1812 g_free (key); |
1813 g_free (value); | |
1814 } | |
1815 | |
1816 | |
1817 static void | |
1818 gftp_destroy_dir_hash (GHashTable * dirhash) | |
1819 { | |
1820 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); | |
1821 g_hash_table_destroy (dirhash); | |
1 | 1822 } |
1823 | |
1824 | |
1825 static GList * | |
469 | 1826 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir, int *ret) |
1 | 1827 { |
1828 GHashTable * dirhash; | |
1829 GList * templist; | |
1830 gftp_file * fle; | |
516 | 1831 off_t *newsize; |
1 | 1832 char *newname; |
1833 | |
1834 if (getothdir && transfer->toreq) | |
469 | 1835 { |
1836 dirhash = gftp_gen_dir_hash (transfer->toreq, ret); | |
1837 if (*ret < 0) | |
1838 return (NULL); | |
1839 } | |
1 | 1840 else |
1841 dirhash = NULL; | |
1842 | |
509 | 1843 *ret = gftp_list_files (transfer->fromreq); |
1844 if (*ret < 0) | |
1 | 1845 return (NULL); |
1846 | |
1847 fle = g_malloc (sizeof (*fle)); | |
1848 templist = NULL; | |
1849 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
1850 { | |
1851 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) | |
1852 { | |
1853 gftp_file_destroy (fle); | |
1854 continue; | |
1855 } | |
1856 | |
1857 if (dirhash && | |
1858 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
1859 fle->startsize = *newsize; | |
1860 | |
381 | 1861 if (transfer->toreq && fle->destfile == NULL) |
245 | 1862 fle->destfile = gftp_build_path (transfer->toreq->directory, |
1863 fle->file, NULL); | |
1864 | |
381 | 1865 if (transfer->fromreq->directory != NULL && |
1866 *transfer->fromreq->directory != '\0' && | |
1867 *fle->file != '/') | |
1868 { | |
1869 newname = gftp_build_path (transfer->fromreq->directory, | |
1870 fle->file, NULL); | |
1871 | |
1872 g_free (fle->file); | |
1873 fle->file = newname; | |
1874 } | |
1 | 1875 |
1876 templist = g_list_append (templist, fle); | |
1877 | |
1878 fle = g_malloc (sizeof (*fle)); | |
1879 } | |
1880 gftp_end_transfer (transfer->fromreq); | |
1881 | |
1882 gftp_file_destroy (fle); | |
1883 g_free (fle); | |
1884 | |
509 | 1885 if (dirhash != NULL) |
1 | 1886 gftp_destroy_dir_hash (dirhash); |
1887 | |
1888 return (templist); | |
1889 } | |
1890 | |
1891 | |
1892 int | |
1893 gftp_get_all_subdirs (gftp_transfer * transfer, | |
1894 void (*update_func) (gftp_transfer * transfer)) | |
1895 { | |
381 | 1896 char *oldfromdir, *oldtodir, *newname, *pos; |
469 | 1897 int forcecd, remotechanged, ret; |
1 | 1898 GList * templist, * lastlist; |
1899 GHashTable * dirhash; | |
1900 gftp_file * curfle; | |
516 | 1901 off_t *newsize; |
500 | 1902 mode_t st_mode; |
1 | 1903 |
84 | 1904 g_return_val_if_fail (transfer != NULL, GFTP_EFATAL); |
1905 g_return_val_if_fail (transfer->fromreq != NULL, GFTP_EFATAL); | |
1906 g_return_val_if_fail (transfer->files != NULL, GFTP_EFATAL); | |
1 | 1907 |
1908 if (transfer->toreq != NULL) | |
469 | 1909 { |
1910 ret = 0; | |
1911 dirhash = gftp_gen_dir_hash (transfer->toreq, &ret); | |
1912 if (ret < 0) | |
1913 return (ret); | |
1914 } | |
1 | 1915 else |
1916 dirhash = NULL; | |
1917 | |
1918 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
1919 { | |
1920 curfle = lastlist->data; | |
381 | 1921 |
1922 if ((pos = strrchr (curfle->file, '/')) != NULL) | |
1923 pos++; | |
1924 else | |
1925 pos = curfle->file; | |
1926 | |
1927 if (dirhash != NULL && | |
1928 (newsize = g_hash_table_lookup (dirhash, pos)) != NULL) | |
1 | 1929 curfle->startsize = *newsize; |
1930 | |
381 | 1931 if (curfle->size < 0 && GFTP_IS_CONNECTED (transfer->fromreq)) |
509 | 1932 { |
1933 curfle->size = gftp_get_file_size (transfer->fromreq, curfle->file); | |
1934 if (curfle->size < 0) | |
1935 return (curfle->size); | |
1936 } | |
381 | 1937 |
1938 if (transfer->toreq && curfle->destfile == NULL) | |
1939 curfle->destfile = gftp_build_path (transfer->toreq->directory, | |
1940 curfle->file, NULL); | |
1941 | |
1942 if (transfer->fromreq->directory != NULL && | |
509 | 1943 *transfer->fromreq->directory != '\0' && *curfle->file != '/') |
381 | 1944 { |
1945 newname = gftp_build_path (transfer->fromreq->directory, | |
1946 curfle->file, NULL); | |
1947 g_free (curfle->file); | |
1948 curfle->file = newname; | |
1949 } | |
1 | 1950 |
1951 if (lastlist->next == NULL) | |
1952 break; | |
1953 } | |
1954 | |
509 | 1955 if (dirhash != NULL) |
1 | 1956 gftp_destroy_dir_hash (dirhash); |
1957 | |
1958 oldfromdir = oldtodir = NULL; | |
1959 remotechanged = 0; | |
1960 forcecd = 0; | |
1961 for (templist = transfer->files; templist != NULL; templist = templist->next) | |
1962 { | |
1963 curfle = templist->data; | |
1964 | |
500 | 1965 if (S_ISLNK (curfle->st_mode) && !S_ISDIR (curfle->st_mode)) |
1966 { | |
1967 st_mode = gftp_stat_filename (transfer->fromreq, curfle->file); | |
509 | 1968 if (st_mode < 0) |
1969 return (st_mode); | |
1970 else if (S_ISDIR (st_mode)) | |
500 | 1971 curfle->st_mode = st_mode; |
1972 } | |
1973 | |
1974 if (curfle->st_mode & S_IFDIR) | |
1 | 1975 { |
1976 oldfromdir = transfer->fromreq->directory; | |
1977 transfer->fromreq->directory = curfle->file; | |
1978 | |
509 | 1979 if (transfer->toreq != NULL) |
1 | 1980 { |
1981 oldtodir = transfer->toreq->directory; | |
1982 transfer->toreq->directory = curfle->destfile; | |
1983 } | |
509 | 1984 |
1 | 1985 forcecd = 1; |
509 | 1986 ret = gftp_set_directory (transfer->fromreq, |
1987 transfer->fromreq->directory); | |
1988 if (ret < 0) | |
1989 return (ret); | |
1990 | |
1991 if (curfle->startsize > 0 && transfer->toreq != NULL) | |
1 | 1992 { |
509 | 1993 remotechanged = 1; |
1994 ret = gftp_set_directory (transfer->toreq, | |
1995 transfer->toreq->directory); | |
469 | 1996 if (ret < 0) |
509 | 1997 return (ret); |
1998 } | |
1999 | |
2000 ret = 0; | |
2001 lastlist->next = gftp_get_dir_listing (transfer, | |
2002 curfle->startsize > 0, | |
2003 &ret); | |
2004 if (ret < 0) | |
2005 return (ret); | |
2006 | |
2007 if (lastlist->next != NULL) | |
2008 { | |
2009 lastlist->next->prev = lastlist; | |
2010 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
1 | 2011 } |
509 | 2012 |
2013 transfer->numdirs++; | |
2014 if (update_func != NULL) | |
2015 update_func (transfer); | |
1 | 2016 |
2017 transfer->fromreq->directory = oldfromdir; | |
509 | 2018 if (transfer->toreq != NULL) |
1 | 2019 transfer->toreq->directory = oldtodir; |
2020 } | |
2021 else | |
122 | 2022 transfer->numfiles++; |
1 | 2023 } |
2024 | |
2025 if (forcecd) | |
509 | 2026 { |
2027 ret = gftp_set_directory (transfer->fromreq, | |
2028 transfer->fromreq->directory); | |
2029 if (ret < 0) | |
2030 return (ret); | |
2031 } | |
2032 | |
2033 if (remotechanged && transfer->toreq != NULL) | |
2034 { | |
2035 ret = gftp_set_directory (transfer->toreq, transfer->toreq->directory); | |
2036 if (ret < 0) | |
2037 return (ret); | |
2038 } | |
2039 | |
2040 if (update_func != NULL) | |
1 | 2041 { |
2042 transfer->numfiles = transfer->numdirs = -1; | |
2043 update_func (transfer); | |
2044 } | |
509 | 2045 |
1 | 2046 return (0); |
2047 } | |
2048 | |
2049 | |
2050 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
122 | 2051 static int |
1 | 2052 get_port (struct addrinfo *addr) |
2053 { | |
2054 struct sockaddr_in * saddr; | |
2055 int port; | |
2056 | |
2057 if (addr->ai_family == AF_INET) | |
2058 { | |
2059 saddr = (struct sockaddr_in *) addr->ai_addr; | |
2060 port = ntohs (saddr->sin_port); | |
2061 } | |
2062 else | |
2063 port = 0; | |
2064 | |
2065 return (port); | |
2066 } | |
2067 #endif | |
2068 | |
2069 | |
2070 int | |
122 | 2071 gftp_connect_server (gftp_request * request, char *service, |
516 | 2072 char *proxy_hostname, unsigned int proxy_port) |
1 | 2073 { |
2074 char *connect_host, *disphost; | |
516 | 2075 unsigned int port; |
2076 int sock = -1; | |
1 | 2077 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
2078 struct addrinfo hints, *res; | |
463 | 2079 intptr_t enable_ipv6; |
1 | 2080 char serv[8]; |
463 | 2081 int errnum; |
1 | 2082 |
516 | 2083 if ((errnum = gftp_need_proxy (request, service, proxy_hostname, |
2084 proxy_port)) < 0) | |
2085 return (errnum); | |
2086 else | |
2087 { | |
2088 request->use_proxy = errnum; | |
2089 if (request->use_proxy) | |
2090 request->hostp = NULL; | |
2091 } | |
1 | 2092 |
313 | 2093 gftp_lookup_request_option (request, "enable_ipv6", &enable_ipv6); |
2094 | |
151 | 2095 request->free_hostp = 1; |
1 | 2096 memset (&hints, 0, sizeof (hints)); |
2097 hints.ai_flags = AI_CANONNAME; | |
313 | 2098 |
2099 if (enable_ipv6) | |
2100 hints.ai_family = PF_UNSPEC; | |
2101 else | |
2102 hints.ai_family = AF_INET; | |
2103 | |
1 | 2104 hints.ai_socktype = SOCK_STREAM; |
2105 | |
122 | 2106 if (request->use_proxy) |
2107 { | |
2108 connect_host = proxy_hostname; | |
2109 port = proxy_port; | |
2110 } | |
2111 else | |
2112 { | |
2113 connect_host = request->hostname; | |
2114 port = request->port; | |
2115 } | |
1 | 2116 |
2117 if (request->hostp == NULL) | |
2118 { | |
2119 if (port == 0) | |
2120 strcpy (serv, service); | |
2121 else | |
2122 snprintf (serv, sizeof (serv), "%d", port); | |
2123 | |
186 | 2124 request->logging_function (gftp_logging_misc, request, |
168 | 2125 _("Looking up %s\n"), connect_host); |
1 | 2126 if ((errnum = getaddrinfo (connect_host, serv, &hints, |
2127 &request->hostp)) != 0) | |
168 | 2128 { |
186 | 2129 request->logging_function (gftp_logging_error, request, |
168 | 2130 _("Cannot look up hostname %s: %s\n"), |
2131 connect_host, gai_strerror (errnum)); | |
2132 return (GFTP_ERETRYABLE); | |
2133 } | |
1 | 2134 } |
2135 | |
2136 disphost = connect_host; | |
2137 for (res = request->hostp; res != NULL; res = res->ai_next) | |
2138 { | |
2139 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
2140 port = get_port (res); | |
56 | 2141 if (!request->use_proxy) |
2142 request->port = port; | |
2143 | |
1 | 2144 if ((sock = socket (res->ai_family, res->ai_socktype, |
2145 res->ai_protocol)) < 0) | |
2146 { | |
186 | 2147 request->logging_function (gftp_logging_error, request, |
1 | 2148 _("Failed to create a socket: %s\n"), |
2149 g_strerror (errno)); | |
2150 continue; | |
66 | 2151 } |
1 | 2152 |
186 | 2153 request->logging_function (gftp_logging_misc, request, |
168 | 2154 _("Trying %s:%d\n"), disphost, port); |
1 | 2155 |
2156 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
168 | 2157 { |
186 | 2158 request->logging_function (gftp_logging_error, request, |
168 | 2159 _("Cannot connect to %s: %s\n"), |
2160 disphost, g_strerror (errno)); | |
1 | 2161 close (sock); |
2162 continue; | |
168 | 2163 } |
1 | 2164 break; |
2165 } | |
2166 | |
2167 if (res == NULL) | |
2168 { | |
2169 if (request->hostp != NULL) | |
2170 { | |
2171 freeaddrinfo (request->hostp); | |
2172 request->hostp = NULL; | |
2173 } | |
84 | 2174 return (GFTP_ERETRYABLE); |
1 | 2175 } |
2176 | |
2177 #else /* !HAVE_GETADDRINFO */ | |
2178 struct sockaddr_in remote_address; | |
2179 struct servent serv_struct; | |
2180 int curhost; | |
2181 | |
122 | 2182 if ((request->use_proxy = gftp_need_proxy (request, service, |
2183 proxy_hostname, proxy_port)) < 0) | |
84 | 2184 return (request->use_proxy); |
1 | 2185 else if (request->use_proxy == 1) |
2186 request->hostp = NULL; | |
2187 | |
516 | 2188 g_return_val_if_fail (sock != -1, GFTP_EFATAL); |
2189 | |
1 | 2190 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) |
2191 { | |
186 | 2192 request->logging_function (gftp_logging_error, request, |
313 | 2193 _("Failed to create a IPv4 socket: %s\n"), |
1 | 2194 g_strerror (errno)); |
84 | 2195 return (GFTP_ERETRYABLE); |
1 | 2196 } |
2197 | |
2198 memset (&remote_address, 0, sizeof (remote_address)); | |
2199 remote_address.sin_family = AF_INET; | |
2200 | |
122 | 2201 if (request->use_proxy) |
2202 { | |
2203 connect_host = proxy_hostname; | |
2204 port = proxy_port; | |
2205 } | |
2206 else | |
2207 { | |
2208 connect_host = request->hostname; | |
2209 port = request->port; | |
2210 } | |
1 | 2211 |
2212 if (port == 0) | |
2213 { | |
2214 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
2215 { | |
186 | 2216 request->logging_function (gftp_logging_error, request, |
122 | 2217 _("Cannot look up service name %s/tcp. Please check your services file\n"), |
2218 service); | |
2219 close (sock); | |
2220 return (GFTP_EFATAL); | |
1 | 2221 } |
451 | 2222 |
2223 port = ntohs (serv_struct.s_port); | |
56 | 2224 |
2225 if (!request->use_proxy) | |
451 | 2226 request->port = port; |
1 | 2227 } |
451 | 2228 |
2229 remote_address.sin_port = htons (port); | |
1 | 2230 |
2231 if (request->hostp == NULL) | |
2232 { | |
186 | 2233 request->logging_function (gftp_logging_misc, request, |
168 | 2234 _("Looking up %s\n"), connect_host); |
1 | 2235 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, |
2236 NULL))) | |
2237 { | |
186 | 2238 request->logging_function (gftp_logging_error, request, |
1 | 2239 _("Cannot look up hostname %s: %s\n"), |
2240 connect_host, g_strerror (errno)); | |
2241 close (sock); | |
84 | 2242 return (GFTP_ERETRYABLE); |
1 | 2243 } |
2244 } | |
2245 | |
2246 disphost = NULL; | |
2247 for (curhost = 0; request->host.h_addr_list[curhost] != NULL; curhost++) | |
2248 { | |
2249 disphost = request->host.h_name; | |
2250 memcpy (&remote_address.sin_addr, request->host.h_addr_list[curhost], | |
2251 request->host.h_length); | |
186 | 2252 request->logging_function (gftp_logging_misc, request, |
1 | 2253 _("Trying %s:%d\n"), |
451 | 2254 request->host.h_name, port); |
1 | 2255 |
2256 if (connect (sock, (struct sockaddr *) &remote_address, | |
2257 sizeof (remote_address)) == -1) | |
2258 { | |
186 | 2259 request->logging_function (gftp_logging_error, request, |
1 | 2260 _("Cannot connect to %s: %s\n"), |
2261 connect_host, g_strerror (errno)); | |
2262 } | |
2263 break; | |
2264 } | |
2265 | |
2266 if (request->host.h_addr_list[curhost] == NULL) | |
2267 { | |
2268 close (sock); | |
84 | 2269 return (GFTP_ERETRYABLE); |
1 | 2270 } |
2271 #endif /* HAVE_GETADDRINFO */ | |
2272 | |
182 | 2273 if (fcntl (sock, F_SETFD, 1) == -1) |
2274 { | |
186 | 2275 request->logging_function (gftp_logging_error, request, |
182 | 2276 _("Error: Cannot set close on exec flag: %s\n"), |
2277 g_strerror (errno)); | |
2278 | |
2279 return (GFTP_ERETRYABLE); | |
2280 } | |
2281 | |
186 | 2282 request->logging_function (gftp_logging_misc, request, |
168 | 2283 _("Connected to %s:%d\n"), connect_host, port); |
58 | 2284 |
168 | 2285 if (gftp_fd_set_sockblocking (request, sock, 1) < 0) |
58 | 2286 { |
2287 close (sock); | |
84 | 2288 return (GFTP_ERETRYABLE); |
58 | 2289 } |
2290 | |
169 | 2291 request->datafd = sock; |
168 | 2292 |
2293 if (request->post_connect != NULL) | |
2294 return (request->post_connect (request)); | |
2295 | |
2296 return (0); | |
1 | 2297 } |
2298 | |
2299 | |
177 | 2300 int |
1 | 2301 gftp_set_config_options (gftp_request * request) |
2302 { | |
58 | 2303 if (request->set_config_options != NULL) |
177 | 2304 return (request->set_config_options (request)); |
2305 else | |
2306 return (0); | |
1 | 2307 } |
2308 | |
2309 | |
2310 void | |
2311 print_file_list (GList * list) | |
2312 { | |
2313 gftp_file * tempfle; | |
2314 GList * templist; | |
499 | 2315 char *attribs; |
1 | 2316 |
2317 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
2318 for (templist = list; ; templist = templist->next) | |
2319 { | |
2320 tempfle = templist->data; | |
499 | 2321 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
2322 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2323 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2324 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
372 | 2325 tempfle->file, tempfle->destfile, |
516 | 2326 tempfle->size, tempfle->startsize, |
499 | 2327 tempfle->user, tempfle->group, attribs); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2328 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2329 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
16 | 2330 tempfle->file, tempfle->destfile, |
2331 tempfle->size, tempfle->startsize, | |
499 | 2332 tempfle->user, tempfle->group, attribs); |
372 | 2333 #endif |
499 | 2334 |
2335 g_free (attribs); | |
1 | 2336 if (templist->next == NULL) |
2337 break; | |
2338 } | |
2339 | |
2340 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
2341 for (; ; templist = templist->prev) | |
2342 { | |
2343 tempfle = templist->data; | |
499 | 2344 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
2345 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2346 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2347 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
372 | 2348 tempfle->file, tempfle->destfile, |
516 | 2349 tempfle->size, tempfle->startsize, |
499 | 2350 tempfle->user, tempfle->group, attribs); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2351 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2352 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
16 | 2353 tempfle->file, tempfle->destfile, |
2354 tempfle->size, tempfle->startsize, | |
499 | 2355 tempfle->user, tempfle->group, attribs); |
372 | 2356 #endif |
499 | 2357 |
2358 g_free (attribs); | |
1 | 2359 if (templist == list) |
2360 break; | |
2361 } | |
2362 printf ("--END OF FILE LISTING--\n"); | |
2363 } | |
2364 | |
41 | 2365 |
201 | 2366 void |
58 | 2367 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) |
41 | 2368 { |
58 | 2369 g_free ((*rbuf)->buffer); |
2370 g_free (*rbuf); | |
2371 *rbuf = NULL; | |
41 | 2372 } |
2373 | |
2374 | |
58 | 2375 ssize_t |
2376 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
2377 char * str, size_t len, int fd) | |
41 | 2378 { |
168 | 2379 ssize_t (*read_function) (gftp_request * request, void *ptr, size_t size, |
2380 int fd); | |
516 | 2381 char *pos, *nextpos; |
2382 size_t rlen, nslen; | |
179 | 2383 int end_of_buffer; |
516 | 2384 ssize_t ret; |
168 | 2385 |
2386 if (request == NULL || request->read_function == NULL) | |
2387 read_function = gftp_fd_read; | |
2388 else | |
2389 read_function = request->read_function; | |
58 | 2390 |
2391 if (*rbuf == NULL) | |
2392 { | |
2393 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
2394 (*rbuf)->max_bufsize = len; | |
249 | 2395 (*rbuf)->buffer = g_malloc0 ((*rbuf)->max_bufsize + 1); |
168 | 2396 |
2397 if ((ret = read_function (request, (*rbuf)->buffer, | |
2398 (*rbuf)->max_bufsize, fd)) <= 0) | |
58 | 2399 { |
2400 gftp_free_getline_buffer (rbuf); | |
2401 return (ret); | |
2402 } | |
60 | 2403 (*rbuf)->buffer[ret] = '\0'; |
58 | 2404 (*rbuf)->cur_bufsize = ret; |
2405 (*rbuf)->curpos = (*rbuf)->buffer; | |
2406 } | |
2407 | |
516 | 2408 ret = 0; |
2409 while (1) | |
58 | 2410 { |
179 | 2411 pos = strchr ((*rbuf)->curpos, '\n'); |
2412 end_of_buffer = (*rbuf)->curpos == (*rbuf)->buffer && | |
2413 ((*rbuf)->max_bufsize == (*rbuf)->cur_bufsize || (*rbuf)->eof); | |
2414 | |
2415 if ((*rbuf)->cur_bufsize > 0 && (pos != NULL || end_of_buffer)) | |
58 | 2416 { |
2417 if (pos != NULL) | |
2418 { | |
516 | 2419 nslen = pos - (*rbuf)->curpos + 1; |
58 | 2420 nextpos = pos + 1; |
2421 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
2422 pos--; | |
2423 *pos = '\0'; | |
2424 } | |
2425 else | |
249 | 2426 { |
516 | 2427 nslen = (*rbuf)->cur_bufsize; |
249 | 2428 nextpos = NULL; |
2429 | |
2430 /* This is not an overflow since we allocated one extra byte to | |
2431 buffer above */ | |
516 | 2432 ((*rbuf)->curpos)[nslen] = '\0'; |
249 | 2433 } |
58 | 2434 |
2435 strncpy (str, (*rbuf)->curpos, len); | |
249 | 2436 str[len - 1] = '\0'; |
516 | 2437 (*rbuf)->cur_bufsize -= nslen; |
58 | 2438 |
168 | 2439 if (nextpos != NULL) |
2440 (*rbuf)->curpos = nextpos; | |
2441 else | |
2442 (*rbuf)->cur_bufsize = 0; | |
516 | 2443 |
2444 ret = nslen; | |
58 | 2445 break; |
2446 } | |
2447 else | |
2448 { | |
2449 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
2450 { | |
2451 rlen = (*rbuf)->max_bufsize; | |
2452 pos = (*rbuf)->buffer; | |
2453 } | |
2454 else | |
2455 { | |
168 | 2456 memmove ((*rbuf)->buffer, (*rbuf)->curpos, (*rbuf)->cur_bufsize); |
2457 pos = (*rbuf)->buffer + (*rbuf)->cur_bufsize; | |
2458 rlen = (*rbuf)->max_bufsize - (*rbuf)->cur_bufsize; | |
58 | 2459 } |
168 | 2460 |
58 | 2461 (*rbuf)->curpos = (*rbuf)->buffer; |
2462 | |
209 | 2463 if ((*rbuf)->eof) |
2464 ret = 0; | |
2465 else | |
2466 ret = read_function (request, pos, rlen, fd); | |
2467 | |
2468 if (ret < 0) | |
58 | 2469 { |
2470 gftp_free_getline_buffer (rbuf); | |
2471 return (ret); | |
2472 } | |
179 | 2473 else if (ret == 0) |
168 | 2474 { |
179 | 2475 if ((*rbuf)->cur_bufsize == 0) |
2476 { | |
2477 gftp_free_getline_buffer (rbuf); | |
2478 return (ret); | |
2479 } | |
2480 | |
2481 (*rbuf)->eof = 1; | |
168 | 2482 } |
2483 | |
2484 (*rbuf)->cur_bufsize += ret; | |
215 | 2485 (*rbuf)->curpos[(*rbuf)->cur_bufsize] = '\0'; |
58 | 2486 } |
2487 } | |
516 | 2488 |
2489 return (ret); | |
58 | 2490 } |
2491 | |
2492 | |
2493 ssize_t | |
168 | 2494 gftp_fd_read (gftp_request * request, void *ptr, size_t size, int fd) |
58 | 2495 { |
325 | 2496 intptr_t network_timeout; |
58 | 2497 struct timeval tv; |
2498 fd_set fset; | |
2499 ssize_t ret; | |
41 | 2500 |
122 | 2501 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
2502 | |
41 | 2503 errno = 0; |
2504 ret = 0; | |
518 | 2505 |
2506 while (1) | |
41 | 2507 { |
58 | 2508 FD_ZERO (&fset); |
2509 FD_SET (fd, &fset); | |
122 | 2510 tv.tv_sec = network_timeout; |
58 | 2511 tv.tv_usec = 0; |
2512 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
2513 if (ret == -1 && errno == EINTR) | |
41 | 2514 { |
58 | 2515 if (request && request->cancel) |
2516 break; | |
2517 else | |
2518 continue; | |
2519 } | |
2520 else if (ret <= 0) | |
2521 { | |
2522 if (request != NULL) | |
41 | 2523 { |
186 | 2524 request->logging_function (gftp_logging_error, request, |
58 | 2525 _("Connection to %s timed out\n"), |
2526 request->hostname); | |
2527 gftp_disconnect (request); | |
2528 } | |
84 | 2529 return (GFTP_ERETRYABLE); |
41 | 2530 } |
2531 | |
58 | 2532 if ((ret = read (fd, ptr, size)) < 0) |
41 | 2533 { |
2534 if (errno == EINTR) | |
2535 { | |
58 | 2536 if (request != NULL && request->cancel) |
41 | 2537 break; |
2538 else | |
58 | 2539 continue; |
518 | 2540 } |
41 | 2541 |
58 | 2542 if (request != NULL) |
2543 { | |
186 | 2544 request->logging_function (gftp_logging_error, request, |
58 | 2545 _("Error: Could not read from socket: %s\n"), |
41 | 2546 g_strerror (errno)); |
58 | 2547 gftp_disconnect (request); |
2548 } | |
84 | 2549 return (GFTP_ERETRYABLE); |
41 | 2550 } |
518 | 2551 break; |
41 | 2552 } |
2553 | |
58 | 2554 if (errno == EINTR && request != NULL && request->cancel) |
41 | 2555 { |
2556 gftp_disconnect (request); | |
84 | 2557 return (GFTP_ERETRYABLE); |
41 | 2558 } |
2559 | |
2560 return (ret); | |
2561 } | |
2562 | |
58 | 2563 |
2564 ssize_t | |
168 | 2565 gftp_fd_write (gftp_request * request, const char *ptr, size_t size, int fd) |
58 | 2566 { |
325 | 2567 intptr_t network_timeout; |
58 | 2568 struct timeval tv; |
248 | 2569 ssize_t w_ret; |
58 | 2570 fd_set fset; |
516 | 2571 int ret; |
58 | 2572 |
122 | 2573 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
2574 | |
58 | 2575 errno = 0; |
2576 ret = 0; | |
2577 do | |
2578 { | |
2579 FD_ZERO (&fset); | |
2580 FD_SET (fd, &fset); | |
122 | 2581 tv.tv_sec = network_timeout; |
58 | 2582 tv.tv_usec = 0; |
2583 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
2584 if (ret == -1 && errno == EINTR) | |
2585 { | |
2586 if (request != NULL && request->cancel) | |
2587 break; | |
2588 else | |
2589 continue; | |
2590 } | |
2591 else if (ret <= 0) | |
2592 { | |
2593 if (request != NULL) | |
2594 { | |
186 | 2595 request->logging_function (gftp_logging_error, request, |
58 | 2596 _("Connection to %s timed out\n"), |
2597 request->hostname); | |
2598 gftp_disconnect (request); | |
2599 } | |
84 | 2600 return (GFTP_ERETRYABLE); |
58 | 2601 } |
2602 | |
248 | 2603 w_ret = write (fd, ptr, size); |
2604 if (w_ret < 0) | |
58 | 2605 { |
2606 if (errno == EINTR) | |
2607 { | |
2608 if (request != NULL && request->cancel) | |
2609 break; | |
2610 else | |
2611 continue; | |
2612 } | |
2613 | |
2614 if (request != NULL) | |
2615 { | |
186 | 2616 request->logging_function (gftp_logging_error, request, |
58 | 2617 _("Error: Could not write to socket: %s\n"), |
2618 g_strerror (errno)); | |
2619 gftp_disconnect (request); | |
2620 } | |
84 | 2621 return (GFTP_ERETRYABLE); |
58 | 2622 } |
2623 | |
2624 ptr += w_ret; | |
2625 size -= w_ret; | |
2626 ret += w_ret; | |
2627 } | |
2628 while (size > 0); | |
2629 | |
2630 if (errno == EINTR && request != NULL && request->cancel) | |
2631 { | |
2632 gftp_disconnect (request); | |
84 | 2633 return (GFTP_ERETRYABLE); |
58 | 2634 } |
2635 | |
2636 return (ret); | |
2637 } | |
2638 | |
2639 | |
2640 ssize_t | |
2641 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
2642 { | |
2643 char *tempstr; | |
2644 va_list argp; | |
2645 ssize_t ret; | |
2646 | |
2647 va_start (argp, fmt); | |
2648 tempstr = g_strdup_vprintf (fmt, argp); | |
2649 va_end (argp); | |
2650 | |
168 | 2651 ret = request->write_function (request, tempstr, strlen (tempstr), fd); |
58 | 2652 g_free (tempstr); |
2653 return (ret); | |
2654 } | |
2655 | |
2656 | |
2657 int | |
168 | 2658 gftp_fd_set_sockblocking (gftp_request * request, int fd, int non_blocking) |
58 | 2659 { |
2660 int flags; | |
2661 | |
84 | 2662 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) |
58 | 2663 { |
186 | 2664 request->logging_function (gftp_logging_error, request, |
58 | 2665 _("Cannot get socket flags: %s\n"), |
2666 g_strerror (errno)); | |
2667 gftp_disconnect (request); | |
84 | 2668 return (GFTP_ERETRYABLE); |
58 | 2669 } |
2670 | |
2671 if (non_blocking) | |
2672 flags |= O_NONBLOCK; | |
2673 else | |
2674 flags &= ~O_NONBLOCK; | |
2675 | |
84 | 2676 if (fcntl (fd, F_SETFL, flags) < 0) |
58 | 2677 { |
186 | 2678 request->logging_function (gftp_logging_error, request, |
58 | 2679 _("Cannot set socket to non-blocking: %s\n"), |
2680 g_strerror (errno)); | |
2681 gftp_disconnect (request); | |
84 | 2682 return (GFTP_ERETRYABLE); |
58 | 2683 } |
2684 | |
2685 return (0); | |
2686 } | |
2687 | |
2688 | |
63 | 2689 void |
2690 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
2691 { | |
2692 g_return_if_fail (dest != NULL); | |
2693 g_return_if_fail (source != NULL); | |
2694 g_return_if_fail (dest->protonum == source->protonum); | |
2695 | |
2696 dest->datafd = source->datafd; | |
2697 dest->cached = 0; | |
397 | 2698 #ifdef USE_SSL |
2699 dest->ssl = source->ssl; | |
2700 #endif | |
2701 | |
63 | 2702 if (!source->always_connected) |
2703 { | |
2704 source->datafd = -1; | |
2705 source->cached = 1; | |
397 | 2706 #ifdef USE_SSL |
2707 source->ssl = NULL; | |
2708 #endif | |
63 | 2709 } |
2710 | |
516 | 2711 if (dest->swap_socks != NULL) |
63 | 2712 dest->swap_socks (dest, source); |
2713 } | |
2714 | |
122 | 2715 |
2716 void | |
2717 gftp_calc_kbs (gftp_transfer * tdata, ssize_t num_read) | |
2718 { | |
469 | 2719 /* Needed for systems that size(float) < size(void *) */ |
2720 union { intptr_t i; float f; } maxkbs; | |
122 | 2721 unsigned long waitusecs; |
220 | 2722 double start_difftime; |
122 | 2723 struct timeval tv; |
222 | 2724 int waited; |
122 | 2725 |
469 | 2726 gftp_lookup_request_option (tdata->fromreq, "maxkbs", &maxkbs.f); |
122 | 2727 |
2728 if (g_thread_supported ()) | |
2729 g_static_mutex_lock (&tdata->statmutex); | |
2730 | |
220 | 2731 gettimeofday (&tv, NULL); |
2732 | |
122 | 2733 tdata->trans_bytes += num_read; |
2734 tdata->curtrans += num_read; | |
2735 tdata->stalled = 0; | |
2736 | |
220 | 2737 start_difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0); |
2738 | |
2739 if (start_difftime <= 0) | |
2740 tdata->kbs = tdata->trans_bytes / 1024.0; | |
122 | 2741 else |
220 | 2742 tdata->kbs = tdata->trans_bytes / 1024.0 / start_difftime; |
2743 | |
222 | 2744 waited = 0; |
469 | 2745 if (maxkbs.f > 0 && tdata->kbs > maxkbs.f) |
122 | 2746 { |
469 | 2747 waitusecs = num_read / 1024.0 / maxkbs.f * 1000000.0 - start_difftime; |
122 | 2748 |
2749 if (waitusecs > 0) | |
2750 { | |
2751 if (g_thread_supported ()) | |
2752 g_static_mutex_unlock (&tdata->statmutex); | |
2753 | |
222 | 2754 waited = 1; |
122 | 2755 usleep (waitusecs); |
2756 | |
2757 if (g_thread_supported ()) | |
2758 g_static_mutex_lock (&tdata->statmutex); | |
2759 } | |
222 | 2760 |
122 | 2761 } |
2762 | |
222 | 2763 if (waited) |
2764 gettimeofday (&tdata->lasttime, NULL); | |
2765 else | |
2766 memcpy (&tdata->lasttime, &tv, sizeof (tdata->lasttime)); | |
122 | 2767 |
2768 if (g_thread_supported ()) | |
2769 g_static_mutex_unlock (&tdata->statmutex); | |
2770 } | |
2771 | |
125 | 2772 |
2773 int | |
2774 gftp_get_transfer_status (gftp_transfer * tdata, ssize_t num_read) | |
2775 { | |
325 | 2776 intptr_t retries, sleep_time; |
125 | 2777 gftp_file * tempfle; |
2778 struct timeval tv; | |
498 | 2779 int ret1, ret2; |
125 | 2780 |
2781 ret1 = ret2 = 0; | |
2782 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
2783 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
2784 | |
2785 if (g_thread_supported ()) | |
2786 g_static_mutex_lock (&tdata->structmutex); | |
2787 | |
2788 if (tdata->curfle == NULL) | |
2789 { | |
2790 if (g_thread_supported ()) | |
2791 g_static_mutex_unlock (&tdata->structmutex); | |
2792 | |
2793 return (GFTP_EFATAL); | |
2794 } | |
2795 | |
2796 tempfle = tdata->curfle->data; | |
2797 | |
2798 if (g_thread_supported ()) | |
2799 g_static_mutex_unlock (&tdata->structmutex); | |
2800 | |
2801 gftp_disconnect (tdata->fromreq); | |
2802 gftp_disconnect (tdata->toreq); | |
2803 | |
2804 if (num_read < 0 || tdata->skip_file) | |
2805 { | |
2806 if (num_read == GFTP_EFATAL) | |
2807 return (GFTP_EFATAL); | |
512 | 2808 else if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout) |
125 | 2809 { |
303 | 2810 if (retries != 0 && |
2811 tdata->current_file_retries >= retries) | |
2812 { | |
2813 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, | |
2814 _("Error: Remote site %s disconnected. Max retries reached...giving up\n"), | |
2815 tdata->fromreq->hostname != NULL ? | |
2816 tdata->fromreq->hostname : tdata->toreq->hostname); | |
2817 return (GFTP_EFATAL); | |
2818 } | |
2819 else | |
2820 { | |
2821 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, | |
2822 _("Error: Remote site %s disconnected. Will reconnect in %d seconds\n"), | |
2823 tdata->fromreq->hostname != NULL ? | |
2824 tdata->fromreq->hostname : tdata->toreq->hostname, | |
2825 sleep_time); | |
2826 } | |
125 | 2827 } |
2828 | |
2829 while (retries == 0 || | |
2830 tdata->current_file_retries <= retries) | |
2831 { | |
512 | 2832 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout && |
2833 !tdata->skip_file) | |
125 | 2834 { |
2835 tv.tv_sec = sleep_time; | |
2836 tv.tv_usec = 0; | |
498 | 2837 |
2838 do | |
2839 { | |
2840 ret1 = select (0, NULL, NULL, NULL, &tv); | |
2841 } | |
2842 while (ret1 == -1 && errno == EINTR); | |
125 | 2843 } |
2844 | |
2845 if ((ret1 = gftp_connect (tdata->fromreq)) == 0 && | |
2846 (ret2 = gftp_connect (tdata->toreq)) == 0) | |
2847 { | |
2848 if (g_thread_supported ()) | |
2849 g_static_mutex_lock (&tdata->structmutex); | |
2850 | |
2851 tdata->resumed_bytes = tdata->resumed_bytes + tdata->trans_bytes - tdata->curresumed - tdata->curtrans; | |
2852 tdata->trans_bytes = 0; | |
2853 if (tdata->skip_file) | |
2854 { | |
2855 tdata->total_bytes -= tempfle->size; | |
2856 tdata->curtrans = 0; | |
2857 | |
2858 tdata->curfle = tdata->curfle->next; | |
2859 tdata->next_file = 1; | |
2860 tdata->skip_file = 0; | |
2861 tdata->cancel = 0; | |
2862 tdata->fromreq->cancel = 0; | |
2863 tdata->toreq->cancel = 0; | |
2864 } | |
2865 else | |
2866 { | |
2867 tempfle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
2868 tempfle->startsize = tdata->curtrans + tdata->curresumed; | |
2869 /* We decrement this here because it will be incremented in | |
2870 the loop again */ | |
2871 tdata->curresumed = 0; | |
2872 tdata->current_file_number--; /* Decrement this because it | |
2873 will be incremented when we | |
2874 continue in the loop */ | |
2875 } | |
2876 | |
2877 gettimeofday (&tdata->starttime, NULL); | |
2878 | |
2879 if (g_thread_supported ()) | |
2880 g_static_mutex_unlock (&tdata->structmutex); | |
2881 | |
2882 return (GFTP_ERETRYABLE); | |
2883 } | |
2884 else if (ret1 == GFTP_EFATAL || ret2 == GFTP_EFATAL) | |
2885 { | |
2886 gftp_disconnect (tdata->fromreq); | |
2887 gftp_disconnect (tdata->toreq); | |
2888 return (GFTP_EFATAL); | |
2889 } | |
2890 else | |
2891 tdata->current_file_retries++; | |
2892 } | |
2893 } | |
2894 else if (tdata->cancel) | |
2895 return (GFTP_EFATAL); | |
2896 | |
2897 return (0); | |
2898 } | |
2899 | |
182 | 2900 |
2901 int | |
2902 gftp_fd_open (gftp_request * request, const char *pathname, int flags, mode_t mode) | |
2903 { | |
2904 int fd; | |
2905 | |
227 | 2906 if (mode == 0) |
2907 fd = open (pathname, flags); | |
2908 else | |
2909 fd = open (pathname, flags, mode); | |
2910 | |
2911 if (fd < 0) | |
182 | 2912 { |
2913 if (request != NULL) | |
186 | 2914 request->logging_function (gftp_logging_error, request, |
182 | 2915 _("Error: Cannot open local file %s: %s\n"), |
2916 pathname, g_strerror (errno)); | |
2917 return (GFTP_ERETRYABLE); | |
2918 } | |
2919 | |
2920 if (fcntl (fd, F_SETFD, 1) == -1) | |
2921 { | |
2922 if (request != NULL) | |
186 | 2923 request->logging_function (gftp_logging_error, request, |
182 | 2924 _("Error: Cannot set close on exec flag: %s\n"), |
2925 g_strerror (errno)); | |
2926 | |
2927 return (-1); | |
2928 } | |
2929 | |
2930 return (fd); | |
2931 } | |
422 | 2932 |
2933 | |
2934 void | |
2935 gftp_setup_startup_directory (gftp_request * request) | |
2936 { | |
2937 char *startup_directory, *tempstr; | |
2938 | |
2939 gftp_lookup_request_option (request, "startup_directory", &startup_directory); | |
2940 | |
2941 if (*startup_directory != '\0' && | |
2942 (tempstr = expand_path (startup_directory)) != NULL) | |
2943 { | |
2944 gftp_set_directory (request, tempstr); | |
2945 g_free (tempstr); | |
2946 } | |
2947 } | |
2948 | |
499 | 2949 |
2950 char * | |
2951 gftp_convert_attributes_from_mode_t (mode_t mode) | |
2952 { | |
2953 char *str; | |
2954 | |
2955 str = g_malloc0 (11); | |
2956 | |
2957 str[0] = '?'; | |
2958 if (S_ISREG (mode)) | |
2959 str[0] = '-'; | |
2960 | |
2961 if (S_ISLNK (mode)) | |
2962 str[0] = 'l'; | |
2963 | |
2964 if (S_ISBLK (mode)) | |
2965 str[0] = 'b'; | |
2966 | |
2967 if (S_ISCHR (mode)) | |
2968 str[0] = 'c'; | |
2969 | |
2970 if (S_ISFIFO (mode)) | |
2971 str[0] = 'p'; | |
2972 | |
2973 if (S_ISSOCK (mode)) | |
2974 str[0] = 's'; | |
2975 | |
2976 if (S_ISDIR (mode)) | |
2977 str[0] = 'd'; | |
2978 | |
2979 str[1] = mode & S_IRUSR ? 'r' : '-'; | |
2980 str[2] = mode & S_IWUSR ? 'w' : '-'; | |
2981 | |
2982 if ((mode & S_ISUID) && (mode & S_IXUSR)) | |
2983 str[3] = 's'; | |
2984 else if (mode & S_ISUID) | |
2985 str[3] = 'S'; | |
2986 else if (mode & S_IXUSR) | |
2987 str[3] = 'x'; | |
2988 else | |
2989 str[3] = '-'; | |
2990 | |
2991 str[4] = mode & S_IRGRP ? 'r' : '-'; | |
2992 str[5] = mode & S_IWGRP ? 'w' : '-'; | |
2993 | |
2994 if ((mode & S_ISGID) && (mode & S_IXGRP)) | |
2995 str[6] = 's'; | |
2996 else if (mode & S_ISGID) | |
2997 str[6] = 'S'; | |
2998 else if (mode & S_IXGRP) | |
2999 str[6] = 'x'; | |
3000 else | |
3001 str[6] = '-'; | |
3002 | |
3003 str[7] = mode & S_IROTH ? 'r' : '-'; | |
3004 str[8] = mode & S_IWOTH ? 'w' : '-'; | |
3005 | |
3006 if ((mode & S_ISVTX) && (mode & S_IXOTH)) | |
3007 str[9] = 't'; | |
3008 else if (mode & S_ISVTX) | |
3009 str[9] = 'T'; | |
3010 else if (mode & S_IXOTH) | |
3011 str[9] = 'x'; | |
3012 else | |
3013 str[9] = '-'; | |
3014 | |
3015 return (str); | |
3016 } | |
3017 | |
3018 | |
3019 mode_t | |
3020 gftp_convert_attributes_to_mode_t (char *attribs) | |
3021 { | |
3022 mode_t mode; | |
3023 | |
3024 if (attribs[0] == 'd') | |
3025 mode = S_IFDIR; | |
3026 else if (attribs[0] == 'l') | |
3027 mode = S_IFLNK; | |
3028 else if (attribs[0] == 's') | |
3029 mode = S_IFSOCK; | |
3030 else if (attribs[0] == 'b') | |
3031 mode = S_IFBLK; | |
3032 else if (attribs[0] == 'c') | |
3033 mode = S_IFCHR; | |
3034 else | |
3035 mode = S_IFREG; | |
3036 | |
3037 if (attribs[1] == 'r') | |
3038 mode |= S_IRUSR; | |
3039 if (attribs[2] == 'w') | |
3040 mode |= S_IWUSR; | |
3041 if (attribs[3] == 'x' || attribs[3] == 's') | |
3042 mode |= S_IXUSR; | |
3043 if (attribs[3] == 's' || attribs[3] == 'S') | |
3044 mode |= S_ISUID; | |
3045 | |
3046 if (attribs[4] == 'r') | |
3047 mode |= S_IRGRP; | |
3048 if (attribs[5] == 'w') | |
3049 mode |= S_IWGRP; | |
3050 if (attribs[6] == 'x' || | |
3051 attribs[6] == 's') | |
3052 mode |= S_IXGRP; | |
3053 if (attribs[6] == 's' || attribs[6] == 'S') | |
3054 mode |= S_ISGID; | |
3055 | |
3056 if (attribs[7] == 'r') | |
3057 mode |= S_IROTH; | |
3058 if (attribs[8] == 'w') | |
3059 mode |= S_IWOTH; | |
3060 if (attribs[9] == 'x' || | |
3061 attribs[9] == 's') | |
3062 mode |= S_IXOTH; | |
3063 if (attribs[9] == 't' || attribs[9] == 'T') | |
3064 mode |= S_ISVTX; | |
3065 | |
3066 return (mode); | |
3067 } | |
3068 |