mirror of
https://github.com/clearml/dropbear
synced 2025-05-24 13:14:14 +00:00
Filled out a bit, with commandline support etc
--HG-- extra : convert_revision : 588496938ac3e4df1fab53f724be00afa9fadeab
This commit is contained in:
parent
7cdad3c200
commit
68f816e8cf
158
cli-runopts.c
158
cli-runopts.c
@ -46,11 +46,13 @@ static void printhelp(const char * progname) {
|
|||||||
|
|
||||||
void cli_getopts(int argc, char ** argv) {
|
void cli_getopts(int argc, char ** argv) {
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i, j;
|
||||||
char ** next = 0;
|
char ** next = 0;
|
||||||
|
unsigned int cmdlen;
|
||||||
|
int nextiskey = 0; /* A flag if the next argument is a keyfile */
|
||||||
|
|
||||||
uid_t uid;
|
uid_t uid;
|
||||||
struct passwd *pw;
|
struct passwd *pw = NULL;
|
||||||
|
|
||||||
char* userhostarg = NULL;
|
char* userhostarg = NULL;
|
||||||
|
|
||||||
@ -73,13 +75,52 @@ void cli_getopts(int argc, char ** argv) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = 1; i < (unsigned int)argc; i++) {
|
||||||
|
if (nextiskey) {
|
||||||
|
/* XXX do stuff */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (next) {
|
||||||
|
*next = argv[i];
|
||||||
|
if (*next == NULL) {
|
||||||
|
dropbear_exit("Invalid null argument");
|
||||||
|
}
|
||||||
|
next = NULL;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argv[i][0] == '-') {
|
||||||
|
|
||||||
|
/* A flag *waves* */
|
||||||
|
switch (argv[i][1]) {
|
||||||
|
case 'p':
|
||||||
|
next = &cli_opts.remoteport;
|
||||||
|
break;
|
||||||
|
#ifdef DROPBEAR_PUBKEY_AUTH
|
||||||
|
case 'i':
|
||||||
|
nextiskey = 1;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "Unknown argument %s\n", argv[i]);
|
||||||
|
printhelp(argv[0]);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
break;
|
||||||
|
} /* Switch */
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
/* Either the hostname or commands */
|
||||||
|
/* Hostname is first up, must be set before we get the cmds */
|
||||||
|
|
||||||
|
if (cli_opts.remotehost == NULL) {
|
||||||
/* We'll be editing it, should probably make a copy */
|
/* We'll be editing it, should probably make a copy */
|
||||||
userhostarg = m_strdup(argv[1]);
|
userhostarg = m_strdup(argv[1]);
|
||||||
|
|
||||||
cli_opts.remotehost = strchr(userhostarg, '@');
|
cli_opts.remotehost = strchr(userhostarg, '@');
|
||||||
if (cli_opts.remotehost == NULL) {
|
if (cli_opts.remotehost == NULL) {
|
||||||
/* no username portion, the cli-auth.c code can figure the local
|
/* no username portion, the cli-auth.c code can figure the
|
||||||
* user's name */
|
* local user's name */
|
||||||
cli_opts.remotehost = userhostarg;
|
cli_opts.remotehost = userhostarg;
|
||||||
} else {
|
} else {
|
||||||
cli_opts.remotehost[0] = '\0'; /* Split the user/host */
|
cli_opts.remotehost[0] = '\0'; /* Split the user/host */
|
||||||
@ -92,112 +133,37 @@ void cli_getopts(int argc, char ** argv) {
|
|||||||
|
|
||||||
pw = getpwuid(uid);
|
pw = getpwuid(uid);
|
||||||
if (pw == NULL || pw->pw_name == NULL) {
|
if (pw == NULL || pw->pw_name == NULL) {
|
||||||
dropbear_exit("Couldn't find username for current user");
|
dropbear_exit("I don't know my own [user]name");
|
||||||
}
|
}
|
||||||
|
|
||||||
cli_opts.username = m_strdup(pw->pw_name);
|
cli_opts.username = m_strdup(pw->pw_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cli_opts.remotehost[0] == '\0') {
|
if (cli_opts.remotehost[0] == '\0') {
|
||||||
dropbear_exit("Bad hostname argument");
|
dropbear_exit("Bad hostname");
|
||||||
}
|
}
|
||||||
|
|
||||||
cli_opts.remoteport = strchr(cli_opts.remotehost, ':');
|
|
||||||
if (cli_opts.remoteport == NULL) {
|
|
||||||
cli_opts.remoteport = "22";
|
|
||||||
} else {
|
} else {
|
||||||
cli_opts.remoteport[0] = '\0';
|
/* this is part of the commands to send - after this we
|
||||||
cli_opts.remoteport++;
|
* don't parse any more options, and flags are sent as the
|
||||||
|
* command */
|
||||||
|
cmdlen = 0;
|
||||||
|
for (j = i; j < (unsigned int)argc; j++) {
|
||||||
|
cmdlen += strlen(argv[j]) + 1; /* +1 for spaces */
|
||||||
}
|
}
|
||||||
|
/* Allocate the space */
|
||||||
|
cli_opts.cmd = (char*)m_malloc(cmdlen);
|
||||||
|
cli_opts.cmd[0] = '\0';
|
||||||
|
|
||||||
#if 0
|
/* Append all the bits */
|
||||||
for (i = 1; i < (unsigned int)argc; i++) {
|
for (j = i; j < (unsigned int)argc; j++) {
|
||||||
if (next) {
|
strlcat(cli_opts.cmd, argv[j], cmdlen);
|
||||||
*next = argv[i];
|
strlcat(cli_opts.cmd, " ", cmdlen);
|
||||||
if (*next == NULL) {
|
|
||||||
dropbear_exit("Invalid null argument");
|
|
||||||
}
|
|
||||||
next = 0x00;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
/* It'll be null-terminated here */
|
||||||
|
|
||||||
if (argv[i][0] == '-') {
|
/* We've eaten all the options and flags */
|
||||||
switch (argv[i][1]) {
|
|
||||||
case 'b':
|
|
||||||
next = &svr_opts.bannerfile;
|
|
||||||
break;
|
|
||||||
#ifdef DROPBEAR_DSS
|
|
||||||
case 'd':
|
|
||||||
next = &svr_opts.dsskeyfile;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
#ifdef DROPBEAR_RSA
|
|
||||||
case 'r':
|
|
||||||
next = &svr_opts.rsakeyfile;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
case 'F':
|
|
||||||
svr_opts.forkbg = 0;
|
|
||||||
break;
|
|
||||||
#ifndef DISABLE_SYSLOG
|
|
||||||
case 'E':
|
|
||||||
svr_opts.usingsyslog = 0;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
#ifndef DISABLE_LOCALTCPFWD
|
|
||||||
case 'j':
|
|
||||||
opts.nolocaltcp = 1;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
#ifndef DISABLE_REMOTETCPFWD
|
|
||||||
case 'k':
|
|
||||||
opts.noremotetcp = 1;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
case 'p':
|
|
||||||
if (portnum < DROPBEAR_MAX_PORTS) {
|
|
||||||
portstring[portnum] = NULL;
|
|
||||||
next = &portstring[portnum];
|
|
||||||
portnum++;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
#ifdef DO_MOTD
|
|
||||||
/* motd is displayed by default, -m turns it off */
|
|
||||||
case 'm':
|
|
||||||
svr_opts.domotd = 0;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
case 'w':
|
|
||||||
svr_opts.norootlogin = 1;
|
|
||||||
break;
|
|
||||||
#ifdef DROPBEAR_PASSWORD_AUTH
|
|
||||||
case 's':
|
|
||||||
svr_opts.noauthpass = 1;
|
|
||||||
break;
|
|
||||||
case 'g':
|
|
||||||
svr_opts.norootpass = 1;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
case 'h':
|
|
||||||
printhelp(argv[0]);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
break;
|
|
||||||
/*
|
|
||||||
case '4':
|
|
||||||
svr_opts.ipv4 = 0;
|
|
||||||
break;
|
|
||||||
case '6':
|
|
||||||
svr_opts.ipv6 = 0;
|
|
||||||
break;
|
|
||||||
*/
|
|
||||||
default:
|
|
||||||
fprintf(stderr, "Unknown argument %s\n", argv[i]);
|
|
||||||
printhelp(argv[0]);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user