Path: blob/main/crypto/krb5/src/lib/krad/t_daemon.py
39536 views
# Copyright 2013 Red Hat, Inc. All rights reserved.1#2# Redistribution and use in source and binary forms, with or without3# modification, are permitted provided that the following conditions are met:4#5# 1. Redistributions of source code must retain the above copyright6# notice, this list of conditions and the following disclaimer.7#8# 2. Redistributions in binary form must reproduce the above copyright9# notice, this list of conditions and the following disclaimer in10# the documentation and/or other materials provided with the11# distribution.12#13# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS14# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED15# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A16# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER17# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,18# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,19# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR20# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF21# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING22# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS23# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425from io import StringIO26import os27import sys28import signal2930try:31from pyrad import dictionary, packet, server32except ImportError:33sys.stderr.write("pyrad not found!\n")34sys.exit(0)3536# We could use a dictionary file, but since we need37# such few attributes, we'll just include them here38DICTIONARY = """39ATTRIBUTE\tUser-Name\t1\tstring40ATTRIBUTE\tUser-Password\t2\toctets41ATTRIBUTE\tNAS-Identifier\t32\tstring42ATTRIBUTE\tMessage-Authenticator\t80\toctets43"""4445class TestServer(server.Server):46def _HandleAuthPacket(self, pkt):47server.Server._HandleAuthPacket(self, pkt)4849passwd = []5051for key in pkt.keys():52if key == "User-Password":53passwd = [pkt.PwDecrypt(x) for x in pkt[key]]5455reply = self.CreateReplyPacket(pkt, message_authenticator=True)56if passwd == ['accept']:57reply.code = packet.AccessAccept58else:59reply.code = packet.AccessReject60self.SendReplyPacket(pkt.fd, reply)6162srv = TestServer(addresses=["localhost"],63hosts={"127.0.0.1":64server.RemoteHost("127.0.0.1", b"foo", "localhost")},65dict=dictionary.Dictionary(StringIO(DICTIONARY)))6667# Write a sentinel character to let the parent process know we're listening.68sys.stdout.write("~")69sys.stdout.flush()7071srv.Run()727374