cdr_adaptive_odbc: Add ability to set character for quoted identifiers.
[asterisk/asterisk.git] / cdr / cdr_radius.c
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 ASTERISK_REGISTER_FILE()
43
44 #ifdef FREERADIUS_CLIENT
45 #include <freeradius-client.h>
46 #else
47 #include <radiusclient-ng.h>
48 #endif
49
50 #include "asterisk/channel.h"
51 #include "asterisk/cdr.h"
52 #include "asterisk/module.h"
53 #include "asterisk/utils.h"
54
55 /*! ISO 8601 standard format */
56 #define DATE_FORMAT "%Y-%m-%d %T %z"
57
58 #define VENDOR_CODE           22736
59
60 enum {
61         PW_AST_ACCT_CODE =    101,
62         PW_AST_SRC =          102,
63         PW_AST_DST =          103,
64         PW_AST_DST_CTX =      104,
65         PW_AST_CLID =         105,
66         PW_AST_CHAN =         106,
67         PW_AST_DST_CHAN =     107,
68         PW_AST_LAST_APP =     108,
69         PW_AST_LAST_DATA =    109,
70         PW_AST_START_TIME =   110,
71         PW_AST_ANSWER_TIME =  111,
72         PW_AST_END_TIME =     112,
73         PW_AST_DURATION =     113,
74         PW_AST_BILL_SEC =     114,
75         PW_AST_DISPOSITION =  115,
76         PW_AST_AMA_FLAGS =    116,
77         PW_AST_UNIQUE_ID =    117,
78         PW_AST_USER_FIELD =   118
79 };
80
81 enum {
82         /*! Log dates and times in UTC */
83         RADIUS_FLAG_USEGMTIME = (1 << 0),
84         /*! Log Unique ID */
85         RADIUS_FLAG_LOGUNIQUEID = (1 << 1),
86         /*! Log User Field */
87         RADIUS_FLAG_LOGUSERFIELD = (1 << 2)
88 };
89
90 static const char desc[] = "RADIUS CDR Backend";
91 static const char name[] = "radius";
92 static const char cdr_config[] = "cdr.conf";
93
94 #ifdef FREERADIUS_CLIENT
95 static char radiuscfg[PATH_MAX] = "/etc/radiusclient/radiusclient.conf";
96 #else
97 static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
98 #endif
99
100 static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD };
101
102 static rc_handle *rh = NULL;
103
104 static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
105 {
106         int recordtype = PW_STATUS_STOP;
107         struct ast_tm tm;
108         char timestr[128];
109         char *tmp;
110
111         if (!rc_avpair_add(rh, tosend, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
112                 return -1;
113
114         /* Account code */
115         if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
116                 return -1;
117
118         /* Source */
119         if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
120                 return -1;
121
122         /* Destination */
123         if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
124                 return -1;
125
126         /* Destination context */
127         if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
128                 return -1;
129
130         /* Caller ID */
131         if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
132                 return -1;
133
134         /* Channel */
135         if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
136                 return -1;
137
138         /* Destination Channel */
139         if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
140                 return -1;
141
142         /* Last Application */
143         if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
144                 return -1;
145
146         /* Last Data */
147         if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
148                 return -1;
149
150
151         /* Start Time */
152         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
153                 ast_localtime(&cdr->start, &tm,
154                         ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
155         if (!rc_avpair_add(rh, tosend, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
156                 return -1;
157
158         /* Answer Time */
159         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
160                 ast_localtime(&cdr->answer, &tm,
161                         ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
162         if (!rc_avpair_add(rh, tosend, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
163                 return -1;
164
165         /* End Time */
166         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
167                 ast_localtime(&cdr->end, &tm,
168                         ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
169         if (!rc_avpair_add(rh, tosend, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
170                 return -1;
171
172         /* Duration */
173         if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
174                 return -1;
175
176         /* Billable seconds */
177         if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
178                 return -1;
179
180         /* Disposition */
181         tmp = ast_strdupa(ast_cdr_disp2str(cdr->disposition));
182         if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
183                 return -1;
184
185         /* AMA Flags */
186         tmp = ast_strdupa(ast_channel_amaflags2string(cdr->amaflags));
187         if (!rc_avpair_add(rh, tosend, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
188                 return -1;
189
190         if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
191                 /* Unique ID */
192                 if (!rc_avpair_add(rh, tosend, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
193                         return -1;
194         }
195
196         if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) {
197                 /* append the user field */
198                 if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
199                         return -1;
200         }
201
202         /* Setting Acct-Session-Id & User-Name attributes for proper generation
203          * of Acct-Unique-Session-Id on server side 
204          */
205         /* Channel */
206         if (!rc_avpair_add(rh, tosend, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
207                 return -1;
208
209         /* Unique ID */
210         if (!rc_avpair_add(rh, tosend, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
211                 return -1;
212
213         return 0;
214 }
215
216 static int radius_log(struct ast_cdr *cdr)
217 {
218         int result = ERROR_RC;
219         VALUE_PAIR *tosend = NULL;
220
221         if (build_radius_record(&tosend, cdr)) {
222                 ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
223                 goto return_cleanup;
224         }
225
226         result = rc_acct(rh, 0, tosend);
227         if (result != OK_RC) {
228                 ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
229         }
230
231 return_cleanup:
232         if (tosend) {
233                 rc_avpair_free(tosend);
234         }
235
236         return result;
237 }
238
239 static int unload_module(void)
240 {
241         if (ast_cdr_unregister(name)) {
242                 return -1;
243         }
244
245         if (rh) {
246                 rc_destroy(rh);
247                 rh = NULL;
248         }
249         return 0;
250 }
251
252 static int load_module(void)
253 {
254         struct ast_config *cfg;
255         struct ast_flags config_flags = { 0 };
256         const char *tmp;
257
258         if ((cfg = ast_config_load(cdr_config, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
259                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
260                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
261                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
262                 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
263                         ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
264                 ast_config_destroy(cfg);
265         } else
266                 return AST_MODULE_LOAD_DECLINE;
267
268         /*
269          * start logging
270          *
271          * NOTE: Yes this causes a slight memory leak if the module is
272          * unloaded.  However, it is better than a crash if cdr_radius
273          * and cel_radius are both loaded.
274          */
275         tmp = ast_strdup("asterisk");
276         if (tmp) {
277                 rc_openlog((char *) tmp);
278         }
279
280         /* read radiusclient-ng config file */
281         if (!(rh = rc_read_config(radiuscfg))) {
282                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
283                 return AST_MODULE_LOAD_DECLINE;
284         }
285
286         /* read radiusclient-ng dictionaries */
287         if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
288                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
289                 rc_destroy(rh);
290                 rh = NULL;
291                 return AST_MODULE_LOAD_DECLINE;
292         }
293
294         if (ast_cdr_register(name, desc, radius_log)) {
295                 rc_destroy(rh);
296                 rh = NULL;
297                 return AST_MODULE_LOAD_DECLINE;
298         } else {
299                 return AST_MODULE_LOAD_SUCCESS;
300         }
301 }
302
303 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RADIUS CDR Backend",
304                 .support_level = AST_MODULE_SUPPORT_EXTENDED,
305                 .load = load_module,
306                 .unload = unload_module,
307                 .load_pri = AST_MODPRI_CDR_DRIVER,
308         );