aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/Twisted-12.2.0-py2.7-linux-x86_64.egg/twisted/runner/inetdconf.py
blob: f06a2ab65770fc4862119921dbd1e465abf75820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

# 
"""
Parser for inetd.conf files

Maintainer: Andrew Bennetts

Future Plans: xinetd configuration file support?
"""

# Various exceptions
class InvalidConfError(Exception):
    """Invalid configuration file"""


class InvalidInetdConfError(InvalidConfError):
    """Invalid inetd.conf file"""


class InvalidServicesConfError(InvalidConfError):
    """Invalid services file"""


class InvalidRPCServicesConfError(InvalidConfError):
    """Invalid rpc services file"""


class UnknownService(Exception):
    """Unknown service name"""


class SimpleConfFile:
    """Simple configuration file parser superclass.

    Filters out comments and empty lines (which includes lines that only 
    contain comments).

    To use this class, override parseLine or parseFields.
    """
    
    commentChar = '#'
    defaultFilename = None
    
    def parseFile(self, file=None):
        """Parse a configuration file
        
        If file is None and self.defaultFilename is set, it will open
        defaultFilename and use it.
        """
        if file is None and self.defaultFilename:
            file = open(self.defaultFilename,'r')
            
        for line in file.readlines():
            # Strip out comments
            comment = line.find(self.commentChar)
            if comment != -1:
                line = line[:comment]

            # Strip whitespace
            line = line.strip()

            # Skip empty lines (and lines which only contain comments)
            if not line:
                continue

            self.parseLine(line)

    def parseLine(self, line):
        """Override this.
        
        By default, this will split the line on whitespace and call
        self.parseFields (catching any errors).
        """
        try:
            self.parseFields(*line.split())
        except ValueError:
            raise InvalidInetdConfError, 'Invalid line: ' + repr(line)
    
    def parseFields(self, *fields):
        """Override this."""


class InetdService:
    """A simple description of an inetd service."""
    name = None
    port = None
    socketType = None
    protocol = None
    wait = None
    user = None
    group = None
    program = None
    programArgs = None
    
    def __init__(self, name, port, socketType, protocol, wait, user, group,
                 program, programArgs):
        self.name = name
        self.port = port
        self.socketType = socketType
        self.protocol = protocol
        self.wait = wait
        self.user = user
        self.group = group
        self.program = program
        self.programArgs = programArgs


class InetdConf(SimpleConfFile):
    """Configuration parser for a traditional UNIX inetd(8)"""

    defaultFilename = '/etc/inetd.conf'
    
    def __init__(self, knownServices=None):
        self.services = []
        
        if knownServices is None:
            knownServices = ServicesConf()
            knownServices.parseFile()
        self.knownServices = knownServices

    def parseFields(self, serviceName, socketType, protocol, wait, user,
                    program, *programArgs):
        """Parse an inetd.conf file.

        Implemented from the description in the Debian inetd.conf man page.
        """
        # Extract user (and optional group)
        user, group = (user.split('.') + [None])[:2]

        # Find the port for a service
        port = self.knownServices.services.get((serviceName, protocol), None)
        if not port and not protocol.startswith('rpc/'):
            # FIXME: Should this be discarded/ignored, rather than throwing
            #        an exception?
            try:
                port = int(serviceName)
                serviceName = 'unknown'
            except:
                raise UnknownService, "Unknown service: %s (%s)" \
                      % (serviceName, protocol)

        self.services.append(InetdService(serviceName, port, socketType,
                                          protocol, wait, user, group, program,
                                          programArgs))
            
            
class ServicesConf(SimpleConfFile):
    """/etc/services parser
    
    @ivar services: dict mapping service names to (port, protocol) tuples.
    """
    
    defaultFilename = '/etc/services'

    def __init__(self):
        self.services = {}

    def parseFields(self, name, portAndProtocol, *aliases):
        try:
            port, protocol = portAndProtocol.split('/')
            port = long(port)
        except:
            raise InvalidServicesConfError, 'Invalid port/protocol:' + \
                                            repr(portAndProtocol)

        self.services[(name, protocol)] = port
        for alias in aliases:
            self.services[(alias, protocol)] = port


class RPCServicesConf(SimpleConfFile):
    """/etc/rpc parser

    @ivar self.services: dict mapping rpc service names to rpc ports.
    """

    defaultFilename = '/etc/rpc'

    def __init__(self):
        self.services = {}
    
    def parseFields(self, name, port, *aliases):
        try:
            port = long(port)
        except:
            raise InvalidRPCServicesConfError, 'Invalid port:' + repr(port)
                        
        self.services[name] = port
        for alias in aliases:
            self.services[alias] = port