"Fossies" - the Fresh Open Source Software Archive

Member "ospd-2.0.1/ospd/main.py" (12 May 2020, 4729 Bytes) of package /linux/misc/openvas/ospd-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 # Copyright (C) 2019 Greenbone Networks GmbH
    2 #
    3 # SPDX-License-Identifier: GPL-2.0-or-later
    4 #
    5 # This program is free software; you can redistribute it and/or
    6 # modify it under the terms of the GNU General Public License
    7 # as published by the Free Software Foundation; either version 2
    8 # of the License, or (at your option) any later version.
    9 #
   10 # This program is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 # GNU General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU General Public License
   16 # along with this program; if not, write to the Free Software
   17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   18 
   19 import logging
   20 
   21 from logging.handlers import SysLogHandler, WatchedFileHandler
   22 
   23 import os
   24 import sys
   25 import atexit
   26 import signal
   27 
   28 from functools import partial
   29 
   30 from typing import Type, Optional
   31 
   32 from ospd.errors import OspdError
   33 from ospd.misc import go_to_background, create_pid, remove_pidfile
   34 from ospd.ospd import OSPDaemon
   35 from ospd.parser import create_parser, ParserType
   36 from ospd.server import TlsServer, UnixSocketServer
   37 
   38 COPYRIGHT = """Copyright (C) 2014, 2015, 2018, 2019 Greenbone Networks GmbH
   39 License GPLv2+: GNU GPL version 2 or later
   40 This is free software: you are free to change and redistribute it.
   41 There is NO WARRANTY, to the extent permitted by law."""
   42 
   43 
   44 def print_version(daemon: OSPDaemon, file=sys.stdout):
   45     """ Prints the server version and license information."""
   46 
   47     scanner_name = daemon.get_scanner_name()
   48     server_version = daemon.get_server_version()
   49     protocol_version = daemon.get_protocol_version()
   50     daemon_name = daemon.get_daemon_name()
   51     daemon_version = daemon.get_daemon_version()
   52 
   53     print(
   54         "OSP Server for {0}: {1}".format(scanner_name, server_version),
   55         file=file,
   56     )
   57     print("OSP: {0}".format(protocol_version), file=file)
   58     print("{0}: {1}".format(daemon_name, daemon_version), file=file)
   59     print(file=file)
   60     print(COPYRIGHT, file=file)
   61 
   62 
   63 def init_logging(
   64     name: str,
   65     log_level: int,
   66     *,
   67     log_file: Optional[str] = None,
   68     foreground: Optional[bool] = False
   69 ):
   70 
   71     rootlogger = logging.getLogger()
   72     rootlogger.setLevel(log_level)
   73 
   74     if foreground:
   75         console = logging.StreamHandler()
   76         console.setFormatter(
   77             logging.Formatter(
   78                 '%(asctime)s {}: %(levelname)s: (%(name)s) %(message)s'.format(
   79                     name
   80                 )
   81             )
   82         )
   83         rootlogger.addHandler(console)
   84     elif log_file:
   85         logfile = WatchedFileHandler(log_file)
   86         logfile.setFormatter(
   87             logging.Formatter(
   88                 '%(asctime)s {}: %(levelname)s: (%(name)s) %(message)s'.format(
   89                     name
   90                 )
   91             )
   92         )
   93         rootlogger.addHandler(logfile)
   94     else:
   95         syslog = SysLogHandler('/dev/log')
   96         syslog.setFormatter(
   97             logging.Formatter(
   98                 '{}: %(levelname)s: (%(name)s) %(message)s'.format(name)
   99             )
  100         )
  101         rootlogger.addHandler(syslog)
  102         # Duplicate syslog's file descriptor to stout/stderr.
  103         syslog_fd = syslog.socket.fileno()
  104         os.dup2(syslog_fd, 1)
  105         os.dup2(syslog_fd, 2)
  106 
  107 
  108 def main(
  109     name: str,
  110     daemon_class: Type[OSPDaemon],
  111     parser: Optional[ParserType] = None,
  112 ):
  113     """ OSPD Main function. """
  114 
  115     if not parser:
  116         parser = create_parser(name)
  117     args = parser.parse_arguments()
  118 
  119     if args.version:
  120         args.foreground = True
  121 
  122     init_logging(
  123         name, args.log_level, log_file=args.log_file, foreground=args.foreground
  124     )
  125 
  126     if args.port == 0:
  127         server = UnixSocketServer(
  128             args.unix_socket, args.socket_mode, args.stream_timeout,
  129         )
  130     else:
  131         server = TlsServer(
  132             args.address,
  133             args.port,
  134             args.cert_file,
  135             args.key_file,
  136             args.ca_file,
  137             args.stream_timeout,
  138         )
  139 
  140     daemon = daemon_class(**vars(args))
  141 
  142     if args.version:
  143         print_version(daemon)
  144         sys.exit()
  145 
  146     if not args.foreground:
  147         go_to_background()
  148 
  149     if not create_pid(args.pid_file):
  150         sys.exit()
  151 
  152     # Set signal handler and cleanup
  153     atexit.register(remove_pidfile, pidfile=args.pid_file)
  154     signal.signal(signal.SIGTERM, partial(remove_pidfile, args.pid_file))
  155 
  156     try:
  157         daemon.init()
  158 
  159         if not daemon.check():
  160             return 1
  161 
  162         daemon.run(server)
  163     except OspdError as e:
  164         print('ERROR: {}'.format(e), file=sys.stderr)
  165         sys.exit(1)
  166 
  167     return 0