Ejemplo n.º 1
0
    def unCache(self, serviceName):
        '''Bypass the cache and read the service configuration from disk

        :param serviceName: 

        '''
        success = False
        #####
        # Input validation.
        if not isinstance(serviceName, str):
            return success

        cmd = {"uncache": [serviceName]}
        success, stdout, stderr, retcode = self.runSubCommand(cmd)

        if str(retcode) != '0':
            success = False
            self.logger.log(
                lp.DEBUG,
                reportStack() + "- success: " + str(success) + " stdout: " +
                str(stdout) + " stderr: " + str(stderr) + " retcode: " +
                str(retcode))
        else:
            self.logger.log(
                lp.DEBUG,
                reportStack() + "- success: " + str(success) + " stdout: " +
                str(stdout) + " stderr: " + str(stderr) + " retcode: " +
                str(retcode))
            success = True
        return success
Ejemplo n.º 2
0
    def asUser(self, uid, command, args=[]):
        '''@note: From the launchctl man page:
          asuser UID command [args]
              This executes the given command in as similar an execution con-
              text as possible to that of the target user's bootstrap.
              Adopted attributes include the Mach bootstrap namespace, excep-
              tion server and security audit session. It does not modify the
              process' credentials (UID, GID, etc.) or adopt any user-spe-
              cific environment variables. It affects only the Mach bootstrap
              context and directly- related attributes.
        
        @author: Roy Nielsen

        :param uid: 
        :param command: 
        :param args:  (Default value = [])

        '''
        success = False
        #####
        # Input validation.
        if not isinstance(uid, int) or \
           not isinstance(command, str) or \
           not isinstance(args, list):
            return success

        cmd = {"asuser": [uid, command] + args}
        success, stdout, stderr, retcode = self.runSubCommand(cmd)

        if retcode != '0':
            raise ValueError(reportStack() + "- success: " + str(success) +
                             " stdout: " + str(stdout) + " stderr: " +
                             str(stderr) + " retcode: " + str(retcode))
        return success
Ejemplo n.º 3
0
    def bootOut(self, domainTarget="", servicePath=''):
        '''@note: From the launchctl man page:
          bootstrap | bootout domain-target [service-path service-path2 ...] |
              service-target
              Bootstraps or removes domains and services. Services may be
              specified as a series of paths or a service identifier. Paths
              may point to XPC service bundles, launchd.plist(5) s, or a
              directories containing a collection of either. If there were
              one or more errors while bootstrapping or removing a collection
              of services, the problematic paths will be printed with the
              errors that occurred.
        
              If no paths or service target are specified, these commands can
              either bootstrap or remove a domain specified as a domain tar-
              get. Some domains will implicitly bootstrap pre-defined paths
              as part of their creation.
        
        @author: Roy Nielsen

        :param domainTarget:  (Default value = "")
        :param servicePath:  (Default value = '')

        '''
        success = False
        #####
        # Input validation.
        if not isinstance(domainTarget, str) or \
           not isinstance(servicePath, str):
            return success

        if servicePath and domainTarget:
            cmd = {"bootout": [domainTarget, servicePath]}
        elif domainTarget:
            cmd = {"bootout": [domainTarget]}
        else:
            return success

        success, stdout, stderr, retcode = self.runSubCommand(cmd)
        #####
        # errors that indicate the process is complete or in
        # progress
        if re.search("No such process", stderr) or \
           re.search("Operation now in progress", stderr):
            success = True

        if retcode != '0' and not success:
            self.logger.log(
                lp.DEBUG,
                reportStack() + "- success: " + str(success) + " stdout: " +
                str(stdout) + " stderr: " + str(stderr) + " retcode: " +
                str(retcode))
        for item in stderr:
            if item and re.search("Could not find specified service", item):
                success = True
                break
        return success
