| 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>cxx . . $ `<file>/config.h`/ |
| 21 | |
| 22 | #include <gpg-error.h> |
| 23 | #include <gcrypt.h> |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #define PAM_SM_AUTHcxx . . $ `src/pam/auth-method-localdb/auth-localdb.c:27:9`! |
| 28 | #include <security/pam_modules.h> |
| 29 | |
| 30 | #include "auth-methods.h"cxx . . $ `<file>/src/pam/auth-methods.h`/ |
| 31 | #include "conv.h"cxx . . $ `<file>/src/pam/auth-support/conv.h`/ |
| 32 | #include "util/util.h"cxx . . $ `<file>/src/util/util.h`/ |
| 33 | |
| 34 | #include "scd/scd.h"cxx . . $ `<file>/src/scd/scd.h`/ |
| 35 | #include "util/support.h"cxx . . $ `<file>/src/util/support.h`/ |
| 36 | #include "auth-support/ctx.h"cxx . . $ `<file>/src/pam/auth-support/ctx.h`/ |
| 37 | #include "auth-support/wait-for-card.h"cxx . . $ `<file>/src/pam/auth-support/wait-for-card.h`/ |
| 38 | |
| 39 | #include "usersdb.h"cxx . . $ `<file>/src/pam/auth-method-localdb/usersdb.h`/ |
| 40 | #include "key-lookup.h"cxx . . $ `<file>/src/pam/auth-method-localdb/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_docxx . . $ auth_method_localdb_auth_do(a0fcef1a420ed44b). (poldi_ctx_tcxx . . $ 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_tcxx . . $ gcry_sexp_t# key; |
| 82 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 83 | char *card_username; |
| 84 | const char *username; |
| 85 | |
| 86 | card_username = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 87 | |
| 88 | challenge = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 89 | response = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 90 | username = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 91 | key = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 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_serialnocxx . . $ usersdb_lookup_by_serialno(a9c69b9d9ab9e4b8). (ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..serialnocxx . . $ scd_cardinfo#serialno., &card_username); |
| 104 | if (gcry_err_codecxx . . $ gcry_err_code(3a8eb0b02ee1fc1a). (err) == GPG_ERR_AMBIGUOUS_NAMEcxx . . $ GPG_ERR_AMBIGUOUS_NAME.) |
| 105 | /* Given serialno is associated with more than one account => |
| 106 | ask the user for desired identity. */ |
| 107 | err = conv_askcxx . . $ conv_ask(b7afd51fbbcedc98). (ctx->convcxx . . $ poldi_ctx_s#conv., 0, &card_username, |
| 108 | _cxx . . $ `src/poldi.h:45:9`!("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->debugcxx . . $ poldi_ctx_s#debug.) |
| 119 | log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 120 | "Trying authentication as user `%s'...", username); |
| 121 | if (!ctx->quietcxx . . $ poldi_ctx_s#quiet.) |
| 122 | conv_tellcxx . . $ conv_tell(93874d2501936ebd). (ctx->convcxx . . $ poldi_ctx_s#conv., |
| 123 | _cxx . . $ `src/poldi.h:45:9`!("Trying authentication as user `%s'..."), username); |
| 124 | |
| 125 | /* Verify (again) that the given account is associated with the |
| 126 | serial number. */ |
| 127 | err = usersdb_checkcxx . . $ usersdb_check(d4414ff42889198). (ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..serialnocxx . . $ scd_cardinfo#serialno., username); |
| 128 | if (err) |
| 129 | { |
| 130 | if (ctx->debugcxx . . $ poldi_ctx_s#debug.) |
| 131 | log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 132 | "Serial number %s is not associated with user %s", |
| 133 | ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..serialnocxx . . $ scd_cardinfo#serialno., username); |
| 134 | conv_tellcxx . . $ conv_tell(93874d2501936ebd). (ctx->convcxx . . $ poldi_ctx_s#conv., |
| 135 | _cxx . . $ `src/poldi.h:45:9`!("Serial number %s is not associated with user %s"), |
| 136 | ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..serialnocxx . . $ scd_cardinfo#serialno., username); |
| 137 | err = gcry_errorcxx . . $ gcry_error(6231b2685059dc28). (GPG_ERR_INV_NAMEcxx . . $ GPG_ERR_INV_NAME.); |
| 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | /* Retrieve key belonging to card. */ |
| 142 | err = key_lookup_by_serialnocxx . . $ key_lookup_by_serialno(94ccc6ed6e7e12d). (ctx, ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..serialnocxx . . $ scd_cardinfo#serialno., &key); |
| 143 | if (err) |
| 144 | goto out; |
| 145 | |
| 146 | /* Generate challenge. */ |
| 147 | err = challenge_generatecxx . . $ challenge_generate(fee6e208587138e9). (&challenge, &challenge_n); |
| 148 | if (err) |
| 149 | { |
| 150 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 151 | "failed to generate challenge: %s", |
| 152 | gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | /* Let card sign the challenge. */ |
| 157 | err = scd_pksigncxx . . $ scd_pksign(def977db0e0fd549). (ctx->scdcxx . . $ poldi_ctx_s#scd., "OPENPGP.3", |
| 158 | challenge, challenge_n, |
| 159 | &response, &response_n); |
| 160 | if (err) |
| 161 | { |
| 162 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., |
| 163 | "failed to retrieve challenge signature from card: %s", |
| 164 | gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 165 | goto out; |
| 166 | } |
| 167 | |
| 168 | /* Verify response. */ |
| 169 | err = challenge_verifycxx . . $ challenge_verify(ffed3e639787afb3). (key, challenge, challenge_n, response, response_n); |
| 170 | if (err) |
| 171 | { |
| 172 | log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#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_releasecxx . . $ gcry_sexp_release(c5bb25d0f29ec38). (key); |
| 185 | |
| 186 | challenge_releasecxx . . $ challenge_release(f25546b61a629de7). (challenge); |
| 187 | xfreecxx . . $ `src/poldi.h:39:9`! (response); |
| 188 | |
| 189 | if (err) |
| 190 | xfreecxx . . $ `src/poldi.h:39:9`! (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_authcxx . . $ auth_method_localdb_auth(611d44073efe9c24). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, void *cookie, char **username) |
| 203 | { |
| 204 | return auth_method_localdb_auth_docxx . . $ auth_method_localdb_auth_do(a0fcef1a420ed44b). (ctx, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 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_ascxx . . $ auth_method_localdb_auth_as(29cbdd2ecba470e0). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx, void *cookie, const char *username) |
| 212 | { |
| 213 | return auth_method_localdb_auth_docxx . . $ auth_method_localdb_auth_do(a0fcef1a420ed44b). (ctx, username, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | struct auth_method_scxx . . $ auth_method_s# auth_method_localdbcxx . . $ auth_method_localdb. = |
| 220 | { |
| 221 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 222 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 223 | auth_method_localdb_authcxx . . $ auth_method_localdb_auth(611d44073efe9c24)., |
| 224 | auth_method_localdb_auth_ascxx . . $ auth_method_localdb_auth_as(29cbdd2ecba470e0)., |
| 225 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 226 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 227 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! |
| 228 | }; |