Mercurial > gftp.yaz
annotate lib/misc.c @ 14:83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
* Sanity checking on the fdopen() calls. Also make sure that all of them have a + in their open mode. Doesn't affect UNIX, but it does affect Windows
* HTTP fixes when running under Solaris. Read from request->sockfd and write to request->sockfd_write (Solaris doesn't like it when you read/write to the same FILE structure)
* Display major/minor properly for a device when connected with the local protocol
author | masneyb |
---|---|
date | Wed, 04 Sep 2002 11:45:56 +0000 |
parents | cd2e26a69461 |
children | ddb983d8574b |
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" | |
22 | |
23 char * | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
24 insert_commas (off_t number, char *dest_str, size_t dest_len) |
1 | 25 { |
26 char *frompos, *topos, src[50], *dest; | |
27 int len, num, rem, i; | |
28 | |
29 if (dest_str != NULL) | |
30 *dest_str = '\0'; | |
13 | 31 len = (number > 0 ? log10 (number) : 0) + 1; |
1 | 32 |
33 if (len <= 0) | |
34 { | |
35 if (dest_str != NULL) | |
36 strncpy (dest_str, "0", dest_len); | |
37 else | |
38 dest_str = g_strconcat ("0", NULL); | |
39 return (dest_str); | |
40 } | |
41 | |
42 len += len / 3; | |
43 if (dest_str != NULL && len > dest_len) | |
44 { | |
45 | |
46 for (i=0; i<dest_len - 1; i++) | |
47 dest_str[i] = 'X'; | |
48 dest_str[dest_len - 1] = '\0'; | |
49 return (dest_str); | |
50 } | |
51 | |
52 if (dest_str == NULL) | |
53 dest = g_malloc0 (len); | |
54 else | |
55 dest = dest_str; | |
56 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
57 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
58 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
|
59 #else |
1 | 60 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
|
61 #endif |
1 | 62 |
63 num = strlen (src) / 3 - 1; | |
64 rem = strlen (src) % 3; | |
65 frompos = src; | |
66 topos = dest; | |
67 for (i = 0; i < rem; i++) | |
68 *topos++ = *frompos++; | |
69 | |
70 if (*frompos != '\0') | |
71 { | |
72 if (rem != 0) | |
73 *topos++ = ','; | |
74 while (num > 0) | |
75 { | |
76 for (i = 0; i < 3; i++) | |
77 *topos++ = *frompos++; | |
78 *topos++ = ','; | |
79 num--; | |
80 } | |
81 for (i = 0; i < 3; i++) | |
82 *topos++ = *frompos++; | |
83 } | |
84 *topos = '\0'; | |
85 return (dest); | |
86 } | |
87 | |
88 | |
89 long | |
90 file_countlf (int filefd, long endpos) | |
91 { | |
92 char tempstr[255]; | |
93 long num, mypos; | |
94 ssize_t n; | |
95 int i; | |
96 | |
97 mypos = num = 0; | |
98 lseek (filefd, 0, SEEK_SET); | |
99 while ((n = read (filefd, tempstr, sizeof (tempstr))) > 0) | |
100 { | |
101 for (i = 0; i < n; i++) | |
102 { | |
103 if ((tempstr[i] == '\n') && (i > 0) && (tempstr[i - 1] != '\r') | |
104 && (endpos == 0 || mypos + i <= endpos)) | |
105 ++num; | |
106 } | |
107 mypos += n; | |
108 } | |
109 lseek (filefd, 0, SEEK_SET); | |
110 return (num); | |
111 } | |
112 | |
113 | |
114 char * | |
115 alltrim (char *str) | |
116 { | |
117 char *pos, *newpos; | |
118 int diff; | |
119 | |
120 pos = str + strlen (str) - 1; | |
121 while (pos >= str && *pos == ' ') | |
122 *pos-- = '\0'; | |
123 | |
124 pos = str; | |
125 diff = 0; | |
126 while (*pos++ == ' ') | |
127 diff++; | |
128 | |
129 if (diff == 0) | |
130 return (str); | |
131 | |
132 pos = str + diff; | |
133 newpos = str; | |
134 while (*pos != '\0') | |
135 *newpos++ = *pos++; | |
136 *newpos = '\0'; | |
137 | |
138 return (str); | |
139 } | |
140 | |
141 | |
142 char * | |
143 expand_path (const char *src) | |
144 { | |
145 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
146 struct passwd *pw; | |
147 | |
148 pw = NULL; | |
149 str = g_malloc (strlen (src) + 1); | |
150 strcpy (str, src); | |
151 | |
152 if (*str == '~') | |
153 { | |
154 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
155 pw = getpwuid (geteuid ()); | |
156 else | |
157 { | |
158 if ((pos = strchr (str, '/')) != NULL) | |
159 *pos = '\0'; | |
160 | |
161 pw = getpwnam (str + 1); | |
162 | |
163 if (pos != NULL) | |
164 *pos = '/'; | |
165 } | |
166 } | |
167 | |
168 endpos = str; | |
169 newstr = NULL; | |
170 while ((pos = strchr (endpos, '/')) != NULL) | |
171 { | |
172 pos++; | |
173 while (*pos == '/') | |
174 pos++; | |
175 if ((endpos = strchr (pos, '/')) == NULL) | |
176 endpos = pos + strlen (pos); | |
177 tempchar = *endpos; | |
178 *endpos = '\0'; | |
179 if (strcmp (pos, "..") == 0) | |
180 { | |
181 *(pos - 1) = '\0'; | |
182 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
183 *prevpos = '\0'; | |
184 } | |
185 else if (strcmp (pos, ".") != 0) | |
186 { | |
187 if (newstr == NULL) | |
188 newstr = g_strconcat (pos - 1, NULL); | |
189 else | |
190 { | |
191 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
192 g_free (newstr); | |
193 newstr = tempstr; | |
194 } | |
195 } | |
196 *endpos = tempchar; | |
197 if (*endpos == '\0') | |
198 break; | |
199 endpos = pos + 1; | |
200 } | |
201 | |
202 if (newstr == NULL || *newstr == '\0') | |
203 { | |
204 if (newstr != NULL) | |
205 g_free (newstr); | |
206 newstr = g_malloc0 (2); | |
207 *newstr = '/'; | |
208 } | |
209 | |
210 g_free (str); | |
211 | |
212 if (pw != NULL) | |
213 { | |
214 if ((pos = strchr (newstr, '/')) == NULL) | |
215 { | |
216 str = g_malloc (strlen (pw->pw_dir) + 1); | |
217 strcpy (str, pw->pw_dir); | |
218 } | |
219 else | |
220 str = g_strconcat (pw->pw_dir, pos, NULL); | |
221 | |
222 g_free (newstr); | |
223 newstr = str; | |
224 } | |
225 | |
226 return (newstr); | |
227 } | |
228 | |
229 | |
230 void | |
231 remove_double_slashes (char *string) | |
232 { | |
233 char *newpos, *oldpos; | |
234 | |
235 oldpos = newpos = string; | |
236 while (*oldpos != '\0') | |
237 { | |
238 *newpos++ = *oldpos++; | |
239 if (*oldpos == '\0') | |
240 break; | |
241 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
242 oldpos++; | |
243 } | |
244 *newpos = '\0'; | |
245 if (string[strlen (string) - 1] == '/') | |
246 string[strlen (string) - 1] = '\0'; | |
247 } | |
248 | |
249 | |
250 void | |
251 make_nonnull (char **str) | |
252 { | |
253 if (*str == NULL) | |
254 *str = g_malloc0 (1); | |
255 } | |
256 | |
257 | |
258 int | |
259 copyfile (char *source, char *dest) | |
260 { | |
261 FILE *srcfd, *destfd; | |
262 char buf[8192]; | |
263 size_t n; | |
264 | |
265 if ((srcfd = fopen (source, "rb")) == NULL) | |
266 return (0); | |
267 | |
268 if ((destfd = fopen (dest, "wb")) == NULL) | |
269 { | |
270 fclose (srcfd); | |
271 return (0); | |
272 } | |
273 | |
274 while ((n = fread (buf, 1, sizeof (buf), srcfd)) > 0) | |
275 fwrite (buf, 1, n, destfd); | |
276 | |
277 fclose (srcfd); | |
278 fclose (destfd); | |
279 | |
280 return (1); | |
281 } | |
282 | |
283 | |
284 int | |
285 gftp_match_filespec (char *filename, char *filespec) | |
286 { | |
287 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
288 size_t len, curlen; | |
289 | |
290 if (filename == NULL || *filename == '\0' || | |
291 filespec == NULL || *filespec == '\0') | |
292 return(1); | |
293 | |
294 filepos = filename; | |
295 wcpos = filespec; | |
296 while(1) | |
297 { | |
298 if (*wcpos == '\0') | |
299 return (1); | |
300 else if (*filepos == '\0') | |
301 return(0); | |
302 else if(*wcpos == '?') | |
303 { | |
304 wcpos++; | |
305 filepos++; | |
306 } | |
307 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
308 return(1); | |
309 else if(*wcpos == '*') | |
310 { | |
311 len = sizeof (search_str); | |
312 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
313 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
314 curlen++, *newpos++ = *pos++); | |
315 *newpos = '\0'; | |
316 | |
317 if ((filepos = strstr (filepos, search_str)) == NULL) | |
318 return(0); | |
319 wcpos += curlen + 1; | |
320 filepos += curlen; | |
321 } | |
322 else if(*wcpos++ != *filepos++) | |
323 return(0); | |
324 } | |
325 return (1); | |
326 } | |
327 | |
328 | |
329 int | |
330 gftp_parse_command_line (int *argc, char ***argv) | |
331 { | |
332 if (*argc > 1) | |
333 { | |
334 if (strcmp (argv[0][1], "--help") == 0 || strcmp (argv[0][1], "-h") == 0) | |
335 gftp_usage (); | |
336 else if (strcmp (argv[0][1], "--version") == 0 || strcmp (argv[0][1], "-v") == 0) | |
337 { | |
338 printf ("%s\n", version); | |
339 exit (0); | |
340 } | |
341 } | |
342 return (0); | |
343 } | |
344 | |
345 | |
346 void | |
347 gftp_usage (void) | |
348 { | |
349 printf (_("usage: gftp [[ftp://][user:[pass]@]ftp-site[:port][/directory]]\n")); | |
350 exit (0); | |
351 } | |
352 | |
353 | |
354 char * | |
355 get_xpm_path (char *filename, int quit_on_err) | |
356 { | |
357 char *tempstr, *exfile; | |
358 | |
359 tempstr = g_strconcat (BASE_CONF_DIR, "/", filename, NULL); | |
360 exfile = expand_path (tempstr); | |
361 g_free (tempstr); | |
362 if (access (exfile, F_OK) != 0) | |
363 { | |
364 g_free (exfile); | |
365 tempstr = g_strconcat (SHARE_DIR, "/", filename, NULL); | |
366 exfile = expand_path (tempstr); | |
367 g_free (tempstr); | |
368 if (access (exfile, F_OK) != 0) | |
369 { | |
370 g_free (exfile); | |
371 exfile = g_strconcat ("/usr/share/icons/", filename, NULL); | |
372 if (access (exfile, F_OK) != 0) | |
373 { | |
374 g_free (exfile); | |
375 if (!quit_on_err) | |
376 return (NULL); | |
377 printf (_("gFTP Error: Cannot find file %s in %s or %s\n"), | |
378 filename, SHARE_DIR, BASE_CONF_DIR); | |
379 exit (-1); | |
380 } | |
381 } | |
382 } | |
383 return (exfile); | |
384 } | |
385 | |
386 | |
387 gint | |
388 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
389 { | |
390 return (strcmp ((char *) path1, (char *) path2) == 0); | |
391 } | |
392 | |
393 | |
394 guint | |
395 string_hash_function (gconstpointer key) | |
396 { | |
397 return (((char *) key)[0] + ((char *) key)[1] + ((char *) key)[2]); | |
398 } | |
399 | |
400 | |
401 void | |
402 free_file_list (GList * filelist) | |
403 { | |
404 gftp_file * tempfle; | |
405 GList * templist; | |
406 | |
407 templist = filelist; | |
408 while (templist != NULL) | |
409 { | |
410 tempfle = templist->data; | |
411 free_fdata (tempfle); | |
412 templist = templist->next; | |
413 } | |
414 g_list_free (filelist); | |
415 } | |
416 | |
417 | |
418 void | |
419 free_fdata (gftp_file * fle) | |
420 { | |
421 if (fle->file) | |
422 g_free (fle->file); | |
423 if (fle->user) | |
424 g_free (fle->user); | |
425 if (fle->group) | |
426 g_free (fle->group); | |
427 if (fle->attribs) | |
428 g_free (fle->attribs); | |
429 if (fle->destfile) | |
430 g_free (fle->destfile); | |
431 if (fle->fd) | |
432 fclose (fle->fd); | |
433 g_free (fle); | |
434 } | |
435 | |
436 | |
437 gftp_file * | |
438 copy_fdata (gftp_file * fle) | |
439 { | |
440 gftp_file * newfle; | |
441 | |
442 newfle = g_malloc0 (sizeof (*newfle)); | |
443 memcpy (newfle, fle, sizeof (*newfle)); | |
444 | |
445 if (fle->file) | |
446 { | |
447 newfle->file = g_malloc (strlen (fle->file) + 1); | |
448 strcpy (newfle->file, fle->file); | |
449 } | |
450 | |
451 if (fle->user) | |
452 { | |
453 newfle->user = g_malloc (strlen (fle->user) + 1); | |
454 strcpy (newfle->user, fle->user); | |
455 } | |
456 | |
457 if (fle->group) | |
458 { | |
459 newfle->group = g_malloc (strlen (fle->group) + 1); | |
460 strcpy (newfle->group, fle->group); | |
461 } | |
462 | |
463 if (fle->attribs) | |
464 { | |
465 newfle->attribs = g_malloc (strlen (fle->attribs) + 1); | |
466 strcpy (newfle->attribs, fle->attribs); | |
467 } | |
468 | |
469 if (fle->destfile) | |
470 { | |
471 newfle->destfile = g_malloc (strlen (fle->destfile) + 1); | |
472 strcpy (newfle->destfile, fle->destfile); | |
473 } | |
474 return (newfle); | |
475 } | |
476 | |
477 | |
478 void | |
479 swap_socks (gftp_request * dest, gftp_request * source) | |
480 { | |
481 dest->sockfd = source->sockfd; | |
482 dest->datafd = source->datafd; | |
483 dest->sockfd_write = source->sockfd_write; | |
484 dest->cached = 0; | |
485 if (!source->always_connected) | |
486 { | |
487 source->sockfd = NULL; | |
488 source->datafd = NULL; | |
489 source->sockfd_write = NULL; | |
490 source->cached = 1; | |
491 } | |
492 } | |
493 | |
494 | |
495 int | |
496 compare_request (gftp_request * request1, gftp_request * request2, | |
497 int compare_dirs) | |
498 { | |
499 char *strarr[3][2]; | |
500 int i, ret; | |
501 | |
502 ret = 1; | |
503 if (strcmp (request1->protocol_name, request2->protocol_name) == 0 && | |
504 request1->port == request2->port) | |
505 { | |
506 strarr[0][0] = request1->hostname; | |
507 strarr[0][1] = request2->hostname; | |
508 strarr[1][0] = request1->username; | |
509 strarr[1][1] = request2->username; | |
510 if (compare_dirs) | |
511 { | |
512 strarr[2][0] = request1->directory; | |
513 strarr[2][1] = request2->directory; | |
514 } | |
515 else | |
516 strarr[2][0] = strarr[2][1] = ""; | |
517 | |
518 for (i = 0; i < 3; i++) | |
519 { | |
520 if ((strarr[i][0] && !strarr[i][1]) || | |
521 (!strarr[i][0] && strarr[i][1])) | |
522 { | |
523 ret = 0; | |
524 break; | |
525 } | |
526 | |
527 if (strarr[i][0] && strarr[i][1] && | |
528 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
529 { | |
530 ret = 0; | |
531 break; | |
532 } | |
533 } | |
534 } | |
535 else | |
536 ret = 0; | |
537 return (ret); | |
538 } | |
539 | |
540 | |
541 void | |
542 free_tdata (gftp_transfer * tdata) | |
543 { | |
544 if (tdata->statmutex) | |
545 g_free (tdata->statmutex); | |
546 if (tdata->structmutex) | |
547 g_free (tdata->structmutex); | |
548 if (tdata->fromreq != NULL) | |
549 gftp_request_destroy (tdata->fromreq); | |
550 if (tdata->toreq != NULL) | |
551 gftp_request_destroy (tdata->toreq); | |
552 free_file_list (tdata->files); | |
553 g_free (tdata); | |
554 } | |
555 | |
556 | |
557 gftp_request * | |
558 copy_request (gftp_request * req) | |
559 { | |
560 gftp_request * newreq; | |
561 | |
562 newreq = g_malloc0 (sizeof (*newreq)); | |
563 memcpy (newreq, req, sizeof (*newreq)); | |
564 | |
565 if (req->hostname) | |
566 newreq->hostname = g_strconcat (req->hostname, NULL); | |
567 if (req->username) | |
568 newreq->username = g_strconcat (req->username, NULL); | |
569 if (req->password) | |
570 newreq->password = g_strconcat (req->password, NULL); | |
571 if (req->account) | |
572 newreq->account = g_strconcat (req->account, NULL); | |
573 if (req->directory) | |
574 newreq->directory = g_strconcat (req->directory, NULL); | |
575 | |
576 newreq->url_prefix = NULL; | |
577 newreq->protocol_name = NULL; | |
578 newreq->sftpserv_path = NULL; | |
579 newreq->proxy_config = NULL; | |
580 newreq->proxy_hostname = NULL; | |
581 newreq->proxy_username = NULL; | |
582 newreq->proxy_password = NULL; | |
583 newreq->proxy_account = NULL; | |
584 newreq->last_ftp_response = NULL; | |
585 newreq->last_dir_entry = NULL; | |
586 newreq->sockfd = NULL; | |
587 newreq->sockfd_write = NULL; | |
588 newreq->datafd = NULL; | |
589 newreq->cachefd = NULL; | |
590 newreq->hostp = NULL; | |
591 newreq->protocol_data = NULL; | |
592 | |
593 if (req->proxy_config != NULL) | |
594 newreq->proxy_config = g_strconcat (req->proxy_config, NULL); | |
595 | |
596 req->init (newreq); | |
597 | |
598 return (newreq); | |
599 } | |
600 | |
601 | |
602 int | |
603 ptym_open (char *pts_name) | |
604 { | |
605 int fd; | |
606 | |
607 #ifdef __sgi | |
608 char *tempstr; | |
609 | |
610 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
611 return (-1); | |
612 | |
613 strcpy (pts_name, tempstr); | |
614 return (fd); | |
615 | |
616 #else /* !__sgi */ | |
617 | |
618 #ifdef SYSV | |
619 | |
620 char *tempstr; | |
621 | |
622 strcpy (pts_name, "/dev/ptmx"); | |
623 if ((fd = open (pts_name, O_RDWR)) < 0) | |
624 return (-1); | |
625 | |
626 if (grantpt (fd) < 0) | |
627 { | |
628 close (fd); | |
629 return (-1); | |
630 } | |
631 | |
632 if (unlockpt (fd) < 0) | |
633 { | |
634 close (fd); | |
635 return (-1); | |
636 } | |
637 | |
638 if ((tempstr = ptsname (fd)) == NULL) | |
639 { | |
640 close (fd); | |
641 return (-1); | |
642 } | |
643 | |
644 strcpy (pts_name, tempstr); | |
645 return (fd); | |
646 | |
647 #else /* !SYSV */ | |
648 | |
649 char *pos1, *pos2; | |
650 | |
651 strcpy (pts_name, "/dev/ptyXY"); | |
652 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
653 { | |
654 pts_name[8] = *pos1; | |
655 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
656 { | |
657 pts_name[9] = *pos2; | |
658 if ((fd = open (pts_name, O_RDWR)) < 0) | |
659 continue; | |
660 pts_name[5] = 't'; | |
661 return (fd); | |
662 } | |
663 } | |
664 return (-1); | |
665 | |
666 #endif | |
667 | |
668 #endif | |
669 | |
670 } | |
671 | |
672 | |
673 int | |
674 ptys_open (int fdm, char *pts_name) | |
675 { | |
676 int fds; | |
677 | |
678 #if !defined (SYSV) && !defined (__sgi) | |
679 | |
680 chmod (pts_name, S_IRUSR | S_IWUSR); | |
681 chown (pts_name, getuid (), -1); | |
682 | |
683 #endif | |
684 | |
685 if ((fds = open (pts_name, O_RDWR)) < 0) | |
686 { | |
687 close (fdm); | |
688 return (-1); | |
689 } | |
690 | |
691 #ifdef SYSV | |
692 | |
693 if (ioctl (fds, I_PUSH, "ptem") < 0) | |
694 { | |
695 close (fdm); | |
696 close (fds); | |
697 return (-1); | |
698 } | |
699 | |
700 if (ioctl (fds, I_PUSH, "ldterm") < 0) | |
701 { | |
702 close (fdm); | |
703 close (fds); | |
704 return (-1); | |
705 } | |
706 | |
707 if (ioctl (fds, I_PUSH, "ttcompat") < 0) | |
708 { | |
709 close (fdm); | |
710 close (fds); | |
711 return (-1); | |
712 } | |
713 | |
714 #endif | |
715 | |
716 #if !defined(SYSV) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) | |
717 | |
718 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
719 { | |
720 close (fdm); | |
721 return (-1); | |
722 } | |
723 | |
724 #endif | |
725 | |
726 return (fds); | |
727 } | |
728 | |
729 | |
730 int | |
731 tty_raw (int fd) | |
732 { | |
733 struct termios buf; | |
734 | |
735 if (tcgetattr (fd, &buf) < 0) | |
736 return (-1); | |
737 | |
738 buf.c_iflag |= IGNPAR; | |
739 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
740 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
741 #ifdef IEXTEN | |
742 buf.c_lflag &= ~(IEXTEN); | |
743 #endif | |
744 | |
745 buf.c_oflag &= ~(OPOST); | |
746 buf.c_cc[VMIN] = 1; | |
747 buf.c_cc[VTIME] = 0; | |
748 | |
749 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
750 return (-1); | |
751 return (0); | |
752 } | |
753 | |
754 | |
755 /* We have the caller send us a pointer to a string so we can write the port | |
756 into it. It makes it easier so we don't have to worry about freeing it | |
757 later on, the caller can just send us an auto variable, The string | |
758 should be at least 6 chars. I know this is messy... */ | |
759 | |
760 char ** | |
761 make_ssh_exec_args (gftp_request * request, char *execname, | |
762 int use_sftp_subsys, char *portstring) | |
763 { | |
764 char **args, *oldstr, *tempstr; | |
765 int i, j; | |
766 | |
767 args = g_malloc (sizeof (char *) * (num_ssh_extra_params + 10)); | |
768 | |
769 args[0] = ssh_prog_name != NULL && *ssh_prog_name != '\0' ? | |
770 ssh_prog_name : "ssh"; | |
771 i = 1; | |
772 tempstr = g_strconcat (args[0], NULL); | |
773 | |
774 if (ssh_extra_params_list != NULL) | |
775 { | |
776 for (j=0; ssh_extra_params_list[j] != NULL; j++) | |
777 { | |
778 oldstr = tempstr; | |
779 args[i++] = ssh_extra_params_list[j]; | |
780 tempstr = g_strconcat (oldstr, " ", ssh_extra_params_list[j], NULL); | |
781 g_free (oldstr); | |
782 } | |
783 } | |
784 | |
785 oldstr = tempstr; | |
786 tempstr = g_strconcat (oldstr, " -e none", NULL); | |
787 g_free (oldstr); | |
788 args[i++] = "-e"; | |
789 args[i++] = "none"; | |
790 | |
791 if (request->username && *request->username != '\0') | |
792 { | |
793 oldstr = tempstr; | |
794 tempstr = g_strconcat (oldstr, " -l ", request->username, NULL); | |
795 g_free (oldstr); | |
796 args[i++] = "-l"; | |
797 args[i++] = request->username; | |
798 } | |
799 | |
800 if (request->port != 0) | |
801 { | |
802 g_snprintf (portstring, 6, "%d", request->port); | |
803 oldstr = tempstr; | |
804 tempstr = g_strconcat (oldstr, " -p ", portstring, NULL); | |
805 g_free (oldstr); | |
806 args[i++] = "-p"; | |
807 args[i++] = portstring; | |
808 } | |
809 | |
810 if (use_sftp_subsys) | |
811 { | |
812 oldstr = tempstr; | |
813 tempstr = g_strconcat (oldstr, " ", request->hostname, " -s sftp", NULL); | |
814 g_free (oldstr); | |
815 args[i++] = request->hostname; | |
816 args[i++] = "-s"; | |
817 args[i++] = "sftp"; | |
818 args[i] = NULL; | |
819 } | |
820 else | |
821 { | |
822 oldstr = tempstr; | |
823 tempstr = g_strconcat (oldstr, " ", request->hostname, " \"", execname, | |
824 "\"", NULL); | |
825 g_free (oldstr); | |
826 args[i++] = request->hostname; | |
827 args[i++] = execname; | |
828 args[i] = NULL; | |
829 } | |
830 | |
831 request->logging_function (gftp_logging_misc, request->user_data, | |
832 _("Running program %s\n"), tempstr); | |
833 g_free (tempstr); | |
834 return (args); | |
835 } | |
836 | |
837 | |
838 char * | |
839 ssh_start_login_sequence (gftp_request * request, int fd) | |
840 { | |
841 size_t rem, len, diff, lastdiff; | |
842 int flags, wrotepw, ok; | |
843 struct timeval tv; | |
844 char *tempstr; | |
845 fd_set rdfds; | |
846 ssize_t rd; | |
847 | |
848 rem = len = 100; | |
849 tempstr = g_malloc0 (len); | |
850 diff = lastdiff = 0; | |
851 wrotepw = 0; | |
852 ok = 1; | |
853 | |
854 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | |
855 { | |
856 g_free (tempstr); | |
857 return (NULL); | |
858 } | |
859 | |
860 if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) < 0) | |
861 { | |
862 g_free (tempstr); | |
863 return (NULL); | |
864 } | |
865 | |
866 while (1) | |
867 { | |
868 FD_ZERO (&rdfds); | |
869 FD_SET (fd, &rdfds); | |
870 tv.tv_sec = 5; | |
871 tv.tv_usec = 0; | |
872 if (select (fd + 1, &rdfds, NULL, NULL, &tv) < 0) | |
873 { | |
874 if (errno == EINTR) | |
875 continue; | |
876 ok = 0; | |
877 break; | |
878 } | |
879 | |
880 if ((rd = read (fd, tempstr + diff, rem - 1)) < 0) | |
881 { | |
882 if (errno == EINTR) | |
883 continue; | |
884 ok = 0; | |
885 break; | |
886 } | |
887 else if (rd == 0) | |
888 { | |
889 ok = 0; | |
890 break; | |
891 } | |
892 tempstr[diff + rd] = '\0'; | |
893 rem -= rd; | |
894 diff += rd; | |
895 if (rem <= 1) | |
896 { | |
897 tempstr = g_realloc (tempstr, len + 100); | |
898 tempstr[diff] = '\0'; | |
899 request->logging_function (gftp_logging_recv, request->user_data, | |
900 "%s", tempstr + lastdiff); | |
901 lastdiff = diff; | |
902 len += 100; | |
903 rem = 100; | |
904 } | |
905 | |
906 if (!wrotepw && | |
907 strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10, | |
908 "password: ") == 0) | |
909 { | |
910 wrotepw = 1; | |
911 write (fd, request->password, strlen (request->password)); | |
912 write (fd, "\n", 1); | |
913 } | |
914 | |
915 else if (!wrotepw && | |
916 (strstr (tempstr, "Enter passphrase for RSA key") != NULL || | |
917 strstr (tempstr, "Enter passphrase for key '") != NULL)) | |
918 { | |
919 wrotepw = 1; | |
920 write (fd, request->password, strlen (request->password)); | |
921 write (fd, "\n", 1); | |
922 } | |
923 else if (strlen (tempstr) >= 5 && | |
924 strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0) | |
925 break; | |
926 } | |
927 | |
928 tempstr[diff] = '\0'; | |
929 request->logging_function (gftp_logging_recv, request->user_data, | |
930 "%s\n", tempstr + lastdiff); | |
931 | |
932 if (ok && fcntl (fd, F_SETFL, flags) < 0) | |
933 ok = 0; | |
934 | |
935 if (!ok) | |
936 { | |
937 g_free (tempstr); | |
938 return (NULL); | |
939 } | |
940 | |
941 return (tempstr); | |
942 } | |
943 | |
944 | |
945 #ifdef G_HAVE_GINT64 | |
946 | |
947 gint64 | |
948 hton64 (gint64 val) | |
949 { | |
950 gint64 num; | |
951 char *pos; | |
952 | |
953 num = 0; | |
954 pos = (char *) # | |
955 pos[0] = (val >> 56) & 0xff; | |
956 pos[1] = (val >> 48) & 0xff; | |
957 pos[2] = (val >> 40) & 0xff; | |
958 pos[3] = (val >> 32) & 0xff; | |
959 pos[4] = (val >> 24) & 0xff; | |
960 pos[5] = (val >> 16) & 0xff; | |
961 pos[6] = (val >> 8) & 0xff; | |
962 pos[7] = val & 0xff; | |
963 return (num); | |
964 } | |
965 | |
966 #endif | |
967 |