Exemple #1
0
 def _test_parse_radius(self, filename):
     def _equal(real, test):
         return real == '' or real == test
     package, packagedict = read_test(filename)
     testdict = unpack(package)
     for att in ['type', 'iden', 'auth']:
         if packagedict[att] != '' and packagedict[att] != testdict[att]:
             return False
     packageatt = packagedict['attr']
     testatt = testdict['attr']
     for att in packageatt.keys():
         if not testatt.has_key(att):
             return False
         if packageatt[att] != [''] and packageatt[att] != map(str, testatt[att]):
             return False
     return True
Exemple #2
0
 def _test_parse_radius(self, filename):
     package = read_test(filename)[0]
     packagedict = unpack(package)
     return packagedict == unpack(pack(packagedict))
Exemple #3
0
def main():
    global EXIT_STATUS
    try:
        config = Config()
        sqldb = SQLDB(config.database)
        server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        address = (config.server['ip-address'], config.server['port'])
        server.bind(address)
        server.settimeout(1)
        logwriter = LogWriter(config.server['logfile'], config.server['print'])
    except ConfigError as e:
        error_exit('')
    except SQLDBError as e:
        error_exit('')
    except socket.error:
        sqldb.close()
        error_exit("can not bind to {0}:{1}".format(*address))
    except LogWriterError:
        error_exit('')
    
    logwriter("waiting for client on {0}:{1}".format(*address))
    
    #getnum, dropnum, acceptnum, rejectnum = (0, 0, 0, 0)
    while True:
        if EXIT_STATUS:
            close(server, sqldb, logwriter)
            break
            
        try:
            package, address = server.recvfrom(4096)
        except socket.error:
            continue

        logwriter.get += 1

        if not address[0] in config.secret:
            logwriter('get package from host for wirch can not find a secret')
            continue
        
        try:
            package_dict = unpack(package)
            check_package(package_dict)
        except UnpackError:
            logwriter("get invalid package. Dropped")
            logwriter.drop += 1
            continue
        except CheckPackageError as cpe:
            logwriter(cpe)
            logwriter.drop += 1
            continue
        
        secret = config.secret[address[0]]
        try:
            passwd, ip = sqldb.getdata(package_dict['attr']['User-Name'][0])
        except SQLDBError as err:
            logwriter('sql error. emergensy exit' + str(err))
            close(server, sqldb, logwriter)
            EXIT_STATUS = True
        recvpass = package_dict['attr']['User-Password'][0]
        recvpass =  decrypt(recvpass, secret, package_dict['auth'])
        
        if passwd != recvpass:
            logwriter('get invalid password. Send Access-Reject')
            logwriter.reject += 1
            access = create_reject(package, package_dict, secret, u'invalid password')
        else:
            logwriter('get cool package. Send Access-Accept')
            logwriter.accept += 1
            access = create_accept(package, package_dict, secret, ip)
            
        # Раскомментируйте что бы увидеть пришедший пакет
        #print package_dict
        
        server.sendto(urllib.unquote(access), address)