app_authenticate.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/lock.h"
  32. #include "asterisk/file.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/app.h"
  37. #include "asterisk/astdb.h"
  38. #include "asterisk/utils.h"
  39. enum {
  40. OPT_ACCOUNT = (1 << 0),
  41. OPT_DATABASE = (1 << 1),
  42. OPT_MULTIPLE = (1 << 3),
  43. OPT_REMOVE = (1 << 4),
  44. };
  45. AST_APP_OPTIONS(auth_app_options, {
  46. AST_APP_OPTION('a', OPT_ACCOUNT),
  47. AST_APP_OPTION('d', OPT_DATABASE),
  48. AST_APP_OPTION('m', OPT_MULTIPLE),
  49. AST_APP_OPTION('r', OPT_REMOVE),
  50. });
  51. static const char app[] = "Authenticate";
  52. /*** DOCUMENTATION
  53. <application name="Authenticate" language="en_US">
  54. <synopsis>
  55. Authenticate a user
  56. </synopsis>
  57. <syntax>
  58. <parameter name="password" required="true">
  59. <para>Password the user should know</para>
  60. </parameter>
  61. <parameter name="options" required="false">
  62. <optionlist>
  63. <option name="a">
  64. <para>Set the channels' account code to the password that is entered</para>
  65. </option>
  66. <option name="d">
  67. <para>Interpret the given path as database key, not a literal file.</para>
  68. <note>
  69. <para>The value is not used at all in the authentication when using this option.
  70. If the family/key is set to <literal>/pin/100</literal> (value does not matter)
  71. then the password field needs to be set to <literal>/pin</literal> and the pin entered
  72. by the user would be authenticated against <literal>100</literal>.</para>
  73. </note>
  74. </option>
  75. <option name="m">
  76. <para>Interpret the given path as a file which contains a list of account
  77. codes and password hashes delimited with <literal>:</literal>, listed one per line in
  78. the file. When one of the passwords is matched, the channel will have
  79. its account code set to the corresponding account code in the file.</para>
  80. </option>
  81. <option name="r">
  82. <para>Remove the database key upon successful entry (valid with <literal>d</literal> only)</para>
  83. </option>
  84. </optionlist>
  85. </parameter>
  86. <parameter name="maxdigits" required="false">
  87. <para>maximum acceptable number of digits. Stops reading after
  88. maxdigits have been entered (without requiring the user to press the <literal>#</literal> key).
  89. Defaults to 0 - no limit - wait for the user press the <literal>#</literal> key.</para>
  90. </parameter>
  91. <parameter name="prompt" required="false">
  92. <para>Override the agent-pass prompt file.</para>
  93. </parameter>
  94. </syntax>
  95. <description>
  96. <para>This application asks the caller to enter a given password in order to continue dialplan execution.</para>
  97. <para>If the password begins with the <literal>/</literal> character,
  98. it is interpreted as a file which contains a list of valid passwords, listed 1 password per line in the file.</para>
  99. <para>When using a database key, the value associated with the key can be anything.</para>
  100. <para>Users have three attempts to authenticate before the channel is hung up.</para>
  101. </description>
  102. <see-also>
  103. <ref type="application">VMAuthenticate</ref>
  104. <ref type="application">DISA</ref>
  105. </see-also>
  106. </application>
  107. ***/
  108. static int auth_exec(struct ast_channel *chan, const char *data)
  109. {
  110. int res = 0, retries, maxdigits;
  111. char passwd[256], *prompt = "agent-pass", *argcopy = NULL;
  112. struct ast_flags flags = {0};
  113. AST_DECLARE_APP_ARGS(arglist,
  114. AST_APP_ARG(password);
  115. AST_APP_ARG(options);
  116. AST_APP_ARG(maxdigits);
  117. AST_APP_ARG(prompt);
  118. );
  119. if (ast_strlen_zero(data)) {
  120. ast_log(LOG_WARNING, "Authenticate requires an argument(password)\n");
  121. return -1;
  122. }
  123. if (ast_channel_state(chan) != AST_STATE_UP) {
  124. if ((res = ast_answer(chan)))
  125. return -1;
  126. }
  127. argcopy = ast_strdupa(data);
  128. AST_STANDARD_APP_ARGS(arglist, argcopy);
  129. if (!ast_strlen_zero(arglist.options))
  130. ast_app_parse_options(auth_app_options, &flags, NULL, arglist.options);
  131. if (!ast_strlen_zero(arglist.maxdigits)) {
  132. maxdigits = atoi(arglist.maxdigits);
  133. if ((maxdigits<1) || (maxdigits>sizeof(passwd)-2))
  134. maxdigits = sizeof(passwd) - 2;
  135. } else {
  136. maxdigits = sizeof(passwd) - 2;
  137. }
  138. if (!ast_strlen_zero(arglist.prompt)) {
  139. prompt = arglist.prompt;
  140. } else {
  141. prompt = "agent-pass";
  142. }
  143. /* Start asking for password */
  144. for (retries = 0; retries < 3; retries++) {
  145. if ((res = ast_app_getdata(chan, prompt, passwd, maxdigits, 0)) < 0)
  146. break;
  147. res = 0;
  148. if (arglist.password[0] != '/') {
  149. /* Compare against a fixed password */
  150. if (!strcmp(passwd, arglist.password))
  151. break;
  152. } else if (ast_test_flag(&flags,OPT_DATABASE)) {
  153. char tmp[256];
  154. /* Compare against a database key */
  155. if (!ast_db_get(arglist.password + 1, passwd, tmp, sizeof(tmp))) {
  156. /* It's a good password */
  157. if (ast_test_flag(&flags,OPT_REMOVE))
  158. ast_db_del(arglist.password + 1, passwd);
  159. break;
  160. }
  161. } else {
  162. /* Compare against a file */
  163. FILE *f;
  164. char buf[256] = "", md5passwd[33] = "", *md5secret = NULL;
  165. if (!(f = fopen(arglist.password, "r"))) {
  166. ast_log(LOG_WARNING, "Unable to open file '%s' for authentication: %s\n", arglist.password, strerror(errno));
  167. continue;
  168. }
  169. for (;;) {
  170. size_t len;
  171. if (feof(f))
  172. break;
  173. if (!fgets(buf, sizeof(buf), f)) {
  174. continue;
  175. }
  176. if (ast_strlen_zero(buf))
  177. continue;
  178. len = strlen(buf) - 1;
  179. if (buf[len] == '\n')
  180. buf[len] = '\0';
  181. if (ast_test_flag(&flags, OPT_MULTIPLE)) {
  182. md5secret = buf;
  183. strsep(&md5secret, ":");
  184. if (!md5secret)
  185. continue;
  186. ast_md5_hash(md5passwd, passwd);
  187. if (!strcmp(md5passwd, md5secret)) {
  188. if (ast_test_flag(&flags, OPT_ACCOUNT)) {
  189. ast_channel_lock(chan);
  190. ast_channel_accountcode_set(chan, buf);
  191. ast_channel_unlock(chan);
  192. }
  193. break;
  194. }
  195. } else {
  196. if (!strcmp(passwd, buf)) {
  197. if (ast_test_flag(&flags, OPT_ACCOUNT)) {
  198. ast_channel_lock(chan);
  199. ast_channel_accountcode_set(chan, buf);
  200. ast_channel_unlock(chan);
  201. }
  202. break;
  203. }
  204. }
  205. }
  206. fclose(f);
  207. if (!ast_strlen_zero(buf)) {
  208. if (ast_test_flag(&flags, OPT_MULTIPLE)) {
  209. if (md5secret && !strcmp(md5passwd, md5secret))
  210. break;
  211. } else {
  212. if (!strcmp(passwd, buf))
  213. break;
  214. }
  215. }
  216. }
  217. prompt = "auth-incorrect";
  218. }
  219. if ((retries < 3) && !res) {
  220. if (ast_test_flag(&flags,OPT_ACCOUNT) && !ast_test_flag(&flags,OPT_MULTIPLE)) {
  221. ast_channel_lock(chan);
  222. ast_channel_accountcode_set(chan, passwd);
  223. ast_channel_unlock(chan);
  224. }
  225. if (!(res = ast_streamfile(chan, "auth-thankyou", ast_channel_language(chan))))
  226. res = ast_waitstream(chan, "");
  227. } else {
  228. if (!ast_streamfile(chan, "vm-goodbye", ast_channel_language(chan)))
  229. res = ast_waitstream(chan, "");
  230. res = -1;
  231. }
  232. return res;
  233. }
  234. static int unload_module(void)
  235. {
  236. return ast_unregister_application(app);
  237. }
  238. static int load_module(void)
  239. {
  240. if (ast_register_application_xml(app, auth_exec))
  241. return AST_MODULE_LOAD_DECLINE;
  242. return AST_MODULE_LOAD_SUCCESS;
  243. }
  244. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Authentication Application");