textops.c 68 KB

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