Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/mod_fix.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2003 FhG Fokus
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 */
20
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
25
#include "mem/mem.h"
26
#include "str.h"
27
#include "ut.h"
28
#include "error.h"
29
#include "mod_fix.h"
30
#include "lib/csv.h"
31
32
static char *re_buff=NULL;
33
static int re_buff_len = 0;
34
int fixup_regcomp(regex_t **re, str *re_str, int dup_nt)
35
0
{
36
0
  char *regex;
37
38
0
  if (dup_nt) {
39
0
    if (re_str->len + 1 > re_buff_len) {
40
0
      re_buff = pkg_realloc(re_buff,re_str->len + 1);
41
0
      if (re_buff == NULL) {
42
0
        LM_ERR("No more pkg \n");
43
0
        return E_OUT_OF_MEM;
44
0
      }
45
46
0
      re_buff_len = re_str->len + 1;
47
0
    }
48
49
0
    memcpy(re_buff,re_str->s,re_str->len);
50
0
    re_buff[re_str->len] = 0;
51
52
0
    regex = re_buff;
53
0
  } else
54
0
    regex = re_str->s;
55
56
0
  if ((*re = pkg_malloc(sizeof **re)) == 0) {
57
0
    LM_ERR("no more pkg memory\n");
58
0
    return E_OUT_OF_MEM;
59
0
  }
60
0
  if (regcomp(*re, regex, (REG_EXTENDED|REG_ICASE|REG_NEWLINE))) {
61
0
    LM_ERR("bad re %s\n", regex);
62
0
    pkg_free(*re);
63
0
    return E_BAD_RE;
64
0
  }
65
66
0
  return 0;
67
0
}
68
69
static inline gparam_p alloc_gp(void)
70
0
{
71
0
  gparam_p gp;
72
73
0
  gp = pkg_malloc(sizeof *gp);
74
0
  if (!gp) {
75
0
    LM_ERR("no more pkg memory\n");
76
0
    return NULL;
77
0
  }
78
0
  memset(gp, 0, sizeof *gp);
79
80
0
  return gp;
81
0
}
82
83
int check_cmd(const struct cmd_param *params, action_elem_t *elems)
84
0
{
85
0
  int i;
86
0
  const struct cmd_param *param;
87
0
  pv_elem_t *pve;
88
89
0
  for (param=params, i=1; param->flags; param++, i++) {
90
0
    pve = NULL;
91
92
0
    if (elems[i].type == NOSUBTYPE || elems[i].type == NULLV_ST) {
93
0
      if (!(param->flags & CMD_PARAM_OPT)) {
94
0
        LM_BUG("Mandatory parameter missing\n");
95
0
        return -1;
96
0
      } else {
97
0
        continue;
98
0
      }
99
0
    }
100
101
0
    if (param->flags & CMD_PARAM_INT) {
102
0
      if (elems[i].type != NUMBER_ST && elems[i].type != SCRIPTVAR_ST) {
103
0
        LM_ERR("Param [%d] expected to be an integer or variable\n", i);
104
0
        return -1;
105
0
      }
106
0
    } else if (param->flags & CMD_PARAM_STR) {
107
0
      if (elems[i].type == STR_ST) {
108
0
        if (!(param->flags & CMD_PARAM_NO_EXPAND) &&
109
0
          pv_parse_format(&elems[i].u.s, &pve) < 0) {
110
0
          LM_ERR("Failed to parse formatted string in param "
111
0
            "[%d]\n",i);
112
0
          return -1;
113
0
        }
114
115
0
        if ((!(param->flags & CMD_PARAM_NO_EXPAND) &&
116
0
            (pve->next || pve->spec.type != PVT_NONE)) &&
117
0
          (param->flags & CMD_PARAM_STATIC)) {
118
0
          LM_ERR("Param [%d] does not support formatted strings\n",i);
119
0
          return -1;
120
0
        }
121
122
0
        if (pve)
123
0
          pv_elem_free_all(pve);
124
0
      } else if (elems[i].type == SCRIPTVAR_ST) {
125
0
        if (param->flags & CMD_PARAM_STATIC) {
126
0
          LM_ERR("Param [%d] does not support variables\n",i);
127
0
          return -1;
128
0
        }
129
0
      } else {
130
0
        LM_ERR("Param [%d] expected to be a string or variable\n", i);
131
0
        return -1;
132
0
      }
133
0
    } else if (param->flags & CMD_PARAM_VAR) {
134
0
      if (elems[i].type != SCRIPTVAR_ST) {
135
0
        LM_ERR("Param [%d] expected to be a variable\n",i);
136
0
        return -1;
137
0
      }
138
0
    } else if (param->flags & CMD_PARAM_REGEX) {
139
0
      if (elems[i].type != STR_ST && elems[i].type != SCRIPTVAR_ST) {
140
0
        LM_ERR("Param [%d] expected to be an integer or variable\n", i);
141
0
        return -1;
142
0
      }
143
0
    }
144
0
  }
145
146
0
  return 0;
147
0
}
148
149
int fix_cmd(const struct cmd_param *params, action_elem_t *elems)
150
0
{
151
0
  int i;
152
0
  const struct cmd_param *param;
153
0
  gparam_p gp = NULL;
154
0
  int ret;
155
0
  pv_elem_t *pve;
156
0
  regex_t *re = NULL;
157
0
  void *h;
158
159
0
  for (param=params, i=1; param->flags; param++, i++) {
160
0
    pve = NULL;
161
0
    if ((elems[i].type == NOSUBTYPE) ||
162
0
      (elems[i].type == NULLV_ST)) {
163
0
      gp = NULL;
164
0
      if (param->flags & CMD_PARAM_OPT) {
165
0
        if (param->fixup && (param->flags & CMD_PARAM_FIX_NULL)) {
166
0
          h = NULL;
167
0
          if (param->fixup(&h) < 0) {
168
0
            LM_ERR("Fixup failed for param [%d]\n", i);
169
0
            ret = E_UNSPEC;
170
0
            goto error;
171
0
          }
172
173
0
          if (h != NULL) {
174
0
            if ((gp = alloc_gp()) == NULL)
175
0
              return E_OUT_OF_MEM;
176
177
0
            gp->type = GPARAM_TYPE_FIXUP;
178
0
            gp->pval = h;
179
0
          }
180
0
        }
181
182
0
        goto fill_elems;
183
0
      } else {
184
0
        LM_BUG("Mandatory parameter missing\n");
185
0
        ret = E_BUG;
186
0
        goto error;
187
0
      }
188
0
    }
189
190
0
    if ((gp = alloc_gp()) == NULL)
191
0
      return E_OUT_OF_MEM;
192
193
0
    if (param->flags & CMD_PARAM_INT) {
194
195
0
      if (elems[i].type == NUMBER_ST) {
196
0
        gp->v.ival = elems[i].u.number;
197
0
        gp->pval = &gp->v.ival;
198
0
        gp->type = GPARAM_TYPE_VAL;
199
200
0
        if (param->fixup) {
201
0
          h = gp->pval;
202
0
          if (param->fixup(&gp->pval) < 0) {
203
0
            LM_ERR("Fixup failed for param [%d]\n", i);
204
0
            ret = E_UNSPEC;
205
0
            goto error;
206
0
          }
207
0
          if (h!=gp->pval)
208
0
            gp->type = GPARAM_TYPE_FIXUP;
209
0
        }
210
0
      } else if (elems[i].type == SCRIPTVAR_ST) {
211
0
        gp->pval = elems[i].u.data;
212
0
        gp->type = GPARAM_TYPE_PVS;
213
0
      } else {
214
0
        LM_ERR("Param [%d] expected to be an integer "
215
0
          "or variable\n", i);
216
0
        return E_CFG;
217
0
      }
218
219
0
    } else if (param->flags & CMD_PARAM_STR) {
220
221
0
      if (elems[i].type == STR_ST) {
222
0
        if (!(param->flags & CMD_PARAM_NO_EXPAND) &&
223
0
          pv_parse_format(&elems[i].u.s, &pve) < 0) {
224
0
          LM_ERR("Failed to parse formatted string in param "
225
0
            "[%d]\n",i);
226
0
          ret = E_UNSPEC;
227
0
          goto error;
228
0
        }
229
230
0
        if ((param->flags & CMD_PARAM_NO_EXPAND) ||
231
0
            (!pve->next && pve->spec.type == PVT_NONE)) {
232
          /* ignoring/no variables in the provided string */
233
0
          if (pve)
234
0
            pv_elem_free_all(pve);
235
236
0
          gp->v.sval = elems[i].u.s;
237
0
          gp->pval = &gp->v.sval;
238
0
          gp->type = GPARAM_TYPE_VAL;
239
240
0
          if (param->fixup) {
241
0
            h = gp->pval;
242
0
            if (param->fixup(&gp->pval) < 0) {
243
0
              LM_ERR("Fixup failed for param [%d]\n", i);
244
0
              ret = E_UNSPEC;
245
0
              goto error;
246
0
            }
247
0
            if (h!=gp->pval)
248
0
              gp->type = GPARAM_TYPE_FIXUP;
249
0
          }
250
0
        } else {
251
0
          if (param->flags & CMD_PARAM_STATIC) {
252
0
            LM_ERR("Param [%d] does not support formatted strings\n",i);
253
0
            return E_CFG;
254
0
          }
255
256
0
          gp->pval = pve;
257
0
          gp->type = GPARAM_TYPE_PVE;
258
0
        }
259
0
      } else if (elems[i].type == SCRIPTVAR_ST) {
260
0
        if (param->flags & CMD_PARAM_STATIC) {
261
0
          LM_ERR("Param [%d] does not support variables\n",i);
262
0
          return E_CFG;
263
0
        }
264
265
0
        gp->pval = elems[i].u.data;
266
0
        gp->type = GPARAM_TYPE_PVS;
267
0
      } else {
268
0
        LM_ERR("Param [%d] expected to be a string "
269
0
          "or variable\n", i);
270
0
        ret = E_CFG;
271
0
        goto error;
272
0
      }
273
274
0
    } else if (param->flags & CMD_PARAM_VAR) {
275
276
0
      if (elems[i].type != SCRIPTVAR_ST) {
277
0
        LM_ERR("Param [%d] expected to be a variable\n",i);
278
0
        ret = E_CFG;
279
0
        goto error;
280
0
      }
281
282
0
      gp->pval = elems[i].u.data;
283
284
0
      if (param->fixup && param->fixup(&gp->pval) < 0) {
285
0
        LM_ERR("Fixup failed for param [%d]\n", i);
286
0
        ret = E_UNSPEC;
287
0
        goto error;
288
0
      }
289
290
0
    } else if (param->flags & CMD_PARAM_REGEX) {
291
292
0
      if (elems[i].type == STR_ST) {
293
0
        ret = fixup_regcomp(&re, &elems[i].u.s, 0);
294
0
        if (ret < 0)
295
0
          return ret;
296
297
0
        gp->pval = re;
298
0
        gp->type = GPARAM_TYPE_VAL;
299
300
0
        if (param->fixup && param->fixup(&gp->pval) < 0) {
301
0
          LM_ERR("Fixup failed for param [%d]\n", i);
302
0
          ret = E_UNSPEC;
303
0
          goto error;
304
0
        }
305
0
      } else if (elems[i].type == SCRIPTVAR_ST) {
306
0
        gp->pval = elems[i].u.data;
307
0
        gp->type = GPARAM_TYPE_PVS;
308
0
      } else {
309
0
        LM_ERR("Param [%d] expected to be a string "
310
0
          "or variable\n", i);
311
0
        ret = E_CFG;
312
0
        goto error;
313
0
      }
314
315
0
    } else {
316
0
      LM_BUG("Bad command parameter type\n");
317
0
      ret = E_BUG;
318
0
      goto error;
319
0
    }
320
321
0
fill_elems:
322
0
    elems[i].u.data = (void*)gp;
323
0
  }
324
325
0
  return 0;
326
0
error:
327
0
  if (gp)
328
0
    pkg_free(gp);
329
0
  if (re)
330
0
    pkg_free(re);
331
0
  return ret;
332
0
}
333
334
int get_cmd_fixups(struct sip_msg* msg, const struct cmd_param *params,
335
        action_elem_t *elems, void **cmdp, pv_value_t *tmp_vals)
