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