26b1305
/*
26b1305
 *  OpenVPN -- An application to securely tunnel IP networks
26b1305
 *             over a single TCP/UDP port, with support for SSL/TLS-based
26b1305
 *             session authentication and key exchange,
26b1305
 *             packet encryption, packet authentication, and
26b1305
 *             packet compression.
26b1305
 *
26b1305
 *  Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
26b1305
 *
26b1305
 *  This program is free software; you can redistribute it and/or modify
26b1305
 *  it under the terms of the GNU General Public License version 2
26b1305
 *  as published by the Free Software Foundation.
26b1305
 *
26b1305
 *  This program is distributed in the hope that it will be useful,
26b1305
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26b1305
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26b1305
 *  GNU General Public License for more details.
26b1305
 *
26b1305
 *  You should have received a copy of the GNU General Public License
26b1305
 *  along with this program (see the file COPYING included with this
26b1305
 *  distribution); if not, write to the Free Software Foundation, Inc.,
26b1305
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26b1305
 */
26b1305
26b1305
#define OPENVPN_PLUGIN_VERSION 2
26b1305
26b1305
/*
26b1305
 * Plug-in types.  These types correspond to the set of script callbacks
26b1305
 * supported by OpenVPN.
26b1305
 */
26b1305
#define OPENVPN_PLUGIN_UP                    0
26b1305
#define OPENVPN_PLUGIN_DOWN                  1
26b1305
#define OPENVPN_PLUGIN_ROUTE_UP              2
26b1305
#define OPENVPN_PLUGIN_IPCHANGE              3
26b1305
#define OPENVPN_PLUGIN_TLS_VERIFY            4
26b1305
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY 5
26b1305
#define OPENVPN_PLUGIN_CLIENT_CONNECT        6
26b1305
#define OPENVPN_PLUGIN_CLIENT_DISCONNECT     7
26b1305
#define OPENVPN_PLUGIN_LEARN_ADDRESS         8
26b1305
#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2     9
26b1305
#define OPENVPN_PLUGIN_TLS_FINAL             10
26b1305
#define OPENVPN_PLUGIN_N                     11
26b1305
26b1305
/*
26b1305
 * Build a mask out of a set of plug-in types.
26b1305
 */
26b1305
#define OPENVPN_PLUGIN_MASK(x) (1<<(x))
26b1305
26b1305
/*
26b1305
 * A pointer to a plugin-defined object which contains
26b1305
 * the object state.
26b1305
 */
26b1305
typedef void *openvpn_plugin_handle_t;
26b1305
26b1305
/*
26b1305
 * Return value for openvpn_plugin_func_v1 function
26b1305
 */
26b1305
#define OPENVPN_PLUGIN_FUNC_SUCCESS  0
26b1305
#define OPENVPN_PLUGIN_FUNC_ERROR    1
26b1305
26b1305
/*
26b1305
 * For Windows (needs to be modified for MSVC)
26b1305
 */
26b1305
#if defined(__MINGW32_VERSION) && !defined(OPENVPN_PLUGIN_H)
26b1305
# define OPENVPN_EXPORT __declspec(dllexport)
26b1305
#else
26b1305
# define OPENVPN_EXPORT
26b1305
#endif
26b1305
26b1305
/*
26b1305
 * If OPENVPN_PLUGIN_H is defined, we know that we are being
26b1305
 * included in an OpenVPN compile, rather than a plugin compile.
26b1305
 */
26b1305
#ifdef OPENVPN_PLUGIN_H
26b1305
26b1305
/*
26b1305
 * We are compiling OpenVPN.
26b1305
 */
26b1305
#define OPENVPN_PLUGIN_DEF        typedef
26b1305
#define OPENVPN_PLUGIN_FUNC(name) (*name)
26b1305
26b1305
#else
26b1305
26b1305
/*
26b1305
 * We are compiling plugin.
26b1305
 */
