Allow explicit group parameters for DH groups.
[u/mdw/catacomb] / passphrase.c
1 /* -*-c-*-
2 *
3 * $Id: passphrase.c,v 1.5 2002/01/13 13:41:37 mdw Exp $
4 *
5 * Reading of passphrases (Unix-specific)
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: passphrase.c,v $
33 * Revision 1.5 2002/01/13 13:41:37 mdw
34 * Fix stupidity in passphrase verification.
35 *
36 * Revision 1.4 2001/04/19 18:26:01 mdw
37 * Re-request broken passphrases.
38 *
39 * Revision 1.3 2000/12/06 20:33:27 mdw
40 * Make flags be macros rather than enumerations, to ensure that they're
41 * unsigned.
42 *
43 * Revision 1.2 2000/06/17 11:49:37 mdw
44 * New pixie protocol allowing application to request passphrases and send
45 * them to the pixie.
46 *
47 * Revision 1.1 1999/12/22 15:58:20 mdw
48 * Portable interface to reading passphrases.
49 *
50 */
51
52 /*----- Header files ------------------------------------------------------*/
53
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include <unistd.h>
60
61 #include <mLib/dstr.h>
62
63 #include "passphrase.h"
64 #include "pixie.h"
65
66 /*----- Static variables --------------------------------------------------*/
67
68 static int fd = -1;
69 static unsigned flags = 0;
70
71 #define f_fail 1u
72
73 /*----- Main code ---------------------------------------------------------*/
74
75 /* --- @pconn@ --- *
76 *
77 * Arguments: ---
78 *
79 * Returns: Zero if OK, nonzero if it failed
80 *
81 * Use: Attempts to connect to the passphrase pixie.
82 */
83
84 static int pconn(void)
85 {
86 if (fd != -1)
87 return (0);
88 if (flags & f_fail)
89 return (-1);
90 if ((fd = pixie_open(0)) < 0) {
91 flags |= f_fail;
92 return (-1);
93 }
94 return (0);
95 }
96
97 /* --- @passphrase_read@ --- *
98 *
99 * Arguments: @const char *tag@ = pointer to passphrase tag string
100 * @unsigned mode@ = reading mode
101 * @char *buf@ = pointer to destination buffer
102 * @size_t sz@ = size of destination buffer
103 *
104 * Returns: Zero if successful, nonzero on failure.
105 *
106 * Use: Reads a passphrase from the user, using some system-specific
107 * secure mechanism. The mechanism may keep a cache of
108 * passphrases, so the user may not necessarily be prompted.
109 */
110
111 int passphrase_read(const char *tag, unsigned mode, char *buf, size_t sz)
112 {
113 dstr d = DSTR_INIT;
114 int rc = 1;
115
116 /* --- Try talking to the pixie --- */
117
118 if (!pconn()) {
119 rc = pixie_read(fd, tag, mode, buf, sz);
120 if (rc < 0) {
121 close(fd);
122 fd = -1;
123 return (-1);
124 }
125 if (rc == 0)
126 return (0);
127 }
128
129 /* --- Read from the terminal --- */
130
131 dstr_putf(&d, "%s %s: ",
132 mode == PMODE_READ ? "Passphrase" : "New passphrase",
133 tag);
134 if (pixie_getpass(d.buf, buf, sz))
135 goto fail;
136 if (mode == PMODE_VERIFY) {
137 char b[1024];
138 DRESET(&d);
139 dstr_putf(&d, "Verify passphrase %s: ", tag);
140 if (pixie_getpass(d.buf, b, sizeof(b)) || strcmp(b, buf) != 0) {
141 memset(b, 0, sizeof(b));
142 goto fail;
143 }
144 }
145 dstr_destroy(&d);
146
147 /* --- If the pixie is interested, tell it the new passphrase --- */
148
149 if (fd >= 0)
150 pixie_set(fd, tag, buf);
151 return (0);
152
153 /* --- Tidy up after a failure --- */
154
155 fail:
156 dstr_destroy(&d);
157 memset(buf, 0, sz);
158 return (-1);
159 }
160
161 /* --- @passphrase_cancel@ --- *
162 *
163 * Arguments: @const char *tag@ = pointer to passphrase tag string
164 *
165 * Returns: ---
166 *
167 * Use: Attempts to make the passphrase cache forget about a
168 * particular passphrase. This may be useful if the passphrase
169 * turns out to be wrong, or if the user is attempting to change
170 * the passphrase.
171 */
172
173 void passphrase_cancel(const char *tag)
174 {
175 if (!pconn())
176 pixie_cancel(fd, tag);
177 }
178
179 /*----- That's all, folks -------------------------------------------------*/