Asterisk - The Open Source Telephony Project GIT-master-f36a736
cdr_radius.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
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.
13 *
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.
17 */
18
19/*!
20 * \file
21 * \brief RADIUS CDR Support
22 *
23 * \author Philippe Sultan
24 * The Radius Client Library
25 * http://developer.berlios.de/projects/radiusclient-ng/
26 *
27 * \arg See also \ref AstCDR
28 * \ingroup cdr_drivers
29 */
30
31/*! \li \ref cdr_radius.c uses the configuration file \ref cdr.conf
32 * \addtogroup configuration_file Configuration Files
33 */
34
35/*** MODULEINFO
36 <depend>radius</depend>
37 <support_level>extended</support_level>
38 ***/
39
40#include "asterisk.h"
41
42#include RADIUS_HEADER_STR
43
44#include "asterisk/channel.h"
45#include "asterisk/cdr.h"
46#include "asterisk/module.h"
47#include "asterisk/utils.h"
48
49/*! ISO 8601 standard format */
50#define DATE_FORMAT "%Y-%m-%d %T %z"
51
52#define VENDOR_CODE 22736
53
54enum {
73};
74
75enum {
76 /*! Log dates and times in UTC */
78 /*! Log Unique ID */
80 /*! Log User Field */
82};
83
84static const char desc[] = "RADIUS CDR Backend";
85static const char name[] = "radius";
86static const char cdr_config[] = "cdr.conf";
87
88#ifdef FREERADIUS_CLIENT
89static char radiuscfg[PATH_MAX] = "/etc/radiusclient/radiusclient.conf";
90#else
91static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
92#endif
93
95
96static rc_handle *rh = NULL;
97
98static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
99{
100 int recordtype = PW_STATUS_STOP;
101 struct ast_tm tm;
102 char timestr[128];
103 char *tmp;
104
105 if (!rc_avpair_add(rh, tosend, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
106 return -1;
107
108 /* Account code */
109 if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
110 return -1;
111
112 /* Source */
113 if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
114 return -1;
115
116 /* Destination */
117 if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
118 return -1;
119
120 /* Destination context */
121 if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
122 return -1;
123
124 /* Caller ID */
125 if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
126 return -1;
127
128 /* Channel */
129 if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
130 return -1;
131
132 /* Destination Channel */
133 if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
134 return -1;
135
136 /* Last Application */
137 if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
138 return -1;
139
140 /* Last Data */
141 if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
142 return -1;
143
144
145 /* Start Time */
146 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
147 ast_localtime(&cdr->start, &tm,
149 if (!rc_avpair_add(rh, tosend, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
150 return -1;
151
152 /* Answer Time */
153 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
154 ast_localtime(&cdr->answer, &tm,
156 if (!rc_avpair_add(rh, tosend, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
157 return -1;
158
159 /* End Time */
160 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
161 ast_localtime(&cdr->end, &tm,
163 if (!rc_avpair_add(rh, tosend, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
164 return -1;
165
166 /* Duration */
167 if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
168 return -1;
169
170 /* Billable seconds */
171 if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
172 return -1;
173
174 /* Disposition */
176 if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
177 return -1;
178
179 /* AMA Flags */
181 if (!rc_avpair_add(rh, tosend, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
182 return -1;
183
185 /* Unique ID */
186 if (!rc_avpair_add(rh, tosend, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
187 return -1;
188 }
189
191 /* append the user field */
192 if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
193 return -1;
194 }
195
196 /* Setting Acct-Session-Id & User-Name attributes for proper generation
197 * of Acct-Unique-Session-Id on server side
198 */
199 /* Channel */
200 if (!rc_avpair_add(rh, tosend, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
201 return -1;
202
203 /* Unique ID */
204 if (!rc_avpair_add(rh, tosend, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
205 return -1;
206
207 return 0;
208}
209
210static int radius_log(struct ast_cdr *cdr)
211{
212 int result = ERROR_RC;
213 VALUE_PAIR *tosend = NULL;
214
215 if (build_radius_record(&tosend, cdr)) {
216 ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
217 goto return_cleanup;
218 }
219
220 result = rc_acct(rh, 0, tosend);
221 if (result != OK_RC) {
222 ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
223 }
224
225return_cleanup:
226 if (tosend) {
227 rc_avpair_free(tosend);
228 }
229
230 return result;
231}
232
233static int unload_module(void)
234{
236 return -1;
237 }
238
239 if (rh) {
240 rc_destroy(rh);
241 rh = NULL;
242 }
243 return 0;
244}
245
246static int load_module(void)
247{
248 struct ast_config *cfg;
249 struct ast_flags config_flags = { 0 };
250 const char *tmp;
251
252 if ((cfg = ast_config_load(cdr_config, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
256 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
259 } else
261
262 /* read radiusclient-ng config file */
263 if (!(rh = rc_read_config(radiuscfg))) {
264 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
266 }
267
268 /* read radiusclient-ng dictionaries */
269 if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
270 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
271 rc_destroy(rh);
272 rh = NULL;
274 }
275
277 rc_destroy(rh);
278 rh = NULL;
280 } else {
282 }
283}
284
286 .support_level = AST_MODULE_SUPPORT_EXTENDED,
287 .load = load_module,
288 .unload = unload_module,
289 .load_pri = AST_MODPRI_CDR_DRIVER,
290 .requires = "cdr",
Asterisk main include file. File version handling, generic pbx functions.
#define PATH_MAX
Definition: asterisk.h:40
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
Call Detail Record API.
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:3054
const char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:3509
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:3009
static const char cdr_config[]
Definition: cdr_radius.c:86
static const char name[]
Definition: cdr_radius.c:85
@ RADIUS_FLAG_USEGMTIME
Definition: cdr_radius.c:77
@ RADIUS_FLAG_LOGUNIQUEID
Definition: cdr_radius.c:79
@ RADIUS_FLAG_LOGUSERFIELD
Definition: cdr_radius.c:81
@ PW_AST_DST_CHAN
Definition: cdr_radius.c:61
@ PW_AST_DURATION
Definition: cdr_radius.c:67
@ PW_AST_SRC
Definition: cdr_radius.c:56
@ PW_AST_ACCT_CODE
Definition: cdr_radius.c:55
@ PW_AST_END_TIME
Definition: cdr_radius.c:66
@ PW_AST_UNIQUE_ID
Definition: cdr_radius.c:71
@ PW_AST_DISPOSITION
Definition: cdr_radius.c:69
@ PW_AST_LAST_APP
Definition: cdr_radius.c:62
@ PW_AST_USER_FIELD
Definition: cdr_radius.c:72
@ PW_AST_AMA_FLAGS
Definition: cdr_radius.c:70
@ PW_AST_CHAN
Definition: cdr_radius.c:60
@ PW_AST_ANSWER_TIME
Definition: cdr_radius.c:65
@ PW_AST_START_TIME
Definition: cdr_radius.c:64
@ PW_AST_DST_CTX
Definition: cdr_radius.c:58
@ PW_AST_CLID
Definition: cdr_radius.c:59
@ PW_AST_BILL_SEC
Definition: cdr_radius.c:68
@ PW_AST_DST
Definition: cdr_radius.c:57
@ PW_AST_LAST_DATA
Definition: cdr_radius.c:63
#define VENDOR_CODE
Definition: cdr_radius.c:52
static rc_handle * rh
Definition: cdr_radius.c:96
static const char desc[]
Definition: cdr_radius.c:84
static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
Definition: cdr_radius.c:98
static int load_module(void)
Definition: cdr_radius.c:246
static struct ast_flags global_flags
Definition: cdr_radius.c:94
static int unload_module(void)
Definition: cdr_radius.c:233
static char radiuscfg[PATH_MAX]
Definition: cdr_radius.c:91
#define DATE_FORMAT
Definition: cdr_radius.c:50
static int radius_log(struct ast_cdr *cdr)
Definition: cdr_radius.c:210
static PGresult * result
Definition: cel_pgsql.c:84
General Asterisk PBX channel definitions.
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4392
#define ast_config_load(filename, flags)
Load a config file.
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:784
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_NOTICE
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CDR_DRIVER
Definition: module.h:345
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define NULL
Definition: resample.c:96
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Responsible for call detail data.
Definition: cdr.h:279
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:291
long int disposition
Definition: cdr.h:307
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:295
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:325
long int billsec
Definition: cdr.h:305
struct timeval answer
Definition: cdr.h:299
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:289
long int duration
Definition: cdr.h:303
long int amaflags
Definition: cdr.h:309
char src[AST_MAX_EXTENSION]
Definition: cdr.h:283
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:285
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:281
char uniqueid[AST_MAX_UNIQUEID]
Definition: cdr.h:317
struct timeval start
Definition: cdr.h:297
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:311
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:293
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:287
struct timeval end
Definition: cdr.h:301
Structure used to handle boolean flags.
Definition: utils.h:199
Utility functions.
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94