Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/mi/mi.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2006 Voice Sistem SRL
3
 * Copyright (C) 2018 OpenSIPS Solutions
4
 *
5
 * This file is part of opensips, a free SIP server.
6
 *
7
 * opensips is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * opensips is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 *
21
 *
22
 */
23
24
/*
25
 * OpenSIPS Management Interface
26
 *
27
 * The OpenSIPS management interface (MI) is a plugin architecture with a few different
28
 * handlers that gives access to the management interface over various transports.
29
 *
30
 * The OpenSIPS core and modules register commands to the interface at runtime.
31
 * Look into the various module documentation files for information of these
32
 * commands.
33
 *
34
 */
35
36
#include <string.h>
37
38
#include "../dprint.h"
39
#include "../mem/mem.h"
40
#include "../mem/shm_mem.h"
41
#include "../lib/cJSON.h"
42
#include "../lib/osips_malloc.h"
43
#include "mi.h"
44
#include "mi_trace.h"
45
46
static struct mi_cmd*  mi_cmds = 0;
47
static int mi_cmds_no = 0;
48
49
static mi_response_t *build_err_resp(int code, const char *msg, int msg_len,
50
    const char *details, int details_len);
51
52
0
#define MI_MODULE_SEP ':'
53
0
#define MI_DEFAULT_MODULE_NAME "core"
54
0
#define MI_PROTO_MODULE_PREFIX "proto_"
55
56
struct mi_mod_group {
57
  int id;
58
  str_const module;
59
  int start;
60
  int end;
61
};
62
63
static struct mi_mod_group *mi_mod_groups = 0;
64
static int mi_mod_groups_no = 0;
65
static struct mi_cmd_alias *mi_cmd_aliases = 0;
66
static int mi_cmd_aliases_no = 0;
67
68
void _init_mi_sys_mem_hooks(void)
69
0
{
70
0
  cJSON_InitHooks(&sys_mem_hooks);
71
0
}
72
73
void _init_mi_shm_mem_hooks(void)
74
0
{
75
0
  cJSON_InitHooks(&shm_mem_hooks);
76
0
}
77
78
void _init_mi_pkg_mem_hooks(void)
79
0
{
80
0
  cJSON_InitHooks(NULL);
81
0
}
82
83
static inline int get_mi_id(const char *name, int len)
84
0
{
85
0
  int n;
86
0
  int i;
87
88
0
  for( n=0,i=0 ; i<len ; n+=name[i] ,i++ );
89
0
  return n;
90
0
}
91
92
93
static inline struct mi_cmd* lookup_mi_cmd_id(int id, const char *name, int len)
94
0
{
95
0
  int i;
96
97
0
  for( i=0 ; i<mi_cmds_no ; i++ ) {
98
0
    if ( id==mi_cmds[i].id && len==mi_cmds[i].name.len &&
99
0
    memcmp(mi_cmds[i].name.s,name,len)==0 )
100
0
      return &mi_cmds[i];
101
0
  }
102
103
0
  return 0;
104
0
}
105
106
static void mark_ambiguous_mi_cmd_name(struct mi_cmd *new_cmd)
107
0
{
108
0
  const char *new_local_name;
109
0
  int new_local_len;
110
0
  int i;
111
112
0
  new_local_name = new_cmd->name.s + new_cmd->module.len + 1;
113
0
  new_local_len = new_cmd->name.len - new_cmd->module.len - 1;
114
115
0
  for (i = 0; i < mi_cmds_no - 1; i++) {
116
0
    if (mi_cmds[i].local_id != new_cmd->local_id ||
117
0
        mi_cmds[i].name.len - mi_cmds[i].module.len - 1 != new_local_len ||
118
0
        memcmp(mi_cmds[i].name.s + mi_cmds[i].module.len + 1,
119
0
          new_local_name, new_local_len) != 0)
120
0
      continue;
121
122
0
    mi_cmds[i].flags |= MI_LOCAL_NAME_AMBIGUOUS;
123
0
    new_cmd->flags |= MI_LOCAL_NAME_AMBIGUOUS;
124
0
  }
125
126
0
}
127
128
static int add_mi_cmd_alias(const char *name, int len, int cmd_idx)
129
0
{
130
0
  struct mi_cmd_alias *aliases;
131
0
  int id;
132
0
  int i;
133
134
0
  if (!name || len <= 0)
135
0
    return 0;
136
137
0
  id = get_mi_id(name, len);
138
0
  for (i = 0; i < mi_cmd_aliases_no; i++) {
139
0
    if (mi_cmd_aliases[i].id != id ||
140
0
        mi_cmd_aliases[i].name.len != len ||
141
0
        memcmp(mi_cmd_aliases[i].name.s, name, len) != 0)
142
0
      continue;
143
0
    if (mi_cmd_aliases[i].cmd_idx == cmd_idx)
144
0
      return 0;
145
0
  }
146
147
0
  aliases = (struct mi_cmd_alias *)pkg_realloc(mi_cmd_aliases,
148
0
      (mi_cmd_aliases_no + 1) * sizeof(struct mi_cmd_alias));
149
0
  if (!aliases) {
150
0
    LM_ERR("no more pkg memory\n");
151
0
    return -1;
152
0
  }
153
154
0
  mi_cmd_aliases = aliases;
155
0
  mi_cmd_aliases[mi_cmd_aliases_no].id = id;
156
0
  mi_cmd_aliases[mi_cmd_aliases_no].name.s = (char *)name;
157
0
  mi_cmd_aliases[mi_cmd_aliases_no].name.len = len;
158
0
  mi_cmd_aliases[mi_cmd_aliases_no].cmd_idx = cmd_idx;
159
0
  mi_cmd_aliases_no++;
160
161
0
  return 0;
162
0
}
163
164
static inline struct mi_cmd* lookup_mi_cmd_alias(const char *name, int len)
165
0
{
166
0
  struct mi_cmd *match = NULL;
167
0
  struct mi_cmd *cmd;
168
0
  int id;
169
0
  int i;
170
171
0
  id = get_mi_id(name, len);
172
0
  for (i = 0; i < mi_cmd_aliases_no; i++) {
173
0
    if (mi_cmd_aliases[i].id != id ||
174
0
        mi_cmd_aliases[i].name.len != len ||
175
0
        memcmp(mi_cmd_aliases[i].name.s, name, len) != 0)
176
0
      continue;
177
178
0
    cmd = &mi_cmds[mi_cmd_aliases[i].cmd_idx];
179
0
    if (!match) {
180
0
      match = cmd;
181
0
      continue;
182
0
    }
183
184
0
    if (match != cmd)
185
0
      return NULL;
186
0
  }
187
188
0
  return match;
189
0
}
190
191
static inline struct mi_cmd* lookup_mi_cmd_local(const char *name, int len,
192
    int *ambiguous)
