Esempio n. 1
0
def acct(host,port,acct_type,username,secret,
    session_id,nas_ip,user_ip):
    """ radius acct testing """
    _dict =dictionary.Dictionary(os.path.join(os.path.dirname(client.__file__), "dictionary/dictionary"))
    req = {'User-Name':username}
    req['Acct-Status-Type'] = ACCT_TYPE_MAP[acct_type]
    req['Acct-Session-Id'] = session_id
    req["Acct-Output-Octets"]  =  4096
    req["Acct-Input-Octets"]  =  1024 
    req['Acct-Session-Time'] = int(time.time())%1999
    req["NAS-IP-Address"]     = nas_ip
    req["NAS-Port-Id"]     = '3/0/1:0.0'
    req["NAS-Port"]           = 0
    req["Service-Type"]       = "Login-User"
    req["NAS-Identifier"]     = "trtest"
    req["Called-Station-Id"]  = random_mac()
    req["Calling-Station-Id"] = random_mac()
    req["Framed-IP-Address"]  = user_ip
    print "radius acct: ",repr(req)
    def onresp(r):
        print message.format_packet_str(r)
        reactor.stop()

    sendacct = lambda : client.send_acct(str(secret), _dict, host, acctport=port, debug=True,**req).addCallback(onresp)
    reactor.callLater(0.01, sendacct)
    reactor.run()
Esempio n. 2
0
def acct(host, port, acct_type, username, secret, session_id, nas_ip, user_ip):
    """ radius acct testing """
    _dict = dictionary.Dictionary(
        os.path.join(os.path.dirname(client.__file__),
                     "dictionary/dictionary"))
    req = {'User-Name': username}
    req['Acct-Status-Type'] = ACCT_TYPE_MAP[acct_type]
    req['Acct-Session-Id'] = session_id
    req["Acct-Output-Octets"] = 4096
    req["Acct-Input-Octets"] = 1024
    req['Acct-Session-Time'] = int(time.time()) % 1999
    req["NAS-IP-Address"] = nas_ip
    req["NAS-Port-Id"] = '3/0/1:0.0'
    req["NAS-Port"] = 0
    req["Service-Type"] = "Login-User"
    req["NAS-Identifier"] = "trtest"
    req["Called-Station-Id"] = random_mac()
    req["Calling-Station-Id"] = random_mac()
    req["Framed-IP-Address"] = user_ip
    print "radius acct: ", repr(req)

    def onresp(r):
        print message.format_packet_str(r)
        reactor.stop()

    sendacct = lambda: client.send_acct(
        str(secret), _dict, host, acctport=port, debug=True, **req
    ).addCallback(onresp)
    reactor.callLater(0.01, sendacct)
    reactor.run()
Esempio n. 3
0
def accounting(dbfile, config):
    ''' update radius accounting
    '''
    try:
        nas_id = config.get('DEFAULT', 'nas_id')
        nas_addr = config.get('DEFAULT', 'nas_addr')
        secret = config.get('DEFAULT', 'radius_secret')
        radius_addr = config.get('DEFAULT', 'radius_addr')
        radius_acct_port = config.getint('DEFAULT', 'radius_acct_port')
        radius_timeout = config.getint('DEFAULT', 'radius_timeout')
        status_dbfile = config.get('DEFAULT', 'statusdb')

        clients = statusdb.query_client(status_dbfile)

        ctime = int(time.time())
        for cli in clients:
            if (ctime - int(cli['uptime'])) < int(cli['acct_interval']):
                continue

            session_id = cli['session_id']
            req = {'User-Name': cli['username']}
            req['Acct-Status-Type'] = ACCT_UPDATE
            req['Acct-Session-Id'] = session_id
            req["Acct-Output-Octets"] = int(cli['outbytes'])
            req["Acct-Input-Octets"] = int(cli['inbytes'])
            req['Acct-Session-Time'] = (ctime - int(cli['ctime']))
            req["NAS-IP-Address"] = nas_addr
            req["NAS-Port-Id"] = '0/0/0:0.0'
            req["NAS-Port"] = 0
            req["Service-Type"] = "Login-User"
            req["NAS-Identifier"] = nas_id
            req["Called-Station-Id"] = '00:00:00:00:00:00'
            req["Calling-Station-Id"] = '00:00:00:00:00:00'
            req["Framed-IP-Address"] = cli['userip']

            def update_uptime(radresp):
                statusdb.update_client_uptime(status_dbfile, session_id)
                log.msg('online<%s> client accounting update' % session_id)

            def onresp(r):
                try:
                    update_uptime(r)
                except Exception as e:
                    log.err('online update uptime error')
                    log.err(e)

            d = client.send_acct(str(secret),
                                 get_dictionary(),
                                 radius_addr,
                                 acctport=radius_acct_port,
                                 debug=True,
                                 **req)
            d.addCallbacks(onresp, log.err)

    except Exception, e:
        log.err('accounting error')
        log.err(e)
