| 1 | /* auth-localdb.c - localdb authentication method for Poldi. |
| 2 | Copyright (C) 2004, 2005, 2007, 2008, 2009 g10 Code GmbH |
| 3 | |
| 4 | This file is part of Poldi. |
| 5 | |
| 6 | Poldi is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | Poldi is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | |
| 22 | #include <gpg-error.h> |
| 23 | #include <gcrypt.h> |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #define PAM_SM_AUTH |
| 28 | #include <security/pam_modules.h> |
| 29 | |
| 30 | #include "auth-methods.h" |
| 31 | #include "conv.h" |
| 32 | #include "util/util.h" |
| 33 | |
| 34 | #include "scd/scd.h" |
| 35 | #include "util/support.h" |
| 36 | #include "auth-support/ctx.h" |
| 37 | #include "auth-support/wait-for-card.h" |
| 38 | |
| 39 | #include "usersdb.h" |
| 40 | #include "key-lookup.h" |
| 41 | |
| 42 | |
| 43 | |
| 44 | #if 0 |
| 45 | /* Currently, the localdb method doesn't require a special cookie. */ |
| 46 | |
| 47 | static gpg_error_t |
| 48 | auth_method_localdb_init (void **cookie) |
| 49 | { |
| 50 | *cookie = NULL; |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | auth_method_localdb_deinit (void *cookie) |
| 56 | { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | static gpg_error_t |
| 61 | auth_method_localdb_parsecb (ARGPARSE_ARGS *parg, void *cookie) |
| 62 | { |
| 63 | /* Do we support any localdb specific options? */ |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | #endif |
| 68 | |
| 69 | |
| 70 | |
| 71 | /* Entry point for the local-db authentication method. Returns TRUE |
| 72 | (1) if authentication succeeded and FALSE (0) otherwise. */ |
| 73 | static int |
| 74 | auth_method_localdb_auth_do (poldi_ctx_t ctx, |
| 75 | const char *username_desired, char **username_authenticated) |
| 76 | { |
| 77 | unsigned char *challenge; |
| 78 | unsigned char *response; |
| 79 | size_t challenge_n; |
| 80 | size_t response_n; |
| 81 | gcry_sexp_t key; |
| 82 | gpg_error_t err; |
| 83 | char *card_username; |
| 84 | const char *username; |
| 85 | |
| 86 | card_username = NULL; |
| 87 | |
| 88 | challenge = NULL; |
| 89 | response = NULL; |
| 90 | username = NULL; |
| 91 | key = NULL; |
| 92 | |
| 93 | /* |
| 94 | * Process authentication request. |
| 95 | */ |
| 96 | |
| 97 | if (!username_desired) |
| 98 | { |
| 99 | /* We didn't receive a username from PAM, therefore we need to |
| 100 | figure it out somehow. We use the card's serialno for looking |
| 101 | up an account. */ |
| 102 | |
| 103 | err = usersdb_lookup_by_serialno (ctx->cardinfo.serialno, &card_username); |
| 104 | if (gcry_err_code (err) == GPG_ERR_AMBIGUOUS_NAME) |
| 105 | /* Given serialno is associated with more than one account => |
| 106 | ask the user for desired identity. */ |
| 107 | err = conv_ask (ctx->conv, 0, &card_username, |
| 108 | _("Please enter username: ")); |
| 109 | |
| 110 | if (err) |
| 111 | goto out; |
| 112 | |
| 113 | username = card_username; |
| 114 | } |
| 115 | else |
| 116 | username = username_desired; |
| 117 | |
| 118 | if (ctx->debug) |
| 119 | log_msg_debug (ctx->loghandle, |
| 120 | "Trying authentication as user `%s'...", username); |
| 121 | if (!ctx->quiet) |
| 122 | conv_tell (ctx->conv, |
| 123 | _("Trying authentication as user `%s'..."), username); |
| 124 | |
| 125 | /* Verify (again) that the given account is associated with the |
| 126 | serial number. */ |
| 127 | err = usersdb_check (ctx->cardinfo.serialno, username); |
| 128 | if (err) |
| 129 | { |
| 130 | if (ctx->debug) |
| 131 | log_msg_debug (ctx->loghandle, |
| 132 | "Serial number %s is not associated with user %s", |
| 133 | ctx->cardinfo.serialno, username); |
| 134 | conv_tell (ctx->conv, |
| 135 | _("Serial number %s is not associated with user %s"), |
| 136 | ctx->cardinfo.serialno, username); |
| 137 | err = gcry_error (GPG_ERR_INV_NAME); |
| 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | /* Retrieve key belonging to card. */ |
| 142 | err = key_lookup_by_serialno (ctx, ctx->cardinfo.serialno, &key); |
| 143 | if (err) |
| 144 | goto out; |
| 145 | |
| 146 | /* Generate challenge. */ |
| 147 | err = challenge_generate (&challenge, &challenge_n); |
| 148 | if (err) |
| 149 | { |
| 150 | log_msg_error (ctx->loghandle, |
| 151 | "failed to generate challenge: %s", |
| 152 | gpg_strerror (err)); |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | /* Let card sign the challenge. */ |
| 157 | err = scd_pksign (ctx->scd, "OPENPGP.3", |
| 158 | challenge, challenge_n, |
| 159 | &response, &response_n); |
| 160 | if (err) |
| 161 | { |
| 162 | log_msg_error (ctx->loghandle, |
| 163 | "failed to retrieve challenge signature from card: %s", |
| 164 | gpg_strerror (err)); |
| 165 | goto out; |
| 166 | } |
| 167 | |
| 168 | /* Verify response. */ |
| 169 | err = challenge_verify (key, challenge, challenge_n, response, response_n); |
| 170 | if (err) |
| 171 | { |
| 172 | log_msg_error (ctx->loghandle, "failed to verify challenge"); |
| 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | if (!username_desired) |
| 177 | *username_authenticated = card_username; |
| 178 | |
| 179 | /* Done. */ |
| 180 | |
| 181 | out: |
| 182 | |
| 183 | /* Release resources. */ |
| 184 | gcry_sexp_release (key); |
| 185 | |
| 186 | challenge_release (challenge); |
| 187 | xfree (response); |
| 188 | |
| 189 | if (err) |
| 190 | xfree (card_username); |
| 191 | |
| 192 | return !err; |
| 193 | } |
| 194 | |
| 195 | /* Try to authenticate a user. The user's identity on the system is |
| 196 | figured out during the authentication process. COOKIE is the |
| 197 | cookie for this authentication method. CTX is the Poldi context |
| 198 | object. On successful authentication, the newly allocated username |
| 199 | as which the user has been authenticated is stored in *USERNAME. |
| 200 | Returns TRUE on success, FALSE on failure. */ |
| 201 | static int |
| 202 | auth_method_localdb_auth (poldi_ctx_t ctx, void *cookie, char **username) |
| 203 | { |
| 204 | return auth_method_localdb_auth_do (ctx, NULL, username); |
| 205 | } |
| 206 | |
| 207 | /* Try to authenticate a user as USERNAME. COOKIE is the cookie for |
| 208 | this authentication method. CTX is the Poldi context object. |
| 209 | Returns TRUE on success, FALSE on failure. */ |
| 210 | static int |
| 211 | auth_method_localdb_auth_as (poldi_ctx_t ctx, void *cookie, const char *username) |
| 212 | { |
| 213 | return auth_method_localdb_auth_do (ctx, username, NULL); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | struct auth_method_s auth_method_localdb = |
| 220 | { |
| 221 | NULL, |
| 222 | NULL, |
| 223 | auth_method_localdb_auth, |
| 224 | auth_method_localdb_auth_as, |
| 225 | NULL, |
| 226 | NULL, |
| 227 | NULL |
| 228 | }; |