193
0
{
194
0
  struct mi_cmd *cmd;
195
0
  struct mi_cmd *match = NULL;
196
0
  int local_id;
197
0
  int i;
198
199
0
  if (ambiguous)
200
0
    *ambiguous = 0;
201
202
0
  local_id = get_mi_id(name, len);
203
204
0
  for (i = 0; i < mi_cmds_no; i++) {
205
0
    cmd = &mi_cmds[i];
206
0
    if (cmd->local_id != local_id ||
207
0
        cmd->name.len - cmd->module.len - 1 != len ||
208
0
        memcmp(cmd->name.s + cmd->module.len + 1, name, len) != 0)
209
0
      continue;
210
211
0
    if ((cmd->flags & MI_LOCAL_NAME_AMBIGUOUS) || match) {
212
0
      if (ambiguous)
213
0
        *ambiguous = 1;
214
0
      return NULL;
215
0
    }
216
217
0
    match = cmd;
218
0
  }
219
220
0
  return match;
221
0
}
222
223
static const char *get_mi_mod_name(const char *mod_name)
224
0
{
225
0
  if (!mod_name || !*mod_name)
226
0
    return MI_DEFAULT_MODULE_NAME;
227
228
0
  if (strncmp(mod_name, MI_PROTO_MODULE_PREFIX,
229
0
      sizeof(MI_PROTO_MODULE_PREFIX) - 1) == 0)
230
0
    return mod_name + sizeof(MI_PROTO_MODULE_PREFIX) - 1;
231
232
0
  return mod_name;
233
0
}
234
235
static mi_response_t *build_ambiguous_mi_cmd_resp(const char *name, int len)
236
0
{
237
0
  struct mi_cmd *cmd;
238
0
  mi_response_t *resp;
239
0
  int *matches;
240
0
  char *details;
241
0
  int details_len;
242
0
  int local_len;
243
0
  int needed;
244
0
  int is_local;
245
0
  int i;
246
0
  int n;
247
0
  int p;
248
0
  int j;
249
250
0
  if (!name || len <= 0)
251
0
    return NULL;
252
253
0
  if (mi_cmds_no <= 1)
254
0
    return NULL;
255
256
0
  matches = pkg_malloc(mi_cmds_no * sizeof(int));
257
0
  if (!matches) {
258
0
    LM_ERR("no more pkg memory\n");
259
0
    return build_err_resp(JSONRPC_AMBIG_METHOD_CODE,
260
0
        MI_SSTR(JSONRPC_AMBIG_METHOD_MSG),
261
0
        MI_SSTR("ambiguous MI command"));
262
0
  }
263
264
0
  is_local = (memchr(name, MI_MODULE_SEP, len) == NULL);
265
0
  n = 0;
266
0
  if (is_local) {
267
0
    for (i = 0; i < mi_cmds_no; i++) {
268
0
      cmd = &mi_cmds[i];
269
0
      local_len = cmd->name.len - cmd->module.len - 1;
270
0
      if (local_len != len ||
271
0
          memcmp(cmd->name.s + cmd->module.len + 1, name, len) != 0)
272
0
        continue;
273
274
0
      matches[n++] = i;
275
0
    }
276
0
  }
277
278
0
  for (i = 0; i < mi_cmd_aliases_no; i++) {
279
0
    if (mi_cmd_aliases[i].name.len != len ||
280
0
        memcmp(mi_cmd_aliases[i].name.s, name, len) != 0)
281
0
      continue;
282
283
0
    for (j = 0; j < n; j++)
284
0
      if (matches[j] == mi_cmd_aliases[i].cmd_idx)
285
0
        break;
286
0
    if (j == n)
287
0
      matches[n++] = mi_cmd_aliases[i].cmd_idx;
288
0
  }
289
290
0
  if (n < 2) {
291
0
    pkg_free(matches);
292
0
    return NULL;
293
0
  }
294
295
0
  details_len = 0;
296
0
  for (i = 0; i < n; i++) {
297
0
    details_len += mi_cmds[matches[i]].name.len;
298
0
    if (i > 0)
299
0
      details_len += 2;
300
0
  }
301
302
0
  needed = sizeof("supported names: ") - 1 + details_len;
303
0
  details = pkg_malloc(needed + 1);
304
0
  if (!details) {
305
0
    LM_ERR("no more pkg memory\n");
306
0
    pkg_free(matches);
307
0
    return build_err_resp(JSONRPC_AMBIG_METHOD_CODE,
308
0
        MI_SSTR(JSONRPC_AMBIG_METHOD_MSG),
309
0
        MI_SSTR("ambiguous MI command"));
310
0
  }
311
312
0
  memcpy(details, "supported names: ", sizeof("supported names: ") - 1);
313
0
  p = sizeof("supported names: ") - 1;
314
0
  for (i = 0; i < n; i++) {
315
0
    if (i > 0) {
316
0
      details[p++] = ',';
317
0
      details[p++] = ' ';
318
0
    }
319
320
0
    cmd = &mi_cmds[matches[i]];
321
0
    memcpy(details + p, cmd->name.s, cmd->name.len);
322
0
    p += cmd->name.len;
323
0
  }
324
325
0
  details[p] = '\0';
326
0
  resp = build_err_resp(JSONRPC_AMBIG_METHOD_CODE,
327
0
      MI_SSTR(JSONRPC_AMBIG_METHOD_MSG), details, p);
328
0
  pkg_free(matches);
329
0
  pkg_free(details);
330
0
  return resp;
331
0
}
332
333
static int add_mi_mod_group(const char *mod_name, int mod_len, int start, int end)
334
0
{
335
0
  struct mi_mod_group *groups;
336
0
  int mod_id;
337
338
0
  if (start >= end)
339
0
    return 0;
340
341
0
  mod_id = get_mi_id(mod_name, mod_len);
342
343
0
  if (mi_mod_groups_no > 0 &&
344
0
      mi_mod_groups[mi_mod_groups_no - 1].id == mod_id &&
345
0
      mi_mod_groups[mi_mod_groups_no - 1].end == start &&
346
0
      mi_mod_groups[mi_mod_groups_no - 1].module.len == mod_len &&
347
0
      memcmp(mi_mod_groups[mi_mod_groups_no - 1].module.s,
348
0
        mod_name, mod_len) == 0) {
349
0
    mi_mod_groups[mi_mod_groups_no - 1].end = end;
350
0
    return 0;
351
0
  }
352
353
0
  groups = (struct mi_mod_group *)pkg_realloc(mi_mod_groups,
354
0
      (mi_mod_groups_no + 1) * sizeof(struct mi_mod_group));
355
0
  if (!groups) {
356
0
    LM_ERR("no more pkg memory\n");
357
0
    return -1;
358
0
  }
359
360
0
  mi_mod_groups = groups;
361
0
  mi_mod_groups[mi_mod_groups_no].id = mod_id;
362
0
  mi_mod_groups[mi_mod_groups_no].module.s = mod_name;
363
0
  mi_mod_groups[mi_mod_groups_no].module.len = mod_len;
364
0
  mi_mod_groups[mi_mod_groups_no].start = start;
365
0
  mi_mod_groups[mi_mod_groups_no].end = end;
366
0
  mi_mod_groups_no++;
367
368
0
  return 0;
369
0
}
370
371
static char *build_mi_cmd_name(const char *mod_name, const char *cmd_name)
372
0
{
373
0
  int mod_len;
374
0
  int cmd_len;
375
0
  char *full_name;
376
377
0
  mod_len = strlen(mod_name);
378
0
  cmd_len = strlen(cmd_name);
379
380
0
  full_name = pkg_malloc(mod_len + 1 + cmd_len + 1);
381
0
  if (!full_name)
382
0
    return NULL;
383
384
0
  memcpy(full_name, mod_name, mod_len);
385
0
  full_name[mod_len] = MI_MODULE_SEP;
386
0
  memcpy(full_name + mod_len + 1, cmd_name, cmd_len);
387
0
  full_name[mod_len + 1 + cmd_len] = '\0';
388
389
0
  return full_name;
390
0
}
391
392
393
int register_mi_mod(const char *mod_name, const mi_export_t *mis)
394
0
{
395
0
  const char *module_name;
396
0
  const char *const *aliases;
397
0
  char *cmd_name;
398
0
  int cmd_idx;
399
0
  int mod_len;
400
0
  int start;
401
0
  int ret;
402
0
  int i;
403
0
  int j;
404
405
0
  if (mis==0)
406
0
    return 0;
407
408
0
  module_name = get_mi_mod_name(mod_name);
409
0
  mod_len = strlen(module_name);
410
0
  start = mi_cmds_no;
411
412
0
  for ( i=0 ; mis[i].name ; i++ ) {
413
0
    if (strchr(mis[i].name, MI_MODULE_SEP)) {
414
0
      LM_ERR("invalid MI cmd <%s> for module %s (must not include '%c')\n",
415
0
          mis[i].name, module_name, MI_MODULE_SEP);
416
0
      return -1;
417
0
    }
418
419
0
    cmd_name = build_mi_cmd_name(module_name, mis[i].name);
420
0
    if (!cmd_name) {
421
0
      LM_ERR("oom while building MI cmd <%s> for module %s\n",
422
0
          mis[i].name, module_name);
423
0
      return -1;
424
0
    }
425
426
0
    ret = register_mi_cmd(cmd_name, mis[i].help, mis[i].flags,
427
0
        mis[i].init_f, MI_EXPORT_RECIPES(mis[i]), module_name);
428
0
    if (ret!=0) {
429
0
      LM_ERR("failed to register cmd <%s> for module %s\n",
430
0
          mis[i].name, module_name);
431
0
      pkg_free(cmd_name);
432
0
      continue;
433
0
    }
434
435
0
    cmd_idx = mi_cmds_no - 1;
436
0
    if (add_mi_cmd_alias(mis[i].name, strlen(mis[i].name), cmd_idx) < 0)
437
0
      return -1;
438
439
0
    aliases = MI_EXPORT_ALIASES(mis[i]);
440
0
    for (j = 0; aliases && aliases[j]; j++)
441
0
      if (add_mi_cmd_alias(aliases[j], strlen(aliases[j]), cmd_idx) < 0)
442
0
        return -1;
443
0
  }
444
445
0
  if (add_mi_mod_group(module_name, mod_len, start, mi_cmds_no) < 0)
446
0
    return -1;
447
448
0
  return 0;
449
0
}
450
451
int register_mi_cmd_alias(const char *mod_name, const char *cmd_name,
452
    const char *alias)
