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