2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief RADIUS CDR Support
22 * \author Philippe Sultan
23 * \extref The Radius Client Library - http://developer.berlios.de/projects/radiusclient-ng/
25 * \arg See also \ref AstCDR
26 * \ingroup cdr_drivers
30 <depend>radius</depend>
35 ASTERISK_FILE_VERSION(__FILE__, "$Rev$")
43 #include <sys/types.h>
44 #include <radiusclient-ng.h>
46 #include "asterisk/channel.h"
47 #include "asterisk/cdr.h"
48 #include "asterisk/module.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/options.h"
53 /*! ISO 8601 standard format */
54 #define DATE_FORMAT "%Y-%m-%d %T %z"
56 #define VENDOR_CODE 22736
59 PW_AST_ACCT_CODE = 101,
65 PW_AST_DST_CHAN = 107,
66 PW_AST_LAST_APP = 108,
67 PW_AST_LAST_DATA = 109,
68 PW_AST_START_TIME = 110,
69 PW_AST_ANSWER_TIME = 111,
70 PW_AST_END_TIME = 112,
71 PW_AST_DURATION = 113,
72 PW_AST_BILL_SEC = 114,
73 PW_AST_DISPOSITION = 115,
74 PW_AST_AMA_FLAGS = 116,
75 PW_AST_UNIQUE_ID = 117,
76 PW_AST_USER_FIELD = 118
80 /*! Log dates and times in UTC */
81 RADIUS_FLAG_USEGMTIME = (1 << 0),
83 RADIUS_FLAG_LOGUNIQUEID = (1 << 1),
85 RADIUS_FLAG_LOGUSERFIELD = (1 << 2)
88 static char *desc = "RADIUS CDR Backend";
89 static char *name = "radius";
90 static char *cdr_config = "cdr.conf";
92 static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
94 static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD };
96 static rc_handle *rh = NULL;
98 static int build_radius_record(VALUE_PAIR **send, struct ast_cdr *cdr)
100 int recordtype = PW_STATUS_STOP;
105 if (!rc_avpair_add(rh, send, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
109 if (!rc_avpair_add(rh, send, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
113 if (!rc_avpair_add(rh, send, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
117 if (!rc_avpair_add(rh, send, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
120 /* Destination context */
121 if (!rc_avpair_add(rh, send, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
125 if (!rc_avpair_add(rh, send, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
129 if (!rc_avpair_add(rh, send, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
132 /* Destination Channel */
133 if (!rc_avpair_add(rh, send, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
136 /* Last Application */
137 if (!rc_avpair_add(rh, send, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
141 if (!rc_avpair_add(rh, send, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
146 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
147 ast_localtime(&cdr->start, &tm,
148 ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
149 if (!rc_avpair_add(rh, send, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
153 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
154 ast_localtime(&cdr->answer, &tm,
155 ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
156 if (!rc_avpair_add(rh, send, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
160 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
161 ast_localtime(&cdr->end, &tm,
162 ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
163 if (!rc_avpair_add(rh, send, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
167 if (!rc_avpair_add(rh, send, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
170 /* Billable seconds */
171 if (!rc_avpair_add(rh, send, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
175 tmp = ast_cdr_disp2str(cdr->disposition);
176 if (!rc_avpair_add(rh, send, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
180 tmp = ast_cdr_flags2str(cdr->amaflags);
181 if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
184 if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
186 if (!rc_avpair_add(rh, send, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
190 if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) {
191 /* append the user field */
192 if (!rc_avpair_add(rh, send, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
196 /* Setting Acct-Session-Id & User-Name attributes for proper generation
197 of Acct-Unique-Session-Id on server side */
199 if (!rc_avpair_add(rh, send, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
203 if (!rc_avpair_add(rh, send, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
209 static int radius_log(struct ast_cdr *cdr)
211 int result = ERROR_RC;
212 VALUE_PAIR *send = NULL;
214 if (build_radius_record(&send, cdr)) {
215 ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
219 result = rc_acct(rh, 0, send);
221 ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
226 static int unload_module(void)
228 ast_cdr_unregister(name);
232 static int load_module(void)
234 struct ast_config *cfg;
235 struct ast_flags config_flags = { 0 };
239 if ((cfg = ast_config_load(cdr_config, config_flags))) {
240 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
241 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
242 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
243 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
244 ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
245 ast_config_destroy(cfg);
247 return AST_MODULE_LOAD_DECLINE;
250 rc_openlog("asterisk");
252 /* read radiusclient-ng config file */
253 if (!(rh = rc_read_config(radiuscfg))) {
254 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
255 return AST_MODULE_LOAD_DECLINE;
258 /* read radiusclient-ng dictionaries */
259 if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
260 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
261 return AST_MODULE_LOAD_DECLINE;
264 res = ast_cdr_register(name, desc, radius_log);
265 return AST_MODULE_LOAD_SUCCESS;
268 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "RADIUS CDR Backend");