Sat Aug 6 00:39:29 2011

Asterisk developer's documentation


func_md5.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005-2006, Digium, Inc.
00005  * Copyright (C) 2005, Olle E. Johansson, Edvina.net
00006  * Copyright (C) 2005, Russell Bryant <russelb@clemson.edu> 
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief MD5 digest related dialplan functions
00022  * 
00023  * \author Olle E. Johansson <oej@edvina.net>
00024  * \author Russell Bryant <russelb@clemson.edu>
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 40722 $")
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <sys/types.h>
00035 
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/utils.h"
00041 #include "asterisk/app.h"
00042 
00043 static int md5(struct ast_channel *chan, char *cmd, char *data,
00044           char *buf, size_t len)
00045 {
00046    if (ast_strlen_zero(data)) {
00047       ast_log(LOG_WARNING, "Syntax: MD5(<data>) - missing argument!\n");
00048       return -1;
00049    }
00050 
00051    ast_md5_hash(buf, data);
00052    buf[32] = '\0';
00053 
00054    return 0;
00055 }
00056 
00057 static int checkmd5(struct ast_channel *chan, char *cmd, char *parse,
00058           char *buf, size_t len)
00059 {
00060    char newmd5[33];
00061    static int deprecated = 0;
00062    AST_DECLARE_APP_ARGS(args, AST_APP_ARG(digest); AST_APP_ARG(data););
00063 
00064    if (ast_strlen_zero(parse)) {
00065       ast_log(LOG_WARNING,
00066             "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
00067       return -1;
00068    }
00069 
00070    AST_STANDARD_APP_ARGS(args, parse);
00071 
00072    if (args.argc < 2) {
00073       ast_log(LOG_WARNING,
00074             "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
00075       return -1;
00076    }
00077 
00078    if (!deprecated) {
00079       deprecated = 1;
00080       ast_log(LOG_WARNING, "CHECK_MD5() is deprecated in Asterisk 1.4 and later.\n");
00081    }
00082 
00083    ast_md5_hash(newmd5, args.data);
00084 
00085    if (!strcasecmp(newmd5, args.digest))  /* they match */
00086       ast_copy_string(buf, "1", len);
00087    else
00088       ast_copy_string(buf, "0", len);
00089 
00090    return 0;
00091 }
00092 
00093 static struct ast_custom_function md5_function = {
00094    .name = "MD5",
00095    .synopsis = "Computes an MD5 digest",
00096    .syntax = "MD5(<data>)",
00097    .read = md5,
00098 };
00099 
00100 static struct ast_custom_function checkmd5_function = {
00101    .name = "CHECK_MD5",
00102    .synopsis = "Checks an MD5 digest",
00103    .desc = "Returns 1 on a match, 0 otherwise\n",
00104    .syntax = "CHECK_MD5(<digest>,<data>)",
00105    .read = checkmd5,
00106 };
00107 
00108 static int unload_module(void)
00109 {
00110    return ast_custom_function_unregister(&md5_function) |
00111       ast_custom_function_unregister(&checkmd5_function);
00112 }
00113 
00114 static int load_module(void)
00115 {
00116    return ast_custom_function_register(&md5_function) |
00117       ast_custom_function_register(&checkmd5_function);
00118 }
00119 
00120 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MD5 digest dialplan functions");

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7