app_authenticate.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Execute arbitrary authenticate commands
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. #include "asterisk/lock.h"
  31. #include "asterisk/file.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/app.h"
  36. #include "asterisk/astdb.h"
  37. #include "asterisk/utils.h"
  38. enum {
  39. OPT_ACCOUNT = (1 << 0),
  40. OPT_DATABASE = (1 << 1),
  41. OPT_MULTIPLE = (1 << 3),
  42. OPT_REMOVE = (1 << 4),
  43. };
  44. AST_APP_OPTIONS(auth_app_options, {
  45. AST_APP_OPTION('a', OPT_ACCOUNT),
  46. AST_APP_OPTION('d', OPT_DATABASE),
  47. AST_APP_OPTION('m', OPT_MULTIPLE),
  48. AST_APP_OPTION('r', OPT_REMOVE),
  49. });
  50. static const char app[] = "Authenticate";
  51. /*** DOCUMENTATION
  52. <application name="Authenticate" language="en_US">
  53. <synopsis>
  54. Authenticate a user
  55. </synopsis>
  56. <syntax>
  57. <parameter name="password" required="true">
  58. <para>Password the user should know</para>
  59. </parameter>
  60. <parameter name="options" required="false">
  61. <optionlist>
  62. <option name="a">
  63. <para>Set the channels' account code to the password that is entered</para>
  64. </option>
  65. <option name="d">
  66. <para>Interpret the given path as database key, not a literal file.</para>
  67. <note>
  68. <para>The value is not used at all in the authentication when using this option.
  69. If the family/key is set to <literal>/pin/100</literal> (value does not matter)
  70. then the password field needs to be set to <literal>/pin</literal> and the pin entered
  71. by the user would be authenticated against <literal>100</literal>.</para>
  72. </note>
  73. </option>
  74. <option name="m">
  75. <para>Interpret the given path as a file which contains a list of account
  76. codes and password hashes delimited with <literal>:</literal>, listed one per line in
  77. the file. When one of the passwords is matched, the channel will have
  78. its account code set to the corresponding account code in the file.</para>
  79. </option>
  80. <option name="r">
  81. <para>Remove the database key upon successful entry (valid with <literal>d</literal> only)</para>
  82. </option>
  83. </optionlist>
  84. </parameter>
  85. <parameter name="maxdigits" required="false">
  86. <para>maximum acceptable number of digits. Stops reading after
  87. maxdigits have been entered (without requiring the user to press the <literal>#</literal> key).
  88. Defaults to 0 - no limit - wait for the user press the <literal>#</literal> key.</para>
  89. </parameter>
  90. <parameter name="prompt" required="false" argsep="&amp;">
  91. <para>Override the &quot;agent-pass&quot; sound file. Can be
  92. an ampersand separated list of filenames. If the filename
  93. is a relative filename (it does not begin with a slash), it
  94. will be searched for in the Asterisk sounds directory. If the
  95. filename is able to be parsed as a URL, Asterisk will
  96. download the file and then begin playback on it. To include a
  97. literal <literal>&amp;</literal> in the URL you can enclose
  98. the URL in single quotes.</para>
  99. <argument name="prompt" required="true" />
  100. <argument name="prompt2" multiple="true" />
  101. </parameter>
  102. </syntax>
  103. <description>
  104. <para>This application asks the caller to enter a given password in order to continue dialplan execution.</para>
  105. <para>If the password begins with the <literal>/</literal> character,
  106. it is interpreted as a file which contains a list of valid passwords, listed 1 password per line in the file.</para>
  107. <para>When using a database key, the value associated with the key can be anything.</para>
  108. <para>Users have three attempts to authenticate before the channel is hung up.</para>
  109. </description>
  110. <see-also>
  111. <ref type="application">VMAuthenticate</ref>
  112. <ref type="application">DISA</ref>
  113. </see-also>
  114. </application>
  115. ***/
  116. static int auth_exec(struct ast_channel *chan, const char *data)
  117. {
  118. int res = 0, retries, maxdigits;
  119. char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
  120. struct ast_flags flags = {0};
  121. AST_DECLARE_APP_ARGS(arglist,
  122. AST_APP_ARG(password);
  123. AST_APP_ARG(options);
  124. AST_APP_ARG(maxdigits);
  125. AST_APP_ARG(prompt);
  126. );
  127. if (ast_strlen_zero(data)) {
  128. ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
  129. return -1;
  130. }
  131. if (ast_channel_state(chan) != AST_STATE_UP) {
  132. if ((res = ast_answer(chan)))
  133. return -1;
  134. }
  135. argcopy = ast_strdupa(data);
  136. AST_STANDARD_APP_ARGS(arglist, argcopy);
  137. if (!ast_strlen_zero(arglist.options))
  138. ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options);
  139. if (!ast_strlen_zero(arglist.maxdigits)) {
  140. maxdigits = atoi(arglist.maxdigits);
  141. if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
  142. maxdigits = sizeof(passwd) - 2;
  143. } else {
  144. maxdigits = sizeof(passwd) - 2;
  145. }
  146. if (!ast_strlen_zero(arglist.prompt)) {
  147. prompt = arglist.prompt;
  148. } else {
  149. prompt = "agent-pass";
  150. }
  151. /* Start asking for password */
  152. for (retries = 0; retries < 3; retries++) {
  153. if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
  154. break;
  155. res = 0;
  156. if (arglist.password[0] != '/') {
  157. /* Compare against a fixed password */
  158. if (!strcmp(passwd, arglist.password))
  159. break;
  160. } else if (ast_test_flag(&flags,OPT_DATABASE)) {
  161. char tmp[256];
  162. /* Compare against a database key */
  163. if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
  164. /* It's a good password */
  165. if (ast_test_flag(&flags,OPT_REMOVE))
  166. ast_db_del(arglist.password + 1, passwd);
  167. break;
  168. }
  169. } else {
  170. /* Compare against a file */
  171. FILE *f;
  172. char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
  173. if (!(f = fopen(arglist.password, "r"))) {
  174. ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
  175. continue;
  176. }
  177. for (;;) {
  178. size_t len;
  179. if (feof(f))
  180. break;
  181. if (!fgets(buf, sizeof(buf), f)) {
  182. continue;
  183. }
  184. if (ast_strlen_zero(buf))
  185. continue;
  186. len = strlen(buf) - 1;
  187. if (buf[len] == '\n')
  188. buf[len] = '\0';
  189. if (ast_test_flag(&flags, OPT_MULTIPLE)) {
  190. md5secret = buf;
  191. strsep(&md5secret, ":");
  192. if (!md5secret)
  193. continue;
  194. ast_md5_hash(md5passwd, passwd);
  195. if (!strcmp(md5passwd, md5secret)) {
  196. if (ast_test_flag(&flags, OPT_ACCOUNT)) {
  197. ast_channel_lock(chan);
  198. ast_channel_accountcode_set(chan, buf);
  199. ast_channel_unlock(chan);
  200. }
  201. break;
  202. }
  203. } else {
  204. if (!strcmp(passwd, buf)) {
  205. if (ast_test_flag(&flags, OPT_ACCOUNT)) {
  206. ast_channel_lock(chan);
  207. ast_channel_accountcode_set(chan, buf);
  208. ast_channel_unlock(chan);
  209. }
  210. break;
  211. }
  212. }
  213. }
  214. fclose(f);
  215. if (!ast_strlen_zero(buf)) {
  216. if (ast_test_flag(&flags, OPT_MULTIPLE)) {
  217. if (md5secret && !strcmp(md5passwd, md5secret))
  218. break;
  219. } else {
  220. if (!strcmp(passwd, buf))
  221. break;
  222. }
  223. }
  224. }
  225. prompt = "auth-incorrect";
  226. }
  227. if ((retries < 3) && !res) {
  228. if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) {
  229. ast_channel_lock(chan);
  230. ast_channel_accountcode_set(chan, passwd);
  231. ast_channel_unlock(chan);
  232. }
  233. if (!(res = ast_streamfile(chan, "auth-thankyou", ast_channel_language(chan))))
  234. res = ast_waitstream(chan, "");
  235. } else {
  236. if (!ast_streamfile(chan, "vm-goodbye", ast_channel_language(chan)))
  237. res = ast_waitstream(chan, "");
  238. res = -1;
  239. }
  240. return res;
  241. }
  242. static int unload_module(void)
  243. {
  244. return ast_unregister_application(app);
  245. }
  246. static int load_module(void)
  247. {
  248. if (ast_register_application_xml(app, auth_exec))
  249. return AST_MODULE_LOAD_DECLINE;
  250. return AST_MODULE_LOAD_SUCCESS;
  251. }
  252. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Authentication Application");