def stand_alone(): if not sys.argv[0].endswith("query") and os.geteuid() != 0: sys.stderr.write('must be root to run this command\n') return 1 try: from ifupdown2.ifupdown.main import Ifupdown2 from ifupdown2.lib.nlcache import NetlinkListenerWithCache, NetlinkListenerWithCacheErrorNotInitialized except: from ifupdown.main import Ifupdown2 from lib.nlcache import NetlinkListenerWithCache, NetlinkListenerWithCacheErrorNotInitialized ifupdown2 = Ifupdown2(daemon=False, uid=os.geteuid()) try: ifupdown2.parse_argv(sys.argv) LogManager.get_instance().start_standalone_logging(ifupdown2.args) except ArgvParseHelp: # on --help parse_args raises SystemExit, we catch it and raise a # custom exception ArgvParseHelp to return 0 return 0 try: status = ifupdown2.main() finally: try: NetlinkListenerWithCache.get_instance().cleanup() except NetlinkListenerWithCacheErrorNotInitialized: status = Status.Client.STATUS_NLERROR LogManager.get_instance().write("exit status %s" % status) return status
def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args): """ run dhcp configuration on the interface object passed as argument Args: **ifaceobj** (object): iface object **operation** (str): any of 'up', 'down', 'query-checkcurr', 'query-running' Kwargs: **query_ifaceobj** (object): query check ifaceobject. This is only valid when op is 'query-checkcurr'. It is an object same as ifaceobj, but contains running attribute values and its config status. The modules can use it to return queried running state of interfaces. status is success if the running state is same as user required state in ifaceobj. error otherwise. """ op_handler = self._run_ops.get(operation) if not op_handler: return try: if (operation != 'query-running' and (ifaceobj.addr_method != 'dhcp' and ifaceobj.addr_method != 'dhcp6')): return except: return if not self.is_dhcp_allowed_on(ifaceobj, syntax_check=False): return disable_syslog_on_exit = False try: if not ifupdownflags.flags.PERFMODE: disable_syslog_on_exit = not LogManager.get_instance( ).is_syslog_enabled_syslog() LogManager.get_instance().enable_syslog() self.logger.info("%s: enabling syslog for dhcp configuration" % ifaceobj.name) if operation == 'query-checkcurr': op_handler(self, ifaceobj, query_ifaceobj) else: op_handler(self, ifaceobj) finally: if disable_syslog_on_exit: LogManager.get_instance().disable_syslog()
def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args): """ run dhcp configuration on the interface object passed as argument Args: **ifaceobj** (object): iface object **operation** (str): any of 'up', 'down', 'query-checkcurr', 'query-running' Kwargs: **query_ifaceobj** (object): query check ifaceobject. This is only valid when op is 'query-checkcurr'. It is an object same as ifaceobj, but contains running attribute values and its config status. The modules can use it to return queried running state of interfaces. status is success if the running state is same as user required state in ifaceobj. error otherwise. """ op_handler = self._run_ops.get(operation) if not op_handler: return try: if (operation != 'query-running' and (ifaceobj.addr_method != 'dhcp' and ifaceobj.addr_method != 'dhcp6')): return except Exception: return if not self.is_dhcp_allowed_on(ifaceobj, syntax_check=False): return log_manager = LogManager.get_instance() syslog_log_level = logging.INFO disable_syslog_on_exit = None if operation in ["up", "down"]: # if syslog is already enabled we shouldn't disable it if log_manager.is_syslog_enabled(): # save current syslog level syslog_log_level = log_manager.get_syslog_log_level() # prevent syslog from being disabled on exit disable_syslog_on_exit = False else: # enabling syslog log_manager.enable_syslog() # syslog will be disabled once we are done disable_syslog_on_exit = True # update the current syslog handler log level if higher than INFO if syslog_log_level >= logging.INFO: log_manager.set_level_syslog(logging.INFO) self.logger.info("%s: enabling syslog for dhcp configuration" % ifaceobj.name) try: if operation == 'query-checkcurr': op_handler(self, ifaceobj, query_ifaceobj) else: op_handler(self, ifaceobj) finally: # disable syslog handler or re-set the proper log-level if disable_syslog_on_exit is True: log_manager.get_instance().disable_syslog() elif disable_syslog_on_exit is False: log_manager.set_level_syslog(syslog_log_level)
# # ifupdown2 - Network Manager # import os import sys try: from ifupdown2.lib.log import LogManager, root_logger from ifupdown2.lib.status import Status except: from lib.log import LogManager, root_logger from lib.status import Status # first thing first, setup the logging infra LogManager.get_instance() try: import ifupdown2.ifupdown.config as config from ifupdown2 import __version__ config.__version__ = __version__ from ifupdown2.lib.exceptions import ExitWithStatus, ExitWithStatusAndError from ifupdown2.ifupdown.client import Client from ifupdown2.ifupdown.exceptions import ArgvParseHelp except: import ifupdown.config as config
def __init__(self, argv): SocketIO.__init__(self) # we setup our log receiver which reads LogRecord from a socket # thus handing the logging-handling to the logging module and it's # dedicated classes. self.socket_receiver = LogRecordSocketReceiver() self.stdin = None self.argv = argv # First we need to set the correct log level for the client. # Unfortunately the only reliable way to do this is to use our main # argument parser. We can't simply have a parse to catch -v and -d, a # simple command like "ifup -av" wouldn't be recognized... # Ideally it would be great to be able to send the Namespace returned by # parse_args, and send it on the socket in pickle format. Unfortunately # this might be a serious security issue. It needs to be studied and # evaluated a bit more, that way we would save time and resources only # parsing argv once. args_parse = Parse(argv) args_parse.validate() # store the args namespace to send it to the daemon, we don't want # the daemon to spend time to parsing argv again... self.args = args_parse.get_args() LogManager.get_instance().start_client_logging(self.args) root_logger.info("starting ifupdown2 client...") self.uds = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: self.uds.connect("/var/run/ifupdown2d/uds") except socket.error: self.__shutdown() sys.stderr.write(""" ERROR: %s could not connect to ifupdown2 daemon Try starting ifupdown2 daemon with: sudo systemctl start ifupdown2 To configure ifupdown2d to start when the box boots: sudo systemctl enable ifupdown2\n\n""" % argv[0]) raise ExitWithStatus(status=Status.Client.STATUS_COULD_NOT_CONNECT) signal.signal(signal.SIGINT, self.__signal_handler) signal.signal(signal.SIGTERM, self.__signal_handler) signal.signal(signal.SIGQUIT, self.__signal_handler) try: self.SO_PEERCRED = socket.SO_PEERCRED except AttributeError: # powerpc is the only non-generic we care about. alpha, mips, # sparc, and parisc also have non-generic values. machine = os.uname()[4] if re.search(r"^(ppc|powerpc)", machine): self.SO_PASSCRED = 20 self.SO_PEERCRED = 21 else: self.SO_PASSCRED = 16 self.SO_PEERCRED = 17 try: self.uds.setsockopt(socket.SOL_SOCKET, self.SO_PASSCRED, 1) except Exception as e: self.__shutdown() raise Exception("setsockopt: %s" % str(e)) self.daemon_pid, _, _ = self.get_socket_peer_cred(self.uds) if self.daemon_pid < 0: self.__shutdown() raise ExitWithStatusAndError( status=Status.Client.STATUS_NO_PID, message="could not get ifupdown2 daemon PID") root_logger.info( "connection to ifupdown2d successful (server pid %s)" % self.daemon_pid)