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("");
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 return -1;
00625 }
00626
00627 try {
00628 _session->process_action(&act);
00629 _xmlrsp = _session->get_last_response();
00630 _xmlcmd = _session->get_last_command();
00631 } catch (const EppException &e) {
00632 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00633 e.get_msg().c_str());
00634 exception = true;
00635 } catch (const IoException &e) {
00636 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00637 e.get_code(), e.get_msg().c_str());
00638 exception = true;
00639 _connected = false;
00640 } catch (const TransportException &e) {
00641 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00642 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00643 exception = true;
00644 _connected = false;
00645 } catch (const GeneralException &e) {
00646 printf("ERROR! General Exception [%d]:\n[%s]\n",
00647 e.get_code(), e.get_msg().c_str());
00648 exception = true;
00649 }
00650
00651 if (exception) {
00652 _cmd_running = false;
00653 return -1;
00654 }
00655
00656 print_cmd_sent_ok();
00657 _cmd_running = false;
00658 return 0;
00659 }
00660
00662
00666 int cmd_login(char *arg)
00667 {
00668 if (!_connected) {
00669 printf("not connected\n");
00670 return -1;
00671 }
00672
00673 if (_user == "") {
00674 printf("no username given.\n");
00675 return -1;
00676 }
00677
00678 Login act;
00679 LoginCmd *cmd = act.get_command();
00680
00681 cmd->set_clID(_user);
00682 cmd->set_pw(_login_pw);
00683
00684 if (_new_pw) {
00685 _new_pw = false;
00686 cmd->set_new_pw(_login_new_pw);
00687 }
00688
00689 if (_lang != "" && _lang != "en") {
00690 cmd->set_lang(_lang);
00691 }
00692
00693 if (process_action(act) != 0) {
00694 return -1;
00695 }
00696
00697 return 0;
00698 }
00699
00701
00705 int cmd_logout(char *arg)
00706 {
00707 if (!_connected) {
00708 printf("not connected\n");
00709 return -1;
00710 }
00711
00712 Logout act;
00713
00714 if (process_action(act) != 0) {
00715 return -1;
00716 }
00717
00718 return 0;
00719 }
00720
00722
00726 int cmd_hello(char *arg)
00727 {
00728 if (!_connected) {
00729 printf("not connected\n");
00730 return -1;
00731 }
00732
00733 bool exception = false;
00734
00735
00736 if (!_ka_running) {
00737 _cmd_running = true;
00738 }
00739
00740 try {
00741 if (_debug) {
00742 printf("Sending EPP Hello\n");
00743 }
00744 _session->send_hello();
00745
00746 if (!_ka_running) {
00747 _xmlrsp = _session->get_last_response();
00748 _xmlcmd = _session->get_last_command();
00749 }
00750 } catch (const EppException &e) {
00751 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00752 e.get_msg().c_str());
00753 exception = true;
00754 } catch (const IoException &e) {
00755 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00756 e.get_code(), e.get_msg().c_str());
00757 exception = true;
00758 _connected = false;
00759 } catch (const TransportException &e) {
00760 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00761 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00762 exception = true;
00763 _connected = false;
00764 } catch (const GeneralException &e) {
00765 printf("ERROR! General Exception [%d]:\n[%s]\n",
00766 e.get_code(), e.get_msg().c_str());
00767 exception = true;
00768 }
00769
00770 if (exception) {
00771 if (_cmd_running) {
00772 _cmd_running = false;
00773 }
00774 return -1;
00775 }
00776
00777 if (!_ka_running) {
00778 print_cmd_sent_ok();
00779 }
00780
00781 if (_cmd_running) {
00782 _cmd_running = false;
00783 }
00784 return 0;
00785 }
00786
00787
00788
00790
00794 int cmd_quit(char *arg)
00795 {
00796 printf("Bye\n");
00797 exit(0);
00798 }
00799
00801
00805 int cmd_help(char *arg)
00806 {
00807 CMD_CONTAINER<SheppCommand>::const_iterator it;
00808 for (it = _commands.begin(); it != _commands.end(); it++) {
00809 printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
00810 }
00811
00812 return 0;
00813 }
00814
00816
00820 int cmd_about(char *arg)
00821 {
00822 printf("shepp version " SHEPP_VERSION ", an EPP client shell!\n");
00823 printf("Copyright 2006 Registro.br <libepp@registro.br>\n");
00824 printf("shepp is distributed with libepp-nicbr: "
00825 "http://registro.br/epp/index-EN.html\n");
00826 #if USE_BR_DOMAINS
00827 printf("Built with BrDomain EPP extension support.\n");
00828 #endif //USE_BR_DOMAINS
00829
00830 return 0;
00831 }
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
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 #endif //__SHEPP_COMMAND_FUNCTIONS_H__