[cedar-backup-svn] SF.net SVN: cedar-backup:[995] cedar-backup2/trunk
Brought to you by:
pronovic
|
From: <pro...@us...> - 2010-07-07 18:50:40
|
Revision: 995
http://cedar-backup.svn.sourceforge.net/cedar-backup/?rev=995&view=rev
Author: pronovic
Date: 2010-07-07 18:50:34 +0000 (Wed, 07 Jul 2010)
Log Message:
-----------
Refactor out util.isRunningAsRoot() to replace scattered os.getuid() calls
Modified Paths:
--------------
cedar-backup2/trunk/CedarBackup2/actions/stage.py
cedar-backup2/trunk/CedarBackup2/peer.py
cedar-backup2/trunk/CedarBackup2/util.py
cedar-backup2/trunk/Changelog
cedar-backup2/trunk/TODO
Modified: cedar-backup2/trunk/CedarBackup2/actions/stage.py
===================================================================
--- cedar-backup2/trunk/CedarBackup2/actions/stage.py 2010-07-01 17:54:16 UTC (rev 994)
+++ cedar-backup2/trunk/CedarBackup2/actions/stage.py 2010-07-07 18:50:34 UTC (rev 995)
@@ -54,7 +54,7 @@
# Cedar Backup modules
from CedarBackup2.peer import RemotePeer, LocalPeer
-from CedarBackup2.util import getUidGid, changeOwnership, isStartOfWeek
+from CedarBackup2.util import getUidGid, changeOwnership, isStartOfWeek, isRunningAsRoot
from CedarBackup2.actions.constants import DIR_TIME_FORMAT, STAGE_INDICATOR
from CedarBackup2.actions.util import writeIndicatorFile
@@ -120,7 +120,7 @@
continue
logger.debug("Found collect indicator.")
targetDir = stagingDirs[peer.name]
- if os.getuid() == 0:
+ if isRunningAsRoot():
# Since we're running as root, we can change ownership
ownership = getUidGid(config.options.backupUser, config.options.backupGroup)
logger.debug("Using target dir [%s], ownership [%d:%d]." % (targetDir, ownership[0], ownership[1]))
@@ -319,7 +319,7 @@
@param config: Config object.
@return: Name of local user that should be used
"""
- if os.getuid() != 0:
+ if not isRunningAsRoot():
return None
return config.options.backupUser
Modified: cedar-backup2/trunk/CedarBackup2/peer.py
===================================================================
--- cedar-backup2/trunk/CedarBackup2/peer.py 2010-07-01 17:54:16 UTC (rev 994)
+++ cedar-backup2/trunk/CedarBackup2/peer.py 2010-07-07 18:50:34 UTC (rev 995)
@@ -59,7 +59,7 @@
# Cedar Backup modules
from CedarBackup2.filesystem import FilesystemList
-from CedarBackup2.util import resolveCommand, executeCommand
+from CedarBackup2.util import resolveCommand, executeCommand, isRunningAsRoot
from CedarBackup2.util import splitCommandLine, encodePath
from CedarBackup2.config import VALID_FAILURE_MODES
@@ -1010,7 +1010,7 @@
beforeSet = RemotePeer._getDirContents(targetDir)
if localUser is not None:
try:
- if os.getuid() != 0:
+ if not isRunningAsRoot():
raise IOError("Only root can remote copy as another user.")
except AttributeError: pass
actualCommand = "%s %s@%s:%s/* %s" % (rcpCommand, remoteUser, remoteHost, sourceDir, targetDir)
@@ -1103,7 +1103,7 @@
raise IOError("Target file [%s] already exists." % targetFile)
if localUser is not None:
try:
- if os.getuid() != 0:
+ if not isRunningAsRoot():
raise IOError("Only root can remote copy as another user.")
except AttributeError: pass
actualCommand = "%s %s@%s:%s %s" % (rcpCommand, remoteUser, remoteHost, sourceFile.replace(" ", "\\ "), targetFile)
@@ -1174,7 +1174,7 @@
raise IOError("Target file [%s] already exists." % targetFile)
if localUser is not None:
try:
- if os.getuid() != 0:
+ if not isRunningAsRoot():
raise IOError("Only root can remote copy as another user.")
except AttributeError: pass
actualCommand = '%s "%s" "%s@%s:%s"' % (rcpCommand, sourceFile, remoteUser, remoteHost, targetFile)
@@ -1217,7 +1217,7 @@
actualCommand = "%s %s@%s '%s'" % (rshCommand, remoteUser, remoteHost, remoteCommand)
if localUser is not None:
try:
- if os.getuid() != 0:
+ if not isRunningAsRoot():
raise IOError("Only root can remote shell as another user.")
except AttributeError: pass
command = resolveCommand(SU_COMMAND)
Modified: cedar-backup2/trunk/CedarBackup2/util.py
===================================================================
--- cedar-backup2/trunk/CedarBackup2/util.py 2010-07-01 17:54:16 UTC (rev 994)
+++ cedar-backup2/trunk/CedarBackup2/util.py 2010-07-07 18:50:34 UTC (rev 995)
@@ -1347,7 +1347,6 @@
try:
uid = pwd.getpwnam(user)[2]
gid = grp.getgrnam(group)[2]
- logger.debug("Translated [%s:%s] into [%d:%d]." % (user, group, uid, gid))
return (uid, gid)
except Exception, e:
logger.debug("Error looking up uid and gid for [%s:%s]: %s" % (user, group, e))
@@ -1365,7 +1364,8 @@
Changes ownership of path to match the user and group.
This is a no-op if user/group functionality is not available on the
- platform, or if the either passed-in user or group is C{None}.
+ platform, or if the either passed-in user or group is C{None}. Further, we
+ won't even try to do it unless running as root, since it's unlikely to work.
@param path: Path whose ownership to change.
@param user: User which owns file.
@@ -1374,7 +1374,7 @@
if _UID_GID_AVAILABLE:
if user is None or group is None:
logger.debug("User or group is None, so not attempting to change owner on [%s]." % path)
- elif os.getuid() != 0:
+ elif not isRunningAsRoot():
logger.debug("Not root, so not attempting to change owner on [%s]." % path)
else:
try:
@@ -1384,6 +1384,17 @@
logger.error("Error changing ownership of [%s]: %s" % (path, e))
+#############################
+# isRunningAsRoot() function
+#############################
+
+def isRunningAsRoot():
+ """
+ Indicates whether the program is running as the root user.
+ """
+ return os.getuid() == 0
+
+
##############################
# splitCommandLine() function
##############################
Modified: cedar-backup2/trunk/Changelog
===================================================================
--- cedar-backup2/trunk/Changelog 2010-07-01 17:54:16 UTC (rev 994)
+++ cedar-backup2/trunk/Changelog 2010-07-07 18:50:34 UTC (rev 995)
@@ -1,6 +1,8 @@
Version 2.19.7 01 Jul 2010
* Make cback script more robust in the case of a bad interpreter version.
+ * Remove "Translate [x:y] into [a:b]" debug message for uid/gid translation.
+ * Refactor out isRunningAsRoot() to replace scattered os.getuid() calls.
* Configure pylint and execute it against the entire codebase.
- Fix a variety of minor warnings and suggestions from pylint
- Move unit tests into testcase folder to avoid test.py naming conflict
Modified: cedar-backup2/trunk/TODO
===================================================================
--- cedar-backup2/trunk/TODO 2010-07-01 17:54:16 UTC (rev 994)
+++ cedar-backup2/trunk/TODO 2010-07-07 18:50:34 UTC (rev 995)
@@ -32,10 +32,6 @@
Can configuration be generalized, or is code the right
way to do it?
-Ugh, we're inconsistent and some code still calls os.chmod and os.chown directly?
-
-Maybe we need an isRunningAsRoot() function rather than getuid() all over the place?
-
============================
Move to Python 2.5 -- is available in etch
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|