DRDE/APIG/client/commander.cpp

564 lines
15 KiB
C++
Raw Normal View History

2014-01-13 01:29:22 +00:00
#include <ncurses.h>
2014-01-11 19:34:15 +00:00
#include "ausreg-cd.h"
2014-01-14 03:00:23 +00:00
#include "eppargs.h"
2014-01-13 23:19:10 +00:00
#define ASSERT(expr) \
if (!(expr)) { \
std::cerr << __FILE__ << ":" << __LINE__ << ":" << #expr << std::endl;}
2014-01-14 19:58:27 +00:00
const char *banner = CD_NAME " " CD_VERSION " compiled on " __DATE__ " @ " __TIME__ " (%d/%d)";
2014-01-13 01:29:22 +00:00
char theBanner[256];
int targetHost = 0;
mdDG mdg;
int is_numeric(const char *p) { int i = strlen(p),j=0;
if (*p) {
char c;
while ((c=*p++)) { j++;
if (!isdigit(c)) {
if (j == i) return 2;
else return 0;
}
}
return 1;
}
return 0;
}
2014-01-13 01:29:22 +00:00
void mdCommander::doHeader() {
int displayCmd = cmdsNow ? cmdNow + 1 : 0;
clear();
mvprintw(0,(col-strlen(banner))/2,"%s",theBanner);
sprintf(wwork,"Target AC host: %d this one: %d commmands: %d current: %d",
targetHost,mdStdDevIdx,activeCommands,displayCmd);
mvprintw(1,(col-strlen(wwork))/2,"%s",wwork);
}
2014-01-14 03:00:23 +00:00
int mdCommander::selectArg() {
2014-01-13 01:29:22 +00:00
2014-01-14 03:00:23 +00:00
char legend1[80]="Arg types: ",selection[10];
int i, wrow=5;
for (i=0;i < eppArgs[commandsNow[cmdNow]].n;i++) {
sprintf(wwork," %d %s ",i+1,eppArgs[commandsNow[cmdNow]].names[i]);
strcat(legend1,wwork);
}
mvprintw(wrow,2,(char *)legend1);
say((char *)"Select arg string type");
prompt(true);
mvgetstr(row-1,2,selection);
thisArg = atoi(selection);
}
void mdCommander::getArgStringsByType() {
eppArgDef *sp1;
char buffer[256],*sp;
int i, nLines=0, wrow=7;
2014-01-13 01:29:22 +00:00
nocbreak();
2014-01-14 03:00:23 +00:00
2014-01-13 23:19:10 +00:00
for (;argsNow[cmdNow] < MAX_CMD_ARGS;)
{memset(buffer,0,sizeof(buffer));
mvgetstr(wrow++,2,buffer);
if (strlen(buffer)) {
2014-01-14 03:00:23 +00:00
sp1 = (eppArgDef *)malloc(sizeof(eppArgDef));
sp = (char *) malloc(strlen(buffer)+1);
sp1->name = commandsNow[cmdNow];
sp1->s = sp;
commandArgs[cmdNow][argsNow[cmdNow]++] = sp1;
2014-01-13 23:19:10 +00:00
strcpy(sp,buffer);
}
else break;
2014-01-13 01:29:22 +00:00
}
cbreak();
prompt(false);
}
void mdCommander::prompt(bool forStrings) {
if (forStrings)
mvprintw(row-1,1,">>");
else
2014-01-13 23:19:10 +00:00
mvprintw(row-1,1,">");
2014-01-13 01:29:22 +00:00
}
void mdCommander::driver() {
2014-01-13 01:29:22 +00:00
bool rc, x;
char next,rawString[128],work[128];
const char *mdErrCode = "";
int i,commandLength;
2014-01-13 01:29:22 +00:00
activeCommands = 0;
cmdNow = 0;
cmdsNow = 0;
2014-01-14 03:00:23 +00:00
initEPPArgs();
2014-01-13 01:29:22 +00:00
memset(commandsNow,0,sizeof(commandsNow));
memset(argsNow,0,sizeof(argsNow));
initscr();
cbreak();
greet();
while(acceptingInput) {
2014-01-14 04:51:30 +00:00
refresh();
2014-01-13 01:29:22 +00:00
mvprintw(row-1,1,"> ");
next=0;
i=0;
memset(rawString,0,sizeof(rawString));
while(next != '\012') {
2014-01-13 01:29:22 +00:00
next = mvgetch(row-1,2+i);
rawString[i++] = next;
if (i > (sizeof(rawString) - 1)) {
2014-01-13 01:29:22 +00:00
say((char *)"Max length exceeded!");
continue;
}
2014-01-13 01:29:22 +00:00
if (next == '\012')
rawString[strlen(rawString)-1] = 0;
}
if (!strlen(rawString)) continue;
2014-01-13 01:29:22 +00:00
if (strlen(rawString) == 1) {
2014-01-11 19:34:15 +00:00
switch(rawString[0]) {
2014-01-13 01:29:22 +00:00
case '1': ; case '2': ; case '3': ; case '4': ; case '5': ;
case '6': ; case '7': ; case '8': ; case '9':
goto retarget ;
case '!': runCommand(false);
break;
2014-01-11 19:34:15 +00:00
case '?': help();
break;
case 'e': eppMainMenu();
break;
case 'a': acMainMenu();
break;
2014-01-13 01:29:22 +00:00
case 'c': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
check(false);
break;
case 'p': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
poll(false);
2014-01-11 19:34:15 +00:00
break;
2014-01-13 01:29:22 +00:00
case 'i': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
info(false);
2014-01-11 19:34:15 +00:00
break;
2014-01-13 01:29:22 +00:00
case 't': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
queryTransfer(false);
2014-01-11 19:34:15 +00:00
break;
2014-01-13 01:29:22 +00:00
case 'C': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
create(false);
2014-01-11 19:34:15 +00:00
break;
2014-01-13 01:29:22 +00:00
case 'R': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
renew(false);
2014-01-11 19:34:15 +00:00
break;
2014-01-13 01:29:22 +00:00
case 'U': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
update(false);
2014-01-11 19:34:15 +00:00
break;
2014-01-13 01:29:22 +00:00
case 'D': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
trash(false);
2014-01-11 19:34:15 +00:00
break;
2014-01-13 01:29:22 +00:00
case 'T': commandsNow[(cmdNow=cmdsNow++)]; activeCommands++;
transfer(false);
2014-01-11 19:34:15 +00:00
break;
}
continue;
}
2014-01-13 01:29:22 +00:00
if (strlen(rawString) == 2) {
2014-01-11 19:34:15 +00:00
if (!strcmp(rawString,"??")) {
2014-01-13 01:29:22 +00:00
showCommand(true);
continue;
}
if (!strcmp(rawString,"!!")) {
runCommand(true);
2014-01-11 19:34:15 +00:00
continue;
}
}
2014-01-13 01:29:22 +00:00
if (is_numeric(rawString) >= 1) {
retarget:
targetHost = atoi(rawString);
doHeader();
continue;
}
if (is_numeric(rawString+1) >= 1) {
int targetCmd = atoi(rawString+1);
switch(rawString[0]) {
case 'C':
if (isValidCmd(targetCmd,false)) {
cmdNow = --targetCmd;
doHeader();}
else say((char *)"Invalid Command Index");
break;
case 'E':
if (isValidCmd(targetCmd,false)) {
cmdNow = --targetCmd;
editingCommand(false);
doHeader();}
2014-01-13 23:19:10 +00:00
help();
2014-01-13 01:29:22 +00:00
break;
case 'S':
if (isValidCmd(targetCmd,false)) {
cmdNow = --targetCmd;
sendCommand(cmdNow);
doHeader();
}
else say((char *)"Invalid Command Index");
break;
case 'X':
if (!targetCmd) {targetCmd = 0; x = true;}
else {targetCmd--; x = false;}
if (isValidCmd(targetCmd,true)) {
flushCommand(targetCmd,x);
doHeader();
}
else say((char *)"Invalid Command Index");
break;
}
continue;
}
2014-01-13 01:29:22 +00:00
if (strlen(rawString) >= 3 && strlen(rawString) <= 5 )
{if (!strcmp(rawString,"log")) {
sprintf(wwork,"cut -b 1-%d /tmp/ausreg-cd.log | less",col);
system(wwork);
clear();
continue;
}
2014-01-13 01:29:22 +00:00
if (!strcmp(rawString,"mlog")) {
sprintf(wwork,"cut -b 1-%d /tmp/drde-cliever.log | less",col);
system(wwork);
clear();
continue;
}
2014-01-13 01:29:22 +00:00
if (!strcmp(rawString,"done")) {
goto done;
}
2014-01-14 19:58:27 +00:00
if (!strcmp(rawString,"quit")) { work[128];
thisConfig->terminateRequest = true;
2014-01-14 19:58:27 +00:00
sprintf(work,"kill -9 %d",thisConfig->daemonProcess);
system(work);
2014-01-13 01:29:22 +00:00
goto done;
}
continue;
}
2014-01-11 19:34:15 +00:00
}
2014-01-13 01:29:22 +00:00
done:
nocbreak();
endwin();
}
void mdCommander::say(char *what)
{ memset(wwork,' ',sizeof(wwork));
mvprintw(row-2,1,wwork);
mvprintw(row-2,1,what);
}
void mdCommander::greet() {
2014-01-13 01:29:22 +00:00
const char *title="AusReg Cliever Commander",
2014-01-14 04:51:30 +00:00
*prompt="Enter ? or command";
2014-01-13 01:29:22 +00:00
2014-01-14 19:58:27 +00:00
sprintf(theBanner,banner,thisConfig->shellProcess,thisConfig->daemonProcess);
2014-01-13 01:29:22 +00:00
getmaxyx(stdscr,row,col);
mvprintw(0,(col-strlen(banner))/2,"%s",theBanner);
mvprintw(row/2,(col-strlen(title))/2,"%s",title);
mvprintw(row-2,1,"%s",prompt);
refresh();
acceptingInput = true;
}
void mdCommander::help() {
2014-01-14 04:51:30 +00:00
const char *prompt="Enter command";
2014-01-13 01:29:22 +00:00
doHeader();
2014-01-14 04:51:30 +00:00
mvprintw(5,10," a - AC system");
mvprintw(6,10," e - EPP ");
mvprintw(7,10," ? - this screen");
2014-01-13 01:29:22 +00:00
mvprintw(8,10," ?? - display commands");
2014-01-14 04:51:30 +00:00
mvprintw(9,10," ! - run ");
mvprintw(10,10," !! - run all active");
mvprintw(11,10," C<n> - make <n> the current command");
mvprintw(12,10," E<n> - display/edit <n> ");
mvprintw(13,10," S<n> - send <n>");
mvprintw(14,10," X<n> - discard ");
mvprintw(15,10," <n> - make <n> the target device");
2014-01-13 01:29:22 +00:00
mvprintw(16,10," log - display this cliever's log");
mvprintw(17,10," mlog - display the master daemon log");
mvprintw(18,10," done - terminate commander but not process");
mvprintw(19,10," quit - terminate this process and its' children");
mvprintw(23,5,"<n> is an integer, above active everywhere outside data entry (>>)");
mvprintw(24,5,"no space before <n>, X0 to delete all commands.");
2014-01-14 04:51:30 +00:00
mvprintw(row-2,1,"%s",prompt);
refresh();
}
2014-01-11 19:34:15 +00:00
void mdCommander::eppMainMenu() {
2014-01-13 01:29:22 +00:00
doHeader();
mvprintw(5,10," Create a new EPP command");
mvprintw(10,10," Queries ");
mvprintw(12,10," c - check");
mvprintw(13,10," i - info");
mvprintw(14,10," p - poll");
mvprintw(15,10," t - transfer query");
mvprintw(17,10," Transactions");
mvprintw(19,10," C - create");
mvprintw(20,10," R - renew");
mvprintw(21,10," U - update");
mvprintw(22,10," D - delete");
mvprintw(23,10," T - transfer");
2014-01-11 19:34:15 +00:00
2014-01-14 04:51:30 +00:00
say((char *)"Action letter or ? to exit");
2014-01-11 19:34:15 +00:00
}
void mdCommander::acMainMenu() {
2014-01-14 04:51:30 +00:00
char selection[10];
2014-01-13 01:29:22 +00:00
doHeader();
mvprintw(5,(col-strlen("AC System Directives"))/2,"AC System Directives");
2014-01-11 19:34:15 +00:00
2014-01-13 01:29:22 +00:00
mvprintw(15,10," (H)ALT - Flush queues, stop, ignore incoming requests");
mvprintw(16,10," (L)OAD - Resume execution, accept work");
mvprintw(17,10," (B)ANG - Force reload the entire AC system");
2014-01-14 04:51:30 +00:00
mvprintw(20,10,"Only active on this screen, there's no confirmation.");
say((char *)"Select Action letter or anything else to exit");
prompt(false);
mvgetstr(row-1,2,selection);
switch(selection[0]) {
case 'H':
break;
case 'L':
break;
case 'B':
break;
}
help();
2014-01-11 19:34:15 +00:00
}
2014-01-13 01:29:22 +00:00
bool sendEPPCommand() {
2014-01-13 01:29:22 +00:00
mdg.dg.hdr.sourceHandle = thisCliever->myHandle;
mdg.dg.hdr.payloadSize = 0;
mdg.dg.hdr.msgType = MDDG_CDRESET;
return thisCliever->fg->send(mdg.dg);
}
bool mdCommander::runCommand(bool allOfEm) {
bool value=false;
2014-01-13 01:29:22 +00:00
if (!activeCommands) {say((char *)"There is no active command."); goto done;}
say((char *)"Command execution temporarily disabled.");
done: return value;
}
//--------------------------------
void mdCommander::flushCommand(int which,bool allOfEm){
int i,j;
if (!allOfEm) {
if (argsNow[which]) {
2014-01-14 03:00:23 +00:00
for (i=0;i<argsNow[which];i++) {
free(commandArgs[which][i]->s);
2014-01-13 01:29:22 +00:00
free(commandArgs[which][i]);
2014-01-14 03:00:23 +00:00
}
2014-01-13 01:29:22 +00:00
}
argsNow[which] = 0;
deleted[which] = true;
activeCommands--;
} else for (i=0;i<cmdsNow;i++) {
if (!deleted[i]) {
if (argsNow[i]) {
2014-01-14 03:00:23 +00:00
for (j=0;j<argsNow[i];j++) {
free(commandArgs[i][j]->s);
2014-01-13 01:29:22 +00:00
free(commandArgs[i][j]);
2014-01-14 03:00:23 +00:00
}
2014-01-13 01:29:22 +00:00
}
2014-01-13 23:19:10 +00:00
else deleted[i] = false; // back to start state.
2014-01-13 01:29:22 +00:00
}
cmdNow = 0;
cmdsNow = 0;
activeCommands = 0;
memset(commandArgs,0,sizeof(commandArgs));
memset(argsNow,0,sizeof(argsNow));
}
};
void mdCommander::sendCommand(int which){
2014-01-13 01:29:22 +00:00
say((char *)"Command processing disabled here during OTE Basic Testing");
};
void mdCommander::showCommand(bool allOfEm){
2014-01-13 01:29:22 +00:00
int activethCommand=0,i,nthItem;
const char *befehlJetzt = cmdNames[commandsNow[cmdNow]];
2014-01-13 01:29:22 +00:00
doHeader();
2014-01-13 01:29:22 +00:00
if (!activeCommands) {say((char *)"There is no active command."); goto done;}
2014-01-13 01:29:22 +00:00
if (!allOfEm) {
sprintf(wwork,"EPP directive: %s",befehlJetzt);
mvprintw(5,2,wwork);
for (nthItem=0;nthItem < argsNow[cmdNow];nthItem++) {
2014-01-14 03:00:23 +00:00
ASSERT(commandArgs[cmdNow][nthItem]->s);
sprintf(wwork,"%d %s %s",nthItem+1,
eppArgs[commandsNow[cmdNow]].names[commandArgs[cmdNow][nthItem]->name],
(commandArgs[cmdNow][nthItem]->s));
2014-01-13 23:19:10 +00:00
mvprintw(7+nthItem,2,wwork);
2014-01-13 01:29:22 +00:00
}
} else {
mvprintw(5,2,"These commands are pending/active: ");
2014-01-13 23:19:10 +00:00
for ( i=0, nthItem=0; nthItem < cmdsNow;nthItem++ ) {
2014-01-13 01:29:22 +00:00
2014-01-13 23:19:10 +00:00
if (!deleted[nthItem]) {
2014-01-14 03:00:23 +00:00
sprintf(wwork,"%d %s %s",nthItem+1,cmdNames[commandsNow[nthItem]],commandArgs[nthItem][0]->s);
2014-01-13 01:29:22 +00:00
mvprintw(7+(i++),2,wwork);
}
}
}
done: return;
};
//--------------------------------
// EPP Queries and Commands
//-------------------------------
bool mdCommander::editingCommand(bool run){
const char *befehlJetzt = cmdNames[commandsNow[cmdNow]];
2014-01-13 23:19:10 +00:00
char sp[256],*spp;
2014-01-14 03:00:23 +00:00
eppArgDef *arg;
2014-01-13 23:19:10 +00:00
int editLine,length;
2014-01-13 01:29:22 +00:00
doHeader();
mvprintw(3,(col-strlen(befehlJetzt))/2,befehlJetzt);
if (!run) {
if (!argsNow[cmdNow]) {
2014-01-14 03:00:23 +00:00
selectArg();
say((char *)"Enter arg string(s), just enter to end");
getArgStringsByType();
2014-01-13 01:29:22 +00:00
} else {
2014-01-13 23:19:10 +00:00
nocbreak();
while(true) {
showCommand(false);
2014-01-14 04:51:30 +00:00
say((char *)"line no. to replace, A to add, enter to return");
2014-01-13 23:19:10 +00:00
prompt(false);
2014-01-13 01:29:22 +00:00
mvgetstr(row-1,2,sp);
2014-01-13 23:19:10 +00:00
if (!strlen(sp)) goto done;
if (*sp == 'A' || *sp == 'a') {
2014-01-14 03:00:23 +00:00
selectArg();
2014-01-13 23:19:10 +00:00
prompt(true);
mvgetstr(row-1,2,sp);
if (length=strlen(sp)) {
2014-01-14 03:00:23 +00:00
arg = (eppArgDef *)malloc(sizeof(eppArgDef));
commandArgs[cmdNow][argsNow[cmdNow]++] = arg;
spp = commandArgs[cmdNow][argsNow[cmdNow]++]->s = (char *) malloc(length+1);
2014-01-13 23:19:10 +00:00
strcpy(spp,sp);
}
}
if (is_numeric(sp) >= 1) {
2014-01-13 01:29:22 +00:00
editLine = atoi(sp);
2014-01-13 23:19:10 +00:00
if (editLine > 1 && editLine < argsNow[cmdNow]) {
prompt(true);
mvgetstr(row-1,2,sp);
if (length=strlen(sp)) {
2014-01-14 03:00:23 +00:00
free (commandArgs[cmdNow][editLine-1]->s);
spp = commandArgs[cmdNow][editLine-1]->s = (char *) malloc(strlen(sp)+1);
2014-01-13 23:19:10 +00:00
strcpy(spp,sp);
}
}
2014-01-13 01:29:22 +00:00
}
} // editing loop
2014-01-13 23:19:10 +00:00
done: cbreak();
} // editing block
2014-01-13 01:29:22 +00:00
} // run / execute
else {
say((char *)"Press 'Y' to confirm");
2014-01-14 04:51:30 +00:00
prompt(false);
mvgetstr(row-1,2,sp);
if (sp[0] == 'Y') return false;
2014-01-13 01:29:22 +00:00
}
2014-01-14 04:51:30 +00:00
return true;
2014-01-13 01:29:22 +00:00
};
2014-01-14 19:58:27 +00:00
//
// Wrappers for specific EPP commands.
//
2014-01-13 01:29:22 +00:00
void mdCommander::check(bool run) {
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 0;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
}
2014-01-13 01:29:22 +00:00
void mdCommander::create(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 4;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};
void mdCommander::info(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 1;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};
void mdCommander::renew(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 5;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};
void mdCommander::queryTransfer(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 3;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};
void mdCommander::trash(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 7;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};
void mdCommander::transfer(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 8;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};
void mdCommander::poll(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 2;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};
void mdCommander::update(bool run){
if (!commandsNow[cmdNow]) commandsNow[cmdNow] = 6;
if (!editingCommand(run)) {
2014-01-13 23:19:10 +00:00
} else help();
2014-01-13 01:29:22 +00:00
};