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