MXS-862: Add authenticator options and instances
Authenticators now have a similar mechanism to the `router_options` parameter which enables configurable authentication. The authenticators also have a new initialize entry point which is similar to the createInstance entry point of the filters and routers. The value of `authenticator_options` is passed as a parameter to this function. The return vaulue of the `initialize` entry point is passed to the `create` entry point.
This commit is contained in:
103
server/core/authenticator.c
Normal file
103
server/core/authenticator.c
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl.
|
||||
*
|
||||
* Change Date: 2019-07-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
#include <gw_authenticator.h>
|
||||
#include <modutil.h>
|
||||
#include <modules.h>
|
||||
#include <maxscale/alloc.h>
|
||||
|
||||
/**
|
||||
* @file authenticator.c - Authenticator module functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Initialize an authenticator module
|
||||
*
|
||||
* Process the options into an array and pass them to the authenticator
|
||||
* initialization function
|
||||
*
|
||||
* The authenticator must implement the @c initialize entry point if this
|
||||
* function is called. If the authenticator does not implement this, behavior is
|
||||
* undefined.
|
||||
*
|
||||
* @param func Authenticator entry point
|
||||
* @param options Authenticator options
|
||||
* @return Authenticator instance or NULL on error
|
||||
*/
|
||||
bool authenticator_init(void** dest, const char *authenticator, const char *options)
|
||||
{
|
||||
bool rval = true;
|
||||
void *instance = NULL;
|
||||
GWAUTHENTICATOR *func = (GWAUTHENTICATOR*)load_module(authenticator, MODULE_AUTHENTICATOR);
|
||||
|
||||
if (func == NULL)
|
||||
{
|
||||
rval = false;
|
||||
}
|
||||
else if (func->initialize)
|
||||
{
|
||||
char *optarray[AUTHENTICATOR_MAX_OPTIONS + 1];
|
||||
size_t optlen = options ? strlen(options) : 0;
|
||||
char optcopy[optlen + 1];
|
||||
int optcount = 0;
|
||||
|
||||
if (options)
|
||||
{
|
||||
strcpy(optcopy, options);
|
||||
char *opt = optcopy;
|
||||
|
||||
while (opt && optcount < AUTHENTICATOR_MAX_OPTIONS)
|
||||
{
|
||||
char *end = strnchr_esc(opt, ',', sizeof(optcopy) - (opt - optcopy));
|
||||
|
||||
if (end)
|
||||
{
|
||||
*end++ = '\0';
|
||||
}
|
||||
|
||||
optarray[optcount++] = opt;
|
||||
opt = end;
|
||||
}
|
||||
}
|
||||
|
||||
optarray[optcount] = NULL;
|
||||
|
||||
if ((instance = func->initialize(optarray)) == NULL)
|
||||
{
|
||||
rval = false;
|
||||
}
|
||||
}
|
||||
|
||||
*dest = instance;
|
||||
return rval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the default authenticator for a protocol
|
||||
*
|
||||
* @param protocol Protocol to inspect
|
||||
* @return The default authenticator for the protocol or NULL if the protocol
|
||||
* does not provide one
|
||||
*/
|
||||
char* get_default_authenticator(const char *protocol)
|
||||
{
|
||||
char *rval = NULL;
|
||||
GWPROTOCOL *protofuncs = (GWPROTOCOL*)load_module(protocol, MODULE_PROTOCOL);
|
||||
|
||||
if (protofuncs && protofuncs->auth_default)
|
||||
{
|
||||
rval = MXS_STRDUP(protofuncs->auth_default());
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
Reference in New Issue
Block a user