336
0
{
337
0
  int i;
338
0
  const struct cmd_param *param;
339
0
  gparam_p gp;
340
0
  regex_t *re = NULL;
341
0
  int ret;
342
343
0
  for (param=params, i=1; param->flags; param++, i++) {
344
0
    gp = (gparam_p)elems[i].u.data;
345
0
    if (!gp) {
346
0
      cmdp[i-1] = NULL;
347
0
      continue;
348
0
    }
349
350
0
    if (param->flags & CMD_PARAM_INT) {
351
352
0
      switch (gp->type) {
353
0
      case GPARAM_TYPE_VAL:
354
0
        tmp_vals[i - 1].ri = *(int*)gp->pval;
355
0
        cmdp[i-1] = &tmp_vals[i - 1].ri;
356
0
        break;
357
0
      case GPARAM_TYPE_FIXUP:
358
0
        cmdp[i-1] = gp->pval;
359
0
        break;
360
0
      case GPARAM_TYPE_PVS:
361
0
        if (pv_get_spec_value(msg, (pv_spec_t *)gp->pval,
362
0
          &tmp_vals[i - 1]) != 0) {
363
0
          LM_ERR("Failed to get spec value in param [%d]\n", i);
364
0
          return E_UNSPEC;
365
0
        }
366
0
        if ((tmp_vals[i - 1].flags & PV_VAL_NULL) &&
367
0
          (param->flags & CMD_PARAM_NULLABLE)) {
368
0
          cmdp[i-1] = NULL;
369
0
          break;
370
0
        }
371
0
        if (tmp_vals[i - 1].flags & PV_VAL_NULL ||
372
0
          !(tmp_vals[i - 1].flags & PV_VAL_INT)) {
373
0
          LM_ERR("Variable in param [%d] is not an integer\n", i);
374
0
          return E_UNSPEC;
375
0
        }
376
377
0
        cmdp[i-1] = (void *)&tmp_vals[i - 1].ri;
378
379
        /* run fixup as we now have the value of the variable */
380
0
        if (param->fixup && param->fixup(&cmdp[i-1]) < 0) {
381
0
          LM_ERR("Fixup failed for param [%d]\n", i);
382
0
          return E_UNSPEC;
383
0
        }
384
385
0
        break;
386
0
      default:
387
0
        LM_BUG("Bad type for generic parameter\n");
388
0
        return E_BUG;
389
0
      }
390
391
0
    } else if (param->flags & CMD_PARAM_STR) {
392
393
0
      switch (gp->type) {
394
0
      case GPARAM_TYPE_VAL:
395
0
        tmp_vals[i - 1].rs = *(str*)gp->pval;
396
0
        cmdp[i-1] = &tmp_vals[i - 1].rs;
397
0
        break;
398
0
      case GPARAM_TYPE_FIXUP:
399
0
        cmdp[i-1] = gp->pval;
400
0
        break;
401
0
      case GPARAM_TYPE_PVE:
402
0
        if (pv_printf_s(msg, (pv_elem_t *)gp->pval,
403
0
          &tmp_vals[i - 1].rs) != 0) {
404
0
          LM_ERR("Failed to print formatted string in param [%d]\n", i);
405
0
          return E_UNSPEC;
406
0
        }
407
408
0
        cmdp[i-1] = &tmp_vals[i - 1].rs;
409
410
0
        if (param->fixup && param->fixup(&cmdp[i-1]) < 0) {
411
0
          LM_ERR("Fixup failed for param [%d]\n", i);
412
0
          return E_UNSPEC;
413
0
        }
414
415
0
        break;
416
0
      case GPARAM_TYPE_PVS:
417
0
        if (pv_get_spec_value(msg, (pv_spec_t *)gp->pval,
418
0
          &tmp_vals[i - 1]) != 0) {
419
0
          LM_ERR("Failed to get spec value in param [%d]\n", i);
420
0
          return E_UNSPEC;
421
0
        }
422
0
        if (tmp_vals[i - 1].flags & PV_VAL_NULL ||
423
0
          !(tmp_vals[i - 1].flags & PV_VAL_STR)) {
424
0
          LM_ERR("Variable in param [%d] is not a string\n", i);
425
0
          return E_UNSPEC;
426
0
        }
427
428
0
        cmdp[i-1] = &tmp_vals[i - 1].rs;
429
430
0
        if (param->fixup && param->fixup(&cmdp[i-1]) < 0) {
431
0
          LM_ERR("Fixup failed for param [%d]\n", i);
432
0
          return E_UNSPEC;
433
0
        }
434
435
0
        break;
436
0
      default:
437
0
        LM_BUG("Bad type for generic parameter\n");
438
0
        return E_BUG;
439
0
      }
440
441
0
    } else if (param->flags & CMD_PARAM_VAR) {
442
443
0
      cmdp[i-1] = gp->pval;
444
445
0
    } else if (param->flags & CMD_PARAM_REGEX) {
446
447
0
      switch (gp->type) {
448
0
      case GPARAM_TYPE_VAL:
449
0
        cmdp[i-1] = gp->pval;
450
0
        break;
451
0
      case GPARAM_TYPE_PVS:
452
0
        if (pv_get_spec_value(msg, (pv_spec_t *)gp->pval,
453
0
          &tmp_vals[i - 1]) != 0) {
454
0
          LM_ERR("Failed to get spec value in param [%d]\n", i);
455
0
          return E_UNSPEC;
456
0
        }
457
0
        if (tmp_vals[i - 1].flags & PV_VAL_NULL ||
458
0
          !(tmp_vals[i - 1].flags & PV_VAL_STR)) {
459
0
          LM_ERR("Variable in param [%d] is not a string\n", i);
460
0
          return E_UNSPEC;
461
0
        }
462
463
0
        ret = fixup_regcomp(&re, &tmp_vals[i - 1].rs, 1);
464
0
        if (ret < 0)
465
0
          return ret;
466
0
        cmdp[i-1] = re;
467
468
0
        if (param->fixup) {
469
0
          if (param->fixup(&cmdp[i-1]) < 0) {
470
0
            LM_ERR("Fixup failed for param [%d]\n", i);
471
0
            ret = E_UNSPEC;
472
0
          }
473
474
0
          regfree(re);
475
0
          pkg_free(re);
476
477
0
          if (ret < 0)
478
0
            return ret;
479
0
        }
480
481
0
        break;
482
0
      default:
483
0
        LM_BUG("Bad type for generic parameter\n");
484
0
        return E_BUG;
485
0
      }
486
487
0
    } else {
488
0
      LM_BUG("Bad command parameter type\n");
489
0
      return E_BUG;
490
0
    }
491
0
  }
492
493
0
  return 0;
494
0
}
495
496
int free_cmd_fixups(const struct cmd_param *params, action_elem_t *elems, void **cmdp)
497
0
{
498
0
  int i;
499
0
  const struct cmd_param *param;
500
0
  gparam_p gp;
501
502
0
  for (param=params, i=1; param->flags; param++, i++) {
503
0
    gp = (gparam_p)elems[i].u.data;
504
0
    if (!gp)
505
0
      continue;
506
507
0
    if (param->flags & CMD_PARAM_INT) {
508
0
      if (param->free_fixup && gp->type == GPARAM_TYPE_PVS)
509
0
        if (param->free_fixup(&cmdp[i-1]) < 0) {
510
0
          LM_ERR("Failed to free fixup for param [%d]\n", i);
511
0
          return E_UNSPEC;
512
0
        }
513
0
    } else if (param->flags & CMD_PARAM_STR) {
514
0
      if (param->free_fixup && (gp->type == GPARAM_TYPE_PVS ||
515
0
        gp->type == GPARAM_TYPE_PVE))
516
0
        if (param->free_fixup(&cmdp[i-1]) < 0) {
517
0
          LM_ERR("Failed to free fixup for param [%d]\n", i);
518
0
          return E_UNSPEC;
519
0
        }
520
0
    } else if (param->flags & CMD_PARAM_REGEX) {
521
0
      if (gp->type == GPARAM_TYPE_PVS || gp->type == GPARAM_TYPE_PVE) {
522
0
        if (param->fixup) {
523
0
          if (param->free_fixup && param->free_fixup(&cmdp[i-1]) < 0) {
524
0
            LM_ERR("Failed to free fixup for param [%d]\n", i);
525
0
            return E_UNSPEC;
526
0
          }
527
0
        } else {
528
0
          regfree((regex_t*)cmdp[i-1]);
529
0
          pkg_free(cmdp[i-1]);
530
0
        }
531
0
      }
532
0
    } else if (param->flags & CMD_PARAM_VAR) {
533
0
      continue;
534
0
    } else {
535
0
      LM_BUG("Bad command parameter type\n");
536
0
      return E_BUG;
537
0
    }
538
0
  }
539
540
0
  return 0;
541
0
}
542
543
int fixup_named_flags(void** param, str *flag_names, str *kv_flag_names,
544
  str *kv_flag_vals)
