mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 18:50:28 +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()
|
||||
*/
|
||||
int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv) {
|
||||
int i;
|
||||
int argc=*argcptr;
|
||||
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 debuglevel = 0;
|
||||
int usersa = 0;
|
||||
int usedss = 0;
|
||||
int compress = 0;
|
||||
int cont = 1;
|
||||
char *cipher=NULL;
|
||||
char *localaddr=NULL;
|
||||
char *identity=NULL;
|
||||
char **save = NULL;
|
||||
int current = 0;
|
||||
#ifdef HAVE_SSH1
|
||||
int ssh1 = 1;
|
||||
@@ -842,7 +842,6 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv){
|
||||
|
||||
opterr = 0; /* shut up getopt */
|
||||
while(cont && ((i = getopt(argc, argv, "c:i:Cl:p:vb:rd12")) != -1)) {
|
||||
|
||||
switch(i) {
|
||||
case 'l':
|
||||
user = optarg;
|
||||
@@ -883,65 +882,103 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv){
|
||||
{
|
||||
char opt[3]="- ";
|
||||
opt[1] = optopt;
|
||||
save[current++]=strdup(opt);
|
||||
if(optarg)
|
||||
save[current] = strdup(opt);
|
||||
if (save[current] == NULL) {
|
||||
SAFE_FREE(save);
|
||||
return -1;
|
||||
}
|
||||
current++;
|
||||
if (optarg) {
|
||||
save[current++] = argv[optind + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
} /* switch */
|
||||
} /* while */
|
||||
opterr = saveopterr;
|
||||
while(optind < argc)
|
||||
while (optind < argc) {
|
||||
save[current++] = argv[optind++];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
ssh_options_set_log_verbosity(options, debuglevel);
|
||||
|
||||
optind = saveoptind;
|
||||
|
||||
if(!cont) {
|
||||
free(save);
|
||||
SAFE_FREE(save);
|
||||
return -1;
|
||||
}
|
||||
/* first recopy the save vector into original's */
|
||||
for(i=0;i<current;i++)
|
||||
argv[i+1]=save[i]; // don't erase argv[0]
|
||||
|
||||
/* first recopy the save vector into the original's */
|
||||
for (i = 0; i < current; i++) {
|
||||
/* don't erase argv[0] */
|
||||
argv[ i + 1] = save[i];
|
||||
}
|
||||
argv[current + 1] = NULL;
|
||||
*argcptr = current + 1;
|
||||
free(save);
|
||||
SAFE_FREE(save);
|
||||
|
||||
/* set a new option struct */
|
||||
if (compress) {
|
||||
if(ssh_options_set_wanted_algos(options,SSH_COMP_C_S,"zlib"))
|
||||
cont=0;
|
||||
if(ssh_options_set_wanted_algos(options,SSH_COMP_S_C,"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") < 0) {
|
||||
cont = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (cont && cipher) {
|
||||
if(ssh_options_set_wanted_algos(options,SSH_CRYPT_C_S,cipher))
|
||||
cont=0;
|
||||
if(cont && ssh_options_set_wanted_algos(options,SSH_CRYPT_S_C,cipher))
|
||||
if (ssh_options_set_wanted_algos(options, SSH_CRYPT_C_S, cipher) < 0) {
|
||||
cont = 0;
|
||||
}
|
||||
if(cont && usersa)
|
||||
if(ssh_options_set_wanted_algos(options,SSH_HOSTKEYS,"ssh-rsa"))
|
||||
if (cont && ssh_options_set_wanted_algos(options, SSH_CRYPT_S_C, cipher) < 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;
|
||||
if(cont && user)
|
||||
ssh_options_set_username(options,user);
|
||||
if(cont && identity)
|
||||
ssh_options_set_identity(options,identity);
|
||||
if(cont && localaddr)
|
||||
ssh_options_set_bind(options,localaddr,0);
|
||||
}
|
||||
}
|
||||
|
||||
if (cont && usedss) {
|
||||
if (ssh_options_set_wanted_algos(options, SSH_HOSTKEYS, "ssh-dss") < 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);
|
||||
//options->bindport=port;
|
||||
ssh_options_allow_ssh1(options, ssh1);
|
||||
ssh_options_allow_ssh2(options, ssh2);
|
||||
|
||||
if (!cont) {
|
||||
return -1;
|
||||
} else
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user