2005-02-18 05:29:31 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- A telephony toolkit for Linux.
|
|
|
|
*
|
|
|
|
* MD5 checksum application
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005, Olle E. Johansson, Edvina.net
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License
|
|
|
|
*/
|
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/options.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/lock.h"
|
2005-02-18 05:29:31 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-03-12 05:37:32 +00:00
|
|
|
static char *tdesc_md5 = "MD5 checksum applications";
|
2005-02-19 16:18:19 +00:00
|
|
|
static char *app_md5 = "MD5";
|
2005-03-12 05:37:32 +00:00
|
|
|
static char *desc_md5 = "Calculate MD5 checksum";
|
2005-02-19 16:18:19 +00:00
|
|
|
static char *synopsis_md5 =
|
|
|
|
" MD5(<var>=<string>): Calculates a MD5 checksum on <string>.\n"
|
2005-02-18 05:29:31 +00:00
|
|
|
"Returns hash value in a channel variable. Always return 0\n";
|
|
|
|
|
2005-02-19 16:18:19 +00:00
|
|
|
static char *app_md5check = "MD5Check";
|
2005-03-12 05:37:32 +00:00
|
|
|
static char *desc_md5check = "Check MD5 checksum";
|
2005-02-19 16:18:19 +00:00
|
|
|
static char *synopsis_md5check =
|
|
|
|
" MD5Check(<md5hash>,<string>): Calculates a MD5 checksum on <string>\n"
|
|
|
|
"and compares it with the hash. Returns 0 if <md5hash> is correct for <string>.\n"
|
|
|
|
"Jumps to priority+101 if incorrect.\n";
|
|
|
|
|
2005-02-18 05:29:31 +00:00
|
|
|
STANDARD_LOCAL_USER;
|
|
|
|
|
|
|
|
LOCAL_USER_DECL;
|
|
|
|
|
2005-02-19 16:18:19 +00:00
|
|
|
/*--- md5_exec: Calculate MD5 checksum (hash) on given string and
|
|
|
|
return it in channel variable ---*/
|
2005-02-18 05:29:31 +00:00
|
|
|
static int md5_exec(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res=0;
|
|
|
|
struct localuser *u;
|
|
|
|
char *varname= NULL; /* Variable to set */
|
|
|
|
char *string = NULL; /* String to calculate on */
|
|
|
|
char retvar[50]; /* Return value */
|
2005-05-04 04:05:34 +00:00
|
|
|
static int dep_warning = 0;
|
|
|
|
|
|
|
|
if (!dep_warning) {
|
|
|
|
ast_log(LOG_WARNING, "This application has been deprecated, please use the MD5 function instead.\n");
|
|
|
|
dep_warning = 1;
|
|
|
|
}
|
2005-02-18 05:29:31 +00:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOCAL_USER_ADD(u);
|
|
|
|
memset(retvar,0, sizeof(retvar));
|
|
|
|
string = ast_strdupa(data);
|
|
|
|
varname = strsep(&string,"=");
|
|
|
|
if (ast_strlen_zero(varname)) {
|
|
|
|
ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
|
|
|
|
LOCAL_USER_REMOVE(u);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ast_md5_hash(retvar, string);
|
|
|
|
pbx_builtin_setvar_helper(chan, varname, retvar);
|
|
|
|
LOCAL_USER_REMOVE(u);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-02-19 16:18:19 +00:00
|
|
|
/*--- md5check_exec: Calculate MD5 checksum and compare it with
|
|
|
|
existing checksum. ---*/
|
|
|
|
static int md5check_exec(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res=0;
|
|
|
|
struct localuser *u;
|
|
|
|
char *hash= NULL; /* Hash to compare with */
|
|
|
|
char *string = NULL; /* String to calculate on */
|
|
|
|
char newhash[50]; /* Return value */
|
2005-05-04 04:05:34 +00:00
|
|
|
static int dep_warning = 0;
|
2005-02-19 16:18:19 +00:00
|
|
|
|
2005-05-04 04:05:34 +00:00
|
|
|
if (!dep_warning) {
|
|
|
|
ast_log(LOG_WARNING, "This application has been deprecated, please use the CHECK_MD5 function instead.\n");
|
|
|
|
dep_warning = 1;
|
|
|
|
}
|
|
|
|
|
2005-02-19 16:18:19 +00:00
|
|
|
if (!data) {
|
|
|
|
ast_log(LOG_WARNING, "Syntax: MD5Check(<md5hash>,<string>) - missing argument!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOCAL_USER_ADD(u);
|
|
|
|
memset(newhash,0, sizeof(newhash));
|
|
|
|
|
|
|
|
string = ast_strdupa(data);
|
|
|
|
hash = strsep(&string,"|");
|
|
|
|
if (ast_strlen_zero(hash)) {
|
|
|
|
ast_log(LOG_WARNING, "Syntax: MD5Check(<md5hash>,<string>) - missing argument!\n");
|
|
|
|
LOCAL_USER_REMOVE(u);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ast_md5_hash(newhash, string);
|
|
|
|
if (!strcmp(newhash, hash)) { /* Verification ok */
|
|
|
|
if (option_debug > 2)
|
|
|
|
ast_log(LOG_DEBUG, "MD5 verified ok: %s -- %s\n", hash, string);
|
|
|
|
LOCAL_USER_REMOVE(u);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (option_debug > 2)
|
|
|
|
ast_log(LOG_DEBUG, "ERROR: MD5 not verified: %s -- %s\n", hash, string);
|
|
|
|
if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
|
|
|
|
chan->priority += 100;
|
|
|
|
else if (option_debug > 2)
|
|
|
|
ast_log(LOG_DEBUG, "ERROR: Can't jump to exten+101 (e%s,p%d), sorry\n", chan->exten,chan->priority+101);
|
|
|
|
LOCAL_USER_REMOVE(u);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-02-18 05:29:31 +00:00
|
|
|
int unload_module(void)
|
|
|
|
{
|
2005-02-19 16:18:19 +00:00
|
|
|
int res;
|
|
|
|
|
2005-02-18 05:29:31 +00:00
|
|
|
STANDARD_HANGUP_LOCALUSERS;
|
2005-02-19 16:18:19 +00:00
|
|
|
res =ast_unregister_application(app_md5);
|
|
|
|
res |= ast_unregister_application(app_md5check);
|
|
|
|
return res;
|
2005-02-18 05:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int load_module(void)
|
|
|
|
{
|
2005-02-19 16:18:19 +00:00
|
|
|
int res;
|
|
|
|
|
2005-03-12 05:37:32 +00:00
|
|
|
res = ast_register_application(app_md5check, md5check_exec, desc_md5check, synopsis_md5check);
|
|
|
|
res |= ast_register_application(app_md5, md5_exec, desc_md5, synopsis_md5);
|
2005-02-19 16:18:19 +00:00
|
|
|
return res;
|
2005-02-18 05:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *description(void)
|
|
|
|
{
|
2005-02-19 16:18:19 +00:00
|
|
|
return tdesc_md5;
|
2005-02-18 05:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usecount(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
STANDARD_USECOUNT(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *key()
|
|
|
|
{
|
|
|
|
return ASTERISK_GPL_KEY;
|
|
|
|
}
|