2 * Asterisk -- A telephony toolkit for Linux.
4 * MD5 checksum application
6 * Copyright (C) 2005, Olle E. Johansson, Edvina.net
8 * This program is free software, distributed under the terms of
9 * the GNU General Public License
12 #include <asterisk/file.h>
13 #include <asterisk/logger.h>
14 #include <asterisk/utils.h>
15 #include <asterisk/options.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/lock.h>
24 static char *tdesc_md5 = "MD5 checksum application";
25 static char *app_md5 = "MD5";
26 static char *synopsis_md5 =
27 " MD5(<var>=<string>): Calculates a MD5 checksum on <string>.\n"
28 "Returns hash value in a channel variable. Always return 0\n";
30 static char *tdesc_md5check = "MD5 checksum verification application";
31 static char *app_md5check = "MD5Check";
32 static char *synopsis_md5check =
33 " MD5Check(<md5hash>,<string>): Calculates a MD5 checksum on <string>\n"
34 "and compares it with the hash. Returns 0 if <md5hash> is correct for <string>.\n"
35 "Jumps to priority+101 if incorrect.\n";
41 /*--- md5_exec: Calculate MD5 checksum (hash) on given string and
42 return it in channel variable ---*/
43 static int md5_exec(struct ast_channel *chan, void *data)
47 char *varname= NULL; /* Variable to set */
48 char *string = NULL; /* String to calculate on */
49 char retvar[50]; /* Return value */
52 ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
56 memset(retvar,0, sizeof(retvar));
57 string = ast_strdupa(data);
58 varname = strsep(&string,"=");
59 if (ast_strlen_zero(varname)) {
60 ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
64 ast_md5_hash(retvar, string);
65 pbx_builtin_setvar_helper(chan, varname, retvar);
70 /*--- md5check_exec: Calculate MD5 checksum and compare it with
71 existing checksum. ---*/
72 static int md5check_exec(struct ast_channel *chan, void *data)
76 char *hash= NULL; /* Hash to compare with */
77 char *string = NULL; /* String to calculate on */
78 char newhash[50]; /* Return value */
81 ast_log(LOG_WARNING, "Syntax: MD5Check(<md5hash>,<string>) - missing argument!\n");
85 memset(newhash,0, sizeof(newhash));
87 string = ast_strdupa(data);
88 hash = strsep(&string,"|");
89 if (ast_strlen_zero(hash)) {
90 ast_log(LOG_WARNING, "Syntax: MD5Check(<md5hash>,<string>) - missing argument!\n");
94 ast_md5_hash(newhash, string);
95 if (!strcmp(newhash, hash)) { /* Verification ok */
97 ast_log(LOG_DEBUG, "MD5 verified ok: %s -- %s\n", hash, string);
101 if (option_debug > 2)
102 ast_log(LOG_DEBUG, "ERROR: MD5 not verified: %s -- %s\n", hash, string);
103 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
104 chan->priority += 100;
105 else if (option_debug > 2)
106 ast_log(LOG_DEBUG, "ERROR: Can't jump to exten+101 (e%s,p%d), sorry\n", chan->exten,chan->priority+101);
107 LOCAL_USER_REMOVE(u);
111 int unload_module(void)
115 STANDARD_HANGUP_LOCALUSERS;
116 res =ast_unregister_application(app_md5);
117 res |= ast_unregister_application(app_md5check);
121 int load_module(void)
125 res = ast_register_application(app_md5check, md5check_exec, synopsis_md5check, tdesc_md5check);
126 res |= ast_register_application(app_md5, md5_exec, synopsis_md5, tdesc_md5);
130 char *description(void)
138 STANDARD_USECOUNT(res);
144 return ASTERISK_GPL_KEY;