453
0
{
454
0
  const char *module_name;
455
0
  struct mi_cmd *cmd;
456
0
  char *full_name;
457
0
  int cmd_idx;
458
0
  int id;
459
0
  int len;
460
0
  int ret;
461
462
0
  if (!cmd_name || !alias) {
463
0
    LM_ERR("invalid params cmd_name=%s alias=%s\n", cmd_name, alias);
464
0
    return -1;
465
0
  }
466
467
0
  module_name = get_mi_mod_name(mod_name);
468
0
  full_name = build_mi_cmd_name(module_name, cmd_name);
469
0
  if (!full_name) {
470
0
    LM_ERR("oom while building canonical MI cmd for alias registration\n");
471
0
    return -1;
472
0
  }
473
474
0
  len = strlen(full_name);
475
0
  id = get_mi_id(full_name, len);
476
0
  cmd = lookup_mi_cmd_id(id, full_name, len);
477
0
  if (!cmd) {
478
0
    LM_ERR("cannot register alias <%s>: unknown canonical cmd <%s>\n",
479
0
        alias, full_name);
480
0
    pkg_free(full_name);
481
0
    return -1;
482
0
  }
483
484
0
  cmd_idx = cmd - mi_cmds;
485
0
  ret = add_mi_cmd_alias(alias, strlen(alias), cmd_idx);
486
0
  pkg_free(full_name);
487
0
  return ret;
488
0
}
489
490
int register_mi_cmd_aliases(const char *mod_name, const char *cmd_name,
491
    const char *const *aliases)
