Mon May 14 04:42:56 2007

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  * \ingroup functions
00027  */
00028 
00029 #include "asterisk.h"
00030 
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <sys/types.h>
00037 
00038 #include "asterisk/module.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/utils.h"
00043 #include "asterisk/app.h"
00044 
00045 static int md5(struct ast_channel *chan, char *cmd, char *data,
00046           char *buf, size_t len)
00047 {
00048    if (ast_strlen_zero(data)) {
00049       ast_log(LOG_WARNING, "Syntax: MD5(<data>) - missing argument!\n");
00050       return -1;
00051    }
00052 
00053    ast_md5_hash(buf, data);
00054    buf[32] = '\0';
00055 
00056    return 0;
00057 }
00058 
00059 static int checkmd5(struct ast_channel *chan, char *cmd, char *parse,
00060           char *buf, size_t len)
00061 {
00062    char newmd5[33];
00063    static int deprecated = 0;
00064    AST_DECLARE_APP_ARGS(args, AST_APP_ARG(digest); AST_APP_ARG(data););
00065 
00066    if (ast_strlen_zero(parse)) {
00067       ast_log(LOG_WARNING,
00068             "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
00069       return -1;
00070    }
00071 
00072    AST_STANDARD_APP_ARGS(args, parse);
00073 
00074    if (args.argc < 2) {
00075       ast_log(LOG_WARNING,
00076             "Syntax: CHECK_MD5(<digest>,<data>) - missing argument!\n");
00077       return -1;
00078    }
00079 
00080    if (!deprecated) {
00081       deprecated = 1;
00082       ast_log(LOG_WARNING, "CHECK_MD5() is deprecated in Asterisk 1.4 and later.\n");
00083    }
00084 
00085    ast_md5_hash(newmd5, args.data);
00086 
00087    if (!strcasecmp(newmd5, args.digest))  /* they match */
00088       ast_copy_string(buf, "1", len);
00089    else
00090       ast_copy_string(buf, "0", len);
00091 
00092    return 0;
00093 }
00094 
00095 static struct ast_custom_function md5_function = {
00096    .name = "MD5",
00097    .synopsis = "Computes an MD5 digest",
00098    .syntax = "MD5(<data>)",
00099    .read = md5,
00100 };
00101 
00102 static struct ast_custom_function checkmd5_function = {
00103    .name = "CHECK_MD5",
00104    .synopsis = "Checks an MD5 digest",
00105    .desc = "Returns 1 on a match, 0 otherwise\n",
00106    .syntax = "CHECK_MD5(<digest>,<data>)",
00107    .read = checkmd5,
00108 };
00109 
00110 static int unload_module(void)
00111 {
00112    return ast_custom_function_unregister(&md5_function) |
00113       ast_custom_function_unregister(&checkmd5_function);
00114 }
00115 
00116 static int load_module(void)
00117 {
00118    return ast_custom_function_register(&md5_function) |
00119       ast_custom_function_register(&checkmd5_function);
00120 }
00121 
00122 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MD5 digest dialplan functions");

Generated on Mon May 14 04:42:56 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1