26b1305
#define OPENVPN_PLUGIN_DEF        OPENVPN_EXPORT
26b1305
#define OPENVPN_PLUGIN_FUNC(name) name
26b1305
26b1305
#endif
26b1305
26b1305
/*
26b1305
 * Used by openvpn_plugin_func to return structured
26b1305
 * data.  The plugin should allocate all structure
26b1305
 * instances, name strings, and value strings with
26b1305
 * malloc, since OpenVPN will assume that it
26b1305
 * can free the list by calling free() over the same.
26b1305
 */
26b1305
struct openvpn_plugin_string_list
26b1305
{
26b1305
  struct openvpn_plugin_string_list *next;
26b1305
  char *name;
26b1305
  char *value;
26b1305
};
26b1305
26b1305
/*
26b1305
 * Multiple plugin modules can be cascaded, and modules can be
26b1305
 * used in tandem with scripts.  The order of operation is that
26b1305
 * the module func() functions are called in the order that
26b1305
 * the modules were specified in the config file.  If a script
26b1305
 * was specified as well, it will be called last.  If the
26b1305
 * return code of the module/script controls an authentication
26b1305
 * function (such as tls-verify or auth-user-pass-verify), then
26b1305
 * every module and script must return success (0) in order for
26b1305
 * the connection to be authenticated.
26b1305
 *
26b1305
 * Notes:
26b1305
 *
26b1305
 * Plugins which use a privilege-separation model (by forking in
26b1305
 * their initialization function before the main OpenVPN process
26b1305
 * downgrades root privileges and/or executes a chroot) must
26b1305
 * daemonize after a fork if the "daemon" environmental variable is
26b1305
 * set.  In addition, if the "daemon_log_redirect" variable is set,
26b1305
 * the plugin should preserve stdout/stderr across the daemon()
26b1305
 * syscall.  See the daemonize() function in plugin/auth-pam/auth-pam.c
26b1305
 * for an example.
26b1305
 */
26b1305
26b1305
/*
26b1305
 * Prototypes for functions which OpenVPN plug-ins must define.
26b1305
 */
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_open_v2
26b1305
 *
26b1305
 * REQUIRED: YES
26b1305
 * 
26b1305
 * Called on initial plug-in load.  OpenVPN will preserve plug-in state
26b1305
 * across SIGUSR1 restarts but not across SIGHUP restarts.  A SIGHUP reset
26b1305
 * will cause the plugin to be closed and reopened.
26b1305
 *
26b1305
 * ARGUMENTS
26b1305
 *
26b1305
 * *type_mask : Set by OpenVPN to the logical OR of all script
26b1305
 *              types which this version of OpenVPN supports.  The plug-in
26b1305
 *              should set this value to the logical OR of all script types
26b1305
 *              which the plug-in wants to intercept.  For example, if the
26b1305
 *              script wants to intercept the client-connect and
26b1305
 *              client-disconnect script types:
26b1305
 *
26b1305
 *              *type_mask = OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_CONNECT)
26b1305
 *                         | OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_CLIENT_DISCONNECT)
26b1305
 *
26b1305
 * argv : a NULL-terminated array of options provided to the OpenVPN
26b1305
 *        "plug-in" directive.  argv[0] is the dynamic library pathname.
26b1305
 *
26b1305
 * envp : a NULL-terminated array of OpenVPN-set environmental
26b1305
 *        variables in "name=value" format.  Note that for security reasons,
26b1305
 *        these variables are not actually written to the "official"
26b1305
 *        environmental variable store of the process.
26b1305
 *
26b1305
 * return_list : used to return data back to OpenVPN.
26b1305
 *
26b1305
 * RETURN VALUE
26b1305
 *
26b1305
 * An openvpn_plugin_handle_t value on success, NULL on failure
26b1305
 */
