privacy.c 2.7 KB

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