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