1
|
1 /*****************************************************************************/
|
|
2 /* sshv2.c - functions that will use the sshv2 protocol */
|
|
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"
|
33
|
21 static const char cvsid[] = "$Id$";
|
1
|
22
|
|
23 #define SSH_MAX_HANDLE_SIZE 256
|
|
24 #define SSH_MAX_STRING_SIZE 34000
|
|
25
|
|
26 typedef struct sshv2_attribs_tag
|
|
27 {
|
|
28 gint32 flags;
|
|
29 gint64 size;
|
|
30 gint32 uid;
|
|
31 gint32 gid;
|
|
32 gint32 perm;
|
|
33 gint32 atime;
|
|
34 gint32 mtime;
|
|
35 } sshv2_attribs;
|
|
36
|
|
37 typedef struct sshv2_message_tag
|
|
38 {
|
|
39 gint32 length;
|
|
40 char command;
|
|
41 char *buffer,
|
|
42 *pos,
|
|
43 *end;
|
|
44 } sshv2_message;
|
|
45
|
|
46 typedef struct sshv2_params_tag
|
|
47 {
|
|
48 char handle[SSH_MAX_HANDLE_SIZE + 4]; /* We'll encode the ID in here too */
|
|
49 int handle_len,
|
|
50 dont_log_status : 1; /* For uploading files */
|
|
51 sshv2_message message;
|
|
52
|
|
53 gint32 id,
|
|
54 count;
|
|
55 #ifdef G_HAVE_GINT64
|
|
56 gint64 offset;
|
|
57 #else
|
|
58 gint32 offset;
|
|
59 #endif
|
|
60 char *read_buffer;
|
|
61 } sshv2_params;
|
|
62
|
|
63
|
|
64 #define SSH_MY_VERSION 3
|
|
65
|
|
66 #define SSH_FXP_INIT 1
|
|
67 #define SSH_FXP_VERSION 2
|
|
68 #define SSH_FXP_OPEN 3
|
|
69 #define SSH_FXP_CLOSE 4
|
|
70 #define SSH_FXP_READ 5
|
|
71 #define SSH_FXP_WRITE 6
|
|
72 #define SSH_FXP_LSTAT 7
|
|
73 #define SSH_FXP_FSTAT 8
|
|
74 #define SSH_FXP_SETSTAT 9
|
|
75 #define SSH_FXP_FSETSTAT 10
|
|
76 #define SSH_FXP_OPENDIR 11
|
|
77 #define SSH_FXP_READDIR 12
|
|
78 #define SSH_FXP_REMOVE 13
|
|
79 #define SSH_FXP_MKDIR 14
|
|
80 #define SSH_FXP_RMDIR 15
|
|
81 #define SSH_FXP_REALPATH 16
|
|
82 #define SSH_FXP_STAT 17
|
|
83 #define SSH_FXP_RENAME 18
|
|
84 #define SSH_FXP_STATUS 101
|
|
85 #define SSH_FXP_HANDLE 102
|
|
86 #define SSH_FXP_DATA 103
|
|
87 #define SSH_FXP_NAME 104
|
|
88 #define SSH_FXP_ATTRS 105
|
|
89 #define SSH_FXP_EXTENDED 200
|
|
90 #define SSH_FXP_EXTENDED_REPLY 201
|
|
91
|
|
92 #define SSH_FILEXFER_ATTR_SIZE 0x00000001
|
|
93 #define SSH_FILEXFER_ATTR_UIDGID 0x00000002
|
|
94 #define SSH_FILEXFER_ATTR_PERMISSIONS 0x00000004
|
|
95 #define SSH_FILEXFER_ATTR_ACMODTIME 0x00000008
|
|
96 #define SSH_FILEXFER_ATTR_EXTENDED 0x80000000
|
|
97
|
|
98 #define SSH_FXF_READ 0x00000001
|
|
99 #define SSH_FXF_WRITE 0x00000002
|
|
100 #define SSH_FXF_APPEND 0x00000004
|
|
101 #define SSH_FXF_CREAT 0x00000008
|
|
102 #define SSH_FXF_TRUNC 0x00000010
|
|
103 #define SSH_FXF_EXCL 0x00000020
|
|
104
|
|
105 #define SSH_FX_OK 0
|
|
106 #define SSH_FX_EOF 1
|
|
107 #define SSH_FX_NO_SUCH_FILE 2
|
|
108 #define SSH_FX_PERMISSION_DENIED 3
|
|
109 #define SSH_FX_FAILURE 4
|
|
110 #define SSH_FX_BAD_MESSAGE 5
|
|
111 #define SSH_FX_NO_CONNECTION 6
|
|
112 #define SSH_FX_CONNECTION_LOST 7
|
|
113 #define SSH_FX_OP_UNSUPPORTED 8
|
|
114
|
|
115
|
48
|
116 static void
|
|
117 sshv2_log_command (gftp_request * request, gftp_logging_level level,
|
|
118 char type, char *message, size_t length)
|
1
|
119 {
|
48
|
120 gint32 id, num, attr, stattype;
|
|
121 char *descr, *pos, oldchar;
|
1
|
122 sshv2_params * params;
|
|
123
|
48
|
124 params = request->protocol_data;
|
|
125 memcpy (&id, message, 4);
|
|
126 id = ntohl (id);
|
|
127 switch (type)
|
|
128 {
|
|
129 case SSH_FXP_INIT:
|
|
130 request->logging_function (level, request->user_data,
|
|
131 _("%d: Protocol Initialization\n"), id);
|
|
132 break;
|
|
133 case SSH_FXP_VERSION:
|
|
134 memcpy (&num, message, 4);
|
|
135 num = ntohl (num);
|
|
136 request->logging_function (level, request->user_data,
|
|
137 _("%d: Protocol version %d\n"), id, num);
|
|
138 break;
|
|
139 case SSH_FXP_OPEN:
|
|
140 memcpy (&num, message + 4, 4);
|
|
141 num = ntohl (num);
|
|
142 pos = message + 12 + num - 1;
|
|
143 oldchar = *pos;
|
|
144 *pos = '\0';
|
|
145 request->logging_function (level, request->user_data,
|
|
146 _("%d: Open %s\n"), id, message + 8);
|
|
147 *pos = oldchar;
|
|
148 break;
|
|
149 case SSH_FXP_CLOSE:
|
|
150 request->logging_function (level, request->user_data,
|
|
151 _("%d: Close\n"), id);
|
|
152 case SSH_FXP_READ:
|
|
153 case SSH_FXP_WRITE:
|
|
154 break;
|
|
155 case SSH_FXP_OPENDIR:
|
|
156 request->logging_function (level, request->user_data,
|
|
157 _("%d: Open Directory %s\n"), id,
|
|
158 message + 8);
|
|
159 break;
|
|
160 case SSH_FXP_READDIR:
|
|
161 request->logging_function (level, request->user_data,
|
|
162 _("%d: Read Directory\n"), id);
|
|
163 break;
|
|
164 case SSH_FXP_REMOVE:
|
|
165 request->logging_function (level, request->user_data,
|
|
166 _("%d: Remove file %s\n"), id,
|
|
167 message + 8);
|
|
168 break;
|
|
169 case SSH_FXP_MKDIR:
|
|
170 request->logging_function (level, request->user_data,
|
|
171 _("%d: Make directory %s\n"), id,
|
|
172 message + 8);
|
|
173 break;
|
|
174 case SSH_FXP_RMDIR:
|
|
175 request->logging_function (level, request->user_data,
|
|
176 _("%d: Remove directory %s\n"), id,
|
|
177 message + 8);
|
|
178 break;
|
|
179 case SSH_FXP_REALPATH:
|
|
180 request->logging_function (level, request->user_data,
|
|
181 _("%d: Realpath %s\n"), id,
|
|
182 message + 8);
|
|
183 break;
|
|
184 case SSH_FXP_ATTRS:
|
|
185 request->logging_function (level, request->user_data,
|
|
186 _("%d: File attributes\n"), id);
|
|
187 break;
|
|
188 case SSH_FXP_STAT:
|
|
189 request->logging_function (level, request->user_data,
|
|
190 _("%d: Stat %s\n"), id,
|
|
191 message + 8);
|
|
192 break;
|
|
193 case SSH_FXP_SETSTAT:
|
|
194 memcpy (&num, message + 4, 4);
|
|
195 num = ntohl (num);
|
|
196 pos = message + 12 + num - 1;
|
|
197 oldchar = *pos;
|
|
198 *pos = '\0';
|
|
199 memcpy (&stattype, message + 8 + num, 4);
|
|
200 stattype = ntohl (stattype);
|
|
201 memcpy (&attr, message + 12 + num, 4);
|
|
202 attr = ntohl (attr);
|
|
203 switch (stattype)
|
|
204 {
|
|
205 case SSH_FILEXFER_ATTR_PERMISSIONS:
|
|
206 request->logging_function (level, request->user_data,
|
|
207 _("%d: Chmod %s %o\n"), id,
|
|
208 message + 8, attr);
|
|
209 break;
|
|
210 case SSH_FILEXFER_ATTR_ACMODTIME:
|
|
211 request->logging_function (level, request->user_data,
|
|
212 _("%d: Utime %s %d\n"), id,
|
|
213 message + 8, attr);
|
|
214 }
|
|
215 *pos = oldchar;
|
|
216 break;
|
|
217 case SSH_FXP_STATUS:
|
|
218 if (params->dont_log_status)
|
|
219 break;
|
|
220 memcpy (&num, message + 4, 4);
|
|
221 num = ntohl (num);
|
|
222 switch (num)
|
|
223 {
|
|
224 case SSH_FX_OK:
|
|
225 descr = _("OK");
|
|
226 break;
|
|
227 case SSH_FX_EOF:
|
|
228 descr = _("EOF");
|
|
229 break;
|
|
230 case SSH_FX_NO_SUCH_FILE:
|
|
231 descr = _("No such file or directory");
|
|
232 break;
|
|
233 case SSH_FX_PERMISSION_DENIED:
|
|
234 descr = _("Permission denied");
|
|
235 break;
|
|
236 case SSH_FX_FAILURE:
|
|
237 descr = _("Failure");
|
|
238 break;
|
|
239 case SSH_FX_BAD_MESSAGE:
|
|
240 descr = _("Bad message");
|
|
241 break;
|
|
242 case SSH_FX_NO_CONNECTION:
|
|
243 descr = _("No connection");
|
|
244 break;
|
|
245 case SSH_FX_CONNECTION_LOST:
|
|
246 descr = _("Connection lost");
|
|
247 break;
|
|
248 case SSH_FX_OP_UNSUPPORTED:
|
|
249 descr = _("Operation unsupported");
|
|
250 break;
|
|
251 default:
|
|
252 descr = _("Unknown message returned from server");
|
|
253 break;
|
|
254 }
|
|
255 request->logging_function (level, request->user_data,
|
|
256 "%d: %s\n", id, descr);
|
|
257 break;
|
|
258 case SSH_FXP_HANDLE:
|
|
259 request->logging_function (level, request->user_data,
|
|
260 "%d: File handle\n", id);
|
|
261 break;
|
|
262 case SSH_FXP_DATA:
|
|
263 break;
|
|
264 case SSH_FXP_NAME:
|
|
265 memcpy (&num, message + 4, 4);
|
|
266 num = ntohl (num);
|
|
267 request->logging_function (level, request->user_data,
|
|
268 "%d: Filenames (%d entries)\n", id,
|
|
269 num);
|
|
270 break;
|
|
271 default:
|
|
272 request->logging_function (level, request->user_data,
|
|
273 "Command: %x\n", type);
|
|
274 }
|
|
275 }
|
1
|
276
|
48
|
277
|
|
278 static int
|
|
279 sshv2_send_command (gftp_request * request, char type, char *command,
|
|
280 gint32 len)
|
|
281 {
|
|
282 char buf[34000];
|
|
283 gint32 clen;
|
|
284
|
|
285 if (len > 33995)
|
|
286 {
|
|
287 request->logging_function (gftp_logging_error, request->user_data,
|
|
288 _("Error: Message size %d too big\n"), len);
|
|
289 gftp_disconnect (request);
|
|
290 return (-1);
|
|
291 }
|
|
292
|
|
293 clen = htonl (len + 1);
|
|
294 memcpy (buf, &clen, 4);
|
|
295 buf[4] = type;
|
|
296 memcpy (&buf[5], command, len);
|
|
297 buf[len + 5] = '\0';
|
|
298
|
|
299 #ifdef DEBUG
|
|
300 printf ("\rSending: ");
|
61
|
301 for (clen=0; clen<len + 5; clen++)
|
|
302 printf ("%x ", buf[clen] & 0xff);
|
48
|
303 printf ("\n");
|
|
304 #endif
|
|
305
|
|
306 sshv2_log_command (request, gftp_logging_send, type, buf + 5, len);
|
|
307
|
58
|
308 if (gftp_write (request, buf, len + 5, request->sockfd) < 0)
|
48
|
309 return (-2);
|
|
310
|
|
311 return 0;
|
|
312 }
|
|
313
|
|
314
|
|
315 static int
|
|
316 sshv2_read_response (gftp_request * request, sshv2_message * message,
|
58
|
317 int fd)
|
48
|
318 {
|
61
|
319 ssize_t numread, rem;
|
|
320 char buf[5], *pos;
|
48
|
321
|
58
|
322 if (fd <= 0)
|
48
|
323 fd = request->sockfd;
|
1
|
324
|
61
|
325 pos = buf;
|
|
326 rem = 5;
|
|
327 while (rem > 0)
|
|
328 {
|
|
329 if ((numread = gftp_read (request, pos, rem, fd)) < 0)
|
|
330 return (-2);
|
|
331 rem -= numread;
|
|
332 pos += numread;
|
|
333 }
|
48
|
334
|
|
335 memcpy (&message->length, buf, 4);
|
|
336 message->length = ntohl (message->length);
|
|
337 if (message->length > 34000)
|
|
338 {
|
|
339 request->logging_function (gftp_logging_error, request->user_data,
|
|
340 _("Error: Message size %d too big from server\n"),
|
|
341 message->length);
|
|
342 memset (message, 0, sizeof (*message));
|
|
343 gftp_disconnect (request);
|
|
344 return (-1);
|
|
345 }
|
58
|
346
|
48
|
347 message->command = buf[4];
|
61
|
348 message->buffer = g_malloc (message->length + 1);
|
48
|
349
|
|
350 message->pos = message->buffer;
|
|
351 message->end = message->buffer + message->length - 1;
|
|
352
|
61
|
353 pos = message->buffer;
|
|
354 rem = message->length - 1;
|
|
355 while (rem > 0)
|
|
356 {
|
|
357 if ((numread = gftp_read (request, pos, rem, fd)) < 0)
|
|
358 return (-2);
|
|
359 rem -= numread;
|
|
360 pos += numread;
|
|
361 }
|
48
|
362
|
61
|
363 message->buffer[message->length] = '\0';
|
48
|
364
|
|
365 sshv2_log_command (request, gftp_logging_recv, message->command,
|
|
366 message->buffer, message->length);
|
|
367
|
|
368 return (message->command);
|
1
|
369 }
|
|
370
|
|
371
|
|
372 static void
|
|
373 sshv2_destroy (gftp_request * request)
|
|
374 {
|
|
375 g_return_if_fail (request != NULL);
|
|
376 g_return_if_fail (request->protonum == GFTP_SSHV2_NUM);
|
|
377
|
|
378 g_free (request->protocol_data);
|
|
379 request->protocol_data = NULL;
|
|
380 }
|
|
381
|
|
382
|
48
|
383 static void
|
|
384 sshv2_message_free (sshv2_message * message)
|
|
385 {
|
|
386 if (message->buffer)
|
|
387 g_free (message->buffer);
|
|
388 memset (message, 0, sizeof (*message));
|
|
389 }
|
|
390
|
|
391
|
|
392 static gint32
|
|
393 sshv2_buffer_get_int32 (gftp_request * request, sshv2_message * message,
|
|
394 int expected_response)
|
|
395 {
|
|
396 gint32 ret;
|
|
397
|
|
398 if (message->end - message->pos < 4)
|
|
399 {
|
|
400 request->logging_function (gftp_logging_error, request->user_data,
|
|
401 _("Received wrong response from server, disconnecting\n"));
|
|
402 sshv2_message_free (message);
|
|
403 gftp_disconnect (request);
|
|
404 return (-2);
|
|
405 }
|
|
406
|
|
407 memcpy (&ret, message->pos, 4);
|
|
408 ret = ntohl (ret);
|
|
409 message->pos += 4;
|
|
410
|
|
411 if (expected_response > 0 && ret != expected_response)
|
|
412 {
|
|
413 request->logging_function (gftp_logging_error, request->user_data,
|
|
414 _("Received wrong response from server, disconnecting\n"));
|
|
415 sshv2_message_free (message);
|
|
416 gftp_disconnect (request);
|
|
417 return (-2);
|
|
418 }
|
|
419
|
|
420 return (ret);
|
|
421 }
|
|
422
|
|
423
|
|
424 static char *
|
|
425 sshv2_buffer_get_string (gftp_request * request, sshv2_message * message)
|
|
426 {
|
|
427 char *string;
|
|
428 gint32 len;
|
|
429
|
|
430 if ((len = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
431 return (NULL);
|
|
432
|
|
433 if (len > SSH_MAX_STRING_SIZE || (message->end - message->pos < len))
|
|
434 {
|
|
435 request->logging_function (gftp_logging_error, request->user_data,
|
|
436 _("Received wrong response from server, disconnecting\n"));
|
|
437 sshv2_message_free (message);
|
|
438 gftp_disconnect (request);
|
|
439 return (NULL);
|
|
440 }
|
|
441
|
|
442 string = g_malloc (len + 1);
|
|
443 memcpy (string, message->pos, len);
|
|
444 string[len] = '\0';
|
|
445 message->pos += len;
|
|
446 return (string);
|
|
447 }
|
|
448
|
|
449
|
|
450 static int
|
|
451 sshv2_getcwd (gftp_request * request)
|
|
452 {
|
|
453 sshv2_message message;
|
|
454 sshv2_params * params;
|
|
455 char *tempstr, *dir;
|
|
456 gint32 num;
|
|
457 size_t len;
|
64
|
458 int ret;
|
48
|
459
|
|
460 g_return_val_if_fail (request != NULL, -2);
|
|
461 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
462
|
|
463 if (request->directory == NULL || *request->directory == '\0')
|
|
464 dir = ".";
|
|
465 else
|
|
466 dir = request->directory;
|
|
467
|
|
468 params = request->protocol_data;
|
|
469 len = strlen (dir);
|
|
470 tempstr = g_malloc (len + 9);
|
|
471 strcpy (tempstr + 8, dir);
|
|
472
|
|
473 num = htonl (params->id++);
|
|
474 memcpy (tempstr, &num, 4);
|
|
475
|
|
476 num = htonl (len);
|
|
477 memcpy (tempstr + 4, &num, 4);
|
|
478 if (sshv2_send_command (request, SSH_FXP_REALPATH, tempstr, len + 8) < 0)
|
|
479 {
|
|
480 g_free (tempstr);
|
|
481 return (-2);
|
|
482 }
|
|
483
|
|
484 g_free (tempstr);
|
|
485 if (request->directory)
|
|
486 {
|
|
487 g_free (request->directory);
|
|
488 request->directory = NULL;
|
|
489 }
|
|
490
|
|
491 memset (&message, 0, sizeof (message));
|
64
|
492 ret = sshv2_read_response (request, &message, -1);
|
|
493 if (ret == SSH_FXP_STATUS)
|
|
494 {
|
|
495 sshv2_message_free (&message);
|
|
496 return (-2);
|
|
497 }
|
|
498 else if (ret != SSH_FXP_NAME)
|
48
|
499 {
|
|
500 request->logging_function (gftp_logging_error, request->user_data,
|
|
501 _("Received wrong response from server, disconnecting\n"));
|
|
502 sshv2_message_free (&message);
|
|
503 gftp_disconnect (request);
|
|
504 return (-2);
|
|
505 }
|
|
506
|
|
507 message.pos += 4;
|
|
508 if (sshv2_buffer_get_int32 (request, &message, 1) < 0)
|
|
509 return (-2);
|
|
510
|
|
511 if ((request->directory = sshv2_buffer_get_string (request, &message)) == NULL)
|
|
512 return (-2);
|
64
|
513
|
48
|
514 sshv2_message_free (&message);
|
|
515 return (0);
|
|
516 }
|
|
517
|
|
518
|
1
|
519 static int
|
|
520 sshv2_connect (gftp_request * request)
|
|
521 {
|
|
522 char **args, *tempstr, pts_name[20], *p1, p2, *exepath, port[6];
|
|
523 int version, fdm, fds, s[2];
|
|
524 sshv2_message message;
|
|
525 pid_t child;
|
|
526
|
|
527 g_return_val_if_fail (request != NULL, -2);
|
|
528 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
529 g_return_val_if_fail (request->hostname != NULL, -2);
|
|
530
|
58
|
531 if (request->sockfd > 0)
|
1
|
532 return (0);
|
|
533
|
|
534 request->logging_function (gftp_logging_misc, request->user_data,
|
|
535 _("Opening SSH connection to %s\n"),
|
|
536 request->hostname);
|
|
537
|
|
538 /* Ugh!! We don't get a login banner from sftp-server, and if we are
|
|
539 using ssh-agent to cache a users password, then we won't receive
|
|
540 any initial text from the server, and we'll block. So I just send a
|
|
541 xsftp server banner over. I hope this works on most Unices */
|
|
542
|
|
543 if (request->sftpserv_path == NULL ||
|
|
544 *request->sftpserv_path == '\0')
|
|
545 {
|
|
546 p1 = "";
|
|
547 p2 = ' ';
|
|
548 }
|
|
549 else
|
|
550 {
|
|
551 p1 = request->sftpserv_path;
|
|
552 p2 = '/';
|
|
553 }
|
|
554
|
|
555 *port = '\0';
|
|
556 exepath = g_strdup_printf ("echo -n xsftp ; %s%csftp-server", p1, p2);
|
|
557 args = make_ssh_exec_args (request, exepath, sshv2_use_sftp_subsys, port);
|
|
558
|
|
559 if (ssh_use_askpass || sshv2_use_sftp_subsys)
|
|
560 {
|
|
561 fdm = fds = 0;
|
|
562 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0)
|
|
563 {
|
|
564 request->logging_function (gftp_logging_error, request->user_data,
|
|
565 _("Cannot create a socket pair: %s\n"),
|
|
566 g_strerror (errno));
|
|
567 return (-2);
|
|
568 }
|
|
569 }
|
|
570 else
|
|
571 {
|
|
572 s[0] = s[1] = 0;
|
|
573 if ((fdm = ptym_open (pts_name)) < 0)
|
|
574 {
|
|
575 request->logging_function (gftp_logging_error, request->user_data,
|
|
576 _("Cannot open master pty %s: %s\n"), pts_name,
|
|
577 g_strerror (errno));
|
|
578 return (-2);
|
|
579 }
|
|
580 }
|
|
581
|
|
582 if ((child = fork ()) == 0)
|
|
583 {
|
|
584 setsid ();
|
|
585 if (ssh_use_askpass || sshv2_use_sftp_subsys)
|
|
586 {
|
|
587 close (s[0]);
|
|
588 fds = s[1];
|
|
589 }
|
|
590 else
|
|
591 {
|
|
592 if ((fds = ptys_open (fdm, pts_name)) < 0)
|
|
593 {
|
|
594 printf ("Cannot open slave pts %s: %s\n", pts_name,
|
|
595 g_strerror (errno));
|
|
596 return (-1);
|
|
597 }
|
|
598 close (fdm);
|
|
599 }
|
|
600
|
|
601 tty_raw (fds);
|
|
602 dup2 (fds, 0);
|
|
603 dup2 (fds, 1);
|
|
604 dup2 (fds, 2);
|
|
605 if (!ssh_use_askpass && fds > 2)
|
|
606 close (fds);
|
|
607 execvp (ssh_prog_name != NULL && *ssh_prog_name != '\0' ?
|
|
608 ssh_prog_name : "ssh", args);
|
|
609
|
58
|
610 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
|
1
|
611 return (-1);
|
|
612 }
|
|
613 else if (child > 0)
|
|
614 {
|
|
615 if (ssh_use_askpass || sshv2_use_sftp_subsys)
|
|
616 {
|
|
617 close (s[1]);
|
|
618 fdm = s[0];
|
|
619 }
|
|
620 tty_raw (fdm);
|
|
621 if (!sshv2_use_sftp_subsys)
|
|
622 {
|
|
623 tempstr = ssh_start_login_sequence (request, fdm);
|
|
624 if (!tempstr ||
|
|
625 !(strlen (tempstr) > 4 && strcmp (tempstr + strlen (tempstr) - 5,
|
|
626 "xsftp") == 0))
|
|
627 {
|
|
628 g_free (args);
|
|
629 g_free (exepath);
|
|
630 return (-2);
|
|
631 }
|
|
632 g_free (tempstr);
|
|
633 }
|
|
634 g_free (args);
|
|
635 g_free (exepath);
|
|
636
|
58
|
637 request->sockfd = fdm;
|
1
|
638
|
|
639 version = htonl (SSH_MY_VERSION);
|
|
640 if (sshv2_send_command (request, SSH_FXP_INIT, (char *) &version, 4) < 0)
|
|
641 return (-2);
|
|
642
|
|
643 memset (&message, 0, sizeof (message));
|
58
|
644 if (sshv2_read_response (request, &message, -1) != SSH_FXP_VERSION)
|
1
|
645 {
|
|
646 request->logging_function (gftp_logging_error, request->user_data,
|
|
647 _("Received wrong response from server, disconnecting\n"));
|
|
648 sshv2_message_free (&message);
|
|
649 gftp_disconnect (request);
|
|
650 return (-2);
|
|
651 }
|
|
652 sshv2_message_free (&message);
|
|
653
|
|
654 request->logging_function (gftp_logging_misc, request->user_data,
|
|
655 _("Successfully logged into SSH server %s\n"),
|
|
656 request->hostname);
|
|
657 }
|
|
658 else
|
|
659 {
|
|
660 request->logging_function (gftp_logging_error, request->user_data,
|
|
661 _("Cannot fork another process: %s\n"),
|
|
662 g_strerror (errno));
|
|
663 g_free (args);
|
|
664 return (-1);
|
|
665 }
|
|
666
|
64
|
667 if (sshv2_getcwd (request) < 0)
|
|
668 {
|
|
669 if (request->directory)
|
|
670 g_free (request->directory);
|
|
671
|
|
672 request->directory = g_strdup (".");
|
|
673 if (sshv2_getcwd (request) < 0)
|
|
674 {
|
|
675 gftp_disconnect (request);
|
|
676 return (-2);
|
|
677 }
|
|
678 }
|
1
|
679
|
|
680 return (0);
|
|
681 }
|
|
682
|
|
683
|
|
684 static void
|
|
685 sshv2_disconnect (gftp_request * request)
|
|
686 {
|
|
687 sshv2_params * params;
|
|
688
|
|
689 g_return_if_fail (request != NULL);
|
|
690 g_return_if_fail (request->protonum == GFTP_SSHV2_NUM);
|
|
691
|
|
692 params = request->protocol_data;
|
|
693
|
58
|
694 if (request->sockfd > 0)
|
1
|
695 {
|
|
696 request->logging_function (gftp_logging_misc, request->user_data,
|
|
697 _("Disconnecting from site %s\n"),
|
|
698 request->hostname);
|
58
|
699
|
|
700 if (close (request->sockfd) < 0)
|
|
701 request->logging_function (gftp_logging_error, request->user_data,
|
|
702 _("Error closing file descriptor: %s\n"),
|
|
703 g_strerror (errno));
|
|
704
|
|
705 request->sockfd = -1;
|
1
|
706 }
|
|
707
|
|
708 if (params->message.buffer != NULL)
|
|
709 sshv2_message_free (¶ms->message);
|
|
710 }
|
|
711
|
|
712
|
|
713 static int
|
|
714 sshv2_end_transfer (gftp_request * request)
|
|
715 {
|
|
716 sshv2_params * params;
|
|
717 sshv2_message message;
|
|
718 gint32 len;
|
|
719
|
|
720 g_return_val_if_fail (request != NULL, -2);
|
|
721 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
722
|
|
723 params = request->protocol_data;
|
|
724 if (params->message.buffer != NULL)
|
|
725 {
|
|
726 sshv2_message_free (¶ms->message);
|
|
727 params->count = 0;
|
|
728 }
|
|
729
|
|
730 if (params->handle_len > 0)
|
|
731 {
|
|
732 len = htonl (params->id++);
|
|
733 memcpy (params->handle, &len, 4);
|
|
734
|
|
735 if (sshv2_send_command (request, SSH_FXP_CLOSE, params->handle,
|
|
736 params->handle_len) < 0)
|
|
737 return (-2);
|
|
738
|
|
739 memset (&message, 0, sizeof (message));
|
58
|
740 if (sshv2_read_response (request, &message, -1) != SSH_FXP_STATUS)
|
1
|
741 {
|
|
742 request->logging_function (gftp_logging_error, request->user_data,
|
|
743 _("Received wrong response from server, disconnecting\n"));
|
|
744 sshv2_message_free (&message);
|
|
745 gftp_disconnect (request);
|
|
746 return (-2);
|
|
747 }
|
|
748 sshv2_message_free (&message);
|
|
749 params->handle_len = 0;
|
|
750 }
|
|
751
|
|
752 if (params->read_buffer != NULL)
|
|
753 {
|
|
754 g_free (params->read_buffer);
|
|
755 params->read_buffer = NULL;
|
|
756 }
|
|
757
|
|
758 return (0);
|
|
759 }
|
|
760
|
|
761
|
|
762 static int
|
|
763 sshv2_list_files (gftp_request * request)
|
|
764 {
|
|
765 sshv2_params * params;
|
|
766 sshv2_message message;
|
|
767 char *tempstr;
|
|
768 gint32 len;
|
64
|
769 int ret;
|
1
|
770
|
|
771 g_return_val_if_fail (request != NULL, -2);
|
|
772 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
58
|
773 g_return_val_if_fail (request->sockfd > 0, -2);
|
1
|
774
|
|
775 params = request->protocol_data;
|
|
776
|
|
777 request->logging_function (gftp_logging_misc, request->user_data,
|
|
778 _("Retrieving directory listing...\n"));
|
|
779
|
|
780 tempstr = g_malloc (strlen (request->directory) + 9);
|
|
781
|
|
782 len = htonl (params->id++);
|
|
783 memcpy (tempstr, &len, 4);
|
|
784
|
|
785 len = htonl (strlen (request->directory));
|
|
786 memcpy (tempstr + 4, &len, 4);
|
|
787 strcpy (tempstr + 8, request->directory);
|
|
788 if (sshv2_send_command (request, SSH_FXP_OPENDIR, tempstr,
|
|
789 strlen (request->directory) + 8) < 0)
|
|
790 {
|
|
791 g_free (tempstr);
|
|
792 return (-2);
|
|
793 }
|
|
794 g_free (tempstr);
|
|
795
|
|
796 memset (&message, 0, sizeof (message));
|
64
|
797 ret = sshv2_read_response (request, &message, -1);
|
|
798 if (ret == SSH_FXP_STATUS)
|
|
799 {
|
|
800 sshv2_message_free (&message);
|
|
801 return (-2);
|
|
802 }
|
|
803 else if (ret != SSH_FXP_HANDLE)
|
1
|
804 {
|
|
805 request->logging_function (gftp_logging_error, request->user_data,
|
|
806 _("Received wrong response from server, disconnecting\n"));
|
|
807 sshv2_message_free (&message);
|
|
808 gftp_disconnect (request);
|
|
809 return (-2);
|
|
810 }
|
|
811
|
|
812 if (message.length - 4 > SSH_MAX_HANDLE_SIZE)
|
|
813 {
|
|
814 request->logging_function (gftp_logging_error, request->user_data,
|
|
815 _("Error: Message size %d too big from server\n"),
|
|
816 message.length - 4);
|
|
817 sshv2_message_free (&message);
|
|
818 gftp_disconnect (request);
|
|
819 return (-1);
|
|
820
|
|
821 }
|
|
822
|
|
823 memset (params->handle, 0, 4);
|
|
824 memcpy (params->handle + 4, message.buffer + 4, message.length - 5);
|
|
825 params->handle_len = message.length - 1;
|
|
826 sshv2_message_free (&message);
|
|
827 params->count = 0;
|
|
828 return (0);
|
|
829 }
|
|
830
|
|
831
|
|
832 static int
|
58
|
833 sshv2_get_next_file (gftp_request * request, gftp_file * fle, int fd)
|
1
|
834 {
|
|
835 gint32 len, attrs, longnamelen;
|
|
836 int ret, i, count, retsize;
|
|
837 sshv2_params *params;
|
|
838 char *longname;
|
|
839
|
|
840 g_return_val_if_fail (request != NULL, -2);
|
|
841 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
842 g_return_val_if_fail (fle != NULL, -2);
|
|
843
|
|
844 params = request->protocol_data;
|
|
845
|
|
846 if (request->last_dir_entry)
|
|
847 {
|
|
848 g_free (request->last_dir_entry);
|
|
849 request->last_dir_entry = NULL;
|
|
850 request->last_dir_entry_len = 0;
|
|
851 }
|
|
852 retsize = 0;
|
|
853
|
|
854 if (params->count > 0)
|
|
855 ret = SSH_FXP_NAME;
|
|
856 else
|
|
857 {
|
|
858 if (!request->cached)
|
|
859 {
|
|
860 if (params->message.buffer != NULL)
|
|
861 sshv2_message_free (¶ms->message);
|
|
862
|
|
863 len = htonl (params->id++);
|
|
864 memcpy (params->handle, &len, 4);
|
|
865
|
|
866 if (sshv2_send_command (request, SSH_FXP_READDIR, params->handle,
|
|
867 params->handle_len) < 0)
|
|
868 return (-2);
|
|
869 }
|
|
870
|
|
871 if ((ret = sshv2_read_response (request, ¶ms->message, fd)) < 0)
|
|
872 return (-2);
|
|
873
|
|
874 if (!request->cached)
|
|
875 {
|
|
876 request->last_dir_entry = g_malloc (params->message.length + 4);
|
|
877 len = htonl (params->message.length);
|
|
878 memcpy (request->last_dir_entry, &len, 4);
|
|
879 request->last_dir_entry[4] = params->message.command;
|
|
880 memcpy (request->last_dir_entry + 5, params->message.buffer,
|
|
881 params->message.length - 1);
|
|
882 request->last_dir_entry_len = params->message.length + 4;
|
|
883 }
|
|
884
|
|
885 if (ret == SSH_FXP_NAME)
|
|
886 {
|
|
887 params->message.pos = params->message.buffer + 4;
|
|
888 if ((params->count = sshv2_buffer_get_int32 (request,
|
|
889 ¶ms->message, -1)) < 0)
|
|
890 return (-2);
|
|
891 }
|
|
892 }
|
|
893
|
|
894 if (ret == SSH_FXP_NAME)
|
|
895 {
|
|
896 if ((len = sshv2_buffer_get_int32 (request, ¶ms->message, -1)) < 0 ||
|
|
897 params->message.pos + len > params->message.end)
|
|
898 return (-2);
|
|
899
|
|
900 params->message.pos += len;
|
|
901
|
|
902 if ((longnamelen = sshv2_buffer_get_int32 (request,
|
|
903 ¶ms->message, -1)) < 0 ||
|
|
904 params->message.pos + longnamelen > params->message.end)
|
|
905 return (-2);
|
|
906
|
|
907 longname = params->message.pos;
|
|
908 params->message.pos += longnamelen;
|
|
909
|
|
910 if ((attrs = sshv2_buffer_get_int32 (request, ¶ms->message, -1)) < 0)
|
|
911 return (-2);
|
|
912
|
|
913 if (attrs & SSH_FILEXFER_ATTR_SIZE)
|
|
914 {
|
|
915 params->message.pos += 8;
|
|
916 if (params->message.pos > params->message.end)
|
|
917 {
|
|
918 request->logging_function (gftp_logging_error, request->user_data,
|
|
919 _("Received wrong response from server, disconnecting\n"));
|
|
920 sshv2_message_free (¶ms->message);
|
|
921 gftp_disconnect (request);
|
|
922 return (-2);
|
|
923 }
|
|
924 }
|
|
925
|
|
926 if (attrs & SSH_FILEXFER_ATTR_UIDGID)
|
|
927 {
|
|
928 params->message.pos += 8;
|
|
929 if (params->message.pos > params->message.end)
|
|
930 {
|
|
931 request->logging_function (gftp_logging_error, request->user_data,
|
|
932 _("Received wrong response from server, disconnecting\n"));
|
|
933 sshv2_message_free (¶ms->message);
|
|
934 gftp_disconnect (request);
|
|
935 return (-2);
|
|
936 }
|
|
937 }
|
|
938
|
|
939 if (attrs & SSH_FILEXFER_ATTR_PERMISSIONS)
|
|
940 {
|
|
941 params->message.pos += 4;
|
|
942 if (params->message.pos > params->message.end)
|
|
943 {
|
|
944 request->logging_function (gftp_logging_error, request->user_data,
|
|
945 _("Received wrong response from server, disconnecting\n"));
|
|
946 sshv2_message_free (¶ms->message);
|
|
947 gftp_disconnect (request);
|
|
948 return (-2);
|
|
949 }
|
|
950 }
|
|
951
|
|
952 if (attrs & SSH_FILEXFER_ATTR_ACMODTIME)
|
|
953 {
|
|
954 params->message.pos += 8;
|
|
955 if (params->message.pos > params->message.end)
|
|
956 {
|
|
957 request->logging_function (gftp_logging_error, request->user_data,
|
|
958 _("Received wrong response from server, disconnecting\n"));
|
|
959 sshv2_message_free (¶ms->message);
|
|
960 gftp_disconnect (request);
|
|
961 return (-2);
|
|
962 }
|
|
963 }
|
|
964
|
|
965 if (attrs & SSH_FILEXFER_ATTR_EXTENDED)
|
|
966 {
|
|
967 if ((count = sshv2_buffer_get_int32 (request,
|
|
968 ¶ms->message, -1)) < 0)
|
|
969 return (-2);
|
|
970
|
|
971 for (i=0; i<count; i++)
|
|
972 {
|
|
973 if ((len = sshv2_buffer_get_int32 (request,
|
|
974 ¶ms->message, -1)) < 0 ||
|
|
975 params->message.pos + len + 4 > params->message.end)
|
|
976 return (-2);
|
|
977
|
|
978 params->message.pos += len + 4;
|
|
979
|
|
980 if ((len = sshv2_buffer_get_int32 (request,
|
|
981 ¶ms->message, -1)) < 0 ||
|
|
982 params->message.pos + len + 4 > params->message.end)
|
|
983 return (-2);
|
|
984
|
|
985 params->message.pos += len + 4;
|
|
986 }
|
|
987 }
|
|
988
|
|
989 longname[longnamelen] = '\0';
|
|
990
|
|
991 /* The commercial SSH2 puts a / and * after some entries */
|
|
992 if (longname[longnamelen - 1] == '*')
|
|
993 longname[--longnamelen] = '\0';
|
|
994 if (longname[longnamelen - 1] == '/')
|
|
995 longname[--longnamelen] = '\0';
|
|
996
|
|
997 if (gftp_parse_ls (longname, fle) != 0)
|
|
998 {
|
|
999 gftp_file_destroy (fle);
|
|
1000 return (-2);
|
|
1001 }
|
|
1002 retsize = strlen (longname);
|
|
1003
|
|
1004 params->count--;
|
|
1005 }
|
|
1006 else if (ret == SSH_FXP_STATUS)
|
|
1007 {
|
|
1008 sshv2_message_free (¶ms->message);
|
|
1009 return (0);
|
|
1010 }
|
|
1011 else
|
|
1012 {
|
|
1013 request->logging_function (gftp_logging_error, request->user_data,
|
|
1014 _("Received wrong response from server, disconnecting\n"));
|
|
1015 sshv2_message_free (¶ms->message);
|
|
1016 gftp_disconnect (request);
|
|
1017 return (-2);
|
|
1018 }
|
|
1019
|
|
1020 return (retsize);
|
|
1021 }
|
|
1022
|
|
1023
|
|
1024 static int
|
|
1025 sshv2_chdir (gftp_request * request, const char *directory)
|
|
1026 {
|
|
1027 sshv2_message message;
|
|
1028 sshv2_params * params;
|
|
1029 char *tempstr, *dir;
|
|
1030 gint32 num;
|
|
1031 size_t len;
|
64
|
1032 int ret;
|
1
|
1033
|
|
1034 g_return_val_if_fail (request != NULL, -2);
|
|
1035 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1036
|
|
1037 params = request->protocol_data;
|
|
1038 if (request->directory != directory)
|
|
1039 {
|
|
1040 if (*directory == '/')
|
|
1041 {
|
|
1042 len = strlen (directory) + 8;
|
|
1043 tempstr = g_malloc (len + 1);
|
|
1044 strcpy (tempstr + 8, directory);
|
|
1045 }
|
|
1046 else
|
|
1047 {
|
|
1048 len = strlen (directory) + strlen (request->directory) + 9;
|
|
1049 tempstr = g_malloc (len + 1);
|
|
1050 strcpy (tempstr + 8, request->directory);
|
|
1051 strcat (tempstr + 8, "/");
|
|
1052 strcat (tempstr + 8, directory);
|
|
1053 }
|
|
1054
|
|
1055 num = htonl (params->id++);
|
|
1056 memcpy (tempstr, &num, 4);
|
|
1057
|
|
1058 num = htonl (len - 8);
|
|
1059 memcpy (tempstr + 4, &num, 4);
|
|
1060 if (sshv2_send_command (request, SSH_FXP_REALPATH, tempstr, len) < 0)
|
|
1061 {
|
|
1062 g_free (tempstr);
|
|
1063 return (-2);
|
|
1064 }
|
|
1065 g_free (tempstr);
|
|
1066
|
|
1067 memset (&message, 0, sizeof (message));
|
64
|
1068 ret = sshv2_read_response (request, &message, -1);
|
|
1069 if (ret == SSH_FXP_STATUS)
|
|
1070 {
|
|
1071 sshv2_message_free (&message);
|
|
1072 return (-2);
|
|
1073 }
|
|
1074 else if (ret != SSH_FXP_NAME)
|
1
|
1075 {
|
|
1076 request->logging_function (gftp_logging_error, request->user_data,
|
|
1077 _("Received wrong response from server, disconnecting\n"));
|
|
1078 sshv2_message_free (&message);
|
|
1079 gftp_disconnect (request);
|
|
1080 return (-2);
|
|
1081 }
|
|
1082
|
|
1083 message.pos += 4;
|
|
1084 if (sshv2_buffer_get_int32 (request, &message, 1) != 1)
|
|
1085 return (-2);
|
|
1086
|
|
1087 if ((dir = sshv2_buffer_get_string (request, &message)) == NULL)
|
|
1088 return (-2);
|
|
1089
|
|
1090 if (request->directory)
|
|
1091 g_free (request->directory);
|
|
1092 request->directory = dir;
|
|
1093 sshv2_message_free (&message);
|
64
|
1094 return (0);
|
1
|
1095 }
|
64
|
1096 else
|
|
1097 return (sshv2_getcwd (request));
|
1
|
1098 }
|
|
1099
|
|
1100
|
|
1101 static int
|
|
1102 sshv2_rmdir (gftp_request * request, const char *directory)
|
|
1103 {
|
|
1104 sshv2_params * params;
|
|
1105 sshv2_message message;
|
|
1106 char *tempstr;
|
|
1107 gint32 num;
|
|
1108 size_t len;
|
|
1109
|
|
1110 g_return_val_if_fail (request != NULL, -2);
|
|
1111 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1112 g_return_val_if_fail (directory != NULL, -2);
|
|
1113
|
|
1114 params = request->protocol_data;
|
|
1115
|
|
1116 if (*directory == '/')
|
|
1117 {
|
|
1118 len = strlen (directory) + 8;
|
|
1119 tempstr = g_malloc (len + 1);
|
|
1120 strcpy (tempstr + 8, directory);
|
|
1121 }
|
|
1122 else
|
|
1123 {
|
|
1124 len = strlen (directory) + strlen (request->directory) + 9;
|
|
1125 tempstr = g_malloc (len + 1);
|
|
1126 strcpy (tempstr + 8, request->directory);
|
|
1127 strcat (tempstr + 8, "/");
|
|
1128 strcat (tempstr + 8, directory);
|
|
1129 }
|
|
1130
|
|
1131 num = htonl (params->id++);
|
|
1132 memcpy (tempstr, &num, 4);
|
|
1133
|
|
1134 num = htonl (len - 8);
|
|
1135 memcpy (tempstr + 4, &num, 4);
|
|
1136
|
|
1137 if (sshv2_send_command (request, SSH_FXP_RMDIR, tempstr, len) < 0)
|
|
1138 {
|
|
1139 g_free (tempstr);
|
|
1140 return (-2);
|
|
1141 }
|
|
1142 g_free (tempstr);
|
|
1143
|
|
1144 memset (&message, 0, sizeof (message));
|
58
|
1145 if (sshv2_read_response (request, &message, -1) < 0)
|
1
|
1146 return (-2);
|
|
1147
|
|
1148 message.pos += 4;
|
|
1149 if (sshv2_buffer_get_int32 (request, &message, SSH_FX_OK) < 0)
|
|
1150 return (-2);
|
|
1151
|
|
1152 sshv2_message_free (&message);
|
|
1153
|
|
1154 return (0);
|
|
1155 }
|
|
1156
|
|
1157
|
|
1158 static int
|
|
1159 sshv2_rmfile (gftp_request * request, const char *file)
|
|
1160 {
|
|
1161 sshv2_params * params;
|
|
1162 sshv2_message message;
|
|
1163 char *tempstr;
|
|
1164 gint32 num;
|
|
1165 size_t len;
|
|
1166
|
|
1167 g_return_val_if_fail (request != NULL, -2);
|
|
1168 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1169 g_return_val_if_fail (file != NULL, -2);
|
|
1170
|
|
1171 params = request->protocol_data;
|
|
1172
|
|
1173 if (*file == '/')
|
|
1174 {
|
|
1175 len = strlen (file) + 8;
|
|
1176 tempstr = g_malloc (len + 1);
|
|
1177 strcpy (tempstr + 8, file);
|
|
1178 }
|
|
1179 else
|
|
1180 {
|
|
1181 len = strlen (file) + strlen (request->directory) + 9;
|
|
1182 tempstr = g_malloc (len + 1);
|
|
1183 strcpy (tempstr + 8, request->directory);
|
|
1184 strcat (tempstr + 8, "/");
|
|
1185 strcat (tempstr + 8, file);
|
|
1186 }
|
|
1187
|
|
1188 num = htonl (params->id++);
|
|
1189 memcpy (tempstr, &num, 4);
|
|
1190
|
|
1191 num = htonl (len - 8);
|
|
1192 memcpy (tempstr + 4, &num, 4);
|
|
1193
|
|
1194 if (sshv2_send_command (request, SSH_FXP_REMOVE, tempstr, len) < 0)
|
|
1195 {
|
|
1196 g_free (tempstr);
|
|
1197 return (-2);
|
|
1198 }
|
|
1199 g_free (tempstr);
|
|
1200
|
|
1201 memset (&message, 0, sizeof (message));
|
58
|
1202 if (sshv2_read_response (request, &message, -1) < 0)
|
1
|
1203 return (-2);
|
|
1204
|
|
1205 message.pos += 4;
|
|
1206 if (sshv2_buffer_get_int32 (request, &message, SSH_FX_OK) < 0)
|
|
1207 return (-2);
|
|
1208
|
|
1209 sshv2_message_free (&message);
|
|
1210
|
|
1211 return (0);
|
|
1212 }
|
|
1213
|
|
1214
|
|
1215 static int
|
|
1216 sshv2_chmod (gftp_request * request, const char *file, int mode)
|
|
1217 {
|
|
1218 char *tempstr, buf[10];
|
|
1219 sshv2_params * params;
|
|
1220 sshv2_message message;
|
|
1221 gint32 num;
|
|
1222 size_t len;
|
|
1223
|
|
1224 g_return_val_if_fail (request != NULL, -2);
|
|
1225 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1226 g_return_val_if_fail (file != NULL, -2);
|
|
1227
|
|
1228 params = request->protocol_data;
|
|
1229
|
|
1230 if (*file == '/')
|
|
1231 {
|
|
1232 len = strlen (file) + 16;
|
|
1233 tempstr = g_malloc (len + 1);
|
|
1234 strcpy (tempstr + 8, file);
|
|
1235 }
|
|
1236 else
|
|
1237 {
|
|
1238 len = strlen (file) + strlen (request->directory) + 17;
|
|
1239 tempstr = g_malloc (len + 1);
|
|
1240 strcpy (tempstr + 8, request->directory);
|
|
1241 strcat (tempstr + 8, "/");
|
|
1242 strcat (tempstr + 8, file);
|
|
1243 }
|
|
1244
|
|
1245 num = htonl (params->id++);
|
|
1246 memcpy (tempstr, &num, 4);
|
|
1247
|
|
1248 num = htonl (len - 16);
|
|
1249 memcpy (tempstr + 4, &num, 4);
|
|
1250
|
|
1251 num = htonl (SSH_FILEXFER_ATTR_PERMISSIONS);
|
|
1252 memcpy (tempstr + len - 8, &num, 4);
|
|
1253
|
|
1254 g_snprintf (buf, sizeof (buf), "%d", mode);
|
|
1255 num = htonl (strtol (buf, NULL, 8));
|
|
1256 memcpy (tempstr + len - 4, &num, 4);
|
|
1257
|
|
1258 if (sshv2_send_command (request, SSH_FXP_SETSTAT, tempstr, len) < 0)
|
|
1259 {
|
|
1260 g_free (tempstr);
|
|
1261 return (-2);
|
|
1262 }
|
|
1263 g_free (tempstr);
|
|
1264
|
|
1265 memset (&message, 0, sizeof (message));
|
58
|
1266 if (sshv2_read_response (request, &message, -1) < 0)
|
1
|
1267 return (-2);
|
|
1268
|
|
1269 message.pos += 4;
|
|
1270 if (sshv2_buffer_get_int32 (request, &message, SSH_FX_OK) < 0)
|
|
1271 return (-2);
|
|
1272
|
|
1273 sshv2_message_free (&message);
|
|
1274
|
|
1275 return (0);
|
|
1276 }
|
|
1277
|
|
1278
|
|
1279 static int
|
|
1280 sshv2_mkdir (gftp_request * request, const char *newdir)
|
|
1281 {
|
|
1282 sshv2_params * params;
|
|
1283 sshv2_message message;
|
|
1284 char *tempstr;
|
|
1285 gint32 num;
|
|
1286 size_t len;
|
|
1287
|
|
1288 g_return_val_if_fail (request != NULL, -2);
|
|
1289 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1290 g_return_val_if_fail (newdir != NULL, -2);
|
|
1291
|
|
1292 params = request->protocol_data;
|
|
1293
|
|
1294 if (*newdir == '/')
|
|
1295 {
|
|
1296 len = strlen (newdir) + 12;
|
|
1297 tempstr = g_malloc (len + 1);
|
|
1298 strcpy (tempstr + 8, newdir);
|
|
1299 }
|
|
1300 else
|
|
1301 {
|
|
1302 len = strlen (newdir) + strlen (request->directory) + 13;
|
|
1303 tempstr = g_malloc (len + 1);
|
|
1304 strcpy (tempstr + 8, request->directory);
|
|
1305 strcat (tempstr + 8, "/");
|
|
1306 strcat (tempstr + 8, newdir);
|
|
1307 }
|
|
1308
|
|
1309 num = htonl (params->id++);
|
|
1310 memcpy (tempstr, &num, 4);
|
|
1311
|
|
1312 num = htonl (len - 12);
|
|
1313 memcpy (tempstr + 4, &num, 4);
|
|
1314 memset (tempstr + len - 4, 0, 4); /* attributes */
|
|
1315
|
|
1316 if (sshv2_send_command (request, SSH_FXP_MKDIR, tempstr, len) < 0)
|
|
1317 {
|
|
1318 g_free (tempstr);
|
|
1319 return (-2);
|
|
1320 }
|
|
1321 g_free (tempstr);
|
|
1322
|
|
1323 memset (&message, 0, sizeof (message));
|
58
|
1324 if (sshv2_read_response (request, &message, -1) < 0)
|
1
|
1325 return (-2);
|
|
1326
|
|
1327 message.pos += 4;
|
|
1328 if (sshv2_buffer_get_int32 (request, &message, SSH_FX_OK) < 0)
|
|
1329 return (-2);
|
|
1330
|
|
1331 sshv2_message_free (&message);
|
|
1332
|
|
1333 return (0);
|
|
1334 }
|
|
1335
|
|
1336
|
|
1337 static int
|
|
1338 sshv2_rename (gftp_request * request, const char *oldname, const char *newname)
|
|
1339 {
|
|
1340 char *tempstr, *oldstr, *newstr;
|
|
1341 sshv2_params * params;
|
|
1342 sshv2_message message;
|
|
1343 gint32 num;
|
|
1344 size_t oldlen, newlen;
|
|
1345
|
|
1346 g_return_val_if_fail (request != NULL, -2);
|
|
1347 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1348 g_return_val_if_fail (oldname != NULL, -2);
|
|
1349 g_return_val_if_fail (newname != NULL, -2);
|
|
1350
|
|
1351 params = request->protocol_data;
|
|
1352
|
|
1353 if (*oldname == '/')
|
|
1354 {
|
|
1355 oldlen = strlen (oldname);
|
56
|
1356 oldstr = g_strdup (oldname);
|
1
|
1357 }
|
|
1358 else
|
|
1359 {
|
|
1360 oldlen = strlen (request->directory) + strlen (oldname) + 1;
|
|
1361 oldstr = g_strconcat (request->directory, "/", oldname, NULL);
|
|
1362 }
|
|
1363
|
|
1364 if (*newname == '/')
|
|
1365 {
|
|
1366 newlen = strlen (newname);
|
56
|
1367 newstr = g_strdup (newname);
|
1
|
1368 }
|
|
1369 else
|
|
1370 {
|
|
1371 newlen = strlen (request->directory) + strlen (newname) + 1;
|
|
1372 newstr = g_strconcat (request->directory, "/", newname, NULL);
|
|
1373 }
|
|
1374
|
|
1375 tempstr = g_malloc (oldlen + newlen + 13);
|
|
1376 num = htonl (params->id++);
|
|
1377 memcpy (tempstr, &num, 4);
|
|
1378
|
|
1379 num = htonl (oldlen);
|
|
1380 memcpy (tempstr + 4, &num, 4);
|
|
1381 strcpy (tempstr + 8, oldstr);
|
|
1382
|
|
1383 num = htonl (newlen);
|
|
1384 memcpy (tempstr + 8 + oldlen, &num, 4);
|
|
1385 strcpy (tempstr + 12 + oldlen, newstr);
|
|
1386
|
|
1387 if (sshv2_send_command (request, SSH_FXP_RENAME, tempstr, oldlen + newlen + 12) < 0)
|
|
1388 {
|
|
1389 g_free (tempstr);
|
|
1390 return (-2);
|
|
1391 }
|
|
1392 g_free (tempstr);
|
|
1393
|
|
1394 memset (&message, 0, sizeof (message));
|
58
|
1395 if (sshv2_read_response (request, &message, -1) < 0)
|
1
|
1396 return (-2);
|
|
1397
|
|
1398 message.pos += 4;
|
|
1399 if (sshv2_buffer_get_int32 (request, &message, SSH_FX_OK) < 0)
|
|
1400 return (-2);
|
|
1401
|
|
1402 sshv2_message_free (&message);
|
|
1403
|
|
1404 return (0);
|
|
1405 }
|
|
1406
|
|
1407
|
|
1408 static int
|
|
1409 sshv2_set_file_time (gftp_request * request, const char *file, time_t datetime)
|
|
1410 {
|
|
1411 sshv2_params * params;
|
|
1412 sshv2_message message;
|
|
1413 char *tempstr;
|
|
1414 gint32 num;
|
|
1415 size_t len;
|
|
1416
|
|
1417 g_return_val_if_fail (request != NULL, -2);
|
|
1418 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1419 g_return_val_if_fail (file != NULL, -2);
|
|
1420
|
|
1421 params = request->protocol_data;
|
|
1422
|
|
1423 if (*file == '/')
|
|
1424 {
|
|
1425 len = strlen (file) + 20;
|
|
1426 tempstr = g_malloc (len + 1);
|
|
1427 strcpy (tempstr + 8, file);
|
|
1428 }
|
|
1429 else
|
|
1430 {
|
|
1431 len = strlen (file) + strlen (request->directory) + 21;
|
|
1432 tempstr = g_malloc (len + 1);
|
|
1433 strcpy (tempstr + 8, request->directory);
|
|
1434 strcat (tempstr + 8, "/");
|
|
1435 strcat (tempstr + 8, file);
|
|
1436 }
|
|
1437
|
|
1438 num = htonl (params->id++);
|
|
1439 memcpy (tempstr, &num, 4);
|
|
1440
|
|
1441 num = htonl (len - 20);
|
|
1442 memcpy (tempstr + 4, &num, 4);
|
|
1443
|
|
1444 num = htonl (SSH_FILEXFER_ATTR_ACMODTIME);
|
|
1445 memcpy (tempstr + len - 12, &num, 4);
|
|
1446
|
|
1447 num = htonl (datetime);
|
|
1448 memcpy (tempstr + len - 8, &num, 4);
|
|
1449
|
|
1450 num = htonl (datetime);
|
|
1451 memcpy (tempstr + len - 4, &num, 4);
|
|
1452
|
|
1453 if (sshv2_send_command (request, SSH_FXP_SETSTAT, tempstr, len) < 0)
|
|
1454 {
|
|
1455 g_free (tempstr);
|
|
1456 return (-2);
|
|
1457 }
|
|
1458 g_free (tempstr);
|
|
1459
|
|
1460 memset (&message, 0, sizeof (message));
|
58
|
1461 if (sshv2_read_response (request, &message, -1) < 0)
|
1
|
1462 return (-2);
|
|
1463
|
|
1464 message.pos += 4;
|
|
1465 if (sshv2_buffer_get_int32 (request, &message, SSH_FX_OK) < 0)
|
|
1466 return (-2);
|
|
1467
|
|
1468 sshv2_message_free (&message);
|
|
1469
|
|
1470 return (0);
|
|
1471 }
|
|
1472
|
|
1473
|
|
1474 static off_t
|
|
1475 sshv2_get_file_size (gftp_request * request, const char *file)
|
|
1476 {
|
|
1477 gint32 len, highnum, lownum, attrs, num;
|
|
1478 sshv2_params * params;
|
|
1479 char *tempstr;
|
64
|
1480 int serv_ret;
|
1
|
1481 #ifdef G_HAVE_GINT64
|
|
1482 gint64 ret;
|
|
1483 #endif
|
|
1484
|
|
1485 g_return_val_if_fail (request != NULL, -2);
|
|
1486 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
|
1487 g_return_val_if_fail (file != NULL, -2);
|
|
1488
|
|
1489 params = request->protocol_data;
|
|
1490
|
|
1491 if (*file == '/')
|
|
1492 {
|
|
1493 len = strlen (file);
|
|
1494 tempstr = g_malloc (len + 9);
|
|
1495 strcpy (tempstr + 8, file);
|
|
1496 }
|
|
1497 else
|
|
1498 {
|
|
1499 len = strlen (file) + strlen (request->directory) + 1;
|
|
1500 tempstr = g_malloc (len + 9);
|
|
1501 strcpy (tempstr + 8, request->directory);
|
|
1502 strcat (tempstr + 8, "/");
|
|
1503 strcat (tempstr + 8, file);
|
|
1504 }
|
|
1505
|
|
1506 num = htonl (params->id++);
|
|
1507 memcpy (tempstr, &num, 4);
|
|
1508
|
|
1509 num = htonl (len);
|
|
1510 memcpy (tempstr + 4, &num, 4);
|
|
1511
|
|
1512 if (sshv2_send_command (request, SSH_FXP_STAT, tempstr, len + 8) < 0)
|
|
1513 {
|
|
1514 g_free (tempstr);
|
|
1515 return (-2);
|
|
1516 }
|
|
1517 g_free (tempstr);
|
|
1518
|
|
1519 memset (¶ms->message, 0, sizeof (params->message));
|
64
|
1520 serv_ret = sshv2_read_response (request, ¶ms->message, -1);
|
|
1521 if (serv_ret == SSH_FXP_STATUS)
|
|
1522 {
|
|
1523 sshv2_message_free (¶ms->message);
|
|
1524 return (-2);
|
|
1525 }
|
|
1526 else if (serv_ret != SSH_FXP_ATTRS)
|
1
|
1527 {
|
|
1528 request->logging_function (gftp_logging_error, request->user_data,
|
|
1529 _("Received wrong response from server, disconnecting\n"));
|
|
1530 sshv2_message_free (¶ms->message);
|
|
1531 gftp_disconnect (request);
|
|
1532 return (-2);
|
|
1533 }
|
|
1534
|
|
1535 if (params->message.length < 5)
|
|
1536 return (-2);
|
|
1537 params->message.pos += 4;
|
|
1538
|
|
1539 if ((attrs = sshv2_buffer_get_int32 (request, ¶ms->message, -1)) < 0)
|
|
1540 return (-2);
|
|
1541
|
|
1542 if (attrs & SSH_FILEXFER_ATTR_SIZE)
|
|
1543 {
|
|
1544 if ((highnum = sshv2_buffer_get_int32 (request, ¶ms->message, -1)) < 0)
|
|
1545 return (-2);
|
|
1546
|
|
1547 if ((lownum = sshv2_buffer_get_int32 (request, ¶ms->message, -1)) < 0)
|
|
1548 return (-2);
|
|
1549
|
|
1550 sshv2_message_free (¶ms->message);
|
|
1551
|
|
1552 #if G_HAVE_GINT64
|
|
1553 ret = (gint64) lownum | ((gint64) highnum >> 32);
|
|
1554 return (ret);
|
|
1555 #else
|
|
1556 return (lownum);
|
|
1557 #endif
|
|
1558 }
|
|
1559
|
|
1560 sshv2_message_free (¶ms->message);
|
|
1561
|
|
1562 return (0);
|
|
1563
|
|
1564 }
|
|
1565
|
|
1566
|
58
|
1567 static off_t
|
|
1568 sshv2_get_file (gftp_request * request, const char *file, int fd,
|
1
|
1569 off_t startsize)
|
|
1570 {
|
|
1571 sshv2_params * params;
|
|
1572 sshv2_message message;
|
|
1573 char *tempstr;
|
|
1574 size_t stlen;
|
|
1575 gint32 num;
|
64
|
1576 int ret;
|
1
|
1577
|
|
1578 g_return_val_if_fail (request != NULL, -2);
|
|
1579 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
58
|
1580 g_return_val_if_fail (request->sockfd > 0, -2);
|
1
|
1581 /* fd ignored for this protocol */
|
|
1582
|
|
1583 params = request->protocol_data;
|
|
1584 params->offset = startsize;
|
|
1585
|
|
1586 if (*file == '/')
|
|
1587 {
|
|
1588 stlen = strlen (file);
|
|
1589 tempstr = g_malloc (stlen + 16);
|
|
1590 strcpy (tempstr + 8, file);
|
|
1591 }
|
|
1592 else
|
|
1593 {
|
|
1594 stlen = strlen (file) + strlen (request->directory) + 1;
|
|
1595 tempstr = g_malloc (stlen + 16);
|
|
1596 strcpy (tempstr + 8, request->directory);
|
|
1597 strcat (tempstr + 8, "/");
|
|
1598 strcat (tempstr + 8, file);
|
|
1599 }
|
|
1600
|
|
1601 num = htonl (params->id++);
|
|
1602 memcpy (tempstr, &num, 4);
|
|
1603
|
|
1604 num = htonl (stlen);
|
|
1605 memcpy (tempstr + 4, &num, 4);
|
|
1606
|
|
1607 num = htonl (SSH_FXF_READ);
|
|
1608 memcpy (tempstr + 8 + stlen, &num, 4);
|
|
1609
|
|
1610 memset (tempstr + 12 + stlen, 0, 4);
|
|
1611
|
|
1612 if (sshv2_send_command (request, SSH_FXP_OPEN, tempstr, stlen + 16) < 0)
|
|
1613 {
|
|
1614 g_free (tempstr);
|
|
1615 return (-2);
|
|
1616 }
|
|
1617
|
|
1618 g_free (tempstr);
|
|
1619 memset (&message, 0, sizeof (message));
|
64
|
1620 ret = sshv2_read_response (request, &message, -1);
|
|
1621 if (ret == SSH_FXP_STATUS)
|
|
1622 {
|
|
1623 sshv2_message_free (&message);
|
|
1624 return (-2);
|
|
1625 }
|
|
1626 else if (ret != SSH_FXP_HANDLE)
|
1
|
1627 {
|
|
1628 request->logging_function (gftp_logging_error, request->user_data,
|
|
1629 _("Received wrong response from server, disconnecting\n"));
|
|
1630 sshv2_message_free (&message);
|
|
1631 gftp_disconnect (request);
|
|
1632 return (-2);
|
|
1633 }
|
|
1634
|
|
1635 if (message.length - 4 > SSH_MAX_HANDLE_SIZE)
|
|
1636 {
|
|
1637 request->logging_function (gftp_logging_error, request->user_data,
|
|
1638 _("Error: Message size %d too big from server\n"),
|
|
1639 message.length - 4);
|
|
1640 sshv2_message_free (&message);
|
|
1641 gftp_disconnect (request);
|
|
1642 return (-1);
|
|
1643
|
|
1644 }
|
|
1645
|
|
1646 memset (params->handle, 0, 4);
|
|
1647 memcpy (params->handle + 4, message.buffer+ 4, message.length - 5);
|
|
1648 params->handle_len = message.length - 1;
|
|
1649 sshv2_message_free (&message);
|
|
1650
|
|
1651 return (sshv2_get_file_size (request, file));
|
|
1652 }
|
|
1653
|
|
1654
|
|
1655 static int
|
58
|
1656 sshv2_put_file (gftp_request * request, const char *file, int fd,
|
1
|
1657 off_t startsize, off_t totalsize)
|
|
1658 {
|
|
1659 sshv2_params * params;
|
|
1660 sshv2_message message;
|
|
1661 char *tempstr;
|
|
1662 size_t stlen;
|
|
1663 gint32 num;
|
64
|
1664 int ret;
|
1
|
1665
|
|
1666 g_return_val_if_fail (request != NULL, -2);
|
|
1667 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
58
|
1668 g_return_val_if_fail (request->sockfd > 0, -2);
|
1
|
1669 /* fd ignored for this protocol */
|
|
1670
|
|
1671 params = request->protocol_data;
|
|
1672 params->offset = 0;
|
|
1673
|
|
1674 if (*file == '/')
|
|
1675 {
|
|
1676 stlen = strlen (file);
|
|
1677 tempstr = g_malloc (stlen + 16);
|
|
1678 strcpy (tempstr + 8, file);
|
|
1679 }
|
|
1680 else
|
|
1681 {
|
|
1682 stlen = strlen (file) + strlen (request->directory) + 1;
|
|
1683 tempstr = g_malloc (stlen + 16);
|
|
1684 strcpy (tempstr + 8, request->directory);
|
|
1685 strcat (tempstr + 8, "/");
|
|
1686 strcat (tempstr + 8, file);
|
|
1687 }
|
|
1688
|
|
1689 num = htonl (params->id++);
|
|
1690 memcpy (tempstr, &num, 4);
|
|
1691
|
|
1692 num = htonl (stlen);
|
|
1693 memcpy (tempstr + 4, &num, 4);
|
|
1694
|
|
1695 if (startsize > 0)
|
|
1696 num = htonl (SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_APPEND);
|
|
1697 else
|
|
1698 num = htonl (SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
|
|
1699 memcpy (tempstr + 8 + stlen, &num, 4);
|
|
1700
|
|
1701 memset (tempstr + 12 + stlen, 0, 4);
|
|
1702
|
|
1703 if (sshv2_send_command (request, SSH_FXP_OPEN, tempstr, stlen + 16) < 0)
|
|
1704 {
|
|
1705 g_free (tempstr);
|
|
1706 return (-2);
|
|
1707 }
|
|
1708
|
|
1709 g_free (tempstr);
|
|
1710 memset (&message, 0, sizeof (message));
|
64
|
1711 ret = sshv2_read_response (request, &message, -1);
|
|
1712 if (ret == SSH_FXP_STATUS)
|
|
1713 {
|
|
1714 sshv2_message_free (&message);
|
|
1715 return (-2);
|
|
1716 }
|
|
1717 else if (ret != SSH_FXP_HANDLE)
|
1
|
1718 {
|
|
1719 request->logging_function (gftp_logging_error, request->user_data,
|
|
1720 _("Received wrong response from server, disconnecting\n"));
|
|
1721 sshv2_message_free (&message);
|
|
1722 gftp_disconnect (request);
|
|
1723 return (-2);
|
|
1724 }
|
|
1725
|
|
1726 if (message.length - 4 > SSH_MAX_HANDLE_SIZE)
|
|
1727 {
|
|
1728 request->logging_function (gftp_logging_error, request->user_data,
|
|
1729 _("Error: Message size %d too big from server\n"),
|
|
1730 message.length - 4);
|
|
1731 sshv2_message_free (&message);
|
|
1732 gftp_disconnect (request);
|
|
1733 return (-1);
|
|
1734
|
|
1735 }
|
|
1736
|
|
1737 memset (params->handle, 0, 4);
|
|
1738 memcpy (params->handle + 4, message.buffer+ 4, message.length - 5);
|
|
1739 params->handle_len = message.length - 1;
|
|
1740 sshv2_message_free (&message);
|
|
1741
|
|
1742 return (0);
|
|
1743 }
|
|
1744
|
|
1745
|
58
|
1746 static ssize_t
|
1
|
1747 sshv2_get_next_file_chunk (gftp_request * request, char *buf, size_t size)
|
|
1748 {
|
|
1749 sshv2_params * params;
|
|
1750 sshv2_message message;
|
|
1751 gint32 num;
|
|
1752
|
|
1753 #ifdef G_HAVE_GINT64
|
|
1754 gint64 offset;
|
|
1755 #else
|
|
1756 gint32 offset;
|
|
1757 #endif
|
|
1758
|
|
1759 g_return_val_if_fail (request != NULL, -2);
|
|
1760 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
58
|
1761 g_return_val_if_fail (request->sockfd > 0, -2);
|
1
|
1762 g_return_val_if_fail (buf != NULL, -2);
|
|
1763
|
|
1764 params = request->protocol_data;
|
|
1765
|
|
1766 if (params->read_buffer == NULL)
|
|
1767 {
|
|
1768 params->read_buffer = g_malloc (params->handle_len + 12);
|
|
1769 num = htonl (params->handle_len);
|
|
1770 memcpy (params->read_buffer, params->handle, params->handle_len);
|
|
1771 }
|
|
1772
|
|
1773 num = htonl (params->id++);
|
|
1774 memcpy (params->read_buffer, &num, 4);
|
|
1775
|
|
1776 #ifdef G_HAVE_GINT64
|
|
1777 offset = hton64 (params->offset);
|
|
1778 memcpy (params->read_buffer + params->handle_len, &offset, 8);
|
|
1779 #else
|
|
1780 memset (params->read_buffer + params->handle_len, 0, 4);
|
|
1781 offset = htonl (params->offset);
|
|
1782 memcpy (params->read_buffer + params->handle_len + 4, &offset, 4);
|
|
1783 #endif
|
|
1784
|
|
1785 num = htonl (size);
|
|
1786 memcpy (params->read_buffer + params->handle_len + 8, &num, 4);
|
|
1787
|
|
1788 if (sshv2_send_command (request, SSH_FXP_READ, params->read_buffer, params->handle_len + 12) < 0)
|
|
1789 return (-2);
|
|
1790
|
|
1791 memset (&message, 0, sizeof (message));
|
58
|
1792 if (sshv2_read_response (request, &message, -1) != SSH_FXP_DATA)
|
1
|
1793 {
|
|
1794 message.pos += 4;
|
|
1795 if ((num = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1796 return (-2);
|
|
1797 sshv2_message_free (&message);
|
|
1798 if (num != SSH_FX_EOF)
|
|
1799 {
|
|
1800 request->logging_function (gftp_logging_error, request->user_data,
|
|
1801 _("Received wrong response from server, disconnecting\n"));
|
|
1802 gftp_disconnect (request);
|
|
1803 return (-2);
|
|
1804 }
|
|
1805 return (0);
|
|
1806 }
|
|
1807
|
|
1808 memcpy (&num, message.buffer + 4, 4);
|
|
1809 num = ntohl (num);
|
|
1810 if (num > size)
|
|
1811 {
|
|
1812 request->logging_function (gftp_logging_error, request->user_data,
|
|
1813 _("Error: Message size %d too big from server\n"),
|
|
1814 num);
|
|
1815 sshv2_message_free (&message);
|
|
1816 gftp_disconnect (request);
|
|
1817 return (-1);
|
|
1818
|
|
1819 }
|
|
1820
|
|
1821 memcpy (buf, message.buffer + 8, num);
|
|
1822 sshv2_message_free (&message);
|
|
1823 params->offset += num;
|
|
1824 return (num);
|
|
1825 }
|
|
1826
|
|
1827
|
58
|
1828 static ssize_t
|
1
|
1829 sshv2_put_next_file_chunk (gftp_request * request, char *buf, size_t size)
|
|
1830 {
|
|
1831 sshv2_params * params;
|
|
1832 sshv2_message message;
|
|
1833 char tempstr[32768];
|
|
1834 gint32 num;
|
|
1835 int ret;
|
|
1836
|
|
1837 #ifdef G_HAVE_GINT64
|
|
1838 gint64 offset;
|
|
1839 #else
|
|
1840 gint32 offset;
|
|
1841 #endif
|
|
1842
|
|
1843 g_return_val_if_fail (request != NULL, -2);
|
|
1844 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, -2);
|
58
|
1845 g_return_val_if_fail (request->sockfd > 0, -2);
|
1
|
1846 g_return_val_if_fail (buf != NULL, -2);
|
|
1847 g_return_val_if_fail (size <= 32500, -2);
|
|
1848
|
|
1849 params = request->protocol_data;
|
|
1850
|
|
1851 num = htonl (params->handle_len);
|
|
1852 memcpy (tempstr, params->handle, params->handle_len);
|
|
1853
|
|
1854 num = htonl (params->id++);
|
|
1855 memcpy (tempstr, &num, 4);
|
|
1856
|
|
1857 #ifdef G_HAVE_GINT64
|
|
1858 offset = hton64 (params->offset);
|
|
1859 memcpy (tempstr + params->handle_len, &offset, 8);
|
|
1860 #else
|
|
1861 memset (tempstr + params->handle_len, 0, 4);
|
|
1862 offset = htonl (params->offset);
|
|
1863 memcpy (tempstr + params->handle_len + 4, &offset, 4);
|
|
1864 #endif
|
|
1865
|
|
1866 num = htonl (size);
|
|
1867 memcpy (tempstr + params->handle_len + 8, &num, 4);
|
|
1868 memcpy (tempstr + params->handle_len + 12, buf, size);
|
|
1869
|
|
1870 if (sshv2_send_command (request, SSH_FXP_WRITE, tempstr, params->handle_len + size + 12) < 0)
|
|
1871 {
|
|
1872 g_free (tempstr);
|
|
1873 return (-2);
|
|
1874 }
|
|
1875
|
|
1876 memset (&message, 0, sizeof (message));
|
|
1877 params->dont_log_status = 1;
|
58
|
1878 ret = sshv2_read_response (request, &message, -1);
|
1
|
1879 params->dont_log_status = 0;
|
|
1880 if (ret != SSH_FXP_STATUS)
|
|
1881 {
|
|
1882 request->logging_function (gftp_logging_error, request->user_data,
|
|
1883 _("Received wrong response from server, disconnecting\n"));
|
|
1884 sshv2_message_free (&message);
|
|
1885 gftp_disconnect (request);
|
|
1886 return (-2);
|
|
1887 }
|
|
1888
|
|
1889 message.pos += 4;
|
|
1890 if ((num = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1891 return (-2);
|
|
1892 sshv2_message_free (&message);
|
|
1893
|
|
1894 if (num == SSH_FX_EOF)
|
|
1895 return (0);
|
|
1896 else if (num != SSH_FX_OK)
|
|
1897 return (-1);
|
|
1898
|
|
1899 params->offset += size;
|
|
1900 return (size);
|
|
1901 }
|
|
1902
|
48
|
1903
|
58
|
1904 static void
|
|
1905 sshv2_set_config_options (gftp_request * request)
|
|
1906 {
|
|
1907 if (request->sftpserv_path != NULL)
|
|
1908 {
|
|
1909 if (ssh2_sftp_path != NULL &&
|
|
1910 strcmp (ssh2_sftp_path, request->sftpserv_path) == 0)
|
|
1911 return;
|
|
1912
|
|
1913 g_free (request->sftpserv_path);
|
|
1914 request->sftpserv_path = NULL;
|
|
1915 }
|
|
1916
|
|
1917 if (ssh2_sftp_path != NULL)
|
|
1918 request->sftpserv_path = g_strdup (ssh2_sftp_path);
|
|
1919
|
|
1920 request->need_userpass = ssh_need_userpass;
|
|
1921 }
|
|
1922
|
|
1923
|
63
|
1924 static void
|
|
1925 sshv2_swap_socks (gftp_request * dest, gftp_request * source)
|
|
1926 {
|
|
1927 sshv2_params * sparams, * dparams;
|
|
1928
|
|
1929 sparams = source->protocol_data;
|
|
1930 dparams = dest->protocol_data;
|
|
1931 dparams->id = sparams->id;
|
|
1932 }
|
|
1933
|
|
1934
|
48
|
1935 void
|
|
1936 sshv2_init (gftp_request * request)
|
|
1937 {
|
|
1938 sshv2_params * params;
|
|
1939
|
|
1940 g_return_if_fail (request != NULL);
|
|
1941
|
|
1942 request->protonum = GFTP_SSHV2_NUM;
|
|
1943 request->init = sshv2_init;
|
|
1944 request->destroy = sshv2_destroy;
|
|
1945 request->connect = sshv2_connect;
|
|
1946 request->disconnect = sshv2_disconnect;
|
|
1947 request->get_file = sshv2_get_file;
|
|
1948 request->put_file = sshv2_put_file;
|
|
1949 request->transfer_file = NULL;
|
|
1950 request->get_next_file_chunk = sshv2_get_next_file_chunk;
|
|
1951 request->put_next_file_chunk = sshv2_put_next_file_chunk;
|
|
1952 request->end_transfer = sshv2_end_transfer;
|
|
1953 request->abort_transfer = sshv2_end_transfer; /* NOTE: uses sshv2_end_transfer */
|
|
1954 request->list_files = sshv2_list_files;
|
|
1955 request->get_next_file = sshv2_get_next_file;
|
|
1956 request->set_data_type = NULL;
|
|
1957 request->get_file_size = sshv2_get_file_size;
|
|
1958 request->chdir = sshv2_chdir;
|
|
1959 request->rmdir = sshv2_rmdir;
|
|
1960 request->rmfile = sshv2_rmfile;
|
|
1961 request->mkdir = sshv2_mkdir;
|
|
1962 request->rename = sshv2_rename;
|
|
1963 request->chmod = sshv2_chmod;
|
|
1964 request->set_file_time = sshv2_set_file_time;
|
|
1965 request->site = NULL;
|
|
1966 request->parse_url = NULL;
|
58
|
1967 request->set_config_options = sshv2_set_config_options;
|
63
|
1968 request->swap_socks = sshv2_swap_socks;
|
48
|
1969 request->url_prefix = "ssh2";
|
|
1970 request->protocol_name = "SSH2";
|
|
1971 request->need_hostport = 1;
|
|
1972 request->need_userpass = ssh_need_userpass;
|
|
1973 request->use_cache = 1;
|
|
1974 request->use_threads = 1;
|
|
1975 request->always_connected = 0;
|
|
1976 request->protocol_data = g_malloc0 (sizeof (sshv2_params));
|
|
1977 gftp_set_config_options (request);
|
|
1978
|
|
1979 params = request->protocol_data;
|
|
1980 params->id = 1;
|
|
1981 }
|
|
1982
|