From a1e4a8a7566d5d4b867465c9b8fe5be72e1d66a8 Mon Sep 17 00:00:00 2001 From: Isis Lovecruft Date: Thu, 27 Nov 2014 00:03:27 +0000 Subject: [PATCH] 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 --- gnupg/_meta.py | 35 ++++++++++++++++++++++++++++------- gnupg/_util.py | 3 +++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/gnupg/_meta.py b/gnupg/_meta.py index 8d21d60..dfa1982 100644 --- a/gnupg/_meta.py +++ b/gnupg/_meta.py @@ -75,19 +75,40 @@ class GPGMeta(type): instance containing the gpg-agent process' information to ``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 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(): if (proc.name == "gpg-agent") and proc.is_running: log.debug("Found gpg-agent process with pid %d" % proc.pid) - if proc.uids == identity: - log.debug( - "Effective UIDs of this process and gpg-agent match") - setattr(cls, '_agent_proc', proc) - return True + if _util._running_windows: + if proc.username() == identity: + ownership_match = True + else: + 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): diff --git a/gnupg/_util.py b/gnupg/_util.py index 50e4cbc..fff8c0c 100644 --- a/gnupg/_util.py +++ b/gnupg/_util.py @@ -56,6 +56,9 @@ try: except NameError: _py3k = True +_running_windows = False +if "win" in sys.platform: + _running_windows = True ## Directory shortcuts: ## we don't want to use this one because it writes to the install dir: