1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-29 01:03:57 +03:00

options: Fix the ssh_options_set() function.

This is a refactor so that we check if the values are sane the user
wants to set. If not return an error.
This commit is contained in:
Andreas Schneider
2011-09-05 20:08:29 +02:00
parent 2e83cc925d
commit 3105b3c205

View File

@@ -367,6 +367,7 @@ int ssh_options_set_algo(ssh_session session, int algo,
*/ */
int ssh_options_set(ssh_session session, enum ssh_options_e type, int ssh_options_set(ssh_session session, enum ssh_options_e type,
const void *value) { const void *value) {
const char *v;
char *p, *q; char *p, *q;
long int i; long int i;
int rc; int rc;
@@ -375,8 +376,18 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
return -1; return -1;
} }
if (value == NULL) {
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
}
switch (type) { switch (type) {
case SSH_OPTIONS_HOST: case SSH_OPTIONS_HOST:
v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
} else {
q = strdup(value); q = strdup(value);
if (q == NULL) { if (q == NULL) {
ssh_set_error_oom(session); ssh_set_error_oom(session);
@@ -405,21 +416,26 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
} else { } else {
session->host = q; session->host = q;
} }
}
break; break;
case SSH_OPTIONS_PORT: case SSH_OPTIONS_PORT:
if (value == NULL) { {
session->port = 22 & 0xffff;
} else {
int *x = (int *) value; int *x = (int *) value;
if (*x <= 0) {
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
}
session->port = *x & 0xffff; session->port = *x & 0xffff;
} }
break; break;
case SSH_OPTIONS_PORT_STR: case SSH_OPTIONS_PORT_STR:
if (value == NULL) { v = value;
session->port = 22 & 0xffff; if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
} else { } else {
q = strdup(value); q = strdup(v);
if (q == NULL) { if (q == NULL) {
ssh_set_error_oom(session); ssh_set_error_oom(session);
return -1; return -1;
@@ -429,25 +445,34 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
SAFE_FREE(q); SAFE_FREE(q);
} }
SAFE_FREE(q); SAFE_FREE(q);
if (i <= 0) {
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
}
session->port = i & 0xffff; session->port = i & 0xffff;
} }
break; break;
case SSH_OPTIONS_FD: case SSH_OPTIONS_FD:
if (value == NULL) { {
session->fd = SSH_INVALID_SOCKET;
} else {
socket_t *x = (socket_t *) value; socket_t *x = (socket_t *) value;
if (*x < 0) {
session->fd = SSH_INVALID_SOCKET;
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
}
session->fd = *x & 0xffff; session->fd = *x & 0xffff;
} }
break; break;
case SSH_OPTIONS_BINDADDR: case SSH_OPTIONS_BINDADDR:
if (value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} }
q = strdup(value);
q = strdup(v);
if (q == NULL) { if (q == NULL) {
return -1; return -1;
} }
@@ -455,10 +480,12 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
session->bindaddr = q; session->bindaddr = q;
break; break;
case SSH_OPTIONS_USER: case SSH_OPTIONS_USER:
v = value;
SAFE_FREE(session->username); SAFE_FREE(session->username);
if (value == NULL) { /* set default username */ if (v[0] == '\0') {
q = ssh_get_local_username(); q = ssh_get_local_username();
if (q == NULL) { if (q == NULL) {
ssh_set_error_oom(session);
return -1; return -1;
} }
session->username = q; session->username = q;
@@ -471,16 +498,15 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
} }
break; break;
case SSH_OPTIONS_SSH_DIR: case SSH_OPTIONS_SSH_DIR:
if (value == NULL) { v = value;
SAFE_FREE(session->sshdir); SAFE_FREE(session->sshdir);
if (v[0] == '\0') {
session->sshdir = ssh_path_expand_tilde("~/.ssh"); session->sshdir = ssh_path_expand_tilde("~/.ssh");
if (session->sshdir == NULL) { if (session->sshdir == NULL) {
return -1; return -1;
} }
} else { } else {
SAFE_FREE(session->sshdir); session->sshdir = ssh_path_expand_tilde(v);
session->sshdir = ssh_path_expand_tilde(value);
if (session->sshdir == NULL) { if (session->sshdir == NULL) {
return -1; return -1;
} }
@@ -488,11 +514,12 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
break; break;
case SSH_OPTIONS_IDENTITY: case SSH_OPTIONS_IDENTITY:
case SSH_OPTIONS_ADD_IDENTITY: case SSH_OPTIONS_ADD_IDENTITY:
if (value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} }
q = strdup(value); q = strdup(v);
if (q == NULL) { if (q == NULL) {
return -1; return -1;
} }
@@ -502,76 +529,89 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
} }
break; break;
case SSH_OPTIONS_KNOWNHOSTS: case SSH_OPTIONS_KNOWNHOSTS:
if (value == NULL) { v = value;
SAFE_FREE(session->knownhosts); SAFE_FREE(session->knownhosts);
if (v[0] == '\0') {
if (session->sshdir == NULL) { if (session->sshdir == NULL) {
ssh_set_error(session, SSH_FATAL, "ssh_dir is not set");
return -1; return -1;
} }
session->knownhosts = ssh_path_expand_escape(session, "%d/known_hosts"); session->knownhosts = ssh_path_expand_escape(session, "%d/known_hosts");
if (session->knownhosts == NULL) { if (session->knownhosts == NULL) {
ssh_set_error_oom(session);
return -1; return -1;
} }
} else { } else {
SAFE_FREE(session->knownhosts); session->knownhosts = strdup(v);
session->knownhosts = strdup(value);
if (session->knownhosts == NULL) { if (session->knownhosts == NULL) {
ssh_set_error_oom(session);
return -1; return -1;
} }
} }
break; break;
case SSH_OPTIONS_TIMEOUT: case SSH_OPTIONS_TIMEOUT:
if (value == NULL) { {
long *x = (long *) value;
if (*x < 0) {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { }
long *x = (long *) value;
session->timeout = *x & 0xffffffff; session->timeout = *x & 0xffffffff;
} }
break; break;
case SSH_OPTIONS_TIMEOUT_USEC: case SSH_OPTIONS_TIMEOUT_USEC:
if (value == NULL) { {
long *x = (long *) value;
if (*x < 0) {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { }
long *x = (long *) value;
session->timeout_usec = *x & 0xffffffff; session->timeout_usec = *x & 0xffffffff;
} }
break; break;
case SSH_OPTIONS_SSH1: case SSH_OPTIONS_SSH1:
if (value == NULL) { {
int *x = (int *) value;
if (*x < 0) {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { }
int *x = (int *) value;
session->ssh1 = *x; session->ssh1 = *x;
} }
break; break;
case SSH_OPTIONS_SSH2: case SSH_OPTIONS_SSH2:
if (value == NULL) { {
int *x = (int *) value;
if (*x < 0) {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { }
int *x = (int *) value;
session->ssh2 = *x & 0xffff; session->ssh2 = *x & 0xffff;
} }
break; break;
case SSH_OPTIONS_LOG_VERBOSITY: case SSH_OPTIONS_LOG_VERBOSITY:
if (value == NULL) { {
int *x = (int *) value;
if (*x < 0) {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { }
int *x = (int *) value;
session->common.log_verbosity = *x & 0xffff; session->common.log_verbosity = *x & 0xffff;
} }
break; break;
case SSH_OPTIONS_LOG_VERBOSITY_STR: case SSH_OPTIONS_LOG_VERBOSITY_STR:
if (value == NULL) { v = value;
session->common.log_verbosity = 0 & 0xffff; if (v[0] == '\0') {
session->common.log_verbosity = 0;
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
} else { } else {
q = strdup(value); q = strdup(v);
if (q == NULL) { if (q == NULL) {
ssh_set_error_oom(session); ssh_set_error_oom(session);
return -1; return -1;
@@ -581,39 +621,47 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
SAFE_FREE(q); SAFE_FREE(q);
} }
SAFE_FREE(q); SAFE_FREE(q);
if (i < 0) {
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
}
session->common.log_verbosity = i & 0xffff; session->common.log_verbosity = i & 0xffff;
} }
break; break;
case SSH_OPTIONS_CIPHERS_C_S: case SSH_OPTIONS_CIPHERS_C_S:
if (value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { } else {
if (ssh_options_set_algo(session, SSH_CRYPT_C_S, value) < 0) if (ssh_options_set_algo(session, SSH_CRYPT_C_S, v) < 0)
return -1; return -1;
} }
break; break;
case SSH_OPTIONS_CIPHERS_S_C: case SSH_OPTIONS_CIPHERS_S_C:
if (value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { } else {
if (ssh_options_set_algo(session, SSH_CRYPT_S_C, value) < 0) if (ssh_options_set_algo(session, SSH_CRYPT_S_C, v) < 0)
return -1; return -1;
} }
break; break;
case SSH_OPTIONS_KEY_EXCHANGE: case SSH_OPTIONS_KEY_EXCHANGE:
if(value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { } else {
if (ssh_options_set_algo(session, SSH_KEX, value) < 0) if (ssh_options_set_algo(session, SSH_KEX, v) < 0)
return -1; return -1;
} }
break; break;
case SSH_OPTIONS_COMPRESSION_C_S: case SSH_OPTIONS_COMPRESSION_C_S:
if (value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { } else {
@@ -624,13 +672,14 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
if(ssh_options_set_algo(session,SSH_COMP_C_S,"none") < 0) if(ssh_options_set_algo(session,SSH_COMP_C_S,"none") < 0)
return -1; return -1;
} else { } else {
if (ssh_options_set_algo(session, SSH_COMP_C_S, value) < 0) if (ssh_options_set_algo(session, SSH_COMP_C_S, v) < 0)
return -1; return -1;
} }
} }
break; break;
case SSH_OPTIONS_COMPRESSION_S_C: case SSH_OPTIONS_COMPRESSION_S_C:
if (value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { } else {
@@ -641,50 +690,48 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
if(ssh_options_set_algo(session,SSH_COMP_S_C,"none") < 0) if(ssh_options_set_algo(session,SSH_COMP_S_C,"none") < 0)
return -1; return -1;
} else { } else {
if (ssh_options_set_algo(session, SSH_COMP_S_C, value) < 0) if (ssh_options_set_algo(session, SSH_COMP_S_C, v) < 0)
return -1; return -1;
} }
} }
break; break;
case SSH_OPTIONS_COMPRESSION: case SSH_OPTIONS_COMPRESSION:
if (value==NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} }
if(ssh_options_set(session,SSH_OPTIONS_COMPRESSION_C_S,value) < 0) if(ssh_options_set(session,SSH_OPTIONS_COMPRESSION_C_S, v) < 0)
return -1; return -1;
if(ssh_options_set(session,SSH_OPTIONS_COMPRESSION_S_C,value) < 0) if(ssh_options_set(session,SSH_OPTIONS_COMPRESSION_S_C, v) < 0)
return -1; return -1;
break; break;
case SSH_OPTIONS_COMPRESSION_LEVEL: case SSH_OPTIONS_COMPRESSION_LEVEL:
if (value==NULL) { {
int *x = (int *)value;
if (*x < 1 || *x > 9) {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} }
else { session->compressionlevel = *x & 0xff;
int *x=(int *)value;
if(*x < 1 || *x > 9){
ssh_set_error_invalid(session, __FUNCTION__);
return -1;
}
session->compressionlevel=*x & 0xff;
} }
break; break;
case SSH_OPTIONS_STRICTHOSTKEYCHECK: case SSH_OPTIONS_STRICTHOSTKEYCHECK:
if (value == NULL) { {
ssh_set_error_invalid(session, __FUNCTION__); int *x = (int *) value;
return -1;
} else { session->StrictHostKeyChecking = (*x & 0xff) > 0 ? 1 : 0;
session->StrictHostKeyChecking = *(int*)value;
} }
session->StrictHostKeyChecking = *(int*)value;
break; break;
case SSH_OPTIONS_PROXYCOMMAND: case SSH_OPTIONS_PROXYCOMMAND:
if (value == NULL) { v = value;
if (v[0] == '\0') {
ssh_set_error_invalid(session, __FUNCTION__); ssh_set_error_invalid(session, __FUNCTION__);
return -1; return -1;
} else { } else {
SAFE_FREE(session->ProxyCommand); SAFE_FREE(session->ProxyCommand);
q = strdup(value); q = strdup(v);
if (q == NULL) { if (q == NULL) {
return -1; return -1;
} }