32#include <security/pam_appl.h>
45#include <sys/socket.h>
55#define DEBUG(verb) ((verb) >= 4)
58#define COMMAND_VERIFY 0
62#define RESPONSE_INIT_SUCCEEDED 10
63#define RESPONSE_INIT_FAILED 11
64#define RESPONSE_VERIFY_SUCCEEDED 12
65#define RESPONSE_VERIFY_FAILED 13
66#define RESPONSE_DEFER 14
101#define N_NAME_VALUE 16
146 if (size ==
sizeof(
c))
162 if (size ==
sizeof(
c))
188#if defined(__GNUC__) || defined(__clang__)
189#pragma GCC diagnostic push
190#pragma GCC diagnostic ignored "-Wsign-conversion"
208#if defined(__GNUC__) || defined(__clang__)
209#pragma GCC diagnostic pop
231#if defined(__APPLE__) && defined(__clang__)
232#pragma clang diagnostic push
233#pragma clang diagnostic ignored "-Wdeprecated-declarations"
239#if defined(__APPLE__) && defined(__clang__)
240#pragma clang diagnostic pop
267 for (
i = 3;
i <= 100; ++
i)
382 const char **envp =
args->envp;
388 "AUTH-PAM: This plugin is incompatible with the running version of OpenVPN\n");
441 const int base = base_parms + i * 2;
451 const char *verb_string =
get_env(
"verb", envp);
454 context->verb = atoi(verb_string);
462 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) == -1)
488 if (fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0)
491 "Set FD_CLOEXEC flag on socket file descriptor failed");
498 context->foreground_fd = fd[0];
544 const char *username =
get_env(
"username", envp);
545 const char *password =
get_env(
"password", envp);
546 const char *common_name =
get_env(
"common_name", envp) ?
get_env(
"common_name", envp) :
"";
547 const char *remote =
get_env(
"untrusted_ip6", envp);
551 remote =
get_env(
"untrusted_ip", envp);
562 const char *auth_control_file =
get_env(
"auth_control_file", envp);
563 const char *deferred_auth_pam =
get_env(
"deferred_auth_pam", envp);
564 if (auth_control_file != NULL && deferred_auth_pam != NULL)
573 auth_control_file =
"";
576 if (username && strlen(username) > 0 && password)
586 "Error sending auth info to background process");
606 "Error receiving auth confirmation from background process");
624 if (
context->foreground_fd >= 0)
633 if (
context->background_pid > 0)
635 waitpid(
context->background_pid, NULL, 0);
663my_conv(
int num_msg,
const struct pam_message **msg_array,
struct pam_response **response_array,
667 struct pam_response *aresp;
668 int ret = PAM_SUCCESS;
670 *response_array = NULL;
672 if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG)
674 return (PAM_CONV_ERR);
676 if ((aresp = calloc((
size_t)num_msg,
sizeof *aresp)) == NULL)
678 return (PAM_BUF_ERR);
682 for (
int i = 0; i < num_msg; ++i)
684 const struct pam_message *
msg = msg_array[i];
685 aresp[i].resp_retcode = 0;
686 aresp[i].resp = NULL;
691 msg->msg ?
msg->msg :
"NULL",
msg->msg_style);
701 for (
j = 0;
j < list->
len; ++
j)
715 "BACKGROUND: name match found, query/match-string ['%s', '%s'] = '%s'",
757 switch (
msg->msg_style)
814 struct pam_conv conv;
815 pam_handle_t *pamh = NULL;
822 conv.appdata_ptr = (
void *)up;
824 if (
status == PAM_SUCCESS)
832 if (
status == PAM_SUCCESS)
834 status = pam_authenticate(pamh, 0);
836 if (
status == PAM_SUCCESS)
838 status = pam_acct_mgmt(pamh, 0);
840 if (
status == PAM_SUCCESS)
889 waitpid(p1, NULL, 0);
913 int ac_fd = open(ac_file_name, O_WRONLY);
921 if (
write(ac_fd, pam_success ?
"1" :
"0", 1) != 1)
927 pam_success ?
"succeeded" :
"rejected");
938 char ac_file_name[PATH_MAX];
941 static const char pam_so[] =
"libpam.so";
956 if (!dlopen_pam(pam_so))
981 memset(&up, 0,
sizeof(up));
999 ||
recv_string(fd, ac_file_name,
sizeof(ac_file_name)) == -1
1003 "BACKGROUND: read error on command channel: code=%d, exiting",
1024 if (strlen(ac_file_name) > 0)
1039 "BACKGROUND: write error on response socket [2]");
1048 "BACKGROUND: write error on response socket [3]");
1060 "BACKGROUND: read error on command channel");
1073#ifdef USE_PAM_DLOPEN
OPENVPN_EXPORT int openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[])
This function is called by OpenVPN each time the OpenVPN reaches a point where plug-in calls should h...
static void set_signals(void)
OPENVPN_EXPORT void openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
This cleans up the last part of the plug-in, allows it to shut down cleanly and release the plug-in g...
static plugin_log_t plugin_log
static int pam_auth(const char *service, const struct user_pass *up)
static int name_value_match(const char *query, const char *match)
#define RESPONSE_INIT_SUCCEEDED
static plugin_secure_memzero_t plugin_secure_memzero
static void do_deferred_pam_auth(int fd, const char *ac_file_name, const char *service, const struct user_pass *up)
#define RESPONSE_INIT_FAILED
static ssize_t send_string(int fd, const char *string)
static plugin_base64_decode_t plugin_base64_decode
static void pam_server(int fd, const char *service, int verb, const struct name_value_list *name_value_list)
#define RESPONSE_VERIFY_FAILED
OPENVPN_EXPORT void openvpn_plugin_abort_v1(openvpn_plugin_handle_t handle)
static int recv_control(int fd)
static int send_control(int fd, int code)
static void close_fds_except(int keep)
OPENVPN_EXPORT int openvpn_plugin_open_v3(const int v3structver, struct openvpn_plugin_args_open_in const *args, struct openvpn_plugin_args_open_return *ret)
This function is called when OpenVPN loads the plug-in.
#define RESPONSE_VERIFY_SUCCEEDED
static int my_conv(int num_msg, const struct pam_message **msg_array, struct pam_response **response_array, void *appdata_ptr)
static ssize_t recv_string(int fd, char *buffer, size_t len)
static void split_scrv1_password(struct user_pass *up)
int string_array_len(const char **array)
Return the number of elements in a NULL-terminated array of strings.
int daemon(int nochdir, int noclose)
static void daemonize(const char *envp[])
static SERVICE_STATUS status
static SERVICE_STATUS_HANDLE service
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
void * openvpn_plugin_handle_t
#define OPENVPN_PLUGIN_MASK(x)
void(* plugin_log_t)(openvpn_plugin_log_flags_t flags, const char *plugin_name, const char *format,...) _ovpn_chk_fmt(3
#define OPENVPN_PLUGIN_FUNC_DEFERRED
#define OPENVPN_PLUGIN_FUNC_SUCCESS
void(*) typedef void(*) typedef void(* plugin_secure_memzero_t)(void *data, size_t len)
Export of secure_memzero() to be used inside plug-ins.
#define OPENVPN_PLUGIN_FUNC_ERROR
int(* plugin_base64_decode_t)(const char *str, void *data, int size)
Export of openvpn_base64_decode() to be used inside plug-ins.
static const char * get_env(const char *name, const char *envp[])
Wrapper structure for dynamically allocated memory.
Contains all state information for one tunnel.
struct name_value data[N_NAME_VALUE]
Arguments used to transport variables to the plug-in.
struct openvpn_plugin_callbacks * callbacks
Arguments used to transport variables from the plug-in back to the OpenVPN process.
openvpn_plugin_handle_t handle
plugin_base64_decode_t plugin_base64_decode
plugin_secure_memzero_t plugin_secure_memzero
const struct name_value_list * name_value_list
char password[USER_PASS_LEN]
char remote[INET6_ADDRSTRLEN]
char username[USER_PASS_LEN]
char * searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
Read 'tosearch', replace all occurrences of 'searchfor' with 'replacewith' and return a pointer to th...