You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

121 lines
3.8 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "rec_channel.hh"
  26. #include <iostream>
  27. #include "pdnsexception.hh"
  28. #include "arguments.hh"
  29. #include "namespaces.hh"
  30. ArgvMap &arg()
  31. {
  32. static ArgvMap arg;
  33. return arg;
  34. }
  35. static void initArguments(int argc, char** argv)
  36. {
  37. arg().set("config-dir","Location of configuration directory (recursor.conf)")=SYSCONFDIR;
  38. arg().set("socket-dir",string("Where the controlsocket will live, ")+LOCALSTATEDIR+"/pdns-recursor when unset and not chrooted" )="";
  39. arg().set("chroot","switch to chroot jail")="";
  40. arg().set("process","When controlling multiple recursors, the target process number")="";
  41. arg().set("timeout", "Number of seconds to wait for the recursor to respond")="5";
  42. arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
  43. arg().setCmd("help","Provide this helpful message");
  44. arg().setCmd("version","Show the version of this program");
  45. arg().laxParse(argc,argv);
  46. if(arg().mustDo("help") || arg().getCommands().empty()) {
  47. cout<<"syntax: rec_control [options] command, options as below: "<<endl<<endl;
  48. cout<<arg().helpstring(arg()["help"])<<endl;
  49. cout<<"In addition, 'rec_control help' can be used to retrieve a list\nof available commands from PowerDNS"<<endl;
  50. exit(arg().mustDo("help") ? 0 : 99);
  51. }
  52. if(arg().mustDo("version")) {
  53. cout<<"rec_control version "<<VERSION<<endl;
  54. exit(0);
  55. }
  56. string configname=::arg()["config-dir"]+"/recursor.conf";
  57. if (::arg()["config-name"] != "")
  58. configname=::arg()["config-dir"]+"/recursor-"+::arg()["config-name"]+".conf";
  59. cleanSlashes(configname);
  60. arg().laxFile(configname.c_str());
  61. arg().laxParse(argc,argv); // make sure the commandline wins
  62. if (::arg()["socket-dir"].empty()) {
  63. if (::arg()["chroot"].empty())
  64. ::arg().set("socket-dir") = std::string(LOCALSTATEDIR) + "/pdns-recursor";
  65. else
  66. ::arg().set("socket-dir") = ::arg()["chroot"] + "/";
  67. } else if (!::arg()["chroot"].empty()) {
  68. ::arg().set("socket-dir") = ::arg()["chroot"] + "/" + ::arg()["socket-dir"];
  69. }
  70. }
  71. int main(int argc, char** argv)
  72. try
  73. {
  74. initArguments(argc, argv);
  75. RecursorControlChannel rccS;
  76. string sockname="pdns_recursor";
  77. if (arg()["config-name"] != "")
  78. sockname+="-"+arg()["config-name"];
  79. if(!arg()["process"].empty())
  80. sockname+="."+arg()["process"];
  81. sockname.append(".controlsocket");
  82. rccS.connect(arg()["socket-dir"], sockname);
  83. const vector<string>&commands=arg().getCommands();
  84. string command;
  85. for(unsigned int i=0; i< commands.size(); ++i) {
  86. if(i>0)
  87. command+=" ";
  88. command+=commands[i];
  89. }
  90. rccS.send(command, nullptr, arg().asNum("timeout"));
  91. string receive=rccS.recv(0, arg().asNum("timeout"));
  92. if(receive.compare(0, 7, "Unknown") == 0) {
  93. cerr<<receive<<endl;
  94. return 1;
  95. }
  96. cout<<receive;
  97. return 0;
  98. }
  99. catch(PDNSException& ae)
  100. {
  101. cerr<<"Fatal: "<<ae.reason<<"\n";
  102. return 1;
  103. }