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>cxx . . $ `<file>/src/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_AUTHcxx . . $ `src/pam/pam_poldi.c:32:9`!
33 #include <security/pam_modules.h>
34 #include <security/pam_appl.h>
35
36 #include "util/simplelog.h"cxx . . $ `<file>/src/util/simplelog.h`/
37 #include "util/simpleparse.h"cxx . . $ `<file>/src/util/simpleparse.h`/
38 #include "util/defs.h"cxx . . $ `<file>/src/util/defs.h`/
39 #include "scd/scd.h"cxx . . $ `<file>/src/scd/scd.h`/
40
41 #include "auth-support/wait-for-card.h"cxx . . $ `<file>/src/pam/auth-support/wait-for-card.h`/
42 #include "auth-support/conv.h"cxx . . $ `<file>/src/pam/auth-support/conv.h`/
43 #include "auth-support/getpin-cb.h"cxx . . $ `<file>/src/pam/auth-support/getpin-cb.h`/
44 #include "auth-methods.h"cxx . . $ `<file>/src/pam/auth-methods.h`/
45
46
47
48 /*** Auth methods declarations. ***/
49
50 /* Declare authentication methods. */
51 extern struct auth_method_scxx . . $ auth_method_s# auth_method_localdbcxx . . $ auth_method_localdb.;
52 extern struct auth_method_scxx . . $ auth_method_s# auth_method_x509cxx . . $ auth_method_x509.;
53
54 /* List element type for AUTH_METHODS list below. */
55 struct auth_methodcxx . . $ auth_method#
56 {
57 const char *namecxx . . $ auth_method#name.;
58 auth_method_tcxx . . $ auth_method_t# methodcxx . . $ auth_method#method.;
59 };
60
61 /* List associating authenting method definitions with their
62 names. */
63 static struct auth_methodcxx . . $ auth_method# auth_methodscxx . . $ auth_methods.[] =
64 {
65 #ifdef ENABLE_AUTH_METHOD_LOCALDBcxx . . $ `config.h:5:9`!
66 { "localdb", &auth_method_localdbcxx . . $ auth_method_localdb. },
67 #endif
68 #ifdef ENABLE_AUTH_METHOD_X509cxx . . $ `config.h:8:9`!
69 { "x509", &auth_method_x509cxx . . $ auth_method_x509. },
70 #endif
71 { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! }
72 };
73
74
75
76 /*** Option parsing. ***/
77
78 /* IDs for supported options. */
79 enum opt_idscxx . . $ opt_ids#
80 {
81 opt_nonecxx . . $ opt_none.,
82 opt_logfilecxx . . $ opt_logfile.,
83 opt_auth_methodcxx . . $ opt_auth_method.,
84 opt_debugcxx . . $ opt_debug.,
85 opt_scdaemon_programcxx . . $ opt_scdaemon_program.,
86 opt_scdaemon_optionscxx . . $ opt_scdaemon_options.,
87 opt_modify_environmentcxx . . $ opt_modify_environment.,
88 opt_quietcxx . . $ opt_quiet.,
89 };
90
91 /* Full specifications for options. */
92 static simpleparse_opt_spec_tcxx . . $ simpleparse_opt_spec_t# opt_specscxx . . $ opt_specs.[] =
93 {
94 { opt_logfilecxx . . $ opt_logfile., "log-file",
95 0, SIMPLEPARSE_ARG_REQUIREDcxx . . $ SIMPLEPARSE_ARG_REQUIRED., 0, "Specify file to user for logging" },
96 { opt_auth_methodcxx . . $ opt_auth_method., "auth-method",
97 0, SIMPLEPARSE_ARG_REQUIREDcxx . . $ SIMPLEPARSE_ARG_REQUIRED., 0, "Specify authentication method" },
98 { opt_debugcxx . . $ opt_debug., "debug",
99 0, SIMPLEPARSE_ARG_NONEcxx . . $ SIMPLEPARSE_ARG_NONE., 0, "Enable debugging mode" },
100 { opt_scdaemon_programcxx . . $ opt_scdaemon_program., "scdaemon-program",
101 0, SIMPLEPARSE_ARG_REQUIREDcxx . . $ SIMPLEPARSE_ARG_REQUIRED., 0, "Specify scdaemon executable to use" },
102 { opt_scdaemon_optionscxx . . $ opt_scdaemon_options., "scdaemon-options",
103 0, SIMPLEPARSE_ARG_REQUIREDcxx . . $ SIMPLEPARSE_ARG_REQUIRED., 0, "Specify scdaemon configuration file to use" },
104 { opt_modify_environmentcxx . . $ opt_modify_environment., "modify-environment",
105 0, SIMPLEPARSE_ARG_NONEcxx . . $ SIMPLEPARSE_ARG_NONE., 0, "Set Poldi related variables in the PAM environment" },
106 { opt_quietcxx . . $ opt_quiet., "quiet",
107 0, SIMPLEPARSE_ARG_NONEcxx . . $ 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_lookupcxx . . $ auth_method_lookup(5c4306d8553b3696). (const char *name)
115 {
116 int i;
117
118 for (i = 0; auth_methodscxx . . $ auth_methods.[i].namecxx . . $ auth_method#name.; i++)
119 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88). (auth_methodscxx . . $ auth_methods.[i].namecxx . . $ auth_method#name., name) == 0)
120 break;
121
122 if (auth_methodscxx . . $ auth_methods.[i].namecxx . . $ auth_method#name.)
123 return i;
124 else
125 return -1;
126 }
127
128 /* Callback for authentication method independent option parsing. */
129 static gpg_error_tcxx . . $ gpg_error_t#
130 pam_poldi_options_cbcxx . . $ pam_poldi_options_cb(c7ea03b5806a970). (void *cookie, simpleparse_opt_spec_tcxx . . $ simpleparse_opt_spec_t# spec, const char *arg)
131 {
132 gpg_err_code_tcxx . . $ gpg_err_code_t# err = GPG_ERR_NO_ERRORcxx . . $ GPG_ERR_NO_ERROR.;
133 poldi_ctx_tcxx . . $ poldi_ctx_t# ctx = cookie;
134
135 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "log-file"))
136 {
137 /* LOG-FILE. */
138 ctx->logfilecxx . . $ poldi_ctx_s#logfile. = xtrystrdupcxx . . $ `src/poldi.h:37:9`! (arg);
139 if (!ctx->logfilecxx . . $ poldi_ctx_s#logfile.)
140 {
141 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
142 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
143 "failed to duplicate %s: %s",
144 "logfile name", gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
145 }
146 }
147 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "scdaemon-program"))
148 {
149 /* SCDAEMON-PROGRAM. */
150
151 ctx->scdaemon_programcxx . . $ poldi_ctx_s#scdaemon_program. = strdupcxx . . $ strdup(a8b8e32347182212). (arg);
152 if (!ctx->scdaemon_programcxx . . $ poldi_ctx_s#scdaemon_program.)
153 {
154 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
155 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
156 "failed to duplicate %s: %s",
157 "scdaemon program name",
158 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
159 }
160 }
161 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "scdaemon-options"))
162 {
163 /* SCDAEMON-OPTIONS. */
164
165 ctx->scdaemon_optionscxx . . $ poldi_ctx_s#scdaemon_options. = strdupcxx . . $ strdup(a8b8e32347182212). (arg);
166 if (!ctx->scdaemon_optionscxx . . $ poldi_ctx_s#scdaemon_options.)
167 {
168 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
169 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
170 "failed to duplicate %s: %s",
171 "scdaemon options name",
172 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
173 }
174 }
175 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "auth-method"))
176 {
177 /* AUTH-METHOD. */
178
179 int method = auth_method_lookupcxx . . $ auth_method_lookup(5c4306d8553b3696). (arg);
180 if (method >= 0)
181 ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method. = method;
182 else
183 {
184 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
185 "unknown authentication method '%s'",
186 arg);
187 err = GPG_ERR_INV_VALUEcxx . . $ GPG_ERR_INV_VALUE.;
188 }
189 }
190 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "debug"))
191 {
192 /* DEBUG. */
193 ctx->debugcxx . . $ poldi_ctx_s#debug. = 1;
194 log_set_min_levelcxx . . $ log_set_min_level(af723febd09b6652). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG.);
195 }
196 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "modify-environment"))
197 {
198 /* MODIFY-ENVIRONMENT. */
199 ctx->modify_environmentcxx . . $ poldi_ctx_s#modify_environment. = 1;
200 }
201 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88). (spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., "quiet"))
202 {
203 /* QUIET. */
204 ctx->quietcxx . . $ poldi_ctx_s#quiet. = 1;
205 }
206
207 return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (err);
208 }
209
210 /* This callback is used for simpleparse. */
211 static const char *
212 i18n_cbcxx . . $ i18n_cb(f93dde937f31de16). (void *cookie, const char *msg)
213 {
214 return _cxx . . $ `src/poldi.h:45:9`!(msg);
215 }
216
217
218
219 static struct poldi_ctx_scxx . . $ poldi_ctx_s# poldi_ctx_NULLcxx . . $ poldi_ctx_NULL.; /* For initialization
220 purpose. */
221
222 /* Create new, empty Poldi context. Return proper error code. */
223 static gpg_error_tcxx . . $ gpg_error_t#
224 create_contextcxx . . $ create_context(f8431182ee8e27fa). (poldi_ctx_tcxx . . $ poldi_ctx_t# *context, pam_handle_tcxx . . $ pam_handle_t# *pam_handle)
225 {
226 gpg_error_tcxx . . $ gpg_error_t# err;
227 poldi_ctx_tcxx . . $ poldi_ctx_t# ctx;
228
229 err = 0;
230
231 /* Allocate. */
232 ctx = xtrymalloccxx . . $ `src/poldi.h:35:9`! (sizeof (*ctx));
233 if (!ctx)
234 {
235 err = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
236 goto out;
237 }
238
239 /* Initialize. */
240
241 *ctx = poldi_ctx_NULLcxx . . $ poldi_ctx_NULL.;
242
243 ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method. = -1;
244 ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo. = scd_cardinfo_nullcxx . . $ scd_cardinfo_null.;
245 ctx->pam_handlecxx . . $ poldi_ctx_s#pam_handle. = pam_handle;
246
247 err = log_createcxx . . $ log_create(ae9175ffffc76e55). (&ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
248 if (err)
249 goto out;
250
251 err = simpleparse_createcxx . . $ simpleparse_create(66008bf8c18601a7). (&ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle.);
252 if (err)
253 goto out;
254
255 simpleparse_set_loghandlecxx . . $ simpleparse_set_loghandle(160e2747cca04437). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle., ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
256 simpleparse_set_parse_cbcxx . . $ simpleparse_set_parse_cb(3172d0a439858735). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle., pam_poldi_options_cbcxx . . $ pam_poldi_options_cb(c7ea03b5806a970)., ctx);
257 simpleparse_set_specscxx . . $ simpleparse_set_specs(79a7ca40abe9bcfd). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle., opt_specscxx . . $ opt_specs.);
258 simpleparse_set_i18n_cbcxx . . $ simpleparse_set_i18n_cb(b8188d2bcc1bdd70). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle., i18n_cbcxx . . $ i18n_cb(f93dde937f31de16)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
259
260 *context = ctx;
261
262 out:
263
264 if (err)
265 {
266 if (ctx)
267 {
268 simpleparse_destroycxx . . $ simpleparse_destroy(f8a3a57ff17a2201). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle.);
269 log_destroycxx . . $ log_destroy(c6d31a59c40299be). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
270 xfreecxx . . $ `src/poldi.h:39:9`! (ctx);
271 }
272 }
273
274 return err;
275 }
276
277 /* Deallocates resources associated with context CTX. */
278 static void
279 destroy_contextcxx . . $ destroy_context(63eeaf9da5f3cc32). (poldi_ctx_tcxx . . $ poldi_ctx_t# ctx)
280 {
281 if (ctx)
282 {
283 xfreecxx . . $ `src/poldi.h:39:9`! (ctx->logfile);
284 simpleparse_destroycxx . . $ simpleparse_destroy(f8a3a57ff17a2201). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle.);
285 log_destroycxx . . $ log_destroy(c6d31a59c40299be). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
286 xfreecxx . . $ `src/poldi.h:39:9`! (ctx->scdaemon_program);
287 xfreecxx . . $ `src/poldi.h:39:9`! (ctx->scdaemon_options);
288 scd_disconnectcxx . . $ scd_disconnect(96f585546e882556). (ctx->scdcxx . . $ poldi_ctx_s#scd.);
289 scd_release_cardinfocxx . . $ scd_release_cardinfo(4bb01e237e9372f6). (ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo.);
290 /* FIXME: not very consistent: conv is (de-)allocated by caller. -mo */
291 xfreecxx . . $ `src/poldi.h:39:9`! (ctx);
292 }
293 }
294
295
296
297 /*
298 * Environment setting.
299 */
300
301 static void
302 modify_environment_putenvcxx . . $ modify_environment_putenv(4f501743968eea8c). (pam_handle_tcxx . . $ pam_handle_t# *pam_handle, poldi_ctx_tcxx . . $ poldi_ctx_t# ctx,
303 const char *name, const char *value)
304 {
305 char *str;
306 int ret;
307
308 str = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
309 ret = asprintfcxx . . $ asprintf(70ad09b5e10823c4). (&str, "%s=%s", name, value);
310 if (ret < 0)
311 {
312 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
313 "asprintf() failed in modify_environment_putenv(): %s",
314 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
315 return;
316 }
317
318 ret = pam_putenvcxx . . $ pam_putenv(f104be91e6692e9). (pam_handle, str);
319 if (ret != PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!)
320 {
321 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
322 "pam_putenv() failed in modify_environment_putenv(): %s",
323 pam_strerrorcxx . . $ pam_strerror(b465d44ed4c5fb1c). (pam_handle, ret));
324 }
325 freecxx . . $ free(5d22bdacc48458e8). (str);
326 }
327
328 static void
329 modify_environmentcxx . . $ modify_environment(30a6f0d227ff4262). (pam_handle_tcxx . . $ pam_handle_t# *pam_handle, poldi_ctx_tcxx . . $ poldi_ctx_t# ctx)
330 {
331 struct scd_cardinfocxx . . $ scd_cardinfo# *cardinfo;
332
333 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (pam_handle);
334 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (ctx);
335
336 cardinfo = &ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo.;
337
338 modify_environment_putenvcxx . . $ modify_environment_putenv(4f501743968eea8c). (pam_handle, ctx,
339 "PAM_POLDI_AUTHENTICATED", "");
340 modify_environment_putenvcxx . . $ modify_environment_putenv(4f501743968eea8c). (pam_handle, ctx,
341 "PAM_POLDI_SERIALNO", cardinfo->serialnocxx . . $ scd_cardinfo#serialno.);
342 modify_environment_putenvcxx . . $ modify_environment_putenv(4f501743968eea8c). (pam_handle, ctx,
343 "PAM_POLDI_LANGUAGE", cardinfo->disp_langcxx . . $ scd_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_EXTERNcxx . . $ `<external>/5130975668888882828/pam_modules.h:80:9`! int
354 pam_sm_authenticatecxx . . $ pam_sm_authenticate(60e77177d68aa4a0). (pam_handle_tcxx . . $ pam_handle_t# *pam_handle,
355 int flags, int argc, const char **argv)
356 {
357 const void *conv_void;
358 gpg_error_tcxx . . $ gpg_error_t# err;
359 poldi_ctx_tcxx . . $ poldi_ctx_t# ctx;
360 conv_tcxx . . $ conv_t# conv;
361 scd_context_tcxx . . $ scd_context_t# scd_ctx;
362 int ret;
363 const char *pam_username;
364 struct auth_method_parse_cookiecxx . . $ auth_method_parse_cookie# method_parse_cookie = { 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`! };
365 simpleparse_handle_tcxx . . $ simpleparse_handle_t# method_parse;
366 struct getpin_cb_datacxx . . $ getpin_cb_data# getpin_cb_data;
367 int use_agent = 0;
368
369 pam_username = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
370 scd_ctx = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
371 conv = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
372 ctx = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
373 method_parse = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
374 err = 0;
375
376 /*** Basic initialization. ***/
377
378 bindtextdomaincxx . . $ bindtextdomain(a713fe7b9a57cf96). (PACKAGEcxx . . $ `config.h:88:9`!, LOCALEDIRcxx . . $ `<built-in>:410:9`!);
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_controlcxx . . $ gcry_control(aec8ac399606d42a). (GCRYCTL_DISABLE_SECMEMcxx . . $ GCRYCTL_DISABLE_SECMEM.);
387
388 /*** Setup main context. ***/
389
390 err = create_contextcxx . . $ create_context(f8431182ee8e27fa). (&ctx, pam_handle);
391 if (err)
392 goto out;
393
394 /* Setup logging prefix. */
395 log_set_flagscxx . . $ log_set_flags(a836c67e96798efc). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
396 LOG_FLAG_WITH_PREFIXcxx . . $ `src/util/simplelog.h:30:9`! | LOG_FLAG_WITH_TIMEcxx . . $ `src/util/simplelog.h:31:9`! | LOG_FLAG_WITH_PIDcxx . . $ `src/util/simplelog.h:32:9`!);
397 log_set_prefixcxx . . $ log_set_prefix(d0a2023680b6f78d). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "Poldi");
398 log_set_backend_syslogcxx . . $ log_set_backend_syslog(7a673cb698c395e0). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
399
400 /*** Parse auth-method independent options. ***/
401
402 /* ... from configuration file: */
403 err = simpleparse_parse_filecxx . . $ simpleparse_parse_file(4e37b2a77adfc21a). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle., 0, POLDI_CONF_FILEcxx . . $ `src/util/defs.h:27:9`!);
404 if (err)
405 {
406 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
407 "failed to parse configuration file '%s': %s",
408 POLDI_CONF_FILEcxx . . $ `src/util/defs.h:27:9`!,
409 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
410 goto out;
411 }
412
413 /* ... and from argument vector provided by PAM: */
414 if (argc)
415 {
416 err = simpleparse_parsecxx . . $ simpleparse_parse(62ade082e5fc30eb). (ctx->parsehandlecxx . . $ poldi_ctx_s#parsehandle., 0, argc, argv, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
417 if (err)
418 {
419 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
420 "failed to parse PAM argument vector: %s",
421 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (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->logfilecxx . . $ poldi_ctx_s#logfile.)
434 {
435 gpg_error_tcxx . . $ gpg_error_t# rc;
436
437 rc = log_set_backend_filecxx . . $ log_set_backend_file(91a1806cddc17828). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., ctx->logfilecxx . . $ poldi_ctx_s#logfile.);
438 if (rc != 0)
439 /* Last try... */
440 log_set_backend_syslogcxx . . $ log_set_backend_syslog(7a673cb698c395e0). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
441 }
442
443 /*** Sanity checks. ***/
444
445 /* Authentication method to use must be specified. */
446 if (ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method. < 0)
447 {
448 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
449 "no authentication method specified");
450 err = GPG_ERR_CONFIGURATIONcxx . . $ 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 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! ((!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->debugcxx . . $ poldi_ctx_s#debug.)
461 {
462 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
463 "using authentication method `%s'",
464 auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].namecxx . . $ auth_method#name.);
465 }
466
467 /*** Init authentication method. ***/
468
469 if (auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->func_initcxx . . $ auth_method_s#func_init.)
470 {
471 err = (*auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->func_initcxx . . $ auth_method_s#func_init.) (&ctx->cookiecxx . . $ poldi_ctx_s#cookie.);
472 if (err)
473 {
474 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
475 "failed to initialize authentication method %i: %s",
476 -1, gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
477 goto out;
478 }
479 }
480
481 if (auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->configcxx . . $ auth_method_s#config.)
482 {
483 /* Do auth-method specific parsing. */
484
485 err = simpleparse_createcxx . . $ simpleparse_create(66008bf8c18601a7). (&method_parse);
486 if (err)
487 {
488 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
489 "failed to initialize parsing of configuration file for authentication method %s: %s",
490 auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].namecxx . . $ auth_method#name., gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
491 goto out_parsing;
492 }
493
494 method_parse_cookie.poldi_ctxcxx . . $ auth_method_parse_cookie#poldi_ctx. = ctx;
495 method_parse_cookie.method_ctxcxx . . $ auth_method_parse_cookie#method_ctx. = ctx->cookiecxx . . $ poldi_ctx_s#cookie.;
496
497 simpleparse_set_loghandlecxx . . $ simpleparse_set_loghandle(160e2747cca04437). (method_parse, ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
498 simpleparse_set_parse_cbcxx . . $ simpleparse_set_parse_cb(3172d0a439858735). (method_parse,
499 auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->parsecbcxx . . $ auth_method_s#parsecb.,
500 &method_parse_cookie);
501 simpleparse_set_i18n_cbcxx . . $ simpleparse_set_i18n_cb(b8188d2bcc1bdd70). (method_parse, i18n_cbcxx . . $ i18n_cb(f93dde937f31de16)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
502 simpleparse_set_specscxx . . $ simpleparse_set_specs(79a7ca40abe9bcfd). (method_parse,
503 auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->opt_specscxx . . $ auth_method_s#opt_specs.);
504
505 err = simpleparse_parse_filecxx . . $ simpleparse_parse_file(4e37b2a77adfc21a). (method_parse, 0,
506 auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->configcxx . . $ auth_method_s#config.);
507 if (err)
508 {
509 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
510 "failed to parse configuration for authentication method %i: %s",
511 auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].namecxx . . $ auth_method#name., gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
512 goto out_parsing;
513 }
514
515 out_parsing:
516
517 simpleparse_destroycxx . . $ simpleparse_destroy(f8a3a57ff17a2201). (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_itemcxx . . $ pam_get_item(e1131d0411280f41). (ctx->pam_handlecxx . . $ poldi_ctx_s#pam_handle., PAM_CONVcxx . . $ `<external>/1475977469199658974/_pam_types.h:136:9`!, &conv_void);
526 if (ret != PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!)
527 {
528 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
529 "failed to retrieve PAM conversation structure");
530 err = GPG_ERR_INTERNALcxx . . $ GPG_ERR_INTERNAL.;
531 goto out;
532 }
533
534 /* Init conv subsystem by creating a conv_t object. */
535 err = conv_createcxx . . $ conv_create(9d51085b6828a066). (&conv, conv_void);
536 if (err)
537 goto out;
538
539 ctx->convcxx . . $ poldi_ctx_s#conv. = conv;
540
541 /*** Retrieve username from PAM. ***/
542
543 ret = pam_get_itemcxx . . $ pam_get_item(e1131d0411280f41). (ctx->pam_handlecxx . . $ poldi_ctx_s#pam_handle., PAM_USERcxx . . $ `<external>/1475977469199658974/_pam_types.h:133:9`!, (const void **)&pam_username);
544 if (ret != PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!)
545 {
546 /* It's not fatal, username can be in the card. */
547 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "Can't retrieve username from PAM");
548 }
549
550 /*** Check if we use gpg-agent. ***/
551 {
552 struct passwdcxx . . $ passwd# *pw;
553 pw = getpwuidcxx . . $ getpwuid(fd5493c96892938c). (getuidcxx . . $ getuid(db52760e79bf9054). ());
554
555 if (pw == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
556 {
557 err = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). ();
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 && !strcmpcxx . . $ strcmp(5ae29d726ae57f88). (pw->pw_namecxx . . $ passwd#pw_name., pam_username))
567 use_agent = 1;
568 }
569
570 /*** Connect to Scdaemon. ***/
571
572 err = scd_connectcxx . . $ scd_connect(b5c77484bce79416). (&scd_ctx, use_agent,
573 ctx->scdaemon_programcxx . . $ poldi_ctx_s#scdaemon_program., ctx->scdaemon_optionscxx . . $ poldi_ctx_s#scdaemon_options.,
574 ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.);
575 if (err)
576 goto out;
577
578 ctx->scdcxx . . $ poldi_ctx_s#scd. = scd_ctx;
579
580 /* Install PIN retrival callback. */
581 getpin_cb_data.poldi_ctxcxx . . $ getpin_cb_data#poldi_ctx. = ctx;
582 scd_set_pincbcxx . . $ scd_set_pincb(bb86724bcfc9ee60). (ctx->scdcxx . . $ poldi_ctx_s#scd., getpin_cbcxx . . $ getpin_cb(7f49723062cc1a32)., &getpin_cb_data);
583
584 /*** Wait for card insertion. ***/
585
586 if (pam_username)
587 {
588 if (ctx->debugcxx . . $ poldi_ctx_s#debug.)
589 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "Waiting for card for user `%s'...", pam_username);
590 if (!ctx->quietcxx . . $ poldi_ctx_s#quiet.)
591 conv_tellcxx . . $ conv_tell(93874d2501936ebd). (ctx->convcxx . . $ poldi_ctx_s#conv., _cxx . . $ `src/poldi.h:45:9`!("Insert authentication card for user `%s'"), pam_username);
592 }
593 else
594 {
595 if (ctx->debugcxx . . $ poldi_ctx_s#debug.)
596 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "Waiting for card...");
597 if (!ctx->quietcxx . . $ poldi_ctx_s#quiet.)
598 conv_tellcxx . . $ conv_tell(93874d2501936ebd). (ctx->convcxx . . $ poldi_ctx_s#conv., _cxx . . $ `src/poldi.h:45:9`!("Insert authentication card"));
599 }
600
•601 err = wait_for_cardcxx . . $ wait_for_card(a48b6dfb027d77a2). (ctx->scdcxx . . $ poldi_ctx_s#scd., 3);
602 if (err)
603 {
604 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "failed to wait for card insertion: %s",
605 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
606 goto out;
607 }
608
609 /*** Receive card info. ***/
610
611 err = scd_learncxx . . $ scd_learn(f694227e099c0ef7). (ctx->scdcxx . . $ poldi_ctx_s#scd., &ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo.);
612 if (err)
613 goto out;
614
615 if (ctx->debugcxx . . $ poldi_ctx_s#debug.)
616 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle.,
617 "connected to card; serial number is: %s",
618 ctx->cardinfocxx . . $ poldi_ctx_s#cardinfo..serialnocxx . . $ scd_cardinfo#serialno.);
619
620 /*** Authenticate. ***/
621
622 if (pam_username)
623 {
624 /* Try to authenticate user as PAM_USERNAME. */
625
626 if (!(*auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->func_auth_ascxx . . $ auth_method_s#func_auth_as.) (ctx, ctx->cookiecxx . . $ poldi_ctx_s#cookie.,
627 pam_username))
628 /* Authentication failed. */
629 err = GPG_ERR_GENERALcxx . . $ 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 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
637
638 if (!(*auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->func_authcxx . . $ auth_method_s#func_auth.) (ctx, ctx->cookiecxx . . $ poldi_ctx_s#cookie.,
639 &username_authenticated))
640 /* Authentication failed. */
641 err = GPG_ERR_GENERALcxx . . $ GPG_ERR_GENERAL.;
642 else
643 {
644 /* Send username received during authentication process back
645 to PAM. */
646 ret = pam_set_itemcxx . . $ pam_set_item(4c94bdcc568305d7). (ctx->pam_handlecxx . . $ poldi_ctx_s#pam_handle., PAM_USERcxx . . $ `<external>/1475977469199658974/_pam_types.h:133:9`!,
647 username_authenticated);
648 if (ret == PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!)
649 err = 0;
650 else
651 err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_INTERNALcxx . . $ GPG_ERR_INTERNAL.);
652
653 xfreecxx . . $ `src/poldi.h:39:9`! (username_authenticated);
654 }
655 }
656
657 out:
658
659 /* Log result. */
660 if (err)
661 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "authentication failed: %s", gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
662 else
663 {
664 if (ctx->debugcxx . . $ poldi_ctx_s#debug.)
665 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (ctx->loghandlecxx . . $ poldi_ctx_s#loghandle., "authentication succeeded");
666 if (ctx->modify_environmentcxx . . $ poldi_ctx_s#modify_environment.)
667 modify_environmentcxx . . $ modify_environment(30a6f0d227ff4262). (pam_handle, ctx);
668 }
669
670 /* Call authentication method's deinit callback. */
671 if ((ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method. >= 0)
672 && auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->func_deinitcxx . . $ auth_method_s#func_deinit.)
673 (*auth_methodscxx . . $ auth_methods.[ctx->auth_methodcxx . . $ poldi_ctx_s#auth_method.].methodcxx . . $ auth_method#method.->func_deinitcxx . . $ auth_method_s#func_deinit.) (ctx->cookiecxx . . $ poldi_ctx_s#cookie.);
674
675 /* FIXME, cosmetics? */
676 conv_destroycxx . . $ conv_destroy(1b6b60d1f1d216d8). (conv);
677 destroy_contextcxx . . $ destroy_context(63eeaf9da5f3cc32). (ctx);
678
679 /* Return to PAM. */
680
681 return err ? PAM_AUTH_ERRcxx . . $ `<external>/1475977469199658974/_pam_types.h:37:9`! : PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!;
682 }
683
684 /* PAM's `set-credentials' interface. */
685 PAM_EXTERNcxx . . $ `<external>/5130975668888882828/pam_modules.h:80:9`! int
686 pam_sm_setcredcxx . . $ pam_sm_setcred(60e77177d68aa4a0). (pam_handle_tcxx . . $ pam_handle_t# *pam_handle,
687 int flags, int argc, const char **argv)
688 {
689 return PAM_SUCCESScxx . . $ `<external>/1475977469199658974/_pam_types.h:29:9`!;
690 }
691
692 /* END */