492
0
{
493
0
  int i;
494
495
0
  if (!aliases)
496
0
    return 0;
497
498
0
  for (i = 0; aliases[i]; i++)
499
0
    if (register_mi_cmd_alias(mod_name, cmd_name, aliases[i]) < 0)
500
0
      return -1;
501
502
0
  return 0;
503
0
}
504
505
506
int init_mi_child(void)
507
0
{
508
0
  int i;
509
510
0
  for ( i=0 ; i<mi_cmds_no ; i++ ) {
511
0
    if ( mi_cmds[i].init_f && mi_cmds[i].init_f()!=0 ) {
512
0
      LM_ERR("failed to init <%.*s>\n",
513
0
          mi_cmds[i].name.len,mi_cmds[i].name.s);
514
0
      return -1;
515
0
    }
516
0
  }
517
0
  return 0;
518
0
}
519
520
521
522
int register_mi_cmd(char *name, char *help, unsigned int flags,
523
    mi_child_init_f in, const mi_recipe_t *recipes, const char* mod_name)
524
0
{
525
0
  struct mi_cmd *cmds;
526
0
  int mod_len;
527
0
  int id;
528
0
  int len;
529
530
0
  if (recipes==0 || name==0 || mod_name==0) {
531
0
    LM_ERR("invalid params recipes=%p, name=%s\n", recipes, name);
532
0
    return -1;
533
0
  }
534
535
0
  mod_len = strlen(mod_name);
536
0
  len = strlen(name);
537
0
  if (len <= mod_len + 1 || memcmp(name, mod_name, mod_len) != 0 ||
538
0
      name[mod_len] != MI_MODULE_SEP) {
539
0
    LM_ERR("invalid command <%s> for module %s\n", name, mod_name);
540
0
    return -1;
541
0
  }
542
543
0
  id = get_mi_id(name,len);
544
545
0
  if (lookup_mi_cmd_id( id, name, len)) {
546
0
    LM_ERR("command <%.*s> already registered\n", len, name);
547
0
    return -1;
548
0
  }
549
550
0
  cmds = (struct mi_cmd*)pkg_realloc( mi_cmds,
551
0
      (mi_cmds_no+1)*sizeof(struct mi_cmd) );
552
0
  if (cmds==0) {
553
0
    LM_ERR("no more pkg memory\n");
554
0
    return -1;
555
0
  }
556
557
0
  mi_cmds = cmds;
558
0
  mi_cmds_no++;
559
560
0
  cmds = &cmds[mi_cmds_no-1];
561
562
0
  cmds->init_f = in;
563
0
  cmds->flags = flags & (~MI_LOCAL_NAME_AMBIGUOUS);
564
0
  cmds->name.s = name;
565
0
  cmds->name.len = len;
566
0
  cmds->module.s = mod_name;
567
0
  cmds->module.len = strlen(mod_name);
568
0
  cmds->local_id = get_mi_id(name + mod_len + 1, len - mod_len - 1);
569
0
  cmds->help.s = help;
570
0
  cmds->help.len = help ? strlen(help) : 0;
571
0
  cmds->id = id;
572
0
  cmds->recipes = recipes;
573
574
0
  mark_ambiguous_mi_cmd_name(cmds);
575
576
  /**
577
   * FIXME we should check if trace_api is loaded not to lose unnecessary space
578
   * but some mi commands might have been already registered before loading the api
579
   * an example is the statistics mi commands
580
   */
581
0
  cmds->trace_mask = shm_malloc(sizeof(volatile unsigned char));
582
0
  if ( !cmds->trace_mask ) {
583
0
    LM_ERR("no more shm mem!\n");
584
0
    return -1;
585
0
  }
586
587
  /* by default all commands are traced */
588
0
  *cmds->trace_mask = (~(volatile unsigned char)0);
589
590
0
  return 0;
591
0
}
592
593
594
595
struct mi_cmd* lookup_mi_cmd( char *name, int len)
596
0
{
597
0
  char *cmd_name;
598
0
  struct mi_cmd *cmd;
599
0
  int amb;
600
0
  int mod_len;
601
0
  int mod_id;
602
0
  int id;
603
0
  int i;
604
0
  int j;
605
606
0
  cmd_name = memchr(name, MI_MODULE_SEP, len);
607
0
  if (!cmd_name) {
608
0
    cmd = lookup_mi_cmd_local(name, len, &amb);
609
0
    if (cmd || amb)
610
0
      return cmd;
611
0
    return lookup_mi_cmd_alias(name, len);
612
0
  }
613
0
  if (cmd_name == name || cmd_name == name + len - 1)
614
0
    return NULL;
615
616
0
  mod_len = cmd_name - name;
617
0
  mod_id = get_mi_id(name, mod_len);
618
0
  id = get_mi_id(name, len);
619
620
0
  for (i = 0; i < mi_mod_groups_no; i++) {
621
0
    if (mi_mod_groups[i].id != mod_id ||
622
0
        mi_mod_groups[i].module.len != mod_len ||
623
0
        memcmp(mi_mod_groups[i].module.s, name, mod_len) != 0)
624
0
      continue;
625
626
0
    for (j = mi_mod_groups[i].start; j < mi_mod_groups[i].end; j++) {
627
0
      cmd = &mi_cmds[j];
628
0
      if (id == cmd->id && len == cmd->name.len &&
629
0
          memcmp(cmd->name.s, name, len) == 0)
630
0
        return cmd;
631
0
    }
632
0
  }
633
634
0
  return lookup_mi_cmd_alias(name, len);
635
0
}
636
637
638
void get_mi_cmds( struct mi_cmd** cmds, int *size)
639
0
{
640
0
  *cmds = mi_cmds;
641
0
  *size = mi_cmds_no;
642
0
}
643
644
void get_mi_cmd_aliases(struct mi_cmd_alias **aliases, int *size)
645
0
{
646
0
  *aliases = mi_cmd_aliases;
647
0
  *size = mi_cmd_aliases_no;
648
0
}
649
650
int parse_mi_request(const char *req, const char **end_ptr, mi_request_t *parsed)
651
0
{
652
0
  mi_item_t *req_jsonrpc;
653
654
0
  _init_mi_sys_mem_hooks();
655
656
0
  parsed->req_obj = cJSON_ParseWithOpts(req, end_ptr, 0);
657
0
  if (!parsed->req_obj) {
658
0
    _init_mi_pkg_mem_hooks();
659
0
    return -1;
660
0
  }
661
662
  /* check if the request is a valid JSON-RPC Request object */
663
  /* get request id (if absent -> notification) */
664
0
  parsed->id = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_ID_S);
