Mercurial > gftp.yaz
annotate lib/protocols.c @ 516:fbb5a02beddb
2004-7-26 Brian Masney <masneyb@gftp.org>
* lib/config_file.c lib/local.c lib/ftps.c lib/gftp.h lib/httpcommon.h
lib/https.c lib/misc.c lib/protocols.c lib/pty.c lib/rfc2068.c
lib/rfc959.c lib/sshv2.c - various fixes found through a static code
analysis checker. (some signed/unsigned fixes, removed unneeded casts,
removed unneeded variables, more consistency, largefile fixes). Thanks
to Derek M Jones for running the static code analysis for me
* lib/cache.c - s/remove/del_entry/g
* lib/bookmark.c lib/protocols.c (gftp_parse_bookmark) - allow
the third parameter of this function to be a NULL pointer
author | masneyb |
---|---|
date | Tue, 27 Jul 2004 02:46:54 +0000 |
parents | 2c8a42a63a31 |
children | c6a6e908dbaf |
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 | |
500 | 342 mode_t |
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; |
173 | 708 int len, i, init_ret; |
168 | 709 const char *cpos; |
1 | 710 |
84 | 711 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
712 g_return_val_if_fail (url != NULL, GFTP_EFATAL); | |
1 | 713 |
67 | 714 logging_function = request->logging_function; |
715 gftp_request_destroy (request, 0); | |
716 request->logging_function = logging_function; | |
717 | |
168 | 718 for (cpos = url; *cpos == ' '; cpos++); |
719 stpos = g_strdup (cpos); | |
720 for (pos = stpos + strlen (stpos) - 1; *pos == ' '; pos--) | |
721 *pos = '\0'; | |
1 | 722 |
122 | 723 i = GFTP_FTP_NUM; |
724 | |
1 | 725 if ((pos = strstr (stpos, "://")) != NULL) |
726 { | |
727 *pos = '\0'; | |
728 | |
729 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
730 { | |
731 if (strcmp (gftp_protocols[i].url_prefix, stpos) == 0) | |
732 break; | |
733 } | |
734 | |
735 if (gftp_protocols[i].url_prefix == NULL) | |
168 | 736 { |
173 | 737 request->logging_function (gftp_logging_misc, NULL, |
738 _("The protocol '%s' is currently not supported.\n"), | |
739 stpos); | |
168 | 740 g_free (stpos); |
741 return (GFTP_EFATAL); | |
742 } | |
173 | 743 |
744 *pos = ':'; | |
1 | 745 } |
746 else | |
747 { | |
122 | 748 gftp_lookup_request_option (request, "default_protocol", |
749 &default_protocol); | |
750 | |
125 | 751 if (default_protocol != NULL && *default_protocol != '\0') |
1 | 752 { |
122 | 753 for (i = 0; gftp_protocols[i].url_prefix; i++) |
754 { | |
755 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
756 break; | |
757 } | |
1 | 758 } |
122 | 759 } |
1 | 760 |
122 | 761 if (gftp_protocols[i].url_prefix == NULL) |
762 i = GFTP_FTP_NUM; | |
1 | 763 |
173 | 764 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
765 { | |
766 gftp_request_destroy (request, 0); | |
767 return (init_ret); | |
768 } | |
1 | 769 |
770 if (request->parse_url != NULL) | |
168 | 771 { |
772 g_free (stpos); | |
773 return (request->parse_url (request, url)); | |
774 } | |
775 | |
776 pos = stpos; | |
777 len = strlen (request->url_prefix); | |
778 if (strncmp (pos, request->url_prefix, len) == 0 && | |
779 strncmp (pos + len, "://", 3) == 0) | |
780 pos += len + 3; | |
1 | 781 |
782 if (i == GFTP_LOCAL_NUM) | |
783 { | |
367 | 784 for (; *pos == ' ' || *pos == '\t'; pos++); |
168 | 785 request->directory = g_strdup (pos); |
786 g_free (stpos); | |
1 | 787 return (0); |
788 } | |
789 | |
182 | 790 if ((endhostpos = strrchr (pos, '@')) != NULL) |
1 | 791 { |
792 /* A user/password was entered */ | |
793 if ((endpos = strchr (pos, ':')) == NULL || endhostpos < endpos) | |
168 | 794 { |
795 /* No password was entered */ | |
796 gftp_set_password (request, ""); | |
797 endpos = endhostpos; | |
798 } | |
1 | 799 |
800 *endpos = '\0'; | |
801 gftp_set_username (request, pos); | |
802 | |
803 pos = endpos + 1; | |
804 if ((endpos = strchr (pos, '@')) != NULL) | |
168 | 805 { |
806 if (strchr (endpos + 1, '@') != NULL) | |
807 endpos = strchr (endpos + 1, '@'); | |
808 *endpos = '\0'; | |
809 gftp_set_password (request, pos); | |
1 | 810 |
168 | 811 pos = endpos + 1; |
812 } | |
1 | 813 } |
814 | |
815 /* Now get the hostname and an optional port and optional directory */ | |
816 endhostpos = pos + strlen (pos); | |
817 if ((endpos = strchr (pos, ':')) != NULL) | |
818 endhostpos = endpos; | |
819 else if ((endpos = strchr (pos, '/')) != NULL) | |
820 endhostpos = endpos; | |
821 tempchar = *endhostpos; | |
822 *endhostpos = '\0'; | |
823 gftp_set_hostname (request, pos); | |
824 *endhostpos = tempchar; | |
825 | |
826 if ((endpos = strchr (pos, ':')) != NULL) | |
827 { | |
828 /* A port was entered */ | |
829 pos = endpos + 1; | |
830 gftp_set_port (request, strtol (pos, NULL, 10)); | |
831 } | |
832 if ((endpos = strchr (pos, '/')) != NULL) | |
833 gftp_set_directory (request, endpos); | |
168 | 834 g_free (stpos); |
1 | 835 return (0); |
836 } | |
837 | |
838 | |
839 void | |
840 gftp_set_hostname (gftp_request * request, const char *hostname) | |
841 { | |
842 g_return_if_fail (request != NULL); | |
843 g_return_if_fail (hostname != NULL); | |
844 | |
845 if (request->hostname) | |
846 g_free (request->hostname); | |
124 | 847 request->hostname = g_strdup (hostname); |
1 | 848 } |
849 | |
850 | |
851 void | |
852 gftp_set_username (gftp_request * request, const char *username) | |
853 { | |
854 g_return_if_fail (request != NULL); | |
855 | |
856 if (request->username) | |
857 g_free (request->username); | |
169 | 858 |
859 if (username != NULL) | |
860 request->username = g_strdup (username); | |
861 else | |
862 request->username = NULL; | |
1 | 863 } |
864 | |
865 | |
866 void | |
867 gftp_set_password (gftp_request * request, const char *password) | |
868 { | |
869 g_return_if_fail (request != NULL); | |
870 | |
871 if (request->password) | |
872 g_free (request->password); | |
169 | 873 |
874 if (password != NULL) | |
875 request->password = g_strdup (password); | |
876 else | |
877 request->password = NULL; | |
1 | 878 } |
879 | |
880 | |
881 void | |
882 gftp_set_account (gftp_request * request, const char *account) | |
883 { | |
884 g_return_if_fail (request != NULL); | |
885 g_return_if_fail (account != NULL); | |
886 | |
887 if (request->account) | |
888 g_free (request->account); | |
124 | 889 request->account = g_strdup (account); |
1 | 890 } |
891 | |
892 | |
893 int | |
894 gftp_set_directory (gftp_request * request, const char *directory) | |
895 { | |
84 | 896 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
897 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
1 | 898 |
899 | |
169 | 900 if (request->datafd <= 0 && !request->always_connected) |
1 | 901 { |
902 if (directory != request->directory) | |
168 | 903 { |
904 if (request->directory) | |
905 g_free (request->directory); | |
906 request->directory = g_strdup (directory); | |
907 } | |
1 | 908 return (0); |
909 } | |
910 else if (request->chdir == NULL) | |
84 | 911 return (GFTP_EFATAL); |
1 | 912 return (request->chdir (request, directory)); |
913 } | |
914 | |
915 | |
916 void | |
917 gftp_set_port (gftp_request * request, unsigned int port) | |
918 { | |
919 g_return_if_fail (request != NULL); | |
920 | |
921 request->port = port; | |
922 } | |
923 | |
924 | |
925 int | |
926 gftp_remove_directory (gftp_request * request, const char *directory) | |
927 { | |
84 | 928 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 929 |
930 if (request->rmdir == NULL) | |
84 | 931 return (GFTP_EFATAL); |
1 | 932 return (request->rmdir (request, directory)); |
933 } | |
934 | |
935 | |
936 int | |
937 gftp_remove_file (gftp_request * request, const char *file) | |
938 { | |
84 | 939 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 940 |
941 if (request->rmfile == NULL) | |
84 | 942 return (GFTP_EFATAL); |
1 | 943 return (request->rmfile (request, file)); |
944 } | |
945 | |
946 | |
947 int | |
948 gftp_make_directory (gftp_request * request, const char *directory) | |
949 { | |
291 | 950 char *utf8; |
951 int ret; | |
952 | |
84 | 953 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 954 |
955 if (request->mkdir == NULL) | |
84 | 956 return (GFTP_EFATAL); |
291 | 957 |
958 utf8 = gftp_string_from_utf8 (request, directory); | |
959 if (utf8 != NULL) | |
960 { | |
961 ret = request->mkdir (request, utf8); | |
962 g_free (utf8); | |
963 } | |
964 else | |
965 ret = request->mkdir (request, directory); | |
966 | |
967 return (ret); | |
1 | 968 } |
969 | |
970 | |
971 int | |
972 gftp_rename_file (gftp_request * request, const char *oldname, | |
168 | 973 const char *newname) |
1 | 974 { |
291 | 975 char *utf8; |
976 int ret; | |
977 | |
84 | 978 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 979 |
980 if (request->rename == NULL) | |
84 | 981 return (GFTP_EFATAL); |
291 | 982 |
983 utf8 = gftp_string_from_utf8 (request, newname); | |
984 if (utf8 != NULL) | |
985 { | |
986 ret = request->rename (request, oldname, utf8); | |
987 g_free (utf8); | |
988 } | |
989 else | |
990 ret = request->rename (request, oldname, newname); | |
991 | |
992 return (ret); | |
1 | 993 } |
994 | |
995 | |
996 int | |
499 | 997 gftp_chmod (gftp_request * request, const char *file, mode_t mode) |
1 | 998 { |
84 | 999 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 1000 |
1001 if (request->chmod == NULL) | |
84 | 1002 return (GFTP_EFATAL); |
504 | 1003 |
1004 mode &= S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX; | |
1 | 1005 return (request->chmod (request, file, mode)); |
1006 } | |
1007 | |
1008 | |
1009 int | |
1010 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
1011 { | |
84 | 1012 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 1013 |
1014 if (request->set_file_time == NULL) | |
84 | 1015 return (GFTP_EFATAL); |
1 | 1016 return (request->set_file_time (request, file, datetime)); |
1017 } | |
1018 | |
1019 | |
1020 char | |
478 | 1021 gftp_site_cmd (gftp_request * request, int specify_site, const char *command) |
1 | 1022 { |
84 | 1023 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 1024 |
1025 if (request->site == NULL) | |
84 | 1026 return (GFTP_EFATAL); |
478 | 1027 return (request->site (request, specify_site, command)); |
1 | 1028 } |
1029 | |
1030 | |
58 | 1031 off_t |
1 | 1032 gftp_get_file_size (gftp_request * request, const char *filename) |
1033 { | |
1034 g_return_val_if_fail (request != NULL, 0); | |
1035 | |
1036 if (request->get_file_size == NULL) | |
1037 return (0); | |
1038 return (request->get_file_size (request, filename)); | |
1039 } | |
1040 | |
1041 | |
122 | 1042 static int |
1043 gftp_need_proxy (gftp_request * request, char *service, char *proxy_hostname, | |
1044 int proxy_port) | |
1 | 1045 { |
122 | 1046 gftp_config_list_vars * proxy_hosts; |
1 | 1047 gftp_proxy_hosts * hostname; |
460 | 1048 size_t hostlen, domlen; |
1 | 1049 unsigned char addy[4]; |
1050 struct sockaddr *addr; | |
1051 GList * templist; | |
1052 gint32 netaddr; | |
1053 char *pos; | |
1054 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
1055 struct addrinfo hints; | |
516 | 1056 unsigned int port; |
1057 int errnum; | |
1 | 1058 char serv[8]; |
122 | 1059 #endif |
1060 | |
218 | 1061 gftp_lookup_global_option ("dont_use_proxy", &proxy_hosts); |
122 | 1062 |
1063 if (proxy_hostname == NULL || *proxy_hostname == '\0') | |
1064 return (0); | |
1065 else if (proxy_hosts->list == NULL) | |
1066 return (proxy_hostname != NULL && | |
1067 *proxy_hostname != '\0'); | |
1 | 1068 |
1069 request->hostp = NULL; | |
122 | 1070 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
151 | 1071 request->free_hostp = 1; |
1 | 1072 memset (&hints, 0, sizeof (hints)); |
1073 hints.ai_flags = AI_CANONNAME; | |
146 | 1074 hints.ai_family = PF_UNSPEC; |
1 | 1075 hints.ai_socktype = SOCK_STREAM; |
1076 | |
122 | 1077 port = request->use_proxy ? proxy_port : request->port; |
1 | 1078 if (port == 0) |
1079 strcpy (serv, service); | |
1080 else | |
1081 snprintf (serv, sizeof (serv), "%d", port); | |
1082 | |
186 | 1083 request->logging_function (gftp_logging_misc, request, |
1 | 1084 _("Looking up %s\n"), request->hostname); |
1085 | |
1086 if ((errnum = getaddrinfo (request->hostname, serv, &hints, | |
1087 &request->hostp)) != 0) | |
1088 { | |
186 | 1089 request->logging_function (gftp_logging_error, request, |
1 | 1090 _("Cannot look up hostname %s: %s\n"), |
1091 request->hostname, gai_strerror (errnum)); | |
84 | 1092 return (GFTP_ERETRYABLE); |
1 | 1093 } |
1094 | |
1095 addr = request->hostp->ai_addr; | |
1096 | |
1097 #else /* !HAVE_GETADDRINFO */ | |
186 | 1098 request->logging_function (gftp_logging_misc, request, |
1 | 1099 _("Looking up %s\n"), request->hostname); |
1100 | |
1101 if (!(request->hostp = r_gethostbyname (request->hostname, &request->host, | |
1102 NULL))) | |
1103 { | |
186 | 1104 request->logging_function (gftp_logging_error, request, |
1 | 1105 _("Cannot look up hostname %s: %s\n"), |
1106 request->hostname, g_strerror (errno)); | |
84 | 1107 return (GFTP_ERETRYABLE); |
1 | 1108 } |
1109 | |
1110 addr = (struct sockaddr *) request->host.h_addr_list[0]; | |
1111 | |
1112 #endif /* HAVE_GETADDRINFO */ | |
1113 | |
122 | 1114 templist = proxy_hosts->list; |
1 | 1115 while (templist != NULL) |
1116 { | |
1117 hostname = templist->data; | |
460 | 1118 if (hostname->domain != NULL) |
168 | 1119 { |
460 | 1120 hostlen = strlen (request->hostname); |
1121 domlen = strlen (hostname->domain); | |
1122 if (hostlen > domlen) | |
1123 { | |
1124 pos = request->hostname + hostlen - domlen; | |
1125 if (strcmp (hostname->domain, pos) == 0) | |
1126 return (0); | |
1127 } | |
168 | 1128 } |
1 | 1129 |
1130 if (hostname->ipv4_network_address != 0) | |
168 | 1131 { |
516 | 1132 memcpy (addy, addr, sizeof (*addy)); |
168 | 1133 netaddr = |
1134 (((addy[0] & 0xff) << 24) | ((addy[1] & 0xff) << 16) | | |
1135 ((addy[2] & 0xff) << 8) | (addy[3] & 0xff)) & | |
1 | 1136 hostname->ipv4_netmask; |
168 | 1137 if (netaddr == hostname->ipv4_network_address) |
1138 return (0); | |
1139 } | |
1 | 1140 templist = templist->next; |
1141 } | |
1142 | |
122 | 1143 return (proxy_hostname != NULL && *proxy_hostname != '\0'); |
1 | 1144 } |
1145 | |
1146 | |
48 | 1147 static char * |
1148 copy_token (char **dest, char *source) | |
1 | 1149 { |
48 | 1150 /* This function is used internally by gftp_parse_ls () */ |
1151 char *endpos, savepos; | |
1 | 1152 |
48 | 1153 endpos = source; |
1154 while (*endpos != ' ' && *endpos != '\t' && *endpos != '\0') | |
1155 endpos++; | |
1156 if (*endpos == '\0') | |
1157 return (NULL); | |
1 | 1158 |
48 | 1159 savepos = *endpos; |
1160 *endpos = '\0'; | |
1161 *dest = g_malloc (endpos - source + 1); | |
1162 strcpy (*dest, source); | |
1163 *endpos = savepos; | |
1 | 1164 |
48 | 1165 /* Skip the blanks till we get to the next entry */ |
1166 source = endpos + 1; | |
1167 while ((*source == ' ' || *source == '\t') && *source != '\0') | |
1168 source++; | |
1169 return (source); | |
1170 } | |
1 | 1171 |
1172 | |
48 | 1173 static char * |
1174 goto_next_token (char *pos) | |
1175 { | |
1176 while (*pos != ' ' && *pos != '\t' && *pos != '\0') | |
1177 pos++; | |
1 | 1178 |
516 | 1179 while (*pos == ' ' || *pos == '\t') |
48 | 1180 pos++; |
1181 | |
1182 return (pos); | |
1 | 1183 } |
1184 | |
1185 | |
485 | 1186 static time_t |
1187 parse_vms_time (char *str, char **endpos) | |
1188 { | |
1189 struct tm curtime; | |
1190 time_t ret; | |
1191 | |
1192 /* 8-JUN-2004 13:04:14 */ | |
1193 memset (&curtime, 0, sizeof (curtime)); | |
1194 | |
1195 *endpos = strptime (str, "%d-%b-%Y %H:%M:%S", &curtime); | |
1196 if (*endpos == NULL) | |
1197 *endpos = strptime (str, "%d-%b-%Y %H:%M", &curtime); | |
1198 | |
1199 if (*endpos != NULL) | |
1200 { | |
1201 ret = mktime (&curtime); | |
1202 for (; **endpos == ' ' || **endpos == '\t'; (*endpos)++); | |
1203 } | |
1204 else | |
1205 { | |
1206 ret = 0; | |
1207 *endpos = goto_next_token (str); | |
1208 if (*endpos != NULL) | |
1209 *endpos = goto_next_token (*endpos); | |
1210 } | |
1211 | |
1212 return (ret); | |
1213 } | |
1214 | |
1215 | |
102 | 1216 time_t |
1217 parse_time (char *str, char **endpos) | |
1 | 1218 { |
102 | 1219 struct tm curtime, *loctime; |
105 | 1220 time_t t, ret; |
102 | 1221 char *tmppos; |
460 | 1222 size_t slen; |
260 | 1223 int i, num; |
1 | 1224 |
460 | 1225 slen = strlen (str); |
102 | 1226 memset (&curtime, 0, sizeof (curtime)); |
1227 curtime.tm_isdst = -1; | |
460 | 1228 |
1229 if (slen > 4 && isdigit ((int) str[0]) && str[2] == '-' && | |
260 | 1230 isdigit ((int) str[3])) |
1 | 1231 { |
1232 /* This is how DOS will return the date/time */ | |
1233 /* 07-06-99 12:57PM */ | |
1234 | |
102 | 1235 tmppos = strptime (str, "%m-%d-%y %I:%M%p", &curtime); |
1 | 1236 } |
460 | 1237 else if (slen > 4 && isdigit ((int) str[0]) && str[2] == '-' && |
260 | 1238 isalpha (str[3])) |
105 | 1239 { |
1240 /* 10-Jan-2003 09:14 */ | |
1241 tmppos = strptime (str, "%d-%h-%Y %H:%M", &curtime); | |
1242 } | |
460 | 1243 else if (slen > 4 && isdigit ((int) str[0]) && str[4] == '/') |
358 | 1244 { |
1245 /* 2003/12/25 */ | |
1246 tmppos = strptime (str, "%Y/%m/%d", &curtime); | |
1247 } | |
1 | 1248 else |
1249 { | |
1250 /* This is how most UNIX, Novell, and MacOS ftp servers send their time */ | |
102 | 1251 /* Jul 06 12:57 or Jul 6 1999 */ |
1 | 1252 |
102 | 1253 if (strchr (str, ':') != NULL) |
1254 { | |
1255 tmppos = strptime (str, "%h %d %H:%M", &curtime); | |
1256 t = time (NULL); | |
1257 loctime = localtime (&t); | |
359 | 1258 |
1259 if (curtime.tm_mon > loctime->tm_mon) | |
1260 curtime.tm_year = loctime->tm_year - 1; | |
1261 else | |
1262 curtime.tm_year = loctime->tm_year; | |
102 | 1263 } |
1264 else | |
1265 tmppos = strptime (str, "%h %d %Y", &curtime); | |
1266 } | |
1 | 1267 |
105 | 1268 if (tmppos != NULL) |
1269 ret = mktime (&curtime); | |
1270 else | |
1271 ret = 0; | |
1272 | |
102 | 1273 if (endpos != NULL) |
105 | 1274 { |
1275 if (tmppos == NULL) | |
1276 { | |
260 | 1277 /* We cannot parse this date format. So, just skip this date field |
1278 and continue to the next token. This is mainly for the HTTP | |
1279 support */ | |
1280 | |
1281 *endpos = str; | |
1282 for (num = 0; num < 2 && **endpos != '\0'; num++) | |
1283 { | |
1284 for (i=0; | |
1285 (*endpos)[i] != ' ' && (*endpos)[i] != '\t' && | |
1286 (*endpos)[i] != '\0'; | |
1287 i++); | |
1288 *endpos += i; | |
1289 | |
1290 for (i=0; (*endpos)[i] == ' ' || (*endpos)[i] == '\t'; i++); | |
1291 *endpos += i; | |
1292 } | |
105 | 1293 } |
1294 else | |
1295 *endpos = tmppos; | |
1296 } | |
1 | 1297 |
281 | 1298 return (ret); |
1 | 1299 } |
1300 | |
1301 | |
499 | 1302 static mode_t |
1303 gftp_parse_vms_attribs (char **src, mode_t mask) | |
107 | 1304 { |
1305 char *endpos; | |
499 | 1306 mode_t ret; |
107 | 1307 |
1308 if (*src == NULL) | |
499 | 1309 return (0); |
107 | 1310 |
1311 if ((endpos = strchr (*src, ',')) != NULL) | |
1312 *endpos = '\0'; | |
1313 | |
499 | 1314 ret = 0; |
107 | 1315 if (strchr (*src, 'R') != NULL) |
499 | 1316 ret |= S_IRUSR | S_IRGRP | S_IROTH; |
107 | 1317 if (strchr (*src, 'W') != NULL) |
499 | 1318 ret |= S_IWUSR | S_IWGRP | S_IWOTH; |
107 | 1319 if (strchr (*src, 'E') != NULL) |
499 | 1320 ret |= S_IXUSR | S_IXGRP | S_IXOTH; |
107 | 1321 |
1322 *src = endpos + 1; | |
499 | 1323 |
1324 return (ret & mask); | |
107 | 1325 } |
1326 | |
1327 | |
1328 static int | |
485 | 1329 gftp_parse_ls_vms (gftp_request * request, int fd, char *str, gftp_file * fle) |
107 | 1330 { |
485 | 1331 char *curpos, *endpos, tempstr[1024]; |
1332 int multiline; | |
1333 ssize_t len; | |
107 | 1334 |
1335 /* .PINE-DEBUG1;1 9 21-AUG-2002 20:06 [MYERSRG] (RWED,RWED,,) */ | |
1336 /* WWW.DIR;1 1 23-NOV-1999 05:47 [MYERSRG] (RWE,RWE,RE,E) */ | |
1337 | |
485 | 1338 /* Multiline VMS |
1339 $MAIN.TPU$JOURNAL;1 | |
1340 1/18 8-JUN-2004 13:04:14 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
1341 TCPIP$FTP_SERVER.LOG;29 | |
1342 0/18 8-JUN-2004 14:42:04 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
1343 TCPIP$FTP_SERVER.LOG;28 | |
1344 5/18 8-JUN-2004 13:05:11 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
1345 TCPIP$FTP_SERVER.LOG;27 | |
1346 5/18 8-JUN-2004 13:03:51 [NUCLEAR,FISSION] (RWED,RWED,RE,) */ | |
1347 | |
107 | 1348 if ((curpos = strchr (str, ';')) == NULL) |
1349 return (GFTP_EFATAL); | |
1350 | |
485 | 1351 multiline = strchr (str, ' ') == NULL; |
1352 | |
107 | 1353 *curpos = '\0'; |
1354 if (strlen (str) > 4 && strcmp (curpos - 4, ".DIR") == 0) | |
1355 { | |
499 | 1356 fle->st_mode |= S_IFDIR; |
107 | 1357 *(curpos - 4) = '\0'; |
1358 } | |
1359 | |
1360 fle->file = g_strdup (str); | |
1361 | |
485 | 1362 if (multiline) |
1363 { | |
1364 if (request->get_next_dirlist_line == NULL) | |
1365 return (GFTP_EFATAL); | |
1366 | |
1367 len = request->get_next_dirlist_line (request, fd, tempstr, | |
1368 sizeof (tempstr)); | |
1369 if (len <= 0) | |
1370 return ((int) len); | |
1371 | |
1372 for (curpos = tempstr; *curpos == ' ' || *curpos == '\t'; curpos++); | |
1373 } | |
1374 else | |
1375 curpos = goto_next_token (curpos + 1); | |
1376 | |
244 | 1377 fle->size = gftp_parse_file_size (curpos) * 512; /* Is this correct? */ |
107 | 1378 |
1379 curpos = goto_next_token (curpos); | |
1380 | |
485 | 1381 fle->datetime = parse_vms_time (curpos, &curpos); |
1382 | |
107 | 1383 if (*curpos != '[') |
1384 return (GFTP_EFATAL); | |
485 | 1385 |
107 | 1386 if ((endpos = strchr (curpos, ']')) == NULL) |
1387 return (GFTP_EFATAL); | |
1388 | |
1389 curpos = goto_next_token (endpos + 1); | |
1390 if ((curpos = strchr (curpos, ',')) == NULL) | |
1391 return (0); | |
1392 curpos++; | |
1393 | |
499 | 1394 fle->st_mode = gftp_parse_vms_attribs (&curpos, S_IRWXU); |
1395 fle->st_mode |= gftp_parse_vms_attribs (&curpos, S_IRWXG); | |
1396 fle->st_mode |= gftp_parse_vms_attribs (&curpos, S_IRWXO); | |
107 | 1397 |
485 | 1398 fle->user = g_strdup (""); |
1399 fle->group = g_strdup (""); | |
1400 | |
107 | 1401 return (0); |
1402 } | |
358 | 1403 |
1404 | |
1405 static int | |
1406 gftp_parse_ls_mvs (char *str, gftp_file * fle) | |
1407 { | |
1408 char *curpos; | |
1409 | |
1410 /* Volume Unit Referred Ext Used Recfm Lrecl BlkSz Dsorg Dsname */ | |
1411 /* SVI52A 3390 2003/12/10 8 216 FB 80 27920 PS CARDS.DELETES */ | |
1412 /* SVI528 3390 2003/12/12 1 5 FB 80 24000 PO CLIST */ | |
1413 | |
1414 curpos = goto_next_token (str + 1); | |
1415 if (curpos == NULL) | |
1416 return (GFTP_EFATAL); | |
1417 | |
1418 curpos = goto_next_token (curpos + 1); | |
1419 if (curpos == NULL) | |
1420 return (GFTP_EFATAL); | |
1421 | |
479 | 1422 fle->datetime = parse_time (curpos, &curpos); |
1423 | |
1424 curpos = goto_next_token (curpos); | |
1425 if (curpos == NULL) | |
358 | 1426 return (GFTP_EFATAL); |
1427 | |
1428 curpos = goto_next_token (curpos + 1); | |
1429 if (curpos == NULL) | |
1430 return (GFTP_EFATAL); | |
1431 | |
1432 fle->size = gftp_parse_file_size (curpos) * 55996; | |
1433 curpos = goto_next_token (curpos + 1); | |
1434 if (curpos == NULL) | |
1435 return (GFTP_EFATAL); | |
1436 | |
1437 curpos = goto_next_token (curpos + 1); | |
1438 if (curpos == NULL) | |
1439 return (GFTP_EFATAL); | |
1440 | |
1441 curpos = goto_next_token (curpos + 1); | |
1442 if (curpos == NULL) | |
1443 return (GFTP_EFATAL); | |
1444 | |
1445 curpos = goto_next_token (curpos + 1); | |
1446 if (curpos == NULL) | |
1447 return (GFTP_EFATAL); | |
1448 | |
1449 if (strncmp (curpos, "PS", 2) == 0) | |
499 | 1450 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
358 | 1451 else if (strncmp (curpos, "PO", 2) == 0) |
499 | 1452 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
358 | 1453 else |
1454 return (GFTP_EFATAL); | |
1455 | |
1456 curpos = goto_next_token (curpos + 1); | |
1457 | |
1458 fle->user = g_strdup (_("unknown")); | |
1459 fle->group = g_strdup (_("unknown")); | |
1460 fle->file = g_strdup (curpos); | |
1461 | |
1462 return (0); | |
1463 } | |
107 | 1464 |
1465 | |
1 | 1466 static int |
1467 gftp_parse_ls_eplf (char *str, gftp_file * fle) | |
1468 { | |
1469 char *startpos; | |
358 | 1470 int isdir = 0; |
1 | 1471 |
1472 startpos = str; | |
1473 while (startpos) | |
1474 { | |
1475 startpos++; | |
1476 switch (*startpos) | |
168 | 1477 { |
516 | 1478 case '/': |
1479 isdir = 1; | |
1480 break; | |
1481 case 's': | |
1482 fle->size = gftp_parse_file_size (startpos + 1); | |
1483 break; | |
1484 case 'm': | |
1485 fle->datetime = strtol (startpos + 1, NULL, 10); | |
1486 break; | |
168 | 1487 } |
1 | 1488 startpos = strchr (startpos, ','); |
1489 } | |
358 | 1490 |
1 | 1491 if ((startpos = strchr (str, 9)) == NULL) |
84 | 1492 return (GFTP_EFATAL); |
358 | 1493 |
1494 if (isdir) | |
499 | 1495 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
358 | 1496 else |
499 | 1497 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
358 | 1498 |
124 | 1499 fle->file = g_strdup (startpos + 1); |
1500 fle->user = g_strdup (_("unknown")); | |
1501 fle->group = g_strdup (_("unknown")); | |
1 | 1502 return (0); |
1503 } | |
1504 | |
1505 | |
1506 static int | |
460 | 1507 gftp_parse_ls_unix (gftp_request * request, char *str, size_t slen, |
1508 gftp_file * fle) | |
1 | 1509 { |
499 | 1510 char *endpos, *startpos, *pos, *attribs; |
91 | 1511 int cols; |
1512 | |
1513 /* If there is no space between the attribs and links field, just make one */ | |
460 | 1514 if (slen > 10) |
91 | 1515 str[10] = ' '; |
1516 | |
1517 /* Determine the number of columns */ | |
1518 cols = 0; | |
1519 pos = str; | |
1520 while (*pos != '\0') | |
1521 { | |
1522 while (*pos != '\0' && *pos != ' ' && *pos != '\t') | |
1523 { | |
1524 if (*pos == ':') | |
1525 break; | |
1526 pos++; | |
1527 } | |
1528 | |
1529 cols++; | |
1530 | |
1531 if (*pos == ':') | |
1532 { | |
1533 cols++; | |
1534 break; | |
1535 } | |
1536 | |
1537 while (*pos == ' ' || *pos == '\t') | |
1538 pos++; | |
1539 } | |
1 | 1540 |
1541 startpos = str; | |
1542 /* Copy file attributes */ | |
499 | 1543 if ((startpos = copy_token (&attribs, startpos)) == NULL) |
84 | 1544 return (GFTP_EFATAL); |
1 | 1545 |
499 | 1546 fle->st_mode = gftp_convert_attributes_to_mode_t (attribs); |
1547 g_free (attribs); | |
1548 | |
1 | 1549 if (cols >= 9) |
1550 { | |
1551 /* Skip the number of links */ | |
1552 startpos = goto_next_token (startpos); | |
1553 | |
1554 /* Copy the user that owns this file */ | |
1555 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
168 | 1556 return (GFTP_EFATAL); |
1 | 1557 |
1558 /* Copy the group that owns this file */ | |
1559 if ((startpos = copy_token (&fle->group, startpos)) == NULL) | |
168 | 1560 return (GFTP_EFATAL); |
1 | 1561 } |
1562 else | |
1563 { | |
124 | 1564 fle->group = g_strdup (_("unknown")); |
1 | 1565 if (cols == 8) |
168 | 1566 { |
1567 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
1568 return (GFTP_EFATAL); | |
1569 } | |
1 | 1570 else |
124 | 1571 fle->user = g_strdup (_("unknown")); |
1 | 1572 startpos = goto_next_token (startpos); |
1573 } | |
1574 | |
281 | 1575 if (request->server_type == GFTP_DIRTYPE_CRAY) |
1 | 1576 { |
91 | 1577 /* See if this is a Cray directory listing. It has the following format: |
1578 drwx------ 2 feiliu g913 DK common 4096 Sep 24 2001 wv */ | |
1579 if (cols == 11 && strstr (str, "->") == NULL) | |
1580 { | |
1581 startpos = goto_next_token (startpos); | |
1582 startpos = goto_next_token (startpos); | |
1583 } | |
1 | 1584 } |
1585 | |
1586 /* See if this is a block or character device. We will store the major number | |
1587 in the high word and the minor number in the low word. */ | |
499 | 1588 if (GFTP_IS_SPECIAL_DEVICE (fle->st_mode) && |
1589 (endpos = strchr (startpos, ',')) != NULL) | |
1 | 1590 { |
1591 fle->size = strtol (startpos, NULL, 10) << 16; | |
1592 | |
1593 startpos = endpos + 1; | |
1594 while (*startpos == ' ') | |
168 | 1595 startpos++; |
1 | 1596 |
1597 /* Get the minor number */ | |
1598 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1599 return (GFTP_EFATAL); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1600 fle->size |= strtol (startpos, NULL, 10) & 0xFF; |
1 | 1601 } |
1602 else | |
1603 { | |
1604 /* This is a regular file */ | |
1605 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1606 return (GFTP_EFATAL); |
244 | 1607 fle->size = gftp_parse_file_size (startpos); |
1 | 1608 } |
1609 | |
1610 /* Skip the blanks till we get to the next entry */ | |
1611 startpos = endpos + 1; | |
1612 while (*startpos == ' ') | |
1613 startpos++; | |
1614 | |
479 | 1615 fle->datetime = parse_time (startpos, &startpos); |
1 | 1616 |
1617 /* Skip the blanks till we get to the next entry */ | |
1618 startpos = goto_next_token (startpos); | |
1619 | |
1620 /* Parse the filename. If this file is a symbolic link, remove the -> part */ | |
499 | 1621 if (S_ISLNK (fle->st_mode) && ((endpos = strstr (startpos, "->")) != NULL)) |
1 | 1622 *(endpos - 1) = '\0'; |
1623 | |
124 | 1624 fle->file = g_strdup (startpos); |
1 | 1625 |
1626 /* Uncomment this if you want to strip the spaces off of the end of the file. | |
1627 I don't want to do this by default since there are valid filenames with | |
1628 spaces at the end of them. Some broken FTP servers like the Paradyne IPC | |
1629 DSLAMS append a bunch of spaces at the end of the file. | |
1630 for (endpos = fle->file + strlen (fle->file) - 1; | |
1631 *endpos == ' '; | |
1632 *endpos-- = '\0'); | |
1633 */ | |
1634 | |
1635 return (0); | |
1636 } | |
1637 | |
1638 | |
1639 static int | |
1640 gftp_parse_ls_nt (char *str, gftp_file * fle) | |
1641 { | |
1642 char *startpos; | |
1643 | |
1644 startpos = str; | |
479 | 1645 fle->datetime = parse_time (startpos, &startpos); |
1 | 1646 |
124 | 1647 fle->user = g_strdup (_("unknown")); |
1648 fle->group = g_strdup (_("unknown")); | |
1 | 1649 |
1650 startpos = goto_next_token (startpos); | |
499 | 1651 |
1 | 1652 if (startpos[0] == '<') |
499 | 1653 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
1 | 1654 else |
1655 { | |
499 | 1656 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
244 | 1657 fle->size = gftp_parse_file_size (startpos); |
1 | 1658 } |
124 | 1659 |
1 | 1660 startpos = goto_next_token (startpos); |
124 | 1661 fle->file = g_strdup (startpos); |
1 | 1662 return (0); |
1663 } | |
1664 | |
1665 | |
1666 static int | |
1667 gftp_parse_ls_novell (char *str, gftp_file * fle) | |
1668 { | |
1669 char *startpos; | |
1670 | |
1671 if (str[12] != ' ') | |
84 | 1672 return (GFTP_EFATAL); |
499 | 1673 |
1 | 1674 str[12] = '\0'; |
499 | 1675 fle->st_mode = gftp_convert_attributes_to_mode_t (str); |
1 | 1676 startpos = str + 13; |
1677 | |
1678 while ((*startpos == ' ' || *startpos == '\t') && *startpos != '\0') | |
1679 startpos++; | |
1680 | |
1681 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
84 | 1682 return (GFTP_EFATAL); |
1 | 1683 |
124 | 1684 fle->group = g_strdup (_("unknown")); |
1 | 1685 |
244 | 1686 fle->size = gftp_parse_file_size (startpos); |
1 | 1687 |
1688 startpos = goto_next_token (startpos); | |
479 | 1689 fle->datetime = parse_time (startpos, &startpos); |
1 | 1690 |
1691 startpos = goto_next_token (startpos); | |
124 | 1692 fle->file = g_strdup (startpos); |
1 | 1693 return (0); |
1694 } | |
1695 | |
1696 | |
48 | 1697 int |
485 | 1698 gftp_parse_ls (gftp_request * request, const char *lsoutput, gftp_file * fle, |
1699 int fd) | |
1 | 1700 { |
107 | 1701 char *str, *endpos, tmpchar; |
1702 int result, is_vms; | |
91 | 1703 size_t len; |
48 | 1704 |
84 | 1705 g_return_val_if_fail (lsoutput != NULL, GFTP_EFATAL); |
1706 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
48 | 1707 |
124 | 1708 str = g_strdup (lsoutput); |
48 | 1709 memset (fle, 0, sizeof (*fle)); |
1 | 1710 |
91 | 1711 len = strlen (str); |
107 | 1712 if (len > 0 && str[len - 1] == '\n') |
91 | 1713 str[--len] = '\0'; |
1714 if (len > 0 && str[len - 1] == '\r') | |
1715 str[--len] = '\0'; | |
39 | 1716 |
91 | 1717 switch (request->server_type) |
1718 { | |
122 | 1719 case GFTP_DIRTYPE_CRAY: |
1720 case GFTP_DIRTYPE_UNIX: | |
460 | 1721 result = gftp_parse_ls_unix (request, str, len, fle); |
91 | 1722 break; |
122 | 1723 case GFTP_DIRTYPE_EPLF: |
91 | 1724 result = gftp_parse_ls_eplf (str, fle); |
1725 break; | |
122 | 1726 case GFTP_DIRTYPE_NOVELL: |
91 | 1727 result = gftp_parse_ls_novell (str, fle); |
1728 break; | |
122 | 1729 case GFTP_DIRTYPE_DOS: |
91 | 1730 result = gftp_parse_ls_nt (str, fle); |
1731 break; | |
122 | 1732 case GFTP_DIRTYPE_VMS: |
485 | 1733 result = gftp_parse_ls_vms (request, fd, str, fle); |
107 | 1734 break; |
358 | 1735 case GFTP_DIRTYPE_MVS: |
1736 result = gftp_parse_ls_mvs (str, fle); | |
1737 break; | |
91 | 1738 default: /* autodetect */ |
1739 if (*lsoutput == '+') | |
1740 result = gftp_parse_ls_eplf (str, fle); | |
1741 else if (isdigit ((int) str[0]) && str[2] == '-') | |
1742 result = gftp_parse_ls_nt (str, fle); | |
1743 else if (str[1] == ' ' && str[2] == '[') | |
1744 result = gftp_parse_ls_novell (str, fle); | |
1745 else | |
107 | 1746 { |
1747 if ((endpos = strchr (str, ' ')) != NULL) | |
1748 { | |
1749 /* If the first token in the string has a ; in it, then */ | |
1750 /* we'll assume that this is a VMS directory listing */ | |
1751 tmpchar = *endpos; | |
1752 *endpos = '\0'; | |
1753 is_vms = strchr (str, ';') != NULL; | |
1754 *endpos = tmpchar; | |
1755 } | |
1756 else | |
1757 is_vms = 0; | |
48 | 1758 |
107 | 1759 if (is_vms) |
485 | 1760 result = gftp_parse_ls_vms (request, fd, str, fle); |
107 | 1761 else |
460 | 1762 result = gftp_parse_ls_unix (request, str, len, fle); |
107 | 1763 } |
91 | 1764 break; |
48 | 1765 } |
1766 g_free (str); | |
1767 | |
1768 return (result); | |
1 | 1769 } |
1770 | |
1771 | |
48 | 1772 static GHashTable * |
469 | 1773 gftp_gen_dir_hash (gftp_request * request, int *ret) |
1 | 1774 { |
48 | 1775 GHashTable * dirhash; |
1776 gftp_file * fle; | |
516 | 1777 off_t *newsize; |
48 | 1778 char * newname; |
1779 | |
1780 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); | |
469 | 1781 *ret = gftp_list_files (request); |
1782 if (*ret == 0) | |
48 | 1783 { |
1784 fle = g_malloc0 (sizeof (*fle)); | |
1785 while (gftp_get_next_file (request, NULL, fle) > 0) | |
1786 { | |
1787 newname = fle->file; | |
516 | 1788 newsize = g_malloc (sizeof (*newsize)); |
48 | 1789 *newsize = fle->size; |
1790 g_hash_table_insert (dirhash, newname, newsize); | |
1791 fle->file = NULL; | |
1792 gftp_file_destroy (fle); | |
1793 } | |
1794 gftp_end_transfer (request); | |
1795 g_free (fle); | |
1796 } | |
1797 else | |
1798 { | |
1799 g_hash_table_destroy (dirhash); | |
1800 dirhash = NULL; | |
1801 } | |
509 | 1802 |
48 | 1803 return (dirhash); |
1804 } | |
39 | 1805 |
48 | 1806 |
1807 static void | |
1808 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
1809 { | |
39 | 1810 |
48 | 1811 g_free (key); |
1812 g_free (value); | |
1813 } | |
1814 | |
1815 | |
1816 static void | |
1817 gftp_destroy_dir_hash (GHashTable * dirhash) | |
1818 { | |
1819 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); | |
1820 g_hash_table_destroy (dirhash); | |
1 | 1821 } |
1822 | |
1823 | |
1824 static GList * | |
469 | 1825 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir, int *ret) |
1 | 1826 { |
1827 GHashTable * dirhash; | |
1828 GList * templist; | |
1829 gftp_file * fle; | |
516 | 1830 off_t *newsize; |
1 | 1831 char *newname; |
1832 | |
1833 if (getothdir && transfer->toreq) | |
469 | 1834 { |
1835 dirhash = gftp_gen_dir_hash (transfer->toreq, ret); | |
1836 if (*ret < 0) | |
1837 return (NULL); | |
1838 } | |
1 | 1839 else |
1840 dirhash = NULL; | |
1841 | |
509 | 1842 *ret = gftp_list_files (transfer->fromreq); |
1843 if (*ret < 0) | |
1 | 1844 return (NULL); |
1845 | |
1846 fle = g_malloc (sizeof (*fle)); | |
1847 templist = NULL; | |
1848 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
1849 { | |
1850 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) | |
1851 { | |
1852 gftp_file_destroy (fle); | |
1853 continue; | |
1854 } | |
1855 | |
1856 if (dirhash && | |
1857 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
1858 fle->startsize = *newsize; | |
1859 | |
381 | 1860 if (transfer->toreq && fle->destfile == NULL) |
245 | 1861 fle->destfile = gftp_build_path (transfer->toreq->directory, |
1862 fle->file, NULL); | |
1863 | |
381 | 1864 if (transfer->fromreq->directory != NULL && |
1865 *transfer->fromreq->directory != '\0' && | |
1866 *fle->file != '/') | |
1867 { | |
1868 newname = gftp_build_path (transfer->fromreq->directory, | |
1869 fle->file, NULL); | |
1870 | |
1871 g_free (fle->file); | |
1872 fle->file = newname; | |
1873 } | |
1 | 1874 |
1875 templist = g_list_append (templist, fle); | |
1876 | |
1877 fle = g_malloc (sizeof (*fle)); | |
1878 } | |
1879 gftp_end_transfer (transfer->fromreq); | |
1880 | |
1881 gftp_file_destroy (fle); | |
1882 g_free (fle); | |
1883 | |
509 | 1884 if (dirhash != NULL) |
1 | 1885 gftp_destroy_dir_hash (dirhash); |
1886 | |
1887 return (templist); | |
1888 } | |
1889 | |
1890 | |
1891 int | |
1892 gftp_get_all_subdirs (gftp_transfer * transfer, | |
1893 void (*update_func) (gftp_transfer * transfer)) | |
1894 { | |
381 | 1895 char *oldfromdir, *oldtodir, *newname, *pos; |
469 | 1896 int forcecd, remotechanged, ret; |
1 | 1897 GList * templist, * lastlist; |
1898 GHashTable * dirhash; | |
1899 gftp_file * curfle; | |
516 | 1900 off_t *newsize; |
500 | 1901 mode_t st_mode; |
1 | 1902 |
84 | 1903 g_return_val_if_fail (transfer != NULL, GFTP_EFATAL); |
1904 g_return_val_if_fail (transfer->fromreq != NULL, GFTP_EFATAL); | |
1905 g_return_val_if_fail (transfer->files != NULL, GFTP_EFATAL); | |
1 | 1906 |
1907 if (transfer->toreq != NULL) | |
469 | 1908 { |
1909 ret = 0; | |
1910 dirhash = gftp_gen_dir_hash (transfer->toreq, &ret); | |
1911 if (ret < 0) | |
1912 return (ret); | |
1913 } | |
1 | 1914 else |
1915 dirhash = NULL; | |
1916 | |
1917 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
1918 { | |
1919 curfle = lastlist->data; | |
381 | 1920 |
1921 if ((pos = strrchr (curfle->file, '/')) != NULL) | |
1922 pos++; | |
1923 else | |
1924 pos = curfle->file; | |
1925 | |
1926 if (dirhash != NULL && | |
1927 (newsize = g_hash_table_lookup (dirhash, pos)) != NULL) | |
1 | 1928 curfle->startsize = *newsize; |
1929 | |
381 | 1930 if (curfle->size < 0 && GFTP_IS_CONNECTED (transfer->fromreq)) |
509 | 1931 { |
1932 curfle->size = gftp_get_file_size (transfer->fromreq, curfle->file); | |
1933 if (curfle->size < 0) | |
1934 return (curfle->size); | |
1935 } | |
381 | 1936 |
1937 if (transfer->toreq && curfle->destfile == NULL) | |
1938 curfle->destfile = gftp_build_path (transfer->toreq->directory, | |
1939 curfle->file, NULL); | |
1940 | |
1941 if (transfer->fromreq->directory != NULL && | |
509 | 1942 *transfer->fromreq->directory != '\0' && *curfle->file != '/') |
381 | 1943 { |
1944 newname = gftp_build_path (transfer->fromreq->directory, | |
1945 curfle->file, NULL); | |
1946 g_free (curfle->file); | |
1947 curfle->file = newname; | |
1948 } | |
1 | 1949 |
1950 if (lastlist->next == NULL) | |
1951 break; | |
1952 } | |
1953 | |
509 | 1954 if (dirhash != NULL) |
1 | 1955 gftp_destroy_dir_hash (dirhash); |
1956 | |
1957 oldfromdir = oldtodir = NULL; | |
1958 remotechanged = 0; | |
1959 forcecd = 0; | |
1960 for (templist = transfer->files; templist != NULL; templist = templist->next) | |
1961 { | |
1962 curfle = templist->data; | |
1963 | |
500 | 1964 if (S_ISLNK (curfle->st_mode) && !S_ISDIR (curfle->st_mode)) |
1965 { | |
1966 st_mode = gftp_stat_filename (transfer->fromreq, curfle->file); | |
509 | 1967 if (st_mode < 0) |
1968 return (st_mode); | |
1969 else if (S_ISDIR (st_mode)) | |
500 | 1970 curfle->st_mode = st_mode; |
1971 } | |
1972 | |
1973 if (curfle->st_mode & S_IFDIR) | |
1 | 1974 { |
1975 oldfromdir = transfer->fromreq->directory; | |
1976 transfer->fromreq->directory = curfle->file; | |
1977 | |
509 | 1978 if (transfer->toreq != NULL) |
1 | 1979 { |
1980 oldtodir = transfer->toreq->directory; | |
1981 transfer->toreq->directory = curfle->destfile; | |
1982 } | |
509 | 1983 |
1 | 1984 forcecd = 1; |
509 | 1985 ret = gftp_set_directory (transfer->fromreq, |
1986 transfer->fromreq->directory); | |
1987 if (ret < 0) | |
1988 return (ret); | |
1989 | |
1990 if (curfle->startsize > 0 && transfer->toreq != NULL) | |
1 | 1991 { |
509 | 1992 remotechanged = 1; |
1993 ret = gftp_set_directory (transfer->toreq, | |
1994 transfer->toreq->directory); | |
469 | 1995 if (ret < 0) |
509 | 1996 return (ret); |
1997 } | |
1998 | |
1999 ret = 0; | |
2000 lastlist->next = gftp_get_dir_listing (transfer, | |
2001 curfle->startsize > 0, | |
2002 &ret); | |
2003 if (ret < 0) | |
2004 return (ret); | |
2005 | |
2006 if (lastlist->next != NULL) | |
2007 { | |
2008 lastlist->next->prev = lastlist; | |
2009 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
1 | 2010 } |
509 | 2011 |
2012 transfer->numdirs++; | |
2013 if (update_func != NULL) | |
2014 update_func (transfer); | |
1 | 2015 |
2016 transfer->fromreq->directory = oldfromdir; | |
509 | 2017 if (transfer->toreq != NULL) |
1 | 2018 transfer->toreq->directory = oldtodir; |
2019 } | |
2020 else | |
122 | 2021 transfer->numfiles++; |
1 | 2022 } |
2023 | |
2024 if (forcecd) | |
509 | 2025 { |
2026 ret = gftp_set_directory (transfer->fromreq, | |
2027 transfer->fromreq->directory); | |
2028 if (ret < 0) | |
2029 return (ret); | |
2030 } | |
2031 | |
2032 if (remotechanged && transfer->toreq != NULL) | |
2033 { | |
2034 ret = gftp_set_directory (transfer->toreq, transfer->toreq->directory); | |
2035 if (ret < 0) | |
2036 return (ret); | |
2037 } | |
2038 | |
2039 if (update_func != NULL) | |
1 | 2040 { |
2041 transfer->numfiles = transfer->numdirs = -1; | |
2042 update_func (transfer); | |
2043 } | |
509 | 2044 |
1 | 2045 return (0); |
2046 } | |
2047 | |
2048 | |
2049 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
122 | 2050 static int |
1 | 2051 get_port (struct addrinfo *addr) |
2052 { | |
2053 struct sockaddr_in * saddr; | |
2054 int port; | |
2055 | |
2056 if (addr->ai_family == AF_INET) | |
2057 { | |
2058 saddr = (struct sockaddr_in *) addr->ai_addr; | |
2059 port = ntohs (saddr->sin_port); | |
2060 } | |
2061 else | |
2062 port = 0; | |
2063 | |
2064 return (port); | |
2065 } | |
2066 #endif | |
2067 | |
2068 | |
2069 int | |
122 | 2070 gftp_connect_server (gftp_request * request, char *service, |
516 | 2071 char *proxy_hostname, unsigned int proxy_port) |
1 | 2072 { |
2073 char *connect_host, *disphost; | |
516 | 2074 unsigned int port; |
2075 int sock = -1; | |
1 | 2076 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
2077 struct addrinfo hints, *res; | |
463 | 2078 intptr_t enable_ipv6; |
1 | 2079 char serv[8]; |
463 | 2080 int errnum; |
1 | 2081 |
516 | 2082 if ((errnum = gftp_need_proxy (request, service, proxy_hostname, |
2083 proxy_port)) < 0) | |
2084 return (errnum); | |
2085 else | |
2086 { | |
2087 request->use_proxy = errnum; | |
2088 if (request->use_proxy) | |
2089 request->hostp = NULL; | |
2090 } | |
1 | 2091 |
313 | 2092 gftp_lookup_request_option (request, "enable_ipv6", &enable_ipv6); |
2093 | |
151 | 2094 request->free_hostp = 1; |
1 | 2095 memset (&hints, 0, sizeof (hints)); |
2096 hints.ai_flags = AI_CANONNAME; | |
313 | 2097 |
2098 if (enable_ipv6) | |
2099 hints.ai_family = PF_UNSPEC; | |
2100 else | |
2101 hints.ai_family = AF_INET; | |
2102 | |
1 | 2103 hints.ai_socktype = SOCK_STREAM; |
2104 | |
122 | 2105 if (request->use_proxy) |
2106 { | |
2107 connect_host = proxy_hostname; | |
2108 port = proxy_port; | |
2109 } | |
2110 else | |
2111 { | |
2112 connect_host = request->hostname; | |
2113 port = request->port; | |
2114 } | |
1 | 2115 |
2116 if (request->hostp == NULL) | |
2117 { | |
2118 if (port == 0) | |
2119 strcpy (serv, service); | |
2120 else | |
2121 snprintf (serv, sizeof (serv), "%d", port); | |
2122 | |
186 | 2123 request->logging_function (gftp_logging_misc, request, |
168 | 2124 _("Looking up %s\n"), connect_host); |
1 | 2125 if ((errnum = getaddrinfo (connect_host, serv, &hints, |
2126 &request->hostp)) != 0) | |
168 | 2127 { |
186 | 2128 request->logging_function (gftp_logging_error, request, |
168 | 2129 _("Cannot look up hostname %s: %s\n"), |
2130 connect_host, gai_strerror (errnum)); | |
2131 return (GFTP_ERETRYABLE); | |
2132 } | |
1 | 2133 } |
2134 | |
2135 disphost = connect_host; | |
2136 for (res = request->hostp; res != NULL; res = res->ai_next) | |
2137 { | |
2138 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
2139 port = get_port (res); | |
56 | 2140 if (!request->use_proxy) |
2141 request->port = port; | |
2142 | |
1 | 2143 if ((sock = socket (res->ai_family, res->ai_socktype, |
2144 res->ai_protocol)) < 0) | |
2145 { | |
186 | 2146 request->logging_function (gftp_logging_error, request, |
1 | 2147 _("Failed to create a socket: %s\n"), |
2148 g_strerror (errno)); | |
2149 continue; | |
66 | 2150 } |
1 | 2151 |
186 | 2152 request->logging_function (gftp_logging_misc, request, |
168 | 2153 _("Trying %s:%d\n"), disphost, port); |
1 | 2154 |
2155 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
168 | 2156 { |
186 | 2157 request->logging_function (gftp_logging_error, request, |
168 | 2158 _("Cannot connect to %s: %s\n"), |
2159 disphost, g_strerror (errno)); | |
1 | 2160 close (sock); |
2161 continue; | |
168 | 2162 } |
1 | 2163 break; |
2164 } | |
2165 | |
2166 if (res == NULL) | |
2167 { | |
2168 if (request->hostp != NULL) | |
2169 { | |
2170 freeaddrinfo (request->hostp); | |
2171 request->hostp = NULL; | |
2172 } | |
84 | 2173 return (GFTP_ERETRYABLE); |
1 | 2174 } |
2175 | |
2176 #else /* !HAVE_GETADDRINFO */ | |
2177 struct sockaddr_in remote_address; | |
2178 struct servent serv_struct; | |
2179 int curhost; | |
2180 | |
122 | 2181 if ((request->use_proxy = gftp_need_proxy (request, service, |
2182 proxy_hostname, proxy_port)) < 0) | |
84 | 2183 return (request->use_proxy); |
1 | 2184 else if (request->use_proxy == 1) |
2185 request->hostp = NULL; | |
2186 | |
516 | 2187 g_return_val_if_fail (sock != -1, GFTP_EFATAL); |
2188 | |
1 | 2189 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) |
2190 { | |
186 | 2191 request->logging_function (gftp_logging_error, request, |
313 | 2192 _("Failed to create a IPv4 socket: %s\n"), |
1 | 2193 g_strerror (errno)); |
84 | 2194 return (GFTP_ERETRYABLE); |
1 | 2195 } |
2196 | |
2197 memset (&remote_address, 0, sizeof (remote_address)); | |
2198 remote_address.sin_family = AF_INET; | |
2199 | |
122 | 2200 if (request->use_proxy) |
2201 { | |
2202 connect_host = proxy_hostname; | |
2203 port = proxy_port; | |
2204 } | |
2205 else | |
2206 { | |
2207 connect_host = request->hostname; | |
2208 port = request->port; | |
2209 } | |
1 | 2210 |
2211 if (port == 0) | |
2212 { | |
2213 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
2214 { | |
186 | 2215 request->logging_function (gftp_logging_error, request, |
122 | 2216 _("Cannot look up service name %s/tcp. Please check your services file\n"), |
2217 service); | |
2218 close (sock); | |
2219 return (GFTP_EFATAL); | |
1 | 2220 } |
451 | 2221 |
2222 port = ntohs (serv_struct.s_port); | |
56 | 2223 |
2224 if (!request->use_proxy) | |
451 | 2225 request->port = port; |
1 | 2226 } |
451 | 2227 |
2228 remote_address.sin_port = htons (port); | |
1 | 2229 |
2230 if (request->hostp == NULL) | |
2231 { | |
186 | 2232 request->logging_function (gftp_logging_misc, request, |
168 | 2233 _("Looking up %s\n"), connect_host); |
1 | 2234 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, |
2235 NULL))) | |
2236 { | |
186 | 2237 request->logging_function (gftp_logging_error, request, |
1 | 2238 _("Cannot look up hostname %s: %s\n"), |
2239 connect_host, g_strerror (errno)); | |
2240 close (sock); | |
84 | 2241 return (GFTP_ERETRYABLE); |
1 | 2242 } |
2243 } | |
2244 | |
2245 disphost = NULL; | |
2246 for (curhost = 0; request->host.h_addr_list[curhost] != NULL; curhost++) | |
2247 { | |
2248 disphost = request->host.h_name; | |
2249 memcpy (&remote_address.sin_addr, request->host.h_addr_list[curhost], | |
2250 request->host.h_length); | |
186 | 2251 request->logging_function (gftp_logging_misc, request, |
1 | 2252 _("Trying %s:%d\n"), |
451 | 2253 request->host.h_name, port); |
1 | 2254 |
2255 if (connect (sock, (struct sockaddr *) &remote_address, | |
2256 sizeof (remote_address)) == -1) | |
2257 { | |
186 | 2258 request->logging_function (gftp_logging_error, request, |
1 | 2259 _("Cannot connect to %s: %s\n"), |
2260 connect_host, g_strerror (errno)); | |
2261 } | |
2262 break; | |
2263 } | |
2264 | |
2265 if (request->host.h_addr_list[curhost] == NULL) | |
2266 { | |
2267 close (sock); | |
84 | 2268 return (GFTP_ERETRYABLE); |
1 | 2269 } |
2270 #endif /* HAVE_GETADDRINFO */ | |
2271 | |
182 | 2272 if (fcntl (sock, F_SETFD, 1) == -1) |
2273 { | |
186 | 2274 request->logging_function (gftp_logging_error, request, |
182 | 2275 _("Error: Cannot set close on exec flag: %s\n"), |
2276 g_strerror (errno)); | |
2277 | |
2278 return (GFTP_ERETRYABLE); | |
2279 } | |
2280 | |
186 | 2281 request->logging_function (gftp_logging_misc, request, |
168 | 2282 _("Connected to %s:%d\n"), connect_host, port); |
58 | 2283 |
168 | 2284 if (gftp_fd_set_sockblocking (request, sock, 1) < 0) |
58 | 2285 { |
2286 close (sock); | |
84 | 2287 return (GFTP_ERETRYABLE); |
58 | 2288 } |
2289 | |
169 | 2290 request->datafd = sock; |
168 | 2291 |
2292 if (request->post_connect != NULL) | |
2293 return (request->post_connect (request)); | |
2294 | |
2295 return (0); | |
1 | 2296 } |
2297 | |
2298 | |
177 | 2299 int |
1 | 2300 gftp_set_config_options (gftp_request * request) |
2301 { | |
58 | 2302 if (request->set_config_options != NULL) |
177 | 2303 return (request->set_config_options (request)); |
2304 else | |
2305 return (0); | |
1 | 2306 } |
2307 | |
2308 | |
2309 void | |
2310 print_file_list (GList * list) | |
2311 { | |
2312 gftp_file * tempfle; | |
2313 GList * templist; | |
499 | 2314 char *attribs; |
1 | 2315 |
2316 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
2317 for (templist = list; ; templist = templist->next) | |
2318 { | |
2319 tempfle = templist->data; | |
499 | 2320 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
2321 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2322 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2323 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
372 | 2324 tempfle->file, tempfle->destfile, |
516 | 2325 tempfle->size, tempfle->startsize, |
499 | 2326 tempfle->user, tempfle->group, attribs); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2327 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2328 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
16 | 2329 tempfle->file, tempfle->destfile, |
2330 tempfle->size, tempfle->startsize, | |
499 | 2331 tempfle->user, tempfle->group, attribs); |
372 | 2332 #endif |
499 | 2333 |
2334 g_free (attribs); | |
1 | 2335 if (templist->next == NULL) |
2336 break; | |
2337 } | |
2338 | |
2339 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
2340 for (; ; templist = templist->prev) | |
2341 { | |
2342 tempfle = templist->data; | |
499 | 2343 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
2344 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2345 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2346 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
372 | 2347 tempfle->file, tempfle->destfile, |
516 | 2348 tempfle->size, tempfle->startsize, |
499 | 2349 tempfle->user, tempfle->group, attribs); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2350 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
2351 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
16 | 2352 tempfle->file, tempfle->destfile, |
2353 tempfle->size, tempfle->startsize, | |
499 | 2354 tempfle->user, tempfle->group, attribs); |
372 | 2355 #endif |
499 | 2356 |
2357 g_free (attribs); | |
1 | 2358 if (templist == list) |
2359 break; | |
2360 } | |
2361 printf ("--END OF FILE LISTING--\n"); | |
2362 } | |
2363 | |
41 | 2364 |
201 | 2365 void |
58 | 2366 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) |
41 | 2367 { |
58 | 2368 g_free ((*rbuf)->buffer); |
2369 g_free (*rbuf); | |
2370 *rbuf = NULL; | |
41 | 2371 } |
2372 | |
2373 | |
58 | 2374 ssize_t |
2375 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
2376 char * str, size_t len, int fd) | |
41 | 2377 { |
168 | 2378 ssize_t (*read_function) (gftp_request * request, void *ptr, size_t size, |
2379 int fd); | |
516 | 2380 char *pos, *nextpos; |
2381 size_t rlen, nslen; | |
179 | 2382 int end_of_buffer; |
516 | 2383 ssize_t ret; |
168 | 2384 |
2385 if (request == NULL || request->read_function == NULL) | |
2386 read_function = gftp_fd_read; | |
2387 else | |
2388 read_function = request->read_function; | |
58 | 2389 |
2390 if (*rbuf == NULL) | |
2391 { | |
2392 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
2393 (*rbuf)->max_bufsize = len; | |
249 | 2394 (*rbuf)->buffer = g_malloc0 ((*rbuf)->max_bufsize + 1); |
168 | 2395 |
2396 if ((ret = read_function (request, (*rbuf)->buffer, | |
2397 (*rbuf)->max_bufsize, fd)) <= 0) | |
58 | 2398 { |
2399 gftp_free_getline_buffer (rbuf); | |
2400 return (ret); | |
2401 } | |
60 | 2402 (*rbuf)->buffer[ret] = '\0'; |
58 | 2403 (*rbuf)->cur_bufsize = ret; |
2404 (*rbuf)->curpos = (*rbuf)->buffer; | |
2405 } | |
2406 | |
516 | 2407 ret = 0; |
2408 while (1) | |
58 | 2409 { |
179 | 2410 pos = strchr ((*rbuf)->curpos, '\n'); |
2411 end_of_buffer = (*rbuf)->curpos == (*rbuf)->buffer && | |
2412 ((*rbuf)->max_bufsize == (*rbuf)->cur_bufsize || (*rbuf)->eof); | |
2413 | |
2414 if ((*rbuf)->cur_bufsize > 0 && (pos != NULL || end_of_buffer)) | |
58 | 2415 { |
2416 if (pos != NULL) | |
2417 { | |
516 | 2418 nslen = pos - (*rbuf)->curpos + 1; |
58 | 2419 nextpos = pos + 1; |
2420 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
2421 pos--; | |
2422 *pos = '\0'; | |
2423 } | |
2424 else | |
249 | 2425 { |
516 | 2426 nslen = (*rbuf)->cur_bufsize; |
249 | 2427 nextpos = NULL; |
2428 | |
2429 /* This is not an overflow since we allocated one extra byte to | |
2430 buffer above */ | |
516 | 2431 ((*rbuf)->curpos)[nslen] = '\0'; |
249 | 2432 } |
58 | 2433 |
2434 strncpy (str, (*rbuf)->curpos, len); | |
249 | 2435 str[len - 1] = '\0'; |
516 | 2436 (*rbuf)->cur_bufsize -= nslen; |
58 | 2437 |
168 | 2438 if (nextpos != NULL) |
2439 (*rbuf)->curpos = nextpos; | |
2440 else | |
2441 (*rbuf)->cur_bufsize = 0; | |
516 | 2442 |
2443 ret = nslen; | |
58 | 2444 break; |
2445 } | |
2446 else | |
2447 { | |
2448 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
2449 { | |
2450 rlen = (*rbuf)->max_bufsize; | |
2451 pos = (*rbuf)->buffer; | |
2452 } | |
2453 else | |
2454 { | |
168 | 2455 memmove ((*rbuf)->buffer, (*rbuf)->curpos, (*rbuf)->cur_bufsize); |
2456 pos = (*rbuf)->buffer + (*rbuf)->cur_bufsize; | |
2457 rlen = (*rbuf)->max_bufsize - (*rbuf)->cur_bufsize; | |
58 | 2458 } |
168 | 2459 |
58 | 2460 (*rbuf)->curpos = (*rbuf)->buffer; |
2461 | |
209 | 2462 if ((*rbuf)->eof) |
2463 ret = 0; | |
2464 else | |
2465 ret = read_function (request, pos, rlen, fd); | |
2466 | |
2467 if (ret < 0) | |
58 | 2468 { |
2469 gftp_free_getline_buffer (rbuf); | |
2470 return (ret); | |
2471 } | |
179 | 2472 else if (ret == 0) |
168 | 2473 { |
179 | 2474 if ((*rbuf)->cur_bufsize == 0) |
2475 { | |
2476 gftp_free_getline_buffer (rbuf); | |
2477 return (ret); | |
2478 } | |
2479 | |
2480 (*rbuf)->eof = 1; | |
168 | 2481 } |
2482 | |
2483 (*rbuf)->cur_bufsize += ret; | |
215 | 2484 (*rbuf)->curpos[(*rbuf)->cur_bufsize] = '\0'; |
58 | 2485 } |
2486 } | |
516 | 2487 |
2488 return (ret); | |
58 | 2489 } |
2490 | |
2491 | |
2492 ssize_t | |
168 | 2493 gftp_fd_read (gftp_request * request, void *ptr, size_t size, int fd) |
58 | 2494 { |
325 | 2495 intptr_t network_timeout; |
58 | 2496 struct timeval tv; |
2497 fd_set fset; | |
2498 ssize_t ret; | |
41 | 2499 |
122 | 2500 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
2501 | |
41 | 2502 errno = 0; |
2503 ret = 0; | |
2504 do | |
2505 { | |
58 | 2506 FD_ZERO (&fset); |
2507 FD_SET (fd, &fset); | |
122 | 2508 tv.tv_sec = network_timeout; |
58 | 2509 tv.tv_usec = 0; |
2510 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
2511 if (ret == -1 && errno == EINTR) | |
41 | 2512 { |
58 | 2513 if (request && request->cancel) |
2514 break; | |
2515 else | |
2516 continue; | |
2517 } | |
2518 else if (ret <= 0) | |
2519 { | |
2520 if (request != NULL) | |
41 | 2521 { |
186 | 2522 request->logging_function (gftp_logging_error, request, |
58 | 2523 _("Connection to %s timed out\n"), |
2524 request->hostname); | |
2525 gftp_disconnect (request); | |
2526 } | |
84 | 2527 return (GFTP_ERETRYABLE); |
41 | 2528 } |
2529 | |
58 | 2530 if ((ret = read (fd, ptr, size)) < 0) |
41 | 2531 { |
2532 if (errno == EINTR) | |
2533 { | |
58 | 2534 if (request != NULL && request->cancel) |
41 | 2535 break; |
2536 else | |
58 | 2537 continue; |
41 | 2538 } |
2539 | |
58 | 2540 if (request != NULL) |
2541 { | |
186 | 2542 request->logging_function (gftp_logging_error, request, |
58 | 2543 _("Error: Could not read from socket: %s\n"), |
41 | 2544 g_strerror (errno)); |
58 | 2545 gftp_disconnect (request); |
2546 } | |
84 | 2547 return (GFTP_ERETRYABLE); |
41 | 2548 } |
2549 } | |
58 | 2550 while (errno == EINTR && !(request != NULL && request->cancel)); |
41 | 2551 |
58 | 2552 if (errno == EINTR && request != NULL && request->cancel) |
41 | 2553 { |
2554 gftp_disconnect (request); | |
84 | 2555 return (GFTP_ERETRYABLE); |
41 | 2556 } |
2557 | |
2558 return (ret); | |
2559 } | |
2560 | |
58 | 2561 |
2562 ssize_t | |
168 | 2563 gftp_fd_write (gftp_request * request, const char *ptr, size_t size, int fd) |
58 | 2564 { |
325 | 2565 intptr_t network_timeout; |
58 | 2566 struct timeval tv; |
248 | 2567 ssize_t w_ret; |
58 | 2568 fd_set fset; |
516 | 2569 int ret; |
58 | 2570 |
122 | 2571 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
2572 | |
58 | 2573 errno = 0; |
2574 ret = 0; | |
2575 do | |
2576 { | |
2577 FD_ZERO (&fset); | |
2578 FD_SET (fd, &fset); | |
122 | 2579 tv.tv_sec = network_timeout; |
58 | 2580 tv.tv_usec = 0; |
2581 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
2582 if (ret == -1 && errno == EINTR) | |
2583 { | |
2584 if (request != NULL && request->cancel) | |
2585 break; | |
2586 else | |
2587 continue; | |
2588 } | |
2589 else if (ret <= 0) | |
2590 { | |
2591 if (request != NULL) | |
2592 { | |
186 | 2593 request->logging_function (gftp_logging_error, request, |
58 | 2594 _("Connection to %s timed out\n"), |
2595 request->hostname); | |
2596 gftp_disconnect (request); | |
2597 } | |
84 | 2598 return (GFTP_ERETRYABLE); |
58 | 2599 } |
2600 | |
248 | 2601 w_ret = write (fd, ptr, size); |
2602 if (w_ret < 0) | |
58 | 2603 { |
2604 if (errno == EINTR) | |
2605 { | |
2606 if (request != NULL && request->cancel) | |
2607 break; | |
2608 else | |
2609 continue; | |
2610 } | |
2611 | |
2612 if (request != NULL) | |
2613 { | |
186 | 2614 request->logging_function (gftp_logging_error, request, |
58 | 2615 _("Error: Could not write to socket: %s\n"), |
2616 g_strerror (errno)); | |
2617 gftp_disconnect (request); | |
2618 } | |
84 | 2619 return (GFTP_ERETRYABLE); |
58 | 2620 } |
2621 | |
2622 ptr += w_ret; | |
2623 size -= w_ret; | |
2624 ret += w_ret; | |
2625 } | |
2626 while (size > 0); | |
2627 | |
2628 if (errno == EINTR && request != NULL && request->cancel) | |
2629 { | |
2630 gftp_disconnect (request); | |
84 | 2631 return (GFTP_ERETRYABLE); |
58 | 2632 } |
2633 | |
2634 return (ret); | |
2635 } | |
2636 | |
2637 | |
2638 ssize_t | |
2639 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
2640 { | |
2641 char *tempstr; | |
2642 va_list argp; | |
2643 ssize_t ret; | |
2644 | |
2645 va_start (argp, fmt); | |
2646 tempstr = g_strdup_vprintf (fmt, argp); | |
2647 va_end (argp); | |
2648 | |
168 | 2649 ret = request->write_function (request, tempstr, strlen (tempstr), fd); |
58 | 2650 g_free (tempstr); |
2651 return (ret); | |
2652 } | |
2653 | |
2654 | |
2655 int | |
168 | 2656 gftp_fd_set_sockblocking (gftp_request * request, int fd, int non_blocking) |
58 | 2657 { |
2658 int flags; | |
2659 | |
84 | 2660 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) |
58 | 2661 { |
186 | 2662 request->logging_function (gftp_logging_error, request, |
58 | 2663 _("Cannot get socket flags: %s\n"), |
2664 g_strerror (errno)); | |
2665 gftp_disconnect (request); | |
84 | 2666 return (GFTP_ERETRYABLE); |
58 | 2667 } |
2668 | |
2669 if (non_blocking) | |
2670 flags |= O_NONBLOCK; | |
2671 else | |
2672 flags &= ~O_NONBLOCK; | |
2673 | |
84 | 2674 if (fcntl (fd, F_SETFL, flags) < 0) |
58 | 2675 { |
186 | 2676 request->logging_function (gftp_logging_error, request, |
58 | 2677 _("Cannot set socket to non-blocking: %s\n"), |
2678 g_strerror (errno)); | |
2679 gftp_disconnect (request); | |
84 | 2680 return (GFTP_ERETRYABLE); |
58 | 2681 } |
2682 | |
2683 return (0); | |
2684 } | |
2685 | |
2686 | |
63 | 2687 void |
2688 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
2689 { | |
2690 g_return_if_fail (dest != NULL); | |
2691 g_return_if_fail (source != NULL); | |
2692 g_return_if_fail (dest->protonum == source->protonum); | |
2693 | |
2694 dest->datafd = source->datafd; | |
2695 dest->cached = 0; | |
397 | 2696 #ifdef USE_SSL |
2697 dest->ssl = source->ssl; | |
2698 #endif | |
2699 | |
63 | 2700 if (!source->always_connected) |
2701 { | |
2702 source->datafd = -1; | |
2703 source->cached = 1; | |
397 | 2704 #ifdef USE_SSL |
2705 source->ssl = NULL; | |
2706 #endif | |
63 | 2707 } |
2708 | |
516 | 2709 if (dest->swap_socks != NULL) |
63 | 2710 dest->swap_socks (dest, source); |
2711 } | |
2712 | |
122 | 2713 |
2714 void | |
2715 gftp_calc_kbs (gftp_transfer * tdata, ssize_t num_read) | |
2716 { | |
469 | 2717 /* Needed for systems that size(float) < size(void *) */ |
2718 union { intptr_t i; float f; } maxkbs; | |
122 | 2719 unsigned long waitusecs; |
220 | 2720 double start_difftime; |
122 | 2721 struct timeval tv; |
222 | 2722 int waited; |
122 | 2723 |
469 | 2724 gftp_lookup_request_option (tdata->fromreq, "maxkbs", &maxkbs.f); |
122 | 2725 |
2726 if (g_thread_supported ()) | |
2727 g_static_mutex_lock (&tdata->statmutex); | |
2728 | |
220 | 2729 gettimeofday (&tv, NULL); |
2730 | |
122 | 2731 tdata->trans_bytes += num_read; |
2732 tdata->curtrans += num_read; | |
2733 tdata->stalled = 0; | |
2734 | |
220 | 2735 start_difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0); |
2736 | |
2737 if (start_difftime <= 0) | |
2738 tdata->kbs = tdata->trans_bytes / 1024.0; | |
122 | 2739 else |
220 | 2740 tdata->kbs = tdata->trans_bytes / 1024.0 / start_difftime; |
2741 | |
222 | 2742 waited = 0; |
469 | 2743 if (maxkbs.f > 0 && tdata->kbs > maxkbs.f) |
122 | 2744 { |
469 | 2745 waitusecs = num_read / 1024.0 / maxkbs.f * 1000000.0 - start_difftime; |
122 | 2746 |
2747 if (waitusecs > 0) | |
2748 { | |
2749 if (g_thread_supported ()) | |
2750 g_static_mutex_unlock (&tdata->statmutex); | |
2751 | |
222 | 2752 waited = 1; |
122 | 2753 usleep (waitusecs); |
2754 | |
2755 if (g_thread_supported ()) | |
2756 g_static_mutex_lock (&tdata->statmutex); | |
2757 } | |
222 | 2758 |
122 | 2759 } |
2760 | |
222 | 2761 if (waited) |
2762 gettimeofday (&tdata->lasttime, NULL); | |
2763 else | |
2764 memcpy (&tdata->lasttime, &tv, sizeof (tdata->lasttime)); | |
122 | 2765 |
2766 if (g_thread_supported ()) | |
2767 g_static_mutex_unlock (&tdata->statmutex); | |
2768 } | |
2769 | |
125 | 2770 |
2771 int | |
2772 gftp_get_transfer_status (gftp_transfer * tdata, ssize_t num_read) | |
2773 { | |
325 | 2774 intptr_t retries, sleep_time; |
125 | 2775 gftp_file * tempfle; |
2776 struct timeval tv; | |
498 | 2777 int ret1, ret2; |
125 | 2778 |
2779 ret1 = ret2 = 0; | |
2780 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
2781 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
2782 | |
2783 if (g_thread_supported ()) | |
2784 g_static_mutex_lock (&tdata->structmutex); | |
2785 | |
2786 if (tdata->curfle == NULL) | |
2787 { | |
2788 if (g_thread_supported ()) | |
2789 g_static_mutex_unlock (&tdata->structmutex); | |
2790 | |
2791 return (GFTP_EFATAL); | |
2792 } | |
2793 | |
2794 tempfle = tdata->curfle->data; | |
2795 | |
2796 if (g_thread_supported ()) | |
2797 g_static_mutex_unlock (&tdata->structmutex); | |
2798 | |
2799 gftp_disconnect (tdata->fromreq); | |
2800 gftp_disconnect (tdata->toreq); | |
2801 | |
2802 if (num_read < 0 || tdata->skip_file) | |
2803 { | |
2804 if (num_read == GFTP_EFATAL) | |
2805 return (GFTP_EFATAL); | |
512 | 2806 else if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout) |
125 | 2807 { |
303 | 2808 if (retries != 0 && |
2809 tdata->current_file_retries >= retries) | |
2810 { | |
2811 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, | |
2812 _("Error: Remote site %s disconnected. Max retries reached...giving up\n"), | |
2813 tdata->fromreq->hostname != NULL ? | |
2814 tdata->fromreq->hostname : tdata->toreq->hostname); | |
2815 return (GFTP_EFATAL); | |
2816 } | |
2817 else | |
2818 { | |
2819 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, | |
2820 _("Error: Remote site %s disconnected. Will reconnect in %d seconds\n"), | |
2821 tdata->fromreq->hostname != NULL ? | |
2822 tdata->fromreq->hostname : tdata->toreq->hostname, | |
2823 sleep_time); | |
2824 } | |
125 | 2825 } |
2826 | |
2827 while (retries == 0 || | |
2828 tdata->current_file_retries <= retries) | |
2829 { | |
512 | 2830 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout && |
2831 !tdata->skip_file) | |
125 | 2832 { |
2833 tv.tv_sec = sleep_time; | |
2834 tv.tv_usec = 0; | |
498 | 2835 |
2836 do | |
2837 { | |
2838 ret1 = select (0, NULL, NULL, NULL, &tv); | |
2839 } | |
2840 while (ret1 == -1 && errno == EINTR); | |
125 | 2841 } |
2842 | |
2843 if ((ret1 = gftp_connect (tdata->fromreq)) == 0 && | |
2844 (ret2 = gftp_connect (tdata->toreq)) == 0) | |
2845 { | |
2846 if (g_thread_supported ()) | |
2847 g_static_mutex_lock (&tdata->structmutex); | |
2848 | |
2849 tdata->resumed_bytes = tdata->resumed_bytes + tdata->trans_bytes - tdata->curresumed - tdata->curtrans; | |
2850 tdata->trans_bytes = 0; | |
2851 if (tdata->skip_file) | |
2852 { | |
2853 tdata->total_bytes -= tempfle->size; | |
2854 tdata->curtrans = 0; | |
2855 | |
2856 tdata->curfle = tdata->curfle->next; | |
2857 tdata->next_file = 1; | |
2858 tdata->skip_file = 0; | |
2859 tdata->cancel = 0; | |
2860 tdata->fromreq->cancel = 0; | |
2861 tdata->toreq->cancel = 0; | |
2862 } | |
2863 else | |
2864 { | |
2865 tempfle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
2866 tempfle->startsize = tdata->curtrans + tdata->curresumed; | |
2867 /* We decrement this here because it will be incremented in | |
2868 the loop again */ | |
2869 tdata->curresumed = 0; | |
2870 tdata->current_file_number--; /* Decrement this because it | |
2871 will be incremented when we | |
2872 continue in the loop */ | |
2873 } | |
2874 | |
2875 gettimeofday (&tdata->starttime, NULL); | |
2876 | |
2877 if (g_thread_supported ()) | |
2878 g_static_mutex_unlock (&tdata->structmutex); | |
2879 | |
2880 return (GFTP_ERETRYABLE); | |
2881 } | |
2882 else if (ret1 == GFTP_EFATAL || ret2 == GFTP_EFATAL) | |
2883 { | |
2884 gftp_disconnect (tdata->fromreq); | |
2885 gftp_disconnect (tdata->toreq); | |
2886 return (GFTP_EFATAL); | |
2887 } | |
2888 else | |
2889 tdata->current_file_retries++; | |
2890 } | |
2891 } | |
2892 else if (tdata->cancel) | |
2893 return (GFTP_EFATAL); | |
2894 | |
2895 return (0); | |
2896 } | |
2897 | |
182 | 2898 |
2899 int | |
2900 gftp_fd_open (gftp_request * request, const char *pathname, int flags, mode_t mode) | |
2901 { | |
2902 int fd; | |
2903 | |
227 | 2904 if (mode == 0) |
2905 fd = open (pathname, flags); | |
2906 else | |
2907 fd = open (pathname, flags, mode); | |
2908 | |
2909 if (fd < 0) | |
182 | 2910 { |
2911 if (request != NULL) | |
186 | 2912 request->logging_function (gftp_logging_error, request, |
182 | 2913 _("Error: Cannot open local file %s: %s\n"), |
2914 pathname, g_strerror (errno)); | |
2915 return (GFTP_ERETRYABLE); | |
2916 } | |
2917 | |
2918 if (fcntl (fd, F_SETFD, 1) == -1) | |
2919 { | |
2920 if (request != NULL) | |
186 | 2921 request->logging_function (gftp_logging_error, request, |
182 | 2922 _("Error: Cannot set close on exec flag: %s\n"), |
2923 g_strerror (errno)); | |
2924 | |
2925 return (-1); | |
2926 } | |
2927 | |
2928 return (fd); | |
2929 } | |
422 | 2930 |
2931 | |
2932 void | |
2933 gftp_setup_startup_directory (gftp_request * request) | |
2934 { | |
2935 char *startup_directory, *tempstr; | |
2936 | |
2937 gftp_lookup_request_option (request, "startup_directory", &startup_directory); | |
2938 | |
2939 if (*startup_directory != '\0' && | |
2940 (tempstr = expand_path (startup_directory)) != NULL) | |
2941 { | |
2942 gftp_set_directory (request, tempstr); | |
2943 g_free (tempstr); | |
2944 } | |
2945 } | |
2946 | |
499 | 2947 |
2948 char * | |
2949 gftp_convert_attributes_from_mode_t (mode_t mode) | |
2950 { | |
2951 char *str; | |
2952 | |
2953 str = g_malloc0 (11); | |
2954 | |
2955 str[0] = '?'; | |
2956 if (S_ISREG (mode)) | |
2957 str[0] = '-'; | |
2958 | |
2959 if (S_ISLNK (mode)) | |
2960 str[0] = 'l'; | |
2961 | |
2962 if (S_ISBLK (mode)) | |
2963 str[0] = 'b'; | |
2964 | |
2965 if (S_ISCHR (mode)) | |
2966 str[0] = 'c'; | |
2967 | |
2968 if (S_ISFIFO (mode)) | |
2969 str[0] = 'p'; | |
2970 | |
2971 if (S_ISSOCK (mode)) | |
2972 str[0] = 's'; | |
2973 | |
2974 if (S_ISDIR (mode)) | |
2975 str[0] = 'd'; | |
2976 | |
2977 str[1] = mode & S_IRUSR ? 'r' : '-'; | |
2978 str[2] = mode & S_IWUSR ? 'w' : '-'; | |
2979 | |
2980 if ((mode & S_ISUID) && (mode & S_IXUSR)) | |
2981 str[3] = 's'; | |
2982 else if (mode & S_ISUID) | |
2983 str[3] = 'S'; | |
2984 else if (mode & S_IXUSR) | |
2985 str[3] = 'x'; | |
2986 else | |
2987 str[3] = '-'; | |
2988 | |
2989 str[4] = mode & S_IRGRP ? 'r' : '-'; | |
2990 str[5] = mode & S_IWGRP ? 'w' : '-'; | |
2991 | |
2992 if ((mode & S_ISGID) && (mode & S_IXGRP)) | |
2993 str[6] = 's'; | |
2994 else if (mode & S_ISGID) | |
2995 str[6] = 'S'; | |
2996 else if (mode & S_IXGRP) | |
2997 str[6] = 'x'; | |
2998 else | |
2999 str[6] = '-'; | |
3000 | |
3001 str[7] = mode & S_IROTH ? 'r' : '-'; | |
3002 str[8] = mode & S_IWOTH ? 'w' : '-'; | |
3003 | |
3004 if ((mode & S_ISVTX) && (mode & S_IXOTH)) | |
3005 str[9] = 't'; | |
3006 else if (mode & S_ISVTX) | |
3007 str[9] = 'T'; | |
3008 else if (mode & S_IXOTH) | |
3009 str[9] = 'x'; | |
3010 else | |
3011 str[9] = '-'; | |
3012 | |
3013 return (str); | |
3014 } | |
3015 | |
3016 | |
3017 mode_t | |
3018 gftp_convert_attributes_to_mode_t (char *attribs) | |
3019 { | |
3020 mode_t mode; | |
3021 | |
3022 if (attribs[0] == 'd') | |
3023 mode = S_IFDIR; | |
3024 else if (attribs[0] == 'l') | |
3025 mode = S_IFLNK; | |
3026 else if (attribs[0] == 's') | |
3027 mode = S_IFSOCK; | |
3028 else if (attribs[0] == 'b') | |
3029 mode = S_IFBLK; | |
3030 else if (attribs[0] == 'c') | |
3031 mode = S_IFCHR; | |
3032 else | |
3033 mode = S_IFREG; | |
3034 | |
3035 if (attribs[1] == 'r') | |
3036 mode |= S_IRUSR; | |
3037 if (attribs[2] == 'w') | |
3038 mode |= S_IWUSR; | |
3039 if (attribs[3] == 'x' || attribs[3] == 's') | |
3040 mode |= S_IXUSR; | |
3041 if (attribs[3] == 's' || attribs[3] == 'S') | |
3042 mode |= S_ISUID; | |
3043 | |
3044 if (attribs[4] == 'r') | |
3045 mode |= S_IRGRP; | |
3046 if (attribs[5] == 'w') | |
3047 mode |= S_IWGRP; | |
3048 if (attribs[6] == 'x' || | |
3049 attribs[6] == 's') | |
3050 mode |= S_IXGRP; | |
3051 if (attribs[6] == 's' || attribs[6] == 'S') | |
3052 mode |= S_ISGID; | |
3053 | |
3054 if (attribs[7] == 'r') | |
3055 mode |= S_IROTH; | |
3056 if (attribs[8] == 'w') | |
3057 mode |= S_IWOTH; | |
3058 if (attribs[9] == 'x' || | |
3059 attribs[9] == 's') | |
3060 mode |= S_IXOTH; | |
3061 if (attribs[9] == 't' || attribs[9] == 'T') | |
3062 mode |= S_ISVTX; | |
3063 | |
3064 return (mode); | |
3065 } | |
3066 |