privacy.c 2.7 KB

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