Portable interface to reading passphrases.
authormdw <mdw>
Wed, 22 Dec 1999 15:58:20 +0000 (15:58 +0000)
committermdw <mdw>
Wed, 22 Dec 1999 15:58:20 +0000 (15:58 +0000)
passphrase.c [new file with mode: 0644]
passphrase.h [new file with mode: 0644]

diff --git a/passphrase.c b/passphrase.c
new file mode 100644 (file)
index 0000000..ced4385
--- /dev/null
@@ -0,0 +1,158 @@
+/* -*-c-*-
+ *
+ * $Id: passphrase.c,v 1.1 1999/12/22 15:58:20 mdw Exp $
+ *
+ * Reading of passphrases (Unix-specific)
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: passphrase.c,v $
+ * Revision 1.1  1999/12/22 15:58:20  mdw
+ * Portable interface to reading passphrases.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <unistd.h>
+
+#include <mLib/dstr.h>
+
+#include "passphrase.h"
+#include "pixie.h"
+
+/*----- Static variables --------------------------------------------------*/
+
+static int fd = -1;
+static unsigned flags = 0;
+
+enum {
+  f_fail = 1
+};
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @pconn@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    Zero if OK, nonzero if it failed
+ *
+ * Use:                Attempts to connect to the passphrase pixie.
+ */
+
+static int pconn(void)
+{
+  if (fd != -1)
+    return (0);
+  if (flags & f_fail)
+    return (-1);
+  if ((fd = pixie_open(0)) < 0) {
+    flags |= f_fail;
+    return (-1);
+  }
+  return (0);
+}
+
+/* --- @passphrase_read@ --- *
+ *
+ * Arguments:  @const char *tag@ = pointer to passphrase tag string
+ *             @unsigned mode@ = reading mode
+ *             @char *buf@ = pointer to destination buffer
+ *             @size_t sz@ = size of destination buffer
+ *
+ * Returns:    Zero if successful, nonzero on failure.
+ *
+ * Use:                Reads a passphrase from the user, using some system-specific
+ *             secure mechanism.  The mechanism may keep a cache of
+ *             passphrases, so the user may not necessarily be prompted.
+ */
+
+int passphrase_read(const char *tag, unsigned mode, char *buf, size_t sz)
+{
+  dstr d = DSTR_INIT;
+
+  /* --- Try talking to the pixie --- */
+
+  if (!pconn()) {
+    if (pixie_read(fd, tag, mode, buf, sz)) {
+      close(fd);
+      fd = -1;
+      return (-1);
+    }
+    return (0);
+  }
+
+  /* --- Read from the terminal --- */
+
+  dstr_putf(&d, "Passphrase %s: ", tag);
+  if (pixie_getpass(d.buf, buf, sz))
+    goto fail;
+  if (mode == PMODE_VERIFY) {
+    char b[1024];
+    DRESET(&d);
+    dstr_putf(&d, "Verify passphrase %s: ", tag);
+    if (pixie_getpass(d.buf, b, sizeof(b)) ||
+       strcmp(b, buf) != 0) {
+      memset(b, 0, sizeof(b));
+      goto fail;
+    }
+  }
+  dstr_destroy(&d);
+  return (0);
+
+  /* --- Tidy up after a failure --- */
+
+fail:
+  dstr_destroy(&d);
+  memset(buf, 0, sz);
+  return (-1);      
+}
+
+/* --- @passphrase_cancel@ --- *
+ *
+ * Arguments:  @const char *tag@ = pointer to passphrase tag string
+ *
+ * Returns:    ---
+ *
+ * Use:                Attempts to make the passphrase cache forget about a
+ *             particular passphrase.  This may be useful if the passphrase
+ *             turns out to be wrong, or if the user is attempting to change
+ *             the passphrase.
+ */
+
+void passphrase_cancel(const char *tag)
+{
+  if (!pconn())
+    pixie_cancel(fd, tag);
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/passphrase.h b/passphrase.h
new file mode 100644 (file)
index 0000000..146251f
--- /dev/null
@@ -0,0 +1,101 @@
+/* -*-c-*-
+ *
+ * $Id: passphrase.h,v 1.1 1999/12/22 15:58:20 mdw Exp $
+ *
+ * Reading passphrases
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: passphrase.h,v $
+ * Revision 1.1  1999/12/22 15:58:20  mdw
+ * Portable interface to reading passphrases.
+ *
+ */
+
+#ifndef CATACOMB_PASSPHRASE_H
+#define CATACOMB_PASSPHRASE_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+/*----- Data structures ---------------------------------------------------*/
+
+/* --- Passphrase modes --- *
+ *
+ * @PMODE_VERIFY@ requests that the passphrase be repeated to make sure it's
+ * right.
+ */
+
+enum {
+  PMODE_READ,
+  PMODE_VERIFY
+};
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @passphrase_read@ --- *
+ *
+ * Arguments:  @const char *tag@ = pointer to passphrase tag string
+ *             @unsigned mode@ = reading mode
+ *             @char *buf@ = pointer to destination buffer
+ *             @size_t sz@ = size of destination buffer
+ *
+ * Returns:    Zero if successful, nonzero on failure.
+ *
+ * Use:                Reads a passphrase from the user, using some system-specific
+ *             secure mechanism.  The mechanism may keep a cache of
+ *             passphrases, so the user may not necessarily be prompted.
+ */
+
+extern int passphrase_read(const char */*tag*/, unsigned /*mode*/,
+                          char */*buf*/, size_t /*sz*/);
+
+/* --- @passphrase_cancel@ --- *
+ *
+ * Arguments:  @const char *tag@ = pointer to passphrase tag string
+ *
+ * Returns:    ---
+ *
+ * Use:                Attempts to make the passphrase cache forget about a
+ *             particular passphrase.  This may be useful if the passphrase
+ *             turns out to be wrong, or if the user is attempting to change
+ *             the passphrase.
+ */
+
+extern void passphrase_cancel(const char */*tag*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif