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_connect __P((char *));
00043
00044 int cmd_help __P((char *));
00045 int cmd_about __P((char *));
00046 int cmd_quit __P((char *));
00047
00048 int cmd_beauty __P((char *));
00049 int cmd_debug __P((char *));
00050
00052 void init_commands()
00053 {
00054 SheppCommand cmd;
00055
00056
00057 cmd.reset("?", cmd_help, "This help screen");
00058 _commands.insert(_commands.end(), cmd);
00059 cmd.reset("help", cmd_help, "This help screen");
00060 _commands.insert(_commands.end(), cmd);
00061 cmd.reset("about", cmd_about, "shepp version information");
00062 _commands.insert(_commands.end(), cmd);
00063 cmd.reset("exit", cmd_quit, "Exit shepp\n");
00064 _commands.insert(_commands.end(), cmd);
00065
00066
00067 cmd.reset("beauty", cmd_beauty, "Turn XML beautifier on/off");
00068 _commands.insert(_commands.end(), cmd);
00069 cmd.reset("debug", cmd_debug, "Turn debug messages on/off\n");
00070 _commands.insert(_commands.end(), cmd);
00071
00072
00073 cmd.reset("server", cmd_server, "Sets server address");
00074 _commands.insert(_commands.end(), cmd);
00075 cmd.reset("port", cmd_port, "Sets server port");
00076 _commands.insert(_commands.end(), cmd);
00077 cmd.reset("client-pem", cmd_client_pem,
00078 "Sets client.pem certificate file location");
00079 _commands.insert(_commands.end(), cmd);
00080 cmd.reset("root-pem", cmd_root_pem,
00081 "Sets root.pem certificate file location");
00082 _commands.insert(_commands.end(), cmd);
00083 cmd.reset("pass", cmd_pass, "Sets SSL certificate passphrase");
00084 _commands.insert(_commands.end(), cmd);
00085 cmd.reset("user", cmd_user, "Sets EPP Login username");
00086 _commands.insert(_commands.end(), cmd);
00087 cmd.reset("pw", cmd_login_pw, "Sets EPP Login password");
00088 _commands.insert(_commands.end(), cmd);
00089 cmd.reset("newpw", cmd_login_new_pw, "Sets new EPP Login password");
00090 _commands.insert(_commands.end(), cmd);
00091 cmd.reset("connect", cmd_connect, "Establishes connection\n");
00092 _commands.insert(_commands.end(), cmd);
00093
00094
00095 cmd.reset("login", cmd_login, "EPP Login");
00096 _commands.insert(_commands.end(), cmd);
00097 cmd.reset("logout", cmd_logout, "EPP Logout");
00098 _commands.insert(_commands.end(), cmd);
00099 cmd.reset("hello", cmd_hello, "EPP Hello");
00100 _commands.insert(_commands.end(), cmd);
00101 cmd.reset("poll", cmd_poll, "EPP Poll related actions");
00102 _commands.insert(_commands.end(), cmd);
00103 cmd.reset("domain", cmd_domain, "EPP Domain related actions");
00104 _commands.insert(_commands.end(), cmd);
00105 cmd.reset("contact", cmd_contact, "EPP Contact related actions");
00106 _commands.insert(_commands.end(), cmd);
00107 cmd.reset("brorg", cmd_brorg, "EPP BrOrg related actions\n");
00108 _commands.insert(_commands.end(), cmd);
00109
00110
00111 cmd.reset("xmlcmd", cmd_xmlcmd, "Show last command XML");
00112 _commands.insert(_commands.end(), cmd);
00113 cmd.reset("xmlrsp", cmd_xmlrsp, "Show last response XML");
00114 _commands.insert(_commands.end(), cmd);
00115 }
00116
00118
00122 const SheppCommand* find_command(string name)
00123 {
00124 CMD_CONTAINER<SheppCommand>::const_iterator it;
00125 for (it = _commands.begin(); it != _commands.end(); it++) {
00126 if ((*it).name == name) {
00127 return &(*it);
00128 }
00129 }
00130
00131 return ((const SheppCommand *) NULL);
00132 }
00133
00135
00139 int cmd_server(char *arg)
00140 {
00141 vector<string> words = SheppStrUtil::parse_line(arg);
00142
00143 if (words.size() != 0) {
00144 string new_port;
00145 string new_server;
00146 SheppStrUtil::split(words[0], new_server, new_port, ":", true);
00147 if (new_server != _server) {
00148 _server = new_server;
00149 }
00150 if (new_port != "" && _port != atoi(new_port.c_str())) {
00151 _port = atoi(new_port.c_str());
00152 }
00153 }
00154
00155 if (_server != "") {
00156 printf("server %s:%d\n", _server.c_str(), _port);
00157 } else {
00158 printf("no server address given.\n");
00159 }
00160
00161 return 0;
00162 }
00163
00165
00169 int cmd_port(char *arg)
00170 {
00171 vector<string> words = SheppStrUtil::parse_line(arg);
00172
00173 if (words.size() == 1) {
00174 if (_port != atoi(words[0].c_str())) {
00175 _port = atoi(words[0].c_str());
00176 }
00177 }
00178
00179 printf("port %d\n", _port);
00180
00181 return 0;
00182 }
00183
00185
00189 int cmd_client_pem(char *arg)
00190 {
00191 vector<string> words = SheppStrUtil::parse_line(arg);
00192
00193 if (words.empty()) {
00194 printf("Current client.pem is %s\n", _client_pem.c_str());
00195 return 0;
00196 } else if (words.size() > 1) {
00197 printf("usage: client-pem <file>\n");
00198 return -1;
00199 }
00200
00201 _client_pem = words[0];
00202
00203 return 0;
00204 }
00205
00207
00211 int cmd_root_pem(char *arg)
00212 {
00213 vector<string> words = SheppStrUtil::parse_line(arg);
00214
00215 if (words.empty()) {
00216 printf("Current root.pem is %s\n", _root_pem.c_str());
00217 return 0;
00218 } else if (words.size() > 1) {
00219 printf("usage: root-pem <file>\n");
00220 return -1;
00221 }
00222
00223 _root_pem = words[0];
00224
00225 return 0;
00226 }
00227
00229
00233 int cmd_pass(char *arg)
00234 {
00235 _passphrase = (string) arg;
00236 return 0;
00237 }
00238
00240
00244 int cmd_user(char *arg)
00245 {
00246 vector<string> words = SheppStrUtil::parse_line(arg);
00247
00248 if (words.size() == 0) {
00249 if (_user != "") {
00250 printf("username: %s\n", _user.c_str());
00251 return 0;
00252 } else {
00253 printf("no username given.\n");
00254 return -1;
00255 }
00256 }
00257
00258 _user = words[0];
00259
00260 return 0;
00261 }
00262
00264
00268 int cmd_login_pw(char *arg)
00269 {
00270 _login_pw = (string) arg;
00271 return 0;
00272 }
00273
00275
00279 int cmd_login_new_pw(char *arg)
00280 {
00281 _login_new_pw = (string) arg;
00282 _new_pw = true;
00283 return 0;
00284 }
00285
00287
00291 int cmd_connect(char *arg)
00292 {
00293 if (cmd_server(arg) != 0) {
00294 return -1;
00295 }
00296
00297 bool exception = false;
00298
00299
00300 try {
00301 _session = auto_ptr<Session>(new Session(_server, _port));
00302 _session->connect(_client_pem, _root_pem, _passphrase);
00303 printf("Connected to %s\n", _server.c_str());
00304
00305 Greeting *greeting = _session->get_greeting();
00306 if (_debug) {
00307 if (greeting) {
00308 printf("Greeting received\n");
00309 }
00310 }
00311
00312 _connected = true;
00313
00314 } catch (const IoException &e) {
00315 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00316 e.get_code(), e.get_msg().c_str());
00317 exception = true;
00318 } catch (const TransportException &e) {
00319 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00320 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00321 exception = true;
00322 } catch (const GeneralException &e) {
00323 printf("ERROR! General Exception [%d]:\n[%s]\n",
00324 e.get_code(), e.get_msg().c_str());
00325 exception = true;
00326 }
00327
00328 if (exception) {
00329 #if 0
00330 printf("error connecting to %s:%d\n", _server.c_str(), _port);
00331 #endif
00332 return -1;
00333 }
00334
00335 return 0;
00336 }
00337
00339
00343 int cmd_beauty(char *arg)
00344 {
00345 vector<string> words = SheppStrUtil::parse_line(arg);
00346
00347 if (words.empty()) {
00348 if (_beauty) {
00349 printf("XML beautifier is ON\n");
00350 } else {
00351 printf("XML beautifier is OFF\n");
00352 }
00353 return 0;
00354 } else if (words.size() > 1) {
00355 printf("usage: beauty [on|off]\n");
00356 return -1;
00357 }
00358
00359 if (words[0] == "on") {
00360 _beauty = true;
00361 printf("XML beautifier now ON\n");
00362 } else if (words[0] == "off") {
00363 _beauty = false;
00364 printf("XML beautifier now OFF\n");
00365 } else {
00366 printf("usage: beauty [on|off]\n");
00367 return -1;
00368 }
00369
00370 return 0;
00371 }
00372
00374
00378 int cmd_debug(char *arg)
00379 {
00380 vector<string> words = SheppStrUtil::parse_line(arg);
00381
00382 if (words.empty()) {
00383 if (_debug) {
00384 printf("Debug is ON\n");
00385 } else {
00386 printf("Debug is OFF\n");
00387 }
00388 return 0;
00389 } else if (words.size() > 1) {
00390 printf("usage: beauty [on|off]\n");
00391 return -1;
00392 }
00393
00394 if (words[0] == "on") {
00395 _debug = true;
00396 printf("Debug now ON\n");
00397 } else if (words[0] == "off") {
00398 _debug = false;
00399 printf("Debug now OFF\n");
00400 } else {
00401 printf("usage: beauty [on|off]\n");
00402 return -1;
00403 }
00404
00405 return 0;
00406 }
00407
00409
00413 int cmd_xmlcmd(char *arg)
00414 {
00415 if (!_connected) {
00416 printf("not connected\n");
00417 return -1;
00418 }
00419
00420 if (_beauty) {
00421
00422 string last_command("");
00423 StrUtil::iso88591_to_utf8(_session->get_last_command(), last_command);
00424
00425 if (_session->get_last_command() == "") {
00426 printf("\n");
00427 return 0;
00428 }
00429 try {
00430 StrUtil str_util;
00431 printf("%s\n", str_util.xml_beautifier(last_command).c_str());
00432 } catch (const XmlException &e) {
00433 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00434 e.get_msg().c_str());
00435 }
00436 } else {
00437 printf("%s\n", _session->get_last_command().c_str());
00438 }
00439 return 0;
00440 }
00441
00443
00447 int cmd_xmlrsp(char *arg)
00448 {
00449 if (!_connected) {
00450 printf("not connected\n");
00451 return -1;
00452 }
00453
00454 if (_beauty) {
00455
00456 string last_response = _session->get_last_response();
00457
00458 if (last_response == "") {
00459 printf("\n");
00460 return 0;
00461 }
00462 try {
00463 StrUtil str_util;
00464 printf("%s\n", str_util.xml_beautifier(last_response).c_str());
00465 } catch (const XmlException &e) {
00466 printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00467 e.get_msg().c_str());
00468 }
00469 } else {
00470 printf("%s\n", _session->get_last_response().c_str());
00471 }
00472 return 0;
00473 }
00474
00475
00476
00478 void print_cmd_sent_ok()
00479 {
00480 printf("Ok! Use 'xmlcmd' and 'xmlrsp' to view command/response "
00481 "XML code.\n");
00482 }
00483
00485
00488 int process_action(Action &act)
00489 {
00490 if (!_connected) {
00491 printf("not connected\n");
00492 return -1;
00493 }
00494
00495 bool exception = false;
00496
00497 try {
00498 _session->process_action(&act);
00499 } catch (const EppException &e) {
00500 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00501 e.get_msg().c_str());
00502 exception = true;
00503 } catch (const IoException &e) {
00504 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00505 e.get_code(), e.get_msg().c_str());
00506 exception = true;
00507 _connected = false;
00508 } catch (const TransportException &e) {
00509 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00510 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00511 exception = true;
00512 _connected = false;
00513 } catch (const GeneralException &e) {
00514 printf("ERROR! General Exception [%d]:\n[%s]\n",
00515 e.get_code(), e.get_msg().c_str());
00516 exception = true;
00517 }
00518
00519 if (exception) {
00520 return -1;
00521 }
00522
00523 print_cmd_sent_ok();
00524 return 0;
00525 }
00526
00528
00532 int cmd_login(char *arg)
00533 {
00534 if (!_connected) {
00535 printf("not connected\n");
00536 return -1;
00537 }
00538
00539 if (_user == "") {
00540 printf("no username given.\n");
00541 return -1;
00542 }
00543
00544 Login act;
00545 LoginCmd *cmd = act.get_command();
00546
00547 cmd->set_clID(_user);
00548 cmd->set_pw(_login_pw);
00549
00550 if (_new_pw) {
00551 _new_pw = false;
00552 cmd->set_new_pw(_login_new_pw);
00553 }
00554
00555 if (process_action(act) != 0) {
00556 return -1;
00557 }
00558
00559 return 0;
00560 }
00561
00563
00567 int cmd_logout(char *arg)
00568 {
00569 if (!_connected) {
00570 printf("not connected\n");
00571 return -1;
00572 }
00573
00574 Logout act;
00575
00576 if (process_action(act) != 0) {
00577 return -1;
00578 }
00579
00580 return 0;
00581 }
00582
00584
00588 int cmd_hello(char *arg)
00589 {
00590 if (!_connected) {
00591 printf("not connected\n");
00592 return -1;
00593 }
00594
00595 bool exception = false;
00596
00597 try {
00598 if (_debug) {
00599 printf("Sending EPP Hello\n");
00600 }
00601 _session->send_hello();
00602 } catch (const EppException &e) {
00603 printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00604 e.get_msg().c_str());
00605 exception = true;
00606 } catch (const IoException &e) {
00607 printf("ERROR! IO Exception [%d]:\n[%s]\n",
00608 e.get_code(), e.get_msg().c_str());
00609 exception = true;
00610 _connected = false;
00611 } catch (const TransportException &e) {
00612 printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00613 e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00614 exception = true;
00615 _connected = false;
00616 } catch (const GeneralException &e) {
00617 printf("ERROR! General Exception [%d]:\n[%s]\n",
00618 e.get_code(), e.get_msg().c_str());
00619 exception = true;
00620 }
00621
00622 if (exception) {
00623 return -1;
00624 }
00625
00626 print_cmd_sent_ok();
00627 return 0;
00628 }
00629
00630
00631
00633
00637 int cmd_quit(char *arg)
00638 {
00639 printf("Bye\n");
00640 exit(0);
00641 }
00642
00644
00648 int cmd_help(char *arg)
00649 {
00650 CMD_CONTAINER<SheppCommand>::const_iterator it;
00651 for (it = _commands.begin(); it != _commands.end(); it++) {
00652 printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
00653 }
00654
00655 return 0;
00656 }
00657
00659
00663 int cmd_about(char *arg)
00664 {
00665 printf("shepp version " SHEPP_VERSION ", an EPP client shell!\n");
00666 printf("Copyright 2006 Registro.br <libepp@registro.br>\n");
00667 printf("shepp is distributed with libepp-nicbr: "
00668 "http://registro.br/epp/index-EN.html\n");
00669 #if USE_BR_DOMAINS
00670 printf("Built with BrDomain EPP extension support.\n");
00671 #endif //USE_BR_DOMAINS
00672
00673 return 0;
00674 }
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735 #endif //__SHEPP_COMMAND_FUNCTIONS_H__