cpl_run.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * -------
  24. * 2003-01-23 : created (bogdan)
  25. * 2003-09-11 : build_lump_rpl() merged into add_lump_rpl() (bogdan)
  26. * 2004-06-14 : all global variables merged into cpl_env and cpl_fct;
  27. * append_branches param added to lookup node (bogdan)
  28. * 2004-06-14 : flag CPL_IS_STATEFUL is set now immediately after the
  29. * transaction is created (bogdan)
  30. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <stdlib.h>
  35. #include "../../mem/mem.h"
  36. #include "../../mem/shm_mem.h"
  37. #include "../../str.h"
  38. #include "../../ut.h"
  39. #include "../../dprint.h"
  40. #include "../../parser/msg_parser.h"
  41. #include "../../data_lump_rpl.h"
  42. #include "../../modules/tm/tm_load.h"
  43. #include "../usrloc/usrloc.h"
  44. #include "CPL_tree.h"
  45. #include "loc_set.h"
  46. #include "cpl_utils.h"
  47. #include "cpl_nonsig.h"
  48. #include "cpl_sig.h"
  49. #include "cpl_env.h"
  50. #include "cpl_run.h"
  51. #define EO_SCRIPT ((char*)0xffffffff)
  52. #define DEFAULT_ACTION ((char*)0xfffffffe)
  53. #define CPL_SCRIPT_ERROR ((char*)0xfffffffd)
  54. #define CPL_RUNTIME_ERROR ((char*)0xfffffffc)
  55. #define CPL_TO_CONTINUE ((char*)0xfffffffb)
  56. #define HDR_NOT_FOUND ((char*)0xffffffff)
  57. #define UNDEF_CHAR (0xff)
  58. static str cpl_301_reason = str_init("Moved permanently");
  59. static str cpl_302_reason = str_init("Moved temporarily");
  60. #define check_overflow_by_ptr(_ptr_,_intr_,_error_) \
  61. do {\
  62. if ( (char*)(_ptr_)>(_intr_)->script.len+(_intr_)->script.s ) {\
  63. LM_ERR("overflow detected ip=%p ptr=%p in "\
  64. "func. %s, line %d\n",(_intr_)->ip,_ptr_,__FILE__,__LINE__);\
  65. goto _error_; \
  66. } \
  67. }while(0)
  68. #define check_overflow_by_offset(_len_,_intr_,_error_) \
  69. do {\
  70. if ( (char*)((_intr_)->ip+(_len_)) > \
  71. (_intr_)->script.len+(_intr_)->script.s ) {\
  72. LM_ERR("overflow detected ip=%p offset=%d in "\
  73. "func. %s, line %d\n",(_intr_)->ip,_len_,__FILE__,__LINE__);\
  74. goto _error_; \
  75. } \
  76. }while(0)
  77. #define get_first_child(_node_) \
  78. ((NR_OF_KIDS(_node_)==0)?DEFAULT_ACTION:(_node_)+KID_OFFSET(_node_,0))
  79. #define get_basic_attr(_p_,_code_,_n_,_intr_,_error_) \
  80. do{\
  81. check_overflow_by_ptr( (_p_)+BASIC_ATTR_SIZE, _intr_, _error_);\
  82. _code_ = ntohs( *((unsigned short*)(_p_)) );\
  83. _n_ = ntohs( *((unsigned short*)((_p_)+2)) );\
  84. (_p_) += 4;\
  85. }while(0)
  86. #define get_str_attr(_p_,_s_,_len_,_intr_,_error_,_FIXUP_) \
  87. do{\
  88. if ( ((int)(_len_))-(_FIXUP_)<=0 ) {\
  89. LM_ERR("%s:%d: attribute is an empty string\n",\
  90. __FILE__,__LINE__);\
  91. goto _error_; \
  92. } else {\
  93. check_overflow_by_ptr( (_p_)+(_len_), _intr_, _error_);\
  94. _s_ = _p_;\
  95. (_p_) += (_len_) + 1*(((_len_)&0x0001)==1);\
  96. (_len_) -= (_FIXUP_);\
  97. }\
  98. }while(0)
  99. struct cpl_interpreter* new_cpl_interpreter( struct sip_msg *msg, str *script)
  100. {
  101. struct cpl_interpreter *intr = 0;
  102. intr = (struct cpl_interpreter*)shm_malloc(sizeof(struct cpl_interpreter));
  103. if (!intr) {
  104. LM_ERR("no more shm free memory!\n");
  105. goto error;
  106. }
  107. memset( intr, 0, sizeof(struct cpl_interpreter));
  108. /* init the interpreter*/
  109. intr->script.s = script->s;
  110. intr->script.len = script->len;
  111. intr->recv_time = time(0);
  112. intr->ip = script->s;
  113. intr->msg = msg;
  114. /* check the beginning of the script */
  115. if ( NODE_TYPE(intr->ip)!=CPL_NODE ) {
  116. LM_ERR("first node is not CPL!!\n");
  117. goto error;
  118. }
  119. return intr;
  120. error:
  121. return 0;
  122. }
  123. void free_cpl_interpreter(struct cpl_interpreter *intr)
  124. {
  125. if (intr) {
  126. empty_location_set( &(intr->loc_set) );
  127. if (intr->script.s)
  128. shm_free( intr->script.s);
  129. if (intr->user.s)
  130. shm_free(intr->user.s);
  131. if (intr->flags&CPL_RURI_DUPLICATED)
  132. shm_free(intr->ruri);
  133. if (intr->flags&CPL_TO_DUPLICATED)
  134. shm_free(intr->to);
  135. if (intr->flags&CPL_FROM_DUPLICATED)
  136. shm_free(intr->from);
  137. if (intr->flags&CPL_SUBJECT_DUPLICATED)
  138. shm_free(intr->subject);
  139. if (intr->flags&CPL_ORGANIZATION_DUPLICATED)
  140. shm_free(intr->organization);
  141. if (intr->flags&CPL_USERAGENT_DUPLICATED)
  142. shm_free(intr->user_agent);
  143. if (intr->flags&CPL_ACCEPTLANG_DUPLICATED)
  144. shm_free(intr->accept_language);
  145. if (intr->flags&CPL_PRIORITY_DUPLICATED)
  146. shm_free(intr->priority);
  147. shm_free(intr);
  148. }
  149. }
  150. /* UPDATED + CHECKED
  151. */
  152. static inline char *run_cpl_node( struct cpl_interpreter *intr )
  153. {
  154. char *kid;
  155. unsigned char start;
  156. int i;
  157. start = (intr->flags&CPL_RUN_INCOMING)?INCOMING_NODE:OUTGOING_NODE;
  158. /* look for the starting node (incoming or outgoing) */
  159. for(i=0;i<NR_OF_KIDS(intr->ip);i++) {
  160. kid= intr->ip + KID_OFFSET(intr->ip,i);
  161. if ( NODE_TYPE(kid)==start ) {
  162. return get_first_child(kid);
  163. } else
  164. if (NODE_TYPE(kid)==SUBACTION_NODE ||
  165. NODE_TYPE(kid)==ANCILLARY_NODE ||
  166. NODE_TYPE(kid)==INCOMING_NODE ||
  167. NODE_TYPE(kid)==OUTGOING_NODE ) {
  168. continue;
  169. } else {
  170. LM_ERR("unknown child type (%d) "
  171. "for CPL node!!\n",NODE_TYPE(kid));
  172. return CPL_SCRIPT_ERROR;
  173. }
  174. }
  175. LM_DBG("CPL node has no %d subnode -> default\n", start);
  176. return DEFAULT_ACTION;
  177. }
  178. /* UPDATED + CHECKED
  179. */
  180. static inline char *run_lookup( struct cpl_interpreter *intr )
  181. {
  182. unsigned short attr_name;
  183. unsigned short n;
  184. unsigned char clear;
  185. char *p;
  186. char *kid;
  187. char *failure_kid = 0;
  188. char *success_kid = 0;
  189. char *notfound_kid = 0;
  190. int i;
  191. time_t tc;
  192. urecord_t* r;
  193. ucontact_t* contact;
  194. clear = NO_VAL;
  195. /* check the params */
  196. for( i=NR_OF_ATTR(intr->ip),p=ATTR_PTR(intr->ip) ; i>0 ; i-- ) {
  197. get_basic_attr(p,attr_name,n,intr,script_error);
  198. switch (attr_name) {
  199. case CLEAR_ATTR:
  200. if (n!=YES_VAL && n!=NO_VAL)
  201. LM_WARN("invalid value (%u) found"
  202. " for param. CLEAR in LOOKUP node -> using "
  203. "default (%u)!\n",n,clear);
  204. else
  205. clear = n;
  206. break;
  207. default:
  208. LM_ERR("unknown attribute (%d) in LOOKUP node\n",attr_name);
  209. goto script_error;
  210. }
  211. }
  212. /* check the kids */
  213. for( i=0 ; i<NR_OF_KIDS(intr->ip) ; i++ ) {
  214. kid = intr->ip + KID_OFFSET(intr->ip,i);
  215. check_overflow_by_ptr( kid+SIMPLE_NODE_SIZE(kid), intr, script_error);
  216. switch ( NODE_TYPE(kid) ) {
  217. case SUCCESS_NODE :
  218. success_kid = kid;
  219. break;
  220. case NOTFOUND_NODE:
  221. notfound_kid = kid;
  222. break;
  223. case FAILURE_NODE:
  224. failure_kid = kid;
  225. break;
  226. default:
  227. LM_ERR("unknown output node type"
  228. " (%d) for LOOKUP node\n",NODE_TYPE(kid));
  229. goto script_error;
  230. }
  231. }
  232. kid = failure_kid;
  233. if (cpl_env.lu_domain) {
  234. /* fetch user's contacts via usrloc */
  235. tc = time(0);
  236. cpl_fct.ulb.lock_udomain( cpl_env.lu_domain, &intr->user );
  237. i = cpl_fct.ulb.get_urecord( cpl_env.lu_domain, &intr->user, &r);
  238. if (i < 0) {
  239. /* failure */
  240. LM_ERR("failed to query usrloc\n");
  241. cpl_fct.ulb.unlock_udomain( cpl_env.lu_domain, &intr->user );
  242. } else if (i > 0) {
  243. /* not found */
  244. LM_DBG("'%.*s' Not found in usrloc\n",
  245. intr->user.len, intr->user.s);
  246. cpl_fct.ulb.unlock_udomain( cpl_env.lu_domain, &intr->user );
  247. kid = notfound_kid;
  248. } else {
  249. contact = r->contacts;
  250. /* skip expired contacts */
  251. while ((contact) && (contact->expires <= tc))
  252. contact = contact->next;
  253. /* any contacts left? */
  254. if (contact) {
  255. /* clear loc set if requested */
  256. if (clear)
  257. empty_location_set( &(intr->loc_set) );
  258. /* start adding locations to set */
  259. do {
  260. LM_DBG("adding <%.*s>q=%d\n",
  261. contact->c.len,contact->c.s,(int)(10*contact->q));
  262. if (add_location( &(intr->loc_set), &contact->c,
  263. &contact->received, (int)(10*contact->q),
  264. CPL_LOC_DUPL|
  265. ((contact->cflags&cpl_fct.ulb.nat_flag)?CPL_LOC_NATED:0)
  266. )==-1) {
  267. LM_ERR("unable to add location to set :-(\n");
  268. cpl_fct.ulb.unlock_udomain( cpl_env.lu_domain, &intr->user );
  269. goto runtime_error;
  270. }
  271. contact = contact->next;
  272. }while( contact && cpl_env.lu_append_branches);
  273. /* set the flag for modifying the location set */
  274. intr->flags |= CPL_LOC_SET_MODIFIED;
  275. /* we found a valid contact */
  276. kid = success_kid;
  277. } else {
  278. /* no valid contact found */
  279. kid = notfound_kid;
  280. }
  281. cpl_fct.ulb.unlock_udomain( cpl_env.lu_domain, &intr->user );
  282. }
  283. }
  284. if (kid)
  285. return get_first_child(kid);
  286. return DEFAULT_ACTION;
  287. runtime_error:
  288. return CPL_RUNTIME_ERROR;
  289. script_error:
  290. return CPL_SCRIPT_ERROR;
  291. }
  292. /* UPDATED + CHECKED
  293. */
  294. static inline char *run_location( struct cpl_interpreter *intr )
  295. {
  296. unsigned short attr_name;
  297. unsigned short n;
  298. char *p;
  299. unsigned char prio;
  300. unsigned char clear;
  301. str url;
  302. int i;
  303. clear = NO_VAL;
  304. prio = 10;
  305. url.s = (char*)UNDEF_CHAR;
  306. url.len = 0;
  307. /* sanity check */
  308. if (NR_OF_KIDS(intr->ip)>1) {
  309. LM_ERR("LOCATION node suppose to have max "
  310. "one child, not %d!\n",NR_OF_KIDS(intr->ip));
  311. goto script_error;
  312. }
  313. for( i=NR_OF_ATTR(intr->ip),p=ATTR_PTR(intr->ip) ; i>0 ; i-- ) {
  314. get_basic_attr(p,attr_name,n,intr,script_error);
  315. switch (attr_name) {
  316. case URL_ATTR:
  317. url.len = n;
  318. get_str_attr( p, url.s, url.len, intr, script_error,1);
  319. break;
  320. case PRIORITY_ATTR:
  321. if ( n>10)
  322. LM_WARN("invalid value (%u) found"
  323. " for param. PRIORITY in LOCATION node -> using "
  324. "default (%u)!\n",n,prio);
  325. else
  326. prio = n;
  327. break;
  328. case CLEAR_ATTR:
  329. if (n!=YES_VAL && n!=NO_VAL)
  330. LM_WARN("invalid value (%u) found"
  331. " for param. CLEAR in LOCATION node -> using "
  332. "default (%u)!\n",n,clear);
  333. else
  334. clear = n;
  335. break;
  336. default:
  337. LM_ERR("unknown attribute (%d) in "
  338. "LOCATION node\n",attr_name);
  339. goto script_error;
  340. }
  341. }
  342. if (url.s==(char*)UNDEF_CHAR) {
  343. LM_ERR("param. URL missing in LOCATION node\n");
  344. goto script_error;
  345. }
  346. if (clear)
  347. empty_location_set( &(intr->loc_set) );
  348. if (add_location( &(intr->loc_set), &url, 0, prio, 0/*no dup*/ )==-1) {
  349. LM_ERR("unable to add location to set :-(\n");
  350. goto runtime_error;
  351. }
  352. /* set the flag for modifying the location set */
  353. intr->flags |= CPL_LOC_SET_MODIFIED;
  354. return get_first_child(intr->ip);
  355. runtime_error:
  356. return CPL_RUNTIME_ERROR;
  357. script_error:
  358. return CPL_SCRIPT_ERROR;
  359. }
  360. /* UPDATED + CHECKED
  361. */
  362. static inline char *run_remove_location( struct cpl_interpreter *intr )
  363. {
  364. unsigned short attr_name;
  365. unsigned short n;
  366. char *p;
  367. str url;
  368. int i;
  369. url.s = (char*)UNDEF_CHAR;
  370. /* sanity check */
  371. if (NR_OF_KIDS(intr->ip)>1) {
  372. LM_ERR("REMOVE_LOCATION node suppose to have max one child, not %d!\n",
  373. NR_OF_KIDS(intr->ip));
  374. goto script_error;
  375. }
  376. /* dirty hack to speed things up in when loc set is already empty */
  377. if (intr->loc_set==0)
  378. goto done;
  379. for( i=NR_OF_ATTR(intr->ip),p=ATTR_PTR(intr->ip) ; i>0 ; i-- ) {
  380. get_basic_attr(p,attr_name,n,intr,script_error);
  381. switch (attr_name) {
  382. case LOCATION_ATTR:
  383. url.len = n;
  384. get_str_attr( p, url.s, url.len, intr, script_error,1);
  385. break;
  386. default:
  387. LM_ERR("unknown attribute "
  388. "(%d) in REMOVE_LOCATION node\n",attr_name);
  389. goto script_error;
  390. }
  391. }
  392. if (url.s==(char*)UNDEF_CHAR) {
  393. LM_DBG("remove all locs from loc_set\n");
  394. empty_location_set( &(intr->loc_set) );
  395. } else {
  396. remove_location( &(intr->loc_set), url.s, url.len );
  397. }
  398. /* set the flag for modifying the location set */
  399. intr->flags |= CPL_LOC_SET_MODIFIED;
  400. done:
  401. return get_first_child(intr->ip);
  402. script_error:
  403. return CPL_SCRIPT_ERROR;
  404. }
  405. /* UPDATED + CHECKED
  406. */
  407. static inline char *run_sub( struct cpl_interpreter *intr )
  408. {
  409. char *p;
  410. unsigned short offset;
  411. unsigned short attr_name;
  412. int i;
  413. /* sanity check */
  414. if (NR_OF_KIDS(intr->ip)!=0) {
  415. LM_ERR("SUB node doesn't suppose to have any "
  416. "sub-nodes. Found %d!\n",NR_OF_KIDS(intr->ip));
  417. goto script_error;
  418. }
  419. /* check the number of attr */
  420. i = NR_OF_ATTR( intr->ip );
  421. if (i!=1) {
  422. LM_ERR("incorrect nr. of attr. %d (<>1) in SUB node\n",i);
  423. goto script_error;
  424. }
  425. /* get attr's name */
  426. p = ATTR_PTR(intr->ip);
  427. get_basic_attr( p, attr_name, offset, intr, script_error);
  428. if (attr_name!=REF_ATTR) {
  429. LM_ERR("invalid attr. %d (expected %d)in "
  430. "SUB node\n", attr_name, REF_ATTR);
  431. goto script_error;
  432. }
  433. /* make the jump */
  434. p = intr->ip - offset;
  435. /* check the destination pointer -> are we still inside the buffer ;-) */
  436. if (((char*)p)<intr->script.s) {
  437. LM_ERR("jump offset lower than the script "
  438. "beginning -> underflow!\n");
  439. goto script_error;
  440. }
  441. check_overflow_by_ptr( p+SIMPLE_NODE_SIZE(intr->ip), intr, script_error);
  442. /* check to see if we hit a subaction node */
  443. if ( NODE_TYPE(p)!=SUBACTION_NODE ) {
  444. LM_ERR("sub. jump hit a nonsubaction node!\n");
  445. goto script_error;
  446. }
  447. if ( NR_OF_ATTR(p)!=0 ) {
  448. LM_ERR("invalid subaction node reached "
  449. "(attrs=%d); expected (0)!\n",NR_OF_ATTR(p));
  450. goto script_error;
  451. }
  452. return get_first_child(p);
  453. script_error:
  454. return CPL_SCRIPT_ERROR;
  455. }
  456. /* UPDATED + CHECKED
  457. */
  458. static inline char *run_reject( struct cpl_interpreter *intr )
  459. {
  460. unsigned short attr_name;
  461. unsigned short status;
  462. unsigned short n;
  463. str reason;
  464. char *p;
  465. int i;
  466. reason.s = (char*)UNDEF_CHAR;
  467. status = UNDEF_CHAR;
  468. /* sanity check */
  469. if (NR_OF_KIDS(intr->ip)!=0) {
  470. LM_ERR("REJECT node doesn't suppose to have "
  471. "any sub-nodes. Found %d!\n",NR_OF_KIDS(intr->ip));
  472. goto script_error;
  473. }
  474. for( i=NR_OF_ATTR(intr->ip),p=ATTR_PTR(intr->ip) ; i>0 ; i-- ) {
  475. get_basic_attr( p, attr_name, n, intr, script_error);
  476. switch (attr_name) {
  477. case STATUS_ATTR:
  478. status = n;
  479. break;
  480. case REASON_ATTR:
  481. reason.len = n;
  482. get_str_attr( p, reason.s, reason.len, intr, script_error,1);
  483. break;
  484. default:
  485. LM_ERR("unknown attribute "
  486. "(%d) in REJECT node\n",attr_name);
  487. goto script_error;
  488. }
  489. }
  490. if (status==UNDEF_CHAR) {
  491. LM_ERR("mandatory attribute STATUS not found\n");
  492. goto script_error;
  493. }
  494. if (status<400 || status>=700) {
  495. LM_ERR("bad attribute STATUS (%d)\n",status);
  496. goto script_error;
  497. }
  498. if (reason.s==(char*)UNDEF_CHAR ) {
  499. switch (status) {
  500. case 486:
  501. reason.s = "Busy Here";
  502. reason.len = 9;
  503. break;
  504. case 404:
  505. reason.s = "Not Found";
  506. reason.len = 9;
  507. break;
  508. case 603:
  509. reason.s = "Decline";
  510. reason.len = 7;
  511. break;
  512. case 500:
  513. reason.s = "Internal Server Error";
  514. reason.len = 21;
  515. break;
  516. default:
  517. reason.s = "Generic Error";
  518. reason.len = 13;
  519. }
  520. }
  521. /* if still stateless and FORCE_STATEFUL set -> build the transaction */
  522. if ( !(intr->flags&CPL_IS_STATEFUL) && intr->flags&CPL_FORCE_STATEFUL) {
  523. i = cpl_fct.tmb.t_newtran( intr->msg );
  524. if (i<0) {
  525. LM_ERR("failed to build new transaction!\n");
  526. goto runtime_error;
  527. } else if (i==0) {
  528. LM_ERR(" processed INVITE is a retransmission!\n");
  529. /* instead of generating an error is better just to break the
  530. * script by returning EO_SCRIPT */
  531. return EO_SCRIPT;
  532. }
  533. intr->flags |= CPL_IS_STATEFUL;
  534. }
  535. /* send the reply */
  536. i = cpl_fct.slb.freply(intr->msg, (int)status, &reason );
  537. if ( i!=1 ) {
  538. LM_ERR("unable to send reject reply!\n");
  539. goto runtime_error;
  540. }
  541. return EO_SCRIPT;
  542. runtime_error:
  543. return CPL_RUNTIME_ERROR;
  544. script_error:
  545. return CPL_SCRIPT_ERROR;
  546. }
  547. /* UPDATED + CHECKED
  548. */
  549. static inline char *run_redirect( struct cpl_interpreter *intr )
  550. {
  551. struct location *loc;
  552. struct lump_rpl *lump;
  553. unsigned short attr_name;
  554. unsigned short permanent;
  555. unsigned short n;
  556. char *p;
  557. str lump_str;
  558. char *cp;
  559. int i;
  560. permanent = NO_VAL;
  561. /* sanity check */
  562. if (NR_OF_KIDS(intr->ip)!=0) {
  563. LM_ERR("REDIRECT node doesn't suppose "
  564. "to have any sub-nodes. Found %d!\n",NR_OF_KIDS(intr->ip));
  565. goto script_error;
  566. }
  567. /* read the attributes of the REDIRECT node*/
  568. for( i=NR_OF_ATTR(intr->ip),p=ATTR_PTR(intr->ip) ; i>0 ; i-- ) {
  569. get_basic_attr( p, attr_name, n, intr, script_error);
  570. switch (attr_name) {
  571. case PERMANENT_ATTR:
  572. if (n!=YES_VAL && n!=NO_VAL) {
  573. LM_ERR("unsupported value (%d)"
  574. " in attribute PERMANENT for REDIRECT node",n);
  575. goto script_error;
  576. }
  577. permanent = n;
  578. break;
  579. default:
  580. LM_ERR("unknown attribute "
  581. "(%d) in REDIRECT node\n",attr_name);
  582. goto script_error;
  583. }
  584. }
  585. /* build the lump for Contact header */
  586. lump_str.len = 9 /*"Contact: "*/;
  587. for(loc=intr->loc_set;loc;loc=loc->next)
  588. lump_str.len += 1/*"<"*/ + loc->addr.uri.len + 7/*">;q=x.x"*/ +
  589. 2*(loc->next!=0)/*" ,"*/;
  590. lump_str.len += CRLF_LEN;
  591. lump_str.s = pkg_malloc( lump_str.len );
  592. if(!lump_str.s) {
  593. LM_ERR("out of pkg memory!\n");
  594. goto runtime_error;
  595. }
  596. cp = lump_str.s;
  597. memcpy( cp , "Contact: " , 9);
  598. cp += 9;
  599. for(loc=intr->loc_set;loc;loc=loc->next) {
  600. *(cp++) = '<';
  601. memcpy(cp,loc->addr.uri.s,loc->addr.uri.len);
  602. cp += loc->addr.uri.len;
  603. memcpy(cp,">;q=",4);
  604. cp += 4;
  605. *(cp++) = (loc->addr.priority!=10)?'0':'1';
  606. *(cp++) = '.';
  607. *(cp++) = '0'+(loc->addr.priority%10);
  608. if (loc->next) {
  609. *(cp++) = ' ';
  610. *(cp++) = ',';
  611. }
  612. }
  613. memcpy(cp,CRLF,CRLF_LEN);
  614. /* if still stateless and FORCE_STATEFUL set -> build the transaction */
  615. if ( !(intr->flags&CPL_IS_STATEFUL) && intr->flags&CPL_FORCE_STATEFUL) {
  616. i = cpl_fct.tmb.t_newtran( intr->msg );
  617. if (i<0) {
  618. LM_ERR("failed to build new transaction!\n");
  619. pkg_free( lump_str.s );
  620. goto runtime_error;
  621. } else if (i==0) {
  622. LM_ERR("processed INVITE is a retransmission!\n");
  623. /* instead of generating an error is better just to break the
  624. * script by returning EO_SCRIPT */
  625. pkg_free( lump_str.s );
  626. return EO_SCRIPT;
  627. }
  628. intr->flags |= CPL_IS_STATEFUL;
  629. }
  630. /* add the lump to the reply */
  631. lump = add_lump_rpl( intr->msg, lump_str.s , lump_str.len , LUMP_RPL_HDR);
  632. if(!lump) {
  633. LM_ERR("unable to add lump_rpl! \n");
  634. pkg_free( lump_str.s );
  635. goto runtime_error;
  636. }
  637. /* send the reply */
  638. if (permanent)
  639. i = cpl_fct.slb.freply( intr->msg,301,&cpl_301_reason);
  640. else
  641. i = cpl_fct.slb.freply( intr->msg,302,&cpl_302_reason);
  642. /* msg which I'm working on can be in private memory or is a clone into
  643. * shared memory (if I'm after a failed proxy); So, it's better to removed
  644. * by myself the lump that I added previously */
  645. unlink_lump_rpl( intr->msg, lump);
  646. free_lump_rpl( lump );
  647. if (i!=1) {
  648. LM_ERR("unable to send redirect reply!\n");
  649. goto runtime_error;
  650. }
  651. return EO_SCRIPT;
  652. runtime_error:
  653. return CPL_RUNTIME_ERROR;
  654. script_error:
  655. return CPL_SCRIPT_ERROR;
  656. }
  657. /* UPDATED + CHECKED
  658. */
  659. static inline char *run_log( struct cpl_interpreter *intr )
  660. {
  661. char *p;
  662. unsigned short attr_name;
  663. unsigned short n;
  664. str name = {0,0};
  665. str comment = {0,0};
  666. str user;
  667. int i;
  668. /* sanity check */
  669. if (NR_OF_KIDS(intr->ip)>1) {
  670. LM_ERR("LOG node suppose to have max one child"
  671. ", not %d!\n",NR_OF_KIDS(intr->ip));
  672. goto script_error;
  673. }
  674. /* is logging enabled? */
  675. if ( cpl_env.log_dir==0 )
  676. goto done;
  677. /* read the attributes of the LOG node*/
  678. p = ATTR_PTR(intr->ip);
  679. for( i=NR_OF_ATTR(intr->ip); i>0 ; i-- ) {
  680. get_basic_attr( p, attr_name, n, intr, script_error);
  681. switch (attr_name) {
  682. case NAME_ATTR:
  683. get_str_attr( p, name.s, n, intr, script_error,1);
  684. name.len = n;
  685. break;
  686. case COMMENT_ATTR:
  687. get_str_attr( p, comment.s, n, intr, script_error,1);
  688. comment.len = n;
  689. break;
  690. default:
  691. LM_ERR("unknown attribute "
  692. "(%d) in LOG node\n",attr_name);
  693. goto script_error;
  694. }
  695. }
  696. if (comment.len==0) {
  697. LM_NOTICE("LOG node has no comment attr -> skipping\n");
  698. goto done;
  699. }
  700. user.len = intr->user.len + name.len + comment.len;
  701. /* duplicate the attrs in shm memory */
  702. user.s = p = (char*)shm_malloc( user.len );
  703. if (!user.s) {
  704. LM_ERR("no more shm memory!\n");
  705. goto runtime_error;
  706. }
  707. /* copy the user name */
  708. memcpy( p, intr->user.s, intr->user.len);
  709. user.len = intr->user.len;
  710. p += intr->user.len;
  711. /* copy the log name */
  712. if (name.len) {
  713. memcpy( p, name.s, name.len );
  714. name.s = p;
  715. p += name.len;
  716. }
  717. /* copy the comment */
  718. memcpy( p, comment.s, comment.len);
  719. comment.s = p;
  720. /* send the command */
  721. write_cpl_cmd( CPL_LOG_CMD, &user, &name, &comment );
  722. done:
  723. return get_first_child(intr->ip);
  724. runtime_error:
  725. return CPL_RUNTIME_ERROR;
  726. script_error:
  727. return CPL_SCRIPT_ERROR;
  728. }
  729. /* UPDATED + CHECKED
  730. */
  731. static inline char *run_mail( struct cpl_interpreter *intr )
  732. {
  733. unsigned short attr_name;
  734. unsigned short n;
  735. char *p;
  736. str subject = {0,0};
  737. str body = {0,0};
  738. str to = {0,0};
  739. int i;
  740. /* sanity check */
  741. if (NR_OF_KIDS(intr->ip)>1) {
  742. LM_ERR("MAIL node suppose to have max one"
  743. " child, not %d!\n",NR_OF_KIDS(intr->ip));
  744. goto script_error;
  745. }
  746. /* read the attributes of the MAIL node*/
  747. for( i=NR_OF_ATTR(intr->ip),p=ATTR_PTR(intr->ip) ; i>0 ; i-- ) {
  748. get_basic_attr(p, attr_name, n, intr, script_error);
  749. switch (attr_name) {
  750. case TO_ATTR:
  751. get_str_attr(p, to.s, n, intr, script_error,0);
  752. to.len = n;
  753. break;
  754. case SUBJECT_ATTR:
  755. get_str_attr(p, subject.s, n, intr, script_error,0);
  756. subject.len = n;
  757. break;
  758. case BODY_ATTR:
  759. get_str_attr(p, body.s, n, intr, script_error,0);
  760. body.len = n;
  761. break;
  762. default:
  763. LM_ERR("unknown attribute (%d) in MAIL node\n",attr_name);
  764. goto script_error;
  765. }
  766. }
  767. if (to.len==0) {
  768. LM_ERR("email has an empty TO hdr!\n");
  769. goto script_error;
  770. }
  771. if (body.len==0 && subject.len==0) {
  772. LM_WARN("I refuse to send email with no "
  773. "body and no subject -> skipping...\n");
  774. goto done;
  775. }
  776. /* duplicate the attrs in shm memory */
  777. p = (char*)shm_malloc( to.len + subject.len + body.len );
  778. if (!p) {
  779. LM_ERR("no more shm memory!\n");
  780. goto runtime_error;
  781. }
  782. /* copy the TO */
  783. memcpy( p, to.s, to.len );
  784. to.s = p;
  785. p += to.len;
  786. /* copy the subject */
  787. if (subject.len) {
  788. memcpy( p, subject.s, subject.len );
  789. subject.s = p;
  790. p += subject.len;
  791. }
  792. /* copy the body */
  793. if (body.len) {
  794. memcpy( p, body.s, body.len );
  795. body.s = p;
  796. p += body.len;
  797. }
  798. /* send the command */
  799. write_cpl_cmd( CPL_MAIL_CMD, &to, &subject, &body);
  800. done:
  801. return get_first_child(intr->ip);
  802. runtime_error:
  803. return CPL_RUNTIME_ERROR;
  804. script_error:
  805. return CPL_SCRIPT_ERROR;
  806. }
  807. static inline int run_default( struct cpl_interpreter *intr )
  808. {
  809. if (!(intr->flags&CPL_PROXY_DONE)) {
  810. /* no signaling operations */
  811. if ( !(intr->flags&CPL_LOC_SET_MODIFIED) ) {
  812. /* no location modifications */
  813. if (intr->loc_set==0 ) {
  814. /* case 1 : no location modifications or signaling operations
  815. * performed, location set empty ->
  816. * Look up the user's location through whatever mechanism the
  817. * server would use if no CPL script were in effect */
  818. return SCRIPT_DEFAULT;
  819. } else {
  820. /* case 2 : no location modifications or signaling operations
  821. * performed, location set non-empty: (This can only happen
  822. * for outgoing calls.) ->
  823. * Proxy the call to the address in the location set.
  824. * With other words, let ser to continue processing the
  825. * request as nothing happened */
  826. return SCRIPT_DEFAULT;
  827. }
  828. } else {
  829. /* case 3 : location modifications performed, no signaling
  830. * operations ->
  831. * Proxy the call to the addresses in the location set */
  832. if (!cpl_proxy_to_loc_set(intr->msg,&(intr->loc_set),intr->flags))
  833. return SCRIPT_END;
  834. return SCRIPT_RUN_ERROR;
  835. }
  836. } else {
  837. /* case 4 : proxy operation previously taken -> return whatever the
  838. * "best" response is of all accumulated responses to the call to this
  839. * point, according to the rules of the underlying signaling
  840. * protocol. */
  841. /* we will let ser to choose and forward one of the replies -> for this
  842. * nothing must be done */
  843. return SCRIPT_END;
  844. }
  845. /*return SCRIPT_RUN_ERROR;*/
  846. }
  847. /* include all inline functions for processing the switches */
  848. #include "cpl_switches.h"
  849. /* include inline function for running proxy node */
  850. #include "cpl_proxy.h"
  851. int cpl_run_script( struct cpl_interpreter *intr )
  852. {
  853. char *new_ip;
  854. do {
  855. check_overflow_by_offset( SIMPLE_NODE_SIZE(intr->ip), intr, error);
  856. switch ( NODE_TYPE(intr->ip) ) {
  857. case CPL_NODE:
  858. LM_DBG("processing CPL node \n");
  859. new_ip = run_cpl_node( intr ); /*UPDATED&TESTED*/
  860. break;
  861. case ADDRESS_SWITCH_NODE:
  862. LM_DBG("processing address-switch node\n");
  863. new_ip = run_address_switch( intr ); /*UPDATED&TESTED*/
  864. break;
  865. case STRING_SWITCH_NODE:
  866. LM_DBG("processing string-switch node\n");
  867. new_ip = run_string_switch( intr ); /*UPDATED&TESTED*/
  868. break;
  869. case PRIORITY_SWITCH_NODE:
  870. LM_DBG("processing priority-switch node\n");
  871. new_ip = run_priority_switch( intr ); /*UPDATED&TESTED*/
  872. break;
  873. case TIME_SWITCH_NODE:
  874. LM_DBG("processing time-switch node\n");
  875. new_ip = run_time_switch( intr ); /*UPDATED&TESTED*/
  876. break;
  877. case LANGUAGE_SWITCH_NODE:
  878. LM_DBG("processing language-switch node\n");
  879. new_ip = run_language_switch( intr ); /*UPDATED&TESTED*/
  880. break;
  881. case LOOKUP_NODE:
  882. LM_DBG("processing lookup node\n");
  883. new_ip = run_lookup( intr ); /*UPDATED&TESTED*/
  884. break;
  885. case LOCATION_NODE:
  886. LM_DBG("processing location node\n");
  887. new_ip = run_location( intr ); /*UPDATED&TESTED*/
  888. break;
  889. case REMOVE_LOCATION_NODE:
  890. LM_DBG("processing remove_location node\n");
  891. new_ip = run_remove_location( intr ); /*UPDATED&TESTED*/
  892. break;
  893. case PROXY_NODE:
  894. LM_DBG("processing proxy node\n");
  895. new_ip = run_proxy( intr );/*UPDATED&TESTED*/
  896. break;
  897. case REJECT_NODE:
  898. LM_DBG("processing reject node\n");
  899. new_ip = run_reject( intr ); /*UPDATED&TESTED*/
  900. break;
  901. case REDIRECT_NODE:
  902. LM_DBG("processing redirect node\n");
  903. new_ip = run_redirect( intr ); /*UPDATED&TESTED*/
  904. break;
  905. case LOG_NODE:
  906. LM_DBG("processing log node\n");
  907. new_ip = run_log( intr ); /*UPDATED&TESTED*/
  908. break;
  909. case MAIL_NODE:
  910. LM_DBG("processing mail node\n");
  911. new_ip = run_mail( intr ); /*UPDATED&TESTED*/
  912. break;
  913. case SUB_NODE:
  914. LM_DBG("processing sub node\n");
  915. new_ip = run_sub( intr ); /*UPDATED&TESTED*/
  916. break;
  917. default:
  918. LM_ERR("unknown type node (%d)\n",
  919. NODE_TYPE(intr->ip));
  920. goto error;
  921. }
  922. if (new_ip==CPL_RUNTIME_ERROR) {
  923. LM_ERR("runtime error\n");
  924. return SCRIPT_RUN_ERROR;
  925. } else if (new_ip==CPL_SCRIPT_ERROR) {
  926. LM_ERR("script error\n");
  927. return SCRIPT_FORMAT_ERROR;
  928. } else if (new_ip==DEFAULT_ACTION) {
  929. LM_DBG("running default action\n");
  930. return run_default(intr);
  931. } else if (new_ip==EO_SCRIPT) {
  932. LM_DBG("script interpretation done!\n");
  933. return SCRIPT_END;
  934. } else if (new_ip==CPL_TO_CONTINUE) {
  935. LM_DBG("done for the moment; waiting after signaling!\n");
  936. return SCRIPT_TO_BE_CONTINUED;
  937. }
  938. /* move to the new instruction */
  939. intr->ip = new_ip;
  940. }while(1);
  941. error:
  942. return SCRIPT_FORMAT_ERROR;
  943. }