Skip to content

Commit 7364b73

Browse files
mssalvatoreNikratio
authored andcommitted
Gracefully handle multiple spaces in ssh_command option (#169)
When using the "ssh_command" option, commands with multiple spaces in a row will not be properly parsed. Example: Properly parsed: ssh_command = "ssh -o IdentityFile=~/.ssh/id_rsa" Improperly parsed: ssh_command = "ssh -o IdentityFile=~/.ssh/id_rsa" This commit changes the ssh_command parsing logic so that both of the above examples are considered valid and properly handled. Fixes: #114.
1 parent 1dbc89f commit 7364b73

2 files changed

Lines changed: 50 additions & 29 deletions

File tree

ChangeLog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Unreleased Changes
22
------------------
33

44
* Fixed "-o idmap=user" to map both UID and GID on all OSs.
5+
* Fixed improper handling of sequential spaces spaces in "ssh_command" option
56

67
Release 3.5.1 (2018-12-22)
78
--------------------------

sshfs.c

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,40 +3596,60 @@ static int read_password(void)
35963596
return 0;
35973597
}
35983598

3599+
// Behaves similarly to strtok(), but allows for the ' ' delimiter to be escaped
3600+
// by '\ '.
3601+
static char *tokenize_on_space(char *str)
3602+
{
3603+
static char *pos = NULL;
3604+
char *start = NULL;
3605+
3606+
if (str)
3607+
pos = str;
3608+
3609+
if (!pos)
3610+
return NULL;
3611+
3612+
// trim any leading spaces
3613+
while (*pos == ' ')
3614+
pos++;
3615+
3616+
start = pos;
3617+
3618+
while (pos && *pos != '\0') {
3619+
// break on space, but not on '\ '
3620+
if (*pos == ' ' && *(pos - 1) != '\\') {
3621+
break;
3622+
}
3623+
pos++;
3624+
}
3625+
3626+
if (*pos == '\0') {
3627+
pos = NULL;
3628+
}
3629+
else {
3630+
*pos = '\0';
3631+
pos++;
3632+
}
3633+
3634+
return start;
3635+
}
3636+
35993637
static void set_ssh_command(void)
36003638
{
3601-
char *s;
3602-
char *d;
3639+
char *token = NULL;
36033640
int i = 0;
3604-
int end = 0;
3605-
3606-
d = sshfs.ssh_command;
3607-
s = sshfs.ssh_command;
3608-
while (!end) {
3609-
switch (*s) {
3610-
case '\0':
3611-
end = 1;
3612-
case ' ':
3613-
*d = '\0';
3614-
if (i == 0) {
3615-
replace_arg(&sshfs.ssh_args.argv[0],
3616-
sshfs.ssh_command);
3617-
} else {
3618-
if (fuse_opt_insert_arg(&sshfs.ssh_args, i,
3619-
sshfs.ssh_command) == -1)
3620-
_exit(1);
3621-
}
3622-
i++;
3623-
d = sshfs.ssh_command;
3624-
break;
36253641

3626-
case '\\':
3627-
if (s[1])
3628-
s++;
3629-
default:
3630-
*d++ = *s;
3642+
token = tokenize_on_space(sshfs.ssh_command);
3643+
while (token != NULL) {
3644+
if (i == 0) {
3645+
replace_arg(&sshfs.ssh_args.argv[0], token);
3646+
} else {
3647+
if (fuse_opt_insert_arg(&sshfs.ssh_args, i, token) == -1)
3648+
_exit(1);
36313649
}
3632-
s++;
3650+
i++;
3651+
3652+
token = tokenize_on_space(NULL);
36333653
}
36343654
}
36353655

0 commit comments

Comments
 (0)