26b1305
OPENVPN_PLUGIN_DEF openvpn_plugin_handle_t OPENVPN_PLUGIN_FUNC(openvpn_plugin_open_v2)
26b1305
     (unsigned int *type_mask,
26b1305
      const char *argv[],
26b1305
      const char *envp[],
26b1305
      struct openvpn_plugin_string_list **return_list);
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_func_v2
26b1305
 *
26b1305
 * Called to perform the work of a given script type.
26b1305
 *
26b1305
 * REQUIRED: YES
26b1305
 * 
26b1305
 * ARGUMENTS
26b1305
 *
26b1305
 * handle : the openvpn_plugin_handle_t value which was returned by
26b1305
 *          openvpn_plugin_open.
26b1305
 *
26b1305
 * type : one of the PLUGIN_x types
26b1305
 *
26b1305
 * argv : a NULL-terminated array of "command line" options which
26b1305
 *        would normally be passed to the script.  argv[0] is the dynamic
26b1305
 *        library pathname.
26b1305
 *
26b1305
 * envp : a NULL-terminated array of OpenVPN-set environmental
26b1305
 *        variables in "name=value" format.  Note that for security reasons,
26b1305
 *        these variables are not actually written to the "official"
26b1305
 *        environmental variable store of the process.
26b1305
 *
26b1305
 * per_client_context : the per-client context pointer which was returned by
26b1305
 *        openvpn_plugin_client_constructor_v1, if defined.
26b1305
 *
26b1305
 * return_list : used to return data back to OpenVPN.
26b1305
 *
26b1305
 * RETURN VALUE
26b1305
 *
26b1305
 * OPENVPN_PLUGIN_FUNC_SUCCESS on success, OPENVPN_PLUGIN_FUNC_ERROR on failure
26b1305
 */
26b1305
OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v2)
26b1305
     (openvpn_plugin_handle_t handle,
26b1305
      const int type,
26b1305
      const char *argv[],
26b1305
      const char *envp[],
26b1305
      void *per_client_context,
26b1305
      struct openvpn_plugin_string_list **return_list);
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_close_v1
26b1305
 *
26b1305
 * REQUIRED: YES
26b1305
 * 
26b1305
 * ARGUMENTS
26b1305
 *
26b1305
 * handle : the openvpn_plugin_handle_t value which was returned by
26b1305
 *          openvpn_plugin_open.
26b1305
 *
26b1305
 * Called immediately prior to plug-in unload.
26b1305
 */
26b1305
OPENVPN_PLUGIN_DEF void OPENVPN_PLUGIN_FUNC(openvpn_plugin_close_v1)
26b1305
     (openvpn_plugin_handle_t handle);
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_abort_v1
26b1305
 *
26b1305
 * REQUIRED: NO
26b1305
 * 
26b1305
 * ARGUMENTS
26b1305
 *
26b1305
 * handle : the openvpn_plugin_handle_t value which was returned by
26b1305
 *          openvpn_plugin_open.
26b1305
 *
26b1305
 * Called when OpenVPN is in the process of aborting due to a fatal error.
26b1305
 * Will only be called on an open context returned by a prior successful
26b1305
 * openvpn_plugin_open callback.
26b1305
 */
26b1305
OPENVPN_PLUGIN_DEF void OPENVPN_PLUGIN_FUNC(openvpn_plugin_abort_v1)
26b1305
     (openvpn_plugin_handle_t handle);
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_client_constructor_v1
26b1305
 *
26b1305
 * Called to allocate a per-client memory region, which
26b1305
 * is then passed to the openvpn_plugin_func_v2 function.
26b1305
 * This function is called every time the OpenVPN server
26b1305
 * constructs a client instance object, which normally
26b1305
 * occurs when a session-initiating packet is received
26b1305
 * by a new client, even before the client has authenticated.
26b1305
 *
26b1305
 * This function should allocate the private memory needed
26b1305
 * by the plugin to track individual OpenVPN clients, and
26b1305
 * return a void * to this memory region.
