Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
net_cli.h
Go to the documentation of this file.
1 #ifndef __LIBCLI_H__
2 #define __LIBCLI_H__
3 
4 // vim:sw=4 tw=120 et
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <sys/time.h>
13 
14 #include "ev_mgr.h"
15 
16 #define LIBCLI_VERSION_MAJOR 1
17 #define LIBCLI_VERISON_MINOR 9
18 #define LIBCLI_VERISON_REVISION 7
19 #define LIBCLI_VERSION ((LIBCLI_VERSION_MAJOR << 16) | (LIBCLI_VERSION_MINOR << 8) | LIBCLI_VERSION_REVISION)
20 
21 #define CLI_OK 0
22 #define CLI_ERROR -1
23 #define CLI_QUIT -2
24 #define CLI_ERROR_ARG -3
25 
26 #define MAX_HISTORY 256
27 
28 #define PRIVILEGE_UNPRIVILEGED 0
29 #define PRIVILEGE_PRIVILEGED 15
30 #define MODE_ANY -1
31 #define MODE_EXEC 0
32 #define MODE_CONFIG 1
33 
34 #define LIBCLI_HAS_ENABLE 1
35 
36 #define PRINT_PLAIN 0
37 #define PRINT_FILTERED 0x01
38 #define PRINT_BUFFERED 0x02
39 
40 #define CLI_MAX_LINE_LENGTH 4096
41 #define CLI_MAX_LINE_WORDS 128
42 
43 struct cli_def {
46  int (*auth_callback)(const char *, const char *);
47  int (*regular_callback)(struct cli_def *cli);
48  int (*enable_callback)(const char *);
49  char *banner;
50  struct unp *users;
53  char showprompt;
54  char *promptchar;
55  char *hostname;
56  char *modestring;
57  int privilege;
58  int mode;
59  int state;
61  void (*print_callback)(struct cli_def *cli, const char *string);
62  FILE *client;
63  /* internal buffers */
64  void *conn;
65  void *service;
66  char *commandname; // temporary buffer for cli_command_name() to prevent leak
67  char *buffer;
68  unsigned buf_size;
69  struct timeval timeout_tm;
70  time_t idle_timeout;
71  int (*idle_timeout_callback)(struct cli_def *);
72  time_t last_action;
74  void *user_context;
75 
77  unsigned int regular_count;
79 };
80 
81 struct cli_filter {
82  int (*filter)(struct cli_def *cli, const char *string, void *data);
83  void *data;
84  struct cli_filter *next;
85 };
86 
87 struct cli_command {
88  char *command;
89  int (*callback)(struct cli_def *, const char *, char **, int);
90  unsigned int unique_len;
91  char *help;
92  int privilege;
93  int mode;
94  struct cli_command *next;
97  int system;
98 };
99 
101 int cli_done(struct cli_def *cli);
102 struct cli_command *cli_register_command(struct cli_def *cli, struct cli_command *parent, const char *command,
103  int (*callback)(struct cli_def *, const char *, char **, int), int privilege,
104  int mode, const char *help);
105 int cli_unregister_command(struct cli_def *cli, const char *command);
106 int cli_run_command(struct cli_def *cli, const char *command);
107 int cli_loop(struct cli_def *cli, int sockfd);
108 int cli_file(struct cli_def *cli, FILE *fh, int privilege, int mode);
109 void cli_set_auth_callback(struct cli_def *cli, int (*auth_callback)(const char *, const char *));
110 void cli_set_enable_callback(struct cli_def *cli, int (*enable_callback)(const char *));
111 void cli_allow_user(struct cli_def *cli, const char *username, const char *password);
112 void cli_allow_enable(struct cli_def *cli, const char *password);
113 void cli_deny_user(struct cli_def *cli, const char *username);
114 void cli_set_banner(struct cli_def *cli, const char *banner);
115 void cli_set_hostname(struct cli_def *cli, const char *hostname);
116 void cli_set_promptchar(struct cli_def *cli, const char *promptchar);
117 void cli_set_modestring(struct cli_def *cli, const char *modestring);
118 int cli_set_privilege(struct cli_def *cli, int privilege);
119 int cli_set_configmode(struct cli_def *cli, int mode, const char *config_desc);
120 void cli_reprompt(struct cli_def *cli);
121 void cli_regular(struct cli_def *cli, int (*callback)(struct cli_def *cli));
122 void cli_regular_interval(struct cli_def *cli, int seconds);
123 void cli_print(struct cli_def *cli, const char *format, ...) __attribute__((format (printf, 2, 3)));
124 void cli_bufprint(struct cli_def *cli, const char *format, ...) __attribute__((format (printf, 2, 3)));
125 void cli_vabufprint(struct cli_def *cli, const char *format, va_list ap);
126 void cli_error(struct cli_def *cli, const char *format, ...) __attribute__((format (printf, 2, 3)));
127 void cli_print_callback(struct cli_def *cli, void (*callback)(struct cli_def *, const char *));
128 void cli_free_history(struct cli_def *cli);
129 void cli_set_idle_timeout(struct cli_def *cli, unsigned int seconds);
130 void cli_set_idle_timeout_callback(struct cli_def *cli, unsigned int seconds, int (*callback)(struct cli_def *));
131 
132 // Enable or disable telnet protocol negotiation.
133 // Note that this is enabled by default and must be changed before cli_loop() is run.
134 void cli_telnet_protocol(struct cli_def *cli, int telnet_protocol);
135 
136 // Set/get user context
137 void cli_set_context(struct cli_def *cli, void *context);
138 void *cli_get_context(struct cli_def *cli);
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif
void(* print_callback)(struct cli_def *cli, const char *string)
Definition: net_cli.h:61
int telnet_protocol
Definition: net_cli.h:73
int(* idle_timeout_callback)(struct cli_def *)
Definition: net_cli.h:71
unsigned int unique_len
Definition: net_cli.h:90
struct cli_command * next
Definition: net_cli.h:94
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...
Definition: net_cli.c:1387
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.
Definition: net_cli.c:2487
Definition: net_cli.h:81
int privilege
Definition: net_cli.h:92
void cli_set_context(struct cli_def *cli, void *context)
Definition: net_cli.c:2930
void 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.
Definition: net_cli.c:2907
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.
Definition: net_cli.c:841
int mode
Definition: net_cli.h:58
struct cli_filter * next
Definition: net_cli.h:84
time_t last_action
Definition: net_cli.h:72
int(* regular_callback)(struct cli_def *cli)
Definition: net_cli.h:47
char * promptchar
Definition: net_cli.h:54
struct cli_filter * filters
Definition: net_cli.h:60
void cli_set_auth_callback(struct cli_def *cli, int(*auth_callback)(const char *, const char *))
Enables or disables callback based authentication.
Definition: net_cli.c:213
int state
Definition: net_cli.h:59
struct cli_command * parent
Definition: net_cli.h:96
char * modestring
Definition: net_cli.h:56
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 ...
Definition: net_cli.c:226
int(* auth_callback)(const char *, const char *)
Definition: net_cli.h:46
int privilege
Definition: net_cli.h:57
struct timeval timeout_tm
Definition: net_cli.h:69
void * user_context
Definition: net_cli.h:74
struct unp * users
Definition: net_cli.h:50
#define MAX_HISTORY
Definition: net_cli.h:26
unsigned buf_size
Definition: net_cli.h:68
void cli_error(struct cli_def *cli, const char *format,...) __attribute__((format(printf
char * history[MAX_HISTORY]
Definition: net_cli.h:52
void cli_free_history(struct cli_def *cli)
Clear history array.
Definition: net_cli.c:914
char * command
Definition: net_cli.h:88
int system
Definition: net_cli.h:97
Definition: net_cli.h:43
char * buffer
Definition: net_cli.h:67
void cli_deny_user(struct cli_def *cli, const char *username)
Removes username/password from the list of allowed users.
Definition: net_cli.c:302
int cli_run_command(struct cli_def *cli, const char *command)
Definition: net_cli.c:1235
void * service
Definition: net_cli.h:65
int cli_set_privilege(struct cli_def *cli, int privilege)
Definition: net_cli.c:398
int cli_loop(struct cli_def *cli, int sockfd)
The main loop of the command-line environment.
Definition: net_cli.c:2349
int(* enable_callback)(const char *)
Definition: net_cli.h:48
void cli_set_idle_timeout_callback(struct cli_def *cli, unsigned int seconds, int(*callback)(struct cli_def *))
Definition: net_cli.c:2920
struct cli_command * children
Definition: net_cli.h:95
struct cli_command * commands
Definition: net_cli.h:45
char * banner
Definition: net_cli.h:49
int(* callback)(struct cli_def *, const char *, char **, int)
Definition: net_cli.h:89
int cli_unregister_command(struct cli_def *cli, const char *command)
Remove a command command and all children.
Definition: net_cli.c:581
int debug_regular
Definition: net_cli.h:78
void cli_allow_enable(struct cli_def *cli, const char *password)
This will allow a static password to be used for the enable command.
Definition: net_cli.c:285
void * cli_get_context(struct cli_def *cli)
Definition: net_cli.c:2934
#define ke
char * enable_password
Definition: net_cli.h:51
KSNet event manager functions data.
Definition: ev_mgr.h:245
void cli_reprompt(struct cli_def *cli)
Definition: net_cli.c:1368
void cli_set_promptchar(struct cli_def *cli, const char *promptchar)
Definition: net_cli.c:353
void cli_regular_interval(struct cli_def *cli, int seconds)
Definition: net_cli.c:1393
char * hostname
Definition: net_cli.h:55
void * data
Definition: net_cli.h:83
void cli_set_banner(struct cli_def *cli, const char *banner)
Sets the greeting that clients will be presented with when they connect.
Definition: net_cli.c:333
struct cli_command * cli_register_command(struct cli_def *cli, struct cli_command *parent, const char *command, int(*callback)(struct cli_def *, const char *, char **, int), int privilege, int mode, const char *help)
time_t idle_timeout
Definition: net_cli.h:70
char * commandname
Definition: net_cli.h:66
Definition: net_cli.h:87
int mode
Definition: net_cli.h:93
ksnetEvMgrClass * ke
Definition: net_cli.h:76
void void cli_bufprint(struct cli_def *cli, const char *format,...) __attribute__((format(printf
void cli_set_hostname(struct cli_def *cli, const char *hostname)
Sets the hostname to be displayed as the first part of the prompt.
Definition: net_cli.c:346
void cli_print(struct cli_def *cli, const char *format,...) __attribute__((format(printf
unsigned int regular_count
Definition: net_cli.h:77
void cli_set_modestring(struct cli_def *cli, const char *modestring)
Definition: net_cli.c:412
struct cli_def * cli_init(ksnetEvMgrClass *ke)
CLI Initialize.
Definition: net_cli.c:756
void * conn
Definition: net_cli.h:64
void cli_telnet_protocol(struct cli_def *cli, int telnet_protocol)
Definition: net_cli.c:2926
void cli_set_idle_timeout(struct cli_def *cli, unsigned int seconds)
Definition: net_cli.c:2912
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...
Definition: net_cli.c:242
int(* filter)(struct cli_def *cli, const char *string, void *data)
Definition: net_cli.h:82
void void void cli_vabufprint(struct cli_def *cli, const char *format, va_list ap)
Definition: net_cli.c:2603
int completion_callback
Definition: net_cli.h:44
int cli_set_configmode(struct cli_def *cli, int mode, const char *config_desc)
This will set the configuration mode.
Definition: net_cli.c:433
char showprompt
Definition: net_cli.h:53
FILE * client
Definition: net_cli.h:62
char * help
Definition: net_cli.h:91