1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.20 2004-11-02 04:35:03 bodea Exp $";
17 #include <arpa/inet.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
34 extern tunnelt
*tunnel
;
35 extern sessiont
*session
;
36 extern radiust
*radius
;
37 extern ippoolt
*ip_address_pool
;
38 extern struct Tstats
*_statistics
;
39 struct cli_def
*cli
= NULL
;
41 extern struct configt
*config
;
42 extern struct config_descriptt config_values
[];
44 extern struct Tringbuffer
*ringbuffer
;
46 extern struct cli_session_actions
*cli_session_actions
;
47 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
48 extern tbft
*filter_list
;
50 char *debug_levels
[] = {
74 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
75 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 int cmd_show_cluster(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 int regular_stuff(struct cli_def
*cli
);
102 void parsemac(char *string
, char mac
[6]);
104 void init_cli(char *hostname
)
108 struct cli_command
*c
;
109 struct cli_command
*c2
;
111 struct sockaddr_in addr
;
114 if (hostname
&& *hostname
)
115 cli_set_hostname(cli
, hostname
);
117 cli_set_hostname(cli
, "l2tpns");
119 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
120 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
122 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
124 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
125 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
126 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
127 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
128 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
129 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
130 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
131 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
132 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
133 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
134 cli_register_command(cli
, c
, "users", cmd_show_users
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of all connected users or details of selected user");
135 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
137 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
138 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
139 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
142 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
144 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
145 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
148 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
150 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
151 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
152 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
154 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable interception for a user");
155 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable throttling for a user");
156 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
158 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
159 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP peer");
161 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
162 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable interception for a user");
163 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable throttling for a user");
164 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
165 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
166 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP peer");
168 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
169 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
170 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
171 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
173 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
174 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
176 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
177 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
179 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
180 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
182 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
184 // Enable regular processing
185 cli_regular(cli
, regular_stuff
);
187 if (!(f
= fopen(CLIUSERS
, "r")))
189 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
193 while (fgets(buf
, 4096, f
))
196 if (*buf
== '#') continue;
197 if ((p
= strchr(buf
, '\r'))) *p
= 0;
198 if ((p
= strchr(buf
, '\n'))) *p
= 0;
200 if (!(p
= strchr((char *)buf
, ':'))) continue;
202 if (!strcmp(buf
, "enable"))
204 cli_allow_enable(cli
, p
);
205 log(3, 0, 0, 0, "Setting enable password\n");
209 cli_allow_user(cli
, buf
, p
);
210 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
216 memset(&addr
, 0, sizeof(addr
));
217 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
218 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
221 // Set cli fd as non-blocking
222 flags
= fcntl(clifd
, F_GETFL
, 0);
223 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
225 addr
.sin_family
= AF_INET
;
226 addr
.sin_port
= htons(23);
227 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
229 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
235 void cli_do(int sockfd
)
237 int require_auth
= 1;
238 struct sockaddr_in addr
;
239 int l
= sizeof(addr
);
241 if (fork_and_close()) return;
242 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
244 log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
245 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
248 log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
252 log(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
253 if (!cli
->users
) /* paranoia */
255 log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
261 /* no username/pass required */
268 debug_rb_tail
= ringbuffer
->tail
;
270 memset(&debug_flags
, 0, sizeof(debug_flags
));
271 debug_flags
.critical
= 1;
273 cli_loop(cli
, sockfd
);
276 log(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
280 void cli_print_log(struct cli_def
*cli
, char *string
)
282 log(3, 0, 0, 0, "%s\n", string
);
285 void cli_do_file(FILE *fh
)
287 log(3, 0, 0, 0, "Reading configuration file\n");
288 cli_print_callback(cli
, cli_print_log
);
289 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
290 cli_print_callback(cli
, NULL
);
293 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
304 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
306 int v
= va_arg(ap
, int);
307 snprintf(buf
, sizeof(buf
), entry
, v
);
313 desc
= va_arg(ap
, char *);
315 cli_print(cli
, " %-20s %s", p
, desc
);
317 cli_print(cli
, " %s", p
);
319 entry
= desc
? va_arg(ap
, char *) : 0;
324 cli_print(cli
, " <cr>");
329 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
333 if (CLI_HELP_REQUESTED
)
334 return cli_arg_help(cli
, 1,
335 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
341 // Show individual session
342 for (i
= 0; i
< argc
; i
++)
344 unsigned int s
, b_in
, b_out
;
346 if (s
<= 0 || s
>= MAXSESSION
)
348 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
351 cli_print(cli
, "\r\nSession %d:", s
);
352 cli_print(cli
, " User: %s", session
[s
].user
[0] ? session
[s
].user
: "none");
353 cli_print(cli
, " Calling Num: %s", session
[s
].calling
);
354 cli_print(cli
, " Called Num: %s", session
[s
].called
);
355 cli_print(cli
, " Tunnel ID: %d", session
[s
].tunnel
);
356 cli_print(cli
, " IP address: %s", inet_toa(htonl(session
[s
].ip
)));
357 cli_print(cli
, " Unique SID: %lu", session
[s
].unique_id
);
358 cli_print(cli
, " Idle time: %u seconds", abs(time_now
- session
[s
].last_packet
));
359 cli_print(cli
, " Next Recv: %u", session
[s
].nr
);
360 cli_print(cli
, " Next Send: %u", session
[s
].ns
);
361 cli_print(cli
, " Bytes In/Out: %lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
362 cli_print(cli
, " Pkts In/Out: %lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
363 cli_print(cli
, " MRU: %d", session
[s
].mru
);
364 cli_print(cli
, " Radius Session: %u", session
[s
].radius
);
365 cli_print(cli
, " Rx Speed: %lu", session
[s
].rx_connect_speed
);
366 cli_print(cli
, " Tx Speed: %lu", session
[s
].tx_connect_speed
);
367 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
368 cli_print(cli
, " Intercepted: %s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
370 cli_print(cli
, " Intercepted: no");
372 cli_print(cli
, " Walled Garden: %s", session
[s
].walled_garden
? "YES" : "no");
374 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
375 cli_print(cli
, " Throttled: %s%s%.0d%s%s%.0d%s%s",
376 t
? "YES" : "no", t
? " (" : "",
377 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
379 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
383 b_in
= session
[s
].tbf_in
;
384 b_out
= session
[s
].tbf_out
;
386 cli_print(cli
, " %5s %6s %6s | %7s %7s %8s %8s %8s %8s",
387 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
388 "ByteSent", "PackSent", "PackDrop", "PackDelay");
391 cli_print(cli
, " TBFI#%d%1s %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
393 (filter_list
[b_in
].next
? "*" : " "),
394 filter_list
[b_in
].rate
* 8,
395 filter_list
[b_in
].credit
,
396 filter_list
[b_in
].queued
,
397 filter_list
[b_in
].b_queued
,
398 filter_list
[b_in
].p_queued
,
399 filter_list
[b_in
].b_sent
,
400 filter_list
[b_in
].p_sent
,
401 filter_list
[b_in
].p_dropped
,
402 filter_list
[b_in
].p_delayed
);
405 cli_print(cli
, " TBFO#%d%1s %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
407 (filter_list
[b_out
].next
? "*" : " "),
408 filter_list
[b_out
].rate
* 8,
409 filter_list
[b_out
].credit
,
410 filter_list
[b_out
].queued
,
411 filter_list
[b_out
].b_queued
,
412 filter_list
[b_out
].p_queued
,
413 filter_list
[b_out
].b_sent
,
414 filter_list
[b_out
].p_sent
,
415 filter_list
[b_out
].p_dropped
,
416 filter_list
[b_out
].p_delayed
);
423 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
438 for (i
= 1; i
< MAXSESSION
; i
++)
440 char *userip
, *tunnelip
;
441 if (!session
[i
].opened
) continue;
442 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
443 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
444 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
447 session
[i
].user
[0] ? session
[i
].user
: "*",
449 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
450 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
451 (session
[i
].walled_garden
) ? "Y" : "N",
452 abs(time_now
- (unsigned long)session
[i
].opened
),
453 (unsigned long)session
[i
].total_cout
,
454 (unsigned long)session
[i
].total_cin
,
455 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
457 session
[i
].calling
[0] ? session
[i
].calling
: "*");
458 if (userip
) free(userip
);
459 if (tunnelip
) free(tunnelip
);
464 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
466 int i
, x
, show_all
= 0;
474 if (CLI_HELP_REQUESTED
)
477 return cli_arg_help(cli
, 1,
478 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
481 return cli_arg_help(cli
, 1,
482 "all", "Show all tunnels, including unused",
483 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
490 if (strcmp(argv
[0], "all") == 0)
496 // Show individual tunnel
497 for (i
= 0; i
< argc
; i
++)
502 if (t
<= 0 || t
>= MAXTUNNEL
)
504 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
507 cli_print(cli
, "\r\nTunnel %d:", t
);
508 cli_print(cli
, " State: %s", states
[tunnel
[t
].state
]);
509 cli_print(cli
, " Hostname: %s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
510 cli_print(cli
, " Remote IP: %s", inet_toa(htonl(tunnel
[t
].ip
)));
511 cli_print(cli
, " Remote Port: %d", tunnel
[t
].port
);
512 cli_print(cli
, " Rx Window: %u", tunnel
[t
].window
);
513 cli_print(cli
, " Next Recv: %u", tunnel
[t
].nr
);
514 cli_print(cli
, " Next Send: %u", tunnel
[t
].ns
);
515 cli_print(cli
, " Queue Len: %u", tunnel
[t
].controlc
);
516 cli_print(cli
, " Last Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
518 for (x
= 0; x
< MAXSESSION
; x
++)
519 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
520 sprintf(s
, "%s%u ", s
, x
);
522 cli_print(cli
, " Sessions: %s", s
);
528 // Show tunnel summary
529 cli_print(cli
, "%4s %20s %20s %6s %s",
536 for (i
= 1; i
< MAXTUNNEL
; i
++)
539 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
|| !tunnel
[i
].hostname
[0])) continue;
541 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
542 cli_print(cli
, "%4d %20s %20s %6s %6d",
544 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
545 inet_toa(htonl(tunnel
[i
].ip
)),
546 states
[tunnel
[i
].state
],
553 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
560 if (CLI_HELP_REQUESTED
)
561 return cli_arg_help(cli
, 1,
562 "USER", "Show details for specific username",
565 for (i
= 0; i
< MAXSESSION
; i
++)
567 if (!session
[i
].opened
) continue;
568 if (!session
[i
].user
[0]) continue;
572 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
574 if (strcmp(argv
[j
], session
[i
].user
) == 0)
576 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
577 sargv
[sargc
] = sid
[sargc
];
585 cli_print(cli
, "%s", session
[i
].user
);
589 return cmd_show_session(cli
, "users", sargv
, sargc
);
594 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
596 if (CLI_HELP_REQUESTED
)
597 return CLI_HELP_NO_ARGS
;
599 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
600 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
601 GET_STAT(tun_rx_bytes
),
602 GET_STAT(tun_rx_packets
),
603 GET_STAT(tun_rx_errors
));
604 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
605 GET_STAT(tun_tx_bytes
),
606 GET_STAT(tun_tx_packets
),
607 GET_STAT(tun_tx_errors
));
610 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
611 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
612 GET_STAT(tunnel_rx_bytes
),
613 GET_STAT(tunnel_rx_packets
),
614 GET_STAT(tunnel_rx_errors
),
616 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
617 GET_STAT(tunnel_tx_bytes
),
618 GET_STAT(tunnel_tx_packets
),
619 GET_STAT(tunnel_tx_errors
),
620 GET_STAT(tunnel_retries
));
623 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
624 cli_print(cli
, "-----------------------------------------");
625 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
626 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
627 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
628 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
629 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
630 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
631 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
632 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
633 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
634 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
635 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
636 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
637 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
638 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
639 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
643 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
644 cli_print(cli
, "-----------------------------------------");
645 cli_print(cli
, "%-30s%lu", "call_processtun", GET_STAT(call_processtun
));
646 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
647 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
648 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
649 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
650 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
651 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
652 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
653 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
654 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
655 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
656 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
657 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
658 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
659 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
660 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
661 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
662 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
663 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
664 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
665 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
666 cli_print(cli
, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address
));
667 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
668 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
669 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
670 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
675 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
681 if (CLI_HELP_REQUESTED
)
682 return cli_arg_help(cli
, 1,
683 "tag", "Include CVS release tag",
684 "file", "Include file versions",
687 for (i
= 0; i
< argc
; i
++)
688 if (!strcmp(argv
[i
], "tag"))
690 else if (!strcmp(argv
[i
], "file"))
693 cli_print(cli
, "L2TPNS %s", VERSION
);
696 char const *p
= strchr(cvs_name
, ':');
708 e
= strpbrk(p
, " \t$");
709 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
714 extern linked_list
*loaded_plugins
;
717 cli_print(cli
, "Files:");
718 cli_print(cli
, " %s", cvs_id_arp
);
720 cli_print(cli
, " %s", cvs_id_bgp
);
722 cli_print(cli
, " %s", cvs_id_cli
);
723 cli_print(cli
, " %s", cvs_id_cluster
);
724 cli_print(cli
, " %s", cvs_id_constants
);
725 cli_print(cli
, " %s", cvs_id_control
);
726 cli_print(cli
, " %s", cvs_id_icmp
);
727 cli_print(cli
, " %s", cvs_id_l2tpns
);
728 cli_print(cli
, " %s", cvs_id_ll
);
729 cli_print(cli
, " %s", cvs_id_md5
);
730 cli_print(cli
, " %s", cvs_id_ppp
);
731 cli_print(cli
, " %s", cvs_id_radius
);
732 cli_print(cli
, " %s", cvs_id_tbf
);
733 cli_print(cli
, " %s", cvs_id_util
);
735 ll_reset(loaded_plugins
);
736 while ((p
= ll_next(loaded_plugins
)))
738 char const **id
= dlsym(p
, "cvs_id");
740 cli_print(cli
, " %s", *id
);
747 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
750 int used
= 0, free
= 0, show_all
= 0;
752 if (!config
->cluster_iam_master
)
754 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
758 if (CLI_HELP_REQUESTED
)
761 return cli_arg_help(cli
, 1, NULL
);
763 return cli_arg_help(cli
, 1,
764 "all", "Show all pool addresses, including unused",
768 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
772 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
773 for (i
= 0; i
< MAXIPPOOL
; i
++)
775 if (!ip_address_pool
[i
].address
) continue;
776 if (ip_address_pool
[i
].assigned
)
778 cli_print(cli
, "%-15s Y %8d %s",
779 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
785 if (ip_address_pool
[i
].last
)
786 cli_print(cli
, "%-15s N %8s [%s] %ds",
787 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
788 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
790 cli_print(cli
, "%-15s N", inet_toa(htonl(ip_address_pool
[i
].address
)));
797 cli_print(cli
, "(Not displaying unused addresses)");
799 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
803 void print_save_config(struct cli_def
*cli
, char *string
)
806 fprintf(save_config_fh
, "%s\n", string
);
809 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
811 if (CLI_HELP_REQUESTED
)
812 return CLI_HELP_NO_ARGS
;
814 if ((save_config_fh
= fopen(config
->config_file
, "w")))
816 cli_print(cli
, "Writing configuration");
817 cli_print_callback(cli
, print_save_config
);
818 cmd_show_run(cli
, command
, argv
, argc
);
819 cli_print_callback(cli
, NULL
);
820 fclose(save_config_fh
);
824 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
829 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
833 if (CLI_HELP_REQUESTED
)
834 return CLI_HELP_NO_ARGS
;
836 cli_print(cli
, "# Current configuration:");
838 for (i
= 0; config_values
[i
].key
; i
++)
840 void *value
= ((void *)config
) + config_values
[i
].offset
;
841 if (config_values
[i
].type
== STRING
)
842 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
843 else if (config_values
[i
].type
== IP
)
844 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
845 else if (config_values
[i
].type
== SHORT
)
846 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
847 else if (config_values
[i
].type
== BOOL
)
848 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
849 else if (config_values
[i
].type
== INT
)
850 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
851 else if (config_values
[i
].type
== UNSIGNED_LONG
)
852 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
853 else if (config_values
[i
].type
== MAC
)
854 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
855 *(unsigned short *)(value
+ 0),
856 *(unsigned short *)(value
+ 1),
857 *(unsigned short *)(value
+ 2),
858 *(unsigned short *)(value
+ 3),
859 *(unsigned short *)(value
+ 4),
860 *(unsigned short *)(value
+ 5));
863 cli_print(cli
, "# Plugins");
864 for (i
= 0; i
< MAXPLUGINS
; i
++)
866 if (*config
->plugins
[i
])
868 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
872 cli_print(cli
, "# end");
876 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
878 int i
, free
= 0, used
= 0, show_all
= 0;
889 if (CLI_HELP_REQUESTED
)
892 return cli_arg_help(cli
, 1, NULL
);
894 return cli_arg_help(cli
, 1,
895 "all", "Show all RADIUS sessions, including unused",
899 cli_print(cli
, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
903 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
906 for (i
= 1; i
< MAXRADIUS
; i
++)
908 if (radius
[i
].state
== RADIUSNULL
)
913 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
915 cli_print(cli
, "%6d%6d%5d%6s%9d%9u%4d",
919 states
[radius
[i
].state
],
925 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
930 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
934 if (CLI_HELP_REQUESTED
)
935 return CLI_HELP_NO_ARGS
;
937 cli_print(cli
, "Plugins currently loaded:");
938 for (i
= 0; i
< MAXPLUGINS
; i
++)
939 if (*config
->plugins
[i
])
940 cli_print(cli
, " %s", config
->plugins
[i
]);
945 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
949 if (CLI_HELP_REQUESTED
)
950 return CLI_HELP_NO_ARGS
;
952 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
961 for (i
= 0; i
< MAXSESSION
; i
++)
963 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
964 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
968 session
[i
].throttle_in
,
969 session
[i
].throttle_out
,
977 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
979 if (CLI_HELP_REQUESTED
)
980 return CLI_HELP_NO_ARGS
;
982 cli_print(cli
, " _\n"
988 " ( \\ `. `-. _,.-:\\\n"
989 " \\ \\ `. `-._ __..--' ,-';/\n"
990 " \\ `. `-. `-..___..---' _.--' ,'/\n"
991 " `. `. `-._ __..--' ,' /\n"
992 " `. `-_ ``--..'' _.-' ,'\n"
993 " `-_ `-.___ __,--' ,'\n"
994 " `-.__ `----\"\"\" __.-'\n"
995 "hh `--..____..--'");
1000 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1002 if (CLI_HELP_REQUESTED
)
1003 return CLI_HELP_NO_ARGS
;
1005 cli_print(cli
, "Counters cleared");
1006 SET_STAT(last_reset
, time(NULL
));
1010 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1015 if (CLI_HELP_REQUESTED
)
1016 return cli_arg_help(cli
, argc
> 1,
1017 "USER", "Username of session to drop", NULL
);
1019 if (!config
->cluster_iam_master
)
1021 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1027 cli_print(cli
, "Specify a user to drop");
1031 for (i
= 0; i
< argc
; i
++)
1033 if (!(s
= sessionbyuser(argv
[i
])))
1035 cli_print(cli
, "User %s is not connected", argv
[i
]);
1039 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1041 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1042 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1049 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1054 if (CLI_HELP_REQUESTED
)
1055 return cli_arg_help(cli
, argc
> 1,
1056 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1058 if (!config
->cluster_iam_master
)
1060 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1066 cli_print(cli
, "Specify a tunnel to drop");
1070 for (i
= 0; i
< argc
; i
++)
1072 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1074 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1080 cli_print(cli
, "Tunnel %d is not connected", t
);
1086 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1090 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1091 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1097 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1102 if (CLI_HELP_REQUESTED
)
1103 return cli_arg_help(cli
, argc
> 1,
1104 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1106 if (!config
->cluster_iam_master
)
1108 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1114 cli_print(cli
, "Specify a session id to drop");
1118 for (i
= 0; i
< argc
; i
++)
1120 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1122 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1126 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1128 cli_print(cli
, "Dropping session %d", s
);
1129 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1133 cli_print(cli
, "Session %d is not active.", s
);
1140 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1146 if (CLI_HELP_REQUESTED
)
1151 return cli_arg_help(cli
, 0,
1152 "USER", "Username of session to snoop", NULL
);
1155 return cli_arg_help(cli
, 0,
1156 "A.B.C.D", "IP address of snoop destination", NULL
);
1159 return cli_arg_help(cli
, 0,
1160 "N", "Port of snoop destination", NULL
);
1164 return cli_arg_help(cli
, 1, NULL
);
1171 if (!config
->cluster_iam_master
)
1173 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1179 cli_print(cli
, "Specify username, ip and port");
1183 if (!(s
= sessionbyuser(argv
[0])))
1185 cli_print(cli
, "User %s is not connected", argv
[0]);
1189 ip
= inet_addr(argv
[1]);
1190 if (!ip
|| ip
== INADDR_NONE
)
1192 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1196 port
= atoi(argv
[2]);
1199 cli_print(cli
, "Invalid port %s", argv
[2]);
1203 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(session
[s
].snoop_ip
), session
[s
].snoop_port
);
1204 cli_session_actions
[s
].snoop_ip
= ip
;
1205 cli_session_actions
[s
].snoop_port
= port
;
1206 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1211 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1216 if (CLI_HELP_REQUESTED
)
1217 return cli_arg_help(cli
, argc
> 1,
1218 "USER", "Username of session to unsnoop", NULL
);
1220 if (!config
->cluster_iam_master
)
1222 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1228 cli_print(cli
, "Specify a user to unsnoop");
1232 for (i
= 0; i
< argc
; i
++)
1234 if (!(s
= sessionbyuser(argv
[i
])))
1236 cli_print(cli
, "User %s is not connected", argv
[i
]);
1240 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1241 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1247 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1254 throttle USER - throttle in/out to default rate
1255 throttle USER RATE - throttle in/out to default rate
1256 throttle USER in RATE - throttle input only
1257 throttle USER out RATE - throttle output only
1258 throttle USER in RATE out RATE - throttle both
1261 if (CLI_HELP_REQUESTED
)
1266 return cli_arg_help(cli
, 0,
1267 "USER", "Username of session to throttle", NULL
);
1270 return cli_arg_help(cli
, 1,
1271 "RATE", "Rate in kbps (in and out)",
1272 "in", "Select incoming rate",
1273 "out", "Select outgoing rate", NULL
);
1276 return cli_arg_help(cli
, 1,
1277 "in", "Select incoming rate",
1278 "out", "Select outgoing rate", NULL
);
1281 if (isdigit(argv
[1][0]))
1282 return cli_arg_help(cli
, 1, NULL
);
1285 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1288 return cli_arg_help(cli
, argc
> 1, NULL
);
1292 if (!config
->cluster_iam_master
)
1294 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1300 cli_print(cli
, "Specify a user to throttle");
1304 if (!(s
= sessionbyuser(argv
[0])))
1306 cli_print(cli
, "User %s is not connected", argv
[0]);
1312 rate_in
= rate_out
= config
->rl_rate
;
1316 rate_in
= rate_out
= atoi(argv
[1]);
1319 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1323 else if (argc
== 3 || argc
== 5)
1326 for (i
= 1; i
< argc
- 1; i
+= 2)
1328 int len
= strlen(argv
[i
]);
1330 if (!strncasecmp(argv
[i
], "in", len
))
1331 r
= rate_in
= atoi(argv
[i
+1]);
1332 else if (!strncasecmp(argv
[i
], "out", len
))
1333 r
= rate_out
= atoi(argv
[i
+1]);
1337 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1344 cli_print(cli
, "Invalid arguments");
1348 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1350 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1354 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1355 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1356 cli_session_actions
[s
].throttle_in
= rate_in
;
1358 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1359 cli_session_actions
[s
].throttle_out
= rate_out
;
1361 if (cli_session_actions
[s
].throttle_in
== -1 &&
1362 cli_session_actions
[s
].throttle_out
== -1)
1364 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1368 cli_print(cli
, "Throttling user %s", argv
[0]);
1369 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1374 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1379 if (CLI_HELP_REQUESTED
)
1380 return cli_arg_help(cli
, argc
> 1,
1381 "USER", "Username of session to unthrottle", NULL
);
1383 if (!config
->cluster_iam_master
)
1385 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1391 cli_print(cli
, "Specify a user to unthrottle");
1395 for (i
= 0; i
< argc
; i
++)
1397 if (!(s
= sessionbyuser(argv
[i
])))
1399 cli_print(cli
, "User %s is not connected", argv
[i
]);
1403 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1405 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1406 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1410 cli_print(cli
, "User %s not throttled", argv
[i
]);
1417 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1421 if (CLI_HELP_REQUESTED
)
1422 return cli_arg_help(cli
, 1,
1423 "all", "Enable debugging for all except \"data\"",
1424 "critical", "", // FIXME: add descriptions
1434 char *p
= (char *) &debug_flags
;
1435 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1439 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1440 (debug_flags
.critical
) ? " critical" : "",
1441 (debug_flags
.error
) ? " error" : "",
1442 (debug_flags
.warning
) ? " warning" : "",
1443 (debug_flags
.info
) ? " info" : "",
1444 (debug_flags
.calls
) ? " calls" : "",
1445 (debug_flags
.data
) ? " data" : "");
1451 cli_print(cli
, "Debugging off");
1455 for (i
= 0; i
< argc
; i
++)
1457 int len
= strlen(argv
[i
]);
1459 if (argv
[i
][0] == 'c' && len
< 2)
1460 len
= 2; /* distinguish [cr]itical from [ca]lls */
1462 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 1; continue; }
1463 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 1; continue; }
1464 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 1; continue; }
1465 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 1; continue; }
1466 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 1; continue; }
1467 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 1; continue; }
1468 if (!strncasecmp(argv
[i
], "all", len
))
1470 memset(&debug_flags
, 1, sizeof(debug_flags
));
1471 debug_flags
.data
= 0;
1475 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1481 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1485 if (CLI_HELP_REQUESTED
)
1486 return cli_arg_help(cli
, 1,
1487 "all", "Disable all debugging",
1488 "critical", "", // FIXME: add descriptions
1498 memset(&debug_flags
, 0, sizeof(debug_flags
));
1502 for (i
= 0; i
< argc
; i
++)
1504 int len
= strlen(argv
[i
]);
1506 if (argv
[i
][0] == 'c' && len
< 2)
1507 len
= 2; /* distinguish [cr]itical from [ca]lls */
1509 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 0; continue; }
1510 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 0; continue; }
1511 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 0; continue; }
1512 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 0; continue; }
1513 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 0; continue; }
1514 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 0; continue; }
1515 if (!strncasecmp(argv
[i
], "all", len
))
1517 memset(&debug_flags
, 0, sizeof(debug_flags
));
1521 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1527 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1529 int i
, firstfree
= 0;
1531 if (CLI_HELP_REQUESTED
)
1532 return cli_arg_help(cli
, argc
> 1,
1533 "PLUGIN", "Name of plugin to load", NULL
);
1537 cli_print(cli
, "Specify a plugin to load");
1541 for (i
= 0; i
< MAXPLUGINS
; i
++)
1543 if (!*config
->plugins
[i
] && !firstfree
)
1545 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1547 cli_print(cli
, "Plugin is already loaded");
1554 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1555 config
->reload_config
= 1;
1556 cli_print(cli
, "Loading plugin %s", argv
[0]);
1562 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1566 if (CLI_HELP_REQUESTED
)
1567 return cli_arg_help(cli
, argc
> 1,
1568 "PLUGIN", "Name of plugin to unload", NULL
);
1572 cli_print(cli
, "Specify a plugin to remove");
1576 for (i
= 0; i
< MAXPLUGINS
; i
++)
1578 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1580 config
->reload_config
= 1;
1581 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1586 cli_print(cli
, "Plugin is not loaded");
1590 char *duration(time_t secs
)
1592 static char *buf
= NULL
;
1595 if (!buf
) buf
= calloc(64, 1);
1599 int days
= secs
/ 86400;
1600 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1606 int mins
= secs
/ 60;
1607 int hrs
= mins
/ 60;
1610 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1612 else if (secs
>= 60)
1614 int mins
= secs
/ 60;
1615 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1618 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1623 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1626 char buf
[100], *p
= buf
, *loads
[3];
1627 int i
, num_sessions
= 0;
1629 if (CLI_HELP_REQUESTED
)
1630 return CLI_HELP_NO_ARGS
;
1632 fh
= fopen("/proc/loadavg", "r");
1633 fgets(buf
, 100, fh
);
1636 for (i
= 0; i
< 3; i
++)
1637 loads
[i
] = strdup(strsep(&p
, " "));
1640 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1642 for (i
= 1; i
< MAXSESSION
; i
++)
1643 if (session
[i
].opened
) num_sessions
++;
1645 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1647 duration(time_now
- config
->start_time
),
1649 loads
[0], loads
[1], loads
[2]
1651 for (i
= 0; i
< 3; i
++)
1652 if (loads
[i
]) free(loads
[i
]);
1654 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1659 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1663 if (CLI_HELP_REQUESTED
)
1669 int len
= strlen(argv
[0])-1;
1670 for (i
= 0; config_values
[i
].key
; i
++)
1671 if (!len
|| !strncmp(argv
[0], config_values
[i
].key
, len
))
1672 cli_print(cli
, " %s", config_values
[i
].key
);
1678 return cli_arg_help(cli
, 0,
1679 "VALUE", "Value for variable", NULL
);
1683 return cli_arg_help(cli
, 1, NULL
);
1692 cli_print(cli
, "Specify variable and value");
1696 for (i
= 0; config_values
[i
].key
; i
++)
1698 void *value
= ((void *)config
) + config_values
[i
].offset
;
1699 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1701 // Found a value to set
1702 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1703 switch (config_values
[i
].type
)
1706 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1709 *(int *)value
= atoi(argv
[1]);
1712 *(unsigned long *)value
= atol(argv
[1]);
1715 *(short *)value
= atoi(argv
[1]);
1718 *(unsigned *)value
= inet_addr(argv
[1]);
1721 parsemac(argv
[1], (char *)value
);
1724 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1730 cli_print(cli
, "Unknown variable type");
1733 config
->reload_config
= 1;
1738 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1742 int regular_stuff(struct cli_def
*cli
)
1744 int i
= debug_rb_tail
;
1748 while (i
!= ringbuffer
->tail
)
1750 int show_message
= 0;
1752 if (*ringbuffer
->buffer
[i
].message
)
1754 // Always show messages if we are doing general debug
1755 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1756 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1757 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1758 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1759 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1760 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1765 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1767 struct in_addr addr
;
1769 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1770 ipaddr
= inet_ntoa(addr
);
1772 cli_print(cli
, "\r%s-%s-%u-%u %s",
1773 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1775 ringbuffer
->buffer
[i
].tunnel
,
1776 ringbuffer
->buffer
[i
].session
,
1777 ringbuffer
->buffer
[i
].message
);
1782 if (++i
== ringbuffer
->tail
) break;
1783 if (i
== RINGBUFFER_SIZE
) i
= 0;
1786 debug_rb_tail
= ringbuffer
->tail
;
1793 // Convert a string in the form of abcd.ef12.3456 into char[6]
1794 void parsemac(char *string
, char mac
[6])
1796 if (sscanf(string
, "%02x%02x.%02x%02x.%02x%02x", (unsigned int *)&mac
[0], (unsigned int *)&mac
[1], (unsigned int *)&mac
[2], (unsigned int *)&mac
[3], (unsigned int *)&mac
[4], (unsigned int *)&mac
[5]) == 6)
1798 if (sscanf(string
, "%02x%02x:%02x%02x:%02x%02x", (unsigned int *)&mac
[0], (unsigned int *)&mac
[1], (unsigned int *)&mac
[2], (unsigned int *)&mac
[3], (unsigned int *)&mac
[4], (unsigned int *)&mac
[5]) == 6)