]> git.stg.codes - stg.git/blobdiff - projects/rlm_stg/rlm_stg.c
Port to CMake, get rid of os_int.h.
[stg.git] / projects / rlm_stg / rlm_stg.c
diff --git a/projects/rlm_stg/rlm_stg.c b/projects/rlm_stg/rlm_stg.c
deleted file mode 100644 (file)
index 3c6e9c9..0000000
+++ /dev/null
@@ -1,388 +0,0 @@
-/*
- *    This program is free software; you can redistribute it and/or modify
- *    it under the terms of the GNU General Public License as published by
- *    the Free Software Foundation; either version 2 of the License, or
- *    (at your option) any later version.
- *
- *    This program is distributed in the hope that it will be useful,
- *    but WITHOUT ANY WARRANTY; without even the implied warranty of
- *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *    GNU General Public License for more details.
- *
- *    You should have received a copy of the GNU General Public License
- *    along with this program; if not, write to the Free Software
- *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-/*
- *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
- */
-
-/*
- *  FreeRADIUS module for data access via Stargazer
- *
- *  $Revision: 1.8 $
- *  $Date: 2010/08/14 04:15:08 $
- *
- */
-
-#ifndef NDEBUG
-#define NDEBUG
-#include <freeradius/ident.h>
-#include <freeradius/radiusd.h>
-#include <freeradius/modules.h>
-#undef NDEBUG
-#endif
-
-#include "iface.h"
-#include "stgpair.h"
-
-typedef struct rlm_stg_t {
-    char* server;
-    uint16_t port;
-    char* password;
-} rlm_stg_t;
-
-static const CONF_PARSER module_config[] = {
-  { "server",  PW_TYPE_STRING_PTR, offsetof(rlm_stg_t,server), NULL,  "localhost"},
-  { "port",  PW_TYPE_INTEGER,     offsetof(rlm_stg_t,port), NULL,  "9091" },
-  { "password",  PW_TYPE_STRING_PTR, offsetof(rlm_stg_t,password), NULL,  "123456"},
-
-  { NULL, -1, 0, NULL, NULL }        /* end the list */
-};
-
-/*
- *    Do any per-module initialization that is separate to each
- *    configured instance of the module.  e.g. set up connections
- *    to external databases, read configuration files, set up
- *    dictionary entries, etc.
- *
- *    If configuration information is given in the config section
- *    that must be referenced in later calls, store a handle to it
- *    in *instance otherwise put a null pointer there.
- */
-static int stg_instantiate(CONF_SECTION* conf, void** instance)
-{
-    rlm_stg_t* data;
-
-    /*
-     *    Set up a storage area for instance data
-     */
-    data = rad_malloc(sizeof(*data));
-    if (!data)
-        return -1;
-
-    memset(data, 0, sizeof(*data));
-
-    /*
-     *    If the configuration parameters can't be parsed, then
-     *    fail.
-     */
-    if (cf_section_parse(conf, data, module_config) < 0) {
-        free(data);
-        return -1;
-    }
-
-    if (!stgInstantiateImpl(data->server, data->port)) {
-        free(data);
-        return -1;
-    }
-
-    *instance = data;
-
-    return 0;
-}
-
-/*
- *    Find the named user in this modules database.  Create the set
- *    of attribute-value pairs to check and reply with for this user
- *    from the database. The authentication code only needs to check
- *    the password, the rest is done here.
- */
-static int stg_authorize(void*, REQUEST* request)
-{
-    const STG_PAIR* pairs;
-    const STG_PAIR* pair;
-    size_t count = 0;
-    const char* username = NULL;
-    const char* password = NULL;
-
-    instance = instance;
-
-    DEBUG("rlm_stg: stg_authorize()");
-
-    if (request->username) {
-        username = request->username->data.strvalue;
-        DEBUG("rlm_stg: stg_authorize() request username field: '%s'", username);
-    }
-
-    if (request->password) {
-        password = request->password->data.strvalue;
-        DEBUG("rlm_stg: stg_authorize() request password field: '%s'", password);
-    }
-
-    pairs = stgAuthorizeImpl(username, password, request->packet->vps);
-
-    if (!pairs) {
-        DEBUG("rlm_stg: stg_authorize() failed.");
-        return RLM_MODULE_REJECT;
-    }
-
-    pair = pairs;
-    while (!emptyPair(pair)) {
-        VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
-        pairadd(&request->config_items, vp);
-        DEBUG("Adding pair '%s': '%s'", pair->key, pair->value);
-        ++pair;
-        ++count;
-    }
-    deletePairs(pairs);
-
-    if (count)
-        return RLM_MODULE_UPDATED;
-
-    return RLM_MODULE_NOOP;
-}
-
-/*
- *    Authenticate the user with the given password.
- */
-static int stg_authenticate(void*, REQUEST* request)
-{
-    const STG_PAIR* pairs;
-    const STG_PAIR* pair;
-    size_t count = 0;
-    const char* username = NULL;
-    const char* password = NULL;
-
-    instance = instance;
-
-    DEBUG("rlm_stg: stg_authenticate()");
-
-    if (request->username) {
-        username = request->username->data.strvalue;
-        DEBUG("rlm_stg: stg_authenticate() request username field: '%s'", username);
-    }
-
-    if (request->password) {
-        password = request->password->data.strvalue;
-        DEBUG("rlm_stg: stg_authenticate() request password field: '%s'", password);
-    }
-
-    pairs = stgAuthenticateImpl(username, password, request->packet->vps);
-
-    if (!pairs) {
-        DEBUG("rlm_stg: stg_authenticate() failed.");
-        return RLM_MODULE_REJECT;
-    }
-
-    pair = pairs;
-    while (!emptyPair(pair)) {
-        VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
-        pairadd(&request->reply->vps, vp);
-        ++pair;
-        ++count;
-    }
-    deletePairs(pairs);
-
-    if (count)
-        return RLM_MODULE_UPDATED;
-
-    return RLM_MODULE_NOOP;
-}
-
-/*
- *    Massage the request before recording it or proxying it
- */
-static int stg_preacct(void*, REQUEST*)
-{
-    const STG_PAIR* pairs;
-    const STG_PAIR* pair;
-    size_t count = 0;
-    const char* username = NULL;
-    const char* password = NULL;
-
-    DEBUG("rlm_stg: stg_preacct()");
-
-    instance = instance;
-
-    if (request->username) {
-        username = request->username->data.strvalue;
-        DEBUG("rlm_stg: stg_preacct() request username field: '%s'", username);
-    }
-
-    if (request->password) {
-        password = request->password->data.strvalue;
-        DEBUG("rlm_stg: stg_preacct() request password field: '%s'", password);
-    }
-
-    pairs = stgPreAcctImpl(username, password, request->packet->vps);
-
-    if (!pairs) {
-        DEBUG("rlm_stg: stg_preacct() failed.");
-        return RLM_MODULE_REJECT;
-    }
-
-    pair = pairs;
-    while (!emptyPair(pair)) {
-        VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
-        pairadd(&request->reply->vps, vp);
-        ++pair;
-        ++count;
-    }
-    deletePairs(pairs);
-
-    if (count)
-        return RLM_MODULE_UPDATED;
-
-    return RLM_MODULE_NOOP;
-}
-
-/*
- *    Write accounting information to this modules database.
- */
-static int stg_accounting(void*, REQUEST* request)
-{
-    const STG_PAIR* pairs;
-    const STG_PAIR* pair;
-    size_t count = 0;
-
-    instance = instance;
-
-    DEBUG("rlm_stg: stg_accounting()");
-
-    VALUE_PAIR* svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
-    VALUE_PAIR* sessid = pairfind(request->packet->vps, PW_ACCT_SESSION_ID);
-    VALUE_PAIR* sttype = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE);
-
-    if (!sessid) {
-        DEBUG("rlm_stg: stg_accounting() Acct-Session-ID undefined");
-        return RLM_MODULE_FAIL;
-    }
-
-    if (sttype) {
-        DEBUG("Acct-Status-Type := %s", sttype->data.strvalue);
-        if (svc) {
-            DEBUG("rlm_stg: stg_accounting() Service-Type defined as '%s'", svc->data.strvalue);
-            pairs = stgAccountingImpl((const char*)request->username->data.strvalue, (const char*)svc->data.strvalue, (const char*)sttype->data.strvalue, (const char*)sessid->data.strvalue);
-        } else {
-            DEBUG("rlm_stg: stg_accounting() Service-Type undefined");
-            pairs = stgAccountingImpl((const char*)request->username->data.strvalue, "", (const char*)sttype->data.strvalue, (const char*)sessid->data.strvalue);
-        }
-    } else {
-        DEBUG("rlm_stg: stg_accounting() Acct-Status-Type := NULL");
-        return RLM_MODULE_OK;
-    }
-    if (!pairs) {
-        DEBUG("rlm_stg: stg_accounting() failed.");
-        return RLM_MODULE_REJECT;
-    }
-
-    pair = pairs;
-    while (!emptyPair(pair)) {
-        VALUE_PAIR* pwd = pairmake(pair->key, pair->value, T_OP_SET);
-        pairadd(&request->reply->vps, pwd);
-        ++pair;
-        ++count;
-    }
-    deletePairs(pairs);
-
-    if (count)
-        return RLM_MODULE_UPDATED;
-
-    return RLM_MODULE_OK;
-}
-
-/*
- *    See if a user is already logged in. Sets request->simul_count to the
- *    current session count for this user and sets request->simul_mpp to 2
- *    if it looks like a multilink attempt based on the requested IP
- *    address, otherwise leaves request->simul_mpp alone.
- *
- *    Check twice. If on the first pass the user exceeds his
- *    max. number of logins, do a second pass and validate all
- *    logins by querying the terminal server (using eg. SNMP).
- */
-static int stg_checksimul(void*, REQUEST* request)
-{
-    DEBUG("rlm_stg: stg_checksimul()");
-
-    instance = instance;
-
-    request->simul_count = 0;
-
-    return RLM_MODULE_OK;
-}
-
-static int stg_postauth(void*, REQUEST* request)
-{
-    const STG_PAIR* pairs;
-    const STG_PAIR* pair;
-    size_t count = 0;
-
-    instance = instance;
-
-    DEBUG("rlm_stg: stg_postauth()");
-
-    VALUE_PAIR* svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
-
-    if (svc) {
-        DEBUG("rlm_stg: stg_postauth() Service-Type defined as '%s'", svc->data.strvalue);
-        pairs = stgPostAuthImpl((const char*)request->username->data.strvalue, (const char*)svc->data.strvalue);
-    } else {
-        DEBUG("rlm_stg: stg_postauth() Service-Type undefined");
-        pairs = stgPostAuthImpl((const char*)request->username->data.strvalue, "");
-    }
-    if (!pairs) {
-        DEBUG("rlm_stg: stg_postauth() failed.");
-        return RLM_MODULE_REJECT;
-    }
-
-    pair = pairs;
-    while (!emptyPair(pair)) {
-        VALUE_PAIR* pwd = pairmake(pair->key, pair->value, T_OP_SET);
-        pairadd(&request->reply->vps, pwd);
-        ++pair;
-        ++count;
-    }
-    deletePairs(pairs);
-
-    if (count)
-        return RLM_MODULE_UPDATED;
-
-    return RLM_MODULE_NOOP;
-}
-
-static int stg_detach(void* instance)
-{
-    free(((struct rlm_stg_t*)instance)->server);
-    free(instance);
-    return 0;
-}
-
-/*
- *    The module name should be the only globally exported symbol.
- *    That is, everything else should be 'static'.
- *
- *    If the module needs to temporarily modify it's instantiation
- *    data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
- *    The server will then take care of ensuring that the module
- *    is single-threaded.
- */
-module_t rlm_stg = {
-    RLM_MODULE_INIT,
-    "stg",
-    RLM_TYPE_THREAD_SAFE, /* type */
-    stg_instantiate,      /* instantiation */
-    stg_detach,           /* detach */
-    {
-        stg_authenticate, /* authentication */
-        stg_authorize,    /* authorization */
-        stg_preacct,      /* preaccounting */
-        stg_accounting,   /* accounting */
-        stg_checksimul,   /* checksimul */
-        stg_pre_proxy,    /* pre-proxy */
-        stg_post_proxy,   /* post-proxy */
-        stg_postauth      /* post-auth */
-    },
-};