Advertisement
Guest User

Untitled

a guest
Aug 15th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import re
  5. import sys
  6. import sqlalchemy
  7. from decorator import decorator
  8. import radiusd
  9. import bitstring
  10. import ipaddress
  11. from _sql import *
  12. from _params import *
  13.  
  14. engine = None
  15. RADIUS_TYPE_MAP = (
  16.     (re.compile('^"(.*)"$'), lambda m: m.group(1)),
  17.     (re.compile('^(-?\d+)$'), lambda m: int(m.group(1))),)
  18.  
  19.  
  20. def radius_value(value_string):
  21.     if type(value_string) in (str, unicode):
  22.         for regexp, processor in RADIUS_TYPE_MAP:
  23.             m = regexp.match(value_string)
  24.             if m is not None:
  25.                 return processor(m)
  26.     return value_string
  27.  
  28.  
  29. @decorator
  30. def radius_function(f, params):
  31.     data = dict()
  32.     for name, value in params:
  33.         data[name] = radius_value(value)
  34.     try:
  35.         data = f(data)
  36.     except:
  37.         return radiusd.RLM_MODULE_FAIL
  38.     if type(data) is tuple:
  39.         res = (data[0],) + tuple(map(lambda d: tuple(d.iteritems()), data[1:]))
  40.     else:
  41.         res = data
  42.     return res
  43.  
  44.  
  45. @radius_function
  46. def attibutes(p):
  47.     return p
  48.  
  49.  
  50. def log(level, s):
  51.     radiusd.radlog(level, s)
  52.    
  53.                
  54. def instantiate(p):
  55.     global engine
  56.     engine = sqlalchemy.create_engine('mysql://root:xcxcxc@2510@localhost:3306/UTM5',pool_size=100, max_overflow=10)
  57.     log(radiusd.L_INFO, 'db connection: ' + str(engine))
  58.    
  59.  
  60. def authorize(p):
  61.     print "*** authorize ***"
  62.     print p
  63.     return radiusd.RLM_MODULE_OK
  64.            
  65.  
  66. def accounting(p):
  67.     print "*** accounting ***"
  68.     print p
  69.     return radiusd.RLM_MODULE_OK
  70.            
  71.  
  72. def pre_proxy(p):
  73.     print "*** pre_proxy ***"
  74.     print p
  75.     return radiusd.RLM_MODULE_OK
  76.  
  77.          
  78. def post_proxy(p):
  79.     print "*** post_proxy ***"
  80.     print p
  81.     return radiusd.RLM_MODULE_OK        
  82.  
  83.  
  84. def post_auth(p):
  85.     try:
  86.     dbHandle = engine.connect()
  87.     except sqlalchemy.exc.DatabaseError, e:
  88.     log(radiusd.L_ERR, str(e))
  89.         return radiusd.RLM_MODULE_FAIL
  90.                                      
  91.     p = attibutes(p)    
  92.    
  93.     msgtype = p.get('DHCP-Message-Type', None)    
  94.     chaddr = p.get('DHCP-Client-Hardware-Address', None)
  95.     vendorid = p.get('DHCP-Vendor-Class-Identifier', None)
  96.     xid = hex(int(p.get('DHCP-Transaction-Id', None)))
  97.     cid = p.get('DHCP-Client-Identifier',None)
  98.     rip = p.get('DHCP-Requested-IP-Address', None) if p.get('DHCP-Requested-IP-Address', None) != '0.0.0.0' else None
  99.     sid = p.get('DHCP-DHCP-Server-Identifier', None) if p.get('DHCP-DHCP-Server-Identifier', None) != '0.0.0.0' else None
  100.     cip = p.get('DHCP-Client-IP-Address', None) if p.get('DHCP-Client-IP-Address', None) != '0.0.0.0' else None    
  101.    
  102.     if msgtype == "DHCP-Discover":
  103.     if ('DHCP-Relay-Remote-Id' in p) and ('DHCP-Relay-Circuit-Id' in p):
  104.             vlan = bitstring.BitArray(p.get('DHCP-Relay-Circuit-Id', None))[16:-16].int
  105.             port = bitstring.BitArray(p.get('DHCP-Relay-Circuit-Id', None))[40:].int
  106.             switch = bitstring.BitArray(p.get('DHCP-Relay-Remote-Id', None))[16:].bytes                
  107.             remote_id = ('DHCP-Relay-Remote-Id', '{0}'.format(p.get('DHCP-Relay-Remote-Id', None)))
  108.             circuit_id = ('DHCP-Relay-Circuit-Id', '{0}'.format(p.get('DHCP-Relay-Circuit-Id', None)))
  109.        
  110.         res = dbHandle.execute(select_mac.format(switch,chaddr)).fetchall()
  111.        
  112.         if res:
  113.         if len(res) == 1:
  114.             nets = [n for n in res][0]
  115.             ipnet = ipaddress.ip_address(u"{0}".format(nets['ip'])) in ipaddress.ip_network(u'{0}/{1}'.format(nets['net'],nets['mask']))
  116.             if ipnet == True:
  117.             log(radiusd.L_INFO, DHCP_Discover.format(xid,switch,port,vlan,chaddr,vendorid))
  118.            
  119.             mask = ('DHCP-Subnet-Mask', '{0}'.format(nets['mask']))
  120.             router = ('DHCP-Router-Address', '{0}'.format(nets['gw']))
  121.             yiaddr = ('DHCP-Your-IP-Address', '{0}'.format(nets['ip']))
  122.            
  123.             log(radiusd.L_INFO, DHCP_Offer.format(xid,switch,port,vlan,chaddr,yiaddr[1],vendorid))
  124.             if vendorid == 'synet':
  125.                 return (radiusd.RLM_MODULE_OK, (circuit_id,remote_id,ns1,ns2,ntp,mask,router,msgoffer,lease,serverid,domain,yiaddr,synet),())
  126.             else:
  127.                 return (radiusd.RLM_MODULE_OK, (circuit_id,remote_id,ns1,ns2,ntp,mask,router,msgoffer,lease,serverid,domain,yiaddr),())
  128.            
  129.             else:
  130.             log(radiusd.L_INFO, DHCP_NAK_error_ip_net.format(xid,switch,port,vlan,chadddr,ciaddr,vendorid))
  131.                     return radiusd.RLM_MODULE_NOTFOUND
  132.                 else:
  133.                     log(radiusd.L_INFO, DHCP_Discover_duble_mac.format(xid,switch,port,vlan,chaddr,vendorid))
  134.                     return radiusd.RLM_MODULE_NOOP
  135.                                                                        
  136.         else:
  137.         log(radiusd.L_INFO, DHCP_Discover_free.format(xid,switch,port,vlan,chaddr,vendorid))
  138.         return radiusd.RLM_MODULE_NOOP
  139.     else:
  140.         log(radiusd.L_INFO, DHCP_Discover_not82.format(xid,chaddr,vendorid))
  141.         return radiusd.RLM_MODULE_NOOP
  142.                                    
  143.            
  144.     elif msgtype == "DHCP-Request":
  145.     if sid and not cip: #SELECTING
  146.         if ('DHCP-Relay-Remote-Id' in p) and ('DHCP-Relay-Circuit-Id' in p):
  147.         vlan = bitstring.BitArray(p.get('DHCP-Relay-Circuit-Id', None))[16:-16].int
  148.         port = bitstring.BitArray(p.get('DHCP-Relay-Circuit-Id', None))[40:].int
  149.         switch = bitstring.BitArray(p.get('DHCP-Relay-Remote-Id', None))[16:].bytes
  150.         remote_id = ('DHCP-Relay-Remote-Id', '{0}'.format(p.get('DHCP-Relay-Remote-Id', None)))
  151.         circuit_id = ('DHCP-Relay-Circuit-Id', '{0}'.format(p.get('DHCP-Relay-Circuit-Id', None)))
  152.                                                        
  153.         res = dbHandle.execute(select_ip.format(switch,rip)).fetchall()
  154.        
  155.         if res:
  156.             if len(res) == 1:
  157.             nets = [n for n in res][0]
  158.             ipnet = ipaddress.ip_address(u"{0}".format(nets['ip'])) in ipaddress.ip_network(u'{0}/{1}'.format(nets['net'],nets['mask']))
  159.             if ipnet == True:
  160.                 mask = ('DHCP-Subnet-Mask', '{0}'.format(nets['mask']))
  161.                 router = ('DHCP-Router-Address', '{0}'.format(nets['gw']))
  162.                 yiaddr = ('DHCP-Your-IP-Address', '{0}'.format(nets['ip']))
  163.                
  164.                 log(radiusd.L_INFO, DHCP_Request_select.format(xid,switch,port,vlan,chaddr,yiaddr[1],vendorid))
  165.                 log(radiusd.L_INFO, DHCP_ASK_select.format(xid,switch,port,vlan,chaddr,yiaddr[1],vendorid))            
  166.                 if vendorid == 'synet':
  167.                 return (radiusd.RLM_MODULE_OK, (circuit_id,remote_id,ns1,ns2,ntp,mask,router,msgask,lease,serverid,domain,yiaddr,synet),())
  168.                 else:
  169.                 return (radiusd.RLM_MODULE_OK, (circuit_id,remote_id,ns1,ns2,ntp,mask,router,msgask,lease,serverid,domain,yiaddr),())
  170.             else:
  171.                 log(radiusd.L_INFO, DHCP_NAK_error_ip_net.format(xid,switch,port,vlan,chaddr,reqipaddr,vendorid))
  172.                             return radiusd.RLM_MODULE_NOTFOUND
  173.                     else:
  174.             log(radiusd.L_INFO, DHCP_NAK_duble.format(xid,switch,port,vlan,chaddr,reqipaddr,vendorid))
  175.                         return radiusd.RLM_MODULE_NOTFOUND
  176.                 else:
  177.                     log(radiusd.L_INFO, DHCP_NAK_req_free.format(xid,switch,port,vlan,chaddr,reqipaddr,vendorid))
  178.                     return radiusd.RLM_MODULE_NOTFOUND                        
  179.                                                
  180.  
  181.     elif not sid and not cip and rip: #INIT-REBOOT
  182.         print "#INIT-REBOOT"
  183.  
  184.     elif not sid and cip and not rip: #RENEWING or REBINDING
  185.         print "#RENEWING or REBINDING"     
  186.    
  187.     else:
  188.         return radiusd.RLM_MODULE_NOOP
  189.  
  190.        
  191.     elif msgtype == "DHCP-Release":
  192.     return radiusd.RLM_MODULE_NOOP
  193.  
  194.    
  195.     elif msgtype == "DHCP-Inform":
  196.     return radiusd.RLM_MODULE_NOOP
  197.  
  198.    
  199.     elif msgtype == "DHCP-Decline":
  200.     return radiusd.RLM_MODULE_NOOP
  201.  
  202. def recv_coa(p):
  203.     print "*** recv_coa ***"
  204.     print p
  205.     return radiusd.RLM_MODULE_OK
  206.  
  207.        
  208. def send_coa(p):
  209.     print "*** send_coa ***"
  210.     print p
  211.     return radiusd.RLM_MODULE_OK
  212.  
  213.    
  214. def detach(_p):
  215.     print "*** detach ***"
  216.     return radiusd.RLM_MODULE_OK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement