Skip to content

Commit

Permalink
passing options via pointers in mscp_ssh_opts
Browse files Browse the repository at this point in the history
We do not need static buf because we have already dropped python biding
support.
  • Loading branch information
upa committed Feb 5, 2024
1 parent c95e6a4 commit e223228
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 85 deletions.
35 changes: 12 additions & 23 deletions include/mscp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -43,40 +41,31 @@ 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
* @brief Structure configuring SSH connections
*/
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 */
Expand Down
65 changes: 23 additions & 42 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -386,15 +358,24 @@ 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);
if (t[0].user != NULL && s.login_name == NULL) {
s.login_name = strndup(t[0].user, LOGIN_NAME_MAX);
if (!s.login_name) {
fprintf(stderr, "strndup: %s", strerror(errno));
return -1;
}
}
} 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);
if (t[i - 1].user != NULL && s.login_name == NULL) {
s.login_name = strndup(t[i - 1].user, LOGIN_NAME_MAX);
if (!s.login_name) {
fprintf(stderr, "strndup: %s", strerror(errno));
return -1;
}
}
}

if ((m = mscp_init(remote, direction, &o, &s)) == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/mscp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
46 changes: 27 additions & 19 deletions src/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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,
Expand Down

0 comments on commit e223228

Please sign in to comment.