2023-10-03 14:37:28 +02:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
2015-03-27 18:59:29 -05:00
|
|
|
|
#include <security/pam_appl.h>
|
2023-10-03 14:37:28 +02:00
|
|
|
|
#include <security/pam_modules.h>
|
2023-09-19 20:04:06 +02:00
|
|
|
|
#include <stdint.h>
|
2023-11-20 21:06:58 +01:00
|
|
|
|
#include <stdlib.h>
|
2015-03-31 12:19:55 -05:00
|
|
|
|
#include <string.h>
|
2015-03-27 18:59:29 -05:00
|
|
|
|
|
2023-11-20 21:06:58 +01:00
|
|
|
|
#ifdef PAM_BINARY_PROMPT
|
|
|
|
|
|
#define BINARY_PROMPT_IS_SUPPORTED 1
|
|
|
|
|
|
#else
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
#define PAM_BINARY_PROMPT INT_MAX
|
|
|
|
|
|
#define BINARY_PROMPT_IS_SUPPORTED 0
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2020-08-07 17:34:46 +01:00
|
|
|
|
#ifdef __sun
|
|
|
|
|
|
#define PAM_CONST
|
|
|
|
|
|
#else
|
|
|
|
|
|
#define PAM_CONST const
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2023-11-20 21:06:58 +01:00
|
|
|
|
extern int _go_pam_conv_handler(struct pam_message *, uintptr_t, char **reply);
|
2023-10-03 14:37:28 +02:00
|
|
|
|
extern void _go_pam_data_cleanup(pam_handle_t *, uintptr_t, int status);
|
2023-11-20 21:06:58 +01:00
|
|
|
|
|
|
|
|
|
|
static inline int cb_pam_conv(int num_msg, PAM_CONST struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
|
2015-03-27 18:59:29 -05:00
|
|
|
|
{
|
2023-09-21 08:11:19 -05:00
|
|
|
|
if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG)
|
2015-03-31 12:19:55 -05:00
|
|
|
|
return PAM_CONV_ERR;
|
2023-09-21 08:11:19 -05:00
|
|
|
|
|
2023-09-21 08:12:18 -05:00
|
|
|
|
*resp = calloc(num_msg, sizeof **resp);
|
2023-09-21 08:11:19 -05:00
|
|
|
|
if (!*resp)
|
2015-03-27 18:59:29 -05:00
|
|
|
|
return PAM_BUF_ERR;
|
2023-09-21 08:11:19 -05:00
|
|
|
|
|
2015-03-27 18:59:29 -05:00
|
|
|
|
for (size_t i = 0; i < num_msg; ++i) {
|
2023-11-20 21:06:58 +01:00
|
|
|
|
int result = _go_pam_conv_handler((struct pam_message *)msg[i], (uintptr_t)appdata_ptr, &(*resp)[i].resp);
|
|
|
|
|
|
if (result != PAM_SUCCESS)
|
2015-03-27 18:59:29 -05:00
|
|
|
|
goto error;
|
|
|
|
|
|
}
|
2023-09-21 08:11:19 -05:00
|
|
|
|
|
2015-03-27 18:59:29 -05:00
|
|
|
|
return PAM_SUCCESS;
|
|
|
|
|
|
error:
|
|
|
|
|
|
for (size_t i = 0; i < num_msg; ++i) {
|
2015-03-31 12:19:55 -05:00
|
|
|
|
if ((*resp)[i].resp) {
|
2023-10-05 05:49:22 +02:00
|
|
|
|
#ifdef PAM_BINARY_PROMPT
|
|
|
|
|
|
if (msg[i]->msg_style != PAM_BINARY_PROMPT)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
memset((*resp)[i].resp, 0, strlen((*resp)[i].resp));
|
2015-03-31 12:19:55 -05:00
|
|
|
|
free((*resp)[i].resp);
|
|
|
|
|
|
}
|
2015-03-27 18:59:29 -05:00
|
|
|
|
}
|
2023-09-21 08:11:19 -05:00
|
|
|
|
|
2015-03-31 12:19:55 -05:00
|
|
|
|
memset(*resp, 0, num_msg * sizeof *resp);
|
2015-03-27 18:59:29 -05:00
|
|
|
|
free(*resp);
|
|
|
|
|
|
*resp = NULL;
|
|
|
|
|
|
return PAM_CONV_ERR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-20 21:06:58 +01:00
|
|
|
|
static inline void init_pam_conv(struct pam_conv *conv, uintptr_t appdata)
|
2015-03-27 18:59:29 -05:00
|
|
|
|
{
|
|
|
|
|
|
conv->conv = cb_pam_conv;
|
2023-09-19 20:04:06 +02:00
|
|
|
|
conv->appdata_ptr = (void *)appdata;
|
2015-03-27 18:59:29 -05:00
|
|
|
|
}
|
2022-09-16 08:09:26 +02:00
|
|
|
|
|
2023-10-04 23:34:20 +02:00
|
|
|
|
static inline int start_pam_conv(struct pam_conv *pc, int num_msgs, const struct pam_message **msgs, struct pam_response **out_resp)
|
|
|
|
|
|
{
|
|
|
|
|
|
return pc->conv(num_msgs, msgs, out_resp, pc->appdata_ptr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-21 08:11:19 -05:00
|
|
|
|
// pam_start_confdir is a recent PAM api to declare a confdir (mostly for
|
|
|
|
|
|
// tests) weaken the linking dependency to detect if it’s present.
|
|
|
|
|
|
int pam_start_confdir(const char *service_name, const char *user, const struct pam_conv *pam_conversation,
|
|
|
|
|
|
const char *confdir, pam_handle_t **pamh) __attribute__((weak));
|
|
|
|
|
|
|
2023-11-20 21:06:58 +01:00
|
|
|
|
static inline int check_pam_start_confdir(void)
|
2023-09-21 08:11:19 -05:00
|
|
|
|
{
|
2022-09-16 08:09:26 +02:00
|
|
|
|
if (pam_start_confdir == NULL)
|
|
|
|
|
|
return 1;
|
2023-09-21 08:11:19 -05:00
|
|
|
|
|
2022-09-16 08:09:26 +02:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2023-10-03 14:37:28 +02:00
|
|
|
|
|
|
|
|
|
|
static inline void data_cleanup(pam_handle_t *pamh, void *data, int error_status)
|
|
|
|
|
|
{
|
|
|
|
|
|
_go_pam_data_cleanup(pamh, (uintptr_t)data, error_status);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int set_data(pam_handle_t *pamh, const char *name, uintptr_t handle)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (handle)
|
|
|
|
|
|
return pam_set_data(pamh, name, (void *)handle, data_cleanup);
|
|
|
|
|
|
|
|
|
|
|
|
return pam_set_data(pamh, name, NULL, NULL);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int get_data(pam_handle_t *pamh, const char *name, uintptr_t *out_handle)
|
|
|
|
|
|
{
|
|
|
|
|
|
return pam_get_data(pamh, name, (const void **)out_handle);
|
|
|
|
|
|
}
|