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