xref: /freebsd/crypto/krb5/src/lib/krad/t_daemon.py (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
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
43ATTRIBUTE\tMessage-Authenticator\t80\toctets
44"""
45
46class TestServer(server.Server):
47    def _HandleAuthPacket(self, pkt):
48        server.Server._HandleAuthPacket(self, pkt)
49
50        passwd = []
51
52        for key in pkt.keys():
53            if key == "User-Password":
54                passwd = [pkt.PwDecrypt(x) for x in pkt[key]]
55
56        reply = self.CreateReplyPacket(pkt, message_authenticator=True)
57        if passwd == ['accept']:
58            reply.code = packet.AccessAccept
59        else:
60            reply.code = packet.AccessReject
61        self.SendReplyPacket(pkt.fd, reply)
62
63srv = TestServer(addresses=["localhost"],
64                 hosts={"127.0.0.1":
65                        server.RemoteHost("127.0.0.1", b"foo", "localhost")},
66                 dict=dictionary.Dictionary(StringIO(DICTIONARY)))
67
68# Write a sentinel character to let the parent process know we're listening.
69sys.stdout.write("~")
70sys.stdout.flush()
71
72srv.Run()
73