665
0
  if (parsed->id && !(parsed->id->type & (cJSON_NULL|cJSON_Number|cJSON_String)))
666
0
    parsed->invalid = 1;
667
668
  /* check 'jsonrpc' member */
669
0
  req_jsonrpc = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_S);
670
0
  if (!req_jsonrpc || !(req_jsonrpc->type & cJSON_String) ||
671
0
    strcmp(req_jsonrpc->valuestring, JSONRPC_VERS_S))
672
0
    parsed->invalid = 1;
673
674
  /* check 'method' member */
675
0
  parsed->method = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_METHOD_S);
676
0
  if (!parsed->method || !(parsed->method->type & cJSON_String)) {
677
0
    parsed->method = NULL;
678
0
    parsed->invalid = 1;
679
0
  }
680
681
  /* check 'params' member */
682
0
  parsed->params = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_PARAMS_S);
683
0
  if (parsed->params) {
684
0
    if (!(parsed->params->type & (cJSON_Array|cJSON_Object)))
685
0
      parsed->invalid = 1;
686
0
    else if (!parsed->params->child) {
687
0
      parsed->params = NULL;
688
0
    }
689
0
  }
690
691
0
  _init_mi_pkg_mem_hooks();
692
693
0
  return 0;
694
0
}
695
696
char *mi_get_req_method(mi_request_t *req)
697
0
{
698
0
  if (!req || !req->method)
699
0
    return NULL;
700
701
0
  return req->method->valuestring;
702
0
}
703
704
static int match_named_params(const mi_recipe_t *recipe, mi_item_t *req_params)
705
0
{
706
0
  mi_item_t *param;
707
0
  int i;
708
709
0
  for (i = 0; recipe->params[i]; i++) {
710
0
    for (param = req_params->child; param; param = param->next)
711
0
      if (param->string && !strcmp(recipe->params[i], param->string))
712
0
        break;
713
714
0
    if (!param)
715
0
      return 0;
716
0
  }
717
718
0
  return 1;
719
0
}
720
721
static int match_no_params(const mi_recipe_t *recipe, mi_item_t *req_params)
722
0
{
723
0
  mi_item_t *param;
724
0
  int i, j;
725
726
0
  for (i = 0; recipe->params[i]; i++) ;
727
728
0
  for (param = req_params->child, j = 0; param; param = param->next, j++) ;
729
730
0
  return i == j;
731
0
}
732
733
static const mi_recipe_t *get_cmd_recipe(const mi_recipe_t *recipes, mi_item_t *req_params,
734
                int pos_params, int *params_err)
