00001
00002
00006 #ifndef __SHEPP_COMMAND_FUNCTIONS_H__
00007 #define __SHEPP_COMMAND_FUNCTIONS_H__
00008
00009 #include <fstream>
00010 #include <unistd.h>
00011
00012
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
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
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
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
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
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
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
00219 if (cmdLine[0] == '#' || cmdLine[0] == ';') {
00220 continue;
00221 }
00222
00223
00224 vector<string> command_args =
00225 SheppStrUtil::parse_line((char*) cmdLine.c_str());
00226
00227
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
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
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
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
00487
00488 set<string> extensions = greeting->get_extURI();
00489 set<string>::iterator it;
00490 for (it = extensions.begin(); it != extensions.end(); it++) {
00491 if (*it == "urn:ietf:params:xml:ns:secDNS-1.0") {
00492 _serverSecDnsVersion = "1.0";
00493 } else if (*it == "urn:ietf:params:xml:ns:secDNS-1.1") {
00494 _serverSecDnsVersion = "1.1";
00495 }
00496 }
00497
00498 if (_debug) {
00499 if (greeting) {
00500 printf("Greeting received\n");
00501 }
00502 }
00503
00504 _xmlrsp = _session->get_last_response();
00505 _xmlcmd = _session->get_last_command();
00506
00507 _connected = true;
00508
00509 } catch (const IoException &e) {
00510 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00511 e.get_code(), e.get_msg().c_str());
00512 exception = true;
00513 } catch (const TransportException &e) {
00514 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00515 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00516 exception = true;
00517 } catch (const GeneralException &e) {
00518 printf("ERROR! General Exception [%d]:\n[%s]\n",
00519 e.get_code(), e.get_msg().c_str());
00520 exception = true;
00521 }
00522
00523 if (exception) {
00524 return -1;
00525 }
00526
00527
00528 vector<string> dummy;
00529 cmd_xmlrsp(dummy);
00530
00531 return 0;
00532 }
00533
00535
00539 int cmd_disconnect(vector<string> &args)
00540 {
00541 if (_connected) {
00542 bool exception = false;
00543
00544
00545 try {
00546 _session->disconnect();
00547 printf("disconnected from %s\n", _server.c_str());
00548 _connected = false;
00549 } catch (const TransportException &e) {
00550 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00551 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00552 exception = true;
00553 } catch (const GeneralException &e) {
00554 printf("ERROR! General Exception [%d]:\n[%s]\n",
00555 e.get_code(), e.get_msg().c_str());
00556 exception = true;
00557 }
00558
00559 if (exception) {
00560 return -1;
00561 }
00562 } else {
00563 printf("not connected\n");
00564 }
00565
00566 return 0;
00567 }
00568
00570
00574 int cmd_beauty(vector<string> &args)
00575 {
00576 if (args.empty()) {
00577 if (_beauty) {
00578 printf("XML beautifier is ON\n");
00579 } else {
00580 printf("XML beautifier is OFF\n");
00581 }
00582 return 0;
00583 } else if (args.size() > 1) {
00584 printf("usage: beauty [on|off]\n");
00585 return -1;
00586 }
00587
00588 if (args[0] == "on") {
00589 _beauty = true;
00590 printf("XML beautifier now ON\n");
00591 } else if (args[0] == "off") {
00592 _beauty = false;
00593 printf("XML beautifier now OFF\n");
00594 } else {
00595 printf("usage: beauty [on|off]\n");
00596 return -1;
00597 }
00598
00599 return 0;
00600 }
00601
00603
00607 int cmd_debug(vector<string> &args)
00608 {
00609 if (args.empty()) {
00610 if (_debug) {
00611 printf("Debug is ON\n");
00612 } else {
00613 printf("Debug is OFF\n");
00614 }
00615 return 0;
00616 } else if (args.size() > 1) {
00617 printf("usage: debug [on|off]\n");
00618 return -1;
00619 }
00620
00621 if (args[0] == "on") {
00622 _debug = true;
00623 printf("Debug now ON\n");
00624 } else if (args[0] == "off") {
00625 _debug = false;
00626 printf("Debug now OFF\n");
00627 } else {
00628 printf("usage: beauty [on|off]\n");
00629 return -1;
00630 }
00631
00632 return 0;
00633 }
00634
00636
00640 int cmd_autorsp(vector<string> &args)
00641 {
00642 if (args.empty()) {
00643 if (_autorsp) {
00644 printf("autorsp is ON\n");
00645 } else {
00646 printf("autorsp is OFF\n");
00647 }
00648 return 0;
00649 } else if (args.size() > 1) {
00650 printf("usage: autorsp [on|off]\n");
00651 return -1;
00652 }
00653
00654 if (args[0] == "on") {
00655 _autorsp = true;
00656 printf("autorsp now ON\n");
00657 } else if (args[0] == "off") {
00658 _autorsp = false;
00659 printf("autorsp now OFF\n");
00660 } else {
00661 printf("usage: autorsp [on|off]\n");
00662 return -1;
00663 }
00664
00665 return 0;
00666 }
00667
00669
00673 int cmd_keepalive(vector<string> &args)
00674 {
00675 if (args.empty()) {
00676 if (_keepalive) {
00677 printf("Keep-alive is ON\n");
00678 } else {
00679 printf("Keep-alive is OFF\n");
00680 }
00681 return 0;
00682 } else if (args.size() > 1) {
00683 printf("usage: keepalive [on|off]\n");
00684 return -1;
00685 }
00686
00687 if (args[0] == "on") {
00688 _keepalive = true;
00689 printf("Keep-alive now ON\n");
00690 } else if (args[0] == "off") {
00691 _keepalive = false;
00692 printf("Keep-alive now OFF\n");
00693 } else {
00694 printf("usage: keepalive [on|off]\n");
00695 return -1;
00696 }
00697
00698 if (_keepalive) {
00699 alarm(_keepalive_timer);
00700 } else {
00701 alarm(0);
00702 }
00703
00704 return 0;
00705 }
00706
00708
00712 int cmd_peer_crt_check(vector<string> &args)
00713 {
00714 if (args.empty()) {
00715 if (_peer_crt_check) {
00716 printf("Peer's certificate check is ON\n");
00717 } else {
00718 printf("Peer's certificate check is OFF\n");
00719 }
00720 return 0;
00721 } else if (args.size() > 1) {
00722 printf("usage: peer-crt-check [on|off]\n");
00723 return -1;
00724 }
00725
00726 if (args[0] == "on") {
00727 _peer_crt_check = true;
00728 printf("Peer's certificate check now ON\n");
00729 } else if (args[0] == "off") {
00730 _peer_crt_check = false;
00731 printf("Peer's certificate check now OFF\n");
00732 } else {
00733 printf("usage: peer-crt-check [on|off]\n");
00734 return -1;
00735 }
00736
00737 return 0;
00738 }
00739
00741 static void sig_alrm(int signo)
00742 {
00743 if (_connected && !_cmd_running) {
00744 _ka_running = true;
00745 vector<string> args;
00746 args.push_back((string) "keepalive");
00747 cmd_hello(args);
00748 _ka_running = false;
00749 }
00750 alarm(_keepalive_timer);
00751 }
00752
00754
00758 int cmd_xmlcmd(vector<string> &args)
00759 {
00760 if (!_connected) {
00761 printf("not connected\n");
00762 return -1;
00763 }
00764
00765 if (_beauty) {
00766
00767 string last_command("");
00768 StrUtil::iso88591_to_utf8(_xmlcmd, last_command);
00769
00770 if (_session->get_last_command() == "") {
00771 printf("\n");
00772 return 0;
00773 }
00774 try {
00775 StrUtil str_util;
00776 printf("%s\n", str_util.xml_beautifier(last_command).c_str());
00777 } catch (const XmlException &e) {
00778 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00779 e.get_msg().c_str());
00780 }
00781 } else {
00782 printf("%s\n", _session->get_last_command().c_str());
00783 }
00784 return 0;
00785 }
00786
00788
00792 int cmd_xmlrsp(vector<string> &args)
00793 {
00794 if (!_connected) {
00795 printf("not connected\n");
00796 return -1;
00797 }
00798
00799 if (_beauty) {
00800
00801 if (_xmlrsp == "") {
00802 printf("\n");
00803 return 0;
00804 }
00805 try {
00806 StrUtil str_util;
00807 printf("%s\n", str_util.xml_beautifier(_xmlrsp).c_str());
00808 } catch (const XmlException &e) {
00809 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00810 e.get_msg().c_str());
00811 }
00812 } else {
00813 printf("%s\n", _xmlrsp.c_str());
00814 }
00815 return 0;
00816 }
00817
00818
00819
00821 void print_cmd_sent_ok()
00822 {
00823 if (!_autorsp) {
00824 printf("Ok! Use 'xmlcmd' and 'xmlrsp' to view command/response "
00825 "XML code.\n");
00826 } else {
00827 vector<string> dummy;
00828 cmd_xmlrsp(dummy);
00829 }
00830 }
00831
00833
00836 int process_action(Action &act)
00837 {
00838 if (!_connected) {
00839 printf("not connected\n");
00840 return -1;
00841 }
00842
00843 bool exception = false;
00844
00845
00846 if (!_ka_running) {
00847 _cmd_running = true;
00848 } else {
00849 printf("ERROR! Keep-alive is running. Please try again.\n");
00850 return -1;
00851 }
00852
00853 try {
00854 _session->process_action(&act);
00855 _xmlrsp = _session->get_last_response();
00856 _xmlcmd = _session->get_last_command();
00857 } catch (const EppException &e) {
00858 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00859 e.get_msg().c_str());
00860 exception = true;
00861 } catch (const IoException &e) {
00862 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00863 e.get_code(), e.get_msg().c_str());
00864 exception = true;
00865 _connected = false;
00866 } catch (const TransportException &e) {
00867 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00868 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00869 exception = true;
00870 _connected = false;
00871 } catch (const GeneralException &e) {
00872 printf("ERROR! General Exception [%d]:\n[%s]\n",
00873 e.get_code(), e.get_msg().c_str());
00874 exception = true;
00875 }
00876
00877 if (exception) {
00878 _cmd_running = false;
00879 return -1;
00880 }
00881
00882 print_cmd_sent_ok();
00883
00884
00885 if (_keepalive) {
00886 alarm(_keepalive_timer);
00887 }
00888
00889 _cmd_running = false;
00890 return 0;
00891 }
00892
00894
00897 int cmd_runxml(vector<string> &args)
00898 {
00899 if (!_connected) {
00900 printf("not connected\n");
00901 return -1;
00902 }
00903
00904 bool exception = false;
00905
00906
00907 if (!_ka_running) {
00908 _cmd_running = true;
00909 } else {
00910 printf("ERROR! Keep-alive is running. Please try again.\n");
00911 return -1;
00912 }
00913
00914 try {
00915 _session->runXML(args[0]);
00916 _xmlrsp = _session->get_last_response();
00917 _xmlcmd = _session->get_last_command();
00918 } catch (const EppException &e) {
00919 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00920 e.get_msg().c_str());
00921 exception = true;
00922 } catch (const IoException &e) {
00923 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00924 e.get_code(), e.get_msg().c_str());
00925 exception = true;
00926 _connected = false;
00927 } catch (const TransportException &e) {
00928 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00929 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00930 exception = true;
00931 _connected = false;
00932 } catch (const GeneralException &e) {
00933 printf("ERROR! General Exception [%d]:\n[%s]\n",
00934 e.get_code(), e.get_msg().c_str());
00935 exception = true;
00936 }
00937
00938 if (exception) {
00939 _cmd_running = false;
00940 return -1;
00941 }
00942
00943 print_cmd_sent_ok();
00944
00945
00946 if (_keepalive) {
00947 alarm(_keepalive_timer);
00948 }
00949
00950 _cmd_running = false;
00951 return 0;
00952 }
00953
00955
00959 int cmd_login(vector<string> &args)
00960 {
00961 if (!_connected) {
00962 printf("not connected\n");
00963 return -1;
00964 }
00965
00966 if (_user == "") {
00967 printf("no username given.\n");
00968 return -1;
00969 }
00970
00971 Login act;
00972 LoginCmd *cmd = act.get_command();
00973
00974
00975 #if USE_BR_ORG
00976 cmd->add_extURI("urn:ietf:params:xml:ns:brorg-1.0");
00977 #endif
00978 #if USE_BR_DOMAINS
00979 cmd->add_extURI("urn:ietf:params:xml:ns:brdomain-1.0");
00980 #endif
00981 #if USE_IP_MANAGEMENT
00982 cmd->add_extURI("urn:ietf:params:xml:ns:ipnetwork-1.0");
00983 cmd->add_extURI("urn:ietf:params:xml:ns:asn-1.0");
00984 #endif
00985 cmd->add_extURI("urn:ietf:params:xml:ns:secDNS-1.0");
00986 cmd->add_extURI("urn:ietf:params:xml:ns:secDNS-1.1");
00987
00988 cmd->set_clID(_user);
00989 cmd->set_pw(_login_pw);
00990
00991 if (_new_pw) {
00992 _new_pw = false;
00993 cmd->set_new_pw(_login_new_pw);
00994 }
00995
00996 if (_lang != "" && _lang != "en") {
00997 cmd->set_lang(_lang);
00998 }
00999
01000 if (process_action(act) != 0) {
01001 return -1;
01002 }
01003
01004 return 0;
01005 }
01006
01008
01012 int cmd_logout(vector<string> &args)
01013 {
01014 if (!_connected) {
01015 printf("not connected\n");
01016 return -1;
01017 }
01018
01019 Logout act;
01020
01021 if (process_action(act) != 0) {
01022 return -1;
01023 }
01024
01025 return 0;
01026 }
01027
01029
01033 int cmd_hello(vector<string> &args)
01034 {
01035 if (!_connected) {
01036 printf("not connected\n");
01037 return -1;
01038 }
01039
01040 if (_ka_running &&
01041 !(args.size() == 1 && args[0] == "keepalive")) {
01042 printf("ERROR! Keep-alive is running. Please try again.\n");
01043 return -1;
01044 }
01045
01046 bool exception = false;
01047
01048
01049 if (!_ka_running) {
01050 _cmd_running = true;
01051 }
01052
01053 try {
01054 if (_debug) {
01055 printf("Sending EPP Hello\n");
01056 }
01057 _session->send_hello();
01058
01059 if (!_ka_running) {
01060 _xmlrsp = _session->get_last_response();
01061 _xmlcmd = _session->get_last_command();
01062 }
01063 } catch (const EppException &e) {
01064 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
01065 e.get_msg().c_str());
01066 exception = true;
01067 } catch (const IoException &e) {
01068 printf("ERROR! IO Exception [%d]:\n[%s]\n",
01069 e.get_code(), e.get_msg().c_str());
01070 exception = true;
01071 _connected = false;
01072 } catch (const TransportException &e) {
01073 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
01074 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
01075 exception = true;
01076 _connected = false;
01077 } catch (const GeneralException &e) {
01078 printf("ERROR! General Exception [%d]:\n[%s]\n",
01079 e.get_code(), e.get_msg().c_str());
01080 exception = true;
01081 }
01082
01083 if (exception) {
01084 if (_cmd_running) {
01085 _cmd_running = false;
01086 }
01087 return -1;
01088 }
01089
01090 if (!_ka_running) {
01091 print_cmd_sent_ok();
01092 }
01093
01094 if (_cmd_running) {
01095
01096 if (_keepalive) {
01097 alarm(_keepalive_timer);
01098 }
01099 _cmd_running = false;
01100 }
01101 return 0;
01102 }
01103
01104
01105
01107
01111 int cmd_quit(vector<string> &args)
01112 {
01113 if (_connected) {
01114 _session->disconnect();
01115 }
01116 printf("Bye\n");
01117 exit(0);
01118 }
01119
01121
01125 int cmd_help(vector<string> &args)
01126 {
01127 list<SheppCommand>::const_iterator it;
01128 for (it = _commands.begin(); it != _commands.end(); it++) {
01129 printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
01130 }
01131
01132 return 0;
01133 }
01134
01136
01140 int cmd_about(vector<string> &args)
01141 {
01142 about();
01143 return 0;
01144 }
01145
01147
01151 string getpasswd(string prompt) {
01152 printf("%s:", prompt.c_str());
01153
01154 string passwd("");
01155 struct termios old_opts;
01156 struct termios new_opts;
01157
01158 int res = 0;
01159
01160
01161 res = tcgetattr(STDIN_FILENO, &old_opts);
01162 assert(res == 0);
01163
01164
01165 memcpy(&new_opts, &old_opts, sizeof(new_opts));
01166 new_opts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
01167 new_opts.c_lflag |= ICANON;
01168 tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
01169
01170
01171 getline(cin, passwd);
01172 cout << endl;
01173
01174
01175 res = tcsetattr(STDIN_FILENO, TCSANOW, &old_opts);
01176 assert(res == 0);
01177
01178 return passwd;
01179 }
01180
01181 #endif //__SHEPP_COMMAND_FUNCTIONS_H__