app_md5.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005, Olle E. Johansson, Edvina.net
  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. /*! \file
  17. *
  18. * \brief MD5 checksum application
  19. *
  20. * \todo Remove this deprecated application in 1.3dev
  21. * \ingroup applications
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/file.h"
  30. #include "asterisk/logger.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/options.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/app.h"
  38. static char *tdesc_md5 = "MD5 checksum applications";
  39. static char *app_md5 = "MD5";
  40. static char *desc_md5 = "Calculate MD5 checksum";
  41. static char *synopsis_md5 =
  42. " MD5(<var>=<string>): Calculates a MD5 checksum on <string>.\n"
  43. "Returns hash value in a channel variable. \n";
  44. static char *app_md5check = "MD5Check";
  45. static char *desc_md5check = "Check MD5 checksum";
  46. static char *synopsis_md5check =
  47. " MD5Check(<md5hash>|<string>[|options]): Calculates a MD5 checksum on <string>\n"
  48. "and compares it with the hash. Returns 0 if <md5hash> is correct for <string>.\n"
  49. "The option string may contain zero or more of the following characters:\n"
  50. " 'j' -- jump to priority n+101 if the hash and string do not match \n"
  51. "This application sets the following channel variable upon completion:\n"
  52. " CHECKMD5STATUS The status of the MD5 check, one of the following\n"
  53. " MATCH | NOMATCH\n";
  54. STANDARD_LOCAL_USER;
  55. LOCAL_USER_DECL;
  56. /*--- md5_exec: Calculate MD5 checksum (hash) on given string and
  57. return it in channel variable ---*/
  58. static int md5_exec(struct ast_channel *chan, void *data)
  59. {
  60. int res=0;
  61. struct localuser *u;
  62. char *varname= NULL; /* Variable to set */
  63. char *string = NULL; /* String to calculate on */
  64. char retvar[50]; /* Return value */
  65. static int dep_warning = 0;
  66. if (!dep_warning) {
  67. ast_log(LOG_WARNING, "This application has been deprecated, please use the MD5 function instead.\n");
  68. dep_warning = 1;
  69. }
  70. if (ast_strlen_zero(data)) {
  71. ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
  72. return -1;
  73. }
  74. LOCAL_USER_ADD(u);
  75. memset(retvar,0, sizeof(retvar));
  76. string = ast_strdupa(data);
  77. varname = strsep(&string,"=");
  78. if (ast_strlen_zero(varname)) {
  79. ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
  80. LOCAL_USER_REMOVE(u);
  81. return -1;
  82. }
  83. ast_md5_hash(retvar, string);
  84. pbx_builtin_setvar_helper(chan, varname, retvar);
  85. LOCAL_USER_REMOVE(u);
  86. return res;
  87. }
  88. /*--- md5check_exec: Calculate MD5 checksum and compare it with
  89. existing checksum. ---*/
  90. static int md5check_exec(struct ast_channel *chan, void *data)
  91. {
  92. int res=0;
  93. struct localuser *u;
  94. char *string = NULL; /* String to calculate on */
  95. char newhash[50]; /* Return value */
  96. static int dep_warning = 0;
  97. int priority_jump = 0;
  98. AST_DECLARE_APP_ARGS(args,
  99. AST_APP_ARG(md5hash);
  100. AST_APP_ARG(string);
  101. AST_APP_ARG(options);
  102. );
  103. if (!dep_warning) {
  104. ast_log(LOG_WARNING, "This application has been deprecated, please use the CHECK_MD5 function instead.\n");
  105. dep_warning = 1;
  106. }
  107. LOCAL_USER_ADD(u);
  108. if (!(string = ast_strdupa(data))) {
  109. ast_log(LOG_WARNING, "Memory Error!\n");
  110. LOCAL_USER_REMOVE(u);
  111. return -1;
  112. }
  113. AST_STANDARD_APP_ARGS(args, string);
  114. if (args.options) {
  115. if (strchr(args.options, 'j'))
  116. priority_jump = 1;
  117. }
  118. if (ast_strlen_zero(args.md5hash) || ast_strlen_zero(args.string)) {
  119. ast_log(LOG_WARNING, "Syntax: MD5Check(<md5hash>|<string>[|options]) - missing argument!\n");
  120. LOCAL_USER_REMOVE(u);
  121. return -1;
  122. }
  123. memset(newhash,0, sizeof(newhash));
  124. ast_md5_hash(newhash, args.string);
  125. if (!strcmp(newhash, args.md5hash)) { /* Verification ok */
  126. if (option_debug > 2)
  127. ast_log(LOG_DEBUG, "MD5 verified ok: %s -- %s\n", args.md5hash, args.string);
  128. pbx_builtin_setvar_helper(chan, "CHECKMD5STATUS", "MATCH");
  129. LOCAL_USER_REMOVE(u);
  130. return 0;
  131. }
  132. if (option_debug > 2)
  133. ast_log(LOG_DEBUG, "ERROR: MD5 not verified: %s -- %s\n", args.md5hash, args.string);
  134. pbx_builtin_setvar_helper(chan, "CHECKMD5STATUS", "NOMATCH");
  135. if (priority_jump || option_priority_jumping) {
  136. if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101))
  137. if (option_debug > 2)
  138. ast_log(LOG_DEBUG, "Can't jump to exten+101 (e%s,p%d), sorry\n", chan->exten,chan->priority+101);
  139. }
  140. LOCAL_USER_REMOVE(u);
  141. return res;
  142. }
  143. int unload_module(void)
  144. {
  145. int res;
  146. res = ast_unregister_application(app_md5);
  147. res |= ast_unregister_application(app_md5check);
  148. STANDARD_HANGUP_LOCALUSERS;
  149. return res;
  150. }
  151. int load_module(void)
  152. {
  153. int res;
  154. res = ast_register_application(app_md5check, md5check_exec, desc_md5check, synopsis_md5check);
  155. res |= ast_register_application(app_md5, md5_exec, desc_md5, synopsis_md5);
  156. return res;
  157. }
  158. char *description(void)
  159. {
  160. return tdesc_md5;
  161. }
  162. int usecount(void)
  163. {
  164. int res;
  165. STANDARD_USECOUNT(res);
  166. return res;
  167. }
  168. char *key()
  169. {
  170. return ASTERISK_GPL_KEY;
  171. }