本文整理汇总了Python中os.setreuid方法的典型用法代码示例。如果您正苦于以下问题:Python os.setreuid方法的具体用法?Python os.setreuid怎么用?Python os.setreuid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.setreuid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mockPTYSetUidInParent
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_mockPTYSetUidInParent(self):
"""
Try creating a PTY process with setting its uid, in the parent path: it
should switch to root before fork, then restore initial uid/gids.
"""
self.mockos.child = False
cmd = '/mock/ouch'
d = defer.Deferred()
p = TrivialProcessProtocol(d)
oldPTYProcess = process.PTYProcess
try:
process.PTYProcess = DumbPTYProcess
reactor.spawnProcess(p, cmd, ['ouch'], env=None,
usePTY=True, uid=8080)
finally:
process.PTYProcess = oldPTYProcess
self.assertEquals(self.mockos.actions,
[('setuid', 0), ('setgid', 0), ('fork', False),
('setregid', 1235, 1234), ('setreuid', 1237, 1236), 'waitpid'])
示例2: test_setreuid
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_setreuid(self):
if os.getuid() != 0:
self.assertRaises(os.error, os.setreuid, 0, 0)
self.assertRaises(TypeError, os.setreuid, 'not an int', 0)
self.assertRaises(TypeError, os.setreuid, 0, 'not an int')
self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
示例3: test_setreuid_neg1
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_setreuid_neg1(self):
# Needs to accept -1. We run this in a subprocess to avoid
# altering the test runner's process state (issue8045).
subprocess.check_call([
sys.executable, '-c',
'import os,sys;os.setreuid(-1,-1);sys.exit(0)'])
示例4: test_setreuid
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_setreuid(self):
if os.getuid() != 0:
self.assertRaises(os.error, os.setreuid, 0, 0)
self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
示例5: test_setreuid_neg1
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_setreuid_neg1(self):
# Needs to accept -1. We run this in a subprocess to avoid
# altering the test runner's process state (issue8045).
subprocess.check_call([
sys.executable, '-c',
'import os,sys;os.setreuid(-1,-1);sys.exit(0)'])
示例6: test_setreuid
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_setreuid(self):
if os.getuid() != 0:
self.assertRaises(os.error, os.setreuid, 0, 0)
self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
示例7: setreuid
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def setreuid(self, val1, val2):
"""
Override C{os.setreuid}. Save the action.
"""
self.actions.append(('setreuid', val1, val2))
示例8: drop_privs
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def drop_privs():
try:
new_uid = int(os.getenv('SUDO_UID'))
new_gid = int(os.getenv('SUDO_GID'))
except TypeError:
# they were running directly from a root user and didn't have
# sudo env variables
print """[!] WARNING: Couldn't drop privileges! To avoid this error, run from a non-root user.
You may also use sudo, from a non-root user. Continue? (y/n)""",
if raw_input().lower()[0] == 'y':
return
die()
debug.info('Dropping privileges to uid: {}, gid: {}'.format(new_uid,
new_gid))
# drop group before user, because otherwise you're not privileged enough
# to drop group
os.setgroups([])
os.setregid(new_gid, new_gid)
os.setreuid(new_uid, new_uid)
# check to make sure we can't re-escalate
try:
os.seteuid(0)
print '[!] WARNING: Failed to drop privileges! Continue? (y/n)',
if raw_input().lower()[0] != 'y':
die()
except OSError:
return
示例9: test_setreuid
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_setreuid(self):
if os.getuid() != 0:
self.assertRaises(OSError, os.setreuid, 0, 0)
self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
示例10: condDropPrivs
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def condDropPrivs(uid, gid):
if gid is not None:
os.setregid(gid, gid)
if uid is not None:
os.setreuid(uid, uid)
示例11: dropPrivsForever
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def dropPrivsForever(self):
self._elevatePrivs()
os.setregid(self.unprivGid, self.unprivGid)
os.setreuid(self.unprivUid, self.unprivUid)
示例12: _safe_child
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def _safe_child(to_exec, q, uid, gid):
try:
os.setgroups([])
os.setregid(gid, gid)
os.setreuid(uid, uid)
res = subprocess.check_output(to_exec, stderr=open(os.devnull, 'w'))
q.put(res)
except Exception as e:
q.put(e)
示例13: test_mockSetUidInParent
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_mockSetUidInParent(self):
"""
Try creating a process with setting its uid, in the parent path: it
should switch to root before fork, then restore initial uid/gids.
"""
self.mockos.child = False
cmd = '/mock/ouch'
d = defer.Deferred()
p = TrivialProcessProtocol(d)
reactor.spawnProcess(p, cmd, ['ouch'], env=None,
usePTY=False, uid=8080)
self.assertEquals(self.mockos.actions,
[('setuid', 0), ('setgid', 0), ('fork', False),
('setregid', 1235, 1234), ('setreuid', 1237, 1236), 'waitpid'])
示例14: test_mockErrorInForkRestoreUID
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def test_mockErrorInForkRestoreUID(self):
"""
If C{os.fork} raises an exception and a UID change has been made, the
previous UID and GID are restored.
"""
self.mockos.raiseFork = OSError(errno.EAGAIN, None)
protocol = TrivialProcessProtocol(None)
self.assertRaises(OSError, reactor.spawnProcess, protocol, None,
uid=8080)
self.assertEqual(self.mockos.actions,
[('setuid', 0), ('setgid', 0), ("fork", False),
('setregid', 1235, 1234), ('setreuid', 1237, 1236)])
示例15: setProcessLimits
# 需要导入模块: import os [as 别名]
# 或者: from os import setreuid [as 别名]
def setProcessLimits(x):
# This is called after fork and before exec. Any messages
# printed here will look like the program that we are calling
# printed them out.
#print("pre switch user")
if switchUser:
if os.geteuid() != 0:
print("We can't switch to a different user if this script is not run as root.")
exit(1)
# If we are supposed to switch to a different user account to do the grading
if os.geteuid() == 0:
os.setreuid(autograderUid,autograderUid)
if os.geteuid() == 0:
print("Still root after trying to switch to autograder user?")
exit(1)
else:
# If we are not switching to a different user, make sure that we don't run as root.
if os.geteuid() == 0:
print("Halting. Do not run submitted programs as root.")
exit(1)
#print("post switch user")
#print("Preexec start")
if os.setpgrp() == -1: # put all processes in the same process group so we can kill it and all children it creates.
print("Failed to set process group!")
#print("Preexec middle")
def limitHelper(limitType, limit):
# limit is a string referring to a previously set environment variable.
if limit in os.environ:
limit = int(os.environ[limit])
(soft, hard) = resource.getrlimit(limitType)
#print("soft %d, hard %d, requested %d\n" % (soft, hard, limit))
if hard > 0 and limit > hard:
limit = hard
resource.setrlimit(limitType, (limit, limit))
limitHelper(resource.RLIMIT_NPROC, "ULIMIT_NPROC")
limitHelper(resource.RLIMIT_AS, "ULIMIT_AS")
limitHelper(resource.RLIMIT_DATA, "ULIMIT_DATA")