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