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