735
0
{
736
0
  const mi_recipe_t *match = NULL;
737
0
  int i;
738
739
0
  for (i = 0; recipes[i].cmd; i++) {
740
0
    if (!req_params) {
741
0
      if (recipes[i].params[0] == NULL)
742
0
        return &recipes[i];
743
0
      else
744
0
        continue;
745
0
    } else {
746
0
      if (recipes[i].params[0] == NULL)
747
0
        continue;
748
0
    }
749
750
0
    if (pos_params) {
751
0
      if (match_no_params(&recipes[i], req_params)) {
752
0
        if (match) {
753
0
          *params_err = -2;
754
0
          return NULL;
755
0
        } else {
756
0
          match = &recipes[i];
757
0
        }
758
0
      }
759
0
    } else {
760
0
      if (match_no_params(&recipes[i], req_params))
761
0
        *params_err = -3;
762
0
      else
763
0
        continue;
764
765
0
      if (match_named_params(&recipes[i], req_params))
766
0
        return &recipes[i];
767
0
    }
768
0
  }
769
770
0
  return match;
771
0
}
772
773
static mi_response_t *build_err_resp(int code, const char *msg, int msg_len,
774
                const char *details, int details_len)
775
0
{
776
0
  mi_response_t *err_resp;
777
778
0
  err_resp = init_mi_error_extra(code, msg, msg_len, details, details_len);
779
0
  if (!err_resp)
780
0
    LM_ERR("Failed to build MI error response object\n");
781
782
0
  return err_resp;
783
0
}
784
785
mi_response_t *handle_mi_request(mi_request_t *req, struct mi_cmd *cmd,
786
              struct mi_handler *async_hdl)
