Example #1
0
 def _loadDHCPPacket(self, packet, result, inform=False):
     """
     Sets DHCP option fields based on values returned from the database.
     
     @type packet: L{libpydhcpserver.dhcp_packet.DHCPPacket}
     @param packet: The packet being updated.
     @type result: tuple(11)
     @param result: The value returned from the SQL broker.
     @type inform: bool
     @param inform: True if this is a response to a DHCPINFORM message.
     """
     (ip, hostname,
      gateway, subnet_mask, broadcast_address,
      domain_name, domain_name_servers, ntp_servers,
      lease_time, subnet, serial) = result
     
     #Core parameters.
     if not inform:
         if not packet.setOption('yiaddr', ipToList(ip)):
             _logInvalidValue('ip', ip, subnet, serial)
         if not packet.setOption('ip_address_lease_time', longToList(int(lease_time))):
             _logInvalidValue('lease_time', lease_time, subnet, serial)
             
     #Default gateway, subnet mask, and broadcast address.
     if gateway:
         if not packet.setOption('router', ipToList(gateway)):
             _logInvalidValue('gateway', gateway, subnet, serial)
     if subnet_mask:
         if not packet.setOption('subnet_mask', ipToList(subnet_mask)):
             _logInvalidValue('subnet_mask', subnet_mask, subnet, serial)
     if broadcast_address:
         if not packet.setOption('broadcast_address', ipToList(broadcast_address)):
             _logInvalidValue('broadcast_address', broadcast_address, subnet, serial)
             
     #Domain details.
     if hostname:
         if not packet.setOption('hostname', strToList(hostname)):
             _logInvalidValue('hostname', hostname, subnet, serial)
     if domain_name:
         if not packet.setOption('domain_name', strToList(domain_name)):
             _logInvalidValue('domain_name', domain_name, subnet, serial)
     if domain_name_servers:
         if not packet.setOption('domain_name_servers', ipsToList(domain_name_servers)):
             _logInvalidValue('domain_name_servers', domain_name_servers, subnet, serial)
             
     #NTP servers.
     if ntp_servers:
         if not packet.setOption('ntp_servers', ipsToList(ntp_servers)):
             _logInvalidValue('ntp_servers', ntp_servers, subnet, serial)
