Fri Sep 29 11:12:23 2006

Asterisk developer's documentation


app_md5.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005, Olle E. Johansson, Edvina.net
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 
00018 /*! \file
00019  *
00020  * \brief MD5 checksum application
00021  * 
00022  * \todo Remove this deprecated application in 1.3dev
00023  * \ingroup applications
00024  */
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <unistd.h>
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00034 
00035 #include "asterisk/file.h"
00036 #include "asterisk/logger.h"
00037 #include "asterisk/utils.h"
00038 #include "asterisk/options.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/module.h"
00042 #include "asterisk/lock.h"
00043 #include "asterisk/app.h"
00044 
00045 static char *tdesc_md5 = "MD5 checksum applications";
00046 static char *app_md5 = "MD5";
00047 static char *desc_md5 = "Calculate MD5 checksum";
00048 static char *synopsis_md5 = 
00049 "  MD5(<var>=<string>): Calculates a MD5 checksum on <string>.\n"
00050 "Returns hash value in a channel variable. \n";
00051 
00052 static char *app_md5check = "MD5Check";
00053 static char *desc_md5check = "Check MD5 checksum";
00054 static char *synopsis_md5check = 
00055 "  MD5Check(<md5hash>|<string>[|options]): Calculates a MD5 checksum on <string>\n"
00056 "and compares it with the hash. Returns 0 if <md5hash> is correct for <string>.\n"
00057 "The option string may contain zero or more of the following characters:\n"
00058 "  'j' -- jump to priority n+101 if the hash and string do not match \n"
00059 "This application sets the following channel variable upon completion:\n"
00060 "  CHECKMD5STATUS The status of the MD5 check, one of the following\n"
00061 "     MATCH | NOMATCH\n";
00062 
00063 STANDARD_LOCAL_USER;
00064 
00065 LOCAL_USER_DECL;
00066 
00067 /*--- md5_exec: Calculate MD5 checksum (hash) on given string and
00068    return it in channel variable ---*/
00069 static int md5_exec(struct ast_channel *chan, void *data)
00070 {
00071    int res=0;
00072    struct localuser *u;
00073    char *varname= NULL; /* Variable to set */
00074    char *string = NULL; /* String to calculate on */
00075    char retvar[50]; /* Return value */
00076    static int dep_warning = 0;
00077 
00078    if (!dep_warning) {
00079       ast_log(LOG_WARNING, "This application has been deprecated, please use the MD5 function instead.\n");
00080       dep_warning = 1;
00081    }  
00082 
00083    if (ast_strlen_zero(data)) {
00084       ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
00085       return -1;
00086    }
00087    
00088    LOCAL_USER_ADD(u);
00089 
00090    memset(retvar,0, sizeof(retvar));
00091    string = ast_strdupa(data);
00092    varname = strsep(&string,"=");
00093    if (ast_strlen_zero(varname)) {
00094       ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
00095       LOCAL_USER_REMOVE(u);
00096       return -1;
00097    }
00098    ast_md5_hash(retvar, string);
00099    pbx_builtin_setvar_helper(chan, varname, retvar);
00100    LOCAL_USER_REMOVE(u);
00101    return res;
00102 }
00103 
00104 /*--- md5check_exec: Calculate MD5 checksum and compare it with
00105    existing checksum. ---*/
00106 static int md5check_exec(struct ast_channel *chan, void *data)
00107 {
00108    int res=0;
00109    struct localuser *u;
00110    char *string = NULL; /* String to calculate on */
00111    char newhash[50]; /* Return value */
00112    static int dep_warning = 0;
00113    int priority_jump = 0;
00114    AST_DECLARE_APP_ARGS(args,
00115       AST_APP_ARG(md5hash);
00116       AST_APP_ARG(string);
00117       AST_APP_ARG(options);
00118    );
00119 
00120    if (!dep_warning) {
00121       ast_log(LOG_WARNING, "This application has been deprecated, please use the CHECK_MD5 function instead.\n");
00122       dep_warning = 1;
00123    }
00124    
00125    LOCAL_USER_ADD(u);
00126 
00127    if (!(string = ast_strdupa(data))) {
00128       ast_log(LOG_WARNING, "Memory Error!\n");
00129       LOCAL_USER_REMOVE(u);
00130       return -1;
00131    }
00132 
00133    AST_STANDARD_APP_ARGS(args, string);
00134 
00135    if (args.options) {
00136       if (strchr(args.options, 'j'))
00137          priority_jump = 1;
00138    }
00139 
00140    if (ast_strlen_zero(args.md5hash) || ast_strlen_zero(args.string)) {
00141       ast_log(LOG_WARNING, "Syntax: MD5Check(<md5hash>|<string>[|options]) - missing argument!\n");
00142       LOCAL_USER_REMOVE(u);
00143       return -1;
00144    }
00145 
00146    memset(newhash,0, sizeof(newhash));
00147 
00148    ast_md5_hash(newhash, args.string);
00149    if (!strcmp(newhash, args.md5hash)) {  /* Verification ok */
00150       if (option_debug > 2)
00151          ast_log(LOG_DEBUG, "MD5 verified ok: %s -- %s\n", args.md5hash, args.string);
00152       pbx_builtin_setvar_helper(chan, "CHECKMD5STATUS", "MATCH");
00153       LOCAL_USER_REMOVE(u);
00154       return 0;
00155    }
00156    if (option_debug > 2)
00157       ast_log(LOG_DEBUG, "ERROR: MD5 not verified: %s -- %s\n", args.md5hash, args.string);
00158    pbx_builtin_setvar_helper(chan, "CHECKMD5STATUS", "NOMATCH");     
00159    if (priority_jump || option_priority_jumping) {
00160       if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101))
00161          if (option_debug > 2)
00162             ast_log(LOG_DEBUG, "Can't jump to exten+101 (e%s,p%d), sorry\n", chan->exten,chan->priority+101);
00163    }
00164    LOCAL_USER_REMOVE(u);
00165    return res;
00166 }
00167 
00168 int unload_module(void)
00169 {
00170    int res;
00171 
00172    res = ast_unregister_application(app_md5);
00173    res |= ast_unregister_application(app_md5check);
00174 
00175    STANDARD_HANGUP_LOCALUSERS;
00176 
00177    return res;
00178 }
00179 
00180 int load_module(void)
00181 {
00182    int res;
00183 
00184    res = ast_register_application(app_md5check, md5check_exec, desc_md5check, synopsis_md5check);
00185    res |= ast_register_application(app_md5, md5_exec, desc_md5, synopsis_md5);
00186    
00187    return res;
00188 }
00189 
00190 char *description(void)
00191 {
00192    return tdesc_md5;
00193 }
00194 
00195 int usecount(void)
00196 {
00197    int res;
00198    STANDARD_USECOUNT(res);
00199    return res;
00200 }
00201 
00202 char *key()
00203 {
00204    return ASTERISK_GPL_KEY;
00205 }

Generated on Fri Sep 29 11:12:23 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.7