summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyo Nakamura <upa@haeena.net>2024-02-05 21:13:11 +0900
committerRyo Nakamura <upa@haeena.net>2024-02-05 23:36:59 +0900
commit9908fb309d9388481769e1a558a1f8dc47c4dabf (patch)
tree6c126167a978c60a1735b3e57899ebae131d400a
parentc95e6a4fffe935330b4018e49f95fff5ad2cc551 (diff)
passing options via pointers in mscp_ssh_opts
We do not need static buf because we have already dropped python biding support.
-rw-r--r--include/mscp.h35
-rw-r--r--src/main.c53
-rw-r--r--src/mscp.c2
-rw-r--r--src/ssh.c46
4 files changed, 51 insertions, 85 deletions
diff --git a/include/mscp.h b/include/mscp.h
index 8d68032..92f6e9e 100644
--- a/include/mscp.h
+++ b/include/mscp.h
@@ -31,8 +31,6 @@
#define MSCP_DIRECTION_L2R 1 /** Indicates local to remote copy */
#define MSCP_DIRECTION_R2L 2 /** Indicates remote to local copy */
-#define MSCP_MAX_COREMASK_STR 64
-
/**
* @struct mscp_opts
* @brief Structure configuring mscp.
@@ -43,22 +41,13 @@ struct mscp_opts {
size_t min_chunk_sz; /** minimum chunk size (default 64MB) */
size_t max_chunk_sz; /** maximum chunk size (default file size/nr_threads) */
size_t buf_sz; /** buffer size, default 16k. */
- char coremask[MSCP_MAX_COREMASK_STR]; /** hex to specifiy usable cpu cores */
+ char *coremask; /** hex to specifiy usable cpu cores */
int max_startups; /** sshd MaxStartups concurrent connections */
int interval; /** interval between SSH connection attempts */
int severity; /** messaging severity. set MSCP_SERVERITY_* */
};
-#define MSCP_SSH_MAX_LOGIN_NAME 64
-#define MSCP_SSH_MAX_PORT_STR 32
-#define MSCP_SSH_MAX_IDENTITY_PATH PATH_MAX
-#define MSCP_SSH_MAX_CIPHER_STR 32
-#define MSCP_SSH_MAX_HMAC_STR 32
-#define MSCP_SSH_MAX_COMP_STR 32 /* yes, no, zlib, zlib@openssh.com, none */
-#define MSCP_SSH_MAX_CCALGO_STR 16
-#define MSCP_SSH_MAX_PASSWORD 128
-#define MSCP_SSH_MAX_PASSPHRASE 128
/**
* @struct mscp_ssh_opts
@@ -66,17 +55,17 @@ struct mscp_opts {
*/
struct mscp_ssh_opts {
/* ssh options */
- char login_name[MSCP_SSH_MAX_LOGIN_NAME]; /** ssh username */
- 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 cipher[MSCP_SSH_MAX_CIPHER_STR]; /** cipher spec */
- char hmac[MSCP_SSH_MAX_HMAC_STR]; /** hmacp spec */
- char compress[MSCP_SSH_MAX_COMP_STR]; /** yes, no, zlib@openssh.com */
- char ccalgo[MSCP_SSH_MAX_CCALGO_STR]; /** TCP cc algorithm */
-
- char password[MSCP_SSH_MAX_PASSWORD]; /** password auth passowrd */
- char passphrase[MSCP_SSH_MAX_PASSPHRASE]; /** passphrase for private key */
+ char *login_name; /** ssh username */
+ char *port; /** ssh port */
+ char *config; /** path to ssh_config, default ~/.ssh/config*/
+ char *identity; /** path to private key */
+ char *cipher; /** cipher spec */
+ char *hmac; /** hmacp spec */
+ char *compress; /** yes, no, zlib@openssh.com */
+ char *ccalgo; /** TCP cc algorithm */
+
+ char *password; /** password auth passowrd */
+ char *passphrase; /** passphrase for private key */
int debug_level; /** inclirement libssh debug output level */
bool no_hostkey_check; /** do not check host keys */
diff --git a/src/main.c b/src/main.c
index 0f2aac8..282837a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -268,7 +268,7 @@ int main(int argc, char **argv)
}
break;
case 'm':
- strncpy(o.coremask, optarg, sizeof(o.coremask));
+ o.coremask = optarg;
break;
case 'u':
o.max_startups = atoi(optarg);
@@ -301,58 +301,30 @@ int main(int argc, char **argv)
/* for compatibility with scp */
break;
case 'l':
- if (strlen(optarg) > MSCP_SSH_MAX_LOGIN_NAME - 1) {
- fprintf(stderr, "long login name: %s\n", optarg);
- return -1;
- }
- strncpy(s.login_name, optarg, MSCP_SSH_MAX_LOGIN_NAME - 1);
+ s.login_name = optarg;
break;
case 'P':
/* fallthough for compatibility with scp */
case 'p':
- if (strlen(optarg) > MSCP_SSH_MAX_PORT_STR - 1) {
- fprintf(stderr, "long port string: %s\n", optarg);
- return -1;
- }
- strncpy(s.port, optarg, MSCP_SSH_MAX_PORT_STR);
+ s.port = optarg;
break;
case 'F':
- strncpy(s.config, optarg, PATH_MAX - 1);
+ s.config = optarg;
break;
case 'i':
- if (strlen(optarg) > MSCP_SSH_MAX_IDENTITY_PATH - 1) {
- fprintf(stderr, "long identity path: %s\n", optarg);
- return -1;
- }
- strncpy(s.identity, optarg, MSCP_SSH_MAX_IDENTITY_PATH);
+ s.identity = optarg;
break;
case 'c':
- if (strlen(optarg) > MSCP_SSH_MAX_CIPHER_STR - 1) {
- fprintf(stderr, "long cipher string: %s\n", optarg);
- return -1;
- }
- strncpy(s.cipher, optarg, MSCP_SSH_MAX_CIPHER_STR);
+ s.cipher = optarg;
break;
case 'M':
- if (strlen(optarg) > MSCP_SSH_MAX_HMAC_STR - 1) {
- fprintf(stderr, "long hmac string: %s\n", optarg);
- return -1;
- }
- strncpy(s.hmac, optarg, MSCP_SSH_MAX_HMAC_STR);
+ s.hmac = optarg;
break;
case 'C':
- if (strlen(optarg) > MSCP_SSH_MAX_COMP_STR - 1) {
- fprintf(stderr, "long compress string: %s\n", optarg);
- return -1;
- }
- strncpy(s.compress, optarg, MSCP_SSH_MAX_COMP_STR);
+ s.compress = optarg;
break;
case 'g':
- if (strlen(optarg) > MSCP_SSH_MAX_CCALGO_STR - 1) {
- fprintf(stderr, "long ccalgo string: %s\n", optarg);
- return -1;
- }
- strncpy(s.ccalgo, optarg, MSCP_SSH_MAX_CCALGO_STR);
+ s.ccalgo = optarg;
break;
case 'H':
s.no_hostkey_check = true;
@@ -386,15 +358,12 @@ int main(int argc, char **argv)
/* copy remote to local */
direction = MSCP_DIRECTION_R2L;
remote = t[0].host;
- if (t[0].user != NULL && s.login_name[0] == '\0')
- strncpy(s.login_name, t[0].user, MSCP_SSH_MAX_LOGIN_NAME - 1);
+ s.login_name = s.login_name ? s.login_name : t[0].user;
} else {
/* copy local to remote */
direction = MSCP_DIRECTION_L2R;
remote = t[i - 1].host;
- if (t[i - 1].user != NULL && s.login_name[0] == '\0')
- strncpy(s.login_name, t[i - 1].user,
- MSCP_SSH_MAX_LOGIN_NAME - 1);
+ s.login_name = s.login_name ? s.login_name : t[i - 1].user;
}
if ((m = mscp_init(remote, direction, &o, &s)) == NULL) {
diff --git a/src/mscp.c b/src/mscp.c
index 7a6d5d6..a7c37da 100644
--- a/src/mscp.c
+++ b/src/mscp.c
@@ -258,7 +258,7 @@ struct mscp *mscp_init(const char *remote_host, int direction,
}
m->direction = direction;
- if (strlen(o->coremask) > 0) {
+ if (o->coremask) {
if (expand_coremask(o->coremask, &m->cores, &m->nr_cores) < 0)
goto free_out;
char b[512], c[8];
diff --git a/src/ssh.c b/src/ssh.c
index 1e223c3..6a06463 100644
--- a/src/ssh.c
+++ b/src/ssh.c
@@ -12,32 +12,29 @@
static int ssh_verify_known_hosts(ssh_session session);
-
-#define is_specified(s) (strlen(s) > 0)
-
static int ssh_set_opts(ssh_session ssh, struct mscp_ssh_opts *opts)
{
ssh_set_log_level(opts->debug_level);
- if (is_specified(opts->login_name) &&
+ if (opts->login_name &&
ssh_options_set(ssh, SSH_OPTIONS_USER, opts->login_name) < 0) {
mscp_set_error("failed to set login name");
return -1;
}
- if (is_specified(opts->port) &&
+ if (opts->port &&
ssh_options_set(ssh, SSH_OPTIONS_PORT_STR, opts->port) < 0) {
mscp_set_error("failed to set port number");
return -1;
}
- if (is_specified(opts->identity) &&
+ if (opts->identity &&
ssh_options_set(ssh, SSH_OPTIONS_IDENTITY, opts->identity) < 0) {
mscp_set_error("failed to set identity");
return -1;
}
- if (is_specified(opts->cipher)) {
+ if (opts->cipher) {
if (ssh_options_set(ssh, SSH_OPTIONS_CIPHERS_C_S, opts->cipher) < 0) {
mscp_set_error("failed to set cipher for client to server");
return -1;
@@ -48,7 +45,7 @@ static int ssh_set_opts(ssh_session ssh, struct mscp_ssh_opts *opts)
}
}
- if (is_specified(opts->hmac)) {
+ if (opts->hmac) {
if (ssh_options_set(ssh, SSH_OPTIONS_HMAC_C_S, opts->hmac) < 0) {
mscp_set_error("failed to set hmac for client to server");
return -1;
@@ -59,13 +56,13 @@ static int ssh_set_opts(ssh_session ssh, struct mscp_ssh_opts *opts)
}
}
- if (is_specified(opts->compress) &&
+ if (opts->compress &&
ssh_options_set(ssh, SSH_OPTIONS_COMPRESSION, opts->compress) < 0) {
mscp_set_error("failed to enable ssh compression");
return -1;
}
- if (is_specified(opts->ccalgo) &&
+ if (opts->ccalgo &&
ssh_options_set(ssh, SSH_OPTIONS_CCALGO, opts->ccalgo) < 0) {
mscp_set_error("failed to set cclago");
return -1;
@@ -80,7 +77,7 @@ static int ssh_set_opts(ssh_session ssh, struct mscp_ssh_opts *opts)
}
}
- if (is_specified(opts->config) &&
+ if (opts->config &&
ssh_options_parse_config(ssh, opts->config) < 0) {
mscp_set_error("failed to parse ssh_config: %s", opts->config);
return -1;
@@ -106,15 +103,19 @@ static int ssh_authenticate(ssh_session ssh, struct mscp_ssh_opts *opts)
return 0;
if (auth_bit_mask & SSH_AUTH_METHOD_PUBLICKEY) {
- char *p = is_specified(opts->passphrase) ? opts->passphrase : NULL;
+ char *p = opts->passphrase ? opts->passphrase : NULL;
if (ssh_userauth_publickey_auto(ssh, NULL, p) == SSH_AUTH_SUCCESS)
return 0;
}
if (auth_bit_mask & SSH_AUTH_METHOD_PASSWORD) {
- if (!is_specified(opts->password)) {
- if (ssh_getpass("Password: ", opts->password,
- MSCP_SSH_MAX_PASSWORD, 0, 0) < 0) {
+ if (!opts->password) {
+ char buf[128] = {};
+ if (ssh_getpass("Password: ", buf, sizeof(buf), 0, 0) < 0) {
+ return -1;
+ }
+ if (!(opts->password = strndup(buf, sizeof(buf)))) {
+ mpr_err("strndup: %s", strerrno());
return -1;
}
}
@@ -136,19 +137,26 @@ static int ssh_cache_passphrase(const char *prompt, char *buf, size_t len, int e
* second time or after because cached passphrase is passed
* to ssh_userauth_publickey_auto(). */
+ /* ToDo: use
+ * ssh_userauth_publickey_auto_get_current_identity() to print
+ * id for which we ask passphrase */
+
if (ssh_getpass("Passphrase: ", buf, len, echo, verify) < 0)
return -1;
/* cache the passphrase */
- if (strlen(buf) > MSCP_SSH_MAX_PASSPHRASE - 1) {
- pr_warn("sorry, passphrase is too long to cache...\n");
- return 0;
+ if (opts->passphrase)
+ free(opts->passphrase);
+
+ if (!(opts->passphrase = strndup(buf, len))) {
+ mpr_err("strndup: %s", strerrno());
+ return -1;
}
- strncpy(opts->passphrase, buf, MSCP_SSH_MAX_PASSPHRASE);
return 0;
}
+
static struct ssh_callbacks_struct cb = {
.auth_function = ssh_cache_passphrase,
.userdata = NULL,