sanity.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  2. * $Id$
  3. *
  4. * Sanity Checks Module
  5. *
  6. * Copyright (C) 2006 iptelorg GbmH
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. */
  30. #include "mod_sanity.h"
  31. #include "sanity.h"
  32. #include "../../ut.h"
  33. #include "../../trim.h"
  34. #include "../../data_lump_rpl.h"
  35. #include "../../mem/mem.h"
  36. #include "../../parser/parse_uri.h"
  37. #include "../../parser/parse_expires.h"
  38. #include "../../parser/parse_content.h"
  39. #include "../../parser/digest/digest.h"
  40. #include "../../parser/contact/parse_contact.h"
  41. #include "../../parser/parse_to.h"
  42. #include "../../parser/parse_from.h"
  43. #define UNSUPPORTED_HEADER "Unsupported: "
  44. #define UNSUPPORTED_HEADER_LEN (sizeof(UNSUPPORTED_HEADER)-1)
  45. /**
  46. * wrapper to SL send reply function
  47. * - check if it is the case for sending a reply before doing it
  48. */
  49. int sanity_reply(sip_msg_t *msg, int code, char *reason)
  50. {
  51. if(msg->first_line.type == SIP_REPLY) {
  52. return 1;
  53. }
  54. if(msg->REQ_METHOD == METHOD_ACK) {
  55. return 1;
  56. }
  57. if(slb.zreply(msg, code, reason) < 0) {
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. /* check if the given string is a valid unsigned int value */
  63. int str2valid_uint(str* _number, unsigned int* _result) {
  64. int i;
  65. int result= 0;
  66. int equal = 1;
  67. char mui[10] = "4294967296";
  68. *_result = 0;
  69. if (_number->len > 10) {
  70. #ifdef EXTRA_DEBUG
  71. DBG("valid_uint(): number is too long\n");
  72. #endif
  73. return -1;
  74. }
  75. if (_number->len < 10) {
  76. equal = 0;
  77. }
  78. for (i=0; i < _number->len; i++) {
  79. if (_number->s[i] < '0' || _number->s[i] > '9') {
  80. #ifdef EXTRA_DEBUG
  81. DBG("valid_uint(): number contains non-number char\n");
  82. #endif
  83. return -1;
  84. }
  85. if (equal == 1) {
  86. if (_number->s[i] < mui[i]) {
  87. equal = 0;
  88. }
  89. else if (_number->s[i] > mui[i]) {
  90. #ifdef EXTRA_DEBUG
  91. DBG("valid_uint(): number exceeds uint\n");
  92. #endif
  93. return -1;
  94. }
  95. }
  96. result *= 10;
  97. result += _number->s[i] - '0';
  98. }
  99. *_result = result;
  100. return 0;
  101. }
  102. /* parses the given comma seperated string into a string list */
  103. strl* parse_str_list(str* _string) {
  104. str input;
  105. strl *parsed_list, *pl;
  106. char *comma;
  107. /* make a copy because we trim it */
  108. input.s = _string->s;
  109. input.len = _string->len;
  110. trim(&input);
  111. if (input.len == 0) {
  112. #ifdef EXTRA_DEBUG
  113. DBG("parse_str_list: list is empty\n");
  114. #endif
  115. return NULL;
  116. }
  117. parsed_list = pkg_malloc(sizeof(strl));
  118. if (parsed_list == NULL) {
  119. LOG(L_ERR, "parse_str_list: OUT OF MEMORY for initial list element\n");
  120. return NULL;
  121. }
  122. memset(parsed_list, 0, sizeof(strl));
  123. parsed_list->string.s = input.s;
  124. parsed_list->string.len = input.len;
  125. comma = q_memchr(input.s, ',', input.len);
  126. pl = parsed_list;
  127. while (comma != NULL) {
  128. pl->next = pkg_malloc(sizeof(strl));
  129. if (pl->next == NULL) {
  130. LOG(L_ERR, "parse_str_list: OUT OF MEMORY for further list"
  131. " element\n");
  132. return parsed_list;
  133. }
  134. memset(pl->next, 0, sizeof(strl));
  135. pl->next->string.s = comma + 1;
  136. pl->next->string.len = pl->string.len
  137. - (pl->next->string.s - pl->string.s);
  138. pl->string.len = comma - pl->string.s;
  139. trim_trailing(&(pl->string));
  140. pl = pl->next;
  141. trim_leading(&(pl->string));
  142. comma = q_memchr(pl->string.s, ',', pl->string.len);
  143. }
  144. return parsed_list;
  145. }
  146. /* free the elements of the linked str list */
  147. void free_str_list(strl *_list) {
  148. strl *cur, *next;
  149. if (_list != NULL) {
  150. cur = _list;
  151. while (cur != NULL) {
  152. next = cur->next;
  153. pkg_free(cur);
  154. cur = next;
  155. }
  156. }
  157. }
  158. int parse_proxyrequire(struct hdr_field* _h) {
  159. strl *pr_l;
  160. if (_h->parsed) {
  161. return 0; /* Already parsed */
  162. }
  163. if ((pr_l = parse_str_list(&(_h->body))) == NULL) {
  164. LOG(L_ERR, "parse_proxy_require(): Error while parsing\n");
  165. return -1;
  166. }
  167. _h->parsed = pr_l;
  168. return 0;
  169. }
  170. /* check the SIP version in the request URI */
  171. int check_ruri_sip_version(struct sip_msg* _msg) {
  172. char *sep;
  173. str version;
  174. #ifdef EXTRA_DEBUG
  175. DBG("check_ruri_sip_version entered\n");
  176. #endif
  177. if (_msg->first_line.u.request.version.len != 0) {
  178. sep = q_memchr(_msg->first_line.u.request.version.s, '/',
  179. _msg->first_line.u.request.version.len);
  180. if (sep == NULL) {
  181. LOG(L_WARN, "sanity_check(): check_ruri_sip_version():"
  182. " failed to find / in ruri version\n");
  183. return SANITY_CHECK_FAILED;
  184. }
  185. version.s = sep + 1;
  186. version.len = _msg->first_line.u.request.version.len - (version.s - _msg->first_line.u.request.version.s);
  187. if (version.len != SIP_VERSION_TWO_POINT_ZERO_LENGTH ||
  188. (memcmp(version.s, SIP_VERSION_TWO_POINT_ZERO,
  189. SIP_VERSION_TWO_POINT_ZERO_LENGTH) != 0)) {
  190. if (_msg->REQ_METHOD != METHOD_ACK) {
  191. if (sanity_reply(_msg, 505, "Version Not Supported (R-URI)")
  192. < 0) {
  193. LOG(L_WARN, "sanity_check(): check_ruri_sip_version():"
  194. " failed to send 505 via sl reply\n");
  195. }
  196. }
  197. #ifdef EXTRA_DEBUG
  198. DBG("check_ruri_sip_version failed\n");
  199. #endif
  200. return SANITY_CHECK_FAILED;
  201. }
  202. }
  203. #ifdef EXTRA_DEBUG
  204. DBG("check_ruri_sip_version passed\n");
  205. #endif
  206. return SANITY_CHECK_PASSED;
  207. }
  208. /* check if the r-uri scheme */
  209. int check_ruri_scheme(struct sip_msg* _msg) {
  210. #ifdef EXTRA_DEBUG
  211. DBG("check_ruri_scheme entered\n");
  212. #endif
  213. if (_msg->parsed_uri_ok == 0 &&
  214. parse_sip_msg_uri(_msg) != 1) {
  215. /* unsupported schemes end up here already */
  216. LM_WARN("failed to parse request uri [%.*s]\n",
  217. GET_RURI(_msg)->len, GET_RURI(_msg)->s);
  218. if (_msg->REQ_METHOD != METHOD_ACK) {
  219. if (slb.zreply(_msg, 400, "Bad Request URI") < 0) {
  220. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  221. " failed to send 400 via sl reply (bad ruri)\n");
  222. }
  223. }
  224. return SANITY_CHECK_FAILED;
  225. }
  226. if (_msg->parsed_uri.type == ERROR_URI_T) {
  227. if (_msg->REQ_METHOD != METHOD_ACK) {
  228. if (sanity_reply(_msg, 416, "Unsupported URI Scheme in Request URI")
  229. < 0) {
  230. LOG(L_WARN, "sanity_check(): check_ruri_scheme():"
  231. " failed to send 416 via sl reply\n");
  232. }
  233. }
  234. DBG("check_ruri_scheme failed\n");
  235. return SANITY_CHECK_FAILED;
  236. }
  237. #ifdef EXTRA_DEBUG
  238. DBG("check_ruri_scheme passed\n");
  239. #endif
  240. return SANITY_CHECK_PASSED;
  241. }
  242. /* check for the presence of the minimal required headers */
  243. int check_required_headers(struct sip_msg* _msg) {
  244. #ifdef EXTRA_DEBUG
  245. DBG("check_required_headers entered\n");
  246. #endif
  247. if (!check_transaction_quadruple(_msg)) {
  248. if (_msg->REQ_METHOD != METHOD_ACK) {
  249. if (sanity_reply(_msg, 400, "Missing Required Header in Request")
  250. < 0) {
  251. LOG(L_WARN, "sanity_check(): check_required_headers():"
  252. " failed to send 400 via sl reply\n");
  253. }
  254. }
  255. DBG("check_required_headers failed\n");
  256. return SANITY_CHECK_FAILED;
  257. }
  258. /* TODO: check for other required headers according to request type */
  259. #ifdef EXTRA_DEBUG
  260. DBG("check_required_headers passed\n");
  261. #endif
  262. return SANITY_CHECK_PASSED;
  263. }
  264. /* check if the SIP version in the Via header is 2.0 */
  265. int check_via_sip_version(struct sip_msg* _msg) {
  266. DBG("sanity_check(): check_via_sip_version(): this is a useless check"
  267. " for now; check the source code comments for details\n");
  268. return SANITY_CHECK_PASSED;
  269. /* FIMXE the Via parser fails already on non-2.0 versions
  270. * thus this check makes no sence yet
  271. DBG("check_via_sip_version entered\n");
  272. // FIXME via parser fails on non 2.0 number
  273. if (parse_headers(_msg, HDR_VIA1_F, 0) != 0) {
  274. LOG(L_WARN, "sanity_check(): check_via_sip_version():"
  275. " failed to parse the first Via header\n");
  276. return SANITY_CHECK_FAILED;
  277. }
  278. if (_msg->via1->version.len != 3 ||
  279. memcmp(_msg->via1->version.s, SIP_VERSION_TWO_POINT_ZERO,
  280. SIP_VERSION_TWO_POINT_ZERO_LENGTH ) != 0) {
  281. if (_msg->REQ_METHOD != METHOD_ACK) {
  282. if (sanity_reply(_msg, 505, "Version Not Supported (Via)") < 0) {
  283. LOG(L_WARN, "sanity_check(): check_via_sip_version():"
  284. " failed to send 505 via sl reply\n");
  285. }
  286. }
  287. DBG("check_via_sip_version failed\n");
  288. return SANITY_CHECK_FAILED;
  289. }
  290. #ifdef EXTRA_DEBUG
  291. DBG("check_via_sip_version passed\n");
  292. #endif
  293. return SANITY_CHECK_PASSED;
  294. */
  295. }
  296. /* compare the protocol string in the Via header with the transport */
  297. int check_via_protocol(struct sip_msg* _msg) {
  298. DBG("sanity_check(): check_via_protocol(): this is a useless check"
  299. " for now; check the source code comment for details\n");
  300. return SANITY_CHECK_PASSED;
  301. /* FIXME as the Via parser fails already on unknown transports
  302. * this function makes no sence yet
  303. DBG("check_via_protocol entered\n");
  304. // FIXME via parser fails on unknown transport
  305. if (parse_headers(_msg, HDR_VIA1_F, 0) != 0) {
  306. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  307. " failed to parse the first Via header\n");
  308. return SANITY_CHECK_FAILED;
  309. }
  310. if (_msg->via1->transport.len != 3 &&
  311. _msg->via1->transport.len != 4) {
  312. if (_msg->REQ_METHOD != METHOD_ACK) {
  313. if (sanity_reply(_msg, 400, "Unsupported Transport in Topmost Via")
  314. < 0) {
  315. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  316. " failed to send 400 via sl reply\n");
  317. }
  318. }
  319. DBG("check_via_protocol failed\n");
  320. return SANITY_CHECK_FAILED;
  321. }
  322. switch (_msg->rcv.proto) {
  323. case PROTO_UDP:
  324. if (memcmp(_msg->via1->transport.s, "UDP", 3) != 0) {
  325. if (_msg->REQ_METHOD != METHOD_ACK) {
  326. if (sanity_reply(_msg, 400,
  327. "Transport Missmatch in Topmost Via") < 0) {
  328. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  329. " failed to send 505 via sl reply\n");
  330. }
  331. }
  332. DBG("check_via_protocol failed\n");
  333. return SANITY_CHECK_FAILED;
  334. }
  335. break;
  336. case PROTO_TCP:
  337. if (memcmp(_msg->via1->transport.s, "TCP", 3) != 0) {
  338. if (_msg->REQ_METHOD != METHOD_ACK) {
  339. if (sanity_reply(_msg, 400,
  340. "Transport Missmatch in Topmost Via") < 0) {
  341. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  342. " failed to send 505 via sl reply\n");
  343. }
  344. }
  345. DBG("check_via_protocol failed\n");
  346. return SANITY_CHECK_FAILED;
  347. }
  348. break;
  349. case PROTO_TLS:
  350. if (memcmp(_msg->via1->transport.s, "TLS", 3) != 0) {
  351. if (_msg->REQ_METHOD != METHOD_ACK) {
  352. if (sanity_reply(_msg, 400,
  353. "Transport Missmatch in Topmost Via") < 0) {
  354. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  355. " failed to send 505 via sl reply\n");
  356. }
  357. }
  358. DBG("check_via_protocol failed\n");
  359. return SANITY_CHECK_FAILED;
  360. }
  361. break;
  362. case PROTO_SCTP:
  363. if (memcmp(_msg->via1->transport.s, "SCTP", 4) != 0) {
  364. if (_msg->REQ_METHOD != METHOD_ACK) {
  365. if (sanity_reply(_msg, 400,
  366. "Transport Missmatch in Topmost Via") < 0) {
  367. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  368. " failed to send 505 via sl reply\n");
  369. }
  370. }
  371. DBG("check_via_protocol failed\n");
  372. return SANITY_CHECK_FAILED;
  373. }
  374. break;
  375. case PROTO_WS:
  376. if (memcmp(_msg->via1->transport.s, "WS", 2) != 0) {
  377. if (_msg->REQ_METHOD != METHOD_ACK) {
  378. if (sanity_reply(_msg, 400,
  379. "Transport Missmatch in Topmost Via") < 0) {
  380. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  381. " failed to send 505 via sl reply\n");
  382. }
  383. }
  384. DBG("check_via_protocol failed\n");
  385. return SANITY_CHECK_FAILED;
  386. }
  387. break;
  388. case PROTO_WSS:
  389. if (memcmp(_msg->via1->transport.s, "WSS", 3) != 0) {
  390. if (_msg->REQ_METHOD != METHOD_ACK) {
  391. if (sanity_reply(_msg, 400,
  392. "Transport Missmatch in Topmost Via") < 0) {
  393. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  394. " failed to send 505 via sl reply\n");
  395. }
  396. }
  397. DBG("check_via_protocol failed\n");
  398. return SANITY_CHECK_FAILED;
  399. }
  400. break;
  401. default:
  402. LOG(L_WARN, "sanity_check(): check_via_protocol():"
  403. " unknown protocol in received structure\n");
  404. return SANITY_CHECK_FAILED;
  405. }
  406. #ifdef EXTRA_DEBUG
  407. DBG("check_via_protocol passed\n");
  408. #endif
  409. return SANITY_CHECK_PASSED;
  410. */
  411. }
  412. /* compare the method in the CSeq header with the request line value */
  413. int check_cseq_method(struct sip_msg* _msg) {
  414. #ifdef EXTRA_DEBUG
  415. DBG("check_cseq_method entered\n");
  416. #endif
  417. if (parse_headers(_msg, HDR_CSEQ_F, 0) != 0) {
  418. LOG(L_WARN, "sanity_check(): check_cseq_method():"
  419. " failed to parse the CSeq header\n");
  420. return SANITY_CHECK_FAILED;
  421. }
  422. if (_msg->cseq != NULL && _msg->cseq->parsed != NULL) {
  423. if (((struct cseq_body*)_msg->cseq->parsed)->method.len == 0) {
  424. if (_msg->REQ_METHOD != METHOD_ACK) {
  425. if (sanity_reply(_msg, 400, "Missing method in CSeq header")
  426. < 0) {
  427. LOG(L_WARN, "sanity_check(): check_cseq_method():"
  428. " failed to send 400 via sl reply\n");
  429. }
  430. }
  431. DBG("check_cseq_method failed (missing method)\n");
  432. return SANITY_CHECK_FAILED;
  433. }
  434. if (((struct cseq_body*)_msg->cseq->parsed)->method.len !=
  435. _msg->first_line.u.request.method.len ||
  436. memcmp(((struct cseq_body*)_msg->cseq->parsed)->method.s,
  437. _msg->first_line.u.request.method.s,
  438. ((struct cseq_body*)_msg->cseq->parsed)->method.len) != 0) {
  439. if (_msg->REQ_METHOD != METHOD_ACK) {
  440. if (sanity_reply(_msg, 400,
  441. "CSeq method does not match request method") < 0) {
  442. LOG(L_WARN, "sanity_check(): check_cseq_method():"
  443. " failed to send 400 via sl reply 2\n");
  444. }
  445. }
  446. DBG("check_cseq_method failed (non-equal method)\n");
  447. return SANITY_CHECK_FAILED;
  448. }
  449. }
  450. else {
  451. LOG(L_WARN, "sanity_check(): check_cseq_method():"
  452. " missing CSeq header\n");
  453. return SANITY_CHECK_FAILED;
  454. }
  455. #ifdef EXTRA_DEBUG
  456. DBG("check_cseq_method passed\n");
  457. #endif
  458. return SANITY_CHECK_PASSED;
  459. }
  460. /* check the number within the CSeq header */
  461. int check_cseq_value(struct sip_msg* _msg) {
  462. unsigned int cseq;
  463. #ifdef EXTRA_DEBUG
  464. DBG("check_cseq_value entered\n");
  465. #endif
  466. if (parse_headers(_msg, HDR_CSEQ_F, 0) != 0) {
  467. LOG(L_WARN, "sanity_check(): check_cseq_value():"
  468. " failed to parse the CSeq header\n");
  469. return SANITY_CHECK_FAILED;
  470. }
  471. if (_msg->cseq != NULL && _msg->cseq->parsed != NULL) {
  472. if (((struct cseq_body*)_msg->cseq->parsed)->number.len == 0) {
  473. if (_msg->REQ_METHOD != METHOD_ACK) {
  474. if (sanity_reply(_msg, 400, "Missing number in CSeq header")
  475. < 0) {
  476. LOG(L_WARN, "sanity_check(): check_cseq_value():"
  477. " failed to send 400 via sl reply\n");
  478. }
  479. }
  480. return SANITY_CHECK_FAILED;
  481. }
  482. if (str2valid_uint(&((struct cseq_body*)_msg->cseq->parsed)->number,
  483. &cseq) != 0) {
  484. if (_msg->REQ_METHOD != METHOD_ACK) {
  485. if (sanity_reply(_msg, 400, "CSeq number is illegal") < 0) {
  486. LOG(L_WARN, "sanity_check(): check_cseq_value():"
  487. " failed to send 400 via sl reply 2\n");
  488. }
  489. }
  490. DBG("check_cseq_value failed\n");
  491. return SANITY_CHECK_FAILED;
  492. }
  493. }
  494. else {
  495. LOG(L_WARN, "sanity_check(): check_cseq_method():"
  496. " missing CSeq header\n");
  497. return SANITY_CHECK_FAILED;
  498. }
  499. #ifdef EXTRA_DEBUG
  500. DBG("check_cseq_value passed\n");
  501. #endif
  502. return SANITY_CHECK_PASSED;
  503. }
  504. /* compare the Content-Length value with the accutal body length */
  505. int check_cl(struct sip_msg* _msg) {
  506. char *body;
  507. #ifdef EXTRA_DEBUG
  508. DBG("check_cl entered\n");
  509. #endif
  510. if (parse_headers(_msg, HDR_CONTENTLENGTH_F, 0) != 0) {
  511. LOG(L_WARN, "sanity_check(): check_cl():"
  512. " failed to parse content-length header\n");
  513. return SANITY_CHECK_FAILED;
  514. }
  515. if (_msg->content_length != NULL) {
  516. //dump_hdr_field(_msg->content_length);
  517. if ((body = get_body(_msg)) == NULL) {
  518. #ifdef EXTRA_DEBUG
  519. DBG("check_cl(): no body\n");
  520. #endif
  521. return SANITY_CHECK_FAILED;
  522. }
  523. if ((_msg->len - (body - _msg->buf)) != get_content_length(_msg)) {
  524. if (_msg->REQ_METHOD != METHOD_ACK) {
  525. if (sanity_reply(_msg, 400, "Content-Length mis-match") < 0) {
  526. LOG(L_WARN, "sanity_check(): check_cl():"
  527. " failed to send 400 via sl reply\n");
  528. }
  529. }
  530. DBG("check_cl failed\n");
  531. return SANITY_CHECK_FAILED;
  532. }
  533. #ifdef EXTRA_DEBUG
  534. DBG("check_cl passed\n");
  535. #endif
  536. }
  537. #ifdef EXTRA_DEBUG
  538. else {
  539. WARN("check_cl(): content length header missing in request\n");
  540. }
  541. #endif
  542. return SANITY_CHECK_PASSED;
  543. }
  544. /* check the number within the Expires header */
  545. int check_expires_value(struct sip_msg* _msg) {
  546. unsigned int expires;
  547. #ifdef EXTRA_DEBUG
  548. DBG("check_expires_value entered\n");
  549. #endif
  550. if (parse_headers(_msg, HDR_EXPIRES_F, 0) != 0) {
  551. LOG(L_WARN, "sanity_check(): check_expires_value():"
  552. " failed to parse expires header\n");
  553. return SANITY_CHECK_FAILED;
  554. }
  555. if (_msg->expires != NULL) {
  556. //dump_hdr_field(_msg->expires);
  557. if (_msg->expires->parsed == NULL &&
  558. parse_expires(_msg->expires) < 0) {
  559. LOG(L_WARN, "sanity_check(): check_expires_value():"
  560. " parse_expires failed\n");
  561. return SANITY_CHECK_FAILED;
  562. }
  563. if (((struct exp_body*)_msg->expires->parsed)->text.len == 0) {
  564. if (_msg->REQ_METHOD != METHOD_ACK) {
  565. if (sanity_reply(_msg, 400, "Missing number in Expires header")
  566. < 0) {
  567. LOG(L_WARN, "sanity_check(): check_expires_value():"
  568. " failed to send 400 via sl reply\n");
  569. }
  570. }
  571. DBG("check_expires_value failed\n");
  572. return SANITY_CHECK_FAILED;
  573. }
  574. if (str2valid_uint(&((struct exp_body*)_msg->expires->parsed)->text, &expires) != 0) {
  575. if (_msg->REQ_METHOD != METHOD_ACK) {
  576. if (sanity_reply(_msg, 400, "Expires value is illegal") < 0) {
  577. LOG(L_WARN, "sanity_check(): check_expires_value():"
  578. " failed to send 400 via sl reply 2\n");
  579. }
  580. }
  581. DBG("check_expires_value failed\n");
  582. return SANITY_CHECK_FAILED;
  583. }
  584. #ifdef EXTRA_DEBUG
  585. DBG("check_expires_value passed\n");
  586. #endif
  587. }
  588. #ifdef EXTRA_DEBUG
  589. else {
  590. DBG("check_expires_value(): no expires header found\n");
  591. }
  592. #endif
  593. return SANITY_CHECK_PASSED;
  594. }
  595. /* check the content of the Proxy-Require header */
  596. int check_proxy_require(struct sip_msg* _msg) {
  597. strl *r_pr, *l_pr;
  598. char *u;
  599. int u_len;
  600. #ifdef EXTRA_DEBUG
  601. DBG("check_proxy_require entered\n");
  602. #endif
  603. if (parse_headers(_msg, HDR_PROXYREQUIRE_F, 0) != 0) {
  604. LOG(L_WARN, "sanity_check(): check_proxy_require():"
  605. " failed to parse proxy require header\n");
  606. return SANITY_CHECK_FAILED;
  607. }
  608. if (_msg->proxy_require != NULL) {
  609. dump_hdr_field(_msg->proxy_require);
  610. if (_msg->proxy_require->parsed == NULL &&
  611. parse_proxyrequire(_msg->proxy_require) < 0) {
  612. LOG(L_WARN, "sanity_check(): check_proxy_require(): parse_proxy_require failed\n");
  613. return SANITY_CHECK_FAILED;
  614. }
  615. r_pr = _msg->proxy_require->parsed;
  616. while (r_pr != NULL) {
  617. l_pr = proxyrequire_list;
  618. while (l_pr != NULL) {
  619. #ifdef EXTRA_DEBUG
  620. DBG("check_proxy_require(): comparing r='%.*s' l='%.*s'\n",
  621. r_pr->string.len, r_pr->string.s, l_pr->string.len,
  622. l_pr->string.s);
  623. #endif
  624. if (l_pr->string.len == r_pr->string.len &&
  625. /* FIXME tokens are case in-sensitive */
  626. memcmp(l_pr->string.s, r_pr->string.s,
  627. l_pr->string.len) == 0) {
  628. break;
  629. }
  630. l_pr = l_pr->next;
  631. }
  632. if (l_pr == NULL) {
  633. DBG("sanit_check(): check_proxy_require():"
  634. " request contains unsupported extension: %.*s\n",
  635. r_pr->string.len, r_pr->string.s);
  636. u_len = UNSUPPORTED_HEADER_LEN + 2 + r_pr->string.len;
  637. u = pkg_malloc(u_len);
  638. if (u == NULL) {
  639. LOG(L_ERR, "sanity_check(): check_proxy_require():"
  640. " failed to allocate memory for"
  641. " Unsupported header\n");
  642. }
  643. else {
  644. memcpy(u, UNSUPPORTED_HEADER, UNSUPPORTED_HEADER_LEN);
  645. memcpy(u + UNSUPPORTED_HEADER_LEN, r_pr->string.s,
  646. r_pr->string.len);
  647. memcpy(u + UNSUPPORTED_HEADER_LEN + r_pr->string.len,
  648. CRLF, CRLF_LEN);
  649. add_lump_rpl(_msg, u, u_len, LUMP_RPL_HDR);
  650. }
  651. if (_msg->REQ_METHOD != METHOD_ACK) {
  652. if (sanity_reply(_msg, 420, "Bad Extension") < 0) {
  653. LOG(L_WARN, "sanity_check(): check_proxy_require():"
  654. " failed to send 420 via sl reply\n");
  655. }
  656. }
  657. #ifdef EXTRA_DEBUG
  658. DBG("check_proxy_require failed\n");
  659. #endif
  660. if (u) pkg_free(u);
  661. return SANITY_CHECK_FAILED;
  662. }
  663. else {
  664. r_pr = r_pr->next;
  665. }
  666. }
  667. #ifdef EXTRA_DEBUG
  668. DBG("check_proxy_require passed\n");
  669. #endif
  670. if (_msg->proxy_require->parsed) {
  671. /* TODO we have to free it here, because it is not automatically
  672. * freed when the message freed. Lets hope nobody needs to access
  673. * this header again later on */
  674. free_str_list(_msg->proxy_require->parsed);
  675. }
  676. }
  677. #ifdef EXTRA_DEBUG
  678. else {
  679. DBG("check_proxy_require(): no proxy-require header found\n");
  680. }
  681. #endif
  682. return SANITY_CHECK_PASSED;
  683. }
  684. /* check if the typical URI's are parseable */
  685. int check_parse_uris(struct sip_msg* _msg, int checks) {
  686. struct to_body *ft_body = NULL;
  687. struct sip_uri uri;
  688. #ifdef EXTRA_DEBUG
  689. DBG("check_parse_uris entered\n");
  690. #endif
  691. /* check R-URI */
  692. if (SANITY_URI_CHECK_RURI & checks) {
  693. #ifdef EXTRA_DEBUG
  694. DBG("check_parse_uris(): parsing ruri\n");
  695. #endif
  696. if (_msg->parsed_uri_ok == 0 &&
  697. parse_sip_msg_uri(_msg) != 1) {
  698. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  699. " failed to parse request uri\n");
  700. if (_msg->REQ_METHOD != METHOD_ACK) {
  701. if (sanity_reply(_msg, 400, "Bad Request URI") < 0) {
  702. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  703. " failed to send 400 via sl reply (bad ruri)\n");
  704. }
  705. }
  706. return SANITY_CHECK_FAILED;
  707. }
  708. /* FIXME: would it make sense to check here for "mandatory"
  709. * or "requested" parts of the URI? */
  710. }
  711. /* check From URI */
  712. if (SANITY_URI_CHECK_FROM & checks) {
  713. #ifdef EXTRA_DEBUG
  714. DBG("check_parse_uris(): looking up From header\n");
  715. #endif
  716. if ((!_msg->from && parse_headers(_msg, HDR_FROM_F, 0) != 0)
  717. || !_msg->from) {
  718. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  719. " missing from header\n");
  720. if (_msg->REQ_METHOD != METHOD_ACK) {
  721. if (sanity_reply(_msg, 400, "Missing From Header") < 0) {
  722. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  723. " failed to send 400 via sl reply (missing From)\n");
  724. }
  725. }
  726. return SANITY_CHECK_FAILED;
  727. }
  728. if (!_msg->from->parsed) {
  729. #ifdef EXTRA_DEBUG
  730. DBG("check_parse_uris(): parsing From header\n");
  731. #endif
  732. ft_body = pkg_malloc(sizeof(struct to_body));
  733. if (!ft_body) {
  734. LOG(L_ERR, "sanity_check(): check_parse_uris():"
  735. " out of pkg_memory (From)\n");
  736. return SANITY_CHECK_ERROR;
  737. }
  738. memset(ft_body, 0, sizeof(struct to_body));
  739. parse_to(_msg->from->body.s, _msg->from->body.s + \
  740. _msg->from->body.len + 1, ft_body);
  741. if (ft_body->error == PARSE_ERROR) {
  742. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  743. " failed to parse From header [%.*s]\n",
  744. _msg->from->body.len, _msg->from->body.s);
  745. free_to(ft_body);
  746. if (_msg->REQ_METHOD != METHOD_ACK) {
  747. if (sanity_reply(_msg, 400, "Bad From header") < 0) {
  748. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  749. " failed to send 400 via sl reply"
  750. " (bad from header)\n");
  751. }
  752. }
  753. return SANITY_CHECK_FAILED;
  754. }
  755. _msg->from->parsed = ft_body;
  756. ft_body = NULL;
  757. }
  758. if (((struct to_body*)_msg->from->parsed)->uri.s) {
  759. #ifdef EXTRA_DEBUG
  760. DBG("check_parse_uris(): parsing From URI\n");
  761. #endif
  762. if (parse_uri(((struct to_body*)_msg->from->parsed)->uri.s,
  763. ((struct to_body*)_msg->from->parsed)->uri.len, &uri) != 0) {
  764. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  765. " failed to parse From uri\n");
  766. if (_msg->REQ_METHOD != METHOD_ACK) {
  767. if (sanity_reply(_msg, 400, "Bad From URI") < 0) {
  768. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  769. " failed to send 400 via sl reply"
  770. " (bad from uri)\n");
  771. }
  772. }
  773. return SANITY_CHECK_FAILED;
  774. }
  775. /* FIXME: we should store this parsed struct somewhere so that
  776. * it could be re-used */
  777. /* FIXME 2: would it make sense to check here for "mandatory"
  778. * or "requested" parts of the URI? */
  779. }
  780. }
  781. /* check To URI */
  782. if (SANITY_URI_CHECK_TO & checks) {
  783. #ifdef EXTRA_DEBUG
  784. DBG("check_parse_uris(): looking up To header\n");
  785. #endif
  786. if ((!_msg->to && parse_headers(_msg, HDR_TO_F, 0) != 0)
  787. || !_msg->to) {
  788. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  789. " missing to header\n");
  790. if (_msg->REQ_METHOD != METHOD_ACK) {
  791. if (sanity_reply(_msg, 400, "Missing To Header") < 0) {
  792. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  793. " failed to send 400 via sl reply (missing To)\n");
  794. }
  795. }
  796. return SANITY_CHECK_FAILED;
  797. }
  798. /* parse_to is automatically called for HDR_TO_F */
  799. if (!_msg->to->parsed) {
  800. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  801. " failed to parse To header\n");
  802. if (_msg->REQ_METHOD != METHOD_ACK) {
  803. if (sanity_reply(_msg, 400, "Bad To URI") < 0) {
  804. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  805. " failed to send 400 via sl reply (bad to uri)\n");
  806. }
  807. }
  808. return SANITY_CHECK_FAILED;
  809. }
  810. if (((struct to_body*)_msg->to->parsed)->uri.s) {
  811. #ifdef EXTRA_DEBUG
  812. DBG("check_parse_uris(): parsing To URI\n");
  813. #endif
  814. if (parse_uri(((struct to_body*)_msg->to->parsed)->uri.s,
  815. ((struct to_body*)_msg->to->parsed)->uri.len, &uri) != 0) {
  816. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  817. " failed to parse To uri\n");
  818. if (_msg->REQ_METHOD != METHOD_ACK) {
  819. if (sanity_reply(_msg, 400, "Bad To URI") < 0) {
  820. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  821. " failed to send 400 via sl reply"
  822. " (bad to uri)\n");
  823. }
  824. }
  825. return SANITY_CHECK_FAILED;
  826. }
  827. /* FIXME: we should store this parsed struct somewhere so that
  828. * it could be re-used */
  829. /* FIXME 2: would it make sense to check here for "mandatory"
  830. * or "requested" parts of the URI? */
  831. }
  832. }
  833. /* check Contact URI */
  834. if (SANITY_URI_CHECK_CONTACT & checks) {
  835. #ifdef EXTRA_DEBUG
  836. DBG("check_parse_uris(): looking up Contact header\n");
  837. #endif
  838. if ((!_msg->contact && parse_headers(_msg, HDR_CONTACT_F, 0) != 0)
  839. || !_msg->contact) {
  840. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  841. " missing contact header\n");
  842. }
  843. if (_msg->contact) {
  844. #ifdef EXTRA_DEBUG
  845. DBG("check_parse_uris(): parsing Contact header\n");
  846. #endif
  847. if (parse_contact(_msg->contact) < 0) {
  848. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  849. " failed to parse Contact header\n");
  850. if (_msg->REQ_METHOD != METHOD_ACK) {
  851. if (sanity_reply(_msg, 400, "Bad Contact Header") < 0) {
  852. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  853. " failed to send 400 via send_reply"
  854. " (bad Contact)\n");
  855. }
  856. }
  857. return SANITY_CHECK_FAILED;
  858. }
  859. if (parse_uri(
  860. ((struct contact_body*)_msg->contact->parsed)->contacts->uri.s,
  861. ((struct contact_body*)_msg->contact->parsed)->contacts->uri.len,
  862. &uri) != 0) {
  863. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  864. " failed to parse Contact uri\n");
  865. if (_msg->REQ_METHOD != METHOD_ACK) {
  866. if (sanity_reply(_msg, 400, "Bad Contact URI") < 0) {
  867. LOG(L_WARN, "sanity_check(): check_parse_uris():"
  868. " failed to send 400 via send_reply"
  869. " (bad Contact uri)\n");
  870. }
  871. }
  872. return SANITY_CHECK_FAILED;
  873. }
  874. }
  875. }
  876. #ifdef EXTRA_DEBUG
  877. DBG("check_parse_uris passed\n");
  878. #endif
  879. return SANITY_CHECK_PASSED;
  880. }
  881. /* Make sure that username attribute in all digest credentials
  882. * instances has a meaningful value
  883. */
  884. int check_digest(struct sip_msg* msg, int checks)
  885. {
  886. struct hdr_field* ptr;
  887. dig_cred_t* cred;
  888. int ret;
  889. int hf_type;
  890. if (parse_headers(msg, HDR_EOH_F, 0) != 0) {
  891. LOG(L_ERR, "sanity_check(): check_digest:"
  892. " failed to parse proxy require header\n");
  893. return SANITY_CHECK_FAILED;
  894. }
  895. if (!msg->authorization && !msg->proxy_auth) {
  896. #ifdef EXTRA_DEBUG
  897. DBG("sanity_check(): check_digest: Nothing to check\n");
  898. #endif
  899. return SANITY_CHECK_PASSED;
  900. }
  901. if (msg->authorization) {
  902. hf_type = HDR_AUTHORIZATION_T;
  903. ptr = msg->authorization;
  904. } else {
  905. hf_type = HDR_PROXYAUTH_T;
  906. ptr = msg->proxy_auth;
  907. }
  908. while(ptr) {
  909. if ((ret = parse_credentials(ptr)) != 0) {
  910. DBG("sanity_check(): check_digest: Cannot parse credentials: %d\n",
  911. ret);
  912. return SANITY_CHECK_FAILED;
  913. }
  914. cred = &((auth_body_t*)ptr->parsed)->digest;
  915. if (check_dig_cred(cred) != E_DIG_OK) {
  916. #ifdef EXTRA_DEBUG
  917. DBG("sanity_check(): check_digest: Digest credentials malformed\n");
  918. #endif
  919. return SANITY_CHECK_FAILED;
  920. }
  921. if (cred->username.whole.len == 0) {
  922. #ifdef EXTRA_DEBUG
  923. DBG("sanity_check(): check_digest: Empty username\n");
  924. #endif
  925. return SANITY_CHECK_FAILED;
  926. }
  927. if (cred->nonce.len == 0) {
  928. #ifdef EXTRA_DEBUG
  929. DBG("sanity_check(): check_digest: Empty nonce attribute\n");
  930. #endif
  931. return SANITY_CHECK_FAILED;
  932. }
  933. if (cred->response.len == 0) {
  934. #ifdef EXTRA_DEBUG
  935. DBG("sanity_check(): check_digest: Empty response attribute\n");
  936. #endif
  937. return SANITY_CHECK_FAILED;
  938. }
  939. do {
  940. ptr = ptr->next;
  941. } while(ptr && ptr->type != hf_type);
  942. if (!ptr && hf_type == HDR_AUTHORIZATION_T) {
  943. hf_type = HDR_PROXYAUTH_T;
  944. ptr = msg->proxy_auth;
  945. }
  946. }
  947. return SANITY_CHECK_PASSED;
  948. }
  949. /* check for the presence of duplicate tag prameters in To/From headers */
  950. int check_duptags(sip_msg_t* _msg)
  951. {
  952. to_body_t *tb;
  953. to_param_t *tp;
  954. int n;
  955. if(parse_from_header(_msg)<0 || parse_to_header(_msg)<0) {
  956. DBG("check_duptags failed while parsing\n");
  957. return SANITY_CHECK_FAILED;
  958. }
  959. tb = get_from(_msg);
  960. if(tb->tag_value.s!=NULL) {
  961. n = 0;
  962. for(tp = tb->param_lst; tp; tp = tp->next) {
  963. if(tp->type==TAG_PARAM)
  964. n++;
  965. }
  966. if(n>1) {
  967. DBG("check_duptags failed for From header\n");
  968. return SANITY_CHECK_FAILED;
  969. }
  970. }
  971. tb = get_to(_msg);
  972. if(tb->tag_value.s!=NULL) {
  973. n = 0;
  974. for(tp = tb->param_lst; tp; tp = tp->next) {
  975. if(tp->type==TAG_PARAM)
  976. n++;
  977. }
  978. if(n>1) {
  979. DBG("check_duptags failed for To header\n");
  980. return SANITY_CHECK_FAILED;
  981. }
  982. }
  983. return SANITY_CHECK_PASSED;
  984. }