Asterisk - The Open Source Telephony Project  18.5.0
cdr_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 /*!
20  * \file
21  * \brief RADIUS CDR Support
22  *
23  * \author Philippe Sultan
24  * The Radius Client Library
25  * http://developer.berlios.de/projects/radiusclient-ng/
26  *
27  * \arg See also \ref AstCDR
28  * \ingroup cdr_drivers
29  */
30 
31 /*! \li \ref cdr_radius.c uses the configuration file \ref cdr.conf
32  * \addtogroup configuration_file Configuration Files
33  */
34 
35 /*** MODULEINFO
36  <depend>radius</depend>
37  <support_level>extended</support_level>
38  ***/
39 
40 #include "asterisk.h"
41 
42 #include RADIUS_HEADER_STR
43 
44 #include "asterisk/channel.h"
45 #include "asterisk/cdr.h"
46 #include "asterisk/module.h"
47 #include "asterisk/utils.h"
48 
49 /*! ISO 8601 standard format */
50 #define DATE_FORMAT "%Y-%m-%d %T %z"
51 
52 #define VENDOR_CODE 22736
53 
54 enum {
56  PW_AST_SRC = 102,
57  PW_AST_DST = 103,
59  PW_AST_CLID = 105,
60  PW_AST_CHAN = 106,
73 };
74 
75 enum {
76  /*! Log dates and times in UTC */
78  /*! Log Unique ID */
80  /*! Log User Field */
82 };
83 
84 static const char desc[] = "RADIUS CDR Backend";
85 static const char name[] = "radius";
86 static const char cdr_config[] = "cdr.conf";
87 
88 #ifdef FREERADIUS_CLIENT
89 static char radiuscfg[PATH_MAX] = "/etc/radiusclient/radiusclient.conf";
90 #else
91 static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
92 #endif
93 
95 
96 static rc_handle *rh = NULL;
97 
98 static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
99 {
100  int recordtype = PW_STATUS_STOP;
101  struct ast_tm tm;
102  char timestr[128];
103  char *tmp;
104 
105  if (!rc_avpair_add(rh, tosend, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
106  return -1;
107 
108  /* Account code */
109  if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
110  return -1;
111 
112  /* Source */
113  if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
114  return -1;
115 
116  /* Destination */
117  if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
118  return -1;
119 
120  /* Destination context */
121  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
122  return -1;
123 
124  /* Caller ID */
125  if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
126  return -1;
127 
128  /* Channel */
129  if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
130  return -1;
131 
132  /* Destination Channel */
133  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
134  return -1;
135 
136  /* Last Application */
137  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
138  return -1;
139 
140  /* Last Data */
141  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
142  return -1;
143 
144 
145  /* Start Time */
146  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
147  ast_localtime(&cdr->start, &tm,
148  ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
149  if (!rc_avpair_add(rh, tosend, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
150  return -1;
151 
152  /* Answer Time */
153  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
154  ast_localtime(&cdr->answer, &tm,
155  ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
156  if (!rc_avpair_add(rh, tosend, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
157  return -1;
158 
159  /* End Time */
160  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
161  ast_localtime(&cdr->end, &tm,
162  ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
163  if (!rc_avpair_add(rh, tosend, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
164  return -1;
165 
166  /* Duration */
167  if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
168  return -1;
169 
170  /* Billable seconds */
171  if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
172  return -1;
173 
174  /* Disposition */
176  if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
177  return -1;
178 
179  /* AMA Flags */
181  if (!rc_avpair_add(rh, tosend, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
182  return -1;
183 
184  if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
185  /* Unique ID */
186  if (!rc_avpair_add(rh, tosend, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
187  return -1;
188  }
189 
190  if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) {
191  /* append the user field */
192  if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
193  return -1;
194  }
195 
196  /* Setting Acct-Session-Id & User-Name attributes for proper generation
197  * of Acct-Unique-Session-Id on server side
198  */
199  /* Channel */
200  if (!rc_avpair_add(rh, tosend, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
201  return -1;
202 
203  /* Unique ID */
204  if (!rc_avpair_add(rh, tosend, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
205  return -1;
206 
207  return 0;
208 }
209 
210 static int radius_log(struct ast_cdr *cdr)
211 {
212  int result = ERROR_RC;
213  VALUE_PAIR *tosend = NULL;
214 
215  if (build_radius_record(&tosend, cdr)) {
216  ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
217  goto return_cleanup;
218  }
219 
220  result = rc_acct(rh, 0, tosend);
221  if (result != OK_RC) {
222  ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
223  }
224 
225 return_cleanup:
226  if (tosend) {
227  rc_avpair_free(tosend);
228  }
229 
230  return result;
231 }
232 
233 static int unload_module(void)
234 {
235  if (ast_cdr_unregister(name)) {
236  return -1;
237  }
238 
239  if (rh) {
240  rc_destroy(rh);
241  rh = NULL;
242  }
243  return 0;
244 }
245 
246 static int load_module(void)
247 {
248  struct ast_config *cfg;
249  struct ast_flags config_flags = { 0 };
250  const char *tmp;
251 
252  if ((cfg = ast_config_load(cdr_config, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
253  ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
254  ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
255  ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
256  if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
257  ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
258  ast_config_destroy(cfg);
259  } else
261 
262  /* read radiusclient-ng config file */
263  if (!(rh = rc_read_config(radiuscfg))) {
264  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
266  }
267 
268  /* read radiusclient-ng dictionaries */
269  if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
270  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
271  rc_destroy(rh);
272  rh = NULL;
274  }
275 
276  if (ast_cdr_register(name, desc, radius_log)) {
277  rc_destroy(rh);
278  rh = NULL;
280  } else {
282  }
283 }
284 
286  .support_level = AST_MODULE_SUPPORT_EXTENDED,
287  .load = load_module,
288  .unload = unload_module,
289  .load_pri = AST_MODPRI_CDR_DRIVER,
290  .requires = "cdr",
291 );
#define VENDOR_CODE
Definition: cdr_radius.c:52
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:308
static int radius_log(struct ast_cdr *cdr)
Definition: cdr_radius.c:210
Asterisk main include file. File version handling, generic pbx functions.
#define DATE_FORMAT
Definition: cdr_radius.c:50
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:2988
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:288
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
#define ast_test_flag(p, flag)
Definition: utils.h:63
static struct ast_flags global_flags
Definition: cdr_radius.c:94
long int billsec
Definition: cdr.h:302
#define CONFIG_STATUS_FILEINVALID
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:284
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
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
static const char name[]
Definition: cdr_radius.c:85
#define NULL
Definition: resample.c:96
static const char cdr_config[]
Definition: cdr_radius.c:86
Utility functions.
Call Detail Record API.
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:292
long int amaflags
Definition: cdr.h:306
#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.
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:2943
General Asterisk PBX channel definitions.
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static rc_handle * rh
Definition: cdr_radius.c:96
char uniqueid[AST_MAX_UNIQUEID]
Definition: cdr.h:314
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:282
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:286
static const char desc[]
Definition: cdr_radius.c:84
struct timeval answer
Definition: cdr.h:296
Responsible for call detail data.
Definition: cdr.h:276
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:290
const char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:3430
#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 char radiuscfg[PATH_MAX]
Definition: cdr_radius.c:91
static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
Definition: cdr_radius.c:98
#define LOG_NOTICE
Definition: logger.h:263
struct timeval start
Definition: cdr.h:294
static int load_module(void)
Definition: cdr_radius.c:246
long int duration
Definition: cdr.h:300
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
Structure used to handle boolean flags.
Definition: utils.h:199
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",)
char src[AST_MAX_EXTENSION]
Definition: cdr.h:280
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:694
static int unload_module(void)
Definition: cdr_radius.c:233
struct timeval end
Definition: cdr.h:298
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
#define PATH_MAX
Definition: asterisk.h:40
long int disposition
Definition: cdr.h:304
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:278
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:318