mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-12 03:00:26 +09:00
Improve ssh_options_getopt().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@372 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
107
libssh/options.c
107
libssh/options.c
@@ -811,19 +811,19 @@ int ssh_options_set_log_verbosity(SSH_OPTIONS *opt, int verbosity) {
|
|||||||
* \sa ssh_options_new()
|
* \sa ssh_options_new()
|
||||||
*/
|
*/
|
||||||
int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv) {
|
int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv) {
|
||||||
int i;
|
|
||||||
int argc=*argcptr;
|
|
||||||
char *user = NULL;
|
char *user = NULL;
|
||||||
|
char *cipher = NULL;
|
||||||
|
char *localaddr = NULL;
|
||||||
|
char *identity = NULL;
|
||||||
|
char **save = NULL;
|
||||||
|
int i = 0;
|
||||||
|
int argc = *argcptr;
|
||||||
int port = 22;
|
int port = 22;
|
||||||
int debuglevel = 0;
|
int debuglevel = 0;
|
||||||
int usersa = 0;
|
int usersa = 0;
|
||||||
int usedss = 0;
|
int usedss = 0;
|
||||||
int compress = 0;
|
int compress = 0;
|
||||||
int cont = 1;
|
int cont = 1;
|
||||||
char *cipher=NULL;
|
|
||||||
char *localaddr=NULL;
|
|
||||||
char *identity=NULL;
|
|
||||||
char **save = NULL;
|
|
||||||
int current = 0;
|
int current = 0;
|
||||||
#ifdef HAVE_SSH1
|
#ifdef HAVE_SSH1
|
||||||
int ssh1 = 1;
|
int ssh1 = 1;
|
||||||
@@ -842,7 +842,6 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv){
|
|||||||
|
|
||||||
opterr = 0; /* shut up getopt */
|
opterr = 0; /* shut up getopt */
|
||||||
while(cont && ((i = getopt(argc, argv, "c:i:Cl:p:vb:rd12")) != -1)) {
|
while(cont && ((i = getopt(argc, argv, "c:i:Cl:p:vb:rd12")) != -1)) {
|
||||||
|
|
||||||
switch(i) {
|
switch(i) {
|
||||||
case 'l':
|
case 'l':
|
||||||
user = optarg;
|
user = optarg;
|
||||||
@@ -883,65 +882,103 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv){
|
|||||||
{
|
{
|
||||||
char opt[3]="- ";
|
char opt[3]="- ";
|
||||||
opt[1] = optopt;
|
opt[1] = optopt;
|
||||||
save[current++]=strdup(opt);
|
save[current] = strdup(opt);
|
||||||
if(optarg)
|
if (save[current] == NULL) {
|
||||||
|
SAFE_FREE(save);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
current++;
|
||||||
|
if (optarg) {
|
||||||
save[current++] = argv[optind + 1];
|
save[current++] = argv[optind + 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} /* switch */
|
||||||
|
} /* while */
|
||||||
opterr = saveopterr;
|
opterr = saveopterr;
|
||||||
while(optind < argc)
|
while (optind < argc) {
|
||||||
save[current++] = argv[optind++];
|
save[current++] = argv[optind++];
|
||||||
|
}
|
||||||
|
|
||||||
if (usersa && usedss) {
|
if (usersa && usedss) {
|
||||||
ssh_set_error(options,SSH_FATAL,"either RSA or DSS must be chosen");
|
ssh_set_error(options, SSH_FATAL, "Either RSA or DSS must be chosen");
|
||||||
cont = 0;
|
cont = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_options_set_log_verbosity(options, debuglevel);
|
ssh_options_set_log_verbosity(options, debuglevel);
|
||||||
|
|
||||||
optind = saveoptind;
|
optind = saveoptind;
|
||||||
|
|
||||||
if(!cont) {
|
if(!cont) {
|
||||||
free(save);
|
SAFE_FREE(save);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* first recopy the save vector into original's */
|
|
||||||
for(i=0;i<current;i++)
|
/* first recopy the save vector into the original's */
|
||||||
argv[i+1]=save[i]; // don't erase argv[0]
|
for (i = 0; i < current; i++) {
|
||||||
|
/* don't erase argv[0] */
|
||||||
|
argv[ i + 1] = save[i];
|
||||||
|
}
|
||||||
argv[current + 1] = NULL;
|
argv[current + 1] = NULL;
|
||||||
*argcptr = current + 1;
|
*argcptr = current + 1;
|
||||||
free(save);
|
SAFE_FREE(save);
|
||||||
|
|
||||||
/* set a new option struct */
|
/* set a new option struct */
|
||||||
if (compress) {
|
if (compress) {
|
||||||
if(ssh_options_set_wanted_algos(options,SSH_COMP_C_S,"zlib"))
|
if (ssh_options_set_wanted_algos(options, SSH_COMP_C_S, "zlib") < 0) {
|
||||||
cont=0;
|
|
||||||
if(ssh_options_set_wanted_algos(options,SSH_COMP_S_C,"zlib"))
|
|
||||||
cont = 0;
|
cont = 0;
|
||||||
}
|
}
|
||||||
|
if (ssh_options_set_wanted_algos(options, SSH_COMP_S_C, "zlib") < 0) {
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (cont && cipher) {
|
if (cont && cipher) {
|
||||||
if(ssh_options_set_wanted_algos(options,SSH_CRYPT_C_S,cipher))
|
if (ssh_options_set_wanted_algos(options, SSH_CRYPT_C_S, cipher) < 0) {
|
||||||
cont=0;
|
|
||||||
if(cont && ssh_options_set_wanted_algos(options,SSH_CRYPT_S_C,cipher))
|
|
||||||
cont = 0;
|
cont = 0;
|
||||||
}
|
}
|
||||||
if(cont && usersa)
|
if (cont && ssh_options_set_wanted_algos(options, SSH_CRYPT_S_C, cipher) < 0) {
|
||||||
if(ssh_options_set_wanted_algos(options,SSH_HOSTKEYS,"ssh-rsa"))
|
|
||||||
cont = 0;
|
cont = 0;
|
||||||
if(cont && usedss)
|
}
|
||||||
if(ssh_options_set_wanted_algos(options,SSH_HOSTKEYS,"ssh-dss"))
|
}
|
||||||
|
|
||||||
|
if (cont && usersa) {
|
||||||
|
if (ssh_options_set_wanted_algos(options, SSH_HOSTKEYS, "ssh-rsa") < 0) {
|
||||||
cont = 0;
|
cont = 0;
|
||||||
if(cont && user)
|
}
|
||||||
ssh_options_set_username(options,user);
|
}
|
||||||
if(cont && identity)
|
|
||||||
ssh_options_set_identity(options,identity);
|
if (cont && usedss) {
|
||||||
if(cont && localaddr)
|
if (ssh_options_set_wanted_algos(options, SSH_HOSTKEYS, "ssh-dss") < 0) {
|
||||||
ssh_options_set_bind(options,localaddr,0);
|
cont = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cont && user) {
|
||||||
|
if (ssh_options_set_username(options, user) < 0) {
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cont && identity) {
|
||||||
|
if (ssh_options_set_identity(options, identity) < 0) {
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cont && localaddr) {
|
||||||
|
if (ssh_options_set_bind(options, localaddr, 0) < 0) {
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ssh_options_set_port(options, port);
|
ssh_options_set_port(options, port);
|
||||||
//options->bindport=port;
|
|
||||||
ssh_options_allow_ssh1(options, ssh1);
|
ssh_options_allow_ssh1(options, ssh1);
|
||||||
ssh_options_allow_ssh2(options, ssh2);
|
ssh_options_allow_ssh2(options, ssh2);
|
||||||
|
|
||||||
if (!cont) {
|
if (!cont) {
|
||||||
return -1;
|
return -1;
|
||||||
} else
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user