Exemplo n.º 1
0
 def __init__(self, data_dir="/usr/share/command-not-found"):
     self.programs = []
     self.priority_overrides = []
     p = os.path.join(data_dir, "priority.txt")
     if os.path.exists(p):
         with open(p) as priority_file:
             self.priority_overrides = [
                 line.strip() for line in priority_file
             ]
     self.components = [
         'main', 'universe', 'contrib', 'restricted', 'non-free',
         'multiverse'
     ]
     self.components.reverse()
     self.sources_list = self._getSourcesList()
     for filename in os.listdir(
             os.path.sep.join([data_dir, self.programs_dir])):
         self.programs.append(
             ProgramDatabase(
                 os.path.sep.join([data_dir, self.programs_dir, filename])))
     try:
         self.user_can_sudo = grp.getgrnam("sudo")[2] in posix.getgroups(
         ) or grp.getgrnam("admin")[2] in posix.getgroups()
     except KeyError:
         self.user_can_sudo = False
Exemplo n.º 2
0
def user_can_sudo():
    try:
        groups = posix.getgroups()
        return (grp.getgrnam("sudo")[2] in groups
                or grp.getgrnam("admin")[2] in groups)
    except KeyError:
        return False
Exemplo n.º 3
0
    def test_getgroups(self):
        with os.popen('id -G 2>/dev/null') as idg:
            groups = idg.read().strip()

        if not groups:
            # This test needs 'id -G'
            return

        # 'id -G' and 'os.getgroups()' should return the same
        # groups, ignoring order and duplicates.
        self.assertEqual(set([int(x) for x in groups.split()]),
                         set(posix.getgroups()))
Exemplo n.º 4
0
            def test_initgroups(self):
                # find missing group

                groups = sorted(self.saved_groups)
                for g1,g2 in zip(groups[:-1], groups[1:]):
                    g = g1 + 1
                    if g < g2:
                        break
                else:
                    g = g2 + 1
                name = pwd.getpwuid(posix.getuid()).pw_name
                posix.initgroups(name, g)
                self.assertIn(g, posix.getgroups())
Exemplo n.º 5
0
    def test_getgroups(self):
        with os.popen('id -G 2>/dev/null') as idg:
            groups = idg.read().strip()

        if not groups:
            # This test needs 'id -G'
            return

        # 'id -G' and 'os.getgroups()' should return the same
        # groups, ignoring order and duplicates.
        self.assertEqual(
                set([int(x) for x in groups.split()]),
                set(posix.getgroups()))
Exemplo n.º 6
0
            def test_initgroups(self):
                # find missing group

                groups = sorted(self.saved_groups)
                for g1, g2 in zip(groups[:-1], groups[1:]):
                    g = g1 + 1
                    if g < g2:
                        break
                else:
                    g = g2 + 1
                name = pwd.getpwuid(posix.getuid()).pw_name
                posix.initgroups(name, g)
                self.assertIn(g, posix.getgroups())
Exemplo n.º 7
0
    def __init__(self, provider_list, password_provider_cls):
        """
        Initialize a new RootViaSudoWithPassExecutionController

        :param provider_list:
            A list of Provider1 objects that will be available for script
            dependency resolutions. Currently all of the scripts are makedirs
            available but this will be refined to the minimal set later.
        :param password_provider_cls:
            A callable that will be used to obtain user's password.
            It is called when the controller runs a sudo command.
        """
        super().__init__(provider_list)
        try:
            in_sudo_group = grp.getgrnam("sudo").gr_gid in posix.getgroups()
        except KeyError:
            in_sudo_group = False
        try:
            in_admin_group = grp.getgrnam("admin").gr_gid in posix.getgroups()
        except KeyError:
            in_admin_group = False
        self.user_can_sudo = in_sudo_group or in_admin_group
        self._password_provider = password_provider_cls
