00001
00002
00006 #ifndef __SHEPP_COMMAND_FUNCTIONS_H__
00007 #define __SHEPP_COMMAND_FUNCTIONS_H__
00008
00009 #include "SheppGlobal.H"
00010 #include "SheppStrUtil.H"
00011 #include "SheppObjSet.H"
00012 #include "SheppPrint.H"
00013
00014 #include "IoException.H"
00015 #include "TransportException.H"
00016 #include "EppException.H"
00017 #include "XmlException.H"
00018
00019 #include "Login.H"
00020 #include "Logout.H"
00021
00022
00023 int cmd_login __P((char *));
00024 int cmd_logout __P((char *));
00025 int cmd_hello __P((char *));
00026 int cmd_poll __P((char *));
00027 int cmd_domain __P((char *));
00028 int cmd_contact __P((char *));
00029 int cmd_brorg __P((char *));
00030
00031 int cmd_xmlcmd __P((char *));
00032 int cmd_xmlrsp __P((char *));
00033
00034 int cmd_server __P((char *));
00035 int cmd_port __P((char *));
00036 int cmd_client_pem __P((char *));
00037 int cmd_root_pem __P((char *));
00038 int cmd_pass __P((char *));
00039 int cmd_user __P((char *));
00040 int cmd_login_pw __P((char *));
00041 int cmd_login_new_pw __P((char *));
00042 int cmd_lang __P((char *));
00043 int cmd_connect __P((char *));
00044
00045 int cmd_help __P((char *));
00046 int cmd_about __P((char *));
00047 int cmd_quit __P((char *));
00048
00049 int cmd_beauty __P((char *));
00050 int cmd_debug __P((char *));
00051 int cmd_autorsp __P((char *));
00052 int cmd_keepalive __P((char *));
00053 int cmd_peer_crt_check __P((char*));
00054
00056 void init_commands()
00057 {
00058 SheppCommand cmd;
00059
00060
00061 cmd.reset("?", cmd_help, "This help screen");
00062 _commands.insert(_commands.end(), cmd);
00063 cmd.reset("help", cmd_help, "This help screen");
00064 _commands.insert(_commands.end(), cmd);
00065 cmd.reset("about", cmd_about, "shepp version information");
00066 _commands.insert(_commands.end(), cmd);
00067 cmd.reset("exit", cmd_quit, "Exit shepp\n");
00068 _commands.insert(_commands.end(), cmd);
00069
00070
00071 cmd.reset("beauty", cmd_beauty, "Turn XML beautifier on/off");
00072 _commands.insert(_commands.end(), cmd);
00073 cmd.reset("debug", cmd_debug, "Turn debug messages on/off");
00074 _commands.insert(_commands.end(), cmd);
00075 cmd.reset("autorsp", cmd_autorsp, "Turn auto-show response on/off");
00076 _commands.insert(_commands.end(), cmd);
00077 cmd.reset("keepalive", cmd_keepalive, "Turn keep-alive on/off");
00078 _commands.insert(_commands.end(), cmd);
00079 cmd.reset("peer-crt-check", cmd_peer_crt_check,
00080 "Turn peer's certificate check on/off\n");
00081 _commands.insert(_commands.end(), cmd);
00082
00083
00084 cmd.reset("server", cmd_server, "Sets server address");
00085 _commands.insert(_commands.end(), cmd);
00086 cmd.reset("port", cmd_port, "Sets server port");
00087 _commands.insert(_commands.end(), cmd);
00088 cmd.reset("client-pem", cmd_client_pem,
00089 "Sets client.pem certificate file location");
00090 _commands.insert(_commands.end(), cmd);
00091 cmd.reset("root-pem", cmd_root_pem,
00092 "Sets root.pem certificate file location");
00093 _commands.insert(_commands.end(), cmd);
00094 cmd.reset("pass", cmd_pass, "Sets SSL certificate passphrase");
00095 _commands.insert(_commands.end(), cmd);
00096 cmd.reset("user", cmd_user, "Sets EPP Login username");
00097 _commands.insert(_commands.end(), cmd);
00098 cmd.reset("pw", cmd_login_pw, "Sets EPP Login password");
00099 _commands.insert(_commands.end(), cmd);
00100 cmd.reset("newpw", cmd_login_new_pw, "Sets new EPP Login password");
00101 _commands.insert(_commands.end(), cmd);
00102 cmd.reset("lang", cmd_lang, "Sets text language");
00103 _commands.insert(_commands.end(), cmd);
00104 cmd.reset("connect", cmd_connect, "Establishes connection\n");
00105 _commands.insert(_commands.end(), cmd);
00106
00107
00108 cmd.reset("login", cmd_login, "EPP Login");
00109 _commands.insert(_commands.end(), cmd);
00110 cmd.reset("logout", cmd_logout, "EPP Logout");
00111 _commands.insert(_commands.end(), cmd);
00112 cmd.reset("hello", cmd_hello, "EPP Hello");
00113 _commands.insert(_commands.end(), cmd);
00114 cmd.reset("poll", cmd_poll, "EPP Poll related actions");
00115 _commands.insert(_commands.end(), cmd);
00116 cmd.reset("domain", cmd_domain, "EPP Domain related actions");
00117 _commands.insert(_commands.end(), cmd);
00118 cmd.reset("contact", cmd_contact, "EPP Contact related actions");
00119 _commands.insert(_commands.end(), cmd);
00120 cmd.reset("brorg", cmd_brorg, "EPP BrOrg related actions\n");
00121 _commands.insert(_commands.end(), cmd);
00122
00123
00124 cmd.reset("xmlcmd", cmd_xmlcmd, "Show last command XML");
00125 _commands.insert(_commands.end(), cmd);
00126 cmd.reset("xmlrsp", cmd_xmlrsp, "Show last response XML");
00127 _commands.insert(_commands.end(), cmd);
00128 }
00129
00131
00135 const SheppCommand* find_command(string name)
00136 {
00137 CMD_CONTAINER<SheppCommand>::const_iterator it;
00138 for (it = _commands.begin(); it != _commands.end(); it++) {
00139 if ((*it).name == name) {
00140 return &(*it);
00141 }
00142 }
00143
00144 return ((const SheppCommand *) NULL);
00145 }
00146
00148
00152 int cmd_server(char *arg)
00153 {
00154 vector<string> words = SheppStrUtil::parse_line(arg);
00155
00156 if (words.size() != 0) {
00157 string new_port;
00158 string new_server;
00159 SheppStrUtil::split(words[0], new_server, new_port, ":", true);
00160 if (new_server != _server) {
00161 _server = new_server;
00162 }
00163 if (new_port != "" && _port != atoi(new_port.c_str())) {
00164 _port = atoi(new_port.c_str());
00165 }
00166 }
00167
00168 if (_server != "") {
00169 printf("server %s:%d\n", _server.c_str(), _port);
00170 } else {
00171 printf("no server address given.\n");
00172 }
00173
00174 return 0;
00175 }
00176
00178
00182 int cmd_port(char *arg)
00183 {
00184 vector<string> words = SheppStrUtil::parse_line(arg);
00185
00186 if (words.size() == 1) {
00187 if (_port != atoi(words[0].c_str())) {
00188 _port = atoi(words[0].c_str());
00189 }
00190 }
00191
00192 printf("port %d\n", _port);
00193
00194 return 0;
00195 }
00196
00198
00202 int cmd_client_pem(char *arg)
00203 {
00204 vector<string> words = SheppStrUtil::parse_line(arg);
00205
00206 if (words.empty()) {
00207 printf("Current client.pem is %s\n", _client_pem.c_str());
00208 return 0;
00209 } else if (words.size() > 1) {
00210 printf("usage: client-pem <file>\n");
00211 return -1;
00212 }
00213
00214 _client_pem = words[0];
00215
00216 return 0;
00217 }
00218
00220
00224 int cmd_root_pem(char *arg)
00225 {
00226 vector<string> words = SheppStrUtil::parse_line(arg);
00227
00228 if (words.empty()) {
00229 printf("Current root.pem is %s\n", _root_pem.c_str());
00230 return 0;
00231 } else if (words.size() > 1) {
00232 printf("usage: root-pem <file>\n");
00233 return -1;
00234 }
00235
00236 _root_pem = words[0];
00237
00238 return 0;
00239 }
00240
00242
00246 int cmd_pass(char *arg)
00247 {
00248 _passphrase = (string) arg;
00249 return 0;
00250 }
00251
00253
00257 int cmd_user(char *arg)
00258 {
00259 vector<string> words = SheppStrUtil::parse_line(arg);
00260
00261 if (words.size() == 0) {
00262 if (_user != "") {
00263 printf("username: %s\n", _user.c_str());
00264 return 0;
00265 } else {
00266 printf("no username given.\n");
00267 return -1;
00268 }
00269 }
00270
00271 _user = words[0];
00272
00273 return 0;
00274 }
00275
00277
00281 int cmd_login_pw(char *arg)
00282 {
00283 _login_pw = (string) arg;
00284 return 0;
00285 }
00286
00288
00292 int cmd_login_new_pw(char *arg)
00293 {
00294 _login_new_pw = (string) arg;
00295 _new_pw = true;
00296 return 0;
00297 }
00298
00300
00304 int cmd_lang(char *arg)
00305 {
00306 vector<string> words = SheppStrUtil::parse_line(arg);
00307
00308 if (words.empty()) {
00309 printf("usage: lang [en|pt]\n");
00310 return -1;
00311 } else {
00312 _lang = words[0];
00313 }
00314
00315 return 0;
00316 }
00317
00319
00323 int cmd_connect(char *arg)
00324 {
00325 if (cmd_server(arg) != 0) {
00326 return -1;
00327 }
00328
00329 bool exception = false;
00330
00331
00332 try {
00333 _session = auto_ptr<Session>(new Session(_server, _port));
00334 if (_peer_crt_check) {
00335 _session->enable_cert_common_name_check();
00336 } else {
00337 _session->disable_cert_common_name_check();
00338 }
00339 _session->connect(_client_pem, _root_pem, _passphrase);
00340 printf("Connected to %s\n", _server.c_str());
00341
00342 Greeting *greeting = _session->get_greeting();
00343 if (_debug) {
00344 if (greeting) {
00345 printf("Greeting received\n");
00346 }
00347 }
00348
00349 _xmlrsp = _session->get_last_response();
00350 _xmlcmd = _session->get_last_command();
00351
00352 _connected = true;
00353
00354 } catch (const IoException &e) {
00355 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00356 e.get_code(), e.get_msg().c_str());
00357 exception = true;
00358 } catch (const TransportException &e) {
00359 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00360 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00361 exception = true;
00362 } catch (const GeneralException &e) {
00363 printf("ERROR! General Exception [%d]:\n[%s]\n",
00364 e.get_code(), e.get_msg().c_str());
00365 exception = true;
00366 }
00367
00368 if (exception) {
00369 #if 0
00370 printf("error connecting to %s:%d\n", _server.c_str(), _port);
00371 #endif
00372 return -1;
00373 }
00374
00375
00376 cmd_xmlrsp("");
00377
00378 return 0;
00379 }
00380
00382
00386 int cmd_beauty(char *arg)
00387 {
00388 vector<string> words = SheppStrUtil::parse_line(arg);
00389
00390 if (words.empty()) {
00391 if (_beauty) {
00392 printf("XML beautifier is ON\n");
00393 } else {
00394 printf("XML beautifier is OFF\n");
00395 }
00396 return 0;
00397 } else if (words.size() > 1) {
00398 printf("usage: beauty [on|off]\n");
00399 return -1;
00400 }
00401
00402 if (words[0] == "on") {
00403 _beauty = true;
00404 printf("XML beautifier now ON\n");
00405 } else if (words[0] == "off") {
00406 _beauty = false;
00407 printf("XML beautifier now OFF\n");
00408 } else {
00409 printf("usage: beauty [on|off]\n");
00410 return -1;
00411 }
00412
00413 return 0;
00414 }
00415
00417
00421 int cmd_debug(char *arg)
00422 {
00423 vector<string> words = SheppStrUtil::parse_line(arg);
00424
00425 if (words.empty()) {
00426 if (_debug) {
00427 printf("Debug is ON\n");
00428 } else {
00429 printf("Debug is OFF\n");
00430 }
00431 return 0;
00432 } else if (words.size() > 1) {
00433 printf("usage: debug [on|off]\n");
00434 return -1;
00435 }
00436
00437 if (words[0] == "on") {
00438 _debug = true;
00439 printf("Debug now ON\n");
00440 } else if (words[0] == "off") {
00441 _debug = false;
00442 printf("Debug now OFF\n");
00443 } else {
00444 printf("usage: beauty [on|off]\n");
00445 return -1;
00446 }
00447
00448 return 0;
00449 }
00450
00452
00456 int cmd_autorsp(char *arg)
00457 {
00458 vector<string> words = SheppStrUtil::parse_line(arg);
00459
00460 if (words.empty()) {
00461 if (_autorsp) {
00462 printf("autorsp is ON\n");
00463 } else {
00464 printf("autorsp is OFF\n");
00465 }
00466 return 0;
00467 } else if (words.size() > 1) {
00468 printf("usage: autorsp [on|off]\n");
00469 return -1;
00470 }
00471
00472 if (words[0] == "on") {
00473 _autorsp = true;
00474 printf("autorsp now ON\n");
00475 } else if (words[0] == "off") {
00476 _autorsp = false;
00477 printf("autorsp now OFF\n");
00478 } else {
00479 printf("usage: autorsp [on|off]\n");
00480 return -1;
00481 }
00482
00483 return 0;
00484 }
00485
00487
00491 int cmd_keepalive(char *arg)
00492 {
00493 vector<string> words = SheppStrUtil::parse_line(arg);
00494
00495 if (words.empty()) {
00496 if (_keepalive) {
00497 printf("Keep-alive is ON\n");
00498 } else {
00499 printf("Keep-alive is OFF\n");
00500 }
00501 return 0;
00502 } else if (words.size() > 1) {
00503 printf("usage: keepalive [on|off]\n");
00504 return -1;
00505 }
00506
00507 if (words[0] == "on") {
00508 _keepalive = true;
00509 printf("Keep-alive now ON\n");
00510 } else if (words[0] == "off") {
00511 _keepalive = false;
00512 printf("Keep-alive now OFF\n");
00513 } else {
00514 printf("usage: keepalive [on|off]\n");
00515 return -1;
00516 }
00517
00518 if (_keepalive) {
00519 alarm(_keepalive_timer);
00520 } else {
00521 alarm(0);
00522 }
00523
00524 return 0;
00525 }
00526
00528
00532 int cmd_peer_crt_check(char *arg)
00533 {
00534 vector<string> words = SheppStrUtil::parse_line(arg);
00535
00536 if (words.empty()) {
00537 if (_peer_crt_check) {
00538 printf("Peer's certificate check is ON\n");
00539 } else {
00540 printf("Peer's certificate check is OFF\n");
00541 }
00542 return 0;
00543 } else if (words.size() > 1) {
00544 printf("usage: peer-crt-check [on|off]\n");
00545 return -1;
00546 }
00547
00548 if (words[0] == "on") {
00549 _peer_crt_check = true;
00550 printf("Peer's certificate check now ON\n");
00551 } else if (words[0] == "off") {
00552 _peer_crt_check = false;
00553 printf("Peer's certificate check now OFF\n");
00554 } else {
00555 printf("usage: peer-crt-check [on|off]\n");
00556 return -1;
00557 }
00558
00559 return 0;
00560 }
00561
00563 static void sig_alrm(int signo)
00564 {
00565 if (_connected && !_cmd_running) {
00566 _ka_running = true;
00567 cmd_hello("keepalive");
00568 _ka_running = false;
00569 }
00570 alarm(_keepalive_timer);
00571 }
00572
00574
00578 int cmd_xmlcmd(char *arg)
00579 {
00580 if (!_connected) {
00581 printf("not connected\n");
00582 return -1;
00583 }
00584
00585 if (_beauty) {
00586
00587 string last_command("");
00588 StrUtil::iso88591_to_utf8(_xmlcmd, last_command);
00589
00590 if (_session->get_last_command() == "") {
00591 printf("\n");
00592 return 0;
00593 }
00594 try {
00595 StrUtil str_util;
00596 printf("%s\n", str_util.xml_beautifier(last_command).c_str());
00597 } catch (const XmlException &e) {
00598 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00599 e.get_msg().c_str());
00600 }
00601 } else {
00602 printf("%s\n", _session->get_last_command().c_str());
00603 }
00604 return 0;
00605 }
00606
00608
00612 int cmd_xmlrsp(char *arg)
00613 {
00614 if (!_connected) {
00615 printf("not connected\n");
00616 return -1;
00617 }
00618
00619 if (_beauty) {
00620
00621 if (_xmlrsp == "") {
00622 printf("\n");
00623 return 0;
00624 }
00625 try {
00626 StrUtil str_util;
00627 printf("%s\n", str_util.xml_beautifier(_xmlrsp).c_str());
00628 } catch (const XmlException &e) {
00629 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00630 e.get_msg().c_str());
00631 }
00632 } else {
00633 printf("%s\n", _xmlrsp.c_str());
00634 }
00635 return 0;
00636 }
00637
00638
00639
00641 void print_cmd_sent_ok()
00642 {
00643 if (!_autorsp) {
00644 printf("Ok! Use 'xmlcmd' and 'xmlrsp' to view command/response "
00645 "XML code.\n");
00646 } else {
00647 cmd_xmlrsp("");
00648 }
00649 }
00650
00652
00655 int process_action(Action &act)
00656 {
00657 if (!_connected) {
00658 printf("not connected\n");
00659 return -1;
00660 }
00661
00662 bool exception = false;
00663
00664
00665 if (!_ka_running) {
00666 _cmd_running = true;;
00667 } else {
00668 printf("ERROR! Keep-alive is running. Please try again.\n");
00669 return -1;
00670 }
00671
00672 try {
00673 _session->process_action(&act);
00674 _xmlrsp = _session->get_last_response();
00675 _xmlcmd = _session->get_last_command();
00676 } catch (const EppException &e) {
00677 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00678 e.get_msg().c_str());
00679 exception = true;
00680 } catch (const IoException &e) {
00681 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00682 e.get_code(), e.get_msg().c_str());
00683 exception = true;
00684 _connected = false;
00685 } catch (const TransportException &e) {
00686 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00687 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00688 exception = true;
00689 _connected = false;
00690 } catch (const GeneralException &e) {
00691 printf("ERROR! General Exception [%d]:\n[%s]\n",
00692 e.get_code(), e.get_msg().c_str());
00693 exception = true;
00694 }
00695
00696 if (exception) {
00697 _cmd_running = false;
00698 return -1;
00699 }
00700
00701 print_cmd_sent_ok();
00702
00703
00704 if (_keepalive) {
00705 alarm(_keepalive_timer);
00706 }
00707
00708 _cmd_running = false;
00709 return 0;
00710 }
00711
00713
00717 int cmd_login(char *arg)
00718 {
00719 if (!_connected) {
00720 printf("not connected\n");
00721 return -1;
00722 }
00723
00724 if (_user == "") {
00725 printf("no username given.\n");
00726 return -1;
00727 }
00728
00729 Login act;
00730 LoginCmd *cmd = act.get_command();
00731
00732 cmd->set_clID(_user);
00733 cmd->set_pw(_login_pw);
00734
00735 if (_new_pw) {
00736 _new_pw = false;
00737 cmd->set_new_pw(_login_new_pw);
00738 }
00739
00740 if (_lang != "" && _lang != "en") {
00741 cmd->set_lang(_lang);
00742 }
00743
00744 if (process_action(act) != 0) {
00745 return -1;
00746 }
00747
00748 return 0;
00749 }
00750
00752
00756 int cmd_logout(char *arg)
00757 {
00758 if (!_connected) {
00759 printf("not connected\n");
00760 return -1;
00761 }
00762
00763 Logout act;
00764
00765 if (process_action(act) != 0) {
00766 return -1;
00767 }
00768
00769 return 0;
00770 }
00771
00773
00777 int cmd_hello(char *arg)
00778 {
00779 if (!_connected) {
00780 printf("not connected\n");
00781 return -1;
00782 }
00783
00784 vector<string> words = SheppStrUtil::parse_line(arg);
00785
00786 if (_ka_running &&
00787 !(words.size() == 1 && words[0] == "keepalive")) {
00788 printf("ERROR! Keep-alive is running. Please try again.\n");
00789 return -1;
00790 }
00791
00792 bool exception = false;
00793
00794
00795 if (!_ka_running) {
00796 _cmd_running = true;
00797 }
00798
00799 try {
00800 if (_debug) {
00801 printf("Sending EPP Hello\n");
00802 }
00803 _session->send_hello();
00804
00805 if (!_ka_running) {
00806 _xmlrsp = _session->get_last_response();
00807 _xmlcmd = _session->get_last_command();
00808 }
00809 } catch (const EppException &e) {
00810 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00811 e.get_msg().c_str());
00812 exception = true;
00813 } catch (const IoException &e) {
00814 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00815 e.get_code(), e.get_msg().c_str());
00816 exception = true;
00817 _connected = false;
00818 } catch (const TransportException &e) {
00819 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00820 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00821 exception = true;
00822 _connected = false;
00823 } catch (const GeneralException &e) {
00824 printf("ERROR! General Exception [%d]:\n[%s]\n",
00825 e.get_code(), e.get_msg().c_str());
00826 exception = true;
00827 }
00828
00829 if (exception) {
00830 if (_cmd_running) {
00831 _cmd_running = false;
00832 }
00833 return -1;
00834 }
00835
00836 if (!_ka_running) {
00837 print_cmd_sent_ok();
00838 }
00839
00840 if (_cmd_running) {
00841
00842 if (_keepalive) {
00843 alarm(_keepalive_timer);
00844 }
00845 _cmd_running = false;
00846 }
00847 return 0;
00848 }
00849
00850
00851
00853
00857 int cmd_quit(char *arg)
00858 {
00859 if (_connected) {
00860 _session->disconnect();
00861 }
00862 printf("Bye\n");
00863 exit(0);
00864 }
00865
00867
00871 int cmd_help(char *arg)
00872 {
00873 CMD_CONTAINER<SheppCommand>::const_iterator it;
00874 for (it = _commands.begin(); it != _commands.end(); it++) {
00875 printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
00876 }
00877
00878 return 0;
00879 }
00880
00882
00886 int cmd_about(char *arg)
00887 {
00888 printf("shepp version " SHEPP_VERSION ", an EPP client shell!\n");
00889 printf("Copyright 2006 Registro.br <libepp@registro.br>\n");
00890 printf("shepp is distributed with libepp-nicbr: "
00891 "http://registro.br/epp/index-EN.html\n");
00892 #if USE_BR_DOMAINS
00893 printf("Built with BrDomain EPP extension support.\n");
00894 #endif //USE_BR_DOMAINS
00895
00896 return 0;
00897 }
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958 #endif //__SHEPP_COMMAND_FUNCTIONS_H__