privacy.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Privacy Routines
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <sys/time.h>
  30. #include <signal.h>
  31. #include <dirent.h>
  32. #include "asterisk/channel.h"
  33. #include "asterisk/file.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/dsp.h"
  36. #include "asterisk/astdb.h"
  37. #include "asterisk/callerid.h"
  38. #include "asterisk/privacy.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/lock.h"
  41. int ast_privacy_check(char *dest, char *cid)
  42. {
  43. char tmp[256] = "";
  44. char *trimcid = "";
  45. char *n, *l;
  46. int res;
  47. char key[256], result[256];
  48. if (cid)
  49. ast_copy_string(tmp, cid, sizeof(tmp));
  50. ast_callerid_parse(tmp, &n, &l);
  51. if (l) {
  52. ast_shrink_phone_number(l);
  53. trimcid = l;
  54. }
  55. snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
  56. res = ast_db_get("privacy", key, result, sizeof(result));
  57. if (!res) {
  58. if (!strcasecmp(result, "allow"))
  59. return AST_PRIVACY_ALLOW;
  60. if (!strcasecmp(result, "deny"))
  61. return AST_PRIVACY_DENY;
  62. if (!strcasecmp(result, "kill"))
  63. return AST_PRIVACY_KILL;
  64. if (!strcasecmp(result, "torture"))
  65. return AST_PRIVACY_TORTURE;
  66. }
  67. return AST_PRIVACY_UNKNOWN;
  68. }
  69. int ast_privacy_reset(char *dest)
  70. {
  71. if (!dest)
  72. return -1;
  73. return ast_db_deltree("privacy", dest);
  74. }
  75. int ast_privacy_set(char *dest, char *cid, int status)
  76. {
  77. char tmp[256] = "";
  78. char *trimcid = "";
  79. char *n, *l;
  80. int res;
  81. char key[256];
  82. if (cid)
  83. ast_copy_string(tmp, cid, sizeof(tmp));
  84. ast_callerid_parse(tmp, &n, &l);
  85. if (l) {
  86. ast_shrink_phone_number(l);
  87. trimcid = l;
  88. }
  89. if (ast_strlen_zero(trimcid)) {
  90. /* Don't store anything for empty Caller*ID */
  91. return 0;
  92. }
  93. snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
  94. if (status == AST_PRIVACY_UNKNOWN)
  95. res = ast_db_del("privacy", key);
  96. else if (status == AST_PRIVACY_ALLOW)
  97. res = ast_db_put("privacy", key, "allow");
  98. else if (status == AST_PRIVACY_DENY)
  99. res = ast_db_put("privacy", key, "deny");
  100. else if (status == AST_PRIVACY_KILL)
  101. res = ast_db_put("privacy", key, "kill");
  102. else if (status == AST_PRIVACY_TORTURE)
  103. res = ast_db_put("privacy", key, "torture");
  104. else
  105. res = -1;
  106. return res;
  107. }