privacy.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Channel Management
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/time.h>
  17. #include <signal.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <dirent.h>
  21. #include <asterisk/channel.h>
  22. #include <asterisk/file.h>
  23. #include <asterisk/app.h>
  24. #include <asterisk/dsp.h>
  25. #include <asterisk/logger.h>
  26. #include <asterisk/options.h>
  27. #include <asterisk/astdb.h>
  28. #include <asterisk/callerid.h>
  29. #include <asterisk/privacy.h>
  30. #include <asterisk/utils.h>
  31. #include <asterisk/lock.h>
  32. #include "asterisk.h"
  33. int ast_privacy_check(char *dest, char *cid)
  34. {
  35. char tmp[256] = "";
  36. char *trimcid = "";
  37. char *n, *l;
  38. int res;
  39. char key[256], result[256];
  40. if (cid)
  41. strncpy(tmp, cid, sizeof(tmp) - 1);
  42. ast_callerid_parse(tmp, &n, &l);
  43. if (l) {
  44. ast_shrink_phone_number(l);
  45. trimcid = l;
  46. }
  47. snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
  48. res = ast_db_get("privacy", key, result, sizeof(result));
  49. if (!res) {
  50. if (!strcasecmp(result, "allow"))
  51. return AST_PRIVACY_ALLOW;
  52. if (!strcasecmp(result, "deny"))
  53. return AST_PRIVACY_DENY;
  54. if (!strcasecmp(result, "kill"))
  55. return AST_PRIVACY_KILL;
  56. if (!strcasecmp(result, "torture"))
  57. return AST_PRIVACY_TORTURE;
  58. }
  59. return AST_PRIVACY_UNKNOWN;
  60. }
  61. int ast_privacy_reset(char *dest)
  62. {
  63. if (!dest)
  64. return -1;
  65. return ast_db_deltree("privacy", dest);
  66. }
  67. int ast_privacy_set(char *dest, char *cid, int status)
  68. {
  69. char tmp[256] = "";
  70. char *trimcid = "";
  71. char *n, *l;
  72. int res;
  73. char key[256];
  74. if (cid)
  75. strncpy(tmp, cid, sizeof(tmp) - 1);
  76. ast_callerid_parse(tmp, &n, &l);
  77. if (l) {
  78. ast_shrink_phone_number(l);
  79. trimcid = l;
  80. }
  81. if (ast_strlen_zero(trimcid)) {
  82. /* Don't store anything for empty Caller*ID */
  83. return 0;
  84. }
  85. snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
  86. if (status == AST_PRIVACY_UNKNOWN)
  87. res = ast_db_del("privacy", key);
  88. else if (status == AST_PRIVACY_ALLOW)
  89. res = ast_db_put("privacy", key, "allow");
  90. else if (status == AST_PRIVACY_DENY)
  91. res = ast_db_put("privacy", key, "deny");
  92. else if (status == AST_PRIVACY_KILL)
  93. res = ast_db_put("privacy", key, "kill");
  94. else if (status == AST_PRIVACY_TORTURE)
  95. res = ast_db_put("privacy", key, "torture");
  96. else
  97. res = -1;
  98. return res;
  99. }