textops.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "../../lib/kcore/parse_privacy.h"
  67. #include "../../msg_translator.h"
  68. #include "../../ut.h"
  69. #include "../../dset.h"
  70. #include "../../lib/kcore/cmpapi.h"
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <sys/types.h> /* for regex */
  75. #include <regex.h>
  76. #include <time.h>
  77. #include <sys/time.h>
  78. #include "textops.h"
  79. #include "txt_var.h"
  80. #include "api.h"
  81. MODULE_VERSION
  82. /* RFC822-conforming dates format:
  83. %a -- abbreviated week of day name (locale), %d day of month
  84. as decimal number, %b abbreviated month name (locale), %Y
  85. year with century, %T time in 24h notation
  86. */
  87. #define TIME_FORMAT "Date: %a, %d %b %Y %H:%M:%S GMT"
  88. #define MAX_TIME 64
  89. #define AUDIO_STR "audio"
  90. #define AUDIO_STR_LEN 5
  91. static int search_body_f(struct sip_msg*, char*, char*);
  92. static int search_hf_f(struct sip_msg*, char*, char*, char*);
  93. static int replace_f(struct sip_msg*, char*, char*);
  94. static int replace_body_f(struct sip_msg*, char*, char*);
  95. static int replace_all_f(struct sip_msg*, char*, char*);
  96. static int replace_body_all_f(struct sip_msg*, char*, char*);
  97. static int replace_body_atonce_f(struct sip_msg*, char*, char*);
  98. static int subst_f(struct sip_msg*, char*, char*);
  99. static int subst_uri_f(struct sip_msg*, char*, char*);
  100. static int subst_user_f(struct sip_msg*, char*, char*);
  101. static int subst_body_f(struct sip_msg*, char*, char*);
  102. static int subst_hf_f(struct sip_msg*, char*, char*, char*);
  103. static int filter_body_f(struct sip_msg*, char*, char*);
  104. static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
  105. static int search_append_body_f(struct sip_msg*, char*, char*);
  106. static int append_to_reply_f(struct sip_msg* msg, char* key, char* str);
  107. static int append_hf_1(struct sip_msg* msg, char* str1, char* str2);
  108. static int append_hf_2(struct sip_msg* msg, char* str1, char* str2);
  109. static int insert_hf_1(struct sip_msg* msg, char* str1, char* str2);
  110. static int insert_hf_2(struct sip_msg* msg, char* str1, char* str2);
  111. static int append_urihf(struct sip_msg* msg, char* str1, char* str2);
  112. static int append_time_f(struct sip_msg* msg, char* , char *);
  113. static int append_time_request_f(struct sip_msg* msg, char* , char *);
  114. static int set_body_f(struct sip_msg* msg, char*, char *);
  115. static int set_rpl_body_f(struct sip_msg* msg, char*, char *);
  116. static int is_method_f(struct sip_msg* msg, char* , char *);
  117. static int has_body_f(struct sip_msg *msg, char *type, char *str2 );
  118. static int in_list_f(struct sip_msg* _msg, char* _subject, char* _list,
  119. char* _sep);
  120. static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 );
  121. static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2 );
  122. static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 );
  123. static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
  124. static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
  125. static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 );
  126. static int fixup_substre(void**, int);
  127. static int hname_fixup(void** param, int param_no);
  128. static int free_hname_fixup(void** param, int param_no);
  129. static int fixup_method(void** param, int param_no);
  130. static int add_header_fixup(void** param, int param_no);
  131. static int fixup_body_type(void** param, int param_no);
  132. static int fixup_in_list(void** param, int param_no);
  133. static int fixup_free_in_list(void** param, int param_no);
  134. int fixup_regexpNL_none(void** param, int param_no);
  135. static int fixup_search_hf(void** param, int param_no);
  136. static int fixup_subst_hf(void** param, int param_no);
  137. static int mod_init(void);
  138. static tr_export_t mod_trans[] = {
  139. { {"re", sizeof("re")-1}, /* regexp class */
  140. tr_txt_parse_re },
  141. { { 0, 0 }, 0 }
  142. };
  143. static cmd_export_t cmds[]={
  144. {"search", (cmd_function)search_f, 1,
  145. fixup_regexp_null, fixup_free_regexp_null,
  146. ANY_ROUTE},
  147. {"search_body", (cmd_function)search_body_f, 1,
  148. fixup_regexp_null, fixup_free_regexp_null,
  149. ANY_ROUTE},
  150. {"search_hf", (cmd_function)search_hf_f, 3,
  151. fixup_search_hf, 0,
  152. ANY_ROUTE},
  153. {"search_append", (cmd_function)search_append_f, 2,
  154. fixup_regexp_none,fixup_free_regexp_none,
  155. ANY_ROUTE},
  156. {"search_append_body", (cmd_function)search_append_body_f, 2,
  157. fixup_regexp_none, fixup_free_regexp_none,
  158. ANY_ROUTE},
  159. {"replace", (cmd_function)replace_f, 2,
  160. fixup_regexp_none, fixup_free_regexp_none,
  161. ANY_ROUTE},
  162. {"replace_body", (cmd_function)replace_body_f, 2,
  163. fixup_regexp_none, fixup_free_regexp_none,
  164. ANY_ROUTE},
  165. {"replace_all", (cmd_function)replace_all_f, 2,
  166. fixup_regexp_none, fixup_free_regexp_none,
  167. ANY_ROUTE},
  168. {"replace_body_all", (cmd_function)replace_body_all_f,2,
  169. fixup_regexp_none, fixup_free_regexp_none,
  170. ANY_ROUTE},
  171. {"replace_body_atonce", (cmd_function)replace_body_atonce_f,2,
  172. fixup_regexpNL_none, fixup_free_regexp_none,
  173. ANY_ROUTE},
  174. {"append_to_reply", (cmd_function)append_to_reply_f, 1,
  175. fixup_spve_null, 0,
  176. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  177. {"append_hf", (cmd_function)append_hf_1, 1,
  178. add_header_fixup, 0,
  179. ANY_ROUTE},
  180. {"append_hf", (cmd_function)append_hf_2, 2,
  181. add_header_fixup, 0,
  182. ANY_ROUTE},
  183. {"insert_hf", (cmd_function)insert_hf_1, 1,
  184. add_header_fixup, 0,
  185. ANY_ROUTE},
  186. {"insert_hf", (cmd_function)insert_hf_2, 2,
  187. add_header_fixup, 0,
  188. ANY_ROUTE},
  189. {"append_urihf", (cmd_function)append_urihf, 2,
  190. fixup_str_str, fixup_free_str_str,
  191. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  192. {"remove_hf", (cmd_function)remove_hf_f, 1,
  193. hname_fixup, free_hname_fixup,
  194. ANY_ROUTE},
  195. {"remove_hf_re", (cmd_function)remove_hf_re_f, 1,
  196. fixup_regexp_null, fixup_free_regexp_null,
  197. ANY_ROUTE},
  198. {"is_present_hf", (cmd_function)is_present_hf_f, 1,
  199. hname_fixup, free_hname_fixup,
  200. ANY_ROUTE},
  201. {"is_present_hf_re", (cmd_function)is_present_hf_re_f,1,
  202. fixup_regexp_null, fixup_free_regexp_null,
  203. ANY_ROUTE},
  204. {"subst", (cmd_function)subst_f, 1,
  205. fixup_substre, 0,
  206. ANY_ROUTE},
  207. {"subst_uri", (cmd_function)subst_uri_f, 1,
  208. fixup_substre, 0,
  209. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  210. {"subst_user", (cmd_function)subst_user_f, 1,
  211. fixup_substre, 0,
  212. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  213. {"subst_body", (cmd_function)subst_body_f, 1,
  214. fixup_substre, 0,
  215. ANY_ROUTE},
  216. {"subst_hf", (cmd_function)subst_hf_f, 3,
  217. fixup_subst_hf, 0,
  218. ANY_ROUTE},
  219. {"filter_body", (cmd_function)filter_body_f, 1,
  220. fixup_str_null, 0,
  221. ANY_ROUTE},
  222. {"append_time", (cmd_function)append_time_f, 0,
  223. 0, 0,
  224. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  225. {"set_body", (cmd_function)set_body_f, 2,
  226. fixup_spve_spve, 0,
  227. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE },
  228. {"set_reply_body", (cmd_function)set_rpl_body_f, 2,
  229. fixup_spve_spve, 0,
  230. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
  231. {"is_method", (cmd_function)is_method_f, 1,
  232. fixup_method, 0,
  233. ANY_ROUTE},
  234. {"has_body", (cmd_function)has_body_f, 0,
  235. 0, 0,
  236. ANY_ROUTE},
  237. {"has_body", (cmd_function)has_body_f, 1,
  238. fixup_body_type, 0,
  239. ANY_ROUTE},
  240. {"is_privacy", (cmd_function)is_privacy_f, 1,
  241. fixup_privacy, 0,
  242. ANY_ROUTE},
  243. {"in_list", (cmd_function)in_list_f, 3, fixup_in_list,
  244. fixup_free_in_list,
  245. ANY_ROUTE},
  246. {"cmp_str", (cmd_function)cmp_str_f, 2,
  247. fixup_spve_spve, 0,
  248. ANY_ROUTE},
  249. {"cmp_istr", (cmd_function)cmp_istr_f, 2,
  250. fixup_spve_spve, 0,
  251. ANY_ROUTE},
  252. {"starts_with", (cmd_function)starts_with_f, 2,
  253. fixup_spve_spve, 0,
  254. ANY_ROUTE},
  255. {"is_audio_on_hold", (cmd_function)is_audio_on_hold_f, 0,
  256. 0, 0,
  257. ANY_ROUTE},
  258. {"append_time_to_request", (cmd_function)append_time_request_f, 0,
  259. 0, 0,
  260. ANY_ROUTE},
  261. {"bind_textops", (cmd_function)bind_textops, 0, 0, 0,
  262. 0},
  263. {0,0,0,0,0,0}
  264. };
  265. struct module_exports exports= {
  266. "textops", /* module name*/
  267. DEFAULT_DLFLAGS, /* dlopen flags */
  268. cmds, /* exported functions */
  269. 0, /* module parameters */
  270. 0, /* exported statistics */
  271. 0, /* exported MI functions */
  272. 0, /* exported pseudo-variables */
  273. 0, /* extra processes */
  274. mod_init, /* module initialization function */
  275. 0, /* response function */
  276. 0, /* destroy function */
  277. 0, /* per-child init function */
  278. };
  279. static int mod_init(void)
  280. {
  281. return 0;
  282. }
  283. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  284. {
  285. return register_trans_mod(path, mod_trans);
  286. }
  287. static char *get_header(struct sip_msg *msg)
  288. {
  289. return msg->buf+msg->first_line.len;
  290. }
  291. int search_f(struct sip_msg* msg, char* key, char* str2)
  292. {
  293. /*we registered only 1 param, so we ignore str2*/
  294. regmatch_t pmatch;
  295. if (regexec((regex_t*) key, msg->buf, 1, &pmatch, 0)!=0) return -1;
  296. return 1;
  297. }
  298. static int search_body_f(struct sip_msg* msg, char* key, char* str2)
  299. {
  300. str body;
  301. /*we registered only 1 param, so we ignore str2*/
  302. regmatch_t pmatch;
  303. body.s = get_body(msg);
  304. if (body.s==0) {
  305. LM_ERR("failed to get the message body\n");
  306. return -1;
  307. }
  308. body.len = msg->len -(int)(body.s-msg->buf);
  309. if (body.len==0) {
  310. LM_DBG("message body has zero length\n");
  311. return -1;
  312. }
  313. if (regexec((regex_t*) key, body.s, 1, &pmatch, 0)!=0) return -1;
  314. return 1;
  315. }
  316. int search_append_f(struct sip_msg* msg, char* key, char* str2)
  317. {
  318. struct lump* l;
  319. regmatch_t pmatch;
  320. char* s;
  321. int len;
  322. char *begin;
  323. int off;
  324. begin=get_header(msg); /* msg->orig/buf previously .. uri problems */
  325. off=begin-msg->buf;
  326. if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
  327. if (pmatch.rm_so!=-1){
  328. if ((l=anchor_lump(msg, off+pmatch.rm_eo, 0, 0))==0)
  329. return -1;
  330. len=strlen(str2);
  331. s=pkg_malloc(len);
  332. if (s==0){
  333. LM_ERR("memory allocation failure\n");
  334. return -1;
  335. }
  336. memcpy(s, str2, len);
  337. if (insert_new_lump_after(l, s, len, 0)==0){
  338. LM_ERR("could not insert new lump\n");
  339. pkg_free(s);
  340. return -1;
  341. }
  342. return 1;
  343. }
  344. return -1;
  345. }
  346. static int search_append_body_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. int off;
  353. str body;
  354. body.s = get_body(msg);
  355. if (body.s==0) {
  356. LM_ERR("failed to get the message body\n");
  357. return -1;
  358. }
  359. body.len = msg->len -(int)(body.s-msg->buf);
  360. if (body.len==0) {
  361. LM_DBG("message body has zero length\n");
  362. return -1;
  363. }
  364. off=body.s-msg->buf;
  365. if (regexec((regex_t*) key, body.s, 1, &pmatch, 0)!=0) return -1;
  366. if (pmatch.rm_so!=-1){
  367. if ((l=anchor_lump(msg, off+pmatch.rm_eo, 0, 0))==0)
  368. return -1;
  369. len=strlen(str2);
  370. s=pkg_malloc(len);
  371. if (s==0){
  372. LM_ERR("memory allocation failure\n");
  373. return -1;
  374. }
  375. memcpy(s, str2, len);
  376. if (insert_new_lump_after(l, s, len, 0)==0){
  377. LM_ERR("could not insert new lump\n");
  378. pkg_free(s);
  379. return -1;
  380. }
  381. return 1;
  382. }
  383. return -1;
  384. }
  385. static int replace_all_f(struct sip_msg* msg, char* key, char* str2)
  386. {
  387. struct lump* l;
  388. regmatch_t pmatch;
  389. char* s;
  390. int len;
  391. char* begin;
  392. int off;
  393. int ret;
  394. int eflags;
  395. begin = get_header(msg);
  396. ret=-1; /* pessimist: we will not find any */
  397. len=strlen(str2);
  398. eflags=0; /* match ^ at the beginning of the string*/
  399. while (begin<msg->buf+msg->len
  400. && regexec((regex_t*) key, begin, 1, &pmatch, eflags)==0) {
  401. off=begin-msg->buf;
  402. if (pmatch.rm_so==-1){
  403. LM_ERR("offset unknown\n");
  404. return -1;
  405. }
  406. if (pmatch.rm_so==pmatch.rm_eo){
  407. LM_ERR("matched string is empty... invalid regexp?\n");
  408. return -1;
  409. }
  410. if ((l=del_lump(msg, pmatch.rm_so+off,
  411. pmatch.rm_eo-pmatch.rm_so, 0))==0) {
  412. LM_ERR("del_lump failed\n");
  413. return -1;
  414. }
  415. s=pkg_malloc(len);
  416. if (s==0){
  417. LM_ERR("memory allocation failure\n");
  418. return -1;
  419. }
  420. memcpy(s, str2, len);
  421. if (insert_new_lump_after(l, s, len, 0)==0){
  422. LM_ERR("could not insert new lump\n");
  423. pkg_free(s);
  424. return -1;
  425. }
  426. /* new cycle */
  427. begin=begin+pmatch.rm_eo;
  428. /* is it still a string start */
  429. if (*(begin-1)=='\n' || *(begin-1)=='\r')
  430. eflags&=~REG_NOTBOL;
  431. else
  432. eflags|=REG_NOTBOL;
  433. ret=1;
  434. } /* while found ... */
  435. return ret;
  436. }
  437. static int do_replace_body_f(struct sip_msg* msg, char* key, char* str2, int nobol)
  438. {
  439. struct lump* l;
  440. regmatch_t pmatch;
  441. char* s;
  442. int len;
  443. char* begin;
  444. int off;
  445. int ret;
  446. int eflags;
  447. str body;
  448. body.s = get_body(msg);
  449. if (body.s==0) {
  450. LM_ERR("failed to get the message body\n");
  451. return -1;
  452. }
  453. body.len = msg->len -(int)(body.s-msg->buf);
  454. if (body.len==0) {
  455. LM_DBG("message body has zero length\n");
  456. return -1;
  457. }
  458. begin=body.s;
  459. ret=-1; /* pessimist: we will not find any */
  460. len=strlen(str2);
  461. eflags=0; /* match ^ at the beginning of the string*/
  462. while (begin<msg->buf+msg->len
  463. && regexec((regex_t*) key, begin, 1, &pmatch, eflags)==0) {
  464. off=begin-msg->buf;
  465. if (pmatch.rm_so==-1){
  466. LM_ERR("offset unknown\n");
  467. return -1;
  468. }
  469. if (pmatch.rm_so==pmatch.rm_eo){
  470. LM_ERR("matched string is empty... invalid regexp?\n");
  471. return -1;
  472. }
  473. if ((l=del_lump(msg, pmatch.rm_so+off,
  474. pmatch.rm_eo-pmatch.rm_so, 0))==0) {
  475. LM_ERR("del_lump failed\n");
  476. return -1;
  477. }
  478. s=pkg_malloc(len);
  479. if (s==0){
  480. LM_ERR("memory allocation failure\n");
  481. return -1;
  482. }
  483. memcpy(s, str2, len);
  484. if (insert_new_lump_after(l, s, len, 0)==0){
  485. LM_ERR("could not insert new lump\n");
  486. pkg_free(s);
  487. return -1;
  488. }
  489. /* new cycle */
  490. begin=begin+pmatch.rm_eo;
  491. /* is it still a string start */
  492. if (nobol && (*(begin-1)=='\n' || *(begin-1)=='\r'))
  493. eflags&=~REG_NOTBOL;
  494. else
  495. eflags|=REG_NOTBOL;
  496. ret=1;
  497. } /* while found ... */
  498. return ret;
  499. }
  500. static int replace_body_all_f(struct sip_msg* msg, char* key, char* str2)
  501. {
  502. return do_replace_body_f(msg, key, str2, 1);
  503. }
  504. static int replace_body_atonce_f(struct sip_msg* msg, char* key, char* str2)
  505. {
  506. return do_replace_body_f(msg, key, str2, 0);
  507. }
  508. static int replace_f(struct sip_msg* msg, char* key, char* str2)
  509. {
  510. struct lump* l;
  511. regmatch_t pmatch;
  512. char* s;
  513. int len;
  514. char* begin;
  515. int off;
  516. begin=get_header(msg); /* msg->orig previously .. uri problems */
  517. if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
  518. off=begin-msg->buf;
  519. if (pmatch.rm_so!=-1){
  520. if ((l=del_lump(msg, pmatch.rm_so+off,
  521. pmatch.rm_eo-pmatch.rm_so, 0))==0)
  522. return -1;
  523. len=strlen(str2);
  524. s=pkg_malloc(len);
  525. if (s==0){
  526. LM_ERR("memory allocation failure\n");
  527. return -1;
  528. }
  529. memcpy(s, str2, len);
  530. if (insert_new_lump_after(l, s, len, 0)==0){
  531. LM_ERR("could not insert new lump\n");
  532. pkg_free(s);
  533. return -1;
  534. }
  535. return 1;
  536. }
  537. return -1;
  538. }
  539. static int replace_body_f(struct sip_msg* msg, char* key, char* str2)
  540. {
  541. struct lump* l;
  542. regmatch_t pmatch;
  543. char* s;
  544. int len;
  545. char* begin;
  546. int off;
  547. str body;
  548. body.s = get_body(msg);
  549. if (body.s==0) {
  550. LM_ERR("failed to get the message body\n");
  551. return -1;
  552. }
  553. body.len = msg->len -(int)(body.s-msg->buf);
  554. if (body.len==0) {
  555. LM_DBG("message body has zero length\n");
  556. return -1;
  557. }
  558. begin=body.s; /* msg->orig previously .. uri problems */
  559. if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
  560. off=begin-msg->buf;
  561. if (pmatch.rm_so!=-1){
  562. if ((l=del_lump(msg, pmatch.rm_so+off,
  563. pmatch.rm_eo-pmatch.rm_so, 0))==0)
  564. return -1;
  565. len=strlen(str2);
  566. s=pkg_malloc(len);
  567. if (s==0){
  568. LM_ERR("memory allocation failure\n");
  569. return -1;
  570. }
  571. memcpy(s, str2, len);
  572. if (insert_new_lump_after(l, s, len, 0)==0){
  573. LM_ERR("could not insert new lump\n");
  574. pkg_free(s);
  575. return -1;
  576. }
  577. return 1;
  578. }
  579. return -1;
  580. }
  581. /* sed-perl style re: s/regular expression/replacement/flags */
  582. static int subst_f(struct sip_msg* msg, char* subst, char* ignored)
  583. {
  584. struct lump* l;
  585. struct replace_lst* lst;
  586. struct replace_lst* rpl;
  587. char* begin;
  588. struct subst_expr* se;
  589. int off;
  590. int ret;
  591. int nmatches;
  592. se=(struct subst_expr*)subst;
  593. begin=get_header(msg); /* start after first line to avoid replacing
  594. the uri */
  595. off=begin-msg->buf;
  596. ret=-1;
  597. if ((lst=subst_run(se, begin, msg, &nmatches))==0)
  598. goto error; /* not found */
  599. for (rpl=lst; rpl; rpl=rpl->next){
  600. LM_DBG("%s: replacing at offset %d [%.*s] with [%.*s]\n",
  601. exports.name, rpl->offset+off,
  602. rpl->size, rpl->offset+off+msg->buf,
  603. rpl->rpl.len, rpl->rpl.s);
  604. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  605. goto error;
  606. /* hack to avoid re-copying rpl, possible because both
  607. * replace_lst & lumps use pkg_malloc */
  608. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0){
  609. LM_ERR("%s: could not insert new lump\n", exports.name);
  610. goto error;
  611. }
  612. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  613. * not free it */
  614. rpl->rpl.s=0;
  615. rpl->rpl.len=0;
  616. }
  617. ret=1;
  618. error:
  619. LM_DBG("lst was %p\n", lst);
  620. if (lst) replace_lst_free(lst);
  621. if (nmatches<0)
  622. LM_ERR("%s: subst_run failed\n", exports.name);
  623. return ret;
  624. }
  625. /* sed-perl style re: s/regular expression/replacement/flags, like
  626. * subst but works on the message uri */
  627. static int subst_uri_f(struct sip_msg* msg, char* subst, char* ignored)
  628. {
  629. char* tmp;
  630. int len;
  631. char c;
  632. struct subst_expr* se;
  633. str* result;
  634. se=(struct subst_expr*)subst;
  635. if (msg->new_uri.s){
  636. len=msg->new_uri.len;
  637. tmp=msg->new_uri.s;
  638. }else{
  639. tmp=msg->first_line.u.request.uri.s;
  640. len =msg->first_line.u.request.uri.len;
  641. };
  642. /* ugly hack: 0 s[len], and restore it afterward
  643. * (our re functions require 0 term strings), we can do this
  644. * because we always alloc len+1 (new_uri) and for first_line, the
  645. * message will always be > uri.len */
  646. c=tmp[len];
  647. tmp[len]=0;
  648. result=subst_str(tmp, msg, se, 0); /* pkg malloc'ed result */
  649. tmp[len]=c;
  650. if (result){
  651. LM_DBG("%s match - old uri= [%.*s], new uri= [%.*s]\n",
  652. exports.name, len, tmp,
  653. (result->len)?result->len:0,(result->s)?result->s:"");
  654. if (msg->new_uri.s) pkg_free(msg->new_uri.s);
  655. msg->new_uri=*result;
  656. msg->parsed_uri_ok=0; /* reset "use cached parsed uri" flag */
  657. ruri_mark_new();
  658. pkg_free(result); /* free str* pointer */
  659. return 1; /* success */
  660. }
  661. return -1; /* false, no subst. made */
  662. }
  663. /* sed-perl style re: s/regular expression/replacement/flags, like
  664. * subst but works on the user part of the uri */
  665. static int subst_user_f(struct sip_msg* msg, char* subst, char* ignored)
  666. {
  667. int rval;
  668. str* result;
  669. struct subst_expr* se;
  670. struct action act;
  671. struct run_act_ctx h;
  672. str user;
  673. char c;
  674. int nmatches;
  675. c=0;
  676. if (parse_sip_msg_uri(msg)<0){
  677. return -1; /* error, bad uri */
  678. }
  679. if (msg->parsed_uri.user.s==0){
  680. /* no user in uri */
  681. user.s="";
  682. user.len=0;
  683. }else{
  684. user=msg->parsed_uri.user;
  685. c=user.s[user.len];
  686. user.s[user.len]=0;
  687. }
  688. se=(struct subst_expr*)subst;
  689. result=subst_str(user.s, msg, se, &nmatches);/* pkg malloc'ed result */
  690. if (c) user.s[user.len]=c;
  691. if (result == NULL) {
  692. if (nmatches<0)
  693. LM_ERR("subst_user(): subst_str() failed\n");
  694. return -1;
  695. }
  696. /* result->s[result->len] = '\0'; --subst_str returns 0-term strings */
  697. memset(&act, 0, sizeof(act)); /* be on the safe side */
  698. act.type = SET_USER_T;
  699. act.val[0].type = STRING_ST;
  700. act.val[0].u.string = result->s;
  701. init_run_actions_ctx(&h);
  702. rval = do_action(&h, &act, msg);
  703. pkg_free(result->s);
  704. pkg_free(result);
  705. return rval;
  706. }
  707. /* sed-perl style re: s/regular expression/replacement/flags */
  708. static int subst_body_f(struct sip_msg* msg, char* subst, char* ignored)
  709. {
  710. struct lump* l;
  711. struct replace_lst* lst;
  712. struct replace_lst* rpl;
  713. char* begin;
  714. struct subst_expr* se;
  715. int off;
  716. int ret;
  717. int nmatches;
  718. str body;
  719. body.s = get_body(msg);
  720. if (body.s==0) {
  721. LM_ERR("failed to get the message body\n");
  722. return -1;
  723. }
  724. body.len = msg->len -(int)(body.s-msg->buf);
  725. if (body.len==0) {
  726. LM_DBG("message body has zero length\n");
  727. return -1;
  728. }
  729. se=(struct subst_expr*)subst;
  730. begin=body.s;
  731. off=begin-msg->buf;
  732. ret=-1;
  733. if ((lst=subst_run(se, begin, msg, &nmatches))==0)
  734. goto error; /* not found */
  735. for (rpl=lst; rpl; rpl=rpl->next){
  736. LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
  737. exports.name, rpl->offset+off,
  738. rpl->size, rpl->offset+off+msg->buf,
  739. rpl->rpl.len, rpl->rpl.s);
  740. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  741. goto error;
  742. /* hack to avoid re-copying rpl, possible because both
  743. * replace_lst & lumps use pkg_malloc */
  744. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0){
  745. LM_ERR("%s could not insert new lump\n",
  746. exports.name);
  747. goto error;
  748. }
  749. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  750. * not free it */
  751. rpl->rpl.s=0;
  752. rpl->rpl.len=0;
  753. }
  754. ret=1;
  755. error:
  756. LM_DBG("lst was %p\n", lst);
  757. if (lst) replace_lst_free(lst);
  758. if (nmatches<0)
  759. LM_ERR("%s subst_run failed\n", exports.name);
  760. return ret;
  761. }
  762. static inline int find_line_start(char *text, unsigned int text_len,
  763. char **buf, unsigned int *buf_len)
  764. {
  765. char *ch, *start;
  766. unsigned int len;
  767. start = *buf;
  768. len = *buf_len;
  769. while (text_len <= len) {
  770. if (strncmp(text, start, text_len) == 0) {
  771. *buf = start;
  772. *buf_len = len;
  773. return 1;
  774. }
  775. if ((ch = memchr(start, 13, len - 1))) {
  776. if (*(ch + 1) != 10) {
  777. LM_ERR("No LF after CR\n");
  778. return 0;
  779. }
  780. len = len - (ch - start + 2);
  781. start = ch + 2;
  782. } else {
  783. LM_ERR("No CRLF found\n");
  784. return 0;
  785. }
  786. }
  787. return 0;
  788. }
  789. /* Filters multipart/mixed body by leaving out everything else except
  790. * first body part of given content type. */
  791. static int filter_body_f(struct sip_msg* msg, char* _content_type,
  792. char* ignored)
  793. {
  794. char *start;
  795. unsigned int len;
  796. str *content_type, body, params, boundary;
  797. param_hooks_t hooks;
  798. param_t *p, *list;
  799. unsigned int mime;
  800. body.s = get_body(msg);
  801. if (body.s == 0) {
  802. LM_ERR("failed to get the message body\n");
  803. return -1;
  804. }
  805. body.len = msg->len - (int)(body.s - msg->buf);
  806. if (body.len == 0) {
  807. LM_DBG("message body has zero length\n");
  808. return -1;
  809. }
  810. content_type = (str *)_content_type;
  811. mime = parse_content_type_hdr(msg);
  812. if (mime <= 0) {
  813. LM_ERR("failed to parse Content-Type hdr\n");
  814. return -1;
  815. }
  816. if (mime != ((TYPE_MULTIPART << 16) + SUBTYPE_MIXED)) {
  817. LM_ERR("content type is not multipart/mixed\n");
  818. return -1;
  819. }
  820. params.s = memchr(msg->content_type->body.s, ';',
  821. msg->content_type->body.len);
  822. if (params.s == NULL) {
  823. LM_ERR("Content-Type hdr has no params\n");
  824. return -1;
  825. }
  826. params.len = msg->content_type->body.len -
  827. (params.s - msg->content_type->body.s);
  828. if (parse_params(&params, CLASS_ANY, &hooks, &list) < 0) {
  829. LM_ERR("while parsing Content-Type params\n");
  830. return -1;
  831. }
  832. boundary.s = NULL;
  833. boundary.len = 0;
  834. for (p = list; p; p = p->next) {
  835. if ((p->name.len == 8)
  836. && (strncasecmp(p->name.s, "boundary", 8) == 0)) {
  837. boundary.s = pkg_malloc(p->body.len + 2);
  838. if (boundary.s == NULL) {
  839. free_params(list);
  840. LM_ERR("no memory for boundary string\n");
  841. return -1;
  842. }
  843. *(boundary.s) = '-';
  844. *(boundary.s + 1) = '-';
  845. memcpy(boundary.s + 2, p->body.s, p->body.len);
  846. boundary.len = 2 + p->body.len;
  847. LM_DBG("boundary is <%.*s>\n", boundary.len, boundary.s);
  848. break;
  849. }
  850. }
  851. free_params(list);
  852. if (boundary.s == NULL) {
  853. LM_ERR("no mandatory param \";boundary\"\n");
  854. return -1;
  855. }
  856. start = body.s;
  857. len = body.len;
  858. while (find_line_start("Content-Type: ", 14, &start, &len)) {
  859. start = start + 14;
  860. len = len - 14;
  861. if (len > content_type->len + 2) {
  862. if (strncasecmp(start, content_type->s, content_type->len)
  863. == 0) {
  864. LM_DBG("found content type %.*s\n",
  865. content_type->len, content_type->s);
  866. start = start + content_type->len;
  867. if ((*start != 13) || (*(start + 1) != 10)) {
  868. LM_ERR("no CRLF found after content type\n");
  869. goto err;
  870. }
  871. start = start + 2;
  872. len = len - content_type->len - 2;
  873. while ((len > 0) && ((*start == 13) || (*start == 10))) {
  874. len = len - 1;
  875. start = start + 1;
  876. }
  877. if (del_lump(msg, body.s - msg->buf, start - body.s, 0)
  878. == 0) {
  879. LM_ERR("deleting lump <%.*s> failed\n",
  880. (int)(start - body.s), body.s);
  881. goto err;
  882. }
  883. if (find_line_start(boundary.s, boundary.len, &start,
  884. &len)) {
  885. if (del_lump(msg, start - msg->buf, len, 0) == 0) {
  886. LM_ERR("deleting lump <%.*s> failed\n",
  887. len, start);
  888. goto err;
  889. } else {
  890. pkg_free(boundary.s);
  891. return 1;
  892. }
  893. } else {
  894. LM_ERR("boundary not found after content\n");
  895. goto err;
  896. }
  897. }
  898. } else {
  899. pkg_free(boundary.s);
  900. return -1;
  901. }
  902. }
  903. err:
  904. pkg_free(boundary.s);
  905. return -1;
  906. }
  907. int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
  908. {
  909. struct hdr_field *hf;
  910. struct lump* l;
  911. int cnt;
  912. gparam_p gp;
  913. gp = (gparam_p)str_hf;
  914. cnt=0;
  915. /* we need to be sure we have seen all HFs */
  916. parse_headers(msg, HDR_EOH_F, 0);
  917. for (hf=msg->headers; hf; hf=hf->next) {
  918. /* for well known header names str_hf->s will be set to NULL
  919. during parsing of kamailio.cfg and str_hf->len contains
  920. the header type */
  921. if(gp->type==GPARAM_TYPE_INT)
  922. {
  923. if (gp->v.i!=hf->type)
  924. continue;
  925. } else {
  926. if (hf->name.len!=gp->v.str.len)
  927. continue;
  928. if (cmp_hdrname_str(&hf->name, &gp->v.str)!=0)
  929. continue;
  930. }
  931. l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
  932. if (l==0) {
  933. LM_ERR("no memory\n");
  934. return -1;
  935. }
  936. cnt++;
  937. }
  938. return cnt==0 ? -1 : 1;
  939. }
  940. static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo)
  941. {
  942. struct hdr_field *hf;
  943. struct lump* l;
  944. int cnt;
  945. regex_t *re;
  946. char c;
  947. regmatch_t pmatch;
  948. re = (regex_t*)key;
  949. cnt=0;
  950. /* we need to be sure we have seen all HFs */
  951. parse_headers(msg, HDR_EOH_F, 0);
  952. for (hf=msg->headers; hf; hf=hf->next)
  953. {
  954. c = hf->name.s[hf->name.len];
  955. hf->name.s[hf->name.len] = '\0';
  956. if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
  957. {
  958. hf->name.s[hf->name.len] = c;
  959. continue;
  960. }
  961. hf->name.s[hf->name.len] = c;
  962. l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
  963. if (l==0)
  964. {
  965. LM_ERR("cannot remove header\n");
  966. return -1;
  967. }
  968. cnt++;
  969. }
  970. return cnt==0 ? -1 : 1;
  971. }
  972. static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
  973. {
  974. struct hdr_field *hf;
  975. gparam_p gp;
  976. gp = (gparam_p)str_hf;
  977. /* we need to be sure we have seen all HFs */
  978. parse_headers(msg, HDR_EOH_F, 0);
  979. for (hf=msg->headers; hf; hf=hf->next) {
  980. if(gp->type==GPARAM_TYPE_INT)
  981. {
  982. if (gp->v.i!=hf->type)
  983. continue;
  984. } else {
  985. if (hf->name.len!=gp->v.str.len)
  986. continue;
  987. if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
  988. continue;
  989. }
  990. return 1;
  991. }
  992. return -1;
  993. }
  994. static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo)
  995. {
  996. struct hdr_field *hf;
  997. regex_t *re;
  998. regmatch_t pmatch;
  999. char c;
  1000. re = (regex_t*)key;
  1001. /* we need to be sure we have seen all HFs */
  1002. parse_headers(msg, HDR_EOH_F, 0);
  1003. for (hf=msg->headers; hf; hf=hf->next)
  1004. {
  1005. c = hf->name.s[hf->name.len];
  1006. hf->name.s[hf->name.len] = '\0';
  1007. if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
  1008. {
  1009. hf->name.s[hf->name.len] = c;
  1010. continue;
  1011. }
  1012. hf->name.s[hf->name.len] = c;
  1013. return 1;
  1014. }
  1015. return -1;
  1016. }
  1017. static int fixup_substre(void** param, int param_no)
  1018. {
  1019. struct subst_expr* se;
  1020. str subst;
  1021. LM_DBG("%s module -- fixing %s\n", exports.name, (char*)(*param));
  1022. if (param_no!=1) return 0;
  1023. subst.s=*param;
  1024. subst.len=strlen(*param);
  1025. se=subst_parser(&subst);
  1026. if (se==0){
  1027. LM_ERR("%s: bad subst. re %s\n", exports.name,
  1028. (char*)*param);
  1029. return E_BAD_RE;
  1030. }
  1031. /* don't free string -- needed for specifiers */
  1032. /* pkg_free(*param); */
  1033. /* replace it with the compiled subst. re */
  1034. *param=se;
  1035. return 0;
  1036. }
  1037. static int append_time_f(struct sip_msg* msg, char* p1, char *p2)
  1038. {
  1039. size_t len;
  1040. char time_str[MAX_TIME];
  1041. time_t now;
  1042. struct tm *bd_time;
  1043. now=time(0);
  1044. bd_time=gmtime(&now);
  1045. if (bd_time==NULL) {
  1046. LM_ERR("gmtime failed\n");
  1047. return -1;
  1048. }
  1049. len=strftime(time_str, MAX_TIME, TIME_FORMAT, bd_time);
  1050. if (len>MAX_TIME-2 || len==0) {
  1051. LM_ERR("unexpected time length\n");
  1052. return -1;
  1053. }
  1054. time_str[len]='\r';
  1055. time_str[len+1]='\n';
  1056. if (add_lump_rpl(msg, time_str, len+2, LUMP_RPL_HDR)==0)
  1057. {
  1058. LM_ERR("unable to add lump\n");
  1059. return -1;
  1060. }
  1061. return 1;
  1062. }
  1063. static int append_time_request_f(struct sip_msg* msg, char* p1, char *p2)
  1064. {
  1065. str time_str = {0, 0};
  1066. time_t now;
  1067. struct tm *bd_time;
  1068. struct hdr_field *hf = msg->headers;
  1069. struct lump *anchor = anchor_lump(msg, hf->name.s + hf->len - msg->buf, 0, 0);
  1070. now=time(0);
  1071. bd_time=gmtime(&now);
  1072. if (bd_time==NULL) {
  1073. LM_ERR("gmtime failed\n");
  1074. goto error;
  1075. }
  1076. time_str.s = pkg_malloc(MAX_TIME);
  1077. time_str.len=strftime(time_str.s, MAX_TIME, TIME_FORMAT, bd_time);
  1078. if (time_str.len>MAX_TIME-2 || time_str.len==0) {
  1079. LM_ERR("unexpected time length\n");
  1080. goto error;
  1081. }
  1082. time_str.s[time_str.len++]='\r';
  1083. time_str.s[time_str.len++]='\n';
  1084. if (anchor == NULL)
  1085. {
  1086. LM_ERR("Problem with getting anchor");
  1087. goto error;
  1088. }
  1089. if (insert_new_lump_after(anchor, time_str.s, time_str.len, 0) == 0)
  1090. {
  1091. LM_ERR("unable to add lump\n");
  1092. goto error;
  1093. }
  1094. return 1;
  1095. error:
  1096. if (time_str.s != NULL)
  1097. pkg_free(time_str.s);
  1098. return -1;
  1099. }
  1100. static int set_body_f(struct sip_msg* msg, char* p1, char* p2)
  1101. {
  1102. struct lump *anchor;
  1103. char* buf;
  1104. int len;
  1105. char* value_s;
  1106. int value_len;
  1107. str body = {0,0};
  1108. str nb = {0,0};
  1109. str nc = {0,0};
  1110. if(p1==0 || p2==0)
  1111. {
  1112. LM_ERR("invalid parameters\n");
  1113. return -1;
  1114. }
  1115. if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
  1116. {
  1117. LM_ERR("unable to get p1\n");
  1118. return -1;
  1119. }
  1120. if(nb.s==NULL || nb.len == 0)
  1121. {
  1122. LM_ERR("invalid body parameter\n");
  1123. return -1;
  1124. }
  1125. if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
  1126. {
  1127. LM_ERR("unable to get p2\n");
  1128. return -1;
  1129. }
  1130. if(nc.s==NULL || nc.len == 0)
  1131. {
  1132. LM_ERR("invalid content-type parameter\n");
  1133. return -1;
  1134. }
  1135. body.len = 0;
  1136. body.s = get_body(msg);
  1137. if (body.s==0)
  1138. {
  1139. LM_ERR("malformed sip message\n");
  1140. return -1;
  1141. }
  1142. del_nonshm_lump( &(msg->body_lumps) );
  1143. msg->body_lumps = NULL;
  1144. if (msg->content_length)
  1145. {
  1146. body.len = get_content_length( msg );
  1147. if(body.len > 0)
  1148. {
  1149. if(body.s+body.len>msg->buf+msg->len)
  1150. {
  1151. LM_ERR("invalid content length: %d\n", body.len);
  1152. return -1;
  1153. }
  1154. if(del_lump(msg, body.s - msg->buf, body.len, 0) == 0)
  1155. {
  1156. LM_ERR("cannot delete existing body");
  1157. return -1;
  1158. }
  1159. }
  1160. }
  1161. anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
  1162. if (anchor == 0)
  1163. {
  1164. LM_ERR("failed to get anchor\n");
  1165. return -1;
  1166. }
  1167. if (msg->content_length==0)
  1168. {
  1169. /* need to add Content-Length */
  1170. len = nb.len;
  1171. value_s=int2str(len, &value_len);
  1172. LM_DBG("content-length: %d (%s)\n", value_len, value_s);
  1173. len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
  1174. buf=pkg_malloc(sizeof(char)*(len));
  1175. if (buf==0)
  1176. {
  1177. LM_ERR("out of pkg memory\n");
  1178. return -1;
  1179. }
  1180. memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  1181. memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
  1182. memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
  1183. if (insert_new_lump_after(anchor, buf, len, 0) == 0)
  1184. {
  1185. LM_ERR("failed to insert content-length lump\n");
  1186. pkg_free(buf);
  1187. return -1;
  1188. }
  1189. }
  1190. /* add content-type */
  1191. if(msg->content_type==NULL || msg->content_type->body.len!=nc.len
  1192. || strncmp(msg->content_type->body.s, nc.s, nc.len)!=0)
  1193. {
  1194. if(msg->content_type!=NULL)
  1195. if(del_lump(msg, msg->content_type->name.s-msg->buf,
  1196. msg->content_type->len, 0) == 0)
  1197. {
  1198. LM_ERR("failed to delete content type\n");
  1199. return -1;
  1200. }
  1201. value_len = nc.len;
  1202. len=sizeof("Content-Type: ") - 1 + value_len + CRLF_LEN;
  1203. buf=pkg_malloc(sizeof(char)*(len));
  1204. if (buf==0)
  1205. {
  1206. LM_ERR("out of pkg memory\n");
  1207. return -1;
  1208. }
  1209. memcpy(buf, "Content-Type: ", sizeof("Content-Type: ") - 1);
  1210. memcpy(buf+sizeof("Content-Type: ") - 1, nc.s, value_len);
  1211. memcpy(buf+sizeof("Content-Type: ") - 1 + value_len, CRLF, CRLF_LEN);
  1212. if (insert_new_lump_after(anchor, buf, len, 0) == 0)
  1213. {
  1214. LM_ERR("failed to insert content-type lump\n");
  1215. pkg_free(buf);
  1216. return -1;
  1217. }
  1218. }
  1219. anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
  1220. if (anchor == 0)
  1221. {
  1222. LM_ERR("failed to get body anchor\n");
  1223. return -1;
  1224. }
  1225. buf=pkg_malloc(sizeof(char)*(nb.len));
  1226. if (buf==0)
  1227. {
  1228. LM_ERR("out of pkg memory\n");
  1229. return -1;
  1230. }
  1231. memcpy(buf, nb.s, nb.len);
  1232. if (insert_new_lump_after(anchor, buf, nb.len, 0) == 0)
  1233. {
  1234. LM_ERR("failed to insert body lump\n");
  1235. pkg_free(buf);
  1236. return -1;
  1237. }
  1238. LM_DBG("new body: [%.*s]", nb.len, nb.s);
  1239. return 1;
  1240. }
  1241. static int set_rpl_body_f(struct sip_msg* msg, char* p1, char* p2)
  1242. {
  1243. char* buf;
  1244. int len;
  1245. int value_len;
  1246. str nb = {0,0};
  1247. str nc = {0,0};
  1248. if(p1==0 || p2==0)
  1249. {
  1250. LM_ERR("invalid parameters\n");
  1251. return -1;
  1252. }
  1253. if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
  1254. {
  1255. LM_ERR("unable to get p1\n");
  1256. return -1;
  1257. }
  1258. if(nb.s==NULL || nb.len == 0)
  1259. {
  1260. LM_ERR("invalid body parameter\n");
  1261. return -1;
  1262. }
  1263. if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
  1264. {
  1265. LM_ERR("unable to get p2\n");
  1266. return -1;
  1267. }
  1268. if(nc.s==NULL || nc.len == 0)
  1269. {
  1270. LM_ERR("invalid content-type parameter\n");
  1271. return -1;
  1272. }
  1273. /* add content-type */
  1274. value_len = nc.len;
  1275. len=sizeof("Content-Type: ") - 1 + value_len + CRLF_LEN;
  1276. buf=pkg_malloc(sizeof(char)*(len));
  1277. if (buf==0)
  1278. {
  1279. LM_ERR("out of pkg memory\n");
  1280. return -1;
  1281. }
  1282. memcpy(buf, "Content-Type: ", sizeof("Content-Type: ") - 1);
  1283. memcpy(buf+sizeof("Content-Type: ") - 1, nc.s, value_len);
  1284. memcpy(buf+sizeof("Content-Type: ") - 1 + value_len, CRLF, CRLF_LEN);
  1285. if (add_lump_rpl(msg, buf, len, LUMP_RPL_HDR) == 0)
  1286. {
  1287. LM_ERR("failed to insert content-type lump\n");
  1288. pkg_free(buf);
  1289. return -1;
  1290. }
  1291. pkg_free(buf);
  1292. if (add_lump_rpl( msg, nb.s, nb.len, LUMP_RPL_BODY)==0) {
  1293. LM_ERR("cannot add body lump\n");
  1294. return -1;
  1295. }
  1296. return 1;
  1297. }
  1298. static int append_to_reply_f(struct sip_msg* msg, char* key, char* str0)
  1299. {
  1300. str s0;
  1301. if(key==NULL)
  1302. {
  1303. LM_ERR("bad parameters\n");
  1304. return -1;
  1305. }
  1306. if(fixup_get_svalue(msg, (gparam_p)key, &s0)!=0)
  1307. {
  1308. LM_ERR("cannot print the format\n");
  1309. return -1;
  1310. }
  1311. if ( add_lump_rpl( msg, s0.s, s0.len, LUMP_RPL_HDR)==0 )
  1312. {
  1313. LM_ERR("unable to add lump_rl\n");
  1314. return -1;
  1315. }
  1316. return 1;
  1317. }
  1318. /* add str1 to end of header or str1.r-uri.str2 */
  1319. int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
  1320. gparam_p hfval, int mode, gparam_p hfanc)
  1321. {
  1322. struct lump* anchor;
  1323. struct hdr_field *hf;
  1324. char *s;
  1325. int len;
  1326. str s0;
  1327. if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
  1328. LM_ERR("error while parsing message\n");
  1329. return -1;
  1330. }
  1331. hf = 0;
  1332. if(hfanc!=NULL) {
  1333. for (hf=msg->headers; hf; hf=hf->next) {
  1334. if(hfanc->type==GPARAM_TYPE_INT)
  1335. {
  1336. if (hfanc->v.i!=hf->type)
  1337. continue;
  1338. } else {
  1339. if (hf->name.len!=hfanc->v.str.len)
  1340. continue;
  1341. if (cmp_hdrname_str(&hf->name,&hfanc->v.str)!=0)
  1342. continue;
  1343. }
  1344. break;
  1345. }
  1346. }
  1347. if(mode == 0) { /* append */
  1348. if(hf==0) { /* after last header */
  1349. anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
  1350. } else { /* after hf */
  1351. anchor = anchor_lump(msg, hf->name.s + hf->len - msg->buf, 0, 0);
  1352. }
  1353. } else { /* insert */
  1354. if(hf==0) { /* before first header */
  1355. anchor = anchor_lump(msg, msg->headers->name.s - msg->buf, 0, 0);
  1356. } else { /* before hf */
  1357. anchor = anchor_lump(msg, hf->name.s - msg->buf, 0, 0);
  1358. }
  1359. }
  1360. if(anchor == 0) {
  1361. LM_ERR("can't get anchor\n");
  1362. return -1;
  1363. }
  1364. if(str1) {
  1365. s0 = *str1;
  1366. } else {
  1367. if(hfval) {
  1368. if(fixup_get_svalue(msg, hfval, &s0)!=0)
  1369. {
  1370. LM_ERR("cannot print the format\n");
  1371. return -1;
  1372. }
  1373. } else {
  1374. s0.len = 0;
  1375. s0.s = 0;
  1376. }
  1377. }
  1378. len=s0.len;
  1379. if (str2) len+= str2->len + REQ_LINE(msg).uri.len;
  1380. s = (char*)pkg_malloc(len);
  1381. if (!s) {
  1382. LM_ERR("no pkg memory left\n");
  1383. return -1;
  1384. }
  1385. if (likely(s0.len>0)){
  1386. memcpy(s, s0.s, s0.len);
  1387. }
  1388. if (str2) {
  1389. memcpy(s+str1->len, REQ_LINE(msg).uri.s, REQ_LINE(msg).uri.len);
  1390. memcpy(s+str1->len+REQ_LINE(msg).uri.len, str2->s, str2->len );
  1391. }
  1392. if (insert_new_lump_before(anchor, s, len, 0) == 0) {
  1393. LM_ERR("can't insert lump\n");
  1394. pkg_free(s);
  1395. return -1;
  1396. }
  1397. return 1;
  1398. }
  1399. static int append_hf_1(struct sip_msg *msg, char *str1, char *str2 )
  1400. {
  1401. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 0, 0);
  1402. }
  1403. static int append_hf_2(struct sip_msg *msg, char *str1, char *str2 )
  1404. {
  1405. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 0,
  1406. (gparam_p)str2);
  1407. }
  1408. static int insert_hf_1(struct sip_msg *msg, char *str1, char *str2 )
  1409. {
  1410. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 1, 0);
  1411. }
  1412. static int insert_hf_2(struct sip_msg *msg, char *str1, char *str2 )
  1413. {
  1414. return add_hf_helper(msg, 0, 0, (gparam_p)str1, 1,
  1415. (gparam_p)str2);
  1416. }
  1417. static int append_urihf(struct sip_msg *msg, char *str1, char *str2)
  1418. {
  1419. return add_hf_helper(msg, (str*)str1, (str*)str2, 0, 0, 0);
  1420. }
  1421. static int is_method_f(struct sip_msg *msg, char *meth, char *str2 )
  1422. {
  1423. str *m;
  1424. m = (str*)meth;
  1425. if(msg->first_line.type==SIP_REQUEST)
  1426. {
  1427. if(m->s==0)
  1428. return (msg->first_line.u.request.method_value&m->len)?1:-1;
  1429. else
  1430. return (msg->first_line.u.request.method_value==METHOD_OTHER
  1431. && msg->first_line.u.request.method.len==m->len
  1432. && (strncasecmp(msg->first_line.u.request.method.s, m->s,
  1433. m->len)==0))?1:-1;
  1434. }
  1435. if(parse_headers(msg, HDR_CSEQ_F, 0)!=0 || msg->cseq==NULL)
  1436. {
  1437. LM_ERR("cannot parse cseq header\n");
  1438. return -1; /* should it be 0 ?!?! */
  1439. }
  1440. if(m->s==0)
  1441. return (get_cseq(msg)->method_id&m->len)?1:-1;
  1442. else
  1443. return (get_cseq(msg)->method_id==METHOD_OTHER
  1444. && get_cseq(msg)->method.len==m->len
  1445. && (strncasecmp(get_cseq(msg)->method.s, m->s,
  1446. m->len)==0))?1:-1;
  1447. }
  1448. /*
  1449. * Convert char* header_name to str* parameter
  1450. */
  1451. static int hname_fixup(void** param, int param_no)
  1452. {
  1453. char c;
  1454. struct hdr_field hdr;
  1455. gparam_p gp = NULL;
  1456. gp = (gparam_p)pkg_malloc(sizeof(gparam_t));
  1457. if(gp == NULL)
  1458. {
  1459. LM_ERR("no more memory\n");
  1460. return E_UNSPEC;
  1461. }
  1462. memset(gp, 0, sizeof(gparam_t));
  1463. gp->v.str.s = (char*)*param;
  1464. gp->v.str.len = strlen(gp->v.str.s);
  1465. if(gp->v.str.len==0)
  1466. {
  1467. LM_ERR("empty header name parameter\n");
  1468. pkg_free(gp);
  1469. return E_UNSPEC;
  1470. }
  1471. c = gp->v.str.s[gp->v.str.len];
  1472. gp->v.str.s[gp->v.str.len] = ':';
  1473. gp->v.str.len++;
  1474. if (parse_hname2(gp->v.str.s, gp->v.str.s
  1475. + ((gp->v.str.len<4)?4:gp->v.str.len), &hdr)==0)
  1476. {
  1477. LM_ERR("error parsing header name\n");
  1478. pkg_free(gp);
  1479. return E_UNSPEC;
  1480. }
  1481. gp->v.str.len--;
  1482. gp->v.str.s[gp->v.str.len] = c;
  1483. if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T)
  1484. {
  1485. LM_DBG("using hdr type (%d) instead of <%.*s>\n",
  1486. hdr.type, gp->v.str.len, gp->v.str.s);
  1487. pkg_free(gp->v.str.s);
  1488. gp->v.str.s = NULL;
  1489. gp->v.i = hdr.type;
  1490. gp->type = GPARAM_TYPE_INT;
  1491. } else {
  1492. gp->type = GPARAM_TYPE_STR;
  1493. LM_DBG("using hdr type name <%.*s>\n", gp->v.str.len, gp->v.str.s);
  1494. }
  1495. *param = (void*)gp;
  1496. return 0;
  1497. }
  1498. static int free_hname_fixup(void** param, int param_no)
  1499. {
  1500. if(*param)
  1501. {
  1502. if(((gparam_p)(*param))->type==GPARAM_TYPE_STR)
  1503. pkg_free(((gparam_p)(*param))->v.str.s);
  1504. pkg_free(*param);
  1505. *param = 0;
  1506. }
  1507. return 0;
  1508. }
  1509. /*
  1510. * Convert char* method to str* parameter
  1511. */
  1512. static int fixup_method(void** param, int param_no)
  1513. {
  1514. str* s;
  1515. char *p;
  1516. int m;
  1517. unsigned int method;
  1518. s = (str*)pkg_malloc(sizeof(str));
  1519. if (!s) {
  1520. LM_ERR("no pkg memory left\n");
  1521. return E_UNSPEC;
  1522. }
  1523. s->s = (char*)*param;
  1524. s->len = strlen(s->s);
  1525. if(s->len==0)
  1526. {
  1527. LM_ERR("empty method name\n");
  1528. pkg_free(s);
  1529. return E_UNSPEC;
  1530. }
  1531. m=0;
  1532. p=s->s;
  1533. while(*p)
  1534. {
  1535. if(*p=='|')
  1536. {
  1537. *p = ',';
  1538. m=1;
  1539. }
  1540. p++;
  1541. }
  1542. if(parse_methods(s, &method)!=0)
  1543. {
  1544. LM_ERR("bad method names\n");
  1545. pkg_free(s);
  1546. return E_UNSPEC;
  1547. }
  1548. if(m==1)
  1549. {
  1550. if(method==METHOD_UNDEF || method&METHOD_OTHER)
  1551. {
  1552. LM_ERR("unknown method in list [%.*s/%d] - must be only defined methods\n",
  1553. s->len, s->s, method);
  1554. return E_UNSPEC;
  1555. }
  1556. LM_DBG("using id for methods [%.*s/%d]\n",
  1557. s->len, s->s, method);
  1558. s->s = 0;
  1559. s->len = method;
  1560. } else {
  1561. if(method!=METHOD_UNDEF && method!=METHOD_OTHER)
  1562. {
  1563. LM_DBG("using id for method [%.*s/%d]\n",
  1564. s->len, s->s, method);
  1565. s->s = 0;
  1566. s->len = method;
  1567. } else
  1568. LM_DBG("name for method [%.*s/%d]\n",
  1569. s->len, s->s, method);
  1570. }
  1571. *param = (void*)s;
  1572. return 0;
  1573. }
  1574. /*
  1575. * Convert char* privacy value to corresponding bit value
  1576. */
  1577. int fixup_privacy(void** param, int param_no)
  1578. {
  1579. str p;
  1580. unsigned int val;
  1581. p.s = (char*)*param;
  1582. p.len = strlen(p.s);
  1583. if (p.len == 0) {
  1584. LM_ERR("empty privacy value\n");
  1585. return E_UNSPEC;
  1586. }
  1587. if (parse_priv_value(p.s, p.len, &val) != p.len) {
  1588. LM_ERR("invalid privacy value\n");
  1589. return E_UNSPEC;
  1590. }
  1591. *param = (void *)(long)val;
  1592. return 0;
  1593. }
  1594. /*
  1595. * Fix in_list params: subject and list (strings that may contain pvars),
  1596. * separator (string)
  1597. */
  1598. static int fixup_in_list(void** param, int param_no)
  1599. {
  1600. if ((param_no == 1) || (param_no == 2)) return fixup_spve_null(param, 1);
  1601. if (param_no == 3) {
  1602. if ((strlen((char *)*param) != 1) || (*((char *)(*param)) == 0)) {
  1603. LM_ERR("invalid separator parameter\n");
  1604. return -1;
  1605. }
  1606. return 0;
  1607. }
  1608. LM_ERR("invalid parameter number <%d>\n", param_no);
  1609. return -1;
  1610. }
  1611. /*
  1612. * Free in_list params
  1613. */
  1614. static int fixup_free_in_list(void** param, int param_no)
  1615. {
  1616. if ((param_no == 1) || (param_no == 2)) {
  1617. LM_WARN("free function has not been defined for spve\n");
  1618. return 0;
  1619. }
  1620. if (param_no == 3) return 0;
  1621. LM_ERR("invalid parameter number <%d>\n", param_no);
  1622. return -1;
  1623. }
  1624. static int add_header_fixup(void** param, int param_no)
  1625. {
  1626. if(param_no==1)
  1627. {
  1628. return fixup_spve_null(param, param_no);
  1629. } else if(param_no==2) {
  1630. return hname_fixup(param, param_no);
  1631. } else {
  1632. LM_ERR("wrong number of parameters\n");
  1633. return E_UNSPEC;
  1634. }
  1635. }
  1636. static int fixup_body_type(void** param, int param_no)
  1637. {
  1638. char *p;
  1639. char *r;
  1640. unsigned int type;
  1641. if(param_no==1) {
  1642. p = (char*)*param;
  1643. if (p==0 || p[0]==0) {
  1644. type = 0;
  1645. } else {
  1646. r = decode_mime_type( p, p+strlen(p) , &type);
  1647. if (r==0) {
  1648. LM_ERR("unsupported mime <%s>\n",p);
  1649. return E_CFG;
  1650. }
  1651. if ( r!=p+strlen(p) ) {
  1652. LM_ERR("multiple mimes not supported!\n");
  1653. return E_CFG;
  1654. }
  1655. }
  1656. pkg_free(*param);
  1657. *param = (void*)(long)type;
  1658. }
  1659. return 0;
  1660. }
  1661. static int has_body_f(struct sip_msg *msg, char *type, char *str2 )
  1662. {
  1663. int mime;
  1664. /* parse content len hdr */
  1665. if ( msg->content_length==NULL &&
  1666. (parse_headers(msg,HDR_CONTENTLENGTH_F, 0)==-1||msg->content_length==NULL))
  1667. return -1;
  1668. if (get_content_length (msg)==0) {
  1669. LM_DBG("content length is zero\n");
  1670. /* Nothing to see here, please move on. */
  1671. return -1;
  1672. }
  1673. /* check type also? */
  1674. if (type==0)
  1675. return 1;
  1676. /* the function search for and parses the Content-Type hdr */
  1677. mime = parse_content_type_hdr (msg);
  1678. if (mime<0) {
  1679. LM_ERR("failed to extract content type hdr\n");
  1680. return -1;
  1681. }
  1682. if (mime==0) {
  1683. /* content type hdr not found -> according the RFC3261 we
  1684. * assume APPLICATION/SDP --bogdan */
  1685. mime = ((TYPE_APPLICATION << 16) + SUBTYPE_SDP);
  1686. }
  1687. LM_DBG("content type is %d\n",mime);
  1688. if ( (unsigned int)mime!=(unsigned int)(unsigned long)type )
  1689. return -1;
  1690. return 1;
  1691. }
  1692. int is_privacy_f(struct sip_msg *msg, char *_privacy, char *str2 )
  1693. {
  1694. if (parse_privacy(msg) == -1)
  1695. return -1;
  1696. return get_privacy_values(msg) & ((unsigned int)(long)_privacy) ? 1 : -1;
  1697. }
  1698. /*
  1699. * Checks if subject is found in list
  1700. */
  1701. int in_list_f(struct sip_msg* _m, char* _subject, char* _list, char* _sep)
  1702. {
  1703. str subject, list;
  1704. int sep;
  1705. char *at, *past, *next_sep, *s;
  1706. if (fixup_get_svalue(_m, (gparam_p)_subject, &subject) != 0) {
  1707. LM_ERR("cannot get subject value\n");
  1708. return -1;
  1709. } else {
  1710. if (subject.len == 0) {
  1711. LM_ERR("subject cannot be empty string\n");
  1712. return -1;
  1713. }
  1714. }
  1715. if (fixup_get_svalue(_m, (gparam_p)_list, &list) != 0) {
  1716. LM_ERR("cannot get list value\n");
  1717. return -1;
  1718. } else {
  1719. if (list.len == 0) return -1;
  1720. }
  1721. sep = _sep[0];
  1722. at = list.s;
  1723. past = list.s + list.len;
  1724. /* Eat leading white space */
  1725. while ((at < past) &&
  1726. ((*at == ' ') || (*at == '\t') || (*at == '\r') || (*at == '\n') )) {
  1727. at++;
  1728. }
  1729. while (at < past) {
  1730. next_sep = index(at, sep);
  1731. s = next_sep;
  1732. if (s == NULL) {
  1733. /* Eat trailing white space */
  1734. while ((at < past) &&
  1735. ((*(past-1) == ' ') || (*(past-1) == '\t') || (*(past-1) == '\r') || (*(past-1) == '\n') )) {
  1736. past--;
  1737. }
  1738. if ((subject.len == (past - at)) &&
  1739. strncmp(at, subject.s, subject.len) == 0) {
  1740. return 1;
  1741. } else {
  1742. return -1;
  1743. }
  1744. } else {
  1745. /* Eat trailing white space */
  1746. while ((at < s) &&
  1747. ((*(s-1) == ' ') || (*(s-1) == '\t') || (*(s-1) == '\r') || (*(s-1) == '\n') )) {
  1748. s--;
  1749. }
  1750. if ((subject.len == (s - at)) &&
  1751. strncmp(at, subject.s, subject.len) == 0) {
  1752. return 1;
  1753. } else {
  1754. at = next_sep + 1;
  1755. /* Eat leading white space */
  1756. while ((at < past) &&
  1757. ((*at == ' ') || (*at == '\t') || (*at == '\r') || (*at == '\n') )) {
  1758. at++;
  1759. }
  1760. }
  1761. }
  1762. }
  1763. return -1;
  1764. }
  1765. static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 )
  1766. {
  1767. str s1;
  1768. str s2;
  1769. int ret;
  1770. if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
  1771. {
  1772. LM_ERR("cannot get first parameter\n");
  1773. return -8;
  1774. }
  1775. if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
  1776. {
  1777. LM_ERR("cannot get second parameter\n");
  1778. return -8;
  1779. }
  1780. ret = cmp_str(&s1, &s2);
  1781. if(ret==0)
  1782. return 1;
  1783. if(ret>0)
  1784. return -1;
  1785. return -2;
  1786. }
  1787. static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2)
  1788. {
  1789. str s1;
  1790. str s2;
  1791. int ret;
  1792. if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
  1793. {
  1794. LM_ERR("cannot get first parameter\n");
  1795. return -8;
  1796. }
  1797. if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
  1798. {
  1799. LM_ERR("cannot get second parameter\n");
  1800. return -8;
  1801. }
  1802. ret = cmpi_str(&s1, &s2);
  1803. if(ret==0)
  1804. return 1;
  1805. if(ret>0)
  1806. return -1;
  1807. return -2;
  1808. }
  1809. static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 )
  1810. {
  1811. str s1;
  1812. str s2;
  1813. int ret;
  1814. if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
  1815. {
  1816. LM_ERR("cannot get first parameter\n");
  1817. return -8;
  1818. }
  1819. if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
  1820. {
  1821. LM_ERR("cannot get second parameter\n");
  1822. return -8;
  1823. }
  1824. if (s1.len < s2.len) return -1;
  1825. ret = strncmp(s1.s, s2.s, s2.len);
  1826. if(ret==0)
  1827. return 1;
  1828. if(ret>0)
  1829. return -1;
  1830. return -2;
  1831. }
  1832. static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 )
  1833. {
  1834. int sdp_session_num = 0, sdp_stream_num;
  1835. sdp_session_cell_t* sdp_session;
  1836. sdp_stream_cell_t* sdp_stream;
  1837. if (0 == parse_sdp(msg)) {
  1838. for(;;) {
  1839. sdp_session = get_sdp_session(msg, sdp_session_num);
  1840. if(!sdp_session) break;
  1841. sdp_stream_num = 0;
  1842. for(;;) {
  1843. sdp_stream = get_sdp_stream(msg, sdp_session_num, sdp_stream_num);
  1844. if(!sdp_stream) break;
  1845. if(sdp_stream->media.len==AUDIO_STR_LEN &&
  1846. strncmp(sdp_stream->media.s,AUDIO_STR,AUDIO_STR_LEN)==0 &&
  1847. sdp_stream->is_on_hold)
  1848. return 1;
  1849. sdp_stream_num++;
  1850. }
  1851. sdp_session_num++;
  1852. }
  1853. }
  1854. return -1;
  1855. }
  1856. int fixup_regexpNL_none(void** param, int param_no)
  1857. {
  1858. regex_t* re;
  1859. if (param_no != 1 && param_no != 2 )
  1860. {
  1861. LM_ERR("invalid parameter number %d\n", param_no);
  1862. return E_UNSPEC;
  1863. }
  1864. if (param_no == 2)
  1865. return 0;
  1866. /* param 1 */
  1867. if ((re=pkg_malloc(sizeof(regex_t)))==0) {
  1868. LM_ERR("no more pkg memory\n");
  1869. return E_OUT_OF_MEM;
  1870. }
  1871. if (regcomp(re, *param, REG_EXTENDED|REG_ICASE)) {
  1872. pkg_free(re);
  1873. LM_ERR("bad re %s\n", (char*)*param);
  1874. return E_BAD_RE;
  1875. }
  1876. /* free string */
  1877. pkg_free(*param);
  1878. /* replace it with the compiled re */
  1879. *param=re;
  1880. return 0;
  1881. }
  1882. /*! \brief
  1883. * fixup for functions that get two parameters
  1884. * - first parameter is converted to regular expression structure
  1885. * - second parameter is not converted
  1886. */
  1887. int fixup_regexp_none(void** param, int param_no)
  1888. {
  1889. if (param_no != 1 && param_no != 2 )
  1890. {
  1891. LM_ERR("invalid parameter number %d\n", param_no);
  1892. return E_UNSPEC;
  1893. }
  1894. if (param_no == 1)
  1895. return fixup_regexp_null(param, 1);
  1896. return 0;
  1897. }
  1898. /**
  1899. * fixup free for functions that get two parameters
  1900. * - first parameter was converted to regular expression
  1901. * - second parameter was notconverted
  1902. */
  1903. int fixup_free_regexp_none(void** param, int param_no)
  1904. {
  1905. if (param_no != 1 && param_no != 2 )
  1906. {
  1907. LM_ERR("invalid parameter number %d\n", param_no);
  1908. return E_UNSPEC;
  1909. }
  1910. if (param_no == 1)
  1911. return fixup_free_regexp_null(param, 1);
  1912. return 0;
  1913. }
  1914. /**
  1915. *
  1916. */
  1917. static int search_hf_f(struct sip_msg* msg, char* str_hf, char* re, char *flags)
  1918. {
  1919. hdr_field_t *hf;
  1920. hdr_field_t *hfl = NULL;
  1921. str body;
  1922. gparam_t *gp;
  1923. regmatch_t pmatch;
  1924. char c;
  1925. int ret;
  1926. gp = (gparam_t*)str_hf;
  1927. /* we need to be sure we have seen all HFs */
  1928. parse_headers(msg, HDR_EOH_F, 0);
  1929. for (hf=msg->headers; hf; hf=hf->next) {
  1930. if(gp->type==GPARAM_TYPE_INT)
  1931. {
  1932. if (gp->v.i!=hf->type)
  1933. continue;
  1934. } else {
  1935. if (hf->name.len!=gp->v.str.len)
  1936. continue;
  1937. if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
  1938. continue;
  1939. }
  1940. if(flags==NULL || *flags!='l')
  1941. {
  1942. body = hf->body;
  1943. c = body.s[body.len];
  1944. body.s[body.len] = '\0';
  1945. ret = regexec((regex_t*) re, body.s, 1, &pmatch, 0);
  1946. body.s[body.len] = c;
  1947. if(ret==0)
  1948. {
  1949. /* match */
  1950. if(flags==NULL || *flags!='l')
  1951. return 1;
  1952. } else {
  1953. if(flags!=NULL && *flags=='f')
  1954. return 1;
  1955. }
  1956. } else {
  1957. hfl = hf;
  1958. }
  1959. }
  1960. if(hfl!=NULL)
  1961. {
  1962. hf = hfl;
  1963. body = hf->body;
  1964. c = body.s[body.len];
  1965. body.s[body.len] = '\0';
  1966. ret = regexec((regex_t*) re, body.s, 1, &pmatch, 0);
  1967. body.s[body.len] = c;
  1968. if(ret==0)
  1969. return 1;
  1970. }
  1971. return -1;
  1972. }
  1973. /*
  1974. * Convert header name, regexp and flags
  1975. */
  1976. static int fixup_search_hf(void** param, int param_no)
  1977. {
  1978. if(param_no==1)
  1979. return hname_fixup(param, param_no);
  1980. if(param_no==2)
  1981. return fixup_regexp_null(param, 1);
  1982. return 0;
  1983. }
  1984. /* sed-perl style re: s/regular expression/replacement/flags */
  1985. static int subst_hf_f(struct sip_msg *msg, char *str_hf, char *subst, char *flags)
  1986. {
  1987. struct lump* l;
  1988. struct replace_lst* lst = NULL;
  1989. struct replace_lst* rpl = NULL;
  1990. char* begin;
  1991. struct subst_expr* se;
  1992. int off;
  1993. int nmatches;
  1994. str body;
  1995. hdr_field_t *hf;
  1996. hdr_field_t *hfl = NULL;
  1997. gparam_t *gp;
  1998. char c;
  1999. int ret;
  2000. ret = -1;
  2001. gp = (gparam_t*)str_hf;
  2002. se=(struct subst_expr*)subst;
  2003. /* we need to be sure we have seen all HFs */
  2004. parse_headers(msg, HDR_EOH_F, 0);
  2005. for (hf=msg->headers; hf; hf=hf->next) {
  2006. if(gp->type==GPARAM_TYPE_INT)
  2007. {
  2008. if (gp->v.i!=hf->type)
  2009. continue;
  2010. } else {
  2011. if (hf->name.len!=gp->v.str.len)
  2012. continue;
  2013. if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
  2014. continue;
  2015. }
  2016. if(flags==NULL || *flags!='l')
  2017. {
  2018. body = hf->body;
  2019. c = body.s[body.len];
  2020. body.s[body.len] = '\0';
  2021. begin=body.s;
  2022. off=begin-msg->buf;
  2023. lst=subst_run(se, begin, msg, &nmatches);
  2024. body.s[body.len] = c;
  2025. if(lst==0 && flags!=NULL && *flags=='f')
  2026. goto error; /* not found */
  2027. if(lst!=0)
  2028. ret=1;
  2029. for (rpl=lst; rpl; rpl=rpl->next)
  2030. {
  2031. LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
  2032. exports.name, rpl->offset+off,
  2033. rpl->size, rpl->offset+off+msg->buf,
  2034. rpl->rpl.len, rpl->rpl.s);
  2035. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  2036. {
  2037. ret=-1;
  2038. goto error;
  2039. }
  2040. /* hack to avoid re-copying rpl, possible because both
  2041. * replace_lst & lumps use pkg_malloc */
  2042. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0)
  2043. {
  2044. LM_ERR("%s could not insert new lump\n",
  2045. exports.name);
  2046. ret=-1;
  2047. goto error;
  2048. }
  2049. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  2050. * not free it */
  2051. rpl->rpl.s=0;
  2052. rpl->rpl.len=0;
  2053. }
  2054. } else {
  2055. hfl = hf;
  2056. }
  2057. }
  2058. if(hfl!=NULL)
  2059. {
  2060. hf= hfl;
  2061. body = hf->body;
  2062. c = body.s[body.len];
  2063. body.s[body.len] = '\0';
  2064. begin=body.s;
  2065. off=begin-msg->buf;
  2066. lst=subst_run(se, begin, msg, &nmatches);
  2067. body.s[body.len] = c;
  2068. if(lst==0)
  2069. goto error; /* not found */
  2070. ret=1;
  2071. for (rpl=lst; rpl; rpl=rpl->next)
  2072. {
  2073. LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
  2074. exports.name, rpl->offset+off,
  2075. rpl->size, rpl->offset+off+msg->buf,
  2076. rpl->rpl.len, rpl->rpl.s);
  2077. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  2078. {
  2079. ret=-1;
  2080. goto error;
  2081. }
  2082. /* hack to avoid re-copying rpl, possible because both
  2083. * replace_lst & lumps use pkg_malloc */
  2084. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0)
  2085. {
  2086. LM_ERR("%s could not insert new lump\n",
  2087. exports.name);
  2088. ret=-1;
  2089. goto error;
  2090. }
  2091. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  2092. * not free it */
  2093. rpl->rpl.s=0;
  2094. rpl->rpl.len=0;
  2095. }
  2096. }
  2097. error:
  2098. LM_DBG("lst was %p\n", lst);
  2099. if (lst) replace_lst_free(lst);
  2100. if (nmatches<0)
  2101. LM_ERR("%s subst_run failed\n", exports.name);
  2102. return ret;
  2103. }
  2104. /*
  2105. * Convert header name, substexp and flags
  2106. */
  2107. static int fixup_subst_hf(void** param, int param_no)
  2108. {
  2109. if(param_no==1)
  2110. return hname_fixup(param, param_no);
  2111. if(param_no==2)
  2112. return fixup_substre(param, 1);
  2113. return 0;
  2114. }