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