def recv(self): while True: try: msg, addr = self.sock.recvfrom(8192) self.lasttime = time.time() if msg: self.reply += 1 if self.argvals.debug: try: resp = packet.Packet(packet=msg,dict=self.dict) attr_keys = resp.keys() print ("\nReceived an response:") print "id:%s" % resp.id print "code:%s" % resp.code print ("Attributes: ") for attr in attr_keys: print ( "%s: %s" % (attr, _decode_attr(self.dict,attr,resp[attr][0]))) except Exception as e: print 'error %s'%str(e) except socket.timeout: self.sock.close() break times = self.lasttime - self.starttime percount = self.reply /times print print ("Total time (sec):%s"%round(times,4)) print ("response total:%s"%self.reply) print ("request per second:%s"%percount)
def setUp(self): self.path = os.path.join(home, 'tests', 'data') self.dict = Dictionary(os.path.join(self.path, 'full')) self.packet = packet.Packet(id=0, secret=six.b('secret'), authenticator=six.b('01234567890ABCDEF'), dict=self.dict)
def CreatePacket(self, **args): """Create a new RADIUS packet. This utility function creates a new RADIUS authentication packet which can be used to communicate with the RADIUS server this client talks to. This is initializing the new packet with the dictionary and secret used for the client. :return: a new empty packet instance :rtype: pyrad.packet.Packet """ return packet.Packet(dict=self.dict, **args)
def on_recv(self, times): _times = 0 stat_time = time.time() while self.running: app.processEvents() if _times == times: break try: msg, addr = self.sock.recvfrom(8192) _times += 1 self.lasttime = time.time() if self.lasttime - stat_time > 2: self.logger("\nCurrent received %s response" % _times) stat_time = self.lasttime if msg: self.reply += 1 if self.is_debug.isChecked(): try: resp = packet.Packet(packet=msg, dict=self.dict) attr_keys = resp.keys() self.logger("\nReceived an response:") self.logger("id:%s" % resp.id) self.logger("code:%s" % resp.code) self.logger("Attributes: ") for attr in attr_keys: self.logger( ":::: %s: %s" % (attr, self.decode_attr( attr, resp[attr][0]))) except Exception as e: import traceback traceback.print_exc() self.logger('\nerror %s' % str(e)) except: break sectimes = self.lasttime - self.starttime if times > 1: percount = self.reply / sectimes self.logger("\nTotal time (sec):%s" % round(sectimes, 4)) self.logger("response total:%s" % self.reply) self.logger("request per second:%s" % percount) self.stop()
def __init__(self, addr, path): """ This initialize the RADIUS server. addr: IP address of the server dict: instance of dictionary.Dictionary """ self.addr = addr self.running = False self.prepaid = False self.log = logging.getLogger('nextestlog') # 32141 self.acclog = None self.count = 0 self.respTime = 0 #PR 133009 Adding path variable to store radius packets self.filename = path + '/rad-pkt.log' self.pkt_copy = packet.Packet() server.Server.__init__(self, dict=dictionary.Dictionary(\ "/usr/lib/python2.3/site-packages/pyrad/dictionary"))
def _receive_request(self,alias,code,timeout): server = self._get_session(self._server, alias) ready = select.select([server['sock']], [], [], float(timeout)) pkt = None if ready[0]: data, addr = server['sock'].recvfrom(1024) pkt = packet.Packet(secret=server['secret'], packet=data, dict=server['dictionary']) server['request'].register(pkt,str(pkt.id)) self.builtin.log(pkt.code) if pkt.code != code: self.builtin.log('Expected {0}, received {1}'.format(code, pkt.code)) raise Exception("received {}".format(pkt.code)) if pkt is None: raise Exception("Did not receive any answer") pkt.addr = addr return pkt
def _receive_response(self,alias,code,timeout): client = self._get_session(self._client, alias) ready = select.select([client['sock']], [], [], float(timeout)) pkt = None if ready[0]: data, addr = client['sock'].recvfrom(1024) pkt = packet.Packet(secret=client['secret'], packet=data, dict=client['dictionary']) client['response'].register(pkt,str(pkt.id)) self.builtin.log(pkt.keys()) if pkt.code != code: # TODO: name packet code instead of id. self.builtin.log('Expected {0}, received {1}'.format(code, pkt.code)) raise Exception("received {}".format(pkt.code)) if pkt is None: raise Exception("Did not receive any answer") return pkt
def main(): dct = Dictionary(StringIO(DICTIONARY)) proc = subprocess.Popen(["./ipa-otpd", sys.argv[1]], stdin=subprocess.PIPE, stdout=subprocess.PIPE) pkt = packet.AuthPacket(secret="", dict=dct) pkt["User-Name"] = sys.argv[2] pkt["User-Password"] = pkt.PwCrypt(sys.argv[3]) pkt["NAS-Identifier"] = "localhost" proc.stdin.write(pkt.RequestPacket()) rsp = packet.Packet(secret="", dict=dict) buf = proc.stdout.read(4) buf += proc.stdout.read(struct.unpack("!BBH", buf)[2] - 4) rsp.DecodePacket(buf) pkt.VerifyReply(rsp) proc.terminate() proc.wait()
realm.kinit(realm.user_princ, 'reject', flags=flags, expected_code=1) verify(daemon, queue, False, 'custom', 'reject') ## Test UDP success / standard username daemon = UDPRadiusDaemon(args=(server_addr, secret_file, 'accept', queue)) daemon.start() queue.get() realm.run_kadminl(setstr(realm.user_princ, 'udp')) realm.kinit(realm.user_princ, 'accept', flags=flags) verify(daemon, queue, True, realm.user_princ.split('@')[0], 'accept') # Detect upstream pyrad bug # https://github.com/wichert/pyrad/pull/18 try: auth = packet.Packet.CreateAuthenticator() packet.Packet(authenticator=auth, secret="").ReplyPacket() except AssertionError: success('Warning: skipping UNIX domain socket tests because of pyrad ' 'assertion bug') exit(0) ## Test Unix fail / custom username daemon = UnixRadiusDaemon(args=(socket_file, '', 'accept', queue)) daemon.start() queue.get() realm.run_kadminl(setstr(realm.user_princ, 'unix', 'custom')) realm.kinit(realm.user_princ, 'reject', flags=flags, expected_code=1) verify(daemon, queue, False, 'custom', 'reject') ## Test Unix success / standard username daemon = UnixRadiusDaemon(args=(socket_file, '', 'accept', queue))
sys.exit(0) # We could use a dictionary file, but since we need # such few attributes, we'll just include them here DICTIONARY = """ ATTRIBUTE User-Name 1 string ATTRIBUTE User-Password 2 string ATTRIBUTE NAS-Identifier 32 string """ dct = Dictionary(StringIO.StringIO(DICTIONARY)) proc = subprocess.Popen(["./ipa-otpd", sys.argv[1]], stdin=subprocess.PIPE, stdout=subprocess.PIPE) pkt = packet.AuthPacket(secret="", dict=dct) pkt["User-Name"] = sys.argv[2] pkt["User-Password"] = pkt.PwCrypt(sys.argv[3]) pkt["NAS-Identifier"] = "localhost" proc.stdin.write(pkt.RequestPacket()) rsp = packet.Packet(secret="", dict=dict) buf = proc.stdout.read(4) buf += proc.stdout.read(struct.unpack("!BBH", buf)[2] - 4) rsp.DecodePacket(buf) pkt.VerifyReply(rsp) proc.terminate() #pylint: disable=E1101 proc.wait()