Blame src/lib/krad/t_daemon.py

Packit Service 99d1c0
# Copyright 2013 Red Hat, Inc.  All rights reserved.
Packit Service 99d1c0
#
Packit Service 99d1c0
# Redistribution and use in source and binary forms, with or without
Packit Service 99d1c0
# modification, are permitted provided that the following conditions are met:
Packit Service 99d1c0
#
Packit Service 99d1c0
#    1. Redistributions of source code must retain the above copyright
Packit Service 99d1c0
#       notice, this list of conditions and the following disclaimer.
Packit Service 99d1c0
#
Packit Service 99d1c0
#    2. Redistributions in binary form must reproduce the above copyright
Packit Service 99d1c0
#       notice, this list of conditions and the following disclaimer in
Packit Service 99d1c0
#       the documentation and/or other materials provided with the
Packit Service 99d1c0
#       distribution.
Packit Service 99d1c0
#
Packit Service 99d1c0
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
Packit Service 99d1c0
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Packit Service 99d1c0
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
Packit Service 99d1c0
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
Packit Service 99d1c0
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit Service 99d1c0
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit Service 99d1c0
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit Service 99d1c0
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit Service 99d1c0
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit Service 99d1c0
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit Service 99d1c0
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 99d1c0
Packit Service 99d1c0
from io import StringIO
Packit Service 99d1c0
import os
Packit Service 99d1c0
import sys
Packit Service 99d1c0
import signal
Packit Service 99d1c0
Packit Service 99d1c0
try:
Packit Service 99d1c0
    from pyrad import dictionary, packet, server
Packit Service 99d1c0
except ImportError:
Packit Service 99d1c0
    sys.stderr.write("pyrad not found!\n")
Packit Service 99d1c0
    sys.exit(0)
Packit Service 99d1c0
Packit Service 99d1c0
# We could use a dictionary file, but since we need
Packit Service 99d1c0
# such few attributes, we'll just include them here
Packit Service 99d1c0
DICTIONARY = """
Packit Service 99d1c0
ATTRIBUTE\tUser-Name\t1\tstring
Packit Service 99d1c0
ATTRIBUTE\tUser-Password\t2\toctets
Packit Service 99d1c0
ATTRIBUTE\tNAS-Identifier\t32\tstring
Packit Service 99d1c0
"""
Packit Service 99d1c0
Packit Service 99d1c0
class TestServer(server.Server):
Packit Service 99d1c0
    def _HandleAuthPacket(self, pkt):
Packit Service 99d1c0
        server.Server._HandleAuthPacket(self, pkt)
Packit Service 99d1c0
Packit Service 99d1c0
        passwd = []
Packit Service 99d1c0
Packit Service 99d1c0
        for key in pkt.keys():
Packit Service 99d1c0
            if key == "User-Password":
Packit Service 99d1c0
                passwd = map(pkt.PwDecrypt, pkt[key])
Packit Service 99d1c0
Packit Service 99d1c0
        reply = self.CreateReplyPacket(pkt)
Packit Service 99d1c0
        if passwd == ['accept']:
Packit Service 99d1c0
            reply.code = packet.AccessAccept
Packit Service 99d1c0
        else:
Packit Service 99d1c0
            reply.code = packet.AccessReject
Packit Service 99d1c0
        self.SendReplyPacket(pkt.fd, reply)
Packit Service 99d1c0
Packit Service 99d1c0
srv = TestServer(addresses=["localhost"],
Packit Service 99d1c0
                 hosts={"127.0.0.1":
Packit Service 99d1c0
                        server.RemoteHost("127.0.0.1", "foo", "localhost")},
Packit Service 99d1c0
                 dict=dictionary.Dictionary(StringIO.StringIO(DICTIONARY)))
Packit Service 99d1c0
Packit Service 99d1c0
# Write a sentinel character to let the parent process know we're listening.
Packit Service 99d1c0
sys.stdout.write("~")
Packit Service 99d1c0
sys.stdout.flush()
Packit Service 99d1c0
Packit Service 99d1c0
srv.Run()