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
|
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;
|
|
157 gint32 num;
|
|
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 {
|
|
173 gint32 num;
|
|
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
|
|
496 #ifdef G_HAVE_GINT64
|
|
497
|
|
498 static gint64
|
|
499 hton64 (gint64 val)
|
|
500 {
|
|
501 if (G_BYTE_ORDER != G_BIG_ENDIAN)
|
|
502 return (GINT64_TO_BE (val));
|
|
503 else
|
|
504 return (val);
|
|
505 }
|
|
506
|
|
507 #endif
|
|
508
|
1
|
509
|
48
|
510 static void
|
|
511 sshv2_log_command (gftp_request * request, gftp_logging_level level,
|
|
512 char type, char *message, size_t length)
|
1
|
513 {
|
48
|
514 gint32 id, num, attr, stattype;
|
|
515 char *descr, *pos, oldchar;
|
1
|
516 sshv2_params * params;
|
|
517
|
48
|
518 params = request->protocol_data;
|
|
519 memcpy (&id, message, 4);
|
|
520 id = ntohl (id);
|
|
521 switch (type)
|
|
522 {
|
487
|
523 case SSH_FXP_DATA:
|
|
524 case SSH_FXP_READ:
|
|
525 case SSH_FXP_WRITE:
|
|
526 break;
|
48
|
527 case SSH_FXP_INIT:
|
186
|
528 request->logging_function (level, request,
|
48
|
529 _("%d: Protocol Initialization\n"), id);
|
|
530 break;
|
|
531 case SSH_FXP_VERSION:
|
186
|
532 request->logging_function (level, request,
|
487
|
533 _("%d: Protocol version %d\n"), id, id);
|
48
|
534 break;
|
|
535 case SSH_FXP_OPEN:
|
|
536 memcpy (&num, message + 4, 4);
|
|
537 num = ntohl (num);
|
|
538 pos = message + 12 + num - 1;
|
|
539 oldchar = *pos;
|
|
540 *pos = '\0';
|
186
|
541 request->logging_function (level, request,
|
48
|
542 _("%d: Open %s\n"), id, message + 8);
|
|
543 *pos = oldchar;
|
|
544 break;
|
|
545 case SSH_FXP_CLOSE:
|
186
|
546 request->logging_function (level, request,
|
48
|
547 _("%d: Close\n"), id);
|
|
548 case SSH_FXP_OPENDIR:
|
186
|
549 request->logging_function (level, request,
|
48
|
550 _("%d: Open Directory %s\n"), id,
|
|
551 message + 8);
|
|
552 break;
|
|
553 case SSH_FXP_READDIR:
|
186
|
554 request->logging_function (level, request,
|
48
|
555 _("%d: Read Directory\n"), id);
|
|
556 break;
|
|
557 case SSH_FXP_REMOVE:
|
186
|
558 request->logging_function (level, request,
|
48
|
559 _("%d: Remove file %s\n"), id,
|
|
560 message + 8);
|
|
561 break;
|
|
562 case SSH_FXP_MKDIR:
|
186
|
563 request->logging_function (level, request,
|
48
|
564 _("%d: Make directory %s\n"), id,
|
|
565 message + 8);
|
|
566 break;
|
|
567 case SSH_FXP_RMDIR:
|
186
|
568 request->logging_function (level, request,
|
48
|
569 _("%d: Remove directory %s\n"), id,
|
|
570 message + 8);
|
|
571 break;
|
|
572 case SSH_FXP_REALPATH:
|
186
|
573 request->logging_function (level, request,
|
48
|
574 _("%d: Realpath %s\n"), id,
|
|
575 message + 8);
|
|
576 break;
|
|
577 case SSH_FXP_ATTRS:
|
186
|
578 request->logging_function (level, request,
|
48
|
579 _("%d: File attributes\n"), id);
|
|
580 break;
|
|
581 case SSH_FXP_STAT:
|
186
|
582 request->logging_function (level, request,
|
48
|
583 _("%d: Stat %s\n"), id,
|
|
584 message + 8);
|
|
585 break;
|
|
586 case SSH_FXP_SETSTAT:
|
|
587 memcpy (&num, message + 4, 4);
|
|
588 num = ntohl (num);
|
|
589 pos = message + 12 + num - 1;
|
|
590 oldchar = *pos;
|
|
591 *pos = '\0';
|
|
592 memcpy (&stattype, message + 8 + num, 4);
|
|
593 stattype = ntohl (stattype);
|
|
594 memcpy (&attr, message + 12 + num, 4);
|
|
595 attr = ntohl (attr);
|
|
596 switch (stattype)
|
|
597 {
|
|
598 case SSH_FILEXFER_ATTR_PERMISSIONS:
|
186
|
599 request->logging_function (level, request,
|
48
|
600 _("%d: Chmod %s %o\n"), id,
|
|
601 message + 8, attr);
|
|
602 break;
|
|
603 case SSH_FILEXFER_ATTR_ACMODTIME:
|
186
|
604 request->logging_function (level, request,
|
48
|
605 _("%d: Utime %s %d\n"), id,
|
|
606 message + 8, attr);
|
|
607 }
|
|
608 *pos = oldchar;
|
|
609 break;
|
|
610 case SSH_FXP_STATUS:
|
|
611 if (params->dont_log_status)
|
|
612 break;
|
|
613 memcpy (&num, message + 4, 4);
|
|
614 num = ntohl (num);
|
|
615 switch (num)
|
|
616 {
|
|
617 case SSH_FX_OK:
|
|
618 descr = _("OK");
|
|
619 break;
|
|
620 case SSH_FX_EOF:
|
|
621 descr = _("EOF");
|
|
622 break;
|
|
623 case SSH_FX_NO_SUCH_FILE:
|
|
624 descr = _("No such file or directory");
|
|
625 break;
|
|
626 case SSH_FX_PERMISSION_DENIED:
|
|
627 descr = _("Permission denied");
|
|
628 break;
|
|
629 case SSH_FX_FAILURE:
|
|
630 descr = _("Failure");
|
|
631 break;
|
|
632 case SSH_FX_BAD_MESSAGE:
|
|
633 descr = _("Bad message");
|
|
634 break;
|
|
635 case SSH_FX_NO_CONNECTION:
|
|
636 descr = _("No connection");
|
|
637 break;
|
|
638 case SSH_FX_CONNECTION_LOST:
|
|
639 descr = _("Connection lost");
|
|
640 break;
|
|
641 case SSH_FX_OP_UNSUPPORTED:
|
|
642 descr = _("Operation unsupported");
|
|
643 break;
|
|
644 default:
|
|
645 descr = _("Unknown message returned from server");
|
|
646 break;
|
|
647 }
|
186
|
648 request->logging_function (level, request,
|
48
|
649 "%d: %s\n", id, descr);
|
|
650 break;
|
|
651 case SSH_FXP_HANDLE:
|
186
|
652 request->logging_function (level, request,
|
48
|
653 "%d: File handle\n", id);
|
|
654 break;
|
|
655 case SSH_FXP_NAME:
|
|
656 memcpy (&num, message + 4, 4);
|
|
657 num = ntohl (num);
|
186
|
658 request->logging_function (level, request,
|
48
|
659 "%d: Filenames (%d entries)\n", id,
|
|
660 num);
|
|
661 break;
|
|
662 default:
|
186
|
663 request->logging_function (level, request,
|
48
|
664 "Command: %x\n", type);
|
|
665 }
|
|
666 }
|
1
|
667
|
48
|
668
|
|
669 static int
|
|
670 sshv2_send_command (gftp_request * request, char type, char *command,
|
321
|
671 size_t len)
|
48
|
672 {
|
|
673 char buf[34000];
|
|
674 gint32 clen;
|
84
|
675 int ret;
|
48
|
676
|
|
677 if (len > 33995)
|
|
678 {
|
186
|
679 request->logging_function (gftp_logging_error, request,
|
48
|
680 _("Error: Message size %d too big\n"), len);
|
|
681 gftp_disconnect (request);
|
84
|
682 return (GFTP_EFATAL);
|
48
|
683 }
|
|
684
|
|
685 clen = htonl (len + 1);
|
|
686 memcpy (buf, &clen, 4);
|
|
687 buf[4] = type;
|
|
688 memcpy (&buf[5], command, len);
|
|
689 buf[len + 5] = '\0';
|
|
690
|
|
691 #ifdef DEBUG
|
458
|
692 printf ("\rSending to FD %d: ", request->datafd);
|
61
|
693 for (clen=0; clen<len + 5; clen++)
|
|
694 printf ("%x ", buf[clen] & 0xff);
|
48
|
695 printf ("\n");
|
|
696 #endif
|
|
697
|
|
698 sshv2_log_command (request, gftp_logging_send, type, buf + 5, len);
|
|
699
|
169
|
700 if ((ret = gftp_fd_write (request, buf, len + 5, request->datafd)) < 0)
|
84
|
701 return (ret);
|
48
|
702
|
84
|
703 return (0);
|
48
|
704 }
|
|
705
|
|
706
|
|
707 static int
|
|
708 sshv2_read_response (gftp_request * request, sshv2_message * message,
|
58
|
709 int fd)
|
48
|
710 {
|
219
|
711 char buf[6], error_buffer[255], *pos;
|
|
712 sshv2_params * params;
|
61
|
713 ssize_t numread, rem;
|
219
|
714
|
|
715 params = request->protocol_data;
|
48
|
716
|
58
|
717 if (fd <= 0)
|
169
|
718 fd = request->datafd;
|
1
|
719
|
61
|
720 pos = buf;
|
|
721 rem = 5;
|
|
722 while (rem > 0)
|
|
723 {
|
168
|
724 if ((numread = gftp_fd_read (request, pos, rem, fd)) < 0)
|
84
|
725 return ((int) numread);
|
61
|
726 rem -= numread;
|
|
727 pos += numread;
|
|
728 }
|
219
|
729 buf[5] = '\0';
|
48
|
730
|
|
731 memcpy (&message->length, buf, 4);
|
|
732 message->length = ntohl (message->length);
|
|
733 if (message->length > 34000)
|
|
734 {
|
219
|
735 if (params->initialized)
|
|
736 {
|
|
737 request->logging_function (gftp_logging_error, request,
|
48
|
738 _("Error: Message size %d too big from server\n"),
|
|
739 message->length);
|
219
|
740 }
|
|
741 else
|
|
742 {
|
|
743 request->logging_function (gftp_logging_error, request,
|
|
744 _("There was an error initializing a SSH connection with the remote server. The error message from the remote server follows:\n"), buf);
|
|
745
|
|
746 request->logging_function (gftp_logging_error, request, "%s", buf);
|
|
747
|
243
|
748 if (gftp_fd_set_sockblocking (request, fd, 0) == -1)
|
|
749 return (GFTP_EFATAL);
|
|
750
|
|
751 if ((numread = gftp_fd_read (NULL, error_buffer,
|
219
|
752 sizeof (error_buffer) - 1,
|
|
753 fd)) > 0)
|
|
754 {
|
|
755 error_buffer[numread] = '\0';
|
|
756 request->logging_function (gftp_logging_error, request,
|
|
757 "%s", error_buffer);
|
|
758 }
|
|
759 }
|
|
760
|
48
|
761 memset (message, 0, sizeof (*message));
|
|
762 gftp_disconnect (request);
|
84
|
763 return (GFTP_EFATAL);
|
48
|
764 }
|
58
|
765
|
48
|
766 message->command = buf[4];
|
61
|
767 message->buffer = g_malloc (message->length + 1);
|
48
|
768
|
|
769 message->pos = message->buffer;
|
|
770 message->end = message->buffer + message->length - 1;
|
|
771
|
61
|
772 pos = message->buffer;
|
|
773 rem = message->length - 1;
|
|
774 while (rem > 0)
|
|
775 {
|
168
|
776 if ((numread = gftp_fd_read (request, pos, rem, fd)) < 0)
|
84
|
777 return ((int) numread);
|
61
|
778 rem -= numread;
|
|
779 pos += numread;
|
|
780 }
|
48
|
781
|
61
|
782 message->buffer[message->length] = '\0';
|
48
|
783
|
|
784 sshv2_log_command (request, gftp_logging_recv, message->command,
|
|
785 message->buffer, message->length);
|
|
786
|
|
787 return (message->command);
|
1
|
788 }
|
|
789
|
|
790
|
|
791 static void
|
|
792 sshv2_destroy (gftp_request * request)
|
|
793 {
|
|
794 g_return_if_fail (request != NULL);
|
|
795 g_return_if_fail (request->protonum == GFTP_SSHV2_NUM);
|
|
796
|
|
797 g_free (request->protocol_data);
|
|
798 request->protocol_data = NULL;
|
|
799 }
|
|
800
|
|
801
|
48
|
802 static void
|
|
803 sshv2_message_free (sshv2_message * message)
|
|
804 {
|
|
805 if (message->buffer)
|
|
806 g_free (message->buffer);
|
|
807 memset (message, 0, sizeof (*message));
|
|
808 }
|
|
809
|
|
810
|
487
|
811 static int
|
493
|
812 sshv2_wrong_response (gftp_request * request, sshv2_message * message)
|
|
813 {
|
|
814 request->logging_function (gftp_logging_error, request,
|
|
815 _("Received wrong response from server, disconnecting\n"));
|
|
816 gftp_disconnect (request);
|
|
817
|
|
818 if (message != NULL)
|
|
819 sshv2_message_free (message);
|
|
820
|
|
821 return (GFTP_EFATAL);
|
|
822 }
|
|
823
|
|
824
|
|
825 static int
|
487
|
826 sshv2_read_status_response (gftp_request * request, sshv2_message * message,
|
|
827 int fd, int fxp_is_retryable, int fxp_is_wrong)
|
|
828 {
|
|
829 int ret;
|
|
830
|
|
831 memset (message, 0, sizeof (*message));
|
|
832 ret = sshv2_read_response (request, message, -1);
|
|
833 if (ret < 0)
|
|
834 return (ret);
|
|
835 else if (fxp_is_retryable > 0 && ret == fxp_is_retryable)
|
|
836 {
|
|
837 sshv2_message_free (message);
|
|
838 return (GFTP_ERETRYABLE);
|
|
839 }
|
|
840 else if (fxp_is_wrong > 0 && ret != fxp_is_wrong)
|
493
|
841 return (sshv2_wrong_response (request, message));
|
487
|
842
|
|
843 return (ret);
|
|
844 }
|
|
845
|
|
846
|
48
|
847 static gint32
|
|
848 sshv2_buffer_get_int32 (gftp_request * request, sshv2_message * message,
|
|
849 int expected_response)
|
|
850 {
|
|
851 gint32 ret;
|
|
852
|
|
853 if (message->end - message->pos < 4)
|
493
|
854 return (sshv2_wrong_response (request, message));
|
48
|
855
|
|
856 memcpy (&ret, message->pos, 4);
|
|
857 ret = ntohl (ret);
|
|
858 message->pos += 4;
|
|
859
|
|
860 if (expected_response > 0 && ret != expected_response)
|
493
|
861 return (sshv2_wrong_response (request, message));
|
48
|
862
|
|
863 return (ret);
|
|
864 }
|
|
865
|
|
866
|
|
867 static char *
|
|
868 sshv2_buffer_get_string (gftp_request * request, sshv2_message * message)
|
|
869 {
|
|
870 char *string;
|
|
871 gint32 len;
|
|
872
|
|
873 if ((len = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
874 return (NULL);
|
|
875
|
|
876 if (len > SSH_MAX_STRING_SIZE || (message->end - message->pos < len))
|
|
877 {
|
493
|
878 sshv2_wrong_response (request, message);
|
48
|
879 return (NULL);
|
|
880 }
|
|
881
|
|
882 string = g_malloc (len + 1);
|
|
883 memcpy (string, message->pos, len);
|
|
884 string[len] = '\0';
|
|
885 message->pos += len;
|
|
886 return (string);
|
|
887 }
|
|
888
|
|
889
|
|
890 static int
|
|
891 sshv2_getcwd (gftp_request * request)
|
|
892 {
|
|
893 sshv2_message message;
|
|
894 char *tempstr, *dir;
|
|
895 size_t len;
|
64
|
896 int ret;
|
48
|
897
|
84
|
898 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
899 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
48
|
900
|
|
901 if (request->directory == NULL || *request->directory == '\0')
|
|
902 dir = ".";
|
|
903 else
|
|
904 dir = request->directory;
|
|
905
|
487
|
906 len = strlen (dir) + 8;
|
|
907 tempstr = sshv2_initialize_string (request, len);
|
|
908 sshv2_add_string_to_buf (tempstr + 4, dir);
|
48
|
909
|
487
|
910 ret = sshv2_send_command (request, SSH_FXP_REALPATH, tempstr, len);
|
48
|
911
|
|
912 g_free (tempstr);
|
487
|
913 if (ret < 0)
|
|
914 return (ret);
|
|
915
|
48
|
916 if (request->directory)
|
|
917 {
|
|
918 g_free (request->directory);
|
|
919 request->directory = NULL;
|
|
920 }
|
|
921
|
487
|
922 ret = sshv2_read_status_response (request, &message, -1, SSH_FXP_STATUS,
|
|
923 SSH_FXP_NAME);
|
84
|
924 if (ret < 0)
|
|
925 return (ret);
|
48
|
926
|
|
927 message.pos += 4;
|
84
|
928 if ((ret = sshv2_buffer_get_int32 (request, &message, 1)) < 0)
|
|
929 return (ret);
|
48
|
930
|
|
931 if ((request->directory = sshv2_buffer_get_string (request, &message)) == NULL)
|
84
|
932 return (GFTP_EFATAL);
|
64
|
933
|
48
|
934 sshv2_message_free (&message);
|
|
935 return (0);
|
|
936 }
|
|
937
|
|
938
|
124
|
939 static void
|
|
940 sshv2_free_args (char **args)
|
|
941 {
|
|
942 int i;
|
|
943
|
|
944 for (i=0; args[i] != NULL; i++)
|
|
945 g_free (args[i]);
|
|
946 g_free (args);
|
|
947 }
|
|
948
|
|
949
|
1
|
950 static int
|
|
951 sshv2_connect (gftp_request * request)
|
|
952 {
|
458
|
953 int version, ret, fdm, ptymfd;
|
124
|
954 struct servent serv_struct;
|
219
|
955 sshv2_params * params;
|
1
|
956 sshv2_message message;
|
484
|
957 char **args;
|
1
|
958 pid_t child;
|
|
959
|
84
|
960 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
961 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
962 g_return_val_if_fail (request->hostname != NULL, GFTP_EFATAL);
|
1
|
963
|
169
|
964 if (request->datafd > 0)
|
1
|
965 return (0);
|
|
966
|
219
|
967 params = request->protocol_data;
|
|
968
|
186
|
969 request->logging_function (gftp_logging_misc, request,
|
1
|
970 _("Opening SSH connection to %s\n"),
|
|
971 request->hostname);
|
|
972
|
124
|
973 if (request->port == 0)
|
|
974 {
|
|
975 if (!r_getservbyname ("ssh", "tcp", &serv_struct, NULL))
|
|
976 {
|
186
|
977 request->logging_function (gftp_logging_error, request,
|
124
|
978 _("Cannot look up service name %s/tcp. Please check your services file\n"),
|
|
979 "ssh");
|
|
980 }
|
|
981 else
|
|
982 request->port = ntohs (serv_struct.s_port);
|
|
983 }
|
|
984
|
484
|
985 args = sshv2_gen_exec_args (request);
|
1
|
986
|
458
|
987 child = gftp_exec (request, &fdm, &ptymfd, args);
|
210
|
988
|
|
989 if (child == 0)
|
|
990 exit (0);
|
1
|
991
|
210
|
992 sshv2_free_args (args);
|
1
|
993
|
458
|
994 if (child < 0)
|
|
995 return (GFTP_ERETRYABLE);
|
|
996
|
210
|
997 request->datafd = fdm;
|
84
|
998
|
210
|
999 version = htonl (SSH_MY_VERSION);
|
|
1000 if ((ret = sshv2_send_command (request, SSH_FXP_INIT, (char *)
|
|
1001 &version, 4)) < 0)
|
|
1002 return (ret);
|
1
|
1003
|
458
|
1004 ret = sshv2_start_login_sequence (request, fdm, ptymfd);
|
|
1005 if (ret < 0)
|
|
1006 return (ret);
|
|
1007
|
210
|
1008 memset (&message, 0, sizeof (message));
|
219
|
1009 ret = sshv2_read_response (request, &message, -1);
|
|
1010 if (ret < 0)
|
|
1011 {
|
|
1012 sshv2_message_free (&message);
|
|
1013 return (ret);
|
|
1014 }
|
|
1015 else if (ret != SSH_FXP_VERSION)
|
493
|
1016 return (sshv2_wrong_response (request, &message));
|
219
|
1017
|
210
|
1018 sshv2_message_free (&message);
|
|
1019
|
219
|
1020 params->initialized = 1;
|
210
|
1021 request->logging_function (gftp_logging_misc, request,
|
|
1022 _("Successfully logged into SSH server %s\n"),
|
|
1023 request->hostname);
|
1
|
1024
|
64
|
1025 if (sshv2_getcwd (request) < 0)
|
|
1026 {
|
|
1027 if (request->directory)
|
|
1028 g_free (request->directory);
|
|
1029
|
|
1030 request->directory = g_strdup (".");
|
84
|
1031 if ((ret = sshv2_getcwd (request)) < 0)
|
64
|
1032 {
|
|
1033 gftp_disconnect (request);
|
84
|
1034 return (ret);
|
64
|
1035 }
|
|
1036 }
|
1
|
1037
|
|
1038 return (0);
|
|
1039 }
|
|
1040
|
|
1041
|
|
1042 static void
|
|
1043 sshv2_disconnect (gftp_request * request)
|
|
1044 {
|
|
1045 sshv2_params * params;
|
|
1046
|
|
1047 g_return_if_fail (request != NULL);
|
|
1048 g_return_if_fail (request->protonum == GFTP_SSHV2_NUM);
|
|
1049
|
|
1050 params = request->protocol_data;
|
|
1051
|
169
|
1052 if (request->datafd > 0)
|
1
|
1053 {
|
186
|
1054 request->logging_function (gftp_logging_misc, request,
|
1
|
1055 _("Disconnecting from site %s\n"),
|
|
1056 request->hostname);
|
58
|
1057
|
169
|
1058 if (close (request->datafd) < 0)
|
186
|
1059 request->logging_function (gftp_logging_error, request,
|
58
|
1060 _("Error closing file descriptor: %s\n"),
|
|
1061 g_strerror (errno));
|
|
1062
|
169
|
1063 request->datafd = -1;
|
1
|
1064 }
|
|
1065
|
|
1066 if (params->message.buffer != NULL)
|
487
|
1067 {
|
|
1068 sshv2_message_free (¶ms->message);
|
|
1069 params->message.buffer = NULL;
|
|
1070 }
|
1
|
1071 }
|
|
1072
|
|
1073
|
|
1074 static int
|
|
1075 sshv2_end_transfer (gftp_request * request)
|
|
1076 {
|
|
1077 sshv2_params * params;
|
|
1078 sshv2_message message;
|
|
1079 gint32 len;
|
84
|
1080 int ret;
|
1
|
1081
|
84
|
1082 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1083 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
1
|
1084
|
|
1085 params = request->protocol_data;
|
|
1086 if (params->message.buffer != NULL)
|
|
1087 {
|
|
1088 sshv2_message_free (¶ms->message);
|
487
|
1089 params->message.buffer = NULL;
|
1
|
1090 params->count = 0;
|
|
1091 }
|
|
1092
|
|
1093 if (params->handle_len > 0)
|
|
1094 {
|
|
1095 len = htonl (params->id++);
|
|
1096 memcpy (params->handle, &len, 4);
|
|
1097
|
84
|
1098 if ((ret = sshv2_send_command (request, SSH_FXP_CLOSE, params->handle,
|
|
1099 params->handle_len)) < 0)
|
|
1100 return (ret);
|
1
|
1101
|
487
|
1102 ret = sshv2_read_status_response (request, &message, -1, 0,
|
|
1103 SSH_FXP_STATUS);
|
|
1104 if (ret < 0)
|
|
1105 return (ret);
|
|
1106
|
1
|
1107 sshv2_message_free (&message);
|
|
1108 params->handle_len = 0;
|
|
1109 }
|
|
1110
|
|
1111 if (params->read_buffer != NULL)
|
|
1112 {
|
|
1113 g_free (params->read_buffer);
|
|
1114 params->read_buffer = NULL;
|
|
1115 }
|
|
1116
|
|
1117 return (0);
|
|
1118 }
|
|
1119
|
|
1120
|
|
1121 static int
|
|
1122 sshv2_list_files (gftp_request * request)
|
|
1123 {
|
|
1124 sshv2_params * params;
|
|
1125 sshv2_message message;
|
|
1126 char *tempstr;
|
|
1127 gint32 len;
|
64
|
1128 int ret;
|
1
|
1129
|
84
|
1130 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1131 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
169
|
1132 g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
|
1
|
1133
|
|
1134 params = request->protocol_data;
|
|
1135
|
186
|
1136 request->logging_function (gftp_logging_misc, request,
|
1
|
1137 _("Retrieving directory listing...\n"));
|
|
1138
|
487
|
1139 len = strlen (request->directory) + 8;
|
|
1140 tempstr = sshv2_initialize_string (request, len);
|
|
1141 sshv2_add_string_to_buf (tempstr + 4, request->directory);
|
1
|
1142
|
487
|
1143 ret = sshv2_send_command (request, SSH_FXP_OPENDIR, tempstr, len);
|
|
1144
|
1
|
1145 g_free (tempstr);
|
84
|
1146 if (ret < 0)
|
|
1147 return (ret);
|
487
|
1148
|
|
1149 ret = sshv2_read_status_response (request, &message, -1, SSH_FXP_STATUS,
|
|
1150 SSH_FXP_HANDLE);
|
|
1151 if (ret < 0)
|
|
1152 return (ret);
|
1
|
1153
|
|
1154 if (message.length - 4 > SSH_MAX_HANDLE_SIZE)
|
|
1155 {
|
186
|
1156 request->logging_function (gftp_logging_error, request,
|
1
|
1157 _("Error: Message size %d too big from server\n"),
|
|
1158 message.length - 4);
|
|
1159 sshv2_message_free (&message);
|
|
1160 gftp_disconnect (request);
|
84
|
1161 return (GFTP_EFATAL);
|
1
|
1162 }
|
|
1163
|
|
1164 memset (params->handle, 0, 4);
|
|
1165 memcpy (params->handle + 4, message.buffer + 4, message.length - 5);
|
|
1166 params->handle_len = message.length - 1;
|
|
1167 sshv2_message_free (&message);
|
|
1168 params->count = 0;
|
|
1169 return (0);
|
|
1170 }
|
|
1171
|
|
1172
|
|
1173 static int
|
498
|
1174 sshv2_decode_file_attributes (gftp_request * request, sshv2_message * message,
|
|
1175 gftp_file * fle)
|
|
1176 {
|
|
1177 gint32 attrs, hinum, num, count;
|
|
1178 sshv2_params *params;
|
|
1179 int i;
|
|
1180
|
|
1181 params = request->protocol_data;
|
|
1182
|
|
1183 if ((attrs = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1184 return (attrs);
|
|
1185
|
|
1186 if (attrs & SSH_FILEXFER_ATTR_SIZE)
|
|
1187 {
|
|
1188 if ((hinum = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1189 return (hinum);
|
|
1190
|
|
1191 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1192 return (num);
|
|
1193
|
|
1194 #if G_HAVE_GINT64
|
|
1195 fle->size = (gint64) num | ((gint64) hinum >> 32);
|
|
1196 #else
|
|
1197 fle->size = num;
|
|
1198 #endif
|
|
1199 }
|
|
1200
|
|
1201 if (attrs & SSH_FILEXFER_ATTR_UIDGID)
|
|
1202 {
|
|
1203 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1204 return (num);
|
|
1205
|
|
1206 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1207 return (num);
|
|
1208 }
|
|
1209
|
|
1210 if (attrs & SSH_FILEXFER_ATTR_PERMISSIONS)
|
|
1211 {
|
|
1212 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1213 return (num);
|
500
|
1214
|
|
1215 fle->st_mode = num;
|
498
|
1216 }
|
|
1217
|
|
1218 if (attrs & SSH_FILEXFER_ATTR_ACMODTIME)
|
|
1219 {
|
|
1220 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1221 return (num);
|
|
1222
|
|
1223 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1224 return (num);
|
|
1225
|
|
1226 fle->datetime = num;
|
|
1227 }
|
|
1228
|
|
1229 if (attrs & SSH_FILEXFER_ATTR_EXTENDED)
|
|
1230 {
|
|
1231 if ((count = sshv2_buffer_get_int32 (request, message, -1)) < 0)
|
|
1232 return (GFTP_EFATAL);
|
|
1233
|
|
1234 for (i=0; i<count; i++)
|
|
1235 {
|
|
1236 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0 ||
|
|
1237 message->pos + num + 4 > message->end)
|
|
1238 return (GFTP_EFATAL);
|
|
1239
|
|
1240 message->pos += num + 4;
|
|
1241
|
|
1242 if ((num = sshv2_buffer_get_int32 (request, message, -1)) < 0 ||
|
|
1243 message->pos + num + 4 > message->end)
|
|
1244 return (GFTP_EFATAL);
|
|
1245
|
|
1246 message->pos += num + 4;
|
|
1247 }
|
|
1248 }
|
|
1249
|
|
1250 return (0);
|
|
1251 }
|
|
1252
|
|
1253
|
|
1254 static int
|
58
|
1255 sshv2_get_next_file (gftp_request * request, gftp_file * fle, int fd)
|
1
|
1256 {
|
498
|
1257 gint32 len, longnamelen;
|
1
|
1258 sshv2_params *params;
|
498
|
1259 int ret, retsize;
|
1
|
1260 char *longname;
|
|
1261
|
84
|
1262 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1263 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
1264 g_return_val_if_fail (fle != NULL, GFTP_EFATAL);
|
1
|
1265
|
|
1266 params = request->protocol_data;
|
|
1267
|
|
1268 if (request->last_dir_entry)
|
|
1269 {
|
|
1270 g_free (request->last_dir_entry);
|
|
1271 request->last_dir_entry = NULL;
|
|
1272 request->last_dir_entry_len = 0;
|
|
1273 }
|
|
1274 retsize = 0;
|
|
1275
|
|
1276 if (params->count > 0)
|
|
1277 ret = SSH_FXP_NAME;
|
|
1278 else
|
|
1279 {
|
|
1280 if (!request->cached)
|
|
1281 {
|
|
1282 if (params->message.buffer != NULL)
|
|
1283 sshv2_message_free (¶ms->message);
|
|
1284
|
|
1285 len = htonl (params->id++);
|
|
1286 memcpy (params->handle, &len, 4);
|
|
1287
|
84
|
1288 if ((ret = sshv2_send_command (request, SSH_FXP_READDIR,
|
|
1289 params->handle,
|
|
1290 params->handle_len)) < 0)
|
|
1291 return (ret);
|
1
|
1292 }
|
|
1293
|
|
1294 if ((ret = sshv2_read_response (request, ¶ms->message, fd)) < 0)
|
84
|
1295 return (ret);
|
1
|
1296
|
|
1297 if (!request->cached)
|
|
1298 {
|
|
1299 request->last_dir_entry = g_malloc (params->message.length + 4);
|
|
1300 len = htonl (params->message.length);
|
|
1301 memcpy (request->last_dir_entry, &len, 4);
|
|
1302 request->last_dir_entry[4] = params->message.command;
|
|
1303 memcpy (request->last_dir_entry + 5, params->message.buffer,
|
|
1304 params->message.length - 1);
|
|
1305 request->last_dir_entry_len = params->message.length + 4;
|
|
1306 }
|
|
1307
|
|
1308 if (ret == SSH_FXP_NAME)
|
|
1309 {
|
|
1310 params->message.pos = params->message.buffer + 4;
|
|
1311 if ((params->count = sshv2_buffer_get_int32 (request,
|
|
1312 ¶ms->message, -1)) < 0)
|
84
|
1313 return (params->count);
|
1
|
1314 }
|
|
1315 }
|
|
1316
|
|
1317 if (ret == SSH_FXP_NAME)
|
|
1318 {
|
|
1319 if ((len = sshv2_buffer_get_int32 (request, ¶ms->message, -1)) < 0 ||
|
|
1320 params->message.pos + len > params->message.end)
|
84
|
1321 return (GFTP_EFATAL);
|
1
|
1322
|
|
1323 params->message.pos += len;
|
|
1324
|
|
1325 if ((longnamelen = sshv2_buffer_get_int32 (request,
|
|
1326 ¶ms->message, -1)) < 0 ||
|
|
1327 params->message.pos + longnamelen > params->message.end)
|
84
|
1328 return (GFTP_EFATAL);
|
1
|
1329
|
|
1330 longname = params->message.pos;
|
|
1331 longname[longnamelen] = '\0';
|
|
1332
|
|
1333 /* The commercial SSH2 puts a / and * after some entries */
|
|
1334 if (longname[longnamelen - 1] == '*')
|
|
1335 longname[--longnamelen] = '\0';
|
|
1336 if (longname[longnamelen - 1] == '/')
|
|
1337 longname[--longnamelen] = '\0';
|
|
1338
|
498
|
1339 params->message.pos += longnamelen;
|
|
1340
|
485
|
1341 if ((ret = gftp_parse_ls (request, longname, fle, 0)) < 0)
|
1
|
1342 {
|
|
1343 gftp_file_destroy (fle);
|
84
|
1344 return (ret);
|
1
|
1345 }
|
498
|
1346
|
|
1347 if ((ret = sshv2_decode_file_attributes (request, ¶ms->message,
|
|
1348 fle)) < 0)
|
|
1349 {
|
|
1350 gftp_file_destroy (fle);
|
|
1351 return (ret);
|
|
1352 }
|
|
1353
|
1
|
1354 retsize = strlen (longname);
|
|
1355 params->count--;
|
|
1356 }
|
|
1357 else if (ret == SSH_FXP_STATUS)
|
|
1358 {
|
|
1359 sshv2_message_free (¶ms->message);
|
|
1360 return (0);
|
|
1361 }
|
|
1362 else
|
493
|
1363 return (sshv2_wrong_response (request, ¶ms->message));
|
1
|
1364
|
|
1365 return (retsize);
|
|
1366 }
|
|
1367
|
|
1368
|
|
1369 static int
|
|
1370 sshv2_chdir (gftp_request * request, const char *directory)
|
|
1371 {
|
|
1372 sshv2_message message;
|
|
1373 char *tempstr, *dir;
|
|
1374 size_t len;
|
64
|
1375 int ret;
|
1
|
1376
|
84
|
1377 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1378 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
1
|
1379
|
|
1380 if (request->directory != directory)
|
|
1381 {
|
487
|
1382 len = 0;
|
489
|
1383 tempstr = sshv2_initialize_string_with_path (request, directory,
|
|
1384 &len, NULL);
|
1
|
1385
|
487
|
1386 ret = sshv2_send_command (request, SSH_FXP_REALPATH, tempstr, len);
|
1
|
1387
|
|
1388 g_free (tempstr);
|
84
|
1389 if (ret < 0)
|
|
1390 return (ret);
|
487
|
1391
|
|
1392 ret = sshv2_read_status_response (request, &message, -1, SSH_FXP_STATUS,
|
|
1393 SSH_FXP_NAME);
|
|
1394 if (ret < 0)
|
|
1395 return (ret);
|
1
|
1396
|
|
1397 message.pos += 4;
|
|
1398 if (sshv2_buffer_get_int32 (request, &message, 1) != 1)
|
84
|
1399 return (GFTP_EFATAL);
|
1
|
1400
|
|
1401 if ((dir = sshv2_buffer_get_string (request, &message)) == NULL)
|
84
|
1402 return (GFTP_EFATAL);
|
1
|
1403
|
|
1404 if (request->directory)
|
|
1405 g_free (request->directory);
|
487
|
1406
|
1
|
1407 request->directory = dir;
|
|
1408 sshv2_message_free (&message);
|
64
|
1409 return (0);
|
1
|
1410 }
|
284
|
1411
|
|
1412 return (0);
|
1
|
1413 }
|
|
1414
|
|
1415
|
|
1416 static int
|
|
1417 sshv2_rmdir (gftp_request * request, const char *directory)
|
|
1418 {
|
|
1419 sshv2_message message;
|
|
1420 char *tempstr;
|
|
1421 size_t len;
|
84
|
1422 int ret;
|
1
|
1423
|
84
|
1424 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1425 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
1426 g_return_val_if_fail (directory != NULL, GFTP_EFATAL);
|
1
|
1427
|
487
|
1428 len = 0;
|
489
|
1429 tempstr = sshv2_initialize_string_with_path (request, directory, &len, NULL);
|
1
|
1430
|
487
|
1431 ret = sshv2_send_command (request, SSH_FXP_RMDIR, tempstr, len);
|
1
|
1432
|
|
1433 g_free (tempstr);
|
487
|
1434 if (ret < 0)
|
|
1435 return (ret);
|
1
|
1436
|
|
1437 memset (&message, 0, sizeof (message));
|
84
|
1438 if ((ret = sshv2_read_response (request, &message, -1)) < 0)
|
|
1439 return (ret);
|
1
|
1440
|
|
1441 message.pos += 4;
|
84
|
1442 if ((ret = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1443 return (ret);
|
1
|
1444
|
|
1445 sshv2_message_free (&message);
|
|
1446
|
|
1447 return (0);
|
|
1448 }
|
|
1449
|
|
1450
|
|
1451 static int
|
|
1452 sshv2_rmfile (gftp_request * request, const char *file)
|
|
1453 {
|
|
1454 sshv2_message message;
|
|
1455 char *tempstr;
|
|
1456 size_t len;
|
84
|
1457 int ret;
|
1
|
1458
|
84
|
1459 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1460 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
1461 g_return_val_if_fail (file != NULL, GFTP_EFATAL);
|
1
|
1462
|
487
|
1463 len = 0;
|
489
|
1464 tempstr = sshv2_initialize_string_with_path (request, file, &len, NULL);
|
1
|
1465
|
487
|
1466 ret = sshv2_send_command (request, SSH_FXP_REMOVE, tempstr, len);
|
1
|
1467
|
|
1468 g_free (tempstr);
|
487
|
1469 if (ret < 0)
|
|
1470 return (ret);
|
1
|
1471
|
|
1472 memset (&message, 0, sizeof (message));
|
84
|
1473 if ((ret = sshv2_read_response (request, &message, -1)) < 0)
|
|
1474 return (ret);
|
1
|
1475
|
|
1476 message.pos += 4;
|
84
|
1477 if ((ret = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1478 return (ret);
|
1
|
1479
|
|
1480 sshv2_message_free (&message);
|
|
1481
|
|
1482 return (0);
|
|
1483 }
|
|
1484
|
|
1485
|
|
1486 static int
|
499
|
1487 sshv2_chmod (gftp_request * request, const char *file, mode_t mode)
|
1
|
1488 {
|
489
|
1489 char *tempstr, *endpos, buf[10];
|
1
|
1490 sshv2_params * params;
|
|
1491 sshv2_message message;
|
|
1492 gint32 num;
|
|
1493 size_t len;
|
84
|
1494 int ret;
|
1
|
1495
|
84
|
1496 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1497 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
1498 g_return_val_if_fail (file != NULL, GFTP_EFATAL);
|
1
|
1499
|
|
1500 params = request->protocol_data;
|
|
1501
|
489
|
1502 len = 8; /* For attributes */
|
|
1503 tempstr = sshv2_initialize_string_with_path (request, file, &len, &endpos);
|
1
|
1504
|
|
1505 num = htonl (SSH_FILEXFER_ATTR_PERMISSIONS);
|
489
|
1506 memcpy (endpos, &num, 4);
|
1
|
1507
|
|
1508 g_snprintf (buf, sizeof (buf), "%d", mode);
|
|
1509 num = htonl (strtol (buf, NULL, 8));
|
489
|
1510 memcpy (endpos + 4, &num, 4);
|
|
1511
|
|
1512 ret = sshv2_send_command (request, SSH_FXP_SETSTAT, tempstr, len);
|
1
|
1513
|
|
1514 g_free (tempstr);
|
489
|
1515 if (ret < 0)
|
|
1516 return (ret);
|
1
|
1517
|
|
1518 memset (&message, 0, sizeof (message));
|
84
|
1519 if ((ret = sshv2_read_response (request, &message, -1)) < 0)
|
|
1520 return (ret);
|
1
|
1521
|
|
1522 message.pos += 4;
|
84
|
1523 if ((ret = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1524 return (ret);
|
1
|
1525
|
|
1526 sshv2_message_free (&message);
|
|
1527
|
|
1528 return (0);
|
|
1529 }
|
|
1530
|
|
1531
|
|
1532 static int
|
|
1533 sshv2_mkdir (gftp_request * request, const char *newdir)
|
|
1534 {
|
|
1535 sshv2_params * params;
|
|
1536 sshv2_message message;
|
|
1537 char *tempstr;
|
|
1538 size_t len;
|
84
|
1539 int ret;
|
1
|
1540
|
84
|
1541 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1542 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
1543 g_return_val_if_fail (newdir != NULL, GFTP_EFATAL);
|
1
|
1544
|
|
1545 params = request->protocol_data;
|
|
1546
|
489
|
1547 len = 4; /* For attributes */
|
|
1548 tempstr = sshv2_initialize_string_with_path (request, newdir, &len, NULL);
|
|
1549
|
|
1550 /* No need to set attributes since all characters of the tempstr buffer is
|
|
1551 initialized to 0 */
|
1
|
1552
|
489
|
1553 ret = sshv2_send_command (request, SSH_FXP_MKDIR, tempstr, len);
|
1
|
1554
|
|
1555 g_free (tempstr);
|
489
|
1556 if (ret < 0)
|
|
1557 return (ret);
|
1
|
1558
|
|
1559 memset (&message, 0, sizeof (message));
|
84
|
1560 if ((ret = sshv2_read_response (request, &message, -1)) < 0)
|
|
1561 return (ret);
|
1
|
1562
|
|
1563 message.pos += 4;
|
84
|
1564 if ((ret = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1565 return (ret);
|
1
|
1566
|
|
1567 sshv2_message_free (&message);
|
|
1568
|
|
1569 return (0);
|
|
1570 }
|
|
1571
|
|
1572
|
|
1573 static int
|
|
1574 sshv2_rename (gftp_request * request, const char *oldname, const char *newname)
|
|
1575 {
|
|
1576 char *tempstr, *oldstr, *newstr;
|
489
|
1577 size_t oldlen, newlen, len;
|
1
|
1578 sshv2_params * params;
|
|
1579 sshv2_message message;
|
84
|
1580 int ret;
|
1
|
1581
|
84
|
1582 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1583 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
1584 g_return_val_if_fail (oldname != NULL, GFTP_EFATAL);
|
|
1585 g_return_val_if_fail (newname != NULL, GFTP_EFATAL);
|
1
|
1586
|
|
1587 params = request->protocol_data;
|
|
1588
|
|
1589 if (*oldname == '/')
|
489
|
1590 oldstr = g_strdup (oldname);
|
1
|
1591 else
|
489
|
1592 oldstr = gftp_build_path (request->directory, oldname, NULL);
|
1
|
1593
|
|
1594 if (*newname == '/')
|
489
|
1595 newstr = g_strdup (newname);
|
1
|
1596 else
|
489
|
1597 newstr = gftp_build_path (request->directory, newname, NULL);
|
1
|
1598
|
489
|
1599 oldlen = strlen (oldstr);
|
|
1600 newlen = strlen (newname);
|
1
|
1601
|
489
|
1602 len = oldlen + newlen + 12;
|
|
1603 tempstr = sshv2_initialize_string (request, len);
|
|
1604 sshv2_add_string_to_buf (tempstr + 4, oldstr);
|
|
1605 sshv2_add_string_to_buf (tempstr + 8 + oldlen, newstr);
|
1
|
1606
|
489
|
1607 g_free (oldstr);
|
|
1608 g_free (newstr);
|
|
1609
|
|
1610 ret = sshv2_send_command (request, SSH_FXP_RENAME, tempstr, len);
|
|
1611
|
1
|
1612 g_free (tempstr);
|
489
|
1613 if (ret < 0)
|
|
1614 return (ret);
|
1
|
1615
|
|
1616 memset (&message, 0, sizeof (message));
|
84
|
1617 if ((ret = sshv2_read_response (request, &message, -1)) < 0)
|
|
1618 return (ret);
|
1
|
1619
|
|
1620 message.pos += 4;
|
84
|
1621 if ((ret = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1622 return (ret);
|
1
|
1623
|
|
1624 sshv2_message_free (&message);
|
|
1625
|
|
1626 return (0);
|
|
1627 }
|
|
1628
|
|
1629
|
|
1630 static int
|
|
1631 sshv2_set_file_time (gftp_request * request, const char *file, time_t datetime)
|
|
1632 {
|
489
|
1633 char *tempstr, *endpos;
|
1
|
1634 sshv2_params * params;
|
|
1635 sshv2_message message;
|
|
1636 gint32 num;
|
|
1637 size_t len;
|
84
|
1638 int ret;
|
1
|
1639
|
84
|
1640 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1641 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
|
1642 g_return_val_if_fail (file != NULL, GFTP_EFATAL);
|
1
|
1643
|
|
1644 params = request->protocol_data;
|
|
1645
|
489
|
1646 len = 12; /* For date/time */
|
|
1647 tempstr = sshv2_initialize_string_with_path (request, file, &len, &endpos);
|
1
|
1648
|
|
1649 num = htonl (SSH_FILEXFER_ATTR_ACMODTIME);
|
489
|
1650 memcpy (endpos, &num, 4);
|
1
|
1651
|
|
1652 num = htonl (datetime);
|
489
|
1653 memcpy (endpos + 4, &num, 4);
|
|
1654 memcpy (endpos + 8, &num, 4);
|
1
|
1655
|
489
|
1656 ret = sshv2_send_command (request, SSH_FXP_SETSTAT, tempstr, len);
|
|
1657
|
1
|
1658 g_free (tempstr);
|
489
|
1659 if (ret < 0)
|
|
1660 return (ret);
|
1
|
1661
|
|
1662 memset (&message, 0, sizeof (message));
|
84
|
1663 if ((ret = sshv2_read_response (request, &message, -1)) < 0)
|
|
1664 return (ret);
|
1
|
1665
|
|
1666 message.pos += 4;
|
84
|
1667 if ((ret = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
|
1668 return (ret);
|
1
|
1669
|
|
1670 sshv2_message_free (&message);
|
|
1671
|
|
1672 return (0);
|
|
1673 }
|
|
1674
|
|
1675
|
500
|
1676 static int
|
|
1677 sshv2_send_stat_command (gftp_request * request, const char *filename,
|
|
1678 gftp_file * fle)
|
1
|
1679 {
|
487
|
1680 sshv2_message message;
|
1
|
1681 char *tempstr;
|
498
|
1682 gint32 len;
|
500
|
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);
|
500
|
1687 g_return_val_if_fail (filename != NULL, GFTP_EFATAL);
|
1
|
1688
|
487
|
1689 len = 0;
|
500
|
1690 tempstr = sshv2_initialize_string_with_path (request, filename, &len, NULL);
|
487
|
1691
|
|
1692 ret = sshv2_send_command (request, SSH_FXP_STAT, tempstr, len);
|
1
|
1693
|
487
|
1694 g_free (tempstr);
|
|
1695 if (ret < 0)
|
|
1696 return (ret);
|
1
|
1697
|
500
|
1698 ret = sshv2_read_status_response (request, &message, -1, SSH_FXP_STATUS,
|
|
1699 SSH_FXP_ATTRS);
|
|
1700 if (ret < 0)
|
|
1701 return (ret);
|
487
|
1702
|
|
1703 if (message.length < 5)
|
|
1704 return (GFTP_EFATAL);
|
1
|
1705
|
487
|
1706 message.pos += 4;
|
500
|
1707 if ((ret = sshv2_decode_file_attributes (request, &message, fle)) < 0)
|
1
|
1708 {
|
500
|
1709 gftp_file_destroy (fle);
|
|
1710 return (ret);
|
1
|
1711 }
|
|
1712
|
487
|
1713 sshv2_message_free (&message);
|
1
|
1714
|
500
|
1715 return (0);
|
|
1716 }
|
|
1717
|
|
1718
|
|
1719 static mode_t
|
|
1720 sshv2_stat_filename (gftp_request * request, const char *filename)
|
|
1721 {
|
|
1722 gftp_file fle;
|
|
1723 mode_t ret;
|
|
1724
|
|
1725 memset (&fle, 0, sizeof (fle));
|
|
1726 ret = sshv2_send_stat_command (request, filename, &fle);
|
|
1727 if (ret < 0)
|
|
1728 return (ret);
|
|
1729
|
|
1730 ret = fle.st_mode;
|
|
1731 gftp_file_destroy (&fle);
|
|
1732
|
498
|
1733 return (ret);
|
500
|
1734 }
|
1
|
1735
|
500
|
1736
|
|
1737 static off_t
|
|
1738 sshv2_get_file_size (gftp_request * request, const char *file)
|
|
1739 {
|
|
1740 gftp_file fle;
|
|
1741 off_t size;
|
|
1742 int ret;
|
|
1743
|
|
1744 memset (&fle, 0, sizeof (fle));
|
|
1745 ret = sshv2_send_stat_command (request, file, &fle);
|
|
1746 if (ret < 0)
|
|
1747 return (ret);
|
|
1748
|
|
1749 size = fle.size;
|
|
1750 gftp_file_destroy (&fle);
|
|
1751
|
|
1752 return (size);
|
1
|
1753 }
|
|
1754
|
|
1755
|
58
|
1756 static off_t
|
|
1757 sshv2_get_file (gftp_request * request, const char *file, int fd,
|
1
|
1758 off_t startsize)
|
|
1759 {
|
489
|
1760 char *tempstr, *endpos;
|
1
|
1761 sshv2_params * params;
|
|
1762 sshv2_message message;
|
489
|
1763 size_t len;
|
1
|
1764 gint32 num;
|
64
|
1765 int ret;
|
1
|
1766
|
84
|
1767 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1768 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
169
|
1769 g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
|
1
|
1770 /* fd ignored for this protocol */
|
|
1771
|
|
1772 params = request->protocol_data;
|
|
1773 params->offset = startsize;
|
|
1774
|
489
|
1775 len = 8;
|
|
1776 tempstr = sshv2_initialize_string_with_path (request, file, &len, &endpos);
|
1
|
1777
|
|
1778 num = htonl (SSH_FXF_READ);
|
489
|
1779 memcpy (endpos, &num, 4);
|
1
|
1780
|
489
|
1781 ret = sshv2_send_command (request, SSH_FXP_OPEN, tempstr, len);
|
1
|
1782
|
|
1783 g_free (tempstr);
|
84
|
1784 if (ret < 0)
|
|
1785 return (ret);
|
489
|
1786
|
|
1787 ret = sshv2_read_status_response (request, &message, -1, SSH_FXP_STATUS,
|
|
1788 SSH_FXP_HANDLE);
|
|
1789 if (ret < 0)
|
|
1790 return (ret);
|
1
|
1791
|
|
1792 if (message.length - 4 > SSH_MAX_HANDLE_SIZE)
|
|
1793 {
|
186
|
1794 request->logging_function (gftp_logging_error, request,
|
1
|
1795 _("Error: Message size %d too big from server\n"),
|
|
1796 message.length - 4);
|
|
1797 sshv2_message_free (&message);
|
|
1798 gftp_disconnect (request);
|
84
|
1799 return (GFTP_ERETRYABLE);
|
1
|
1800
|
|
1801 }
|
|
1802
|
|
1803 memset (params->handle, 0, 4);
|
|
1804 memcpy (params->handle + 4, message.buffer+ 4, message.length - 5);
|
|
1805 params->handle_len = message.length - 1;
|
|
1806 sshv2_message_free (&message);
|
|
1807
|
|
1808 return (sshv2_get_file_size (request, file));
|
|
1809 }
|
|
1810
|
|
1811
|
|
1812 static int
|
58
|
1813 sshv2_put_file (gftp_request * request, const char *file, int fd,
|
1
|
1814 off_t startsize, off_t totalsize)
|
|
1815 {
|
489
|
1816 char *tempstr, *endpos;
|
1
|
1817 sshv2_params * params;
|
|
1818 sshv2_message message;
|
489
|
1819 size_t len;
|
1
|
1820 gint32 num;
|
64
|
1821 int ret;
|
1
|
1822
|
84
|
1823 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1824 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
169
|
1825 g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
|
1
|
1826 /* fd ignored for this protocol */
|
|
1827
|
|
1828 params = request->protocol_data;
|
399
|
1829 params->offset = startsize;
|
1
|
1830
|
489
|
1831 len = 8;
|
|
1832 tempstr = sshv2_initialize_string_with_path (request, file, &len, &endpos);
|
1
|
1833
|
|
1834 if (startsize > 0)
|
|
1835 num = htonl (SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_APPEND);
|
|
1836 else
|
|
1837 num = htonl (SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
|
489
|
1838 memcpy (endpos, &num, 4);
|
1
|
1839
|
489
|
1840 ret = sshv2_send_command (request, SSH_FXP_OPEN, tempstr, len);
|
1
|
1841
|
|
1842 g_free (tempstr);
|
84
|
1843 if (ret < 0)
|
|
1844 return (ret);
|
489
|
1845
|
|
1846 ret = sshv2_read_status_response (request, &message, -1, SSH_FXP_STATUS,
|
|
1847 SSH_FXP_HANDLE);
|
|
1848 if (ret < 0)
|
|
1849 return (ret);
|
1
|
1850
|
|
1851 if (message.length - 4 > SSH_MAX_HANDLE_SIZE)
|
|
1852 {
|
186
|
1853 request->logging_function (gftp_logging_error, request,
|
1
|
1854 _("Error: Message size %d too big from server\n"),
|
|
1855 message.length - 4);
|
|
1856 sshv2_message_free (&message);
|
|
1857 gftp_disconnect (request);
|
84
|
1858 return (GFTP_ERETRYABLE);
|
1
|
1859
|
|
1860 }
|
|
1861
|
|
1862 memset (params->handle, 0, 4);
|
321
|
1863 memcpy (params->handle + 4, message.buffer + 4, message.length - 5);
|
1
|
1864 params->handle_len = message.length - 1;
|
|
1865 sshv2_message_free (&message);
|
|
1866
|
|
1867 return (0);
|
|
1868 }
|
|
1869
|
|
1870
|
58
|
1871 static ssize_t
|
1
|
1872 sshv2_get_next_file_chunk (gftp_request * request, char *buf, size_t size)
|
|
1873 {
|
|
1874 sshv2_params * params;
|
|
1875 sshv2_message message;
|
|
1876 gint32 num;
|
84
|
1877 int ret;
|
1
|
1878
|
|
1879 #ifdef G_HAVE_GINT64
|
|
1880 gint64 offset;
|
|
1881 #else
|
|
1882 gint32 offset;
|
|
1883 #endif
|
|
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
|
|
1902 #ifdef G_HAVE_GINT64
|
|
1903 offset = hton64 (params->offset);
|
|
1904 memcpy (params->read_buffer + params->handle_len, &offset, 8);
|
|
1905 #else
|
|
1906 memset (params->read_buffer + params->handle_len, 0, 4);
|
|
1907 offset = htonl (params->offset);
|
|
1908 memcpy (params->read_buffer + params->handle_len + 4, &offset, 4);
|
|
1909 #endif
|
|
1910
|
|
1911 num = htonl (size);
|
|
1912 memcpy (params->read_buffer + params->handle_len + 8, &num, 4);
|
|
1913
|
489
|
1914 if (sshv2_send_command (request, SSH_FXP_READ, params->read_buffer,
|
|
1915 params->handle_len + 12) < 0)
|
84
|
1916 return (GFTP_ERETRYABLE);
|
1
|
1917
|
|
1918 memset (&message, 0, sizeof (message));
|
84
|
1919 if ((ret = sshv2_read_response (request, &message, -1)) != SSH_FXP_DATA)
|
1
|
1920 {
|
84
|
1921 if (ret < 0)
|
|
1922 return (ret);
|
|
1923
|
1
|
1924 message.pos += 4;
|
|
1925 if ((num = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
84
|
1926 return (num);
|
1
|
1927 sshv2_message_free (&message);
|
498
|
1928
|
|
1929 if (num == SSH_FX_FAILURE)
|
|
1930 return (GFTP_ERETRYABLE);
|
|
1931 else if (num != SSH_FX_EOF)
|
493
|
1932 return (sshv2_wrong_response (request, &message));
|
|
1933
|
1
|
1934 return (0);
|
|
1935 }
|
|
1936
|
|
1937 memcpy (&num, message.buffer + 4, 4);
|
|
1938 num = ntohl (num);
|
|
1939 if (num > size)
|
|
1940 {
|
186
|
1941 request->logging_function (gftp_logging_error, request,
|
1
|
1942 _("Error: Message size %d too big from server\n"),
|
|
1943 num);
|
|
1944 sshv2_message_free (&message);
|
|
1945 gftp_disconnect (request);
|
84
|
1946 return (GFTP_ERETRYABLE);
|
1
|
1947
|
|
1948 }
|
|
1949
|
|
1950 memcpy (buf, message.buffer + 8, num);
|
|
1951 sshv2_message_free (&message);
|
|
1952 params->offset += num;
|
|
1953 return (num);
|
|
1954 }
|
|
1955
|
|
1956
|
58
|
1957 static ssize_t
|
1
|
1958 sshv2_put_next_file_chunk (gftp_request * request, char *buf, size_t size)
|
|
1959 {
|
|
1960 sshv2_params * params;
|
|
1961 sshv2_message message;
|
380
|
1962 char tempstr[32768];
|
1
|
1963 gint32 num;
|
321
|
1964 size_t len;
|
1
|
1965 int ret;
|
|
1966
|
|
1967 #ifdef G_HAVE_GINT64
|
|
1968 gint64 offset;
|
|
1969 #else
|
|
1970 gint32 offset;
|
|
1971 #endif
|
|
1972
|
84
|
1973 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
|
1974 g_return_val_if_fail (request->protonum == GFTP_SSHV2_NUM, GFTP_EFATAL);
|
169
|
1975 g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
|
84
|
1976 g_return_val_if_fail (buf != NULL, GFTP_EFATAL);
|
|
1977 g_return_val_if_fail (size <= 32500, GFTP_EFATAL);
|
1
|
1978
|
|
1979 params = request->protocol_data;
|
|
1980
|
|
1981 memcpy (tempstr, params->handle, params->handle_len);
|
|
1982
|
|
1983 num = htonl (params->id++);
|
|
1984 memcpy (tempstr, &num, 4);
|
|
1985
|
|
1986 #ifdef G_HAVE_GINT64
|
|
1987 offset = hton64 (params->offset);
|
|
1988 memcpy (tempstr + params->handle_len, &offset, 8);
|
|
1989 #else
|
|
1990 memset (tempstr + params->handle_len, 0, 4);
|
|
1991 offset = htonl (params->offset);
|
|
1992 memcpy (tempstr + params->handle_len + 4, &offset, 4);
|
|
1993 #endif
|
|
1994
|
|
1995 num = htonl (size);
|
|
1996 memcpy (tempstr + params->handle_len + 8, &num, 4);
|
|
1997 memcpy (tempstr + params->handle_len + 12, buf, size);
|
|
1998
|
321
|
1999 len = params->handle_len + size + 12;
|
|
2000 if (sshv2_send_command (request, SSH_FXP_WRITE, tempstr, len) < 0)
|
1
|
2001 {
|
|
2002 g_free (tempstr);
|
84
|
2003 return (GFTP_ERETRYABLE);
|
1
|
2004 }
|
|
2005
|
|
2006 memset (&message, 0, sizeof (message));
|
|
2007 params->dont_log_status = 1;
|
58
|
2008 ret = sshv2_read_response (request, &message, -1);
|
84
|
2009 if (ret < 0)
|
|
2010 return (ret);
|
|
2011
|
1
|
2012 params->dont_log_status = 0;
|
|
2013 if (ret != SSH_FXP_STATUS)
|
493
|
2014 return (sshv2_wrong_response (request, &message));
|
1
|
2015
|
|
2016 message.pos += 4;
|
|
2017 if ((num = sshv2_buffer_get_int32 (request, &message, SSH_FX_OK)) < 0)
|
84
|
2018 return (GFTP_ERETRYABLE);
|
1
|
2019 sshv2_message_free (&message);
|
|
2020
|
|
2021 if (num == SSH_FX_EOF)
|
|
2022 return (0);
|
|
2023 else if (num != SSH_FX_OK)
|
84
|
2024 return (GFTP_ERETRYABLE);
|
1
|
2025
|
|
2026 params->offset += size;
|
|
2027 return (size);
|
|
2028 }
|
|
2029
|
48
|
2030
|
177
|
2031 static int
|
58
|
2032 sshv2_set_config_options (gftp_request * request)
|
|
2033 {
|
325
|
2034 intptr_t ssh_need_userpass;
|
58
|
2035
|
122
|
2036 gftp_lookup_request_option (request, "ssh_need_userpass", &ssh_need_userpass);
|
58
|
2037 request->need_userpass = ssh_need_userpass;
|
177
|
2038 return (0);
|
58
|
2039 }
|
|
2040
|
|
2041
|
63
|
2042 static void
|
|
2043 sshv2_swap_socks (gftp_request * dest, gftp_request * source)
|
|
2044 {
|
|
2045 sshv2_params * sparams, * dparams;
|
|
2046
|
|
2047 sparams = source->protocol_data;
|
|
2048 dparams = dest->protocol_data;
|
|
2049 dparams->id = sparams->id;
|
|
2050 }
|
|
2051
|
|
2052
|
122
|
2053 void
|
|
2054 sshv2_register_module (void)
|
|
2055 {
|
|
2056 gftp_register_config_vars (config_vars);
|
|
2057 }
|
|
2058
|
|
2059
|
173
|
2060 int
|
48
|
2061 sshv2_init (gftp_request * request)
|
|
2062 {
|
|
2063 sshv2_params * params;
|
|
2064
|
173
|
2065 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
|
48
|
2066
|
|
2067 request->protonum = GFTP_SSHV2_NUM;
|
|
2068 request->init = sshv2_init;
|
309
|
2069 request->copy_param_options = NULL;
|
48
|
2070 request->destroy = sshv2_destroy;
|
168
|
2071 request->read_function = gftp_fd_read;
|
|
2072 request->write_function = gftp_fd_write;
|
48
|
2073 request->connect = sshv2_connect;
|
168
|
2074 request->post_connect = NULL;
|
48
|
2075 request->disconnect = sshv2_disconnect;
|
|
2076 request->get_file = sshv2_get_file;
|
|
2077 request->put_file = sshv2_put_file;
|
|
2078 request->transfer_file = NULL;
|
|
2079 request->get_next_file_chunk = sshv2_get_next_file_chunk;
|
|
2080 request->put_next_file_chunk = sshv2_put_next_file_chunk;
|
|
2081 request->end_transfer = sshv2_end_transfer;
|
|
2082 request->abort_transfer = sshv2_end_transfer; /* NOTE: uses sshv2_end_transfer */
|
500
|
2083 request->stat_filename = sshv2_stat_filename;
|
48
|
2084 request->list_files = sshv2_list_files;
|
|
2085 request->get_next_file = sshv2_get_next_file;
|
485
|
2086 request->get_next_dirlist_line = NULL;
|
48
|
2087 request->get_file_size = sshv2_get_file_size;
|
|
2088 request->chdir = sshv2_chdir;
|
|
2089 request->rmdir = sshv2_rmdir;
|
|
2090 request->rmfile = sshv2_rmfile;
|
|
2091 request->mkdir = sshv2_mkdir;
|
|
2092 request->rename = sshv2_rename;
|
|
2093 request->chmod = sshv2_chmod;
|
|
2094 request->set_file_time = sshv2_set_file_time;
|
|
2095 request->site = NULL;
|
|
2096 request->parse_url = NULL;
|
58
|
2097 request->set_config_options = sshv2_set_config_options;
|
63
|
2098 request->swap_socks = sshv2_swap_socks;
|
48
|
2099 request->url_prefix = "ssh2";
|
|
2100 request->need_hostport = 1;
|
122
|
2101 request->need_userpass = 1;
|
48
|
2102 request->use_cache = 1;
|
|
2103 request->always_connected = 0;
|
|
2104 request->protocol_data = g_malloc0 (sizeof (sshv2_params));
|
122
|
2105 request->server_type = GFTP_DIRTYPE_UNIX;
|
48
|
2106
|
|
2107 params = request->protocol_data;
|
|
2108 params->id = 1;
|
173
|
2109
|
177
|
2110 return (gftp_set_config_options (request));
|
48
|
2111 }
|
487
|
2112
|