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