787
0
{
788
0
  mi_response_t *resp;
789
0
  const mi_recipe_t *cmd_recipe;
790
0
  char *method;
791
0
  mi_params_t cmd_params;
792
0
  int params_err = -1;
793
0
  int pos_params;
794
795
0
  if (!req->req_obj) {  /* error parsing the request JSON text */
796
0
    LM_ERR("Failed to parse the request JSON text\n");
797
0
    return build_err_resp(JSONRPC_PARSE_ERR_CODE,
798
0
          MI_SSTR(JSONRPC_PARSE_ERR_MSG), NULL, 0);
799
0
  }
800
801
0
  if (req->invalid) {  /* invalid jsonrpc request */
802
0
    LM_ERR("Invalid JSON-RPC request\n");
803
0
    return build_err_resp(JSONRPC_INVAL_REQ_CODE,
804
0
          MI_SSTR(JSONRPC_INVAL_REQ_MSG), NULL, 0);
805
0
  }
806
807
0
  if (!cmd) {
808
0
    method = mi_get_req_method(req);
809
0
    resp = build_ambiguous_mi_cmd_resp(method, strlen(method));
810
0
    if (resp)
811
0
      return resp;
812
813
0
    LM_ERR("Command not found\n");
814
0
    return build_err_resp(JSONRPC_NOT_FOUND_CODE,
815
0
        MI_SSTR(JSONRPC_NOT_FOUND_MSG), NULL, 0);
816
0
  }
817
818
0
  pos_params = req->params ? req->params->type & cJSON_Array : 0;
819
0
  if (pos_params && (cmd->flags & MI_NAMED_PARAMS_ONLY)) {
820
0
    LM_ERR("Command only supports named parameters\n");
821
0
    return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
822
0
        MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
823
0
        MI_SSTR(ERR_DET_POS_PARAMS_S));
824
0
  }
825
826
  /* use the correct 'recipe' of the command based
827
   * on the received parameters */
828
0
  cmd_recipe = get_cmd_recipe(cmd->recipes, req->params, pos_params,
829
0
          &params_err);
830
0
  if (!cmd_recipe) {
831
0
    LM_ERR("Invalid parameters\n");
832
0
    if (params_err == -1)
833
0
      return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
834
0
        MI_SSTR(JSONRPC_INVAL_PARAMS_MSG), MI_SSTR(ERR_DET_NO_PARAMS_S));
835
0
    else if (params_err == -2)
836
0
      return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
837
0
        MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
838
0
        MI_SSTR(ERR_DET_AMBIG_CALL_S));
839
0
    else
840
0
      return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
841
0
        MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
842
0
        MI_SSTR(ERR_DET_MATCH_PARAMS_S));
843
0
  }
844
845
0
  cmd_params.item = req->params;
846
0
  cmd_params.list = cmd_recipe->params;
847
848
0
  resp = cmd_recipe->cmd(&cmd_params, async_hdl);
849
850
0
  if (resp == NULL) {
851
0
    LM_ERR("Command failed\n");
852
0
    return build_err_resp(JSONRPC_SERVER_ERR_CODE,
853
0
        MI_SSTR(JSONRPC_SERVER_ERR_MSG), MI_SSTR(ERR_DET_CMD_NULL_S));
854
0
  } else
855
0
    return resp;
