Asterisk - The Open Source Telephony Project GIT-master-f36a736
cel_radius.c
Go to the documentation of this file.
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 * \ingroup cel_drivers
25 */
26
27/*** MODULEINFO
28 <depend>radius</depend>
29 <support_level>extended</support_level>
30 ***/
31
32#include "asterisk.h"
33
34#include RADIUS_HEADER_STR
35
36#include "asterisk/channel.h"
37#include "asterisk/cel.h"
38#include "asterisk/module.h"
39#include "asterisk/logger.h"
40#include "asterisk/utils.h"
41#include "asterisk/options.h"
42
43/*! ISO 8601 standard format */
44#define DATE_FORMAT "%Y-%m-%d %T %z"
45
46#define VENDOR_CODE 22736
47
48enum {
65};
66
67enum {
68 /*! Log dates and times in UTC */
70 /*! Log Unique ID */
72 /*! Log User Field */
74};
75
76static char *cel_config = "cel.conf";
77
78#ifdef FREERADIUS_CLIENT
79static char radiuscfg[PATH_MAX] = "/etc/radiusclient/radiusclient.conf";
80#else
81static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
82#endif
83
85
86static rc_handle *rh = NULL;
87
88#define RADIUS_BACKEND_NAME "CEL Radius Logging"
89
90#define ADD_VENDOR_CODE(x,y) (rc_avpair_add(rh, send, x, (void *)y, strlen(y), VENDOR_CODE))
91
92static int build_radius_record(VALUE_PAIR **send, struct ast_cel_event_record *record)
93{
94 int recordtype = PW_STATUS_STOP;
95 struct ast_tm tm;
96 char timestr[128];
97 char *amaflags;
98
99 if (!rc_avpair_add(rh, send, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0)) {
100 return -1;
101 }
102 /* Account code */
104 return -1;
105 }
106 /* Source */
108 return -1;
109 }
110 /* Destination */
111 if (!ADD_VENDOR_CODE(PW_AST_EXTEN, record->extension)) {
112 return -1;
113 }
114 /* Destination context */
115 if (!ADD_VENDOR_CODE(PW_AST_CONTEXT, record->context)) {
116 return -1;
117 }
118 /* Caller ID */
120 return -1;
121 }
122 /* Caller ID ani */
124 return -1;
125 }
126 /* Caller ID rdnis */
128 return -1;
129 }
130 /* Caller ID dnid */
132 return -1;
133 }
134 /* Channel */
136 return -1;
137 }
138 /* Last Application */
140 return -1;
141 }
142 /* Last Data */
144 return -1;
145 }
146 /* Event Time */
147 ast_localtime(&record->event_time, &tm,
149 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
150 if (!rc_avpair_add(rh, send, PW_AST_EVENT_TIME, timestr, strlen(timestr), VENDOR_CODE)) {
151 return -1;
152 }
153 /* AMA Flags */
155 if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, amaflags, strlen(amaflags), VENDOR_CODE)) {
156 return -1;
157 }
159 /* Unique ID */
161 return -1;
162 }
163 }
164 /* LinkedID */
166 return -1;
167 }
168 /* Setting Acct-Session-Id & User-Name attributes for proper generation
169 of Acct-Unique-Session-Id on server side */
170 /* Channel */
171 if (!rc_avpair_add(rh, send, PW_USER_NAME, (void *)record->channel_name,
172 strlen(record->channel_name), 0)) {
173 return -1;
174 }
175 return 0;
176}
177
178static void radius_log(struct ast_event *event)
179{
180 int result = ERROR_RC;
181 VALUE_PAIR *send = NULL;
182 struct ast_cel_event_record record = {
184 };
185
186 if (ast_cel_fill_record(event, &record)) {
187 return;
188 }
189
190 if (build_radius_record(&send, &record)) {
191 ast_debug(1, "Unable to create RADIUS record. CEL not recorded!\n");
192 goto return_cleanup;
193 }
194
195 result = rc_acct(rh, 0, send);
196 if (result != OK_RC) {
197 ast_log(LOG_ERROR, "Failed to record Radius CEL record!\n");
198 }
199
200return_cleanup:
201 if (send) {
202 rc_avpair_free(send);
203 }
204}
205
206static int unload_module(void)
207{
209 if (rh) {
210 rc_destroy(rh);
211 rh = NULL;
212 }
214}
215
216static int load_module(void)
217{
218 struct ast_config *cfg;
219 struct ast_flags config_flags = { 0 };
220 const char *tmp;
221
222 if ((cfg = ast_config_load(cel_config, config_flags))) {
224 if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg"))) {
226 }
228 } else {
230 }
231
232 /* read radiusclient-ng config file */
233 if (!(rh = rc_read_config(radiuscfg))) {
234 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
236 }
237
238 /* read radiusclient-ng dictionaries */
239 if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
240 ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
241 rc_destroy(rh);
242 rh = NULL;
244 }
245
247 rc_destroy(rh);
248 rh = NULL;
250 } else {
252 }
253}
254
256 .support_level = AST_MODULE_SUPPORT_EXTENDED,
257 .load = load_module,
258 .unload = unload_module,
259 .load_pri = AST_MODPRI_CDR_DRIVER,
260 .requires = "cel",
Asterisk main include file. File version handling, generic pbx functions.
#define PATH_MAX
Definition: asterisk.h:40
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
Call Event Logging API.
int ast_cel_backend_unregister(const char *name)
Unregister a CEL backend.
Definition: cel.c:1771
int ast_cel_fill_record(const struct ast_event *event, struct ast_cel_event_record *r)
Fill in an ast_cel_event_record from a CEL event.
Definition: cel.c:822
#define AST_CEL_EVENT_RECORD_VERSION
struct ABI version
Definition: cel.h:143
int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
Register a CEL backend.
Definition: cel.c:1783
static PGresult * result
Definition: cel_pgsql.c:84
#define RADIUS_BACKEND_NAME
Definition: cel_radius.c:88
@ RADIUS_FLAG_USEGMTIME
Definition: cel_radius.c:69
@ RADIUS_FLAG_LOGUNIQUEID
Definition: cel_radius.c:71
@ RADIUS_FLAG_LOGUSERFIELD
Definition: cel_radius.c:73
@ PW_AST_CIDNAME
Definition: cel_radius.c:51
@ PW_AST_ACCT_CODE
Definition: cel_radius.c:49
@ PW_AST_USER_NAME
Definition: cel_radius.c:63
@ PW_AST_LINKED_ID
Definition: cel_radius.c:64
@ PW_AST_CIDDNID
Definition: cel_radius.c:54
@ PW_AST_UNIQUE_ID
Definition: cel_radius.c:62
@ PW_AST_CIDNUM
Definition: cel_radius.c:50
@ PW_AST_AMA_FLAGS
Definition: cel_radius.c:61
@ PW_AST_CIDRDNIS
Definition: cel_radius.c:53
@ PW_AST_APPDATA
Definition: cel_radius.c:59
@ PW_AST_CONTEXT
Definition: cel_radius.c:56
@ PW_AST_EXTEN
Definition: cel_radius.c:55
@ PW_AST_APPNAME
Definition: cel_radius.c:58
@ PW_AST_EVENT_TIME
Definition: cel_radius.c:60
@ PW_AST_CIDANI
Definition: cel_radius.c:52
@ PW_AST_CHANNAME
Definition: cel_radius.c:57
#define VENDOR_CODE
Definition: cel_radius.c:46
static void radius_log(struct ast_event *event)
Definition: cel_radius.c:178
static rc_handle * rh
Definition: cel_radius.c:86
static int build_radius_record(VALUE_PAIR **send, struct ast_cel_event_record *record)
Definition: cel_radius.c:92
#define ADD_VENDOR_CODE(x, y)
Definition: cel_radius.c:90
static int load_module(void)
Definition: cel_radius.c:216
static struct ast_flags global_flags
Definition: cel_radius.c:84
static int unload_module(void)
Definition: cel_radius.c:206
static char radiuscfg[PATH_MAX]
Definition: cel_radius.c:81
#define DATE_FORMAT
Definition: cel_radius.c:44
static int amaflags
Definition: chan_iax2.c:476
General Asterisk PBX channel definitions.
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4392
#define ast_config_load(filename, flags)
Load a config file.
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:784
Support for logging to various files, console and syslog Configuration in file logger....
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_NOTICE
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CDR_DRIVER
Definition: module.h:345
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Options provided by main asterisk program.
#define NULL
Definition: resample.c:96
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Helper struct for getting the fields out of a CEL event.
Definition: cel.h:138
const char * caller_id_dnid
Definition: cel.h:157
const char * application_data
Definition: cel.h:162
const char * account_code
Definition: cel.h:163
const char * caller_id_rdnis
Definition: cel.h:156
const char * extension
Definition: cel.h:158
const char * caller_id_num
Definition: cel.h:154
const char * channel_name
Definition: cel.h:160
const char * linked_id
Definition: cel.h:166
const char * unique_id
Definition: cel.h:165
const char * context
Definition: cel.h:159
const char * application_name
Definition: cel.h:161
struct timeval event_time
Definition: cel.h:150
uint32_t version
struct ABI version
Definition: cel.h:148
const char * caller_id_ani
Definition: cel.h:155
const char * caller_id_name
Definition: cel.h:153
An event.
Definition: event.c:81
Structure used to handle boolean flags.
Definition: utils.h:199
A container that holds all config-related information.
Definition: cel_custom.c:52
Definition: astman.c:222
Utility functions.
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94