本文整理汇总了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)
示例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")
示例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
示例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)
示例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")
示例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")
示例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")
示例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]
示例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 {}
示例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)
示例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
示例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)
示例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
示例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