Mercurial > gftp.yaz
annotate lib/misc.c @ 80:f4f6f44225d3
2002-12-10 Brian Masney <masneyb@gftp.org>
* configure.in - check for grantpt function
* lib/misc.c (pty[ms]_open) - use Unix98 PTY allocation if grantpt()
is on the current system
* lib/gftp.h - include stropts.h if grantpt is here
2002-12-5 Brian Masney <masneyb@gftp.org>
* Release final 2.0.14
* lib/Chagelog-old - brief updates since 2.0.13
author | masneyb |
---|---|
date | Wed, 11 Dec 2002 02:33:33 +0000 |
parents | 100a4b310c60 |
children | 7ef60ce2bdb2 |
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) | |
67 | 559 gftp_request_destroy (tdata->fromreq, 1); |
1 | 560 if (tdata->toreq != NULL) |
67 | 561 gftp_request_destroy (tdata->toreq, 1); |
1 | 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 | |
66 | 572 newreq = gftp_request_new (); |
1 | 573 |
574 if (req->hostname) | |
56 | 575 newreq->hostname = g_strdup (req->hostname); |
1 | 576 if (req->username) |
56 | 577 newreq->username = g_strdup (req->username); |
1 | 578 if (req->password) |
56 | 579 newreq->password = g_strdup (req->password); |
1 | 580 if (req->account) |
56 | 581 newreq->account = g_strdup (req->account); |
1 | 582 if (req->directory) |
56 | 583 newreq->directory = g_strdup (req->directory); |
66 | 584 newreq->port = req->port; |
585 newreq->data_type = req->data_type; | |
586 newreq->use_proxy = req->use_proxy; | |
587 gftp_set_proxy_config (newreq, req->proxy_config); | |
588 newreq->transfer_type = req->transfer_type; | |
589 newreq->network_timeout = req->network_timeout; | |
590 newreq->retries = req->retries; | |
591 newreq->sleep_time = req->sleep_time; | |
592 newreq->passive_transfer = req->passive_transfer; | |
593 newreq->maxkbs = req->maxkbs; | |
594 newreq->logging_function = req->logging_function; | |
1 | 595 |
66 | 596 if (req->sftpserv_path != NULL) |
597 newreq->sftpserv_path = g_strdup (req->sftpserv_path); | |
1 | 598 |
599 req->init (newreq); | |
600 | |
601 return (newreq); | |
602 } | |
603 | |
604 | |
605 int | |
606 ptym_open (char *pts_name) | |
607 { | |
608 int fd; | |
609 | |
610 #ifdef __sgi | |
611 char *tempstr; | |
612 | |
613 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
614 return (-1); | |
615 | |
616 strcpy (pts_name, tempstr); | |
617 return (fd); | |
618 | |
619 #else /* !__sgi */ | |
620 | |
80 | 621 #ifdef HAVE_GRANTPT |
1 | 622 |
623 char *tempstr; | |
624 | |
625 strcpy (pts_name, "/dev/ptmx"); | |
626 if ((fd = open (pts_name, O_RDWR)) < 0) | |
627 return (-1); | |
628 | |
629 if (grantpt (fd) < 0) | |
630 { | |
631 close (fd); | |
632 return (-1); | |
633 } | |
634 | |
635 if (unlockpt (fd) < 0) | |
636 { | |
637 close (fd); | |
638 return (-1); | |
639 } | |
640 | |
641 if ((tempstr = ptsname (fd)) == NULL) | |
642 { | |
643 close (fd); | |
644 return (-1); | |
645 } | |
646 | |
647 strcpy (pts_name, tempstr); | |
648 return (fd); | |
649 | |
80 | 650 #else /* !GRANTPT */ |
1 | 651 |
652 char *pos1, *pos2; | |
653 | |
654 strcpy (pts_name, "/dev/ptyXY"); | |
655 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
656 { | |
657 pts_name[8] = *pos1; | |
658 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
659 { | |
660 pts_name[9] = *pos2; | |
661 if ((fd = open (pts_name, O_RDWR)) < 0) | |
662 continue; | |
663 pts_name[5] = 't'; | |
664 return (fd); | |
665 } | |
666 } | |
667 return (-1); | |
668 | |
669 #endif | |
670 | |
671 #endif | |
672 | |
673 } | |
674 | |
675 | |
676 int | |
677 ptys_open (int fdm, char *pts_name) | |
678 { | |
679 int fds; | |
680 | |
80 | 681 #if !defined (HAVE_GRANTPT) && !defined (__sgi) |
1 | 682 |
683 chmod (pts_name, S_IRUSR | S_IWUSR); | |
684 chown (pts_name, getuid (), -1); | |
685 | |
686 #endif | |
687 | |
688 if ((fds = open (pts_name, O_RDWR)) < 0) | |
689 { | |
690 close (fdm); | |
691 return (-1); | |
692 } | |
693 | |
80 | 694 #ifdef HAVE_GRANTPT |
695 /* I intentionally ignore these errors */ | |
696 ioctl (fds, I_PUSH, "ptem"); | |
697 ioctl (fds, I_PUSH, "ldterm"); | |
698 ioctl (fds, I_PUSH, "ttcompat"); | |
1 | 699 #endif |
700 | |
80 | 701 #if !defined(HAVE_GRANTPT) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) |
1 | 702 |
703 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
704 { | |
705 close (fdm); | |
706 return (-1); | |
707 } | |
708 | |
709 #endif | |
710 | |
711 return (fds); | |
712 } | |
713 | |
714 | |
715 int | |
716 tty_raw (int fd) | |
717 { | |
718 struct termios buf; | |
719 | |
720 if (tcgetattr (fd, &buf) < 0) | |
721 return (-1); | |
722 | |
723 buf.c_iflag |= IGNPAR; | |
724 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
725 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
726 #ifdef IEXTEN | |
727 buf.c_lflag &= ~(IEXTEN); | |
728 #endif | |
729 | |
730 buf.c_oflag &= ~(OPOST); | |
731 buf.c_cc[VMIN] = 1; | |
732 buf.c_cc[VTIME] = 0; | |
733 | |
734 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
735 return (-1); | |
736 return (0); | |
737 } | |
738 | |
739 | |
740 /* We have the caller send us a pointer to a string so we can write the port | |
741 into it. It makes it easier so we don't have to worry about freeing it | |
742 later on, the caller can just send us an auto variable, The string | |
743 should be at least 6 chars. I know this is messy... */ | |
744 | |
745 char ** | |
746 make_ssh_exec_args (gftp_request * request, char *execname, | |
747 int use_sftp_subsys, char *portstring) | |
748 { | |
749 char **args, *oldstr, *tempstr; | |
56 | 750 struct servent serv_struct; |
1 | 751 int i, j; |
752 | |
56 | 753 args = g_malloc (sizeof (char *) * (num_ssh_extra_params + 15)); |
1 | 754 |
755 args[0] = ssh_prog_name != NULL && *ssh_prog_name != '\0' ? | |
756 ssh_prog_name : "ssh"; | |
757 i = 1; | |
56 | 758 tempstr = g_strdup (args[0]); |
1 | 759 |
760 if (ssh_extra_params_list != NULL) | |
761 { | |
762 for (j=0; ssh_extra_params_list[j] != NULL; j++) | |
763 { | |
764 oldstr = tempstr; | |
765 args[i++] = ssh_extra_params_list[j]; | |
766 tempstr = g_strconcat (oldstr, " ", ssh_extra_params_list[j], NULL); | |
767 g_free (oldstr); | |
768 } | |
769 } | |
770 | |
771 oldstr = tempstr; | |
772 tempstr = g_strconcat (oldstr, " -e none", NULL); | |
773 g_free (oldstr); | |
774 args[i++] = "-e"; | |
775 args[i++] = "none"; | |
776 | |
777 if (request->username && *request->username != '\0') | |
778 { | |
779 oldstr = tempstr; | |
780 tempstr = g_strconcat (oldstr, " -l ", request->username, NULL); | |
781 g_free (oldstr); | |
782 args[i++] = "-l"; | |
783 args[i++] = request->username; | |
784 } | |
785 | |
786 if (request->port != 0) | |
787 { | |
788 g_snprintf (portstring, 6, "%d", request->port); | |
789 oldstr = tempstr; | |
790 tempstr = g_strconcat (oldstr, " -p ", portstring, NULL); | |
791 g_free (oldstr); | |
792 args[i++] = "-p"; | |
793 args[i++] = portstring; | |
794 } | |
56 | 795 else |
796 { | |
797 if (!r_getservbyname ("ssh", "tcp", &serv_struct, NULL)) | |
798 request->port = 22; | |
799 else | |
800 request->port = ntohs (serv_struct.s_port); | |
801 } | |
1 | 802 |
803 if (use_sftp_subsys) | |
804 { | |
805 oldstr = tempstr; | |
806 tempstr = g_strconcat (oldstr, " ", request->hostname, " -s sftp", NULL); | |
807 g_free (oldstr); | |
808 args[i++] = request->hostname; | |
809 args[i++] = "-s"; | |
810 args[i++] = "sftp"; | |
811 args[i] = NULL; | |
812 } | |
813 else | |
814 { | |
815 oldstr = tempstr; | |
816 tempstr = g_strconcat (oldstr, " ", request->hostname, " \"", execname, | |
817 "\"", NULL); | |
818 g_free (oldstr); | |
819 args[i++] = request->hostname; | |
820 args[i++] = execname; | |
821 args[i] = NULL; | |
822 } | |
823 | |
824 request->logging_function (gftp_logging_misc, request->user_data, | |
825 _("Running program %s\n"), tempstr); | |
826 g_free (tempstr); | |
827 return (args); | |
828 } | |
829 | |
61 | 830 #define SSH_LOGIN_BUFSIZE 200 |
831 #define SSH_ERROR_BADPASS -1 | |
832 #define SSH_ERROR_QUESTION -2 | |
77 | 833 #define SSH_WARNING -3 |
1 | 834 |
835 char * | |
836 ssh_start_login_sequence (gftp_request * request, int fd) | |
837 { | |
65 | 838 char *tempstr, *pwstr, *tmppos; |
839 size_t rem, len, diff, lastdiff, key_pos; | |
58 | 840 int wrotepw, ok; |
1 | 841 ssize_t rd; |
842 | |
61 | 843 rem = len = SSH_LOGIN_BUFSIZE; |
65 | 844 tempstr = g_malloc0 (len + 1); |
845 key_pos = diff = lastdiff = 0; | |
1 | 846 wrotepw = 0; |
847 ok = 1; | |
848 | |
61 | 849 if (gftp_set_sockblocking (request, fd, 1) == -1) |
58 | 850 return (NULL); |
1 | 851 |
58 | 852 pwstr = g_strconcat (request->password, "\n", NULL); |
1 | 853 |
41 | 854 errno = 0; |
1 | 855 while (1) |
856 { | |
61 | 857 if ((rd = gftp_read (request, tempstr + diff, rem - 1, fd)) <= 0) |
1 | 858 { |
859 ok = 0; | |
860 break; | |
861 } | |
862 rem -= rd; | |
863 diff += rd; | |
65 | 864 tempstr[diff] = '\0'; |
1 | 865 |
61 | 866 if (diff > 11 && strcmp (tempstr + diff - 10, "password: ") == 0) |
1 | 867 { |
61 | 868 if (wrotepw) |
869 { | |
870 ok = SSH_ERROR_BADPASS; | |
871 break; | |
872 } | |
77 | 873 |
874 if (strstr (tempstr, "WARNING") != NULL || | |
875 strstr (tempstr, _("WARNING")) != NULL) | |
876 { | |
877 ok = SSH_WARNING; | |
878 break; | |
879 } | |
61 | 880 |
1 | 881 wrotepw = 1; |
58 | 882 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
883 { | |
884 ok = 0; | |
885 break; | |
886 } | |
1 | 887 } |
65 | 888 else if (diff > 2 && strcmp (tempstr + diff - 2, ": ") == 0 && |
889 ((tmppos = strstr (tempstr + key_pos, "Enter passphrase for RSA key")) != NULL || | |
890 ((tmppos = strstr (tempstr + key_pos, "Enter passphrase for key '")) != NULL))) | |
61 | 891 { |
65 | 892 key_pos = diff; |
61 | 893 if (wrotepw) |
894 { | |
895 ok = SSH_ERROR_BADPASS; | |
896 break; | |
897 } | |
1 | 898 |
77 | 899 if (strstr (tempstr, "WARNING") != NULL || |
900 strstr (tempstr, _("WARNING")) != NULL) | |
901 { | |
902 ok = SSH_WARNING; | |
903 break; | |
904 } | |
905 | |
1 | 906 wrotepw = 1; |
58 | 907 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
908 { | |
909 ok = 0; | |
910 break; | |
911 } | |
1 | 912 } |
65 | 913 else if (diff > 10 && strcmp (tempstr + diff - 10, "(yes/no)? ") == 0) |
61 | 914 { |
915 ok = SSH_ERROR_QUESTION; | |
916 break; | |
917 } | |
918 else if (diff >= 5 && strcmp (tempstr + diff - 5, "xsftp") == 0) | |
1 | 919 break; |
65 | 920 else if (rem <= 1) |
921 { | |
922 request->logging_function (gftp_logging_recv, request->user_data, | |
923 "%s", tempstr + lastdiff); | |
924 len += SSH_LOGIN_BUFSIZE; | |
925 rem += SSH_LOGIN_BUFSIZE; | |
926 lastdiff = diff; | |
927 tempstr = g_realloc (tempstr, len); | |
928 continue; | |
929 } | |
1 | 930 } |
931 | |
58 | 932 g_free (pwstr); |
61 | 933 |
934 if (*(tempstr + lastdiff) != '\0') | |
935 request->logging_function (gftp_logging_recv, request->user_data, | |
936 "%s\n", tempstr + lastdiff); | |
1 | 937 |
61 | 938 if (ok <= 0) |
1 | 939 { |
61 | 940 if (ok == SSH_ERROR_BADPASS) |
941 request->logging_function (gftp_logging_error, request->user_data, | |
942 _("Error: An incorrect password was entered\n")); | |
943 else if (ok == SSH_ERROR_QUESTION) | |
944 request->logging_function (gftp_logging_error, request->user_data, | |
945 _("Please connect to this host with the command line SSH utility and answer this question appropriately.\n")); | |
77 | 946 else if (ok == SSH_WARNING) |
947 request->logging_function (gftp_logging_error, request->user_data, | |
948 _("Please correct the above warning to connect to this host.\n")); | |
61 | 949 |
1 | 950 g_free (tempstr); |
951 return (NULL); | |
952 } | |
953 | |
954 return (tempstr); | |
955 } | |
956 | |
957 | |
958 #ifdef G_HAVE_GINT64 | |
959 | |
960 gint64 | |
961 hton64 (gint64 val) | |
962 { | |
963 gint64 num; | |
964 char *pos; | |
965 | |
966 num = 0; | |
967 pos = (char *) # | |
968 pos[0] = (val >> 56) & 0xff; | |
969 pos[1] = (val >> 48) & 0xff; | |
970 pos[2] = (val >> 40) & 0xff; | |
971 pos[3] = (val >> 32) & 0xff; | |
972 pos[4] = (val >> 24) & 0xff; | |
973 pos[5] = (val >> 16) & 0xff; | |
974 pos[6] = (val >> 8) & 0xff; | |
975 pos[7] = val & 0xff; | |
976 return (num); | |
977 } | |
978 | |
979 #endif | |
980 | |
16 | 981 |
982 static gint | |
983 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
984 { | |
985 const gftp_file * f1, * f2; | |
986 | |
987 f1 = a; | |
988 f2 = b; | |
989 return (strcmp (f1->file, f2->file)); | |
990 } | |
991 | |
992 | |
993 static gint | |
994 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
995 { | |
996 const gftp_file * f1, * f2; | |
997 gint ret; | |
998 | |
999 f1 = a; | |
1000 f2 = b; | |
1001 ret = strcmp (f1->file, f2->file); | |
1002 if (ret < 0) | |
1003 ret = 1; | |
1004 else if (ret > 0) | |
1005 ret = -1; | |
1006 return (ret); | |
1007 } | |
1008 | |
1009 | |
1010 static gint | |
1011 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
1012 { | |
1013 const gftp_file * f1, * f2; | |
1014 | |
1015 f1 = a; | |
1016 f2 = b; | |
1017 return (strcmp (f1->user, f2->user)); | |
1018 } | |
1019 | |
1020 | |
1021 static gint | |
1022 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
1023 { | |
1024 const gftp_file * f1, * f2; | |
1025 gint ret; | |
1026 | |
1027 f1 = a; | |
1028 f2 = b; | |
1029 ret = strcmp (f1->user, f2->user); | |
1030 if (ret < 0) | |
1031 ret = 1; | |
1032 else if (ret > 0) | |
1033 ret = -1; | |
1034 return (ret); | |
1035 } | |
1036 | |
1037 | |
1038 static gint | |
1039 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
1040 { | |
1041 const gftp_file * f1, * f2; | |
1042 | |
1043 f1 = a; | |
1044 f2 = b; | |
1045 return (strcmp (f1->group, f2->group)); | |
1046 } | |
1047 | |
1048 | |
1049 static gint | |
1050 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
1051 { | |
1052 const gftp_file * f1, * f2; | |
1053 gint ret; | |
1054 | |
1055 f1 = a; | |
1056 f2 = b; | |
1057 ret = strcmp (f1->group, f2->group); | |
1058 if (ret < 0) | |
1059 ret = 1; | |
1060 else if (ret > 0) | |
1061 ret = -1; | |
1062 return (ret); | |
1063 } | |
1064 | |
1065 | |
1066 static gint | |
1067 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
1068 { | |
1069 const gftp_file * f1, * f2; | |
1070 | |
1071 f1 = a; | |
1072 f2 = b; | |
1073 return (strcmp (f1->attribs, f2->attribs)); | |
1074 } | |
1075 | |
1076 | |
1077 static gint | |
1078 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
1079 { | |
1080 const gftp_file * f1, * f2; | |
1081 gint ret; | |
1082 | |
1083 f1 = a; | |
1084 f2 = b; | |
1085 ret = strcmp (f1->attribs, f2->attribs); | |
1086 if (ret < 0) | |
1087 ret = 1; | |
1088 else if (ret > 0) | |
1089 ret = -1; | |
1090 return (ret); | |
1091 } | |
1092 | |
1093 | |
1094 static gint | |
1095 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
1096 { | |
1097 const gftp_file * f1, * f2; | |
1098 | |
1099 f1 = a; | |
1100 f2 = b; | |
1101 if (f1->size < f2->size) | |
1102 return (-1); | |
1103 else if (f1->size == f2->size) | |
1104 return (0); | |
1105 else | |
1106 return (1); | |
1107 } | |
1108 | |
1109 | |
1110 static gint | |
1111 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
1112 { | |
1113 const gftp_file * f1, * f2; | |
1114 | |
1115 f1 = a; | |
1116 f2 = b; | |
1117 if (f1->size < f2->size) | |
1118 return (1); | |
1119 else if (f1->size == f2->size) | |
1120 return (0); | |
1121 else | |
1122 return (-1); | |
1123 } | |
1124 | |
1125 | |
1126 static gint | |
1127 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
1128 { | |
1129 const gftp_file * f1, * f2; | |
1130 | |
1131 f1 = a; | |
1132 f2 = b; | |
1133 if (f1->datetime < f2->datetime) | |
1134 return (-1); | |
1135 else if (f1->datetime == f2->datetime) | |
1136 return (0); | |
1137 else | |
1138 return (1); | |
1139 } | |
1140 | |
1141 | |
1142 static gint | |
1143 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
1144 { | |
1145 const gftp_file * f1, * f2; | |
1146 | |
1147 f1 = a; | |
1148 f2 = b; | |
1149 if (f1->datetime < f2->datetime) | |
1150 return (1); | |
1151 else if (f1->datetime == f2->datetime) | |
1152 return (0); | |
1153 else | |
1154 return (-1); | |
1155 } | |
1156 | |
1157 | |
1158 GList * | |
1159 gftp_sort_filelist (GList * filelist, int column, int asds) | |
1160 { | |
1161 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
1162 GCompareFunc sortfunc; | |
1163 gftp_file * tempfle; | |
1164 | |
1165 files = dirs = dotdot = NULL; | |
1166 | |
1167 if (column == GFTP_SORT_COL_FILE) | |
1168 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
1169 else if (column == GFTP_SORT_COL_SIZE) | |
1170 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
1171 else if (column == GFTP_SORT_COL_USER) | |
1172 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
1173 else if (column == GFTP_SORT_COL_GROUP) | |
1174 sortfunc = asds ? | |
1175 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
1176 else if (column == GFTP_SORT_COL_DATETIME) | |
1177 sortfunc = asds ? | |
1178 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; else /* GFTP_SORT_COL_ATTRIBS */ | |
1179 sortfunc = asds ? | |
1180 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
1181 | |
1182 for (tempitem = filelist; tempitem != NULL; ) | |
1183 { | |
1184 tempfle = tempitem->data; | |
1185 insitem = tempitem; | |
1186 tempitem = tempitem->next; | |
1187 insitem->next = NULL; | |
1188 | |
1189 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
1190 dotdot = insitem; | |
1191 else if (sort_dirs_first && tempfle->isdir) | |
1192 { | |
1193 insitem->next = dirs; | |
1194 dirs = insitem; | |
1195 } | |
1196 else | |
1197 { | |
1198 insitem->next = files; | |
1199 files = insitem; | |
1200 } | |
1201 } | |
1202 | |
1203 if (dirs != NULL) | |
1204 dirs = g_list_sort (dirs, sortfunc); | |
1205 if (files != NULL) | |
1206 files = g_list_sort (files, sortfunc); | |
1207 | |
1208 filelist = dotdot; | |
1209 | |
1210 if (filelist == NULL) | |
1211 filelist = dirs; | |
1212 else | |
1213 filelist = g_list_concat (filelist, dirs); | |
1214 | |
1215 if (filelist == NULL) | |
1216 filelist = files; | |
1217 else | |
1218 filelist = g_list_concat (filelist, files); | |
1219 | |
39 | 1220 /* I haven't check this, but I'm pretty sure some older versions of glib |
1221 had a bug that the prev pointer wasn't being sent to NULL */ | |
1222 filelist->prev = NULL; | |
16 | 1223 return (filelist); |
1224 } | |
1225 |