From e0162250d97b927aa58ba6a6ab277bad65513c99 Mon Sep 17 00:00:00 2001 From: russell Date: Sat, 20 May 2006 22:30:05 +0000 Subject: Add support for logging CDR recrods to a radius server (issue #6639, phsultan) - with contributions from miconda, jcollie, and sb - branch maintained by oej Thanks everyone! git-svn-id: http://svn.digium.com/svn/asterisk/trunk@29094 f38db490-d61c-443f-a65b-d21fe96a405b --- cdr/Makefile | 7 ++ cdr/cdr_radius.c | 318 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 325 insertions(+) create mode 100644 cdr/cdr_radius.c (limited to 'cdr') diff --git a/cdr/Makefile b/cdr/Makefile index ccce4d484..d6ec161d2 100644 --- a/cdr/Makefile +++ b/cdr/Makefile @@ -75,6 +75,13 @@ cdr_sqlite.so: cdr_sqlite.o cdr_sqlite.o: cdr_sqlite.c $(CC) -c -o $@ $(CFLAGS) $(SQLITE_INCLUDE) $< +cdr_radius.so: cdr_radius.o + $(CC) $(SOLINK) -o $@ $< $(RADIUSCLIENT_LIB) + +cdr_radius.o: cdr_radius.c + $(CC) -c -o $@ $(CFLAGS) $(RADIUSCLIENT_INCLUDE) $< + + ifneq ($(wildcard .depend),) include .depend endif diff --git a/cdr/cdr_radius.c b/cdr/cdr_radius.c new file mode 100644 index 000000000..721edca2d --- /dev/null +++ b/cdr/cdr_radius.c @@ -0,0 +1,318 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief RADIUS CDR Support + * \author Philippe Sultan + * + * \arg See also \ref AstCDR + * \ingroup cdr_drivers + */ + +/*** MODULEINFO + radiusclient + ***/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Rev$") + +#include "asterisk/channel.h" +#include "asterisk/cdr.h" +#include "asterisk/module.h" +#include "asterisk/logger.h" +#include "asterisk/utils.h" + +/*! ISO 8601 standard format */ +#define DATE_FORMAT "%Y-%m-%d %T %z" + +#define VENDOR_CODE 22736 + +enum { + PW_AST_ACCT_CODE = 101, + PW_AST_SRC = 102, + PW_AST_DST = 103, + PW_AST_DST_CTX = 104, + PW_AST_CLID = 105, + PW_AST_CHAN = 106, + PW_AST_DST_CHAN = 107, + PW_AST_LAST_APP = 108, + PW_AST_LAST_DATA = 109, + PW_AST_START_TIME = 110, + PW_AST_ANSWER_TIME = 111, + PW_AST_END_TIME = 112, + PW_AST_DURATION = 113, + PW_AST_BILL_SEC = 114, + PW_AST_DISPOSITION = 115, + PW_AST_AMA_FLAGS = 116, + PW_AST_UNIQUE_ID = 117, + PW_AST_USER_FIELD = 118 +}; + +enum { + /*! Log dates and times in UTC */ + RADIUS_FLAG_USEGMTIME = (1 << 0), + /*! Log Unique ID */ + RADIUS_FLAG_LOGUNIQUEID = (1 << 1), + /*! Log User Field */ + RADIUS_FLAG_LOGUSERFIELD = (1 << 2) +}; + +static char *desc = "RADIUS CDR Backend"; +static char *name = "radius"; +static char *cdr_config = "cdr.conf"; + +static char radiuscfg[AST_CONFIG_MAX_PATH] = "/etc/radiusclient-ng/radiusclient.conf"; + +static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD }; + +static rc_handle *rh = NULL; + +static int build_radius_record(VALUE_PAIR **send, struct ast_cdr *cdr) +{ + int recordtype = PW_STATUS_STOP; + struct tm tm; + char timestr[128]; + char *tmp; + + if (!rc_avpair_add(rh, send, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Account code */ + if (!rc_avpair_add(rh, send, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Source */ + if (!rc_avpair_add(rh, send, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Destination */ + if (!rc_avpair_add(rh, send, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Destination context */ + if (!rc_avpair_add(rh, send, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Caller ID */ + if (!rc_avpair_add(rh, send, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Channel */ + if (!rc_avpair_add(rh, send, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Destination Channel */ + if (!rc_avpair_add(rh, send, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Last Application */ + if (!rc_avpair_add(rh, send, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Last Data */ + if (!rc_avpair_add(rh, send, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + + /* Start Time */ + if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME)) + gmtime_r(&(cdr->start.tv_sec), &tm); + else + localtime_r(&(cdr->start.tv_sec), &tm); + strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm); + if (!rc_avpair_add(rh, send, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Answer Time */ + if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME)) + gmtime_r(&(cdr->answer.tv_sec), &tm); + else + localtime_r(&(cdr->answer.tv_sec), &tm); + strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm); + if (!rc_avpair_add(rh, send, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* End Time */ + if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME)) + gmtime_r(&(cdr->end.tv_sec), &tm); + else + localtime_r(&(cdr->end.tv_sec), &tm); + strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm); + if (!rc_avpair_add(rh, send, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Duration */ + if (!rc_avpair_add(rh, send, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Billable seconds */ + if (!rc_avpair_add(rh, send, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* Disposition */ + tmp = ast_cdr_disp2str(cdr->disposition); + if (!rc_avpair_add(rh, send, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + /* AMA Flags */ + tmp = ast_cdr_flags2str(cdr->amaflags); + if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + + if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) { + /* Unique ID */ + if (!rc_avpair_add(rh, send, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + } + + if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) { + /* append the user field */ + if (!rc_avpair_add(rh, send, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + } + + /* Setting Acct-Session-Id & User-Name attributes for proper generation + of Acct-Unique-Session-Id on server side */ + /* Channel */ + if (!rc_avpair_add(rh, send, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + /* Unique ID */ + if (!rc_avpair_add(rh, send, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0)) { + ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n"); + return -1; + } + return 0; +} + +static int radius_log(struct ast_cdr *cdr) +{ + int result = ERROR_RC; + VALUE_PAIR *send = NULL; + + if (build_radius_record(&send, cdr)) { + ast_log(LOG_WARNING, "Unable to create RADIUS record. CDR not recorded!\n"); + return result; + } + + result = rc_acct(rh, 0, send); + if (result != OK_RC) + ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n"); + + return result; +} + +static const char *description(void) +{ + return desc; +} + +static int unload_module(void *mod) +{ + ast_cdr_unregister(name); + return 0; +} + +static int load_module(void *mod) +{ + struct ast_config *cfg; + char *tmp; + + if ((cfg = ast_config_load(cdr_config))) { + ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME); + ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID); + ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD); + if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg"))) + ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg)); + ast_config_destroy(cfg); + } + + /* start logging */ + rc_openlog("asterisk"); + + /* read radiusclient-ng config file */ + if (!(rh = rc_read_config(radiuscfg))) { + ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg); + return -1; + } + + /* read radiusclient-ng dictionaries */ + if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) { + ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n"); + return -1; + } + + return ast_cdr_register(name, desc, radius_log); +} + +static const char *key(void) +{ + return ASTERISK_GPL_KEY; +} + +STD_MOD(MOD_0, NULL, NULL, NULL); -- cgit v1.2.3