Beispiel #1
0
 def _handleDHCPInform(self, packet, source_address, pxe):
     """
     Evaluates a DHCPINFORM request from a client and determines whether a
     DHCPACK should be sent.
     
     The logic here is to make sure the MAC isn't ignored or acting
     maliciously, then check the database to see whether it has an assigned
     IP. If it does, and the IP it thinks it has a right to matches this IP,
     then an ACK is sent, along with all relevant options; if not, the
     request is ignored.
     
     @type packet: L{libpydhcpserver.dhcp_packet.DHCPPacket}
     @param packet: The DHCPREQUEST to be evaluated.
     @type source_address: tuple
     @param source_address: The address (host, port) from which the request
         was received.
     @type pxe: bool
     @param pxe: True if the packet was received on the PXE port.
     """
     if not self._evaluateRelay(packet, pxe):
         return
         
     start_time = time.time()
     mac = packet.getHardwareAddress()
     if not [None for (ignored_mac, timeout) in self._ignored_addresses if mac == ignored_mac]:
         if not self._logDHCPAccess(mac):
             self._logDiscardedPacket()
             return
             
         ciaddr = packet.getOption("ciaddr")
         giaddr = packet.getOption("giaddr")
         s_ciaddr = '.'.join(map(str, ciaddr))
         if not ciaddr or ciaddr == [0,0,0,0]:
             ciaddr = None
         if not giaddr or giaddr == [0,0,0,0]:
             giaddr = None
         else:
             giaddr = tuple(giaddr)
             
         src.logging.writeLog('DHCPINFORM from %(mac)s' % {
          'mac': mac,
         })
         
         if not ciaddr:
             src.logging.writeLog('%(mac)s sent malformed packet; ignoring for %(time)i seconds' % {
              'mac': mac,
              'time': conf.UNAUTHORIZED_CLIENT_TIMEOUT,
             })
             self._stats_lock.acquire()
             self._ignored_addresses.append([mac, conf.UNAUTHORIZED_CLIENT_TIMEOUT])
             self._stats_lock.release()
             self._logDiscardedPacket()
             return
             
         try:
             result = self._sql_broker.lookupMAC(mac)
             if result:
                 packet.transformToDHCPAckPacket()
                 pxe_options = packet.extractPXEOptions()
                 vendor_options = packet.extractVendorOptions()
                 self._loadDHCPPacket(packet, result, True)
                 if conf.loadDHCPPacket(
                  packet,
                  mac, tuple(ipToList(result[0])), giaddr,
                  result[9], result[10],
                  pxe and pxe_options, vendor_options
                 ):
                     self._sendDHCPPacket(packet, source_address, 'ACK', mac, s_ciaddr, pxe)
                 else:
                     src.logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                      'mac': mac,
                     })
                     self._logDiscardedPacket()
             else:
                 src.logging.writeLog('%(mac)s unknown; ignoring for %(time)i seconds' % {
                  'mac': mac,
                  'time': conf.UNAUTHORIZED_CLIENT_TIMEOUT,
                 })
                 self._stats_lock.acquire()
                 self._ignored_addresses.append([mac, conf.UNAUTHORIZED_CLIENT_TIMEOUT])
                 self._stats_lock.release()
                 self._logDiscardedPacket()
         except Exception, e:
             src.logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)
