Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 {
57  PW_AST_EXTEN = 107,
67 };
68 
69 enum {
70  /*! Log dates and times in UTC */
72  /*! Log Unique ID */
74  /*! Log User Field */
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 
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 */
106  return -1;
107  }
108  /* Source */
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 */
122  return -1;
123  }
124  /* Caller ID ani */
126  return -1;
127  }
128  /* Caller ID rdnis */
130  return -1;
131  }
132  /* Caller ID dnid */
134  return -1;
135  }
136  /* Channel */
138  return -1;
139  }
140  /* Last Application */
142  return -1;
143  }
144  /* Last 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 = {
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 {
211  if (rh) {
212  rc_destroy(rh);
213  rh = NULL;
214  }
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 {
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);
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;
246  }
247 
249  rc_destroy(rh);
250  rh = NULL;
252  } else {
254  }
255 }
256 
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 );
const char * account_code
Definition: cel.h:161
const char * caller_id_name
Definition: cel.h:151
Helper struct for getting the fields out of a CEL event.
Definition: cel.h:136
const char * linked_id
Definition: cel.h:164
static int unload_module(void)
Definition: cel_radius.c:208
An event.
Definition: event.c:81
static int build_radius_record(VALUE_PAIR **send, struct ast_cel_event_record *record)
Definition: cel_radius.c:94
A container that holds all config-related information.
Definition: cel_custom.c:56
Asterisk main include file. File version handling, generic pbx functions.
static int amaflags
Definition: chan_iax2.c:431
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
#define ast_test_flag(p, flag)
Definition: utils.h:63
Call Event Logging API.
int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
Register a CEL backend.
Definition: cel.c:1740
static int load_module(void)
Definition: cel_radius.c:218
const char * application_data
Definition: cel.h:160
const char * application_name
Definition: cel.h:159
static int tmp()
Definition: bt_open.c:389
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
#define RADIUS_BACKEND_NAME
Definition: cel_radius.c:90
const char * extension
Definition: cel.h:156
Definition: astman.c:222
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4418
const char * caller_id_num
Definition: cel.h:152
#define ADD_VENDOR_CODE(x, y)
Definition: cel_radius.c:92
#define NULL
Definition: resample.c:96
Utility functions.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define ast_config_load(filename, flags)
Load a config file.
General Asterisk PBX channel definitions.
const char * context
Definition: cel.h:157
static char radiuscfg[PATH_MAX]
Definition: cel_radius.c:83
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
uint32_t version
struct ABI version
Definition: cel.h:146
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static struct ast_flags global_flags
Definition: cel_radius.c:86
int ast_cel_backend_unregister(const char *name)
Unregister a CEL backend.
Definition: cel.c:1728
#define LOG_ERROR
Definition: logger.h:285
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: main/utils.c:1951
static void radius_log(struct ast_event *event)
Definition: cel_radius.c:180
const char * caller_id_rdnis
Definition: cel.h:154
#define VENDOR_CODE
Definition: cel_radius.c:48
#define LOG_NOTICE
Definition: logger.h:263
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
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
const char * caller_id_ani
Definition: cel.h:153
#define AST_CEL_EVENT_RECORD_VERSION
struct ABI version
Definition: cel.h:141
Structure used to handle boolean flags.
Definition: utils.h:199
Support for logging to various files, console and syslog Configuration in file logger.conf.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:694
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static PGresult * result
Definition: cel_pgsql.c:88
const char * unique_id
Definition: cel.h:163
Options provided by main asterisk program.
const char * caller_id_dnid
Definition: cel.h:155
const char * channel_name
Definition: cel.h:158
#define PATH_MAX
Definition: asterisk.h:40
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
#define DATE_FORMAT
Definition: cel_radius.c:46
Asterisk module definitions.
static rc_handle * rh
Definition: cel_radius.c:88
struct timeval event_time
Definition: cel.h:148
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:819