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:
Andreas Schneider
2009-04-03 08:19:57 +00:00
parent 4db7fc77ff
commit 497c31d9a0

View File

@@ -810,45 +810,44 @@ int ssh_options_set_log_verbosity(SSH_OPTIONS *opt, int verbosity) {
* \returns 0 on success, -1 on error * \returns 0 on success, -1 on error
* \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; char *user = NULL;
int argc=*argcptr; char *cipher = NULL;
char *user=NULL; char *localaddr = NULL;
int port=22; char *identity = NULL;
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; char **save = NULL;
int current=0; int i = 0;
int argc = *argcptr;
int port = 22;
int debuglevel = 0;
int usersa = 0;
int usedss = 0;
int compress = 0;
int cont = 1;
int current = 0;
#ifdef HAVE_SSH1 #ifdef HAVE_SSH1
int ssh1=1; int ssh1 = 1;
#else #else
int ssh1=0; int ssh1 = 0;
#endif #endif
int ssh2=1; int ssh2 = 1;
int saveoptind=optind; /* need to save 'em */ int saveoptind = optind; /* need to save 'em */
int saveopterr=opterr; int saveopterr = opterr;
save = malloc(argc * sizeof(char *)); save = malloc(argc * sizeof(char *));
if (save == NULL) { if (save == NULL) {
return -1; return -1;
} }
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;
break; break;
case 'p': case 'p':
port=atoi(optarg)&0xffff; port = atoi(optarg) & 0xffff;
break; break;
case 'v': case 'v':
debuglevel++; debuglevel++;
@@ -860,89 +859,127 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv){
usedss++; usedss++;
break; break;
case 'c': case 'c':
cipher=optarg; cipher = optarg;
break; break;
case 'i': case 'i':
identity=optarg; identity = optarg;
break; break;
case 'b': case 'b':
localaddr=optarg; localaddr = optarg;
break; break;
case 'C': case 'C':
compress++; compress++;
break; break;
case '2': case '2':
ssh2=1; ssh2 = 1;
ssh1=0; ssh1 = 0;
break; break;
case '1': case '1':
ssh2=0; ssh2 = 0;
ssh1=1; ssh1 = 1;
break; break;
default: default:
{ {
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) {
save[current++]=argv[optind+1]; SAFE_FREE(save);
}
}
}
opterr=saveopterr;
while(optind < argc)
save[current++]=argv[optind++];
if(usersa && usedss){
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);
return -1; return -1;
} }
/* first recopy the save vector into original's */ current++;
for(i=0;i<current;i++) if (optarg) {
argv[i+1]=save[i]; // don't erase argv[0] save[current++] = argv[optind + 1];
argv[current+1]=NULL; }
*argcptr=current+1; }
free(save); } /* switch */
} /* while */
opterr = saveopterr;
while (optind < argc) {
save[current++] = argv[optind++];
}
if (usersa && usedss) {
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) {
SAFE_FREE(save);
return -1;
}
/* 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;
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; cont = 0;
if(ssh_options_set_wanted_algos(options,SSH_COMP_S_C,"zlib")) }
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))
cont=0;
} }
if(cont && usersa)
if(ssh_options_set_wanted_algos(options,SSH_HOSTKEYS,"ssh-rsa"))
cont=0;
if(cont && usedss)
if(ssh_options_set_wanted_algos(options,SSH_HOSTKEYS,"ssh-dss"))
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);
ssh_options_set_port(options,port);
//options->bindport=port;
ssh_options_allow_ssh1(options,ssh1);
ssh_options_allow_ssh2(options,ssh2);
if(!cont){ if (cont && 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) < 0) {
cont = 0;
}
}
if (cont && usersa) {
if (ssh_options_set_wanted_algos(options, SSH_HOSTKEYS, "ssh-rsa") < 0) {
cont = 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);
ssh_options_allow_ssh1(options, ssh1);
ssh_options_allow_ssh2(options, ssh2);
if (!cont) {
return -1; return -1;
} else }
return 0 ;
return 0;
} }
/** /**