Re: 2.0.2 released ...
- From: Christophe Fergeau <teuf users sourceforge net>
- To: gnome-vfs-list gnome org
- Subject: Re: 2.0.2 released ...
- Date: 16 Sep 2002 17:10:34 +0200
Here is the patch for the ssh and ftp modules
? gnome-vfs.diff
? login.diff
? modules.diff
? modules/vfolder
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnome-vfs/ChangeLog,v
retrieving revision 1.1334.2.48
diff -u -r1.1334.2.48 ChangeLog
--- ChangeLog 13 Sep 2002 13:39:18 -0000 1.1334.2.48
+++ ChangeLog 16 Sep 2002 14:48:02 -0000
@@ -1,3 +1,15 @@
+2002-09-16 Christophe Fergeau <teuf users sourceforge net>
+ * modules/ftp-method.c: fixed do_read to correctly report EOF when
+ read return 0, moved the login code to a separate function to
+ make it easier to try to login several times when the
+ call to an authentification callback in in place
+ * modules/ssh-method.c: fixed do_read to correctly report EOF when
+ read return 0, correctly reports "login failed" if the remote site
+ expected a password (which is not handled right now), fixed a
+ buffer overflow, fixed connection when no username was specified
+ or when the locale wasn't C, escape the filenames passed to the
+ shell using glib
+
2002-09-13 Ghee Teo <ghee teo sun com>
* Fixes #92132 in modules/http-method.c which leaks file descriptor
on a bad page, error 405.
Index: modules/ftp-method.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/modules/ftp-method.c,v
retrieving revision 1.87
diff -u -r1.87 ftp-method.c
--- modules/ftp-method.c 8 May 2002 19:04:51 -0000 1.87
+++ modules/ftp-method.c 16 Sep 2002 14:48:02 -0000
@@ -42,15 +42,18 @@
#include <netinet/in.h>
#include <arpa/inet.h>
+
#include <libgnomevfs/gnome-vfs-context.h>
#include <libgnomevfs/gnome-vfs-inet-connection.h>
#include <libgnomevfs/gnome-vfs-iobuf.h>
#include <libgnomevfs/gnome-vfs-method.h>
#include <libgnomevfs/gnome-vfs-mime.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
-#include <libgnomevfs/gnome-vfs-module-shared.h>
#include <libgnomevfs/gnome-vfs-module.h>
+#include <libgnomevfs/gnome-vfs-module-callback-module-api.h>
+#include <libgnomevfs/gnome-vfs-module-shared.h>
#include <libgnomevfs/gnome-vfs-parse-ls.h>
+#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <stdio.h> /* for sscanf */
#include <stdlib.h> /* for atoi */
@@ -504,12 +507,32 @@
}
+
+static GnomeVFSResult ftp_login(FtpConnection *conn,
+ const char *user, const char *password)
+{
+ gchar *tmpstring;
+ GnomeVFSResult result;
+
+ tmpstring = g_strdup_printf ("USER %s", user);
+ result = do_basic_command (conn, tmpstring);
+ g_free (tmpstring);
+
+ if (IS_300 (conn->response_code)) {
+ tmpstring = g_strdup_printf ("PASS %s", password);
+ result = do_basic_command (conn, tmpstring);
+ g_free (tmpstring);
+ }
+
+ return result;
+}
+
+
static GnomeVFSResult
ftp_connection_create (FtpConnection **connptr, GnomeVFSURI *uri, GnomeVFSContext *context)
{
FtpConnection *conn = g_new0 (FtpConnection, 1);
GnomeVFSResult result;
- gchar *tmpstring;
gint port = control_port;
const gchar *user = anon_user;
const gchar *pass = anon_pass;
@@ -573,16 +596,8 @@
return result;
}
- tmpstring = g_strdup_printf ("USER %s", user);
- result = do_basic_command (conn, tmpstring);
- g_free (tmpstring);
-
- if (IS_300 (conn->response_code)) {
- tmpstring = g_strdup_printf ("PASS %s", pass);
- result = do_basic_command (conn, tmpstring);
- g_free (tmpstring);
- }
-
+ result = ftp_login(conn, user, pass);
+
if (result != GNOME_VFS_OK) {
/* login failed */
g_warning ("FTP server said: \"%d %s\"\n", conn->response_code,
@@ -592,6 +607,7 @@
gnome_vfs_uri_unref (conn->uri);
g_string_free (conn->response_buffer, TRUE);
g_free (conn);
+
return result;
}
@@ -785,7 +801,7 @@
gboolean
do_is_local (GnomeVFSMethod *method,
const GnomeVFSURI *uri)
-{
+{
return FALSE;
}
@@ -860,7 +876,7 @@
GnomeVFSContext *context)
{
FtpConnection *conn = (FtpConnection * )method_handle;
-
+ GnomeVFSResult result;
#if 0
/*
if (conn->operation != FTP_READ) {
@@ -870,7 +886,11 @@
g_print ("do_read (%p)\n", method_handle);
#endif
- return gnome_vfs_iobuf_read (conn->data_iobuf, buffer, num_bytes, bytes_read);
+ result = gnome_vfs_iobuf_read (conn->data_iobuf, buffer, num_bytes, bytes_read);
+ if (*bytes_read == 0) {
+ result = GNOME_VFS_ERROR_EOF;
+ }
+ return result;
}
static GnomeVFSResult
Index: modules/ssh-method.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/modules/ssh-method.c,v
retrieving revision 1.8.2.1
diff -u -r1.8.2.1 ssh-method.c
--- modules/ssh-method.c 19 Jul 2002 07:57:11 -0000 1.8.2.1
+++ modules/ssh-method.c 16 Sep 2002 14:48:02 -0000
@@ -21,7 +21,6 @@
Author: Ian McKellar <yakk yakk net> */
#include <config.h>
-
#include <errno.h>
#include <glib/gstrfuncs.h>
#include <libgnomevfs/gnome-vfs-cancellation.h>
@@ -37,6 +36,8 @@
#include <unistd.h>
#include <signal.h>
+#define LINE_LENGTH 4096 /* max line length we'll grok */
+
typedef struct {
GnomeVFSMethodHandle method_handle;
GnomeVFSURI *uri;
@@ -47,6 +48,7 @@
GnomeVFSOpenMode open_mode;
int read_fd;
int write_fd;
+ int error_fd;
pid_t pid;
} SshHandle;
@@ -145,6 +147,86 @@
NULL /* create_symbolic_link */
};
+
+static GnomeVFSResult
+ssh_write (SshHandle *handle,
+ gconstpointer buffer,
+ GnomeVFSFileSize num_bytes,
+ GnomeVFSFileSize *bytes_written);
+
+
+static gboolean
+ssh_data_available_on_stderr (SshHandle *handle)
+{
+ struct timeval timeout;
+ fd_set set;
+ int max;
+
+ /* This function checks if there was data output by ssh
+ * on stderr to detect if we could successfully connect
+ * (without that, trying to read from stderr may block)
+ * It first check both stderr and stdout because if we
+ * have data on stdout, we assume there will be nothing
+ * output on stderr (we don't want to block on the first
+ * select for too long if there will be nothing output on
+ * stderr)
+ * FIXME: that's some quite ugly code only to detect if
+ * a try to read from stderr will block. Maybe there is
+ * a cleaner way to do that
+ */
+ FD_ZERO(&set);
+ FD_SET(handle->read_fd, &set);
+ FD_SET(handle->error_fd, &set);
+ timeout.tv_sec = 3;
+ timeout.tv_usec = 0;
+ if (handle->error_fd >= handle->read_fd) {
+ max = handle->error_fd;
+ } else {
+ max = handle->read_fd;
+ }
+ if (select(max+1,&set, NULL, NULL, &timeout) <= 0){
+ return FALSE;
+ }
+ FD_CLR(handle->read_fd, &set);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0;
+ if (select(handle->error_fd+1, &set, NULL, NULL, &timeout) <= 0) {
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static GnomeVFSResult
+ssh_check_connection (SshHandle *handle)
+{
+ GIOChannel *channel;
+ GIOStatus result;
+ gchar *line;
+
+ if (!ssh_data_available_on_stderr(handle)) {
+ return GNOME_VFS_OK;
+ }
+
+ channel = g_io_channel_unix_new(handle->error_fd);
+ result = g_io_channel_read_line(channel, &line, NULL, NULL, NULL);
+ if (result != G_IO_STATUS_NORMAL) {
+ return GNOME_VFS_OK;
+ }
+ g_io_channel_shutdown(channel, FALSE, NULL);
+ g_io_channel_unref(channel);
+
+ if (strcmp("Permission denied.\r\n", line) == 0) {
+ g_free(line);
+ return GNOME_VFS_ERROR_ACCESS_DENIED;
+ }
+
+ /* FIXME: which other errors can ssh return ? */
+ g_print("Unknown error: %s\n", line);
+ g_free(line);
+ return GNOME_VFS_ERROR_GENERIC;
+}
+
+
/* FIXME: does this like FDs? */
static GnomeVFSResult
ssh_connect (SshHandle **handle_return,
@@ -153,17 +235,22 @@
char ** argv;
SshHandle *handle;
char *command_line;
+ const gchar *username;
int argc;
GError *gerror = NULL;
+ GnomeVFSResult result;
-
- command_line = g_strconcat ("ssh -oBatchmode=yes -x -l ",
- gnome_vfs_uri_get_user_name (uri),
+ username = gnome_vfs_uri_get_user_name(uri);
+ if (username == NULL) {
+ username = g_get_user_name();
+ }
+
+ command_line = g_strconcat ("ssh -oBatchMode=yes -x -l ",
+ username,
" ", gnome_vfs_uri_get_host_name (uri),
- " ", command,
+ " ", "\"LC_ALL=C;", command,"\"",
NULL);
-
g_shell_parse_argv (command_line, &argc, &argv, &gerror);
g_free (command_line);
if (gerror) {
@@ -177,29 +264,39 @@
handle->uri = uri;
g_spawn_async_with_pipes (NULL, argv, NULL,
- G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
- NULL, NULL,
- &handle->pid, &handle->write_fd, &handle->read_fd,
- NULL, &gerror);
+ G_SPAWN_SEARCH_PATH, NULL, NULL,
+ &handle->pid,
+ &handle->write_fd, &handle->read_fd,
+ &handle->error_fd, &gerror);
g_strfreev (argv);
if (gerror) {
g_warning (gerror->message);
g_free (handle);
+ handle = NULL;
+ return GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE;
}
gnome_vfs_uri_ref (handle->uri);
*handle_return = handle;
- return GNOME_VFS_OK;
+ result = ssh_check_connection(handle);
+ if (result != GNOME_VFS_OK) {
+ gnome_vfs_uri_unref (handle->uri);
+ g_free (handle);
+ handle = NULL;
+ }
+ return result;
}
+
static GnomeVFSResult
ssh_destroy (SshHandle *handle)
{
close (handle->read_fd);
close (handle->write_fd);
+ close (handle->error_fd);
gnome_vfs_uri_unref (handle->uri);
kill (handle->pid, SIGINT);
g_free (handle);
@@ -256,26 +353,6 @@
}
#if 0
-static char *ssh_escape (const char *string)
-{
- char *new_str;
- int i,j;
-
- new_str = g_malloc0 (strlen (string)*2+3);
-
- new_str[0]='\'';
-
- for (i=0,j=1; string[i] != '\0'; i++, j++) {
- if (string[i] == '\'') {
- new_str[j] = '\\';
- j++;
- }
- new_str[j] = string[i];
- }
-
- return new_str;
-}
-
static GnomeVFSResult
ssh_send (SshHandle *handle,
const char *string)
@@ -305,27 +382,23 @@
GnomeVFSResult result = GNOME_VFS_OK;
char *cmd;
SshHandle *handle = NULL;
- char *name;
-
- /* FIXME: escape for shell */
- name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
if (mode == GNOME_VFS_OPEN_READ) {
- /* FIXME: escape for shell */
- cmd = g_strdup_printf ("cat '%s'", name);
+ char *name;
+ char *quoted_name;
+ name = gnome_vfs_unescape_string (uri->text,
+ G_DIR_SEPARATOR_S);
+ quoted_name = g_shell_quote(name);
+ g_free(name);
+
+ cmd = g_strdup_printf ("cat %s", quoted_name);
result = ssh_connect (&handle, uri, cmd);
g_free (cmd);
-
+ g_free (quoted_name);
if (result != GNOME_VFS_OK) {
- g_free (name);
return result;
- }
-
- } else if (mode == GNOME_VFS_OPEN_WRITE) {
- g_free (name);
- return GNOME_VFS_ERROR_INVALID_OPEN_MODE;
+ }
} else {
- g_free (name);
return GNOME_VFS_ERROR_INVALID_OPEN_MODE;
}
@@ -333,9 +406,7 @@
handle->type = SSH_FILE;
*method_handle = (GnomeVFSMethodHandle *)handle;
- g_free (name);
-
- return result;
+ return GNOME_VFS_OK;
}
static GnomeVFSResult
@@ -351,33 +422,30 @@
char *cmd;
GnomeVFSResult result;
char *name;
-
- /* FIXME: escape for shell */
- name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
+ char *quoted_name;
if (mode != GNOME_VFS_OPEN_WRITE) {
- g_free (name);
return GNOME_VFS_ERROR_INVALID_OPEN_MODE;
}
+ name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
+ quoted_name = g_shell_quote(name);
- /* FIXME: escape for shell */
- cmd = g_strdup_printf ("cat > '%s'", name);
+ cmd = g_strdup_printf ("cat > %s", quoted_name);
result = ssh_connect (&handle, uri, cmd);
g_free (cmd);
+ g_free (name);
+ g_free (quoted_name);
if (result != GNOME_VFS_OK) {
- g_free (name);
return result;
}
/* FIXME: set perm */
-
handle->open_mode = mode;
handle->type = SSH_FILE;
*method_handle = (GnomeVFSMethodHandle *)handle;
- g_free (name);
return result;
}
@@ -397,8 +465,14 @@
GnomeVFSFileSize *bytes_read,
GnomeVFSContext *context)
{
- return ssh_read ((SshHandle *)method_handle, buffer, num_bytes,
- bytes_read);
+ GnomeVFSResult result;
+
+ result = ssh_read ((SshHandle *)method_handle, buffer, num_bytes,
+ bytes_read);
+ if (*bytes_read == 0) {
+ result = GNOME_VFS_ERROR_EOF;
+ }
+ return result;
}
/* alternative impl:
@@ -427,17 +501,19 @@
char *cmd = NULL;
GnomeVFSResult result;
char *name;
+ char *quoted_name;
- /* FIXME: escape for shell */
name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
+ quoted_name = g_shell_quote(name);
- if (strlen (name) > 0) {
- cmd = g_strdup_printf ("ls -l '%s'", name);
+ if (*name != '\0') {
+ cmd = g_strdup_printf ("ls -l %s", quoted_name);
} else {
cmd = g_strdup_printf ("ls -l '/'");
}
-
+
result = ssh_connect (&handle, uri, cmd);
+ g_free (quoted_name);
g_free (name);
g_free (cmd);
@@ -449,7 +525,6 @@
handle->type = SSH_LIST;
*method_handle = (GnomeVFSMethodHandle *)handle;
- return result;
return GNOME_VFS_OK;
}
@@ -461,8 +536,6 @@
return ssh_destroy ((SshHandle *)method_handle);
}
-#define LINE_LENGTH 4096 /* max line length we'll grok */
-
static GnomeVFSResult
do_read_directory (GnomeVFSMethod *method,
GnomeVFSMethodHandle *method_handle,
@@ -470,10 +543,10 @@
GnomeVFSContext *context)
{
GnomeVFSResult result = GNOME_VFS_OK;
- char line[LINE_LENGTH];
+ char line[LINE_LENGTH+1];
char c;
int i=0;
- GnomeVFSFileSize j;
+ GnomeVFSFileSize bytes_read;
struct stat st;
char *tempfilename, *filename, *linkname;
@@ -482,11 +555,12 @@
filename = NULL;
linkname = NULL;
i = 0;
- j = 0;
+ bytes_read = 0;
while (i<LINE_LENGTH) {
- result = ssh_read ((SshHandle *)method_handle, &c, 1, &j);
- if (j == 0 || c == '\r' || c == '\n') {
+ result = ssh_read ((SshHandle *)method_handle, &c,
+ sizeof(char), &bytes_read);
+ if (bytes_read == 0 || c == '\r' || c == '\n') {
break;
}
@@ -497,12 +571,13 @@
line[i] = c;
i++;
}
-
+ /* Here we can have i == LINE_LENGTH which explains
+ * why the size of line is LINE_LENGTH+1
+ */
line[i] = 0;
if (i == 0) {
return GNOME_VFS_ERROR_EOF;
}
-
if (!gnome_vfs_parse_ls_lga (line, &st, &tempfilename, &linkname)) {
/* Maybe the file doesn't exist? */
if (strstr (line, "No such file or directory"))
@@ -556,12 +631,13 @@
char *cmd = NULL;
GnomeVFSResult result;
char *name;
+ char *quoted_name;
- /* FIXME: escape for shell */
name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
+ quoted_name = g_shell_quote (name);
- if (strlen (name) > 0) {
- cmd = g_strdup_printf ("ls -ld '%s' 2>&1", name);
+ if (*name != '\0') {
+ cmd = g_strdup_printf ("ls -ld %s 2>&1", quoted_name);
} else {
cmd = g_strdup_printf ("ls -ld '/' 2>&1");
}
@@ -569,6 +645,7 @@
result = ssh_connect (&handle, uri, cmd);
g_free (cmd);
g_free (name);
+ g_free (quoted_name);
if (result != GNOME_VFS_OK) {
return result;
@@ -595,12 +672,12 @@
char *cmd = NULL;
GnomeVFSResult result;
char *name;
+ char *quoted_name;
- /* FIXME: escape for shell */
name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
+ quoted_name = g_shell_quote (name);
- /* FIXME: escape for shell */
- cmd = g_strdup_printf ("mkdir '%s'", name);
+ cmd = g_strdup_printf ("mkdir %s", quoted_name);
result = ssh_connect (&handle, uri, cmd);
g_free (cmd);
g_free (name);
@@ -623,16 +700,18 @@
char *cmd = NULL;
GnomeVFSResult result;
gchar *name;
+ gchar *quoted_name;
name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
if (name == NULL)
return GNOME_VFS_ERROR_INVALID_URI;
+ quoted_name = g_shell_quote(name);
- /* FIXME: escape for shell */
- cmd = g_strdup_printf ("rm -rf '%s'", name);
+ cmd = g_strdup_printf ("rm -rf %s", quoted_name);
result = ssh_connect (&handle, uri, cmd);
g_free (cmd);
g_free (name);
+ g_free (quoted_name);
if (result != GNOME_VFS_OK) {
return result;
@@ -652,13 +731,14 @@
char *cmd = NULL;
GnomeVFSResult result;
gchar *name;
+ gchar *quoted_name;
name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
if (name == NULL)
return GNOME_VFS_ERROR_INVALID_URI;
+ quoted_name = g_shell_quote (name);
- /* FIXME: escape for shell */
- cmd = g_strdup_printf ("rm -rf '%s'", name);
+ cmd = g_strdup_printf ("rm -rf %s", quoted_name);
result = ssh_connect (&handle, uri, cmd);
g_free (cmd);
g_free (name);
@@ -692,6 +772,8 @@
char *encoded_dir;
char *dir;
char *new_name;
+ char *quoted_full_name;
+ char *quoted_new_name;
encoded_dir = gnome_vfs_uri_extract_dirname (uri);
dir = gnome_vfs_unescape_string (encoded_dir, G_DIR_SEPARATOR_S);
@@ -710,32 +792,35 @@
}
/* FIXME: escape for shell */
- cmd = g_strdup_printf ("mv '%s' '%s'", full_name,
- new_name);
+ quoted_new_name = g_shell_quote(new_name);
+ quoted_full_name = g_shell_quote(full_name);
+ cmd = g_strdup_printf ("mv %s %s", quoted_full_name,
+ quoted_new_name);
result = ssh_connect (&handle, uri, cmd);
g_free (cmd);
g_free (dir);
g_free (new_name);
+ g_free (quoted_new_name);
+ g_free (quoted_full_name);
+ g_free (full_name);
+
+ if (result != GNOME_VFS_OK) {
+ return result;
+ }
/* Read all info from remote host */
while (1) {
char c;
GnomeVFSResult res;
- GnomeVFSFileSize j;
- res = ssh_read (handle, &c, 1, &j);
- if (j == 0 || res != GNOME_VFS_OK)
+ GnomeVFSFileSize bytes_read;
+ res = ssh_read (handle, &c, 1, &bytes_read);
+ if (bytes_read == 0 || res != GNOME_VFS_OK)
break;
}
ssh_destroy (handle);
-
- if (result != GNOME_VFS_OK) {
- g_free (full_name);
- return result;
- }
}
- g_free (full_name);
return result;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]