summaryrefslogtreecommitdiffstats
path: root/lib/command_execute.c
blob: cf04bb314a6b15818f73487f019f58bb1a252a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* Command Line Execution
 * Copyright (C) 1997, 98 Kunihiro Ishiguro
 *
 * Recast and extended: Copyright (C) 2010 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your
 * option) any later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "misc.h"

#include "command_local.h"
#include "command_parse.h"
#include "command_execute.h"
#include "command_queue.h"
#include "vty_common.h"
#include "vty_command.h"
#include "memory.h"

/*==============================================================================
 * Construct and destroy cmd_exec object.
 *
 *
 */

/*------------------------------------------------------------------------------
 * Construct cmd_exec object and initialise.
 *
 * This is done when a command loop is entered.
 *
 * The initial cmd_context reflects the vty->type and initial vty->node.
 */
extern cmd_exec
cmd_exec_new(vty vty)
{
  cmd_exec     exec ;
  cmd_context  context ;

  exec = XCALLOC(MTYPE_CMD_EXEC, sizeof(struct cmd_exec)) ;

  /* Zeroising has set:
   *
   *   vty          = X         -- set below
   *
   *   action       = all zeros
   *
   *   context      = NULL      -- see below
   *
   *   parsed       = NULL      -- see below
   *
   *   password_failures   = 0  -- none, yet
   *
   *   state        = exec_null
   *   locus        = NULL      -- not significant in exec_null
   *   ret          = CMD_SUCCESS
   *
   *   cq           = NULL      -- no mqb (qpthreads)
   *                            -- no thread (legacy thread)
   */
  confirm(CMD_ACTION_ALL_ZEROS) ;       /* action       */
  confirm(exec_null == 0) ;             /* state        */
  confirm(CMD_SUCCESS == 0) ;           /* ret          */

  exec->vty     = vty ;

  exec->parsed  = cmd_parsed_new() ;

  /* Initialise the context                                             */
  VTY_ASSERT_LOCKED() ;

  exec->context = context = cmd_context_new() ;

  context->node            = vty->node ;

  context->parse_execution = true ;
  context->parse_only      = false ;
  context->reflect_enabled = false ;

  context->onode           = NULL_NODE ;

  context->dir_cd   = qpath_dup(host.cwd) ;
  context->dir_home = qpath_dup(host.config_dir) ;
  context->dir_here = qpath_dup(host.config_dir) ;

  switch (vty->type)
    {
      case VTY_TERMINAL:
        context->full_lex      = true ;

        context->parse_strict  = false ;
        context->parse_no_do   = false ;
        context->parse_no_tree = false ;

        context->can_enable    = context->node >= ENABLE_NODE ;
        context->can_auth_enable = true ;

        break ;

      case VTY_SHELL_SERVER:
        zabort("missing VTY_SHELL_SERVER context") ;

        context->full_lex      = true ;

        context->parse_strict  = false ;
        context->parse_no_do   = false ;
        context->parse_no_tree = false ;

        context->can_enable    = true ;
        context->can_auth_enable = false ;

        break ;

      case VTY_CONFIG_READ:
        context->full_lex      = false ;

        context->parse_strict  = true ;
        context->parse_no_do   = true ;
        context->parse_no_tree = false ;

        context->can_enable    = true ;
        context->can_auth_enable = false ;

        break ;

      default:
        zabort("vty type unknown to cmd_exec_new()") ;
    } ;

  return exec ;
} ;

/*------------------------------------------------------------------------------
 * Make a new context -- returns a completely empty context object.
 */
extern cmd_context
cmd_context_new(void)
{
  return XCALLOC(MTYPE_CMD_EXEC, sizeof(struct cmd_context)) ;
} ;

/*------------------------------------------------------------------------------
 * Save current context and update for new, child vin.
 *
 *   - updates the dir_here if required
 *
 *   - cannot inherit can_enable unless is ENABLE_NODE or better
 *
 *   - cannot inherit can_auth_enable, no how
 */