Beispiel #2
0
 def _handleDHCPDiscover(self, packet, source_address, pxe):
     """
     Evaluates a DHCPDISCOVER request from a client and determines whether a
     DHCPOFFER should be sent.
     
     The logic here is to make sure the MAC isn't ignored or acting
     maliciously, then check the database to see whether it has an assigned
     IP. If it does, that IP is offered, along with all relevant options; if
     not, the MAC is ignored to mitigate spam from follow-up DHCPDISCOVERS.
     
     @type packet: L{libpydhcpserver.dhcp_packet.DHCPPacket}
     @param packet: The DHCPDISCOVER to be evaluated.
     @type source_address: tuple
     @param source_address: The address (host, port) from which the request
         was received.
     @type pxe: bool
     @param pxe: True if the packet was received on the PXE port.
     """
     if not self._evaluateRelay(packet, pxe):
         return
         
     start_time = time.time()
     mac = packet.getHardwareAddress()
     if not [None for (ignored_mac, timeout) in self._ignored_addresses if mac == ignored_mac]:
         if not self._logDHCPAccess(mac):
             self._logDiscardedPacket()
             return
             
         src.logging.writeLog('DHCPDISCOVER from %(mac)s' % {
          'mac': mac,
         })
         
         try:
             result = self._sql_broker.lookupMAC(mac)
             if result:
                 rapid_commit = not packet.getOption('rapid_commit') is None
                 if rapid_commit:
                     packet.transformToDHCPAckPacket()
                     packet.forceOption('rapid_commit', [])
                 else:
                     packet.transformToDHCPOfferPacket()
                 pxe_options = packet.extractPXEOptions()
                 vendor_options = packet.extractVendorOptions()
                     
                 self._loadDHCPPacket(packet, result)
                 giaddr = packet.getOption("giaddr")
                 if not giaddr or giaddr == [0,0,0,0]:
                     giaddr = None
                 else:
                     giaddr = tuple(giaddr)
                 if conf.loadDHCPPacket(
                  packet,
                  mac, tuple(ipToList(result[0])), giaddr,
                  result[9], result[10],
                  pxe and pxe_options, vendor_options
                 ):
                     if rapid_commit:
                         self._sendDHCPPacket(packet, source_address, 'ACK-rapid', mac, result[0], pxe)
                     else:
                         self._sendDHCPPacket(packet, source_address, 'OFFER', mac, result[0], pxe)
                 else:
                     src.logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                      'mac': mac,
                     })
                     self._logDiscardedPacket()
             else:
                 if conf.AUTHORITATIVE:
                     packet.transformToDHCPNackPacket()
                     self._sendDHCPPacket(packet, source_address, 'NAK', mac, '?.?.?.?', pxe)
                 else:
                     src.logging.writeLog('%(mac)s unknown; ignoring for %(time)i seconds' % {
                      'mac': mac,
                      'time': conf.UNAUTHORIZED_CLIENT_TIMEOUT,
                     })
                     self._stats_lock.acquire()
                     self._ignored_addresses.append([mac, conf.UNAUTHORIZED_CLIENT_TIMEOUT])
                     self._stats_lock.release()
         except Exception, e:
             src.logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)
