Debian Codegraph @

src/shared/pam-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "log.h"
4 #include "pam-util.h"
5
6 #if HAVE_PAM
7
8 #include <security/pam_ext.h>
9 #include <syslog.h>
10
11 #include "sd-bus.h"
12 #include "sd-dlopen.h"
13
14 #include "alloc-util.h"
15 #include "bus-internal.h"
16 #include "errno-util.h"
17 #include "fd-util.h"
18 #include "format-util.h"
19 #include "process-util.h"
20 #include "stdio-util.h"
21 #include "string-util.h"
22
23 DLSYM_PROTOTYPE(pam_acct_mgmt) = NULL;
24 DLSYM_PROTOTYPE(pam_close_session) = NULL;
25 DLSYM_PROTOTYPE(pam_end) = NULL;
26 DLSYM_PROTOTYPE(pam_get_authtok_noverify) = NULL;
27 DLSYM_PROTOTYPE(pam_get_authtok_verify) = NULL;
28 DLSYM_PROTOTYPE(pam_get_data) = NULL;
29 DLSYM_PROTOTYPE(pam_get_item) = NULL;
30 DLSYM_PROTOTYPE(pam_get_user) = NULL;
31 DLSYM_PROTOTYPE(pam_getenv) = NULL;
32 DLSYM_PROTOTYPE(pam_getenvlist) = NULL;
33 DLSYM_PROTOTYPE(pam_open_session) = NULL;
34 DLSYM_PROTOTYPE(pam_prompt) = NULL;
35 DLSYM_PROTOTYPE(pam_putenv) = NULL;
36 DLSYM_PROTOTYPE(pam_set_data) = NULL;
37 DLSYM_PROTOTYPE(pam_set_item) = NULL;
38 DLSYM_PROTOTYPE(pam_setcred) = NULL;
39 DLSYM_PROTOTYPE(pam_start) = NULL;
40 DLSYM_PROTOTYPE(pam_strerror) = NULL;
41 DLSYM_PROTOTYPE(pam_syslog) = NULL;
42 DLSYM_PROTOTYPE(pam_vsyslog) = NULL;
43
44 void pam_log_setup(void) {
45 /* Make sure we don't leak the syslog fd we open by opening/closing the fd each time. */
46 log_set_open_when_needed(true);
47
48 /* pam logs to syslog so let's make our generic logging functions do the same thing. */
49 log_set_target(LOG_TARGET_SYSLOG);
50 }
51
52 int errno_to_pam_error(int error) {
53 return ERRNO_VALUE(error) == ENOMEM ? PAM_BUF_ERR : PAM_SERVICE_ERR;
54 }
55
56 int pam_syslog_errno(pam_handle_t *pamh, int level, int error, const char *format, ...) {
57 va_list ap;
58
59 error = ERRNO_VALUE(error);
60 LOCAL_ERRNO(error);
61
62 va_start(ap, format);
63 sym_pam_vsyslog(pamh, level, format, ap);
64 va_end(ap);
65
66 return errno_to_pam_error(error);
67 }
68
69 int pam_syslog_pam_error(pam_handle_t *pamh, int level, int error, const char *format, ...) {
70 /* This wraps pam_syslog() but will replace @PAMERR@ with a string from pam_strerror().
71 * @PAMERR@ must be at the very end. */
72
73 va_list ap;
74 va_start(ap, format);
75
76 const char *p = endswith(format, "@PAMERR@");
77 if (p) {
78 const char *pamerr = sym_pam_strerror(pamh, error);
79 if (strchr(pamerr, '%'))
80 pamerr = "n/a"; /* We cannot have any formatting chars */
81
82 char buf[p - format + strlen(pamerr) + 1];
83 xsprintf(buf, "%.*s%s", (int)(p - format), format, pamerr);
84
85 DISABLE_WARNING_FORMAT_NONLITERAL;
86 sym_pam_vsyslog(pamh, level, buf, ap);
87 REENABLE_WARNING;
88 } else
89 sym_pam_vsyslog(pamh, level, format, ap);
90
91 va_end(ap);
92
93 return error;
94 }
95
96 /* A small structure we store inside the PAM session object, that allows us to reuse bus connections but pins
97 * it to the process thoroughly. */
98 struct PamBusData {
99 sd_bus *bus;
100 pam_handle_t *pam_handle;
101 char *cache_id;
102 };
103
104 static PamBusData *pam_bus_data_free(PamBusData *d) {
105 /* The actual destructor */
106 if (!d)
107 return NULL;
108
109 /* NB: PAM sessions usually involve forking off a child process, and thus the PAM context might be
110 * duplicated in the child. This destructor might be called twice: both in the parent and in the
111 * child. sd_bus_flush_close_unref() however is smart enough to be a NOP when invoked in any other
112 * process than the one it was invoked from, hence we don't need to add any extra protection here to
113 * ensure that destruction of the bus connection in the child affects the parent's connection
114 * somehow. */
115 sd_bus_flush_close_unref(d->bus);
116 free(d->cache_id);
117
118 /* Note: we don't destroy pam_handle here, because this object is pinned by the handle, and not vice versa! */
119
120 return mfree(d);
121 }
122
123 DEFINE_TRIVIAL_CLEANUP_FUNC(PamBusData*, pam_bus_data_free);
124
125 static void pam_bus_data_destroy(pam_handle_t *pamh, void *data, int error_status) {
126 /* Destructor when called from PAM. Note that error_status is supposed to tell us via PAM_DATA_SILENT
127 * whether we are called in a forked off child of the PAM session or in the original parent. We don't
128 * bother with that however, and instead rely on the PID checks that sd_bus_flush_close_unref() does
129 * internally anyway. That said, we still generate a warning message, since this really shouldn't
130 * happen. */
131
132 if (!data)
133 return;
134
135 PamBusData *d = data;
136 if (FLAGS_SET(error_status, PAM_DATA_SILENT) &&
137 d->bus && bus_origin_changed(d->bus))
138 /* Please adjust test/units/end.sh when updating the log message. */
139 sym_pam_syslog(pamh, LOG_DEBUG,
140 "Warning: cannot close sd-bus connection (%s) after fork when it was opened before the fork.",
141 strna(d->cache_id));
142
143 pam_bus_data_free(data);
144 }
145
146 static char* pam_make_bus_cache_id(const char *module_name) {
147 char *id;
148
149 /* We want to cache bus connections between hooks. But we don't want to allow them to be reused in
150 * child processes (because sd-bus doesn't support that). We also don't want them to be reused
151 * between our own PAM modules, because they might be linked against different versions of our
152 * utility functions and share different state. Hence include both a module ID and a PID in the data
153 * field ID. */
154
155 if (asprintf(&id, "system-bus-%s-" PID_FMT, ASSERT_PTR(module_name), getpid_cached()) < 0)
156 return NULL;
157
158 return id;
159 }
160
161 void pam_bus_data_disconnectp(PamBusData **_d) {
162 PamBusData *d = *ASSERT_PTR(_d);
163 pam_handle_t *pamh;
164 int r;
165
166 /* Disconnects the connection explicitly (for use via _cleanup_()) when called */
167
168 if (!d)
169 return;
170
171 pamh = ASSERT_PTR(d->pam_handle); /* Keep a reference to the session even after 'd' might be invalidated */
172
173 r = sym_pam_set_data(pamh, ASSERT_PTR(d->cache_id), NULL, NULL);
174 if (r != PAM_SUCCESS)
175 pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to release PAM user record data, ignoring: @PAMERR@");
176
177 /* Note, the pam_set_data() call will invalidate 'd', don't access here anymore */
178 }
179
180 int pam_acquire_bus_connection(
181 pam_handle_t *pamh,
182 const char *module_name,
183 bool debug,
184 sd_bus **ret_bus,
185 PamBusData **ret_pam_bus_data) {
186
187 _cleanup_(pam_bus_data_freep) PamBusData *d = NULL;
188 _cleanup_free_ char *cache_id = NULL;
189 int r;
190
191 assert(pamh);
192 assert(module_name);
193 assert(ret_bus);
194
195 cache_id = pam_make_bus_cache_id(module_name);
196 if (!cache_id)
197 return pam_log_oom(pamh);
198
199 /* We cache the bus connection so that we can share it between the session and the authentication hooks */
200 r = sym_pam_get_data(pamh, cache_id, (const void**) &d);
201 if (r == PAM_SUCCESS && d)
202 goto success;
203 if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA))
204 return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get bus connection: @PAMERR@");
205
206 d = new(PamBusData, 1);
207 if (!d)
208 return pam_log_oom(pamh);
209
210 *d = (PamBusData) {
211 .cache_id = TAKE_PTR(cache_id),
212 .pam_handle = pamh,
213 };
214
215 r = sd_bus_open_system(&d->bus);
216 if (r < 0)
217 return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to connect to system bus: %m");
218
219 r = sym_pam_set_data(pamh, d->cache_id, d, pam_bus_data_destroy);
220 if (r != PAM_SUCCESS)
221 return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to set PAM bus data: @PAMERR@");
222
223 pam_debug_syslog(pamh, debug, "New sd-bus connection (%s) opened.", d->cache_id);
224
225 success:
226 *ret_bus = sd_bus_ref(d->bus);
227
228 if (ret_pam_bus_data)
229 *ret_pam_bus_data = d;
230
231 TAKE_PTR(d); /* don't auto-destroy anymore, it's installed now */
232
233 return PAM_SUCCESS;
234 }
235
236 int pam_get_bus_data(pam_handle_t *pamh, const char *module_name, PamBusData **ret) {
237 PamBusData *d = NULL;
238 _cleanup_free_ char *cache_id = NULL;
239 int r;
240
241 assert(pamh);
242 assert(module_name);
243 assert(ret);
244
245 cache_id = pam_make_bus_cache_id(module_name);
246 if (!cache_id)
247 return pam_log_oom(pamh);
248
249 /* We cache the bus connection so that we can share it between the session and the authentication hooks */
250 r = sym_pam_get_data(pamh, cache_id, (const void**) &d);
251 if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA))
252 return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get bus connection: @PAMERR@");
253
254 *ret = d;
255 return PAM_SUCCESS;
256 }
257
258 void pam_cleanup_free(pam_handle_t *pamh, void *data, int error_status) {
259 /* A generic destructor for pam_set_data() that just frees the specified data */
260 free(data);
261 }
262
263 void pam_cleanup_close(pam_handle_t *pamh, void *data, int error_status) {
264
265 /* A generic destructor for pam_set_data() that just closes the specified fd.
266 *
267 * As per pam_set_data() docs: the PAM_DATA_SILENT indicates whether we are called in the forked off
268 * payload child of the new session. However, all file descriptors are most likely already closed
269 * there (that's what /bin/login does after all), hence let's simply turn this into a NOP in the
270 * child, and only close the fd in the parent. */
271 if (FLAGS_SET(error_status, PAM_DATA_SILENT))
272 return;
273
274 safe_close(PTR_TO_FD(data));
275 }
276
277 int pam_get_item_many_internal(pam_handle_t *pamh, ...) {
278 va_list ap;
279 int r;
280
281 assert(pamh);
282
283 va_start(ap, pamh);
284 for (;;) {
285 int item_type = va_arg(ap, int);
286 if (item_type <= 0) {
287 r = PAM_SUCCESS;
288 break;
289 }
290
291 const void **value = ASSERT_PTR(va_arg(ap, const void **));
292 r = sym_pam_get_item(pamh, item_type, value);
293 if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS))
294 break;
295 }
296 va_end(ap);
297
298 return r;
299 }
300
301 int pam_get_data_many_internal(pam_handle_t *pamh, ...) {
302 va_list ap;
303 int r;
304
305 assert(pamh);
306
307 va_start(ap, pamh);
308 for (;;) {
309 const char *data_name = va_arg(ap, const char *);
310 if (!data_name) {
311 r = PAM_SUCCESS;
312 break;
313 }
314
315 const void **value = ASSERT_PTR(va_arg(ap, const void **));
316 r = sym_pam_get_data(pamh, data_name, value);
317 if (!IN_SET(r, PAM_NO_MODULE_DATA, PAM_SUCCESS))
318 break;
319 }
320 va_end(ap);
321
322 return r;
323 }
324
325 int pam_prompt_graceful(pam_handle_t *pamh, int style, char **ret_response, const char *fmt, ...) {
326 va_list args;
327 int r;
328
329 assert(pamh);
330 assert(fmt);
331
332 /* This is just like pam_prompt(), but does not noisily (i.e. beyond LOG_DEBUG) log on its own, but leaves that to the caller */
333
334 _cleanup_free_ char *msg = NULL;
335 va_start(args, fmt);
336 r = vasprintf(&msg, fmt, args);
337 va_end(args);
338 if (r < 0)
339 return PAM_BUF_ERR;
340
341 const struct pam_conv *conv = NULL;
342 r = sym_pam_get_item(pamh, PAM_CONV, (const void**) &conv);
343 if (!IN_SET(r, PAM_SUCCESS, PAM_BAD_ITEM))
344 return pam_syslog_pam_error(pamh, LOG_DEBUG, r, "Failed to get conversation function structure: @PAMERR@");
345 if (!conv || !conv->conv) {
346 sym_pam_syslog(pamh, LOG_DEBUG, "No conversation function.");
347 return PAM_SYSTEM_ERR;
348 }
349
350 struct pam_message message = {
351 .msg_style = style,
352 .msg = msg,
353 };
354 const struct pam_message *pmessage = &message;
355 _cleanup_free_ struct pam_response *response = NULL;
356 r = conv->conv(1, &pmessage, &response, conv->appdata_ptr);
357 _cleanup_(erase_and_freep) char *rr = response ? response->resp : NULL; /* make sure string is freed + erased */
358 if (r != PAM_SUCCESS)
359 return pam_syslog_pam_error(pamh, LOG_DEBUG, r, "Conversation function failed: @PAMERR@");
360
361 if (ret_response)
362 *ret_response = TAKE_PTR(rr);
363
364 return PAM_SUCCESS;
365 }
366
367 int pam_putenv_assign(pam_handle_t *pamh, const char *name, const char *value) {
368 _cleanup_(erase_and_freep) char *s = NULL;
369
370 assert(pamh);
371 assert(name);
372 assert(value);
373
374 if (asprintf(&s, "%s=%s", name, value) < 0)
375 return PAM_BUF_ERR;
376
377 return sym_pam_putenv(pamh, s);
378 }
379
380 #endif
381
382 int dlopen_libpam(int log_level) {
383 #if HAVE_PAM
384 static void *libpam_dl = NULL;
385
386 LIBPAM_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
387
388 return dlopen_many_sym_or_warn(
389 &libpam_dl,
390 "libpam.so.0",
391 log_level,
392 DLSYM_ARG(pam_acct_mgmt),
393 DLSYM_ARG(pam_close_session),
394 DLSYM_ARG(pam_end),
395 DLSYM_ARG(pam_get_authtok_noverify),
396 DLSYM_ARG(pam_get_authtok_verify),
397 DLSYM_ARG(pam_get_data),
398 DLSYM_ARG(pam_get_item),
399 DLSYM_ARG(pam_get_user),
400 DLSYM_ARG(pam_getenv),
401 DLSYM_ARG(pam_getenvlist),
402 DLSYM_ARG(pam_open_session),
403 DLSYM_ARG(pam_prompt),
404 DLSYM_ARG(pam_putenv),
405 DLSYM_ARG(pam_set_data),
406 DLSYM_ARG(pam_set_item),
407 DLSYM_ARG(pam_setcred),
408 DLSYM_ARG(pam_start),
409 DLSYM_ARG(pam_strerror),
410 DLSYM_ARG(pam_syslog),
411 DLSYM_ARG(pam_vsyslog));
412 #else
413 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
414 "libpam support is not compiled in.");
415 #endif
416 }