rls_handler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #include "rls_mod.h"
  2. #include "rls_handler.h"
  3. #include "rl_subscription.h"
  4. #include <cds/dstring.h>
  5. #include <cds/logger.h>
  6. #include <cds/sip_utils.h>
  7. #include "result_codes.h"
  8. #include "../../str.h"
  9. #include "../../id.h"
  10. #include "../../dprint.h"
  11. #include "../../mem/mem.h"
  12. #include "../../parser/parse_uri.h"
  13. #include "../../parser/parse_from.h"
  14. #include "../../parser/parse_to.h"
  15. #include "../../parser/parse_expires.h"
  16. #include "../../parser/parse_event.h"
  17. #include "../../parser/parse_expires.h"
  18. #include "../../parser/parse_content.h"
  19. #include "../../data_lump_rpl.h"
  20. #include "../../usr_avp.h"
  21. #include <xcap/resource_list.h>
  22. /* static variables for sharing data loaded from XCAP */
  23. typedef struct {
  24. xcap_query_params_t xcap_params;
  25. flat_list_t *flat_list; /* may be NULL for empty lists!!! */
  26. int have_flat_list; /* added due to possibility of NULL flat list */
  27. } rls_xcap_query_t;
  28. static rls_xcap_query_t query = {
  29. xcap_params: { xcap_root: {s: NULL, len: 0} },
  30. flat_list: NULL,
  31. have_flat_list: 0
  32. };
  33. /* clears last data stored from one of query_... functions */
  34. static void clear_last_query()
  35. {
  36. if (query.have_flat_list) {
  37. if (query.flat_list) free_flat_list(query.flat_list);
  38. query.flat_list = NULL;
  39. query.have_flat_list = 0;
  40. /* str_clear(&query.params.xcap_root); */
  41. memset(&query.xcap_params, 0, sizeof(query.xcap_params));
  42. }
  43. }
  44. static int send_reply(struct sip_msg* _m, int code, char *msg)
  45. {
  46. if (tmb.t_reply(_m, code, msg) == -1) {
  47. LOG(L_ERR, "send_reply(): Error while sending %d %s\n", code, msg);
  48. return -1;
  49. }
  50. else return 0;
  51. }
  52. static int parse_rls_headers(struct sip_msg* _m)
  53. {
  54. struct hdr_field *acc;
  55. if ( (parse_headers(_m, HDR_EOH_T, 0) == -1) || /* we need all Accept headers... */
  56. (_m->from==0)||(_m->to==0)||(_m->event==0) ) {
  57. LOG(L_ERR, "parse_rls_headers(): Error while parsing headers\n");
  58. return -1;
  59. }
  60. /* there is no parse_to_header function (only parse_to)
  61. if (parse_to_header(_m) < 0) {
  62. LOG(L_ERR, "parse_rls_headers(): To malformed or missing\n");
  63. return -1;
  64. }*/
  65. if (parse_from_header(_m) < 0) {
  66. LOG(L_ERR, "parse_rls_headers(): From malformed or missing\n");
  67. return -1;
  68. }
  69. if (_m->expires) {
  70. if (parse_expires(_m->expires) < 0) {
  71. LOG(L_ERR, "parse_rls_headers(): Error parsing Expires header\n");
  72. return -1;
  73. }
  74. }
  75. if (_m->event) {
  76. if (parse_event(_m->event) < 0) {
  77. LOG(L_ERR, "parse_rls_headers(): Error while parsing Event header field\n");
  78. return -1;
  79. }
  80. }
  81. acc = _m->accept;
  82. while (acc) { /* parse all accept headers */
  83. if (acc->type == HDR_ACCEPT_T) {
  84. /* DEBUG_LOG("parsing accept header: %.*s\n", FMT_STR(acc->body)); */
  85. if (parse_accept_body(acc) < 0) {
  86. LOG(L_ERR, "parse_rls_headers(): Error while parsing Accept header field\n");
  87. return -1;
  88. }
  89. }
  90. acc = acc->next;
  91. }
  92. return 0;
  93. }
  94. static int get_event(struct sip_msg *_m)
  95. {
  96. int et = 0;
  97. event_t *event = NULL;
  98. if (_m->event) {
  99. event = (event_t*)(_m->event->parsed);
  100. et = event->parsed;
  101. } else {
  102. LOG(L_ERR, "no event package for RLS - using EVENT_PRESENCE\n");
  103. et = EVENT_PRESENCE;
  104. }
  105. return et;
  106. }
  107. /* returns 1 if package supported by RLS */
  108. static int verify_event_package(struct sip_msg *m)
  109. {
  110. int et = get_event(m);
  111. switch (et) {
  112. case EVENT_PRESENCE: return 0;
  113. default: return -1;
  114. }
  115. return -1;
  116. }
  117. static int add_response_header(struct sip_msg *_m, char *hdr)
  118. {
  119. if (!add_lump_rpl(_m, hdr, strlen(hdr), LUMP_RPL_HDR)) return -1;
  120. return 0;
  121. }
  122. static int add_response_min_expires_header(struct sip_msg *_m)
  123. {
  124. char tmp[64];
  125. sprintf(tmp, "Min-Expires: %d\r\n", rls_min_expiration);
  126. if (!add_lump_rpl(_m, tmp, strlen(tmp), LUMP_RPL_HDR)) return -1;
  127. return 0;
  128. }
  129. struct accepted_types {
  130. char *mime_txt;
  131. int mime;
  132. int needed;
  133. int found;
  134. };
  135. /* marks mime_type as found */
  136. void mark_accepted_type(struct accepted_types *types, int mime_type)
  137. {
  138. int i;
  139. for (i = 0; types[i].mime_txt; i++)
  140. if (mime_type == types[i].mime) types[i].found = 1;
  141. }
  142. static int check_message(struct sip_msg *_m, int send_err)
  143. {
  144. int *accepts_mimes = NULL;
  145. int i;
  146. struct hdr_field *acc;
  147. struct accepted_types accepts[] = {
  148. { "multipart/related", MIMETYPE(MULTIPART,RELATED), 1, 0 },
  149. { "application/rlmi+xml", MIMETYPE(APPLICATION,RLMIXML), 1, 0 },
  150. { "application/pidf+xml", MIMETYPE(APPLICATION, PIDFXML), 1, 0 },
  151. { NULL, 0, 0, 0 }
  152. };
  153. if (verify_event_package(_m) != 0) {
  154. /* allow only selected packages independently on rls document */
  155. if (send_err) {
  156. ERR("unsupported events\n");
  157. add_response_header(_m, "Allow-Events: presence\r\n");
  158. send_reply(_m, 489, "Bad Event");
  159. }
  160. return -1;
  161. }
  162. /* verify Accept: multipart/related, application/rlmi+xml, application/pidf+xml */
  163. acc = _m->accept;
  164. while (acc) { /* go through all Accept headers */
  165. if (acc->type == HDR_ACCEPT_T) {
  166. /* it MUST be parsed from parse_hdr !!! */
  167. accepts_mimes = acc->parsed;
  168. /* go through all in accept mimes and test our */
  169. for (i = 0; accepts_mimes[i]; i++)
  170. mark_accepted_type(accepts, accepts_mimes[i]);
  171. }
  172. acc = acc->next;
  173. }
  174. for (i = 0; accepts[i].mime_txt; i++)
  175. if ((!accepts[i].found) && (accepts[i].needed)) {
  176. if (send_err) {
  177. ERR("required type %s not in Accept headers\n",
  178. accepts[i].mime_txt);
  179. send_reply(_m, 400, "Bad Request");
  180. }
  181. return -1;
  182. }
  183. /* verify Supported: eventlist */
  184. return 0;
  185. }
  186. static int handle_new_subscription(struct sip_msg *m, rls_xcap_query_t *query, int send_error_responses)
  187. {
  188. rl_subscription_t *s;
  189. int res = 0;
  190. xcap_query_params_t *params = NULL;
  191. if (query) params = &query->xcap_params;
  192. rls_lock();
  193. DBG("handle_new_subscription(rls)\n");
  194. /* create a new subscription structure */
  195. res = rls_create_subscription(m, &s, query->flat_list, params);
  196. if (res != RES_OK) {
  197. rls_unlock();
  198. switch (res) {
  199. case RES_PARSE_HEADERS_ERR:
  200. if (!send_error_responses) return -1; /* "unprocessed" */
  201. add_response_header(m, "Reason-Phrase: Bad or missing headers\r\n");
  202. send_reply(m, 400, "Bad Request");
  203. break;
  204. case RES_SUBSCRIPTION_REJECTED:
  205. /* if (!send_error_responses) return -1; */
  206. /* FIXME: authorization is done before XCAP query, so though it is NOT
  207. * resource-list subscription it may be marked as rejected !!! */
  208. DEBUG_LOG("subscription rejected\n");
  209. add_response_header(m, "Reason-Phrase: Subscription rejected\r\n");
  210. send_reply(m, 403, "Forbidden");
  211. break;
  212. case RES_EXPIRATION_INTERVAL_TOO_SHORT:
  213. if (!send_error_responses) return -1; /* "unprocessed" */
  214. add_response_min_expires_header(m);
  215. send_reply(m, 423, "Interval too small");
  216. break;
  217. case RES_BAD_EVENT_PACKAGE_ERR:
  218. if (!send_error_responses) return -1; /* "unprocessed" */
  219. /* TODO: add_response_header(_m, "Allow-Events: \r\n"); */
  220. send_reply(m, 489, "Bad Event");
  221. break;
  222. case RES_BAD_GATEWAY_ERR:
  223. if (!send_error_responses) return -1; /* "unprocessed" */
  224. send_reply(m, 502, "Bad Gateway");
  225. break;
  226. case RES_XCAP_QUERY_ERR:
  227. if (!send_error_responses) return -1; /* "unprocessed" */
  228. add_response_header(m, "Reason-Phrase: XCAP query error\r\n");
  229. send_reply(m, 502, "Bad Gateway");
  230. /*send_reply(m, 500, "Internal error"); */
  231. break;
  232. case RES_XCAP_PARSE_ERR:
  233. if (!send_error_responses) return -1; /* "unprocessed" */
  234. add_response_header(m, "Reason-Phrase: XCAP result parsing error\r\n");
  235. send_reply(m, 500, "Internal error");
  236. break;
  237. default:
  238. if (!send_error_responses) return -1; /* "unprocessed" */
  239. send_reply(m, 500, "Internal error");
  240. }
  241. return 0; /* processed */
  242. }
  243. /* send a response */
  244. rls_prepare_subscription_response(s, m);
  245. send_reply(m, 200, "OK");
  246. DEBUG_LOG("RLS subscription successfully handled\n");
  247. /* create NOTIFY message
  248. * FIXME - this may be a nonsense for polling, because the notifier might not
  249. * catch up sent notification */
  250. rls_generate_notify(s, 1);
  251. /* free subscription if only polling */
  252. if (sm_subscription_terminated(&s->u.external) == 0) {
  253. rls_remove(s);
  254. }
  255. rls_unlock();
  256. return 0;
  257. }
  258. static int handle_renew_subscription(struct sip_msg *m, int send_error_responses)
  259. {
  260. str *from_tag;
  261. str *to_tag;
  262. str *call_id;
  263. rl_subscription_t *s = NULL;
  264. int res;
  265. to_tag = &((struct to_body*)m->to->parsed)->tag_value;
  266. from_tag = &((struct to_body*)m->from->parsed)->tag_value;
  267. call_id = NULL;
  268. if (m->callid) call_id = &m->callid->body;
  269. DBG("handle_renew_subscription(rls)\n");
  270. rls_lock();
  271. res = rls_find_subscription(from_tag, to_tag, call_id, &s);
  272. if ((res != RES_OK) || (!s)) {
  273. rls_unlock();
  274. if (send_error_responses) {
  275. WARN("can't refresh unknown subscription\n");
  276. send_reply(m, 481, "Call/Transaction Does Not Exist");
  277. }
  278. return -1; /* "unprocessed" */
  279. }
  280. res = rls_refresh_subscription(m, s);
  281. if (res != RES_OK) {
  282. rls_unlock();
  283. switch (res) {
  284. case RES_PARSE_HEADERS_ERR:
  285. if (!send_error_responses) return -1; /* "unprocessed" */
  286. add_response_header(m, "Reason-Phrase: Bad or missing headers\r\n");
  287. send_reply(m, 400, "Bad Request");
  288. break;
  289. case RES_EXPIRATION_INTERVAL_TOO_SHORT:
  290. if (!send_error_responses) return -1; /* "unprocessed" */
  291. add_response_min_expires_header(m);
  292. send_reply(m, 423, "Interval too small");
  293. break;
  294. case RES_SUBSCRIPTION_TERMINATED:
  295. send_reply(m, 481, "Subscription terminated");
  296. break;
  297. default:
  298. if (!send_error_responses) return -1; /* "unprocessed" */
  299. send_reply(m, 500, "Internal error");
  300. }
  301. return 0; /* processed */
  302. }
  303. /* send a response */
  304. rls_prepare_subscription_response(s, m);
  305. send_reply(m, 200, "OK");
  306. /* create NOTIFY message */
  307. rls_generate_notify(s, 1);
  308. /* free subscription if only polling */
  309. if (sm_subscription_terminated(&s->u.external) == 0) {
  310. rls_remove(s);
  311. }
  312. rls_unlock();
  313. return 0;
  314. }
  315. int handle_rls_subscription(struct sip_msg* _m, char *send_bad_resp)
  316. {
  317. int res;
  318. long send_err = 1;
  319. PROF_START(rls_handle_subscription)
  320. send_err = (long)send_bad_resp;
  321. res = parse_rls_headers(_m);
  322. if (res == -1) {
  323. LOG(L_INFO, "handle_rls_subscription(): problems parsing headers.\n");
  324. if (send_err) {
  325. add_response_header(_m, "Reason-Phrase: Bad or missing headers\r\n");
  326. send_reply(_m, 400, "Bad Request");
  327. }
  328. clear_last_query();
  329. PROF_STOP(rls_handle_subscription)
  330. return -1;
  331. }
  332. if (check_message(_m, send_err) != 0) {
  333. DBG("check message failed\n");
  334. clear_last_query();
  335. PROF_STOP(rls_handle_subscription)
  336. return -1;
  337. }
  338. if (has_to_tag(_m)) {
  339. /* handle SUBSCRIBE for an existing subscription */
  340. res = handle_renew_subscription(_m, send_err);
  341. }
  342. else {
  343. /* handle SUBSCRIBE for a new subscription */
  344. res = handle_new_subscription(_m, &query, send_err);
  345. }
  346. clear_last_query();
  347. PROF_STOP(rls_handle_subscription)
  348. if (res == 0) return 1;
  349. else return -1;
  350. }
  351. /*****************************************************************/
  352. /* XCAP query functions accessible from the CFG script */
  353. /* Get resource-list URI from SUBSCRIBE request */
  354. static int get_dst_uri(struct sip_msg* _m, str* dst_uri)
  355. {
  356. /* FIXME: get raw request URI? or from TO?
  357. * FIXME: skip uri parameters and everything else, leave only
  358. * sip:xxx@yyy ???!!! */
  359. str uri;
  360. if (_m->new_uri.s) {
  361. uri.s = _m->new_uri.s;
  362. uri.len = _m->new_uri.len;
  363. } else {
  364. uri.s = _m->first_line.u.request.uri.s;
  365. uri.len = _m->first_line.u.request.uri.len;
  366. }
  367. if (dst_uri) *dst_uri = uri;
  368. return RES_OK;
  369. }
  370. int query_rls_services(struct sip_msg* _m, char *a, char *b)
  371. {
  372. str uri;
  373. static str package = STR_STATIC_INIT("presence");
  374. /* TODO: take package from Event header or allow packages
  375. * given as parameter? */
  376. PROF_START(rls_query_rls_sevices)
  377. clear_last_query();
  378. if (fill_xcap_params) fill_xcap_params(_m, &query.xcap_params);
  379. if (get_dst_uri(_m, &uri) < 0) {
  380. ERR("can't get destination URI\n");
  381. clear_last_query();
  382. PROF_STOP(rls_query_rls_sevices)
  383. return -1;
  384. }
  385. if (xcap_query_rls_services(&query.xcap_params,
  386. &uri, &package, &query.flat_list) < 0) {
  387. ERR("XCAP query problems for uri %.*s\n", FMT_STR(uri));
  388. clear_last_query();
  389. PROF_STOP(rls_query_rls_sevices)
  390. return -1;
  391. }
  392. query.have_flat_list = 1;
  393. PROF_STOP(rls_query_rls_sevices)
  394. return 1;
  395. }
  396. int query_resource_list(struct sip_msg* _m, char *list_name, char *b)
  397. {
  398. int res;
  399. str_t uid;
  400. PROF_START(rls_query_resource_list)
  401. clear_last_query();
  402. if (fill_xcap_params) fill_xcap_params(_m, &query.xcap_params);
  403. if (get_from_uid(&uid, _m) < 0) {
  404. ERR("can't get From uid\n");
  405. clear_last_query();
  406. PROF_STOP(rls_query_resource_list)
  407. return -1;
  408. }
  409. res = get_resource_list_from_full_doc(&uid,
  410. NULL, /* TODO: filename */
  411. &query.xcap_params,
  412. list_name, &query.flat_list);
  413. /* TODO: add function for real XCAP server */
  414. if (res < 0) {
  415. ERR("XCAP query problems\n");
  416. clear_last_query();
  417. PROF_STOP(rls_query_resource_list)
  418. return -1;
  419. }
  420. query.have_flat_list = 1;
  421. PROF_STOP(rls_query_resource_list)
  422. return 1;
  423. }
  424. int have_flat_list(struct sip_msg* _m, char *a, char *b)
  425. {
  426. PROF_START(rls_have_flat_list)
  427. if (query.have_flat_list) {
  428. PROF_STOP(rls_have_flat_list)
  429. return 1;
  430. }
  431. else {
  432. PROF_STOP(rls_have_flat_list)
  433. return -1;
  434. }
  435. }