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