Use process uids and usernames on Unix and Windows respectively.

Windows doesn't have EUIDs, so instead we'll check that the usernames
match. This doesn't seem the least bit secure to me, but it's Windows so
they're probably owned anyway. If anyone knows one of the "proper" ways
to determine if another process has the same owner on Windows, I'd love
to know about it.

 * FIXES Issue #58 but I don't have a Windows machine to test so maybe
   it's still broken.
   https://github.com/isislovecruft/python-gnupg/issues/58
fix/58-win32-uid
Isis Lovecruft 2014-11-27 00:03:27 +00:00
parent cc959c755d
commit a1e4a8a756
No known key found for this signature in database
GPG Key ID: 18C16EC5F9F1D673
2 changed files with 31 additions and 7 deletions

View File

@ -75,19 +75,40 @@ class GPGMeta(type):
instance containing the gpg-agent process' information to instance containing the gpg-agent process' information to
``cls._agent_proc``. ``cls._agent_proc``.
For Unix systems, we check that the effective UID of this
``python-gnupg`` process is also the owner of the gpg-agent
process. For Windows, we check that the usernames of the owners are
the same. (Sorry Windows users; maybe you should switch to anything
else.)
:returns: True if there exists a gpg-agent process running under the :returns: True if there exists a gpg-agent process running under the
same effective user ID as that of this program. Otherwise, same effective user ID as that of this program. Otherwise,
returns None. returns False.
""" """
identity = psutil.Process(os.getpid()).uids this_process = psutil.Process(os.getpid())
ownership_match = False
if _util._running_windows:
identity = this_process.username()
else:
identity = this_process.uids
for proc in psutil.process_iter(): for proc in psutil.process_iter():
if (proc.name == "gpg-agent") and proc.is_running: if (proc.name == "gpg-agent") and proc.is_running:
log.debug("Found gpg-agent process with pid %d" % proc.pid) log.debug("Found gpg-agent process with pid %d" % proc.pid)
if proc.uids == identity: if _util._running_windows:
log.debug( if proc.username() == identity:
"Effective UIDs of this process and gpg-agent match") ownership_match = True
setattr(cls, '_agent_proc', proc) else:
return True if proc.uids == identity:
ownership_match = True
if ownership_match:
log.debug("Effective UIDs of this process and gpg-agent match")
setattr(cls, '_agent_proc', proc)
return True
return False
class GPGBase(object): class GPGBase(object):

View File

@ -56,6 +56,9 @@ try:
except NameError: except NameError:
_py3k = True _py3k = True
_running_windows = False
if "win" in sys.platform:
_running_windows = True
## Directory shortcuts: ## Directory shortcuts:
## we don't want to use this one because it writes to the install dir: ## we don't want to use this one because it writes to the install dir: