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