Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
net_cli.c File Reference
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <memory.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include <regex.h>
#include <sys/socket.h>
#include "net_cli.h"
#include "utils/rlutil.h"

Macros

#define _GNU_SOURCE
 KSnet Network CLI module. More...
 
#define UNUSED(d)   d
 
#define MATCH_REGEX   1
 
#define MATCH_INVERT   2
 
#define free_z(p)   do { if (p) { free(p); (p) = 0; } } while (0)
 
#define DES_PREFIX   "{crypt}" /* to distinguish clear text from DES crypted */
 
#define MD5_PREFIX   "$1$"
 
#define CTRL(c)   (c - '@')
 

Enumerations

enum  cli_states {
  STATE_LOGIN, STATE_PASSWORD, STATE_NORMAL, STATE_ENABLE_PASSWORD,
  STATE_ENABLE
}
 

Functions

int cli_match_filter_init (struct cli_def *cli, int argc, char **argv, struct cli_filter *filt)
 
int cli_range_filter_init (struct cli_def *cli, int argc, char **argv, struct cli_filter *filt)
 
int cli_count_filter_init (struct cli_def *cli, int argc, char **argv, struct cli_filter *filt)
 
int cli_match_filter (struct cli_def *cli, const char *string, void *data)
 
int cli_range_filter (struct cli_def *cli, const char *string, void *data)
 
int cli_count_filter (struct cli_def *cli, const char *string, void *data)
 
char * cli_command_name (struct cli_def *cli, struct cli_command *command)
 
void cli_set_auth_callback (struct cli_def *cli, int(*auth_callback)(const char *, const char *))
 Enables or disables callback based authentication. More...
 
void cli_set_enable_callback (struct cli_def *cli, int(*enable_callback)(const char *))
 Just like cli_set_auth_callback, this takes a pointer to a callback function to authorize privileged access. More...
 
void cli_allow_user (struct cli_def *cli, const char *username, const char *password)
 Enables internal authentication, and adds username/password to the list of allowed users. More...
 
void cli_allow_enable (struct cli_def *cli, const char *password)
 This will allow a static password to be used for the enable command. More...
 
void cli_deny_user (struct cli_def *cli, const char *username)
 Removes username/password from the list of allowed users. More...
 
void cli_set_banner (struct cli_def *cli, const char *banner)
 Sets the greeting that clients will be presented with when they connect. More...
 
void cli_set_hostname (struct cli_def *cli, const char *hostname)
 Sets the hostname to be displayed as the first part of the prompt. More...
 
void cli_set_promptchar (struct cli_def *cli, const char *promptchar)
 
int cli_set_privilege (struct cli_def *cli, int priv)
 
void cli_set_modestring (struct cli_def *cli, const char *modestring)
 
int cli_set_configmode (struct cli_def *cli, int mode, const char *config_desc)
 This will set the configuration mode. More...
 
struct cli_commandcli_register_command (struct cli_def *cli, struct cli_command *parent, const char *command, int(*callback)(struct cli_def *cli, const char *, char **, int), int privilege, int mode, const char *help)
 Add a command to the internal command tree. More...
 
int cli_unregister_command (struct cli_def *cli, const char *command)
 Remove a command command and all children. More...
 
int cli_show_help (struct cli_def *cli, struct cli_command *c)
 
