textops.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. *
  21. */
  22. /*!
  23. * \file
  24. * \brief Module interface
  25. * \ingroup textops
  26. * Module: \ref textops
  27. */
  28. /**
  29. * @defgroup Textops Various text operatoins on messages
  30. * @brief Kamailio textops module
  31. */
  32. #include "../../action.h"
  33. #include "../../sr_module.h"
  34. #include "../../dprint.h"
  35. #include "../../data_lump.h"
  36. #include "../../data_lump_rpl.h"
  37. #include "../../error.h"
  38. #include "../../mem/mem.h"
  39. #include "../../str.h"
  40. #include "../../re.h"
  41. #include "../../mod_fix.h"
  42. #include "../../parser/parse_uri.h"
  43. #include "../../parser/parse_hname2.h"
  44. #include "../../parser/parse_methods.h"
  45. #include "../../parser/parse_content.h"
  46. #include "../../parser/parse_param.h"
  47. #include "../../parser/sdp/sdp.h"
  48. #include "../../parser/sdp/sdp_helpr_funcs.h"
  49. #include "../../lib/kcore/parse_privacy.h"
  50. #include "../../msg_translator.h"
  51. #include "../../ut.h"
  52. #include "../../dset.h"
  53. #include "../../lib/kcore/cmpapi.h"
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <sys/types.h> /* for regex */
  58. #include <regex.h>
  59. #include <time.h>
  60. #include <sys/time.h>
  61. #include "textops.h"
  62. #include "txt_var.h"
  63. #include "api.h"
  64. MODULE_VERSION
  65. /* RFC822-conforming dates format:
  66. %a -- abbreviated week of day name (locale), %d day of month
  67. as decimal number, %b abbreviated month name (locale), %Y
  68. year with century, %T time in 24h notation
  69. */
  70. #define TIME_FORMAT "Date: %a, %d %b %Y %H:%M:%S GMT"
  71. #define MAX_TIME 64
  72. #define AUDIO_STR "audio"
  73. #define AUDIO_STR_LEN 5
  74. static int search_body_f(struct sip_msg*, char*, char*);
  75. static int search_hf_f(struct sip_msg*, char*, char*, char*);
  76. static int replace_f(struct sip_msg*, char*, char*);
  77. static int replace_body_f(struct sip_msg*, char*, char*);
  78. static int replace_all_f(struct sip_msg*, char*, char*);
  79. static int replace_body_all_f(struct sip_msg*, char*, char*);
  80. static int replace_body_atonce_f(struct sip_msg*, char*, char*);
  81. static int subst_f(struct sip_msg*, char*, char*);
  82. static int subst_uri_f(struct sip_msg*, char*, char*);
  83. static int subst_user_f(struct sip_msg*, char*, char*);
  84. static int subst_body_f(struct sip_msg*, char*, char*);
  85. static int subst_hf_f(struct sip_msg*, char*, char*, char*);
  86. static int filter_body_f(struct sip_msg*, char*, char*);
  87. static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
  88. static int search_append_body_f(struct sip_msg*, char*, char*);
  89. static int append_to_reply_f(struct sip_msg* msg, char* key, char* str);
  90. static int append_hf_1(struct sip_msg* msg, char* str1, char* str2);
  91. static int append_hf_2(struct sip_msg* msg, char* str1, char* str2);
  92. static int insert_hf_1(struct sip_msg* msg, char* str1, char* str2);
  93. static int insert_hf_2(struct sip_msg* msg, char* str1, char* str2);
  94. static int append_urihf(struct sip_msg* msg, char* str1, char* str2);
  95. static int append_time_f(struct sip_msg* msg, char* , char *);
  96. static int append_time_request_f(struct sip_msg* msg, char* , char *);
  97. static int set_body_f(struct sip_msg* msg, char*, char *);
  98. static int set_rpl_body_f(struct sip_msg* msg, char*, char *);
  99. static int set_multibody_0(struct sip_msg* msg, char*, char *, char *);
  100. static int set_multibody_1(struct sip_msg* msg, char*, char *, char *);
  101. static int set_multibody_2(struct sip_msg* msg, char*, char *, char *);
  102. static int set_multibody_3(struct sip_msg* msg, char*, char *, char *);
  103. static int append_multibody_2(struct sip_msg* msg, char*, char *);
  104. static int append_multibody_3(struct sip_msg* msg, char*, char *, char *);
  105. static int fixup_multibody_f(void** param, int param_no);
  106. static int remove_multibody_f(struct sip_msg *msg, char *);
  107. static int is_method_f(struct sip_msg* msg, char* , char *);
  108. static int has_body_f(struct sip_msg *msg, char *type, char *str2 );
  109. static int in_list_f(struct sip_msg* _msg, char* _subject, char* _list,
  110. char* _sep);
  111. static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 );
  112. static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2 );
  113. static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 );
  114. static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
  115. static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
  116. static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 );
  117. static int fixup_substre(void**, int);
  118. static int hname_fixup(void** param, int param_no);
  119. static int free_hname_fixup(void** param, int param_no);
  120. static int fixup_method(void** param, int param_no);
  121. static int add_header_fixup(void** param, int param_no);
  122. static int fixup_body_type(void** param, int param_no);
  123. static int fixup_in_list(void** param, int param_no);
  124. static int fixup_free_in_list(void** param, int param_no);
  125. int fixup_regexpNL_none(void** param, int param_no);
  126. static int fixup_search_hf(void** param, int param_no);
  127. static int fixup_subst_hf(void** param, int param_no);
  128. static int mod_init(void);
  129. static tr_export_t mod_trans[] = {
  130. { {"re", sizeof("re")-1}, /* regexp class */
  131. tr_txt_parse_re },
  132. { { 0, 0 }, 0 }
  133. };
  134. static cmd_export_t cmds[]={
  135. {"search", (cmd_function)search_f, 1,
  136. fixup_regexp_null, fixup_free_regexp_null,
  137. ANY_ROUTE},
  138. {"search_body", (cmd_function)search_body_f, 1,
  139. fixup_regexp_null, fixup_free_regexp_null,
  140. ANY_ROUTE},
  141. {"search_hf", (cmd_function)search_hf_f, 3,
  142. fixup_search_hf, 0,
  143. ANY_ROUTE},
  144. {"search_append", (cmd_function)search_append_f, 2,
  145. fixup_regexp_none,fixup_free_regexp_none,
  146. ANY_ROUTE},
  147. {"search_append_body", (cmd_function)search_append_body_f, 2,
  148. fixup_regexp_none, fixup_free_regexp_none,
  149. ANY_ROUTE},
  150. {"replace", (cmd_function)replace_f, 2,
  151. fixup_regexp_none, fixup_free_regexp_none,
  152. ANY_ROUTE},
  153. {"replace_body", (cmd_function)replace_body_f, 2,
  154. fixup_regexp_none, fixup_free_regexp_none,
  155. ANY_ROUTE},
  156. {"replace_all", (cmd_function)replace_all_f, 2,
  157. fixup_regexp_none, fixup_free_regexp_none,
  158. ANY_ROUTE},
  159. {"replace_body_all", (cmd_function)replace_body_all_f,2,
  160. fixup_regexp_none, fixup_free_regexp_none,
  161. ANY_ROUTE},
  162. {"replace_body_atonce", (cmd_function)replace_body_atonce_f,2,
  163. fixup_regexpNL_none, fixup_free_regexp_none,
  164. ANY_ROUTE},
  165. {"append_to_reply", (cmd_function)append_to_reply_f, 1,
  166. fixup_spve_null, 0,
  167. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  168. {"append_hf", (cmd_function)append_hf_1, 1,
  169. add_header_fixup, 0,
  170. ANY_ROUTE},
  171. {"append_hf", (cmd_function)append_hf_2, 2,
  172. add_header_fixup, 0,
  173. ANY_ROUTE},
  174. {"insert_hf", (cmd_function)insert_hf_1, 1,
  175. add_header_fixup, 0,
  176. ANY_ROUTE},
  177. {"insert_hf", (cmd_function)insert_hf_2, 2,
  178. add_header_fixup, 0,
  179. ANY_ROUTE},
  180. {"append_urihf", (cmd_function)append_urihf, 2,
  181. fixup_str_str, fixup_free_str_str,
  182. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  183. {"remove_hf", (cmd_function)remove_hf_f, 1,
  184. hname_fixup, free_hname_fixup,
  185. ANY_ROUTE},
  186. {"remove_hf_re", (cmd_function)remove_hf_re_f, 1,
  187. fixup_regexp_null, fixup_free_regexp_null,
  188. ANY_ROUTE},
  189. {"is_present_hf", (cmd_function)is_present_hf_f, 1,
  190. hname_fixup, free_hname_fixup,
  191. ANY_ROUTE},
  192. {"is_present_hf_re", (cmd_function)is_present_hf_re_f,1,
  193. fixup_regexp_null, fixup_free_regexp_null,
  194. ANY_ROUTE},
  195. {"subst", (cmd_function)subst_f, 1,
  196. fixup_substre, 0,
  197. ANY_ROUTE},
  198. {"subst_uri", (cmd_function)subst_uri_f, 1,
  199. fixup_substre, 0,
  200. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  201. {"subst_user", (cmd_function)subst_user_f, 1,
  202. fixup_substre, 0,
  203. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  204. {"subst_body", (cmd_function)subst_body_f, 1,
  205. fixup_substre, 0,
  206. ANY_ROUTE},
  207. {"subst_hf", (cmd_function)subst_hf_f, 3,
  208. fixup_subst_hf, 0,
  209. ANY_ROUTE},
  210. {"filter_body", (cmd_function)filter_body_f, 1,
  211. fixup_str_null, 0,
  212. ANY_ROUTE},
  213. {"append_time", (cmd_function)append_time_f, 0,
  214. 0, 0,
  215. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  216. {"set_body", (cmd_function)set_body_f, 2,
  217. fixup_spve_spve, 0,
  218. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE },
  219. {"set_reply_body", (cmd_function)set_rpl_body_f, 2,
  220. fixup_spve_spve, 0,
  221. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  222. {"is_method", (cmd_function)is_method_f, 1,
  223. fixup_method, 0,
  224. ANY_ROUTE},
  225. {"has_body", (cmd_function)has_body_f, 0,
  226. 0, 0,
  227. ANY_ROUTE},
  228. {"has_body", (cmd_function)has_body_f, 1,
  229. fixup_body_type, 0,
  230. ANY_ROUTE},
  231. {"is_privacy", (cmd_function)is_privacy_f, 1,
  232. fixup_privacy, 0,
  233. ANY_ROUTE},
  234. {"in_list", (cmd_function)in_list_f, 3, fixup_in_list,
  235. fixup_free_in_list,
  236. ANY_ROUTE},
  237. {"cmp_str", (cmd_function)cmp_str_f, 2,
  238. fixup_spve_spve, 0,
  239. ANY_ROUTE},
  240. {"cmp_istr", (cmd_function)cmp_istr_f, 2,
  241. fixup_spve_spve, 0,
  242. ANY_ROUTE},
  243. {"starts_with", (cmd_function)starts_with_f, 2,
  244. fixup_spve_spve, 0,
  245. ANY_ROUTE},
  246. {"is_audio_on_hold", (cmd_function)is_audio_on_hold_f, 0,
  247. 0, 0,
  248. ANY_ROUTE},
  249. {"append_time_to_request", (cmd_function)append_time_request_f, 0,
  250. 0, 0,
  251. ANY_ROUTE},
  252. {"bind_textops", (cmd_function)bind_textops, 0, 0, 0,
  253. 0},
  254. {"set_body_multipart", (cmd_function)set_multibody_0, 0,
  255. 0, 0,
  256. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  257. {"set_body_multipart", (cmd_function)set_multibody_1, 1,
  258. fixup_spve_null, 0,
  259. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  260. {"set_body_multipart", (cmd_function)set_multibody_2, 2,
  261. fixup_spve_spve, 0,
  262. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  263. {"set_body_multipart", (cmd_function)set_multibody_3, 3,
  264. fixup_multibody_f, 0,
  265. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  266. {"append_body_part", (cmd_function)append_multibody_2, 2,
  267. fixup_spve_spve, 0,
  268. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  269. {"append_body_part", (cmd_function)append_multibody_3, 3,
  270. fixup_multibody_f, 0,
  271. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  272. {"remove_body_part", (cmd_function)remove_multibody_f, 1,
  273. fixup_spve_null, 0,
  274. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  275. {0,0,0,0,0,0}
  276. };
  277. struct module_exports exports= {
  278. "textops", /* module name*/
  279. DEFAULT_DLFLAGS, /* dlopen flags */
  280. cmds, /* exported functions */
  281. 0, /* module parameters */
  282. 0, /* exported statistics */
  283. 0, /* exported MI functions */
  284. 0, /* exported pseudo-variables */
  285. 0, /* extra processes */
  286. mod_init, /* module initialization function */
  287. 0, /* response function */
  288. 0, /* destroy function */
  289. 0, /* per-child init function */
  290. };
  291. static int mod_init(void)
  292. {
  293. return 0;
  294. }
  295. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  296. {
  297. return register_trans_mod(path, mod_trans);
  298. }
  299. static char *get_header(struct sip_msg *msg)
  300. {
  301. return msg->buf+msg->first_line.len;
  302. }
  303. int search_f(struct sip_msg* msg, char* key, char* str2)
  304. {
  305. /*we registered only 1 param, so we ignore str2*/
  306. regmatch_t pmatch;
  307. if (regexec((regex_t*) key, msg->buf, 1, &pmatch, 0)!=0) return -1;
  308. return 1;
  309. }
  310. static int search_body_f(struct sip_msg* msg, char* key, char* str2)
  311. {
  312. str body;
  313. /*we registered only 1 param, so we ignore str2*/
  314. regmatch_t pmatch;
  315. body.s = get_body(msg);
  316. if (body.s==0) {
  317. LM_ERR("failed to get the message body\n");
  318. return -1;
  319. }
  320. body.len = msg->len -(int)(body.s-msg->buf);
  321. if (body.len==0) {
  322. LM_DBG("message body has zero length\n");
  323. return -1;
  324. }
  325. if (regexec((regex_t*) key, body.s, 1, &pmatch, 0)!=0) return -1;
  326. return 1;
  327. }
  328. int search_append_f(struct sip_msg* msg, char* key, char* str2)
  329. {
  330. struct lump* l;
  331. regmatch_t pmatch;
  332. char* s;
  333. int len;
  334. char *begin;
  335. int off;
  336. begin=get_header(msg); /* msg->orig/buf previously .. uri problems */
  337. off=begin-msg->buf;
  338. if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
  339. if (pmatch.rm_so!=-1){
  340. if ((l=anchor_lump(msg, off+pmatch.rm_eo, 0, 0))==0)
  341. return -1;
  342. len=strlen(str2);
  343. s=pkg_malloc(len);
  344. if (s==0){
  345. LM_ERR("memory allocation failure\n");
  346. return -1;
  347. }
  348. memcpy(s, str2, len);
  349. if (insert_new_lump_after(l, s, len, 0)==0){
  350. LM_ERR("could not insert new lump\n");
  351. pkg_free(s);
  352. return -1;
  353. }
  354. return 1;
  355. }
  356. return -1;
  357. }
  358. static int search_append_body_f(struct sip_msg* msg, char* key, char* str2)
  359. {
  360. struct lump* l;
  361. regmatch_t pmatch;
  362. char* s;
  363. int len;
  364. int off;
  365. str body;
  366. body.s = get_body(msg);
  367. if (body.s==0) {
  368. LM_ERR("failed to get the message body\n");
  369. return -1;
  370. }
  371. body.len = msg->len -(int)(body.s-msg->buf);
  372. if (body.len==0) {
  373. LM_DBG("message body has zero length\n");
  374. return -1;
  375. }
  376. off=body.s-msg->buf;
  377. if (regexec((regex_t*) key, body.s, 1, &pmatch, 0)!=0) return -1;
  378. if (pmatch.rm_so!=-1){
  379. if ((l=anchor_lump(msg, off+pmatch.rm_eo, 0, 0))==0)
  380. return -1;
  381. len=strlen(str2);
  382. s=pkg_malloc(len);
  383. if (s==0){
  384. LM_ERR("memory allocation failure\n");
  385. return -1;
  386. }
  387. memcpy(s, str2, len);
  388. if (insert_new_lump_after(l, s, len, 0)==0){
  389. LM_ERR("could not insert new lump\n");
  390. pkg_free(s);
  391. return -1;
  392. }
  393. return 1;
  394. }
  395. return -1;
  396. }
  397. static int replace_all_f(struct sip_msg* msg, char* key, char* str2)
  398. {
  399. struct lump* l;
  400. regmatch_t pmatch;
  401. char* s;
  402. int len;
  403. char* begin;
  404. int off;
  405. int ret;
  406. int eflags;
  407. begin = get_header(msg);
  408. ret=-1; /* pessimist: we will not find any */
  409. len=strlen(str2);
  410. eflags=0; /* match ^ at the beginning of the string*/
  411. while (begin<msg->buf+msg->len
  412. && regexec((regex_t*) key, begin, 1, &pmatch, eflags)==0) {
  413. off=begin-msg->buf;
  414. if (pmatch.rm_so==-1){
  415. LM_ERR("offset unknown\n");
  416. return -1;
  417. }
  418. if (pmatch.rm_so==pmatch.rm_eo){
  419. LM_ERR("matched string is empty... invalid regexp?\n");
  420. return -1;
  421. }
  422. if ((l=del_lump(msg, pmatch.rm_so+off,
  423. pmatch.rm_eo-pmatch.rm_so, 0))==0) {
  424. LM_ERR("del_lump failed\n");
  425. return -1;
  426. }
  427. s=pkg_malloc(len);
  428. if (s==0){
  429. LM_ERR("memory allocation failure\n");
  430. return -1;
  431. }
  432. memcpy(s, str2, len);
  433. if (insert_new_lump_after(l, s, len, 0)==0){
  434. LM_ERR("could not insert new lump\n");
  435. pkg_free(s);
  436. return -1;
  437. }
  438. /* new cycle */
  439. begin=begin+pmatch.rm_eo;
  440. /* is it still a string start */
  441. if (*(begin-1)=='\n' || *(begin-1)=='\r')
  442. eflags&=~REG_NOTBOL;
  443. else
  444. eflags|=REG_NOTBOL;
  445. ret=1;
  446. } /* while found ... */
  447. return ret;
  448. }
  449. static int do_replace_body_f(struct sip_msg* msg, char* key, char* str2, int nobol)
  450. {
  451. struct lump* l;
  452. regmatch_t pmatch;
  453. char* s;
  454. int len;
  455. char* begin;
  456. int off;
  457. int ret;
  458. int eflags;
  459. str body;
  460. body.s = get_body(msg);
  461. if (body.s==0) {
  462. LM_ERR("failed to get the message body\n");
  463. return -1;
  464. }
  465. body.len = msg->len -(int)(body.s-msg->buf);
  466. if (body.len==0) {
  467. LM_DBG("message body has zero length\n");
  468. return -1;
  469. }
  470. begin=body.s;
  471. ret=-1; /* pessimist: we will not find any */
  472. len=strlen(str2);
  473. eflags=0; /* match ^ at the beginning of the string*/
  474. while (begin<msg->buf+msg->len
  475. && regexec((regex_t*) key, begin, 1, &pmatch, eflags)==0) {
  476. off=begin-msg->buf;
  477. if (pmatch.rm_so==-1){
  478. LM_ERR("offset unknown\n");
  479. return -1;
  480. }
  481. if (pmatch.rm_so==pmatch.rm_eo){
  482. LM_ERR("matched string is empty... invalid regexp?\n");
  483. return -1;
  484. }
  485. if ((l=del_lump(msg, pmatch.rm_so+off,
  486. pmatch.rm_eo-pmatch.rm_so, 0))==0) {
  487. LM_ERR("del_lump failed\n");
  488. return -1;
  489. }
  490. s=pkg_malloc(len);
  491. if (s==0){
  492. LM_ERR("memory allocation failure\n");
  493. return -1;
  494. }
  495. memcpy(s, str2, len);
  496. if (insert_new_lump_after(l, s, len, 0)==0){
  497. LM_ERR("could not insert new lump\n");
  498. pkg_free(s);
  499. return -1;
  500. }
  501. /* new cycle */
  502. begin=begin+pmatch.rm_eo;
  503. /* is it still a string start */
  504. if (nobol && (*(begin-1)=='\n' || *(begin-1)=='\r'))
  505. eflags&=~REG_NOTBOL;
  506. else
  507. eflags|=REG_NOTBOL;
  508. ret=1;
  509. } /* while found ... */
  510. return ret;
  511. }
  512. static int replace_body_all_f(struct sip_msg* msg, char* key, char* str2)
  513. {
  514. return do_replace_body_f(msg, key, str2, 1);
  515. }
  516. static int replace_body_atonce_f(struct sip_msg* msg, char* key, char* str2)
  517. {
  518. return do_replace_body_f(msg, key, str2, 0);
  519. }
  520. static int replace_f(struct sip_msg* msg, char* key, char* str2)
  521. {
  522. struct lump* l;
  523. regmatch_t pmatch;
  524. char* s;
  525. int len;
  526. char* begin;
  527. int off;
  528. begin=get_header(msg); /* msg->orig previously .. uri problems */
  529. if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
  530. off=begin-msg->buf;
  531. if (pmatch.rm_so!=-1){
  532. if ((l=del_lump(msg, pmatch.rm_so+off,
  533. pmatch.rm_eo-pmatch.rm_so, 0))==0)
  534. return -1;
  535. len=strlen(str2);
  536. s=pkg_malloc(len);
  537. if (s==0){
  538. LM_ERR("memory allocation failure\n");
  539. return -1;
  540. }
  541. memcpy(s, str2, len);
  542. if (insert_new_lump_after(l, s, len, 0)==0){
  543. LM_ERR("could not insert new lump\n");
  544. pkg_free(s);
  545. return -1;
  546. }
  547. return 1;
  548. }
  549. return -1;
  550. }
  551. static int replace_body_f(struct sip_msg* msg, char* key, char* str2)
  552. {
  553. struct lump* l;
  554. regmatch_t pmatch;
  555. char* s;
  556. int len;
  557. char* begin;
  558. int off;
  559. str body;
  560. body.s = get_body(msg);
  561. if (body.s==0) {
  562. LM_ERR("failed to get the message body\n");
  563. return -1;
  564. }
  565. body.len = msg->len -(int)(body.s-msg->buf);
  566. if (body.len==0) {
  567. LM_DBG("message body has zero length\n");
  568. return -1;
  569. }
  570. begin=body.s; /* msg->orig previously .. uri problems */
  571. if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
  572. off=begin-msg->buf;
  573. if (pmatch.rm_so!=-1){
  574. if ((l=del_lump(msg, pmatch.rm_so+off,
  575. pmatch.rm_eo-pmatch.rm_so, 0))==0)
  576. return -1;
  577. len=strlen(str2);
  578. s=pkg_malloc(len);
  579. if (s==0){
  580. LM_ERR("memory allocation failure\n");
  581. return -1;
  582. }
  583. memcpy(s, str2, len);
  584. if (insert_new_lump_after(l, s, len, 0)==0){
  585. LM_ERR("could not insert new lump\n");
  586. pkg_free(s);
  587. return -1;
  588. }
  589. return 1;
  590. }
  591. return -1;
  592. }
  593. /* sed-perl style re: s/regular expression/replacement/flags */
  594. static int subst_f(struct sip_msg* msg, char* subst, char* ignored)
  595. {
  596. struct lump* l;
  597. struct replace_lst* lst;
  598. struct replace_lst* rpl;
  599. char* begin;
  600. struct subst_expr* se;
  601. int off;
  602. int ret;
  603. int nmatches;
  604. se=(struct subst_expr*)subst;
  605. begin=get_header(msg); /* start after first line to avoid replacing
  606. the uri */
  607. off=begin-msg->buf;
  608. ret=-1;
  609. if ((lst=subst_run(se, begin, msg, &nmatches))==0)
  610. goto error; /* not found */
  611. for (rpl=lst; rpl; rpl=rpl->next){
  612. LM_DBG("%s: replacing at offset %d [%.*s] with [%.*s]\n",
  613. exports.name, rpl->offset+off,
  614. rpl->size, rpl->offset+off+msg->buf,
  615. rpl->rpl.len, rpl->rpl.s);
  616. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  617. goto error;
  618. /* hack to avoid re-copying rpl, possible because both
  619. * replace_lst & lumps use pkg_malloc */
  620. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0){
  621. LM_ERR("%s: could not insert new lump\n", exports.name);
  622. goto error;
  623. }
  624. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  625. * not free it */
  626. rpl->rpl.s=0;
  627. rpl->rpl.len=0;
  628. }
  629. ret=1;
  630. error:
  631. LM_DBG("lst was %p\n", lst);
  632. if (lst) replace_lst_free(lst);
  633. if (nmatches<0)
  634. LM_ERR("%s: subst_run failed\n", exports.name);
  635. return ret;
  636. }
  637. /* sed-perl style re: s/regular expression/replacement/flags, like
  638. * subst but works on the message uri */
  639. static int subst_uri_f(struct sip_msg* msg, char* subst, char* ignored)
  640. {
  641. char* tmp;
  642. int len;
  643. char c;
  644. struct subst_expr* se;
  645. str* result;
  646. se=(struct subst_expr*)subst;
  647. if (msg->new_uri.s){
  648. len=msg->new_uri.len;
  649. tmp=msg->new_uri.s;
  650. }else{
  651. tmp=msg->first_line.u.request.uri.s;
  652. len =msg->first_line.u.request.uri.len;
  653. };
  654. /* ugly hack: 0 s[len], and restore it afterward
  655. * (our re functions require 0 term strings), we can do this
  656. * because we always alloc len+1 (new_uri) and for first_line, the
  657. * message will always be > uri.len */
  658. c=tmp[len];
  659. tmp[len]=0;
  660. result=subst_str(tmp, msg, se, 0); /* pkg malloc'ed result */
  661. tmp[len]=c;
  662. if (result){
  663. LM_DBG("%s match - old uri= [%.*s], new uri= [%.*s]\n",
  664. exports.name, len, tmp,
  665. (result->len)?result->len:0,(result->s)?result->s:"");
  666. if (msg->new_uri.s) pkg_free(msg->new_uri.s);
  667. msg->new_uri=*result;
  668. msg->parsed_uri_ok=0; /* reset "use cached parsed uri" flag */
  669. ruri_mark_new();
  670. pkg_free(result); /* free str* pointer */
  671. return 1; /* success */
  672. }
  673. return -1; /* false, no subst. made */
  674. }
  675. /* sed-perl style re: s/regular expression/replacement/flags, like
  676. * subst but works on the user part of the uri */
  677. static int subst_user_f(struct sip_msg* msg, char* subst, char* ignored)
  678. {
  679. int rval;
  680. str* result;
  681. struct subst_expr* se;
  682. struct action act;
  683. struct run_act_ctx h;
  684. str user;
  685. char c;
  686. int nmatches;
  687. c=0;
  688. if (parse_sip_msg_uri(msg)<0){
  689. return -1; /* error, bad uri */
  690. }
  691. if (msg->parsed_uri.user.s==0){
  692. /* no user in uri */
  693. user.s="";
  694. user.len=0;
  695. }else{
  696. user=msg->parsed_uri.user;
  697. c=user.s[user.len];
  698. user.s[user.len]=0;
  699. }
  700. se=(struct subst_expr*)subst;
  701. result=subst_str(user.s, msg, se, &nmatches);/* pkg malloc'ed result */
  702. if (c) user.s[user.len]=c;
  703. if (result == NULL) {
  704. if (nmatches<0)
  705. LM_ERR("subst_user(): subst_str() failed\n");
  706. return -1;
  707. }
  708. /* result->s[result->len] = '\0'; --subst_str returns 0-term strings */
  709. memset(&act, 0, sizeof(act)); /* be on the safe side */
  710. act.type = SET_USER_T;
  711. act.val[0].type = STRING_ST;
  712. act.val[0].u.string = result->s;
  713. init_run_actions_ctx(&h);
  714. rval = do_action(&h, &act, msg);
  715. pkg_free(result->s);
  716. pkg_free(result);
  717. return rval;
  718. }
  719. /* sed-perl style re: s/regular expression/replacement/flags */
  720. static int subst_body_f(struct sip_msg* msg, char* subst, char* ignored)
  721. {
  722. struct lump* l;
  723. struct replace_lst* lst;
  724. struct replace_lst* rpl;
  725. char* begin;
  726. struct subst_expr* se;
  727. int off;
  728. int ret;
  729. int nmatches;
  730. str body;
  731. body.s = get_body(msg);
  732. if (body.s==0) {
  733. LM_ERR("failed to get the message body\n");
  734. return -1;
  735. }
  736. body.len = msg->len -(int)(body.s-msg->buf);
  737. if (body.len==0) {
  738. LM_DBG("message body has zero length\n");
  739. return -1;
  740. }
  741. se=(struct subst_expr*)subst;
  742. begin=body.s;
  743. off=begin-msg->buf;
  744. ret=-1;
  745. if ((lst=subst_run(se, begin, msg, &nmatches))==0)
  746. goto error; /* not found */
  747. for (rpl=lst; rpl; rpl=rpl->next){
  748. LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
  749. exports.name, rpl->offset+off,
  750. rpl->size, rpl->offset+off+msg->buf,
  751. rpl->rpl.len, rpl->rpl.s);
  752. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  753. goto error;
  754. /* hack to avoid re-copying rpl, possible because both
  755. * replace_lst & lumps use pkg_malloc */
  756. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0){
  757. LM_ERR("%s could not insert new lump\n",
  758. exports.name);
  759. goto error;
  760. }
  761. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  762. * not free it */
  763. rpl->rpl.s=0;
  764. rpl->rpl.len=0;
  765. }
  766. ret=1;
  767. error:
  768. LM_DBG("lst was %p\n", lst);
  769. if (lst) replace_lst_free(lst);
  770. if (nmatches<0)
  771. LM_ERR("%s subst_run failed\n", exports.name);
  772. return ret;
  773. }
  774. static inline int find_line_start(char *text, unsigned int text_len,
  775. char **buf, unsigned int *buf_len)
  776. {
  777. char *ch, *start;
  778. unsigned int len;
  779. start = *buf;
  780. len = *buf_len;
  781. while (text_len <= len) {
  782. if (strncmp(text, start, text_len) == 0) {
  783. *buf = start;
  784. *buf_len = len;
  785. return 1;
  786. }
  787. if ((ch = memchr(start, 13, len - 1))) {
  788. if (*(ch + 1) != 10) {
  789. LM_ERR("No LF after CR\n");
  790. return 0;
  791. }
  792. len = len - (ch - start + 2);
  793. start = ch + 2;
  794. } else {
  795. LM_ERR("No CRLF found\n");
  796. return 0;
  797. }
  798. }
  799. return 0;
  800. }
  801. /**
  802. * return:
  803. * 1: multipart
  804. */
  805. static int check_multipart(struct sip_msg *msg)
  806. {
  807. int mime;
  808. /* the function search for and parses the Content-Type hdr */
  809. mime = parse_content_type_hdr (msg);
  810. if(mime<0) {
  811. LM_ERR("failed to extract content type hdr\n");
  812. return -1;
  813. }
  814. if(mime!=MIMETYPE(MULTIPART,MIXED)) return 0;
  815. return 1;
  816. }
  817. /* Filters multipart/mixed body by leaving out everything else except
  818. * first body part of given content type. */
  819. static int filter_body_f(struct sip_msg* msg, char* _content_type,
  820. char* ignored)
  821. {
  822. char *start;
  823. unsigned int len;
  824. str *content_type, body;
  825. str boundary = {0,0};
  826. body.s = get_body(msg);
  827. if (body.s == 0) {
  828. LM_ERR("failed to get the message body\n");
  829. return -1;
  830. }
  831. body.len = msg->len - (int)(body.s - msg->buf);
  832. if (body.len == 0) {
  833. LM_DBG("message body has zero length\n");
  834. return -1;
  835. }
  836. if(check_multipart(msg)!=1) {
  837. LM_WARN("body not multipart\n");
  838. return -1;
  839. }
  840. if(get_boundary(msg, &boundary)!=0) {
  841. return -1;
  842. }
  843. content_type = (str *)_content_type;
  844. start = body.s;
  845. len = body.len;
  846. while (find_line_start("Content-Type: ", 14, &start, &len))
  847. {
  848. start = start + 14;
  849. len = len - 14;
  850. LM_DBG("line: [%.*s]\n", len, start);
  851. if (len > content_type->len + 2) {
  852. if (strncasecmp(start, content_type->s, content_type->len)== 0)
  853. {
  854. LM_DBG("found content type %.*s\n",
  855. content_type->len, content_type->s);
  856. start = start + content_type->len;
  857. if ((*start != 13) || (*(start + 1) != 10))
  858. {
  859. LM_ERR("no CRLF found after content type\n");
  860. goto err;
  861. }
  862. start = start + 2;
  863. len = len - content_type->len - 2;
  864. while ((len > 0) && ((*start == 13) || (*start == 10)))
  865. {
  866. len = len - 1;
  867. start = start + 1;
  868. }
  869. if (del_lump(msg, body.s - msg->buf, start - body.s, 0)== 0)
  870. {
  871. LM_ERR("deleting lump <%.*s> failed\n",
  872. (int)(start - body.s), body.s);
  873. goto err;
  874. }
  875. if (find_line_start(boundary.s, boundary.len, &start,
  876. &len))
  877. {
  878. if (del_lump(msg, start - msg->buf, len, 0) == 0)
  879. {
  880. LM_ERR("deleting lump <%.*s> failed\n", len, start);
  881. goto err;
  882. }
  883. else
  884. {
  885. pkg_free(boundary.s);
  886. return 1;
  887. }
  888. }
  889. else
  890. {
  891. LM_ERR("boundary not found after content\n");
  892. goto err;
  893. }
  894. }
  895. } else {
  896. goto err;
  897. }
  898. }
  899. err:
  900. if(boundary.s) pkg_free(boundary.s);
  901. return -1;
  902. }
  903. int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
  904. {
  905. struct hdr_field *hf;
  906. struct lump* l;
  907. int cnt;
  908. gparam_p gp;
  909. gp = (gparam_p)str_hf;
  910. cnt=0;
  911. /* we need to be sure we have seen all HFs */
  912. parse_headers(msg, HDR_EOH_F, 0);
  913. for (hf=msg->headers; hf; hf=hf->next) {
  914. /* for well known header names str_hf->s will be set to NULL
  915. during parsing of kamailio.cfg and str_hf->len contains
  916. the header type */
  917. if(gp->type==GPARAM_TYPE_INT)
  918. {
  919. if (gp->v.i!=hf->type)
  920. continue;
  921. } else {
  922. if (hf->name.len!=gp->v.str.len)
  923. continue;
  924. if (cmp_hdrname_str(&hf->name, &gp->v.str)!=0)
  925. continue;
  926. }
  927. l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
  928. if (l==0) {
  929. LM_ERR("no memory\n");
  930. return -1;
  931. }
  932. cnt++;
  933. }
  934. return cnt==0 ? -1 : 1;
  935. }
  936. static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo)
  937. {
  938. struct hdr_field *hf;
  939. struct lump* l;
  940. int cnt;
  941. regex_t *re;
  942. char c;
  943. regmatch_t pmatch;
  944. re = (regex_t*)key;
  945. cnt=0;
  946. /* we need to be sure we have seen all HFs */
  947. parse_headers(msg, HDR_EOH_F, 0);
  948. for (hf=msg->headers; hf; hf=hf->next)
  949. {
  950. c = hf->name.s[hf->name.len];
  951. hf->name.s[hf->name.len] = '\0';
  952. if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
  953. {
  954. hf->name.s[hf->name.len] = c;
  955. continue;
  956. }
  957. hf->name.s[hf->name.len] = c;
  958. l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
  959. if (l==0)
  960. {
  961. LM_ERR("cannot remove header\n");
  962. return -1;
  963. }
  964. cnt++;
  965. }
  966. return cnt==0 ? -1 : 1;
  967. }
  968. static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
  969. {
  970. struct hdr_field *hf;
  971. gparam_p gp;
  972. gp = (gparam_p)str_hf;
  973. /* we need to be sure we have seen all HFs */
  974. parse_headers(msg, HDR_EOH_F, 0);
  975. for (hf=msg->headers; hf; hf=hf->next) {
  976. if(gp->type==GPARAM_TYPE_INT)
  977. {
  978. if (gp->v.i!=hf->type)
  979. continue;
  980. } else {
  981. if (hf->name.len!=gp->v.str.len)
  982. continue;
  983. if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
  984. continue;
  985. }
  986. return 1;
  987. }
  988. return -1;
  989. }
  990. static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo)
  991. {
  992. struct hdr_field *hf;
  993. regex_t *re;
  994. regmatch_t pmatch;
  995. char c;
  996. re = (regex_t*)key;
  997. /* we need to be sure we have seen all HFs */
  998. parse_headers(msg, HDR_EOH_F, 0);
  999. for (hf=msg->headers; hf; hf=hf->next)
  1000. {
  1001. c = hf->name.s[hf->name.len];
  1002. hf->name.s[hf->name.len] = '\0';
  1003. if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
  1004. {
  1005. hf->name.s[hf->name.len] = c;
  1006. continue;
  1007. }
  1008. hf->name.s[hf->name.len] = c;
  1009. return 1;
  1010. }
  1011. return -1;
  1012. }
  1013. static int fixup_substre(void** param, int param_no)
  1014. {
  1015. struct subst_expr* se;
  1016. str subst;
  1017. LM_DBG("%s module -- fixing %s\n", exports.name, (char*)(*param));
  1018. if (param_no!=1) return 0;
  1019. subst.s=*param;
  1020. subst.len=strlen(*param);
  1021. se=subst_parser(&subst);
  1022. if (se==0){
  1023. LM_ERR("%s: bad subst. re %s\n", exports.name,
  1024. (char*)*param);
  1025. return E_BAD_RE;
  1026. }
  1027. /* don't free string -- needed for specifiers */
  1028. /* pkg_free(*param); */
  1029. /* replace it with the compiled subst. re */
  1030. *param=se;
  1031. return 0;
  1032. }
  1033. static int append_time_f(struct sip_msg* msg, char* p1, char *p2)
  1034. {
  1035. size_t len;
  1036. char time_str[MAX_TIME];
  1037. time_t now;
  1038. struct tm *bd_time;
  1039. now=time(0);
  1040. bd_time=gmtime(&now);
  1041. if (bd_time==NULL) {
  1042. LM_ERR("gmtime failed\n");
  1043. return -1;
  1044. }
  1045. len=strftime(time_str, MAX_TIME, TIME_FORMAT, bd_time);
  1046. if (len>MAX_TIME-2 || len==0) {
  1047. LM_ERR("unexpected time length\n");
  1048. return -1;
  1049. }
  1050. time_str[len]='\r';
  1051. time_str[len+1]='\n';
  1052. if (add_lump_rpl(msg, time_str, len+2, LUMP_RPL_HDR)==0)
  1053. {
  1054. LM_ERR("unable to add lump\n");
  1055. return -1;
  1056. }
  1057. return 1;
  1058. }
  1059. static int append_time_request_f(struct sip_msg* msg, char* p1, char *p2)
  1060. {
  1061. str time_str = {0, 0};
  1062. time_t now;
  1063. struct tm *bd_time;
  1064. struct hdr_field *hf = msg->headers;
  1065. struct lump *anchor = anchor_lump(msg, hf->name.s + hf->len - msg->buf, 0, 0);
  1066. now=time(0);
  1067. bd_time=gmtime(&now);
  1068. if (bd_time==NULL) {
  1069. LM_ERR("gmtime failed\n");
  1070. goto error;
  1071. }
  1072. time_str.s = pkg_malloc(MAX_TIME);
  1073. time_str.len=strftime(time_str.s, MAX_TIME, TIME_FORMAT, bd_time);
  1074. if (time_str.len>MAX_TIME-2 || time_str.len==0) {
  1075. LM_ERR("unexpected time length\n");
  1076. goto error;
  1077. }
  1078. time_str.s[time_str.len++]='\r';
  1079. time_str.s[time_str.len++]='\n';
  1080. if (anchor == NULL)
  1081. {
  1082. LM_ERR("Problem with getting anchor");
  1083. goto error;
  1084. }
  1085. if (insert_new_lump_after(anchor, time_str.s, time_str.len, 0) == 0)
  1086. {
  1087. LM_ERR("unable to add lump\n");
  1088. goto error;
  1089. }
  1090. return 1;
  1091. error:
  1092. if (time_str.s != NULL)
  1093. pkg_free(time_str.s);
  1094. return -1;
  1095. }
  1096. static int set_body_f(struct sip_msg* msg, char* p1, char* p2)
  1097. {
  1098. struct lump *anchor;
  1099. char* buf;
  1100. int len;
  1101. char* value_s;
  1102. int value_len;
  1103. str body = {0,0};
  1104. str nb = {0,0};
  1105. str nc = {0,0};
  1106. if(p1==0 || p2==0)
  1107. {
  1108. LM_ERR("invalid parameters\n");
  1109. return -1;
  1110. }
  1111. if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
  1112. {
  1113. LM_ERR("unable to get p1\n");
  1114. return -1;
  1115. }
  1116. if(nb.s==NULL || nb.len == 0)
  1117. {
  1118. LM_ERR("invalid body parameter\n");
  1119. return -1;
  1120. }
  1121. if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
  1122. {
  1123. LM_ERR("unable to get p2\n");
  1124. return -1;
  1125. }
  1126. if(nc.s==NULL || nc.len == 0)
  1127. {
  1128. LM_ERR("invalid content-type parameter\n");
  1129. return -1;
  1130. }
  1131. body.len = 0;
  1132. body.s = get_body(msg);
  1133. if (body.s==0)
  1134. {
  1135. LM_ERR("malformed sip message\n");
  1136. return -1;
  1137. }
  1138. del_nonshm_lump( &(msg->body_lumps) );
  1139. msg->body_lumps = NULL;
  1140. if (msg->content_length)
  1141. {
  1142. body.len = get_content_length( msg );
  1143. if(body.len > 0)
  1144. {
  1145. if(body.s+body.len>msg->buf+msg->len)
  1146. {
  1147. LM_ERR("invalid content length: %d\n", body.len);
  1148. return -1;
  1149. }
  1150. if(del_lump(msg, body.s - msg->buf, body.len, 0) == 0)
  1151. {
  1152. LM_ERR("cannot delete existing body");
  1153. return -1;
  1154. }
  1155. }
  1156. }
  1157. anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
  1158. if (anchor == 0)
  1159. {
  1160. LM_ERR("failed to get anchor\n");
  1161. return -1;
  1162. }
  1163. if (msg->content_length==0)
  1164. {
  1165. /* need to add Content-Length */
  1166. len = nb.len;
  1167. value_s=int2str(len, &value_len);
  1168. LM_DBG("content-length: %d (%s)\n", value_len, value_s);
  1169. len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
  1170. buf=pkg_malloc(sizeof(char)*(len));
  1171. if (buf==0)
  1172. {
  1173. LM_ERR("out of pkg memory\n");
  1174. return -1;
  1175. }
  1176. memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  1177. memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
  1178. memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
  1179. if (insert_new_lump_after(anchor, buf, len, 0) == 0)
  1180. {
  1181. LM_ERR("failed to insert content-length lump\n");
  1182. pkg_free(buf);
  1183. return -1;
  1184. }
  1185. }
  1186. /* add content-type */
  1187. if(msg->content_type==NULL || msg->content_type->body.len!=nc.len
  1188. || strncmp(msg->content_type->body.s, nc.s, nc.len)!=0)
  1189. {
  1190. if(msg->content_type!=NULL)
  1191. if(del_lump(msg, msg->content_type->name.s-msg->buf,
  1192. msg->content_type->len, 0) == 0)
  1193. {
  1194. LM_ERR("failed to delete content type\n");
  1195. return -1;
  1196. }
  1197. value_len = nc.len;
  1198. len=sizeof("Content-Type: ") - 1 + value_len + CRLF_LEN;
  1199. buf=pkg_malloc(sizeof(char)*(len));
  1200. if (buf==0)
  1201. {
  1202. LM_ERR("out of pkg memory\n");
  1203. return -1;
  1204. }
  1205. memcpy(buf, "Content-Type: ", sizeof("Content-Type: ") - 1);
  1206. memcpy(buf+sizeof("Content-Type: ") - 1, nc.s, value_len);
  1207. memcpy(buf+sizeof("Content-Type: ") - 1 + value_len, CRLF, CRLF_LEN);
  1208. if (insert_new_lump_after(anchor, buf, len, 0) == 0)
  1209. {
  1210. LM_ERR("failed to insert content-type lump\n");
  1211. pkg_free(buf);
  1212. return -1;
  1213. }
  1214. }
  1215. anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
  1216. if (anchor == 0)
  1217. {
  1218. LM_ERR("failed to get body anchor\n");
  1219. return -1;
  1220. }
  1221. buf=pkg_malloc(sizeof(char)*(nb.len));
  1222. if (buf==0)
  1223. {
  1224. LM_ERR("out of pkg memory\n");
  1225. return -1;
  1226. }
  1227. memcpy(buf, nb.s, nb.len);
  1228. if (insert_new_lump_after(anchor, buf, nb.len, 0) == 0)
  1229. {
  1230. LM_ERR("failed to insert body lump\n");
  1231. pkg_free(buf);
  1232. return -1;
  1233. }
  1234. LM_DBG("new body: [%.*s]", nb.len, nb.s);
  1235. return 1;
  1236. }
  1237. static int set_rpl_body_f(struct sip_msg* msg, char* p1, char* p2)
  1238. {
  1239. char* buf;
  1240. int len;
  1241. int value_len;
  1242. str nb = {0,0};
  1243. str nc = {0,0};
  1244. if(p1==0 || p2==0)
  1245. {
  1246. LM_ERR("invalid parameters\n");
  1247. return -1;
  1248. }
  1249. if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
  1250. {
  1251. LM_ERR("unable to get p1\n");
  1252. return -1;
  1253. }
  1254. if(nb.s==NULL || nb.len == 0)
  1255. {
  1256. LM_ERR("invalid body parameter\n");
  1257. return -1;
  1258. }
  1259. if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
  1260. {
  1261. LM_ERR("unable to get p2\n");
  1262. return -1;
  1263. }
  1264. if(nc.s==NULL || nc.len == 0)
  1265. {
  1266. LM_ERR("invalid content-type parameter\n");
  1267. return -1;
  1268. }
  1269. /* add content-type */
  1270. value_len = nc.len;
  1271. len=sizeof("Content-Type: ") - 1 + value_len + CRLF_LEN;
  1272. buf=pkg_malloc(sizeof(char)*(len));
  1273. if (buf==0)
  1274. {
  1275. LM_ERR("out of pkg memory\n");
  1276. return -1;
  1277. }
  1278. memcpy(buf, "Content-Type: ", sizeof("Content-Type: ") - 1);
  1279. memcpy(buf+sizeof("Content-Type: ") - 1, nc.s, value_len);
  1280. memcpy(buf+sizeof("Content-Type: ") - 1 + value_len, CRLF, CRLF_LEN);
  1281. if (add_lump_rpl(msg, buf, len, LUMP_RPL_HDR) == 0)
  1282. {
  1283. LM_ERR("failed to insert content-type lump\n");
  1284. pkg_free(buf);
  1285. return -1;
  1286. }
  1287. pkg_free(buf);
  1288. if (add_lump_rpl( msg, nb.s, nb.len, LUMP_RPL_BODY)==0) {
  1289. LM_ERR("cannot add body lump\n");
  1290. return -1;
  1291. }
  1292. return 1;
  1293. }
  1294. static str* generate_boundary(str *txt, str *content_type,
  1295. str *content_disposition, str *delimiter, unsigned int initial)
  1296. {
  1297. unsigned int i = 0;
  1298. str cth = {"Content-Type: ", 14};
  1299. str cdh = {"Content-Disposition: ", 21};
  1300. str* n;
  1301. unsigned int flag = 0;
  1302. if(txt==NULL||txt->len==0
  1303. ||content_type==NULL||content_type->len==0
  1304. ||delimiter==NULL||delimiter->len==0)
  1305. {
  1306. LM_ERR("invalid parameters\n");
  1307. return NULL;
  1308. }
  1309. if (delimiter->s[0] == '-') {
  1310. LM_ERR("delimiter with initial '-'. Invalid parameter.\n");
  1311. return NULL;
  1312. }
  1313. n = pkg_malloc(sizeof(str));
  1314. if(n==NULL)
  1315. {
  1316. LM_ERR("out of pkg memory\n");
  1317. return NULL;
  1318. }
  1319. n->len = delimiter->len + 2 + CRLF_LEN;
  1320. if(initial) n->len = 2*n->len;
  1321. if(strncmp("\r\n\r\n", txt->s+txt->len-4,4)!=0)
  1322. {
  1323. n->len = n->len + CRLF_LEN;
  1324. flag = 1;
  1325. LM_DBG("adding final CRLF+CRLF\n");
  1326. }
  1327. n->len=n->len + cth.len + content_type->len + 2*CRLF_LEN;
  1328. if(content_disposition->len>0)
  1329. {
  1330. n->len = n->len + cdh.len + content_disposition->len + CRLF_LEN;
  1331. }
  1332. n->len = n->len + txt->len;
  1333. n->s = pkg_malloc(sizeof(char)*(n->len));
  1334. if(n->s==0)
  1335. {
  1336. LM_ERR("out of pkg memory\n");
  1337. pkg_free(n);
  1338. return NULL;
  1339. }
  1340. memset(n->s, 0, sizeof(char)*n->len);
  1341. if(initial)
  1342. {
  1343. memcpy(n->s, "--", 2); i=2;
  1344. memcpy(n->s+i, delimiter->s, delimiter->len); i=i+delimiter->len;
  1345. memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
  1346. }
  1347. memcpy(n->s+i, cth.s, cth.len); i=i+cth.len;
  1348. memcpy(n->s+i, content_type->s, content_type->len); i=i+content_type->len;
  1349. memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
  1350. if(content_disposition->len>0)
  1351. {
  1352. memcpy(n->s+i, cdh.s, cdh.len); i=i+cdh.len;
  1353. memcpy(n->s+i, content_disposition->s, content_disposition->len);
  1354. i=i+content_disposition->len;
  1355. memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
  1356. }
  1357. memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
  1358. memcpy(n->s+i, txt->s, txt->len); i=i+txt->len;
  1359. if(flag) { memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN; }
  1360. memcpy(n->s+i, "--", 2); i=i+2;
  1361. memcpy(n->s+i, delimiter->s, delimiter->len); i=i+delimiter->len;
  1362. memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
  1363. if(i!=n->len)
  1364. {
  1365. LM_ERR("out of bounds\n");
  1366. }
  1367. return n;
  1368. }
  1369. int set_multibody_helper(struct sip_msg* msg, char* p1, char* p2, char* p3)
  1370. {
  1371. struct lump *anchor;
  1372. char* buf = NULL;
  1373. int len;
  1374. char* value_s;
  1375. int value_len;
  1376. str body = {0,0};
  1377. str nb = {0,0};
  1378. str oc = {0,0};
  1379. str cd = {0,0};
  1380. str delimiter = {0,0};
  1381. str default_delimiter = {"unique-boundary-1", 17};
  1382. str nc = {0,0};
  1383. str cth = {"Content-Type: ", 14};
  1384. str* nbb = NULL;
  1385. unsigned int convert = 0;
  1386. fparam_t header;
  1387. header.orig = NULL;
  1388. header.type = FPARAM_STR;
  1389. header.v.str.s = "Mime-Version: 1.0\r\n";
  1390. header.v.str.len = 19;
  1391. if(p3==0)
  1392. {
  1393. delimiter.s = default_delimiter.s;
  1394. delimiter.len = default_delimiter.len;
  1395. }
  1396. else
  1397. {
  1398. if(fixup_get_svalue(msg, (gparam_p)p3, &delimiter)!=0)
  1399. {
  1400. LM_ERR("unable to get p3\n");
  1401. return -1;
  1402. }
  1403. if(delimiter.s==NULL || delimiter.len == 0)
  1404. {
  1405. LM_ERR("invalid boundary parameter\n");
  1406. return -1;
  1407. }
  1408. }
  1409. LM_DBG("delimiter<%d>:[%.*s]\n", delimiter.len, delimiter.len, delimiter.s);
  1410. if(p1==0 || p2==0)
  1411. {
  1412. if(check_multipart(msg)==1) {
  1413. LM_WARN("body is already multipart. Do nothing\n");
  1414. return -1;
  1415. }
  1416. convert = 1;
  1417. }
  1418. else
  1419. {
  1420. if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
  1421. {
  1422. LM_ERR("unable to get p1\n");
  1423. return -1;
  1424. }
  1425. if(nb.s==NULL || nb.len == 0)
  1426. {
  1427. LM_ERR("invalid body parameter\n");
  1428. return -1;
  1429. }
  1430. if(fixup_get_svalue(msg, (gparam_p)p2, &oc)!=0)
  1431. {
  1432. LM_ERR("unable to get p2\n");
  1433. return -1;
  1434. }
  1435. if(oc.s==NULL || oc.len==0)
  1436. {
  1437. LM_ERR("invalid content-type parameter\n");
  1438. return -1;
  1439. }
  1440. if(check_multipart(msg)==1) {
  1441. convert = -1;
  1442. }
  1443. }
  1444. body.len = 0;
  1445. body.s = get_body(msg);
  1446. if(body.s==0)
  1447. {
  1448. LM_ERR("malformed sip message\n");
  1449. return -1;
  1450. }
  1451. body.len = msg->len -(int)(body.s-msg->buf);
  1452. del_nonshm_lump( &(msg->body_lumps) );
  1453. msg->body_lumps = NULL;
  1454. if(msg->content_length)
  1455. {
  1456. if(body.len > 0)
  1457. {
  1458. if(body.s+body.len>msg->buf+msg->len)
  1459. {
  1460. LM_ERR("invalid content length: %d\n", body.len);
  1461. return -1;
  1462. }
  1463. if(convert==1)
  1464. {
  1465. /* need to copy body */
  1466. nb.s=pkg_malloc(sizeof(char)*body.len);
  1467. if (nb.s==0)
  1468. {
  1469. LM_ERR("out of pkg memory\n");
  1470. return -1;
  1471. }
  1472. memcpy(nb.s, body.s, body.len);
  1473. nb.len = body.len;
  1474. if(msg->content_type!=NULL && msg->content_type->body.s!=NULL)
  1475. {
  1476. oc.len = msg->content_type->body.len;
  1477. oc.s=pkg_malloc(sizeof(char)*oc.len);
  1478. if (oc.s==0)
  1479. {
  1480. LM_ERR("out of pkg memory\n");
  1481. goto error;
  1482. }
  1483. memcpy(oc.s, msg->content_type->body.s, oc.len);
  1484. }
  1485. }
  1486. if(del_lump(msg, body.s-msg->buf, body.len, 0) == 0)
  1487. {
  1488. LM_ERR("cannot delete existing body");
  1489. goto error;
  1490. }
  1491. }
  1492. }
  1493. anchor = anchor_lump(msg, msg->unparsed-msg->buf, 0, 0);
  1494. if(anchor==0)
  1495. {
  1496. LM_ERR("failed to get anchor\n");
  1497. goto error;
  1498. }
  1499. /* get initial boundary */
  1500. nbb = generate_boundary(&nb, &oc, &cd, &delimiter, 1);
  1501. if(nbb==NULL)
  1502. {
  1503. LM_ERR("couldn't create initial boundary\n");
  1504. goto error;
  1505. }
  1506. if(msg->content_length==0)
  1507. {
  1508. /* need to add Content-Length */
  1509. len = nbb->len;
  1510. value_s=int2str(len, &value_len);
  1511. len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
  1512. buf=pkg_malloc(sizeof(char)*len);
  1513. if (buf==0)
  1514. {
  1515. LM_ERR("out of pkg memory\n");
  1516. goto error;
  1517. }
  1518. memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  1519. memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
  1520. memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
  1521. if (insert_new_lump_after(anchor, buf, len, 0) == 0)
  1522. {
  1523. LM_ERR("failed to insert content-length lump\n");
  1524. goto error;
  1525. }
  1526. buf = NULL;
  1527. }
  1528. if(convert!=-1)
  1529. {
  1530. /* set new content type with delimiter */
  1531. nc.len = delimiter.len + 27;
  1532. nc.s = pkg_malloc(sizeof(char)*nc.len);
  1533. memcpy(nc.s, "multipart/mixed;boundary=\"", 26);
  1534. memcpy(nc.s+26, delimiter.s, delimiter.len);
  1535. nc.s[26+delimiter.len] = '"';
  1536. LM_DBG("content-type<%d>:[%.*s]\n", nc.len, nc.len, nc.s);
  1537. /* add content-type */
  1538. if(msg->content_type==NULL || msg->content_type->body.len!=nc.len
  1539. || strncmp(msg->content_type->body.s, nc.s, nc.len)!=0)
  1540. {
  1541. if(msg->content_type!=NULL)
  1542. if(del_lump(msg, msg->content_type->name.s-msg->buf,
  1543. msg->content_type->len, 0) == 0)
  1544. {
  1545. LM_ERR("failed to delete content type\n");
  1546. goto error;
  1547. }
  1548. value_len = nc.len;
  1549. len = cth.len + value_len + CRLF_LEN;
  1550. buf = pkg_malloc(sizeof(char)*len);
  1551. if(buf==0)
  1552. {
  1553. LM_ERR("out of pkg memory\n");
  1554. goto error;
  1555. }
  1556. memcpy(buf, cth.s, cth.len);
  1557. memcpy(buf + cth.len, nc.s, value_len);
  1558. memcpy(buf + cth.len + value_len, CRLF, CRLF_LEN);
  1559. if (insert_new_lump_after(anchor, buf, len, 0) == 0)
  1560. {
  1561. LM_ERR("failed to insert content-type lump\n");
  1562. goto error;
  1563. }
  1564. buf = NULL;
  1565. }
  1566. /* add Mime-Version header */
  1567. if(add_hf_helper(msg, 0, 0, &header, 0, 0)<0)
  1568. {
  1569. LM_ERR("failed to add Mime-Version header\n");
  1570. goto error;
  1571. }
  1572. }
  1573. anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
  1574. if(anchor==0)
  1575. {
  1576. LM_ERR("failed to get body anchor\n");
  1577. goto error;
  1578. }
  1579. if(insert_new_lump_after(anchor, nbb->s, nbb->len, 0)==0)
  1580. {
  1581. LM_ERR("failed to insert body lump\n");
  1582. goto error;
  1583. }
  1584. pkg_free(nbb);
  1585. if(nc.s!=NULL) pkg_free(nc.s);
  1586. LM_DBG("set flag FL_BODY_MULTIPART\n");
  1587. msg->msg_flags |= FL_BODY_MULTIPART;
  1588. return 1;
  1589. error:
  1590. if(nbb!=NULL) { pkg_free(nbb->s); pkg_free(nbb); }
  1591. if(nc.s!=NULL) pkg_free(nc.s);
  1592. if(buf!=NULL) pkg_free(buf);
  1593. if(convert && nb.s!=NULL) pkg_free(nb.s);
  1594. if(convert && oc.s!=NULL) pkg_free(oc.s);
  1595. return -1;
  1596. }
  1597. static int set_multibody_0(struct sip_msg* msg, char* p1, char* p2, char* p3)
  1598. {
  1599. return set_multibody_helper(msg, NULL, NULL, NULL);
  1600. }
  1601. static int set_multibody_1(struct sip_msg* msg, char* p1, char* p2, char* p3)
  1602. {
  1603. return set_multibody_helper(msg, NULL, NULL, p1);
  1604. }
  1605. static int set_multibody_2(struct sip_msg* msg, char* p1, char* p2, char* p3)
  1606. {
  1607. return set_multibody_helper(msg, p1, p2, NULL);
  1608. }
  1609. static int set_multibody_3(struct sip_msg* msg, char* p1, char* p2, char *p3)
  1610. {
  1611. return set_multibody_helper(msg, p1, p2, p3);
  1612. }
  1613. int append_multibody_helper(struct sip_msg* msg, char* p1, char* p2, char* p3)
  1614. {
  1615. struct lump *l;
  1616. int off;
  1617. str body = {0,0};
  1618. str nc = {0,0};
  1619. str cd = {0,0};
  1620. str txt = {0,0};
  1621. str* nbb = NULL;
  1622. str delimiter = {0,0};
  1623. if(p1==0 || p2==0)
  1624. {
  1625. LM_ERR("invalid parameters\n");
  1626. return -1;
  1627. }
  1628. if(fixup_get_svalue(msg, (gparam_p)p1, &txt)!=0)
  1629. {
  1630. LM_ERR("unable to get p1\n");
  1631. return -1;
  1632. }
  1633. if(txt.s==NULL || txt.len==0)
  1634. {
  1635. LM_ERR("invalid body parameter\n");
  1636. return -1;
  1637. }
  1638. if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
  1639. {
  1640. LM_ERR("unable to get p2\n");
  1641. return -1;
  1642. }
  1643. if(nc.s==NULL || nc.len==0)
  1644. {
  1645. LM_ERR("invalid content-type parameter\n");
  1646. return -1;
  1647. }
  1648. if(p3!=NULL)
  1649. {
  1650. if(fixup_get_svalue(msg, (gparam_p)p3, &cd)!=0)
  1651. {
  1652. LM_ERR("unable to get p3\n");
  1653. return -1;
  1654. }
  1655. }
  1656. body.s = get_body(msg);
  1657. if(body.s==0) {
  1658. LM_ERR("failed to get the message body\n");
  1659. return -1;
  1660. }
  1661. body.len = msg->len -(int)(body.s-msg->buf);
  1662. if(body.len==0) {
  1663. LM_DBG("message body has zero length\n");
  1664. return -1;
  1665. }
  1666. off=body.s-msg->buf;
  1667. if((l=anchor_lump(msg, off+body.len, 0, 0))==0)
  1668. {
  1669. LM_ERR("WTF\n");
  1670. return -1;
  1671. }
  1672. /* get delimiter no initial -- */
  1673. if(get_mixed_part_delimiter(&msg->content_type->body, &delimiter) < 0) {
  1674. LM_ERR("Cannot get boundary. Is body multipart?\n");
  1675. return -1;
  1676. }
  1677. nbb = generate_boundary(&txt, &nc, &cd, &delimiter, 0);
  1678. if(nbb==NULL)
  1679. {
  1680. LM_ERR("couldn't create initial boundary\n");
  1681. return -1;
  1682. }
  1683. if(insert_new_lump_after(l, nbb->s, nbb->len, 0)==0){
  1684. LM_ERR("could not insert new lump\n");
  1685. pkg_free(nbb->s); pkg_free(nbb);
  1686. return -1;
  1687. }
  1688. pkg_free(nbb);
  1689. if(!(msg->msg_flags&FL_BODY_MULTIPART))
  1690. {
  1691. LM_DBG("set flag FL_BODY_MULTIPART\n");
  1692. msg->msg_flags |= FL_BODY_MULTIPART;
  1693. }
  1694. return 1;
  1695. }
  1696. static int append_multibody_2(struct sip_msg* msg, char* p1, char* p2)
  1697. {
  1698. return append_multibody_helper(msg, p1, p2, NULL);
  1699. }
  1700. static int append_multibody_3(struct sip_msg* msg, char* p1, char* p2, char *p3)
  1701. {
  1702. return append_multibody_helper(msg, p1, p2, p3);
  1703. }
  1704. static int fixup_multibody_f(void** param, int param_no)
  1705. {
  1706. int ret;
  1707. fparam_t* fp;
  1708. if(param_no<=3){
  1709. if((ret=fix_param_types(FPARAM_PVE, param))<0){
  1710. ERR("Cannot convert function parameter %d to spve \n",
  1711. param_no);
  1712. return E_UNSPEC;
  1713. } else {
  1714. fp=(fparam_t*)*param;
  1715. if((ret==0) && (fp->v.pve->spec==0
  1716. || fp->v.pve->spec->getf==0)){
  1717. fparam_free_restore(param);
  1718. return fix_param_types(FPARAM_STR, param);
  1719. } else if(ret==1)
  1720. return fix_param_types(FPARAM_STR, param);
  1721. return ret;
  1722. }
  1723. } else {
  1724. LM_ERR("wrong number of parameters\n");
  1725. return E_UNSPEC;
  1726. }
  1727. }
  1728. static inline int get_line(char *s, int len)
  1729. {
  1730. char *ch;
  1731. if ((ch = memchr(s, 13, len))) {
  1732. if (*(ch + 1) != 10) {
  1733. LM_ERR("No LF after CR\n");
  1734. return 0;
  1735. }
  1736. return ch - s + 2;
  1737. } else {
  1738. LM_ERR("No CRLF found\n");
  1739. return len;
  1740. }
  1741. return 0;
  1742. }
  1743. static int remove_multibody_f(struct sip_msg* msg, char* p1)
  1744. {
  1745. char *start, *end;
  1746. unsigned int len, t;
  1747. str content_type, body;
  1748. str boundary = {0,0};
  1749. if(p1==0)
  1750. {
  1751. LM_ERR("invalid parameters\n");
  1752. return -1;
  1753. }
  1754. if(fixup_get_svalue(msg, (gparam_p)p1, &content_type)!=0)
  1755. {
  1756. LM_ERR("unable to get p1\n");
  1757. return -1;
  1758. }
  1759. body.s = get_body(msg);
  1760. if (body.s == 0) {
  1761. LM_ERR("failed to get the message body\n");
  1762. return -1;
  1763. }
  1764. body.len = msg->len - (int)(body.s - msg->buf);
  1765. if (body.len == 0) {
  1766. LM_DBG("message body has zero length\n");
  1767. return -1;
  1768. }
  1769. if(get_boundary(msg, &boundary)!=0) {
  1770. LM_ERR("Cannot get boundary. Is body multipart?\n");
  1771. return -1;
  1772. }
  1773. start = body.s;
  1774. len = body.len;
  1775. while (find_line_start("Content-Type: ", 14, &start, &len))
  1776. {
  1777. end = start + 14;
  1778. len = len - 14;
  1779. if (len > (content_type.len + 2)) {
  1780. if (strncasecmp(end, content_type.s, content_type.len)== 0)
  1781. {
  1782. LM_DBG("found content type %.*s\n",
  1783. content_type.len, content_type.s);
  1784. end = end + content_type.len;
  1785. if ((*end != 13) || (*(end + 1) != 10))
  1786. {
  1787. LM_ERR("no CRLF found after content type\n");
  1788. goto err;
  1789. }
  1790. end = end + 2;
  1791. len = len - content_type.len - 2;
  1792. if (find_line_start(boundary.s, boundary.len, &end,
  1793. &len))
  1794. {
  1795. LM_DBG("found boundary %.*s\n", boundary.len, boundary.s);
  1796. end = end + boundary.len;
  1797. len = len - boundary.len;
  1798. if (!(t = get_line(end, len))) goto err;
  1799. end += t; len = end-start;
  1800. if (del_lump(msg, start - msg->buf, len, 0) == 0)
  1801. {
  1802. LM_ERR("deleting lump <%.*s> failed\n", len, start);
  1803. goto err;
  1804. }
  1805. pkg_free(boundary.s);
  1806. if(!(msg->msg_flags&FL_BODY_MULTIPART))
  1807. {
  1808. LM_DBG("set flag FL_BODY_MULTIPART\n");
  1809. msg->msg_flags |= FL_BODY_MULTIPART;
  1810. }
  1811. return 1;
  1812. }
  1813. LM_ERR("boundary not found after content\n");
  1814. goto err;
  1815. }
  1816. start = end;
  1817. }
  1818. else goto err;
  1819. }
  1820. err:
  1821. pkg_free(boundary.s);
  1822. return -1;
  1823. }
  1824. static int append_to_reply_f(struct sip_msg* msg, char* key, char* str0)
  1825. {
  1826. str s0;
  1827. if(key==NULL)
  1828. {
  1829. LM_ERR("bad parameters\n");
  1830. return -1;
  1831. }
  1832. if(fixup_get_svalue(msg, (gparam_p)key, &s0)!=0)
  1833. {
  1834. LM_ERR("cannot print the format\n");
  1835. return -1;
  1836. }
  1837. if ( add_lump_rpl( msg, s0.s, s0.len, LUMP_RPL_HDR)==0 )
  1838. {
  1839. LM_ERR("unable to add lump_rl\n");
  1840. return -1;
  1841. }
  1842. return 1;
  1843. }
  1844. /* add str1 to end of header or str1.r-uri.str2 */
  1845. int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
  1846. gparam_p hfval, int mode, gparam_p hfanc)
  1847. {
  1848. struct lump* anchor;
  1849. struct hdr_field *hf;
  1850. char *s;
  1851. int len;
  1852. str s0;
  1853. if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
  1854. LM_ERR("error while parsing message\n");
  1855. return -1;
  1856. }
  1857. hf = 0;
  1858. if(hfanc!=NULL) {
  1859. for (hf=msg->headers; hf; hf=hf->next) {
  1860. if(hfanc->type==GPARAM_TYPE_INT)
  1861. {
  1862. if (hfanc->v.i!=hf->type)
  1863. continue;
  1864. } else {
  1865. if (hf->name.len!=hfanc->v.str.len)
  1866. continue;
  1867. if (cmp_hdrname_str(&hf->name,&hfanc->v.str)!=0)
  1868. continue;
  1869. }
  1870. break;
  1871. }
  1872. }
  1873. if(mode == 0) { /* append */
  1874. if(hf==0) { /* after last header */
  1875. anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
  1876. } else { /* after hf */
  1877. anchor = anchor_lump(msg, hf->name.s + hf->len - msg->buf, 0, 0);
  1878. }
  1879. } else { /* insert */
  1880. if(hf==0) { /* before first header */
  1881. anchor = anchor_lump(msg, msg->headers->name.s - msg->buf, 0, 0);
  1882. } else { /* before hf */
  1883. anchor = anchor_lump(msg, hf->name.s - msg->buf, 0, 0);
  1884. }
  1885. }
  1886. if(anchor == 0) {
  1887. LM_ERR("can't get anchor\n");
  1888. return -1;
  1889. }
  1890. if(str1) {
  1891. s0 = *str1;
  1892. } else {
  1893. if(hfval) {
  1894. if(fixup_get_svalue(msg, hfval, &s0)!=0)
  1895. {
  1896. LM_ERR("cannot print the format\n");
  1897. return -1;
  1898. }
  1899. } else {
  1900. s0.len = 0;
  1901. s0.s = 0;
  1902. }
  1903. }
  1904. len=s0.len;
  1905. if (str2) len+= str2->len + REQ_LINE(msg).uri.len;
  1906. if(len==0) {
  1907. LM_INFO("nothing to add\n");
  1908. return -1;
  1909. }
  1910. s = (char*)pkg_malloc(len);
  1911. if (!s) {
  1912. LM_ERR("no pkg memory left\n");
  1913. return -1;
  1914. }
  1915. if (likely(s0.len>0)){
  1916. memcpy(s, s0.s, s0.len);
  1917. }
  1918. if (str2) {
  1919. memcpy(s+s0.len, REQ_LINE(msg).uri.s, REQ_LINE(msg).uri.len);
  1920. memcpy(s+s0.len+REQ_LINE(msg).uri.len, str2->s, str2->len );
  1921. }
  1922. if (insert_new_lump_before(anchor, s, len, 0) == 0) {
  1923. LM_ERR("can't insert lump\n");
  1924. pkg_free(s);
  1925. return -1;
  1926. }
  1927. return 1;
  1928. }
  1929. static int append_hf_1(struct sip_msg *msg, char *str1, char *str2 )
  1930. {
  1931. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 0, 0);
  1932. }
  1933. static int append_hf_2(struct sip_msg *msg, char *str1, char *str2 )
  1934. {
  1935. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 0,
  1936. (gparam_p)str2);
  1937. }
  1938. static int insert_hf_1(struct sip_msg *msg, char *str1, char *str2 )
  1939. {
  1940. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 1, 0);
  1941. }
  1942. static int insert_hf_2(struct sip_msg *msg, char *str1, char *str2 )
  1943. {
  1944. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 1,
  1945. (gparam_p)str2);
  1946. }
  1947. static int append_urihf(struct sip_msg *msg, char *str1, char *str2)
  1948. {
  1949. return add_hf_helper(msg, (str*)str1, (str*)str2, 0, 0, 0);
  1950. }
  1951. static int is_method_f(struct sip_msg *msg, char *meth, char *str2 )
  1952. {
  1953. str *m;
  1954. m = (str*)meth;
  1955. if(msg->first_line.type==SIP_REQUEST)
  1956. {
  1957. if(m->s==0)
  1958. return (msg->first_line.u.request.method_value&m->len)?1:-1;
  1959. else
  1960. return (msg->first_line.u.request.method_value==METHOD_OTHER
  1961. && msg->first_line.u.request.method.len==m->len
  1962. && (strncasecmp(msg->first_line.u.request.method.s, m->s,
  1963. m->len)==0))?1:-1;
  1964. }
  1965. if(parse_headers(msg, HDR_CSEQ_F, 0)!=0 || msg->cseq==NULL)
  1966. {
  1967. LM_ERR("cannot parse cseq header\n");
  1968. return -1; /* should it be 0 ?!?! */
  1969. }
  1970. if(m->s==0)
  1971. return (get_cseq(msg)->method_id&m->len)?1:-1;
  1972. else
  1973. return (get_cseq(msg)->method_id==METHOD_OTHER
  1974. && get_cseq(msg)->method.len==m->len
  1975. && (strncasecmp(get_cseq(msg)->method.s, m->s,
  1976. m->len)==0))?1:-1;
  1977. }
  1978. /*
  1979. * Convert char* header_name to str* parameter
  1980. */
  1981. static int hname_fixup(void** param, int param_no)
  1982. {
  1983. char c;
  1984. struct hdr_field hdr;
  1985. gparam_p gp = NULL;
  1986. gp = (gparam_p)pkg_malloc(sizeof(gparam_t));
  1987. if(gp == NULL)
  1988. {
  1989. LM_ERR("no more memory\n");
  1990. return E_UNSPEC;
  1991. }
  1992. memset(gp, 0, sizeof(gparam_t));
  1993. gp->v.str.s = (char*)*param;
  1994. gp->v.str.len = strlen(gp->v.str.s);
  1995. if(gp->v.str.len==0)
  1996. {
  1997. LM_ERR("empty header name parameter\n");
  1998. pkg_free(gp);
  1999. return E_UNSPEC;
  2000. }
  2001. c = gp->v.str.s[gp->v.str.len];
  2002. gp->v.str.s[gp->v.str.len] = ':';
  2003. gp->v.str.len++;
  2004. if (parse_hname2_short(gp->v.str.s, gp->v.str.s + gp->v.str.len, &hdr)==0)
  2005. {
  2006. LM_ERR("error parsing header name\n");
  2007. pkg_free(gp);
  2008. return E_UNSPEC;
  2009. }
  2010. gp->v.str.len--;
  2011. gp->v.str.s[gp->v.str.len] = c;
  2012. if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T)
  2013. {
  2014. LM_DBG("using hdr type (%d) instead of <%.*s>\n",
  2015. hdr.type, gp->v.str.len, gp->v.str.s);
  2016. pkg_free(gp->v.str.s);
  2017. gp->v.str.s = NULL;
  2018. gp->v.i = hdr.type;
  2019. gp->type = GPARAM_TYPE_INT;
  2020. } else {
  2021. gp->type = GPARAM_TYPE_STR;
  2022. LM_DBG("using hdr type name <%.*s>\n", gp->v.str.len, gp->v.str.s);
  2023. }
  2024. *param = (void*)gp;
  2025. return 0;
  2026. }
  2027. static int free_hname_fixup(void** param, int param_no)
  2028. {
  2029. if(*param)
  2030. {
  2031. if(((gparam_p)(*param))->type==GPARAM_TYPE_STR)
  2032. pkg_free(((gparam_p)(*param))->v.str.s);
  2033. pkg_free(*param);
  2034. *param = 0;
  2035. }
  2036. return 0;
  2037. }
  2038. /*
  2039. * Convert char* method to str* parameter
  2040. */
  2041. static int fixup_method(void** param, int param_no)
  2042. {
  2043. str* s;
  2044. char *p;
  2045. int m;
  2046. unsigned int method;
  2047. s = (str*)pkg_malloc(sizeof(str));
  2048. if (!s) {
  2049. LM_ERR("no pkg memory left\n");
  2050. return E_UNSPEC;
  2051. }
  2052. s->s = (char*)*param;
  2053. s->len = strlen(s->s);
  2054. if(s->len==0)
  2055. {
  2056. LM_ERR("empty method name\n");
  2057. pkg_free(s);
  2058. return E_UNSPEC;
  2059. }
  2060. m=0;
  2061. p=s->s;
  2062. while(*p)
  2063. {
  2064. if(*p=='|')
  2065. {
  2066. *p = ',';
  2067. m=1;
  2068. }
  2069. p++;
  2070. }
  2071. if(parse_methods(s, &method)!=0)
  2072. {
  2073. LM_ERR("bad method names\n");
  2074. pkg_free(s);
  2075. return E_UNSPEC;
  2076. }
  2077. if(m==1)
  2078. {
  2079. if(method==METHOD_UNDEF || method&METHOD_OTHER)
  2080. {
  2081. LM_ERR("unknown method in list [%.*s/%d] - must be only defined methods\n",
  2082. s->len, s->s, method);
  2083. pkg_free(s);
  2084. return E_UNSPEC;
  2085. }
  2086. LM_DBG("using id for methods [%.*s/%d]\n",
  2087. s->len, s->s, method);
  2088. s->s = 0;
  2089. s->len = method;
  2090. } else {
  2091. if(method!=METHOD_UNDEF && method!=METHOD_OTHER)
  2092. {
  2093. LM_DBG("using id for method [%.*s/%d]\n",
  2094. s->len, s->s, method);
  2095. s->s = 0;
  2096. s->len = method;
  2097. } else
  2098. LM_DBG("name for method [%.*s/%d]\n",
  2099. s->len, s->s, method);
  2100. }
  2101. *param = (void*)s;
  2102. return 0;
  2103. }
  2104. /*
  2105. * Convert char* privacy value to corresponding bit value
  2106. */
  2107. int fixup_privacy(void** param, int param_no)
  2108. {
  2109. str p;
  2110. unsigned int val;
  2111. p.s = (char*)*param;
  2112. p.len = strlen(p.s);
  2113. if (p.len == 0) {
  2114. LM_ERR("empty privacy value\n");
  2115. return E_UNSPEC;
  2116. }
  2117. if (parse_priv_value(p.s, p.len, &val) != p.len) {
  2118. LM_ERR("invalid privacy value\n");
  2119. return E_UNSPEC;
  2120. }
  2121. *param = (void *)(long)val;
  2122. return 0;
  2123. }
  2124. /*
  2125. * Fix in_list params: subject and list (strings that may contain pvars),
  2126. * separator (string)
  2127. */
  2128. static int fixup_in_list(void** param, int param_no)
  2129. {
  2130. if ((param_no == 1) || (param_no == 2)) return fixup_spve_null(param, 1);
  2131. if (param_no == 3) {
  2132. if ((strlen((char *)*param) != 1) || (*((char *)(*param)) == 0)) {
  2133. LM_ERR("invalid separator parameter\n");
  2134. return -1;
  2135. }
  2136. return 0;
  2137. }
  2138. LM_ERR("invalid parameter number <%d>\n", param_no);
  2139. return -1;
  2140. }
  2141. /*
  2142. * Free in_list params
  2143. */
  2144. static int fixup_free_in_list(void** param, int param_no)
  2145. {
  2146. if ((param_no == 1) || (param_no == 2)) {
  2147. return fixup_free_spve_null(param, 1);
  2148. }
  2149. if (param_no == 3) return 0;
  2150. LM_ERR("invalid parameter number <%d>\n", param_no);
  2151. return -1;
  2152. }
  2153. static int add_header_fixup(void** param, int param_no)
  2154. {
  2155. if(param_no==1)
  2156. {
  2157. return fixup_spve_null(param, param_no);
  2158. } else if(param_no==2) {
  2159. return hname_fixup(param, param_no);
  2160. } else {
  2161. LM_ERR("wrong number of parameters\n");
  2162. return E_UNSPEC;
  2163. }
  2164. }
  2165. static int fixup_body_type(void** param, int param_no)
  2166. {
  2167. char *p;
  2168. char *r;
  2169. unsigned int type;
  2170. if(param_no==1) {
  2171. p = (char*)*param;
  2172. if (p==0 || p[0]==0) {
  2173. type = 0;
  2174. } else {
  2175. r = decode_mime_type( p, p+strlen(p) , &type);
  2176. if (r==0) {
  2177. LM_ERR("unsupported mime <%s>\n",p);
  2178. return E_CFG;
  2179. }
  2180. if ( r!=p+strlen(p) ) {
  2181. LM_ERR("multiple mimes not supported!\n");
  2182. return E_CFG;
  2183. }
  2184. }
  2185. pkg_free(*param);
  2186. *param = (void*)(long)type;
  2187. }
  2188. return 0;
  2189. }
  2190. static int has_body_f(struct sip_msg *msg, char *type, char *str2 )
  2191. {
  2192. int mime;
  2193. /* parse content len hdr */
  2194. if ( msg->content_length==NULL &&
  2195. (parse_headers(msg,HDR_CONTENTLENGTH_F, 0)==-1||msg->content_length==NULL))
  2196. return -1;
  2197. if (get_content_length (msg)==0) {
  2198. LM_DBG("content length is zero\n");
  2199. /* Nothing to see here, please move on. */
  2200. return -1;
  2201. }
  2202. /* check type also? */
  2203. if (type==0)
  2204. return 1;
  2205. /* the function search for and parses the Content-Type hdr */
  2206. mime = parse_content_type_hdr (msg);
  2207. if (mime<0) {
  2208. LM_ERR("failed to extract content type hdr\n");
  2209. return -1;
  2210. }
  2211. if (mime==0) {
  2212. /* content type hdr not found -> according the RFC3261 we
  2213. * assume APPLICATION/SDP --bogdan */
  2214. mime = ((TYPE_APPLICATION << 16) + SUBTYPE_SDP);
  2215. }
  2216. LM_DBG("content type is %d\n",mime);
  2217. if ( (unsigned int)mime!=(unsigned int)(unsigned long)type )
  2218. return -1;
  2219. return 1;
  2220. }
  2221. int is_privacy_f(struct sip_msg *msg, char *_privacy, char *str2 )
  2222. {
  2223. if (parse_privacy(msg) == -1)
  2224. return -1;
  2225. return get_privacy_values(msg) & ((unsigned int)(long)_privacy) ? 1 : -1;
  2226. }
  2227. /*
  2228. * Checks if subject is found in list
  2229. */
  2230. int in_list_f(struct sip_msg* _m, char* _subject, char* _list, char* _sep)
  2231. {
  2232. str subject, list;
  2233. int sep;
  2234. char *at, *past, *next_sep, *s;
  2235. if (fixup_get_svalue(_m, (gparam_p)_subject, &subject) != 0) {
  2236. LM_ERR("cannot get subject value\n");
  2237. return -1;
  2238. } else {
  2239. if (subject.len == 0) {
  2240. LM_ERR("subject cannot be empty string\n");
  2241. return -1;
  2242. }
  2243. }
  2244. if (fixup_get_svalue(_m, (gparam_p)_list, &list) != 0) {
  2245. LM_ERR("cannot get list value\n");
  2246. return -1;
  2247. } else {
  2248. if (list.len == 0) return -1;
  2249. }
  2250. sep = _sep[0];
  2251. at = list.s;
  2252. past = list.s + list.len;
  2253. /* Eat leading white space */
  2254. while ((at < past) &&
  2255. ((*at == ' ') || (*at == '\t') || (*at == '\r') || (*at == '\n') )) {
  2256. at++;
  2257. }
  2258. while (at < past) {
  2259. next_sep = index(at, sep);
  2260. s = next_sep;
  2261. if (s == NULL) {
  2262. /* Eat trailing white space */
  2263. while ((at < past) &&
  2264. ((*(past-1) == ' ') || (*(past-1) == '\t') || (*(past-1) == '\r') || (*(past-1) == '\n') )) {
  2265. past--;
  2266. }
  2267. if ((subject.len == (past - at)) &&
  2268. strncmp(at, subject.s, subject.len) == 0) {
  2269. return 1;
  2270. } else {
  2271. return -1;
  2272. }
  2273. } else {
  2274. /* Eat trailing white space */
  2275. while ((at < s) &&
  2276. ((*(s-1) == ' ') || (*(s-1) == '\t') || (*(s-1) == '\r') || (*(s-1) == '\n') )) {
  2277. s--;
  2278. }
  2279. if ((subject.len == (s - at)) &&
  2280. strncmp(at, subject.s, subject.len) == 0) {
  2281. return 1;
  2282. } else {
  2283. at = next_sep + 1;
  2284. /* Eat leading white space */
  2285. while ((at < past) &&
  2286. ((*at == ' ') || (*at == '\t') || (*at == '\r') || (*at == '\n') )) {
  2287. at++;
  2288. }
  2289. }
  2290. }
  2291. }
  2292. return -1;
  2293. }
  2294. static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 )
  2295. {
  2296. str s1;
  2297. str s2;
  2298. int ret;
  2299. if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
  2300. {
  2301. LM_ERR("cannot get first parameter\n");
  2302. return -8;
  2303. }
  2304. if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
  2305. {
  2306. LM_ERR("cannot get second parameter\n");
  2307. return -8;
  2308. }
  2309. ret = cmp_str(&s1, &s2);
  2310. if(ret==0)
  2311. return 1;
  2312. if(ret>0)
  2313. return -1;
  2314. return -2;
  2315. }
  2316. static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2)
  2317. {
  2318. str s1;
  2319. str s2;
  2320. int ret;
  2321. if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
  2322. {
  2323. LM_ERR("cannot get first parameter\n");
  2324. return -8;
  2325. }
  2326. if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
  2327. {
  2328. LM_ERR("cannot get second parameter\n");
  2329. return -8;
  2330. }
  2331. ret = cmpi_str(&s1, &s2);
  2332. if(ret==0)
  2333. return 1;
  2334. if(ret>0)
  2335. return -1;
  2336. return -2;
  2337. }
  2338. static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 )
  2339. {
  2340. str s1;
  2341. str s2;
  2342. int ret;
  2343. if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
  2344. {
  2345. LM_ERR("cannot get first parameter\n");
  2346. return -8;
  2347. }
  2348. if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
  2349. {
  2350. LM_ERR("cannot get second parameter\n");
  2351. return -8;
  2352. }
  2353. if (s1.len < s2.len) return -1;
  2354. ret = strncmp(s1.s, s2.s, s2.len);
  2355. if(ret==0)
  2356. return 1;
  2357. if(ret>0)
  2358. return -1;
  2359. return -2;
  2360. }
  2361. static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 )
  2362. {
  2363. int sdp_session_num = 0, sdp_stream_num;
  2364. sdp_session_cell_t* sdp_session;
  2365. sdp_stream_cell_t* sdp_stream;
  2366. if (0 == parse_sdp(msg)) {
  2367. for(;;) {
  2368. sdp_session = get_sdp_session(msg, sdp_session_num);
  2369. if(!sdp_session) break;
  2370. sdp_stream_num = 0;
  2371. for(;;) {
  2372. sdp_stream = get_sdp_stream(msg, sdp_session_num, sdp_stream_num);
  2373. if(!sdp_stream) break;
  2374. if(sdp_stream->media.len==AUDIO_STR_LEN &&
  2375. strncmp(sdp_stream->media.s,AUDIO_STR,AUDIO_STR_LEN)==0 &&
  2376. sdp_stream->is_on_hold)
  2377. return 1;
  2378. sdp_stream_num++;
  2379. }
  2380. sdp_session_num++;
  2381. }
  2382. }
  2383. return -1;
  2384. }
  2385. int fixup_regexpNL_none(void** param, int param_no)
  2386. {
  2387. regex_t* re;
  2388. if (param_no != 1 && param_no != 2 )
  2389. {
  2390. LM_ERR("invalid parameter number %d\n", param_no);
  2391. return E_UNSPEC;
  2392. }
  2393. if (param_no == 2)
  2394. return 0;
  2395. /* param 1 */
  2396. if ((re=pkg_malloc(sizeof(regex_t)))==0) {
  2397. LM_ERR("no more pkg memory\n");
  2398. return E_OUT_OF_MEM;
  2399. }
  2400. if (regcomp(re, *param, REG_EXTENDED|REG_ICASE)) {
  2401. pkg_free(re);
  2402. LM_ERR("bad re %s\n", (char*)*param);
  2403. return E_BAD_RE;
  2404. }
  2405. /* free string */
  2406. pkg_free(*param);
  2407. /* replace it with the compiled re */
  2408. *param=re;
  2409. return 0;
  2410. }
  2411. /*! \brief
  2412. * fixup for functions that get two parameters
  2413. * - first parameter is converted to regular expression structure
  2414. * - second parameter is not converted
  2415. */
  2416. int fixup_regexp_none(void** param, int param_no)
  2417. {
  2418. if (param_no != 1 && param_no != 2 )
  2419. {
  2420. LM_ERR("invalid parameter number %d\n", param_no);
  2421. return E_UNSPEC;
  2422. }
  2423. if (param_no == 1)
  2424. return fixup_regexp_null(param, 1);
  2425. return 0;
  2426. }
  2427. /**
  2428. * fixup free for functions that get two parameters
  2429. * - first parameter was converted to regular expression
  2430. * - second parameter was notconverted
  2431. */
  2432. int fixup_free_regexp_none(void** param, int param_no)
  2433. {
  2434. if (param_no != 1 && param_no != 2 )
  2435. {
  2436. LM_ERR("invalid parameter number %d\n", param_no);
  2437. return E_UNSPEC;
  2438. }
  2439. if (param_no == 1)
  2440. return fixup_free_regexp_null(param, 1);
  2441. return 0;
  2442. }
  2443. /**
  2444. *
  2445. */
  2446. static int search_hf_f(struct sip_msg* msg, char* str_hf, char* re, char *flags)
  2447. {
  2448. hdr_field_t *hf;
  2449. hdr_field_t *hfl = NULL;
  2450. str body;
  2451. gparam_t *gp;
  2452. regmatch_t pmatch;
  2453. char c;
  2454. int ret;
  2455. gp = (gparam_t*)str_hf;
  2456. /* we need to be sure we have seen all HFs */
  2457. parse_headers(msg, HDR_EOH_F, 0);
  2458. for (hf=msg->headers; hf; hf=hf->next) {
  2459. if(gp->type==GPARAM_TYPE_INT)
  2460. {
  2461. if (gp->v.i!=hf->type)
  2462. continue;
  2463. } else {
  2464. if (hf->name.len!=gp->v.str.len)
  2465. continue;
  2466. if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
  2467. continue;
  2468. }
  2469. if(flags==NULL || *flags!='l')
  2470. {
  2471. body = hf->body;
  2472. c = body.s[body.len];
  2473. body.s[body.len] = '\0';
  2474. ret = regexec((regex_t*) re, body.s, 1, &pmatch, 0);
  2475. body.s[body.len] = c;
  2476. if(ret==0)
  2477. {
  2478. /* match */
  2479. if(flags==NULL || *flags!='l')
  2480. return 1;
  2481. } else {
  2482. if(flags!=NULL && *flags=='f')
  2483. return -1;
  2484. }
  2485. } else {
  2486. hfl = hf;
  2487. }
  2488. }
  2489. if(hfl!=NULL)
  2490. {
  2491. hf = hfl;
  2492. body = hf->body;
  2493. c = body.s[body.len];
  2494. body.s[body.len] = '\0';
  2495. ret = regexec((regex_t*) re, body.s, 1, &pmatch, 0);
  2496. body.s[body.len] = c;
  2497. if(ret==0)
  2498. return 1;
  2499. }
  2500. return -1;
  2501. }
  2502. /*
  2503. * Convert header name, regexp and flags
  2504. */
  2505. static int fixup_search_hf(void** param, int param_no)
  2506. {
  2507. if(param_no==1)
  2508. return hname_fixup(param, param_no);
  2509. if(param_no==2)
  2510. return fixup_regexp_null(param, 1);
  2511. return 0;
  2512. }
  2513. /* sed-perl style re: s/regular expression/replacement/flags */
  2514. static int subst_hf_f(struct sip_msg *msg, char *str_hf, char *subst, char *flags)
  2515. {
  2516. struct lump* l;
  2517. struct replace_lst* lst = NULL;
  2518. struct replace_lst* rpl = NULL;
  2519. char* begin;
  2520. struct subst_expr* se;
  2521. int off;
  2522. int nmatches=0;
  2523. str body;
  2524. hdr_field_t *hf;
  2525. hdr_field_t *hfl = NULL;
  2526. gparam_t *gp;
  2527. char c;
  2528. int ret;
  2529. ret = -1;
  2530. gp = (gparam_t*)str_hf;
  2531. se=(struct subst_expr*)subst;
  2532. /* we need to be sure we have seen all HFs */
  2533. parse_headers(msg, HDR_EOH_F, 0);
  2534. for (hf=msg->headers; hf; hf=hf->next) {
  2535. if(gp->type==GPARAM_TYPE_INT)
  2536. {
  2537. if (gp->v.i!=hf->type)
  2538. continue;
  2539. } else {
  2540. if (hf->name.len!=gp->v.str.len)
  2541. continue;
  2542. if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
  2543. continue;
  2544. }
  2545. if(flags==NULL || *flags!='l')
  2546. {
  2547. body = hf->body;
  2548. c = body.s[body.len];
  2549. body.s[body.len] = '\0';
  2550. begin=body.s;
  2551. off=begin-msg->buf;
  2552. lst=subst_run(se, begin, msg, &nmatches);
  2553. body.s[body.len] = c;
  2554. if(lst==0 && flags!=NULL && *flags=='f')
  2555. goto error; /* not found */
  2556. if(lst!=0)
  2557. ret=1;
  2558. for (rpl=lst; rpl; rpl=rpl->next)
  2559. {
  2560. LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
  2561. exports.name, rpl->offset+off,
  2562. rpl->size, rpl->offset+off+msg->buf,
  2563. rpl->rpl.len, rpl->rpl.s);
  2564. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  2565. {
  2566. ret=-1;
  2567. goto error;
  2568. }
  2569. /* hack to avoid re-copying rpl, possible because both
  2570. * replace_lst & lumps use pkg_malloc */
  2571. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0)
  2572. {
  2573. LM_ERR("%s could not insert new lump\n",
  2574. exports.name);
  2575. ret=-1;
  2576. goto error;
  2577. }
  2578. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  2579. * not free it */
  2580. rpl->rpl.s=0;
  2581. rpl->rpl.len=0;
  2582. }
  2583. } else {
  2584. hfl = hf;
  2585. }
  2586. /* if flags set for first header, then all done */
  2587. if(flags!=NULL && *flags=='f')
  2588. return ret;
  2589. }
  2590. if(hfl!=NULL)
  2591. {
  2592. hf= hfl;
  2593. body = hf->body;
  2594. c = body.s[body.len];
  2595. body.s[body.len] = '\0';
  2596. begin=body.s;
  2597. off=begin-msg->buf;
  2598. lst=subst_run(se, begin, msg, &nmatches);
  2599. body.s[body.len] = c;
  2600. if(lst==0)
  2601. goto error; /* not found */
  2602. ret=1;
  2603. for (rpl=lst; rpl; rpl=rpl->next)
  2604. {
  2605. LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
  2606. exports.name, rpl->offset+off,
  2607. rpl->size, rpl->offset+off+msg->buf,
  2608. rpl->rpl.len, rpl->rpl.s);
  2609. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  2610. {
  2611. ret=-1;
  2612. goto error;
  2613. }
  2614. /* hack to avoid re-copying rpl, possible because both
  2615. * replace_lst & lumps use pkg_malloc */
  2616. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0)
  2617. {
  2618. LM_ERR("%s could not insert new lump\n",
  2619. exports.name);
  2620. ret=-1;
  2621. goto error;
  2622. }
  2623. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  2624. * not free it */
  2625. rpl->rpl.s=0;
  2626. rpl->rpl.len=0;
  2627. }
  2628. }
  2629. error:
  2630. LM_DBG("lst was %p\n", lst);
  2631. if (lst) replace_lst_free(lst);
  2632. if (nmatches<0)
  2633. LM_ERR("%s subst_run failed\n", exports.name);
  2634. return ret;
  2635. }
  2636. /*
  2637. * Convert header name, substexp and flags
  2638. */
  2639. static int fixup_subst_hf(void** param, int param_no)
  2640. {
  2641. if(param_no==1)
  2642. return hname_fixup(param, param_no);
  2643. if(param_no==2)
  2644. return fixup_substre(param, 1);
  2645. return 0;
  2646. }