Debian Codegraph @

src/pam/pam_poldi.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* pam_poldi.c - PAM authentication via OpenPGP smartcards.
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 <poldi.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <syslog.h>
25 #include <stdarg.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <pwd.h>
30 #include <assert.h>
31
32 #define PAM_SM_AUTH
33 #include <security/pam_modules.h>
34 #include <security/pam_appl.h>
35
36 #include "util/simplelog.h"
37 #include "util/simpleparse.h"
38 #include "util/defs.h"
39 #include "scd/scd.h"
40
41 #include "auth-support/wait-for-card.h"
42 #include "auth-support/conv.h"
43 #include "auth-support/getpin-cb.h"
44 #include "auth-methods.h"
45
46
47
48 /*** Auth methods declarations. ***/
49
50 /* Declare authentication methods. */
51 extern struct auth_method_s auth_method_localdb;
52 extern struct auth_method_s auth_method_x509;
53
54 /* List element type for AUTH_METHODS list below. */
55 struct auth_method
56 {
57 const char *name;
58 auth_method_t method;
59 };
60
61 /* List associating authenting method definitions with their
62 names. */
63 static struct auth_method auth_methods[] =
64 {
65 #ifdef ENABLE_AUTH_METHOD_LOCALDB
66 { "localdb", &auth_method_localdb },
67 #endif
68 #ifdef ENABLE_AUTH_METHOD_X509
69 { "x509", &auth_method_x509 },
70 #endif
71 { NULL }
72 };
73
74
75
76 /*** Option parsing. ***/
77
78 /* IDs for supported options. */
79 enum opt_ids
80 {
81 opt_none,
82 opt_logfile,
83 opt_auth_method,
84 opt_debug,
85 opt_scdaemon_program,
86 opt_scdaemon_options,
87 opt_modify_environment,
88 opt_quiet,
89 };
90
91 /* Full specifications for options. */
92 static simpleparse_opt_spec_t opt_specs[] =
93 {
94 { opt_logfile, "log-file",
95 0, SIMPLEPARSE_ARG_REQUIRED, 0, "Specify file to user for logging" },
96 { opt_auth_method, "auth-method",
97 0, SIMPLEPARSE_ARG_REQUIRED, 0, "Specify authentication method" },
98 { opt_debug, "debug",
99 0, SIMPLEPARSE_ARG_NONE, 0, "Enable debugging mode" },
100 { opt_scdaemon_program, "scdaemon-program",
101 0, SIMPLEPARSE_ARG_REQUIRED, 0, "Specify scdaemon executable to use" },
102 { opt_scdaemon_options, "scdaemon-options",
103 0, SIMPLEPARSE_ARG_REQUIRED, 0, "Specify scdaemon configuration file to use" },
104 { opt_modify_environment, "modify-environment",
105 0, SIMPLEPARSE_ARG_NONE, 0, "Set Poldi related variables in the PAM environment" },
106 { opt_quiet, "quiet",
107 0, SIMPLEPARSE_ARG_NONE, 0, "Be more quiet during PAM conversation with user" },
108 { 0 }
109 };
110
111 /* Lookup an auth_method struct by it's NAME, return it's index in
112 AUTH_METHODS list or -1 if lookup failed. */
113 static int
114 auth_method_lookup (const char *name)
115 {
116 int i;
117
118 for (i = 0; auth_methods[i].name; i++)
119 if (strcmp (auth_methods[i].name, name) == 0)
120 break;
121
122 if (auth_methods[i].name)
123 return i;
124 else
125 return -1;
126 }
127
128 /* Callback for authentication method independent option parsing. */
129 static gpg_error_t
130 pam_poldi_options_cb (void *cookie, simpleparse_opt_spec_t spec, const char *arg)
131 {
132 gpg_err_code_t err = GPG_ERR_NO_ERROR;
133 poldi_ctx_t ctx = cookie;
134
135 if (!strcmp (spec.long_opt, "log-file"))
136 {
137 /* LOG-FILE. */
138 ctx->logfile = xtrystrdup (arg);
139 if (!ctx->logfile)
140 {
141 err = gpg_error_from_errno (errno);
142 log_msg_error (ctx->loghandle,
143 "failed to duplicate %s: %s",
144 "logfile name", gpg_strerror (err));
145 }
146 }
147 else if (!strcmp (spec.long_opt, "scdaemon-program"))
148 {
149 /* SCDAEMON-PROGRAM. */
150
151 ctx->scdaemon_program = strdup (arg);
152 if (!ctx->scdaemon_program)
153 {
154 err = gpg_error_from_errno (errno);
155 log_msg_error (ctx->loghandle,
156 "failed to duplicate %s: %s",
157 "scdaemon program name",
158 gpg_strerror (err));
159 }
160 }
161 else if (!strcmp (spec.long_opt, "scdaemon-options"))
162 {
163 /* SCDAEMON-OPTIONS. */
164
165 ctx->scdaemon_options = strdup (arg);
166 if (!ctx->scdaemon_options)
167 {
168 err = gpg_error_from_errno (errno);
169 log_msg_error (ctx->loghandle,
170 "failed to duplicate %s: %s",
171 "scdaemon options name",
172 gpg_strerror (err));
173 }
174 }
175 else if (!strcmp (spec.long_opt, "auth-method"))
176 {
177 /* AUTH-METHOD. */
178
179 int method = auth_method_lookup (arg);
180 if (method >= 0)
181 ctx->auth_method = method;
182 else
183 {
184 log_msg_error (ctx->loghandle,
185 "unknown authentication method '%s'",
186 arg);
187 err = GPG_ERR_INV_VALUE;
188 }
189 }
190 else if (!strcmp (spec.long_opt, "debug"))
191 {
192 /* DEBUG. */
193 ctx->debug = 1;
194 log_set_min_level (ctx->loghandle, LOG_LEVEL_DEBUG);
195 }
196 else if (!strcmp (spec.long_opt, "modify-environment"))
197 {
198 /* MODIFY-ENVIRONMENT. */
199 ctx->modify_environment = 1;
200 }
201 else if (!strcmp (spec.long_opt, "quiet"))
202 {
203 /* QUIET. */
204 ctx->quiet = 1;
205 }
206
207 return gpg_error (err);
208 }
209
210 /* This callback is used for simpleparse. */
211 static const char *
212 i18n_cb (void *cookie, const char *msg)
213 {
214 return _(msg);
215 }
216
217
218
219 static struct poldi_ctx_s poldi_ctx_NULL; /* For initialization
220 purpose. */
221
222 /* Create new, empty Poldi context. Return proper error code. */
223 static gpg_error_t
224 create_context (poldi_ctx_t *context, pam_handle_t *pam_handle)
225 {
226 gpg_error_t err;
227 poldi_ctx_t ctx;
228
229 err = 0;
230
231 /* Allocate. */
232 ctx = xtrymalloc (sizeof (*ctx));
233 if (!ctx)
234 {
235 err = gpg_error_from_errno (errno);
236 goto out;
237 }
238
239 /* Initialize. */
240
241 *ctx = poldi_ctx_NULL;
242
243 ctx->auth_method = -1;
244 ctx->cardinfo = scd_cardinfo_null;
245 ctx->pam_handle = pam_handle;
246
247 err = log_create (&ctx->loghandle);
248 if (err)
249 goto out;
250
251 err = simpleparse_create (&ctx->parsehandle);
252 if (err)
253 goto out;
254
255 simpleparse_set_loghandle (ctx->parsehandle, ctx->loghandle);
256 simpleparse_set_parse_cb (ctx->parsehandle, pam_poldi_options_cb, ctx);
257 simpleparse_set_specs (ctx->parsehandle, opt_specs);
258 simpleparse_set_i18n_cb (ctx->parsehandle, i18n_cb, NULL);
259
260 *context = ctx;
261
262 out:
263
264 if (err)
265 {
266 if (ctx)
267 {
268 simpleparse_destroy (ctx->parsehandle);
269 log_destroy (ctx->loghandle);
270 xfree (ctx);
271 }
272 }
273
274 return err;
275 }
276
277 /* Deallocates resources associated with context CTX. */
278 static void
279 destroy_context (poldi_ctx_t ctx)
280 {
281 if (ctx)
282 {
283 xfree (ctx->logfile);
284 simpleparse_destroy (ctx->parsehandle);
285 log_destroy (ctx->loghandle);
286 xfree (ctx->scdaemon_program);
287 xfree (ctx->scdaemon_options);
288 scd_disconnect (ctx->scd);
289 scd_release_cardinfo (ctx->cardinfo);
290 /* FIXME: not very consistent: conv is (de-)allocated by caller. -mo */
291 xfree (ctx);
292 }
293 }
294
295
296
297 /*
298 * Environment setting.
299 */
300
301 static void
302 modify_environment_putenv (pam_handle_t *pam_handle, poldi_ctx_t ctx,
303 const char *name, const char *value)
304 {
305 char *str;
306 int ret;
307
308 str = NULL;
309 ret = asprintf (&str, "%s=%s", name, value);
310 if (ret < 0)
311 {
312 log_msg_error (ctx->loghandle,
313 "asprintf() failed in modify_environment_putenv(): %s",
314 errno);
315 return;
316 }
317
318 ret = pam_putenv (pam_handle, str);
319 if (ret != PAM_SUCCESS)
320 {
321 log_msg_error (ctx->loghandle,
322 "pam_putenv() failed in modify_environment_putenv(): %s",
323 pam_strerror (pam_handle, ret));
324 }
325 free (str);
326 }
327
328 static void
329 modify_environment (pam_handle_t *pam_handle, poldi_ctx_t ctx)
330 {
331 struct scd_cardinfo *cardinfo;
332
333 assert (pam_handle);
334 assert (ctx);
335
336 cardinfo = &ctx->cardinfo;
337
338 modify_environment_putenv (pam_handle, ctx,
339 "PAM_POLDI_AUTHENTICATED", "");
340 modify_environment_putenv (pam_handle, ctx,
341 "PAM_POLDI_SERIALNO", cardinfo->serialno);
342 modify_environment_putenv (pam_handle, ctx,
343 "PAM_POLDI_LANGUAGE", cardinfo->disp_lang);
344 }
345
346
347 /*
348 * PAM interface.
349 */
350
351 /* Uaaahahahh, ich will dir einloggen! PAM authentication entry
352 point. */
353 PAM_EXTERN int
354 pam_sm_authenticate (pam_handle_t *pam_handle,
355 int flags, int argc, const char **argv)
356 {
357 const void *conv_void;
358 gpg_error_t err;
359 poldi_ctx_t ctx;
360 conv_t conv;
361 scd_context_t scd_ctx;
362 int ret;
363 const char *pam_username;
364 struct auth_method_parse_cookie method_parse_cookie = { NULL, NULL };
365 simpleparse_handle_t method_parse;
366 struct getpin_cb_data getpin_cb_data;
367 int use_agent = 0;
368
369 pam_username = NULL;
370 scd_ctx = NULL;
371 conv = NULL;
372 ctx = NULL;
373 method_parse = NULL;
374 err = 0;
375
376 /*** Basic initialization. ***/
377
378 bindtextdomain (PACKAGE, LOCALEDIR);
379
380 /* Initialize Libgcrypt. Disable secure memory for now; because of
381 the implicit priviledge dropping, having secure memory enabled
382 causes the following error:
383
384 su: Authentication service cannot retrieve authentication
385 info. */
386 gcry_control (GCRYCTL_DISABLE_SECMEM);
387
388 /*** Setup main context. ***/
389
390 err = create_context (&ctx, pam_handle);
391 if (err)
392 goto out;
393
394 /* Setup logging prefix. */
395 log_set_flags (ctx->loghandle,
396 LOG_FLAG_WITH_PREFIX | LOG_FLAG_WITH_TIME | LOG_FLAG_WITH_PID);
397 log_set_prefix (ctx->loghandle, "Poldi");
398 log_set_backend_syslog (ctx->loghandle);
399
400 /*** Parse auth-method independent options. ***/
401
402 /* ... from configuration file: */
403 err = simpleparse_parse_file (ctx->parsehandle, 0, POLDI_CONF_FILE);
404 if (err)
405 {
406 log_msg_error (ctx->loghandle,
407 "failed to parse configuration file '%s': %s",
408 POLDI_CONF_FILE,
409 gpg_strerror (err));
410 goto out;
411 }
412
413 /* ... and from argument vector provided by PAM: */
414 if (argc)
415 {
416 err = simpleparse_parse (ctx->parsehandle, 0, argc, argv, NULL);
417 if (err)
418 {
419 log_msg_error (ctx->loghandle,
420 "failed to parse PAM argument vector: %s",
421 gpg_strerror (err));
422 goto out;
423 }
424 }
425
426 /*** Initialize logging. ***/
427
428 /* In case `logfile' has been set in the configuration file,
429 initialize jnlib-logging the traditional file, loggin to the file
430 (or socket special file) specified in the configuration file; in
431 case `logfile' has NOT been set in the configuration file, log
432 through Syslog. */
433 if (ctx->logfile)
434 {
435 gpg_error_t rc;
436
437 rc = log_set_backend_file (ctx->loghandle, ctx->logfile);
438 if (rc != 0)
439 /* Last try... */
440 log_set_backend_syslog (ctx->loghandle);
441 }
442
443 /*** Sanity checks. ***/
444
445 /* Authentication method to use must be specified. */
446 if (ctx->auth_method < 0)
447 {
448 log_msg_error (ctx->loghandle,
449 "no authentication method specified");
450 err = GPG_ERR_CONFIGURATION;
451 goto out;
452 }
453
454 /* Authentication methods must provide a parser callback in case
455 they have specific a configuration file. */
456 assert ((!auth_methods[ctx->auth_method].method->config)
457 || (auth_methods[ctx->auth_method].method->parsecb
458 && auth_methods[ctx->auth_method].method->opt_specs));
459
460 if (ctx->debug)
461 {
462 log_msg_debug (ctx->loghandle,
463 "using authentication method `%s'",
464 auth_methods[ctx->auth_method].name);
465 }
466
467 /*** Init authentication method. ***/
468
469 if (auth_methods[ctx->auth_method].method->func_init)
470 {
471 err = (*auth_methods[ctx->auth_method].method->func_init) (&ctx->cookie);
472 if (err)
473 {
474 log_msg_error (ctx->loghandle,
475 "failed to initialize authentication method %i: %s",
476 -1, gpg_strerror (err));
477 goto out;
478 }
479 }
480
481 if (auth_methods[ctx->auth_method].method->config)
482 {
483 /* Do auth-method specific parsing. */
484
485 err = simpleparse_create (&method_parse);
486 if (err)
487 {
488 log_msg_error (ctx->loghandle,
489 "failed to initialize parsing of configuration file for authentication method %s: %s",
490 auth_methods[ctx->auth_method].name, gpg_strerror (err));
491 goto out_parsing;
492 }
493
494 method_parse_cookie.poldi_ctx = ctx;
495 method_parse_cookie.method_ctx = ctx->cookie;
496
497 simpleparse_set_loghandle (method_parse, ctx->loghandle);
498 simpleparse_set_parse_cb (method_parse,
499 auth_methods[ctx->auth_method].method->parsecb,
500 &method_parse_cookie);
501 simpleparse_set_i18n_cb (method_parse, i18n_cb, NULL);
502 simpleparse_set_specs (method_parse,
503 auth_methods[ctx->auth_method].method->opt_specs);
504
505 err = simpleparse_parse_file (method_parse, 0,
506 auth_methods[ctx->auth_method].method->config);
507 if (err)
508 {
509 log_msg_error (ctx->loghandle,
510 "failed to parse configuration for authentication method %i: %s",
511 auth_methods[ctx->auth_method].name, gpg_strerror (err));
512 goto out_parsing;
513 }
514
515 out_parsing:
516
517 simpleparse_destroy (method_parse);
518 if (err)
519 goto out;
520 }
521
522 /*** Prepare PAM interaction. ***/
523
524 /* Ask PAM for conv structure. */
525 ret = pam_get_item (ctx->pam_handle, PAM_CONV, &conv_void);
526 if (ret != PAM_SUCCESS)
527 {
528 log_msg_error (ctx->loghandle,
529 "failed to retrieve PAM conversation structure");
530 err = GPG_ERR_INTERNAL;
531 goto out;
532 }
533
534 /* Init conv subsystem by creating a conv_t object. */
535 err = conv_create (&conv, conv_void);
536 if (err)
537 goto out;
538
539 ctx->conv = conv;
540
541 /*** Retrieve username from PAM. ***/
542
543 ret = pam_get_item (ctx->pam_handle, PAM_USER, (const void **)&pam_username);
544 if (ret != PAM_SUCCESS)
545 {
546 /* It's not fatal, username can be in the card. */
547 log_msg_error (ctx->loghandle, "Can't retrieve username from PAM");
548 }
549
550 /*** Check if we use gpg-agent. ***/
551 {
552 struct passwd *pw;
553 pw = getpwuid (getuid ());
554
555 if (pw == NULL)
556 {
557 err = gpg_error_from_syserror ();
558 goto out;
559 }
560
561 /* Supporting backward compatibility of old Poldi.
562 *
563 * For use cases of sudo and screen unlock where a user wants to
564 * use smartcard using the existing scdaemon under gpg-agent.
565 */
566 if (pam_username && !strcmp (pw->pw_name, pam_username))
567 use_agent = 1;
568 }
569
570 /*** Connect to Scdaemon. ***/
571
572 err = scd_connect (&scd_ctx, use_agent,
573 ctx->scdaemon_program, ctx->scdaemon_options,
574 ctx->loghandle);
575 if (err)
576 goto out;
577
578 ctx->scd = scd_ctx;
579
580 /* Install PIN retrival callback. */
581 getpin_cb_data.poldi_ctx = ctx;
582 scd_set_pincb (ctx->scd, getpin_cb, &getpin_cb_data);
583
584 /*** Wait for card insertion. ***/
585
586 if (pam_username)
587 {
588 if (ctx->debug)
589 log_msg_debug (ctx->loghandle, "Waiting for card for user `%s'...", pam_username);
590 if (!ctx->quiet)
591 conv_tell (ctx->conv, _("Insert authentication card for user `%s'"), pam_username);
592 }
593 else
594 {
595 if (ctx->debug)
596 log_msg_debug (ctx->loghandle, "Waiting for card...");
597 if (!ctx->quiet)
598 conv_tell (ctx->conv, _("Insert authentication card"));
599 }
600
•601 err = wait_for_card (ctx->scd, 3);
602 if (err)
603 {
604 log_msg_error (ctx->loghandle, "failed to wait for card insertion: %s",
605 gpg_strerror (err));
606 goto out;
607 }
608
609 /*** Receive card info. ***/
610
611 err = scd_learn (ctx->scd, &ctx->cardinfo);
612 if (err)
613 goto out;
614
615 if (ctx->debug)
616 log_msg_debug (ctx->loghandle,
617 "connected to card; serial number is: %s",
618 ctx->cardinfo.serialno);
619
620 /*** Authenticate. ***/
621
622 if (pam_username)
623 {
624 /* Try to authenticate user as PAM_USERNAME. */
625
626 if (!(*auth_methods[ctx->auth_method].method->func_auth_as) (ctx, ctx->cookie,
627 pam_username))
628 /* Authentication failed. */
629 err = GPG_ERR_GENERAL;
630 }
631 else
632 {
633 /* Try to authenticate user, choosing an identity is up to the
634 user. */
635
636 char *username_authenticated = NULL;
637
638 if (!(*auth_methods[ctx->auth_method].method->func_auth) (ctx, ctx->cookie,
639 &username_authenticated))
640 /* Authentication failed. */
641 err = GPG_ERR_GENERAL;
642 else
643 {
644 /* Send username received during authentication process back
645 to PAM. */
646 ret = pam_set_item (ctx->pam_handle, PAM_USER,
647 username_authenticated);
648 if (ret == PAM_SUCCESS)
649 err = 0;
650 else
651 err = gpg_error (GPG_ERR_INTERNAL);
652
653 xfree (username_authenticated);
654 }
655 }
656
657 out:
658
659 /* Log result. */
660 if (err)
661 log_msg_error (ctx->loghandle, "authentication failed: %s", gpg_strerror (err));
662 else
663 {
664 if (ctx->debug)
665 log_msg_debug (ctx->loghandle, "authentication succeeded");
666 if (ctx->modify_environment)
667 modify_environment (pam_handle, ctx);
668 }
669
670 /* Call authentication method's deinit callback. */
671 if ((ctx->auth_method >= 0)
672 && auth_methods[ctx->auth_method].method->func_deinit)
673 (*auth_methods[ctx->auth_method].method->func_deinit) (ctx->cookie);
674
675 /* FIXME, cosmetics? */
676 conv_destroy (conv);
677 destroy_context (ctx);
678
679 /* Return to PAM. */
680
681 return err ? PAM_AUTH_ERR : PAM_SUCCESS;
682 }
683
684 /* PAM's `set-credentials' interface. */
685 PAM_EXTERN int
686 pam_sm_setcred (pam_handle_t *pam_handle,
687 int flags, int argc, const char **argv)
688 {
689 return PAM_SUCCESS;
690 }
691
692 /* END */