Mercurial > gftp.yaz
annotate lib/misc.c @ 63:41b71c4e5076
2002-11-2333 Brian Masney <masneyb@gftp.org>
* lib/local.c lib/rfc959.c lib/rfc2068.c lib/ssh.c lib/sshv2.c
lib/gftp.h - added swap_socks function to gftp_request structure
* lib/misc.c lib/protocols.c - move swap_socks() from misc.c to
protocols.c (renamed to gftp_swap_socks)
* src/gtk/misc-gtk.c src/gtk/transfer.c - removed fix_display()
* src/gtk/delete_dialog.c src/gtk/transfer.c - changed all occurances of swap_socks() to gftp_swap_socks()
* src/gtk/gftp-gtk.c src/gtk/gftp-gtk.h - removed gftp_is_started
variable
| author | masneyb |
|---|---|
| date | Sat, 23 Nov 2002 13:45:05 +0000 |
| parents | 42df9e4be8e0 |
| children | 4b5fec7711e9 |
| rev | line source |
|---|---|
| 1 | 1 /*****************************************************************************/ |
| 2 /* misc.c - general purpose routines */ | |
| 3 /* Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org> */ | |
| 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" | |
| 21 #include "options.h" | |
| 33 | 22 static const char cvsid[] = "$Id$"; |
| 1 | 23 |
| 24 char * | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
25 insert_commas (off_t number, char *dest_str, size_t dest_len) |
| 1 | 26 { |
| 27 char *frompos, *topos, src[50], *dest; | |
| 28 int len, num, rem, i; | |
| 29 | |
| 30 if (dest_str != NULL) | |
| 31 *dest_str = '\0'; | |
| 56 | 32 len = (number > 0 ? log10 (number) : 0) + 2; |
| 1 | 33 |
| 34 if (len <= 0) | |
| 35 { | |
| 36 if (dest_str != NULL) | |
| 37 strncpy (dest_str, "0", dest_len); | |
| 38 else | |
| 56 | 39 dest_str = g_strdup ("0"); |
| 1 | 40 return (dest_str); |
| 41 } | |
| 42 | |
| 43 len += len / 3; | |
| 44 if (dest_str != NULL && len > dest_len) | |
| 45 { | |
| 46 | |
| 47 for (i=0; i<dest_len - 1; i++) | |
| 48 dest_str[i] = 'X'; | |
| 49 dest_str[dest_len - 1] = '\0'; | |
| 50 return (dest_str); | |
| 51 } | |
| 52 | |
| 53 if (dest_str == NULL) | |
| 54 dest = g_malloc0 (len); | |
| 55 else | |
| 56 dest = dest_str; | |
| 57 | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
58 #if defined (_LARGEFILE_SOURCE) |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
59 g_snprintf (src, sizeof (src), "%lld", number); |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
60 #else |
| 1 | 61 g_snprintf (src, sizeof (src), "%ld", number); |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
62 #endif |
| 1 | 63 |
| 64 num = strlen (src) / 3 - 1; | |
| 65 rem = strlen (src) % 3; | |
| 66 frompos = src; | |
| 67 topos = dest; | |
| 68 for (i = 0; i < rem; i++) | |
| 69 *topos++ = *frompos++; | |
| 70 | |
| 71 if (*frompos != '\0') | |
| 72 { | |
| 73 if (rem != 0) | |
| 74 *topos++ = ','; | |
| 75 while (num > 0) | |
| 76 { | |
| 77 for (i = 0; i < 3; i++) | |
| 78 *topos++ = *frompos++; | |
| 79 *topos++ = ','; | |
| 80 num--; | |
| 81 } | |
| 82 for (i = 0; i < 3; i++) | |
| 83 *topos++ = *frompos++; | |
| 84 } | |
| 85 *topos = '\0'; | |
| 86 return (dest); | |
| 87 } | |
| 88 | |
| 89 | |
| 90 long | |
| 91 file_countlf (int filefd, long endpos) | |
| 92 { | |
| 93 char tempstr[255]; | |
| 94 long num, mypos; | |
| 95 ssize_t n; | |
| 96 int i; | |
| 97 | |
| 98 mypos = num = 0; | |
| 99 lseek (filefd, 0, SEEK_SET); | |
| 100 while ((n = read (filefd, tempstr, sizeof (tempstr))) > 0) | |
| 101 { | |
| 102 for (i = 0; i < n; i++) | |
| 103 { | |
| 104 if ((tempstr[i] == '\n') && (i > 0) && (tempstr[i - 1] != '\r') | |
| 105 && (endpos == 0 || mypos + i <= endpos)) | |
| 106 ++num; | |
| 107 } | |
| 108 mypos += n; | |
| 109 } | |
| 110 lseek (filefd, 0, SEEK_SET); | |
| 111 return (num); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 char * | |
| 116 alltrim (char *str) | |
| 117 { | |
| 118 char *pos, *newpos; | |
| 119 int diff; | |
| 120 | |
| 121 pos = str + strlen (str) - 1; | |
| 122 while (pos >= str && *pos == ' ') | |
| 123 *pos-- = '\0'; | |
| 124 | |
| 125 pos = str; | |
| 126 diff = 0; | |
| 127 while (*pos++ == ' ') | |
| 128 diff++; | |
| 129 | |
| 130 if (diff == 0) | |
| 131 return (str); | |
| 132 | |
| 133 pos = str + diff; | |
| 134 newpos = str; | |
| 135 while (*pos != '\0') | |
| 136 *newpos++ = *pos++; | |
| 137 *newpos = '\0'; | |
| 138 | |
| 139 return (str); | |
| 140 } | |
| 141 | |
| 142 | |
| 143 char * | |
| 144 expand_path (const char *src) | |
| 145 { | |
| 146 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
| 147 struct passwd *pw; | |
| 148 | |
| 149 pw = NULL; | |
| 150 str = g_malloc (strlen (src) + 1); | |
| 151 strcpy (str, src); | |
| 152 | |
| 153 if (*str == '~') | |
| 154 { | |
| 155 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
| 156 pw = getpwuid (geteuid ()); | |
| 157 else | |
| 158 { | |
| 159 if ((pos = strchr (str, '/')) != NULL) | |
| 160 *pos = '\0'; | |
| 161 | |
| 162 pw = getpwnam (str + 1); | |
| 163 | |
| 164 if (pos != NULL) | |
| 165 *pos = '/'; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 endpos = str; | |
| 170 newstr = NULL; | |
| 171 while ((pos = strchr (endpos, '/')) != NULL) | |
| 172 { | |
| 173 pos++; | |
| 174 while (*pos == '/') | |
| 175 pos++; | |
| 176 if ((endpos = strchr (pos, '/')) == NULL) | |
| 177 endpos = pos + strlen (pos); | |
| 178 tempchar = *endpos; | |
| 179 *endpos = '\0'; | |
| 180 if (strcmp (pos, "..") == 0) | |
| 181 { | |
| 182 *(pos - 1) = '\0'; | |
| 183 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
| 184 *prevpos = '\0'; | |
| 185 } | |
| 186 else if (strcmp (pos, ".") != 0) | |
| 187 { | |
| 188 if (newstr == NULL) | |
| 56 | 189 newstr = g_strdup (pos - 1); |
| 1 | 190 else |
| 191 { | |
| 192 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
| 193 g_free (newstr); | |
| 194 newstr = tempstr; | |
| 195 } | |
| 196 } | |
| 197 *endpos = tempchar; | |
| 198 if (*endpos == '\0') | |
| 199 break; | |
| 200 endpos = pos + 1; | |
| 201 } | |
| 202 | |
| 203 if (newstr == NULL || *newstr == '\0') | |
| 204 { | |
| 205 if (newstr != NULL) | |
| 206 g_free (newstr); | |
| 207 newstr = g_malloc0 (2); | |
| 208 *newstr = '/'; | |
| 209 } | |
| 210 | |
| 211 g_free (str); | |
| 212 | |
| 213 if (pw != NULL) | |
| 214 { | |
| 215 if ((pos = strchr (newstr, '/')) == NULL) | |
| 216 { | |
| 217 str = g_malloc (strlen (pw->pw_dir) + 1); | |
| 218 strcpy (str, pw->pw_dir); | |
| 219 } | |
| 220 else | |
| 221 str = g_strconcat (pw->pw_dir, pos, NULL); | |
| 222 | |
| 223 g_free (newstr); | |
| 224 newstr = str; | |
| 225 } | |
| 226 | |
| 227 return (newstr); | |
| 228 } | |
| 229 | |
| 230 | |
| 231 void | |
| 232 remove_double_slashes (char *string) | |
| 233 { | |
| 234 char *newpos, *oldpos; | |
| 235 | |
| 236 oldpos = newpos = string; | |
| 237 while (*oldpos != '\0') | |
| 238 { | |
| 239 *newpos++ = *oldpos++; | |
| 240 if (*oldpos == '\0') | |
| 241 break; | |
| 242 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
| 243 oldpos++; | |
| 244 } | |
| 245 *newpos = '\0'; | |
| 246 if (string[strlen (string) - 1] == '/') | |
| 247 string[strlen (string) - 1] = '\0'; | |
| 248 } | |
| 249 | |
| 250 | |
| 251 void | |
| 252 make_nonnull (char **str) | |
| 253 { | |
| 254 if (*str == NULL) | |
| 255 *str = g_malloc0 (1); | |
| 256 } | |
| 257 | |
| 258 | |
| 259 int | |
| 260 copyfile (char *source, char *dest) | |
| 261 { | |
| 58 | 262 int srcfd, destfd; |
| 1 | 263 char buf[8192]; |
| 58 | 264 ssize_t n; |
| 1 | 265 |
| 58 | 266 if ((srcfd = open (source, O_RDONLY)) == -1) |
| 267 { | |
| 268 printf (_("Error: Cannot open local file %s: %s\n"), | |
| 269 source, g_strerror (errno)); | |
| 270 exit (1); | |
| 271 } | |
| 1 | 272 |
| 58 | 273 if ((destfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) |
| 1 | 274 { |
| 58 | 275 printf (_("Error: Cannot open local file %s: %s\n"), |
| 276 dest, g_strerror (errno)); | |
| 277 close (srcfd); | |
| 278 exit (1); | |
| 1 | 279 } |
| 280 | |
| 58 | 281 while ((n = read (srcfd, buf, sizeof (buf))) > 0) |
| 282 { | |
| 283 if (write (destfd, buf, n) == -1) | |
| 284 { | |
| 285 printf (_("Error: Could not write to socket: %s\n"), | |
| 286 g_strerror (errno)); | |
| 287 exit (1); | |
| 288 } | |
| 289 } | |
| 1 | 290 |
| 58 | 291 if (n == -1) |
| 292 { | |
| 293 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno)); | |
| 294 exit (1); | |
| 295 } | |
| 296 | |
| 297 close (srcfd); | |
| 298 close (destfd); | |
| 1 | 299 |
| 300 return (1); | |
| 301 } | |
| 302 | |
| 303 | |
| 304 int | |
| 305 gftp_match_filespec (char *filename, char *filespec) | |
| 306 { | |
| 307 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
| 308 size_t len, curlen; | |
| 309 | |
| 310 if (filename == NULL || *filename == '\0' || | |
| 311 filespec == NULL || *filespec == '\0') | |
| 312 return(1); | |
| 313 | |
| 314 filepos = filename; | |
| 315 wcpos = filespec; | |
| 316 while(1) | |
| 317 { | |
| 318 if (*wcpos == '\0') | |
| 319 return (1); | |
| 320 else if (*filepos == '\0') | |
| 321 return(0); | |
| 322 else if(*wcpos == '?') | |
| 323 { | |
| 324 wcpos++; | |
| 325 filepos++; | |
| 326 } | |
| 327 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
| 328 return(1); | |
| 329 else if(*wcpos == '*') | |
| 330 { | |
| 331 len = sizeof (search_str); | |
| 332 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
| 333 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
| 334 curlen++, *newpos++ = *pos++); | |
| 335 *newpos = '\0'; | |
| 336 | |
| 337 if ((filepos = strstr (filepos, search_str)) == NULL) | |
| 338 return(0); | |
| 339 wcpos += curlen + 1; | |
| 340 filepos += curlen; | |
| 341 } | |
| 342 else if(*wcpos++ != *filepos++) | |
| 343 return(0); | |
| 344 } | |
| 345 return (1); | |
| 346 } | |
| 347 | |
| 348 | |
| 349 int | |
| 350 gftp_parse_command_line (int *argc, char ***argv) | |
| 351 { | |
| 352 if (*argc > 1) | |
| 353 { | |
| 354 if (strcmp (argv[0][1], "--help") == 0 || strcmp (argv[0][1], "-h") == 0) | |
| 355 gftp_usage (); | |
| 356 else if (strcmp (argv[0][1], "--version") == 0 || strcmp (argv[0][1], "-v") == 0) | |
| 357 { | |
| 358 printf ("%s\n", version); | |
| 359 exit (0); | |
| 360 } | |
| 361 } | |
| 362 return (0); | |
| 363 } | |
| 364 | |
| 365 | |
| 366 void | |
| 367 gftp_usage (void) | |
| 368 { | |
| 369 printf (_("usage: gftp [[ftp://][user:[pass]@]ftp-site[:port][/directory]]\n")); | |
| 370 exit (0); | |
| 371 } | |
| 372 | |
| 373 | |
| 374 char * | |
| 375 get_xpm_path (char *filename, int quit_on_err) | |
| 376 { | |
| 377 char *tempstr, *exfile; | |
| 378 | |
| 379 tempstr = g_strconcat (BASE_CONF_DIR, "/", filename, NULL); | |
| 380 exfile = expand_path (tempstr); | |
| 381 g_free (tempstr); | |
| 382 if (access (exfile, F_OK) != 0) | |
| 383 { | |
| 384 g_free (exfile); | |
| 385 tempstr = g_strconcat (SHARE_DIR, "/", filename, NULL); | |
| 386 exfile = expand_path (tempstr); | |
| 387 g_free (tempstr); | |
| 388 if (access (exfile, F_OK) != 0) | |
| 389 { | |
| 390 g_free (exfile); | |
| 391 exfile = g_strconcat ("/usr/share/icons/", filename, NULL); | |
| 392 if (access (exfile, F_OK) != 0) | |
| 393 { | |
| 394 g_free (exfile); | |
| 395 if (!quit_on_err) | |
| 396 return (NULL); | |
| 397 printf (_("gFTP Error: Cannot find file %s in %s or %s\n"), | |
| 398 filename, SHARE_DIR, BASE_CONF_DIR); | |
| 399 exit (-1); | |
| 400 } | |
| 401 } | |
| 402 } | |
| 403 return (exfile); | |
| 404 } | |
| 405 | |
| 406 | |
| 407 gint | |
| 408 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
| 409 { | |
| 410 return (strcmp ((char *) path1, (char *) path2) == 0); | |
| 411 } | |
| 412 | |
| 413 | |
| 414 guint | |
| 415 string_hash_function (gconstpointer key) | |
| 416 { | |
| 59 | 417 guint ret; |
| 418 int i; | |
| 419 | |
| 420 ret = 0; | |
| 421 for (i=0; ((char *) key)[0] != '\0' && i < 3; i++) | |
| 422 ret += ((char *) key)[i]; | |
| 423 | |
| 424 return (ret); | |
| 1 | 425 } |
| 426 | |
| 427 | |
| 428 void | |
| 429 free_file_list (GList * filelist) | |
| 430 { | |
| 431 gftp_file * tempfle; | |
| 432 GList * templist; | |
| 433 | |
| 434 templist = filelist; | |
| 435 while (templist != NULL) | |
| 436 { | |
| 437 tempfle = templist->data; | |
| 438 free_fdata (tempfle); | |
| 439 templist = templist->next; | |
| 440 } | |
| 441 g_list_free (filelist); | |
| 442 } | |
| 443 | |
| 444 | |
| 445 void | |
| 446 free_fdata (gftp_file * fle) | |
| 447 { | |
| 448 if (fle->file) | |
| 449 g_free (fle->file); | |
| 450 if (fle->user) | |
| 451 g_free (fle->user); | |
| 452 if (fle->group) | |
| 453 g_free (fle->group); | |
| 454 if (fle->attribs) | |
| 455 g_free (fle->attribs); | |
| 456 if (fle->destfile) | |
| 457 g_free (fle->destfile); | |
| 58 | 458 if (fle->fd > 0) |
| 459 close (fle->fd); | |
| 1 | 460 g_free (fle); |
| 461 } | |
| 462 | |
| 463 | |
| 464 gftp_file * | |
| 465 copy_fdata (gftp_file * fle) | |
| 466 { | |
| 467 gftp_file * newfle; | |
| 468 | |
| 469 newfle = g_malloc0 (sizeof (*newfle)); | |
| 470 memcpy (newfle, fle, sizeof (*newfle)); | |
| 471 | |
| 472 if (fle->file) | |
| 473 { | |
| 474 newfle->file = g_malloc (strlen (fle->file) + 1); | |
| 475 strcpy (newfle->file, fle->file); | |
| 476 } | |
| 477 | |
| 478 if (fle->user) | |
| 479 { | |
| 480 newfle->user = g_malloc (strlen (fle->user) + 1); | |
| 481 strcpy (newfle->user, fle->user); | |
| 482 } | |
| 483 | |
| 484 if (fle->group) | |
| 485 { | |
| 486 newfle->group = g_malloc (strlen (fle->group) + 1); | |
| 487 strcpy (newfle->group, fle->group); | |
| 488 } | |
| 489 | |
| 490 if (fle->attribs) | |
| 491 { | |
| 492 newfle->attribs = g_malloc (strlen (fle->attribs) + 1); | |
| 493 strcpy (newfle->attribs, fle->attribs); | |
| 494 } | |
| 495 | |
| 496 if (fle->destfile) | |
| 497 { | |
| 498 newfle->destfile = g_malloc (strlen (fle->destfile) + 1); | |
| 499 strcpy (newfle->destfile, fle->destfile); | |
| 500 } | |
| 501 return (newfle); | |
| 502 } | |
| 503 | |
| 504 | |
| 505 int | |
| 506 compare_request (gftp_request * request1, gftp_request * request2, | |
| 507 int compare_dirs) | |
| 508 { | |
| 509 char *strarr[3][2]; | |
| 510 int i, ret; | |
| 511 | |
| 512 ret = 1; | |
| 513 if (strcmp (request1->protocol_name, request2->protocol_name) == 0 && | |
| 514 request1->port == request2->port) | |
| 515 { | |
| 516 strarr[0][0] = request1->hostname; | |
| 517 strarr[0][1] = request2->hostname; | |
| 518 strarr[1][0] = request1->username; | |
| 519 strarr[1][1] = request2->username; | |
| 520 if (compare_dirs) | |
| 521 { | |
| 522 strarr[2][0] = request1->directory; | |
| 523 strarr[2][1] = request2->directory; | |
| 524 } | |
| 525 else | |
| 526 strarr[2][0] = strarr[2][1] = ""; | |
| 527 | |
| 528 for (i = 0; i < 3; i++) | |
| 529 { | |
| 530 if ((strarr[i][0] && !strarr[i][1]) || | |
| 531 (!strarr[i][0] && strarr[i][1])) | |
| 532 { | |
| 533 ret = 0; | |
| 534 break; | |
| 535 } | |
| 536 | |
| 537 if (strarr[i][0] && strarr[i][1] && | |
| 538 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
| 539 { | |
| 540 ret = 0; | |
| 541 break; | |
| 542 } | |
| 543 } | |
| 544 } | |
| 545 else | |
| 546 ret = 0; | |
| 547 return (ret); | |
| 548 } | |
| 549 | |
| 550 | |
| 551 void | |
| 552 free_tdata (gftp_transfer * tdata) | |
| 553 { | |
| 554 if (tdata->statmutex) | |
| 555 g_free (tdata->statmutex); | |
| 556 if (tdata->structmutex) | |
| 557 g_free (tdata->structmutex); | |
| 558 if (tdata->fromreq != NULL) | |
| 559 gftp_request_destroy (tdata->fromreq); | |
| 560 if (tdata->toreq != NULL) | |
| 561 gftp_request_destroy (tdata->toreq); | |
| 562 free_file_list (tdata->files); | |
| 563 g_free (tdata); | |
| 564 } | |
| 565 | |
| 566 | |
| 567 gftp_request * | |
| 568 copy_request (gftp_request * req) | |
| 569 { | |
| 570 gftp_request * newreq; | |
| 571 | |
| 572 newreq = g_malloc0 (sizeof (*newreq)); | |
| 573 memcpy (newreq, req, sizeof (*newreq)); | |
| 574 | |
| 575 if (req->hostname) | |
| 56 | 576 newreq->hostname = g_strdup (req->hostname); |
| 1 | 577 if (req->username) |
| 56 | 578 newreq->username = g_strdup (req->username); |
| 1 | 579 if (req->password) |
| 56 | 580 newreq->password = g_strdup (req->password); |
| 1 | 581 if (req->account) |
| 56 | 582 newreq->account = g_strdup (req->account); |
| 1 | 583 if (req->directory) |
| 56 | 584 newreq->directory = g_strdup (req->directory); |
| 1 | 585 |
| 586 newreq->url_prefix = NULL; | |
| 587 newreq->protocol_name = NULL; | |
| 588 newreq->sftpserv_path = NULL; | |
| 589 newreq->proxy_config = NULL; | |
| 590 newreq->proxy_hostname = NULL; | |
| 591 newreq->proxy_username = NULL; | |
| 592 newreq->proxy_password = NULL; | |
| 593 newreq->proxy_account = NULL; | |
| 594 newreq->last_ftp_response = NULL; | |
| 595 newreq->last_dir_entry = NULL; | |
| 58 | 596 newreq->sockfd = -1; |
| 597 newreq->datafd = -1; | |
| 598 newreq->cachefd = -1; | |
| 1 | 599 newreq->hostp = NULL; |
| 600 | |
| 601 if (req->proxy_config != NULL) | |
| 56 | 602 newreq->proxy_config = g_strdup (req->proxy_config); |
| 1 | 603 |
| 604 req->init (newreq); | |
| 605 | |
| 606 return (newreq); | |
| 607 } | |
| 608 | |
| 609 | |
| 610 int | |
| 611 ptym_open (char *pts_name) | |
| 612 { | |
| 613 int fd; | |
| 614 | |
| 615 #ifdef __sgi | |
| 616 char *tempstr; | |
| 617 | |
| 618 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
| 619 return (-1); | |
| 620 | |
| 621 strcpy (pts_name, tempstr); | |
| 622 return (fd); | |
| 623 | |
| 624 #else /* !__sgi */ | |
| 625 | |
| 626 #ifdef SYSV | |
| 627 | |
| 628 char *tempstr; | |
| 629 | |
| 630 strcpy (pts_name, "/dev/ptmx"); | |
| 631 if ((fd = open (pts_name, O_RDWR)) < 0) | |
| 632 return (-1); | |
| 633 | |
| 634 if (grantpt (fd) < 0) | |
| 635 { | |
| 636 close (fd); | |
| 637 return (-1); | |
| 638 } | |
| 639 | |
| 640 if (unlockpt (fd) < 0) | |
| 641 { | |
| 642 close (fd); | |
| 643 return (-1); | |
| 644 } | |
| 645 | |
| 646 if ((tempstr = ptsname (fd)) == NULL) | |
| 647 { | |
| 648 close (fd); | |
| 649 return (-1); | |
| 650 } | |
| 651 | |
| 652 strcpy (pts_name, tempstr); | |
| 653 return (fd); | |
| 654 | |
| 655 #else /* !SYSV */ | |
| 656 | |
| 657 char *pos1, *pos2; | |
| 658 | |
| 659 strcpy (pts_name, "/dev/ptyXY"); | |
| 660 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
| 661 { | |
| 662 pts_name[8] = *pos1; | |
| 663 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
| 664 { | |
| 665 pts_name[9] = *pos2; | |
| 666 if ((fd = open (pts_name, O_RDWR)) < 0) | |
| 667 continue; | |
| 668 pts_name[5] = 't'; | |
| 669 return (fd); | |
| 670 } | |
| 671 } | |
| 672 return (-1); | |
| 673 | |
| 674 #endif | |
| 675 | |
| 676 #endif | |
| 677 | |
| 678 } | |
| 679 | |
| 680 | |
| 681 int | |
| 682 ptys_open (int fdm, char *pts_name) | |
| 683 { | |
| 684 int fds; | |
| 685 | |
| 686 #if !defined (SYSV) && !defined (__sgi) | |
| 687 | |
| 688 chmod (pts_name, S_IRUSR | S_IWUSR); | |
| 689 chown (pts_name, getuid (), -1); | |
| 690 | |
| 691 #endif | |
| 692 | |
| 693 if ((fds = open (pts_name, O_RDWR)) < 0) | |
| 694 { | |
| 695 close (fdm); | |
| 696 return (-1); | |
| 697 } | |
| 698 | |
| 699 #ifdef SYSV | |
| 700 | |
| 701 if (ioctl (fds, I_PUSH, "ptem") < 0) | |
| 702 { | |
| 703 close (fdm); | |
| 704 close (fds); | |
| 705 return (-1); | |
| 706 } | |
| 707 | |
| 708 if (ioctl (fds, I_PUSH, "ldterm") < 0) | |
| 709 { | |
| 710 close (fdm); | |
| 711 close (fds); | |
| 712 return (-1); | |
| 713 } | |
| 714 | |
| 715 if (ioctl (fds, I_PUSH, "ttcompat") < 0) | |
| 716 { | |
| 717 close (fdm); | |
| 718 close (fds); | |
| 719 return (-1); | |
| 720 } | |
| 721 | |
| 722 #endif | |
| 723 | |
| 724 #if !defined(SYSV) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) | |
| 725 | |
| 726 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
| 727 { | |
| 728 close (fdm); | |
| 729 return (-1); | |
| 730 } | |
| 731 | |
| 732 #endif | |
| 733 | |
| 734 return (fds); | |
| 735 } | |
| 736 | |
| 737 | |
| 738 int | |
| 739 tty_raw (int fd) | |
| 740 { | |
| 741 struct termios buf; | |
| 742 | |
| 743 if (tcgetattr (fd, &buf) < 0) | |
| 744 return (-1); | |
| 745 | |
| 746 buf.c_iflag |= IGNPAR; | |
| 747 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
| 748 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
| 749 #ifdef IEXTEN | |
| 750 buf.c_lflag &= ~(IEXTEN); | |
| 751 #endif | |
| 752 | |
| 753 buf.c_oflag &= ~(OPOST); | |
| 754 buf.c_cc[VMIN] = 1; | |
| 755 buf.c_cc[VTIME] = 0; | |
| 756 | |
| 757 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
| 758 return (-1); | |
| 759 return (0); | |
| 760 } | |
| 761 | |
| 762 | |
| 763 /* We have the caller send us a pointer to a string so we can write the port | |
| 764 into it. It makes it easier so we don't have to worry about freeing it | |
| 765 later on, the caller can just send us an auto variable, The string | |
| 766 should be at least 6 chars. I know this is messy... */ | |
| 767 | |
| 768 char ** | |
| 769 make_ssh_exec_args (gftp_request * request, char *execname, | |
| 770 int use_sftp_subsys, char *portstring) | |
| 771 { | |
| 772 char **args, *oldstr, *tempstr; | |
| 56 | 773 struct servent serv_struct; |
| 1 | 774 int i, j; |
| 775 | |
| 56 | 776 args = g_malloc (sizeof (char *) * (num_ssh_extra_params + 15)); |
| 1 | 777 |
| 778 args[0] = ssh_prog_name != NULL && *ssh_prog_name != '\0' ? | |
| 779 ssh_prog_name : "ssh"; | |
| 780 i = 1; | |
| 56 | 781 tempstr = g_strdup (args[0]); |
| 1 | 782 |
| 783 if (ssh_extra_params_list != NULL) | |
| 784 { | |
| 785 for (j=0; ssh_extra_params_list[j] != NULL; j++) | |
| 786 { | |
| 787 oldstr = tempstr; | |
| 788 args[i++] = ssh_extra_params_list[j]; | |
| 789 tempstr = g_strconcat (oldstr, " ", ssh_extra_params_list[j], NULL); | |
| 790 g_free (oldstr); | |
| 791 } | |
| 792 } | |
| 793 | |
| 794 oldstr = tempstr; | |
| 795 tempstr = g_strconcat (oldstr, " -e none", NULL); | |
| 796 g_free (oldstr); | |
| 797 args[i++] = "-e"; | |
| 798 args[i++] = "none"; | |
| 799 | |
| 800 if (request->username && *request->username != '\0') | |
| 801 { | |
| 802 oldstr = tempstr; | |
| 803 tempstr = g_strconcat (oldstr, " -l ", request->username, NULL); | |
| 804 g_free (oldstr); | |
| 805 args[i++] = "-l"; | |
| 806 args[i++] = request->username; | |
| 807 } | |
| 808 | |
| 809 if (request->port != 0) | |
| 810 { | |
| 811 g_snprintf (portstring, 6, "%d", request->port); | |
| 812 oldstr = tempstr; | |
| 813 tempstr = g_strconcat (oldstr, " -p ", portstring, NULL); | |
| 814 g_free (oldstr); | |
| 815 args[i++] = "-p"; | |
| 816 args[i++] = portstring; | |
| 817 } | |
| 56 | 818 else |
| 819 { | |
| 820 if (!r_getservbyname ("ssh", "tcp", &serv_struct, NULL)) | |
| 821 request->port = 22; | |
| 822 else | |
| 823 request->port = ntohs (serv_struct.s_port); | |
| 824 } | |
| 1 | 825 |
| 826 if (use_sftp_subsys) | |
| 827 { | |
| 828 oldstr = tempstr; | |
| 829 tempstr = g_strconcat (oldstr, " ", request->hostname, " -s sftp", NULL); | |
| 830 g_free (oldstr); | |
| 831 args[i++] = request->hostname; | |
| 832 args[i++] = "-s"; | |
| 833 args[i++] = "sftp"; | |
| 834 args[i] = NULL; | |
| 835 } | |
| 836 else | |
| 837 { | |
| 838 oldstr = tempstr; | |
| 839 tempstr = g_strconcat (oldstr, " ", request->hostname, " \"", execname, | |
| 840 "\"", NULL); | |
| 841 g_free (oldstr); | |
| 842 args[i++] = request->hostname; | |
| 843 args[i++] = execname; | |
| 844 args[i] = NULL; | |
| 845 } | |
| 846 | |
| 847 request->logging_function (gftp_logging_misc, request->user_data, | |
| 848 _("Running program %s\n"), tempstr); | |
| 849 g_free (tempstr); | |
| 850 return (args); | |
| 851 } | |
| 852 | |
| 61 | 853 #define SSH_LOGIN_BUFSIZE 200 |
| 854 #define SSH_ERROR_BADPASS -1 | |
| 855 #define SSH_ERROR_QUESTION -2 | |
| 1 | 856 |
| 857 char * | |
| 858 ssh_start_login_sequence (gftp_request * request, int fd) | |
| 859 { | |
| 61 | 860 char *tempstr, *pwstr, *key_pos, *tmppos; |
| 1 | 861 size_t rem, len, diff, lastdiff; |
| 58 | 862 int wrotepw, ok; |
| 1 | 863 ssize_t rd; |
| 864 | |
| 61 | 865 rem = len = SSH_LOGIN_BUFSIZE; |
| 866 key_pos = tempstr = g_malloc0 (len + 1); | |
| 1 | 867 diff = lastdiff = 0; |
| 868 wrotepw = 0; | |
| 869 ok = 1; | |
| 870 | |
| 61 | 871 if (gftp_set_sockblocking (request, fd, 1) == -1) |
| 58 | 872 return (NULL); |
| 1 | 873 |
| 58 | 874 pwstr = g_strconcat (request->password, "\n", NULL); |
| 1 | 875 |
| 41 | 876 errno = 0; |
| 1 | 877 while (1) |
| 878 { | |
| 61 | 879 if ((rd = gftp_read (request, tempstr + diff, rem - 1, fd)) <= 0) |
| 1 | 880 { |
| 881 ok = 0; | |
| 882 break; | |
| 883 } | |
| 61 | 884 |
| 1 | 885 tempstr[diff + rd] = '\0'; |
| 886 rem -= rd; | |
| 887 diff += rd; | |
| 888 if (rem <= 1) | |
| 889 { | |
| 61 | 890 tempstr = g_realloc (tempstr, len + SSH_LOGIN_BUFSIZE); |
| 891 | |
| 1 | 892 request->logging_function (gftp_logging_recv, request->user_data, |
| 893 "%s", tempstr + lastdiff); | |
| 894 lastdiff = diff; | |
| 61 | 895 len += SSH_LOGIN_BUFSIZE; |
| 896 rem = SSH_LOGIN_BUFSIZE; | |
| 1 | 897 } |
| 898 | |
| 61 | 899 if (diff > 11 && strcmp (tempstr + diff - 10, "password: ") == 0) |
| 1 | 900 { |
| 61 | 901 if (wrotepw) |
| 902 { | |
| 903 ok = SSH_ERROR_BADPASS; | |
| 904 break; | |
| 905 } | |
| 906 | |
| 1 | 907 wrotepw = 1; |
| 58 | 908 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
| 909 { | |
| 910 ok = 0; | |
| 911 break; | |
| 912 } | |
| 1 | 913 } |
| 61 | 914 else if ((tmppos = strstr (key_pos, "Enter passphrase for RSA key")) != NULL || |
| 915 ((tmppos = strstr (key_pos, "Enter passphrase for key '")) != NULL)) | |
| 916 { | |
| 917 key_pos = tmppos + 1; | |
| 918 if (wrotepw) | |
| 919 { | |
| 920 ok = SSH_ERROR_BADPASS; | |
| 921 break; | |
| 922 } | |
| 1 | 923 |
| 924 wrotepw = 1; | |
| 58 | 925 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
| 926 { | |
| 927 ok = 0; | |
| 928 break; | |
| 929 } | |
| 1 | 930 } |
| 61 | 931 else if (diff >= 10 && strcmp (tempstr + diff - 10, "(yes/no)? ") == 0) |
| 932 { | |
| 933 ok = SSH_ERROR_QUESTION; | |
| 934 break; | |
| 935 } | |
| 936 else if (diff >= 5 && strcmp (tempstr + diff - 5, "xsftp") == 0) | |
| 1 | 937 break; |
| 938 } | |
| 939 | |
| 58 | 940 g_free (pwstr); |
| 61 | 941 |
| 942 if (*(tempstr + lastdiff) != '\0') | |
| 943 request->logging_function (gftp_logging_recv, request->user_data, | |
| 944 "%s\n", tempstr + lastdiff); | |
| 1 | 945 |
| 61 | 946 if (ok <= 0) |
| 1 | 947 { |
| 61 | 948 if (ok == SSH_ERROR_BADPASS) |
| 949 request->logging_function (gftp_logging_error, request->user_data, | |
| 950 _("Error: An incorrect password was entered\n")); | |
| 951 else if (ok == SSH_ERROR_QUESTION) | |
| 952 request->logging_function (gftp_logging_error, request->user_data, | |
| 953 _("Please connect to this host with the command line SSH utility and answer this question appropriately.\n")); | |
| 954 | |
| 1 | 955 g_free (tempstr); |
| 956 return (NULL); | |
| 957 } | |
| 958 | |
| 959 return (tempstr); | |
| 960 } | |
| 961 | |
| 962 | |
| 963 #ifdef G_HAVE_GINT64 | |
| 964 | |
| 965 gint64 | |
| 966 hton64 (gint64 val) | |
| 967 { | |
| 968 gint64 num; | |
| 969 char *pos; | |
| 970 | |
| 971 num = 0; | |
| 972 pos = (char *) # | |
| 973 pos[0] = (val >> 56) & 0xff; | |
| 974 pos[1] = (val >> 48) & 0xff; | |
| 975 pos[2] = (val >> 40) & 0xff; | |
| 976 pos[3] = (val >> 32) & 0xff; | |
| 977 pos[4] = (val >> 24) & 0xff; | |
| 978 pos[5] = (val >> 16) & 0xff; | |
| 979 pos[6] = (val >> 8) & 0xff; | |
| 980 pos[7] = val & 0xff; | |
| 981 return (num); | |
| 982 } | |
| 983 | |
| 984 #endif | |
| 985 | |
| 16 | 986 |
| 987 static gint | |
| 988 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
| 989 { | |
| 990 const gftp_file * f1, * f2; | |
| 991 | |
| 992 f1 = a; | |
| 993 f2 = b; | |
| 994 return (strcmp (f1->file, f2->file)); | |
| 995 } | |
| 996 | |
| 997 | |
| 998 static gint | |
| 999 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1000 { | |
| 1001 const gftp_file * f1, * f2; | |
| 1002 gint ret; | |
| 1003 | |
| 1004 f1 = a; | |
| 1005 f2 = b; | |
| 1006 ret = strcmp (f1->file, f2->file); | |
| 1007 if (ret < 0) | |
| 1008 ret = 1; | |
| 1009 else if (ret > 0) | |
| 1010 ret = -1; | |
| 1011 return (ret); | |
| 1012 } | |
| 1013 | |
| 1014 | |
| 1015 static gint | |
| 1016 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1017 { | |
| 1018 const gftp_file * f1, * f2; | |
| 1019 | |
| 1020 f1 = a; | |
| 1021 f2 = b; | |
| 1022 return (strcmp (f1->user, f2->user)); | |
| 1023 } | |
| 1024 | |
| 1025 | |
| 1026 static gint | |
| 1027 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1028 { | |
| 1029 const gftp_file * f1, * f2; | |
| 1030 gint ret; | |
| 1031 | |
| 1032 f1 = a; | |
| 1033 f2 = b; | |
| 1034 ret = strcmp (f1->user, f2->user); | |
| 1035 if (ret < 0) | |
| 1036 ret = 1; | |
| 1037 else if (ret > 0) | |
| 1038 ret = -1; | |
| 1039 return (ret); | |
| 1040 } | |
| 1041 | |
| 1042 | |
| 1043 static gint | |
| 1044 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1045 { | |
| 1046 const gftp_file * f1, * f2; | |
| 1047 | |
| 1048 f1 = a; | |
| 1049 f2 = b; | |
| 1050 return (strcmp (f1->group, f2->group)); | |
| 1051 } | |
| 1052 | |
| 1053 | |
| 1054 static gint | |
| 1055 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1056 { | |
| 1057 const gftp_file * f1, * f2; | |
| 1058 gint ret; | |
| 1059 | |
| 1060 f1 = a; | |
| 1061 f2 = b; | |
| 1062 ret = strcmp (f1->group, f2->group); | |
| 1063 if (ret < 0) | |
| 1064 ret = 1; | |
| 1065 else if (ret > 0) | |
| 1066 ret = -1; | |
| 1067 return (ret); | |
| 1068 } | |
| 1069 | |
| 1070 | |
| 1071 static gint | |
| 1072 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1073 { | |
| 1074 const gftp_file * f1, * f2; | |
| 1075 | |
| 1076 f1 = a; | |
| 1077 f2 = b; | |
| 1078 return (strcmp (f1->attribs, f2->attribs)); | |
| 1079 } | |
| 1080 | |
| 1081 | |
| 1082 static gint | |
| 1083 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1084 { | |
| 1085 const gftp_file * f1, * f2; | |
| 1086 gint ret; | |
| 1087 | |
| 1088 f1 = a; | |
| 1089 f2 = b; | |
| 1090 ret = strcmp (f1->attribs, f2->attribs); | |
| 1091 if (ret < 0) | |
| 1092 ret = 1; | |
| 1093 else if (ret > 0) | |
| 1094 ret = -1; | |
| 1095 return (ret); | |
| 1096 } | |
| 1097 | |
| 1098 | |
| 1099 static gint | |
| 1100 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1101 { | |
| 1102 const gftp_file * f1, * f2; | |
| 1103 | |
| 1104 f1 = a; | |
| 1105 f2 = b; | |
| 1106 if (f1->size < f2->size) | |
| 1107 return (-1); | |
| 1108 else if (f1->size == f2->size) | |
| 1109 return (0); | |
| 1110 else | |
| 1111 return (1); | |
| 1112 } | |
| 1113 | |
| 1114 | |
| 1115 static gint | |
| 1116 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1117 { | |
| 1118 const gftp_file * f1, * f2; | |
| 1119 | |
| 1120 f1 = a; | |
| 1121 f2 = b; | |
| 1122 if (f1->size < f2->size) | |
| 1123 return (1); | |
| 1124 else if (f1->size == f2->size) | |
| 1125 return (0); | |
| 1126 else | |
| 1127 return (-1); | |
| 1128 } | |
| 1129 | |
| 1130 | |
| 1131 static gint | |
| 1132 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1133 { | |
| 1134 const gftp_file * f1, * f2; | |
| 1135 | |
| 1136 f1 = a; | |
| 1137 f2 = b; | |
| 1138 if (f1->datetime < f2->datetime) | |
| 1139 return (-1); | |
| 1140 else if (f1->datetime == f2->datetime) | |
| 1141 return (0); | |
| 1142 else | |
| 1143 return (1); | |
| 1144 } | |
| 1145 | |
| 1146 | |
| 1147 static gint | |
| 1148 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1149 { | |
| 1150 const gftp_file * f1, * f2; | |
| 1151 | |
| 1152 f1 = a; | |
| 1153 f2 = b; | |
| 1154 if (f1->datetime < f2->datetime) | |
| 1155 return (1); | |
| 1156 else if (f1->datetime == f2->datetime) | |
| 1157 return (0); | |
| 1158 else | |
| 1159 return (-1); | |
| 1160 } | |
| 1161 | |
| 1162 | |
| 1163 GList * | |
| 1164 gftp_sort_filelist (GList * filelist, int column, int asds) | |
| 1165 { | |
| 1166 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
| 1167 GCompareFunc sortfunc; | |
| 1168 gftp_file * tempfle; | |
| 1169 | |
| 1170 files = dirs = dotdot = NULL; | |
| 1171 | |
| 1172 if (column == GFTP_SORT_COL_FILE) | |
| 1173 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
| 1174 else if (column == GFTP_SORT_COL_SIZE) | |
| 1175 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
| 1176 else if (column == GFTP_SORT_COL_USER) | |
| 1177 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
| 1178 else if (column == GFTP_SORT_COL_GROUP) | |
| 1179 sortfunc = asds ? | |
| 1180 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
| 1181 else if (column == GFTP_SORT_COL_DATETIME) | |
| 1182 sortfunc = asds ? | |
| 1183 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; else /* GFTP_SORT_COL_ATTRIBS */ | |
| 1184 sortfunc = asds ? | |
| 1185 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
| 1186 | |
| 1187 for (tempitem = filelist; tempitem != NULL; ) | |
| 1188 { | |
| 1189 tempfle = tempitem->data; | |
| 1190 insitem = tempitem; | |
| 1191 tempitem = tempitem->next; | |
| 1192 insitem->next = NULL; | |
| 1193 | |
| 1194 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
| 1195 dotdot = insitem; | |
| 1196 else if (sort_dirs_first && tempfle->isdir) | |
| 1197 { | |
| 1198 insitem->next = dirs; | |
| 1199 dirs = insitem; | |
| 1200 } | |
| 1201 else | |
| 1202 { | |
| 1203 insitem->next = files; | |
| 1204 files = insitem; | |
| 1205 } | |
| 1206 } | |
| 1207 | |
| 1208 if (dirs != NULL) | |
| 1209 dirs = g_list_sort (dirs, sortfunc); | |
| 1210 if (files != NULL) | |
| 1211 files = g_list_sort (files, sortfunc); | |
| 1212 | |
| 1213 filelist = dotdot; | |
| 1214 | |
| 1215 if (filelist == NULL) | |
| 1216 filelist = dirs; | |
| 1217 else | |
| 1218 filelist = g_list_concat (filelist, dirs); | |
| 1219 | |
| 1220 if (filelist == NULL) | |
| 1221 filelist = files; | |
| 1222 else | |
| 1223 filelist = g_list_concat (filelist, files); | |
| 1224 | |
| 39 | 1225 /* I haven't check this, but I'm pretty sure some older versions of glib |
| 1226 had a bug that the prev pointer wasn't being sent to NULL */ | |
| 1227 filelist->prev = NULL; | |
| 16 | 1228 return (filelist); |
| 1229 } | |
| 1230 |
