key/pixie-common.c, progs/pixie.c: Handle error returns better.
[u/mdw/catacomb] / key / pixie-common.c
index f24b576..2975c20 100644 (file)
@@ -181,6 +181,9 @@ void pixie_fdline(int fd, char *buf, size_t sz)
 int pixie_getpass(const char *prompt, char *buf, size_t sz)
 {
   const char *pfd = getenv("CATACOMB_PASSPHRASE_FD");
+  struct termios ta;
+  struct termios ota;
+  char nl = '\n';
   int fd = 0;
 
   /* --- See whether a terminal is what's wanted --- */
@@ -189,28 +192,25 @@ int pixie_getpass(const char *prompt, char *buf, size_t sz)
     fd = atoi(pfd);
     pixie_fdline(fd, buf, sz);
   } else {
-    struct termios ta;
-    struct termios ota;
-    char nl = '\n';
-
     if ((fd = open("/dev/tty", O_RDWR)) < 0)
       goto fail_0;
     if (tcgetattr(fd, &ta) < 0)
       goto fail_1;
     ota = ta;
     ta.c_lflag &= ~(ECHO | ISIG);
-    if (tcsetattr(fd, TCSAFLUSH, &ta))
-      goto fail_1;
-    write(fd, prompt, strlen(prompt));
+    if (tcsetattr(fd, TCSAFLUSH, &ta)) goto fail_1;
+    if (write(fd, prompt, strlen(prompt)) < 0) goto fail_2;
     pixie_fdline(fd, buf, sz);
     tcsetattr(fd, TCSAFLUSH, &ota);
-    write(fd, &nl, 1);
+    if (write(fd, &nl, 1) < 0) goto fail_1;
     close(fd);
   }
   return (0);
 
   /* --- Tidy up if things went wrong --- */
 
+fail_2:
+  tcsetattr(fd, TCSAFLUSH, &ota);
 fail_1:
   close(fd);
 fail_0:
@@ -273,7 +273,7 @@ int pixie_read(int fd, const char *tag, unsigned mode, char *buf, size_t sz)
   /* --- Send the request --- */
 
   dstr_putf(&d, "%s %s\n", mode == PMODE_READ ? "PASS" : "VERIFY", tag);
-  write(fd, d.buf, d.len);
+  if (write(fd, d.buf, d.len) < 0) return (-1);
   dstr_destroy(&d);
 
   /* --- Sort out the result --- */
@@ -325,9 +325,10 @@ void pixie_set(int fd, const char *tag, const char *phrase)
    */
 
   dstr_putf(&d, "SET %s -- ", tag);
-  write(fd, d.buf, d.len);
-  write(fd, phrase, sz);
-  write(fd, &nl, 1);
+  if (write(fd, d.buf, d.len) < 0 ||
+      write(fd, phrase, sz) < 0 ||
+      write(fd, &nl, 1) < 0)
+    return;
   dstr_destroy(&d);
 
   /* --- Pick up the pieces --- */
@@ -358,7 +359,7 @@ void pixie_cancel(int fd, const char *tag)
   /* --- Send the request --- */
 
   dstr_putf(&d, "FLUSH %s\n", tag);
-  write(fd, d.buf, d.len);
+  if (write(fd, d.buf, d.len) < 0) return;
   dstr_destroy(&d);
 
   /* --- Sort out the result --- */
@@ -371,4 +372,3 @@ again:
 }
 
 /*----- That's all, folks -------------------------------------------------*/
-