def test_listen_3_fds_with_names(self): os.environ['LISTEN_FDS'] = '3' os.environ['LISTEN_PID'] = str(os.getpid()) os.environ['LISTEN_FDNAMES'] = 'one:two:three' self.assertEqual(listen_fds_with_names(), { 3: 'one', 4: 'two', 5: 'three' })
def test_listen_3_fds_no_names(self): os.environ['LISTEN_FDS'] = '3' os.environ['LISTEN_PID'] = str(os.getpid()) os.unsetenv('LISTEN_FDNAMES') self.assertEqual(listen_fds_with_names(), { 3: 'unknown', 4: 'unknown', 5: 'unknown' })
def perform(args): global errfile # Set up logging logfile = args.log + '/xcauth.log' signal.signal(signal.SIGUSR1, log_info) if (args.interactive or args.auth_test or args.isuser_test or args.roster_test): signal.signal(signal.SIGHUP, signal.SIG_IGN) logging.basicConfig(stream=sys.stderr, level=logging.DEBUG, format='%(asctime)s %(levelname)s: %(message)s') else: errfile = args.log + '/xcauth.err' rebind_stderr(0, None) signal.signal(signal.SIGHUP, rebind_stderr) try: from logging.handlers import WatchedFileHandler logging.basicConfig(handlers=(WatchedFileHandler(logfile),), level=logging.DEBUG if args.debug else logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') except OSError as e: logging.basicConfig(stream=sys.stderr) logging.warning('Cannot log to %s: %s' % (logfile, str(e))) logging.debug('Start external auth script %s for %s with endpoint: %s', VERSION, args.type, args.url) # Set up global environment (incl. cache, db) if args.cache_storage != 'none': try: import bcrypt except ImportError as e: logging.warn('Cannot import bcrypt (%s); caching disabled' % e) args.cache_storage = 'none' ttls = {'query': args.cache_query_ttl, 'verify': args.cache_verification_ttl, 'unreach': args.cache_unreachable_ttl} xc = xcauth(default_url = args.url, default_secret = args.secret, ejabberdctl = args.ejabberdctl if 'ejabberdctl' in args else None, sql_db = args.db, cache_storage = args.cache_storage, domain_db = args.domain_db, cache_db = args.cache_db, shared_roster_db = args.shared_roster_db, timeout = args.timeout, ttls = ttls, bcrypt_rounds = args.cache_bcrypt_rounds) # Check for one-shot commands if args.isuser_test: sc = sigcloud(xc, args.isuser_test[0], args.isuser_test[1]) success = sc.isuser() print(success) return if args.roster_test: sc = sigcloud(xc, args.roster_test[0], args.roster_test[1]) success, response = sc.roster_cloud() print(str(response)) if args.ejabberdctl: sc.try_roster(async=False) return elif args.auth_test: sc = sigcloud(xc, args.auth_test[0], args.auth_test[1], args.auth_test[2]) success = sc.auth() print(success) return # Read commands from file descriptors # Acceptor socket? listeners = listen_fds_with_names() if listeners is None: # Single socket; unclear whether it is connected or an acceptor try: stdinfd = sys.stdin.fileno() except io.UnsupportedOperation: stdinfd = None if stdinfd is None: # Not a real socket, assume stdio communication perform_from_fd(sys.stdin, sys.stdout, xc, args.type) else: s = socket.socket(fileno=stdinfd) try: # Is it an acceptor socket? s.listen() # Yes, accept connections (fake systemd context) perform_from_listeners({0: args.type}, xc, args.type) except OSError: # Not an acceptor socket, use for stdio perform_from_fd(sys.stdin, sys.stdout, xc, args.type, closefds=(sys.stdin,sys.stdout,s)) else: # Uses systemd socket activation perform_from_listeners(listeners, xc, args.type)
def test_no_systemd_module_only(self): os.environ['LISTEN_FDS'] = '5' os.environ['LISTEN_PID'] = str(os.getpid()) with self.assertRaises(ImportError): listen_fds_with_names()
def test_no_systemd_at_all(self): os.unsetenv('LISTEN_FDS') os.unsetenv('LISTEN_PID') self.assertEqual(listen_fds_with_names(), None)