Beispiel #3
0
 def _handleDHCPRequest(self, packet, source_address, pxe):
     """
     Evaluates a DHCPREQUEST request from a client and determines whether a
     DHCPACK should be sent.
     
     The logic here is to make sure the MAC isn't ignored or acting
     maliciously, then check the database to see whether it has an assigned
     IP. If it does, and the IP it thinks it has a right to matches this IP,
     then an ACK is sent, along with all relevant options; if not, a DHCPNAK
     is sent to inform the client that it is not allowed to use the requested
     IP, forcing it to DISCOVER a new one.
     
     If policy forbids RENEW and REBIND operations, perhaps to prepare for a
     new configuration rollout, all such requests are NAKed immediately.
     
     @type packet: L{libpydhcpserver.dhcp_packet.DHCPPacket}
     @param packet: The DHCPREQUEST to be evaluated.
     @type source_address: tuple
     @param source_address: The address (host, port) from which the request
         was received.
     @type pxe: bool
     @param pxe: True if the packet was received on the PXE port.
     """
     if not self._evaluateRelay(packet, pxe):
         return
         
     start_time = time.time()
     mac = packet.getHardwareAddress()
     if not [None for (ignored_mac, timeout) in self._ignored_addresses if mac == ignored_mac]:
         if not self._logDHCPAccess(mac):
             self._logDiscardedPacket()
             return
             
         ip = packet.getOption("requested_ip_address")
         sid = packet.getOption("server_identifier")
         ciaddr = packet.getOption("ciaddr")
         giaddr = packet.getOption("giaddr")
         s_ip = ip and '.'.join(map(str, ip))
         s_sid = sid and '.'.join(map(str, sid))
         s_ciaddr = ciaddr and '.'.join(map(str, ciaddr))
         
         if not ip or ip == [0,0,0,0]:
             ip = None
         if not sid or sid == [0,0,0,0]:
             sid = None
         if not ciaddr or ciaddr == [0,0,0,0]:
             ciaddr = None
         if not giaddr or giaddr == [0,0,0,0]:
             giaddr = None
         else:
             giaddr = tuple(giaddr)
             
         if sid and not ciaddr: #SELECTING
             if s_sid == self._server_address: #Chosen!
                 src.logging.writeLog('DHCPREQUEST:SELECTING from %(mac)s' % {
                  'mac': mac,
                 })
                 try:
                     result = self._sql_broker.lookupMAC(mac)
                     if result and (not ip or result[0] == s_ip):
                         packet.transformToDHCPAckPacket()
                         pxe_options = packet.extractPXEOptions()
                         vendor_options = packet.extractVendorOptions()
                         self._loadDHCPPacket(packet, result)
                         if conf.loadDHCPPacket(
                          packet,
                          mac, tuple(ipToList(result[0])), giaddr,
                          result[9], result[10],
                          pxe and pxe_options, vendor_options
                         ):
                             self._sendDHCPPacket(packet, source_address, 'ACK', mac, s_ip, pxe)
                         else:
                             src.logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                              'mac': mac,
                             })
                             self._logDiscardedPacket()
                     else:
                         packet.transformToDHCPNackPacket()
                         self._sendDHCPPacket(packet, source_address, 'NAK', mac, 'NO-MATCH', pxe)
                 except Exception, e:
                     src.logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)
             else:
                 self._logDiscardedPacket()
         elif not sid and not ciaddr and ip: #INIT-REBOOT
             src.logging.writeLog('DHCPREQUEST:INIT-REBOOT from %(mac)s' % {
              'mac': mac,
             })
             try:
                 result = self._sql_broker.lookupMAC(mac)
                 if result and result[0] == s_ip:
                     packet.transformToDHCPAckPacket()
                     pxe_options = packet.extractPXEOptions()
                     vendor_options = packet.extractVendorOptions()
                     self._loadDHCPPacket(packet, result)
                     if conf.loadDHCPPacket(
                      packet,
                      mac, tuple(ip), giaddr,
                      result[9], result[10],
                      pxe and pxe_options, vendor_options
                     ):
                         self._sendDHCPPacket(packet, source_address, 'ACK', mac, s_ip, pxe)
                     else:
                         src.logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                          'mac': mac,
                         })
                         self._logDiscardedPacket()
                 else:
                     packet.transformToDHCPNackPacket()
                     self._sendDHCPPacket(packet, source_address, 'NAK', mac, s_ip, pxe)
             except Exception, e:
                 src.logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)
Beispiel #4
0
 else:
     src.logging.writeLog('DHCPREQUEST:REBIND from %(mac)s' % {
      'mac': mac,
     })
     
 try:
     result = self._sql_broker.lookupMAC(mac)
     if result and result[0] == s_ciaddr:
         packet.transformToDHCPAckPacket()
         pxe_options = packet.extractPXEOptions()
         vendor_options = packet.extractVendorOptions()
         packet.setOption('yiaddr', ciaddr)
         self._loadDHCPPacket(packet, result)
         if conf.loadDHCPPacket(
          packet,
          mac, tuple(ciaddr), giaddr,
          result[9], result[10],
          pxe and pxe_options, vendor_options
         ):
             self._sendDHCPPacket(packet, (s_ciaddr, 0), 'ACK', mac, s_ciaddr, pxe)
         else:
             src.logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
              'mac': mac,
             })
             self._logDiscardedPacket()
     else:
         if renew:
             packet.transformToDHCPNackPacket()
             self._sendDHCPPacket(packet, (s_ciaddr, 0), 'NAK', mac, s_ciaddr, pxe)
         else:
             self._logDiscardedPacket()
 except Exception, e: