xref: /freebsd/crypto/krb5/src/lib/krad/t_daemon.py (revision e7be843b4a162e68651d3911f0357ed464915629)
1# Copyright 2013 Red Hat, Inc.  All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are met:
5#
6#    1. Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#
9#    2. Redistributions in binary form must reproduce the above copyright
10#       notice, this list of conditions and the following disclaimer in
11#       the documentation and/or other materials provided with the
12#       distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
15# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26from io import StringIO
27import os
28import sys
29import signal
30
31try:
32    from pyrad import dictionary, packet, server
33except ImportError:
34    sys.stderr.write("pyrad not found!\n")
35    sys.exit(0)
36
37# We could use a dictionary file, but since we need
38# such few attributes, we'll just include them here
39DICTIONARY = """
40ATTRIBUTE\tUser-Name\t1\tstring
41ATTRIBUTE\tUser-Password\t2\toctets
42ATTRIBUTE\tNAS-Identifier\t32\tstring
43"""
44
45class TestServer(server.Server):
46    def _HandleAuthPacket(self, pkt):
47        server.Server._HandleAuthPacket(self, pkt)
48
49        passwd = []
50
51        for key in pkt.keys():
52            if key == "User-Password":
53                passwd = [pkt.PwDecrypt(x) for x in pkt[key]]
54
55        reply = self.CreateReplyPacket(pkt)
56        if passwd == ['accept']:
57            reply.code = packet.AccessAccept
58        else:
59            reply.code = packet.AccessReject
60        self.SendReplyPacket(pkt.fd, reply)
61
62srv = TestServer(addresses=["localhost"],
63                 hosts={"127.0.0.1":
64                        server.RemoteHost("127.0.0.1", b"foo", "localhost")},
65                 dict=dictionary.Dictionary(StringIO(DICTIONARY)))
66
67# Write a sentinel character to let the parent process know we're listening.
68sys.stdout.write("~")
69sys.stdout.flush()
70
71srv.Run()
72