extern cmd_context
cmd_context_new_save(cmd_context context, qpath file_here)
{
  cmd_context  saved ;

  saved = cmd_context_new() ;

  *saved = *context ;         /* copy as is           */

  /* The saved copy of the context now owns the current paths, so now need
   * to duplicate (or set new) paths.
   */
  context->dir_cd   = qpath_dup(saved->dir_cd) ;
  context->dir_home = qpath_dup(saved->dir_home) ;

  if (file_here == NULL)
    context->dir_here = qpath_dup(saved->dir_here) ;
  else
    {
      context->dir_here = qpath_dup(file_here) ;
      qpath_shave(context->dir_here) ;
    } ;

  /* The inheritance of can_enable is tricky.  Will not bequeath can_enable
   * is is not currently in ENABLE_NODE or better !
   */
  if (context->node <= ENABLE_NODE)
    context->can_enable = false ;

  context->can_auth_enable = false ;

  return saved ;
} ;

/*------------------------------------------------------------------------------
 * Restore given context -- frees the copy restored from.
 *
 * Has to free the directories in the context being restored to.
 *
 * Note that can restore any node it likes: if it was enabled, that's fine; if
 * it was in some config mode, will still have the symbol of power because
 * only at vin_depth <= 1 is the symbol of power actually released.
 *
 * Returns NULL.
 */
extern cmd_context
cmd_context_restore(cmd_context dst, cmd_context src)
{
  assert(src != NULL) ;

  qpath_free(dst->dir_cd) ;
  qpath_free(dst->dir_home) ;
  qpath_free(dst->dir_here) ;

  *dst = *src ;         /* copy as is           */

  return cmd_context_free(src, true) ;
} ;

/*------------------------------------------------------------------------------
 * Free the given context.
 *
 * If the context is a copy of an existing context, then must not free the
 * directories -- they will be freed when that existing context is freed.
 */
extern cmd_context
cmd_context_free(cmd_context context, bool copy)
{
  if (context != NULL)
    {
      if (!copy)
        {
          qpath_free(context->dir_cd) ;
          qpath_free(context->dir_home) ;
          qpath_free(context->dir_here) ;
        } ;

      XFREE(MTYPE_CMD_EXEC, context) ;
    } ;

  return NULL ;
} ;

/*------------------------------------------------------------------------------
 * Destroy cmd_exec object.
 */
extern cmd_exec
cmd_exec_free(cmd_exec exec)
{
  if (exec != NULL)
    {
      exec->vty = NULL ;        /* no longer required.  */

      exec->context = cmd_context_free(exec->context, false) ;  /* not a copy */
      cmd_action_clear(exec->action) ;
      exec->parsed  = cmd_parsed_free(exec->parsed) ;

      if    (vty_nexus)
        exec->cq.mqb = mqb_free(exec->cq.mqb) ;
      else if (exec->cq.thread != NULL)
        {
          thread_cancel(exec->cq.thread) ;
          exec->cq.thread = NULL ;
        } ;

      XFREE(MTYPE_CMD_EXEC, exec) ;
    } ;

  return NULL ;
} ;

/*==============================================================================
 *
 */
/*------------------------------------------------------------------------------
 * Read configuration from file.
 *
 * In the qpthreads world this assumes that it is running with the vty
 * locked, and that all commands are to be executed directly.
 *
 * If the 'first_cmd' argument is not NULL it is the address of the first
 * command that is expected to appear.  If the first command is not this, then
 * the 'first_cmd' is called with argv == NULL (and argc == 0) to signal the
 * command is being invoked by default.
 *
 * Command processing continues while CMD_SUCCESS is returned by the command
 * parser and command execution.
 *
 * If 'ignore_warning' is set, then any CMD_WARNING returned by command
 * execution is converted to CMD_SUCCESS.  Note that any CMD_WARNING returned
 * by command parsing (or in execution of any default 'first_cmd').
 *
 * Returns: cmd_return_code for last command or I/O operation
 *
 *          when returns, the entire vin/vout stack will have been closed.
 *
 * If reaches EOF on the config file, returns CMD_SUCCESS.  If anything else
 * happens, will generate an error message and exits the command loop.
 */
