Mercurial > gftp.yaz
annotate lib/misc.c @ 58:c01d91c10f6c
2002-11-20 Brian Masney <masneyb@gftp.org>
* lib/protocols.c lib/gftp.h - added gftp_get_line(), gftp_read(),
gftp_write(), gftp_writefmt(), and gftp_set_sockblocking() functions.
Added struct_gftp_getline_buffer for gftp_get_line function()
* lib/cache.c lib/gftp.h lib/local.c lib/misc.c lib/protocols.c
lib/rfc2068.c lib/rfc959.c lib/ssh.c lib/sshv2.c - *_get_file() returns
off_t instead of long. *_{get,put}_next_file_chunk returns ssize_t
instead of size_t. Added *_set_config_options function to gftp_request
structure and protocol files. Use the new network functions
documented above. Convert usage of ANSI C IO (FILE *) to standard BSD
sockets so that I can use timeouts properly with select
* lib/misc.c (ssh_start_login_sequence) - use gftp_set_sockblock(),
gftp_read() and gftp_write() functions
* lib/protocols.c - move some protocol specific code to the protocol
specific files
* lib/local.c - log succesful messages to gftp_logging_misc instead
of gftp_logging_error
* lib/cache.c - log some more error conditions to the user
* lib/rfc959.c - added rfc959_getcwd(). In,
rfc959_accept_active_connection(), set set socket to blocking mode
before calling accept()
* src/text/gftk-text.c - If we get no files in gftp_text_ls(),
return instead of segfaulting
* src/gtk/gftp-gtk.c - expand the port field in the toolbar to be 45
pixels wide
* src/text/gftp-text.c src/gtk/misc-gtk.c src/gtk/transfer.c
src/gtk/view_dialog.c - changes for conversion of request->{sock,data}
from ANSI C IO (FILE *) to standard BSD sockets
author | masneyb |
---|---|
date | Thu, 21 Nov 2002 00:33:51 +0000 |
parents | a12bcbc2fce4 |
children | 618423504fe0 |
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 { | |
417 return (((char *) key)[0] + ((char *) key)[1] + ((char *) key)[2]); | |
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) |
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) | |
466 { | |
467 newfle->file = g_malloc (strlen (fle->file) + 1); | |
468 strcpy (newfle->file, fle->file); | |
469 } | |
470 | |
471 if (fle->user) | |
472 { | |
473 newfle->user = g_malloc (strlen (fle->user) + 1); | |
474 strcpy (newfle->user, fle->user); | |
475 } | |
476 | |
477 if (fle->group) | |
478 { | |
479 newfle->group = g_malloc (strlen (fle->group) + 1); | |
480 strcpy (newfle->group, fle->group); | |
481 } | |
482 | |
483 if (fle->attribs) | |
484 { | |
485 newfle->attribs = g_malloc (strlen (fle->attribs) + 1); | |
486 strcpy (newfle->attribs, fle->attribs); | |
487 } | |
488 | |
489 if (fle->destfile) | |
490 { | |
491 newfle->destfile = g_malloc (strlen (fle->destfile) + 1); | |
492 strcpy (newfle->destfile, fle->destfile); | |
493 } | |
494 return (newfle); | |
495 } | |
496 | |
497 | |
498 void | |
499 swap_socks (gftp_request * dest, gftp_request * source) | |
500 { | |
501 dest->sockfd = source->sockfd; | |
502 dest->datafd = source->datafd; | |
503 dest->cached = 0; | |
504 if (!source->always_connected) | |
505 { | |
58 | 506 source->sockfd = -1; |
507 source->datafd = -1; | |
1 | 508 source->cached = 1; |
509 } | |
510 } | |
511 | |
512 | |
513 int | |
514 compare_request (gftp_request * request1, gftp_request * request2, | |
515 int compare_dirs) | |
516 { | |
517 char *strarr[3][2]; | |
518 int i, ret; | |
519 | |
520 ret = 1; | |
521 if (strcmp (request1->protocol_name, request2->protocol_name) == 0 && | |
522 request1->port == request2->port) | |
523 { | |
524 strarr[0][0] = request1->hostname; | |
525 strarr[0][1] = request2->hostname; | |
526 strarr[1][0] = request1->username; | |
527 strarr[1][1] = request2->username; | |
528 if (compare_dirs) | |
529 { | |
530 strarr[2][0] = request1->directory; | |
531 strarr[2][1] = request2->directory; | |
532 } | |
533 else | |
534 strarr[2][0] = strarr[2][1] = ""; | |
535 | |
536 for (i = 0; i < 3; i++) | |
537 { | |
538 if ((strarr[i][0] && !strarr[i][1]) || | |
539 (!strarr[i][0] && strarr[i][1])) | |
540 { | |
541 ret = 0; | |
542 break; | |
543 } | |
544 | |
545 if (strarr[i][0] && strarr[i][1] && | |
546 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
547 { | |
548 ret = 0; | |
549 break; | |
550 } | |
551 } | |
552 } | |
553 else | |
554 ret = 0; | |
555 return (ret); | |
556 } | |
557 | |
558 | |
559 void | |
560 free_tdata (gftp_transfer * tdata) | |
561 { | |
562 if (tdata->statmutex) | |
563 g_free (tdata->statmutex); | |
564 if (tdata->structmutex) | |
565 g_free (tdata->structmutex); | |
566 if (tdata->fromreq != NULL) | |
567 gftp_request_destroy (tdata->fromreq); | |
568 if (tdata->toreq != NULL) | |
569 gftp_request_destroy (tdata->toreq); | |
570 free_file_list (tdata->files); | |
571 g_free (tdata); | |
572 } | |
573 | |
574 | |
575 gftp_request * | |
576 copy_request (gftp_request * req) | |
577 { | |
578 gftp_request * newreq; | |
579 | |
580 newreq = g_malloc0 (sizeof (*newreq)); | |
581 memcpy (newreq, req, sizeof (*newreq)); | |
582 | |
583 if (req->hostname) | |
56 | 584 newreq->hostname = g_strdup (req->hostname); |
1 | 585 if (req->username) |
56 | 586 newreq->username = g_strdup (req->username); |
1 | 587 if (req->password) |
56 | 588 newreq->password = g_strdup (req->password); |
1 | 589 if (req->account) |
56 | 590 newreq->account = g_strdup (req->account); |
1 | 591 if (req->directory) |
56 | 592 newreq->directory = g_strdup (req->directory); |
1 | 593 |
594 newreq->url_prefix = NULL; | |
595 newreq->protocol_name = NULL; | |
596 newreq->sftpserv_path = NULL; | |
597 newreq->proxy_config = NULL; | |
598 newreq->proxy_hostname = NULL; | |
599 newreq->proxy_username = NULL; | |
600 newreq->proxy_password = NULL; | |
601 newreq->proxy_account = NULL; | |
602 newreq->last_ftp_response = NULL; | |
603 newreq->last_dir_entry = NULL; | |
58 | 604 newreq->sockfd = -1; |
605 newreq->datafd = -1; | |
606 newreq->cachefd = -1; | |
1 | 607 newreq->hostp = NULL; |
608 | |
609 if (req->proxy_config != NULL) | |
56 | 610 newreq->proxy_config = g_strdup (req->proxy_config); |
1 | 611 |
612 req->init (newreq); | |
613 | |
614 return (newreq); | |
615 } | |
616 | |
617 | |
618 int | |
619 ptym_open (char *pts_name) | |
620 { | |
621 int fd; | |
622 | |
623 #ifdef __sgi | |
624 char *tempstr; | |
625 | |
626 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
627 return (-1); | |
628 | |
629 strcpy (pts_name, tempstr); | |
630 return (fd); | |
631 | |
632 #else /* !__sgi */ | |
633 | |
634 #ifdef SYSV | |
635 | |
636 char *tempstr; | |
637 | |
638 strcpy (pts_name, "/dev/ptmx"); | |
639 if ((fd = open (pts_name, O_RDWR)) < 0) | |
640 return (-1); | |
641 | |
642 if (grantpt (fd) < 0) | |
643 { | |
644 close (fd); | |
645 return (-1); | |
646 } | |
647 | |
648 if (unlockpt (fd) < 0) | |
649 { | |
650 close (fd); | |
651 return (-1); | |
652 } | |
653 | |
654 if ((tempstr = ptsname (fd)) == NULL) | |
655 { | |
656 close (fd); | |
657 return (-1); | |
658 } | |
659 | |
660 strcpy (pts_name, tempstr); | |
661 return (fd); | |
662 | |
663 #else /* !SYSV */ | |
664 | |
665 char *pos1, *pos2; | |
666 | |
667 strcpy (pts_name, "/dev/ptyXY"); | |
668 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
669 { | |
670 pts_name[8] = *pos1; | |
671 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
672 { | |
673 pts_name[9] = *pos2; | |
674 if ((fd = open (pts_name, O_RDWR)) < 0) | |
675 continue; | |
676 pts_name[5] = 't'; | |
677 return (fd); | |
678 } | |
679 } | |
680 return (-1); | |
681 | |
682 #endif | |
683 | |
684 #endif | |
685 | |
686 } | |
687 | |
688 | |
689 int | |
690 ptys_open (int fdm, char *pts_name) | |
691 { | |
692 int fds; | |
693 | |
694 #if !defined (SYSV) && !defined (__sgi) | |
695 | |
696 chmod (pts_name, S_IRUSR | S_IWUSR); | |
697 chown (pts_name, getuid (), -1); | |
698 | |
699 #endif | |
700 | |
701 if ((fds = open (pts_name, O_RDWR)) < 0) | |
702 { | |
703 close (fdm); | |
704 return (-1); | |
705 } | |
706 | |
707 #ifdef SYSV | |
708 | |
709 if (ioctl (fds, I_PUSH, "ptem") < 0) | |
710 { | |
711 close (fdm); | |
712 close (fds); | |
713 return (-1); | |
714 } | |
715 | |
716 if (ioctl (fds, I_PUSH, "ldterm") < 0) | |
717 { | |
718 close (fdm); | |
719 close (fds); | |
720 return (-1); | |
721 } | |
722 | |
723 if (ioctl (fds, I_PUSH, "ttcompat") < 0) | |
724 { | |
725 close (fdm); | |
726 close (fds); | |
727 return (-1); | |
728 } | |
729 | |
730 #endif | |
731 | |
732 #if !defined(SYSV) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) | |
733 | |
734 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
735 { | |
736 close (fdm); | |
737 return (-1); | |
738 } | |
739 | |
740 #endif | |
741 | |
742 return (fds); | |
743 } | |
744 | |
745 | |
746 int | |
747 tty_raw (int fd) | |
748 { | |
749 struct termios buf; | |
750 | |
751 if (tcgetattr (fd, &buf) < 0) | |
752 return (-1); | |
753 | |
754 buf.c_iflag |= IGNPAR; | |
755 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
756 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
757 #ifdef IEXTEN | |
758 buf.c_lflag &= ~(IEXTEN); | |
759 #endif | |
760 | |
761 buf.c_oflag &= ~(OPOST); | |
762 buf.c_cc[VMIN] = 1; | |
763 buf.c_cc[VTIME] = 0; | |
764 | |
765 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
766 return (-1); | |
767 return (0); | |
768 } | |
769 | |
770 | |
771 /* We have the caller send us a pointer to a string so we can write the port | |
772 into it. It makes it easier so we don't have to worry about freeing it | |
773 later on, the caller can just send us an auto variable, The string | |
774 should be at least 6 chars. I know this is messy... */ | |
775 | |
776 char ** | |
777 make_ssh_exec_args (gftp_request * request, char *execname, | |
778 int use_sftp_subsys, char *portstring) | |
779 { | |
780 char **args, *oldstr, *tempstr; | |
56 | 781 struct servent serv_struct; |
1 | 782 int i, j; |
783 | |
56 | 784 args = g_malloc (sizeof (char *) * (num_ssh_extra_params + 15)); |
1 | 785 |
786 args[0] = ssh_prog_name != NULL && *ssh_prog_name != '\0' ? | |
787 ssh_prog_name : "ssh"; | |
788 i = 1; | |
56 | 789 tempstr = g_strdup (args[0]); |
1 | 790 |
791 if (ssh_extra_params_list != NULL) | |
792 { | |
793 for (j=0; ssh_extra_params_list[j] != NULL; j++) | |
794 { | |
795 oldstr = tempstr; | |
796 args[i++] = ssh_extra_params_list[j]; | |
797 tempstr = g_strconcat (oldstr, " ", ssh_extra_params_list[j], NULL); | |
798 g_free (oldstr); | |
799 } | |
800 } | |
801 | |
802 oldstr = tempstr; | |
803 tempstr = g_strconcat (oldstr, " -e none", NULL); | |
804 g_free (oldstr); | |
805 args[i++] = "-e"; | |
806 args[i++] = "none"; | |
807 | |
808 if (request->username && *request->username != '\0') | |
809 { | |
810 oldstr = tempstr; | |
811 tempstr = g_strconcat (oldstr, " -l ", request->username, NULL); | |
812 g_free (oldstr); | |
813 args[i++] = "-l"; | |
814 args[i++] = request->username; | |
815 } | |
816 | |
817 if (request->port != 0) | |
818 { | |
819 g_snprintf (portstring, 6, "%d", request->port); | |
820 oldstr = tempstr; | |
821 tempstr = g_strconcat (oldstr, " -p ", portstring, NULL); | |
822 g_free (oldstr); | |
823 args[i++] = "-p"; | |
824 args[i++] = portstring; | |
825 } | |
56 | 826 else |
827 { | |
828 if (!r_getservbyname ("ssh", "tcp", &serv_struct, NULL)) | |
829 request->port = 22; | |
830 else | |
831 request->port = ntohs (serv_struct.s_port); | |
832 } | |
1 | 833 |
834 if (use_sftp_subsys) | |
835 { | |
836 oldstr = tempstr; | |
837 tempstr = g_strconcat (oldstr, " ", request->hostname, " -s sftp", NULL); | |
838 g_free (oldstr); | |
839 args[i++] = request->hostname; | |
840 args[i++] = "-s"; | |
841 args[i++] = "sftp"; | |
842 args[i] = NULL; | |
843 } | |
844 else | |
845 { | |
846 oldstr = tempstr; | |
847 tempstr = g_strconcat (oldstr, " ", request->hostname, " \"", execname, | |
848 "\"", NULL); | |
849 g_free (oldstr); | |
850 args[i++] = request->hostname; | |
851 args[i++] = execname; | |
852 args[i] = NULL; | |
853 } | |
854 | |
855 request->logging_function (gftp_logging_misc, request->user_data, | |
856 _("Running program %s\n"), tempstr); | |
857 g_free (tempstr); | |
858 return (args); | |
859 } | |
860 | |
861 | |
862 char * | |
863 ssh_start_login_sequence (gftp_request * request, int fd) | |
864 { | |
865 size_t rem, len, diff, lastdiff; | |
58 | 866 int wrotepw, ok; |
867 char *tempstr, *pwstr; | |
1 | 868 ssize_t rd; |
869 | |
870 rem = len = 100; | |
871 tempstr = g_malloc0 (len); | |
872 diff = lastdiff = 0; | |
873 wrotepw = 0; | |
874 ok = 1; | |
875 | |
58 | 876 if (gftp_set_sockblocking (request, request->datafd, 1) == -1) |
877 return (NULL); | |
1 | 878 |
58 | 879 pwstr = g_strconcat (request->password, "\n", NULL); |
1 | 880 |
41 | 881 errno = 0; |
1 | 882 while (1) |
883 { | |
58 | 884 if ((rd = gftp_read (request, tempstr + diff, rem -1, fd)) <= 0) |
1 | 885 { |
886 ok = 0; | |
887 break; | |
888 } | |
889 tempstr[diff + rd] = '\0'; | |
890 rem -= rd; | |
891 diff += rd; | |
892 if (rem <= 1) | |
893 { | |
894 tempstr = g_realloc (tempstr, len + 100); | |
895 tempstr[diff] = '\0'; | |
896 request->logging_function (gftp_logging_recv, request->user_data, | |
897 "%s", tempstr + lastdiff); | |
898 lastdiff = diff; | |
899 len += 100; | |
900 rem = 100; | |
901 } | |
902 | |
903 if (!wrotepw && | |
904 strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10, | |
905 "password: ") == 0) | |
906 { | |
907 wrotepw = 1; | |
58 | 908 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
909 { | |
910 ok = 0; | |
911 break; | |
912 } | |
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; | |
58 | 920 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
921 { | |
922 ok = 0; | |
923 break; | |
924 } | |
1 | 925 } |
926 else if (strlen (tempstr) >= 5 && | |
927 strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0) | |
928 break; | |
929 } | |
930 | |
58 | 931 g_free (pwstr); |
1 | 932 tempstr[diff] = '\0'; |
933 request->logging_function (gftp_logging_recv, request->user_data, | |
934 "%s\n", tempstr + lastdiff); | |
935 | |
936 if (!ok) | |
937 { | |
938 g_free (tempstr); | |
939 return (NULL); | |
940 } | |
941 | |
942 return (tempstr); | |
943 } | |
944 | |
945 | |
946 #ifdef G_HAVE_GINT64 | |
947 | |
948 gint64 | |
949 hton64 (gint64 val) | |
950 { | |
951 gint64 num; | |
952 char *pos; | |
953 | |
954 num = 0; | |
955 pos = (char *) # | |
956 pos[0] = (val >> 56) & 0xff; | |
957 pos[1] = (val >> 48) & 0xff; | |
958 pos[2] = (val >> 40) & 0xff; | |
959 pos[3] = (val >> 32) & 0xff; | |
960 pos[4] = (val >> 24) & 0xff; | |
961 pos[5] = (val >> 16) & 0xff; | |
962 pos[6] = (val >> 8) & 0xff; | |
963 pos[7] = val & 0xff; | |
964 return (num); | |
965 } | |
966 | |
967 #endif | |
968 | |
16 | 969 |
970 static gint | |
971 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
972 { | |
973 const gftp_file * f1, * f2; | |
974 | |
975 f1 = a; | |
976 f2 = b; | |
977 return (strcmp (f1->file, f2->file)); | |
978 } | |
979 | |
980 | |
981 static gint | |
982 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
983 { | |
984 const gftp_file * f1, * f2; | |
985 gint ret; | |
986 | |
987 f1 = a; | |
988 f2 = b; | |
989 ret = strcmp (f1->file, f2->file); | |
990 if (ret < 0) | |
991 ret = 1; | |
992 else if (ret > 0) | |
993 ret = -1; | |
994 return (ret); | |
995 } | |
996 | |
997 | |
998 static gint | |
999 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
1000 { | |
1001 const gftp_file * f1, * f2; | |
1002 | |
1003 f1 = a; | |
1004 f2 = b; | |
1005 return (strcmp (f1->user, f2->user)); | |
1006 } | |
1007 | |
1008 | |
1009 static gint | |
1010 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
1011 { | |
1012 const gftp_file * f1, * f2; | |
1013 gint ret; | |
1014 | |
1015 f1 = a; | |
1016 f2 = b; | |
1017 ret = strcmp (f1->user, f2->user); | |
1018 if (ret < 0) | |
1019 ret = 1; | |
1020 else if (ret > 0) | |
1021 ret = -1; | |
1022 return (ret); | |
1023 } | |
1024 | |
1025 | |
1026 static gint | |
1027 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
1028 { | |
1029 const gftp_file * f1, * f2; | |
1030 | |
1031 f1 = a; | |
1032 f2 = b; | |
1033 return (strcmp (f1->group, f2->group)); | |
1034 } | |
1035 | |
1036 | |
1037 static gint | |
1038 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
1039 { | |
1040 const gftp_file * f1, * f2; | |
1041 gint ret; | |
1042 | |
1043 f1 = a; | |
1044 f2 = b; | |
1045 ret = strcmp (f1->group, f2->group); | |
1046 if (ret < 0) | |
1047 ret = 1; | |
1048 else if (ret > 0) | |
1049 ret = -1; | |
1050 return (ret); | |
1051 } | |
1052 | |
1053 | |
1054 static gint | |
1055 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
1056 { | |
1057 const gftp_file * f1, * f2; | |
1058 | |
1059 f1 = a; | |
1060 f2 = b; | |
1061 return (strcmp (f1->attribs, f2->attribs)); | |
1062 } | |
1063 | |
1064 | |
1065 static gint | |
1066 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
1067 { | |
1068 const gftp_file * f1, * f2; | |
1069 gint ret; | |
1070 | |
1071 f1 = a; | |
1072 f2 = b; | |
1073 ret = strcmp (f1->attribs, f2->attribs); | |
1074 if (ret < 0) | |
1075 ret = 1; | |
1076 else if (ret > 0) | |
1077 ret = -1; | |
1078 return (ret); | |
1079 } | |
1080 | |
1081 | |
1082 static gint | |
1083 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
1084 { | |
1085 const gftp_file * f1, * f2; | |
1086 | |
1087 f1 = a; | |
1088 f2 = b; | |
1089 if (f1->size < f2->size) | |
1090 return (-1); | |
1091 else if (f1->size == f2->size) | |
1092 return (0); | |
1093 else | |
1094 return (1); | |
1095 } | |
1096 | |
1097 | |
1098 static gint | |
1099 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
1100 { | |
1101 const gftp_file * f1, * f2; | |
1102 | |
1103 f1 = a; | |
1104 f2 = b; | |
1105 if (f1->size < f2->size) | |
1106 return (1); | |
1107 else if (f1->size == f2->size) | |
1108 return (0); | |
1109 else | |
1110 return (-1); | |
1111 } | |
1112 | |
1113 | |
1114 static gint | |
1115 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
1116 { | |
1117 const gftp_file * f1, * f2; | |
1118 | |
1119 f1 = a; | |
1120 f2 = b; | |
1121 if (f1->datetime < f2->datetime) | |
1122 return (-1); | |
1123 else if (f1->datetime == f2->datetime) | |
1124 return (0); | |
1125 else | |
1126 return (1); | |
1127 } | |
1128 | |
1129 | |
1130 static gint | |
1131 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
1132 { | |
1133 const gftp_file * f1, * f2; | |
1134 | |
1135 f1 = a; | |
1136 f2 = b; | |
1137 if (f1->datetime < f2->datetime) | |
1138 return (1); | |
1139 else if (f1->datetime == f2->datetime) | |
1140 return (0); | |
1141 else | |
1142 return (-1); | |
1143 } | |
1144 | |
1145 | |
1146 GList * | |
1147 gftp_sort_filelist (GList * filelist, int column, int asds) | |
1148 { | |
1149 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
1150 GCompareFunc sortfunc; | |
1151 gftp_file * tempfle; | |
1152 | |
1153 files = dirs = dotdot = NULL; | |
1154 | |
1155 if (column == GFTP_SORT_COL_FILE) | |
1156 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
1157 else if (column == GFTP_SORT_COL_SIZE) | |
1158 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
1159 else if (column == GFTP_SORT_COL_USER) | |
1160 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
1161 else if (column == GFTP_SORT_COL_GROUP) | |
1162 sortfunc = asds ? | |
1163 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
1164 else if (column == GFTP_SORT_COL_DATETIME) | |
1165 sortfunc = asds ? | |
1166 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; else /* GFTP_SORT_COL_ATTRIBS */ | |
1167 sortfunc = asds ? | |
1168 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
1169 | |
1170 for (tempitem = filelist; tempitem != NULL; ) | |
1171 { | |
1172 tempfle = tempitem->data; | |
1173 insitem = tempitem; | |
1174 tempitem = tempitem->next; | |
1175 insitem->next = NULL; | |
1176 | |
1177 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
1178 dotdot = insitem; | |
1179 else if (sort_dirs_first && tempfle->isdir) | |
1180 { | |
1181 insitem->next = dirs; | |
1182 dirs = insitem; | |
1183 } | |
1184 else | |
1185 { | |
1186 insitem->next = files; | |
1187 files = insitem; | |
1188 } | |
1189 } | |
1190 | |
1191 if (dirs != NULL) | |
1192 dirs = g_list_sort (dirs, sortfunc); | |
1193 if (files != NULL) | |
1194 files = g_list_sort (files, sortfunc); | |
1195 | |
1196 filelist = dotdot; | |
1197 | |
1198 if (filelist == NULL) | |
1199 filelist = dirs; | |
1200 else | |
1201 filelist = g_list_concat (filelist, dirs); | |
1202 | |
1203 if (filelist == NULL) | |
1204 filelist = files; | |
1205 else | |
1206 filelist = g_list_concat (filelist, files); | |
1207 | |
39 | 1208 /* I haven't check this, but I'm pretty sure some older versions of glib |
1209 had a bug that the prev pointer wasn't being sent to NULL */ | |
1210 filelist->prev = NULL; | |
16 | 1211 return (filelist); |
1212 } | |
1213 |