message.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "pa_mod.h"
  2. #include "message.h"
  3. #include "../../id.h"
  4. #include "../../parser/parse_from.h"
  5. #include <cds/sstr.h>
  6. #include <xcap/msg_rules.h>
  7. static int xcap_get_msg_rules(str *uid,
  8. msg_rules_t **dst, str *filename,
  9. struct sip_msg *m)
  10. {
  11. xcap_query_params_t xcap;
  12. int res;
  13. /* str u; */
  14. /* get only presentity name, not whole uri
  15. * can't use parse_uri because of absence
  16. * of protocol specification ! */
  17. /* if (get_user_from_uri(uri, &u) != 0) u = *uri; */
  18. memset(&xcap, 0, sizeof(xcap));
  19. if (fill_xcap_params) fill_xcap_params(m, &xcap);
  20. res = get_msg_rules(uid, filename, &xcap, dst);
  21. return res;
  22. }
  23. static int get_sender_uri(struct sip_msg* _m, str* uri)
  24. {
  25. struct sip_uri puri;
  26. int res = 0;
  27. if (parse_headers(_m, HDR_FROM_F, 0) < 0) {
  28. ERR("Error while parsing headers\n");
  29. return -1;
  30. }
  31. uri->s = get_from(_m)->uri.s;
  32. uri->len = get_from(_m)->uri.len;
  33. if (parse_uri(uri->s, uri->len, &puri) < 0) {
  34. LOG(L_ERR, "Error while parsing URI\n");
  35. return -1;
  36. }
  37. uri->s = puri.user.s;
  38. if ((!uri->s) || (puri.user.len < 1)) {
  39. uri->s = puri.host.s;
  40. uri->len = puri.host.len;
  41. res = 1; /* it is uri without username ! */
  42. }
  43. uri->len = puri.host.s + puri.host.len - uri->s;
  44. return res;
  45. }
  46. int authorize_message(struct sip_msg* _m, char* _filename, char*_st)
  47. {
  48. /* get and process XCAP authorization document */
  49. /* may modify the message or its body */
  50. str uid = STR_NULL;
  51. msg_rules_t *rules = NULL;
  52. msg_handling_t mh = msg_handling_allow;
  53. str sender_uri = STR_NULL;
  54. str tmp = STR_NULL;
  55. str *filename = NULL;
  56. int len;
  57. get_sender_uri(_m, &sender_uri);
  58. if (get_to_uid(&uid, _m) < 0) {
  59. ERR("get_to_uid failed\n");
  60. /* enabled */
  61. return 1;
  62. }
  63. if (_filename) {
  64. len =strlen(_filename);
  65. if (len > 0) {
  66. tmp.s = _filename;
  67. tmp.len = len;
  68. filename = &tmp;
  69. }
  70. }
  71. if (xcap_get_msg_rules(&uid, &rules, filename, _m) < 0) {
  72. /* enabled */
  73. DBG("get_msg_rules failed\n");
  74. return 1;
  75. }
  76. if (get_msg_rules_action(rules, &sender_uri, &mh) != 0)
  77. mh = msg_handling_allow;
  78. free_msg_rules(rules);
  79. switch (mh) {
  80. case msg_handling_block:
  81. DBG("XCAP AUTH MESSAGE: block\n");
  82. return -1;
  83. case msg_handling_allow:
  84. DBG("XCAP AUTH MESSAGE: allow\n");
  85. return 1;
  86. }
  87. return -1;
  88. }