当前位置: 首页>>代码示例>>Python>>正文


Python grp.getgrnam方法代码示例

本文整理汇总了Python中grp.getgrnam方法的典型用法代码示例。如果您正苦于以下问题:Python grp.getgrnam方法的具体用法?Python grp.getgrnam怎么用?Python grp.getgrnam使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在grp的用法示例。


在下文中一共展示了grp.getgrnam方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: drop_privileges

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def drop_privileges():
    from certidude import config
    import pwd
    _, _, uid, gid, gecos, root, shell = pwd.getpwnam("certidude")
    restricted_groups = []
    restricted_groups.append(gid)

    # PAM needs access to /etc/shadow
    if config.AUTHENTICATION_BACKENDS == {"pam"}:
        import grp
        name, passwd, num, mem = grp.getgrnam("shadow")
        click.echo("Adding current user to shadow group due to PAM authentication backend")
        restricted_groups.append(num)

    os.setgroups(restricted_groups)
    os.setgid(gid)
    os.setuid(uid)
    click.echo("Switched %s (pid=%d) to user %s (uid=%d, gid=%d); member of groups %s" %
        (getproctitle(), os.getpid(), "certidude", os.getuid(), os.getgid(), ", ".join([str(j) for j in os.getgroups()])))
    os.umask(0o007) 
开发者ID:laurivosandi,项目名称:certidude,代码行数:22,代码来源:common.py

示例2: chown

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def chown(self, tarinfo, targetpath):
        """Set owner of targetpath according to tarinfo.
        """
        if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
            # We have to be root to do so.
            try:
                g = grp.getgrnam(tarinfo.gname)[2]
            except KeyError:
                g = tarinfo.gid
            try:
                u = pwd.getpwnam(tarinfo.uname)[2]
            except KeyError:
                u = tarinfo.uid
            try:
                if tarinfo.issym() and hasattr(os, "lchown"):
                    os.lchown(targetpath, u, g)
                else:
                    if sys.platform != "os2emx":
                        os.chown(targetpath, u, g)
            except EnvironmentError as e:
                raise ExtractError("could not change owner") 
开发者ID:war-and-code,项目名称:jawfish,代码行数:23,代码来源:tarfile.py

示例3: chown_path

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def chown_path(path, user=None, group=None):
    user_unsudoed = get_user_unsudoed()
    if not user:
        user = user_unsudoed
    if not group:
        group = user_unsudoed
    try:
        uid = pwd.getpwnam(user).pw_uid
        gid = grp.getgrnam(group).gr_gid
        os.chown(path, uid, gid)
    except KeyError as e:
        from kano.logging import logger
        logger.error(
            'user {} or group {} do not match with existing'.format(user, group))
        ret_val = False
    except OSError as e:
        from kano.logging import logger
        logger.error(
            'Error while trying to chown, root priviledges needed {}'.format(e))
        ret_val = False
    else:
        ret_val = True
    return ret_val 
开发者ID:KanoComputing,项目名称:kano-toolset,代码行数:25,代码来源:file_operations.py

示例4: check_for_writable_imgdir

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def check_for_writable_imgdir():
    c.debug("Testing write perms by creating tempfile in {}".format(cfg.opts.img_dir))
    try:
        f = tempfile.TemporaryFile(dir=cfg.opts.img_dir)
        f.close()
    except:
        dirstat = os.stat(cfg.opts.img_dir)
        user = pwd.getpwuid(dirstat.st_uid).pw_name
        group = grp.getgrgid(dirstat.st_gid).gr_name
        print(c.RED("Unable to create new file in image dir '{}' owned by {}:{}".format(cfg.opts.img_dir, user, group)))
        if myUser in grp.getgrnam(group).gr_mem:
            print("Your user ({}) *is* a member of the appropriate group ({}); however ...\n"
                  "Your current login session is not running with that group credential\n"
                  "To fix this, open a new session (ssh/su -) or log out & log back in (GUI)".format(myUser, group))
        else:
            print("Either fix directory permissions as root or specify alternate dir with '--img-dir' option")
        exit(1) 
开发者ID:ryran,项目名称:upvm,代码行数:19,代码来源:sysvalidator.py

示例5: chown

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def chown(self, tarinfo, targetpath):
        """Set owner of targetpath according to tarinfo.
        """
        if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
            # We have to be root to do so.
            try:
                g = grp.getgrnam(tarinfo.gname)[2]
            except KeyError:
                g = tarinfo.gid
            try:
                u = pwd.getpwnam(tarinfo.uname)[2]
            except KeyError:
                u = tarinfo.uid
            try:
                if tarinfo.issym() and hasattr(os, "lchown"):
                    os.lchown(targetpath, u, g)
                else:
                    if sys.platform != "os2emx":
                        os.chown(targetpath, u, g)
            except EnvironmentError, e:
                raise ExtractError("could not change owner") 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:23,代码来源:tarfile.py

