Creating new doxygen macro "\extref" to create page that lists
[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 FreeRadius http://www.freeradius.org/
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 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         if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
147                 gmtime_r(&(cdr->start.tv_sec), &tm);
148         else
149                 localtime_r(&(cdr->start.tv_sec), &tm);
150         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
151         if (!rc_avpair_add(rh, send, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
152                 return -1;
153
154         /* Answer Time */
155         if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
156                 gmtime_r(&(cdr->answer.tv_sec), &tm);
157         else
158                 localtime_r(&(cdr->answer.tv_sec), &tm);
159         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
160         if (!rc_avpair_add(rh, send, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
161                 return -1;
162
163         /* End Time */
164         if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
165                 gmtime_r(&(cdr->end.tv_sec), &tm);
166         else
167                 localtime_r(&(cdr->end.tv_sec), &tm);
168         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
169         if (!rc_avpair_add(rh, send, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
170                 return -1;
171
172         /* Duration */ 
173         if (!rc_avpair_add(rh, send, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
174                 return -1;
175
176         /* Billable seconds */
177         if (!rc_avpair_add(rh, send, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
178                 return -1;
179
180         /* Disposition */
181         tmp = ast_cdr_disp2str(cdr->disposition);
182         if (!rc_avpair_add(rh, send, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
183                 return -1;
184
185         /* AMA Flags */
186         tmp = ast_cdr_flags2str(cdr->amaflags);
187         if (!rc_avpair_add(rh, send, 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, send, 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, send, 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         /* Channel */
205         if (!rc_avpair_add(rh, send, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
206                 return -1;
207
208         /* Unique ID */
209         if (!rc_avpair_add(rh, send, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
210                 return -1;
211
212         return 0;
213 }
214
215 static int radius_log(struct ast_cdr *cdr)
216 {
217         int result = ERROR_RC;
218         VALUE_PAIR *send = NULL;
219
220         if (build_radius_record(&send, cdr)) {
221                 if (option_debug)
222                         ast_log(LOG_DEBUG, "Unable to create RADIUS record. CDR not recorded!\n");
223                 return result;
224         }
225         
226         result = rc_acct(rh, 0, send);
227         if (result != OK_RC)
228                 ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
229
230         return result;
231 }
232
233 static int unload_module(void)
234 {
235         ast_cdr_unregister(name);
236         return 0;
237 }
238
239 static int load_module(void)
240 {
241         struct ast_config *cfg;
242         const char *tmp;
243
244         if ((cfg = ast_config_load(cdr_config))) {
245                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
246                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
247                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
248                 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
249                         ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
250                 ast_config_destroy(cfg);
251         } else 
252                 return AST_MODULE_LOAD_DECLINE;
253         
254         /* start logging */
255         rc_openlog("asterisk");
256
257         /* read radiusclient-ng config file */
258         if (!(rh = rc_read_config(radiuscfg))) {
259                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
260                 return -1;
261         }
262
263         /* read radiusclient-ng dictionaries */
264         if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
265                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
266                 return -1;
267         }
268         
269         return ast_cdr_register(name, desc, radius_log);
270 }
271
272 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "RADIUS CDR Backend");