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