privacy.c 2.3 KB

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