Changeset 1083

Show
Ignore:
Timestamp:
08/16/07 23:10:47 (1 year ago)
Author:
ahu
Message:

finish up syslog, setgid, setuid, chroot

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/pdns/pdns/nproxy.cc

    r1082 r1083  
    4040nifs_t g_nifs; 
    4141 
     42void syslogFmt(const boost::format& fmt) 
     43{ 
     44  cerr<<"nproxy: "<<fmt<<endl; 
     45  syslog(LOG_WARNING, "%s", str(fmt).c_str()); 
     46} 
     47 
    4248void handleOutsideUDPPacket(int fd, boost::any&) 
    4349try 
     
    6167  nif.origID = mdp.d_header.id; 
    6268 
    63   cerr<<"External notification received for: "<< nif.domain << endl; 
     69 
    6470 
    6571  if(mdp.d_header.opcode != Opcode::Notify || mdp.d_qtype != QType::SOA) { 
    66     cerr<<"Opcode: "<<mdp.d_header.opcode<<", != notify\n"
     72    syslogFmt(boost::format("Received non-notification packet for domain '%s' from external nameserver %s") % nif.domain % nif.source.toStringWithPort())
    6773    return; 
    6874  } 
    69    
     75  syslogFmt(boost::format("External notification received for domain '%s' from %s") % nif.domain % nif.source.toStringWithPort());   
    7076  vector<uint8_t> outpacket; 
    7177  DNSPacketWriter pw(outpacket, mdp.d_qname, mdp.d_qtype, 1, Opcode::Notify); 
     
    8389catch(exception &e) 
    8490{ 
    85   cerr<<"Error parsing incoming packet: "<<e.what()<<endl
     91  syslogFmt(boost::format("Error parsing packet from external nameserver: %s") % e.what())
    8692} 
    8793 
     
    105111  MOADNSParser mdp(packet); 
    106112 
    107   cerr<<"Inside notification response for: "<<mdp.d_qname<<endl; 
     113  //  cerr<<"Inside notification response for: "<<mdp.d_qname<<endl; 
    108114 
    109115  if(!g_nifs.count(mdp.d_header.id)) { 
    110     cerr<<"Response from inner PowerDNS with unknown ID "<<mdp.d_header.id<<endl
     116    syslogFmt(boost::format("Response from inner PowerDNS with unkown ID %1%") % (uint16_t)mdp.d_header.id)
    111117    return; 
    112118  } 
     
    115121 
    116122  if(!iequals(nif.domain,mdp.d_qname)) { 
    117     cerr<<"Response from inner PowerDNS for different domain '"<<mdp.d_qname<<"' than original notification '"<<nif.domain<<"'"<<endl
     123    syslogFmt(boost::format("Response from inner nameserver for different domain '%s' than original notification '%s'") % mdp.d_qname % nif.domain)
    118124  } else { 
    119125    struct dnsheader dh; 
     
    122128     
    123129    if(sendto(nif.origSocket, buffer, len, 0, (sockaddr*) &nif.source, nif.source.getSocklen()) < 0) { 
    124       throw runtime_error("Unable to send notify to PowerDNS: "+stringerror()); 
     130      syslogFmt(boost::format("Unable to send notification response to external nameserver %s - %s") % nif.source.toStringWithPort() % stringerror()); 
    125131    } 
     132    else 
     133      syslogFmt(boost::format("Sent notification response to external nameserver %s for domain '%s'") % nif.source.toStringWithPort() % nif.domain); 
    126134  } 
    127135  g_nifs.erase(mdp.d_header.id); 
     
    130138catch(exception &e) 
    131139{ 
    132   cerr<<"Error parsing incoming packet: "<<e.what()<<endl
     140  syslogFmt(boost::format("Error parsing packet from internal nameserver: %s") % e.what())
    133141} 
    134142 
     
    138146  for(nifs_t::iterator iter = g_nifs.begin(); iter != g_nifs.end(); ) { 
    139147    if(iter->second.resentTime < limit) { 
    140       cerr<<"Removing notification proxy entry for '"<<iter->second.domain<<"', expired"<<endl
     148      syslogFmt(boost::format("Notification for domain '%s' was sent to inner nameserver, but no response within 10 seconds") % iter->second.domain)
    141149      g_nifs.erase(iter++); 
    142150    } 
     
    151159try 
    152160{ 
     161  openlog("nproxy", LOG_NDELAY | LOG_PID, LOG_DAEMON); 
     162 
    153163  po::options_description desc("Allowed options"); 
    154164  desc.add_options() 
    155165    ("help,h", "produce help message") 
    156166    ("powerdns-address", po::value<string>(), "IP address of PowerDNS server") 
     167    ("chroot", po::value<string>(), "chroot to this directory for additional security") 
     168    ("setuid", po::value<int>(), "setuid to this numerical user id") 
     169    ("setgid", po::value<int>(), "setgid to this numerical user id") 
    157170    ("origin-address", po::value<string>()->default_value("::"), "Source address for notifications to PowerDNS") 
    158171    ("listen-address", po::value<vector<string> >(), "IP addresses to listen on") 
     
    185198  // create sockets to listen on 
    186199   
    187   cerr<<"Binding sockets\n"
     200  syslogFmt(boost::format("Starting up"))
    188201  for(vector<string>::const_iterator address = addresses.begin(); address != addresses.end(); ++address) { 
    189202    ComboAddress local(*address, 53); 
     
    196209 
    197210    g_fdm.addReadFD(sock, handleOutsideUDPPacket); // add to fdmultiplexer for each socket 
     211    syslogFmt(boost::format("Listening for external notifications on address %s") % local.toStringWithPort()); 
    198212  } 
    199213 
     
    211225  ComboAddress pdns(g_vm["powerdns-address"].as<string>(), 53); 
    212226  if(connect(g_pdnssocket, (struct sockaddr*) &pdns, pdns.getSocklen()) < 0)  
    213     throw runtime_error("Failed to connect PowerDNS socket to address "+pdns.toString()+": "+stringerror()); 
     227    throw runtime_error("Failed to connect PowerDNS socket to address "+pdns.toStringWithPort()+": "+stringerror()); 
     228 
     229  syslogFmt(boost::format("Sending notifications to internal address %s") % pdns.toStringWithPort()); 
    214230 
    215231  g_fdm.addReadFD(g_pdnssocket, handleInsideUDPPacket); 
     
    218234    if(chroot(g_vm["chroot"].as<string>().c_str()) < 0) 
    219235      throw runtime_error("while chrooting to "+g_vm["chroot"].as<string>()); 
     236    syslogFmt(boost::format("Changed root to directory '%s'") % g_vm["chroot"].as<string>()); 
     237  } 
     238 
     239  if(g_vm.count("setuid")) { 
     240    if(setuid(g_vm["setuid"].as<int>()) < 0) 
     241      throw runtime_error("while changing uid to "+g_vm["setuid"].as<int>()); 
     242    syslogFmt(boost::format("Changed uid to %d") % g_vm["setuid"].as<int>()); 
     243  } 
     244 
     245  if(g_vm.count("setgid")) { 
     246    if(setuid(g_vm["setgid"].as<int>()) < 0) 
     247      throw runtime_error("while changing gid to "+g_vm["setgid"].as<int>()); 
     248    syslogFmt(boost::format("Changed gid to %d") % g_vm["setgid"].as<int>()); 
    220249  } 
    221250 
    222251  if(g_vm["daemon"].as<bool>()) { 
     252    syslogFmt(boost::format("Daemonizing")); 
    223253    daemonize(); 
    224254  } 
     255  syslogFmt(boost::format("Program operational")); 
    225256 
    226257 
     
    234265  } 
    235266} 
     267catch(boost::program_options::error& e)  
     268{ 
     269  syslogFmt(boost::format("Error parsing command line options: %s") % e.what()); 
     270} 
    236271catch(exception& e) 
    237272{ 
    238   cerr<<"Fatal: "<<e.what()<<endl
     273  syslogFmt(boost::format("Fatal: %s") % e.what())
    239274} 
    240275catch(AhuException& e) 
    241276{ 
    242   cerr<<"Fatal: "<<e.reason<<endl
     277  syslogFmt(boost::format("Fatal: %s") % e.reason)
    243278} 
    244279