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