Ejemplo n.º 4
0
    def enable(self, serviceTarget, servicePath=''):
        '''From the launchctl man page:
          enable | disable service-target
              Enables or disables the service in the requested domain. Once a
              service is disabled, it cannot be loaded in the specified
              domain until it is once again enabled. This state persists
              across boots of the device. This subcommand may only target
              services within the system domain or user and user-login
              domains.

        :param serviceTarget: 
        :param servicePath:  (Default value = '')

        '''
        success = False
        #####
        # Input validation.
        if not isinstance(serviceTarget, str):
            return success

        if servicePath and isinstance(servicePath, str):
            cmd = {"enable": [serviceTarget, servicePath]}
        else:
            cmd = {"enable": [serviceTarget]}

        success, stdout, stderr, retcode = self.runSubCommand(cmd)
        if str(retcode) != '0':
            success = False
            self.logger.log(
                lp.DEBUG,
                reportStack() + "- success: " + str(success) + " stdout: " +
                str(stdout) + " stderr: " + str(stderr) + " retcode: " +
                str(retcode))
        else:
            self.logger.log(
                lp.DEBUG,
                reportStack() + "- success: " + str(success) + " stdout: " +
                str(stdout) + " stderr: " + str(stderr) + " retcode: " +
                str(retcode))
            success = True
        return success
Ejemplo n.º 5
0
    def targetValid(self, service, **kwargs):
        '''Validate a service or domain target, possibly via
        servicename|serviceName|servicetarget|serviceTarget|domaintarget|domainTarget.

        :param service: 
        :param **kwargs: 
        :returns: the value of one of the above as "target", in the order
                found below.
        
        @author: Roy Nielsen

        '''
        serviceName = False
        if 'servicename' in kwargs:
            serviceName = kwargs.get('servicename')
        elif 'serviceName' in kwargs:
            serviceName = kwargs.get('serviceName')
        elif 'servicetarget' in kwargs:
            serviceName = kwargs.get('servicetarget')
        elif 'serviceTarget' in kwargs:
            serviceName = kwargs.get('serviceTarget')
        elif 'domaintarget' in kwargs:
            serviceName = kwargs.get('domaintarget')
        elif 'domainTarget' in kwargs:
            serviceName = kwargs.get('domainTarget')
        else:
            self.logger.log(lp.DEBUG, reportStack(2) +
                            "Either the service (full " +
                            "path to the service) or One of 'servicename', " +
                            "'serviceName', 'serviceTarget'" +
                            ", 'domainTarget', 'servicetarget', " +
                            "'domaintarget' are expected for this method.")

        if not isinstance(serviceName, str) or not serviceName or \
           not re.match("^[A-Za-z]+[A-Za-z0-9_\-\.]*$", serviceName):
            serviceName = self.getServiceNameFromService(service)

        user = False
        userUid = False
        target = ""
        if serviceName:
            if 'LaunchDaemon' in service:
                target = 'system/' + serviceName
            if 'LaunchAgent' in service:
                user = findUserLoggedIn(self.logger)
                if user:
                    userUid = pwd.getpwnam(user).pw_uid
                if userUid:
                    target = 'gui/' + str(userUid) + '/' + serviceName

                target = target.strip()
        return target
Ejemplo n.º 6
0
    def targetValid(self, service, **kwargs):
        """Validate a service or domain target, possibly via
        servicename|serviceName|servicetarget|serviceTarget|domaintarget|domainTarget.

        :param service: 
        :param **kwargs: 
        :returns: the value of one of the above as "target", in the order
                found below.
        
        @author: Roy Nielsen

        """

        if service:
            pass
        if 'servicename' in kwargs:
            target = kwargs.get('servicename')
        elif 'serviceName' in kwargs:
            target = kwargs.get('serviceName')
        elif 'serviceTarget' in kwargs:
            target = kwargs.get('serviceTarget')
        elif 'domainTarget' in kwargs:
            target = kwargs.get('domainTarget')
        elif 'serviceTarget' in kwargs:
            target = kwargs.get('servicetarget')
        elif 'domaintarget' in kwargs:
            target = kwargs.get('domaintarget')
        else:
            raise ValueError(
                reportStack(2) + "Either the service (full " +
                "path to the service) or One of 'servicename', " +
                "'serviceName', 'serviceTarget'" +
                ", 'domainTarget', 'servicetarget', " +
                "'domaintarget' are required for this method.")

        return target