[PATCH 1/4] pinentry-tty: handle designated tty outside of read_password

Daniel Kahn Gillmor dkg at fifthhorseman.net
Tue Apr 28 19:01:13 CEST 2015


* tty/pinentry-tty.c: reorganize, wrapping read_password in tty
  open/close.

--

This patch sets the stage to simplify the subsequent fixes.
---
 tty/pinentry-tty.c | 66 ++++++++++++++++++++++++++++--------------------------
 1 file changed, 34 insertions(+), 32 deletions(-)

diff --git a/tty/pinentry-tty.c b/tty/pinentry-tty.c
index 8f680fd..1aeffbd 100644
--- a/tty/pinentry-tty.c
+++ b/tty/pinentry-tty.c
@@ -59,36 +59,13 @@ cbreak (int fd)
 }
 
 static int
-read_password (pinentry_t pinentry, const char *tty_name, const char *tty_type)
+read_password (pinentry_t pinentry, FILE *ttyfi, FILE *ttyfo)
 {
-  FILE *ttyfi = stdin;
-  FILE *ttyfo = stdout;
   int count;
 
-  if (tty_name)
-    {
-      ttyfi = fopen (tty_name, "r");
-      if (!ttyfi)
-        return -1;
-
-      ttyfo = fopen (tty_name, "w");
-      if (!ttyfo)
-        {
-          int err = errno;
-          fclose (ttyfi);
-          errno = err;
-          return -1;
-        }
-    }
-
   if (cbreak (fileno (ttyfi)) == -1)
     {
       int err = errno;
-      if (tty_name)
-        {
-          fclose (ttyfi);
-          fclose (ttyfo);
-        }
       fprintf (stderr, "cbreak failure, exiting\n");
       errno = err;
       return -1;
@@ -113,11 +90,6 @@ read_password (pinentry_t pinentry, const char *tty_name, const char *tty_type)
     }
 
   tcsetattr (fileno(ttyfi), TCSANOW, &o_term);
-  if (tty_name)
-    {
-      fclose (ttyfi);
-      fclose (ttyfo);
-    }
   return strlen (pinentry->pin);
 }
 
@@ -158,7 +130,9 @@ catchsig(int sig)
 int
 tty_cmd_handler(pinentry_t pinentry)
 {
-  int rc;
+  int rc = 0;
+  FILE *ttyfi = stdin;
+  FILE *ttyfo = stdout;
 
 #ifndef HAVE_DOSISH_SYSTEM
   timed_out = 0;
@@ -174,8 +148,36 @@ tty_cmd_handler(pinentry_t pinentry)
     }
 #endif
 
-  rc = read_password (pinentry, pinentry->ttyname, pinentry->ttytype);
-  do_touch_file (pinentry);
+  if (pinentry->ttyname)
+    {
+      ttyfi = fopen (pinentry->ttyname, "r");
+      if (!ttyfi)
+        rc = -1;
+      else
+        {
+          ttyfo = fopen (pinentry->ttyname, "w");
+          if (!ttyfo)
+            {
+              int err = errno;
+              fclose (ttyfi);
+              errno = err;
+              rc = -1;
+            }
+        }
+    }
+
+  if (rc == 0)
+    {
+      rc = read_password (pinentry, ttyfi, ttyfo);
+      do_touch_file (pinentry);
+    }
+  
+  if (pinentry->ttyname)
+    {
+      fclose (ttyfi);
+      fclose (ttyfo);
+    }
+
   return rc;
 }
 
-- 
2.1.4




More information about the Gnupg-devel mailing list