本文整理汇总了Python中twisted.python.util.switchUID函数的典型用法代码示例。如果您正苦于以下问题:Python switchUID函数的具体用法?Python switchUID怎么用?Python switchUID使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了switchUID函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _execChild
def _execChild(self, path, settingUID, uid, gid, command, args, environment):
if path:
os.chdir(path)
# set the UID before I actually exec the process
if settingUID:
switchUID(uid, gid)
os.execvpe(command, args, environment)
示例2: cbLoggedIn
def cbLoggedIn(e, config):
mail = only(e, config.getLDAPAttributeMailbox())
username = mail.split('@', 1)[0]
hostname = mail.split('@', 1)[1]
username = quot(username)
hostname = quot(hostname)
userpad = (username+'__')[:2]
mailhost = only(e, config.getLDAPAttributeMailHost())
userdir = os.path.join(
config.getSpool(),
hostname,
mailhost,
userpad)
switchUID(uid=pwd.getpwnam('scalemail')[2],
gid=grp.getgrnam('scalemail')[2])
if not os.path.isdir(userdir):
os.mkdir(userdir, 0700)
os.chdir(userdir)
if not os.path.isdir(username):
initializeMaildir(username)
os.chdir(username)
os.execlp(sys.argv[1], *sys.argv[1:])
print >>sys.stderr, "scalemail-courier-login: Cannot exec command."
sys.exit(2)
示例3: test_uid
def test_uid(self):
"""
L{util.switchUID} calls L{util.initgroups} and then C{os.setuid} with
the given uid.
"""
util.switchUID(12000, None)
self.assertEqual(self.initgroupsCalls, [(12000, None)])
self.assertEqual(self.mockos.actions, [("setuid", 12000)])
示例4: test_euid
def test_euid(self):
"""
L{util.switchUID} calls L{util.initgroups} and then C{os.seteuid} with
the given uid if the C{euid} parameter is set to C{True}.
"""
util.switchUID(12000, None, True)
self.assertEqual(self.initgroupsCalls, [(12000, None)])
self.assertEqual(self.mockos.seteuidCalls, [12000])
示例5: main
def main():
usage = "%prog [options] ACTION"
epilog = """
ACTION is one of add|modify|remove|print
add: add a user record
modify: modify a user record
remove: remove a user record
"""
description = "Tool to manipulate CalendarServer augments XML file"
version = "%prog v1.0"
parser = OptionParser(usage=usage, description=description, version=version)
parser.epilog = epilog
parser.format_epilog = lambda _:epilog
parser.add_option("-f", "--file", dest="configfilename",
help="caldavd.plist defining Augment Service", metavar="FILE")
parser.add_option("-u", "--uid", dest="uid",
help="OD GUID to manipulate", metavar="UID")
parser.add_option("-i", "--uidfile", dest="uidfile",
help="File containing a list of GUIDs to manipulate", metavar="UIDFILE")
parser.add_option("-n", "--node", dest="node",
help="Partition node to assign to UID", metavar="NODE")
parser.add_option("-c", "--enable-calendar", action="store_true", dest="enable_calendar",
default=True, help="Enable calendaring for this UID: %default")
parser.add_option("-a", "--enable-addressbooks", action="store_true", dest="enable_addressbook",
default=True, help="Enable calendaring for this UID: %default")
parser.add_option("-s", "--auto-schedule", action="store_true", dest="auto_schedule",
default=False, help="Enable auto-schedule for this UID: %default")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
observer = StandardIOObserver()
observer.start()
#
# Get configuration
#
try:
loadConfig(options.configfilename)
setLogLevelForNamespace(None, "warn")
# Shed privileges
if config.UserName and config.GroupName and os.getuid() == 0:
uid = getpwnam(config.UserName).pw_uid
gid = getgrnam(config.GroupName).gr_gid
switchUID(uid, uid, gid)
os.umask(config.umask)
config.directory = getDirectory()
autoDisableMemcached(config)
except ConfigurationError, e:
usage("Unable to start: %s" % (e,))
示例6: switchUID
def switchUID(uid, gid):
"""
Switch uid and gid to what the user has specified on the command
line.
"""
if uid:
uid = util.uidFromString(uid)
if gid:
gid = util.gidFromString(gid)
util.switchUID(uid, gid)
示例7: _execChild
def _execChild(self, path, settingUID, uid, gid, executable, args, environment):
"""
The exec() which is done in the forked child.
"""
if path:
os.chdir(path)
# set the UID before I actually exec the process
if settingUID:
switchUID(uid, gid)
os.execvpe(executable, args, environment)
示例8: run
def run():
import traceback
from twisted.trial import util
try:
cfg = config.ScalemailConfig()
authfile = os.fdopen(3, 'r')
service = authfile.readline().rstrip()
authtype = authfile.readline().rstrip()
authdata = authfile.read()
authfile.close()
try:
d = main(config=cfg,
argv=sys.argv,
env=os.environ,
service=service,
authtype=authtype,
authdata=authdata)
r = util.wait(d, timeout=60.0)
userdir, username = r
switchUID(uid=pwd.getpwnam('scalemail')[2],
gid=grp.getgrnam('scalemail')[2])
if not os.path.isdir(userdir):
os.mkdir(userdir, 0700)
os.chdir(userdir)
if not os.path.isdir(username):
initializeMaildir(username)
os.chdir(username)
os.execlp(sys.argv[1], *sys.argv[1:])
die("Something is very wrong")
except (error.UnauthorizedLogin,
ChainLogin):
# TODO pass on authinfo
os.execlp(sys.argv[1], *sys.argv[1:])
die("Something is very wrong")
except RetryLogin:
# TODO pass on authinfo
l = []
argc = int(os.environ['AUTHARGC'])
for i in range(argc):
l.append(os.environ['AUTHARGV%d' % i])
os.execlp(*l)
die("Something is very wrong")
except SystemExit:
raise
except:
try:
traceback.print_exc(file=sys.stderr)
finally:
sys.exit(EX_TEMPFAIL)
示例9: test_currentEUID
def test_currentEUID(self):
"""
If the current euid is the same as the euid passed to L{util.switchUID},
then initgroups does not get called, but a warning is issued.
"""
euid = self.mockos.geteuid()
util.switchUID(euid, None, True)
self.assertEqual(self.initgroupsCalls, [])
self.assertEqual(self.mockos.seteuidCalls, [])
warnings = self.flushWarnings([util.switchUID])
self.assertEqual(len(warnings), 1)
self.assertIn("tried to drop privileges and seteuid %i" % euid, warnings[0]["message"])
self.assertIn("but euid is already %i" % euid, warnings[0]["message"])
示例10: test_currentUID
def test_currentUID(self):
"""
If the current uid is the same as the uid passed to L{util.switchUID},
then initgroups does not get called, but a warning is issued.
"""
uid = self.mockos.getuid()
util.switchUID(uid, None)
self.assertEqual(self.initgroupsCalls, [])
self.assertEqual(self.mockos.actions, [])
warnings = self.flushWarnings([util.switchUID])
self.assertEqual(len(warnings), 1)
self.assertIn('tried to drop privileges and setuid %i' % uid,
warnings[0]['message'])
self.assertIn('but uid is already %i' % uid, warnings[0]['message'])
示例11: _execChild
def _execChild(self, path, uid, gid, executable, args, environment):
"""
The exec() which is done in the forked child.
"""
if path:
os.chdir(path)
if uid is not None or gid is not None:
if uid is None:
uid = os.geteuid()
if gid is None:
gid = os.getegid()
# set the UID before I actually exec the process
os.setuid(0)
os.setgid(0)
switchUID(uid, gid)
os.execvpe(executable, args, environment)
示例12: shedPrivileges
def shedPrivileges(self, euid, uid, gid):
"""
Change the UID and GID or the EUID and EGID of this process.
@type euid: C{bool}
@param euid: A flag which, if set, indicates that only the I{effective}
UID and GID should be set.
@type uid: C{int} or C{NoneType}
@param uid: If not C{None}, the UID to which to switch.
@type gid: C{int} or C{NoneType}
@param gid: If not C{None}, the GID to which to switch.
"""
if uid is not None or gid is not None:
switchUID(uid, gid, euid)
extra = euid and 'e' or ''
log.msg('set %suid/%sgid %s/%s' % (extra, extra, uid, gid))
示例13: shared_main
def shared_main(configFileName, method, *args, **kwds):
try:
loadConfig(configFileName)
# Shed privileges
if config.UserName and config.GroupName and os.getuid() == 0:
uid = getpwnam(config.UserName).pw_uid
gid = getgrnam(config.GroupName).gr_gid
switchUID(uid, uid, gid)
os.umask(config.umask)
try:
rootResource = getRootResource(config)
directory = rootResource.getDirectory()
except DirectoryError, e:
print "Error: %s" % (e,)
return
setupMemcached(config)
setupNotifications(config)
示例14: shedPrivileges
def shedPrivileges(self, euid, uid, gid):
"""
Change the UID and GID or the EUID and EGID of this process.
@type euid: C{bool}
@param euid: A flag which, if set, indicates that only the I{effective}
UID and GID should be set.
@type uid: C{int} or C{NoneType}
@param uid: If not C{None}, the UID to which to switch.
@type gid: C{int} or C{NoneType}
@param gid: If not C{None}, the GID to which to switch.
"""
if uid is not None or gid is not None:
extra = euid and 'e' or ''
desc = '%suid/%sgid %s/%s' % (extra, extra, uid, gid)
try:
switchUID(uid, gid, euid)
except OSError:
log.msg('failed to set %s (are you root?) -- exiting.' % desc)
sys.exit(1)
else:
log.msg('set %s' % desc)
示例15: StandardIOObserver
observer = StandardIOObserver()
observer.start()
#
# Get configuration
#
try:
loadConfig(configFileName)
setLogLevelForNamespace(None, "warn")
# Shed privileges
if config.UserName and config.GroupName and os.getuid() == 0:
uid = getpwnam(config.UserName).pw_uid
gid = getgrnam(config.GroupName).gr_gid
switchUID(uid, uid, gid)
os.umask(config.umask)
config.directory = getDirectory()
autoDisableMemcached(config)
except ConfigurationError, e:
usage("Unable to start: %s" % (e,))
try:
dbxml = AugmentXMLDB((xmlFileName,)) if not remove else None
except IOError, e:
usage("Could not read XML augment file: %s" % (e,))
#
# Start the reactor