Debian Codegraph @

main/pbx_functions.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, CFWare, LLC
5 *
6 * Corey Farrell <[email protected]>
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 *
21 * \brief Custom function management routines.
22 *
23 * \author Corey Farrell <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/_private.h"
33 #include "asterisk/cli.h"
34 #include "asterisk/linkedlists.h"
35 #include "asterisk/module.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/term.h"
38 #include "asterisk/threadstorage.h"
39 #include "asterisk/xmldoc.h"
40 #include "pbx_private.h"
41
42 /*!
43 * \brief A thread local indicating whether the current thread can run
44 * 'dangerous' dialplan functions.
45 */
46 AST_THREADSTORAGE(thread_inhibit_escalations_tl);
47
48 /*!
49 * \brief Set to true (non-zero) to globally allow all dangerous dialplan
50 * functions to run.
51 */
52 static int live_dangerously;
53
54 /*!
55 * \brief Registered functions container.
56 *
57 * It is sorted by function name.
58 */
59 static AST_RWLIST_HEAD_STATIC(acf_root, ast_custom_function);
60
61 static char *handle_show_functions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
62 {
63 struct ast_custom_function *acf;
64 int count_acf = 0;
65 int like = 0;
66
67 switch (cmd) {
68 case CLI_INIT:
69 e->command = "core show functions [like]";
70 e->usage =
71 "Usage: core show functions [like <text>]\n"
72 " List builtin functions, optionally only those matching a given string\n";
73 return NULL;
74 case CLI_GENERATE:
75 return NULL;
76 }
77
78 if (a->argc == 5 && (!strcmp(a->argv[3], "like")) ) {
79 like = 1;
80 } else if (a->argc != 3) {
81 return CLI_SHOWUSAGE;
82 }
83
84 ast_cli(a->fd, "%s Custom Functions:\n"
85 "--------------------------------------------------------------------------------\n",
86 like ? "Matching" : "Installed");
87
88 AST_RWLIST_RDLOCK(&acf_root);
89 AST_RWLIST_TRAVERSE(&acf_root, acf, acflist) {
90 if (!like || strstr(acf->name, a->argv[4])) {
91 count_acf++;
92 ast_cli(a->fd, "%-20.20s %-35.35s %s\n",
93 S_OR(acf->name, ""),
94 S_OR(acf->syntax, ""),
95 S_OR(acf->synopsis, ""));
96 }
97 }
98 AST_RWLIST_UNLOCK(&acf_root);
99
100 ast_cli(a->fd, "%d %scustom functions installed.\n", count_acf, like ? "matching " : "");
101
102 return CLI_SUCCESS;
103 }
104
105 static char *complete_functions(const char *word, int pos, int state)
106 {
107 struct ast_custom_function *cur;
108 char *ret = NULL;
109 int which = 0;
110 int wordlen;
111 int cmp;
112
113 if (pos != 3) {
114 return NULL;
115 }
116
117 wordlen = strlen(word);
118 AST_RWLIST_RDLOCK(&acf_root);
119 AST_RWLIST_TRAVERSE(&acf_root, cur, acflist) {
120 /*
121 * Do a case-insensitive search for convenience in this
122 * 'complete' function.
123 *
124 * We must search the entire container because the functions are
125 * sorted and normally found case sensitively.
126 */
127 cmp = strncasecmp(word, cur->name, wordlen);
128 if (!cmp) {
129 /* Found match. */
130 if (++which <= state) {
131 /* Not enough matches. */
132 continue;
133 }
134 ret = ast_strdup(cur->name);
135 break;
136 }
137 }
138 AST_RWLIST_UNLOCK(&acf_root);
139
140 return ret;
141 }
142
143 static char *handle_show_function(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
144 {
145 struct ast_custom_function *acf;
146 /* Maximum number of characters added by terminal coloring is 22 */
147 char *synopsis = NULL, *provided_by = NULL, *since = NULL, *description = NULL, *syntax = NULL, *arguments = NULL, *seealso = NULL;
148 char *rtn = CLI_SUCCESS;
149
150 switch (cmd) {
151 case CLI_INIT:
152 e->command = "core show function";
153 e->usage =
154 "Usage: core show function <function>\n"
155 " Describe a particular dialplan function.\n";
156 return NULL;
157 case CLI_GENERATE:
158 return complete_functions(a->word, a->pos, a->n);
159 }
160
161 if (a->argc != 4) {
162 return CLI_SHOWUSAGE;
163 }
164
165 if (!(acf = ast_custom_function_find(a->argv[3]))) {
166 ast_cli(a->fd, "No function by that name registered.\n");
167
168 return CLI_FAILURE;
169 }
170
171 #ifdef AST_XML_DOCS
172 if (acf->docsrc == AST_XML_DOC) {
173 synopsis = ast_xmldoc_printable(S_OR(acf->synopsis, "Not available"), 1);
174 provided_by = ast_xmldoc_printable(S_OR(acf->provided_by, "Not available"), 1);
175 since = ast_xmldoc_printable(S_OR(acf->since, "Not available"), 1);
176 description = ast_xmldoc_printable(S_OR(acf->desc, "Not available"), 1);
177 syntax = ast_xmldoc_printable(S_OR(acf->syntax, "Not available"), 1);
178 arguments = ast_xmldoc_printable(S_OR(acf->arguments, "Not available"), 1);
179 seealso = ast_xmldoc_printable(S_OR(acf->seealso, "Not available"), 1);
180 } else
181 #endif
182 {
183 synopsis = ast_strdup(S_OR(acf->synopsis, "Not Available"));
184 provided_by = ast_strdup(S_OR(acf->provided_by, "Not available"));
185 since = ast_strdup(S_OR(acf->since, "Not Available"));
186 description = ast_strdup(S_OR(acf->desc, "Not Available"));
187 syntax = ast_strdup(S_OR(acf->syntax, "Not Available"));
188 arguments = ast_strdup(S_OR(acf->arguments, "Not Available"));
189 seealso = ast_strdup(S_OR(acf->seealso, "Not Available"));
190 }
191 /* check allocated memory. */
192 if (!synopsis || !provided_by || !since || !description || !syntax || !arguments || !seealso) {
193 rtn = CLI_FAILURE;
194 goto free_docs;
195 }
196
197 ast_cli(a->fd, "\n"
198 "%s -= Info about Function '%s' =- %s\n\n"
199 COLORIZE_FMT "\n"
200 "%s\n\n"
201 COLORIZE_FMT "\n"
202 "%s\n\n"
203 COLORIZE_FMT "\n"
204 "%s\n\n"
205 COLORIZE_FMT "\n"
206 "%s\n\n"
207 COLORIZE_FMT "\n"
208 "%s\n\n"
209 COLORIZE_FMT "\n"
210 "%s\n\n"
211 COLORIZE_FMT "\n"
212 "%s\n\n",
213 ast_term_color(COLOR_MAGENTA, 0), acf->name, ast_term_reset(),
214 COLORIZE(COLOR_MAGENTA, 0, "[Synopsis]"), synopsis,
215 COLORIZE(COLOR_MAGENTA, 0, "[Provided By]"), provided_by,
216 COLORIZE(COLOR_MAGENTA, 0, "[Since]"), since,
217 COLORIZE(COLOR_MAGENTA, 0, "[Description]"), description,
218 COLORIZE(COLOR_MAGENTA, 0, "[Syntax]"), syntax,
219 COLORIZE(COLOR_MAGENTA, 0, "[Arguments]"), arguments,
220 COLORIZE(COLOR_MAGENTA, 0, "[See Also]"), seealso
221 );
222
223 free_docs:
224 ast_free(synopsis);
225 ast_free(provided_by);
226 ast_free(since);
227 ast_free(description);
228 ast_free(syntax);
229 ast_free(arguments);
230 ast_free(seealso);
231
232 return rtn;
233 }
234
235 static struct ast_custom_function *ast_custom_function_find_nolock(const char *name)
236 {
237 struct ast_custom_function *cur;
238 int cmp;
239
240 AST_RWLIST_TRAVERSE(&acf_root, cur, acflist) {
241 cmp = strcmp(name, cur->name);
242 if (cmp > 0) {
243 continue;
244 }
245 if (!cmp) {
246 /* Found it. */
247 break;
248 }
249 /* Not in container. */
250 cur = NULL;
251 break;
252 }
253
254 return cur;
255 }
256
257 struct ast_custom_function *ast_custom_function_find(const char *name)
258 {
259 struct ast_custom_function *acf;
260
261 AST_RWLIST_RDLOCK(&acf_root);
262 acf = ast_custom_function_find_nolock(name);
263 AST_RWLIST_UNLOCK(&acf_root);
264
265 return acf;
266 }
267
268 int ast_custom_function_unregister(struct ast_custom_function *acf)
269 {
270 struct ast_custom_function *cur;
271
272 if (!acf) {
273 return -1;
274 }
275
276 AST_RWLIST_WRLOCK(&acf_root);
277 cur = AST_RWLIST_REMOVE(&acf_root, acf, acflist);
278 if (cur) {
279 #ifdef AST_XML_DOCS
280 if (cur->docsrc == AST_XML_DOC) {
281 ast_string_field_free_memory(acf);
282 }
283 #endif
284 ast_verb(5, "Unregistered custom function %s\n", cur->name);
285 }
286 AST_RWLIST_UNLOCK(&acf_root);
287
288 return cur ? 0 : -1;
289 }
290
291 /*!
292 * \brief Returns true if given custom function escalates privileges on read.
293 *
294 * \param acf Custom function to query.
295 * \return True (non-zero) if reads escalate privileges.
296 * \return False (zero) if reads just read.
297 */
298 static int read_escalates(const struct ast_custom_function *acf)
299 {
300 return acf->read_escalates;
301 }
302
303 /*!
304 * \brief Returns true if given custom function escalates privileges on write.
305 *
306 * \param acf Custom function to query.
307 * \return True (non-zero) if writes escalate privileges.
308 * \return False (zero) if writes just write.
309 */
310 static int write_escalates(const struct ast_custom_function *acf)
311 {
312 return acf->write_escalates;
313 }
314
315 /*! \internal
316 * \brief Retrieve the XML documentation of a specified ast_custom_function,
317 * and populate ast_custom_function string fields.
318 * \param acf ast_custom_function structure with empty 'desc' and 'synopsis'
319 * but with a function 'name'.
320 * \retval -1 On error.
321 * \retval 0 On success.
322 */
323 static int acf_retrieve_docs(struct ast_custom_function *acf)
324 {
325 #ifdef AST_XML_DOCS
326 char *tmpxml;
327
328 /* Let's try to find it in the Documentation XML */
329 if (!ast_strlen_zero(acf->desc) || !ast_strlen_zero(acf->synopsis)) {
330 return 0;
331 }
332
333 if (ast_string_field_init(acf, 128)) {
334 return -1;
335 }
336
337 if (ast_string_field_init_extended(acf, since)) {
338 ast_string_field_free_memory(acf);
339 return -1;
340 }
341
342 if (ast_string_field_init_extended(acf, provided_by)) {
343 ast_string_field_free_memory(acf);
344 return -1;
345 }
346
347 /* load synopsis */
348 tmpxml = ast_xmldoc_build_synopsis("function", acf->name, ast_module_name(acf->mod));
349 ast_string_field_set(acf, synopsis, tmpxml);
350 ast_free(tmpxml);
351
352 /* load provided_by */
353 tmpxml = ast_xmldoc_build_provided_by("function", acf->name, ast_module_name(acf->mod));
354 ast_string_field_set(acf, provided_by, tmpxml);
355 ast_free(tmpxml);
356
357 /* load since */
358 tmpxml = ast_xmldoc_build_since("function", acf->name, ast_module_name(acf->mod));
359 ast_string_field_set(acf, since, tmpxml);
360 ast_free(tmpxml);
361
362 /* load description */
363 tmpxml = ast_xmldoc_build_description("function", acf->name, ast_module_name(acf->mod));
364 ast_string_field_set(acf, desc, tmpxml);
365 ast_free(tmpxml);
366
367 /* load syntax */
368 tmpxml = ast_xmldoc_build_syntax("function", acf->name, ast_module_name(acf->mod));
369 ast_string_field_set(acf, syntax, tmpxml);
370 ast_free(tmpxml);
371
372 /* load arguments */
373 tmpxml = ast_xmldoc_build_arguments("function", acf->name, ast_module_name(acf->mod));
374 ast_string_field_set(acf, arguments, tmpxml);
375 ast_free(tmpxml);
376
377 /* load seealso */
378 tmpxml = ast_xmldoc_build_seealso("function", acf->name, ast_module_name(acf->mod));
379 ast_string_field_set(acf, seealso, tmpxml);
380 ast_free(tmpxml);
381
382 acf->docsrc = AST_XML_DOC;
383 #endif
384
385 return 0;
386 }
387
388 int __ast_custom_function_register(struct ast_custom_function *acf, struct ast_module *mod)
389 {
390 struct ast_custom_function *cur;
391
392 if (!acf) {
393 return -1;
394 }
395
396 acf->mod = mod;
397 #ifdef AST_XML_DOCS
398 acf->docsrc = AST_STATIC_DOC;
399 #endif
400
401 if (acf_retrieve_docs(acf)) {
402 return -1;
403 }
404
405 AST_RWLIST_WRLOCK(&acf_root);
406
407 cur = ast_custom_function_find_nolock(acf->name);
408 if (cur) {
409 ast_log(LOG_ERROR, "Function %s already registered.\n", acf->name);
410 AST_RWLIST_UNLOCK(&acf_root);
411 return -1;
412 }
413
414 /* Store in alphabetical order */
415 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&acf_root, cur, acflist) {
416 if (strcmp(acf->name, cur->name) < 0) {
417 AST_RWLIST_INSERT_BEFORE_CURRENT(acf, acflist);
418 break;
419 }
420 }
421 AST_RWLIST_TRAVERSE_SAFE_END;
422 if (!cur) {
423 AST_RWLIST_INSERT_TAIL(&acf_root, acf, acflist);
424 }
425
426 AST_RWLIST_UNLOCK(&acf_root);
427
428 ast_verb(5, "Registered custom function '" COLORIZE_FMT "'\n", COLORIZE(COLOR_BRCYAN, 0, acf->name));
429
430 return 0;
431 }
432
433 int __ast_custom_function_register_escalating(struct ast_custom_function *acf, enum ast_custom_function_escalation escalation, struct ast_module *mod)
434 {
435 int res;
436
437 res = __ast_custom_function_register(acf, mod);
438 if (res != 0) {
439 return -1;
440 }
441
442 switch (escalation) {
443 case AST_CFE_NONE:
444 break;
445 case AST_CFE_READ:
446 acf->read_escalates = 1;
447 break;
448 case AST_CFE_WRITE:
449 acf->write_escalates = 1;
450 break;
451 case AST_CFE_BOTH:
452 acf->read_escalates = 1;
453 acf->write_escalates = 1;
454 break;
455 }
456
457 return 0;
458 }
459
460 /*! \brief return a pointer to the arguments of the function,
461 * and terminates the function name with '\\0'
462 */
463 static char *func_args(char *function)
464 {
465 char *args = strchr(function, '(');
466
467 if (!args) {
468 ast_log(LOG_WARNING, "Function '%s' doesn't contain parentheses. Assuming null argument.\n", function);
469 } else {
470 char *p;
471 *args++ = '\0';
472 if ((p = strrchr(args, ')'))) {
473 *p = '\0';
474 } else {
475 ast_log(LOG_WARNING, "Can't find trailing parenthesis for function '%s(%s'?\n", function, args);
476 }
477 }
478 return args;
479 }
480
481 void pbx_live_dangerously(int new_live_dangerously)
482 {
483 if (new_live_dangerously && !live_dangerously) {
484 ast_log(LOG_WARNING, "Privilege escalation protection disabled!\n"
485 "See https://docs.asterisk.org/Configuration/Dialplan/Privilege-Escalations-with-Dialplan-Functions/ for more details.\n");
486 }
487
488 if (!new_live_dangerously && live_dangerously) {
489 ast_log(LOG_NOTICE, "Privilege escalation protection enabled.\n");
490 }
491 live_dangerously = new_live_dangerously;
492 }
493
494 int ast_thread_inhibit_escalations(void)
495 {
496 int *thread_inhibit_escalations;
497
498 thread_inhibit_escalations = ast_threadstorage_get(
499 &thread_inhibit_escalations_tl, sizeof(*thread_inhibit_escalations));
500 if (thread_inhibit_escalations == NULL) {
501 ast_log(LOG_ERROR, "Error inhibiting privilege escalations for current thread\n");
502 return -1;
503 }
504
505 *thread_inhibit_escalations = 1;
506 return 0;
507 }
508
509 int ast_thread_inhibit_escalations_swap(int inhibit)
510 {
511 int *thread_inhibit_escalations;
512 int orig;
513
514 thread_inhibit_escalations = ast_threadstorage_get(
515 &thread_inhibit_escalations_tl, sizeof(*thread_inhibit_escalations));
516 if (thread_inhibit_escalations == NULL) {
517 ast_log(LOG_ERROR, "Error swapping privilege escalations inhibit for current thread\n");
518 return -1;
519 }
520
521 orig = *thread_inhibit_escalations;
522 *thread_inhibit_escalations = !!inhibit;
523 return orig;
524 }
525
526 /*!
527 * \brief Indicates whether the current thread inhibits the execution of
528 * dangerous functions.
529 *
530 * \return True (non-zero) if dangerous function execution is inhibited.
531 * \return False (zero) if dangerous function execution is allowed.
532 */
533 static int thread_inhibits_escalations(void)
534 {
535 int *thread_inhibit_escalations;
536
537 thread_inhibit_escalations = ast_threadstorage_get(
538 &thread_inhibit_escalations_tl, sizeof(*thread_inhibit_escalations));
539 if (thread_inhibit_escalations == NULL) {
540 ast_log(LOG_ERROR, "Error checking thread's ability to run dangerous functions\n");
541 /* On error, assume that we are inhibiting */
542 return 1;
543 }
544
545 return *thread_inhibit_escalations;
546 }
547
548 /*!
549 * \brief Determines whether execution of a custom function's read function
550 * is allowed.
551 *
552 * \param acfptr Custom function to check
553 * \return True (non-zero) if reading is allowed.
554 * \return False (zero) if reading is not allowed.
555 */
556 static int is_read_allowed(struct ast_custom_function *acfptr)
557 {
558 if (!acfptr) {
559 return 1;
560 }
561
562 if (!read_escalates(acfptr)) {
563 return 1;
564 }
565
566 if (!thread_inhibits_escalations()) {
567 return 1;
568 }
569
570 if (live_dangerously) {
571 /* Global setting overrides the thread's preference */
572 ast_debug(2, "Reading %s from a dangerous context\n",
573 acfptr->name);
574 return 1;
575 }
576
577 /* We have no reason to allow this function to execute */
578 return 0;
579 }
580
581 /*!
582 * \brief Determines whether execution of a custom function's write function
583 * is allowed.
584 *
585 * \param acfptr Custom function to check
586 * \return True (non-zero) if writing is allowed.
587 * \return False (zero) if writing is not allowed.
588 */
589 static int is_write_allowed(struct ast_custom_function *acfptr)
590 {
591 if (!acfptr) {
592 return 1;
593 }
594
595 if (!write_escalates(acfptr)) {
596 return 1;
597 }
598
599 if (!thread_inhibits_escalations()) {
600 return 1;
601 }
602
603 if (live_dangerously) {
604 /* Global setting overrides the thread's preference */
605 ast_debug(2, "Writing %s from a dangerous context\n",
606 acfptr->name);
607 return 1;
608 }
609
610 /* We have no reason to allow this function to execute */
611 return 0;
612 }
613
614 int ast_func_read(struct ast_channel *chan, const char *function, char *workspace, size_t len)
615 {
616 char *copy = ast_strdupa(function);
617 char *args = func_args(copy);
618 struct ast_custom_function *acfptr = ast_custom_function_find(copy);
619 int res;
620 struct ast_module_user *u = NULL;
621 /*
622 * The module pointer needs to be saved because some modules, notably func_odbc,
623 * dynamically create and destroy functions so the acfptr might not be valid
624 * when the read callback returns.
625 *
626 * NEVER ASSUME THE acfptr IS STILL VALID AFTER THE CALLBACK RETURNS!
627 */
628 struct ast_module *mod = acfptr ? acfptr->mod : NULL;
629
630 if (acfptr == NULL) {
631 ast_log(LOG_ERROR, "Function %s not registered\n", copy);
632 } else if (!acfptr->read && !acfptr->read2) {
633 ast_log(LOG_ERROR, "Function %s cannot be read\n", copy);
634 } else if (!is_read_allowed(acfptr)) {
635 ast_log(LOG_ERROR, "Dangerous function %s read blocked\n", copy);
636 } else if (acfptr->read) {
637 if (mod) {
638 u = __ast_module_user_add(mod, chan);
639 }
640 res = acfptr->read(chan, copy, args, workspace, len);
641 if (mod && u) {
642 __ast_module_user_remove(mod, u);
643 }
644
645 return res;
646 } else {
647 struct ast_str *str = ast_str_create(16);
648
649 if (mod) {
650 u = __ast_module_user_add(mod, chan);
651 }
652 res = acfptr->read2(chan, copy, args, &str, 0);
653 if (mod && u) {
654 __ast_module_user_remove(mod, u);
655 }
656 ast_copy_string(workspace, ast_str_buffer(str), len > ast_str_size(str) ? ast_str_size(str) : len);
657 ast_free(str);
658
659 return res;
660 }
661
662 return -1;
663 }
664
665 int ast_func_read2(struct ast_channel *chan, const char *function, struct ast_str **str, ssize_t maxlen)
666 {
667 char *copy = ast_strdupa(function);
668 char *args = func_args(copy);
669 struct ast_custom_function *acfptr = ast_custom_function_find(copy);
670 int res;
671 struct ast_module_user *u = NULL;
672 /*
673 * The module pointer needs to be saved because some modules, notably func_odbc,
674 * dynamically create and destroy functions so the acfptr might not be valid
675 * when the read callback returns.
676 *
677 * NEVER ASSUME THE acfptr IS STILL VALID AFTER THE CALLBACK RETURNS!
678 */
679 struct ast_module *mod = acfptr ? acfptr->mod : NULL;
680
681 if (acfptr == NULL) {
682 ast_log(LOG_ERROR, "Function %s not registered\n", copy);
683 } else if (!acfptr->read && !acfptr->read2) {
684 ast_log(LOG_ERROR, "Function %s cannot be read\n", copy);
685 } else if (!is_read_allowed(acfptr)) {
686 ast_log(LOG_ERROR, "Dangerous function %s read blocked\n", copy);
687 } else {
688 if (mod) {
689 u = __ast_module_user_add(mod, chan);
690 }
691 ast_str_reset(*str);
692 if (acfptr->read2) {
693 /* ast_str enabled */
694 res = acfptr->read2(chan, copy, args, str, maxlen);
695 } else {
696 /* Legacy function pointer, allocate buffer for result */
697 int maxsize = ast_str_size(*str);
698
699 if (maxlen > -1) {
700 if (maxlen == 0) {
701 if (acfptr->read_max) {
702 maxsize = acfptr->read_max;
703 } else {
704 maxsize = VAR_BUF_SIZE;
705 }
706 } else {
707 maxsize = maxlen;
708 }
709 ast_str_make_space(str, maxsize);
710 }
711 res = acfptr->read(chan, copy, args, ast_str_buffer(*str), maxsize);
712 ast_str_update(*str); /* Manually set the string length */
713 }
714 if (mod && u) {
715 __ast_module_user_remove(mod, u);
716 }
717
718 return res;
719 }
720
721 return -1;
722 }
723
724 int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
725 {
726 char *copy = ast_strdupa(function);
727 char *args = func_args(copy);
728 struct ast_custom_function *acfptr = ast_custom_function_find(copy);
729 /*
730 * The module pointer needs to be saved because some modules, notably func_odbc,
731 * dynamically create and destroy functions so the acfptr might not be valid
732 * when the write callback returns.
733 *
734 * NEVER ASSUME THE acfptr IS STILL VALID AFTER THE CALLBACK RETURNS!
735 */
736 struct ast_module *mod = acfptr ? acfptr->mod : NULL;
737
738 if (acfptr == NULL) {
739 ast_log(LOG_ERROR, "Function %s not registered\n", copy);
740 } else if (!acfptr->write) {
741 ast_log(LOG_ERROR, "Function %s cannot be written to\n", copy);
742 } else if (!is_write_allowed(acfptr)) {
743 ast_log(LOG_ERROR, "Dangerous function %s write blocked\n", copy);
744 } else {
745 int res;
746 struct ast_module_user *u = NULL;
747
748 if (mod) {
749 u = __ast_module_user_add(mod, chan);
750 }
751 res = acfptr->write(chan, copy, args, value);
752 if (mod && u) {
753 __ast_module_user_remove(mod, u);
754 }
755
756 return res;
757 }
758
759 return -1;
760 }
761
762 static struct ast_cli_entry acf_cli[] = {
763 AST_CLI_DEFINE(handle_show_functions, "Shows registered dialplan functions"),
764 AST_CLI_DEFINE(handle_show_function, "Describe a specific dialplan function"),
765 };
766
767 static void unload_pbx_functions_cli(void)
768 {
769 ast_cli_unregister_multiple(acf_cli, ARRAY_LEN(acf_cli));
770 }
771
772 int load_pbx_functions_cli(void)
773 {
774 ast_cli_register_multiple(acf_cli, ARRAY_LEN(acf_cli));
775 ast_register_cleanup(unload_pbx_functions_cli);
776
777 return 0;
778 }