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