/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2012-05-26 22:56:38 UTC
  • mfrom: (589.1.1 socket-option)
  • Revision ID: teddy@recompile.se-20120526225638-4hvqyrvmj0036lfn
Merge "--socket" option for server.

This is suggested by the GNU Coding Standards' Table of Long Options,
and will probably also allow socket activation (e.g. by systemd(8)).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1899
1899
        use_ipv6:       Boolean; to use IPv6 or not
1900
1900
    """
1901
1901
    def __init__(self, server_address, RequestHandlerClass,
1902
 
                 interface=None, use_ipv6=True):
 
1902
                 interface=None, use_ipv6=True, socketfd=None):
 
1903
        """If socketfd is set, use that file descriptor instead of
 
1904
        creating a new one with socket.socket().
 
1905
        """
1903
1906
        self.interface = interface
1904
1907
        if use_ipv6:
1905
1908
            self.address_family = socket.AF_INET6
 
1909
        if socketfd is not None:
 
1910
            # Save the file descriptor
 
1911
            self.socketfd = socketfd
 
1912
            # Save the original socket.socket() function
 
1913
            self.socket_socket = socket.socket
 
1914
            # To implement --socket, we monkey patch socket.socket.
 
1915
            # 
 
1916
            # (When socketserver.TCPServer is a new-style class, we
 
1917
            # could make self.socket into a property instead of monkey
 
1918
            # patching socket.socket.)
 
1919
            # 
 
1920
            # Create a one-time-only replacement for socket.socket()
 
1921
            @functools.wraps(socket.socket)
 
1922
            def socket_wrapper(*args, **kwargs):
 
1923
                # Restore original function so subsequent calls are
 
1924
                # not affected.
 
1925
                socket.socket = self.socket_socket
 
1926
                del self.socket_socket
 
1927
                # This time only, return a new socket object from the
 
1928
                # saved file descriptor.
 
1929
                return socket.fromfd(self.socketfd, *args, **kwargs)
 
1930
            # Replace socket.socket() function with wrapper
 
1931
            socket.socket = socket_wrapper
 
1932
        # The socketserver.TCPServer.__init__ will call
 
1933
        # socket.socket(), which might be our replacement,
 
1934
        # socket_wrapper(), if socketfd was set.
1906
1935
        socketserver.TCPServer.__init__(self, server_address,
1907
1936
                                        RequestHandlerClass)
 
1937
    
1908
1938
    def server_bind(self):
1909
1939
        """This overrides the normal server_bind() function
1910
1940
        to bind to an interface if one was specified, and also NOT to
1968
1998
    """
1969
1999
    def __init__(self, server_address, RequestHandlerClass,
1970
2000
                 interface=None, use_ipv6=True, clients=None,
1971
 
                 gnutls_priority=None, use_dbus=True):
 
2001
                 gnutls_priority=None, use_dbus=True, socketfd=None):
1972
2002
        self.enabled = False
1973
2003
        self.clients = clients
1974
2004
        if self.clients is None:
1978
2008
        IPv6_TCPServer.__init__(self, server_address,
1979
2009
                                RequestHandlerClass,
1980
2010
                                interface = interface,
1981
 
                                use_ipv6 = use_ipv6)
 
2011
                                use_ipv6 = use_ipv6,
 
2012
                                socketfd = socketfd)
1982
2013
    def server_activate(self):
1983
2014
        if self.enabled:
1984
2015
            return socketserver.TCPServer.server_activate(self)
2165
2196
    parser.add_argument("--no-restore", action="store_false",
2166
2197
                        dest="restore", help="Do not restore stored"
2167
2198
                        " state")
 
2199
    parser.add_argument("--socket", type=int,
 
2200
                        help="Specify a file descriptor to a network"
 
2201
                        " socket to use instead of creating one")
2168
2202
    parser.add_argument("--statedir", metavar="DIR",
2169
2203
                        help="Directory to save/restore state in")
2170
2204
    
2187
2221
                        "use_ipv6": "True",
2188
2222
                        "debuglevel": "",
2189
2223
                        "restore": "True",
 
2224
                        "socket": "",
2190
2225
                        "statedir": "/var/lib/mandos"
2191
2226
                        }
2192
2227
    
2204
2239
    if server_settings["port"]:
2205
2240
        server_settings["port"] = server_config.getint("DEFAULT",
2206
2241
                                                       "port")
 
2242
    if server_settings["socket"]:
 
2243
        server_settings["socket"] = server_config.getint("DEFAULT",
 
2244
                                                         "socket")
 
2245
        # Later, stdin will, and stdout and stderr might, be dup'ed
 
2246
        # over with an opened os.devnull.  But we don't want this to
 
2247
        # happen with a supplied network socket.
 
2248
        if 0 <= server_settings["socket"] <= 2:
 
2249
            server_settings["socket"] = os.dup(server_settings
 
2250
                                               ["socket"])
2207
2251
    del server_config
2208
2252
    
2209
2253
    # Override the settings from the config file with command line
2211
2255
    for option in ("interface", "address", "port", "debug",
2212
2256
                   "priority", "servicename", "configdir",
2213
2257
                   "use_dbus", "use_ipv6", "debuglevel", "restore",
2214
 
                   "statedir"):
 
2258
                   "statedir", "socket"):
2215
2259
        value = getattr(options, option)
2216
2260
        if value is not None:
2217
2261
            server_settings[option] = value
2265
2309
                              use_ipv6=use_ipv6,
2266
2310
                              gnutls_priority=
2267
2311
                              server_settings["priority"],
2268
 
                              use_dbus=use_dbus)
 
2312
                              use_dbus=use_dbus,
 
2313
                              socketfd=(server_settings["socket"]
 
2314
                                        or None))
2269
2315
    if not debug:
2270
2316
        pidfilename = "/var/run/mandos.pid"
2271
2317
        try: