本文整理匯總了Python中grp.getgrall方法的典型用法代碼示例。如果您正苦於以下問題:Python grp.getgrall方法的具體用法?Python grp.getgrall怎麽用?Python grp.getgrall使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類grp
的用法示例。
在下文中一共展示了grp.getgrall方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _getgroups
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def _getgroups(uid):
"""
Return the primary and supplementary groups for the given UID.
@type uid: C{int}
"""
result = []
pwent = pwd.getpwuid(uid)
result.append(pwent.pw_gid)
for grent in grp.getgrall():
if pwent.pw_name in grent.gr_mem:
result.append(grent.gr_gid)
return result
示例2: __init__
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def __init__(self, username):
ConchUser.__init__(self)
self.username = username
self.pwdData = pwd.getpwnam(self.username)
l = [self.pwdData[3]]
for groupname, password, gid, userlist in grp.getgrall():
if username in userlist:
l.append(gid)
self.otherGroups = l
self.listeners = {} # Dict mapping (interface, port) -> listener
self.channelLookup.update(
{b"session": session.SSHSession,
b"direct-tcpip": forwarding.openConnectForwardingClient})
self.subsystemLookup.update(
{b"sftp": filetransfer.FileTransferServer})
示例3: test_restore_nonexistent_user_group
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def test_restore_nonexistent_user_group():
tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
try:
path = tmpdir + '/foo'
os.mkdir(path)
m = metadata.from_path(path, archive_path=path, save_symlinks=True)
WVPASSEQ(m.path, path)
junk,m.owner = max([(len(x.pw_name), x.pw_name + 'x')
for x in pwd.getpwall()])
junk,m.group = max([(len(x.gr_name), x.gr_name + 'x')
for x in grp.getgrall()])
WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=True), None)
WVPASSEQ(os.stat(path).st_uid, m.uid)
WVPASSEQ(os.stat(path).st_gid, m.gid)
WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=False), None)
WVPASSEQ(os.stat(path).st_uid, m.uid)
WVPASSEQ(os.stat(path).st_gid, m.gid)
finally:
subprocess.call(['rm', '-rf', tmpdir])
示例4: setup_uid_manager
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def setup_uid_manager(mockgid):
unprivUid = os.getuid()
unprivGid = os.getgid()
# sudo
if os.environ.get("SUDO_UID") is not None:
unprivUid = int(os.environ['SUDO_UID'])
os.setgroups((mockgid,))
unprivGid = int(os.environ['SUDO_GID'])
# consolehelper
if os.environ.get("USERHELPER_UID") is not None:
unprivUid = int(os.environ['USERHELPER_UID'])
unprivName = pwd.getpwuid(unprivUid).pw_name
secondary_groups = [g.gr_gid for g in grp.getgrall() if unprivName in g.gr_mem]
os.setgroups([mockgid] + secondary_groups)
unprivGid = pwd.getpwuid(unprivUid)[3]
uidManager = mockbuild.uid.UidManager(unprivUid, unprivGid)
return uidManager
示例5: drop_privileges
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def drop_privileges(user):
"""
Change the system user of the current python process.
It will only work if called as root or as the target user.
:param string user: target user
:raise KeyError: if the target user doesn't exists
:raise OSError: when the user change fails
"""
pw = pwd.getpwnam(user)
if pw.pw_uid == os.getuid():
return
groups = [e.gr_gid for e in grp.getgrall() if pw.pw_name in e.gr_mem]
groups.append(pw.pw_gid)
os.setgroups(groups)
os.setgid(pw.pw_gid)
os.setuid(pw.pw_uid)
os.environ['HOME'] = pw.pw_dir
示例6: make_preexec_fn
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def make_preexec_fn(self, cluster): # pragma: nocover
# Borrowed and modified from jupyterhub/spawner.py
pwnam = getpwnam(cluster.username)
uid = pwnam.pw_uid
gid = pwnam.pw_gid
groups = [g.gr_gid for g in grp.getgrall() if cluster.username in g.gr_mem]
workdir = cluster.state["workdir"]
def preexec():
os.setgid(gid)
try:
os.setgroups(groups)
except Exception as e:
print("Failed to set groups %s" % e, file=sys.stderr)
os.setuid(uid)
os.chdir(workdir)
return preexec
示例7: __init__
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def __init__(self, username):
ConchUser.__init__(self)
self.username = username
self.pwdData = pwd.getpwnam(self.username)
l = [self.pwdData[3]]
for groupname, password, gid, userlist in grp.getgrall():
if username in userlist:
l.append(gid)
self.otherGroups = l
self.listeners = {} # dict mapping (interface, port) -> listener
self.channelLookup.update(
{"session": session.SSHSession,
"direct-tcpip": forwarding.openConnectForwardingClient})
self.subsystemLookup.update(
{"sftp": filetransfer.FileTransferServer})
示例8: test_values
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def test_values(self):
entries = grp.getgrall()
for e in entries:
self.check_value(e)
if len(entries) > 1000: # Huge group file (NIS?) -- skip the rest
return
for e in entries:
e2 = grp.getgrgid(e.gr_gid)
self.check_value(e2)
self.assertEqual(e2.gr_gid, e.gr_gid)
e2 = grp.getgrnam(e.gr_name)
self.check_value(e2)
# There are instances where getgrall() returns group names in
# lowercase while getgrgid() returns proper casing.
# Discovered on Ubuntu 5.04 (custom).
self.assertEqual(e2.gr_name.lower(), e.gr_name.lower())
示例9: set_user
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def set_user(username):
if username is None:
return
import pwd
import grp
try:
pwrec = pwd.getpwnam(username)
except KeyError:
logging.error('user not found: %s' % username)
raise
user = pwrec[0]
uid = pwrec[2]
gid = pwrec[3]
cur_uid = os.getuid()
if uid == cur_uid:
return
if cur_uid != 0:
logging.error('can not set user as nonroot user')
# will raise later
# inspired by supervisor
if hasattr(os, 'setgroups'):
groups = [grprec[2] for grprec in grp.getgrall() if user in grprec[3]]
groups.insert(0, gid)
os.setgroups(groups)
os.setgid(gid)
os.setuid(uid)
示例10: setUp
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def setUp(self):
if POSIX:
import pwd
import grp
users = pwd.getpwall()
groups = grp.getgrall()
self.all_uids = set([x.pw_uid for x in users])
self.all_usernames = set([x.pw_name for x in users])
self.all_gids = set([x.gr_gid for x in groups])
示例11: _get_groups
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def _get_groups(self):
grplist = []
if self._groups is None:
# Is there a better/faster way than this?
name = self.name
for gent in grp.getgrall():
if name in gent.gr_mem:
grplist.append(gent.gr_gid)
self._groups = grplist
return self._groups
示例12: change_user_group
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def change_user_group(self, user, group):
if not user and not group:
return
import pwd, grp
uid = gid = None
if group:
try:
gid = int(group)
group = grp.getgrgid(gid).gr_name
except ValueError:
import grp
try:
entry = grp.getgrnam(group)
except KeyError:
raise BadCommand(
"Bad group: %r; no such group exists" % group)
gid = entry.gr_gid
try:
uid = int(user)
user = pwd.getpwuid(uid).pw_name
except ValueError:
try:
entry = pwd.getpwnam(user)
except KeyError:
raise BadCommand(
"Bad username: %r; no such user exists" % user)
if not gid:
gid = entry.pw_gid
uid = entry.pw_uid
if self.verbose > 0:
print('Changing user to %s:%s (%s:%s)' % (
user, group or '(unknown)', uid, gid))
if hasattr(os, 'initgroups'):
os.initgroups(user, gid)
else:
os.setgroups([e.gr_gid for e in grp.getgrall()
if user in e.gr_mem] + [gid])
if gid:
os.setgid(gid)
if uid:
os.setuid(uid)
示例13: check_write_permissions
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def check_write_permissions(user, path):
"""
Returns `True` if the given *user* has write permissions to *path*. *user*
can be a UID (int) or a username (string).
"""
import pwd, grp, stat
# Get the user's complete passwd record
if isinstance(user, int):
user = pwd.getpwuid(user)
else:
user = pwd.getpwnam(user)
if user.pw_uid == 0:
return True # Assume root can write to everything (NFS notwithstanding)
groups = [] # A combination of user's primary GID and supplemental groups
for group in grp.getgrall():
if user.pw_name in group.gr_mem:
groups.append(group.gr_gid)
if group.gr_gid == user.pw_gid:
groups.append(group.gr_gid)
st = os.stat(path)
other_write = bool(st.st_mode & stat.S_IWOTH)
if other_write:
return True # Read/write world!
owner_write = bool(st.st_mode & stat.S_IWUSR)
if st.st_uid == user.pw_uid and owner_write:
return True # User can write to their own file
group_write = bool(st.st_mode & stat.S_IWGRP)
if st.st_gid in groups and group_write:
return True # User belongs to a group that can write to the file
return False
示例14: add
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def add(self):
"""Add group."""
shell("groupadd {0}".format(self.name))
self.update()
for x in grp.getgrall():
if x.gr_name == self.name:
self.gid = x.gr_gid
示例15: get_system
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrall [as 別名]
def get_system(gid=None):
"""
Get all system groups.
:param str gid: ID of single group to fetch
:returns: SystemGroup(s)
:rtype: SystemGroup or list thereof
"""
r = []
for x in grp.getgrall():
g = SystemGroup(name=x.gr_name, gid=x.gr_gid, users=x.gr_mem)
if gid == g.name:
return g
r.append(g)
return r if not gid else None