1da21ed2da5d811a1e0186ad34d222153d9607bb
[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  * 
24  * \arg See also \ref AstCDR
25  * \ingroup cdr_drivers
26  */
27
28 /*** MODULEINFO
29         <depend>radiusclient</depend>
30  ***/
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <radiusclient-ng.h>
40
41 #include "asterisk.h"
42
43 ASTERISK_FILE_VERSION(__FILE__, "$Rev$")
44
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/module.h"
48 #include "asterisk/logger.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 char *desc = "RADIUS CDR Backend";
87 static char *name = "radius";
88 static char *cdr_config = "cdr.conf";
89
90 static char radiuscfg[AST_CONFIG_MAX_PATH] = "/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 **send, struct ast_cdr *cdr)
97 {
98         int recordtype = PW_STATUS_STOP;
99         struct tm tm;
100         char timestr[128];
101         char *tmp;
102
103         if (!rc_avpair_add(rh, send, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
104                 return -1;
105
106         /* Account code */
107         if (!rc_avpair_add(rh, send, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
108                 return -1;
109
110         /* Source */
111         if (!rc_avpair_add(rh, send, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
112                 return -1;
113
114         /* Destination */
115         if (!rc_avpair_add(rh, send, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
116                 return -1;
117
118         /* Destination context */
119         if (!rc_avpair_add(rh, send, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
120                 return -1;
121
122         /* Caller ID */
123         if (!rc_avpair_add(rh, send, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
124                 return -1;
125
126         /* Channel */
127         if (!rc_avpair_add(rh, send, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
128                 return -1;
129
130         /* Destination Channel */
131         if (!rc_avpair_add(rh, send, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
132                 return -1;
133
134         /* Last Application */
135         if (!rc_avpair_add(rh, send, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
136                 return -1;
137
138         /* Last Data */
139         if (!rc_avpair_add(rh, send, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
140                 return -1;
141
142
143         /* Start Time */
144         if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
145                 gmtime_r(&(cdr->start.tv_sec), &tm);
146         else
147                 localtime_r(&(cdr->start.tv_sec), &tm);
148         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
149         if (!rc_avpair_add(rh, send, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
150                 return -1;
151
152         /* Answer Time */
153         if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
154                 gmtime_r(&(cdr->answer.tv_sec), &tm);
155         else
156                 localtime_r(&(cdr->answer.tv_sec), &tm);
157         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
158         if (!rc_avpair_add(rh, send, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
159                 return -1;
160
161         /* End Time */
162         if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
163                 gmtime_r(&(cdr->end.tv_sec), &tm);
164         else
165                 localtime_r(&(cdr->end.tv_sec), &tm);
166         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
167         if (!rc_avpair_add(rh, send, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
168                 return -1;
169
170         /* Duration */ 
171         if (!rc_avpair_add(rh, send, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
172                 return -1;
173
174         /* Billable seconds */
175         if (!rc_avpair_add(rh, send, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
176                 return -1;
177
178         /* Disposition */
179         tmp = ast_cdr_disp2str(cdr->disposition);
180         if (!rc_avpair_add(rh, send, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
181                 return -1;
182
183         /* AMA Flags */
184         tmp = ast_cdr_flags2str(cdr->amaflags);
185         if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
186                 return -1;
187
188         if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
189                 /* Unique ID */
190                 if (!rc_avpair_add(rh, send, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
191                         return -1;
192         }
193
194         if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) {
195                 /* append the user field */
196                 if (!rc_avpair_add(rh, send, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
197                         return -1;
198         }
199
200         /* Setting Acct-Session-Id & User-Name attributes for proper generation
201            of Acct-Unique-Session-Id on server side */ 
202         /* Channel */
203         if (!rc_avpair_add(rh, send, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
204                 return -1;
205
206         /* Unique ID */
207         if (!rc_avpair_add(rh, send, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
208                 return -1;
209
210         return 0;
211 }
212
213 static int radius_log(struct ast_cdr *cdr)
214 {
215         int result = ERROR_RC;
216         VALUE_PAIR *send = NULL;
217
218         if (build_radius_record(&send, cdr)) {
219                 ast_log(LOG_WARNING, "Unable to create RADIUS record. CDR not recorded!\n");
220                 return result;
221         }
222         
223         result = rc_acct(rh, 0, send);
224         if (result != OK_RC)
225                 ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
226
227         return result;
228 }
229
230 static const char *description(void)
231 {
232         return desc;
233 }
234
235 static int unload_module(void *mod)
236 {
237         ast_cdr_unregister(name);
238         return 0;
239 }
240
241 static int load_module(void *mod)
242 {
243         struct ast_config *cfg;
244         char *tmp;
245
246         if ((cfg = ast_config_load(cdr_config))) {
247                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
248                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
249                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
250                 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
251                         ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
252                 ast_config_destroy(cfg);
253         }
254         
255         /* start logging */
256         rc_openlog("asterisk");
257
258         /* read radiusclient-ng config file */
259         if (!(rh = rc_read_config(radiuscfg))) {
260                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
261                 return -1;
262         }
263
264         /* read radiusclient-ng dictionaries */
265         if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
266                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
267                 return -1;
268         }
269         
270         return ast_cdr_register(name, desc, radius_log);
271 }
272
273 static const char *key(void)
274 {
275         return ASTERISK_GPL_KEY;
276 }
277
278 STD_MOD(MOD_0, NULL, NULL, NULL);