extern cmd_return_code_t
cmd_read_config(struct vty *vty, cmd_command first_cmd, bool ignore_warning)
{
  cmd_exec          exec   = vty->exec ;
  cmd_parsed        parsed = exec->parsed ;
  cmd_return_code_t ret, hret ;

  ret = CMD_SUCCESS ;           /* so far, so good      */

  while (1)
    {
      /* Deal with anything which is not success !
       */
      if ((ret != CMD_SUCCESS) && (ret != CMD_EMPTY))
        {
          /* Will drop straight out of the loop if have anything other
           * than CMD_HIATUS, CMD_EOF or CMD_CLOSE, which are all signals
           * that some adjustment to the vin/vout stacks is required,
           * or that we are all done here.
           *
           * Everything else is deemed to be an error that stops the
           * command loop.
           */
          if ((ret != CMD_HIATUS) && (ret != CMD_EOF) && (ret != CMD_CLOSE))
            break ;

          ret = vty_cmd_hiatus(vty, ret) ;

          if (ret == CMD_STOP)
            return CMD_SUCCESS ;        /* the expected outcome !       */

          if (ret != CMD_SUCCESS)
            break ;
        } ;

      /* If all is well, need another command line                      */

      ret = vty_cmd_fetch_line(vty) ;   /* sets exec->action    */
      if (ret != CMD_SUCCESS)
        continue ;

      /* Parse the command line we now have                             */
      assert(exec->action->to_do == cmd_do_command) ;

      cmd_tokenize(parsed, exec->action->line, exec->context->full_lex) ;
      ret = cmd_parse_command(parsed, exec->context) ;

      if (ret != CMD_SUCCESS)
        continue ;

      /* Special handling before first active line.                     */
      if (first_cmd != NULL)
        {
          if (first_cmd != parsed->cmd)
            {
              ret = (*first_cmd->func)(first_cmd, vty, -1, NULL) ;
              if (ret != CMD_SUCCESS)
                continue ;
            } ;
          first_cmd = NULL ;
        } ;

      /* reflection now.....                                            */
      if (exec->reflect)
        {
          ret = vty_cmd_reflect_line(vty) ;
          if (ret != CMD_SUCCESS)
            continue ;
        } ;

      /* Pipe work, if any                                              */
      if ((parsed->parts & cmd_parts_pipe) != 0)
        {
          ret = cmd_open_pipes(vty) ;
          if (ret != CMD_SUCCESS)
            continue ;
        } ;

      /* Command execution, if any                                      */
      if ((parsed->parts & cmd_part_command) != 0)
        ret = cmd_execute(vty) ;

      /* Deal with success (or suppressed warning).                     */
      if ((ret == CMD_SUCCESS) || ((ret == CMD_WARNING) && ignore_warning))
        ret = vty_cmd_success(vty) ;
    } ;

  /* Arrives here if:
   *
   *   - vty_cmd_fetch_line() returns anything except CMD_SUCCESS, CMD_EOF or
   *     CMD_HIATUS -- which are not errors.
   *
   *   - any other operation returns anything except CMD_SUCCESS
   *     (or CMD_WARNING, if they are being ignored), CMD_EOF or CMD_CLOSE.
   *
   * Deal with any errors -- generate suitable error messages and close back
   * to (but excluding) vout_base.
   *
   * CMD_SUCCESS and CMD_EMPTY are impossible at this point -- they should
   * have been dealt with in the loop.
   *
   * CMD_EOF is also impossible -- vty_cmd_fetch_line() or vty_cmd_hiatus()
   * can return that, but that will have been dealt with.
   *
   * CMD_CLOSE is also impossible -- will have been given to vty_cmd_hiatus(),
   * which never returns it.
   *
   * CMD_WAITING is not valid for blocking vio !
   */
  qassert(ret != CMD_SUCCESS) ;
  qassert(ret != CMD_EMPTY) ;
  qassert(ret != CMD_EOF) ;
  qassert(ret != CMD_CLOSE) ;
  qassert(ret != CMD_WAITING) ;

  hret = ret ;
  do
    hret = vty_cmd_hiatus(vty, hret) ;
  while (hret == CMD_IO_ERROR) ;

  return ret ;
} ;

/*==============================================================================
 */

/*------------------------------------------------------------------------------
 * Open in and/or out pipes
 *
 *  * Returns:
 *
 *   - OK       -- CMD_SUCCESS
 *   - error    -- CMD_ERROR, etc
 */
