Use non-blocking socket() and pipe() wrappers
[asterisk/asterisk.git] / cel / cel_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 CEL Support
22  * \author Philippe Sultan
23  * The Radius Client Library - http://developer.berlios.de/projects/radiusclient-ng/
24  *
25  * \arg See also \ref AstCEL
26  * \ingroup cel_drivers
27  */
28
29 /*** MODULEINFO
30         <depend>radius</depend>
31         <support_level>extended</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include RADIUS_HEADER_STR
37
38 #include "asterisk/channel.h"
39 #include "asterisk/cel.h"
40 #include "asterisk/module.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/options.h"
44
45 /*! ISO 8601 standard format */
46 #define DATE_FORMAT "%Y-%m-%d %T %z"
47
48 #define VENDOR_CODE           22736
49
50 enum {
51         PW_AST_ACCT_CODE =    101,
52         PW_AST_CIDNUM =       102,
53         PW_AST_CIDNAME =      103,
54         PW_AST_CIDANI =       104,
55         PW_AST_CIDRDNIS =     105,
56         PW_AST_CIDDNID =      106,
57         PW_AST_EXTEN =        107,
58         PW_AST_CONTEXT =      108,
59         PW_AST_CHANNAME =     109,
60         PW_AST_APPNAME =      110,
61         PW_AST_APPDATA =      111,
62         PW_AST_EVENT_TIME =   112,
63         PW_AST_AMA_FLAGS =    113,
64         PW_AST_UNIQUE_ID =    114,
65         PW_AST_USER_NAME =    115,
66         PW_AST_LINKED_ID =    116,
67 };
68
69 enum {
70         /*! Log dates and times in UTC */
71         RADIUS_FLAG_USEGMTIME = (1 << 0),
72         /*! Log Unique ID */
73         RADIUS_FLAG_LOGUNIQUEID = (1 << 1),
74         /*! Log User Field */
75         RADIUS_FLAG_LOGUSERFIELD = (1 << 2)
76 };
77
78 static char *cel_config = "cel.conf";
79
80 #ifdef FREERADIUS_CLIENT
81 static char radiuscfg[PATH_MAX] = "/etc/radiusclient/radiusclient.conf";
82 #else
83 static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
84 #endif
85
86 static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD };
87
88 static rc_handle *rh = NULL;
89
90 #define RADIUS_BACKEND_NAME "CEL Radius Logging"
91
92 #define ADD_VENDOR_CODE(x,y) (rc_avpair_add(rh, send, x, (void *)y, strlen(y), VENDOR_CODE))
93
94 static int build_radius_record(VALUE_PAIR **send, struct ast_cel_event_record *record)
95 {
96         int recordtype = PW_STATUS_STOP;
97         struct ast_tm tm;
98         char timestr[128];
99         char *amaflags;
100
101         if (!rc_avpair_add(rh, send, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0)) {
102                 return -1;
103         }
104         /* Account code */
105         if (!ADD_VENDOR_CODE(PW_AST_ACCT_CODE, record->account_code)) {
106                 return -1;
107         }
108         /* Source */
109         if (!ADD_VENDOR_CODE(PW_AST_CIDNUM, record->caller_id_num)) {
110                 return -1;
111         }
112         /* Destination */
113         if (!ADD_VENDOR_CODE(PW_AST_EXTEN, record->extension)) {
114                 return -1;
115         }
116         /* Destination context */
117         if (!ADD_VENDOR_CODE(PW_AST_CONTEXT, record->context)) {
118                 return -1;
119         }
120         /* Caller ID */
121         if (!ADD_VENDOR_CODE(PW_AST_CIDNAME, record->caller_id_name)) {
122                 return -1;
123         }
124         /* Caller ID ani */
125         if (!ADD_VENDOR_CODE(PW_AST_CIDANI, record->caller_id_ani)) {
126                 return -1;
127         }
128         /* Caller ID rdnis */
129         if (!ADD_VENDOR_CODE(PW_AST_CIDRDNIS, record->caller_id_rdnis)) {
130                 return -1;
131         }
132         /* Caller ID dnid */
133         if (!ADD_VENDOR_CODE(PW_AST_CIDDNID, record->caller_id_dnid)) {
134                 return -1;
135         }
136         /* Channel */
137         if (!ADD_VENDOR_CODE(PW_AST_CHANNAME, record->channel_name)) {
138                 return -1;
139         }
140         /* Last Application */
141         if (!ADD_VENDOR_CODE(PW_AST_APPNAME, record->application_name)) {
142                 return -1;
143         }
144         /* Last Data */
145         if (!ADD_VENDOR_CODE(PW_AST_APPDATA, record->application_data)) {
146                 return -1;
147         }
148         /* Event Time */
149         ast_localtime(&record->event_time, &tm,
150                 ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL);
151         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
152         if (!rc_avpair_add(rh, send, PW_AST_EVENT_TIME, timestr, strlen(timestr), VENDOR_CODE)) {
153                 return -1;
154         }
155         /* AMA Flags */
156         amaflags = ast_strdupa(ast_channel_amaflags2string(record->amaflag));
157         if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, amaflags, strlen(amaflags), VENDOR_CODE)) {
158                 return -1;
159         }
160         if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
161                 /* Unique ID */
162                 if (!ADD_VENDOR_CODE(PW_AST_UNIQUE_ID, record->unique_id)) {
163                         return -1;
164                 }
165         }
166         /* LinkedID */
167         if (!ADD_VENDOR_CODE(PW_AST_LINKED_ID, record->linked_id)) {
168                 return -1;
169         }
170         /* Setting Acct-Session-Id & User-Name attributes for proper generation
171            of Acct-Unique-Session-Id on server side */
172         /* Channel */
173         if (!rc_avpair_add(rh, send, PW_USER_NAME, (void *)record->channel_name,
174                         strlen(record->channel_name), 0)) {
175                 return -1;
176         }
177         return 0;
178 }
179
180 static void radius_log(struct ast_event *event)
181 {
182         int result = ERROR_RC;
183         VALUE_PAIR *send = NULL;
184         struct ast_cel_event_record record = {
185                 .version = AST_CEL_EVENT_RECORD_VERSION,
186         };
187
188         if (ast_cel_fill_record(event, &record)) {
189                 return;
190         }
191
192         if (build_radius_record(&send, &record)) {
193                 ast_debug(1, "Unable to create RADIUS record. CEL not recorded!\n");
194                 goto return_cleanup;
195         }
196
197         result = rc_acct(rh, 0, send);
198         if (result != OK_RC) {
199                 ast_log(LOG_ERROR, "Failed to record Radius CEL record!\n");
200         }
201
202 return_cleanup:
203         if (send) {
204                 rc_avpair_free(send);
205         }
206 }
207
208 static int unload_module(void)
209 {
210         ast_cel_backend_unregister(RADIUS_BACKEND_NAME);
211         if (rh) {
212                 rc_destroy(rh);
213                 rh = NULL;
214         }
215         return AST_MODULE_LOAD_SUCCESS;
216 }
217
218 static int load_module(void)
219 {
220         struct ast_config *cfg;
221         struct ast_flags config_flags = { 0 };
222         const char *tmp;
223
224         if ((cfg = ast_config_load(cel_config, config_flags))) {
225                 ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
226                 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg"))) {
227                         ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
228                 }
229                 ast_config_destroy(cfg);
230         } else {
231                 return AST_MODULE_LOAD_DECLINE;
232         }
233
234         /* read radiusclient-ng config file */
235         if (!(rh = rc_read_config(radiuscfg))) {
236                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
237                 return AST_MODULE_LOAD_DECLINE;
238         }
239
240         /* read radiusclient-ng dictionaries */
241         if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
242                 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
243                 rc_destroy(rh);
244                 rh = NULL;
245                 return AST_MODULE_LOAD_DECLINE;
246         }
247
248         if (ast_cel_backend_register(RADIUS_BACKEND_NAME, radius_log)) {
249                 rc_destroy(rh);
250                 rh = NULL;
251                 return AST_MODULE_LOAD_DECLINE;
252         } else {
253                 return AST_MODULE_LOAD_SUCCESS;
254         }
255 }
256
257 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RADIUS CEL Backend",
258         .support_level = AST_MODULE_SUPPORT_EXTENDED,
259         .load = load_module,
260         .unload = unload_module,
261         .load_pri = AST_MODPRI_CDR_DRIVER,
262         .requires = "cel",
263 );