From ce9a0b7459733b1c74d196f6076fdd77858596a9 Mon Sep 17 00:00:00 2001 From: kpfleming Date: Wed, 4 May 2005 04:05:34 +0000 Subject: add MD5 and CHECK_MD5 functions, deprecate MD5 and MD5Check apps (bug #4123) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@5567 f38db490-d61c-443f-a65b-d21fe96a405b --- pbx.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'pbx.c') diff --git a/pbx.c b/pbx.c index bf9db3d0a..311ab075f 100755 --- a/pbx.c +++ b/pbx.c @@ -1370,6 +1370,51 @@ static char *builtin_function_regex(struct ast_channel *chan, char *cmd, char *d return ret; } +static char *builtin_function_md5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) +{ + char md5[33]; + + if (!data || ast_strlen_zero(data)) { + ast_log(LOG_WARNING, "Syntax: MD5() - missing argument!\n"); + return NULL; + } + + ast_md5_hash(md5, data); + ast_copy_string(buf, md5, len); + + return buf; +} + +static char *builtin_function_checkmd5(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) +{ + int argc; + char *argv[2]; + char *args; + char newmd5[33]; + + if (!data || ast_strlen_zero(data)) { + ast_log(LOG_WARNING, "Syntax: CHECK_MD5(,) - missing argument!\n"); + return NULL; + } + + args = ast_strdupa(data); + argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])); + + if (argc < 2) { + ast_log(LOG_WARNING, "Syntax: CHECK_MD5(,) - missing argument!\n"); + return NULL; + } + + ast_md5_hash(newmd5, argv[1]); + + if (!strcasecmp(newmd5, argv[0])) /* they match */ + ast_copy_string(buf, "1", len); + else + ast_copy_string(buf, "0", len); + + return buf; +} + static void pbx_substitute_variables_helper_full(struct ast_channel *c, const char *cp1, char *cp2, int count, struct varshead *headp) { char *cp4; @@ -3554,6 +3599,21 @@ static struct ast_custom_function_obj cdr_function = { .write = builtin_function_cdr_write, }; +static struct ast_custom_function_obj md5_function = { + .name = "MD5", + .desc = "Computes an MD5 digest", + .syntax = "MD5()", + .read = builtin_function_md5, + .write = NULL, +}; + +static struct ast_custom_function_obj checkmd5_function = { + .name = "CHECK_MD5", + .desc = "Checks an MD5 digest. Returns 1 on a match, 0 otherwise", + .syntax = "CHECK_MD5(,)", + .read = builtin_function_checkmd5, + .write = NULL, +}; /* * CLI entries for upper commands ... @@ -6000,6 +6060,8 @@ int load_pbx(void) ast_custom_function_register(&env_function); ast_custom_function_register(&len_function); ast_custom_function_register(&cdr_function); + ast_custom_function_register(&md5_function); + ast_custom_function_register(&checkmd5_function); /* Register builtin applications */ for (x=0; x