| 1 | /* usersdb.c - PAM authentication via OpenPGP smartcards. |
| 2 | Copyright (C) 2004, 2005, 2007, 2008 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 <poldi.h>cxx . . $ `<file>/src/poldi.h`/ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <assert.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include <gcrypt.h> |
| 28 | |
| 29 | #include "usersdb.h"cxx . . $ `<file>/src/pam/auth-method-localdb/usersdb.h`/ |
| 30 | #include "defs-localdb.h"cxx . . $ `<file>/src/pam/auth-method-localdb/defs-localdb.h`/ |
| 31 | |
| 32 | |
| 33 | |
| 34 | /* This is the type for callbacks functions, which need to be passed |
| 35 | to usersdb_process(). The callback function receives one |
| 36 | (SERIALNO, USERNAME) pair per invocation. OPAQUE is the opaque |
| 37 | arugment passed to usersdb_process(). The return code of such a |
| 38 | callback functions has the following meanings: |
| 39 | |
| 40 | 0: Continue processing the users database. |
| 41 | 1: Stop processing. */ |
| 42 | typedef int (*usersdb_cb_tcxx . . $ usersdb_cb_t#) (const char *serialno, const char *username, |
| 43 | void *opaque); |
| 44 | |
| 45 | /* This functions processes the users database. For each read pair of |
| 46 | a card serial number and a account, the callback function specified |
| 47 | as (CB, OPAQUE) is called. Depending on CB's return code, |
| 48 | processing is continued or aborted. */ |
| 49 | static gpg_error_tcxx . . $ gpg_error_t# |
| 50 | usersdb_processcxx . . $ usersdb_process(7973ccea2fa516b7). (usersdb_cb_tcxx . . $ usersdb_cb_t# cb, void *opaque) |
| 51 | { |
| 52 | const char *delimiters = "\t\n "; |
| 53 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 54 | FILEcxx . . $ FILE# *usersdb; |
| 55 | char *line; |
| 56 | char *line_serialno; |
| 57 | char *line_username; |
| 58 | size_t line_n; |
| 59 | ssize_tcxx . . $ ssize_t# ret; |
| 60 | int cb_ret; |
| 61 | char *save_ptr; /* For strtok_r. */ |
| 62 | |
| 63 | line_serialno = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 64 | line_username = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 65 | line = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 66 | err = 0; |
| 67 | |
| 68 | /* Open users database. */ |
| 69 | usersdb = fopencxx . . $ fopen(82ec0787cd37e0a3). (POLDI_USERS_DB_FILEcxx . . $ `src/pam/auth-method-localdb/defs-localdb.h:27:9`!, "r"); |
| 70 | if (! usersdb) |
| 71 | { |
| 72 | err = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). (); |
| 73 | goto out; |
| 74 | } |
| 75 | |
| 76 | /* Process lines. */ |
| 77 | while (1) |
| 78 | { |
| 79 | /* Get next line. */ |
| 80 | save_ptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 81 | line = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 82 | line_n = 0; |
| 83 | ret = getlinecxx . . $ getline(960b3da7d5d5ad33). (&line, &line_n, usersdb); |
| 84 | if (ret == -1) |
| 85 | { |
| 86 | if (ferrorcxx . . $ ferror(89bc1efcdac0ca22). (usersdb)) |
| 87 | err = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). (); |
| 88 | /* else EOF. */ |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | { |
| 93 | /* Cut off comments. */ |
| 94 | char *comment = strchrcxx . . $ strchr(2050848944e00d54). (line, '#'); |
| 95 | if (comment) |
| 96 | *comment = '\0'; |
| 97 | } |
| 98 | |
| 99 | /* Extract first token: the card serial number. */ |
| 100 | line_serialno = strtok_rcxx . . $ strtok_r(a42394c85d6397dd). (line, delimiters, &save_ptr); |
| 101 | if (!line_serialno) |
| 102 | /* Ignore this incomplete entry. */ |
| 103 | goto skip; |
| 104 | |
| 105 | /* Extract second token: the username. */ |
| 106 | line_username = strtok_rcxx . . $ strtok_r(a42394c85d6397dd). (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, delimiters, &save_ptr); |
| 107 | if (!line_username) |
| 108 | /* Ignore this incomplete entry. */ |
| 109 | goto skip; |
| 110 | |
| 111 | /* Looks like a valid entry, pass to callback function. */ |
| 112 | cb_ret = (*cb) (line_serialno, line_username, opaque); |
| 113 | if (cb_ret) |
| 114 | /* Callback functions wants us to stop. */ |
| 115 | break; |
| 116 | |
| 117 | skip: |
| 118 | freecxx . . $ free(5d22bdacc48458e8). (line); |
| 119 | } |
| 120 | if (err) |
| 121 | goto out; |
| 122 | |
| 123 | /* Finalize. */ |
| 124 | (*cb) (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, opaque); |
| 125 | |
| 126 | out: |
| 127 | |
| 128 | if (usersdb) |
| 129 | fclosecxx . . $ fclose(89bc1efcdac0ca22). (usersdb); |
| 130 | freecxx . . $ free(5d22bdacc48458e8). (line); /* Allocated by getline, thus standard |
| 131 | free. */ |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | * Implementation of "usersdb_check" function. usersdb_check() |
| 139 | * figures out wether a given serial number is assocated with a given |
| 140 | * username or not. |
| 141 | */ |
| 142 | |
| 143 | /* Type for opaque callback argument. */ |
| 144 | typedef struct check_cb_scxx . . $ check_cb_s# |
| 145 | { |
| 146 | /* This is the pair we are looking for. */ |
| 147 | const char *serialnocxx . . $ check_cb_s#serialno.; |
| 148 | const char *usernamecxx . . $ check_cb_s#username.; |
| 149 | |
| 150 | /* If found, this is set to TRUE by the callback function. */ |
| 151 | int matchcxx . . $ check_cb_s#match.; |
| 152 | } *check_cb_tcxx . . $ check_cb_t#; |
| 153 | |
| 154 | /* Callback function. */ |
| 155 | static int |
| 156 | usersdb_check_cbcxx . . $ usersdb_check_cb(285a709643be31b2). (const char *serialno, const char *username, void *opaque) |
| 157 | { |
| 158 | check_cb_tcxx . . $ check_cb_t# ctx = opaque; |
| 159 | |
| 160 | if (! (serialno || username)) |
| 161 | /* Finalizing */; |
| 162 | else |
| 163 | { |
| 164 | /* Regular entry. */ |
| 165 | |
| 166 | if ((! strcmpcxx . . $ strcmp(5ae29d726ae57f88). (ctx->serialnocxx . . $ check_cb_s#serialno., serialno)) |
| 167 | && (! strcmpcxx . . $ strcmp(5ae29d726ae57f88). (ctx->usernamecxx . . $ check_cb_s#username., username))) |
| 168 | { |
| 169 | /* The current entry is exactly the one we were looking |
| 170 | for. */ |
| 171 | ctx->matchcxx . . $ check_cb_s#match. = 1; |
| 172 | return 1; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* This functions figures out wether the provided (SERIALNO, USERNAME) |
| 180 | pair is contained in the users database. */ |
| 181 | gpg_error_tcxx . . $ gpg_error_t# |
| 182 | usersdb_checkcxx . . $ usersdb_check(d4414ff42889198). (const char *serialno, const char *username) |
| 183 | { |
| 184 | struct check_cb_scxx . . $ check_cb_s# ctx = { serialno, username, 0 }; |
| 185 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 186 | |
| 187 | err = usersdb_processcxx . . $ usersdb_process(7973ccea2fa516b7). (usersdb_check_cbcxx . . $ usersdb_check_cb(285a709643be31b2)., &ctx); |
| 188 | if (! err) |
| 189 | { |
| 190 | /* Now we have a result in CTX. */ |
| 191 | |
| 192 | if (! ctx.matchcxx . . $ check_cb_s#match.) |
| 193 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_NOT_FOUNDcxx . . $ GPG_ERR_NOT_FOUND.); /* FIXME: not the best |
| 194 | return code... */ |
| 195 | } |
| 196 | |
| 197 | return err; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | /* |
| 203 | * |
| 204 | */ |
| 205 | |
| 206 | /* Looking up a username for a given serial number. */ |
| 207 | |
| 208 | typedef struct lookup_cb_scxx . . $ lookup_cb_s# |
| 209 | { |
| 210 | const char *serialnocxx . . $ lookup_cb_s#serialno.; |
| 211 | const char *usernamecxx . . $ lookup_cb_s#username.; |
| 212 | int matchescxx . . $ lookup_cb_s#matches.; |
| 213 | char *foundcxx . . $ lookup_cb_s#found.; |
| 214 | gpg_error_tcxx . . $ gpg_error_t# errcxx . . $ lookup_cb_s#err.; |
| 215 | } *lookup_cb_tcxx . . $ lookup_cb_t#; |
| 216 | |
| 217 | static int |
| 218 | usersdb_lookup_cbcxx . . $ usersdb_lookup_cb(285a709643be31b2). (const char *serialno, const char *username, void *opaque) |
| 219 | { |
| 220 | lookup_cb_tcxx . . $ lookup_cb_t# ctx = opaque; |
| 221 | char *str; |
| 222 | |
| 223 | if (! (serialno || username)) |
| 224 | goto out; |
| 225 | |
| 226 | /* FIXME: this function is not nice. */ |
| 227 | |
| 228 | if (ctx->serialnocxx . . $ lookup_cb_s#serialno.) |
| 229 | { |
| 230 | if (! strcmpcxx . . $ strcmp(5ae29d726ae57f88). (ctx->serialnocxx . . $ lookup_cb_s#serialno., serialno)) |
| 231 | { |
| 232 | if (! ctx->matchescxx . . $ lookup_cb_s#matches.) |
| 233 | { |
| 234 | str = xtrystrdupcxx . . $ `src/poldi.h:37:9`! (username); |
| 235 | if (! str) |
| 236 | { |
| 237 | ctx->errcxx . . $ lookup_cb_s#err. = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). (); |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | ctx->foundcxx . . $ lookup_cb_s#found. = str; |
| 242 | ctx->matchescxx . . $ lookup_cb_s#matches.++; |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88). (ctx->foundcxx . . $ lookup_cb_s#found., username)) |
| 247 | ctx->matchescxx . . $ lookup_cb_s#matches.++; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | if (! strcmpcxx . . $ strcmp(5ae29d726ae57f88). (ctx->usernamecxx . . $ lookup_cb_s#username., username)) |
| 254 | { |
| 255 | if (! ctx->matchescxx . . $ lookup_cb_s#matches.) |
| 256 | { |
| 257 | str = xtrystrdupcxx . . $ `src/poldi.h:37:9`! (serialno); |
| 258 | if (! str) |
| 259 | { |
| 260 | ctx->errcxx . . $ lookup_cb_s#err. = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). (); |
| 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | ctx->foundcxx . . $ lookup_cb_s#found. = str; |
| 265 | ctx->matchescxx . . $ lookup_cb_s#matches.++; |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88). (ctx->foundcxx . . $ lookup_cb_s#found., serialno)) |
| 270 | ctx->matchescxx . . $ lookup_cb_s#matches.++; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | out: |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* This function tries to lookup a username by it's serial number; |
| 281 | this is only possible in case the specified serial number SERIALNO |
| 282 | is associated with exactly one username. The username will be |
| 283 | stored in newly allocated memory in *USERNAME. Returns proper |
| 284 | error code. */ |
| 285 | gpg_error_tcxx . . $ gpg_error_t# |
| 286 | usersdb_lookup_by_serialnocxx . . $ usersdb_lookup_by_serialno(a9c69b9d9ab9e4b8). (const char *serialno, char **username) |
| 287 | { |
| 288 | struct lookup_cb_scxx . . $ lookup_cb_s# ctx = { serialno, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0 }; |
| 289 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 290 | |
| 291 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (serialno); |
| 292 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (username); |
| 293 | |
| 294 | err = usersdb_processcxx . . $ usersdb_process(7973ccea2fa516b7). (usersdb_lookup_cbcxx . . $ usersdb_lookup_cb(285a709643be31b2)., &ctx); |
| 295 | if (err) |
| 296 | goto out; |
| 297 | |
| 298 | /* Now we have a result in CTX. */ |
| 299 | |
| 300 | if (ctx.errcxx . . $ lookup_cb_s#err.) |
| 301 | { |
| 302 | err = ctx.errcxx . . $ lookup_cb_s#err.; |
| 303 | goto out; |
| 304 | } |
| 305 | |
| 306 | if (! ctx.matchescxx . . $ lookup_cb_s#matches.) |
| 307 | { |
| 308 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_NOT_FOUNDcxx . . $ GPG_ERR_NOT_FOUND.); |
| 309 | goto out; |
| 310 | } |
| 311 | else if (ctx.matchescxx . . $ lookup_cb_s#matches. > 1) |
| 312 | { |
| 313 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_AMBIGUOUS_NAMEcxx . . $ GPG_ERR_AMBIGUOUS_NAME.); |
| 314 | goto out; |
| 315 | } |
| 316 | else if (ctx.matchescxx . . $ lookup_cb_s#matches. == 1) |
| 317 | { |
| 318 | *username = ctx.foundcxx . . $ lookup_cb_s#found.; |
| 319 | ctx.foundcxx . . $ lookup_cb_s#found. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 320 | } |
| 321 | |
| 322 | out: |
| 323 | |
| 324 | xfreecxx . . $ `src/poldi.h:39:9`! (ctx.found); |
| 325 | |
| 326 | return err; |
| 327 | } |
| 328 | |
| 329 | /* This function tries to lookup a serial number by it's username; |
| 330 | this is only possible in case the specified username USERNAME is |
| 331 | associated with exactly one serial number. The serial number will |
| 332 | be stored in newly allocated memory in *SERIALNO. Returns proper |
| 333 | error code. */ |
| 334 | gpg_error_tcxx . . $ gpg_error_t# |
| 335 | usersdb_lookup_by_usernamecxx . . $ usersdb_lookup_by_username(a9c69b9d9ab9e4b8). (const char *username, char **serialno) |
| 336 | { |
| 337 | struct lookup_cb_scxx . . $ lookup_cb_s# ctx = { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, username, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0 }; |
| 338 | gpg_error_tcxx . . $ gpg_error_t# err; |
| 339 | |
| 340 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (username); |
| 341 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (serialno); |
| 342 | |
| 343 | err = usersdb_processcxx . . $ usersdb_process(7973ccea2fa516b7). (usersdb_lookup_cbcxx . . $ usersdb_lookup_cb(285a709643be31b2)., &ctx); |
| 344 | if (err) |
| 345 | goto out; |
| 346 | |
| 347 | /* Now we have a result in CTX. */ |
| 348 | |
| 349 | if (ctx.errcxx . . $ lookup_cb_s#err.) |
| 350 | { |
| 351 | err = ctx.errcxx . . $ lookup_cb_s#err.; |
| 352 | goto out; |
| 353 | } |
| 354 | |
| 355 | if (! ctx.matchescxx . . $ lookup_cb_s#matches.) |
| 356 | { |
| 357 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_NOT_FOUNDcxx . . $ GPG_ERR_NOT_FOUND.); |
| 358 | goto out; |
| 359 | } |
| 360 | else if (ctx.matchescxx . . $ lookup_cb_s#matches. > 1) |
| 361 | { |
| 362 | err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_AMBIGUOUS_NAMEcxx . . $ GPG_ERR_AMBIGUOUS_NAME.); |
| 363 | goto out; |
| 364 | } |
| 365 | else if (ctx.matchescxx . . $ lookup_cb_s#matches. == 1) |
| 366 | { |
| 367 | *serialno = ctx.foundcxx . . $ lookup_cb_s#found.; |
| 368 | ctx.foundcxx . . $ lookup_cb_s#found. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 369 | } |
| 370 | |
| 371 | out: |
| 372 | |
| 373 | xfreecxx . . $ `src/poldi.h:39:9`! (ctx.found); |
| 374 | |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | /* END */ |