int cli_int_enable (struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
 
int cli_int_disable (struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
 
int cli_int_help (struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
 
int cli_int_history (struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
 Show history command. More...
 
int cli_int_quit (struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
 
int cli_int_exit (struct cli_def *cli, const char *command, char *argv[], int argc)
 
int cli_int_idle_timeout (struct cli_def *cli)
 
int cli_int_configure_terminal (struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
 
struct cli_defcli_init (ksnetEvMgrClass *ke)
 CLI Initialize. More...
 
void cli_unregister_all (struct cli_def *cli, struct cli_command *command)
 
int cli_done (struct cli_def *cli)
 This is optional, but it's a good idea to call this when you are finished with libcli. More...
 
void cli_free_history (struct cli_def *cli)
 Clear history array. More...
 
int cli_run_command (struct cli_def *cli, const char *command)
 
void cli_reprompt (struct cli_def *cli)
 
void cli_regular (struct cli_def *cli, int(*callback)(struct cli_def *cli))
 Adds a callback function which will be called every second that a user is connected to the cli. More...
 
void cli_regular_interval (struct cli_def *cli, int seconds)
 
void cli_loop_set_show_prompt (struct cli_loop_data_def *cd)
 
void cli_loop_show_prompt (struct cli_loop_data_def *cd)
 Show prompt before select or ask User/Password. More...
 
int cli_loop_idle (struct cli_loop_data_def *cd)
 Timeout every second. More...
 
int cli_loop_read (struct cli_loop_data_def *cd)
 
int cli_loop_select_read (struct cli_loop_data_def *cd)
 
int cli_loop_check_state (struct cli_loop_data_def *cd)
 
void cli_loop_free (struct cli_loop_data_def *cd)
 
void cli_loop_read_cb (EV_P_ ev_io *w, int revents)
 
void cli_loop_timer_cb (EV_P_ ev_timer *w, int revents)
 
void cli_loop_idle_cb (EV_P_ ev_idle *w, int revents)
 
int cli_loop (struct cli_def *cli, int sockfd)
 The main loop of the command-line environment. More...
 
int cli_file (struct cli_def *cli, FILE *fh, int privilege, int mode)
 This reads and processes every line read from f as if it were entered at the console. More...
 
void cli_bufprint (struct cli_def *cli, const char *format,...)
 
void cli_vabufprint (struct cli_def *cli, const char *format, va_list ap)
 
void cli_print (struct cli_def *cli, const char *format,...)
 This function should be called for any output generated by a command callback. More...
 
void cli_error (struct cli_def *cli, const char *format,...)
 A variant of cli_print() which does not have filters applied. More...
 
int cli_match_filter (UNUSED(struct cli_def *cli), const char *string, void *data)
 
int cli_range_filter (UNUSED(struct cli_def *cli), const char *string, void *data)
 
int cli_count_filter_init (struct cli_def *cli, int argc, UNUSED(char **argv), struct cli_filter *filt)
 
void cli_print_callback (struct cli_def *cli, void(*callback)(struct cli_def *, const char *))
 Whenever cli_print() or cli_error() is called, the output generally goes to the user. More...
 
void cli_set_idle_timeout (struct cli_def *cli, unsigned int seconds)
 
void cli_set_idle_timeout_callback (struct cli_def *cli, unsigned int seconds, int(*callback)(struct cli_def *))
 
void cli_telnet_protocol (struct cli_def *cli, int telnet_protocol)
 
void cli_set_context (struct cli_def *cli, void *context)
 
void * cli_get_context (struct cli_def *cli)
 

Macro Definition Documentation

#define _GNU_SOURCE

KSnet Network CLI module.

Modified by Kirill Scherba to use in KSNet on May 07, 2015, 23:43 from the libcli library code http://sites.dparrish.com/libcli code Developer's Reference: http://sites.dparrish.com/libcli/developers-reference

#define CTRL (   c)    (c - '@')

Referenced by cli_loop_read().

#define DES_PREFIX   "{crypt}" /* to distinguish clear text from DES crypted */
#define MATCH_INVERT   2
#define MATCH_REGEX   1
#define MD5_PREFIX   "$1$"
#define UNUSED (   d)    d

Enumeration Type Documentation

enum cli_states
Enumerator
STATE_LOGIN 
STATE_PASSWORD 
STATE_NORMAL 
STATE_ENABLE_PASSWORD 
STATE_ENABLE 

Function Documentation

void cli_allow_enable ( struct cli_def cli,
const char *  password 
)

This will allow a static password to be used for the enable command.

This static password will be checked before running any enable callbacks.

Set this to NULL to not have a static enable password.

Parameters
cli
password

References cli_def::enable_password, and free_z.

void cli_allow_user ( struct cli_def cli,
const char *  username,
const char *  password 
)

Enables internal authentication, and adds username/password to the list of allowed users.

The internal list of users will be checked before callback based authentication is tried.

Parameters
cli
username
password

References free(), malloc(), and cli_def::users.

void cli_bufprint ( struct cli_def cli,
const char *  format,
  ... 
)

References PRINT_FILTERED.

char* cli_command_name ( struct cli_def cli,
struct cli_command command 
)
int cli_count_filter ( struct cli_def cli,
const char *  string,
void *  data 
)

References CLI_ERROR, CLI_OK, cli_def::client, data, and free().

Referenced by cli_count_filter_init().

int cli_count_filter_init ( struct cli_def cli,
int  argc,
char **  argv,
struct cli_filter filt 
)
int cli_count_filter_init ( struct cli_def cli,
int  argc,
UNUSED(char **argv)  ,
struct cli_filter filt 
)
void cli_deny_user ( struct cli_def cli,
const char *  username 
)

Removes username/password from the list of allowed users.

If this is the last combination in the list, then internal authentication will be disabled.

Parameters
cli
username

References free(), and cli_def::users.

int cli_done ( struct cli_def cli)

This is optional, but it's a good idea to call this when you are finished with libcli.

This frees memory used by libcli.

Parameters
cli
Returns

References cli_def::banner, cli_def::buffer, cli_free_history(), CLI_OK, cli_unregister_all(), cli_def::commandname, free(), free_z, cli_def::hostname, cli_def::modestring, cli_def::promptchar, and cli_def::users.

void cli_error ( struct cli_def cli,
const char *  format,
  ... 
)

A variant of cli_print() which does not have filters applied.

Parameters
cli
format
...

References PRINT_PLAIN.

Referenced by cli_int_help(), cli_int_history(), cli_loop(), cli_loop_check_state(), and cli_show_help().

int cli_file ( struct cli_def cli,
FILE *  fh,
int  privilege,
int  mode 
)

This reads and processes every line read from f as if it were entered at the console.

The privilege level will be set to privilege and mode set to mode during the processing of the file.

Parameters
cli
fh
privilege
mode
Returns

References CLI_MAX_LINE_LENGTH, CLI_OK, CLI_QUIT, cli_run_command(), cli_set_configmode(), and cli_set_privilege().

void cli_free_history ( struct cli_def cli)

Clear history array.

Parameters
cli

References free_z, cli_def::history, and MAX_HISTORY.

Referenced by cli_done(), cli_loop(), and cli_loop_free().

void* cli_get_context ( struct cli_def cli)

References cli_def::user_context.

int cli_int_configure_terminal ( struct cli_def cli,
UNUSED(const char *command)  ,
UNUSED(char *argv[])  ,
UNUSED(int argc)   
)

References CLI_OK, cli_set_configmode(), and MODE_CONFIG.

Referenced by cli_init().

int cli_int_disable ( struct cli_def cli,
UNUSED(const char *command)  ,
UNUSED(char *argv[])  ,
UNUSED(int argc)   
)
int cli_int_enable ( struct cli_def cli,
UNUSED(const char *command)  ,
UNUSED(char *argv[])  ,
UNUSED(int argc)   
)
int cli_int_exit ( struct cli_def cli,
const char *  command,
char *  argv[],
int  argc 
)
int cli_int_help ( struct cli_def cli,
UNUSED(const char *command)  ,
UNUSED(char *argv[])  ,
UNUSED(int argc)   
)

References cli_error(), CLI_OK, cli_show_help(), and cli_def::commands.

Referenced by cli_init().

int cli_int_history ( struct cli_def cli,
UNUSED(const char *command)  ,
UNUSED(char *argv[])  ,
UNUSED(int argc)   
)

Show history command.

Parameters
cliparam command param argv param argc
Returns

References cli_error(), CLI_OK, cli_def::history, and MAX_HISTORY.

Referenced by cli_init().

int cli_int_idle_timeout ( struct cli_def cli)

References cli_print(), and CLI_QUIT.

Referenced by cli_init().

int cli_int_quit ( struct cli_def cli,
UNUSED(const char *command)  ,
UNUSED(char *argv[])  ,
UNUSED(int argc)   
)
int cli_loop ( struct cli_def cli,
int  sockfd 
)

The main loop of the command-line environment.

This must be called with the FD of a socket open for bi-directional communication (sockfd).

cli_loop() handles the telnet negotiation and authentication. It returns only when the connection is finished, either by a server or client disconnect. Returns CLI_OK.

Parameters
cli
sockfd
Returns

References cli_def::auth_callback, cli_def::banner, CLI_ERROR, cli_error(), cli_free_history(), cli_loop_check_state(), cli_loop_free(), cli_loop_idle_cb(), cli_loop_read_cb(), cli_loop_select_read(), cli_loop_set_show_prompt(), cli_loop_show_prompt(), cli_loop_timer_cb(), CLI_MAX_LINE_LENGTH, CLI_OK, cli_set_configmode(), cli_set_privilege(), cli_def::client, cli_def::commands, ksnetEvMgrClass::ev_loop, free(), cli_def::idle_timeout, cli_def::ke, cli_def::last_action, malloc(), MODE_EXEC, PRIVILEGE_UNPRIVILEGED, cli_def::state, STATE_LOGIN, STATE_NORMAL, cli_def::telnet_protocol, and cli_def::users.

void cli_loop_free ( struct cli_loop_data_def *  cd)
int cli_loop_idle ( struct cli_loop_data_def *  cd)

Timeout every second.

Parameters
cdPointer to cli_loop_data_def structure
Returns

References CLI_OK.

Referenced by cli_loop_idle_cb(), and cli_loop_select_read().

void cli_loop_idle_cb ( EV_P_ ev_idle *  w,
int  revents 
)

References cli_loop_free(), and cli_loop_idle().

Referenced by cli_loop().

void cli_loop_read_cb ( EV_P_ ev_io *  w,
int  revents 
)
int cli_loop_select_read ( struct cli_loop_data_def *  cd)

References cli_loop_idle(), and cli_loop_read().

Referenced by cli_loop().

void cli_loop_set_show_prompt ( struct cli_loop_data_def *  cd)

References CLI_MAX_LINE_LENGTH.

Referenced by cli_loop(), and cli_loop_read_cb().

void cli_loop_show_prompt ( struct cli_loop_data_def *  cd)

Show prompt before select or ask User/Password.

Parameters
cdPointer to cli_loop_data_def structure

References STATE_ENABLE, STATE_ENABLE_PASSWORD, STATE_LOGIN, STATE_NORMAL, and STATE_PASSWORD.

Referenced by cli_loop(), and cli_loop_read_cb().

void cli_loop_timer_cb ( EV_P_ ev_timer *  w,
int  revents 
)

Referenced by cli_loop().

int cli_match_filter ( struct cli_def cli,
const char *  string,
void *  data 
)

Referenced by cli_match_filter_init().

int cli_match_filter ( UNUSED(struct cli_def *cli)  ,
const char *  string,
void *  data 
)
int cli_match_filter_init ( struct cli_def cli,
int  argc,
char **  argv,
struct cli_filter filt 
)
void cli_print ( struct cli_def cli,
const char *  format,
  ... 
)

This function should be called for any output generated by a command callback.

It takes a printf() style format string and a variable number of arguments.

Be aware that any output generated by cli_print() will be passed through any filter currently being applied, and the output will be redirected to the cli_print_callback() if one has been specified.

Parameters
cli
format
...
Examples:
teoterm.c.

References PRINT_FILTERED.

Referenced by cli_int_idle_timeout(), and cmd_user().

void cli_print_callback ( struct cli_def cli,
void(*)(struct cli_def *, const char *)  callback 
)

Whenever cli_print() or cli_error() is called, the output generally goes to the user.

If you specify a callback using this function, then the output will be sent to that callback. The function will be called once for each line, and it will be passed a single null-terminated string, without any newline characters.

Specifying NULL as the callback parameter will make libcli use the default cli_print() function.

Parameters
cli
callback

References cli_def::print_callback.

int cli_range_filter ( struct cli_def cli,
const char *  string,
void *  data 
)

Referenced by cli_range_filter_init().

int cli_range_filter ( UNUSED(struct cli_def *cli)  ,
const char *  string,
void *  data 
)

References CLI_ERROR, CLI_OK, data, free_z, and state.

int cli_range_filter_init ( struct cli_def cli,
int  argc,
char **  argv,
struct cli_filter filt 
)
struct cli_command* cli_register_command ( struct cli_def cli,
struct cli_command parent,
const char *  command,
int(*)(struct cli_def *cli, const char *, char **, int)  callback,
int  privilege,
int  mode,
const char *  help 
)

Add a command to the internal command tree.

Parameters
cliThe handle of the cli structure
parentIf parent is NULL, the command is added to the top level of commands, otherwise it is a subcommand of parent.
commandCommand name
callbackWhen the command has been entered by the user, callback is checked. If it is not NULL, then the callback is called with:

struct cli_def * - the handle of the cli structure. This must be passed to all cli functions, including cli_print()
char * - the entire command which was entered. This is after command expansion
char ** - the list of arguments entered
int - the number of arguments entered
The callback must return CLI_OK if the command was successful, CLI_ERROR if processing wasn't successful and the next matching command should be tried (if any), or CLI_QUIT to drop the connection (e.g. on a fatal error).

Parameters
privilegeprivilege should be set to either PRIVILEGE_PRIVILEGED or PRIVILEGE_UNPRIVILEGED. If set to PRIVILEGE_PRIVILEGED then the user must have entered enable before running this command.
modeMode should be set to MODE_EXEC for no configuration mode, MODE_CONFIG for generic configuration commands, or your own config level. The user can enter the generic configuration level by entering configure terminal, and can return to MODE_EXEC by entering exit or CTRL-Z. You can define commands to enter your own configuration levels, which should call the cli_set_configmode() function.
helpIf help is provided, it is given to the user when the use the help command or press ?.
Returns
Returns a struct cli_command *, which you can pass as parent to another call to cli_register_command().
Examples:
teoterm.c.

References cli_command::callback, cli_command::children, cli_command::command, cli_def::commands, free(), cli_command::help, cli_command::mode, cli_command::next, cli_command::parent, cli_command::privilege, and cli_command::system.

Referenced by cli_init(), and event_cb().

void cli_regular ( struct cli_def cli,
int(*)(struct cli_def *cli)  callback 
)

Adds a callback function which will be called every second that a user is connected to the cli.

This can be used for regular processing such as debugging, time counting or implementing idle timeouts.

Pass NULL as the callback function to disable this at runtime.

If the callback function does not return CLI_OK, then the user will be disconnected.

Parameters
cli
callback

References cli_command::callback, and cli_def::regular_callback.

void cli_regular_interval ( struct cli_def cli,
int  seconds 
)

References cli_def::timeout_tm.

void cli_reprompt ( struct cli_def cli)

References cli_def::showprompt.

int cli_run_command ( struct cli_def cli,
const char *  command 
)
void cli_set_auth_callback ( struct cli_def cli,
int(*)(const char *, const char *)  auth_callback 
)

Enables or disables callback based authentication.

If auth_callback is not NULL, then authentication will be required on connection. auth_callback will be called with the username and password that the user enters.

Parameters
cli
auth_callbackmust return a non-zero value if authentication is successful. If auth_callback is NULL, then callback based authentication will be disabled.

References cli_def::auth_callback.

void cli_set_banner ( struct cli_def cli,
const char *  banner 
)

Sets the greeting that clients will be presented with when they connect.

This may be a security warning for example.

If this function is not called or called with a NULL argument, no banner will be presented.

Parameters
cli
banner

References cli_def::banner, and free_z.

int cli_set_configmode ( struct cli_def cli,
int  mode,
const char *  config_desc 
)

This will set the configuration mode.

Once set, commands will be restricted to only ones in the selected configuration mode, plus any set to MODE_ANY. The previous mode value is returned.

The string passed will be used to build the prompt in the set configuration mode. e.g. if you set the string test, the prompt will become: hostname(config-test)#

Parameters
cli
mode
config_desc
Returns

References cli_set_modestring(), cli_def::commands, cli_def::mode, and cli_command::mode.

Referenced by cli_file(), cli_init(), cli_int_configure_terminal(), cli_int_disable(), cli_int_enable(), cli_int_exit(), cli_int_quit(), cli_loop(), and cli_loop_read().

void cli_set_context ( struct cli_def cli,
void *  context 
)

References cli_def::user_context.

void cli_set_enable_callback ( struct cli_def cli,
int(*)(const char *)  enable_callback 
)

Just like cli_set_auth_callback, this takes a pointer to a callback function to authorize privileged access.

However this callback only takes a single string - the password.

Parameters
cli
enable_callback

References cli_def::enable_callback.

void cli_set_hostname ( struct cli_def cli,
const char *  hostname 
)

Sets the hostname to be displayed as the first part of the prompt.

Parameters
cli
hostname

References free_z, and cli_def::hostname.

void cli_set_idle_timeout ( struct cli_def cli,
unsigned int  seconds 
)
void cli_set_idle_timeout_callback ( struct cli_def cli,
unsigned int  seconds,
int(*)(struct cli_def *)  callback 
)
void cli_set_modestring ( struct cli_def cli,
const char *  modestring 
)

References free_z, and cli_def::modestring.

Referenced by cli_set_configmode().

void cli_set_promptchar ( struct cli_def cli,
const char *  promptchar 
)

References free_z, and cli_def::promptchar.

Referenced by cli_set_privilege().

void cli_telnet_protocol ( struct cli_def cli,
int  telnet_protocol 
)
void cli_unregister_all ( struct cli_def cli,
struct cli_command command 
)
int cli_unregister_command ( struct cli_def cli,
const char *  command 
)

Remove a command command and all children.

There is not provision yet for removing commands at lower than the top level.

Parameters
cli
command
Returns

References CLI_OK, cli_command::command, cli_def::commands, and cli_command::next.

void cli_vabufprint ( struct cli_def cli,
const char *  format,
va_list  ap 
)