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