1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-08-05 20:55:47 +03:00

scp: option to not quote paths (#803)

A new flag named `LIBSSH2_FLAG_QUOTE_PATHS` has been added, to make
libssh2 not quote file paths sent to the remote's scp subsystem. Some
custom ssh daemons cannot handle quoted paths, and this makes this flag
useful.

Authored-by: Jörgen Sigvardsson <jorgen.sigvardsson@westermo.com>
This commit is contained in:
Jörgen Sigvardsson
2023-04-15 18:11:19 +02:00
committed by GitHub
parent 31e6d95d01
commit 0a500b3554
4 changed files with 40 additions and 8 deletions

View File

@@ -372,6 +372,7 @@ typedef struct _LIBSSH2_SK_SIG_INFO {
/* flags */ /* flags */
#define LIBSSH2_FLAG_SIGPIPE 1 #define LIBSSH2_FLAG_SIGPIPE 1
#define LIBSSH2_FLAG_COMPRESS 2 #define LIBSSH2_FLAG_COMPRESS 2
#define LIBSSH2_FLAG_QUOTE_PATHS 3
typedef struct _LIBSSH2_SESSION LIBSSH2_SESSION; typedef struct _LIBSSH2_SESSION LIBSSH2_SESSION;
typedef struct _LIBSSH2_CHANNEL LIBSSH2_CHANNEL; typedef struct _LIBSSH2_CHANNEL LIBSSH2_CHANNEL;

View File

@@ -578,8 +578,9 @@ struct _LIBSSH2_PUBLICKEY
#define LIBSSH2_SCP_RESPONSE_BUFLEN 256 #define LIBSSH2_SCP_RESPONSE_BUFLEN 256
struct flags { struct flags {
int sigpipe; /* LIBSSH2_FLAG_SIGPIPE */ int sigpipe; /* LIBSSH2_FLAG_SIGPIPE */
int compress; /* LIBSSH2_FLAG_COMPRESS */ int compress; /* LIBSSH2_FLAG_COMPRESS */
int quote_paths; /* LIBSSH2_FLAG_QUOTE_PATHS */
}; };
struct _LIBSSH2_SESSION struct _LIBSSH2_SESSION

View File

@@ -299,9 +299,21 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, libssh2_struct_stat * sb)
"scp -%sf ", sb ? "p" : ""); "scp -%sf ", sb ? "p" : "");
cmd_len = strlen((char *)session->scpRecv_command); cmd_len = strlen((char *)session->scpRecv_command);
cmd_len += shell_quotearg(path,
&session->scpRecv_command[cmd_len], if(!session->flag.quote_paths) {
session->scpRecv_command_len - cmd_len); size_t path_len;
path_len = strlen(path);
/* no NUL-termination neeed, so memcpy will do */
memcpy(&session->scpRecv_command[cmd_len], path, path_len);
cmd_len += path_len;
}
else {
cmd_len += shell_quotearg(path,
&session->scpRecv_command[cmd_len],
session->scpRecv_command_len - cmd_len);
}
/* the command to exec should _not_ be NUL-terminated */ /* the command to exec should _not_ be NUL-terminated */
session->scpRecv_command_len = cmd_len; session->scpRecv_command_len = cmd_len;
@@ -860,9 +872,22 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
"scp -%st ", (mtime || atime) ? "p" : ""); "scp -%st ", (mtime || atime) ? "p" : "");
cmd_len = strlen((char *)session->scpSend_command); cmd_len = strlen((char *)session->scpSend_command);
cmd_len += shell_quotearg(path,
&session->scpSend_command[cmd_len], if(!session->flag.quote_paths) {
session->scpSend_command_len - cmd_len); size_t path_len;
path_len = strlen(path);
/* no NUL-termination neeed, so memcpy will do */
memcpy(&session->scpSend_command[cmd_len], path, path_len);
cmd_len += path_len;
}
else {
cmd_len += shell_quotearg(path,
&session->scpSend_command[cmd_len],
session->scpSend_command_len - cmd_len);
}
/* the command to exec should _not_ be NUL-terminated */ /* the command to exec should _not_ be NUL-terminated */
session->scpSend_command_len = cmd_len; session->scpSend_command_len = cmd_len;

View File

@@ -524,6 +524,8 @@ libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_alloc)),
session->api_timeout = 0; /* timeout-free API by default */ session->api_timeout = 0; /* timeout-free API by default */
session->api_block_mode = 1; /* blocking API by default */ session->api_block_mode = 1; /* blocking API by default */
session->packet_read_timeout = LIBSSH2_DEFAULT_READ_TIMEOUT; session->packet_read_timeout = LIBSSH2_DEFAULT_READ_TIMEOUT;
session->flag.quote_paths = 1; /* default behavior is to quote paths
for the scp subsystem */
_libssh2_debug((session, LIBSSH2_TRACE_TRANS, _libssh2_debug((session, LIBSSH2_TRACE_TRANS,
"New session resource allocated")); "New session resource allocated"));
_libssh2_init_if_needed(); _libssh2_init_if_needed();
@@ -1409,6 +1411,9 @@ libssh2_session_flag(LIBSSH2_SESSION * session, int flag, int value)
case LIBSSH2_FLAG_COMPRESS: case LIBSSH2_FLAG_COMPRESS:
session->flag.compress = value; session->flag.compress = value;
break; break;
case LIBSSH2_FLAG_QUOTE_PATHS:
session->flag.quote_paths = value;
break;
default: default:
/* unknown flag */ /* unknown flag */
return LIBSSH2_ERROR_INVAL; return LIBSSH2_ERROR_INVAL;