mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-27 13:21:11 +03:00
options: Reformat ssh_options_getopt()
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
278
src/options.c
278
src/options.c
@@ -1070,164 +1070,168 @@ int ssh_options_get(ssh_session session, enum ssh_options_e type, char** value)
|
|||||||
*
|
*
|
||||||
* @see ssh_session_new()
|
* @see ssh_session_new()
|
||||||
*/
|
*/
|
||||||
int ssh_options_getopt(ssh_session session, int *argcptr, char **argv) {
|
int ssh_options_getopt(ssh_session session, int *argcptr, char **argv)
|
||||||
char *user = NULL;
|
{
|
||||||
char *cipher = NULL;
|
|
||||||
char *identity = NULL;
|
|
||||||
char *port = NULL;
|
|
||||||
char **save = NULL;
|
|
||||||
char **tmp = NULL;
|
|
||||||
size_t i = 0;
|
|
||||||
int argc = *argcptr;
|
|
||||||
int debuglevel = 0;
|
|
||||||
int usersa = 0;
|
|
||||||
int usedss = 0;
|
|
||||||
int compress = 0;
|
|
||||||
int cont = 1;
|
|
||||||
int current = 0;
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
(void)session;
|
||||||
|
(void)argcptr;
|
||||||
|
(void)argv;
|
||||||
/* Not supported with a Microsoft compiler */
|
/* Not supported with a Microsoft compiler */
|
||||||
return -1;
|
return -1;
|
||||||
#else
|
#else
|
||||||
int saveoptind = optind; /* need to save 'em */
|
char *user = NULL;
|
||||||
int saveopterr = opterr;
|
char *cipher = NULL;
|
||||||
|
char *identity = NULL;
|
||||||
|
char *port = NULL;
|
||||||
|
char **save = NULL;
|
||||||
|
char **tmp = NULL;
|
||||||
|
size_t i = 0;
|
||||||
|
int argc = *argcptr;
|
||||||
|
int debuglevel = 0;
|
||||||
|
int usersa = 0;
|
||||||
|
int usedss = 0;
|
||||||
|
int compress = 0;
|
||||||
|
int cont = 1;
|
||||||
|
int current = 0;
|
||||||
|
int saveoptind = optind; /* need to save 'em */
|
||||||
|
int saveopterr = opterr;
|
||||||
|
|
||||||
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 = optarg;
|
port = optarg;
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
debuglevel++;
|
debuglevel++;
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
usersa++;
|
usersa++;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
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 'C':
|
case 'C':
|
||||||
compress++;
|
compress++;
|
||||||
break;
|
break;
|
||||||
case '2':
|
case '2':
|
||||||
break;
|
break;
|
||||||
case '1':
|
case '1':
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
char opt[3]="- ";
|
char opt[3]="- ";
|
||||||
opt[1] = optopt;
|
opt[1] = optopt;
|
||||||
tmp = realloc(save, (current + 1) * sizeof(char*));
|
tmp = realloc(save, (current + 1) * sizeof(char*));
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
|
SAFE_FREE(save);
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
save = tmp;
|
||||||
|
save[current] = strdup(opt);
|
||||||
|
if (save[current] == NULL) {
|
||||||
|
SAFE_FREE(save);
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
current++;
|
||||||
|
if (optarg) {
|
||||||
|
save[current++] = argv[optind + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} /* switch */
|
||||||
|
} /* while */
|
||||||
|
opterr = saveopterr;
|
||||||
|
tmp = realloc(save, (current + (argc - optind)) * sizeof(char*));
|
||||||
|
if (tmp == NULL) {
|
||||||
|
SAFE_FREE(save);
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
save = tmp;
|
||||||
|
while (optind < argc) {
|
||||||
|
tmp = realloc(save, (current + 1) * sizeof(char*));
|
||||||
|
if (tmp == NULL) {
|
||||||
SAFE_FREE(save);
|
SAFE_FREE(save);
|
||||||
ssh_set_error_oom(session);
|
ssh_set_error_oom(session);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
save = tmp;
|
|
||||||
save[current] = strdup(opt);
|
|
||||||
if (save[current] == NULL) {
|
|
||||||
SAFE_FREE(save);
|
|
||||||
ssh_set_error_oom(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
current++;
|
|
||||||
if (optarg) {
|
|
||||||
save[current++] = argv[optind + 1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} /* switch */
|
save = tmp;
|
||||||
} /* while */
|
save[current] = argv[optind];
|
||||||
opterr = saveopterr;
|
current++;
|
||||||
tmp = realloc(save, (current + (argc - optind)) * sizeof(char*));
|
optind++;
|
||||||
if (tmp == NULL) {
|
}
|
||||||
|
|
||||||
|
if (usersa && usedss) {
|
||||||
|
ssh_set_error(session, SSH_FATAL, "Either RSA or DSS must be chosen");
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssh_set_log_level(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);
|
SAFE_FREE(save);
|
||||||
ssh_set_error_oom(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
save = tmp;
|
|
||||||
while (optind < argc) {
|
|
||||||
tmp = realloc(save, (current + 1) * sizeof(char*));
|
|
||||||
if (tmp == NULL) {
|
|
||||||
SAFE_FREE(save);
|
|
||||||
ssh_set_error_oom(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
save = tmp;
|
|
||||||
save[current] = argv[optind];
|
|
||||||
current++;
|
|
||||||
optind++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (usersa && usedss) {
|
/* set a new option struct */
|
||||||
ssh_set_error(session, SSH_FATAL, "Either RSA or DSS must be chosen");
|
if (compress) {
|
||||||
cont = 0;
|
if (ssh_options_set(session, SSH_OPTIONS_COMPRESSION, "yes") < 0) {
|
||||||
}
|
cont = 0;
|
||||||
|
}
|
||||||
ssh_set_log_level(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 */
|
|
||||||
if (compress) {
|
|
||||||
if (ssh_options_set(session, SSH_OPTIONS_COMPRESSION, "yes") < 0) {
|
|
||||||
cont = 0;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (cont && cipher) {
|
if (cont && cipher) {
|
||||||
if (ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, cipher) < 0) {
|
if (ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, cipher) < 0) {
|
||||||
cont = 0;
|
cont = 0;
|
||||||
|
}
|
||||||
|
if (cont && ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, cipher) < 0) {
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (cont && ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, cipher) < 0) {
|
|
||||||
cont = 0;
|
if (cont && user) {
|
||||||
|
if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0) {
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (cont && user) {
|
if (cont && identity) {
|
||||||
if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0) {
|
if (ssh_options_set(session, SSH_OPTIONS_IDENTITY, identity) < 0) {
|
||||||
cont = 0;
|
cont = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (cont && identity) {
|
if (port != NULL) {
|
||||||
if (ssh_options_set(session, SSH_OPTIONS_IDENTITY, identity) < 0) {
|
ssh_options_set(session, SSH_OPTIONS_PORT_STR, port);
|
||||||
cont = 0;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (port != NULL) {
|
if (!cont) {
|
||||||
ssh_options_set(session, SSH_OPTIONS_PORT_STR, port);
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cont) {
|
return SSH_OK;
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return SSH_OK;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user