Passphrase pixie support.
[u/mdw/catacomb] / pixie-client.c
diff --git a/pixie-client.c b/pixie-client.c
new file mode 100644 (file)
index 0000000..5b7c0dc
--- /dev/null
@@ -0,0 +1,172 @@
+/* -*-c-*-
+ *
+ * $Id: pixie-client.c,v 1.1 1999/12/22 15:58:41 mdw Exp $
+ *
+ * Simple passphrase pixie client (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: pixie-client.c,v $
+ * Revision 1.1  1999/12/22 15:58:41  mdw
+ * Passphrase pixie support.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <pwd.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <mLib/dstr.h>
+#include <mLib/fdflags.h>
+#include <mLib/str.h>
+
+#include "passphrase.h"
+#include "pixie.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @pixie_open@ --- *
+ *
+ * Arguments:  @const char *sock@ = path to pixie socket
+ *
+ * Returns:    Less than zero if it failed, or file descriptor.
+ *
+ * Use:                Opens a connection to a passphrase pixie.
+ */
+
+int pixie_open(const char *sock)
+{
+  struct sockaddr_un *sun;
+  size_t sz;
+  int fd;
+
+  /* --- Open the connection --- */
+
+  if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
+    goto fail_0;
+  sun = pixie_address(sock, &sz);
+  if (connect(fd, (struct sockaddr *)sun, sz))
+    goto fail_1;
+  free(sun);
+  return (fd);
+
+  /* --- Tidy up if things went wrong --- */
+
+fail_1:
+  free(sun);
+  close(fd);
+fail_0:
+  return (-1);
+}
+
+/* --- @pixie_read@ --- *
+ *
+ * Arguments:  @int fd@ = connection to passphrase pixie
+ *             @const char *tag@ = pointer to tag string
+ *             @unsigned mode@ = reading mode
+ *             @char *buf@ = pointer to destination buffer
+ *             @size_t sz@ = size of the buffer
+ *
+ * Returns:    Zero if all went well, nonzero if the read fails.
+ *
+ * Use:                Reads a passphrase from the pixie.
+ */
+
+int pixie_read(int fd, const char *tag, unsigned mode, char *buf, size_t sz)
+{
+  dstr d = DSTR_INIT;
+  char *p, *q;
+
+  /* --- Send the request --- */
+
+  dstr_putf(&d, "%s %s\n", mode == PMODE_READ ? "PASS" : "VERIFY", tag);
+  write(fd, d.buf, d.len);
+  dstr_destroy(&d);
+
+  /* --- Sort out the result --- */
+
+again:
+  pixie_fdline(fd, buf, sz);
+  p = buf;
+  if ((q = str_getword(&p)) == 0)
+    return (-1);
+  if (strcmp(q, "INFO") == 0)
+    goto again;
+  else if (strcmp(q, "OK") != 0)
+    return (-1);
+
+  /* --- Return the final answer --- */
+
+  if (p)
+    memmove(buf, p, strlen(p) + 1);
+  else
+    *buf = 0;
+  return (0);
+}
+
+/* --- @pixie_cancel@ --- *
+ *
+ * Arguments:  @int fd@ = pixie file descriptor
+ *             @const char *tag@ = pointer to tag string
+ *
+ * Returns:    ---
+ *
+ * Use:                Cancels a passphrase if it turns out to be bogus.
+ */
+
+void pixie_cancel(int fd, const char *tag)
+{
+  dstr d = DSTR_INIT;
+  char buf[16];
+  char *p, *q;
+
+  /* --- Send the request --- */
+
+  dstr_putf(&d, "FLUSH %s\n", tag);
+  write(fd, d.buf, d.len);
+  dstr_destroy(&d);
+
+  /* --- Sort out the result --- */
+
+again:
+  pixie_fdline(fd, buf, sizeof(buf));
+  p = buf;
+  if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0)
+    goto again;
+}
+
+/*----- That's all, folks -------------------------------------------------*/