86
86
import xml.dom.minidom
 
 
89
# Try to find the value of SO_BINDTODEVICE:
 
 
91
    # This is where SO_BINDTODEVICE is in Python 3.3 (or 3.4?) and
 
 
92
    # newer, and it is also the most natural place for it:
 
90
93
    SO_BINDTODEVICE = socket.SO_BINDTODEVICE
 
91
94
except AttributeError:
 
 
96
        # This is where SO_BINDTODEVICE was up to and including Python
 
93
98
        from IN import SO_BINDTODEVICE
 
94
99
    except ImportError:
 
95
 
        SO_BINDTODEVICE = None
 
 
100
        # In Python 2.7 it seems to have been removed entirely.
 
 
101
        # Try running the C preprocessor:
 
 
103
            cc = subprocess.Popen(["cc", "--language=c", "-E",
 
 
105
                                  stdin=subprocess.PIPE,
 
 
106
                                  stdout=subprocess.PIPE)
 
 
107
            stdout = cc.communicate(
 
 
108
                "#include <sys/socket.h>\nSO_BINDTODEVICE\n")[0]
 
 
109
            SO_BINDTODEVICE = int(stdout.splitlines()[-1])
 
 
110
        except (OSError, ValueError, IndexError):
 
 
112
            SO_BINDTODEVICE = None
 
97
114
if sys.version_info.major == 2:
 
101
118
stored_state_file = "clients.pickle"
 
103
120
logger = logging.getLogger()
 
 
180
197
        self.gnupgargs = ['--batch',
 
181
198
                          '--homedir', self.tempdir,
 
 
201
        # Only GPG version 1 has the --no-use-agent option.
 
 
202
        if self.gpg == "gpg" or self.gpg.endswith("/gpg"):
 
 
203
            self.gnupgargs.append("--no-use-agent")
 
186
205
    def __enter__(self):
 
 
2172
2191
            priority = self.server.gnutls_priority
 
2173
2192
            if priority is None:
 
2174
2193
                priority = "NORMAL"
 
2175
 
            gnutls.priority_set_direct(session._c_object, priority,
 
 
2194
            gnutls.priority_set_direct(session._c_object,
 
 
2195
                                       priority.encode("utf-8"),
 
2178
2198
            # Start communication using the Mandos protocol
 
 
2433
2453
        """This overrides the normal server_bind() function
 
2434
2454
        to bind to an interface if one was specified, and also NOT to
 
2435
2455
        bind to an address or port if they were not specified."""
 
 
2456
        global SO_BINDTODEVICE
 
2436
2457
        if self.interface is not None:
 
2437
2458
            if SO_BINDTODEVICE is None:
 
2438
 
                logger.error("SO_BINDTODEVICE does not exist;"
 
2439
 
                             " cannot bind to interface %s",
 
2443
 
                    self.socket.setsockopt(
 
2444
 
                        socket.SOL_SOCKET, SO_BINDTODEVICE,
 
2445
 
                        (self.interface + "\0").encode("utf-8"))
 
2446
 
                except socket.error as error:
 
2447
 
                    if error.errno == errno.EPERM:
 
2448
 
                        logger.error("No permission to bind to"
 
2449
 
                                     " interface %s", self.interface)
 
2450
 
                    elif error.errno == errno.ENOPROTOOPT:
 
2451
 
                        logger.error("SO_BINDTODEVICE not available;"
 
2452
 
                                     " cannot bind to interface %s",
 
2454
 
                    elif error.errno == errno.ENODEV:
 
2455
 
                        logger.error("Interface %s does not exist,"
 
2456
 
                                     " cannot bind", self.interface)
 
 
2459
                # Fall back to a hard-coded value which seems to be
 
 
2461
                logger.warning("SO_BINDTODEVICE not found, trying 25")
 
 
2462
                SO_BINDTODEVICE = 25
 
 
2464
                self.socket.setsockopt(
 
 
2465
                    socket.SOL_SOCKET, SO_BINDTODEVICE,
 
 
2466
                    (self.interface + "\0").encode("utf-8"))
 
 
2467
            except socket.error as error:
 
 
2468
                if error.errno == errno.EPERM:
 
 
2469
                    logger.error("No permission to bind to"
 
 
2470
                                 " interface %s", self.interface)
 
 
2471
                elif error.errno == errno.ENOPROTOOPT:
 
 
2472
                    logger.error("SO_BINDTODEVICE not available;"
 
 
2473
                                 " cannot bind to interface %s",
 
 
2475
                elif error.errno == errno.ENODEV:
 
 
2476
                    logger.error("Interface %s does not exist,"
 
 
2477
                                 " cannot bind", self.interface)
 
2459
2480
        # Only bind(2) the socket if we really need to.
 
2460
2481
        if self.server_address[0] or self.server_address[1]:
 
2461
2482
            if not self.server_address[0]:
 
 
3308
3329
        mandos_dbus_service = MandosDBusService()
 
 
3331
    # Save modules to variables to exempt the modules from being
 
 
3332
    # unloaded before the function registered with atexit() is run.
 
 
3333
    mp = multiprocessing
 
3311
3336
        "Cleanup function; run on exit"
 
3313
3338
            service.cleanup()
 
3315
 
        multiprocessing.active_children()
 
 
3340
        mp.active_children()
 
3317
3342
        if not (tcp_server.clients or client_settings):