Merge in ast_strftime branch, which changes timestamps to be accurate to the microsec...
[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 /*! \file
20  *
21  * \brief RADIUS CDR Support
22  * \author Philippe Sultan
23  * \extref The Radius Client Library - http://developer.berlios.de/projects/radiusclient-ng/
24  * 
25  * \arg See also \ref AstCDR
26  * \ingroup cdr_drivers
27  */
28
29 /*** MODULEINFO
30         <depend>radius</depend>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Rev$")
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <time.h>
43 #include <sys/types.h>
44 #include <radiusclient-ng.h>
45
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"
52
53 /*! ISO 8601 standard format */
54 #define DATE_FORMAT "%Y-%m-%d %T %z"
55
56 #define VENDOR_CODE           22736
57
58 enum {
59         PW_AST_ACCT_CODE =    101,
60         PW_AST_SRC =          102,
61         PW_AST_DST =          103,
62         PW_AST_DST_CTX =      104,
63         PW_AST_CLID =         105,
64         PW_AST_CHAN =         106,
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
77 };
78
79 enum {
80         /*! Log dates and times in UTC */
81         RADIUS_FLAG_USEGMTIME = (1 << 0),
82         /*! Log Unique ID */
83         RADIUS_FLAG_LOGUNIQUEID = (1 << 1),
84         /*! Log User Field */
85         RADIUS_FLAG_LOGUSERFIELD = (1 << 2)
86 };
87
88 static char *desc = "RADIUS CDR Backend";
89 static char *name = "radius";
90 static char *cdr_config = "cdr.conf";
91
92 static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
93
94 static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD };
95
96 static rc_handle *rh = NULL;
97
98 static int build_radius_record(VALUE_PAIR **send, 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, send, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
106                 return -1;
107
108         /* Account code */
109         if (!rc_avpair_add(rh, send, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
110                 return -1;
111
112         /* Source */
113         if (!rc_avpair_add(rh, send, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
114                 return -1;
115
116         /* Destination */
117         if (!rc_avpair_add(rh, send, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
118                 return -1;
119
120         /* Destination context */
121         if (!rc_avpair_add(rh, send, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
122                 return -1;
123
124         /* Caller ID */
125         if (!rc_avpair_add(rh, send, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
126                 return -1;
127
128         /* Channel */
129         if (!rc_avpair_add(rh, send, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
130                 return -1;
131
132         /* Destination Channel */
133         if (!rc_avpair_add(rh, send, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
134                 return -1;
135
136         /* Last Application */
137         if (!rc_avpair_add(rh, send, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
138                 return -1;
139
140         /* Last Data */
141         if (!rc_avpair_add(rh, send, 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,
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))
150                 return -1;
151
152         /* Answer Time */
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))
157                 return -1;
158
159         /* End Time */
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))
164                 return -1;
165
166         /* Duration */ 
167         if (!rc_avpair_add(rh, send, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
168                 return -1;
169
170         /* Billable seconds */
171         if (!rc_avpair_add(rh, send, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
172                 return -1;
173
174         /* Disposition */
175         tmp = ast_cdr_disp2str(cdr->disposition);
176         if (!rc_avpair_add(rh, send, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
177                 return -1;
178
179         /* AMA Flags */
180         tmp = ast_cdr_flags2str(cdr->amaflags);
181         if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
182                 return -1;
183
184         if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
185                 /* Unique ID */
186                 if (!rc_avpair_add(rh, send, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
187                         return -1;
188         }
189
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))
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         /* Channel */
199         if (!rc_avpair_add(rh, send, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
200                 return -1;
201
202         /* Unique ID */
203         if (!rc_avpair_add(rh, send, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
204                 return -1;
205
206         return 0;
207 }
208
209 static int radius_log(struct ast_cdr *cdr)
210 {
211         int result = ERROR_RC;
212         VALUE_PAIR *send = NULL;
213
214         if (build_radius_record(&send, cdr)) {
215                 ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
216                 return result;
217         }
218         
219         result = rc_acct(rh, 0, send);
220         if (result != OK_RC)
221                 ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
222
223         return result;
224 }
225
226 static int unload_module(void)
227 {
228         ast_cdr_unregister(name);
229         return 0;
230 }
231
232 static int load_module(void)
233 {
234         struct ast_config *cfg;
235         int res;
236         const char *tmp;
237
238         if ((cfg = ast_config_load(cdr_config))) {
239                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
240                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
241                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
242                 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
243                         ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
244                 ast_config_destroy(cfg);
245         } else 
246                 return AST_MODULE_LOAD_DECLINE;
247         
248         /* start logging */
249         rc_openlog("asterisk");
250
251         /* read radiusclient-ng config file */
252         if (!(rh = rc_read_config(radiuscfg))) {
253                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
254                 return AST_MODULE_LOAD_DECLINE;
255         }
256
257         /* read radiusclient-ng dictionaries */
258         if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
259                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
260                 return AST_MODULE_LOAD_DECLINE;
261         }
262         
263         res = ast_cdr_register(name, desc, radius_log);
264         return AST_MODULE_LOAD_SUCCESS;
265 }
266
267 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "RADIUS CDR Backend");