26b1305
 *
26b1305
 * REQUIRED: NO
26b1305
 * 
26b1305
 * ARGUMENTS
26b1305
 *
26b1305
 * handle : the openvpn_plugin_handle_t value which was returned by
26b1305
 *          openvpn_plugin_open.
26b1305
 *
26b1305
 * RETURN VALUE
26b1305
 *
26b1305
 * void * pointer to plugin's private per-client memory region, or NULL
26b1305
 * if no memory region is required.
26b1305
 */
26b1305
OPENVPN_PLUGIN_DEF void * OPENVPN_PLUGIN_FUNC(openvpn_plugin_client_constructor_v1)
26b1305
     (openvpn_plugin_handle_t handle);
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_client_destructor_v1
26b1305
 *
26b1305
 * This function is called on client instance object destruction.
26b1305
 *
26b1305
 * REQUIRED: NO
26b1305
 * 
26b1305
 * ARGUMENTS
26b1305
 *
26b1305
 * handle : the openvpn_plugin_handle_t value which was returned by
26b1305
 *          openvpn_plugin_open.
26b1305
 *
26b1305
 * per_client_context : the per-client context pointer which was returned by
26b1305
 *        openvpn_plugin_client_constructor_v1, if defined.
26b1305
 */
26b1305
OPENVPN_PLUGIN_DEF void OPENVPN_PLUGIN_FUNC(openvpn_plugin_client_destructor_v1)
26b1305
     (openvpn_plugin_handle_t handle, void *per_client_context);
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_select_initialization_point_v1
26b1305
 *
26b1305
 * Several different points exist in OpenVPN's initialization sequence where
26b1305
 * the openvpn_plugin_open function can be called.  While the default is
26b1305
 * OPENVPN_PLUGIN_INIT_PRE_DAEMON, this function can be used to select a
26b1305
 * different initialization point.  For example, if your plugin needs to
26b1305
 * return configuration parameters to OpenVPN, use
26b1305
 * OPENVPN_PLUGIN_INIT_PRE_CONFIG_PARSE.
26b1305
 *
26b1305
 * REQUIRED: NO
26b1305
 * 
26b1305
 * RETURN VALUE:
26b1305
 *
26b1305
 * An OPENVPN_PLUGIN_INIT_x value.
26b1305
 */
26b1305
#define OPENVPN_PLUGIN_INIT_PRE_CONFIG_PARSE 1
26b1305
#define OPENVPN_PLUGIN_INIT_PRE_DAEMON       2 /* default */
26b1305
#define OPENVPN_PLUGIN_INIT_POST_DAEMON      3
26b1305
#define OPENVPN_PLUGIN_INIT_POST_UID_CHANGE  4
26b1305
26b1305
OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_select_initialization_point_v1)
26b1305
     (void);
26b1305
26b1305
/*
26b1305
 * FUNCTION: openvpn_plugin_min_version_required_v1
26b1305
 *
26b1305
 * This function is called by OpenVPN to query the minimum
26b1305
   plugin interface version number required by the plugin.
26b1305
 *
26b1305
 * REQUIRED: NO
26b1305
 * 
26b1305
 * RETURN VALUE
26b1305
 *
26b1305
 * The minimum OpenVPN plugin interface version number necessary to support
26b1305
 * this plugin.
26b1305
 */
26b1305
OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_min_version_required_v1)
26b1305
     (void);
26b1305
26b1305
/*
26b1305
 * Deprecated functions which are still supported for backward compatibility.
26b1305
 */
26b1305
26b1305
OPENVPN_PLUGIN_DEF openvpn_plugin_handle_t OPENVPN_PLUGIN_FUNC(openvpn_plugin_open_v1)
26b1305
     (unsigned int *type_mask,
26b1305
      const char *argv[],
26b1305
      const char *envp[]);
26b1305
26b1305
OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_func_v1)
26b1305
     (openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[]);