| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 50 |
#define DATE_FORMAT "%Y-%m-%d %T %z" |
| 51 |
|
| 52 |
#define VENDOR_CODE 22736 |
| 53 |
|
| 54 |
enum { |
| 55 |
PW_AST_ACCT_CODE = 101, |
| 56 |
PW_AST_SRC = 102, |
| 57 |
PW_AST_DST = 103, |
| 58 |
PW_AST_DST_CTX = 104, |
| 59 |
PW_AST_CLID = 105, |
| 60 |
PW_AST_CHAN = 106, |
| 61 |
PW_AST_DST_CHAN = 107, |
| 62 |
PW_AST_LAST_APP = 108, |
| 63 |
PW_AST_LAST_DATA = 109, |
| 64 |
PW_AST_START_TIME = 110, |
| 65 |
PW_AST_ANSWER_TIME = 111, |
| 66 |
PW_AST_END_TIME = 112, |
| 67 |
PW_AST_DURATION = 113, |
| 68 |
PW_AST_BILL_SEC = 114, |
| 69 |
PW_AST_DISPOSITION = 115, |
| 70 |
PW_AST_AMA_FLAGS = 116, |
| 71 |
PW_AST_UNIQUE_ID = 117, |
| 72 |
PW_AST_USER_FIELD = 118 |
| 73 |
}; |
| 74 |
|
| 75 |
enum { |
| 76 |
|
| 77 |
RADIUS_FLAG_USEGMTIME = (1 << 0), |
| 78 |
|
| 79 |
RADIUS_FLAG_LOGUNIQUEID = (1 << 1), |
| 80 |
|
| 81 |
RADIUS_FLAG_LOGUSERFIELD = (1 << 2) |
| 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 |
|
| 94 |
static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD }; |
| 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 |
|
| 109 |
if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE)) |
| 110 |
return -1; |
| 111 |
|
| 112 |
|
| 113 |
if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE)) |
| 114 |
return -1; |
| 115 |
|
| 116 |
|
| 117 |
if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE)) |
| 118 |
return -1; |
| 119 |
|
| 120 |
|
| 121 |
if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE)) |
| 122 |
return -1; |
| 123 |
|
| 124 |
|
| 125 |
if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE)) |
| 126 |
return -1; |
| 127 |
|
| 128 |
|
| 129 |
if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE)) |
| 130 |
return -1; |
| 131 |
|
| 132 |
|
| 133 |
if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE)) |
| 134 |
return -1; |
| 135 |
|
| 136 |
|
| 137 |
if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE)) |
| 138 |
return -1; |
| 139 |
|
| 140 |
|
| 141 |
if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE)) |
| 142 |
return -1; |
| 143 |
|
| 144 |
|
| 145 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 167 |
if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE)) |
| 168 |
return -1; |
| 169 |
|
| 170 |
|
| 171 |
if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE)) |
| 172 |
return -1; |
| 173 |
|
| 174 |
|
| 175 |
tmp = ast_strdupa(ast_cdr_disp2str(cdr->disposition)); |
| 176 |
if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE)) |
| 177 |
return -1; |
| 178 |
|
| 179 |
|
| 180 |
tmp = ast_strdupa(ast_channel_amaflags2string(cdr->amaflags)); |
| 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 |
|
| 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 |
|
| 192 |
if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE)) |
| 193 |
return -1; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if (!rc_avpair_add(rh, tosend, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0)) |
| 201 |
return -1; |
| 202 |
|
| 203 |
|
| 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 |
| 260 |
return AST_MODULE_LOAD_DECLINE; |
| 261 |
|
| 262 |
|
| 263 |
if (!(rh = rc_read_config(radiuscfg))) { |
| 264 |
ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg); |
| 265 |
return AST_MODULE_LOAD_DECLINE; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 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; |
| 273 |
return AST_MODULE_LOAD_DECLINE; |
| 274 |
} |
| 275 |
|
| 276 |
if (ast_cdr_register(name, desc, radius_log)) { |
| 277 |
rc_destroy(rh); |
| 278 |
rh = NULL; |
| 279 |
return AST_MODULE_LOAD_DECLINE; |
| 280 |
} else { |
| 281 |
return AST_MODULE_LOAD_SUCCESS; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RADIUS CDR Backend", |
| 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 |
); |