Leave a note about some unsafe code in cdr_manager
[asterisk/asterisk.git] / cdr / cdr_manager.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief Asterisk Call Manager CDR records.
20  *
21  * See also
22  * \arg \ref AstCDR
23  * \arg \ref AstAMI
24  * \arg \ref Config_ami
25  * \ingroup cdr_drivers
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <time.h>
33
34 #include "asterisk/channel.h"
35 #include "asterisk/cdr.h"
36 #include "asterisk/module.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/manager.h"
39 #include "asterisk/config.h"
40 #include "asterisk/pbx.h"
41
42 #define DATE_FORMAT     "%Y-%m-%d %T"
43 #define CONF_FILE       "cdr_manager.conf"
44 #define CUSTOM_FIELDS_BUF_SIZE 1024
45
46 static char *name = "cdr_manager";
47
48 static int enablecdr = 0;
49
50 /*! 
51  * XXX
52  * \bug The handling of this variable is not thread-safe.  Crashes are possible on reload.
53  */
54 static struct ast_str *customfields;
55
56 static int manager_log(struct ast_cdr *cdr);
57
58 static int load_config(int reload)
59 {
60         char *cat = NULL;
61         struct ast_config *cfg;
62         struct ast_variable *v;
63         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
64         int newenablecdr = 0;
65
66         cfg = ast_config_load(CONF_FILE, config_flags);
67         if (cfg == CONFIG_STATUS_FILEUNCHANGED)
68                 return 0;
69
70         if (cfg == CONFIG_STATUS_FILEINVALID) {
71                 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
72                 return 1;
73         }
74
75         if (reload && customfields) {
76                 ast_free(customfields);
77         }
78         customfields = NULL;
79
80         if (!cfg) {
81                 /* Standard configuration */
82                 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
83                 if (enablecdr)
84                         ast_cdr_unregister(name);
85                 enablecdr = 0;
86                 return 0;
87         }
88
89         while ( (cat = ast_category_browse(cfg, cat)) ) {
90                 if (!strcasecmp(cat, "general")) {
91                         v = ast_variable_browse(cfg, cat);
92                         while (v) {
93                                 if (!strcasecmp(v->name, "enabled"))
94                                         newenablecdr = ast_true(v->value);
95
96                                 v = v->next;
97                         }
98                 } else if (!strcasecmp(cat, "mappings")) {
99                         customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
100                         v = ast_variable_browse(cfg, cat);
101                         while (v) {
102                                 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
103                                         if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
104                                                 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
105                                                 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
106                                         } else {
107                                                 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
108                                                 break;
109                                         }
110
111                                 }
112                                 v = v->next;
113                         }
114                 }
115         }
116
117         ast_config_destroy(cfg);
118
119         if (enablecdr && !newenablecdr)
120                 ast_cdr_unregister(name);
121         else if (!enablecdr && newenablecdr)
122                 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
123         enablecdr = newenablecdr;
124
125         return 1;
126 }
127
128 static int manager_log(struct ast_cdr *cdr)
129 {
130         struct ast_tm timeresult;
131         char strStartTime[80] = "";
132         char strAnswerTime[80] = "";
133         char strEndTime[80] = "";
134         char buf[CUSTOM_FIELDS_BUF_SIZE];
135
136         if (!enablecdr)
137                 return 0;
138
139         ast_localtime(&cdr->start, &timeresult, NULL);
140         ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
141
142         if (cdr->answer.tv_sec) {
143                 ast_localtime(&cdr->answer, &timeresult, NULL);
144                 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
145         }
146
147         ast_localtime(&cdr->end, &timeresult, NULL);
148         ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
149
150         buf[0] = 0;
151         /* Custom fields handling */
152         if (customfields != NULL && ast_str_strlen(customfields)) {
153                 struct ast_channel *dummy = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Substitution/%p", cdr);
154                 if (!dummy) {
155                         ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
156                         return 0;
157                 }
158                 dummy->cdr = ast_cdr_dup(cdr);
159                 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
160                 ast_channel_release(dummy);
161         }
162
163         manager_event(EVENT_FLAG_CDR, "Cdr",
164             "AccountCode: %s\r\n"
165             "Source: %s\r\n"
166             "Destination: %s\r\n"
167             "DestinationContext: %s\r\n"
168             "CallerID: %s\r\n"
169             "Channel: %s\r\n"
170             "DestinationChannel: %s\r\n"
171             "LastApplication: %s\r\n"
172             "LastData: %s\r\n"
173             "StartTime: %s\r\n"
174             "AnswerTime: %s\r\n"
175             "EndTime: %s\r\n"
176             "Duration: %ld\r\n"
177             "BillableSeconds: %ld\r\n"
178             "Disposition: %s\r\n"
179             "AMAFlags: %s\r\n"
180             "UniqueID: %s\r\n"
181             "UserField: %s\r\n"
182             "%s",
183             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
184             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
185             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
186             ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
187
188         return 0;
189 }
190
191 static int unload_module(void)
192 {
193         ast_cdr_unregister(name);
194         if (customfields)
195                 ast_free(customfields);
196
197         return 0;
198 }
199
200 static int load_module(void)
201 {
202         /* Configuration file */
203         if (!load_config(0))
204                 return AST_MODULE_LOAD_DECLINE;
205
206         return AST_MODULE_LOAD_SUCCESS;
207 }
208
209 static int reload(void)
210 {
211         return load_config(1);
212 }
213
214 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
215                 .load = load_module,
216                 .unload = unload_module,
217                 .reload = reload,
218                );