示例6: chown

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def chown(self, tarinfo, targetpath, numeric_owner):
        """Set owner of targetpath according to tarinfo. If numeric_owner
           is True, use .gid/.uid instead of .gname/.uname.
        """
        if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
            # We have to be root to do so.
            if numeric_owner:
                g = tarinfo.gid
                u = tarinfo.uid
            else:
                try:
                    g = grp.getgrnam(tarinfo.gname)[2]
                except KeyError:
                    g = tarinfo.gid
                try:
                    u = pwd.getpwnam(tarinfo.uname)[2]
                except KeyError:
                    u = tarinfo.uid
            try:
                if tarinfo.issym() and hasattr(os, "lchown"):
                    os.lchown(targetpath, u, g)
                else:
                    os.chown(targetpath, u, g)
            except OSError as e:
                raise ExtractError("could not change owner") 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:27,代码来源:tarfile.py

示例7: prepare

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def prepare():
	# create control file if necessary
	if not os.path.exists(filepath):
		f = file(filepath, "w")
		f.write("255") # continous lit
		f.close()

		# fix ownership
		os.chown(filepath, pwd.getpwnam(uid).pw_uid, grp.getgrnam(gid).gr_gid)
		os.chmod(filepath, 0o666)
		
	# setup manual led control
	with open(ledpath + "trigger", "w") as trigger:
		trigger.write("none")
		
	# disable LED
	with open(ledpath + "brightness", "w") as brightness:
		brightness.write("1") 
开发者ID:RoganDawes,项目名称:P4wnP1,代码行数:20,代码来源:ledtool.py

示例8: gidFromString

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def gidFromString(gidString):
    """
    Convert a group identifier, as a string, into an integer GID.

    @type uid: C{str}
    @param uid: A string giving the base-ten representation of a GID or the
        name of a group which can be converted to a GID via L{grp.getgrnam}.

    @rtype: C{int}
    @return: The integer GID corresponding to the given string.

    @raise ValueError: If the group name is supplied and L{grp} is not
        available.
    """
    try:
        return int(gidString)
    except ValueError:
        if grp is None:
            raise
        return grp.getgrnam(gidString)[2] 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:22,代码来源:util.py

示例9: shutil_setuid

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def shutil_setuid(user = None, group = None):
    """ set fork-child uid/gid (returns pw-info env-settings)"""
    if group:
        import grp
        gid = grp.getgrnam(group).gr_gid
        os.setgid(gid)
        logg.debug("setgid %s '%s'", gid, group)
    if user:
        import pwd
        pw = pwd.getpwnam(user)
        if not group:
            gid = pw.pw_gid
            os.setgid(gid)
            logg.debug("setgid %s", gid)
        uid = pw.pw_uid
        os.setuid(uid)
        logg.debug("setuid %s '%s'", uid, user)
        home = pw.pw_dir
        shell = pw.pw_shell
        logname = pw.pw_name
        return { "USER": user, "LOGNAME": logname, "HOME": home, "SHELL": shell }
    return {} 
开发者ID:vanilla,项目名称:vanilla-docker,代码行数:24,代码来源:systemctl3.py

示例10: start_daemon

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def start_daemon(*command, input=None, **kwargs):
        """Start a daemon for the VM

        This function take care to run it as appropriate user.

        :param command: command to run (array for
            :py:meth:`subprocess.check_call`)
        :param kwargs: args for :py:meth:`subprocess.check_call`
        :return: None
        """  # pylint: disable=redefined-builtin

        if os.getuid() == 0:
            # try to always have VM daemons running as normal user, otherwise
            # some files (like clipboard) may be created as root and cause
            # permission problems
            qubes_group = grp.getgrnam('qubes')
            command = ['runuser', '-u', qubes_group.gr_mem[0], '--'] + \
                      list(command)
        p = yield from asyncio.create_subprocess_exec(*command, **kwargs)
        stdout, stderr = yield from p.communicate(input=input)
        if p.returncode:
            raise subprocess.CalledProcessError(p.returncode, command,
                                                output=stdout, stderr=stderr) 
开发者ID:QubesOS,项目名称:qubes-core-admin,代码行数:25,代码来源:qubesvm.py

示例11: gid

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def gid(self, val):
        if val is not None:
            if grp is None:
                self.bus.log('grp module not available; ignoring gid.',
                             level=30)
                val = None
            elif isinstance(val, text_or_bytes):
                val = grp.getgrnam(val)[2]
        self._gid = val 
开发者ID:cherrypy,项目名称:cherrypy,代码行数:11,代码来源:plugins.py

示例12: filter_admins

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def filter_admins(self):
        _, _, gid, members = grp.getgrnam(config.ADMIN_GROUP)
        for username in members:
            yield self.get(username) 
开发者ID:laurivosandi,项目名称:certidude,代码行数:6,代码来源:user.py

示例13: is_admin

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def is_admin(self, user):
        import grp
        _, _, gid, members = grp.getgrnam(config.ADMIN_GROUP)
        return user.name in members 
开发者ID:laurivosandi,项目名称:certidude,代码行数:6,代码来源:user.py

示例14: all

# 需要导入模块: import grp [as 别名]
# 或者: from grp import getgrnam [as 别名]
def all(self):
        _, _, gid, members = grp.getgrnam(config.USERS_GROUP)
        for username in members:
            yield self.get(username)
        for user in self.filter_admins(): # TODO: dedup
            yield user 
开发者ID:laurivosandi,项目名称:certidude,代码行数:8,代码来源:user.py


注:本文中的grp.getgrnam方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。