Add override_ttyname option to PinEntry.
authorW. Trevor King <wking@drexel.edu>
Sat, 24 Mar 2012 00:59:35 +0000 (20:59 -0400)
committerW. Trevor King <wking@drexel.edu>
Sat, 24 Mar 2012 00:59:35 +0000 (20:59 -0400)
bin/pinentry.py

index a9773f27cc96a578ce1c88fcaaa715b775f63e5f..3bc6771f3fc32278dc9edd3a9c494c1c938a3b81 100755 (executable)
@@ -78,6 +78,15 @@ class PinEntry (_server.AssuanServer):
       S: OK
       C: BYE
       S: OK closing connection
+
+    Some drivers (e.g. ``gpgme``) have ``gpg-agent`` set ``ttyname``
+    to the terminal running ``gpgme``.  I don't like this, because the
+    pinentry program doesn't always play nicely with whatever is going
+    on in that terminal.  I'd rather have a free terminal that had
+    just been running Bash, and I export ``GPG_TTY`` to point to the
+    desired terminal.  To ignore the requested ``ttyname`` and use
+    whatever is in ``GPG_TTY``, initialize with ``override_ttyname``
+    set to ``True``.
     """
     _digit_regexp = _re.compile(r'\d+')
 
@@ -85,13 +94,14 @@ class PinEntry (_server.AssuanServer):
     _tpgrp_regexp = _re.compile(r'\d+ \(\S+\) . \d+ \d+ \d+ \d+ (\d+)')
 
     def __init__(self, name='pinentry', strict_options=False,
-                 single_request=True, **kwargs):
+                 single_request=True, override_ttyname=False, **kwargs):
         self.strings = {}
         self.connection = {}
         super(PinEntry, self).__init__(
             name=name, strict_options=strict_options,
             single_request=single_request, **kwargs)
         self.valid_options.append('ttyname')
+        self.override_ttyname = override_ttyname
 
     def reset(self):
         super(PinEntry, self).reset()
@@ -103,7 +113,11 @@ class PinEntry (_server.AssuanServer):
     def _connect(self):
         self.logger.info('connecting to user')
         self.logger.debug('options:\n{}'.format(_pprint.pformat(self.options)))
-        tty_name = self.options.get('ttyname', None)
+        tty_name = None
+        if self.override_ttyname:
+            tty_name = _os.getenv('TTY_NAME')
+        if not tty_name:  # override not requested, or fall back on undefined
+            tty_name = self.options.get('ttyname', None)
         if tty_name:
             self.connection['tpgrp'] = self._get_pgrp(tty_name)
             self.logger.info(
@@ -339,7 +353,7 @@ if __name__ == '__main__':
                 logging.DEBUG, p.logger.level - 10*args.verbose))
 
     try:
-        p = PinEntry()
+        p = PinEntry(override_ttyname=True)
         p.run()
     except:
         p.logger.error(