| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/lock.h" |
| 35 |
#include "asterisk/file.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/pbx.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/app.h" |
| 40 |
#include "asterisk/astdb.h" |
| 41 |
#include "asterisk/utils.h" |
| 42 |
|
| 43 |
enum { |
| 44 |
OPT_ACCOUNT = (1 << 0), |
| 45 |
OPT_DATABASE = (1 << 1), |
| 46 |
OPT_MULTIPLE = (1 << 3), |
| 47 |
OPT_REMOVE = (1 << 4), |
| 48 |
}; |
| 49 |
|
| 50 |
AST_APP_OPTIONS(auth_app_options, { |
| 51 |
AST_APP_OPTION('a', OPT_ACCOUNT), |
| 52 |
AST_APP_OPTION('d', OPT_DATABASE), |
| 53 |
AST_APP_OPTION('m', OPT_MULTIPLE), |
| 54 |
AST_APP_OPTION('r', OPT_REMOVE), |
| 55 |
}); |
| 56 |
|
| 57 |
|
| 58 |
static const char app[] = "Authenticate"; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
static int auth_exec(struct ast_channel *chan, const char *data) |
| 129 |
{ |
| 130 |
int res = 0, retries, maxdigits; |
| 131 |
char passwd[256], *prompt = "agent-pass", *argcopy = NULL; |
| 132 |
struct ast_flags flags = {0}; |
| 133 |
|
| 134 |
AST_DECLARE_APP_ARGS(arglist, |
| 135 |
AST_APP_ARG(password); |
| 136 |
AST_APP_ARG(options); |
| 137 |
AST_APP_ARG(maxdigits); |
| 138 |
AST_APP_ARG(prompt); |
| 139 |
); |
| 140 |
|
| 141 |
if (ast_strlen_zero(data)) { |
| 142 |
ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n"); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
|
| 146 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 147 |
if ((res = ast_answer(chan))) |
| 148 |
return -1; |
| 149 |
} |
| 150 |
|
| 151 |
argcopy = ast_strdupa(data); |
| 152 |
|
| 153 |
AST_STANDARD_APP_ARGS(arglist, argcopy); |
| 154 |
|
| 155 |
if (!ast_strlen_zero(arglist.options)) |
| 156 |
ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options); |
| 157 |
|
| 158 |
if (!ast_strlen_zero(arglist.maxdigits)) { |
| 159 |
maxdigits = atoi(arglist.maxdigits); |
| 160 |
if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2)) |
| 161 |
maxdigits = sizeof(passwd) - 2; |
| 162 |
} else { |
| 163 |
maxdigits = sizeof(passwd) - 2; |
| 164 |
} |
| 165 |
|
| 166 |
if (!ast_strlen_zero(arglist.prompt)) { |
| 167 |
prompt = arglist.prompt; |
| 168 |
} else { |
| 169 |
prompt = "agent-pass"; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
for (retries = 0; retries < 3; retries++) { |
| 174 |
if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0) |
| 175 |
break; |
| 176 |
|
| 177 |
res = 0; |
| 178 |
|
| 179 |
if (arglist.password[0] != '/') { |
| 180 |
|
| 181 |
if (!strcmp(passwd, arglist.password)) |
| 182 |
break; |
| 183 |
} else if (ast_test_flag(&flags,OPT_DATABASE)) { |
| 184 |
char tmp[256]; |
| 185 |
|
| 186 |
if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) { |
| 187 |
|
| 188 |
if (ast_test_flag(&flags,OPT_REMOVE)) |
| 189 |
ast_db_del(arglist.password + 1, passwd); |
| 190 |
break; |
| 191 |
} |
| 192 |
} else { |
| 193 |
|
| 194 |
FILE *f; |
| 195 |
char buf[256] = "", md5passwd[33] = "", *md5secret = NULL; |
| 196 |
|
| 197 |
if (!(f = fopen(arglist.password, "r"))) { |
| 198 |
ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno)); |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
for (;;) { |
| 203 |
size_t len; |
| 204 |
|
| 205 |
if (feof(f)) |
| 206 |
break; |
| 207 |
|
| 208 |
if (!fgets(buf, sizeof(buf), f)) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
if (ast_strlen_zero(buf)) |
| 213 |
continue; |
| 214 |
|
| 215 |
len = strlen(buf) - 1; |
| 216 |
if (buf[len] == '\n') |
| 217 |
buf[len] = '\0'; |
| 218 |
|
| 219 |
if (ast_test_flag(&flags, OPT_MULTIPLE)) { |
| 220 |
md5secret = buf; |
| 221 |
strsep(&md5secret, ":"); |
| 222 |
if (!md5secret) |
| 223 |
continue; |
| 224 |
ast_md5_hash(md5passwd, passwd); |
| 225 |
if (!strcmp(md5passwd, md5secret)) { |
| 226 |
if (ast_test_flag(&flags, OPT_ACCOUNT)) { |
| 227 |
ast_channel_lock(chan); |
| 228 |
ast_channel_accountcode_set(chan, buf); |
| 229 |
ast_channel_unlock(chan); |
| 230 |
} |
| 231 |
break; |
| 232 |
} |
| 233 |
} else { |
| 234 |
if (!strcmp(passwd, buf)) { |
| 235 |
if (ast_test_flag(&flags, OPT_ACCOUNT)) { |
| 236 |
ast_channel_lock(chan); |
| 237 |
ast_channel_accountcode_set(chan, buf); |
| 238 |
ast_channel_unlock(chan); |
| 239 |
} |
| 240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
fclose(f); |
| 246 |
|
| 247 |
if (!ast_strlen_zero(buf)) { |
| 248 |
if (ast_test_flag(&flags, OPT_MULTIPLE)) { |
| 249 |
if (md5secret && !strcmp(md5passwd, md5secret)) |
| 250 |
break; |
| 251 |
} else { |
| 252 |
if (!strcmp(passwd, buf)) |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
prompt = "auth-incorrect"; |
| 258 |
} |
| 259 |
|
| 260 |
if ((retries < 3) && !res) { |
| 261 |
if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) { |
| 262 |
ast_channel_lock(chan); |
| 263 |
ast_channel_accountcode_set(chan, passwd); |
| 264 |
ast_channel_unlock(chan); |
| 265 |
} |
| 266 |
if (!(res = ast_streamfile(chan, "auth-thankyou", ast_channel_language(chan)))) |
| 267 |
res = ast_waitstream(chan, ""); |
| 268 |
} else { |
| 269 |
if (!ast_streamfile(chan, "vm-goodbye", ast_channel_language(chan))) |
| 270 |
res = ast_waitstream(chan, ""); |
| 271 |
res = -1; |
| 272 |
} |
| 273 |
|
| 274 |
return res; |
| 275 |
} |
| 276 |
|
| 277 |
static int unload_module(void) |
| 278 |
{ |
| 279 |
return ast_unregister_application(app); |
| 280 |
} |
| 281 |
|
| 282 |
static int load_module(void) |
| 283 |
{ |
| 284 |
if (ast_register_application_xml(app, auth_exec)) |
| 285 |
return AST_MODULE_LOAD_DECLINE; |
| 286 |
return AST_MODULE_LOAD_SUCCESS; |
| 287 |
} |
| 288 |
|
| 289 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Authentication Application"); |