Exemplo n.º 1
0
def is_admin(user=lib.global_parameters['USER'], activeroot='/'):
    """ Check if the specified user has administrative privileges on given system"""
    platform = get_system_type_from_active_root(activeroot)
    ip = get_address_from_active_root(activeroot)
    if activeroot.startswith('/'):
        if platform.startswith('linux'):
            # on linux check if euid is 0
            from pwd import getpwnam
            if getpwnam(user).pw_uid == 0:
                return True
            return False

        elif platform.startswith('win'):
            from win32net import NetUserGetLocalGroups
            return 'Administrators' in NetUserGetLocalGroups(
                ip, user)  # should work on remote systems too
            # on Windows only admins can write to C:\Windows\temp
            #if os.access(os.path.join(os.environ.get('SystemRoot', 'C:\\Windows'), 'temp'), os.W_OK):
            #    return True
            #return False
        else:
            log.warn(
                'Cannot check root privileges, platform is not fully supported (%s).'
                % platform)
            return False
    else:
        log.warn(
            'Cannot check root privileges, remote system analysis (%s) not supported yet.'
            % activeroot)
        return False
Exemplo n.º 2
0
def is_admin(user=lib.global_parameters['USER'], activeroot='/'):
    """ Check if the specified user has administrative privileges on given system"""
    platform = get_system_type_from_active_root(activeroot)
    ip = get_address_from_active_root(activeroot)
    if activeroot.startswith('/'):
        if platform.startswith('linux'):
            # on linux check if euid is 0
            from pwd import getpwnam
            if getpwnam(user).pw_uid == 0:
                return True
            return False
        
        elif platform.startswith('win'):
            from win32net import NetUserGetLocalGroups
            return 'Administrators' in NetUserGetLocalGroups(ip, user) # should work on remote systems too
            # on Windows only admins can write to C:\Windows\temp
            #if os.access(os.path.join(os.environ.get('SystemRoot', 'C:\\Windows'), 'temp'), os.W_OK):
            #    return True
            #return False
        else:
            log.warn('Cannot check root privileges, platform is not fully supported (%s).' % platform)
            return False
    else:
        log.warn('Cannot check root privileges, remote system analysis (%s) not supported yet.' % activeroot)
        return False
Exemplo n.º 3
0
def command_exists(activeroot, c):
    """ Check if a given command exists """
    if 'linux' in get_system_type_from_active_root(activeroot):
        # get path to the command
        com = command('which %s' % (c))
        return len(com) > 0
    else:
        log.warn('Cannot determine \'%s\' existence.' % c)
        return False
Exemplo n.º 4
0
def command_exists(activeroot, c):
    """ Check if a given command exists """
    if 'linux' in get_system_type_from_active_root(activeroot):
        # get path to the command
        com = command('which %s' % (c))
        return len(com) > 0
    # TODO more system types
    else:
        log.warn('Cannot determine \'%s\' existence.' % c)
        return False
Exemplo n.º 5
0
def load_modules():
    """ Import modules from source/modules/ folder """
    lib.module_objects = []
    lib.modules = {}
    module_names = [x[:-3] for x in os.listdir('source/modules') if x[0]!='_' and x[-3:] == '.py']

    # import/reimport modules
    for m in module_names:
        if 'source.modules.' + m in sys.modules:
            imp.reload(sys.modules['source.modules.' + m]) # TODO deprecated?
        else:
            importlib.import_module('source.modules.' + m)
    
    # initialize modules dictionary
    for v in lib.module_objects:
        if v.name in lib.modules:
            log.warn('Duplicit module %s.' % (v.name))
        lib.modules[v.name] = v 
    
    log.info('%d modules loaded.' % (len(lib.modules)))
Exemplo n.º 6
0
    def add(self, name, start, job, timeout=None, waitfor=None):
        if self.terminate:  # in terminating state, do not create anything new
            return None
        # add a new job
        self.lock.acquire()
        jobid = self.newid()  # get lowest unused id
        if waitfor is None:
            self.jobs[jobid] = Job(name, start, job, timeout)
            job.start()
            log.info('Module %s will run in the background with id %d.' %
                     (name, jobid))
        else:
            # what to wait for?
            ids_to_wait_for = []
            for x in waitfor:
                if type(x) == int:
                    ids_to_wait_for.append(x)
                elif type(x) == str:
                    if x.isdigit():
                        ids_to_wait_for.append(int(x))
                    else:
                        matches = search_abbr(x, lib.modules.keys())
                        ids_to_wait_for += [
                            k for k, v in self.jobs.items()
                            if v.name in matches
                        ]
                else:
                    log.warn(
                        'Could not process wait parameter \'%s\', ignoring...'
                        % (x))
                    continue
            ids_to_wait_for = list(set(ids_to_wait_for))
            self.waitjobs[jobid] = Job(name, start, job, timeout,
                                       ids_to_wait_for)
            log.info(
                'Module %s with id %d will be executed after following jobs finish: %s'
                % (name, jobid, ', '.join(map(str, ids_to_wait_for))))

        self.lock.release()
        return jobid
Exemplo n.º 7
0
def load_modules():
    """ Import modules from source/modules/ folder """
    lib.module_objects = []
    lib.modules = {}
    module_names = [
        x[:-3] for x in os.listdir('source/modules')
        if x[0] != '_' and x[-3:] == '.py'
    ]

    # import/reimport modules
    for m in module_names:
        if 'source.modules.' + m in sys.modules:
            imp.reload(sys.modules['source.modules.' + m])  # TODO deprecated?
        else:
            importlib.import_module('source.modules.' + m)

    # initialize modules dictionary
    for v in lib.module_objects:
        if v.name in lib.modules:
            log.warn('Duplicit module %s.' % (v.name))
        lib.modules[v.name] = v

    log.info('%d modules loaded.' % (len(lib.modules)))
Exemplo n.º 8
0
    def stop(self):
        # scheduler should terminate (program is going to exit)
        self.lock.acquire()
        # kick waiting jobs
        self.waitjobs = {}
        # terminate all jobs and user threads which supports it
        for j in self.jobs:
            if hasattr(self.jobs[j].job, 'stop'):
                self.jobs[j].job.stop()
                #log.info('Background job %d is going to terminate.' % j)
            else:
                log.warn('%s cannot be terminated by force.' %
                         (self.jobs[j].name))

        for u in self.user_threads:
            if hasattr(u, 'stop'):
                u.stop()

        #self.lock.release()
        while len(self.jobs) > 0 and len(
                self.user_threads) > 0:  # wait for everything to die
            time.sleep(0.5)
        self.terminate = True  # sets DIE flag for itself
        self.lock.release()