Mercurial > gftp.yaz
annotate lib/protocols.c @ 811:789ff8cb3170
2006-9-14 Brian Masney <masneyb@gftp.org>
* src/uicommon/gftpui.c - added _do_transfer_file(). This will take
care of the actual file transfer. It will also make sure that the
entire buffer has been transmitted properly.
* lib/protocols.c lib/rfc959.c (*_put_next_file_chunk) - removed
unneeded code that checks for a block size of 0
* lib/rfc959.c (rfc959_syst) - disable show_hidden_files and
resolve_remote_symlinks if the remote system type is OS/400.
| author | masneyb |
|---|---|
| date | Thu, 14 Sep 2006 09:07:29 +0000 |
| parents | d59f62126c97 |
| children | 11159114bb97 |
| 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 | |
| 168 | 298 return (request->write_function (request, buf, size, request->datafd)); |
| 1 | 299 } |
| 300 | |
| 301 | |
| 302 int | |
| 303 gftp_end_transfer (gftp_request * request) | |
| 304 { | |
| 305 int ret; | |
| 306 | |
| 84 | 307 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 308 |
| 40 | 309 if (!request->cached && |
| 310 request->end_transfer != NULL) | |
| 311 ret = request->end_transfer (request); | |
| 312 else | |
| 313 ret = 0; | |
| 1 | 314 |
| 58 | 315 if (request->cachefd > 0) |
| 1 | 316 { |
| 58 | 317 close (request->cachefd); |
| 318 request->cachefd = -1; | |
| 1 | 319 } |
| 320 | |
| 321 if (request->last_dir_entry) | |
| 322 { | |
| 323 g_free (request->last_dir_entry); | |
| 324 request->last_dir_entry = NULL; | |
| 325 request->last_dir_entry_len = 0; | |
| 326 } | |
| 327 | |
| 328 return (ret); | |
| 329 } | |
| 330 | |
| 331 | |
| 332 int | |
| 40 | 333 gftp_abort_transfer (gftp_request * request) |
| 334 { | |
| 84 | 335 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 40 | 336 |
| 337 if (request->abort_transfer == NULL) | |
| 84 | 338 return (GFTP_EFATAL); |
| 40 | 339 |
| 340 return (request->abort_transfer (request)); | |
| 341 } | |
| 342 | |
| 343 | |
| 520 | 344 int |
| 787 | 345 gftp_stat_filename (gftp_request * request, const char *filename, mode_t * mode, |
| 346 off_t * filesize) | |
| 500 | 347 { |
| 348 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
| 520 | 349 g_return_val_if_fail (filename != NULL, GFTP_EFATAL); |
| 500 | 350 |
| 351 if (request->stat_filename != NULL) | |
| 787 | 352 return (request->stat_filename (request, filename, mode, filesize)); |
| 500 | 353 else |
| 520 | 354 return (0); |
| 500 | 355 } |
| 356 | |
| 357 | |
| 40 | 358 int |
| 1 | 359 gftp_list_files (gftp_request * request) |
| 360 { | |
| 473 | 361 char *remote_lc_time, *locret; |
| 58 | 362 int fd; |
| 1 | 363 |
| 84 | 364 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 365 |
| 623 | 366 #if ENABLE_NLS |
| 473 | 367 gftp_lookup_request_option (request, "remote_lc_time", &remote_lc_time); |
| 368 if (remote_lc_time != NULL && *remote_lc_time != '\0') | |
| 369 locret = setlocale (LC_TIME, remote_lc_time); | |
| 370 else | |
| 371 locret = setlocale (LC_TIME, NULL); | |
| 372 | |
| 373 if (locret == NULL) | |
| 374 { | |
| 375 locret = setlocale (LC_TIME, NULL); | |
| 677 | 376 request->logging_function (gftp_logging_error, request, |
| 473 | 377 _("Error setting LC_TIME to '%s'. Falling back to '%s'\n"), |
| 378 remote_lc_time, locret); | |
| 379 } | |
| 623 | 380 #else |
| 381 locret = _("<unknown>"); | |
| 382 #endif | |
| 473 | 383 |
| 1 | 384 request->cached = 0; |
| 58 | 385 if (request->use_cache && (fd = gftp_find_cache_entry (request)) > 0) |
| 1 | 386 { |
| 186 | 387 request->logging_function (gftp_logging_misc, request, |
| 473 | 388 _("Loading directory listing %s from cache (LC_TIME=%s)\n"), |
| 389 request->directory, locret); | |
| 1 | 390 |
| 391 request->cachefd = fd; | |
| 392 request->cached = 1; | |
| 393 return (0); | |
| 394 } | |
| 395 else if (request->use_cache) | |
| 396 { | |
| 473 | 397 request->logging_function (gftp_logging_misc, request, |
| 398 _("Loading directory listing %s from server (LC_TIME=%s)\n"), | |
| 399 request->directory, locret); | |
| 400 | |
| 1 | 401 request->cachefd = gftp_new_cache_entry (request); |
| 402 request->cached = 0; | |
| 403 } | |
| 404 | |
| 405 if (request->list_files == NULL) | |
| 84 | 406 return (GFTP_EFATAL); |
| 473 | 407 |
| 1 | 408 return (request->list_files (request)); |
| 409 } | |
| 410 | |
| 411 | |
| 184 | 412 #if GLIB_MAJOR_VERSION > 1 |
| 291 | 413 |
| 765 | 414 static /*@null@*/ char * |
| 423 | 415 _gftp_get_next_charset (char **curpos) |
| 184 | 416 { |
| 417 char *ret, *endpos; | |
| 418 | |
| 419 if (**curpos == '\0') | |
| 420 return (NULL); | |
| 421 | |
| 422 ret = *curpos; | |
| 185 | 423 if ((endpos = strchr (*curpos, ',')) == NULL) |
| 184 | 424 *curpos += strlen (*curpos); |
| 425 else | |
| 426 { | |
| 427 *endpos = '\0'; | |
| 423 | 428 *curpos = endpos + 1; |
| 184 | 429 } |
| 430 | |
| 431 return (ret); | |
| 432 } | |
| 433 | |
| 434 | |
| 765 | 435 /*@null@*/ char * |
| 291 | 436 gftp_string_to_utf8 (gftp_request * request, const char *str) |
| 184 | 437 { |
| 423 | 438 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; |
| 598 | 439 GError * error = NULL; |
| 184 | 440 gsize bread, bwrite; |
| 441 | |
| 188 | 442 if (request == NULL) |
| 443 return (NULL); | |
| 444 | |
| 579 | 445 if (g_utf8_validate (str, -1, NULL)) |
| 446 return (NULL); | |
| 447 else if (request->iconv_initialized) | |
| 582 | 448 { |
| 449 ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 450 &error); | |
| 451 if (ret == NULL) | |
| 452 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), | |
| 453 str, _("<unknown>"), "UTF-8", error->message); | |
| 454 | |
| 455 return (ret); | |
| 456 } | |
| 184 | 457 |
| 423 | 458 gftp_lookup_request_option (request, "remote_charsets", &tempstr); |
| 459 if (*tempstr == '\0') | |
| 184 | 460 { |
| 461 error = NULL; | |
| 462 if ((ret = g_locale_to_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) | |
| 463 return (ret); | |
| 464 | |
| 579 | 465 /* Don't use request->logging_function since the strings must be in UTF-8 |
| 466 for the GTK+ 2.x port */ | |
| 467 printf (_("Error converting string '%s' to UTF-8 from current locale: %s\n"), | |
| 468 str, error->message); | |
| 184 | 469 return (NULL); |
| 470 } | |
| 471 | |
| 423 | 472 remote_charsets = g_strdup (tempstr); |
| 184 | 473 ret = NULL; |
| 474 stpos = remote_charsets; | |
| 423 | 475 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) |
| 184 | 476 { |
| 477 if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1) | |
| 478 continue; | |
| 479 | |
| 480 error = NULL; | |
| 481 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 482 &error)) == NULL) | |
| 483 { | |
| 582 | 484 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), |
| 485 str, cur_charset, "UTF-8", error->message); | |
| 486 | |
| 184 | 487 g_iconv_close (request->iconv); |
| 488 request->iconv = NULL; | |
| 489 continue; | |
| 490 } | |
| 491 else | |
| 492 { | |
| 493 request->iconv_initialized = 1; | |
| 494 break; | |
| 495 } | |
| 496 } | |
| 497 | |
| 423 | 498 g_free (remote_charsets); |
| 499 | |
| 184 | 500 return (ret); |
| 501 } | |
| 291 | 502 |
| 503 | |
| 504 char * | |
| 505 gftp_string_from_utf8 (gftp_request * request, const char *str) | |
| 506 { | |
| 423 | 507 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; |
| 598 | 508 GError * error = NULL; |
| 291 | 509 gsize bread, bwrite; |
| 510 | |
| 511 if (request == NULL) | |
| 512 return (NULL); | |
| 513 | |
| 582 | 514 /* If the string isn't in UTF-8 format, assume it is already in the current |
| 515 locale... */ | |
| 516 if (!g_utf8_validate (str, -1, NULL)) | |
| 517 return (NULL); | |
| 518 else if (request->iconv_initialized) | |
| 519 { | |
| 520 ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 521 &error); | |
| 522 if (ret == NULL) | |
| 523 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), | |
| 524 str, "UTF-8", _("<unknown>"), error->message); | |
| 525 | |
| 526 return (ret); | |
| 527 } | |
| 291 | 528 |
| 423 | 529 gftp_lookup_request_option (request, "remote_charsets", &tempstr); |
| 530 if (*tempstr == '\0') | |
| 291 | 531 { |
| 532 error = NULL; | |
| 533 if ((ret = g_locale_from_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) | |
| 534 return (ret); | |
| 535 | |
| 579 | 536 /* Don't use request->logging_function since the strings must be in UTF-8 |
| 537 for the GTK+ 2.x port */ | |
| 538 printf (_("Error converting string '%s' to current locale from UTF-8: %s\n"), | |
| 539 str, error->message); | |
| 291 | 540 return (NULL); |
| 541 } | |
| 542 | |
| 423 | 543 remote_charsets = g_strdup (tempstr); |
| 291 | 544 ret = NULL; |
| 545 stpos = remote_charsets; | |
| 423 | 546 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) |
| 291 | 547 { |
| 548 if ((request->iconv = g_iconv_open (cur_charset, "UTF-8")) == (GIConv) -1) | |
| 549 continue; | |
| 550 | |
| 551 error = NULL; | |
| 552 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 553 &error)) == NULL) | |
| 554 { | |
| 582 | 555 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), |
| 556 str, "UTF-8", cur_charset, error->message); | |
| 557 | |
| 291 | 558 g_iconv_close (request->iconv); |
| 559 request->iconv = NULL; | |
| 560 continue; | |
| 561 } | |
| 562 else | |
| 563 { | |
| 564 request->iconv_initialized = 1; | |
| 565 break; | |
| 566 } | |
| 567 } | |
| 568 | |
| 423 | 569 g_free (remote_charsets); |
| 570 | |
| 291 | 571 return (ret); |
| 572 } | |
| 573 | |
| 574 #else | |
| 575 | |
| 576 char * | |
| 577 gftp_string_to_utf8 (gftp_request * request, const char *str) | |
| 578 { | |
| 579 return (NULL); | |
| 580 } | |
| 581 | |
| 582 | |
| 583 char * | |
| 584 gftp_string_from_utf8 (gftp_request * request, const char *str) | |
| 585 { | |
| 586 return (NULL); | |
| 587 } | |
| 588 | |
| 184 | 589 #endif |
| 590 | |
| 591 | |
| 1 | 592 int |
| 377 | 593 gftp_get_next_file (gftp_request * request, const char *filespec, |
| 594 gftp_file * fle) | |
| 1 | 595 { |
| 666 | 596 char *slashpos, *newfile; |
| 58 | 597 int fd, ret; |
| 1 | 598 |
| 84 | 599 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 600 |
| 601 if (request->get_next_file == NULL) | |
| 84 | 602 return (GFTP_EFATAL); |
| 1 | 603 |
| 58 | 604 if (request->cached && request->cachefd > 0) |
| 1 | 605 fd = request->cachefd; |
| 606 else | |
| 607 fd = request->datafd; | |
| 608 | |
| 609 memset (fle, 0, sizeof (*fle)); | |
| 610 do | |
| 611 { | |
| 598 | 612 gftp_file_destroy (fle, 0); |
| 1 | 613 ret = request->get_next_file (request, fle, fd); |
| 666 | 614 if (fle->file != NULL && (slashpos = strrchr (fle->file, '/')) != NULL) |
| 615 { | |
| 616 if (*(slashpos + 1) == '\0') | |
| 617 { | |
| 618 gftp_file_destroy (fle, 0); | |
| 619 continue; | |
| 620 } | |
| 621 | |
| 622 *slashpos = '\0'; | |
| 623 newfile = g_strdup (slashpos + 1); | |
| 624 | |
| 625 if (strcmp (fle->file, request->directory) != 0) | |
| 626 request->logging_function (gftp_logging_error, request, | |
| 627 _("Warning: Stripping path off of file '%s'. The stripped path (%s) doesn't match the current directory (%s)\n"), | |
| 628 newfile, fle->file, request->directory, | |
| 629 g_strerror (errno)); | |
| 630 | |
| 631 g_free (fle->file); | |
| 632 fle->file = newfile; | |
| 633 } | |
| 1 | 634 |
| 291 | 635 if (ret >= 0 && fle->file != NULL) |
| 184 | 636 fle->utf8_file = gftp_string_to_utf8 (request, fle->file); |
| 45 | 637 |
| 60 | 638 if (ret >= 0 && !request->cached && request->cachefd > 0 && |
| 1 | 639 request->last_dir_entry != NULL) |
| 640 { | |
| 168 | 641 if (gftp_fd_write (request, request->last_dir_entry, |
| 60 | 642 request->last_dir_entry_len, request->cachefd) < 0) |
| 1 | 643 { |
| 186 | 644 request->logging_function (gftp_logging_error, request, |
| 1 | 645 _("Error: Cannot write to cache: %s\n"), |
| 646 g_strerror (errno)); | |
| 60 | 647 close (request->cachefd); |
| 648 request->cachefd = -1; | |
| 1 | 649 } |
| 650 } | |
| 651 } while (ret > 0 && !gftp_match_filespec (fle->file, filespec)); | |
| 652 | |
| 653 return (ret); | |
| 654 } | |
| 655 | |
| 656 | |
| 657 int | |
| 243 | 658 gftp_parse_bookmark (gftp_request * request, gftp_request * local_request, |
| 275 | 659 const char * bookmark, int *refresh_local) |
| 87 | 660 { |
| 661 gftp_logging_func logging_function; | |
| 122 | 662 gftp_bookmarks_var * tempentry; |
| 578 | 663 char *default_protocol, *utf8; |
| 646 | 664 const char *email; |
| 173 | 665 int i, init_ret; |
| 87 | 666 |
| 667 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
| 668 g_return_val_if_fail (bookmark != NULL, GFTP_EFATAL); | |
| 669 | |
| 670 logging_function = request->logging_function; | |
| 671 gftp_request_destroy (request, 0); | |
| 672 request->logging_function = logging_function; | |
| 673 | |
| 122 | 674 if ((tempentry = g_hash_table_lookup (gftp_bookmarks_htable, |
| 675 bookmark)) == NULL) | |
| 87 | 676 { |
| 186 | 677 request->logging_function (gftp_logging_error, request, |
| 87 | 678 _("Error: Could not find bookmark %s\n"), |
| 679 bookmark); | |
| 680 return (GFTP_EFATAL); | |
| 681 } | |
| 682 else if (tempentry->hostname == NULL || *tempentry->hostname == '\0') | |
| 683 { | |
| 186 | 684 request->logging_function (gftp_logging_error, request, |
| 87 | 685 _("Bookmarks Error: The bookmark entry %s does not have a hostname\n"), bookmark); |
| 686 return (GFTP_EFATAL); | |
| 687 } | |
| 688 | |
| 689 if (tempentry->user != NULL) | |
| 690 gftp_set_username (request, tempentry->user); | |
| 691 | |
| 692 if (tempentry->pass != NULL) | |
| 646 | 693 { |
| 694 if (strcmp (tempentry->pass, "@EMAIL@") == 0) | |
| 695 { | |
| 696 gftp_lookup_request_option (request, "email", &email); | |
| 697 gftp_set_password (request, email); | |
| 698 } | |
| 699 else | |
| 700 gftp_set_password (request, tempentry->pass); | |
| 701 } | |
| 87 | 702 |
| 703 if (tempentry->acct != NULL) | |
| 704 gftp_set_account (request, tempentry->acct); | |
| 705 | |
| 706 gftp_set_hostname (request, tempentry->hostname); | |
| 578 | 707 |
| 708 utf8 = gftp_string_from_utf8 (request, tempentry->remote_dir); | |
| 709 if (utf8 != NULL) | |
| 710 { | |
| 711 gftp_set_directory (request, utf8); | |
| 712 g_free (utf8); | |
| 713 } | |
| 714 else | |
| 715 gftp_set_directory (request, tempentry->remote_dir); | |
| 716 | |
| 87 | 717 gftp_set_port (request, tempentry->port); |
| 718 | |
| 516 | 719 if (local_request != NULL && tempentry->local_dir != NULL && |
| 243 | 720 *tempentry->local_dir != '\0') |
| 275 | 721 { |
| 578 | 722 utf8 = gftp_string_from_utf8 (request, tempentry->local_dir); |
| 723 if (utf8 != NULL) | |
| 724 { | |
| 725 gftp_set_directory (local_request, utf8); | |
| 726 g_free (utf8); | |
| 727 } | |
| 728 else | |
| 729 gftp_set_directory (local_request, tempentry->local_dir); | |
| 730 | |
| 516 | 731 if (refresh_local != NULL) |
| 732 *refresh_local = 1; | |
| 275 | 733 } |
| 516 | 734 else if (refresh_local != NULL) |
| 275 | 735 *refresh_local = 0; |
| 243 | 736 |
| 87 | 737 for (i = 0; gftp_protocols[i].name; i++) |
| 738 { | |
| 739 if (strcmp (gftp_protocols[i].name, tempentry->protocol) == 0) | |
| 740 { | |
| 173 | 741 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
| 742 { | |
| 743 gftp_request_destroy (request, 0); | |
| 744 return (init_ret); | |
| 745 } | |
| 87 | 746 break; |
| 747 } | |
| 748 } | |
| 749 | |
| 122 | 750 if (gftp_protocols[i].name == NULL) |
| 87 | 751 { |
| 122 | 752 gftp_lookup_request_option (request, "default_protocol", |
| 753 &default_protocol); | |
| 754 | |
| 125 | 755 if (default_protocol != NULL && *default_protocol != '\0') |
| 87 | 756 { |
| 122 | 757 for (i = 0; gftp_protocols[i].url_prefix; i++) |
| 758 { | |
| 759 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
| 760 break; | |
| 761 } | |
| 87 | 762 } |
| 763 | |
| 764 if (gftp_protocols[i].url_prefix == NULL) | |
| 765 i = GFTP_FTP_NUM; | |
| 766 } | |
| 767 | |
| 199 | 768 gftp_copy_local_options (&request->local_options_vars, |
| 769 &request->local_options_hash, | |
| 429 | 770 &request->num_local_options_vars, |
| 199 | 771 tempentry->local_options_vars, |
| 772 tempentry->num_local_options_vars); | |
| 773 | |
| 173 | 774 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
| 775 { | |
| 776 gftp_request_destroy (request, 0); | |
| 777 return (init_ret); | |
| 778 } | |
| 779 | |
| 87 | 780 return (0); |
| 781 } | |
| 782 | |
| 783 | |
| 784 int | |
| 1 | 785 gftp_parse_url (gftp_request * request, const char *url) |
| 786 { | |
| 676 | 787 char *pos, *endpos, *default_protocol, *new_url; |
| 67 | 788 gftp_logging_func logging_function; |
| 676 | 789 const char *clear_pos; |
| 790 int i, ret; | |
| 1 | 791 |
| 84 | 792 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 793 g_return_val_if_fail (url != NULL, GFTP_EFATAL); | |
| 1 | 794 |
| 67 | 795 logging_function = request->logging_function; |
| 796 gftp_request_destroy (request, 0); | |
| 797 request->logging_function = logging_function; | |
| 798 | |
| 676 | 799 for (clear_pos = url; |
| 800 *clear_pos == ' ' || *clear_pos == '\t'; | |
| 801 clear_pos++); | |
| 802 | |
| 803 new_url = g_strdup (clear_pos); | |
| 804 | |
| 805 for (pos = new_url + strlen (new_url) - 1; | |
| 806 *pos == ' ' || *pos == '\t'; | |
| 807 pos--) | |
| 168 | 808 *pos = '\0'; |
| 1 | 809 |
| 676 | 810 /* See if the URL has a protocol... */ |
| 811 if ((pos = strstr (new_url, "://")) != NULL) | |
| 1 | 812 { |
| 813 *pos = '\0'; | |
| 814 | |
| 815 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
| 816 { | |
| 676 | 817 if (strcmp (gftp_protocols[i].url_prefix, new_url) == 0) |
| 1 | 818 break; |
| 819 } | |
| 820 | |
| 821 if (gftp_protocols[i].url_prefix == NULL) | |
| 168 | 822 { |
| 677 | 823 request->logging_function (gftp_logging_error, NULL, |
| 173 | 824 _("The protocol '%s' is currently not supported.\n"), |
| 676 | 825 new_url); |
| 826 g_free (new_url); | |
| 168 | 827 return (GFTP_EFATAL); |
| 828 } | |
| 173 | 829 |
| 830 *pos = ':'; | |
| 676 | 831 pos += 3; |
| 1 | 832 } |
| 833 else | |
| 834 { | |
| 122 | 835 gftp_lookup_request_option (request, "default_protocol", |
| 836 &default_protocol); | |
| 837 | |
| 676 | 838 i = GFTP_FTP_NUM; |
| 125 | 839 if (default_protocol != NULL && *default_protocol != '\0') |
| 1 | 840 { |
| 122 | 841 for (i = 0; gftp_protocols[i].url_prefix; i++) |
| 842 { | |
| 843 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
| 844 break; | |
| 845 } | |
| 1 | 846 } |
| 676 | 847 |
| 848 if (gftp_protocols[i].url_prefix == NULL) | |
| 849 { | |
| 677 | 850 request->logging_function (gftp_logging_error, NULL, |
| 676 | 851 _("The protocol '%s' is currently not supported.\n"), |
| 852 default_protocol); | |
| 853 g_free (new_url); | |
| 854 return (GFTP_EFATAL); | |
| 855 } | |
| 856 | |
| 857 pos = new_url; | |
| 122 | 858 } |
| 1 | 859 |
| 676 | 860 if ((ret = gftp_protocols[i].init (request)) < 0) |
| 173 | 861 { |
| 862 gftp_request_destroy (request, 0); | |
| 676 | 863 return (ret); |
| 864 } | |
| 865 | |
| 866 if ((endpos = strchr (pos, '/')) != NULL) | |
| 867 { | |
| 868 gftp_set_directory (request, endpos); | |
| 869 *endpos = '\0'; | |
| 173 | 870 } |
| 1 | 871 |
| 872 if (request->parse_url != NULL) | |
| 168 | 873 { |
| 676 | 874 ret = request->parse_url (request, new_url); |
| 875 g_free (new_url); | |
| 876 return (ret); | |
| 168 | 877 } |
| 878 | |
| 676 | 879 if (*pos != '\0') |
| 1 | 880 { |
| 676 | 881 if (endpos == NULL) |
| 882 endpos = pos + strlen (pos) - 1; | |
| 883 else | |
| 884 endpos--; | |
| 885 | |
| 886 for (; isdigit (*endpos); endpos--); | |
| 887 | |
| 888 if (*endpos == ':' && isdigit (*(endpos + 1))) | |
| 168 | 889 { |
| 676 | 890 gftp_set_port (request, strtol (endpos + 1, NULL, 10)); |
| 891 *endpos = '\0'; | |
| 168 | 892 } |
| 1 | 893 |
| 676 | 894 if ((endpos = strrchr (pos, '@')) != NULL) |
| 168 | 895 { |
| 676 | 896 gftp_set_hostname (request, endpos + 1); |
| 168 | 897 *endpos = '\0'; |
| 676 | 898 |
| 899 if ((endpos = strchr (pos, ':')) != NULL) | |
| 900 { | |
| 901 *endpos = '\0'; | |
| 902 gftp_set_username (request, pos); | |
| 903 gftp_set_password (request, endpos + 1); | |
| 904 } | |
| 905 else | |
| 906 { | |
| 907 gftp_set_username (request, pos); | |
| 908 gftp_set_password (request, ""); | |
| 909 } | |
| 168 | 910 } |
| 676 | 911 else |
| 912 gftp_set_hostname (request, pos); | |
| 1 | 913 } |
| 914 | |
| 676 | 915 g_free (new_url); |
| 1 | 916 return (0); |
| 917 } | |
| 918 | |
| 919 | |
| 920 void | |
| 921 gftp_set_hostname (gftp_request * request, const char *hostname) | |
| 922 { | |
| 923 g_return_if_fail (request != NULL); | |
| 924 g_return_if_fail (hostname != NULL); | |
| 925 | |
| 926 if (request->hostname) | |
| 927 g_free (request->hostname); | |
| 124 | 928 request->hostname = g_strdup (hostname); |
| 1 | 929 } |
| 930 | |
| 931 | |
| 932 void | |
| 933 gftp_set_username (gftp_request * request, const char *username) | |
| 934 { | |
| 935 g_return_if_fail (request != NULL); | |
| 936 | |
| 937 if (request->username) | |
| 938 g_free (request->username); | |
| 169 | 939 |
| 940 if (username != NULL) | |
| 941 request->username = g_strdup (username); | |
| 942 else | |
| 943 request->username = NULL; | |
| 1 | 944 } |
| 945 | |
| 946 | |
| 947 void | |
| 948 gftp_set_password (gftp_request * request, const char *password) | |
| 949 { | |
| 950 g_return_if_fail (request != NULL); | |
| 951 | |
| 952 if (request->password) | |
| 953 g_free (request->password); | |
| 169 | 954 |
| 955 if (password != NULL) | |
| 956 request->password = g_strdup (password); | |
| 957 else | |
| 958 request->password = NULL; | |
| 1 | 959 } |
| 960 | |
| 961 | |
| 962 void | |
| 963 gftp_set_account (gftp_request * request, const char *account) | |
| 964 { | |
| 965 g_return_if_fail (request != NULL); | |
| 966 g_return_if_fail (account != NULL); | |
| 967 | |
| 968 if (request->account) | |
| 969 g_free (request->account); | |
| 124 | 970 request->account = g_strdup (account); |
| 1 | 971 } |
| 972 | |
| 973 | |
| 974 int | |
| 975 gftp_set_directory (gftp_request * request, const char *directory) | |
| 976 { | |
| 84 | 977 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 978 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
| 1 | 979 |
| 980 | |
| 169 | 981 if (request->datafd <= 0 && !request->always_connected) |
| 1 | 982 { |
| 983 if (directory != request->directory) | |
| 168 | 984 { |
| 985 if (request->directory) | |
| 986 g_free (request->directory); | |
| 987 request->directory = g_strdup (directory); | |
| 988 } | |
| 1 | 989 return (0); |
| 990 } | |
| 991 else if (request->chdir == NULL) | |
| 84 | 992 return (GFTP_EFATAL); |
| 1 | 993 return (request->chdir (request, directory)); |
| 994 } | |
| 995 | |
| 996 | |
| 997 void | |
| 998 gftp_set_port (gftp_request * request, unsigned int port) | |
| 999 { | |
| 1000 g_return_if_fail (request != NULL); | |
| 1001 | |
| 1002 request->port = port; | |
| 1003 } | |
| 1004 | |
| 1005 | |
| 1006 int | |
| 1007 gftp_remove_directory (gftp_request * request, const char *directory) | |
| 1008 { | |
| 84 | 1009 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1010 |
| 1011 if (request->rmdir == NULL) | |
| 84 | 1012 return (GFTP_EFATAL); |
| 1 | 1013 return (request->rmdir (request, directory)); |
| 1014 } | |
| 1015 | |
| 1016 | |
| 1017 int | |
| 1018 gftp_remove_file (gftp_request * request, const char *file) | |
| 1019 { | |
| 84 | 1020 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1021 |
| 1022 if (request->rmfile == NULL) | |
| 84 | 1023 return (GFTP_EFATAL); |
| 1 | 1024 return (request->rmfile (request, file)); |
| 1025 } | |
| 1026 | |
| 1027 | |
| 1028 int | |
| 1029 gftp_make_directory (gftp_request * request, const char *directory) | |
| 1030 { | |
| 291 | 1031 char *utf8; |
| 1032 int ret; | |
| 1033 | |
| 84 | 1034 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1035 |
| 1036 if (request->mkdir == NULL) | |
| 84 | 1037 return (GFTP_EFATAL); |
| 291 | 1038 |
| 1039 utf8 = gftp_string_from_utf8 (request, directory); | |
| 1040 if (utf8 != NULL) | |
| 1041 { | |
| 1042 ret = request->mkdir (request, utf8); | |
| 1043 g_free (utf8); | |
| 1044 } | |
| 1045 else | |
| 1046 ret = request->mkdir (request, directory); | |
| 1047 | |
| 1048 return (ret); | |
| 1 | 1049 } |
| 1050 | |
| 1051 | |
| 1052 int | |
| 1053 gftp_rename_file (gftp_request * request, const char *oldname, | |
| 168 | 1054 const char *newname) |
| 1 | 1055 { |
| 291 | 1056 char *utf8; |
| 1057 int ret; | |
| 1058 | |
| 84 | 1059 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1060 |
| 1061 if (request->rename == NULL) | |
| 84 | 1062 return (GFTP_EFATAL); |
| 291 | 1063 |
| 1064 utf8 = gftp_string_from_utf8 (request, newname); | |
| 1065 if (utf8 != NULL) | |
| 1066 { | |
| 1067 ret = request->rename (request, oldname, utf8); | |
| 1068 g_free (utf8); | |
| 1069 } | |
| 1070 else | |
| 1071 ret = request->rename (request, oldname, newname); | |
| 1072 | |
| 1073 return (ret); | |
| 1 | 1074 } |
| 1075 | |
| 1076 | |
| 1077 int | |
| 499 | 1078 gftp_chmod (gftp_request * request, const char *file, mode_t mode) |
| 1 | 1079 { |
| 84 | 1080 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1081 |
| 1082 if (request->chmod == NULL) | |
| 84 | 1083 return (GFTP_EFATAL); |
| 504 | 1084 |
| 1085 mode &= S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX; | |
| 1 | 1086 return (request->chmod (request, file, mode)); |
| 1087 } | |
| 1088 | |
| 1089 | |
| 1090 int | |
| 1091 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
| 1092 { | |
| 84 | 1093 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1094 |
| 1095 if (request->set_file_time == NULL) | |
| 84 | 1096 return (GFTP_EFATAL); |
| 1 | 1097 return (request->set_file_time (request, file, datetime)); |
| 1098 } | |
| 1099 | |
| 1100 | |
| 765 | 1101 int |
| 478 | 1102 gftp_site_cmd (gftp_request * request, int specify_site, const char *command) |
| 1 | 1103 { |
| 84 | 1104 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1105 |
| 1106 if (request->site == NULL) | |
| 84 | 1107 return (GFTP_EFATAL); |
| 478 | 1108 return (request->site (request, specify_site, command)); |
| 1 | 1109 } |
| 1110 | |
| 1111 | |
| 58 | 1112 off_t |
| 1 | 1113 gftp_get_file_size (gftp_request * request, const char *filename) |
| 1114 { | |
| 1115 g_return_val_if_fail (request != NULL, 0); | |
| 1116 | |
| 1117 if (request->get_file_size == NULL) | |
| 1118 return (0); | |
| 1119 return (request->get_file_size (request, filename)); | |
| 1120 } | |
| 1121 | |
| 1122 | |
| 122 | 1123 static int |
| 1124 gftp_need_proxy (gftp_request * request, char *service, char *proxy_hostname, | |
| 518 | 1125 unsigned int proxy_port) |
| 1 | 1126 { |
| 122 | 1127 gftp_config_list_vars * proxy_hosts; |
| 1 | 1128 gftp_proxy_hosts * hostname; |
| 460 | 1129 size_t hostlen, domlen; |
| 1 | 1130 unsigned char addy[4]; |
| 1131 struct sockaddr *addr; | |
| 1132 GList * templist; | |
| 1133 gint32 netaddr; | |
| 1134 char *pos; | |
| 1135 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 1136 struct addrinfo hints; | |
| 516 | 1137 unsigned int port; |
| 1138 int errnum; | |
| 1 | 1139 char serv[8]; |
| 122 | 1140 #endif |
| 1141 | |
| 218 | 1142 gftp_lookup_global_option ("dont_use_proxy", &proxy_hosts); |
| 122 | 1143 |
| 1144 if (proxy_hostname == NULL || *proxy_hostname == '\0') | |
| 1145 return (0); | |
| 1146 else if (proxy_hosts->list == NULL) | |
| 1147 return (proxy_hostname != NULL && | |
| 1148 *proxy_hostname != '\0'); | |
| 1 | 1149 |
| 1150 request->hostp = NULL; | |
| 122 | 1151 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
| 151 | 1152 request->free_hostp = 1; |
| 1 | 1153 memset (&hints, 0, sizeof (hints)); |
| 1154 hints.ai_flags = AI_CANONNAME; | |
| 146 | 1155 hints.ai_family = PF_UNSPEC; |
| 1 | 1156 hints.ai_socktype = SOCK_STREAM; |
| 1157 | |
| 122 | 1158 port = request->use_proxy ? proxy_port : request->port; |
| 1 | 1159 if (port == 0) |
| 1160 strcpy (serv, service); | |
| 1161 else | |
| 1162 snprintf (serv, sizeof (serv), "%d", port); | |
| 1163 | |
| 186 | 1164 request->logging_function (gftp_logging_misc, request, |
| 1 | 1165 _("Looking up %s\n"), request->hostname); |
| 1166 | |
| 1167 if ((errnum = getaddrinfo (request->hostname, serv, &hints, | |
| 1168 &request->hostp)) != 0) | |
| 1169 { | |
| 186 | 1170 request->logging_function (gftp_logging_error, request, |
| 1 | 1171 _("Cannot look up hostname %s: %s\n"), |
| 1172 request->hostname, gai_strerror (errnum)); | |
| 84 | 1173 return (GFTP_ERETRYABLE); |
| 1 | 1174 } |
| 1175 | |
| 1176 addr = request->hostp->ai_addr; | |
| 1177 | |
| 1178 #else /* !HAVE_GETADDRINFO */ | |
| 186 | 1179 request->logging_function (gftp_logging_misc, request, |
| 1 | 1180 _("Looking up %s\n"), request->hostname); |
| 1181 | |
| 1182 if (!(request->hostp = r_gethostbyname (request->hostname, &request->host, | |
| 1183 NULL))) | |
| 1184 { | |
| 186 | 1185 request->logging_function (gftp_logging_error, request, |
| 1 | 1186 _("Cannot look up hostname %s: %s\n"), |
| 1187 request->hostname, g_strerror (errno)); | |
| 84 | 1188 return (GFTP_ERETRYABLE); |
| 1 | 1189 } |
| 1190 | |
| 1191 addr = (struct sockaddr *) request->host.h_addr_list[0]; | |
| 1192 | |
| 1193 #endif /* HAVE_GETADDRINFO */ | |
| 1194 | |
| 122 | 1195 templist = proxy_hosts->list; |
| 1 | 1196 while (templist != NULL) |
| 1197 { | |
| 1198 hostname = templist->data; | |
| 460 | 1199 if (hostname->domain != NULL) |
| 168 | 1200 { |
| 460 | 1201 hostlen = strlen (request->hostname); |
| 1202 domlen = strlen (hostname->domain); | |
| 1203 if (hostlen > domlen) | |
| 1204 { | |
| 1205 pos = request->hostname + hostlen - domlen; | |
| 1206 if (strcmp (hostname->domain, pos) == 0) | |
| 1207 return (0); | |
| 1208 } | |
| 168 | 1209 } |
| 1 | 1210 |
| 1211 if (hostname->ipv4_network_address != 0) | |
| 168 | 1212 { |
| 516 | 1213 memcpy (addy, addr, sizeof (*addy)); |
| 168 | 1214 netaddr = |
| 1215 (((addy[0] & 0xff) << 24) | ((addy[1] & 0xff) << 16) | | |
| 1216 ((addy[2] & 0xff) << 8) | (addy[3] & 0xff)) & | |
| 1 | 1217 hostname->ipv4_netmask; |
| 168 | 1218 if (netaddr == hostname->ipv4_network_address) |
| 1219 return (0); | |
| 1220 } | |
| 1 | 1221 templist = templist->next; |
| 1222 } | |
| 1223 | |
| 122 | 1224 return (proxy_hostname != NULL && *proxy_hostname != '\0'); |
| 1 | 1225 } |
| 1226 | |
| 1227 | |
| 48 | 1228 static char * |
| 765 | 1229 copy_token (/*@out@*/ char **dest, char *source) |
| 1 | 1230 { |
| 48 | 1231 /* This function is used internally by gftp_parse_ls () */ |
| 1232 char *endpos, savepos; | |
| 1 | 1233 |
| 48 | 1234 endpos = source; |
| 1235 while (*endpos != ' ' && *endpos != '\t' && *endpos != '\0') | |
| 1236 endpos++; | |
| 1237 if (*endpos == '\0') | |
| 765 | 1238 { |
| 1239 *dest = NULL; | |
| 1240 return (NULL); | |
| 1241 } | |
| 1 | 1242 |
| 48 | 1243 savepos = *endpos; |
| 1244 *endpos = '\0'; | |
| 765 | 1245 *dest = g_malloc ((gulong) (endpos - source + 1)); |
| 48 | 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 |
| 798 | 1630 if (strlen (attribs) < 10) |
| 1631 return (GFTP_EFATAL); | |
| 1632 | |
| 499 | 1633 fle->st_mode = gftp_convert_attributes_to_mode_t (attribs); |
| 1634 g_free (attribs); | |
| 1635 | |
| 1 | 1636 if (cols >= 9) |
| 1637 { | |
| 1638 /* Skip the number of links */ | |
| 1639 startpos = goto_next_token (startpos); | |
| 1640 | |
| 1641 /* Copy the user that owns this file */ | |
| 1642 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 168 | 1643 return (GFTP_EFATAL); |
| 1 | 1644 |
| 1645 /* Copy the group that owns this file */ | |
| 1646 if ((startpos = copy_token (&fle->group, startpos)) == NULL) | |
| 168 | 1647 return (GFTP_EFATAL); |
| 1 | 1648 } |
| 1649 else | |
| 1650 { | |
| 124 | 1651 fle->group = g_strdup (_("unknown")); |
| 1 | 1652 if (cols == 8) |
| 168 | 1653 { |
| 1654 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 1655 return (GFTP_EFATAL); | |
| 1656 } | |
| 1 | 1657 else |
| 124 | 1658 fle->user = g_strdup (_("unknown")); |
| 1 | 1659 startpos = goto_next_token (startpos); |
| 1660 } | |
| 1661 | |
| 281 | 1662 if (request->server_type == GFTP_DIRTYPE_CRAY) |
| 1 | 1663 { |
| 91 | 1664 /* See if this is a Cray directory listing. It has the following format: |
| 1665 drwx------ 2 feiliu g913 DK common 4096 Sep 24 2001 wv */ | |
| 1666 if (cols == 11 && strstr (str, "->") == NULL) | |
| 1667 { | |
| 1668 startpos = goto_next_token (startpos); | |
| 1669 startpos = goto_next_token (startpos); | |
| 1670 } | |
| 1 | 1671 } |
| 1672 | |
| 1673 /* See if this is a block or character device. We will store the major number | |
| 1674 in the high word and the minor number in the low word. */ | |
| 499 | 1675 if (GFTP_IS_SPECIAL_DEVICE (fle->st_mode) && |
| 1676 (endpos = strchr (startpos, ',')) != NULL) | |
| 1 | 1677 { |
| 765 | 1678 fle->size = (unsigned long) strtol (startpos, NULL, 10) << 16; |
| 1 | 1679 |
| 1680 startpos = endpos + 1; | |
| 1681 while (*startpos == ' ') | |
| 168 | 1682 startpos++; |
| 1 | 1683 |
| 1684 /* Get the minor number */ | |
| 1685 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 168 | 1686 return (GFTP_EFATAL); |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1687 fle->size |= strtol (startpos, NULL, 10) & 0xFF; |
| 1 | 1688 } |
| 1689 else | |
| 1690 { | |
| 1691 /* This is a regular file */ | |
| 1692 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 168 | 1693 return (GFTP_EFATAL); |
| 244 | 1694 fle->size = gftp_parse_file_size (startpos); |
| 1 | 1695 } |
| 1696 | |
| 1697 /* Skip the blanks till we get to the next entry */ | |
| 1698 startpos = endpos + 1; | |
| 1699 while (*startpos == ' ') | |
| 1700 startpos++; | |
| 1701 | |
| 479 | 1702 fle->datetime = parse_time (startpos, &startpos); |
| 1 | 1703 |
| 1704 /* Skip the blanks till we get to the next entry */ | |
| 1705 startpos = goto_next_token (startpos); | |
| 1706 | |
| 1707 /* Parse the filename. If this file is a symbolic link, remove the -> part */ | |
| 499 | 1708 if (S_ISLNK (fle->st_mode) && ((endpos = strstr (startpos, "->")) != NULL)) |
| 1 | 1709 *(endpos - 1) = '\0'; |
| 1710 | |
| 124 | 1711 fle->file = g_strdup (startpos); |
| 1 | 1712 |
| 1713 /* Uncomment this if you want to strip the spaces off of the end of the file. | |
| 1714 I don't want to do this by default since there are valid filenames with | |
| 1715 spaces at the end of them. Some broken FTP servers like the Paradyne IPC | |
| 1716 DSLAMS append a bunch of spaces at the end of the file. | |
| 1717 for (endpos = fle->file + strlen (fle->file) - 1; | |
| 1718 *endpos == ' '; | |
| 1719 *endpos-- = '\0'); | |
| 1720 */ | |
| 1721 | |
| 1722 return (0); | |
| 1723 } | |
| 1724 | |
| 1725 | |
| 1726 static int | |
| 1727 gftp_parse_ls_nt (char *str, gftp_file * fle) | |
| 1728 { | |
| 1729 char *startpos; | |
| 1730 | |
| 1731 startpos = str; | |
| 479 | 1732 fle->datetime = parse_time (startpos, &startpos); |
| 1 | 1733 |
| 124 | 1734 fle->user = g_strdup (_("unknown")); |
| 1735 fle->group = g_strdup (_("unknown")); | |
| 1 | 1736 |
| 1737 startpos = goto_next_token (startpos); | |
| 499 | 1738 |
| 1 | 1739 if (startpos[0] == '<') |
| 499 | 1740 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
| 1 | 1741 else |
| 1742 { | |
| 499 | 1743 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 244 | 1744 fle->size = gftp_parse_file_size (startpos); |
| 1 | 1745 } |
| 124 | 1746 |
| 1 | 1747 startpos = goto_next_token (startpos); |
| 124 | 1748 fle->file = g_strdup (startpos); |
| 1 | 1749 return (0); |
| 1750 } | |
| 1751 | |
| 1752 | |
| 1753 static int | |
| 1754 gftp_parse_ls_novell (char *str, gftp_file * fle) | |
| 1755 { | |
| 1756 char *startpos; | |
| 1757 | |
| 1758 if (str[12] != ' ') | |
| 84 | 1759 return (GFTP_EFATAL); |
| 499 | 1760 |
| 1 | 1761 str[12] = '\0'; |
| 499 | 1762 fle->st_mode = gftp_convert_attributes_to_mode_t (str); |
| 1 | 1763 startpos = str + 13; |
| 1764 | |
| 1765 while ((*startpos == ' ' || *startpos == '\t') && *startpos != '\0') | |
| 1766 startpos++; | |
| 1767 | |
| 1768 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 84 | 1769 return (GFTP_EFATAL); |
| 1 | 1770 |
| 124 | 1771 fle->group = g_strdup (_("unknown")); |
| 1 | 1772 |
| 601 | 1773 while (*startpos != '\0' && !isdigit (*startpos)) |
| 1774 startpos++; | |
| 1775 | |
| 244 | 1776 fle->size = gftp_parse_file_size (startpos); |
| 1 | 1777 |
| 1778 startpos = goto_next_token (startpos); | |
| 479 | 1779 fle->datetime = parse_time (startpos, &startpos); |
| 1 | 1780 |
| 1781 startpos = goto_next_token (startpos); | |
| 124 | 1782 fle->file = g_strdup (startpos); |
| 1 | 1783 return (0); |
| 1784 } | |
| 1785 | |
| 1786 | |
| 48 | 1787 int |
| 485 | 1788 gftp_parse_ls (gftp_request * request, const char *lsoutput, gftp_file * fle, |
| 1789 int fd) | |
| 1 | 1790 { |
| 107 | 1791 char *str, *endpos, tmpchar; |
| 1792 int result, is_vms; | |
| 91 | 1793 size_t len; |
| 48 | 1794 |
| 84 | 1795 g_return_val_if_fail (lsoutput != NULL, GFTP_EFATAL); |
| 1796 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
| 48 | 1797 |
| 124 | 1798 str = g_strdup (lsoutput); |
| 48 | 1799 memset (fle, 0, sizeof (*fle)); |
| 1 | 1800 |
| 91 | 1801 len = strlen (str); |
| 107 | 1802 if (len > 0 && str[len - 1] == '\n') |
| 91 | 1803 str[--len] = '\0'; |
| 1804 if (len > 0 && str[len - 1] == '\r') | |
| 1805 str[--len] = '\0'; | |
| 39 | 1806 |
| 91 | 1807 switch (request->server_type) |
| 1808 { | |
| 122 | 1809 case GFTP_DIRTYPE_CRAY: |
| 1810 case GFTP_DIRTYPE_UNIX: | |
| 460 | 1811 result = gftp_parse_ls_unix (request, str, len, fle); |
| 91 | 1812 break; |
| 122 | 1813 case GFTP_DIRTYPE_EPLF: |
| 91 | 1814 result = gftp_parse_ls_eplf (str, fle); |
| 1815 break; | |
| 122 | 1816 case GFTP_DIRTYPE_NOVELL: |
| 91 | 1817 result = gftp_parse_ls_novell (str, fle); |
| 1818 break; | |
| 122 | 1819 case GFTP_DIRTYPE_DOS: |
| 91 | 1820 result = gftp_parse_ls_nt (str, fle); |
| 1821 break; | |
| 122 | 1822 case GFTP_DIRTYPE_VMS: |
| 485 | 1823 result = gftp_parse_ls_vms (request, fd, str, fle); |
| 107 | 1824 break; |
| 358 | 1825 case GFTP_DIRTYPE_MVS: |
| 1826 result = gftp_parse_ls_mvs (str, fle); | |
| 1827 break; | |
| 91 | 1828 default: /* autodetect */ |
| 1829 if (*lsoutput == '+') | |
| 1830 result = gftp_parse_ls_eplf (str, fle); | |
| 1831 else if (isdigit ((int) str[0]) && str[2] == '-') | |
| 1832 result = gftp_parse_ls_nt (str, fle); | |
| 1833 else if (str[1] == ' ' && str[2] == '[') | |
| 1834 result = gftp_parse_ls_novell (str, fle); | |
| 1835 else | |
| 107 | 1836 { |
| 1837 if ((endpos = strchr (str, ' ')) != NULL) | |
| 1838 { | |
| 1839 /* If the first token in the string has a ; in it, then */ | |
| 1840 /* we'll assume that this is a VMS directory listing */ | |
| 1841 tmpchar = *endpos; | |
| 1842 *endpos = '\0'; | |
| 1843 is_vms = strchr (str, ';') != NULL; | |
| 1844 *endpos = tmpchar; | |
| 1845 } | |
| 1846 else | |
| 1847 is_vms = 0; | |
| 48 | 1848 |
| 107 | 1849 if (is_vms) |
| 485 | 1850 result = gftp_parse_ls_vms (request, fd, str, fle); |
| 107 | 1851 else |
| 460 | 1852 result = gftp_parse_ls_unix (request, str, len, fle); |
| 107 | 1853 } |
| 91 | 1854 break; |
| 48 | 1855 } |
| 1856 g_free (str); | |
| 1857 | |
| 1858 return (result); | |
| 1 | 1859 } |
| 1860 | |
| 1861 | |
| 48 | 1862 static GHashTable * |
| 469 | 1863 gftp_gen_dir_hash (gftp_request * request, int *ret) |
| 1 | 1864 { |
| 48 | 1865 GHashTable * dirhash; |
| 1866 gftp_file * fle; | |
| 516 | 1867 off_t *newsize; |
| 48 | 1868 |
| 1869 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); | |
| 469 | 1870 *ret = gftp_list_files (request); |
| 1871 if (*ret == 0) | |
| 48 | 1872 { |
| 1873 fle = g_malloc0 (sizeof (*fle)); | |
| 1874 while (gftp_get_next_file (request, NULL, fle) > 0) | |
| 1875 { | |
| 516 | 1876 newsize = g_malloc (sizeof (*newsize)); |
| 48 | 1877 *newsize = fle->size; |
| 787 | 1878 g_hash_table_insert (dirhash, fle->file, newsize); |
| 48 | 1879 fle->file = NULL; |
| 598 | 1880 gftp_file_destroy (fle, 0); |
| 48 | 1881 } |
| 1882 gftp_end_transfer (request); | |
| 1883 g_free (fle); | |
| 1884 } | |
| 1885 else | |
| 1886 { | |
| 1887 g_hash_table_destroy (dirhash); | |
| 1888 dirhash = NULL; | |
| 1889 } | |
| 509 | 1890 |
| 48 | 1891 return (dirhash); |
| 1892 } | |
| 39 | 1893 |
| 48 | 1894 |
| 1895 static void | |
| 1896 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
| 1897 { | |
| 39 | 1898 |
| 48 | 1899 g_free (key); |
| 1900 g_free (value); | |
| 1901 } | |
| 1902 | |
| 1903 | |
| 1904 static void | |
| 1905 gftp_destroy_dir_hash (GHashTable * dirhash) | |
| 1906 { | |
| 787 | 1907 if (dirhash == NULL) |
| 1908 return; | |
| 1909 | |
| 48 | 1910 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); |
| 1911 g_hash_table_destroy (dirhash); | |
| 1 | 1912 } |
| 1913 | |
| 1914 | |
| 1915 static GList * | |
| 469 | 1916 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir, int *ret) |
| 1 | 1917 { |
| 1918 GHashTable * dirhash; | |
| 1919 GList * templist; | |
| 1920 gftp_file * fle; | |
| 516 | 1921 off_t *newsize; |
| 1 | 1922 char *newname; |
| 1923 | |
| 787 | 1924 if (getothdir && transfer->toreq != NULL) |
| 469 | 1925 { |
| 1926 dirhash = gftp_gen_dir_hash (transfer->toreq, ret); | |
| 787 | 1927 if (*ret == GFTP_EFATAL) |
| 469 | 1928 return (NULL); |
| 1929 } | |
| 1 | 1930 else |
| 1931 dirhash = NULL; | |
| 1932 | |
| 509 | 1933 *ret = gftp_list_files (transfer->fromreq); |
| 1934 if (*ret < 0) | |
| 787 | 1935 { |
| 1936 gftp_destroy_dir_hash (dirhash); | |
| 1937 return (NULL); | |
| 1938 } | |
| 1 | 1939 |
| 1940 fle = g_malloc (sizeof (*fle)); | |
| 1941 templist = NULL; | |
| 1942 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
| 1943 { | |
| 1944 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) | |
| 1945 { | |
| 598 | 1946 gftp_file_destroy (fle, 0); |
| 1 | 1947 continue; |
| 1948 } | |
| 1949 | |
| 1950 if (dirhash && | |
| 1951 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
| 787 | 1952 { |
| 1953 fle->exists_other_side = 1; | |
| 1954 fle->startsize = *newsize; | |
| 1955 } | |
| 1956 else | |
| 1957 fle->exists_other_side = 0; | |
| 1 | 1958 |
| 381 | 1959 if (transfer->toreq && fle->destfile == NULL) |
| 555 | 1960 fle->destfile = gftp_build_path (transfer->toreq, |
| 1961 transfer->toreq->directory, | |
| 245 | 1962 fle->file, NULL); |
| 1963 | |
| 381 | 1964 if (transfer->fromreq->directory != NULL && |
| 1965 *transfer->fromreq->directory != '\0' && | |
| 1966 *fle->file != '/') | |
| 1967 { | |
| 555 | 1968 newname = gftp_build_path (transfer->fromreq, |
| 1969 transfer->fromreq->directory, | |
| 381 | 1970 fle->file, NULL); |
| 1971 | |
| 1972 g_free (fle->file); | |
| 1973 fle->file = newname; | |
| 1974 } | |
| 1 | 1975 |
| 1976 templist = g_list_append (templist, fle); | |
| 1977 | |
| 787 | 1978 fle = g_malloc0 (sizeof (*fle)); |
| 1 | 1979 } |
| 1980 gftp_end_transfer (transfer->fromreq); | |
| 1981 | |
| 598 | 1982 gftp_file_destroy (fle, 1); |
| 787 | 1983 gftp_destroy_dir_hash (dirhash); |
| 1 | 1984 |
| 1985 return (templist); | |
| 1986 } | |
| 1987 | |
| 1988 | |
| 787 | 1989 static void |
| 1990 _cleanup_get_all_subdirs (gftp_transfer * transfer, char *oldfromdir, | |
| 1991 char *oldtodir, | |
| 1992 void (*update_func) (gftp_transfer * transfer)) | |
| 1 | 1993 { |
| 787 | 1994 if (update_func != NULL) |
| 1995 { | |
| 1996 transfer->numfiles = transfer->numdirs = -1; | |
| 1997 update_func (transfer); | |
| 1998 } | |
| 1999 | |
| 2000 if (oldfromdir != NULL) | |
| 2001 g_free (oldfromdir); | |
| 2002 | |
| 2003 if (oldtodir != NULL) | |
| 2004 g_free (oldtodir); | |
| 2005 } | |
| 2006 | |
| 2007 | |
| 2008 static GList * | |
| 2009 _setup_current_directory_transfer (gftp_transfer * transfer, int *ret) | |
| 2010 { | |
| 1 | 2011 GHashTable * dirhash; |
| 787 | 2012 char *pos, *newname; |
| 1 | 2013 gftp_file * curfle; |
| 787 | 2014 GList * lastlist; |
| 516 | 2015 off_t *newsize; |
| 787 | 2016 |
| 2017 *ret = 0; | |
| 1 | 2018 if (transfer->toreq != NULL) |
| 469 | 2019 { |
| 787 | 2020 dirhash = gftp_gen_dir_hash (transfer->toreq, ret); |
| 2021 if (*ret == GFTP_EFATAL) | |
| 2022 return (NULL); | |
| 469 | 2023 } |
| 1 | 2024 else |
| 2025 dirhash = NULL; | |
| 2026 | |
| 2027 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
| 2028 { | |
| 2029 curfle = lastlist->data; | |
| 381 | 2030 |
| 2031 if ((pos = strrchr (curfle->file, '/')) != NULL) | |
| 2032 pos++; | |
| 2033 else | |
| 2034 pos = curfle->file; | |
| 2035 | |
| 2036 if (dirhash != NULL && | |
| 2037 (newsize = g_hash_table_lookup (dirhash, pos)) != NULL) | |
| 787 | 2038 { |
| 2039 curfle->exists_other_side = 1; | |
| 2040 curfle->startsize = *newsize; | |
| 2041 } | |
| 2042 else | |
| 2043 curfle->exists_other_side = 0; | |
| 1 | 2044 |
| 381 | 2045 if (curfle->size < 0 && GFTP_IS_CONNECTED (transfer->fromreq)) |
| 509 | 2046 { |
| 2047 curfle->size = gftp_get_file_size (transfer->fromreq, curfle->file); | |
| 787 | 2048 if (curfle->size == GFTP_EFATAL) |
| 2049 { | |
| 2050 gftp_destroy_dir_hash (dirhash); | |
| 2051 *ret = curfle->size; | |
| 2052 return (NULL); | |
| 2053 } | |
| 509 | 2054 } |
| 381 | 2055 |
| 2056 if (transfer->toreq && curfle->destfile == NULL) | |
| 555 | 2057 curfle->destfile = gftp_build_path (transfer->toreq, |
| 2058 transfer->toreq->directory, | |
| 381 | 2059 curfle->file, NULL); |
| 2060 | |
| 2061 if (transfer->fromreq->directory != NULL && | |
| 509 | 2062 *transfer->fromreq->directory != '\0' && *curfle->file != '/') |
| 381 | 2063 { |
| 555 | 2064 newname = gftp_build_path (transfer->fromreq, |
| 2065 transfer->fromreq->directory, | |
| 381 | 2066 curfle->file, NULL); |
| 2067 g_free (curfle->file); | |
| 2068 curfle->file = newname; | |
| 2069 } | |
| 1 | 2070 |
| 2071 if (lastlist->next == NULL) | |
| 2072 break; | |
| 2073 } | |
| 2074 | |
| 787 | 2075 gftp_destroy_dir_hash (dirhash); |
| 2076 | |
| 2077 return (lastlist); | |
| 2078 } | |
| 2079 | |
| 2080 | |
| 2081 int | |
| 2082 gftp_get_all_subdirs (gftp_transfer * transfer, | |
| 2083 void (*update_func) (gftp_transfer * transfer)) | |
| 2084 { | |
| 2085 GList * templist, * lastlist; | |
| 2086 char *oldfromdir, *oldtodir; | |
| 2087 gftp_file * curfle; | |
| 2088 off_t linksize; | |
| 2089 mode_t st_mode; | |
| 2090 int ret; | |
| 2091 | |
| 2092 g_return_val_if_fail (transfer != NULL, GFTP_EFATAL); | |
| 2093 g_return_val_if_fail (transfer->fromreq != NULL, GFTP_EFATAL); | |
| 2094 g_return_val_if_fail (transfer->files != NULL, GFTP_EFATAL); | |
| 2095 | |
| 2096 if (transfer->files == NULL) | |
| 2097 return (0); | |
| 2098 | |
| 2099 ret = 0; | |
| 2100 lastlist = _setup_current_directory_transfer (transfer, &ret); | |
| 2101 if (lastlist == NULL) | |
| 2102 return (ret); | |
| 1 | 2103 |
| 2104 oldfromdir = oldtodir = NULL; | |
| 787 | 2105 |
| 1 | 2106 for (templist = transfer->files; templist != NULL; templist = templist->next) |
| 2107 { | |
| 2108 curfle = templist->data; | |
| 2109 | |
| 500 | 2110 if (S_ISLNK (curfle->st_mode) && !S_ISDIR (curfle->st_mode)) |
| 2111 { | |
| 520 | 2112 st_mode = 0; |
| 787 | 2113 linksize = 0; |
| 2114 ret = gftp_stat_filename (transfer->fromreq, curfle->file, &st_mode, | |
| 2115 &linksize); | |
| 520 | 2116 if (ret < 0) |
| 787 | 2117 { |
| 2118 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
| 2119 update_func); | |
| 2120 return (ret); | |
| 2121 } | |
| 509 | 2122 else if (S_ISDIR (st_mode)) |
| 500 | 2123 curfle->st_mode = st_mode; |
| 787 | 2124 else |
| 2125 curfle->size = linksize; | |
| 500 | 2126 } |
| 2127 | |
| 787 | 2128 if (!(curfle->st_mode & S_IFDIR)) |
| 1 | 2129 { |
| 787 | 2130 transfer->numfiles++; |
| 2131 continue; | |
| 2132 } | |
| 2133 | |
| 2134 /* Got a directory... */ | |
| 2135 if (oldfromdir == NULL) | |
| 2136 oldfromdir = g_strdup (transfer->fromreq->directory); | |
| 2137 | |
| 2138 ret = gftp_set_directory (transfer->fromreq, curfle->file); | |
| 2139 if (ret < 0) | |
| 2140 { | |
| 2141 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
| 2142 update_func); | |
| 2143 return (ret); | |
| 2144 } | |
| 2145 | |
| 2146 if (transfer->toreq != NULL) | |
| 2147 { | |
| 2148 if (oldtodir == NULL) | |
| 2149 oldtodir = g_strdup (transfer->toreq->directory); | |
| 2150 | |
| 2151 if (curfle->exists_other_side) | |
| 1 | 2152 { |
| 787 | 2153 ret = gftp_set_directory (transfer->toreq, curfle->destfile); |
| 2154 if (ret == GFTP_EFATAL) | |
| 2155 { | |
| 2156 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
| 2157 update_func); | |
| 2158 return (ret); | |
| 2159 } | |
| 2160 } | |
| 2161 else | |
| 509 | 2162 { |
| 787 | 2163 if (transfer->toreq->directory != NULL) |
| 2164 g_free (transfer->toreq->directory); | |
| 2165 | |
| 2166 transfer->toreq->directory = g_strdup (curfle->destfile); | |
| 1 | 2167 } |
| 787 | 2168 } |
| 2169 | |
| 2170 ret = 0; | |
| 2171 lastlist->next = gftp_get_dir_listing (transfer, | |
| 2172 curfle->exists_other_side, &ret); | |
| 2173 if (ret < 0) | |
| 2174 { | |
| 2175 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
| 2176 update_func); | |
| 2177 return (ret); | |
| 1 | 2178 } |
| 787 | 2179 |
| 2180 if (lastlist->next != NULL) | |
| 2181 { | |
| 2182 lastlist->next->prev = lastlist; | |
| 2183 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
| 2184 } | |
| 2185 | |
| 2186 transfer->numdirs++; | |
| 2187 if (update_func != NULL) | |
| 2188 update_func (transfer); | |
| 1 | 2189 } |
| 2190 | |
| 787 | 2191 if (oldfromdir != NULL) |
| 509 | 2192 { |
| 787 | 2193 ret = gftp_set_directory (transfer->fromreq, oldfromdir); |
| 509 | 2194 if (ret < 0) |
| 787 | 2195 { |
| 2196 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
| 2197 update_func); | |
| 2198 return (ret); | |
| 2199 } | |
| 509 | 2200 } |
| 2201 | |
| 787 | 2202 if (oldtodir != NULL) |
| 509 | 2203 { |
| 787 | 2204 ret = gftp_set_directory (transfer->toreq, oldtodir); |
| 509 | 2205 if (ret < 0) |
| 787 | 2206 { |
| 2207 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
| 2208 update_func); | |
| 2209 return (ret); | |
| 2210 } | |
| 509 | 2211 } |
| 2212 | |
| 787 | 2213 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, update_func); |
| 509 | 2214 |
| 1 | 2215 return (0); |
| 2216 } | |
| 2217 | |
| 2218 | |
| 2219 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 122 | 2220 static int |
| 1 | 2221 get_port (struct addrinfo *addr) |
| 2222 { | |
| 2223 struct sockaddr_in * saddr; | |
| 2224 int port; | |
| 2225 | |
| 2226 if (addr->ai_family == AF_INET) | |
| 2227 { | |
| 2228 saddr = (struct sockaddr_in *) addr->ai_addr; | |
| 2229 port = ntohs (saddr->sin_port); | |
| 2230 } | |
| 2231 else | |
| 2232 port = 0; | |
| 2233 | |
| 2234 return (port); | |
| 2235 } | |
| 2236 #endif | |
| 2237 | |
| 2238 | |
| 2239 int | |
| 122 | 2240 gftp_connect_server (gftp_request * request, char *service, |
| 516 | 2241 char *proxy_hostname, unsigned int proxy_port) |
| 1 | 2242 { |
| 2243 char *connect_host, *disphost; | |
| 516 | 2244 unsigned int port; |
| 2245 int sock = -1; | |
| 1 | 2246 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
| 2247 struct addrinfo hints, *res; | |
| 463 | 2248 intptr_t enable_ipv6; |
| 1 | 2249 char serv[8]; |
| 463 | 2250 int errnum; |
| 1 | 2251 |
| 516 | 2252 if ((errnum = gftp_need_proxy (request, service, proxy_hostname, |
| 2253 proxy_port)) < 0) | |
| 2254 return (errnum); | |
| 2255 else | |
| 2256 { | |
| 2257 request->use_proxy = errnum; | |
| 2258 if (request->use_proxy) | |
| 2259 request->hostp = NULL; | |
| 2260 } | |
| 1 | 2261 |
| 313 | 2262 gftp_lookup_request_option (request, "enable_ipv6", &enable_ipv6); |
| 2263 | |
| 151 | 2264 request->free_hostp = 1; |
| 1 | 2265 memset (&hints, 0, sizeof (hints)); |
| 2266 hints.ai_flags = AI_CANONNAME; | |
| 313 | 2267 |
| 2268 if (enable_ipv6) | |
| 2269 hints.ai_family = PF_UNSPEC; | |
| 2270 else | |
| 2271 hints.ai_family = AF_INET; | |
| 2272 | |
| 1 | 2273 hints.ai_socktype = SOCK_STREAM; |
| 2274 | |
| 122 | 2275 if (request->use_proxy) |
| 2276 { | |
| 2277 connect_host = proxy_hostname; | |
| 2278 port = proxy_port; | |
| 2279 } | |
| 2280 else | |
| 2281 { | |
| 2282 connect_host = request->hostname; | |
| 2283 port = request->port; | |
| 2284 } | |
| 1 | 2285 |
| 2286 if (request->hostp == NULL) | |
| 2287 { | |
| 2288 if (port == 0) | |
| 2289 strcpy (serv, service); | |
| 2290 else | |
| 2291 snprintf (serv, sizeof (serv), "%d", port); | |
| 2292 | |
| 186 | 2293 request->logging_function (gftp_logging_misc, request, |
| 168 | 2294 _("Looking up %s\n"), connect_host); |
| 1 | 2295 if ((errnum = getaddrinfo (connect_host, serv, &hints, |
| 2296 &request->hostp)) != 0) | |
| 168 | 2297 { |
| 186 | 2298 request->logging_function (gftp_logging_error, request, |
| 168 | 2299 _("Cannot look up hostname %s: %s\n"), |
| 2300 connect_host, gai_strerror (errnum)); | |
| 2301 return (GFTP_ERETRYABLE); | |
| 2302 } | |
| 1 | 2303 } |
| 2304 | |
| 2305 disphost = connect_host; | |
| 2306 for (res = request->hostp; res != NULL; res = res->ai_next) | |
| 2307 { | |
| 2308 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
| 2309 port = get_port (res); | |
| 56 | 2310 if (!request->use_proxy) |
| 2311 request->port = port; | |
| 2312 | |
| 1 | 2313 if ((sock = socket (res->ai_family, res->ai_socktype, |
| 2314 res->ai_protocol)) < 0) | |
| 2315 { | |
| 186 | 2316 request->logging_function (gftp_logging_error, request, |
| 1 | 2317 _("Failed to create a socket: %s\n"), |
| 2318 g_strerror (errno)); | |
| 2319 continue; | |
| 66 | 2320 } |
| 1 | 2321 |
| 186 | 2322 request->logging_function (gftp_logging_misc, request, |
| 168 | 2323 _("Trying %s:%d\n"), disphost, port); |
| 1 | 2324 |
| 2325 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
| 168 | 2326 { |
| 186 | 2327 request->logging_function (gftp_logging_error, request, |
| 168 | 2328 _("Cannot connect to %s: %s\n"), |
| 2329 disphost, g_strerror (errno)); | |
| 1 | 2330 close (sock); |
| 2331 continue; | |
| 168 | 2332 } |
| 547 | 2333 |
| 572 | 2334 request->current_hostp = res; |
| 547 | 2335 request->ai_family = res->ai_family; |
| 1 | 2336 break; |
| 2337 } | |
| 2338 | |
| 2339 if (res == NULL) | |
| 2340 { | |
| 2341 if (request->hostp != NULL) | |
| 2342 { | |
| 2343 freeaddrinfo (request->hostp); | |
| 2344 request->hostp = NULL; | |
| 2345 } | |
| 84 | 2346 return (GFTP_ERETRYABLE); |
| 1 | 2347 } |
| 2348 | |
| 2349 #else /* !HAVE_GETADDRINFO */ | |
| 2350 struct sockaddr_in remote_address; | |
| 2351 struct servent serv_struct; | |
| 765 | 2352 int ret; |
| 2353 | |
| 2354 if ((ret = gftp_need_proxy (request, service, proxy_hostname, | |
| 2355 proxy_port)) < 0) | |
| 2356 return (ret); | |
| 2357 | |
| 2358 request->use_proxy = ret; | |
| 2359 if (request->use_proxy == 1) | |
| 1 | 2360 request->hostp = NULL; |
| 2361 | |
| 547 | 2362 request->ai_family = AF_INET; |
| 1 | 2363 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) |
| 2364 { | |
| 186 | 2365 request->logging_function (gftp_logging_error, request, |
| 313 | 2366 _("Failed to create a IPv4 socket: %s\n"), |
| 1 | 2367 g_strerror (errno)); |
| 84 | 2368 return (GFTP_ERETRYABLE); |
| 1 | 2369 } |
| 2370 | |
| 2371 memset (&remote_address, 0, sizeof (remote_address)); | |
| 2372 remote_address.sin_family = AF_INET; | |
| 2373 | |
| 122 | 2374 if (request->use_proxy) |
| 2375 { | |
| 2376 connect_host = proxy_hostname; | |
| 2377 port = proxy_port; | |
| 2378 } | |
| 2379 else | |
| 2380 { | |
| 2381 connect_host = request->hostname; | |
| 2382 port = request->port; | |
| 2383 } | |
| 1 | 2384 |
| 2385 if (port == 0) | |
| 2386 { | |
| 2387 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
| 2388 { | |
| 186 | 2389 request->logging_function (gftp_logging_error, request, |
| 122 | 2390 _("Cannot look up service name %s/tcp. Please check your services file\n"), |
| 2391 service); | |
| 2392 close (sock); | |
| 2393 return (GFTP_EFATAL); | |
| 1 | 2394 } |
| 451 | 2395 |
| 2396 port = ntohs (serv_struct.s_port); | |
| 56 | 2397 |
| 2398 if (!request->use_proxy) | |
| 451 | 2399 request->port = port; |
| 1 | 2400 } |
| 451 | 2401 |
| 2402 remote_address.sin_port = htons (port); | |
| 1 | 2403 |
| 2404 if (request->hostp == NULL) | |
| 2405 { | |
| 186 | 2406 request->logging_function (gftp_logging_misc, request, |
| 168 | 2407 _("Looking up %s\n"), connect_host); |
| 1 | 2408 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, |
| 2409 NULL))) | |
| 2410 { | |
| 186 | 2411 request->logging_function (gftp_logging_error, request, |
| 1 | 2412 _("Cannot look up hostname %s: %s\n"), |
| 2413 connect_host, g_strerror (errno)); | |
| 2414 close (sock); | |
| 84 | 2415 return (GFTP_ERETRYABLE); |
| 1 | 2416 } |
| 2417 } | |
| 2418 | |
| 2419 disphost = NULL; | |
| 641 | 2420 for (request->curhost = 0; |
| 2421 request->host.h_addr_list[request->curhost] != NULL; | |
| 2422 request->curhost++) | |
| 1 | 2423 { |
| 2424 disphost = request->host.h_name; | |
| 641 | 2425 memcpy (&remote_address.sin_addr, |
| 2426 request->host.h_addr_list[request->curhost], | |
| 1 | 2427 request->host.h_length); |
| 186 | 2428 request->logging_function (gftp_logging_misc, request, |
| 1 | 2429 _("Trying %s:%d\n"), |
| 451 | 2430 request->host.h_name, port); |
| 1 | 2431 |
| 2432 if (connect (sock, (struct sockaddr *) &remote_address, | |
| 2433 sizeof (remote_address)) == -1) | |
| 2434 { | |
| 186 | 2435 request->logging_function (gftp_logging_error, request, |
| 1 | 2436 _("Cannot connect to %s: %s\n"), |
| 2437 connect_host, g_strerror (errno)); | |
| 2438 } | |
| 2439 break; | |
| 2440 } | |
| 2441 | |
| 641 | 2442 if (request->host.h_addr_list[request->curhost] == NULL) |
| 1 | 2443 { |
| 2444 close (sock); | |
| 84 | 2445 return (GFTP_ERETRYABLE); |
| 1 | 2446 } |
| 2447 #endif /* HAVE_GETADDRINFO */ | |
| 2448 | |
| 182 | 2449 if (fcntl (sock, F_SETFD, 1) == -1) |
| 2450 { | |
| 186 | 2451 request->logging_function (gftp_logging_error, request, |
| 182 | 2452 _("Error: Cannot set close on exec flag: %s\n"), |
| 2453 g_strerror (errno)); | |
| 2454 | |
| 2455 return (GFTP_ERETRYABLE); | |
| 2456 } | |
| 2457 | |
| 186 | 2458 request->logging_function (gftp_logging_misc, request, |
| 168 | 2459 _("Connected to %s:%d\n"), connect_host, port); |
| 58 | 2460 |
| 168 | 2461 if (gftp_fd_set_sockblocking (request, sock, 1) < 0) |
| 58 | 2462 { |
| 2463 close (sock); | |
| 84 | 2464 return (GFTP_ERETRYABLE); |
| 58 | 2465 } |
| 2466 | |
| 169 | 2467 request->datafd = sock; |
| 168 | 2468 |
| 2469 if (request->post_connect != NULL) | |
| 2470 return (request->post_connect (request)); | |
| 2471 | |
| 2472 return (0); | |
| 1 | 2473 } |
| 2474 | |
| 2475 | |
| 177 | 2476 int |
| 1 | 2477 gftp_set_config_options (gftp_request * request) |
| 2478 { | |
| 58 | 2479 if (request->set_config_options != NULL) |
| 177 | 2480 return (request->set_config_options (request)); |
| 2481 else | |
| 2482 return (0); | |
| 1 | 2483 } |
| 2484 | |
| 2485 | |
| 2486 void | |
| 2487 print_file_list (GList * list) | |
| 2488 { | |
| 2489 gftp_file * tempfle; | |
| 2490 GList * templist; | |
| 499 | 2491 char *attribs; |
| 1 | 2492 |
| 2493 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
| 2494 for (templist = list; ; templist = templist->next) | |
| 2495 { | |
| 2496 tempfle = templist->data; | |
| 499 | 2497 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
| 2498 | |
| 532 | 2499 printf ("%s:%s:" GFTP_OFF_T_PRINTF_MOD ":" GFTP_OFF_T_PRINTF_MOD ":%s:%s:%s\n", |
| 372 | 2500 tempfle->file, tempfle->destfile, |
| 516 | 2501 tempfle->size, tempfle->startsize, |
| 499 | 2502 tempfle->user, tempfle->group, attribs); |
| 2503 | |
| 2504 g_free (attribs); | |
| 1 | 2505 if (templist->next == NULL) |
| 2506 break; | |
| 2507 } | |
| 2508 | |
| 2509 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
| 2510 for (; ; templist = templist->prev) | |
| 2511 { | |
| 2512 tempfle = templist->data; | |
| 499 | 2513 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
| 2514 | |
| 532 | 2515 printf ("%s:%s:" GFTP_OFF_T_PRINTF_MOD ":" GFTP_OFF_T_PRINTF_MOD ":%s:%s:%s\n", |
| 372 | 2516 tempfle->file, tempfle->destfile, |
| 516 | 2517 tempfle->size, tempfle->startsize, |
| 499 | 2518 tempfle->user, tempfle->group, attribs); |
| 2519 | |
| 2520 g_free (attribs); | |
| 1 | 2521 if (templist == list) |
| 2522 break; | |
| 2523 } | |
| 2524 printf ("--END OF FILE LISTING--\n"); | |
| 2525 } | |
| 2526 | |
| 41 | 2527 |
| 201 | 2528 void |
| 58 | 2529 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) |
| 41 | 2530 { |
| 58 | 2531 g_free ((*rbuf)->buffer); |
| 2532 g_free (*rbuf); | |
| 2533 *rbuf = NULL; | |
| 41 | 2534 } |
| 2535 | |
| 2536 | |
| 58 | 2537 ssize_t |
| 2538 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
| 2539 char * str, size_t len, int fd) | |
| 41 | 2540 { |
| 168 | 2541 ssize_t (*read_function) (gftp_request * request, void *ptr, size_t size, |
| 2542 int fd); | |
| 516 | 2543 char *pos, *nextpos; |
| 2544 size_t rlen, nslen; | |
| 179 | 2545 int end_of_buffer; |
| 516 | 2546 ssize_t ret; |
| 168 | 2547 |
| 2548 if (request == NULL || request->read_function == NULL) | |
| 2549 read_function = gftp_fd_read; | |
| 2550 else | |
| 2551 read_function = request->read_function; | |
| 58 | 2552 |
| 2553 if (*rbuf == NULL) | |
| 2554 { | |
| 2555 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
| 2556 (*rbuf)->max_bufsize = len; | |
| 765 | 2557 (*rbuf)->buffer = g_malloc0 ((gulong) ((*rbuf)->max_bufsize + 1)); |
| 168 | 2558 |
| 2559 if ((ret = read_function (request, (*rbuf)->buffer, | |
| 2560 (*rbuf)->max_bufsize, fd)) <= 0) | |
| 58 | 2561 { |
| 2562 gftp_free_getline_buffer (rbuf); | |
| 2563 return (ret); | |
| 2564 } | |
| 60 | 2565 (*rbuf)->buffer[ret] = '\0'; |
| 58 | 2566 (*rbuf)->cur_bufsize = ret; |
| 2567 (*rbuf)->curpos = (*rbuf)->buffer; | |
| 2568 } | |
| 2569 | |
| 516 | 2570 ret = 0; |
| 2571 while (1) | |
| 58 | 2572 { |
| 179 | 2573 pos = strchr ((*rbuf)->curpos, '\n'); |
| 2574 end_of_buffer = (*rbuf)->curpos == (*rbuf)->buffer && | |
| 2575 ((*rbuf)->max_bufsize == (*rbuf)->cur_bufsize || (*rbuf)->eof); | |
| 2576 | |
| 2577 if ((*rbuf)->cur_bufsize > 0 && (pos != NULL || end_of_buffer)) | |
| 58 | 2578 { |
| 2579 if (pos != NULL) | |
| 2580 { | |
| 516 | 2581 nslen = pos - (*rbuf)->curpos + 1; |
| 58 | 2582 nextpos = pos + 1; |
| 2583 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
| 2584 pos--; | |
| 2585 *pos = '\0'; | |
| 2586 } | |
| 2587 else | |
| 249 | 2588 { |
| 516 | 2589 nslen = (*rbuf)->cur_bufsize; |
| 249 | 2590 nextpos = NULL; |
| 2591 | |
| 2592 /* This is not an overflow since we allocated one extra byte to | |
| 2593 buffer above */ | |
| 798 | 2594 ((*rbuf)->buffer)[nslen] = '\0'; |
| 249 | 2595 } |
| 58 | 2596 |
| 2597 strncpy (str, (*rbuf)->curpos, len); | |
| 249 | 2598 str[len - 1] = '\0'; |
| 516 | 2599 (*rbuf)->cur_bufsize -= nslen; |
| 58 | 2600 |
| 168 | 2601 if (nextpos != NULL) |
| 2602 (*rbuf)->curpos = nextpos; | |
| 2603 else | |
| 2604 (*rbuf)->cur_bufsize = 0; | |
| 516 | 2605 |
| 2606 ret = nslen; | |
| 58 | 2607 break; |
| 2608 } | |
| 2609 else | |
| 2610 { | |
| 2611 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
| 2612 { | |
| 2613 rlen = (*rbuf)->max_bufsize; | |
| 2614 pos = (*rbuf)->buffer; | |
| 2615 } | |
| 2616 else | |
| 2617 { | |
| 168 | 2618 memmove ((*rbuf)->buffer, (*rbuf)->curpos, (*rbuf)->cur_bufsize); |
| 2619 pos = (*rbuf)->buffer + (*rbuf)->cur_bufsize; | |
| 2620 rlen = (*rbuf)->max_bufsize - (*rbuf)->cur_bufsize; | |
| 58 | 2621 } |
| 168 | 2622 |
| 58 | 2623 (*rbuf)->curpos = (*rbuf)->buffer; |
| 2624 | |
| 209 | 2625 if ((*rbuf)->eof) |
| 2626 ret = 0; | |
| 2627 else | |
| 58 | 2628 { |
| 798 | 2629 ret = read_function (request, pos, rlen, fd); |
| 2630 if (ret < 0) | |
| 2631 { | |
| 2632 gftp_free_getline_buffer (rbuf); | |
| 2633 return (ret); | |
| 2634 } | |
| 58 | 2635 } |
| 798 | 2636 |
| 2637 if (ret == 0) | |
| 168 | 2638 { |
| 179 | 2639 if ((*rbuf)->cur_bufsize == 0) |
| 2640 { | |
| 2641 gftp_free_getline_buffer (rbuf); | |
| 2642 return (ret); | |
| 2643 } | |
| 2644 | |
| 2645 (*rbuf)->eof = 1; | |
| 168 | 2646 } |
| 2647 | |
| 2648 (*rbuf)->cur_bufsize += ret; | |
| 798 | 2649 (*rbuf)->buffer[(*rbuf)->cur_bufsize] = '\0'; |
| 58 | 2650 } |
| 2651 } | |
| 516 | 2652 |
| 2653 return (ret); | |
| 58 | 2654 } |
| 2655 | |
| 2656 | |
| 2657 ssize_t | |
| 168 | 2658 gftp_fd_read (gftp_request * request, void *ptr, size_t size, int fd) |
| 58 | 2659 { |
| 325 | 2660 intptr_t network_timeout; |
| 58 | 2661 struct timeval tv; |
| 2662 fd_set fset; | |
| 2663 ssize_t ret; | |
| 41 | 2664 |
| 122 | 2665 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
| 2666 | |
| 41 | 2667 errno = 0; |
| 2668 ret = 0; | |
| 518 | 2669 |
| 546 | 2670 do |
| 41 | 2671 { |
| 58 | 2672 FD_ZERO (&fset); |
| 2673 FD_SET (fd, &fset); | |
| 122 | 2674 tv.tv_sec = network_timeout; |
| 58 | 2675 tv.tv_usec = 0; |
| 2676 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
| 546 | 2677 if (ret == -1 && (errno == EINTR || errno == EAGAIN)) |
| 41 | 2678 { |
| 546 | 2679 if (request != NULL && request->cancel) |
| 2680 { | |
| 2681 gftp_disconnect (request); | |
| 2682 return (GFTP_ERETRYABLE); | |
| 2683 } | |
| 2684 | |
| 2685 continue; | |
| 58 | 2686 } |
| 2687 else if (ret <= 0) | |
| 2688 { | |
| 2689 if (request != NULL) | |
| 41 | 2690 { |
| 186 | 2691 request->logging_function (gftp_logging_error, request, |
| 58 | 2692 _("Connection to %s timed out\n"), |
| 2693 request->hostname); | |
| 2694 gftp_disconnect (request); | |
| 2695 } | |
| 546 | 2696 |
| 84 | 2697 return (GFTP_ERETRYABLE); |
| 41 | 2698 } |
| 2699 | |
| 58 | 2700 if ((ret = read (fd, ptr, size)) < 0) |
| 41 | 2701 { |
| 546 | 2702 if (errno == EINTR || errno == EAGAIN) |
| 41 | 2703 { |
| 58 | 2704 if (request != NULL && request->cancel) |
| 546 | 2705 { |
| 2706 gftp_disconnect (request); | |
| 2707 return (GFTP_ERETRYABLE); | |
| 2708 } | |
| 2709 | |
| 2710 continue; | |
| 518 | 2711 } |
| 41 | 2712 |
| 58 | 2713 if (request != NULL) |
| 2714 { | |
| 186 | 2715 request->logging_function (gftp_logging_error, request, |
| 58 | 2716 _("Error: Could not read from socket: %s\n"), |
| 41 | 2717 g_strerror (errno)); |
| 58 | 2718 gftp_disconnect (request); |
| 2719 } | |
| 546 | 2720 |
| 84 | 2721 return (GFTP_ERETRYABLE); |
| 41 | 2722 } |
| 546 | 2723 |
| 518 | 2724 break; |
| 41 | 2725 } |
| 546 | 2726 while (1); |
| 41 | 2727 |
| 2728 return (ret); | |
| 2729 } | |
| 2730 | |
| 58 | 2731 |
| 2732 ssize_t | |
| 168 | 2733 gftp_fd_write (gftp_request * request, const char *ptr, size_t size, int fd) |
| 58 | 2734 { |
| 325 | 2735 intptr_t network_timeout; |
| 58 | 2736 struct timeval tv; |
| 248 | 2737 ssize_t w_ret; |
| 58 | 2738 fd_set fset; |
| 516 | 2739 int ret; |
| 58 | 2740 |
| 122 | 2741 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
| 2742 | |
| 58 | 2743 errno = 0; |
| 2744 ret = 0; | |
| 2745 do | |
| 2746 { | |
| 2747 FD_ZERO (&fset); | |
| 2748 FD_SET (fd, &fset); | |
| 122 | 2749 tv.tv_sec = network_timeout; |
| 58 | 2750 tv.tv_usec = 0; |
| 2751 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
| 546 | 2752 if (ret == -1 && (errno == EINTR || errno == EAGAIN)) |
| 58 | 2753 { |
| 2754 if (request != NULL && request->cancel) | |
| 546 | 2755 { |
| 2756 gftp_disconnect (request); | |
| 2757 return (GFTP_ERETRYABLE); | |
| 2758 } | |
| 2759 | |
| 2760 continue; | |
| 58 | 2761 } |
| 2762 else if (ret <= 0) | |
| 2763 { | |
| 2764 if (request != NULL) | |
| 2765 { | |
| 186 | 2766 request->logging_function (gftp_logging_error, request, |
| 58 | 2767 _("Connection to %s timed out\n"), |
| 2768 request->hostname); | |
| 2769 gftp_disconnect (request); | |
| 2770 } | |
| 546 | 2771 |
| 84 | 2772 return (GFTP_ERETRYABLE); |
| 58 | 2773 } |
| 2774 | |
| 248 | 2775 w_ret = write (fd, ptr, size); |
| 2776 if (w_ret < 0) | |
| 58 | 2777 { |
| 546 | 2778 if (errno == EINTR || errno == EAGAIN) |
| 58 | 2779 { |
| 2780 if (request != NULL && request->cancel) | |
| 546 | 2781 { |
| 2782 gftp_disconnect (request); | |
| 2783 return (GFTP_ERETRYABLE); | |
| 2784 } | |
| 2785 | |
| 2786 continue; | |
| 58 | 2787 } |
| 2788 | |
| 2789 if (request != NULL) | |
| 2790 { | |
| 186 | 2791 request->logging_function (gftp_logging_error, request, |
| 58 | 2792 _("Error: Could not write to socket: %s\n"), |
| 2793 g_strerror (errno)); | |
| 2794 gftp_disconnect (request); | |
| 2795 } | |
| 546 | 2796 |
| 84 | 2797 return (GFTP_ERETRYABLE); |
| 58 | 2798 } |
| 2799 | |
| 2800 ptr += w_ret; | |
| 2801 size -= w_ret; | |
| 2802 ret += w_ret; | |
| 2803 } | |
| 2804 while (size > 0); | |
| 2805 | |
| 2806 return (ret); | |
| 2807 } | |
| 2808 | |
| 2809 | |
| 2810 ssize_t | |
| 2811 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
| 2812 { | |
| 2813 char *tempstr; | |
| 2814 va_list argp; | |
| 2815 ssize_t ret; | |
| 2816 | |
| 2817 va_start (argp, fmt); | |
| 2818 tempstr = g_strdup_vprintf (fmt, argp); | |
| 2819 va_end (argp); | |
| 2820 | |
| 168 | 2821 ret = request->write_function (request, tempstr, strlen (tempstr), fd); |
| 58 | 2822 g_free (tempstr); |
| 2823 return (ret); | |
| 2824 } | |
| 2825 | |
| 2826 | |
| 2827 int | |
| 168 | 2828 gftp_fd_set_sockblocking (gftp_request * request, int fd, int non_blocking) |
| 58 | 2829 { |
| 2830 int flags; | |
| 2831 | |
| 84 | 2832 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) |
| 58 | 2833 { |
| 186 | 2834 request->logging_function (gftp_logging_error, request, |
| 58 | 2835 _("Cannot get socket flags: %s\n"), |
| 2836 g_strerror (errno)); | |
| 2837 gftp_disconnect (request); | |
| 84 | 2838 return (GFTP_ERETRYABLE); |
| 58 | 2839 } |
| 2840 | |
| 2841 if (non_blocking) | |
| 2842 flags |= O_NONBLOCK; | |
| 2843 else | |
| 2844 flags &= ~O_NONBLOCK; | |
| 2845 | |
| 84 | 2846 if (fcntl (fd, F_SETFL, flags) < 0) |
| 58 | 2847 { |
| 186 | 2848 request->logging_function (gftp_logging_error, request, |
| 58 | 2849 _("Cannot set socket to non-blocking: %s\n"), |
| 2850 g_strerror (errno)); | |
| 2851 gftp_disconnect (request); | |
| 84 | 2852 return (GFTP_ERETRYABLE); |
| 58 | 2853 } |
| 2854 | |
| 2855 return (0); | |
| 2856 } | |
| 2857 | |
| 2858 | |
| 63 | 2859 void |
| 2860 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
| 2861 { | |
| 2862 g_return_if_fail (dest != NULL); | |
| 2863 g_return_if_fail (source != NULL); | |
| 2864 g_return_if_fail (dest->protonum == source->protonum); | |
| 2865 | |
| 2866 dest->datafd = source->datafd; | |
| 2867 dest->cached = 0; | |
| 397 | 2868 #ifdef USE_SSL |
| 2869 dest->ssl = source->ssl; | |
| 2870 #endif | |
| 2871 | |
| 63 | 2872 if (!source->always_connected) |
| 2873 { | |
| 2874 source->datafd = -1; | |
| 2875 source->cached = 1; | |
| 397 | 2876 #ifdef USE_SSL |
| 2877 source->ssl = NULL; | |
| 2878 #endif | |
| 63 | 2879 } |
| 2880 | |
| 516 | 2881 if (dest->swap_socks != NULL) |
| 63 | 2882 dest->swap_socks (dest, source); |
| 2883 } | |
| 2884 | |
| 122 | 2885 |
| 2886 void | |
| 2887 gftp_calc_kbs (gftp_transfer * tdata, ssize_t num_read) | |
| 2888 { | |
| 469 | 2889 /* Needed for systems that size(float) < size(void *) */ |
| 2890 union { intptr_t i; float f; } maxkbs; | |
| 122 | 2891 unsigned long waitusecs; |
| 220 | 2892 double start_difftime; |
| 122 | 2893 struct timeval tv; |
| 222 | 2894 int waited; |
| 122 | 2895 |
| 469 | 2896 gftp_lookup_request_option (tdata->fromreq, "maxkbs", &maxkbs.f); |
| 122 | 2897 |
| 2898 if (g_thread_supported ()) | |
| 2899 g_static_mutex_lock (&tdata->statmutex); | |
| 2900 | |
| 220 | 2901 gettimeofday (&tv, NULL); |
| 2902 | |
| 122 | 2903 tdata->trans_bytes += num_read; |
| 2904 tdata->curtrans += num_read; | |
| 2905 tdata->stalled = 0; | |
| 2906 | |
| 220 | 2907 start_difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0); |
| 2908 | |
| 2909 if (start_difftime <= 0) | |
| 2910 tdata->kbs = tdata->trans_bytes / 1024.0; | |
| 122 | 2911 else |
| 220 | 2912 tdata->kbs = tdata->trans_bytes / 1024.0 / start_difftime; |
| 2913 | |
| 222 | 2914 waited = 0; |
| 469 | 2915 if (maxkbs.f > 0 && tdata->kbs > maxkbs.f) |
| 122 | 2916 { |
| 469 | 2917 waitusecs = num_read / 1024.0 / maxkbs.f * 1000000.0 - start_difftime; |
| 122 | 2918 |
| 2919 if (waitusecs > 0) | |
| 2920 { | |
| 2921 if (g_thread_supported ()) | |
| 2922 g_static_mutex_unlock (&tdata->statmutex); | |
| 2923 | |
| 222 | 2924 waited = 1; |
| 122 | 2925 usleep (waitusecs); |
| 2926 | |
| 2927 if (g_thread_supported ()) | |
| 2928 g_static_mutex_lock (&tdata->statmutex); | |
| 2929 } | |
| 222 | 2930 |
| 122 | 2931 } |
| 2932 | |
| 222 | 2933 if (waited) |
| 2934 gettimeofday (&tdata->lasttime, NULL); | |
| 2935 else | |
| 2936 memcpy (&tdata->lasttime, &tv, sizeof (tdata->lasttime)); | |
| 122 | 2937 |
| 2938 if (g_thread_supported ()) | |
| 2939 g_static_mutex_unlock (&tdata->statmutex); | |
| 2940 } | |
| 2941 | |
| 125 | 2942 |
| 764 | 2943 static int |
| 2944 _do_sleep (int sleep_time) | |
| 2945 { | |
| 2946 struct timeval tv; | |
| 2947 int ret; | |
| 2948 | |
| 2949 tv.tv_sec = sleep_time; | |
| 2950 tv.tv_usec = 0; | |
| 2951 | |
| 2952 /* FIXME - check for user aborted connection */ | |
| 2953 do | |
| 2954 { | |
| 2955 ret = select (0, NULL, NULL, NULL, &tv); | |
| 2956 } | |
| 2957 while (ret == -1 && (errno == EINTR || errno == EAGAIN)); | |
| 2958 | |
| 2959 return (ret); | |
| 2960 } | |
| 2961 | |
| 2962 | |
| 125 | 2963 int |
| 2964 gftp_get_transfer_status (gftp_transfer * tdata, ssize_t num_read) | |
| 2965 { | |
| 325 | 2966 intptr_t retries, sleep_time; |
| 125 | 2967 gftp_file * tempfle; |
| 498 | 2968 int ret1, ret2; |
| 125 | 2969 |
| 2970 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
| 2971 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
| 2972 | |
| 2973 if (g_thread_supported ()) | |
| 2974 g_static_mutex_lock (&tdata->structmutex); | |
| 2975 | |
| 2976 if (tdata->curfle == NULL) | |
| 2977 { | |
| 2978 if (g_thread_supported ()) | |
| 2979 g_static_mutex_unlock (&tdata->structmutex); | |
| 2980 | |
| 2981 return (GFTP_EFATAL); | |
| 2982 } | |
| 2983 | |
| 2984 tempfle = tdata->curfle->data; | |
| 2985 | |
| 2986 if (g_thread_supported ()) | |
| 2987 g_static_mutex_unlock (&tdata->structmutex); | |
| 2988 | |
| 2989 gftp_disconnect (tdata->fromreq); | |
| 2990 gftp_disconnect (tdata->toreq); | |
| 2991 | |
| 764 | 2992 if (tdata->cancel || num_read == GFTP_EFATAL) |
| 2993 return (GFTP_EFATAL); | |
| 2994 else if (num_read >= 0 && !tdata->skip_file) | |
| 2995 return (0); | |
| 2996 | |
| 2997 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout) | |
| 125 | 2998 { |
| 764 | 2999 if (retries != 0 && |
| 3000 tdata->current_file_retries >= retries) | |
| 3001 { | |
| 3002 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, | |
| 3003 _("Error: Remote site %s disconnected. Max retries reached...giving up\n"), | |
| 3004 tdata->fromreq->hostname != NULL ? | |
| 3005 tdata->fromreq->hostname : tdata->toreq->hostname); | |
| 3006 return (GFTP_EFATAL); | |
| 3007 } | |
| 3008 else | |
| 125 | 3009 { |
| 764 | 3010 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, |
| 3011 _("Error: Remote site %s disconnected. Will reconnect in %d seconds\n"), | |
| 3012 tdata->fromreq->hostname != NULL ? | |
| 3013 tdata->fromreq->hostname : tdata->toreq->hostname, | |
| 3014 sleep_time); | |
| 3015 } | |
| 3016 } | |
| 3017 | |
| 3018 while (retries == 0 || | |
| 3019 tdata->current_file_retries <= retries) | |
| 3020 { | |
| 3021 /* Look up the options in case the user changes them... */ | |
| 3022 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
| 3023 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
| 3024 | |
| 3025 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout && | |
| 3026 !tdata->skip_file) | |
| 3027 _do_sleep (sleep_time); | |
| 3028 | |
| 3029 tdata->current_file_retries++; | |
| 3030 | |
| 3031 ret1 = ret2 = 0; | |
| 3032 if ((ret1 = gftp_connect (tdata->fromreq)) == 0 && | |
| 3033 (ret2 = gftp_connect (tdata->toreq)) == 0) | |
| 3034 { | |
| 3035 if (g_thread_supported ()) | |
| 3036 g_static_mutex_lock (&tdata->structmutex); | |
| 3037 | |
| 3038 tdata->resumed_bytes = tdata->resumed_bytes + tdata->trans_bytes - tdata->curresumed - tdata->curtrans; | |
| 3039 tdata->trans_bytes = 0; | |
| 3040 if (tdata->skip_file) | |
| 303 | 3041 { |
| 764 | 3042 tdata->total_bytes -= tempfle->size; |
| 3043 tdata->curtrans = 0; | |
| 3044 | |
| 3045 tdata->curfle = tdata->curfle->next; | |
| 3046 tdata->next_file = 1; | |
| 3047 tdata->skip_file = 0; | |
| 3048 tdata->cancel = 0; | |
| 3049 tdata->fromreq->cancel = 0; | |
| 3050 tdata->toreq->cancel = 0; | |
| 303 | 3051 } |
| 3052 else | |
| 3053 { | |
| 764 | 3054 tempfle->transfer_action = GFTP_TRANS_ACTION_RESUME; |
| 3055 tempfle->startsize = tdata->curtrans + tdata->curresumed; | |
| 3056 /* We decrement this here because it will be incremented in | |
| 3057 the loop again */ | |
| 3058 tdata->curresumed = 0; | |
| 3059 tdata->current_file_number--; /* Decrement this because it | |
| 3060 will be incremented when we | |
| 3061 continue in the loop */ | |
| 125 | 3062 } |
| 3063 | |
| 764 | 3064 gettimeofday (&tdata->starttime, NULL); |
| 3065 | |
| 3066 if (g_thread_supported ()) | |
| 3067 g_static_mutex_unlock (&tdata->structmutex); | |
| 3068 | |
| 3069 return (GFTP_ERETRYABLE); | |
| 3070 } | |
| 3071 else if (ret1 == GFTP_EFATAL || ret2 == GFTP_EFATAL) | |
| 3072 { | |
| 3073 gftp_disconnect (tdata->fromreq); | |
| 3074 gftp_disconnect (tdata->toreq); | |
| 3075 return (GFTP_EFATAL); | |
| 125 | 3076 } |
| 3077 } | |
| 3078 | |
| 3079 return (0); | |
| 3080 } | |
| 3081 | |
| 182 | 3082 |
| 3083 int | |
| 3084 gftp_fd_open (gftp_request * request, const char *pathname, int flags, mode_t mode) | |
| 3085 { | |
| 3086 int fd; | |
| 3087 | |
| 227 | 3088 if (mode == 0) |
| 3089 fd = open (pathname, flags); | |
| 3090 else | |
| 3091 fd = open (pathname, flags, mode); | |
| 3092 | |
| 3093 if (fd < 0) | |
| 182 | 3094 { |
| 3095 if (request != NULL) | |
| 186 | 3096 request->logging_function (gftp_logging_error, request, |
| 182 | 3097 _("Error: Cannot open local file %s: %s\n"), |
| 3098 pathname, g_strerror (errno)); | |
| 3099 return (GFTP_ERETRYABLE); | |
| 3100 } | |
| 3101 | |
| 3102 if (fcntl (fd, F_SETFD, 1) == -1) | |
| 3103 { | |
| 3104 if (request != NULL) | |
| 186 | 3105 request->logging_function (gftp_logging_error, request, |
| 182 | 3106 _("Error: Cannot set close on exec flag: %s\n"), |
| 3107 g_strerror (errno)); | |
| 3108 | |
| 3109 return (-1); | |
| 3110 } | |
| 3111 | |
| 3112 return (fd); | |
| 3113 } | |
| 422 | 3114 |
| 3115 | |
| 3116 void | |
| 792 | 3117 gftp_setup_startup_directory (gftp_request * request, const char *option_name) |
| 422 | 3118 { |
| 3119 char *startup_directory, *tempstr; | |
| 3120 | |
| 792 | 3121 gftp_lookup_request_option (request, option_name, &startup_directory); |
| 422 | 3122 |
| 3123 if (*startup_directory != '\0' && | |
| 555 | 3124 (tempstr = gftp_expand_path (request, startup_directory)) != NULL) |
| 422 | 3125 { |
| 3126 gftp_set_directory (request, tempstr); | |
| 3127 g_free (tempstr); | |
| 3128 } | |
| 3129 } | |
| 3130 | |
| 499 | 3131 |
| 3132 char * | |
| 3133 gftp_convert_attributes_from_mode_t (mode_t mode) | |
| 3134 { | |
| 3135 char *str; | |
| 3136 | |
| 765 | 3137 str = g_malloc0 (11UL); |
| 499 | 3138 |
| 3139 str[0] = '?'; | |
| 3140 if (S_ISREG (mode)) | |
| 3141 str[0] = '-'; | |
| 3142 | |
| 3143 if (S_ISLNK (mode)) | |
| 3144 str[0] = 'l'; | |
| 3145 | |
| 3146 if (S_ISBLK (mode)) | |
| 3147 str[0] = 'b'; | |
| 3148 | |
| 3149 if (S_ISCHR (mode)) | |
| 3150 str[0] = 'c'; | |
| 3151 | |
| 3152 if (S_ISFIFO (mode)) | |
| 3153 str[0] = 'p'; | |
| 3154 | |
| 3155 if (S_ISSOCK (mode)) | |
| 3156 str[0] = 's'; | |
| 3157 | |
| 3158 if (S_ISDIR (mode)) | |
| 3159 str[0] = 'd'; | |
| 3160 | |
| 3161 str[1] = mode & S_IRUSR ? 'r' : '-'; | |
| 3162 str[2] = mode & S_IWUSR ? 'w' : '-'; | |
| 3163 | |
| 3164 if ((mode & S_ISUID) && (mode & S_IXUSR)) | |
| 3165 str[3] = 's'; | |
| 3166 else if (mode & S_ISUID) | |
| 3167 str[3] = 'S'; | |
| 3168 else if (mode & S_IXUSR) | |
| 3169 str[3] = 'x'; | |
| 3170 else | |
| 3171 str[3] = '-'; | |
| 3172 | |
| 3173 str[4] = mode & S_IRGRP ? 'r' : '-'; | |
| 3174 str[5] = mode & S_IWGRP ? 'w' : '-'; | |
| 3175 | |
| 3176 if ((mode & S_ISGID) && (mode & S_IXGRP)) | |
| 3177 str[6] = 's'; | |
| 3178 else if (mode & S_ISGID) | |
| 3179 str[6] = 'S'; | |
| 3180 else if (mode & S_IXGRP) | |
| 3181 str[6] = 'x'; | |
| 3182 else | |
| 3183 str[6] = '-'; | |
| 3184 | |
| 3185 str[7] = mode & S_IROTH ? 'r' : '-'; | |
| 3186 str[8] = mode & S_IWOTH ? 'w' : '-'; | |
| 3187 | |
| 3188 if ((mode & S_ISVTX) && (mode & S_IXOTH)) | |
| 3189 str[9] = 't'; | |
| 3190 else if (mode & S_ISVTX) | |
| 3191 str[9] = 'T'; | |
| 3192 else if (mode & S_IXOTH) | |
| 3193 str[9] = 'x'; | |
| 3194 else | |
| 3195 str[9] = '-'; | |
| 3196 | |
| 3197 return (str); | |
| 3198 } | |
| 3199 | |
| 3200 | |
| 3201 mode_t | |
| 3202 gftp_convert_attributes_to_mode_t (char *attribs) | |
| 3203 { | |
| 3204 mode_t mode; | |
| 3205 | |
| 3206 if (attribs[0] == 'd') | |
| 3207 mode = S_IFDIR; | |
| 3208 else if (attribs[0] == 'l') | |
| 3209 mode = S_IFLNK; | |
| 3210 else if (attribs[0] == 's') | |
| 3211 mode = S_IFSOCK; | |
| 3212 else if (attribs[0] == 'b') | |
| 3213 mode = S_IFBLK; | |
| 3214 else if (attribs[0] == 'c') | |
| 3215 mode = S_IFCHR; | |
| 3216 else | |
| 3217 mode = S_IFREG; | |
| 3218 | |
| 3219 if (attribs[1] == 'r') | |
| 3220 mode |= S_IRUSR; | |
| 3221 if (attribs[2] == 'w') | |
| 3222 mode |= S_IWUSR; | |
| 3223 if (attribs[3] == 'x' || attribs[3] == 's') | |
| 3224 mode |= S_IXUSR; | |
| 3225 if (attribs[3] == 's' || attribs[3] == 'S') | |
| 3226 mode |= S_ISUID; | |
| 3227 | |
| 3228 if (attribs[4] == 'r') | |
| 3229 mode |= S_IRGRP; | |
| 3230 if (attribs[5] == 'w') | |
| 3231 mode |= S_IWGRP; | |
| 3232 if (attribs[6] == 'x' || | |
| 3233 attribs[6] == 's') | |
| 3234 mode |= S_IXGRP; | |
| 3235 if (attribs[6] == 's' || attribs[6] == 'S') | |
| 3236 mode |= S_ISGID; | |
| 3237 | |
| 3238 if (attribs[7] == 'r') | |
| 3239 mode |= S_IROTH; | |
| 3240 if (attribs[8] == 'w') | |
| 3241 mode |= S_IWOTH; | |
| 3242 if (attribs[9] == 'x' || | |
| 3243 attribs[9] == 's') | |
| 3244 mode |= S_IXOTH; | |
| 3245 if (attribs[9] == 't' || attribs[9] == 'T') | |
| 3246 mode |= S_ISVTX; | |
| 3247 | |
| 3248 return (mode); | |
| 3249 } | |
| 3250 | |
| 542 | 3251 |
| 3252 unsigned int | |
| 3253 gftp_protocol_default_port (gftp_request * request) | |
| 3254 { | |
| 3255 struct servent serv_struct; | |
| 3256 | |
| 3257 if (r_getservbyname (gftp_protocols[request->protonum].url_prefix, "tcp", | |
| 3258 &serv_struct, NULL) == NULL) | |
| 3259 return (gftp_protocols[request->protonum].default_port); | |
| 3260 else | |
| 3261 return (ntohs (serv_struct.s_port)); | |
| 3262 } | |
| 3263 |
