Add checking capability to MD5 (bug #3619)
[asterisk/asterisk.git] / apps / app_md5.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * MD5 checksum application
5  * 
6  * Copyright (C) 2005, Olle E. Johansson, Edvina.net
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
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>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23
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";
29
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";
36
37 STANDARD_LOCAL_USER;
38
39 LOCAL_USER_DECL;
40
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)
44 {
45         int res=0;
46         struct localuser *u;
47         char *varname= NULL; /* Variable to set */
48         char *string = NULL; /* String to calculate on */
49         char retvar[50]; /* Return value */
50
51         if (!data) {
52                 ast_log(LOG_WARNING, "Syntax: md5(<varname>=<string>) - missing argument!\n");
53                 return -1;
54         }
55         LOCAL_USER_ADD(u);
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");
61                 LOCAL_USER_REMOVE(u);
62                 return -1;
63         }
64         ast_md5_hash(retvar, string);
65         pbx_builtin_setvar_helper(chan, varname, retvar);
66         LOCAL_USER_REMOVE(u);
67         return res;
68 }
69
70 /*--- md5check_exec: Calculate MD5 checksum and compare it with
71         existing checksum. ---*/
72 static int md5check_exec(struct ast_channel *chan, void *data)
73 {
74         int res=0;
75         struct localuser *u;
76         char *hash= NULL; /* Hash to compare with */
77         char *string = NULL; /* String to calculate on */
78         char newhash[50]; /* Return value */
79
80         if (!data) {
81                 ast_log(LOG_WARNING, "Syntax: MD5Check(<md5hash>,<string>) - missing argument!\n");
82                 return -1;
83         }
84         LOCAL_USER_ADD(u);
85         memset(newhash,0, sizeof(newhash));
86
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");
91                 LOCAL_USER_REMOVE(u);
92                 return -1;
93         }
94         ast_md5_hash(newhash, string);
95         if (!strcmp(newhash, hash)) {   /* Verification ok */
96                 if (option_debug > 2)
97                         ast_log(LOG_DEBUG, "MD5 verified ok: %s -- %s\n", hash, string);
98                 LOCAL_USER_REMOVE(u);
99                 return 0;
100         }
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);
108         return res;
109 }
110
111 int unload_module(void)
112 {
113         int res;
114
115         STANDARD_HANGUP_LOCALUSERS;
116         res =ast_unregister_application(app_md5);
117         res |= ast_unregister_application(app_md5check);
118         return res;
119 }
120
121 int load_module(void)
122 {
123         int res;
124
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);
127         return res;
128 }
129
130 char *description(void)
131 {
132         return tdesc_md5;
133 }
134
135 int usecount(void)
136 {
137         int res;
138         STANDARD_USECOUNT(res);
139         return res;
140 }
141
142 char *key()
143 {
144         return ASTERISK_GPL_KEY;
145 }