Debian Codegraph @

src/shared/pam-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "sd-dlopen.h"
5
6 #include "shared-forward.h"
7
8 #if HAVE_PAM
9 #include <security/pam_appl.h>
10 #include <security/pam_ext.h>
11 #include <security/pam_modules.h> /* IWYU pragma: export */
12 #include <syslog.h>
13
14 #include "dlfcn-util.h"
15
16 extern DLSYM_PROTOTYPE(pam_acct_mgmt);
17 extern DLSYM_PROTOTYPE(pam_close_session);
18 extern DLSYM_PROTOTYPE(pam_end);
19 extern DLSYM_PROTOTYPE(pam_get_authtok_noverify);
20 extern DLSYM_PROTOTYPE(pam_get_authtok_verify);
21 extern DLSYM_PROTOTYPE(pam_get_data);
22 extern DLSYM_PROTOTYPE(pam_get_item);
23 extern DLSYM_PROTOTYPE(pam_get_user);
24 extern DLSYM_PROTOTYPE(pam_getenv);
25 extern DLSYM_PROTOTYPE(pam_getenvlist);
26 extern DLSYM_PROTOTYPE(pam_open_session);
27 extern DLSYM_PROTOTYPE(pam_prompt);
28 extern DLSYM_PROTOTYPE(pam_putenv);
29 extern DLSYM_PROTOTYPE(pam_set_data);
30 extern DLSYM_PROTOTYPE(pam_set_item);
31 extern DLSYM_PROTOTYPE(pam_setcred);
32 extern DLSYM_PROTOTYPE(pam_start);
33 extern DLSYM_PROTOTYPE(pam_strerror);
34 extern DLSYM_PROTOTYPE(pam_syslog);
35 extern DLSYM_PROTOTYPE(pam_vsyslog);
36
37 /* sym_pam_prompt() replacement for the pam_info() macro from <security/pam_ext.h>, which expands to a direct
38 * pam_prompt() call. */
39 #define sym_pam_info(pamh, fmt, ...) \
40 sym_pam_prompt((pamh), PAM_TEXT_INFO, NULL, (fmt), ## __VA_ARGS__)
41
42 void pam_log_setup(void);
43
44 int errno_to_pam_error(int error) _const_;
45
46 int pam_syslog_errno(pam_handle_t *pamh, int level, int error, const char *format, ...) _printf_(4,5);
47
48 int pam_syslog_pam_error(pam_handle_t *pamh, int level, int error, const char *format, ...) _printf_(4,5);
49
50 /* Call sym_pam_syslog if debug is enabled */
51 #define pam_debug_syslog(handle, debug, fmt, ...) \
52 ({ \
53 if (debug) \
54 sym_pam_syslog(handle, LOG_DEBUG, fmt, ## __VA_ARGS__); \
55 })
56
57 /* Call pam_syslog_errno if debug is enabled */
58 #define pam_debug_syslog_errno(handle, debug, error, fmt, ...) \
59 ({ \
60 int _error = (error); \
61 debug ? \
62 pam_syslog_errno(handle, LOG_DEBUG, _error, fmt, ## __VA_ARGS__) : \
63 errno_to_pam_error(_error); \
64 })
65
66 static inline int pam_log_oom(pam_handle_t *pamh) {
67 /* This is like log_oom(), but uses PAM logging */
68 return pam_syslog_errno(pamh, LOG_ERR, ENOMEM, "Out of memory.");
69 }
70
71 static inline int pam_bus_log_create_error(pam_handle_t *pamh, int r) {
72 /* This is like bus_log_create_error(), but uses PAM logging */
73 return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to create bus message: %m");
74 }
75
76 static inline int pam_bus_log_parse_error(pam_handle_t *pamh, int r) {
77 /* This is like bus_log_parse_error(), but uses PAM logging */
78 return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to parse bus message: %m");
79 }
80
81 typedef struct PamBusData PamBusData;
82 void pam_bus_data_disconnectp(PamBusData **d);
83
84 /* Use a different module name per different PAM module. They are all loaded in the same namespace, and this
85 * helps avoid a clash in the internal data structures of sd-bus. It will be used as key for cache items. */
86 int pam_acquire_bus_connection(
87 pam_handle_t *pamh,
88 const char *module_name,
89 bool debug,
90 sd_bus **ret_bus,
91 PamBusData **ret_pam_bus_data);
92 int pam_get_bus_data(pam_handle_t *pamh, const char *module_name, PamBusData **ret);
93
94 void pam_cleanup_free(pam_handle_t *pamh, void *data, int error_status);
95 void pam_cleanup_close(pam_handle_t *pamh, void *data, int error_status);
96
97 int pam_get_item_many_internal(pam_handle_t *pamh, ...);
98 #define pam_get_item_many(handle, ...) pam_get_item_many_internal(handle, __VA_ARGS__, -1)
99
100 int pam_get_data_many_internal(pam_handle_t *pamh, ...) _sentinel_;
101 #define pam_get_data_many(handle, ...) pam_get_data_many_internal(handle, __VA_ARGS__, NULL)
102
103 int pam_prompt_graceful(pam_handle_t *pamh, int style, char **ret_response, const char *fmt, ...) _printf_(4,5);
104
105 /* Equivalent of pam_misc_setenv(pamh, name, value, 0), without the libpam_misc dep — builds "name=value"
106 * and hands it to sym_pam_putenv(), then erases the buffer before freeing in case it carried a secret. */
107 int pam_putenv_assign(pam_handle_t *pamh, const char *name, const char *value);
108
109 #define LIBPAM_NOTE(priority) \
110 SD_ELF_NOTE_DLOPEN("pam", \
111 "Support for LinuxPAM", \
112 priority, \
113 "libpam.so.0")
114
115 #define DLOPEN_LIBPAM(log_level, priority) \
116 ({ \
117 LIBPAM_NOTE(priority); \
118 dlopen_libpam(log_level); \
119 })
120 #else
121 #define DLOPEN_LIBPAM(log_level, priority) dlopen_libpam(log_level)
122 #endif
123
124 int dlopen_libpam(int log_level);