Prevent crashes from occurring when reading from data sources with large values
[asterisk/asterisk.git] / funcs / func_realtime.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005-2006, BJ Weschke. All rights reserved.
5  *
6  * BJ Weschke <bweschke@btwtech.com>
7  *
8  * This code is released by the author with no restrictions on usage.
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  */
17
18 /*! \file
19  *
20  * \brief REALTIME dialplan function
21  *
22  * \author BJ Weschke <bweschke@btwtech.com>
23  *
24  * \ingroup functions
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/file.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/config.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43
44 /*** DOCUMENTATION
45         <function name="REALTIME" language="en_US">
46                 <synopsis>
47                         RealTime Read/Write Functions.
48                 </synopsis>
49                 <syntax>
50                         <parameter name="family" required="true" />
51                         <parameter name="fieldmatch" required="true" />
52                         <parameter name="matchvalue" />
53                         <parameter name="delim1|field">
54                                 <para>Use <replaceable>delim1</replaceable> with <replaceable>delim2</replaceable> on
55                                 read and <replaceable>field</replaceable> without <replaceable>delim2</replaceable> on
56                                 write</para>
57                                 <para>If we are reading and <replaceable>delim1</replaceable> is not specified, defaults
58                                 to <literal>,</literal></para>
59                         </parameter>
60                         <parameter name="delim2">
61                                 <para>Parameter only used when reading, if not specified defaults to <literal>=</literal></para>
62                         </parameter>
63                 </syntax>
64                 <description>
65                         <para>This function will read or write values from/to a RealTime repository.
66                         REALTIME(....) will read names/values from the repository, and
67                         REALTIME(....)= will write a new value/field to the repository. On a
68                         read, this function returns a delimited text string. The name/value
69                         pairs are delimited by <replaceable>delim1</replaceable>, and the name and value are delimited
70                         between each other with delim2.
71                         If there is no match, NULL will be returned by the function.
72                         On a write, this function will always return NULL.</para>
73                 </description>
74                 <see-also>
75                         <ref type="function">REALTIME_STORE</ref>
76                         <ref type="function">REALTIME_DESTROY</ref>
77                         <ref type="function">REALTIME_FIELD</ref>
78                         <ref type="function">REALTIME_HASH</ref>
79                 </see-also>
80         </function>
81         <function name="REALTIME_STORE" language="en_US">
82                 <synopsis>
83                         RealTime Store Function.
84                 </synopsis>
85                 <syntax>
86                         <parameter name="family" required="true" />
87                         <parameter name="field1" required="true" />
88                         <parameter name="fieldN" required="true" multiple="true" />
89                         <parameter name="field30" required="true" />
90                 </syntax>
91                 <description>
92                         <para>This function will insert a new set of values into the RealTime repository.
93                         If RT engine provides an unique ID of the stored record, REALTIME_STORE(...)=..
94                         creates channel variable named RTSTOREID, which contains value of unique ID.
95                         Currently, a maximum of 30 field/value pairs is supported.</para>
96                 </description>
97                 <see-also>
98                         <ref type="function">REALTIME</ref>
99                         <ref type="function">REALTIME_DESTROY</ref>
100                         <ref type="function">REALTIME_FIELD</ref>
101                         <ref type="function">REALTIME_HASH</ref>
102                 </see-also>
103         </function>
104         <function name="REALTIME_DESTROY" language="en_US">
105                 <synopsis>
106                         RealTime Destroy Function.
107                 </synopsis>
108                 <syntax>
109                         <parameter name="family" required="true" />
110                         <parameter name="fieldmatch" required="true" />
111                         <parameter name="matchvalue" />
112                         <parameter name="delim1" />
113                         <parameter name="delim2" />
114                 </syntax>
115                 <description>
116                         <para>This function acts in the same way as REALTIME(....) does, except that
117                         it destroys the matched record in the RT engine.</para>
118                 </description>
119                 <see-also>
120                         <ref type="function">REALTIME</ref>
121                         <ref type="function">REALTIME_STORE</ref>
122                         <ref type="function">REALTIME_FIELD</ref>
123                         <ref type="function">REALTIME_HASH</ref>
124                 </see-also>
125         </function>
126         <function name="REALTIME_FIELD" language="en_US">
127                 <synopsis>
128                         RealTime query function.
129                 </synopsis>
130                 <syntax>
131                         <parameter name="family" required="true" />
132                         <parameter name="fieldmatch" required="true" />
133                         <parameter name="matchvalue" required="true" />
134                         <parameter name="fieldname" required="true" />
135                 </syntax>
136                 <description>
137                         <para>This function retrieves a single item, <replaceable>fieldname</replaceable>
138                         from the RT engine, where <replaceable>fieldmatch</replaceable> contains the value
139                         <replaceable>matchvalue</replaceable>.  When written to, the REALTIME_FIELD() function
140                         performs identically to the REALTIME() function.</para>
141                 </description>
142                 <see-also>
143                         <ref type="function">REALTIME</ref>
144                         <ref type="function">REALTIME_STORE</ref>
145                         <ref type="function">REALTIME_DESTROY</ref>
146                         <ref type="function">REALTIME_HASH</ref>
147                 </see-also>
148         </function>
149         <function name="REALTIME_HASH" language="en_US">
150                 <synopsis>
151                         RealTime query function.
152                 </synopsis>
153                 <syntax>
154                         <parameter name="family" required="true" />
155                         <parameter name="fieldmatch" required="true" />
156                         <parameter name="matchvalue" required="true" />
157                 </syntax>
158                 <description>
159                         <para>This function retrieves a single record from the RT engine, where
160                         <replaceable>fieldmatch</replaceable> contains the value
161                         <replaceable>matchvalue</replaceable> and formats the output suitably, such that
162                         it can be assigned to the HASH() function.  The HASH() function then provides
163                         a suitable method for retrieving each field value of the record.</para>
164                 </description>
165                 <see-also>
166                         <ref type="function">REALTIME</ref>
167                         <ref type="function">REALTIME_STORE</ref>
168                         <ref type="function">REALTIME_DESTROY</ref>
169                         <ref type="function">REALTIME_FIELD</ref>
170                 </see-also>
171         </function>
172  ***/
173
174 AST_THREADSTORAGE(buf1);
175 AST_THREADSTORAGE(buf2);
176 AST_THREADSTORAGE(buf3);
177
178 static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
179 {
180         struct ast_variable *var, *head;
181         struct ast_str *out;
182         size_t resultslen;
183         int n;
184         AST_DECLARE_APP_ARGS(args,
185                 AST_APP_ARG(family);
186                 AST_APP_ARG(fieldmatch);
187                 AST_APP_ARG(value);
188                 AST_APP_ARG(delim1);
189                 AST_APP_ARG(delim2);
190         );
191
192         if (ast_strlen_zero(data)) {
193                 ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
194                 return -1;
195         }
196
197         AST_STANDARD_APP_ARGS(args, data);
198
199         if (!args.delim1)
200                 args.delim1 = ",";
201         if (!args.delim2)
202                 args.delim2 = "=";
203
204         if (chan)
205                 ast_autoservice_start(chan);
206
207         head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
208
209         if (!head) {
210                 if (chan)
211                         ast_autoservice_stop(chan);
212                 return -1;
213         }
214
215         resultslen = 0;
216         n = 0;
217         for (var = head; var; n++, var = var->next)
218                 resultslen += strlen(var->name) + strlen(var->value);
219         /* add space for delimiters and final '\0' */
220         resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
221
222         if (resultslen > len) {
223                 ast_log(LOG_WARNING, "Failed to fetch. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
224                 return -1;
225         }
226
227         /* len is going to be sensible, so we don't need to check for stack
228          * overflows here. */
229         out = ast_str_alloca(resultslen);
230         for (var = head; var; var = var->next)
231                 ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
232         ast_copy_string(buf, ast_str_buffer(out), len);
233
234         ast_variables_destroy(head);
235
236         if (chan)
237                 ast_autoservice_stop(chan);
238
239         return 0;
240 }
241
242 static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
243 {
244         int res = 0;
245         AST_DECLARE_APP_ARGS(args,
246                 AST_APP_ARG(family);
247                 AST_APP_ARG(fieldmatch);
248                 AST_APP_ARG(value);
249                 AST_APP_ARG(field);
250         );
251
252         if (ast_strlen_zero(data)) {
253                 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
254                 return -1;
255         }
256
257         AST_STANDARD_APP_ARGS(args, data);
258
259         if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) {
260                 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
261                 return -1;
262         }
263
264         if (chan) {
265                 ast_autoservice_start(chan);
266         }
267
268         res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL);
269
270         if (res < 0) {
271                 ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
272         }
273
274         if (chan) {
275                 ast_autoservice_stop(chan);
276         }
277
278         return res;
279 }
280
281 static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
282 {
283         struct ast_variable *var, *head;
284         struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
285         struct ast_str *fields = ast_str_thread_get(&buf2, 16);
286         struct ast_str *values = ast_str_thread_get(&buf3, 16);
287         int first = 0;
288         enum { rtfield, rthash } which;
289         AST_DECLARE_APP_ARGS(args,
290                 AST_APP_ARG(family);
291                 AST_APP_ARG(fieldmatch);
292                 AST_APP_ARG(value);
293                 AST_APP_ARG(fieldname);
294         );
295
296         if (!strcmp(cmd, "REALTIME_FIELD")) {
297                 which = rtfield;
298         } else {
299                 which = rthash;
300         }
301
302         if (ast_strlen_zero(data)) {
303                 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
304                 return -1;
305         }
306
307         AST_STANDARD_APP_ARGS(args, data);
308
309         if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
310                 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
311                 return -1;
312         }
313
314         if (chan) {
315                 ast_autoservice_start(chan);
316         }
317
318         if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
319                 if (chan) {
320                         ast_autoservice_stop(chan);
321                 }
322                 return -1;
323         }
324
325         ast_str_reset(fields);
326         ast_str_reset(values);
327
328         for (var = head; var; var = var->next) {
329                 if (which == rtfield) {
330                         ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
331                         if (!strcasecmp(var->name, args.fieldname)) {
332                                 ast_debug(1, "Match! Value is %s\n", var->value);
333                                 ast_copy_string(buf, var->value, len);
334                                 break;
335                         }
336                 } else if (which == rthash) {
337                         ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
338                         ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX));
339                         ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX));
340                         first = 0;
341                 }
342         }
343         ast_variables_destroy(head);
344
345         if (which == rthash) {
346                 pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
347                 ast_copy_string(buf, ast_str_buffer(values), len);
348         }
349
350         if (chan) {
351                 ast_autoservice_stop(chan);
352         }
353
354         return 0;
355 }
356
357 static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value)
358 {
359         int res = 0;
360         char storeid[32];
361         char *valcopy;
362         AST_DECLARE_APP_ARGS(a,
363                 AST_APP_ARG(family);
364                 AST_APP_ARG(f)[30]; /* fields */
365         );
366
367         AST_DECLARE_APP_ARGS(v,
368                 AST_APP_ARG(v)[30]; /* values */
369         );
370
371         if (ast_strlen_zero(data)) {
372                 ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n");
373                 return -1;
374         }
375
376         if (chan)
377                 ast_autoservice_start(chan);
378
379         valcopy = ast_strdupa(value);
380         AST_STANDARD_APP_ARGS(a, data);
381         AST_STANDARD_APP_ARGS(v, valcopy);
382
383         res = ast_store_realtime(a.family,
384                 a.f[0], v.v[0], a.f[1], v.v[1], a.f[2], v.v[2], a.f[3], v.v[3], a.f[4], v.v[4],
385                 a.f[5], v.v[5], a.f[6], v.v[6], a.f[7], v.v[7], a.f[8], v.v[8], a.f[9], v.v[9],
386                 a.f[10], v.v[10], a.f[11], v.v[11], a.f[12], v.v[12], a.f[13], v.v[13], a.f[14], v.v[14],
387                 a.f[15], v.v[15], a.f[16], v.v[16], a.f[17], v.v[17], a.f[18], v.v[18], a.f[19], v.v[19],
388                 a.f[20], v.v[20], a.f[21], v.v[21], a.f[22], v.v[22], a.f[23], v.v[23], a.f[24], v.v[24],
389                 a.f[25], v.v[25], a.f[26], v.v[26], a.f[27], v.v[27], a.f[28], v.v[28], a.f[29], v.v[29], SENTINEL
390         );
391
392         if (res < 0) {
393                 ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n");
394         } else {
395                 snprintf(storeid, sizeof(storeid), "%d", res);
396                 pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid);
397         }
398
399         if (chan)
400                 ast_autoservice_stop(chan);
401
402         return 0;
403 }
404
405 static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
406 {
407         struct ast_variable *var, *head;
408         struct ast_str *out;
409         size_t resultslen;
410         int n;
411         AST_DECLARE_APP_ARGS(args,
412                 AST_APP_ARG(family);
413                 AST_APP_ARG(fieldmatch);
414                 AST_APP_ARG(value);
415                 AST_APP_ARG(delim1);
416                 AST_APP_ARG(delim2);
417         );
418
419         if (ast_strlen_zero(data)) {
420                 ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
421                 return -1;
422         }
423
424         AST_STANDARD_APP_ARGS(args, data);
425
426         if (!args.delim1)
427                 args.delim1 = ",";
428         if (!args.delim2)
429                 args.delim2 = "=";
430
431         if (chan)
432                 ast_autoservice_start(chan);
433
434         head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
435
436         if (!head) {
437                 if (chan)
438                         ast_autoservice_stop(chan);
439                 return -1;
440         }
441
442         resultslen = 0;
443         n = 0;
444         for (var = head; var; n++, var = var->next)
445                 resultslen += strlen(var->name) + strlen(var->value);
446         /* add space for delimiters and final '\0' */
447         resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
448
449         if (resultslen > len) {
450                 /* Unfortunately this does mean that we cannot destroy the row
451                  * anymore. But OTOH, we're not destroying someones data without
452                  * giving him the chance to look at it. */
453                 ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
454                 return -1;
455         }
456
457         /* len is going to be sensible, so we don't need to check for stack
458          * overflows here. */
459         out = ast_str_alloca(resultslen);
460         for (var = head; var; var = var->next) {
461                 ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
462         }
463         ast_copy_string(buf, ast_str_buffer(out), len);
464
465         ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL);
466         ast_variables_destroy(head);
467
468         if (chan)
469                 ast_autoservice_stop(chan);
470
471         return 0;
472 }
473
474 static struct ast_custom_function realtime_function = {
475         .name = "REALTIME",
476         .read = function_realtime_read,
477         .write = function_realtime_write,
478 };
479
480 static struct ast_custom_function realtimefield_function = {
481         .name = "REALTIME_FIELD",
482         .read = realtimefield_read,
483         .write = function_realtime_write,
484 };
485
486 static struct ast_custom_function realtimehash_function = {
487         .name = "REALTIME_HASH",
488         .read = realtimefield_read,
489 };
490
491 static struct ast_custom_function realtime_store_function = {
492         .name = "REALTIME_STORE",
493         .write = function_realtime_store,
494 };
495
496 static struct ast_custom_function realtime_destroy_function = {
497         .name = "REALTIME_DESTROY",
498         .read = function_realtime_readdestroy,
499 };
500
501 static int unload_module(void)
502 {
503         int res = 0;
504         res |= ast_custom_function_unregister(&realtime_function);
505         res |= ast_custom_function_unregister(&realtime_store_function);
506         res |= ast_custom_function_unregister(&realtime_destroy_function);
507         res |= ast_custom_function_unregister(&realtimefield_function);
508         res |= ast_custom_function_unregister(&realtimehash_function);
509         return res;
510 }
511
512 static int load_module(void)
513 {
514         int res = 0;
515         res |= ast_custom_function_register(&realtime_function);
516         res |= ast_custom_function_register(&realtime_store_function);
517         res |= ast_custom_function_register(&realtime_destroy_function);
518         res |= ast_custom_function_register(&realtimefield_function);
519         res |= ast_custom_function_register(&realtimehash_function);
520         return res;
521 }
522
523 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write/Store/Destroy values from a RealTime repository");