libepp_nicbr
SheppCommandFunctions.H
Go to the documentation of this file.
1 /* ${copyright}$ */
2 /* $Id: SheppCommandFunctions.H 1267 2014-12-03 18:59:06Z mendelson $ */
6 #ifndef __SHEPP_COMMAND_FUNCTIONS_H__
7 #define __SHEPP_COMMAND_FUNCTIONS_H__
8 
9 #include <fstream>
10 #include <unistd.h> //alarm
11 
12 //used by getpasswd
13 #include <termios.h>
14 #include <iostream>
15 using std::cin;
16 using std::cout;
17 using std::endl;
18 
19 #include "SheppGlobal.H"
20 #include "SheppStrUtil.H"
21 #include "SheppObjSet.H"
22 #include "SheppPrint.H"
23 
24 #include "IoException.H"
25 #include "TransportException.H"
26 #include "EppException.H"
27 #include "XmlException.H"
28 
29 #include "Login.H"
30 #include "Logout.H"
31 
32 using std::ifstream;
33 
34 // Functions for eppsh commands
35 int cmd_source(vector<string> &);
36 int cmd_login(vector<string> &);
37 int cmd_logout(vector<string> &);
38 int cmd_hello(vector<string> &);
39 int cmd_poll(vector<string> &);
40 int cmd_domain(vector<string> &);
41 int cmd_contact(vector<string> &);
42 #if USE_BR_ORG
43 int cmd_brorg(vector<string> &);
44 #endif //USE_BR_ORG
45 #if USE_IP_MANAGEMENT
46 int cmd_asn(vector<string> &);
47 int cmd_asn_reserve(vector<string> &);
48 int cmd_ipnetwork(vector<string> &);
49 #endif //USE_IP_MANAGEMENT
50 int cmd_def_reg(vector<string> &);
51 
52 int cmd_xmlcmd(vector<string> &);
53 int cmd_xmlrsp(vector<string> &);
54 int cmd_runxml(vector<string> &);
55 
56 int cmd_server(vector<string> &);
57 int cmd_port(vector<string> &);
58 int cmd_client_pem(vector<string> &);
59 int cmd_root_pem(vector<string> &);
60 int cmd_pass(vector<string> &);
61 int cmd_user(vector<string> &);
62 int cmd_login_pw(vector<string> &);
63 int cmd_login_new_pw(vector<string> &);
64 int cmd_lang(vector<string> &);
65 int cmd_connect(vector<string> &);
66 int cmd_disconnect(vector<string> &);
67 int cmd_cltrid(vector<string> &);
68 
69 int cmd_help(vector<string> &);
70 int cmd_about(vector<string> &);
71 int cmd_quit(vector<string> &);
72 
73 int cmd_beauty(vector<string> &);
74 int cmd_debug(vector<string> &);
75 int cmd_autorsp(vector<string> &);
76 int cmd_keepalive(vector<string> &);
77 int cmd_peer_crt_check(vector<string> &);
78 
79 string getpasswd(string prompt);
80 
83 {
84  SheppCommand cmd;
85 
86  //non-EPP commands
87  cmd.reset("?", cmd_help, "This help screen");
88  _commands.insert(_commands.end(), cmd);
89  cmd.reset("help", cmd_help, "This help screen");
90  _commands.insert(_commands.end(), cmd);
91  cmd.reset("about", cmd_about, "shepp version information");
92  _commands.insert(_commands.end(), cmd);
93  cmd.reset("exit", cmd_quit, "Exit shepp\n");
94  _commands.insert(_commands.end(), cmd);
95 
96  //runtime flags
97  cmd.reset("beauty", cmd_beauty, "Turn XML beautifier on/off");
98  _commands.insert(_commands.end(), cmd);
99  cmd.reset("debug", cmd_debug, "Turn debug messages on/off");
100  _commands.insert(_commands.end(), cmd);
101  cmd.reset("autorsp", cmd_autorsp, "Turn auto-show response on/off");
102  _commands.insert(_commands.end(), cmd);
103  cmd.reset("keepalive", cmd_keepalive, "Turn keep-alive on/off");
104  _commands.insert(_commands.end(), cmd);
105  cmd.reset("peer-crt-check", cmd_peer_crt_check,
106  "Turn peer's certificate check on/off\n");
107  _commands.insert(_commands.end(), cmd);
108 
109  //connection related commands
110  cmd.reset("source", cmd_source, "Read and execute file with shepp commands");
111  _commands.insert(_commands.end(), cmd);
112  cmd.reset("server", cmd_server, "Sets server address");
113  _commands.insert(_commands.end(), cmd);
114  cmd.reset("port", cmd_port, "Sets server port");
115  _commands.insert(_commands.end(), cmd);
116  cmd.reset("client-pem", cmd_client_pem,
117  "Sets client.pem certificate file location");
118  _commands.insert(_commands.end(), cmd);
119  cmd.reset("root-pem", cmd_root_pem,
120  "Sets root.pem certificate file location");
121  _commands.insert(_commands.end(), cmd);
122  cmd.reset("pass", cmd_pass, "Sets SSL certificate passphrase");
123  _commands.insert(_commands.end(), cmd);
124  cmd.reset("user", cmd_user, "Sets EPP Login username");
125  _commands.insert(_commands.end(), cmd);
126  cmd.reset("pw", cmd_login_pw, "Sets EPP Login password");
127  _commands.insert(_commands.end(), cmd);
128  cmd.reset("newpw", cmd_login_new_pw, "Sets new EPP Login password");
129  _commands.insert(_commands.end(), cmd);
130  cmd.reset("lang", cmd_lang, "Sets text language");
131  _commands.insert(_commands.end(), cmd);
132  cmd.reset("connect", cmd_connect, "Establishes connection");
133  _commands.insert(_commands.end(), cmd);
134  cmd.reset("disconnect", cmd_disconnect, "Closes connection");
135  _commands.insert(_commands.end(), cmd);
136  cmd.reset("cltrid", cmd_cltrid, "Client transaction id\n");
137  _commands.insert(_commands.end(), cmd);
138 
139  //EPP commands
140  cmd.reset("login", cmd_login, "EPP Login");
141  _commands.insert(_commands.end(), cmd);
142  cmd.reset("logout", cmd_logout, "EPP Logout");
143  _commands.insert(_commands.end(), cmd);
144  cmd.reset("hello", cmd_hello, "EPP Hello");
145  _commands.insert(_commands.end(), cmd);
146  cmd.reset("poll", cmd_poll, "EPP Poll related actions");
147  _commands.insert(_commands.end(), cmd);
148  cmd.reset("domain", cmd_domain, "EPP Domain related actions");
149  _commands.insert(_commands.end(), cmd);
150  cmd.reset("contact", cmd_contact,
151 #if USE_BR_ORG
152  "EPP Contact related actions"
153 #else
154  "EPP Contact related actions\n"
155 #endif
156  );
157  _commands.insert(_commands.end(), cmd);
158 #if USE_IP_MANAGEMENT
159  cmd.reset("asn", cmd_asn, "Autonomous System related actions");
160  _commands.insert(_commands.end(), cmd);
161  cmd.reset("asnreserve", cmd_asn_reserve, "Autonomous System Numbers Reserve related actions");
162  _commands.insert(_commands.end(), cmd);
163  cmd.reset("ipnetwork", cmd_ipnetwork, "IP network related actions");
164  _commands.insert(_commands.end(), cmd);
165 #endif //USE_IP_MANAGEMENT
166  cmd.reset("defreg", cmd_def_reg, "Defensive registration related actions");
167  _commands.insert(_commands.end(), cmd);
168 #if USE_BR_ORG
169  cmd.reset("brorg", cmd_brorg, "EPP BrOrg related actions\n");
170  _commands.insert(_commands.end(), cmd);
171 #endif //USE_BR_ORG
172 
173  //XML related commands
174  cmd.reset("xmlcmd", cmd_xmlcmd, "Show last command XML");
175  _commands.insert(_commands.end(), cmd);
176  cmd.reset("xmlrsp", cmd_xmlrsp, "Show last response XML");
177  _commands.insert(_commands.end(), cmd);
178  cmd.reset("runxml", cmd_runxml, "Run XML input (good for debugging)");
179  _commands.insert(_commands.end(), cmd);
180 }
181 
183 
187 const SheppCommand* find_command(string name)
188 {
189  list<SheppCommand>::const_iterator it;
190  for (it = _commands.begin(); it != _commands.end(); it++) {
191  if ((*it).name == name) {
192  return &(*it);
193  }
194  }
195 
196  return ((const SheppCommand *) NULL);
197 }
198 
201 
205 int executeInputFile(const string &file)
206 {
207  ifstream inputFile;
208  inputFile.open(file.c_str());
209  if (inputFile.eof() == true ||
210  inputFile.fail() == true ||
211  inputFile.bad() == true) {
212  printf("error reading input file.\n");
213  return -1;
214  }
215 
216  init_commands();
217 
218  while (inputFile.good() == true) {
219  string cmdLine("");
220  getline(inputFile, cmdLine);
221 
222  SheppStrUtil::trim((char*) cmdLine.c_str());
223  if (cmdLine.size() == 0) {
224  continue;
225  }
226 
227  // Comments symbols
228  if (cmdLine[0] == '#' || cmdLine[0] == ';') {
229  continue;
230  }
231 
232  // get the first word (command name)
233  vector<string> command_args =
234  SheppStrUtil::parse_line((char*) cmdLine.c_str());
235 
236  // args will be empty if any word exceed MAX_WORD_LENGTH
237  if (command_args.empty()) {
238  continue;
239  }
240 
241  const SheppCommand* cmd = find_command(command_args[0]);
242 
243  if (cmd == NULL) {
244  fprintf(stderr, "Unknown command: %s\n", command_args[0].c_str());
245  continue;
246  }
247 
248  // commands that require raw command-line input
249  if (command_args[0] == "runxml") {
250  string rawcmd(cmdLine);
251  rawcmd = rawcmd.substr(command_args[0].length());
252  while (whitespace(rawcmd[0])) {
253  rawcmd = rawcmd.substr(1);
254  }
255 
256  if (rawcmd == "") {
257  // read input
258  printf("Enter XML code. Escape character is '^]'.\n");
259 
260  string unit;
261  getline(cin, unit, (char)29);
262  rawcmd += unit;
263  }
264 
265  command_args.clear();
266  command_args.push_back(rawcmd);
267  } else {
268  command_args.erase(command_args.begin());
269  }
270 
271  (*(cmd->function))(command_args);
272  }
273 
274  inputFile.close();
275 
276  return 0;
277 }
278 
281 
285 int cmd_source(vector<string> &args)
286 {
287  if (args.size() != 0) {
288  return executeInputFile(args[0]);
289  }
290 
291  return 0;
292 }
293 
295 
299 int cmd_server(vector<string> &args)
300 {
301  if (args.size() != 0) {
302  string new_port;
303  string new_server;
304  SheppStrUtil::split(args[0], new_server, new_port, ":", true);
305  if (new_server != _server) {
306  _server = new_server;
307  }
308  if (new_port != "" && _port != atoi(new_port.c_str())) {
309  _port = atoi(new_port.c_str());
310  }
311  }
312 
313  if (_server != "") {
314  printf("server %s:%d\n", _server.c_str(), _port);
315  } else {
316  printf("no server address given.\n");
317  }
318 
319  return 0;
320 }
321 
323 
327 int cmd_port(vector<string> &args)
328 {
329  if (args.size() == 1) {
330  if (_port != atoi(args[0].c_str())) {
331  _port = atoi(args[0].c_str());
332  }
333  }
334 
335  printf("port %d\n", _port);
336 
337  return 0;
338 }
339 
341 
345 int cmd_client_pem(vector<string> &args)
346 {
347  if (args.size() == 1) {
348  if (_client_pem != args[0]) {
349  _client_pem = args[0];
350  printf("Warning: changing cetificates requires you to reconnect.\n");
351  }
352  } else if (args.size() > 1) {
353  printf("usage: client-pem <file>\n");
354  return -1;
355  }
356 
357  printf("Current client.pem is %s\n", _client_pem.c_str());
358  return 0;
359 }
360 
362 
366 int cmd_root_pem(vector<string> &args)
367 {
368  if (args.size() == 1) {
369  if (_root_pem != args[0]) {
370  _root_pem = args[0];
371  printf("Warning: changing cetificates requires you to reconnect.\n");
372  }
373  } else if (args.size() > 1) {
374  printf("usage: root-pem <file>\n");
375  return -1;
376  }
377 
378  printf("Current root.pem is %s\n", _root_pem.c_str());
379  return 0;
380 }
381 
383 
387 int cmd_pass(vector<string> &args)
388 {
389  if (!args.empty()) {
390  _passphrase = args[0];
391  } else {
392  _passphrase = getpasswd("Passphrase");
393  }
394 
395  return 0;
396 }
397 
399 
403 int cmd_user(vector<string> &args)
404 {
405  if (args.size() == 0) {
406  if (_user != "") {
407  printf("username: %s\n", _user.c_str());
408  return 0;
409  } else {
410  printf("no username given.\n");
411  return -1;
412  }
413  }
414 
415  _user = args[0];
416 
417  return 0;
418 }
419 
421 
425 int cmd_login_pw(vector<string> &args)
426 {
427  if (!args.empty()) {
428  _login_pw = args[0];
429  } else {
430  _login_pw = getpasswd("Password");
431  }
432 
433  return 0;
434 }
435 
437 
441 int cmd_login_new_pw(vector<string> &args)
442 {
443  if (!args.empty()) {
444  _login_new_pw = args[0];
445  } else {
446  _login_new_pw = getpasswd("New Password");
447  }
448 
449  _new_pw = true;
450  return 0;
451 }
452 
454 
458 int cmd_lang(vector<string> &args)
459 {
460  if (args.empty()) {
461  printf("usage: lang [en|pt]\n");
462  return -1;
463  } else {
464  _lang = args[0];
465  }
466 
467  return 0;
468 }
469 
471 
475 int cmd_connect(vector<string> &args)
476 {
477  if (cmd_server(args) != 0) {
478  return -1;
479  }
480 
481  bool exception = false;
482 
483  // Session code goes here
484  try {
485  _session = auto_ptr<Session>(new Session(_server, _port));
486  if (_peer_crt_check) {
487  _session->enable_cert_common_name_check();
488  } else {
489  _session->disable_cert_common_name_check();
490  }
491  _session->set_clTrID(_clTrID);
492  _session->connect(_client_pem, _root_pem, _passphrase);
493  printf("Connected to %s\n", _server.c_str());
494 
495  Greeting *greeting = _session->get_greeting();
496 
497  // attempt to verify secDNS extension version supported by server
498  set<string> extensions = greeting->get_extURI();
499  set<string>::iterator it;
500  for (it = extensions.begin(); it != extensions.end(); it++) {
501  if (*it == "urn:ietf:params:xml:ns:secDNS-1.0") {
502  _serverSecDnsVersion = "1.0";
503  } else if (*it == "urn:ietf:params:xml:ns:secDNS-1.1") {
504  _serverSecDnsVersion = "1.1";
505  }
506  }
507 
508  if (_debug) {
509  if (greeting) {
510  printf("Greeting received\n");
511  }
512  } //_debug
513 
514  _xmlrsp = _session->get_last_response();
515  _xmlcmd = _session->get_last_command();
516 
517  _connected = true;
518 
519  } catch (const IoException &e) {
520  printf("ERROR! IO Exception [%d]:\n[%s]\n",
521  e.get_code(), e.get_msg().c_str());
522  exception = true;
523  } catch (const TransportException &e) {
524  printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
525  e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
526  exception = true;
527  } catch (const GeneralException &e) {
528  printf("ERROR! General Exception [%d]:\n[%s]\n",
529  e.get_code(), e.get_msg().c_str());
530  exception = true;
531  }
532 
533  if (exception) {
534  return -1;
535  }
536 
537  // print greeting
538  vector<string> dummy;
539  cmd_xmlrsp(dummy);
540 
541  return 0;
542 }
543 
545 
549 int cmd_disconnect(vector<string> &args)
550 {
551  if (_connected) {
552  bool exception = false;
553 
554  // Session code goes here
555  try {
556  _session->disconnect();
557  printf("disconnected from %s\n", _server.c_str());
558  _connected = false;
559  } catch (const TransportException &e) {
560  printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
561  e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
562  exception = true;
563  } catch (const GeneralException &e) {
564  printf("ERROR! General Exception [%d]:\n[%s]\n",
565  e.get_code(), e.get_msg().c_str());
566  exception = true;
567  }
568 
569  if (exception) {
570  return -1;
571  }
572  } else {
573  printf("not connected\n");
574  }
575 
576  return 0;
577 }
578 
580 
584 int cmd_cltrid(vector<string> &args)
585 {
586  if (args.size() == 0) {
587  if (!_clTrID.empty()) {
588  printf("clTrID: %s\n", _clTrID.c_str());
589  return 0;
590  } else {
591  printf("clTrID: <random>\n");
592  return -1;
593  }
594  }
595 
596  _clTrID = args[0];
597  if (_connected) {
598  _session->set_clTrID(_clTrID);
599  }
600 
601  return 0;
602 }
603 
605 
609 int cmd_beauty(vector<string> &args)
610 {
611  if (args.empty()) {
612  if (_beauty) {
613  printf("XML beautifier is ON\n");
614  } else {
615  printf("XML beautifier is OFF\n");
616  }
617  return 0;
618  } else if (args.size() > 1) {
619  printf("usage: beauty [on|off]\n");
620  return -1;
621  }
622 
623  if (args[0] == "on") {
624  _beauty = true;
625  printf("XML beautifier now ON\n");
626  } else if (args[0] == "off") {
627  _beauty = false;
628  printf("XML beautifier now OFF\n");
629  } else {
630  printf("usage: beauty [on|off]\n");
631  return -1;
632  }
633 
634  return 0;
635 }
636 
638 
642 int cmd_debug(vector<string> &args)
643 {
644  if (args.empty()) {
645  if (_debug) {
646  printf("Debug is ON\n");
647  } else {
648  printf("Debug is OFF\n");
649  }
650  return 0;
651  } else if (args.size() > 1) {
652  printf("usage: debug [on|off]\n");
653  return -1;
654  }
655 
656  if (args[0] == "on") {
657  _debug = true;
658  printf("Debug now ON\n");
659  } else if (args[0] == "off") {
660  _debug = false;
661  printf("Debug now OFF\n");
662  } else {
663  printf("usage: beauty [on|off]\n");
664  return -1;
665  }
666 
667  return 0;
668 }
669 
671 
675 int cmd_autorsp(vector<string> &args)
676 {
677  if (args.empty()) {
678  if (_autorsp) {
679  printf("autorsp is ON\n");
680  } else {
681  printf("autorsp is OFF\n");
682  }
683  return 0;
684  } else if (args.size() > 1) {
685  printf("usage: autorsp [on|off]\n");
686  return -1;
687  }
688 
689  if (args[0] == "on") {
690  _autorsp = true;
691  printf("autorsp now ON\n");
692  } else if (args[0] == "off") {
693  _autorsp = false;
694  printf("autorsp now OFF\n");
695  } else {
696  printf("usage: autorsp [on|off]\n");
697  return -1;
698  }
699 
700  return 0;
701 }
702 
704 
708 int cmd_keepalive(vector<string> &args)
709 {
710  if (args.empty()) {
711  if (_keepalive) {
712  printf("Keep-alive is ON\n");
713  } else {
714  printf("Keep-alive is OFF\n");
715  }
716  return 0;
717  } else if (args.size() > 1) {
718  printf("usage: keepalive [on|off]\n");
719  return -1;
720  }
721 
722  if (args[0] == "on") {
723  _keepalive = true;
724  printf("Keep-alive now ON\n");
725  } else if (args[0] == "off") {
726  _keepalive = false;
727  printf("Keep-alive now OFF\n");
728  } else {
729  printf("usage: keepalive [on|off]\n");
730  return -1;
731  }
732 
733  if (_keepalive) {
734  alarm(_keepalive_timer);
735  } else {
736  alarm(0);
737  }
738 
739  return 0;
740 }
741 
743 
747 int cmd_peer_crt_check(vector<string> &args)
748 {
749  if (args.empty()) {
750  if (_peer_crt_check) {
751  printf("Peer's certificate check is ON\n");
752  } else {
753  printf("Peer's certificate check is OFF\n");
754  }
755  return 0;
756  } else if (args.size() > 1) {
757  printf("usage: peer-crt-check [on|off]\n");
758  return -1;
759  }
760 
761  if (args[0] == "on") {
762  _peer_crt_check = true;
763  printf("Peer's certificate check now ON\n");
764  } else if (args[0] == "off") {
765  _peer_crt_check = false;
766  printf("Peer's certificate check now OFF\n");
767  } else {
768  printf("usage: peer-crt-check [on|off]\n");
769  return -1;
770  }
771 
772  return 0;
773 }
774 
776 static void sig_alrm(int signo)
777 {
778  if (_connected && !_cmd_running) {
779  _ka_running = true;
780  vector<string> args;
781  args.push_back((string) "keepalive");
782  cmd_hello(args);
783  _ka_running = false;
784  }
785  alarm(_keepalive_timer);
786 }
787 
789 
793 int cmd_xmlcmd(vector<string> &args)
794 {
795  if (!_connected) {
796  printf("not connected\n");
797  return -1;
798  }
799 
800  if (_beauty) {
801  // Convert to UTF8
802  string last_command("");
803  StrUtil::iso88591_to_utf8(_xmlcmd, last_command);
804 
805  if (_session->get_last_command() == "") {
806  printf("\n");
807  return 0;
808  }
809  try {
810  StrUtil str_util;
811  printf("%s\n", str_util.xml_beautifier(last_command).c_str());
812  } catch (const XmlException &e) {
813  printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
814  e.get_msg().c_str());
815  }
816  } else {
817  printf("%s\n", _session->get_last_command().c_str());
818  }
819  return 0;
820 }
821 
823 
827 int cmd_xmlrsp(vector<string> &args)
828 {
829  if (!_connected) {
830  printf("not connected\n");
831  return -1;
832  }
833 
834  if (_beauty) {
835  // No need to convert to UTF8
836  if (_xmlrsp == "") {
837  printf("\n");
838  return 0;
839  }
840  try {
841  StrUtil str_util;
842  printf("%s\n", str_util.xml_beautifier(_xmlrsp).c_str());
843  } catch (const XmlException &e) {
844  printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
845  e.get_msg().c_str());
846  }
847  } else {
848  printf("%s\n", _xmlrsp.c_str());
849  }
850  return 0;
851 }
852 
853 // EPP action processing functions
854 
857 {
858  if (!_autorsp) {
859  printf("Ok! Use 'xmlcmd' and 'xmlrsp' to view command/response "
860  "XML code.\n");
861  } else {
862  vector<string> dummy;
863  cmd_xmlrsp(dummy);
864  }
865 }
866 
868 
872 {
873  if (!_connected) {
874  printf("not connected\n");
875  return -1;
876  }
877 
878  bool exception = false;
879 
880  // prevents concurrency with keep-alive
881  if (!_ka_running) {
882  _cmd_running = true;
883  } else {
884  printf("ERROR! Keep-alive is running. Please try again.\n");
885  return -1;
886  }
887 
888  try {
889  _session->process_action(&act);
890  _xmlrsp = _session->get_last_response();
891  _xmlcmd = _session->get_last_command();
892  } catch (const EppException &e) {
893  printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
894  e.get_msg().c_str());
895  exception = true;
896  } catch (const IoException &e) {
897  printf("ERROR! IO Exception [%d]:\n[%s]\n",
898  e.get_code(), e.get_msg().c_str());
899  exception = true;
900  _connected = false;
901  } catch (const TransportException &e) {
902  printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
903  e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
904  exception = true;
905  _connected = false;
906  } catch (const GeneralException &e) {
907  printf("ERROR! General Exception [%d]:\n[%s]\n",
908  e.get_code(), e.get_msg().c_str());
909  exception = true;
910  }
911 
912  if (exception) {
913  _cmd_running = false;
914  return -1;
915  }
916 
918 
919  // a command was just run; postpone keepalive alarm
920  if (_keepalive) {
921  alarm(_keepalive_timer);
922  }
923 
924  _cmd_running = false;
925  return 0;
926 }
927 
929 
932 int cmd_runxml(vector<string> &args)
933 {
934  if (!_connected) {
935  printf("not connected\n");
936  return -1;
937  }
938 
939  bool exception = false;
940 
941  // prevents concurrency with keep-alive
942  if (!_ka_running) {
943  _cmd_running = true;
944  } else {
945  printf("ERROR! Keep-alive is running. Please try again.\n");
946  return -1;
947  }
948 
949  try {
950  _session->runXML(args[0]);
951  _xmlrsp = _session->get_last_response();
952  _xmlcmd = _session->get_last_command();
953  } catch (const EppException &e) {
954  printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
955  e.get_msg().c_str());
956  exception = true;
957  } catch (const IoException &e) {
958  printf("ERROR! IO Exception [%d]:\n[%s]\n",
959  e.get_code(), e.get_msg().c_str());
960  exception = true;
961  _connected = false;
962  } catch (const TransportException &e) {
963  printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
964  e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
965  exception = true;
966  _connected = false;
967  } catch (const GeneralException &e) {
968  printf("ERROR! General Exception [%d]:\n[%s]\n",
969  e.get_code(), e.get_msg().c_str());
970  exception = true;
971  }
972 
973  if (exception) {
974  _cmd_running = false;
975  return -1;
976  }
977 
979 
980  // a command was just run; postpone keepalive alarm
981  if (_keepalive) {
982  alarm(_keepalive_timer);
983  }
984 
985  _cmd_running = false;
986  return 0;
987 }
988 
990 
994 int cmd_login(vector<string> &args)
995 {
996  if (!_connected) {
997  printf("not connected\n");
998  return -1;
999  }
1000 
1001  if (_user == "") {
1002  printf("no username given.\n");
1003  return -1;
1004  }
1005 
1006  Login act;
1007  LoginCmd *cmd = act.get_command();
1008 
1009  // add extensions supported by client
1010 #if USE_BR_ORG
1011  cmd->add_extURI("urn:ietf:params:xml:ns:brorg-1.0");
1012 #endif
1013 #if USE_BR_DOMAINS
1014  cmd->add_extURI("urn:ietf:params:xml:ns:brdomain-1.0");
1015 #endif
1016 #if USE_IP_MANAGEMENT
1017  cmd->add_extURI("urn:ietf:params:xml:ns:ipnetwork-1.0");
1018  cmd->add_extURI("urn:ietf:params:xml:ns:asn-1.0");
1019 #endif
1020 #if USE_SECDNS_1_0
1021  cmd->add_extURI("urn:ietf:params:xml:ns:secDNS-1.0");
1022 #endif
1023  cmd->add_extURI("urn:ietf:params:xml:ns:secDNS-1.1");
1024 
1025  cmd->set_clID(_user);
1026  cmd->set_pw(_login_pw);
1027 
1028  if (_new_pw) {
1029  _new_pw = false;
1030  cmd->set_new_pw(_login_new_pw);
1031  }
1032 
1033  if (_lang != "" && _lang != "en") {
1034  cmd->set_lang(_lang);
1035  }
1036 
1037  if (process_action(act) != 0) {
1038  return -1;
1039  }
1040 
1041  return 0;
1042 }
1043 
1045 
1049 int cmd_logout(vector<string> &args)
1050 {
1051  if (!_connected) {
1052  printf("not connected\n");
1053  return -1;
1054  }
1055 
1056  Logout act;
1057 
1058  if (process_action(act) != 0) {
1059  return -1;
1060  }
1061 
1062  return 0;
1063 }
1064 
1066 
1070 int cmd_hello(vector<string> &args)
1071 {
1072  if (!_connected) {
1073  printf("not connected\n");
1074  return -1;
1075  }
1076 
1077  if (_ka_running &&
1078  !(args.size() == 1 && args[0] == "keepalive")) {
1079  printf("ERROR! Keep-alive is running. Please try again.\n");
1080  return -1;
1081  }
1082 
1083  bool exception = false;
1084 
1085  // prevents concurrency with keep-alive
1086  if (!_ka_running) {
1087  _cmd_running = true;
1088  }
1089 
1090  try {
1091  if (_debug) {
1092  printf("Sending EPP Hello\n");
1093  }
1094  _session->send_hello();
1095  // updates _xmlcmd and _xmlrsp only if it's not a keep-alive run
1096  if (!_ka_running) {
1097  _xmlrsp = _session->get_last_response();
1098  _xmlcmd = _session->get_last_command();
1099  }
1100  } catch (const EppException &e) {
1101  printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
1102  e.get_msg().c_str());
1103  exception = true;
1104  } catch (const IoException &e) {
1105  printf("ERROR! IO Exception [%d]:\n[%s]\n",
1106  e.get_code(), e.get_msg().c_str());
1107  exception = true;
1108  _connected = false;
1109  } catch (const TransportException &e) {
1110  printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
1111  e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
1112  exception = true;
1113  _connected = false;
1114  } catch (const GeneralException &e) {
1115  printf("ERROR! General Exception [%d]:\n[%s]\n",
1116  e.get_code(), e.get_msg().c_str());
1117  exception = true;
1118  }
1119 
1120  if (exception) {
1121  if (_cmd_running) {
1122  _cmd_running = false;
1123  }
1124  return -1;
1125  }
1126 
1127  if (!_ka_running) {
1129  }
1130 
1131  if (_cmd_running) {
1132  // a command was just run; postpone keepalive alarm
1133  if (_keepalive) {
1134  alarm(_keepalive_timer);
1135  }
1136  _cmd_running = false;
1137  }
1138  return 0;
1139 }
1140 
1141 // non-EPP commands
1142 
1144 
1148 int cmd_quit(vector<string> &args)
1149 {
1150  if (_connected) {
1151  _session->disconnect();
1152  }
1153  printf("Bye\n");
1154  exit(0);
1155 }
1156 
1158 
1162 int cmd_help(vector<string> &args)
1163 {
1164  list<SheppCommand>::const_iterator it;
1165  for (it = _commands.begin(); it != _commands.end(); it++) {
1166  printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
1167  }
1168 
1169  return 0;
1170 }
1171 
1173 
1177 int cmd_about(vector<string> &args)
1178 {
1179  about();
1180  return 0;
1181 }
1182 
1184 
1188 string getpasswd(string prompt) {
1189  printf("%s:", prompt.c_str());
1190 
1191  string passwd("");
1192  struct termios old_opts;
1193  struct termios new_opts;
1194 
1195  int res = 0;
1196 
1197  // store old settings
1198  res = tcgetattr(STDIN_FILENO, &old_opts);
1199  assert(res == 0);
1200 
1201  // set new terminal parms
1202  memcpy(&new_opts, &old_opts, sizeof(new_opts));
1203  new_opts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
1204  new_opts.c_lflag |= ICANON;
1205  tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
1206 
1207  // read password
1208  getline(cin, passwd);
1209  cout << endl;
1210 
1211  // restore old settings
1212  res = tcsetattr(STDIN_FILENO, TCSANOW, &old_opts);
1213  assert(res == 0);
1214 
1215  return passwd;
1216 }
1217 
1218 #endif //__SHEPP_COMMAND_FUNCTIONS_H__
LoginCmd * get_command()
Returns raw pointer to the command.
Definition: Login.H:57
Transport Exception Class.
Definition: TransportException.H:17
int cmd_xmlrsp(vector< string > &)
Shows last XML response.
Definition: SheppCommandFunctions.H:827
int cmd_autorsp(vector< string > &)
Turns auto-show response ON and OFF.
Definition: SheppCommandFunctions.H:675
StrUtil Class: String Manipulation Utilities.
Definition: StrUtil.H:26
const SheppCommand * find_command(string name)
Given a word check if it's a valid SheppCommand.
Definition: SheppCommandFunctions.H:187
void set_new_pw(const string &new_pw)
Sets new password.
Definition: LoginCmd.H:44
static int split(string input, string &first, string &second, string splitter, bool relaxed=false)
Definition: SheppStrUtil.H:120
shepp compiler definitions and global variables
int cmd_poll(vector< string > &)
main poll command
Definition: PollFunctions.H:44
XML Exception Class.
int cmd_keepalive(vector< string > &)
Turns keep-alive ON and OFF.
Definition: SheppCommandFunctions.H:708
int cmd_brorg(vector< string > &args)
main brorg command
Definition: BrOrgFunctions.H:1002
EPP Logout Class.
int cmd_contact(vector< string > &)
main contact command
Definition: ContactFunctions.H:820
shepp specific objects information setting class
int cmd_connect(vector< string > &)
Connects to previously specified server and port.
Definition: SheppCommandFunctions.H:475
string getpasswd(string prompt)
Function to read from stdin without echoing.
Definition: SheppCommandFunctions.H:1188
int cmd_asn_reserve(vector< string > &args)
main asn reserve command
Definition: AsnReserveFunctions.H:212
int cmd_domain(vector< string > &)
main domain command
Definition: DomainFunctions.H:1296
EPP LoginCmd Class.
Definition: LoginCmd.H:17
int cmd_xmlcmd(vector< string > &)
Shows last XML command.
Definition: SheppCommandFunctions.H:793
static int iso88591_to_utf8(const string &iso88591, string &utf8)
int cmd_lang(vector< string > &)
Sets text language based on input command-line.
Definition: SheppCommandFunctions.H:458
static vector< string > parse_line(char *line)
Given a line, split its words by white space into a string vector (shepp)
Definition: SheppStrUtil.H:63
int cmd_client_pem(vector< string > &)
Sets client.pem certificate file location.
Definition: SheppCommandFunctions.H:345
shepp object information screen printing class
Transport Exception Class.
int cmd_login_new_pw(vector< string > &)
Sets new EPP login password based on input command-line.
Definition: SheppCommandFunctions.H:441
isfunc_t * function
Function to be called.
Definition: SheppCommand.H:49
EPP Action Class.
Definition: Action.H:24
void print_cmd_sent_ok()
Prints "command sent ok" message.
Definition: SheppCommandFunctions.H:856
XML Exception Class.
Definition: XmlException.H:17
int cmd_runxml(vector< string > &)
Run XML input command.
Definition: SheppCommandFunctions.H:932
int cmd_beauty(vector< string > &)
Turns XML beautifier ON and OFF.
Definition: SheppCommandFunctions.H:609
void set_pw(const string &pw)
Sets password.
Definition: LoginCmd.H:38
int cmd_logout(vector< string > &)
Send an EPP Logout.
Definition: SheppCommandFunctions.H:1049
int cmd_pass(vector< string > &)
Sets SSL certificate passphrase based on input command-line.
Definition: SheppCommandFunctions.H:387
int cmd_source(vector< string > &)
Definition: SheppCommandFunctions.H:285
void add_extURI(const string &extURI)
Adds an object extension supported by the client.
Definition: LoginCmd.H:92
int cmd_login(vector< string > &)
Send an EPP Login.
Definition: SheppCommandFunctions.H:994
General Exception Class.
Definition: GeneralException.H:19
int cmd_peer_crt_check(vector< string > &)
Turn peer's certificate check ON and OFF.
Definition: SheppCommandFunctions.H:747
int cmd_port(vector< string > &)
Sets server port based on input command-line.
Definition: SheppCommandFunctions.H:327
string _server
Global connection setup variables.
Definition: SheppGlobal.H:40
auto_ptr< Session > _session
Reference to session.
Definition: SheppGlobal.H:71
int cmd_about(vector< string > &)
Prints version information.
Definition: SheppCommandFunctions.H:1177
string get_msg() const
Returns the Exception Message.
Definition: GeneralException.H:43
int process_action(Action &act)
Send and EPP Action.
Definition: SheppCommandFunctions.H:871
int cmd_server(vector< string > &)
Sets server address [and port] based on input command-line.
Definition: SheppCommandFunctions.H:299
static char * trim(char *line)
Removes leading and ending white spaces from line (shepp)
Definition: SheppStrUtil.H:30
int cmd_root_pem(vector< string > &)
Sets root.pem certificate file location.
Definition: SheppCommandFunctions.H:366
int cmd_quit(vector< string > &)
Leaves shepp.
Definition: SheppCommandFunctions.H:1148
void reset(string name, isfunc_t *function, string brief)
Reinitializes all object attributes.
Definition: SheppCommand.H:38
EPP Login Class.
Definition: Login.H:23
string xml_beautifier(const string &input_txt)
XML Beautifier.
EPP Exception Class.
void init_commands()
Populates set of available commands.
Definition: SheppCommandFunctions.H:82
int cmd_ipnetwork(vector< string > &args)
main ipnetwork command
Definition: IpNetworkFunctions.H:1048
int cmd_hello(vector< string > &)
Send an EPP HELLO.
Definition: SheppCommandFunctions.H:1070
SheppCommand class: command name, function and a brief description.
Definition: SheppCommand.H:16
string get_low_level_msg() const
Return Low Level Error Message.
Definition: GeneralException.H:49
int cmd_login_pw(vector< string > &)
Sets EPP login password based on input command-line.
Definition: SheppCommandFunctions.H:425
int executeInputFile(const string &file)
Definition: SheppCommandFunctions.H:205
int cmd_help(vector< string > &)
Prints help.
Definition: SheppCommandFunctions.H:1162
int get_code() const
Returns the Exception Code.
Definition: GeneralException.H:37
EPP Session Class.
Definition: Session.H:30
void set_lang(const string &lang)
Sets text language.
Definition: LoginCmd.H:56
EPP Greeting Class.
Definition: Greeting.H:21
int cmd_def_reg(vector< string > &)
main defensive registration command
Definition: DefRegFunctions.H:657
int cmd_disconnect(vector< string > &)
Closes connection.
Definition: SheppCommandFunctions.H:549
IO Exception Class.
Definition: IoException.H:17
void about()
shepp build info
Definition: SheppGlobal.H:74
EPP Login Class.
int cmd_asn(vector< string > &args)
main asn command
Definition: AsnFunctions.H:674
void set_clID(const string &clID)
Sets Client ID.
Definition: LoginCmd.H:32
int cmd_debug(vector< string > &)
Turns debug messages ON and OFF.
Definition: SheppCommandFunctions.H:642
EPP command-line shell client string manipulation routines class.
LIBEPP_NICBR_NS_USE list< SheppCommand > _commands
Global container of available commands.
Definition: SheppGlobal.H:37
EPP Logout Class.
Definition: Logout.H:23
int cmd_cltrid(vector< string > &)
Sets the client transaction id for the next commands.
Definition: SheppCommandFunctions.H:584
int cmd_user(vector< string > &)
Sets login user based on input command-line.
Definition: SheppCommandFunctions.H:403
EPP Exception Class.
Definition: EppException.H:17