cdr_radius.c 8.4 KB

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