Uprating of the passphrase pixie.
[u/mdw/catacomb] / pixie-common.c
index 6efd763..2395f52 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: pixie-common.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
  *
  * Common code for Pixie client and server (Unix-specific)
  *
@@ -46,6 +46,7 @@
 
 #include <mLib/alloc.h>
 #include <mLib/dstr.h>
+#include <mLib/str.h>
 
 #include "pixie.h"
 
@@ -218,6 +219,158 @@ fail_0:
   return (-1);
 }
 
+/* --- @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;
+  xfree(sun);
+  return (fd);
+
+  /* --- Tidy up if things went wrong --- */
+
+fail_1:
+  xfree(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, @-1@ if the read fails, @+1@ to
+ *             request the passphrase from the user.
+ *
+ * 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, "MISSING") == 0)
+    return (+1);
+  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_set@ --- *
+ *
+ * Arguments:  @int fd@ = pixie file descriptor
+ *             @const char *tag@ = pointer to tag string
+ *             @const char *phrase@ = pointer to passphrase string
+ *
+ * Returns:    ---
+ *
+ * Use:                Sends a passphrase to the passphrase pixie.
+ */
+
+void pixie_set(int fd, const char *tag, const char *phrase)
+{
+  dstr d = DSTR_INIT;
+  char buf[16];
+  size_t sz = strlen(phrase);
+  char nl = '\n';
+  char *p, *q;
+
+  /* --- Send the request --- *
+   *
+   * I didn't want to copy it out of the caller's buffer.  @writev@ may
+   * produce a copy, too, so I didn't do that either.
+   */
+
+  dstr_putf(&d, "SET %s -- ", tag);
+  write(fd, d.buf, d.len);
+  write(fd, phrase, sz);
+  write(fd, &nl, 1);
+  dstr_destroy(&d);
+
+  /* --- Pick up the pieces --- */
+
+again:
+  pixie_fdline(fd, buf, sizeof(buf));
+  p = buf;
+  if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0)
+    goto again;
+}
+
+/* --- @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 -------------------------------------------------*/