Exemplo n.º 8
0
    def __init__(self, session_dir, provider_list):
        """
        Initialize a new RootViaSudoExecutionController

        :param session_dir:
            Base directory of the session this job will execute in.
            This directory is used to co-locate some data that is unique to
            this execution as well as data that is shared by all executions.
        """
        super().__init__(session_dir, provider_list)
        # Check if the user can use 'sudo' on this machine. This check is a bit
        # Ubuntu specific and can be wrong due to local configuration but
        # without a better API all we can do is guess.
        #
        # Shamelessly stolen from command-not-found
        try:
            in_sudo_group = grp.getgrnam("sudo").gr_gid in posix.getgroups()
        except KeyError:
            in_sudo_group = False
        try:
            in_admin_group = grp.getgrnam("admin").gr_gid in posix.getgroups()
        except KeyError:
            in_admin_group = False
        self.user_can_sudo = in_sudo_group or in_admin_group
Exemplo n.º 9
0
Arquivo: which.py Projeto: u-u-h/adpc
def which(s):
    for i in os.getenv('PATH').split(':'):
        x = os.path.normpath(os.path.join(i, s))
        try:
            st = os.stat(x)
        except:
            continue
        if not stat.S_ISREG(st[stat.ST_MODE]):
            continue
        r = stat.S_IMODE(st[stat.ST_MODE])
        uid = st[stat.ST_UID]
        gid = st[stat.ST_GID]
        if (r & 0001) or (gid in posix.getgroups() and r & 0010) \
          or (uid == posix.getuid() and r & 0100):
            return x
Exemplo n.º 10
0
    def __init__(self, session_dir, provider_list):
        """
        Initialize a new RootViaSudoExecutionController

        :param session_dir:
            Base directory of the session this job will execute in.
            This directory is used to co-locate some data that is unique to
            this execution as well as data that is shared by all executions.
        """
        super().__init__(session_dir, provider_list)
        # Check if the user can use 'sudo' on this machine. This check is a bit
        # Ubuntu specific and can be wrong due to local configuration but
        # without a better API all we can do is guess.
        #
        # Shamelessly stolen from command-not-found
        try:
            in_sudo_group = grp.getgrnam("sudo").gr_gid in posix.getgroups()
        except KeyError:
            in_sudo_group = False
        try:
            in_admin_group = grp.getgrnam("admin").gr_gid in posix.getgroups()
        except KeyError:
            in_admin_group = False
        self.user_can_sudo = in_sudo_group or in_admin_group
 def __init__(self, data_dir="/usr/share/command-not-found"):
     self.programs = []
     self.priority_overrides = []
     p = os.path.join(data_dir, "priority.txt")
     if os.path.exists(p):
         self.priority_overrides = map(string.strip, open(p).readlines())
     self.components = ['main', 'universe', 'contrib', 'restricted',
                        'non-free', 'multiverse']
     self.components.reverse()
     self.sources_list = self._getSourcesList()
     for filename in os.listdir(os.path.sep.join([data_dir, self.programs_dir])):
         self.programs.append(ProgramDatabase(os.path.sep.join([data_dir, self.programs_dir, filename])))
     try:
         self.user_can_sudo = grp.getgrnam("sudo")[2] in posix.getgroups() or grp.getgrnam("admin")[2] in posix.getgroups()
     except KeyError:
         self.user_can_sudo = False
Exemplo n.º 12
0
def posix_getgroups(space):
    """ posix_getgroups - Return the group set of the current process """
    arr_list = []
    for g in posix.getgroups():
        arr_list.append(space.newint(rffi.cast(lltype.Signed, g)))
    return space.new_array_from_list(arr_list)
Exemplo n.º 13
0
 def setUp(self):
     self.saved_groups = posix.getgroups()
Exemplo n.º 14
0
 def setUp(self):
     self.saved_groups = posix.getgroups()
Exemplo n.º 15
0
 def test_setgroups(self):
     for groups in [[0], range(16)]:
         posix.setgroups(groups)
         self.assertListEqual(groups, posix.getgroups())
Exemplo n.º 16
0
def posix_getgroups(space):
    """ posix_getgroups - Return the group set of the current process """
    arr_list = []
    for g in posix.getgroups():
        arr_list.append(space.newint(rffi.cast(lltype.Signed, g)))
    return space.new_array_from_list(arr_list)
Exemplo n.º 17
0
 def test_setgroups(self):
     for groups in [[0], range(16)]:
         posix.setgroups(groups)
         self.assertListEqual(groups, posix.getgroups())