add -F ssh_config option

This commit is contained in:
Ryo Nakamura
2023-08-04 13:31:10 +09:00
parent 3b26c7c719
commit 518aa42208
4 changed files with 23 additions and 7 deletions

View File

@@ -67,6 +67,7 @@ struct mscp_ssh_opts {
/* ssh options */ /* ssh options */
char login_name[MSCP_SSH_MAX_LOGIN_NAME]; /** ssh username */ char login_name[MSCP_SSH_MAX_LOGIN_NAME]; /** ssh username */
char port[MSCP_SSH_MAX_PORT_STR]; /** ssh port */ char port[MSCP_SSH_MAX_PORT_STR]; /** ssh port */
char config[PATH_MAX]; /** path to ssh_config, default ~/.ssh/config*/
char identity[MSCP_SSH_MAX_IDENTITY_PATH]; /** path to private key */ char identity[MSCP_SSH_MAX_IDENTITY_PATH]; /** path to private key */
char cipher[MSCP_SSH_MAX_CIPHER_STR]; /** cipher spec */ char cipher[MSCP_SSH_MAX_CIPHER_STR]; /** cipher spec */
char hmac[MSCP_SSH_MAX_HMAC_STR]; /** hmacp spec */ char hmac[MSCP_SSH_MAX_HMAC_STR]; /** hmacp spec */

View File

@@ -26,7 +26,7 @@ void usage(bool print_help) {
"\n" "\n"
"Usage: mscp [vqDHdNh] [-n nr_conns] [-m coremask] [-u max_startups]\n" "Usage: mscp [vqDHdNh] [-n nr_conns] [-m coremask] [-u max_startups]\n"
" [-s min_chunk_sz] [-S max_chunk_sz] [-a nr_ahead] [-b buf_sz]\n" " [-s min_chunk_sz] [-S max_chunk_sz] [-a nr_ahead] [-b buf_sz]\n"
" [-l login_name] [-p port] [-i identity_file]\n" " [-l login_name] [-p port] [-F ssh_config] [-i identity_file]\n"
" [-c cipher_spec] [-M hmac_spec] [-C compress] source ... target\n" " [-c cipher_spec] [-M hmac_spec] [-C compress] source ... target\n"
"\n"); "\n");
@@ -51,6 +51,8 @@ void usage(bool print_help) {
"\n" "\n"
" -l LOGIN_NAME login name\n" " -l LOGIN_NAME login name\n"
" -p PORT port number\n" " -p PORT port number\n"
" -F CONFIG path to user ssh config (default ~/.ssh/config)\n"
" if set to 'none', no config files will be read.\n"
" -i IDENTITY identity file for public key authentication\n" " -i IDENTITY identity file for public key authentication\n"
" -c CIPHER cipher spec\n" " -c CIPHER cipher spec\n"
" -M HMAC hmac spec\n" " -M HMAC hmac spec\n"
@@ -207,7 +209,7 @@ int main(int argc, char **argv)
memset(&o, 0, sizeof(o)); memset(&o, 0, sizeof(o));
o.severity = MSCP_SEVERITY_WARN; o.severity = MSCP_SEVERITY_WARN;
while ((ch = getopt(argc, argv, "n:m:u:s:S:a:b:vqDrl:p:i:c:M:C:HdNh")) != -1) { while ((ch = getopt(argc, argv, "n:m:u:s:S:a:b:vqDrl:p:i:F:c:M:C:HdNh")) != -1) {
switch (ch) { switch (ch) {
case 'n': case 'n':
o.nr_threads = atoi(optarg); o.nr_threads = atoi(optarg);
@@ -261,6 +263,9 @@ int main(int argc, char **argv)
} }
strncpy(s.port, optarg, MSCP_SSH_MAX_PORT_STR); strncpy(s.port, optarg, MSCP_SSH_MAX_PORT_STR);
break; break;
case 'F':
strncpy(s.config, optarg, PATH_MAX - 1);
break;
case 'i': case 'i':
if (strlen(optarg) > MSCP_SSH_MAX_IDENTITY_PATH - 1) { if (strlen(optarg) > MSCP_SSH_MAX_IDENTITY_PATH - 1) {
fprintf(stderr, "long identity path: %s\n", optarg); fprintf(stderr, "long identity path: %s\n", optarg);

View File

@@ -103,6 +103,7 @@ static PyObject *wrap_mscp_init(PyObject *self, PyObject *args, PyObject *kw)
/* mscp_ssh_opts */ /* mscp_ssh_opts */
"login_name", /* const char * */ "login_name", /* const char * */
"port", /* const char * */ "port", /* const char * */
"config", /* const char * */
"identity", /* const char * */ "identity", /* const char * */
"cipher", /* const char * */ "cipher", /* const char * */
@@ -116,9 +117,9 @@ static PyObject *wrap_mscp_init(PyObject *self, PyObject *args, PyObject *kw)
"enable_nagle", /* bool */ "enable_nagle", /* bool */
NULL, NULL,
}; };
const char *fmt = "si" "|" "ii" "kkk" "s" "iii" "sss" "sssss" "ipp"; const char *fmt = "si" "|" "ii" "kkk" "s" "iii" "ssss" "sssss" "ipp";
char *coremask = NULL; char *coremask = NULL;
char *login_name = NULL, *port = NULL, *identity = NULL; char *login_name = NULL, *port = NULL, *config = NULL, *identity = NULL;
char *cipher = NULL, *hmac = NULL, *compress = NULL; char *cipher = NULL, *hmac = NULL, *compress = NULL;
char *password = NULL, *passphrase = NULL; char *password = NULL, *passphrase = NULL;
@@ -148,6 +149,7 @@ static PyObject *wrap_mscp_init(PyObject *self, PyObject *args, PyObject *kw)
&i->mo.msg_fd, &i->mo.msg_fd,
&login_name, &login_name,
&port, &port,
&config,
&identity, &identity,
&cipher, &cipher,
&hmac, &hmac,
@@ -167,6 +169,8 @@ static PyObject *wrap_mscp_init(PyObject *self, PyObject *args, PyObject *kw)
strncpy(i->so.login_name, login_name, MSCP_SSH_MAX_LOGIN_NAME - 1); strncpy(i->so.login_name, login_name, MSCP_SSH_MAX_LOGIN_NAME - 1);
if (port) if (port)
strncpy(i->so.port, port, MSCP_SSH_MAX_PORT_STR - 1); strncpy(i->so.port, port, MSCP_SSH_MAX_PORT_STR - 1);
if (config)
strncpy(i->so.config, config, PATH_MAX - 1);
if (identity) if (identity)
strncpy(i->so.identity, identity, MSCP_SSH_MAX_IDENTITY_PATH - 1); strncpy(i->so.identity, identity, MSCP_SSH_MAX_IDENTITY_PATH - 1);
if (cipher) if (cipher)

View File

@@ -73,6 +73,12 @@ static int ssh_set_opts(ssh_session ssh, struct mscp_ssh_opts *opts)
} }
} }
if (is_specified(opts->config) &&
ssh_options_parse_config(ssh, opts->config) < 0) {
mscp_set_error("failed to parse ssh_config: %s", opts->config);
return -1;
}
return 0; return 0;
} }
@@ -149,14 +155,14 @@ static ssh_session ssh_init_session(const char *sshdst, struct mscp_ssh_opts *op
cb.userdata = opts; cb.userdata = opts;
ssh_set_callbacks(ssh, &cb); ssh_set_callbacks(ssh, &cb);
if (ssh_set_opts(ssh, opts) != 0)
goto free_out;
if (ssh_options_set(ssh, SSH_OPTIONS_HOST, sshdst) != SSH_OK) { if (ssh_options_set(ssh, SSH_OPTIONS_HOST, sshdst) != SSH_OK) {
mscp_set_error("failed to set destination host"); mscp_set_error("failed to set destination host");
goto free_out; goto free_out;
} }
if (ssh_set_opts(ssh, opts) != 0)
goto free_out;
if (ssh_connect(ssh) != SSH_OK) { if (ssh_connect(ssh) != SSH_OK) {
mscp_set_error("failed to connect ssh server: %s", ssh_get_error(ssh)); mscp_set_error("failed to connect ssh server: %s", ssh_get_error(ssh));
goto free_out; goto free_out;