545
0
{
546
0
  str *s = (str*)*param;
547
0
  csv_record *list, *rec;
548
0
  int i;
549
0
  unsigned int flags = 0;
550
0
  char *p;
551
0
  str name;
552
553
0
  list = parse_csv_record(s);
554
0
  if (!list) {
555
0
    LM_ERR("Failed to parse list of flags\n");
556
0
    return -1;
557
0
  }
558
559
0
  if (kv_flag_names)
560
0
    for (i = 0; kv_flag_names[i].s ; i++) {
561
0
      kv_flag_vals[i].s = NULL;
562
0
      kv_flag_vals[i].len = 0;
563
0
    }
564
565
0
  for (rec = list; rec; rec = rec->next) {
566
0
    if (flag_names) {
567
0
      for (i = 0; flag_names[i].s && !str_match(&rec->s, &flag_names[i]);
568
0
        i++) ;
569
570
0
      if (!flag_names[i].s) {
571
0
        if (!kv_flag_names) {
572
0
          LM_ERR("Unknown flag: %.*s\n", rec->s.len, rec->s.s);
573
0
          goto error;
574
0
        }
575
0
      } else {
576
0
        flags |= (1<<i);
577
0
        continue;
578
0
      }
579
0
    }
580
581
    /* the current flag must be a key-value flag at this point */
582
0
    if (kv_flag_names) {
583
0
      p = q_memchr(rec->s.s, '=', rec->s.len);
584
585
0
      name.s = rec->s.s;
586
0
      name.len = p ? (p - rec->s.s) : rec->s.len;
587
588
0
      for (i = 0; kv_flag_names[i].s &&
589
0
        !str_match(&name, &kv_flag_names[i]); i++) ;
590
591
0
      if (!kv_flag_names[i].s) {
592
0
        LM_ERR("Unknown flag: %.*s\n", name.len, name.s);
593
0
        goto error;
594
0
      }
595
596
0
      if (!p || (rec->s.len - (p - rec->s.s) - 1) == 0) {
597
0
        LM_ERR("Bad format for key-value flag: %.*s\n",
598
0
          rec->s.len, rec->s.s);
599
0
        goto error;
600
0
      }
601
602
0
      kv_flag_vals[i].s = p+1;
603
0
      kv_flag_vals[i].len = rec->s.len - name.len - 1;
604
0
    }
605
0
  }
606
607
0
  if (flag_names)
608
0
    *param = (void*)(unsigned long)flags;
609
610
0
  free_csv_record(list);
611
0
  return 0;
612
0
error:
613
0
  free_csv_record(list);
614
0
  return -1;
615
0
}