856
0
}
857
858
int add_id_to_response(mi_item_t *id, mi_response_t *resp)
859
0
{
860
0
  if (!id) {
861
0
    if (add_mi_null(resp, MI_SSTR(JSONRPC_ID_S)) < 0) {
862
0
      LM_ERR("Failed to add null value to MI item\n");
863
0
      return -1;
864
0
    }
865
866
0
    return 0;
867
0
  }
868
869
0
  switch ((id->type) & 0xFF) {
870
0
    case cJSON_Number:
871
0
      if (add_mi_number(resp, MI_SSTR(JSONRPC_ID_S), id->valueint) < 0) {
872
0
        LM_ERR("Failed to add int value to MI item\n");
873
0
        return -1;
874
0
      }
875
0
      break;
876
0
    case cJSON_String:
877
0
      if (add_mi_string(resp, MI_SSTR(JSONRPC_ID_S), id->valuestring,
878
0
        strlen(id->valuestring)) < 0) {
879
0
        LM_ERR("Failed to add string value to MI item\n");
880
0
        return -1;
881
0
      }
882
0
      break;
883
0
    case cJSON_NULL:
884
0
      if (add_mi_null(resp, MI_SSTR(JSONRPC_ID_S)) < 0) {
885
0
        LM_ERR("Failed to add null value to MI item\n");
886
0
        return -1;
887
0
      }
888
0
      break;
889
0
    default:
890
0
      LM_ERR("'id' must be a String, Number or Null value\n");
891
0
      return -1;
892
0
  }
893
894
0
  return 0;
895
0
}
896
897
static int prepare_mi_response(mi_response_t *resp, mi_item_t *id)
898
0
{
899
0
  mi_item_t *res_err, *res_err_code = NULL;
900
901
0
  res_err = cJSON_GetObjectItem(resp, JSONRPC_ERROR_S);
902
0
  if (res_err) {
903
0
    res_err_code = cJSON_GetObjectItem(res_err, JSONRPC_ERR_CODE_S);
904
0
    if (!res_err_code) {
905
0
      LM_ERR("no error code for MI error response\n");
906
0
      return -1;
907
0
    }
908
0
  }
909
910
0
  if (!id) {
911
    /* this is a jsonrpc notification (no id but valid request otherwise)
912
     * -> no response */
913
0
    if (!res_err)
914
0
      return MI_NO_RPL;
915
916
0
    if (res_err_code->valueint != JSONRPC_PARSE_ERR_CODE &&
917
0
      res_err_code->valueint != JSONRPC_INVAL_REQ_CODE)
918
0
      return MI_NO_RPL;
919
0
  }
920
921
0
  if (add_id_to_response(id, resp) < 0)
922
0
    return -1;
923
924
0
  return 0;
925
0
}
926
927
int print_mi_response(mi_response_t *resp, mi_item_t *id, str *buf, int pretty)
928
0
{
929
0
  int ret = prepare_mi_response(resp, id);
930
931
0
  if (ret != 0)
932
0
    return ret;
933
934
0
  if (cJSON_PrintPreallocated(resp, buf->s, buf->len, pretty) == 0) {
935
0
    LM_ERR("Failed to print JSON\n");
936
0
    return -1;
937
0
  }
938
939
0
  return 0;
940
0
}
941
942
int print_mi_response_flush(mi_response_t *resp, mi_item_t *id,
943
    mi_flush_f *func, void *func_p, str *buf, int pretty)
944
0
{
945
0
  int ret = prepare_mi_response(resp, id);
946
947
0
  if (ret != 0)
948
0
    return ret;
949
950
0
  if (cJSON_PrintFlushed(resp, buf->s, buf->len, pretty, func, func_p) == 0) {
951
0
    LM_ERR("Failed to print JSON\n");
952
0
    return -1;
953
0
  }
954
955
0
  return 0;
956
0
}
957
958
void free_mi_request_parsed(mi_request_t *request)
959
0
{
960
0
  _init_mi_sys_mem_hooks();
961
962
0
  if (request->req_obj)
963
0
    cJSON_Delete(request->req_obj);
964
965
0
  _init_mi_pkg_mem_hooks();
966
0
}
967
968
969
#define MI_HELP_STR "Usage: help mi_cmd - " \
970
  "returns information about 'mi_cmd'"
971
#define MI_UNKNOWN_CMD "unknown MI command"
972
#define MI_NO_HELP "not available for this command"
973
#define MI_MODULE_STR "by \"%.*s\" module"
974
975
mi_response_t *w_mi_help(const mi_params_t *params,
976
              struct mi_handler *async_hdl)
977
0
{
978
0
  return init_mi_result_string(MI_SSTR(MI_HELP_STR));
979
0
}
980
981
mi_response_t *w_mi_help_1(const mi_params_t *params,
982
                struct mi_handler *async_hdl)
983
0
{
984
0
  mi_response_t *resp;
985
0
  mi_item_t *resp_obj;
986
0
  struct mi_cmd *cmd;
987
0
  str cmd_s;
988
989
0
  if (get_mi_string_param(params, "mi_cmd", &cmd_s.s, &cmd_s.len) < 0)
990
0
    return init_mi_param_error();
991
992
  /* search the command */
993
0
  cmd = lookup_mi_cmd(cmd_s.s, cmd_s.len);
994
0
  if (!cmd)
995
0
    return init_mi_error(404, MI_SSTR(MI_UNKNOWN_CMD));
996
997
0
  resp = init_mi_result_object(&resp_obj);
998
0
  if (!resp)
999
0
    return 0;
1000
1001
0
  if (cmd->help.s) {
1002
0
    if (add_mi_string(resp_obj, MI_SSTR("Help"), cmd->help.s, cmd->help.len)
1003
0
      < 0) {
1004
0
      LM_ERR("cannot add mi item\n");
1005
0
      goto error;
1006
0
    }
1007
0
  } else {
1008
0
    if (add_mi_string(resp_obj, MI_SSTR("Help"), MI_SSTR(MI_NO_HELP)) < 0) {
1009
0
      LM_ERR("cannot add mi item\n");
1010
0
      goto error;
1011
0
    }
1012
0
  }
1013
1014
0
  if (cmd->module.len && cmd->module.s && add_mi_string(resp_obj,
1015
0
    MI_SSTR("Exported by"), cmd->module.s, cmd->module.len) < 0) {
1016
0
    LM_ERR("cannot add mi item\n");
1017
0
    goto error;
1018
0
  }
1019
1020
0
  return resp;
1021
1022
0
error:
1023
0
  free_mi_response(resp);
1024
0
  return 0;
1025
0
}