3 * If you want to dynamically load libpam using dlopen() or something,
4 * then dlopen( ' this shared object ' ); It takes care of exporting
5 * the right symbols to any modules loaded by libpam.
7 * Modified by JY for use with openvpn-pam-auth
12 #include <security/pam_appl.h>
13 #include <security/_pam_macros.h>
17 static void *libpam_h
= NULL
;
19 #define RESOLVE_PAM_FUNCTION(x, y, z, err) \
21 union { const void *tpointer; y (*fn) z ; } fptr; \
22 fptr.tpointer = dlsym(libpam_h, #x); real_##x = fptr.fn; \
23 if (real_##x == NULL) { \
24 fprintf (stderr, "PAMDL: unable to resolve '%s': %s\n", #x, dlerror()); \
30 dlopen_pam (const char *so
)
34 libpam_h
= dlopen(so
, RTLD_GLOBAL
|RTLD_NOW
);
36 return libpam_h
!= NULL
;
49 int pam_start(const char *service_name
, const char *user
,
50 const struct pam_conv
*pam_conversation
,
53 int (*real_pam_start
)(const char *, const char *,
54 const struct pam_conv
*,
56 RESOLVE_PAM_FUNCTION(pam_start
, int, (const char *, const char *,
57 const struct pam_conv
*,
58 pam_handle_t
**), PAM_ABORT
);
59 return real_pam_start(service_name
, user
, pam_conversation
, pamh
);
62 int pam_end(pam_handle_t
*pamh
, int pam_status
)
64 int (*real_pam_end
)(pam_handle_t
*, int);
65 RESOLVE_PAM_FUNCTION(pam_end
, int, (pam_handle_t
*, int), PAM_ABORT
);
66 return real_pam_end(pamh
, pam_status
);
69 int pam_set_item(pam_handle_t
*pamh
, int item_type
, const void *item
)
71 int (*real_pam_set_item
)(pam_handle_t
*, int, const void *);
72 RESOLVE_PAM_FUNCTION(pam_set_item
, int,
73 (pam_handle_t
*, int, const void *), PAM_ABORT
);
74 return real_pam_set_item(pamh
, item_type
, item
);
77 int pam_get_item(const pam_handle_t
*pamh
, int item_type
, const void **item
)
79 int (*real_pam_get_item
)(const pam_handle_t
*, int, const void **);
80 RESOLVE_PAM_FUNCTION(pam_get_item
, int,
81 (const pam_handle_t
*, int, const void **),
83 return real_pam_get_item(pamh
, item_type
, item
);
86 int pam_fail_delay(pam_handle_t
*pamh
, unsigned int musec_delay
)
88 int (*real_pam_fail_delay
)(pam_handle_t
*, unsigned int);
89 RESOLVE_PAM_FUNCTION(pam_fail_delay
, int, (pam_handle_t
*, unsigned int),
91 return real_pam_fail_delay(pamh
, musec_delay
);
94 typedef const char * const_char_pointer
;
96 const_char_pointer
pam_strerror(pam_handle_t
*pamh
, int errnum
)
98 const_char_pointer (*real_pam_strerror
)(pam_handle_t
*, int);
99 RESOLVE_PAM_FUNCTION(pam_strerror
, const_char_pointer
,
100 (pam_handle_t
*, int), NULL
);
101 return real_pam_strerror(pamh
, errnum
);
104 int pam_putenv(pam_handle_t
*pamh
, const char *name_value
)
106 int (*real_pam_putenv
)(pam_handle_t
*, const char *);
107 RESOLVE_PAM_FUNCTION(pam_putenv
, int, (pam_handle_t
*, const char *),
109 return real_pam_putenv(pamh
, name_value
);
112 const_char_pointer
pam_getenv(pam_handle_t
*pamh
, const char *name
)
114 const_char_pointer (*real_pam_getenv
)(pam_handle_t
*, const char *);
115 RESOLVE_PAM_FUNCTION(pam_getenv
, const_char_pointer
,
116 (pam_handle_t
*, const char *), NULL
);
117 return real_pam_getenv(pamh
, name
);
120 typedef char ** char_ppointer
;
121 char_ppointer
pam_getenvlist(pam_handle_t
*pamh
)
123 char_ppointer (*real_pam_getenvlist
)(pam_handle_t
*);
124 RESOLVE_PAM_FUNCTION(pam_getenvlist
, char_ppointer
, (pam_handle_t
*),
126 return real_pam_getenvlist(pamh
);
129 /* Authentication management */
131 int pam_authenticate(pam_handle_t
*pamh
, int flags
)
133 int (*real_pam_authenticate
)(pam_handle_t
*, int);
134 RESOLVE_PAM_FUNCTION(pam_authenticate
, int, (pam_handle_t
*, int),
136 return real_pam_authenticate(pamh
, flags
);
139 int pam_setcred(pam_handle_t
*pamh
, int flags
)
141 int (*real_pam_setcred
)(pam_handle_t
*, int);
142 RESOLVE_PAM_FUNCTION(pam_setcred
, int, (pam_handle_t
*, int), PAM_ABORT
);
143 return real_pam_setcred(pamh
, flags
);
146 /* Account Management API's */
148 int pam_acct_mgmt(pam_handle_t
*pamh
, int flags
)
150 int (*real_pam_acct_mgmt
)(pam_handle_t
*, int);
151 RESOLVE_PAM_FUNCTION(pam_acct_mgmt
, int, (pam_handle_t
*, int), PAM_ABORT
);
152 return real_pam_acct_mgmt(pamh
, flags
);
155 /* Session Management API's */
157 int pam_open_session(pam_handle_t
*pamh
, int flags
)
159 int (*real_pam_open_session
)(pam_handle_t
*, int);
160 RESOLVE_PAM_FUNCTION(pam_open_session
, int, (pam_handle_t
*, int),
162 return real_pam_open_session(pamh
, flags
);
165 int pam_close_session(pam_handle_t
*pamh
, int flags
)
167 int (*real_pam_close_session
)(pam_handle_t
*, int);
168 RESOLVE_PAM_FUNCTION(pam_close_session
, int, (pam_handle_t
*, int),
170 return real_pam_close_session(pamh
, flags
);
173 /* Password Management API's */
175 int pam_chauthtok(pam_handle_t
*pamh
, int flags
)
177 int (*real_pam_chauthtok
)(pam_handle_t
*, int);
178 RESOLVE_PAM_FUNCTION(pam_chauthtok
, int, (pam_handle_t
*, int), PAM_ABORT
);
179 return real_pam_chauthtok(pamh
, flags
);