Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


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 <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 /*!
20  * \file
21  * \brief RADIUS CDR Support
22  *
23  * \author Philippe Sultan
24  * \extref The Radius Client Library - http://developer.berlios.de/projects/radiusclient-ng/
25  *
26  * \arg See also \ref AstCDR
27  * \ingroup cdr_drivers
28  */
29 
30 /*** MODULEINFO
31  <depend>radius</depend>
32  <support_level>extended</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 406801 $")
38 
39 #ifdef FREERADIUS_CLIENT
40 #include <freeradius-client.h>
41 #else
42 #include <radiusclient-ng.h>
43 #endif
44 
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/module.h"
48 #include "asterisk/utils.h"
49 
50 /*! ISO 8601 standard format */
51 #define DATE_FORMAT "%Y-%m-%d %T %z"
52 
53 #define VENDOR_CODE 22736
54 
55 enum {
57  PW_AST_SRC = 102,
58  PW_AST_DST = 103,
60  PW_AST_CLID = 105,
61  PW_AST_CHAN = 106,
74 };
75 
76 enum {
77  /*! Log dates and times in UTC */
79  /*! Log Unique ID */
81  /*! Log User Field */
83 };
84 
85 static const char desc[] = "RADIUS CDR Backend";
86 static const char name[] = "radius";
87 static const char cdr_config[] = "cdr.conf";
88 
89 #ifdef FREERADIUS_CLIENT
90 static char radiuscfg[PATH_MAX] = "/etc/radiusclient/radiusclient.conf";
91 #else
92 static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
93 #endif
94 
96 
97 static rc_handle *rh = NULL;
98 
99 static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
100 {
101  int recordtype = PW_STATUS_STOP;
102  struct ast_tm tm;
103  char timestr[128];
104  char *tmp;
105 
106  if (!rc_avpair_add(rh, tosend, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
107  return -1;
108 
109  /* Account code */
110  if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
111  return -1;
112 
113  /* Source */
114  if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
115  return -1;
116 
117  /* Destination */
118  if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
119  return -1;
120 
121  /* Destination context */
122  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
123  return -1;
124 
125  /* Caller ID */
126  if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
127  return -1;
128 
129  /* Channel */
130  if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
131  return -1;
132 
133  /* Destination Channel */
134  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
135  return -1;
136 
137  /* Last Application */
138  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
139  return -1;
140 
141  /* Last Data */
142  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
143  return -1;
144 
145 
146  /* Start Time */
147  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
148  ast_localtime(&cdr->start, &tm,
149  ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
150  if (!rc_avpair_add(rh, tosend, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
151  return -1;
152 
153  /* Answer Time */
154  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
155  ast_localtime(&cdr->answer, &tm,
156  ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
157  if (!rc_avpair_add(rh, tosend, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
158  return -1;
159 
160  /* End Time */
161  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
162  ast_localtime(&cdr->end, &tm,
163  ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
164  if (!rc_avpair_add(rh, tosend, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
165  return -1;
166 
167  /* Duration */
168  if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
169  return -1;
170 
171  /* Billable seconds */
172  if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
173  return -1;
174 
175  /* Disposition */
176  tmp = ast_cdr_disp2str(cdr->disposition);
177  if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
178  return -1;
179 
180  /* AMA Flags */
181  tmp = ast_cdr_flags2str(cdr->amaflags);
182  if (!rc_avpair_add(rh, tosend, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
183  return -1;
184 
185  if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
186  /* Unique ID */
187  if (!rc_avpair_add(rh, tosend, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
188  return -1;
189  }
190 
191  if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) {
192  /* append the user field */
193  if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
194  return -1;
195  }
196 
197  /* Setting Acct-Session-Id & User-Name attributes for proper generation
198  of Acct-Unique-Session-Id on server side */
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  ast_cdr_unregister(name);
236  if (rh) {
237  rc_destroy(rh);
238  rh = NULL;
239  }
240  return 0;
241 }
242 
243 static int load_module(void)
244 {
245  struct ast_config *cfg;
246  struct ast_flags config_flags = { 0 };
247  const char *tmp;
248 
249  if ((cfg = ast_config_load(cdr_config, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
250  ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
251  ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
252  ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
253  if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
254  ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
255  ast_config_destroy(cfg);
256  } else
258 
259  /*
260  * start logging
261  *
262  * NOTE: Yes this causes a slight memory leak if the module is
263  * unloaded. However, it is better than a crash if cdr_radius
264  * and cel_radius are both loaded.
265  */
266  tmp = ast_strdup("asterisk");
267  if (tmp) {
268  rc_openlog((char *) tmp);
269  }
270 
271  /* read radiusclient-ng config file */
272  if (!(rh = rc_read_config(radiuscfg))) {
273  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
275  }
276 
277  /* read radiusclient-ng dictionaries */
278  if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
279  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
280  rc_destroy(rh);
281  rh = NULL;
283  }
284 
285  if (ast_cdr_register(name, desc, radius_log)) {
286  rc_destroy(rh);
287  rh = NULL;
289  } else {
291  }
292 }
293 
295  .load = load_module,
296  .unload = unload_module,
297  .load_pri = AST_MODPRI_CDR_DRIVER,
298  );
#define VENDOR_CODE
Definition: cdr_radius.c:53
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:114
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:51
const char * ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
Gets a variable.
Definition: config.c:625
#define ast_strdup(a)
Definition: astmm.h:109
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:94
#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:95
long int billsec
Definition: cdr.h:108
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:90
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:1570
char uniqueid[150]
Definition: cdr.h:121
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
char * ast_cdr_flags2str(int flags)
Definition: cdr.c:977
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
Utility functions.
Call Detail Record API.
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:98
long int amaflags
Definition: cdr.h:112
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
General Asterisk PBX channel definitions.
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
static rc_handle * rh
Definition: cdr_radius.c:97
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:88
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:92
static const char desc[]
Definition: cdr_radius.c:85
struct timeval answer
Definition: cdr.h:102
Responsible for call detail data.
Definition: cdr.h:82
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:96
#define LOG_ERROR
Definition: logger.h:155
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
static char radiuscfg[PATH_MAX]
Definition: cdr_radius.c:92
static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
Definition: cdr_radius.c:99
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
struct timeval start
Definition: cdr.h:100
static const char name[]
static int load_module(void)
Definition: cdr_radius.c:243
long int duration
Definition: cdr.h:106
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:2351
Structure used to handle boolean flags.
Definition: utils.h:200
char src[AST_MAX_EXTENSION]
Definition: cdr.h:86
static int unload_module(void)
Definition: cdr_radius.c:233
struct timeval end
Definition: cdr.h:104
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
long int disposition
Definition: cdr.h:110
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:84
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:959
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:125