src/shepp/SheppCommandFunctions.H

Go to the documentation of this file.
00001 /* ${copyright}$ */
00002 /* $Id: SheppCommandFunctions.H 1010 2009-03-12 17:50:50Z eduardo $ */
00006 #ifndef __SHEPP_COMMAND_FUNCTIONS_H__
00007 #define __SHEPP_COMMAND_FUNCTIONS_H__
00008 
00009 #include <fstream>
00010 #include <unistd.h>   //alarm
00011 
00012 //used by getpasswd
00013 #include <termios.h>
00014 #include <iostream>
00015 using std::cin;
00016 using std::cout;
00017 using std::endl;
00018 
00019 #include "SheppGlobal.H"
00020 #include "SheppStrUtil.H"
00021 #include "SheppObjSet.H"
00022 #include "SheppPrint.H"
00023 
00024 #include "IoException.H"
00025 #include "TransportException.H"
00026 #include "EppException.H"
00027 #include "XmlException.H"
00028 
00029 #include "Login.H"
00030 #include "Logout.H"
00031 
00032 using std::ifstream;
00033 
00034 // Functions for eppsh commands
00035 int cmd_source(vector<string> &);
00036 int cmd_login(vector<string> &);
00037 int cmd_logout(vector<string> &);
00038 int cmd_hello(vector<string> &);
00039 int cmd_poll(vector<string> &);
00040 int cmd_domain(vector<string> &);
00041 int cmd_contact(vector<string> &);
00042 #if USE_BR_ORG
00043 int cmd_brorg(vector<string> &);
00044 #endif //USE_BR_ORG
00045 #if USE_IP_MANAGEMENT
00046 int cmd_asn(vector<string> &);
00047 int cmd_ipnetwork(vector<string> &);
00048 #endif //USE_IP_MANAGEMENT
00049 
00050 int cmd_xmlcmd(vector<string> &);
00051 int cmd_xmlrsp(vector<string> &);
00052 int cmd_runxml(vector<string> &);
00053 
00054 int cmd_server(vector<string> &);
00055 int cmd_port(vector<string> &);
00056 int cmd_client_pem(vector<string> &);
00057 int cmd_root_pem(vector<string> &);
00058 int cmd_pass(vector<string> &);
00059 int cmd_user(vector<string> &);
00060 int cmd_login_pw(vector<string> &);
00061 int cmd_login_new_pw(vector<string> &);
00062 int cmd_lang(vector<string> &);
00063 int cmd_connect(vector<string> &);
00064 int cmd_disconnect(vector<string> &);
00065 
00066 int cmd_help(vector<string> &);
00067 int cmd_about(vector<string> &);
00068 int cmd_quit(vector<string> &);
00069 
00070 int cmd_beauty(vector<string> &);
00071 int cmd_debug(vector<string> &);
00072 int cmd_autorsp(vector<string> &);
00073 int cmd_keepalive(vector<string> &);
00074 int cmd_peer_crt_check(vector<string> &);
00075 
00076 string getpasswd(string prompt);
00077 
00079 void init_commands()
00080 {
00081   SheppCommand cmd;
00082 
00083   //non-EPP commands
00084   cmd.reset("?", cmd_help, "This help screen");
00085   _commands.insert(_commands.end(), cmd);
00086   cmd.reset("help", cmd_help, "This help screen");
00087   _commands.insert(_commands.end(), cmd);
00088   cmd.reset("about", cmd_about, "shepp version information");
00089   _commands.insert(_commands.end(), cmd);
00090   cmd.reset("exit", cmd_quit, "Exit shepp\n");
00091   _commands.insert(_commands.end(), cmd);
00092 
00093   //runtime flags
00094   cmd.reset("beauty", cmd_beauty, "Turn XML beautifier on/off");
00095   _commands.insert(_commands.end(), cmd);
00096   cmd.reset("debug", cmd_debug, "Turn debug messages on/off");
00097   _commands.insert(_commands.end(), cmd);
00098   cmd.reset("autorsp", cmd_autorsp, "Turn auto-show response on/off");
00099   _commands.insert(_commands.end(), cmd);
00100   cmd.reset("keepalive", cmd_keepalive, "Turn keep-alive on/off");
00101   _commands.insert(_commands.end(), cmd);
00102   cmd.reset("peer-crt-check", cmd_peer_crt_check,
00103             "Turn peer's certificate check on/off\n");
00104   _commands.insert(_commands.end(), cmd);
00105 
00106   //connection related commands
00107   cmd.reset("source", cmd_source, "Read and execute file with shepp commands");
00108   _commands.insert(_commands.end(), cmd);
00109   cmd.reset("server", cmd_server, "Sets server address");
00110   _commands.insert(_commands.end(), cmd);
00111   cmd.reset("port", cmd_port, "Sets server port");
00112   _commands.insert(_commands.end(), cmd);
00113   cmd.reset("client-pem", cmd_client_pem,
00114             "Sets client.pem certificate file location");
00115   _commands.insert(_commands.end(), cmd);
00116   cmd.reset("root-pem", cmd_root_pem,
00117             "Sets root.pem certificate file location");
00118   _commands.insert(_commands.end(), cmd);
00119   cmd.reset("pass", cmd_pass, "Sets SSL certificate passphrase");
00120   _commands.insert(_commands.end(), cmd);  
00121   cmd.reset("user", cmd_user, "Sets EPP Login username");
00122   _commands.insert(_commands.end(), cmd);
00123   cmd.reset("pw", cmd_login_pw, "Sets EPP Login password");
00124   _commands.insert(_commands.end(), cmd);
00125   cmd.reset("newpw", cmd_login_new_pw, "Sets new EPP Login password");
00126   _commands.insert(_commands.end(), cmd);
00127   cmd.reset("lang", cmd_lang, "Sets text language");
00128   _commands.insert(_commands.end(), cmd);
00129   cmd.reset("connect", cmd_connect, "Establishes connection");
00130   _commands.insert(_commands.end(), cmd);
00131   cmd.reset("disconnect", cmd_disconnect, "Closes connection\n");
00132   _commands.insert(_commands.end(), cmd);
00133 
00134   //EPP commands
00135   cmd.reset("login", cmd_login, "EPP Login");
00136   _commands.insert(_commands.end(), cmd);
00137   cmd.reset("logout", cmd_logout, "EPP Logout");
00138   _commands.insert(_commands.end(), cmd);
00139   cmd.reset("hello", cmd_hello, "EPP Hello");
00140   _commands.insert(_commands.end(), cmd);
00141   cmd.reset("poll", cmd_poll, "EPP Poll related actions");
00142   _commands.insert(_commands.end(), cmd);
00143   cmd.reset("domain", cmd_domain, "EPP Domain related actions");
00144   _commands.insert(_commands.end(), cmd);
00145   cmd.reset("contact", cmd_contact,
00146 #if USE_BR_ORG
00147             "EPP Contact related actions"
00148 #else
00149             "EPP Contact related actions\n"
00150 #endif
00151 );
00152   _commands.insert(_commands.end(), cmd);
00153 #if USE_IP_MANAGEMENT
00154   cmd.reset("asn", cmd_asn, "Autonomous System related actions");
00155   _commands.insert(_commands.end(), cmd);
00156   cmd.reset("ipnetwork", cmd_ipnetwork, "IP network related actions");
00157   _commands.insert(_commands.end(), cmd);
00158 #endif //USE_IP_MANAGEMENT
00159 #if USE_BR_ORG
00160   cmd.reset("brorg", cmd_brorg, "EPP BrOrg related actions\n");
00161   _commands.insert(_commands.end(), cmd);
00162 #endif //USE_BR_ORG
00163 
00164   //XML related commands
00165   cmd.reset("xmlcmd", cmd_xmlcmd, "Show last command XML");
00166   _commands.insert(_commands.end(), cmd);
00167   cmd.reset("xmlrsp", cmd_xmlrsp, "Show last response XML");
00168   _commands.insert(_commands.end(), cmd);
00169   cmd.reset("runxml", cmd_runxml, "Run XML input (good for debugging)");
00170   _commands.insert(_commands.end(), cmd);
00171 }
00172 
00174 
00178 const SheppCommand* find_command(string name)
00179 {
00180   list<SheppCommand>::const_iterator it;
00181   for (it = _commands.begin(); it != _commands.end(); it++) {
00182     if ((*it).name == name) {
00183       return &(*it);
00184     }
00185   }
00186 
00187   return ((const SheppCommand *) NULL);
00188 }
00189 
00192 
00196 int executeInputFile(const string &file)
00197 {
00198   ifstream inputFile;
00199   inputFile.open(file.c_str());
00200   if (inputFile.eof() == true || 
00201       inputFile.fail() == true || 
00202       inputFile.bad() == true) {
00203     printf("error reading input file.\n");
00204     return -1;
00205   }
00206 
00207   init_commands();
00208 
00209   while (inputFile.good() == true) {
00210     string cmdLine("");
00211     getline(inputFile, cmdLine);
00212 
00213     SheppStrUtil::trim((char*) cmdLine.c_str());
00214     if (cmdLine.size() == 0) {
00215       continue;
00216     }
00217 
00218     // Comments symbols
00219     if (cmdLine[0] == '#' || cmdLine[0] == ';') {
00220       continue;
00221     }
00222 
00223     // get the first word (command name)
00224     vector<string> command_args = 
00225       SheppStrUtil::parse_line((char*) cmdLine.c_str());
00226     
00227     // args will be empty if any word exceed MAX_WORD_LENGTH
00228     if (command_args.empty()) {
00229       continue;
00230     }
00231     
00232     const SheppCommand* cmd = find_command(command_args[0]);
00233     
00234     if (cmd == NULL) {
00235       fprintf(stderr, "Unknown command: %s\n", command_args[0].c_str());
00236       continue;
00237     }
00238     
00239     // commands that require raw command-line input
00240     if (command_args[0] == "runxml") {
00241       string rawcmd(cmdLine);
00242       rawcmd = rawcmd.substr(command_args[0].length());
00243       while (whitespace(rawcmd[0])) {
00244         rawcmd = rawcmd.substr(1);
00245       }
00246       
00247       if (rawcmd == "") {
00248         // read input
00249         printf("Enter XML code. Escape character is '^]'.\n");
00250 
00251         string unit;
00252         getline(cin, unit, (char)29);
00253         rawcmd += unit;
00254       }
00255       
00256       command_args.clear();
00257       command_args.push_back(rawcmd);
00258     } else {
00259       command_args.erase(command_args.begin());
00260     }
00261 
00262     (*(cmd->function))(command_args);
00263   }
00264 
00265   inputFile.close();
00266 
00267   return 0;
00268 }
00269 
00272 
00276 int cmd_source(vector<string> &args)
00277 {
00278   if (args.size() != 0) {
00279     return executeInputFile(args[0]);
00280   }
00281 
00282   return 0;
00283 }
00284 
00286 
00290 int cmd_server(vector<string> &args)
00291 {
00292   if (args.size() != 0) {
00293     string new_port;
00294     string new_server;
00295     SheppStrUtil::split(args[0], new_server, new_port, ":", true);
00296     if (new_server != _server) {
00297       _server = new_server;
00298     }
00299     if (new_port != "" && _port != atoi(new_port.c_str())) {
00300       _port = atoi(new_port.c_str());
00301     }
00302   }
00303 
00304   if (_server != "") {
00305     printf("server %s:%d\n", _server.c_str(), _port);
00306   } else {
00307     printf("no server address given.\n");
00308   }
00309 
00310   return 0;
00311 }
00312 
00314 
00318 int cmd_port(vector<string> &args)
00319 {
00320   if (args.size() == 1) {
00321     if (_port != atoi(args[0].c_str())) {
00322       _port = atoi(args[0].c_str());
00323     }
00324   }
00325 
00326   printf("port %d\n", _port);
00327 
00328   return 0;
00329 }
00330 
00332 
00336 int cmd_client_pem(vector<string> &args)
00337 {
00338   if (args.size() == 1) {
00339     if (_client_pem != args[0]) {
00340       _client_pem = args[0];
00341       printf("Warning: changing cetificates requires you to reconnect.\n");
00342     }
00343   } else if (args.size() > 1) {
00344     printf("usage: client-pem <file>\n");
00345     return -1;
00346   }
00347 
00348   printf("Current client.pem is %s\n", _client_pem.c_str());
00349   return 0;
00350 }
00351 
00353 
00357 int cmd_root_pem(vector<string> &args)
00358 {
00359   if (args.size() == 1) {
00360     if (_root_pem != args[0]) {
00361       _root_pem = args[0];
00362       printf("Warning: changing cetificates requires you to reconnect.\n");
00363     }
00364   } else if (args.size() > 1) {
00365     printf("usage: root-pem <file>\n");
00366     return -1;
00367   }
00368 
00369   printf("Current root.pem is %s\n", _root_pem.c_str());
00370   return 0;
00371 }
00372 
00374 
00378 int cmd_pass(vector<string> &args)
00379 {
00380   if (!args.empty()) {
00381     _passphrase = args[0];
00382   } else {
00383     _passphrase = getpasswd("Passphrase");
00384   }
00385 
00386   return 0;
00387 }
00388 
00390 
00394 int cmd_user(vector<string> &args)
00395 {
00396   if (args.size() == 0) {
00397     if (_user != "") {
00398       printf("username: %s\n", _user.c_str());
00399       return 0;
00400     } else {
00401       printf("no username given.\n");
00402       return -1;
00403     }
00404   }
00405 
00406   _user = args[0];
00407 
00408   return 0;
00409 }
00410 
00412 
00416 int cmd_login_pw(vector<string> &args)
00417 {
00418   if (!args.empty()) {
00419     _login_pw = args[0];
00420   } else {
00421     _login_pw = getpasswd("Password");
00422   }
00423 
00424   return 0;
00425 }
00426 
00428 
00432 int cmd_login_new_pw(vector<string> &args)
00433 {
00434   if (!args.empty()) {
00435     _login_new_pw = args[0];
00436   } else {
00437     _login_new_pw = getpasswd("New Password");
00438   }
00439 
00440   _new_pw = true;
00441   return 0;
00442 }
00443 
00445 
00449 int cmd_lang(vector<string> &args)
00450 {
00451   if (args.empty()) {
00452     printf("usage: lang [en|pt]\n");
00453     return -1;
00454   } else {
00455     _lang = args[0];
00456   }
00457 
00458   return 0;
00459 }
00460 
00462 
00466 int cmd_connect(vector<string> &args)
00467 {
00468   if (cmd_server(args) != 0) {
00469     return -1;
00470   }
00471 
00472   bool exception = false;
00473 
00474   // Session code goes here
00475   try {
00476     _session = auto_ptr<Session>(new Session(_server, _port));
00477     if (_peer_crt_check) {
00478       _session->enable_cert_common_name_check();
00479     } else {
00480       _session->disable_cert_common_name_check();
00481     }
00482     _session->connect(_client_pem, _root_pem, _passphrase);
00483     printf("Connected to %s\n", _server.c_str());
00484     
00485     Greeting *greeting = _session->get_greeting();  
00486     if (_debug) {
00487       if (greeting) {
00488         printf("Greeting received\n");
00489       }
00490     } //_debug
00491 
00492     _xmlrsp = _session->get_last_response();
00493     _xmlcmd = _session->get_last_command();
00494 
00495     _connected = true;
00496 
00497   } catch (const IoException &e) {
00498     printf("ERROR! IO Exception [%d]:\n[%s]\n", 
00499            e.get_code(), e.get_msg().c_str());
00500     exception = true;
00501   } catch (const TransportException &e) {
00502     printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00503            e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00504     exception = true;
00505   } catch (const GeneralException &e) {
00506     printf("ERROR! General Exception [%d]:\n[%s]\n", 
00507            e.get_code(), e.get_msg().c_str());
00508     exception = true;
00509   }
00510 
00511   if (exception) {
00512     return -1;
00513   }
00514 
00515   // print greeting
00516   vector<string> dummy;
00517   cmd_xmlrsp(dummy);
00518 
00519   return 0;
00520 }
00521 
00523 
00527 int cmd_disconnect(vector<string> &args)
00528 {
00529   if (_connected) {
00530     bool exception = false;
00531 
00532     // Session code goes here
00533     try {
00534       _session->disconnect();
00535       printf("disconnected from %s\n", _server.c_str());
00536       _connected = false;
00537     } catch (const TransportException &e) {
00538       printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00539              e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00540       exception = true;
00541     } catch (const GeneralException &e) {
00542       printf("ERROR! General Exception [%d]:\n[%s]\n", 
00543              e.get_code(), e.get_msg().c_str());
00544       exception = true;
00545     }
00546 
00547     if (exception) {
00548       return -1;
00549     }
00550   } else {
00551     printf("not connected\n");
00552   }
00553 
00554   return 0;
00555 }
00556 
00558 
00562 int cmd_beauty(vector<string> &args)
00563 {
00564   if (args.empty()) {
00565     if (_beauty) {
00566       printf("XML beautifier is ON\n");
00567     } else {
00568       printf("XML beautifier is OFF\n");
00569     }
00570     return 0;
00571   } else if (args.size() > 1) {
00572     printf("usage: beauty [on|off]\n");
00573     return -1;
00574   }
00575 
00576   if (args[0] == "on") {
00577     _beauty = true;
00578     printf("XML beautifier now ON\n");
00579   } else if (args[0] == "off") {
00580     _beauty = false;
00581     printf("XML beautifier now OFF\n");
00582   } else {
00583     printf("usage: beauty [on|off]\n");
00584     return -1;
00585   }
00586 
00587   return 0;
00588 }
00589 
00591 
00595 int cmd_debug(vector<string> &args)
00596 {
00597   if (args.empty()) {
00598     if (_debug) {
00599       printf("Debug is ON\n");
00600     } else {
00601       printf("Debug is OFF\n");
00602     }
00603     return 0;
00604   } else if (args.size() > 1) {
00605     printf("usage: debug [on|off]\n");
00606     return -1;
00607   }
00608 
00609   if (args[0] == "on") {
00610     _debug = true;
00611     printf("Debug now ON\n");
00612   } else if (args[0] == "off") {
00613     _debug = false;
00614     printf("Debug now OFF\n");
00615   } else {
00616     printf("usage: beauty [on|off]\n");
00617     return -1;
00618   }
00619 
00620   return 0;
00621 }
00622 
00624 
00628 int cmd_autorsp(vector<string> &args)
00629 {
00630   if (args.empty()) {
00631     if (_autorsp) {
00632       printf("autorsp is ON\n");
00633     } else {
00634       printf("autorsp is OFF\n");
00635     }
00636     return 0;
00637   } else if (args.size() > 1) {
00638     printf("usage: autorsp [on|off]\n");
00639     return -1;
00640   }
00641 
00642   if (args[0] == "on") {
00643     _autorsp = true;
00644     printf("autorsp now ON\n");
00645   } else if (args[0] == "off") {
00646     _autorsp = false;
00647     printf("autorsp now OFF\n");
00648   } else {
00649     printf("usage: autorsp [on|off]\n");
00650     return -1;
00651   }
00652 
00653   return 0;
00654 }
00655 
00657 
00661 int cmd_keepalive(vector<string> &args)
00662 {
00663   if (args.empty()) {
00664     if (_keepalive) {
00665       printf("Keep-alive is ON\n");
00666     } else {
00667       printf("Keep-alive is OFF\n");
00668     }
00669     return 0;
00670   } else if (args.size() > 1) {
00671     printf("usage: keepalive [on|off]\n");
00672     return -1;
00673   }
00674 
00675   if (args[0] == "on") {
00676     _keepalive = true;
00677     printf("Keep-alive now ON\n");
00678   } else if (args[0] == "off") {
00679     _keepalive = false;
00680     printf("Keep-alive now OFF\n");
00681   } else {
00682     printf("usage: keepalive [on|off]\n");
00683     return -1;
00684   }
00685 
00686   if (_keepalive) {
00687     alarm(_keepalive_timer);
00688   } else {
00689     alarm(0);
00690   }
00691 
00692   return 0;
00693 }
00694 
00696 
00700 int cmd_peer_crt_check(vector<string> &args)
00701 {
00702   if (args.empty()) {
00703     if (_peer_crt_check) {
00704       printf("Peer's certificate check is ON\n");
00705     } else {
00706       printf("Peer's certificate check is OFF\n");
00707     }
00708     return 0;
00709   } else if (args.size() > 1) {
00710     printf("usage: peer-crt-check [on|off]\n");
00711     return -1;
00712   }
00713 
00714   if (args[0] == "on") {
00715     _peer_crt_check = true;
00716     printf("Peer's certificate check now ON\n");
00717   } else if (args[0] == "off") {
00718     _peer_crt_check = false;
00719     printf("Peer's certificate check now OFF\n");
00720   } else {
00721     printf("usage: peer-crt-check [on|off]\n");
00722     return -1;
00723   }
00724 
00725   return 0;
00726 }
00727 
00729 static void sig_alrm(int signo)
00730 {
00731   if (_connected && !_cmd_running) {
00732     _ka_running = true;
00733     vector<string> args;
00734     args.push_back((string) "keepalive");
00735     cmd_hello(args);
00736     _ka_running = false;
00737   }
00738   alarm(_keepalive_timer);
00739 }
00740 
00742 
00746 int cmd_xmlcmd(vector<string> &args)
00747 {
00748   if (!_connected) {
00749     printf("not connected\n");
00750     return -1;
00751   }
00752 
00753   if (_beauty) {
00754     // Convert to UTF8
00755     string last_command("");
00756     StrUtil::iso88591_to_utf8(_xmlcmd, last_command);
00757     
00758     if (_session->get_last_command() == "") {
00759       printf("\n");
00760       return 0;
00761     }
00762     try {
00763       StrUtil str_util;
00764       printf("%s\n", str_util.xml_beautifier(last_command).c_str());
00765     } catch (const XmlException &e) {
00766       printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00767              e.get_msg().c_str());
00768     }
00769   } else {
00770     printf("%s\n", _session->get_last_command().c_str());
00771   }
00772   return 0;
00773 }
00774 
00776 
00780 int cmd_xmlrsp(vector<string> &args)
00781 {
00782   if (!_connected) {
00783     printf("not connected\n");
00784     return -1;
00785   }
00786 
00787   if (_beauty) {
00788     // No need to convert to UTF8
00789     if (_xmlrsp == "") {
00790       printf("\n");
00791       return 0;
00792     }
00793     try {
00794       StrUtil str_util;
00795       printf("%s\n", str_util.xml_beautifier(_xmlrsp).c_str());
00796     } catch (const XmlException &e) {
00797       printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00798              e.get_msg().c_str());
00799     }
00800   } else {
00801     printf("%s\n", _xmlrsp.c_str());
00802   }
00803   return 0;
00804 }
00805 
00806 // EPP action processing functions
00807 
00809 void print_cmd_sent_ok()
00810 {
00811   if (!_autorsp) {
00812     printf("Ok! Use 'xmlcmd' and 'xmlrsp' to view command/response "
00813            "XML code.\n");
00814   } else {
00815     vector<string> dummy;
00816     cmd_xmlrsp(dummy);
00817   }
00818 }
00819 
00821 
00824 int process_action(Action &act)
00825 {
00826   if (!_connected) {
00827     printf("not connected\n");
00828     return -1;
00829   }
00830 
00831   bool exception = false;
00832 
00833   // prevents concurrency with keep-alive
00834   if (!_ka_running) {
00835     _cmd_running = true;
00836   } else {
00837     printf("ERROR! Keep-alive is running. Please try again.\n");
00838     return -1;
00839   }
00840 
00841   try {
00842     _session->process_action(&act);
00843     _xmlrsp = _session->get_last_response();
00844     _xmlcmd = _session->get_last_command();
00845   } catch (const EppException &e) {
00846     printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00847            e.get_msg().c_str());  
00848     exception = true;
00849   } catch (const IoException &e) {
00850     printf("ERROR! IO Exception [%d]:\n[%s]\n", 
00851            e.get_code(), e.get_msg().c_str());
00852     exception = true;
00853     _connected = false;
00854   } catch (const TransportException &e) {
00855     printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00856            e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00857     exception = true;
00858     _connected = false;
00859   } catch (const GeneralException &e) {
00860     printf("ERROR! General Exception [%d]:\n[%s]\n", 
00861            e.get_code(), e.get_msg().c_str());
00862     exception = true;
00863   }
00864 
00865   if (exception) {
00866     _cmd_running = false;
00867     return -1;
00868   }
00869 
00870   print_cmd_sent_ok();
00871 
00872   // a command was just run; postpone keepalive alarm
00873   if (_keepalive) {
00874     alarm(_keepalive_timer);
00875   }
00876 
00877   _cmd_running = false;
00878   return 0;
00879 }
00880 
00882 
00885 int cmd_runxml(vector<string> &args)
00886 {
00887   if (!_connected) {
00888     printf("not connected\n");
00889     return -1;
00890   }
00891 
00892   bool exception = false;
00893 
00894   // prevents concurrency with keep-alive
00895   if (!_ka_running) {
00896     _cmd_running = true;
00897   } else {
00898     printf("ERROR! Keep-alive is running. Please try again.\n");
00899     return -1;
00900   }
00901 
00902   try {
00903     _session->runXML(args[0]);
00904     _xmlrsp = _session->get_last_response();
00905     _xmlcmd = _session->get_last_command();
00906   } catch (const EppException &e) {
00907     printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00908            e.get_msg().c_str());  
00909     exception = true;
00910   } catch (const IoException &e) {
00911     printf("ERROR! IO Exception [%d]:\n[%s]\n", 
00912            e.get_code(), e.get_msg().c_str());
00913     exception = true;
00914     _connected = false;
00915   } catch (const TransportException &e) {
00916     printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00917            e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00918     exception = true;
00919     _connected = false;
00920   } catch (const GeneralException &e) {
00921     printf("ERROR! General Exception [%d]:\n[%s]\n", 
00922            e.get_code(), e.get_msg().c_str());
00923     exception = true;
00924   }
00925 
00926   if (exception) {
00927     _cmd_running = false;
00928     return -1;
00929   }
00930 
00931   print_cmd_sent_ok();
00932 
00933   // a command was just run; postpone keepalive alarm
00934   if (_keepalive) {
00935     alarm(_keepalive_timer);
00936   }
00937 
00938   _cmd_running = false;
00939   return 0;
00940 }
00941 
00943 
00947 int cmd_login(vector<string> &args)
00948 {
00949   if (!_connected) {
00950     printf("not connected\n");
00951     return -1;
00952   }
00953 
00954   if (_user == "") {
00955     printf("no username given.\n");
00956     return -1;
00957   }
00958 
00959   Login act;
00960   LoginCmd *cmd = act.get_command();
00961 
00962   cmd->set_clID(_user);
00963   cmd->set_pw(_login_pw);
00964 
00965   if (_new_pw) {
00966     _new_pw = false;
00967     cmd->set_new_pw(_login_new_pw);
00968   }
00969 
00970   if (_lang != "" && _lang != "en") {
00971     cmd->set_lang(_lang);
00972   }
00973 
00974   if (process_action(act) != 0) {
00975     return -1;
00976   }
00977 
00978   return 0;
00979 }
00980 
00982 
00986 int cmd_logout(vector<string> &args)
00987 {
00988   if (!_connected) {
00989     printf("not connected\n");
00990     return -1;
00991   }
00992 
00993   Logout act;
00994 
00995   if (process_action(act) != 0) {
00996     return -1;
00997   }  
00998 
00999   return 0;
01000 }
01001 
01003 
01007 int cmd_hello(vector<string> &args)
01008 {
01009   if (!_connected) {
01010     printf("not connected\n");
01011     return -1;
01012   }
01013 
01014   if (_ka_running &&
01015       !(args.size() == 1 && args[0] == "keepalive")) {
01016     printf("ERROR! Keep-alive is running. Please try again.\n");
01017     return -1;
01018   }
01019 
01020   bool exception = false;
01021 
01022   // prevents concurrency with keep-alive
01023   if (!_ka_running) {
01024     _cmd_running = true;
01025   }
01026 
01027   try {
01028     if (_debug) {
01029       printf("Sending EPP Hello\n");
01030     }
01031     _session->send_hello();
01032     // updates _xmlcmd and _xmlrsp only if it's not a keep-alive run
01033     if (!_ka_running) {
01034       _xmlrsp = _session->get_last_response();
01035       _xmlcmd = _session->get_last_command();
01036     }
01037   } catch (const EppException &e) {
01038     printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
01039            e.get_msg().c_str());  
01040     exception = true;
01041   } catch (const IoException &e) {
01042     printf("ERROR! IO Exception [%d]:\n[%s]\n", 
01043            e.get_code(), e.get_msg().c_str());
01044     exception = true;
01045     _connected = false;
01046   } catch (const TransportException &e) {
01047     printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
01048            e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
01049     exception = true;
01050     _connected = false;
01051   } catch (const GeneralException &e) {
01052     printf("ERROR! General Exception [%d]:\n[%s]\n", 
01053            e.get_code(), e.get_msg().c_str());
01054     exception = true;
01055   }
01056 
01057   if (exception) {
01058     if (_cmd_running) {
01059       _cmd_running = false;
01060     }
01061     return -1;
01062   }
01063 
01064   if (!_ka_running) {
01065     print_cmd_sent_ok();
01066   }
01067 
01068   if (_cmd_running) {
01069     // a command was just run; postpone keepalive alarm
01070     if (_keepalive) {
01071       alarm(_keepalive_timer);
01072     }
01073     _cmd_running = false;
01074   }
01075   return 0;
01076 }
01077 
01078 // non-EPP commands
01079 
01081 
01085 int cmd_quit(vector<string> &args)
01086 {
01087   if (_connected) {
01088     _session->disconnect();
01089   }
01090   printf("Bye\n");
01091   exit(0);
01092 }
01093 
01095 
01099 int cmd_help(vector<string> &args)
01100 {
01101   list<SheppCommand>::const_iterator it;
01102   for (it = _commands.begin(); it != _commands.end(); it++) {
01103     printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
01104   }
01105 
01106   return 0;
01107 }
01108 
01110 
01114 int cmd_about(vector<string> &args)
01115 {
01116   about();
01117   return 0;
01118 }
01119 
01121 
01125 string getpasswd(string prompt) {
01126   printf("%s:", prompt.c_str());
01127 
01128   string passwd("");
01129   struct termios old_opts;
01130   struct termios new_opts;
01131 
01132   int res = 0;
01133 
01134   // store old settings
01135   res = tcgetattr(STDIN_FILENO, &old_opts);
01136   assert(res == 0);
01137 
01138   // set new terminal parms
01139   memcpy(&new_opts, &old_opts, sizeof(new_opts));
01140   new_opts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
01141   new_opts.c_lflag |= ICANON;
01142   tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
01143 
01144   // read password
01145   getline(cin, passwd);
01146   cout << endl;
01147 
01148   // restore old settings
01149   res = tcsetattr(STDIN_FILENO, TCSANOW, &old_opts);
01150   assert(res == 0);
01151 
01152   return passwd;
01153 }
01154 
01155 #endif //__SHEPP_COMMAND_FUNCTIONS_H__

Generated on Tue Mar 17 16:03:07 2009 for libepp_nicbr by  doxygen 1.4.7