textops.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. /*$Id$
  2. *
  3. * Example ser module, it implements the following commands:
  4. * search_append("key", "txt") - insert a "txt" after "key"
  5. * replace("txt1", "txt2") - replaces txt1 with txt2 (txt1 can be a re)
  6. * search("txt") - searches for txt (txt can be a regular expression)
  7. * append_to_reply("txt") - appends txt to the reply?
  8. * append_hf("P-foo: bar\r\n");
  9. *
  10. *
  11. *
  12. * Copyright (C) 2001-2003 FhG Fokus
  13. *
  14. * This file is part of ser, a free SIP server.
  15. *
  16. * ser is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version
  20. *
  21. * For a license to use the ser software under conditions
  22. * other than those described here, or to purchase support for this
  23. * software, please contact iptel.org by e-mail at the following addresses:
  24. * [email protected]
  25. *
  26. * ser is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  34. *
  35. *
  36. * History:
  37. * -------
  38. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  39. * 2003-01-29: - rewriting actions (replace, search_append) now begin
  40. * at the second line -- previously, they could affect
  41. * first line too, which resulted in wrong calculation of
  42. * forwarded requests and an error consequently
  43. * - replace_all introduced
  44. * 2003-01-28 scratchpad removed (jiri)
  45. * 2003-01-18 append_urihf introduced (jiri)
  46. * 2003-03-10 module export interface updated to the new format (andrei)
  47. * 2003-03-16 flags export parameter added (janakj)
  48. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  49. * 2003-04-97 actions permitted to be used from failure/reply routes (jiri)
  50. * 2003-04-21 remove_hf and is_present_hf introduced (jiri)
  51. * 2003-08-19 subst added (support for sed like res:s/re/repl/flags) (andrei)
  52. * 2003-08-20 subst_uri added (like above for uris) (andrei)
  53. * 2003-09-11 updated to new build_lump_rpl() interface (bogdan)
  54. * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
  55. * 2004-05-09: append_time introduced (jiri)
  56. * 2004-07-06 subst_user added (like subst_uri but only for user) (sobomax)
  57. * 2004-11-12 subst_user changes (old serdev mails) (andrei)
  58. * 2006-02-23 xl_lib formating, multi-value support (tma)
  59. * 2006-08-30 added static buffer support (tma)
  60. */
  61. #include "../../comp_defs.h"
  62. #include "../../action.h"
  63. #include "../../sr_module.h"
  64. #include "../../dprint.h"
  65. #include "../../data_lump.h"
  66. #include "../../data_lump_rpl.h"
  67. #include "../../error.h"
  68. #include "../../mem/mem.h"
  69. #include "../../re.h"
  70. #include "../../parser/parse_uri.h"
  71. #include "../../parser/parse_hname2.h"
  72. #include "../../onsend.h"
  73. #include "../../ut.h"
  74. #include "../../select.h"
  75. #include "../../modules/xprint/xp_lib.h"
  76. #include "../../script_cb.h"
  77. #include "../../select_buf.h"
  78. #include "../../ser_time.h"
  79. #include "../../dset.h"
  80. #include <stdio.h>
  81. #include <stdlib.h>
  82. #include <string.h>
  83. #include <sys/types.h> /* for regex */
  84. #include <regex.h>
  85. #include <time.h>
  86. #include <sys/time.h>
  87. MODULE_VERSION
  88. /* RFC822-conforming dates format:
  89. %a -- abbreviated week of day name (locale), %d day of month
  90. as decimal number, %b abbreviated month name (locale), %Y
  91. year with century, %T time in 24h notation
  92. */
  93. #define TIME_FORMAT "Date: %a, %d %b %Y %H:%M:%S GMT"
  94. #define MAX_TIME 64
  95. static int xlbuf_size = 4096;
  96. static int search_f(struct sip_msg*, char*, char*);
  97. static int replace_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 remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
  102. static int remove_hf_re_f(struct sip_msg* msg, char* str_hf, char* foo);
  103. static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
  104. static int replace_all_f(struct sip_msg* msg, char* key, char* str);
  105. static int search_append_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(struct sip_msg* msg, char* str1, char* str2);
  108. static int append_urihf(struct sip_msg* msg, char* str1, char* str2);
  109. static int append_time_f(struct sip_msg* msg, char* , char *);
  110. static int incexc_hf_value_f(struct sip_msg* msg, char* , char *);
  111. static int include_hf_value_fixup(void**, int);
  112. static int exclude_hf_value_fixup(void**, int);
  113. static int hf_value_exists_fixup(void**, int);
  114. static int insupddel_hf_value_f(struct sip_msg* msg, char* _hname, char* _val);
  115. static int append_hf_value_fixup(void** param, int param_no);
  116. static int insert_hf_value_fixup(void** param, int param_no);
  117. static int remove_hf_value_fixup(void** param, int param_no);
  118. static int assign_hf_value_fixup(void** param, int param_no);
  119. static int remove_hf_value2_fixup(void** param, int param_no);
  120. static int assign_hf_value2_fixup(void** param, int param_no);
  121. static int fixup_xlstr(void** param, int param_no);
  122. static int fixup_regex_xlstr(void** param, int param_no);
  123. static int fixup_substre(void**, int);
  124. extern select_row_t sel_declaration[];
  125. static int mod_init(void);
  126. static cmd_export_t cmds[]={
  127. {"search", search_f, 1, fixup_regex_1,
  128. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
  129. {"search_append", search_append_f, 2, fixup_regex_xlstr,
  130. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  131. {"replace", replace_f, 2, fixup_regex_xlstr,
  132. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  133. {"replace_all", replace_all_f, 2, fixup_regex_xlstr,
  134. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  135. {"append_to_reply", append_to_reply_f, 1, fixup_xlstr,
  136. REQUEST_ROUTE},
  137. {"append_hf", append_hf, 1, fixup_xlstr,
  138. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE | BRANCH_ROUTE},
  139. {"append_urihf", append_urihf, 2, fixup_xlstr,
  140. REQUEST_ROUTE|FAILURE_ROUTE},
  141. /* obsolete: use remove_hf_value(), does not support compact headers */
  142. {"remove_hf", remove_hf_f, 1, fixup_var_str_1,
  143. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  144. {"remove_hf_re", remove_hf_re_f, 1, fixup_regex_12,
  145. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  146. /* obsolete: use @msg.HFNAME, , does not support compact headers */
  147. {"is_present_hf", is_present_hf_f, 1, fixup_var_str_1,
  148. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  149. {"subst", subst_f, 1, fixup_substre,
  150. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  151. {"subst_uri", subst_uri_f, 1, fixup_substre,
  152. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  153. {"subst_user", subst_user_f, 1, fixup_substre,
  154. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
  155. {"append_time", append_time_f, 0, 0,
  156. REQUEST_ROUTE },
  157. {"append_hf_value", insupddel_hf_value_f, 2, append_hf_value_fixup,
  158. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  159. {"insert_hf_value", insupddel_hf_value_f, 2, insert_hf_value_fixup,
  160. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  161. {"remove_hf_value", insupddel_hf_value_f, 1, remove_hf_value_fixup,
  162. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  163. {"assign_hf_value", insupddel_hf_value_f, 2, assign_hf_value_fixup,
  164. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  165. {"remove_hf_value2", insupddel_hf_value_f, 1, remove_hf_value2_fixup,
  166. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  167. {"assign_hf_value2", insupddel_hf_value_f, 2, assign_hf_value2_fixup,
  168. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  169. {"include_hf_value", incexc_hf_value_f, 2, include_hf_value_fixup,
  170. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  171. {"exclude_hf_value", incexc_hf_value_f, 2, exclude_hf_value_fixup,
  172. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  173. {"hf_value_exists", incexc_hf_value_f, 2, hf_value_exists_fixup,
  174. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  175. {0,0,0,0,0}
  176. };
  177. static param_export_t params[]={
  178. {"xlbuf_size", PARAM_INT, &xlbuf_size},
  179. {0,0,0}
  180. }; /* no params */
  181. struct module_exports exports= {
  182. "textops",
  183. cmds,
  184. 0, /* RPC methods */
  185. params,
  186. mod_init, /* module initialization function */
  187. 0, /* response function */
  188. 0, /* destroy function */
  189. 0, /* on_cancel function */
  190. 0, /* per-child init function */
  191. };
  192. static int mod_init(void)
  193. {
  194. DBG("%s - initializing\n", exports.name);
  195. register_select_table(sel_declaration);
  196. return 0;
  197. }
  198. struct xlstr {
  199. str s;
  200. xl_elog_t* xlfmt;
  201. };
  202. static xl_print_log_f* xl_print = NULL;
  203. static xl_parse_format_f* xl_parse = NULL;
  204. #define NO_SCRIPT -1
  205. static int fixup_xlstr(void** param, int param_no) {
  206. struct xlstr* s;
  207. s = pkg_malloc(sizeof(*s));
  208. if (!s) return E_OUT_OF_MEM;
  209. s->s.s = *param;
  210. s->s.len = strlen(s->s.s);
  211. s->xlfmt = 0;
  212. if (strchr(s->s.s, '%')) {
  213. if (!xl_print) {
  214. xl_print=(xl_print_log_f*)find_export("xprint", NO_SCRIPT, 0);
  215. if (!xl_print) {
  216. LOG(L_CRIT,"ERROR: textops: cannot find \"xprint\", is module xprint loaded?\n");
  217. return E_UNSPEC;
  218. }
  219. }
  220. if (!xl_parse) {
  221. xl_parse=(xl_parse_format_f*)find_export("xparse", NO_SCRIPT, 0);
  222. if (!xl_parse) {
  223. LOG(L_CRIT,"ERROR: textops: cannot find \"xparse\", is module xprint loaded?\n");
  224. return E_UNSPEC;
  225. }
  226. }
  227. if(xl_parse(s->s.s, &s->xlfmt) < 0) {
  228. LOG(L_ERR, "ERROR: textops: wrong format '%s'\n", s->s.s);
  229. return E_UNSPEC;
  230. }
  231. }
  232. *param = s;
  233. return 0;
  234. }
  235. static int eval_xlstr(struct sip_msg* msg, struct xlstr* val, str* s) {
  236. static char *xlbuf = NULL;
  237. if (val) {
  238. if (val->xlfmt) {
  239. if (!xlbuf) {
  240. xlbuf = pkg_malloc(xlbuf_size);
  241. if (!xlbuf) {
  242. LOG(L_ERR, "ERROR: out of memory\n");
  243. return E_OUT_OF_MEM;
  244. }
  245. }
  246. s->len = xlbuf_size-1;
  247. if (xl_print(msg, val->xlfmt, xlbuf, &s->len) < 0) {
  248. LOG(L_ERR, "ERROR: textops: eval_xlstr: Error while formating result '%.*s'\n", val->s.len, val->s.s);
  249. s->len = 0;
  250. return E_UNSPEC;
  251. }
  252. s->s = xlbuf;
  253. }
  254. else {
  255. *s = val->s;
  256. }
  257. }
  258. else
  259. s->len = 0;
  260. return 1;
  261. }
  262. static int fixup_regex_xlstr(void** param, int param_no) {
  263. if (param_no == 1)
  264. return fixup_regex_1(param, param_no);
  265. else if (param_no == 2)
  266. return fixup_xlstr(param, param_no);
  267. else
  268. return 0;
  269. }
  270. static char *get_header(struct sip_msg *msg)
  271. {
  272. return SIP_MSG_START(msg)+msg->first_line.len;
  273. }
  274. static int search_f(struct sip_msg* msg, char* key, char* str2)
  275. {
  276. /*we registered only 1 param, so we ignore str2*/
  277. regmatch_t pmatch;
  278. char* buf;
  279. struct onsend_info* snd_inf;
  280. if ((snd_inf=get_onsend_info())!=0)
  281. buf=snd_inf->buf;
  282. else
  283. buf=msg->buf;
  284. if (regexec(((fparam_t*)key)->v.regex, buf, 1, &pmatch, 0)!=0) return -1;
  285. return 1;
  286. }
  287. static int search_append_f(struct sip_msg* msg, char* key, char* _str)
  288. {
  289. struct lump* l;
  290. regmatch_t pmatch;
  291. str str;
  292. char* s;
  293. char *begin;
  294. int off;
  295. begin=get_header(msg); /* msg->orig/buf previously .. uri problems */
  296. off=begin-msg->buf;
  297. if (regexec(((fparam_t*)key)->v.regex, begin, 1, &pmatch, 0)!=0) return -1;
  298. if (pmatch.rm_so!=-1){
  299. if (eval_xlstr(msg, (void*) _str, &str) < 0) return -1;
  300. if ((l=anchor_lump(msg, off+pmatch.rm_eo, 0, 0))==0)
  301. return -1;
  302. s=pkg_malloc(str.len);
  303. if (s==0){
  304. LOG(L_ERR, "ERROR: search_append_f: mem. allocation failure\n");
  305. return -1;
  306. }
  307. memcpy(s, str.s, str.len);
  308. if (insert_new_lump_after(l, s, str.len, 0)==0){
  309. LOG(L_ERR, "ERROR: could not insert new lump\n");
  310. pkg_free(s);
  311. return -1;
  312. }
  313. return 1;
  314. }
  315. return -1;
  316. }
  317. static int replace_all_f(struct sip_msg* msg, char* key, char* _str)
  318. {
  319. struct lump* l;
  320. regmatch_t pmatch;
  321. char* s;
  322. str str;
  323. char* begin;
  324. int off;
  325. int ret;
  326. int eflags;
  327. /* we need to be sure we have seen all HFs */
  328. parse_headers(msg, HDR_EOH_F, 0);
  329. begin=get_header(msg); /* msg->orig previously .. uri problems */
  330. ret=-1; /* pessimist: we will not find any */
  331. eflags=0; /* match ^ at the beginning of the string*/
  332. if (eval_xlstr(msg, (void*) _str, &str) < 0) return -1;
  333. while (begin<msg->buf+msg->len
  334. && regexec(((fparam_t*)key)->v.regex, begin, 1, &pmatch, eflags)==0) {
  335. off=begin-msg->buf;
  336. /* change eflags, not to match any more at string start */
  337. eflags|=REG_NOTBOL;
  338. if (pmatch.rm_so==-1){
  339. LOG(L_ERR, "ERROR: replace_all_f: offset unknown\n");
  340. return -1;
  341. }
  342. if ((l=del_lump(msg, pmatch.rm_so+off,
  343. pmatch.rm_eo-pmatch.rm_so, 0))==0) {
  344. LOG(L_ERR, "ERROR: replace_all_f: del_lump failed\n");
  345. return -1;
  346. }
  347. s=pkg_malloc(str.len);
  348. if (s==0){
  349. LOG(L_ERR, "ERROR: replace_all_f: mem. allocation failure\n");
  350. return -1;
  351. }
  352. memcpy(s, str.s, str.len);
  353. if (insert_new_lump_after(l, s, str.len, 0)==0){
  354. LOG(L_ERR, "ERROR: could not insert new lump\n");
  355. pkg_free(s);
  356. return -1;
  357. }
  358. /* new cycle */
  359. begin=begin+pmatch.rm_eo;
  360. ret=1;
  361. } /* while found ... */
  362. return ret;
  363. }
  364. static int replace_f(struct sip_msg* msg, char* key, char* _str)
  365. {
  366. struct lump* l;
  367. regmatch_t pmatch;
  368. char* s;
  369. str str;
  370. char* begin;
  371. int off;
  372. /* we need to be sure we have seen all HFs */
  373. parse_headers(msg, HDR_EOH_F, 0);
  374. begin=get_header(msg); /* msg->orig previously .. uri problems */
  375. if (regexec(((fparam_t*)key)->v.regex, begin, 1, &pmatch, 0)!=0) return -1;
  376. off=begin-msg->buf;
  377. if (pmatch.rm_so!=-1){
  378. if (eval_xlstr(msg, (void*) _str, &str) < 0) return -1;
  379. if ((l=del_lump(msg, pmatch.rm_so+off,
  380. pmatch.rm_eo-pmatch.rm_so, 0))==0)
  381. return -1;
  382. s=pkg_malloc(str.len);
  383. if (s==0){
  384. LOG(L_ERR, "ERROR: replace_f: mem. allocation failure\n");
  385. return -1;
  386. }
  387. memcpy(s, str.s, str.len);
  388. if (insert_new_lump_after(l, s, str.len, 0)==0){
  389. LOG(L_ERR, "ERROR: could not insert new lump\n");
  390. pkg_free(s);
  391. return -1;
  392. }
  393. return 1;
  394. }
  395. return -1;
  396. }
  397. /* sed-perl style re: s/regular expression/replacement/flags */
  398. static int subst_f(struct sip_msg* msg, char* subst, char* ignored)
  399. {
  400. struct lump* l;
  401. struct replace_lst* lst;
  402. struct replace_lst* rpl;
  403. char* begin;
  404. struct subst_expr* se;
  405. int off;
  406. int ret;
  407. int nmatches;
  408. se=(struct subst_expr*)subst;
  409. begin=get_header(msg); /* start after first line to avoid replacing
  410. the uri */
  411. off=begin-msg->buf;
  412. ret=-1;
  413. if ((lst=subst_run(se, begin, msg, &nmatches))==0)
  414. goto error; /* not found */
  415. for (rpl=lst; rpl; rpl=rpl->next){
  416. DBG(" %s: subst_f: replacing at offset %d [%.*s] with [%.*s]\n",
  417. exports.name, rpl->offset+off,
  418. rpl->size, rpl->offset+off+msg->buf,
  419. rpl->rpl.len, rpl->rpl.s);
  420. if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
  421. goto error;
  422. /* hack to avoid re-copying rpl, possible because both
  423. * replace_lst & lumps use pkg_malloc */
  424. if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0){
  425. LOG(L_ERR, "ERROR: %s: subst_f: could not insert new lump\n",
  426. exports.name);
  427. goto error;
  428. }
  429. /* hack continued: set rpl.s to 0 so that replace_lst_free will
  430. * not free it */
  431. rpl->rpl.s=0;
  432. rpl->rpl.len=0;
  433. }
  434. ret=1;
  435. error:
  436. DBG("subst_f: lst was %p\n", lst);
  437. if (lst) replace_lst_free(lst);
  438. if (nmatches<0)
  439. LOG(L_ERR, "ERROR: %s: subst_run failed\n", exports.name);
  440. return ret;
  441. }
  442. /* sed-perl style re: s/regular expression/replacement/flags, like
  443. * subst but works on the message uri */
  444. static int subst_uri_f(struct sip_msg* msg, char* subst, char* ignored)
  445. {
  446. char* tmp;
  447. int len;
  448. char c;
  449. struct subst_expr* se;
  450. str* result;
  451. se=(struct subst_expr*)subst;
  452. if (msg->new_uri.s){
  453. len=msg->new_uri.len;
  454. tmp=msg->new_uri.s;
  455. }else{
  456. tmp=msg->first_line.u.request.uri.s;
  457. len =msg->first_line.u.request.uri.len;
  458. };
  459. /* ugly hack: 0 s[len], and restore it afterward
  460. * (our re functions require 0 term strings), we can do this
  461. * because we always alloc len+1 (new_uri) and for first_line, the
  462. * message will always be > uri.len */
  463. c=tmp[len];
  464. tmp[len]=0;
  465. result=subst_str(tmp, msg, se, 0); /* pkg malloc'ed result */
  466. tmp[len]=c;
  467. if (result){
  468. DBG("%s: subst_uri_f: match - old uri= [%.*s], new uri= [%.*s]\n",
  469. exports.name, len, tmp,
  470. (result->len)?result->len:0,(result->s)?result->s:"");
  471. if (msg->new_uri.s) pkg_free(msg->new_uri.s);
  472. msg->new_uri=*result;
  473. msg->parsed_uri_ok=0; /* reset "use cached parsed uri" flag */
  474. ruri_mark_new();
  475. pkg_free(result); /* free str* pointer */
  476. return 1; /* success */
  477. }
  478. return -1; /* false, no subst. made */
  479. }
  480. /* sed-perl style re: s/regular expression/replacement/flags, like
  481. * subst but works on the user part of the uri */
  482. static int subst_user_f(struct sip_msg* msg, char* subst, char* ignored)
  483. {
  484. int rval;
  485. str* result;
  486. struct subst_expr* se;
  487. struct action act;
  488. str user;
  489. char c;
  490. int nmatches;
  491. struct run_act_ctx ra_ctx;
  492. c=0;
  493. if (parse_sip_msg_uri(msg)<0){
  494. return -1; /* error, bad uri */
  495. }
  496. if (msg->parsed_uri.user.s==0){
  497. /* no user in uri */
  498. user.s="";
  499. user.len=0;
  500. }else{
  501. user=msg->parsed_uri.user;
  502. c=user.s[user.len];
  503. user.s[user.len]=0;
  504. }
  505. se=(struct subst_expr*)subst;
  506. result=subst_str(user.s, msg, se, &nmatches);/* pkg malloc'ed result */
  507. if (c) user.s[user.len]=c;
  508. if (result == NULL) {
  509. if (nmatches<0)
  510. LOG(L_ERR, "subst_user(): subst_str() failed\n");
  511. return -1;
  512. }
  513. /* result->s[result->len] = '\0'; --subst_str returns 0-term strings */
  514. memset(&act, 0, sizeof(act)); /* be on the safe side */
  515. act.type = SET_USER_T;
  516. act.val[0].type = STRING_ST;
  517. act.val[0].u.string = result->s;
  518. init_run_actions_ctx(&ra_ctx);
  519. rval = do_action(&ra_ctx, &act, msg);
  520. if (result->s) pkg_free(result->s); /* SET_USER_T doesn't consume s */
  521. pkg_free(result);
  522. return rval;
  523. }
  524. static inline int remove_hf(struct sip_msg* msg, char* p1, int by_re)
  525. {
  526. struct hdr_field *hf;
  527. struct lump* l;
  528. int cnt;
  529. str hfn;
  530. regex_t regexp;
  531. regmatch_t matches;
  532. char bkup;
  533. int no_match;
  534. if (by_re) {
  535. if (get_regex_fparam(&regexp, msg, (fparam_t*)p1) < 0) {
  536. ERR("remove_hf: Error while obtaining parameter value\n");
  537. return -1;
  538. }
  539. } else {
  540. if (get_str_fparam(&hfn, msg, (fparam_t*)p1) < 0) {
  541. ERR("remove_hf: Error while obtaining parameter value\n");
  542. return -1;
  543. }
  544. }
  545. cnt=0;
  546. /* we need to be sure we have seen all HFs */
  547. parse_headers(msg, HDR_EOH_F, 0);
  548. for (hf=msg->headers; hf; hf=hf->next) {
  549. if (by_re) {
  550. /* 0-term. it for regexp run; should always be a safe op. */
  551. bkup = hf->name.s[hf->name.len];
  552. hf->name.s[hf->name.len] = 0;
  553. no_match = regexec(&regexp, hf->name.s, /*# matches */1, &matches,
  554. /*flags*/0);
  555. hf->name.s[hf->name.len] = bkup;
  556. if (no_match)
  557. continue;
  558. } else {
  559. if (hf->name.len!=hfn.len)
  560. continue;
  561. if (strncasecmp(hf->name.s, hfn.s, hf->name.len)!=0)
  562. continue;
  563. }
  564. l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
  565. if (l==0) {
  566. ERR("no memory\n");
  567. return -1;
  568. }
  569. cnt++;
  570. }
  571. return cnt==0 ? -1 : 1;
  572. }
  573. static int remove_hf_f(struct sip_msg* msg, char* p1, char* foo)
  574. {
  575. return remove_hf(msg, p1, /*use string comparison*/0);
  576. }
  577. static int remove_hf_re_f(struct sip_msg* msg, char* p1, char* foo)
  578. {
  579. return remove_hf(msg, p1, /*use regexp comparison*/1);
  580. }
  581. static int is_present_hf_f(struct sip_msg* msg, char* p1, char* foo)
  582. {
  583. struct hdr_field *hf;
  584. str hfn;
  585. if (get_str_fparam(&hfn, msg, (fparam_t*)p1) < 0) {
  586. ERR("is_present_hf: Error while obtaining parameter value\n");
  587. return -1;
  588. }
  589. /* we need to be sure we have seen all HFs */
  590. parse_headers(msg, HDR_EOH_F, 0);
  591. for (hf=msg->headers; hf; hf=hf->next) {
  592. if (hf->name.len!=hfn.len)
  593. continue;
  594. if (strncasecmp(hf->name.s, hfn.s, hf->name.len)!=0)
  595. continue;
  596. return 1;
  597. }
  598. return -1;
  599. }
  600. static int fixup_substre(void** param, int param_no)
  601. {
  602. struct subst_expr* se;
  603. str subst;
  604. DBG("%s module -- fixing %s\n", exports.name, (char*)(*param));
  605. if (param_no!=1) return 0;
  606. subst.s=*param;
  607. subst.len=strlen(*param);
  608. se=subst_parser(&subst);
  609. if (se==0){
  610. LOG(L_ERR, "ERROR: %s: bad subst. re %s\n", exports.name,
  611. (char*)*param);
  612. return E_BAD_RE;
  613. }
  614. /* free string */
  615. pkg_free(*param);
  616. /* replace it with the compiled subst. re */
  617. *param=se;
  618. return 0;
  619. }
  620. static int append_time_f(struct sip_msg* msg, char* p1, char *p2)
  621. {
  622. size_t len;
  623. char time_str[MAX_TIME];
  624. time_t now;
  625. struct tm *bd_time;
  626. now=ser_time(0);
  627. bd_time=gmtime(&now);
  628. if (bd_time==NULL) {
  629. LOG(L_ERR, "ERROR: append_time: gmtime failed\n");
  630. return -1;
  631. }
  632. len=strftime(time_str, MAX_TIME, TIME_FORMAT, bd_time);
  633. if (len>MAX_TIME-2 || len==0) {
  634. LOG(L_ERR, "ERROR: append_time: unexpected time length\n");
  635. return -1;
  636. }
  637. time_str[len]='\r';
  638. time_str[len+1]='\n';
  639. if (add_lump_rpl(msg, time_str, len+2, LUMP_RPL_HDR)==0)
  640. {
  641. LOG(L_ERR, "ERROR: append_time: unable to add lump\n");
  642. return -1;
  643. }
  644. return 1;
  645. }
  646. static int append_to_reply_f(struct sip_msg* msg, char* _str, char* dummy)
  647. {
  648. str str;
  649. if (eval_xlstr(msg, (void*) _str, &str) < 0) return -1;
  650. if ( add_lump_rpl( msg, str.s, str.len, LUMP_RPL_HDR)==0 )
  651. {
  652. LOG(L_ERR,"ERROR:append_to_reply : unable to add lump_rl\n");
  653. return -1;
  654. }
  655. return 1;
  656. }
  657. /* add str1 to end of header or str1.r-uri.str2 */
  658. static int append_hf_helper(struct sip_msg* msg, struct xlstr *_str1, struct xlstr *_str2)
  659. {
  660. struct lump* anchor;
  661. char *s;
  662. str str1, str2;
  663. int len;
  664. if (eval_xlstr(msg, _str1, &str1) < 0) return -1;
  665. if (_str2) {
  666. if (eval_xlstr(msg, _str2, &str2) < 0) return -1;
  667. }
  668. if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
  669. LOG(L_ERR, "append_hf(): Error while parsing message\n");
  670. return -1;
  671. }
  672. anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
  673. if (anchor == 0) {
  674. LOG(L_ERR, "append_hf(): Can't get anchor\n");
  675. return -1;
  676. }
  677. len=str1.len;
  678. if (_str2) len+= str2.len + REQ_LINE(msg).uri.len;
  679. s = (char*)pkg_malloc(len);
  680. if (!s) {
  681. LOG(L_ERR, "append_hf(): No memory left\n");
  682. return -1;
  683. }
  684. memcpy(s, str1.s, str1.len);
  685. if (_str2) {
  686. memcpy(s+str1.len, REQ_LINE(msg).uri.s, REQ_LINE(msg).uri.len);
  687. memcpy(s+str1.len+REQ_LINE(msg).uri.len, str2.s, str2.len );
  688. }
  689. if (insert_new_lump_before(anchor, s, len, 0) == 0) {
  690. LOG(L_ERR, "append_hf(): Can't insert lump\n");
  691. pkg_free(s);
  692. return -1;
  693. }
  694. return 1;
  695. }
  696. static int append_hf(struct sip_msg *msg, char *str1, char *str2 )
  697. {
  698. return append_hf_helper(msg, (struct xlstr *) str1, (struct xlstr *) 0);
  699. }
  700. static int append_urihf(struct sip_msg *msg, char *str1, char *str2 )
  701. {
  702. return append_hf_helper(msg, (struct xlstr *) str1, (struct xlstr *) str2);
  703. }
  704. #define HNF_ALL 0x01
  705. #define HNF_IDX 0x02
  706. #define MAX_HF_VALUE_STACK 10
  707. enum {hnoInsert, hnoAppend, hnoAssign, hnoRemove, hnoInclude, hnoExclude, hnoIsIncluded, hnoGetValue, hnoGetValueUri, hnoGetValueName, hnoRemove2, hnoAssign2, hnoGetValue2};
  708. struct hname_data {
  709. int oper;
  710. int htype;
  711. str hname;
  712. int flags;
  713. int idx;
  714. str param;
  715. };
  716. #define is_space(_p) ((_p) == '\t' || (_p) == '\n' || (_p) == '\r' || (_p) == ' ')
  717. #define eat_spaces(_p) \
  718. while( is_space(*(_p)) ){\
  719. (_p)++;}
  720. #define is_alphanum(_p) (((_p) >= 'a' && (_p) <= 'z') || ((_p) >= 'A' && (_p) <= 'Z') || ((_p) >= '0' && (_p) <= '9') || (_p) == '_' || (_p) == '-')
  721. #define eat_while_alphanum(_p) \
  722. while ( is_alphanum(*(_p)) ) {\
  723. (_p)++; }
  724. /* parse: hname [ ([] | [*] | [number]) ] [ "." param ] */
  725. static int fixup_hname_param(char *hname, struct hname_data** h) {
  726. struct hdr_field hdr;
  727. char *savep, savec;
  728. *h = pkg_malloc(sizeof(**h));
  729. if (!*h) return E_OUT_OF_MEM;
  730. memset(*h, 0, sizeof(**h));
  731. memset(&hdr, 0, sizeof(hdr));
  732. eat_spaces(hname);
  733. (*h)->hname.s = hname;
  734. savep = hname;
  735. eat_while_alphanum(hname);
  736. (*h)->hname.len = hname - (*h)->hname.s;
  737. savec = *hname;
  738. *hname = ':';
  739. parse_hname2((*h)->hname.s, (*h)->hname.s+(*h)->hname.len+3, &hdr);
  740. *hname = savec;
  741. if (hdr.type == HDR_ERROR_T) goto err;
  742. (*h)->htype = hdr.type;
  743. eat_spaces(hname);
  744. savep = hname;
  745. if (*hname == '[') {
  746. hname++;
  747. eat_spaces(hname);
  748. savep = hname;
  749. (*h)->flags |= HNF_IDX;
  750. if (*hname == '*') {
  751. (*h)->flags |= HNF_ALL;
  752. hname++;
  753. }
  754. else if (*hname != ']') {
  755. char* c;
  756. (*h)->idx = strtol(hname, &c, 10);
  757. if (hname == c) goto err;
  758. hname = c;
  759. }
  760. eat_spaces(hname);
  761. savep = hname;
  762. if (*hname != ']') goto err;
  763. hname++;
  764. }
  765. eat_spaces(hname);
  766. savep = hname;
  767. if (*hname == '.') {
  768. hname++;
  769. eat_spaces(hname);
  770. savep = hname;
  771. (*h)->param.s = hname;
  772. eat_while_alphanum(hname);
  773. (*h)->param.len = hname-(*h)->param.s;
  774. if ((*h)->param.len == 0) goto err;
  775. }
  776. else {
  777. (*h)->param.s = hname;
  778. }
  779. savep = hname;
  780. if (*hname != '\0') goto err;
  781. (*h)->hname.s[(*h)->hname.len] = '\0';
  782. (*h)->param.s[(*h)->param.len] = '\0';
  783. return 0;
  784. err:
  785. pkg_free(*h);
  786. LOG(L_ERR, "ERROR: textops: cannot parse header near '%s'\n", savep);
  787. return E_CFG;
  788. }
  789. static int fixup_hname_str(void** param, int param_no) {
  790. if (param_no == 1) {
  791. struct hname_data* h;
  792. int res = fixup_hname_param(*param, &h);
  793. if (res < 0) return res;
  794. *param = h;
  795. }
  796. else if (param_no == 2) {
  797. return fixup_xlstr(param, param_no);
  798. }
  799. return 0;
  800. }
  801. static int find_next_hf(struct sip_msg* msg, struct hname_data* hname, struct hdr_field** hf) {
  802. if (!*hf) {
  803. if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
  804. LOG(L_ERR, "ERROR: textops: find_next_hf: Error while parsing message\n");
  805. return -1;
  806. }
  807. *hf = msg->headers;
  808. }
  809. else {
  810. *hf = (*hf)->next;
  811. }
  812. for (; *hf; *hf = (*hf)->next) {
  813. if (hname->htype == HDR_OTHER_T) {
  814. if ((*hf)->name.len==hname->hname.len && strncasecmp((*hf)->name.s, hname->hname.s, (*hf)->name.len)==0)
  815. return 1;
  816. }
  817. else if (hname->htype == (*hf)->type) {
  818. return 1;
  819. }
  820. }
  821. return 0;
  822. }
  823. static int find_next_value(char** start, char* end, str* val, str* lump_val) {
  824. int quoted = 0;
  825. lump_val->s = *start;
  826. while (*start < end && is_space(**start) ) (*start)++;
  827. val->s = *start;
  828. while ( *start < end && (**start != ',' || quoted) ) {
  829. if (**start == '\"' && (!quoted || (*start)[-1]!='\\') )
  830. quoted = ~quoted;
  831. (*start)++;
  832. }
  833. val->len = *start - val->s;
  834. while (val->len > 0 && is_space(val->s[val->len-1])) val->len--;
  835. /* we cannot automatically strip quotes!!! an example why: "name" <sip:ssss>;param="bar"
  836. if (val->len >= 2 && val->s[0] == '\"' && val->s[val->len-1] == '\"') {
  837. val->s++;
  838. val->len -= 2;
  839. }
  840. */
  841. while (*start < end && **start != ',') (*start)++;
  842. if (*start < end) {
  843. (*start)++;
  844. }
  845. lump_val->len = *start - lump_val->s;
  846. return (*start < end);
  847. }
  848. static void adjust_lump_val_for_delete(struct hdr_field* hf, str* lump_val) {
  849. if ( lump_val->s+lump_val->len == hf->body.s+hf->body.len ) {
  850. if (lump_val->s > hf->body.s) {
  851. /* in case if is it last value in header save position of last delimiter to remove it with rightmost value */
  852. lump_val->s--;
  853. lump_val->len++;
  854. }
  855. }
  856. }
  857. static int find_hf_value_idx(struct sip_msg* msg, struct hname_data* hname, struct hdr_field** hf, str* val, str* lump_val) {
  858. int res;
  859. char *p;
  860. if ( hname->flags & HNF_ALL || hname->idx == 0) return -1;
  861. *hf = 0;
  862. if (hname->idx > 0) {
  863. int idx;
  864. idx = hname->idx;
  865. do {
  866. res = find_next_hf(msg, hname, hf);
  867. if (res < 0) return -1;
  868. if (*hf) {
  869. if (val) {
  870. lump_val->len = 0;
  871. p = (*hf)->body.s;
  872. do {
  873. res = find_next_value(&p, (*hf)->body.s+(*hf)->body.len, val, lump_val);
  874. idx--;
  875. } while (res && idx);
  876. }
  877. else {
  878. idx--;
  879. }
  880. }
  881. } while (*hf && idx);
  882. }
  883. else if (hname->idx < 0) { /* search from the bottom */
  884. struct hf_value_stack {
  885. str val, lump_val;
  886. struct hdr_field* hf;
  887. } stack[MAX_HF_VALUE_STACK];
  888. int stack_pos, stack_num;
  889. if ( -hname->idx > MAX_HF_VALUE_STACK ) return -1;
  890. stack_pos = stack_num = 0;
  891. do {
  892. res = find_next_hf(msg, hname, hf);
  893. if (res < 0) return -1;
  894. if (*hf) {
  895. stack[stack_pos].lump_val.len = 0;
  896. p = (*hf)->body.s;
  897. do {
  898. stack[stack_pos].hf = *hf;
  899. if (val)
  900. res = find_next_value(&p, (*hf)->body.s+(*hf)->body.len, &stack[stack_pos].val, &stack[stack_pos].lump_val);
  901. else
  902. res = 0;
  903. stack_pos++;
  904. if (stack_pos >= MAX_HF_VALUE_STACK)
  905. stack_pos = 0;
  906. if (stack_num < MAX_HF_VALUE_STACK)
  907. stack_num++;
  908. } while (res);
  909. }
  910. } while (*hf);
  911. if (-hname->idx <= stack_num) {
  912. stack_pos += hname->idx;
  913. if (stack_pos < 0)
  914. stack_pos += MAX_HF_VALUE_STACK;
  915. *hf = stack[stack_pos].hf;
  916. if (val) {
  917. *val = stack[stack_pos].val;
  918. *lump_val = stack[stack_pos].lump_val;
  919. }
  920. }
  921. else {
  922. *hf = 0;
  923. }
  924. }
  925. else
  926. return -1;
  927. return *hf?1:0;
  928. }
  929. static int find_hf_value_param(struct hname_data* hname, str* param_area, str* value, str* lump_upd, str* lump_del) {
  930. int i, j, found;
  931. i = 0;
  932. while (1) {
  933. lump_del->s = param_area->s + i;
  934. for (; i < param_area->len && is_space(param_area->s[i]); i++);
  935. if (i < param_area->len && param_area->s[i] == ';') { /* found a param ? */
  936. i++;
  937. for (; i < param_area->len && is_space(param_area->s[i]); i++);
  938. j = i;
  939. for (; i < param_area->len && !is_space(param_area->s[i]) && param_area->s[i]!='=' && param_area->s[i]!=';'; i++);
  940. found = hname->param.len == i-j && !strncasecmp(hname->param.s, param_area->s+j, i-j);
  941. lump_upd->s = param_area->s+i;
  942. value->s = param_area->s+i;
  943. value->len = 0;
  944. for (; i < param_area->len && is_space(param_area->s[i]); i++);
  945. if (i < param_area->len && param_area->s[i]=='=') {
  946. i++;
  947. for (; i < param_area->len && is_space(param_area->s[i]); i++);
  948. value->s = param_area->s+i;
  949. if (i < param_area->len) {
  950. if (param_area->s[i]=='\"') {
  951. i++;
  952. value->s++;
  953. for (; i<param_area->len; i++) {
  954. if (param_area->s[i]=='\"') {
  955. i++;
  956. break;
  957. }
  958. value->len++;
  959. }
  960. }
  961. else {
  962. for (; i<param_area->len && !is_space(param_area->s[i]) && param_area->s[i]!=';'; i++, value->len++);
  963. }
  964. }
  965. }
  966. if (found) {
  967. lump_del->len = param_area->s+i - lump_del->s;
  968. lump_upd->len = param_area->s+i - lump_upd->s;
  969. return 1;
  970. }
  971. }
  972. else { /* not found, return last correct position, should be end of param area */
  973. lump_del->len = 0;
  974. return 0;
  975. }
  976. }
  977. }
  978. /* parse: something param_name=param_value something [ "," something param_name="param_value" ....]
  979. * 'something' is required by Authenticate
  980. */
  981. static int find_hf_value2_param(struct hname_data* hname, str* param_area, str* value, str* lump_upd, str* lump_del, char* delim) {
  982. int i, j, k, found, comma_flag;
  983. i = 0;
  984. *delim = 0;
  985. lump_del->len = 0;
  986. while (i < param_area->len) {
  987. lump_del->s = param_area->s + i;
  988. while (i<param_area->len && is_space(param_area->s[i])) i++;
  989. comma_flag = i < param_area->len && param_area->s[i] == ',';
  990. if (comma_flag) i++;
  991. while (i<param_area->len && is_space(param_area->s[i])) i++;
  992. if (i < param_area->len && is_alphanum(param_area->s[i])) { /* found a param name ? */
  993. j = i;
  994. if (!*delim) *delim = ' ';
  995. while (i<param_area->len && is_alphanum(param_area->s[i])) i++;
  996. k = i;
  997. while (i<param_area->len && is_space(param_area->s[i])) i++;
  998. lump_upd->s = param_area->s + i;
  999. if (i < param_area->len && param_area->s[i] == '=') { /* if equal then it's the param */
  1000. *delim = ',';
  1001. i++;
  1002. found = hname->param.len == k-j && !strncasecmp(hname->param.s, param_area->s+j, k-j);
  1003. while (i<param_area->len && is_space(param_area->s[i])) i++;
  1004. value->s = param_area->s+i;
  1005. value->len = 0;
  1006. if (i < param_area->len) {
  1007. if (param_area->s[i]=='\"') {
  1008. i++;
  1009. value->s++;
  1010. for (; i<param_area->len; i++) {
  1011. if (param_area->s[i]=='\"') {
  1012. i++;
  1013. break;
  1014. }
  1015. value->len++;
  1016. }
  1017. }
  1018. else {
  1019. for (; i<param_area->len && !is_space(param_area->s[i]) && param_area->s[i]!=','; i++, value->len++);
  1020. }
  1021. }
  1022. if (found) {
  1023. lump_upd->len = param_area->s+i - lump_upd->s;
  1024. lump_del->len = param_area->s+i - lump_del->s;
  1025. while (i<param_area->len && is_space(param_area->s[i])) i++;
  1026. if (!comma_flag && i < param_area->len && param_area->s[i]==',') {
  1027. i++;
  1028. lump_del->len = param_area->s+i - lump_del->s;
  1029. }
  1030. return 1;
  1031. }
  1032. }
  1033. while (i<param_area->len && is_space(param_area->s[i])) i++;
  1034. }
  1035. else {
  1036. while (i<param_area->len && !is_space(param_area->s[i]) && !param_area->s[i]!=',') i++;
  1037. }
  1038. }
  1039. lump_del->s = param_area->s + i;
  1040. return 0;
  1041. }
  1042. static int insert_header_lump(struct sip_msg* msg, char* msg_position, int lump_before, str* hname, str *val) {
  1043. struct lump* anchor;
  1044. char *s;
  1045. int len;
  1046. anchor = anchor_lump(msg, msg_position - msg->buf, 0, 0);
  1047. if (anchor == 0) {
  1048. LOG(L_ERR, "ERROR: textops: insert_header_lump(): Can't get anchor\n");
  1049. return -1;
  1050. }
  1051. len=hname->len+2+val->len+2;
  1052. s = (char*)pkg_malloc(len);
  1053. if (!s) {
  1054. LOG(L_ERR, "ERROR: textops: insert_header_lump(): not enough memory\n");
  1055. return -1;
  1056. }
  1057. memcpy(s, hname->s, hname->len);
  1058. s[hname->len] = ':';
  1059. s[hname->len+1] = ' ';
  1060. memcpy(s+hname->len+2, val->s, val->len);
  1061. s[hname->len+2+val->len] = '\r';
  1062. s[hname->len+2+val->len+1] = '\n';
  1063. if ( (lump_before?insert_new_lump_before(anchor, s, len, 0):insert_new_lump_after(anchor, s, len, 0)) == 0) {
  1064. LOG(L_ERR, "ERROR: textops: insert_header_lump(): Can't insert lump\n");
  1065. pkg_free(s);
  1066. return -1;
  1067. }
  1068. return 1;
  1069. }
  1070. static int insert_value_lump(struct sip_msg* msg, struct hdr_field* hf, char* msg_position, int lump_before, str *val) {
  1071. struct lump* anchor;
  1072. char *s;
  1073. int len;
  1074. anchor = anchor_lump(msg, msg_position - msg->buf, 0, 0);
  1075. if (anchor == 0) {
  1076. LOG(L_ERR, "ERROR: textops: insert_value_lump(): Can't get anchor\n");
  1077. return -1;
  1078. }
  1079. len=val->len+1;
  1080. s = (char*)pkg_malloc(len);
  1081. if (!s) {
  1082. LOG(L_ERR, "ERROR: textops: insert_value_lump(): not enough memory\n");
  1083. return -1;
  1084. }
  1085. if (!hf) {
  1086. memcpy(s, val->s, val->len);
  1087. len--;
  1088. }
  1089. else if (msg_position == hf->body.s+hf->body.len) {
  1090. s[0] = ',';
  1091. memcpy(s+1, val->s, val->len);
  1092. }
  1093. else {
  1094. memcpy(s, val->s, val->len);
  1095. s[val->len] = ',';
  1096. }
  1097. if ( (lump_before?insert_new_lump_before(anchor, s, len, 0):insert_new_lump_after(anchor, s, len, 0)) == 0) {
  1098. LOG(L_ERR, "ERROR: textops: insert_value_lump(): Can't insert lump\n");
  1099. pkg_free(s);
  1100. return -1;
  1101. }
  1102. return 1;
  1103. }
  1104. static int delete_value_lump(struct sip_msg* msg, struct hdr_field* hf, str *val) {
  1105. struct lump* l;
  1106. /* TODO: check already existing lumps */
  1107. if (hf && val->s == hf->body.s && val->len == hf->body.len) /* check if remove whole haeder? */
  1108. l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
  1109. else
  1110. l=del_lump(msg, val->s-msg->buf, val->len, 0);
  1111. if (l==0) {
  1112. LOG(L_ERR, "ERROR: textops: delete_value_lump: not enough memory\n");
  1113. return -1;
  1114. }
  1115. return 1;
  1116. }
  1117. static int incexc_hf_value_f(struct sip_msg* msg, char* _hname, char* _val) {
  1118. struct hname_data* hname = (void*) _hname;
  1119. struct hdr_field* hf, *lump_hf;
  1120. str val, hval1, hval2;
  1121. char *p;
  1122. int res = eval_xlstr(msg, (void*) _val, &val);
  1123. if (res < 0) return res;
  1124. if (!val.len) return -1;
  1125. hf = 0;
  1126. lump_hf = 0;
  1127. while (1) {
  1128. if (find_next_hf(msg, hname, &hf) < 0) return -1;
  1129. if (!hf) break;
  1130. hval2.len = 0;
  1131. p = hf->body.s;
  1132. do {
  1133. res = find_next_value(&p, hf->body.s+hf->body.len, &hval1, &hval2);
  1134. if (hval1.len && val.len == hval1.len && strncasecmp(val.s, hval1.s, val.len) == 0) {
  1135. switch (hname->oper) {
  1136. case hnoIsIncluded:
  1137. case hnoInclude:
  1138. return 1;
  1139. case hnoExclude:
  1140. adjust_lump_val_for_delete(hf, &hval2);
  1141. delete_value_lump(msg, hf, &hval2);
  1142. default:
  1143. break;
  1144. }
  1145. }
  1146. } while (res);
  1147. switch (hname->oper) {
  1148. case hnoInclude:
  1149. if (!lump_hf) {
  1150. lump_hf = hf;
  1151. }
  1152. break;
  1153. default:
  1154. break;
  1155. }
  1156. }
  1157. switch (hname->oper) {
  1158. case hnoIsIncluded:
  1159. return -1;
  1160. case hnoInclude:
  1161. if (lump_hf)
  1162. return insert_value_lump(msg, lump_hf, lump_hf->body.s+lump_hf->body.len, 1, &val);
  1163. else
  1164. return insert_header_lump(msg, msg->unparsed, 1, &hname->hname, &val);
  1165. default:
  1166. return 1;
  1167. }
  1168. }
  1169. #define INCEXC_HF_VALUE_FIXUP(_func,_oper) \
  1170. static int _func (void** param, int param_no) {\
  1171. char* p = *param; \
  1172. int res=fixup_hname_str(param, param_no); \
  1173. if (res < 0) return res; \
  1174. if (param_no == 1) {\
  1175. if ( ((struct hname_data*)*param)->flags & HNF_IDX || ((struct hname_data*)*param)->param.len ) { \
  1176. LOG(L_ERR, "ERROR: textops: neither index nor param may be specified in '%s'\n", p);\
  1177. return E_CFG;\
  1178. }\
  1179. ((struct hname_data*)*param)->oper = _oper;\
  1180. }\
  1181. return 0;\
  1182. }
  1183. INCEXC_HF_VALUE_FIXUP(include_hf_value_fixup, hnoInclude)
  1184. INCEXC_HF_VALUE_FIXUP(exclude_hf_value_fixup, hnoExclude)
  1185. INCEXC_HF_VALUE_FIXUP(hf_value_exists_fixup, hnoIsIncluded)
  1186. static void get_uri_and_skip_until_params(str *param_area, str *name, str *uri) {
  1187. int i, quoted, uri_pos, uri_done;
  1188. name->len = 0;
  1189. uri->len = 0;
  1190. uri_done = 0;
  1191. name->s = param_area->s;
  1192. for (i=0; i<param_area->len && param_area->s[i]!=';'; ) { /* [ *(token LSW)/quoted-string ] "<" addr-spec ">" | addr-spec */
  1193. /* skip name */
  1194. for (quoted=0, uri_pos=i; i<param_area->len; i++) {
  1195. if (!quoted) {
  1196. if (param_area->s[i] == '\"') {
  1197. quoted = 1;
  1198. uri_pos = -1;
  1199. }
  1200. else if (param_area->s[i] == '<' || param_area->s[i] == ';' || is_space(param_area->s[i])) break;
  1201. }
  1202. else if (param_area->s[i] == '\"' && param_area->s[i-1] != '\\') quoted = 0;
  1203. }
  1204. if (!name->len)
  1205. name->len = param_area->s+i-name->s;
  1206. if (uri_pos >= 0 && !uri_done) {
  1207. uri->s = param_area->s+uri_pos;
  1208. uri->len = param_area->s+i-uri->s;
  1209. }
  1210. /* skip uri */
  1211. while (i<param_area->len && is_space(param_area->s[i])) i++;
  1212. if (i<param_area->len && param_area->s[i]=='<') {
  1213. uri->s = param_area->s+i;
  1214. uri->len = 0;
  1215. for (quoted=0; i<param_area->len; i++) {
  1216. if (!quoted) {
  1217. if (param_area->s[i] == '\"') quoted = 1;
  1218. else if (param_area->s[i] == '>') {
  1219. uri->len = param_area->s+i-uri->s+1;
  1220. uri_done = 1;
  1221. break;
  1222. }
  1223. }
  1224. else if (param_area->s[i] == '\"' && param_area->s[i-1] != '\\') quoted = 0;
  1225. }
  1226. }
  1227. }
  1228. param_area->s+= i;
  1229. param_area->len-= i;
  1230. if (uri->s == name->s)
  1231. name->len = 0;
  1232. }
  1233. static int assign_hf_do_lumping(struct sip_msg* msg,struct hdr_field* hf, struct hname_data* hname, str* value, int upd_del_fl, str* lump_upd, str* lump_del, char delim) {
  1234. int len, i;
  1235. char *s;
  1236. struct lump* anchor;
  1237. if (upd_del_fl) {
  1238. len = value?lump_upd->len:lump_del->len;
  1239. if (len > 0) {
  1240. if (!del_lump(msg, (value?lump_upd->s:lump_del->s)-msg->buf, len, 0)) {
  1241. LOG(L_ERR, "ERROR: textops: assign_hf_do_lumping: not enough memory\n");
  1242. return -1;
  1243. }
  1244. }
  1245. if (value && value->len) {
  1246. anchor = anchor_lump(msg, lump_upd->s - msg->buf, 0, 0);
  1247. if (anchor == 0) {
  1248. LOG(L_ERR, "ERROR: textops: assign_hf_do_lumping: Can't get anchor\n");
  1249. return -1;
  1250. }
  1251. len = 1+value->len;
  1252. s = pkg_malloc(len);
  1253. if (!s) {
  1254. LOG(L_ERR, "ERROR: textops: assign_hf_do_lumping: not enough memory\n");
  1255. return -1;
  1256. }
  1257. s[0]='=';
  1258. memcpy(s+1, value->s, value->len);
  1259. if ( (insert_new_lump_before(anchor, s, len, 0)) == 0) {
  1260. LOG(L_ERR, "ERROR: textops: assign_hf_do_lumping: Can't insert lump\n");
  1261. pkg_free(s);
  1262. return -1;
  1263. }
  1264. }
  1265. }
  1266. else {
  1267. if (!value) return -1;
  1268. anchor = anchor_lump(msg, lump_del->s - msg->buf, 0, 0);
  1269. if (anchor == 0) {
  1270. LOG(L_ERR, "ERROR: textops: assign_hf_do_lumping: Can't get anchor\n");
  1271. return -1;
  1272. }
  1273. len = 1+hname->param.len+(value->len?value->len+1:0);
  1274. s = pkg_malloc(len);
  1275. if (!s) {
  1276. LOG(L_ERR, "ERROR: textops: assign_hf_do_lumping: not enough memory\n");
  1277. return -1;
  1278. }
  1279. if (delim) {
  1280. s[0] = delim;
  1281. i = 1;
  1282. }
  1283. else {
  1284. i = 0;
  1285. len--;
  1286. }
  1287. memcpy(s+i, hname->param.s, hname->param.len);
  1288. if (value->len) {
  1289. s[hname->param.len+i]='=';
  1290. memcpy(s+i+hname->param.len+1, value->s, value->len);
  1291. }
  1292. if ( (insert_new_lump_before(anchor, s, len, 0)) == 0) {
  1293. LOG(L_ERR, "ERROR: textops: assign_hf_do_lumping: Can't insert lump\n");
  1294. pkg_free(s);
  1295. return -1;
  1296. }
  1297. }
  1298. return 1;
  1299. }
  1300. static int assign_hf_process_params(struct sip_msg* msg, struct hdr_field* hf, struct hname_data* hname, str* value, str* value_area) {
  1301. int r, r2, res=0;
  1302. str param_area, lump_upd, lump_del, dummy_val, dummy_name, dummy_uri;
  1303. param_area = *value_area;
  1304. get_uri_and_skip_until_params(&param_area, &dummy_name, &dummy_uri);
  1305. do {
  1306. r = find_hf_value_param(hname, &param_area, &dummy_val, &lump_upd, &lump_del);
  1307. r2 = assign_hf_do_lumping(msg, hf, hname, value, r, &lump_upd, &lump_del, ';');
  1308. if (res == 0)
  1309. res = r2;
  1310. if (r && !value) { /* remove all parameters */
  1311. param_area.len -= lump_del.s+lump_del.len-param_area.s;
  1312. param_area.s = lump_del.s+lump_del.len;
  1313. }
  1314. } while (!value && r);
  1315. return res;
  1316. }
  1317. static int assign_hf_process2_params(struct sip_msg* msg, struct hdr_field* hf, struct hname_data* hname, str* value) {
  1318. int r, r2, res = 0;
  1319. str param_area, lump_upd, lump_del, dummy_val;
  1320. char delim;
  1321. param_area = hf->body;
  1322. do {
  1323. r = find_hf_value2_param(hname, &param_area, &dummy_val, &lump_upd, &lump_del, &delim);
  1324. r2 = assign_hf_do_lumping(msg, hf, hname, value, r, &lump_upd, &lump_del, delim);
  1325. if (res == 0)
  1326. res = r2;
  1327. if (r && !value) { /* remove all parameters */
  1328. param_area.len -= lump_del.s+lump_del.len-param_area.s;
  1329. param_area.s = lump_del.s+lump_del.len;
  1330. }
  1331. } while (!value && r);
  1332. return res;
  1333. }
  1334. static int insupddel_hf_value_f(struct sip_msg* msg, char* _hname, char* _val) {
  1335. struct hname_data* hname = (void*) _hname;
  1336. struct hdr_field* hf;
  1337. str val, hval1, hval2;
  1338. int res;
  1339. if (_val) {
  1340. res = eval_xlstr(msg, (void*) _val, &val);
  1341. if (res < 0) return res;
  1342. }
  1343. switch (hname->oper) {
  1344. case hnoAppend:
  1345. if ((hname->flags & HNF_IDX) == 0) {
  1346. if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
  1347. LOG(L_ERR, "ERROR: textops: Error while parsing message\n");
  1348. return -1;
  1349. }
  1350. return insert_header_lump(msg, msg->unparsed, 1, &hname->hname, &val);
  1351. }
  1352. else {
  1353. res = find_hf_value_idx(msg, hname, &hf, &hval1, &hval2);
  1354. if (res < 0) return res;
  1355. if (hf) {
  1356. return insert_value_lump(msg, hf, hval2.s+hval2.len, res /* insert after, except it is last value in header */, &val);
  1357. }
  1358. else {
  1359. return insert_header_lump(msg, msg->unparsed, 1, &hname->hname, &val);
  1360. }
  1361. }
  1362. case hnoInsert:
  1363. /* if !HNF_IDX is possible parse only until first hname header but not trivial for HDR_OTHER_T header, not implemented */
  1364. res = find_hf_value_idx(msg, hname, &hf, &hval1, &hval2);
  1365. if (res < 0) return res;
  1366. if (hf && (hname->flags & HNF_IDX) == 0) {
  1367. return insert_header_lump(msg, hf->name.s, 1, &hname->hname, &val);
  1368. }
  1369. else if (!hf && hname->idx == 1) {
  1370. return insert_header_lump(msg, msg->unparsed, 1, &hname->hname, &val);
  1371. }
  1372. else if (hf) {
  1373. return insert_value_lump(msg, hf, hval2.s, 1, &val);
  1374. }
  1375. else
  1376. return -1;
  1377. case hnoRemove:
  1378. case hnoAssign:
  1379. if (hname->flags & HNF_ALL) {
  1380. struct hdr_field* hf = 0;
  1381. int fl = -1;
  1382. do {
  1383. res = find_next_hf(msg, hname, &hf);
  1384. if (res < 0) return res;
  1385. if (hf) {
  1386. if (!hname->param.len) {
  1387. fl = 1;
  1388. delete_value_lump(msg, hf, &hf->body);
  1389. }
  1390. else {
  1391. char *p;
  1392. hval2.len = 0;
  1393. p = hf->body.s;
  1394. do {
  1395. res = find_next_value(&p, hf->body.s+hf->body.len, &hval1, &hval2);
  1396. if (assign_hf_process_params(msg, hf, hname, _val?&val:0, &hval1) > 0)
  1397. fl = 1;
  1398. } while (res);
  1399. }
  1400. }
  1401. } while (hf);
  1402. return fl;
  1403. }
  1404. else {
  1405. res = find_hf_value_idx(msg, hname, &hf, &hval1, &hval2);
  1406. if (res < 0) return res;
  1407. if (hf) {
  1408. if (!hname->param.len) {
  1409. if (hname->oper == hnoRemove) {
  1410. adjust_lump_val_for_delete(hf, &hval2);
  1411. return delete_value_lump(msg, hf, &hval2);
  1412. }
  1413. else {
  1414. res = delete_value_lump(msg, 0 /* delete only value part */, &hval1);
  1415. if (res < 0) return res;
  1416. if (val.len) {
  1417. return insert_value_lump(msg, 0 /* do not add delims */, hval1.s, 1, &val);
  1418. }
  1419. return 1;
  1420. }
  1421. }
  1422. else {
  1423. return assign_hf_process_params(msg, hf, hname, _val?&val:0, &hval1);
  1424. }
  1425. }
  1426. }
  1427. break;
  1428. case hnoRemove2:
  1429. case hnoAssign2:
  1430. if (hname->flags & HNF_ALL) {
  1431. struct hdr_field* hf = 0;
  1432. int fl = -1;
  1433. do {
  1434. res = find_next_hf(msg, hname, &hf);
  1435. if (res < 0) return res;
  1436. if (hf) {
  1437. if (!hname->param.len) { /* the same as hnoRemove/hnoAssign */
  1438. fl = 1;
  1439. delete_value_lump(msg, hf, &hf->body);
  1440. }
  1441. else {
  1442. if (assign_hf_process2_params(msg, hf, hname, _val?&val:0) > 0)
  1443. fl = 1;
  1444. }
  1445. }
  1446. } while (hf);
  1447. return fl;
  1448. }
  1449. else {
  1450. res = find_hf_value_idx(msg, hname, &hf, 0, 0);
  1451. if (res < 0) return res;
  1452. if (hf) {
  1453. if (!hname->param.len) {
  1454. if (hname->oper == hnoRemove2) {
  1455. return delete_value_lump(msg, hf, &hf->body);
  1456. }
  1457. else {
  1458. res = delete_value_lump(msg, 0 /* delete only value part */, &hf->body);
  1459. if (res < 0) return res;
  1460. if (val.len) {
  1461. return insert_value_lump(msg, 0 /* do not add delims */, hf->body.s, 1, &val);
  1462. }
  1463. return 1;
  1464. }
  1465. }
  1466. else {
  1467. return assign_hf_process2_params(msg, hf, hname, _val?&val:0);
  1468. }
  1469. }
  1470. }
  1471. break;
  1472. }
  1473. return -1;
  1474. }
  1475. static int append_hf_value_fixup(void** param, int param_no) {
  1476. int res=fixup_hname_str(param, param_no);
  1477. if (res < 0) return res;
  1478. if (param_no == 1) {
  1479. if ( ((struct hname_data*)*param)->flags & HNF_ALL ) {
  1480. LOG(L_ERR, "ERROR: textops: asterisk not supported\n");
  1481. return E_CFG;
  1482. } else if ( (((struct hname_data*)*param)->flags & HNF_IDX) == 0 || !((struct hname_data*)*param)->idx ) {
  1483. ((struct hname_data*)*param)->idx = -1;
  1484. }
  1485. if (((struct hname_data*)*param)->idx < -MAX_HF_VALUE_STACK) {
  1486. LOG(L_ERR, "ERROR: textops: index cannot be lower than %d\n", -MAX_HF_VALUE_STACK);
  1487. return E_CFG;
  1488. }
  1489. if ( ((struct hname_data*)*param)->param.len ) {
  1490. LOG(L_ERR, "ERROR: textops: param not supported\n");
  1491. return E_CFG;
  1492. }
  1493. ((struct hname_data*)*param)->oper = hnoAppend;
  1494. }
  1495. return 0;
  1496. }
  1497. static int insert_hf_value_fixup(void** param, int param_no) {
  1498. int res=fixup_hname_str(param, param_no);
  1499. if (res < 0) return res;
  1500. if (param_no == 1) {
  1501. if ( ((struct hname_data*)*param)->flags & HNF_ALL ) {
  1502. LOG(L_ERR, "ERROR: textops: asterisk not supported\n");
  1503. return E_CFG;
  1504. } else if ( (((struct hname_data*)*param)->flags & HNF_IDX) == 0 || !((struct hname_data*)*param)->idx ) {
  1505. ((struct hname_data*)*param)->idx = 1;
  1506. }
  1507. if (((struct hname_data*)*param)->idx < -MAX_HF_VALUE_STACK) {
  1508. LOG(L_ERR, "ERROR: textops: index cannot be lower than %d\n", -MAX_HF_VALUE_STACK);
  1509. return E_CFG;
  1510. }
  1511. if ( ((struct hname_data*)*param)->param.len ) {
  1512. LOG(L_ERR, "ERROR: textops: param not supported\n");
  1513. return E_CFG;
  1514. }
  1515. ((struct hname_data*)*param)->oper = hnoInsert;
  1516. }
  1517. return 0;
  1518. }
  1519. static int remove_hf_value_fixup(void** param, int param_no) {
  1520. int res=fixup_hname_str(param, param_no);
  1521. if (res < 0) return res;
  1522. if (param_no == 1) {
  1523. if ( (((struct hname_data*)*param)->flags & HNF_IDX) == 0 || !((struct hname_data*)*param)->idx ) {
  1524. ((struct hname_data*)*param)->idx = 1;
  1525. ((struct hname_data*)*param)->flags |= HNF_IDX;
  1526. }
  1527. if (((struct hname_data*)*param)->idx < -MAX_HF_VALUE_STACK) {
  1528. LOG(L_ERR, "ERROR: textops: index cannot be lower than %d\n", -MAX_HF_VALUE_STACK);
  1529. return E_CFG;
  1530. }
  1531. ((struct hname_data*)*param)->oper = hnoRemove;
  1532. }
  1533. return 0;
  1534. }
  1535. static int assign_hf_value_fixup(void** param, int param_no) {
  1536. int res=fixup_hname_str(param, param_no);
  1537. if (res < 0) return res;
  1538. if (param_no == 1) {
  1539. if ( (((struct hname_data*)*param)->flags & HNF_ALL) && !((struct hname_data*)*param)->param.len) {
  1540. LOG(L_ERR, "ERROR: textops: asterisk not supported without param\n");
  1541. return E_CFG;
  1542. } else if ( (((struct hname_data*)*param)->flags & HNF_IDX) == 0 || !((struct hname_data*)*param)->idx ) {
  1543. ((struct hname_data*)*param)->idx = 1;
  1544. ((struct hname_data*)*param)->flags |= HNF_IDX;
  1545. }
  1546. if (((struct hname_data*)*param)->idx < -MAX_HF_VALUE_STACK) {
  1547. LOG(L_ERR, "ERROR: textops: index cannot be lower than %d\n", -MAX_HF_VALUE_STACK);
  1548. return E_CFG;
  1549. }
  1550. ((struct hname_data*)*param)->oper = hnoAssign;
  1551. }
  1552. return 0;
  1553. }
  1554. static int remove_hf_value2_fixup(void** param, int param_no) {
  1555. int res=remove_hf_value_fixup(param, param_no);
  1556. if (res < 0) return res;
  1557. if (param_no == 1) {
  1558. ((struct hname_data*)*param)->oper = hnoRemove2;
  1559. }
  1560. return 0;
  1561. }
  1562. static int assign_hf_value2_fixup(void** param, int param_no) {
  1563. int res=assign_hf_value_fixup(param, param_no);
  1564. if (res < 0) return res;
  1565. if (param_no == 1) {
  1566. ((struct hname_data*)*param)->oper = hnoAssign2;
  1567. }
  1568. return 0;
  1569. }
  1570. static int sel_hf_value(str* res, select_t* s, struct sip_msg* msg) { /* dummy */
  1571. return 0;
  1572. }
  1573. #define _ALLOC_INC_SIZE 1024
  1574. static int sel_hf_value_name(str* res, select_t* s, struct sip_msg* msg) {
  1575. struct hname_data* hname;
  1576. struct hdr_field* hf;
  1577. str val, hval1, hval2, huri, dummy_name;
  1578. int r;
  1579. if (!msg) {
  1580. struct hdr_field hdr;
  1581. char buf[50];
  1582. int i, n;
  1583. if (s->params[1].type == SEL_PARAM_STR) {
  1584. hname = pkg_malloc(sizeof(*hname));
  1585. if (!hname) return E_OUT_OF_MEM;
  1586. memset(hname, 0, sizeof(*hname));
  1587. for (i=s->params[1].v.s.len-1; i>0; i--) {
  1588. if (s->params[1].v.s.s[i]=='_')
  1589. s->params[1].v.s.s[i]='-';
  1590. }
  1591. i = snprintf(buf, sizeof(buf)-1, "%.*s: X\n", s->params[1].v.s.len, s->params[1].v.s.s);
  1592. buf[i] = 0;
  1593. hname->hname = s->params[1].v.s;
  1594. parse_hname2(buf, buf+i, &hdr);
  1595. if (hdr.type == HDR_ERROR_T) return E_CFG;
  1596. hname->htype = hdr.type;
  1597. s->params[1].v.p = hname;
  1598. s->params[1].type = SEL_PARAM_PTR;
  1599. }
  1600. else {
  1601. hname = s->params[1].v.p;
  1602. }
  1603. n = s->param_offset[select_level+1] - s->param_offset[select_level]; /* number of values before NESTED */
  1604. if (n > 2 && s->params[2].type == SEL_PARAM_INT) {
  1605. hname->idx = s->params[2].v.i;
  1606. hname->flags |= HNF_IDX;
  1607. if (hname->idx < -MAX_HF_VALUE_STACK) {
  1608. LOG(L_ERR, "ERROR: textops: index cannot be lower than %d\n", -MAX_HF_VALUE_STACK);
  1609. return E_CFG;
  1610. }
  1611. if (hname->idx == 0)
  1612. hname->idx = 1;
  1613. i = 3;
  1614. }
  1615. else {
  1616. i = 2;
  1617. hname->idx = 1;
  1618. }
  1619. if (n > i && s->params[i].type == SEL_PARAM_STR) {
  1620. hname->param = s->params[i].v.s;
  1621. for (i=hname->param.len-1; i>0; i--) {
  1622. if (hname->param.s[i]=='_')
  1623. hname->param.s[i]='-';
  1624. }
  1625. }
  1626. s->params[1].v.p = hname;
  1627. s->params[1].type = SEL_PARAM_PTR;
  1628. hname->oper = hnoGetValue;
  1629. return 0;
  1630. }
  1631. res->len = 0;
  1632. res->s = 0;
  1633. hname = s->params[1].v.p;
  1634. switch (hname->oper) {
  1635. case hnoGetValueUri:
  1636. if (hname->flags & HNF_ALL || (hname->flags & HNF_IDX) == 0) {
  1637. char *buf = NULL;
  1638. int buf_len = 0;
  1639. hf = 0;
  1640. do {
  1641. r = find_next_hf(msg, hname, &hf);
  1642. if (r < 0) break;
  1643. if (hf) {
  1644. char *p;
  1645. str huri;
  1646. hval2.len = 0;
  1647. p = hf->body.s;
  1648. do {
  1649. r = find_next_value(&p, hf->body.s+hf->body.len, &hval1, &hval2);
  1650. get_uri_and_skip_until_params(&hval1, &dummy_name, &huri);
  1651. if (huri.len) {
  1652. /* TODO: normalize uri, lowercase except quoted params, add/strip < > */
  1653. if (*huri.s == '<') {
  1654. huri.s++;
  1655. huri.len -= 2;
  1656. }
  1657. }
  1658. if (res->len == 0) {
  1659. *res = huri; /* first value, if is also last value then we don't need any buffer */
  1660. }
  1661. else {
  1662. if (buf) {
  1663. if (res->len+huri.len+1 > buf_len) {
  1664. buf_len = res->len+huri.len+1+_ALLOC_INC_SIZE;
  1665. res->s = pkg_realloc(buf, buf_len);
  1666. if (!res->s) {
  1667. pkg_free(buf);
  1668. LOG(L_ERR, "ERROR: textops: cannot realloc buffer\n");
  1669. res->len = 0;
  1670. return E_OUT_OF_MEM;
  1671. }
  1672. buf = res->s;
  1673. }
  1674. }
  1675. else {
  1676. /* 2nd value */
  1677. buf_len = res->len+huri.len+1+_ALLOC_INC_SIZE;
  1678. buf = pkg_malloc(buf_len);
  1679. if (!buf) {
  1680. LOG(L_ERR, "ERROR: testops: out of memory\n");
  1681. res->len = 0;
  1682. return E_OUT_OF_MEM;
  1683. }
  1684. /* copy 1st value */
  1685. memcpy(buf, res->s, res->len);
  1686. res->s = buf;
  1687. }
  1688. res->s[res->len] = ',';
  1689. res->len++;
  1690. if (huri.len) {
  1691. memcpy(res->s+res->len, huri.s, huri.len);
  1692. res->len += huri.len;
  1693. }
  1694. }
  1695. } while (r);
  1696. }
  1697. } while (hf);
  1698. if (buf) {
  1699. res->s = get_static_buffer(res->len);
  1700. if (!res->s) {
  1701. pkg_free(buf);
  1702. res->len = 0;
  1703. LOG(L_ERR, "ERROR: testops: cannot allocate static buffer\n");
  1704. return E_OUT_OF_MEM;
  1705. }
  1706. memcpy(res->s, buf, res->len);
  1707. pkg_free(buf);
  1708. }
  1709. }
  1710. else {
  1711. r = find_hf_value_idx(msg, hname, &hf, &hval1, &hval2);
  1712. if (r > 0) {
  1713. get_uri_and_skip_until_params(&hval1, &dummy_name, res);
  1714. if (res->len && *res->s == '<') {
  1715. res->s++; /* strip < & > */
  1716. res->len-=2;
  1717. }
  1718. }
  1719. }
  1720. break;
  1721. case hnoGetValueName:
  1722. if ((hname->flags & HNF_ALL) == 0) {
  1723. r = find_hf_value_idx(msg, hname, &hf, &hval1, &hval2);
  1724. if (r > 0) {
  1725. get_uri_and_skip_until_params(&hval1, res, &dummy_name);
  1726. if (res->len >= 2 && res->s[0] == '\"' && res->s[res->len-1]=='\"' ) {
  1727. res->s++; /* strip quotes */
  1728. res->len-=2;
  1729. }
  1730. }
  1731. }
  1732. break;
  1733. case hnoGetValue:
  1734. if (hname->flags & HNF_ALL || (hname->flags & HNF_IDX) == 0) {
  1735. char *buf = NULL;
  1736. int buf_len = 0;
  1737. hf = 0;
  1738. do {
  1739. r = find_next_hf(msg, hname, &hf);
  1740. if (r < 0) break;
  1741. if (hf) {
  1742. char *p;
  1743. hval2.len = 0;
  1744. p = hf->body.s;
  1745. do {
  1746. r = find_next_value(&p, hf->body.s+hf->body.len, &hval1, &hval2);
  1747. if (res->len == 0) {
  1748. *res = hval1; /* first value, if is also last value then we don't need any buffer */
  1749. }
  1750. else {
  1751. if (buf) {
  1752. if (res->len+hval1.len+1 > buf_len) {
  1753. buf_len = res->len+hval1.len+1+_ALLOC_INC_SIZE;
  1754. res->s = pkg_realloc(buf, buf_len);
  1755. if (!res->s) {
  1756. pkg_free(buf);
  1757. LOG(L_ERR, "ERROR: textops: cannot realloc buffer\n");
  1758. res->len = 0;
  1759. return E_OUT_OF_MEM;
  1760. }
  1761. buf = res->s;
  1762. }
  1763. }
  1764. else {
  1765. /* 2nd value */
  1766. buf_len = res->len+hval1.len+1+_ALLOC_INC_SIZE;
  1767. buf = pkg_malloc(buf_len);
  1768. if (!buf) {
  1769. LOG(L_ERR, "ERROR: testops: out of memory\n");
  1770. res->len = 0;
  1771. return E_OUT_OF_MEM;
  1772. }
  1773. /* copy 1st value */
  1774. memcpy(buf, res->s, res->len);
  1775. res->s = buf;
  1776. }
  1777. res->s[res->len] = ',';
  1778. res->len++;
  1779. if (hval1.len) {
  1780. memcpy(res->s+res->len, hval1.s, hval1.len);
  1781. res->len += hval1.len;
  1782. }
  1783. }
  1784. } while (r);
  1785. }
  1786. } while (hf);
  1787. if (buf) {
  1788. res->s = get_static_buffer(res->len);
  1789. if (!res->s) {
  1790. pkg_free(buf);
  1791. res->len = 0;
  1792. LOG(L_ERR, "ERROR: testops: cannot allocate static buffer\n");
  1793. return E_OUT_OF_MEM;
  1794. }
  1795. memcpy(res->s, buf, res->len);
  1796. pkg_free(buf);
  1797. }
  1798. }
  1799. else {
  1800. r = find_hf_value_idx(msg, hname, &hf, &hval1, &hval2);
  1801. if (r > 0) {
  1802. if (hname->param.len) {
  1803. str d1, d2;
  1804. get_uri_and_skip_until_params(&hval1, &dummy_name, &huri);
  1805. if (find_hf_value_param(hname, &hval1, &val, &d1, &d2)) {
  1806. *res = val;
  1807. }
  1808. }
  1809. else {
  1810. *res = hval1;
  1811. }
  1812. }
  1813. }
  1814. break;
  1815. case hnoGetValue2:
  1816. r = find_hf_value_idx(msg, hname, &hf, 0, 0);
  1817. if (r > 0) {
  1818. if (hname->param.len) {
  1819. str d1, d2;
  1820. char c;
  1821. if (find_hf_value2_param(hname, &hf->body, &val, &d1, &d2, &c)) {
  1822. *res = val;
  1823. }
  1824. }
  1825. else {
  1826. *res = hf->body;
  1827. }
  1828. }
  1829. break;
  1830. default:
  1831. break;
  1832. }
  1833. return 0;
  1834. }
  1835. static int sel_hf_value_name_param_name(str* res, select_t* s, struct sip_msg* msg) {
  1836. return sel_hf_value_name(res, s, msg);
  1837. }
  1838. static int sel_hf_value_name_param_name2(str* res, select_t* s, struct sip_msg* msg) {
  1839. if (!msg) { /* eliminate "param" level */
  1840. int n;
  1841. n = s->param_offset[select_level+1] - s->param_offset[select_level];
  1842. s->params[n-2] = s->params[n-1];
  1843. }
  1844. return sel_hf_value_name(res, s, msg);
  1845. }
  1846. static int sel_hf_value_name_uri(str* res, select_t* s, struct sip_msg* msg) {
  1847. int r;
  1848. r = sel_hf_value_name(res, s, msg);
  1849. if (!msg && r==0) {
  1850. ((struct hname_data*) s->params[1].v.p)->oper = hnoGetValueUri;
  1851. }
  1852. return r;
  1853. }
  1854. static int sel_hf_value_name_name(str* res, select_t* s, struct sip_msg* msg) {
  1855. int r;
  1856. r = sel_hf_value_name(res, s, msg);
  1857. if (!msg && r==0) {
  1858. ((struct hname_data*) s->params[1].v.p)->oper = hnoGetValueName;
  1859. }
  1860. return r;
  1861. }
  1862. static int sel_hf_value_exists(str* res, select_t* s, struct sip_msg* msg) { /* dummy */
  1863. return 0;
  1864. }
  1865. static int sel_hf_value_exists_param(str* res, select_t* s, struct sip_msg* msg) {
  1866. static char ret_val[] = "01";
  1867. struct xlstr xlstr;
  1868. int r;
  1869. if (!msg) {
  1870. r = sel_hf_value_name(res, s, msg);
  1871. if (r == 0)
  1872. ((struct hname_data*) s->params[1].v.p)->oper = hnoIsIncluded;
  1873. return r;
  1874. }
  1875. xlstr.s = s->params[2].v.s;
  1876. xlstr.xlfmt = 0;
  1877. r = incexc_hf_value_f(msg, s->params[1].v.p, (void*) &xlstr);
  1878. res->s = &ret_val[r > 0];
  1879. res->len = 1;
  1880. return 0;
  1881. }
  1882. static int sel_hf_value2(str* res, select_t* s, struct sip_msg* msg) { /* dummy */
  1883. return 0;
  1884. }
  1885. static int sel_hf_value2_name(str* res, select_t* s, struct sip_msg* msg) {
  1886. int r;
  1887. r = sel_hf_value_name(res, s, msg);
  1888. if (!msg && r==0) {
  1889. ((struct hname_data*) s->params[1].v.p)->oper = hnoGetValue2;
  1890. }
  1891. return r;
  1892. }
  1893. static int sel_hf_value2_name_param_name(str* res, select_t* s, struct sip_msg* msg) {
  1894. return sel_hf_value2_name(res, s, msg);
  1895. }
  1896. SELECT_F(select_any_nameaddr)
  1897. SELECT_F(select_any_uri)
  1898. SELECT_F(select_anyheader_params)
  1899. select_row_t sel_declaration[] = {
  1900. { NULL, SEL_PARAM_STR, STR_STATIC_INIT("hf_value"), sel_hf_value, SEL_PARAM_EXPECTED},
  1901. { sel_hf_value, SEL_PARAM_STR, STR_NULL, sel_hf_value_name, CONSUME_NEXT_INT | OPTIONAL | FIXUP_CALL},
  1902. { sel_hf_value_name, SEL_PARAM_STR, STR_STATIC_INIT("param"), sel_hf_value_name_param_name2, CONSUME_NEXT_STR | FIXUP_CALL},
  1903. { sel_hf_value_name, SEL_PARAM_STR, STR_STATIC_INIT("p"), sel_hf_value_name_param_name2, CONSUME_NEXT_STR | FIXUP_CALL},
  1904. { sel_hf_value_name, SEL_PARAM_STR, STR_STATIC_INIT("uri"), sel_hf_value_name_uri, FIXUP_CALL},
  1905. { sel_hf_value_name, SEL_PARAM_STR, STR_STATIC_INIT("name"), sel_hf_value_name_name, FIXUP_CALL},
  1906. { sel_hf_value_name, SEL_PARAM_STR, STR_STATIC_INIT("nameaddr"), select_any_nameaddr, NESTED | CONSUME_NEXT_STR}, /* it duplicates param,p,name,... */
  1907. { sel_hf_value_name, SEL_PARAM_STR, STR_STATIC_INIT("params"), select_anyheader_params, NESTED},
  1908. { sel_hf_value_name_uri, SEL_PARAM_INT, STR_NULL, select_any_uri, NESTED},
  1909. { sel_hf_value_name, SEL_PARAM_STR, STR_NULL, sel_hf_value_name_param_name, FIXUP_CALL},
  1910. { NULL, SEL_PARAM_STR, STR_STATIC_INIT("hf_value_exists"), sel_hf_value_exists, CONSUME_NEXT_STR | SEL_PARAM_EXPECTED},
  1911. { sel_hf_value_exists, SEL_PARAM_STR, STR_NULL, sel_hf_value_exists_param, FIXUP_CALL},
  1912. { NULL, SEL_PARAM_STR, STR_STATIC_INIT("hf_value2"), sel_hf_value2, SEL_PARAM_EXPECTED},
  1913. { sel_hf_value2, SEL_PARAM_STR, STR_NULL, sel_hf_value2_name, CONSUME_NEXT_INT | OPTIONAL | FIXUP_CALL},
  1914. { sel_hf_value2_name, SEL_PARAM_STR, STR_STATIC_INIT("params"), select_anyheader_params, NESTED},
  1915. { sel_hf_value2_name, SEL_PARAM_STR, STR_NULL, sel_hf_value2_name_param_name, FIXUP_CALL},
  1916. { sel_hf_value2_name_param_name, SEL_PARAM_STR, STR_STATIC_INIT("nameaddr"), select_any_nameaddr, NESTED},
  1917. { sel_hf_value2_name_param_name, SEL_PARAM_STR, STR_STATIC_INIT("uri"), select_any_uri, NESTED},
  1918. { NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
  1919. };