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
00055 void init_commands()
00056 {
00057 SheppCommand cmd;
00058
00059
00060 cmd.reset("?", cmd_help, "This help screen");
00061 _commands.insert(_commands.end(), cmd);
00062 cmd.reset("help", cmd_help, "This help screen");
00063 _commands.insert(_commands.end(), cmd);
00064 cmd.reset("about", cmd_about, "shepp version information");
00065 _commands.insert(_commands.end(), cmd);
00066 cmd.reset("exit", cmd_quit, "Exit shepp\n");
00067 _commands.insert(_commands.end(), cmd);
00068
00069
00070 cmd.reset("beauty", cmd_beauty, "Turn XML beautifier on/off");
00071 _commands.insert(_commands.end(), cmd);
00072 cmd.reset("debug", cmd_debug, "Turn debug messages on/off");
00073 _commands.insert(_commands.end(), cmd);
00074 cmd.reset("autorsp", cmd_autorsp, "Turn auto-show response on/off");
00075 _commands.insert(_commands.end(), cmd);
00076 cmd.reset("keepalive", cmd_keepalive, "Turn keep-alive on/off\n");
00077 _commands.insert(_commands.end(), cmd);
00078
00079
00080 cmd.reset("server", cmd_server, "Sets server address");
00081 _commands.insert(_commands.end(), cmd);
00082 cmd.reset("port", cmd_port, "Sets server port");
00083 _commands.insert(_commands.end(), cmd);
00084 cmd.reset("client-pem", cmd_client_pem,
00085 "Sets client.pem certificate file location");
00086 _commands.insert(_commands.end(), cmd);
00087 cmd.reset("root-pem", cmd_root_pem,
00088 "Sets root.pem certificate file location");
00089 _commands.insert(_commands.end(), cmd);
00090 cmd.reset("pass", cmd_pass, "Sets SSL certificate passphrase");
00091 _commands.insert(_commands.end(), cmd);
00092 cmd.reset("user", cmd_user, "Sets EPP Login username");
00093 _commands.insert(_commands.end(), cmd);
00094 cmd.reset("pw", cmd_login_pw, "Sets EPP Login password");
00095 _commands.insert(_commands.end(), cmd);
00096 cmd.reset("newpw", cmd_login_new_pw, "Sets new EPP Login password");
00097 _commands.insert(_commands.end(), cmd);
00098 cmd.reset("lang", cmd_lang, "Sets text language");
00099 _commands.insert(_commands.end(), cmd);
00100 cmd.reset("connect", cmd_connect, "Establishes connection\n");
00101 _commands.insert(_commands.end(), cmd);
00102
00103
00104 cmd.reset("login", cmd_login, "EPP Login");
00105 _commands.insert(_commands.end(), cmd);
00106 cmd.reset("logout", cmd_logout, "EPP Logout");
00107 _commands.insert(_commands.end(), cmd);
00108 cmd.reset("hello", cmd_hello, "EPP Hello");
00109 _commands.insert(_commands.end(), cmd);
00110 cmd.reset("poll", cmd_poll, "EPP Poll related actions");
00111 _commands.insert(_commands.end(), cmd);
00112 cmd.reset("domain", cmd_domain, "EPP Domain related actions");
00113 _commands.insert(_commands.end(), cmd);
00114 cmd.reset("contact", cmd_contact, "EPP Contact related actions");
00115 _commands.insert(_commands.end(), cmd);
00116 cmd.reset("brorg", cmd_brorg, "EPP BrOrg related actions\n");
00117 _commands.insert(_commands.end(), cmd);
00118
00119
00120 cmd.reset("xmlcmd", cmd_xmlcmd, "Show last command XML");
00121 _commands.insert(_commands.end(), cmd);
00122 cmd.reset("xmlrsp", cmd_xmlrsp, "Show last response XML");
00123 _commands.insert(_commands.end(), cmd);
00124 }
00125
00127
00131 const SheppCommand* find_command(string name)
00132 {
00133 CMD_CONTAINER<SheppCommand>::const_iterator it;
00134 for (it = _commands.begin(); it != _commands.end(); it++) {
00135 if ((*it).name == name) {
00136 return &(*it);
00137 }
00138 }
00139
00140 return ((const SheppCommand *) NULL);
00141 }
00142
00144
00148 int cmd_server(char *arg)
00149 {
00150 vector<string> words = SheppStrUtil::parse_line(arg);
00151
00152 if (words.size() != 0) {
00153 string new_port;
00154 string new_server;
00155 SheppStrUtil::split(words[0], new_server, new_port, ":", true);
00156 if (new_server != _server) {
00157 _server = new_server;
00158 }
00159 if (new_port != "" && _port != atoi(new_port.c_str())) {
00160 _port = atoi(new_port.c_str());
00161 }
00162 }
00163
00164 if (_server != "") {
00165 printf("server %s:%d\n", _server.c_str(), _port);
00166 } else {
00167 printf("no server address given.\n");
00168 }
00169
00170 return 0;
00171 }
00172
00174
00178 int cmd_port(char *arg)
00179 {
00180 vector<string> words = SheppStrUtil::parse_line(arg);
00181
00182 if (words.size() == 1) {
00183 if (_port != atoi(words[0].c_str())) {
00184 _port = atoi(words[0].c_str());
00185 }
00186 }
00187
00188 printf("port %d\n", _port);
00189
00190 return 0;
00191 }
00192
00194
00198 int cmd_client_pem(char *arg)
00199 {
00200 vector<string> words = SheppStrUtil::parse_line(arg);
00201
00202 if (words.empty()) {
00203 printf("Current client.pem is %s\n", _client_pem.c_str());
00204 return 0;
00205 } else if (words.size() > 1) {
00206 printf("usage: client-pem <file>\n");
00207 return -1;
00208 }
00209
00210 _client_pem = words[0];
00211
00212 return 0;
00213 }
00214
00216
00220 int cmd_root_pem(char *arg)
00221 {
00222 vector<string> words = SheppStrUtil::parse_line(arg);
00223
00224 if (words.empty()) {
00225 printf("Current root.pem is %s\n", _root_pem.c_str());
00226 return 0;
00227 } else if (words.size() > 1) {
00228 printf("usage: root-pem <file>\n");
00229 return -1;
00230 }
00231
00232 _root_pem = words[0];
00233
00234 return 0;
00235 }
00236
00238
00242 int cmd_pass(char *arg)
00243 {
00244 _passphrase = (string) arg;
00245 return 0;
00246 }
00247
00249
00253 int cmd_user(char *arg)
00254 {
00255 vector<string> words = SheppStrUtil::parse_line(arg);
00256
00257 if (words.size() == 0) {
00258 if (_user != "") {
00259 printf("username: %s\n", _user.c_str());
00260 return 0;
00261 } else {
00262 printf("no username given.\n");
00263 return -1;
00264 }
00265 }
00266
00267 _user = words[0];
00268
00269 return 0;
00270 }
00271
00273
00277 int cmd_login_pw(char *arg)
00278 {
00279 _login_pw = (string) arg;
00280 return 0;
00281 }
00282
00284
00288 int cmd_login_new_pw(char *arg)
00289 {
00290 _login_new_pw = (string) arg;
00291 _new_pw = true;
00292 return 0;
00293 }
00294
00296
00300 int cmd_lang(char *arg)
00301 {
00302 vector<string> words = SheppStrUtil::parse_line(arg);
00303
00304 if (words.empty()) {
00305 printf("usage: lang [en|pt]\n");
00306 return -1;
00307 } else {
00308 _lang = words[0];
00309 }
00310
00311 return 0;
00312 }
00313
00315
00319 int cmd_connect(char *arg)
00320 {
00321 if (cmd_server(arg) != 0) {
00322 return -1;
00323 }
00324
00325 bool exception = false;
00326
00327
00328 try {
00329 _session = auto_ptr<Session>(new Session(_server, _port));
00330 _session->connect(_client_pem, _root_pem, _passphrase);
00331 printf("Connected to %s\n", _server.c_str());
00332
00333 Greeting *greeting = _session->get_greeting();
00334 if (_debug) {
00335 if (greeting) {
00336 printf("Greeting received\n");
00337 }
00338 }
00339
00340 _xmlrsp = _session->get_last_response();
00341 _xmlcmd = _session->get_last_command();
00342
00343 _connected = true;
00344
00345 } catch (const IoException &e) {
00346 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00347 e.get_code(), e.get_msg().c_str());
00348 exception = true;
00349 } catch (const TransportException &e) {
00350 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00351 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00352 exception = true;
00353 } catch (const GeneralException &e) {
00354 printf("ERROR! General Exception [%d]:\n[%s]\n",
00355 e.get_code(), e.get_msg().c_str());
00356 exception = true;
00357 }
00358
00359 if (exception) {
00360 #if 0
00361 printf("error connecting to %s:%d\n", _server.c_str(), _port);
00362 #endif
00363 return -1;
00364 }
00365
00366
00367 cmd_xmlrsp("");
00368
00369 return 0;
00370 }
00371
00373
00377 int cmd_beauty(char *arg)
00378 {
00379 vector<string> words = SheppStrUtil::parse_line(arg);
00380
00381 if (words.empty()) {
00382 if (_beauty) {
00383 printf("XML beautifier is ON\n");
00384 } else {
00385 printf("XML beautifier is OFF\n");
00386 }
00387 return 0;
00388 } else if (words.size() > 1) {
00389 printf("usage: beauty [on|off]\n");
00390 return -1;
00391 }
00392
00393 if (words[0] == "on") {
00394 _beauty = true;
00395 printf("XML beautifier now ON\n");
00396 } else if (words[0] == "off") {
00397 _beauty = false;
00398 printf("XML beautifier now OFF\n");
00399 } else {
00400 printf("usage: beauty [on|off]\n");
00401 return -1;
00402 }
00403
00404 return 0;
00405 }
00406
00408
00412 int cmd_debug(char *arg)
00413 {
00414 vector<string> words = SheppStrUtil::parse_line(arg);
00415
00416 if (words.empty()) {
00417 if (_debug) {
00418 printf("Debug is ON\n");
00419 } else {
00420 printf("Debug is OFF\n");
00421 }
00422 return 0;
00423 } else if (words.size() > 1) {
00424 printf("usage: debug [on|off]\n");
00425 return -1;
00426 }
00427
00428 if (words[0] == "on") {
00429 _debug = true;
00430 printf("Debug now ON\n");
00431 } else if (words[0] == "off") {
00432 _debug = false;
00433 printf("Debug now OFF\n");
00434 } else {
00435 printf("usage: beauty [on|off]\n");
00436 return -1;
00437 }
00438
00439 return 0;
00440 }
00441
00443
00447 int cmd_autorsp(char *arg)
00448 {
00449 vector<string> words = SheppStrUtil::parse_line(arg);
00450
00451 if (words.empty()) {
00452 if (_autorsp) {
00453 printf("autorsp is ON\n");
00454 } else {
00455 printf("autorsp is OFF\n");
00456 }
00457 return 0;
00458 } else if (words.size() > 1) {
00459 printf("usage: autorsp [on|off]\n");
00460 return -1;
00461 }
00462
00463 if (words[0] == "on") {
00464 _autorsp = true;
00465 printf("autorsp now ON\n");
00466 } else if (words[0] == "off") {
00467 _autorsp = false;
00468 printf("autorsp now OFF\n");
00469 } else {
00470 printf("usage: autorsp [on|off]\n");
00471 return -1;
00472 }
00473
00474 return 0;
00475 }
00476
00478
00482 int cmd_keepalive(char *arg)
00483 {
00484 vector<string> words = SheppStrUtil::parse_line(arg);
00485
00486 if (words.empty()) {
00487 if (_keepalive) {
00488 printf("Keep-alive is ON\n");
00489 } else {
00490 printf("Keep-alive is OFF\n");
00491 }
00492 return 0;
00493 } else if (words.size() > 1) {
00494 printf("usage: keepalive [on|off]\n");
00495 return -1;
00496 }
00497
00498 if (words[0] == "on") {
00499 _keepalive = true;
00500 printf("Keep-alive now ON\n");
00501 } else if (words[0] == "off") {
00502 _keepalive = false;
00503 printf("Keep-alive now OFF\n");
00504 } else {
00505 printf("usage: keepalive [on|off]\n");
00506 return -1;
00507 }
00508
00509 if (_keepalive) {
00510 alarm(_keepalive_timer);
00511 } else {
00512 alarm(0);
00513 }
00514
00515 return 0;
00516 }
00517
00519 static void sig_alrm(int signo)
00520 {
00521 if (_connected && !_cmd_running) {
00522 _ka_running = true;
00523 cmd_hello("keepalive");
00524 _ka_running = false;
00525 }
00526 alarm(_keepalive_timer);
00527 }
00528
00530
00534 int cmd_xmlcmd(char *arg)
00535 {
00536 if (!_connected) {
00537 printf("not connected\n");
00538 return -1;
00539 }
00540
00541 if (_beauty) {
00542
00543 string last_command("");
00544 StrUtil::iso88591_to_utf8(_xmlcmd, last_command);
00545
00546 if (_session->get_last_command() == "") {
00547 printf("\n");
00548 return 0;
00549 }
00550 try {
00551 StrUtil str_util;
00552 printf("%s\n", str_util.xml_beautifier(last_command).c_str());
00553 } catch (const XmlException &e) {
00554 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00555 e.get_msg().c_str());
00556 }
00557 } else {
00558 printf("%s\n", _session->get_last_command().c_str());
00559 }
00560 return 0;
00561 }
00562
00564
00568 int cmd_xmlrsp(char *arg)
00569 {
00570 if (!_connected) {
00571 printf("not connected\n");
00572 return -1;
00573 }
00574
00575 if (_beauty) {
00576
00577 if (_xmlrsp == "") {
00578 printf("\n");
00579 return 0;
00580 }
00581 try {
00582 StrUtil str_util;
00583 printf("%s\n", str_util.xml_beautifier(_xmlrsp).c_str());
00584 } catch (const XmlException &e) {
00585 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00586 e.get_msg().c_str());
00587 }
00588 } else {
00589 printf("%s\n", _xmlrsp.c_str());
00590 }
00591 return 0;
00592 }
00593
00594
00595
00597 void print_cmd_sent_ok()
00598 {
00599 if (!_autorsp) {
00600 printf("Ok! Use 'xmlcmd' and 'xmlrsp' to view command/response "
00601 "XML code.\n");
00602 } else {
00603 cmd_xmlrsp("");
00604 }
00605 }
00606
00608
00611 int process_action(Action &act)
00612 {
00613 if (!_connected) {
00614 printf("not connected\n");
00615 return -1;
00616 }
00617
00618 bool exception = false;
00619
00620
00621 if (!_ka_running) {
00622 _cmd_running = true;;
00623 } else {
00624 printf("ERROR! Keep-alive is running. Please try again.\n");
00625 return -1;
00626 }
00627
00628 try {
00629 _session->process_action(&act);
00630 _xmlrsp = _session->get_last_response();
00631 _xmlcmd = _session->get_last_command();
00632 } catch (const EppException &e) {
00633 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00634 e.get_msg().c_str());
00635 exception = true;
00636 } catch (const IoException &e) {
00637 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00638 e.get_code(), e.get_msg().c_str());
00639 exception = true;
00640 _connected = false;
00641 } catch (const TransportException &e) {
00642 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00643 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00644 exception = true;
00645 _connected = false;
00646 } catch (const GeneralException &e) {
00647 printf("ERROR! General Exception [%d]:\n[%s]\n",
00648 e.get_code(), e.get_msg().c_str());
00649 exception = true;
00650 }
00651
00652 if (exception) {
00653 _cmd_running = false;
00654 return -1;
00655 }
00656
00657 print_cmd_sent_ok();
00658
00659
00660 if (_keepalive) {
00661 alarm(_keepalive_timer);
00662 }
00663
00664 _cmd_running = false;
00665 return 0;
00666 }
00667
00669
00673 int cmd_login(char *arg)
00674 {
00675 if (!_connected) {
00676 printf("not connected\n");
00677 return -1;
00678 }
00679
00680 if (_user == "") {
00681 printf("no username given.\n");
00682 return -1;
00683 }
00684
00685 Login act;
00686 LoginCmd *cmd = act.get_command();
00687
00688 cmd->set_clID(_user);
00689 cmd->set_pw(_login_pw);
00690
00691 if (_new_pw) {
00692 _new_pw = false;
00693 cmd->set_new_pw(_login_new_pw);
00694 }
00695
00696 if (_lang != "" && _lang != "en") {
00697 cmd->set_lang(_lang);
00698 }
00699
00700 if (process_action(act) != 0) {
00701 return -1;
00702 }
00703
00704 return 0;
00705 }
00706
00708
00712 int cmd_logout(char *arg)
00713 {
00714 if (!_connected) {
00715 printf("not connected\n");
00716 return -1;
00717 }
00718
00719 Logout act;
00720
00721 if (process_action(act) != 0) {
00722 return -1;
00723 }
00724
00725 return 0;
00726 }
00727
00729
00733 int cmd_hello(char *arg)
00734 {
00735 if (!_connected) {
00736 printf("not connected\n");
00737 return -1;
00738 }
00739
00740 vector<string> words = SheppStrUtil::parse_line(arg);
00741
00742 if (_ka_running &&
00743 !(words.size() == 1 && words[0] == "keepalive")) {
00744 printf("ERROR! Keep-alive is running. Please try again.\n");
00745 return -1;
00746 }
00747
00748 bool exception = false;
00749
00750
00751 if (!_ka_running) {
00752 _cmd_running = true;
00753 }
00754
00755 try {
00756 if (_debug) {
00757 printf("Sending EPP Hello\n");
00758 }
00759 _session->send_hello();
00760
00761 if (!_ka_running) {
00762 _xmlrsp = _session->get_last_response();
00763 _xmlcmd = _session->get_last_command();
00764 }
00765 } catch (const EppException &e) {
00766 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00767 e.get_msg().c_str());
00768 exception = true;
00769 } catch (const IoException &e) {
00770 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00771 e.get_code(), e.get_msg().c_str());
00772 exception = true;
00773 _connected = false;
00774 } catch (const TransportException &e) {
00775 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00776 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00777 exception = true;
00778 _connected = false;
00779 } catch (const GeneralException &e) {
00780 printf("ERROR! General Exception [%d]:\n[%s]\n",
00781 e.get_code(), e.get_msg().c_str());
00782 exception = true;
00783 }
00784
00785 if (exception) {
00786 if (_cmd_running) {
00787 _cmd_running = false;
00788 }
00789 return -1;
00790 }
00791
00792 if (!_ka_running) {
00793 print_cmd_sent_ok();
00794 }
00795
00796 if (_cmd_running) {
00797
00798 if (_keepalive) {
00799 alarm(_keepalive_timer);
00800 }
00801 _cmd_running = false;
00802 }
00803 return 0;
00804 }
00805
00806
00807
00809
00813 int cmd_quit(char *arg)
00814 {
00815 if (_connected) {
00816 _session->disconnect();
00817 }
00818 printf("Bye\n");
00819 exit(0);
00820 }
00821
00823
00827 int cmd_help(char *arg)
00828 {
00829 CMD_CONTAINER<SheppCommand>::const_iterator it;
00830 for (it = _commands.begin(); it != _commands.end(); it++) {
00831 printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
00832 }
00833
00834 return 0;
00835 }
00836
00838
00842 int cmd_about(char *arg)
00843 {
00844 printf("shepp version " SHEPP_VERSION ", an EPP client shell!\n");
00845 printf("Copyright 2006 Registro.br <libepp@registro.br>\n");
00846 printf("shepp is distributed with libepp-nicbr: "
00847 "http://registro.br/epp/index-EN.html\n");
00848 #if USE_BR_DOMAINS
00849 printf("Built with BrDomain EPP extension support.\n");
00850 #endif //USE_BR_DOMAINS
00851
00852 return 0;
00853 }
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914 #endif //__SHEPP_COMMAND_FUNCTIONS_H__