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