cdr_radius.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * \brief RADIUS CDR Support
  21. *
  22. * \author Philippe Sultan
  23. * The Radius Client Library
  24. * http://developer.berlios.de/projects/radiusclient-ng/
  25. *
  26. * \arg See also \ref AstCDR
  27. * \ingroup cdr_drivers
  28. */
  29. /*! \li \ref cdr_radius.c uses the configuration file \ref cdr.conf
  30. * \addtogroup configuration_file Configuration Files
  31. */
  32. /*** MODULEINFO
  33. <depend>radius</depend>
  34. <support_level>extended</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #ifdef FREERADIUS_CLIENT
  39. #include <freeradius-client.h>
  40. #else
  41. #include <radiusclient-ng.h>
  42. #endif
  43. #include "asterisk/channel.h"
  44. #include "asterisk/cdr.h"
  45. #include "asterisk/module.h"
  46. #include "asterisk/utils.h"
  47. /*! ISO 8601 standard format */
  48. #define DATE_FORMAT "%Y-%m-%d %T %z"
  49. #define VENDOR_CODE 22736
  50. enum {
  51. PW_AST_ACCT_CODE = 101,
  52. PW_AST_SRC = 102,
  53. PW_AST_DST = 103,
  54. PW_AST_DST_CTX = 104,
  55. PW_AST_CLID = 105,
  56. PW_AST_CHAN = 106,
  57. PW_AST_DST_CHAN = 107,
  58. PW_AST_LAST_APP = 108,
  59. PW_AST_LAST_DATA = 109,
  60. PW_AST_START_TIME = 110,
  61. PW_AST_ANSWER_TIME = 111,
  62. PW_AST_END_TIME = 112,
  63. PW_AST_DURATION = 113,
  64. PW_AST_BILL_SEC = 114,
  65. PW_AST_DISPOSITION = 115,
  66. PW_AST_AMA_FLAGS = 116,
  67. PW_AST_UNIQUE_ID = 117,
  68. PW_AST_USER_FIELD = 118
  69. };
  70. enum {
  71. /*! Log dates and times in UTC */
  72. RADIUS_FLAG_USEGMTIME = (1 << 0),
  73. /*! Log Unique ID */
  74. RADIUS_FLAG_LOGUNIQUEID = (1 << 1),
  75. /*! Log User Field */
  76. RADIUS_FLAG_LOGUSERFIELD = (1 << 2)
  77. };
  78. static const char desc[] = "RADIUS CDR Backend";
  79. static const char name[] = "radius";
  80. static const char cdr_config[] = "cdr.conf";
  81. #ifdef FREERADIUS_CLIENT
  82. static char radiuscfg[PATH_MAX] = "/etc/radiusclient/radiusclient.conf";
  83. #else
  84. static char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf";
  85. #endif
  86. static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD };
  87. static rc_handle *rh = NULL;
  88. static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
  89. {
  90. int recordtype = PW_STATUS_STOP;
  91. struct ast_tm tm;
  92. char timestr[128];
  93. char *tmp;
  94. if (!rc_avpair_add(rh, tosend, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
  95. return -1;
  96. /* Account code */
  97. if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
  98. return -1;
  99. /* Source */
  100. if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
  101. return -1;
  102. /* Destination */
  103. if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
  104. return -1;
  105. /* Destination context */
  106. if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
  107. return -1;
  108. /* Caller ID */
  109. if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
  110. return -1;
  111. /* Channel */
  112. if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
  113. return -1;
  114. /* Destination Channel */
  115. if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
  116. return -1;
  117. /* Last Application */
  118. if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
  119. return -1;
  120. /* Last Data */
  121. if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
  122. return -1;
  123. /* Start Time */
  124. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
  125. ast_localtime(&cdr->start, &tm,
  126. ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
  127. if (!rc_avpair_add(rh, tosend, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
  128. return -1;
  129. /* Answer Time */
  130. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
  131. ast_localtime(&cdr->answer, &tm,
  132. ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
  133. if (!rc_avpair_add(rh, tosend, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
  134. return -1;
  135. /* End Time */
  136. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
  137. ast_localtime(&cdr->end, &tm,
  138. ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
  139. if (!rc_avpair_add(rh, tosend, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
  140. return -1;
  141. /* Duration */
  142. if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
  143. return -1;
  144. /* Billable seconds */
  145. if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
  146. return -1;
  147. /* Disposition */
  148. tmp = ast_strdupa(ast_cdr_disp2str(cdr->disposition));
  149. if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
  150. return -1;
  151. /* AMA Flags */
  152. tmp = ast_strdupa(ast_channel_amaflags2string(cdr->amaflags));
  153. if (!rc_avpair_add(rh, tosend, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
  154. return -1;
  155. if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
  156. /* Unique ID */
  157. if (!rc_avpair_add(rh, tosend, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
  158. return -1;
  159. }
  160. if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) {
  161. /* append the user field */
  162. if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
  163. return -1;
  164. }
  165. /* Setting Acct-Session-Id & User-Name attributes for proper generation
  166. * of Acct-Unique-Session-Id on server side
  167. */
  168. /* Channel */
  169. if (!rc_avpair_add(rh, tosend, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
  170. return -1;
  171. /* Unique ID */
  172. if (!rc_avpair_add(rh, tosend, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
  173. return -1;
  174. return 0;
  175. }
  176. static int radius_log(struct ast_cdr *cdr)
  177. {
  178. int result = ERROR_RC;
  179. VALUE_PAIR *tosend = NULL;
  180. if (build_radius_record(&tosend, cdr)) {
  181. ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
  182. goto return_cleanup;
  183. }
  184. result = rc_acct(rh, 0, tosend);
  185. if (result != OK_RC) {
  186. ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
  187. }
  188. return_cleanup:
  189. if (tosend) {
  190. rc_avpair_free(tosend);
  191. }
  192. return result;
  193. }
  194. static int unload_module(void)
  195. {
  196. if (ast_cdr_unregister(name)) {
  197. return -1;
  198. }
  199. if (rh) {
  200. rc_destroy(rh);
  201. rh = NULL;
  202. }
  203. return 0;
  204. }
  205. static int load_module(void)
  206. {
  207. struct ast_config *cfg;
  208. struct ast_flags config_flags = { 0 };
  209. const char *tmp;
  210. if ((cfg = ast_config_load(cdr_config, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
  211. ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
  212. ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
  213. ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
  214. if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
  215. ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
  216. ast_config_destroy(cfg);
  217. } else
  218. return AST_MODULE_LOAD_DECLINE;
  219. /*
  220. * start logging
  221. *
  222. * NOTE: Yes this causes a slight memory leak if the module is
  223. * unloaded. However, it is better than a crash if cdr_radius
  224. * and cel_radius are both loaded.
  225. */
  226. tmp = ast_strdup("asterisk");
  227. if (tmp) {
  228. rc_openlog((char *) tmp);
  229. }
  230. /* read radiusclient-ng config file */
  231. if (!(rh = rc_read_config(radiuscfg))) {
  232. ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
  233. return AST_MODULE_LOAD_DECLINE;
  234. }
  235. /* read radiusclient-ng dictionaries */
  236. if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
  237. ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
  238. rc_destroy(rh);
  239. rh = NULL;
  240. return AST_MODULE_LOAD_DECLINE;
  241. }
  242. if (ast_cdr_register(name, desc, radius_log)) {
  243. rc_destroy(rh);
  244. rh = NULL;
  245. return AST_MODULE_LOAD_DECLINE;
  246. } else {
  247. return AST_MODULE_LOAD_SUCCESS;
  248. }
  249. }
  250. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RADIUS CDR Backend",
  251. .load = load_module,
  252. .unload = unload_module,
  253. .load_pri = AST_MODPRI_CDR_DRIVER,
  254. );