extern cmd_return_code_t
cmd_open_pipes(vty vty)
{
  cmd_exec          exec   = vty->exec ;
  cmd_parsed        parsed = exec->parsed ;
  cmd_return_code_t ret ;
  vty_io            vio ;
  bool              after ;
  VTY_LOCK() ;
  vio = vty->vio ;

  ret = CMD_SUCCESS ;

  after = false ;               /* assuming no in pipe                  */

  /* Deal with any in pipe stuff                                        */
  if ((parsed->parts & cmd_part_in_pipe) != 0)
    {
      qstring args ;

      args = cmd_tokens_concat(parsed, parsed->first_in_pipe,
                                       parsed->num_in_pipe) ;

      if      ((parsed->in_pipe & cmd_pipe_file) != 0)
        ret = uty_cmd_open_in_pipe_file(vio, exec->context, args,
                                                              parsed->in_pipe) ;
      else if ((parsed->in_pipe & cmd_pipe_shell) != 0)
        ret = uty_cmd_open_in_pipe_shell(vio, exec->context, args,
                                                              parsed->in_pipe) ;
      else
        zabort("invalid in pipe state") ;

      qs_free(args) ;

      after = true ;
    } ;

  /* Deal with any out pipe stuff                                       */
  if (((parsed->parts & cmd_part_out_pipe) != 0) && (ret == CMD_SUCCESS))
    {
      qstring args ;

      args = cmd_tokens_concat(parsed, parsed->first_out_pipe,
                                       parsed->num_out_pipe) ;

      if      ((parsed->out_pipe & cmd_pipe_file) != 0)
        ret = uty_cmd_open_out_pipe_file(vio, exec->context, args,
                                                      parsed->out_pipe, after) ;
      else if ((parsed->out_pipe & cmd_pipe_shell) != 0)
        ret = uty_cmd_open_out_pipe_shell(vio, exec->context, args,
                                                      parsed->out_pipe, after) ;
      else if ((parsed->out_pipe & cmd_pipe_dev_null) != 0)
        ret = uty_cmd_open_out_dev_null(vio, after) ;
      else
        zabort("invalid out pipe state") ;

      qs_free(args) ;
    } ;

  VTY_UNLOCK() ;
  return ret ;
} ;

/*------------------------------------------------------------------------------
 * Command Execution.
 *
 * If !parse_only, set vty->node and dispatch the command.
 *
 * Returns: CMD_SUCCESS   -- it all want very well
 *          CMD_WARNING   -- not so good: warning message sent by vty_out()
 *          CMD_ERROR     -- not so good: warning message sent by vty_out()
 *          CMD_EOF       -- close the current input
 *
 * NB: the distinction between CMD_WARNING and CMD_ERROR is that CMD_WARNING
 *     may be ignored when reading a configuration file.
 *
 * NB: no other returns are acceptable !
 */
extern cmd_return_code_t
cmd_execute(vty vty)
{
  cmd_parsed  parsed  = vty->exec->parsed ;
  cmd_context context = vty->exec->context ;
  cmd_command cmd     = parsed->cmd ;

  cmd_return_code_t ret ;

  vty->node = parsed->cnode ;

  if (context->parse_only)
    ret = CMD_SUCCESS ;
  else
    ret = (*(cmd->func))(cmd, vty, cmd_arg_vector_argc(parsed),
                                   cmd_arg_vector_argv(parsed)) ;

  if (ret == CMD_SUCCESS)
    {
      /* If the node is changed by the command, do that now and make sure
       * that the configuration symbol of power is straight.
       *
       * If the new node is >= CONFIG_NODE, then MUST already have acquired
       * the symbol of power (otherwise the command would have failed !)
       *
       * If the new node is < CONFIG_NODE, then we will here release the
       * symbol of power iff we are at the vin_base !
       *
       * If the new node is NULL_NODE, then treat as CMD_EOF.
       */
      if (context->node != parsed->nnode)
        {
          context->node = parsed->nnode ;
          vty_cmd_config_lock_check(vty, context->node) ;

          if (context->node == NULL_NODE)
            ret = CMD_EOF ;
        } ;

      /* The command should (no longer) change the vty->node, but if it does,
       * it had better be to the same as what the parser expected -- for if
       * not, that will break "parse_only" and generally cause confusion.
       */
      qassert((vty->node == parsed->cnode) || (vty->node == parsed->nnode)) ;
    }
  else
    {
      /* Enforce restrictions on return codes.                          */
      assert((ret == CMD_WARNING) || (ret == CMD_ERROR)
                                  || (ret == CMD_EOF)) ;
    } ;

  return ret ;
} ;