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 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
// Copyright (c) 2001-2011 XORP, Inc and Others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2, June
// 1991 as published by the Free Software Foundation. Redistribution
// and/or modification of this program under the terms of any other
// version of the GNU General Public License is not permitted.
//
// This program 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. For more details,
// see the GNU General Public License, Version 2, a copy of which can be
// found in the XORP LICENSE.gpl file.
//
// XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net
//
// CLI (Command-Line Interface) implementation for XORP.
//
#include "cli_module.h"
#include "libxorp/xorp.h"
#include "libxorp/xlog.h"
#include "libxorp/debug.h"
#include "libxorp/ipvx.hh"
#include "libxorp/ipvxnet.hh"
#include "libxorp/token.hh"
#include "libxorp/utils.hh"
#include "libcomm/comm_api.h"
#include "cli_client.hh"
#include "cli_node.hh"
#include "cli_private.hh"
//
// Exported variables
//
//
// Local constants definitions
//
//
// Local structures/classes, typedefs and macros
//
//
// Local variables
//
//
// Local functions prototypes
//
/**
* CliNode::CliNode:
* @init_family: The address family (%AF_INET or %AF_INET6
* for IPv4 and IPv6 respectively).
* @init_module_id: The module ID (must be %XORP_MODULE_CLI).
* @init_eventloop: The event loop.
*
* CLI node constructor.
**/
CliNode::CliNode(int init_family, xorp_module_id module_id,
EventLoop& init_eventloop)
: ProtoNode<Vif>(init_family, module_id, init_eventloop),
_cli_port(0), // XXX: not defined yet
_next_session_id(0),
_startup_cli_prompt(XORP_CLI_PROMPT),
_cli_command_root(NULL, "", ""),
_is_log_trace(false)
{
string error_msg;
if (module_id != XORP_MODULE_CLI) {
XLOG_FATAL("Invalid module ID = %d (must be 'XORP_MODULE_CLI' = %d)",
module_id, XORP_MODULE_CLI);
}
cli_command_root()->set_allow_cd(true, _startup_cli_prompt);
cli_command_root()->create_default_cli_commands();
if (cli_command_root()->add_pipes(error_msg) != XORP_OK) {
XLOG_FATAL("Cannot add command pipes: %s", error_msg.c_str());
}
}
/**
* CliNode::~CliNode:
* @:
*
* CLI node destructor.
**/
CliNode::~CliNode()
{
stop();
// TODO: perform CLI-specific operations.
}
int
CliNode::start()
{
string error_msg;
if (! is_enabled())
return (XORP_OK);
if (is_up() || is_pending_up())
return (XORP_OK);
if (ProtoNode<Vif>::start() != XORP_OK)
return (XORP_ERROR);
// Perform misc. CLI-specific start operations
//
// Start accepting connections
//
if (_cli_port != 0) {
if (sock_serv_open().is_valid()) {
eventloop().add_ioevent_cb(_cli_socket, IOT_ACCEPT,
callback(this, &CliNode::accept_connection),
XorpTask::PRIORITY_HIGHEST);
}
}
if (add_internal_cli_commands(error_msg) != XORP_OK) {
XLOG_ERROR("Cannot add internal CLI commands: %s", error_msg.c_str());
return (XORP_ERROR);
}
XLOG_TRACE(is_log_trace(), "CLI started");
return (XORP_OK);
}
/**
* CliNode::stop:
* @:
*
* Stop the CLI operation.
*
* Return value: %XORP_OK on success, otherwise %XORP_ERROR.
**/
int
CliNode::stop()
{
if (is_down())
return (XORP_OK);
if (! is_up())
return (XORP_ERROR);
if (ProtoNode<Vif>::pending_stop() != XORP_OK)
return (XORP_ERROR);
// Perform misc. CLI-specific stop operations
// TODO: add as necessary
delete_pointers_list(_client_list);
if (_cli_socket.is_valid())
eventloop().remove_ioevent_cb(_cli_socket, IOT_ACCEPT);
sock_serv_close();
if (ProtoNode<Vif>::stop() != XORP_OK)
return (XORP_ERROR);
XLOG_TRACE(is_log_trace(), "CLI stopped");
return (XORP_OK);
}
/**
* Enable the node operation.
*
* If an unit is not enabled, it cannot be start, or pending-start.
*/
void
CliNode::enable()
{
ProtoUnit::enable();
XLOG_TRACE(is_log_trace(), "CLI enabled");
}
/**
* Disable the node operation.
*
* If an unit is disabled, it cannot be start or pending-start.
* If the unit was runnning, it will be stop first.
*/
void
CliNode::disable()
{
stop();
ProtoUnit::disable();
XLOG_TRACE(is_log_trace(), "CLI disabled");
}
/**
* CliNode::add_enable_cli_access_from_subnet:
* @subnet_addr: The subnet address to add.
*
* Add a subnet address to the list of subnet addresses enabled
* for CLI access.
* This method can be called more than once to add a number of
* subnet addresses.
**/
void
CliNode::add_enable_cli_access_from_subnet(const IPvXNet& subnet_addr)
{
list<IPvXNet>::iterator iter;
// Test if we have this subnet already
for (iter = _enable_cli_access_subnet_list.begin();
iter != _enable_cli_access_subnet_list.end();
++iter) {
const IPvXNet& tmp_ipvxnet = *iter;
if (tmp_ipvxnet == subnet_addr)
return; // Subnet address already added
}
_enable_cli_access_subnet_list.push_back(subnet_addr);
}
/**
* CliNode::delete_enable_cli_access_from_subnet:
* @subnet_addr: The subnet address to delete.
*
* Delete a subnet address from the list of subnet addresses enabled
* for CLI access.
*
* Return value: %XORP_OK on success, otherwise %XORP_ERROR.
**/
int
CliNode::delete_enable_cli_access_from_subnet(const IPvXNet& subnet_addr)
{
list<IPvXNet>::iterator iter;
// Test if we have this subnet already
for (iter = _enable_cli_access_subnet_list.begin();
iter != _enable_cli_access_subnet_list.end();
++iter) {
const IPvXNet& tmp_ipvxnet = *iter;
if (tmp_ipvxnet == subnet_addr) {
_enable_cli_access_subnet_list.erase(iter);
return (XORP_OK); // Entry erased
}
}
return (XORP_ERROR); // No entry found
}
/**
* CliNode::add_disable_cli_access_from_subnet:
* @subnet_addr: The subnet address to add.
*
* Add a subnet address to the list of subnet addresses disabled
* for CLI access.
* This method can be called more than once to add a number of
* subnet addresses.
**/
void
CliNode::add_disable_cli_access_from_subnet(const IPvXNet& subnet_addr)
{
list<IPvXNet>::iterator iter;
// Test if we have this subnet already
for (iter = _disable_cli_access_subnet_list.begin();
iter != _disable_cli_access_subnet_list.end();
++iter) {
const IPvXNet& tmp_ipvxnet = *iter;
if (tmp_ipvxnet == subnet_addr)
return; // Subnet address already added
}
_disable_cli_access_subnet_list.push_back(subnet_addr);
}
/**
* CliNode::delete_disable_cli_access_from_subnet:
* @subnet_addr: The subnet address to delete.
*
* Delete a subnet address from the list of subnet addresses disabled
* for CLI access.
*
* Return value: %XORP_OK on success, otherwise %XORP_ERROR.
**/
int
CliNode::delete_disable_cli_access_from_subnet(const IPvXNet& subnet_addr)
{
list<IPvXNet>::iterator iter;
// Test if we have this subnet already
for (iter = _disable_cli_access_subnet_list.begin();
iter != _disable_cli_access_subnet_list.end();
++iter) {
const IPvXNet& tmp_ipvxnet = *iter;
if (tmp_ipvxnet == subnet_addr) {
_disable_cli_access_subnet_list.erase(iter);
return (XORP_OK); // Entry erased
}
}
return (XORP_ERROR); // No entry found
}
bool
CliNode::is_allow_cli_access(const IPvX& ipvx) const
{
list<IPvXNet>::const_iterator iter;
IPvXNet best_enable = IPvXNet(IPvX::ZERO(ipvx.af()), 0);
IPvXNet best_disable = IPvXNet(IPvX::ZERO(ipvx.af()), 0);
bool best_enable_found = false;
bool best_disable_found = false;
// Find the longest-match subnet address that may enable access
for (iter = _enable_cli_access_subnet_list.begin();
iter != _enable_cli_access_subnet_list.end();
++iter) {
const IPvXNet& ipvxnet = *iter;
if (ipvx.af() != ipvxnet.masked_addr().af())
continue;
if (! ipvxnet.contains(ipvx))
continue;
if (best_enable.contains(ipvxnet))
best_enable = ipvxnet;
best_enable_found = true;
}
// Find the longest-match subnet address that may disable access
for (iter = _disable_cli_access_subnet_list.begin();
iter != _disable_cli_access_subnet_list.end();
++iter) {
const IPvXNet& ipvxnet = *iter;
if (ipvx.af() != ipvxnet.masked_addr().af())
continue;
if (! ipvxnet.contains(ipvx))
continue;
if (best_disable.contains(ipvxnet))
best_disable = ipvxnet;
best_disable_found = true;
}
if (! best_disable_found) {
// XXX: no disable match, so enable access by default
return (true);
}
if (! best_enable_found) {
// XXX: no enable match, so definitely disable
return (false);
}
// Both enable and disable match
if (best_enable.prefix_len() > best_disable.prefix_len())
return (true);
return (false); // XXX: disable even if conflicting config
}
/**
* CliNode::find_cli_by_term_name:
* @term_name: The CLI terminal name to search for.
*
* Find a CLI client #CliClient for a given terminal name.
*
* Return value: The CLI client with name of @term_name on success,
* otherwise NULL.
**/
CliClient *
CliNode::find_cli_by_term_name(const string& term_name) const
{
list<CliClient *>::const_iterator iter;
for (iter = _client_list.begin(); iter != _client_list.end(); ++ iter) {
CliClient *cli_client = *iter;
if (term_name == cli_client->cli_session_term_name()) {
return (cli_client);
}
}
return (NULL);
}
/**
* CliNode::find_cli_by_session_id:
* @session_id: The CLI session ID to search for.
*
* Find a CLI client #CliClient for a given session ID.
*
* Return value: The CLI client with session ID of @session_id on success,
* otherwise NULL.
**/
CliClient *
CliNode::find_cli_by_session_id(uint32_t session_id) const
{
list<CliClient *>::const_iterator iter;
for (iter = _client_list.begin();
iter != _client_list.end();
++ iter) {
CliClient *cli_client = *iter;
if (cli_client->cli_session_session_id() == session_id)
return (cli_client);
}
return (NULL);
}
/**
* CliNode::xlog_output:
* @obj: The #CliClient object to apply this function to.
* @msg: A C-style string with the message to output.
*
* Output a log message to a #CliClient object.
*
* Return value: On success, the number of characters printed,
* otherwise %XORP_ERROR.
**/
int
CliNode::xlog_output(void *obj, xlog_level_t level, const char *msg)
{
CliClient *cli_client = static_cast<CliClient *>(obj);
int ret_value = cli_client->cli_print(msg);
if (ret_value >= 0 &&
cli_client->cli_print("") >= 0 && cli_client->cli_flush() == 0) {
return ret_value;
}
return XORP_ERROR;
UNUSED(level);
}
//
// CLI add_cli_command
//
int
CliNode::add_cli_command(
// Input values,
const string& processor_name,
const string& command_name,
const string& command_help,
const bool& is_command_cd,
const string& command_cd_prompt,
const bool& is_command_processor,
// Output values,
string& error_msg)
{
// Reset the error message
error_msg = "";
//
// Check the request
//
if (command_name.empty()) {
error_msg = "ERROR: command name is empty";
return (XORP_ERROR);
}
CliCommand *c0 = cli_command_root();
CliCommand *c1 = NULL;
if (! is_command_processor) {
if (is_command_cd) {
c1 = c0->add_command(command_name, command_help,
command_cd_prompt, true, error_msg);
} else {
c1 = c0->add_command(command_name, command_help, true, error_msg);
}
} else {
// Command processor
c1 = c0->add_command(command_name, command_help, true,
callback(this, &CliNode::send_process_command),
error_msg);
if (c1 != NULL)
c1->set_can_pipe(true);
}
//
// TODO: return the result of the command installation
//
if (c1 == NULL) {
error_msg = c_format("Cannot install command '%s': %s",
command_name.c_str(),
error_msg.c_str());
return (XORP_ERROR);
}
c1->set_global_name(token_line2vector(command_name));
c1->set_server_name(processor_name);
return (XORP_OK);
}
//
// CLI delete_cli_command
//
int
CliNode::delete_cli_command(
// Input values,
const string& processor_name,
const string& command_name,
// Output values,
string& error_msg)
{
// Reset the error message
error_msg = "";
//
// Check the request
//
if (command_name.empty()) {
error_msg = "ERROR: command name is empty";
return (XORP_ERROR);
}
CliCommand *c0 = cli_command_root();
if (c0->delete_command(command_name) != XORP_OK) {
error_msg = c_format("Cannot delete command '%s'",
command_name.c_str());
return (XORP_ERROR);
}
UNUSED(processor_name);
return (XORP_OK);
}
//
// Send a command request to a remote node
//
int
CliNode::send_process_command(const string& server_name,
const string& cli_term_name,
uint32_t cli_session_id,
const vector<string>& command_global_name,
const vector<string>& argv)
{
if (server_name.empty())
return (XORP_ERROR);
if (cli_term_name.empty())
return (XORP_ERROR);
if (command_global_name.empty())
return (XORP_ERROR);
CliClient *cli_client = find_cli_by_session_id(cli_session_id);
if (cli_client == NULL)
return (XORP_ERROR);
if (cli_client != find_cli_by_term_name(cli_term_name))
return (XORP_ERROR);
//
// Send the request
//
if (! _send_process_command_callback.is_empty()) {
(_send_process_command_callback)->dispatch(server_name,
server_name,
cli_term_name,
cli_session_id,
command_global_name,
argv);
}
cli_client->set_is_waiting_for_data(true);
return (XORP_OK);
}
//
// Process the response of a command processed by a remote node
//
void
CliNode::recv_process_command_output(const string * , // processor_name,
const string *cli_term_name,
const uint32_t *cli_session_id,
const string *command_output)
{
//
// Find if a client is waiting for that command
//
if ((cli_term_name == NULL) || (cli_session_id == NULL))
return;
CliClient *cli_client = find_cli_by_session_id(*cli_session_id);
if (cli_client == NULL)
return;
if (cli_client != find_cli_by_term_name(*cli_term_name))
return;
if (! cli_client->is_waiting_for_data()) {
// ERROR: client is not waiting for that data (e.g., probably too late)
return;
}
//
// Print the result and reset client status
//
if (command_output != NULL) {
cli_client->cli_print(c_format("%s", command_output->c_str()));
}
cli_client->cli_flush();
cli_client->set_is_waiting_for_data(false);
cli_client->post_process_command();
}
CliClient *
CliNode::add_client(XorpFd input_fd, XorpFd output_fd, bool is_network,
const string& startup_cli_prompt, string& error_msg)
{
return (add_connection(input_fd, output_fd, is_network,
startup_cli_prompt, error_msg));
}
int
CliNode::remove_client(CliClient *cli_client, string& error_msg)
{
if (delete_connection(cli_client, error_msg) != XORP_OK)
return (XORP_ERROR);
// XXX: Remove the client itself if it is still around
list<CliClient *>::iterator iter;
iter = find(_client_list.begin(), _client_list.end(), cli_client);
if (iter != _client_list.end()) {
_client_list.erase(iter);
}
return (XORP_OK);
}
|