configure.ac: Replace with a new version.
[u/mdw/catacomb] / passphrase.c
CommitLineData
9c5b124e 1/* -*-c-*-
2 *
025c5f4a 3 * $Id$
9c5b124e 4 *
5 * Reading of passphrases (Unix-specific)
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
9c5b124e 11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
45c0fd36 18 *
9c5b124e 19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
45c0fd36 23 *
9c5b124e 24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
9c5b124e 30/*----- Header files ------------------------------------------------------*/
31
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include <unistd.h>
38
39#include <mLib/dstr.h>
40
41#include "passphrase.h"
42#include "pixie.h"
43
44/*----- Static variables --------------------------------------------------*/
45
46static int fd = -1;
47static unsigned flags = 0;
48
16efd15b 49#define f_fail 1u
9c5b124e 50
51/*----- Main code ---------------------------------------------------------*/
52
025c5f4a 53/* --- @passphrase_connect@ ---
9c5b124e 54 *
025c5f4a 55 * Arguments: @const char *sock@ = socket name to connect to, or null for
56 * default
9c5b124e 57 *
58 * Returns: Zero if OK, nonzero if it failed
59 *
60 * Use: Attempts to connect to the passphrase pixie.
61 */
62
025c5f4a 63int passphrase_connect(const char *sock)
9c5b124e 64{
65 if (fd != -1)
025c5f4a 66 close(fd);
67 if ((fd = pixie_open(sock)) < 0) {
9c5b124e 68 flags |= f_fail;
69 return (-1);
70 }
025c5f4a 71 flags &= ~f_fail;
9c5b124e 72 return (0);
73}
74
025c5f4a 75static int pconn(void)
76{
77 if (fd != -1)
78 return (0);
79 if (flags & f_fail)
80 return (-1);
81 return (passphrase_connect(0));
82}
83
9c5b124e 84/* --- @passphrase_read@ --- *
85 *
86 * Arguments: @const char *tag@ = pointer to passphrase tag string
87 * @unsigned mode@ = reading mode
88 * @char *buf@ = pointer to destination buffer
89 * @size_t sz@ = size of destination buffer
90 *
91 * Returns: Zero if successful, nonzero on failure.
92 *
93 * Use: Reads a passphrase from the user, using some system-specific
94 * secure mechanism. The mechanism may keep a cache of
95 * passphrases, so the user may not necessarily be prompted.
96 */
97
98int passphrase_read(const char *tag, unsigned mode, char *buf, size_t sz)
99{
100 dstr d = DSTR_INIT;
95959d10 101 int rc = 1;
9c5b124e 102
103 /* --- Try talking to the pixie --- */
104
105 if (!pconn()) {
95959d10 106 rc = pixie_read(fd, tag, mode, buf, sz);
107 if (rc < 0) {
9c5b124e 108 close(fd);
109 fd = -1;
110 return (-1);
111 }
95959d10 112 if (rc == 0)
113 return (0);
9c5b124e 114 }
115
116 /* --- Read from the terminal --- */
117
95959d10 118 dstr_putf(&d, "%s %s: ",
119 mode == PMODE_READ ? "Passphrase" : "New passphrase",
120 tag);
9c5b124e 121 if (pixie_getpass(d.buf, buf, sz))
122 goto fail;
123 if (mode == PMODE_VERIFY) {
124 char b[1024];
125 DRESET(&d);
126 dstr_putf(&d, "Verify passphrase %s: ", tag);
14d553d6 127 if (pixie_getpass(d.buf, b, sizeof(b)) || strcmp(b, buf) != 0) {
9c5b124e 128 memset(b, 0, sizeof(b));
14d553d6 129 goto fail;
9c5b124e 130 }
131 }
132 dstr_destroy(&d);
95959d10 133
134 /* --- If the pixie is interested, tell it the new passphrase --- */
135
136 if (fd >= 0)
137 pixie_set(fd, tag, buf);
9c5b124e 138 return (0);
139
140 /* --- Tidy up after a failure --- */
141
142fail:
143 dstr_destroy(&d);
144 memset(buf, 0, sz);
45c0fd36 145 return (-1);
9c5b124e 146}
147
148/* --- @passphrase_cancel@ --- *
149 *
150 * Arguments: @const char *tag@ = pointer to passphrase tag string
151 *
152 * Returns: ---
153 *
154 * Use: Attempts to make the passphrase cache forget about a
155 * particular passphrase. This may be useful if the passphrase
156 * turns out to be wrong, or if the user is attempting to change
157 * the passphrase.
158 */
159
160void passphrase_cancel(const char *tag)
161{
162 if (!pconn())
163 pixie_cancel(fd, tag);
164}
165
166/*----- That's all, folks -------------------------------------------------*/