func_md5.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005, Digium, Inc.
  5. * Copyright (C) 2005, Olle E. Johansson, Edvina.net
  6. * Copyright (C) 2005, Russell Bryant <russelb@clemson.edu>
  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. /*! \file
  19. *
  20. * \brief MD5 digest related dialplan functions
  21. *
  22. */
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include "asterisk.h"
  27. /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
  28. #include "asterisk/channel.h"
  29. #include "asterisk/pbx.h"
  30. #include "asterisk/logger.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/app.h"
  33. static char *builtin_function_md5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  34. {
  35. char md5[33];
  36. if (ast_strlen_zero(data)) {
  37. ast_log(LOG_WARNING, "Syntax: MD5(<data>) - missing argument!\n");
  38. return NULL;
  39. }
  40. ast_md5_hash(md5, data);
  41. ast_copy_string(buf, md5, len);
  42. return buf;
  43. }
  44. static char *builtin_function_checkmd5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  45. {
  46. int argc;
  47. char *argv[2];
  48. char *args;
  49. char newmd5[33];
  50. if (!data || ast_strlen_zero(data)) {
  51. ast_log(LOG_WARNING, "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
  52. return NULL;
  53. }
  54. args = ast_strdupa(data);
  55. argc = ast_app_separate_args(args, '|', argv, sizeof(argv) / sizeof(argv[0]));
  56. if (argc < 2) {
  57. ast_log(LOG_WARNING, "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
  58. return NULL;
  59. }
  60. ast_md5_hash(newmd5, argv[1]);
  61. if (!strcasecmp(newmd5, argv[0])) /* they match */
  62. ast_copy_string(buf, "1", len);
  63. else
  64. ast_copy_string(buf, "0", len);
  65. return buf;
  66. }
  67. #ifndef BUILTIN_FUNC
  68. static
  69. #endif
  70. struct ast_custom_function md5_function = {
  71. .name = "MD5",
  72. .synopsis = "Computes an MD5 digest",
  73. .syntax = "MD5(<data>)",
  74. .read = builtin_function_md5,
  75. };
  76. #ifndef BUILTIN_FUNC
  77. static
  78. #endif
  79. struct ast_custom_function checkmd5_function = {
  80. .name = "CHECK_MD5",
  81. .synopsis = "Checks an MD5 digest",
  82. .desc = "Returns 1 on a match, 0 otherwise\n",
  83. .syntax = "CHECK_MD5(<digest>,<data>)",
  84. .read = builtin_function_checkmd5,
  85. };