General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / passphrase.c
CommitLineData
9c5b124e 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: passphrase.c,v 1.6 2004/04/08 01:36:15 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
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
53/* --- @pconn@ --- *
54 *
55 * Arguments: ---
56 *
57 * Returns: Zero if OK, nonzero if it failed
58 *
59 * Use: Attempts to connect to the passphrase pixie.
60 */
61
62static int pconn(void)
63{
64 if (fd != -1)
65 return (0);
66 if (flags & f_fail)
67 return (-1);
68 if ((fd = pixie_open(0)) < 0) {
69 flags |= f_fail;
70 return (-1);
71 }
72 return (0);
73}
74
75/* --- @passphrase_read@ --- *
76 *
77 * Arguments: @const char *tag@ = pointer to passphrase tag string
78 * @unsigned mode@ = reading mode
79 * @char *buf@ = pointer to destination buffer
80 * @size_t sz@ = size of destination buffer
81 *
82 * Returns: Zero if successful, nonzero on failure.
83 *
84 * Use: Reads a passphrase from the user, using some system-specific
85 * secure mechanism. The mechanism may keep a cache of
86 * passphrases, so the user may not necessarily be prompted.
87 */
88
89int passphrase_read(const char *tag, unsigned mode, char *buf, size_t sz)
90{
91 dstr d = DSTR_INIT;
95959d10 92 int rc = 1;
9c5b124e 93
94 /* --- Try talking to the pixie --- */
95
96 if (!pconn()) {
95959d10 97 rc = pixie_read(fd, tag, mode, buf, sz);
98 if (rc < 0) {
9c5b124e 99 close(fd);
100 fd = -1;
101 return (-1);
102 }
95959d10 103 if (rc == 0)
104 return (0);
9c5b124e 105 }
106
107 /* --- Read from the terminal --- */
108
95959d10 109 dstr_putf(&d, "%s %s: ",
110 mode == PMODE_READ ? "Passphrase" : "New passphrase",
111 tag);
9c5b124e 112 if (pixie_getpass(d.buf, buf, sz))
113 goto fail;
114 if (mode == PMODE_VERIFY) {
115 char b[1024];
116 DRESET(&d);
117 dstr_putf(&d, "Verify passphrase %s: ", tag);
14d553d6 118 if (pixie_getpass(d.buf, b, sizeof(b)) || strcmp(b, buf) != 0) {
9c5b124e 119 memset(b, 0, sizeof(b));
14d553d6 120 goto fail;
9c5b124e 121 }
122 }
123 dstr_destroy(&d);
95959d10 124
125 /* --- If the pixie is interested, tell it the new passphrase --- */
126
127 if (fd >= 0)
128 pixie_set(fd, tag, buf);
9c5b124e 129 return (0);
130
131 /* --- Tidy up after a failure --- */
132
133fail:
134 dstr_destroy(&d);
135 memset(buf, 0, sz);
136 return (-1);
137}
138
139/* --- @passphrase_cancel@ --- *
140 *
141 * Arguments: @const char *tag@ = pointer to passphrase tag string
142 *
143 * Returns: ---
144 *
145 * Use: Attempts to make the passphrase cache forget about a
146 * particular passphrase. This may be useful if the passphrase
147 * turns out to be wrong, or if the user is attempting to change
148 * the passphrase.
149 */
150
151void passphrase_cancel(const char *tag)
152{
153 if (!pconn())
154 pixie_cancel(fd, tag);
155}
156
157/*----- That's all, folks -------------------------------------------------*/