Example #2
0
 def _handleDHCPLeaseQuery(self, packet, source_address, pxe):
     """
     Evaluates a DHCPLEASEQUERY request from a relay and determines whether
     a DHCPLEASEACTIVE or DHCPLEASEUNKNOWN 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, DHCPLEASEACTIVE is sent. Otherwise, DHCPLEASEUNKNOWN is
     sent.
     
     @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):
         return
         
     start_time = time.time()
     mac = None
     #noinspection PyBroadException
     try:
         mac = packet.getHardwareAddress()
     except:
         pass
     if not mac: #IP/client-ID-based lookup; not supported.
         self._logDiscardedPacket()
         return
     
     vlan = packet.getVlanNum()
     
     if not [None for (ignored_mac, timeout) in self._ignored_addresses if mac == ignored_mac]:
         if not self._logDHCPAccess(mac):
             self._logDiscardedPacket()
             return
             
         logging.writeLog('DHCPLEASEQUERY for %(mac)s on vlan %(vlan)s' % {
          'mac': mac,
          'vlan': vlan
         })
         
         try:
             result = self._sql_broker.lookupMAC(mac)
             if result:
                 packet.transformToDHCPLeaseActivePacket()
                 if packet.setOption('yiaddr', ipToList(result[0])):
                     self._sendDHCPPacket(packet, source_address, 'LEASEACTIVE', mac, result[0])
                 else:
                     _logInvalidValue('ip', result[0], result[-2], result[-1])
             else:
                 packet.transformToDHCPLeaseUnknownPacket()
                 self._sendDHCPPacket(packet, source_address, 'LEASEUNKNOWN', mac, '?.?.?.?')
         except Exception, e:
             logging.sendErrorReport('Unable to respond for %(mac)s' % {'mac': mac,}, e)
Example #3
0
 def _sendDHCPPacket(self, packet, address, response_type, mac, client_ip, pxe):
     """
     Sends the given packet to the right destination based on its properties.
     
     If the request originated from a host that knows its own IP, the packet
     is transmitted via unicast; in the event of a relayed request, it is sent
     to the 'server port', rather than the 'client port', per RFC 2131.
     
     If it was picked up as a broadcast packet, it is sent to the local subnet
     via the same mechanism, but to the 'client port'.
     
     @type packet: L{libpydhcpserver.dhcp_packet.DHCPPacket}
     @param packet: The packet to be transmitted.
     @type address: tuple
     @param address: The address from which the packet was received:
         (host, port)
     @type response_type: basestring
     @param response_type: The DHCP subtype of this response: 'OFFER', 'ACK',
         'NAK'
     @type mac: basestring
     @param mac: The MAC of the client for which this packet is destined.
     @type client_ip: basestring
     @param client_ip: The IP being assigned to the client.
     @type pxe: bool
     @param pxe: True if the packet was received via the PXE port
     
     @rtype: int
     @return: The number of bytes transmitted.
     """
     ip = port = None
     if address[0] not in ('255.255.255.255', '0.0.0.0', ''): #Unicast.
         giaddr = packet.getOption("giaddr")
         if giaddr and not giaddr == [0,0,0,0]: #Relayed request.
             ip = '.'.join(map(str, giaddr))
             port = self._server_port
         else: #Request directly from client, routed or otherwise.
             ip = address[0]
             if pxe:
                 port = address[1] or self._client_port #BSD doesn't seem to preserve port information
             else:
                 port = self._client_port
     else: #Broadcast.
         ip = '255.255.255.255'
         port = self._client_port
         
     packet.setOption('server_identifier', ipToList(self._server_address))
     bytes = self._sendDHCPPacketTo(packet, ip, port, pxe)
     src.logging.writeLog('DHCP%(type)s sent to %(mac)s for %(client)s via %(ip)s:%(port)i %(pxe)s[%(bytes)i bytes]' % {
          'type': response_type,
          'mac': mac,
          'client': client_ip,
          'bytes': bytes,
          'ip': ip,
          'port': port,
          'pxe': pxe and '(PXE) ' or '',
     })
     return bytes
Example #4
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):
         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)
         
         vlan = packet.getVlanNum()
             
         logging.writeLog('DHCPINFORM from %(mac)s on vlan %(vlan)s' % {
             'mac': mac,
             'vlan': vlan
         })
         
         if not ciaddr:
             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()
                 vendor_options = packet.extractVendorOptions()
                 self._loadDHCPPacket(packet, result, True)
                 if conf.loadDHCPPacket(
                  packet,
                  mac, tuple(ipToList(result[0])), giaddr,
                  result[9], result[10],
                  pxe, vendor_options
                 ):
                     self._sendDHCPPacket(packet, source_address, 'ACK', mac, s_ciaddr)
                 else:
                     logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                      'mac': mac,
                     })
                     self._logDiscardedPacket()
             else:
                 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:
             logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)
Example #5
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):
         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)
         
         vlan = packet.getVlanNum()
         
         if sid and not ciaddr: #SELECTING
             if s_sid == self._server_address: #Chosen!
                 logging.writeLog('DHCPREQUEST:SELECTING from %(mac)s on vlan %(vlan)s' % {
                     'mac': mac,
                     'vlan': vlan
                 })
                 try:
                     result = self._sql_broker.lookupMAC(mac)
                     if result and (not ip or result[0] == s_ip):
                         packet.transformToDHCPAckPacket()
                         vendor_options = packet.extractVendorOptions()
                         self._loadDHCPPacket(packet, result)
                         if conf.loadDHCPPacket(
                          packet,
                          mac, tuple(ipToList(result[0])), giaddr,
                          result[9], result[10],
                          pxe, vendor_options
                         ):
                             self._sendDHCPPacket(packet, source_address, 'ACK', mac, s_ip)
                         else:
                             logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                              'mac': mac,
                             })
                             self._logDiscardedPacket()
                     else:
                         packet.transformToDHCPNackPacket()
                         self._sendDHCPPacket(packet, source_address, 'NAK', mac, 'NO-MATCH')
                 except Exception, e:
                     logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)
             else:
                 self._logDiscardedPacket()
         elif not sid and not ciaddr and ip: #INIT-REBOOT
             logging.writeLog('DHCPREQUEST:INIT-REBOOT from %(mac)s on vlan %(vlan)s' % {
                 'mac': mac,
                 'vlan': vlan
             })
             try:
                 result = self._sql_broker.lookupMAC(mac)
                 if result and result[0] == s_ip:
                     packet.transformToDHCPAckPacket()
                     vendor_options = packet.extractVendorOptions()
                     self._loadDHCPPacket(packet, result)
                     if conf.loadDHCPPacket(
                      packet,
                      mac, tuple(ip), giaddr,
                      result[9], result[10],
                      pxe, vendor_options
                     ):
                         self._sendDHCPPacket(packet, source_address, 'ACK', mac, s_ip)
                     else:
                         logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                          'mac': mac,
                         })
                         self._logDiscardedPacket()
                 else:
                     packet.transformToDHCPNackPacket()
                     self._sendDHCPPacket(packet, source_address, 'NAK', mac, s_ip)
             except Exception, e:
                 logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)
Example #6
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.
        """
        #assert False
        from conf import PKT_CAPT

        PKT_CAPT.append((packet, source_address, pxe))

        if not self._evaluateRelay(packet):
            return

        start_time = time.time()
        mac = packet.getHardwareAddress()
        vlan = packet.getVlanNum()
        if not [None for (ignored_mac, timeout) in self._ignored_addresses if mac == ignored_mac]:
            if not self._logDHCPAccess(mac):
                self._logDiscardedPacket()
                return
                
            logging.writeLog('DHCPDISCOVER from %(mac)s on vlan %(vlan)s' % {
             'mac': mac,
             'vlan': vlan
            })                
            
            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()
                    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, vendor_options
                    ):
                        if rapid_commit:
                            self._sendDHCPPacket(packet, source_address, 'ACK-rapid', mac, result[0])
                        else:
                            self._sendDHCPPacket(packet, source_address, 'OFFER', mac, result[0])
                    else:
                        logging.writeLog('Ignoring %(mac)s per loadDHCPPacket()' % {
                         'mac': mac,
                        })
                        self._logDiscardedPacket()
                else:
                    if conf.AUTHORITATIVE:
                        packet.transformToDHCPNackPacket()
                        self._sendDHCPPacket(packet, source_address, 'NAK', mac, '?.?.?.?')
                    else:
                        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:
                logging.sendErrorReport('Unable to respond to %(mac)s' % {'mac': mac,}, e)