Esempio n. 4
0
def cli(conf):
    """ OpenVPN client_disconnect method
    """
    config = init_config(conf)
    nas_id = config.get('DEFAULT', 'nas_id')
    secret = config.get('DEFAULT', 'radius_secret')
    nas_addr = config.get('DEFAULT', 'nas_addr')
    radius_addr = config.get('DEFAULT', 'radius_addr')
    radius_acct_port = config.getint('DEFAULT', 'radius_acct_port')
    radius_timeout = config.getint('DEFAULT', 'radius_timeout')
    status_dbfile = config.get('DEFAULT', 'statusdb')

    username = os.environ.get('username')
    userip = os.environ.get('ifconfig_pool_remote_ip')
    realip = os.environ.get('trusted_ip')
    realport = os.environ.get('trusted_port')
    session_id = md5(nas_addr + realip + realport).hexdigest()

    req = {'User-Name':username}
    req['Acct-Status-Type'] = ACCT_STOP
    req['Acct-Session-Id'] = session_id
    req["Acct-Output-Octets"]  =  0
    req["Acct-Input-Octets"]  =  0 
    req['Acct-Session-Time'] = 0
    req["NAS-IP-Address"]     = nas_addr
    req["NAS-Port-Id"]     = '0/0/0:0.0'
    req["NAS-Port"]           = 0
    req["Service-Type"]       = "Login-User"
    req["NAS-Identifier"]     = nas_id
    req["Called-Station-Id"]  = '00:00:00:00:00:00'
    req["Calling-Station-Id"] = '00:00:00:00:00:00'
    req["Framed-IP-Address"]  = userip
 
    def shutdown(exitcode=0):
        reactor.addSystemEventTrigger('after', 'shutdown', os._exit,exitcode)
        reactor.stop()

    def onresp(r):
        try:
            statusdb.del_client(status_dbfile,session_id)
            log.msg('delete online<%s> client from db'%session_id)
        except Exception as e:
            log.err('del client online error')
            log.err(e)        
        shutdown(0)

    def onerr(e):
        log.err(e)
        shutdown(1)

    d = client.send_acct(str(secret), get_dictionary(), radius_addr, 
        acctport=radius_acct_port, debug=True,**req)
    d.addCallbacks(onresp,onerr)
    reactor.callLater(radius_timeout,shutdown,1)
    reactor.run()    
Esempio n. 5
0
def cli(conf):
    """ OpenVPN client_connect method
    """
    config = init_config(conf)
    nas_id = config.get('DEFAULT', 'nas_id')
    nas_addr = config.get('DEFAULT', 'nas_addr')
    secret = config.get('DEFAULT', 'radius_secret')
    radius_addr = config.get('DEFAULT', 'radius_addr')
    radius_acct_port = config.getint('DEFAULT', 'radius_acct_port')
    radius_timeout = config.getint('DEFAULT', 'radius_timeout')
    acct_interval = config.getint('DEFAULT', 'acct_interval')
    session_timeout = config.getint('DEFAULT', 'session_timeout')
    status_dbfile = config.get('DEFAULT', 'statusdb')

    username = os.environ.get('username')
    userip = os.environ.get('ifconfig_pool_remote_ip')
    realip = os.environ.get('trusted_ip')
    realport = os.environ.get('trusted_port')
    session_id = md5(nas_addr + realip + realport).hexdigest()

    req = {'User-Name': username}
    req['Acct-Status-Type'] = ACCT_START
    req['Acct-Session-Id'] = session_id
    req["Acct-Output-Octets"] = 0
    req["Acct-Input-Octets"] = 0
    req['Acct-Session-Time'] = 0
    req["NAS-IP-Address"] = nas_addr
    req["NAS-Port-Id"] = '0/0/0:0.0'
    req["NAS-Port"] = 0
    req["Service-Type"] = "Login-User"
    req["NAS-Identifier"] = nas_id
    req["Called-Station-Id"] = '00:00:00:00:00:00'
    req["Calling-Station-Id"] = '00:00:00:00:00:00'
    req["Framed-IP-Address"] = userip

    def addonline(radresp):
        client = {}
        client['session_id'] = session_id
        client['username'] = username
        client['userip'] = userip
        client['realip'] = realip
        client['realport'] = realport
        client['ctime'] = int(time.time())
        client['inbytes'] = 0
        client['outbytes'] = 0
        try:
            client['acct_interval'] = tools.DecodeInteger(
                radresp.get(85)[0]) or 0
        except:
            client['acct_interval'] = acct_interval

        try:
            client['session_timeout'] = tools.DecodeInteger(
                radresp.get(27)[0]) or 0
        except:
            client['session_timeout'] = session_timeout

        statusdb.add_client(status_dbfile, client)
        log.msg('add new online<%s> client to db' % session_id)

    def shutdown(exitcode=0):
        reactor.addSystemEventTrigger('after', 'shutdown', os._exit, exitcode)
        reactor.stop()

    def onresp(r):
        try:
            addonline(r)
        except Exception as e:
            log.err('add client online error')
            log.err(e)
        shutdown(0)

    def onerr(e):
        log.err(e)
        shutdown(1)

    d = client.send_acct(str(secret),
                         get_dictionary(),
                         radius_addr,
                         acctport=radius_acct_port,
                         debug=True,
                         **req)
    d.addCallbacks(onresp, onerr)
    reactor.callLater(radius_timeout, shutdown, 1)
    reactor.run()