8c43590d78a69588f73042adbf5b6433d379634e
[l2tpns.git] / cli.c
1 // L2TPNS Command Line Interface
2 // vim: sw=8 ts=8
3
4 char const *cvs_name = "$Name: $";
5 char const *cvs_id_cli = "$Id: cli.c,v 1.16 2004/09/19 23:26:46 fred_nerk Exp $";
6
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <sys/file.h>
10 #include <sys/stat.h>
11 #include <syslog.h>
12 #include <malloc.h>
13 #include <sched.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <dlfcn.h>
25 #include <libcli.h>
26 #include "l2tpns.h"
27 #include "util.h"
28 #include "cluster.h"
29 #include "tbf.h"
30 #include "ll.h"
31 #ifdef BGP
32 #include "bgp.h"
33 #endif
34
35 extern tunnelt *tunnel;
36 extern sessiont *session;
37 extern radiust *radius;
38 extern ippoolt *ip_address_pool;
39 extern struct Tstats *_statistics;
40 struct cli_def *cli = NULL;
41 int cli_quit = 0;
42 extern int clifd, udpfd, tunfd, snoopfd, ifrfd, cluster_sockfd;
43 extern int *radfds;
44 extern struct configt *config;
45 extern struct config_descriptt config_values[];
46 #ifdef RINGBUFFER
47 extern struct Tringbuffer *ringbuffer;
48 #endif
49 extern struct cli_session_actions *cli_session_actions;
50 extern struct cli_tunnel_actions *cli_tunnel_actions;
51 extern tbft *filter_list;
52
53 char *debug_levels[] = {
54 "CRIT",
55 "ERROR",
56 "WARN",
57 "INFO",
58 "CALL",
59 "DATA",
60 };
61
62 struct
63 {
64 char critical;
65 char error;
66 char warning;
67 char info;
68 char calls;
69 char data;
70 } debug_flags;
71
72 int debug_session;
73 int debug_tunnel;
74 int debug_rb_tail;
75 FILE *save_config_fh;
76
77 int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
78 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
79 int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
80 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
81 int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
82 int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
83 int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
84 int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
85 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
86 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
87 int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc);
88 int cmd_show_cluster(struct cli_def *cli, char *command, char **argv, int argc);
89 int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
90 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
91 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
92 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
93 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
94 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
95 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
96 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
97 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
98 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
99 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
100 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
101 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
102 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
103 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
104 int regular_stuff(struct cli_def *cli);
105
106 void init_cli()
107 {
108 FILE *f;
109 char buf[4096];
110 struct cli_command *c;
111 struct cli_command *c2;
112 int on = 1;
113 struct sockaddr_in addr;
114
115 cli = cli_init();
116 if (config->hostname && *config->hostname)
117 cli_set_hostname(cli, config->hostname);
118 else
119 cli_set_hostname(cli, "l2tpns");
120
121 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
122 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
123 #ifdef BGP
124 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
125 #endif /* BGP */
126 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
127 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
128 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
129 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
130 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
131 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the currently running configuration");
132 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
133 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
134 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
135 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
136 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");
137 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
138
139 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
140 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
141 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
142
143 #ifdef STATISTICS
144 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
145
146 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
147 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
148 #endif
149
150 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
151
152 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
153 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
154 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
155
156 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable interception for a user");
157 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable throttling for a user");
158 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
159
160 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
161 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP peer");
162
163 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
164 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable interception for a user");
165 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable throttling for a user");
166 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
167 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
168 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP peer");
169
170 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
171 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
172 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
173 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
174
175 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
176 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
177
178 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
179 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
180
181 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
182 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
183
184 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
185
186 // Enable regular processing
187 cli_regular(cli, regular_stuff);
188
189 if (!(f = fopen(CLIUSERS, "r")))
190 {
191 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
192 }
193 else
194 {
195 while (fgets(buf, 4096, f))
196 {
197 char *p;
198 if (*buf == '#') continue;
199 if ((p = strchr(buf, '\r'))) *p = 0;
200 if ((p = strchr(buf, '\n'))) *p = 0;
201 if (!*buf) continue;
202 if (!(p = strchr((char *)buf, ':'))) continue;
203 *p++ = 0;
204 if (!strcmp(buf, "enable"))
205 {
206 cli_allow_enable(cli, p);
207 log(3, 0, 0, 0, "Setting enable password\n");
208 }
209 else
210 {
211 cli_allow_user(cli, buf, p);
212 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
213 }
214 }
215 fclose(f);
216 }
217
218 memset(&addr, 0, sizeof(addr));
219 clifd = socket(PF_INET, SOCK_STREAM, 6);
220 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
221 {
222 int flags;
223 // Set cli fd as non-blocking
224 flags = fcntl(clifd, F_GETFL, 0);
225 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
226 }
227 addr.sin_family = AF_INET;
228 addr.sin_port = htons(23);
229 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
230 {
231 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
232 return;
233 }
234 listen(clifd, 10);
235 }
236
237 void cli_do(int sockfd)
238 {
239 int i;
240 int require_auth = 1;
241 struct sockaddr_in addr;
242 int l = sizeof(addr);
243
244 if (fork()) return;
245 if (config->scheduler_fifo)
246 {
247 int ret;
248 struct sched_param params = {0};
249 params.sched_priority = 0;
250 if ((ret = sched_setscheduler(0, SCHED_OTHER, &params)) == 0)
251 {
252 log(3, 0, 0, 0, "Dropped FIFO scheduler\n");
253 }
254 else
255 {
256 log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno));
257 log(0, 0, 0, 0, "This is probably really really bad.\n");
258 }
259 }
260
261 if (config->hostname && *config->hostname)
262 cli_set_hostname(cli, config->hostname);
263
264 signal(SIGPIPE, SIG_DFL);
265 signal(SIGCHLD, SIG_DFL);
266 signal(SIGHUP, SIG_DFL);
267 signal(SIGUSR1, SIG_DFL);
268 signal(SIGQUIT, SIG_DFL);
269 signal(SIGKILL, SIG_DFL);
270 signal(SIGALRM, SIG_DFL);
271 signal(SIGTERM, SIG_DFL);
272
273 // Close sockets
274 if (udpfd) close(udpfd); udpfd = 0;
275 if (tunfd) close(tunfd); tunfd = 0;
276 if (snoopfd) close(snoopfd); snoopfd = 0;
277 for (i = 0; i < config->num_radfds; i++)
278 if (radfds[i]) close(radfds[i]);
279 if (ifrfd) close(ifrfd); ifrfd = 0;
280 if (cluster_sockfd) close(cluster_sockfd); cluster_sockfd = 0;
281 if (clifd) close(clifd); clifd = 0;
282 #ifdef BGP
283 for (i = 0; i < BGP_NUM_PEERS; i++)
284 if (bgp_peers[i].sock != -1)
285 close(bgp_peers[i].sock);
286 #endif /* BGP */
287
288 if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0)
289 {
290 log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr.sin_addr.s_addr));
291 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
292 }
293 else
294 log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
295
296 if (require_auth)
297 {
298 log(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
299 if (!cli->users) /* paranoia */
300 {
301 log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
302 exit(0);
303 }
304 }
305 else
306 {
307 /* no username/pass required */
308 cli->users = 0;
309 }
310
311 debug_session = 0;
312 debug_tunnel = 0;
313 #ifdef RINGBUFFER
314 debug_rb_tail = ringbuffer->tail;
315 #endif
316 memset(&debug_flags, 0, sizeof(debug_flags));
317 debug_flags.critical = 1;
318
319 cli_loop(cli, sockfd);
320
321 close(sockfd);
322 log(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr.sin_addr.s_addr));
323 exit(0);
324 }
325
326 void cli_print_log(struct cli_def *cli, char *string)
327 {
328 log(3, 0, 0, 0, "%s\n", string);
329 }
330
331 void cli_do_file(FILE *fh)
332 {
333 log(3, 0, 0, 0, "Reading configuration file\n");
334 cli_print_callback(cli, cli_print_log);
335 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
336 cli_print_callback(cli, NULL);
337 }
338
339 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
340 {
341 va_list ap;
342 char *desc;
343 char buf[16];
344 char *p;
345
346 va_start(ap, entry);
347 while (entry)
348 {
349 /* allow one %d */
350 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
351 {
352 int v = va_arg(ap, int);
353 snprintf(buf, sizeof(buf), entry, v);
354 p = buf;
355 }
356 else
357 p = entry;
358
359 desc = va_arg(ap, char *);
360 if (desc && *desc)
361 cli_print(cli, " %-20s %s", p, desc);
362 else
363 cli_print(cli, " %s", p);
364
365 entry = desc ? va_arg(ap, char *) : 0;
366 }
367
368 va_end(ap);
369 if (cr_ok)
370 cli_print(cli, " <cr>");
371
372 return CLI_OK;
373 }
374
375 int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
376 {
377 int i;
378
379 if (CLI_HELP_REQUESTED)
380 return cli_arg_help(cli, 1,
381 "<1-%d>", MAXSESSION-1, "Show specific session by id",
382 NULL);
383
384 time(&time_now);
385 if (argc > 0)
386 {
387 // Show individual session
388 for (i = 0; i < argc; i++)
389 {
390 unsigned int s, b_in, b_out;
391 s = atoi(argv[i]);
392 if (s <= 0 || s >= MAXSESSION)
393 {
394 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
395 continue;
396 }
397 cli_print(cli, "\r\nSession %d:", s);
398 cli_print(cli, " User: %s", session[s].user[0] ? session[s].user : "none");
399 cli_print(cli, " Calling Num: %s", session[s].calling);
400 cli_print(cli, " Called Num: %s", session[s].called);
401 cli_print(cli, " Tunnel ID: %d", session[s].tunnel);
402 cli_print(cli, " IP address: %s", inet_toa(htonl(session[s].ip)));
403 cli_print(cli, " Unique SID: %lu", session[s].unique_id);
404 cli_print(cli, " Idle time: %u seconds", abs(time_now - session[s].last_packet));
405 cli_print(cli, " Next Recv: %u", session[s].nr);
406 cli_print(cli, " Next Send: %u", session[s].ns);
407 cli_print(cli, " Bytes In/Out: %lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin);
408 cli_print(cli, " Pkts In/Out: %lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin);
409 cli_print(cli, " MRU: %d", session[s].mru);
410 cli_print(cli, " Radius Session: %u", session[s].radius);
411 cli_print(cli, " Rx Speed: %lu", session[s].rx_connect_speed);
412 cli_print(cli, " Tx Speed: %lu", session[s].tx_connect_speed);
413 if (session[s].snoop_ip && session[s].snoop_port)
414 cli_print(cli, " Intercepted: %s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port);
415 else
416 cli_print(cli, " Intercepted: no");
417 cli_print(cli, " Throttled: %s", session[s].throttle ? "YES" : "no");
418 cli_print(cli, " Walled Garden: %s", session[s].walled_garden ? "YES" : "no");
419 b_in = session[s].tbf_in;
420 b_out = session[s].tbf_out;
421 if (b_in || b_out)
422 cli_print(cli, " %5s %6s %6s | %7s %7s %8s %8s %8s %8s",
423 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
424 "ByteSent", "PackSent", "PackDrop", "PackDelay");
425
426 if (b_in)
427 cli_print(cli, " TBFI#%d%1s %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
428 b_in,
429 (filter_list[b_in].next ? "*" : " "),
430 filter_list[b_in].rate * 8,
431 filter_list[b_in].credit,
432 filter_list[b_in].queued,
433 filter_list[b_in].b_queued,
434 filter_list[b_in].p_queued,
435 filter_list[b_in].b_sent,
436 filter_list[b_in].p_sent,
437 filter_list[b_in].p_dropped,
438 filter_list[b_in].p_delayed);
439
440 if (b_out)
441 cli_print(cli, " TBFO#%d%1s %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
442 b_out,
443 (filter_list[b_out].next ? "*" : " "),
444 filter_list[b_out].rate * 8,
445 filter_list[b_out].credit,
446 filter_list[b_out].queued,
447 filter_list[b_out].b_queued,
448 filter_list[b_out].p_queued,
449 filter_list[b_out].b_sent,
450 filter_list[b_out].p_sent,
451 filter_list[b_out].p_dropped,
452 filter_list[b_out].p_delayed);
453
454 }
455 return CLI_OK;
456 }
457
458 // Show Summary
459 cli_print(cli, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
460 "SID",
461 "TID",
462 "Username",
463 "IP",
464 "I",
465 "T",
466 "G",
467 "opened",
468 "downloaded",
469 "uploaded",
470 "idle",
471 "LAC",
472 "CLI");
473 for (i = 1; i < MAXSESSION; i++)
474 {
475 char *userip, *tunnelip;
476 if (!session[i].opened) continue;
477 userip = strdup(inet_toa(htonl(session[i].ip)));
478 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
479 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
480 i,
481 session[i].tunnel,
482 session[i].user[0] ? session[i].user : "*",
483 userip,
484 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
485 (session[i].throttle) ? "Y" : "N",
486 (session[i].walled_garden) ? "Y" : "N",
487 abs(time_now - (unsigned long)session[i].opened),
488 (unsigned long)session[i].total_cout,
489 (unsigned long)session[i].total_cin,
490 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
491 tunnelip,
492 session[i].calling[0] ? session[i].calling : "*");
493 if (userip) free(userip);
494 if (tunnelip) free(tunnelip);
495 }
496 return CLI_OK;
497 }
498
499 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
500 {
501 int i, x, show_all = 0;
502 char *states[] = {
503 "Free",
504 "Open",
505 "Closing",
506 "Opening",
507 };
508
509 if (CLI_HELP_REQUESTED)
510 {
511 if (argc > 1)
512 return cli_arg_help(cli, 1,
513 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
514 NULL);
515
516 return cli_arg_help(cli, 1,
517 "all", "Show all tunnels, including unused",
518 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
519 NULL);
520 }
521
522 time(&time_now);
523 if (argc > 0)
524 {
525 if (strcmp(argv[0], "all") == 0)
526 {
527 show_all = 1;
528 }
529 else
530 {
531 // Show individual tunnel
532 for (i = 0; i < argc; i++)
533 {
534 char s[65535] = {0};
535 unsigned int t;
536 t = atoi(argv[i]);
537 if (t <= 0 || t >= MAXTUNNEL)
538 {
539 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
540 continue;
541 }
542 cli_print(cli, "\r\nTunnel %d:", t);
543 cli_print(cli, " State: %s", states[tunnel[t].state]);
544 cli_print(cli, " Hostname: %s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
545 cli_print(cli, " Remote IP: %s", inet_toa(htonl(tunnel[t].ip)));
546 cli_print(cli, " Remote Port: %d", tunnel[t].port);
547 cli_print(cli, " Rx Window: %u", tunnel[t].window);
548 cli_print(cli, " Next Recv: %u", tunnel[t].nr);
549 cli_print(cli, " Next Send: %u", tunnel[t].ns);
550 cli_print(cli, " Queue Len: %u", tunnel[t].controlc);
551 cli_print(cli, " Last Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
552
553 for (x = 0; x < MAXSESSION; x++)
554 if (session[x].tunnel == t && session[x].opened && !session[x].die)
555 sprintf(s, "%s%u ", s, x);
556 cli_print(cli, " Sessions: %s", s);
557 }
558 return CLI_OK;
559 }
560 }
561
562 // Show tunnel summary
563 cli_print(cli, "%4s %20s %20s %6s %s",
564 "TID",
565 "Hostname",
566 "IP",
567 "State",
568 "Sessions");
569 for (i = 1; i < MAXTUNNEL; i++)
570 {
571 int sessions = 0;
572 if (!show_all && (!tunnel[i].ip || tunnel[i].die || !tunnel[i].hostname[0])) continue;
573
574 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
575 cli_print(cli, "%4d %20s %20s %6s %6d",
576 i,
577 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
578 inet_toa(htonl(tunnel[i].ip)),
579 states[tunnel[i].state],
580 sessions);
581 }
582 return CLI_OK;
583 }
584
585 int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
586 {
587 char sid[32][8];
588 char *sargv[32];
589 int sargc = 0;
590 int i;
591
592 if (CLI_HELP_REQUESTED)
593 return cli_arg_help(cli, 1,
594 "USER", "Show details for specific username",
595 NULL);
596
597 for (i = 0; i < MAXSESSION; i++)
598 {
599 if (!session[i].opened) continue;
600 if (!session[i].user[0]) continue;
601 if (argc > 0)
602 {
603 int j;
604 for (j = 0; j < argc && sargc < 32; j++)
605 {
606 if (strcmp(argv[j], session[i].user) == 0)
607 {
608 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
609 sargv[sargc] = sid[sargc];
610 sargc++;
611 }
612 }
613
614 continue;
615 }
616
617 cli_print(cli, "%s", session[i].user);
618 }
619
620 if (sargc > 0)
621 return cmd_show_session(cli, "users", sargv, sargc);
622
623 return CLI_OK;
624 }
625
626 int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
627 {
628 if (CLI_HELP_REQUESTED)
629 return CLI_HELP_NO_ARGS;
630
631 cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
632 cli_print(cli, "%-10s %8lu %8lu %8lu", "RX",
633 GET_STAT(tun_rx_bytes),
634 GET_STAT(tun_rx_packets),
635 GET_STAT(tun_rx_errors));
636 cli_print(cli, "%-10s %8lu %8lu %8lu", "TX",
637 GET_STAT(tun_tx_bytes),
638 GET_STAT(tun_tx_packets),
639 GET_STAT(tun_tx_errors));
640 cli_print(cli, "");
641
642 cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
643 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX",
644 GET_STAT(tunnel_rx_bytes),
645 GET_STAT(tunnel_rx_packets),
646 GET_STAT(tunnel_rx_errors),
647 0L);
648 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX",
649 GET_STAT(tunnel_tx_bytes),
650 GET_STAT(tunnel_tx_packets),
651 GET_STAT(tunnel_tx_errors),
652 GET_STAT(tunnel_retries));
653 cli_print(cli, "");
654
655 cli_print(cli, "%-30s%-10s", "Counter", "Value");
656 cli_print(cli, "-----------------------------------------");
657 cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries));
658 cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent));
659 cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped));
660 cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created));
661 cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created));
662 cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout));
663 cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout));
664 cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout));
665 cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow));
666 cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow));
667 cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow));
668 cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated));
669 cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed));
670 cli_print(cli, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded));
671 cli_print(cli, "%-30s%lu", "recv_forward", GET_STAT(recv_forward));
672
673
674 #ifdef STATISTICS
675 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
676 cli_print(cli, "-----------------------------------------");
677 cli_print(cli, "%-30s%lu", "call_processtun", GET_STAT(call_processtun));
678 cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout));
679 cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp));
680 cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap));
681 cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap));
682 cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp));
683 cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp));
684 cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin));
685 cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp));
686 cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad));
687 cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp));
688 cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp));
689 cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap));
690 cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip));
691 cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
692 cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend));
693 cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill));
694 cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
695 cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill));
696 cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
697 cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup));
698 cli_print(cli, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address));
699 cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address));
700 cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
701 cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend));
702 cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry));
703 #endif
704 return CLI_OK;
705 }
706
707 int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
708 {
709 int tag = 0;
710 int file = 0;
711 int i = 0;
712
713 if (CLI_HELP_REQUESTED)
714 return cli_arg_help(cli, 1,
715 "tag", "Include CVS release tag",
716 "file", "Include file versions",
717 NULL);
718
719 for (i = 0; i < argc; i++)
720 if (!strcmp(argv[i], "tag"))
721 tag++;
722 else if (!strcmp(argv[i], "file"))
723 file++;
724
725 cli_print(cli, "L2TPNS %s", VERSION);
726 if (tag)
727 {
728 char const *p = strchr(cvs_name, ':');
729 char const *e;
730 if (p)
731 {
732 p++;
733 while (isspace(*p))
734 p++;
735 }
736
737 if (!p || *p == '$')
738 p = "HEAD";
739
740 e = strpbrk(p, " \t$");
741 cli_print(cli, "Tag: %.*s", e ? e - p + 1 : strlen(p), p);
742 }
743
744 if (file)
745 {
746 extern linked_list *loaded_plugins;
747 void *p;
748
749 cli_print(cli, "Files:");
750 cli_print(cli, " %s", cvs_id_arp);
751 #ifdef BGP
752 cli_print(cli, " %s", cvs_id_bgp);
753 #endif /* BGP */
754 cli_print(cli, " %s", cvs_id_cli);
755 cli_print(cli, " %s", cvs_id_cluster);
756 cli_print(cli, " %s", cvs_id_constants);
757 cli_print(cli, " %s", cvs_id_control);
758 cli_print(cli, " %s", cvs_id_icmp);
759 cli_print(cli, " %s", cvs_id_l2tpns);
760 cli_print(cli, " %s", cvs_id_ll);
761 cli_print(cli, " %s", cvs_id_md5);
762 cli_print(cli, " %s", cvs_id_ppp);
763 cli_print(cli, " %s", cvs_id_radius);
764 cli_print(cli, " %s", cvs_id_tbf);
765 cli_print(cli, " %s", cvs_id_util);
766
767 ll_reset(loaded_plugins);
768 while ((p = ll_next(loaded_plugins)))
769 {
770 char const **id = dlsym(p, "cvs_id");
771 if (id)
772 cli_print(cli, " %s", *id);
773 }
774 }
775
776 return CLI_OK;
777 }
778
779 int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
780 {
781 int i;
782 int used = 0, free = 0, show_all = 0;
783
784 if (!config->cluster_iam_master)
785 {
786 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
787 return CLI_OK;
788 }
789
790 if (CLI_HELP_REQUESTED)
791 {
792 if (argc > 1)
793 return cli_arg_help(cli, 1, NULL);
794
795 return cli_arg_help(cli, 1,
796 "all", "Show all pool addresses, including unused",
797 NULL);
798 }
799
800 if (argc > 0 && strcmp(argv[0], "all") == 0)
801 show_all = 1;
802
803 time(&time_now);
804 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
805 for (i = 0; i < MAXIPPOOL; i++)
806 {
807 if (!ip_address_pool[i].address) continue;
808 if (ip_address_pool[i].assigned)
809 {
810 cli_print(cli, "%-15s Y %8d %s",
811 inet_toa(htonl(ip_address_pool[i].address)), ip_address_pool[i].session, session[ip_address_pool[i].session].user);
812
813 used++;
814 }
815 else
816 {
817 if (ip_address_pool[i].last)
818 cli_print(cli, "%-15s N %8s [%s] %ds",
819 inet_toa(htonl(ip_address_pool[i].address)), "",
820 ip_address_pool[i].user, time_now - ip_address_pool[i].last);
821 else if (show_all)
822 cli_print(cli, "%-15s N", inet_toa(htonl(ip_address_pool[i].address)));
823
824 free++;
825 }
826 }
827
828 if (!show_all)
829 cli_print(cli, "(Not displaying unused addresses)");
830
831 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
832 return CLI_OK;
833 }
834
835 void print_save_config(struct cli_def *cli, char *string)
836 {
837 if (save_config_fh)
838 fprintf(save_config_fh, "%s\n", string);
839 }
840
841 int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
842 {
843 if (CLI_HELP_REQUESTED)
844 return CLI_HELP_NO_ARGS;
845
846 if ((save_config_fh = fopen(config->config_file, "w")))
847 {
848 cli_print(cli, "Writing configuration");
849 cli_print_callback(cli, print_save_config);
850 cmd_show_run(cli, command, argv, argc);
851 cli_print_callback(cli, NULL);
852 fclose(save_config_fh);
853 }
854 else
855 {
856 cli_print(cli, "Error writing configuration: %s", strerror(errno));
857 }
858 return CLI_OK;
859 }
860
861 int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
862 {
863 int i;
864
865 if (CLI_HELP_REQUESTED)
866 return CLI_HELP_NO_ARGS;
867
868 cli_print(cli, "# Current configuration:");
869
870 for (i = 0; config_values[i].key; i++)
871 {
872 void *value = ((void *)config) + config_values[i].offset;
873 if (config_values[i].type == STRING)
874 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value);
875 else if (config_values[i].type == IP)
876 cli_print(cli, "set %s %s", config_values[i].key, inet_toa(*(unsigned *)value));
877 else if (config_values[i].type == SHORT)
878 cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value);
879 else if (config_values[i].type == BOOL)
880 cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no");
881 else if (config_values[i].type == INT)
882 cli_print(cli, "set %s %d", config_values[i].key, *(int *)value);
883 else if (config_values[i].type == UNSIGNED_LONG)
884 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value);
885 }
886
887 cli_print(cli, "# Plugins");
888 for (i = 0; i < MAXPLUGINS; i++)
889 {
890 if (*config->plugins[i])
891 {
892 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
893 }
894 }
895
896 cli_print(cli, "# end");
897 return CLI_OK;
898 }
899
900 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
901 {
902 int i, free = 0, used = 0, show_all = 0;
903 char *states[] = {
904 "NULL",
905 "CHAP",
906 "AUTH",
907 "IPCP",
908 "START",
909 "STOP",
910 "WAIT",
911 };
912
913 if (CLI_HELP_REQUESTED)
914 {
915 if (argc > 1)
916 return cli_arg_help(cli, 1, NULL);
917
918 return cli_arg_help(cli, 1,
919 "all", "Show all RADIUS sessions, including unused",
920 NULL);
921 }
922
923 cli_print(cli, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
924
925 time(&time_now);
926
927 if (argc > 0 && strcmp(argv[0], "all") == 0)
928 show_all = 1;
929
930 for (i = 1; i < MAXRADIUS; i++)
931 {
932 if (radius[i].state == RADIUSNULL)
933 free++;
934 else
935 used++;
936
937 if (!show_all && radius[i].state == RADIUSNULL) continue;
938
939 cli_print(cli, "%6d%6d%5d%6s%9d%9u%4d",
940 i,
941 i >> RADIUS_SHIFT,
942 i & RADIUS_MASK,
943 states[radius[i].state],
944 radius[i].session,
945 radius[i].retry,
946 radius[i].try);
947 }
948
949 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
950
951 return CLI_OK;
952 }
953
954 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
955 {
956 int i;
957
958 if (CLI_HELP_REQUESTED)
959 return CLI_HELP_NO_ARGS;
960
961 cli_print(cli, "Plugins currently loaded:");
962 for (i = 0; i < MAXPLUGINS; i++)
963 {
964 if (*config->plugins[i])
965 {
966 cli_print(cli, " %s", config->plugins[i]);
967 }
968 }
969 return CLI_OK;
970 }
971
972 int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc)
973 {
974 int i;
975
976 if (CLI_HELP_REQUESTED)
977 return CLI_HELP_NO_ARGS;
978
979 cli_print(cli, "Token bucket filters:");
980 cli_print(cli, "%-6s %8s %-4s", "ID", "Handle", "Used");
981 for (i = 0; i < MAXSESSION; i++)
982 {
983 if (!session[i].throttle)
984 continue;
985
986 cli_print(cli, "%-6d %8d %8d",
987 i,
988 session[i].tbf_in,
989 session[i].tbf_out);
990 }
991 return CLI_OK;
992 }
993
994 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
995 {
996 if (CLI_HELP_REQUESTED)
997 return CLI_HELP_NO_ARGS;
998
999 cli_print(cli, " _\n"
1000 "//\\\n"
1001 "V \\\n"
1002 " \\ \\_\n"
1003 " \\,'.`-.\n"
1004 " |\\ `. `.\n"
1005 " ( \\ `. `-. _,.-:\\\n"
1006 " \\ \\ `. `-._ __..--' ,-';/\n"
1007 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1008 " `. `. `-._ __..--' ,' /\n"
1009 " `. `-_ ``--..'' _.-' ,'\n"
1010 " `-_ `-.___ __,--' ,'\n"
1011 " `-.__ `----\"\"\" __.-'\n"
1012 "hh `--..____..--'");
1013
1014 return CLI_OK;
1015 }
1016
1017 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
1018 {
1019 if (CLI_HELP_REQUESTED)
1020 return CLI_HELP_NO_ARGS;
1021
1022 cli_print(cli, "Counters cleared");
1023 SET_STAT(last_reset, time(NULL));
1024 return CLI_OK;
1025 }
1026
1027 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
1028 {
1029 int i;
1030 sessionidt s;
1031
1032 if (CLI_HELP_REQUESTED)
1033 return cli_arg_help(cli, argc > 1,
1034 "USER", "Username of session to drop", NULL);
1035
1036 if (!config->cluster_iam_master)
1037 {
1038 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1039 return CLI_OK;
1040 }
1041
1042 if (!argc)
1043 {
1044 cli_print(cli, "Specify a user to drop");
1045 return CLI_OK;
1046 }
1047
1048 for (i = 0; i < argc; i++)
1049 {
1050 if (!(s = sessionbyuser(argv[i])))
1051 {
1052 cli_print(cli, "User %s is not connected", argv[i]);
1053 continue;
1054 }
1055
1056 if (session[s].ip && session[s].opened && !session[s].die)
1057 {
1058 cli_print(cli, "Dropping user %s", session[s].user);
1059 cli_session_actions[s].action |= CLI_SESS_KILL;
1060 }
1061 }
1062
1063 return CLI_OK;
1064 }
1065
1066 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1067 {
1068 int i;
1069 tunnelidt t;
1070
1071 if (CLI_HELP_REQUESTED)
1072 return cli_arg_help(cli, argc > 1,
1073 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1074
1075 if (!config->cluster_iam_master)
1076 {
1077 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1078 return CLI_OK;
1079 }
1080
1081 if (!argc)
1082 {
1083 cli_print(cli, "Specify a tunnel to drop");
1084 return CLI_OK;
1085 }
1086
1087 for (i = 0; i < argc; i++)
1088 {
1089 if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL))
1090 {
1091 cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1092 continue;
1093 }
1094
1095 if (!tunnel[t].ip)
1096 {
1097 cli_print(cli, "Tunnel %d is not connected", t);
1098 continue;
1099 }
1100
1101 if (tunnel[t].die)
1102 {
1103 cli_print(cli, "Tunnel %d is already being shut down", t);
1104 continue;
1105 }
1106
1107 cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname);
1108 cli_tunnel_actions[t].action |= CLI_TUN_KILL;
1109 }
1110
1111 return CLI_OK;
1112 }
1113
1114 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1115 {
1116 int i;
1117 sessionidt s;
1118
1119 if (CLI_HELP_REQUESTED)
1120 return cli_arg_help(cli, argc > 1,
1121 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1122
1123 if (!config->cluster_iam_master)
1124 {
1125 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1126 return CLI_OK;
1127 }
1128
1129 if (!argc)
1130 {
1131 cli_print(cli, "Specify a session id to drop");
1132 return CLI_OK;
1133 }
1134
1135 for (i = 0; i < argc; i++)
1136 {
1137 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1138 {
1139 cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1140 continue;
1141 }
1142
1143 if (session[s].ip && session[s].opened && !session[s].die)
1144 {
1145 cli_print(cli, "Dropping session %d", s);
1146 cli_session_actions[s].action |= CLI_SESS_KILL;
1147 }
1148 else
1149 {
1150 cli_print(cli, "Session %d is not active.", s);
1151 }
1152 }
1153
1154 return CLI_OK;
1155 }
1156
1157 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1158 {
1159 ipt ip;
1160 u16 port;
1161 sessionidt s;
1162
1163 if (CLI_HELP_REQUESTED)
1164 {
1165 switch (argc)
1166 {
1167 case 1:
1168 return cli_arg_help(cli, 0,
1169 "USER", "Username of session to snoop", NULL);
1170
1171 case 2:
1172 return cli_arg_help(cli, 0,
1173 "A.B.C.D", "IP address of snoop destination", NULL);
1174
1175 case 3:
1176 return cli_arg_help(cli, 0,
1177 "N", "Port of snoop destination", NULL);
1178
1179 case 4:
1180 if (!argv[3][1])
1181 return cli_arg_help(cli, 1, NULL);
1182
1183 default:
1184 return CLI_OK;
1185 }
1186 }
1187
1188 if (!config->cluster_iam_master)
1189 {
1190 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1191 return CLI_OK;
1192 }
1193
1194 if (argc < 3)
1195 {
1196 cli_print(cli, "Specify username ip port");
1197 return CLI_OK;
1198 }
1199
1200 if (!(s = sessionbyuser(argv[0])))
1201 {
1202 cli_print(cli, "User %s is not connected", argv[0]);
1203 return CLI_OK;
1204 }
1205
1206 ip = inet_addr(argv[1]);
1207 if (!ip || ip == INADDR_NONE)
1208 {
1209 cli_print(cli, "Cannot parse IP \"%s\"", argv[1]);
1210 return CLI_OK;
1211 }
1212
1213 port = atoi(argv[2]);
1214 if (!port)
1215 {
1216 cli_print(cli, "Invalid port %s", argv[2]);
1217 return CLI_OK;
1218 }
1219
1220 cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(session[s].snoop_ip), session[s].snoop_port);
1221 cli_session_actions[s].snoop_ip = ip;
1222 cli_session_actions[s].snoop_port = port;
1223 cli_session_actions[s].action |= CLI_SESS_SNOOP;
1224
1225 return CLI_OK;
1226 }
1227
1228 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1229 {
1230 int i;
1231 sessionidt s;
1232
1233 if (CLI_HELP_REQUESTED)
1234 return cli_arg_help(cli, argc > 1,
1235 "USER", "Username of session to un-snoop", NULL);
1236
1237 if (!config->cluster_iam_master)
1238 {
1239 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1240 return CLI_OK;
1241 }
1242
1243 if (!argc)
1244 {
1245 cli_print(cli, "Specify a user");
1246 return CLI_OK;
1247 }
1248
1249 for (i = 0; i < argc; i++)
1250 {
1251 if (!(s = sessionbyuser(argv[i])))
1252 {
1253 cli_print(cli, "User %s is not connected", argv[i]);
1254 continue;
1255 }
1256
1257 cli_print(cli, "Not snooping user %s", argv[i]);
1258 cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
1259 }
1260 return CLI_OK;
1261 }
1262
1263 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1264 {
1265 int rate_in = 0;
1266 int rate_out = 0;
1267 sessionidt s;
1268
1269 if (CLI_HELP_REQUESTED)
1270 {
1271 switch (argc)
1272 {
1273 case 1:
1274 return cli_arg_help(cli, 0, "user", "Username of session to throttle", NULL);
1275
1276 case 2:
1277 return cli_arg_help(cli, 1, "rate", "Incoming rate in kb/s", NULL);
1278
1279 case 3:
1280 return cli_arg_help(cli, 1, "rate", "Outgoing rate in kb/s", NULL);
1281
1282 default:
1283 return cli_arg_help(cli, argc > 1, "user", "Username of session to throttle", NULL);
1284 }
1285 }
1286
1287 if (!config->cluster_iam_master)
1288 {
1289 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1290 return CLI_OK;
1291 }
1292
1293 if (argc == 0)
1294 {
1295 cli_print(cli, "You must specify at least a username");
1296 return CLI_OK;
1297 }
1298
1299 rate_in = rate_out = config->rl_rate;
1300 if (argc >= 2) rate_in = atoi(argv[1]);
1301 if (argc >= 3) rate_out = atoi(argv[2]);
1302
1303 if (!(s = sessionbyuser(argv[0])))
1304 {
1305 cli_print(cli, "User %s is not connected", argv[0]);
1306 return CLI_OK;
1307 }
1308
1309 if (session[s].throttle)
1310 {
1311 cli_print(cli, "User %s already throttled, unthrottle first", argv[0]);
1312 return CLI_OK;
1313 }
1314
1315 cli_print(cli, "Throttling user %s", argv[0]);
1316 cli_session_actions[s].throttle = rate_in << 16 | rate_out;
1317 cli_session_actions[s].action |= CLI_SESS_THROTTLE;
1318
1319 return CLI_OK;
1320 }
1321
1322 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1323 {
1324 int i;
1325 sessionidt s;
1326
1327 if (CLI_HELP_REQUESTED)
1328 return cli_arg_help(cli, argc > 1,
1329 "USER", "Username of session to un-throttle", NULL);
1330
1331 if (!config->cluster_iam_master)
1332 {
1333 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1334 return CLI_OK;
1335 }
1336
1337 if (!argc)
1338 {
1339 cli_print(cli, "Specify a user");
1340 return CLI_OK;
1341 }
1342
1343 for (i = 0; i < argc; i++)
1344 {
1345 if (!(s = sessionbyuser(argv[i])))
1346 {
1347 cli_print(cli, "User %s is not connected", argv[i]);
1348 continue;
1349 }
1350
1351 if (!session[s].throttle)
1352 {
1353 cli_print(cli, "User %s not throttled", argv[i]);
1354 continue;
1355 }
1356
1357 cli_print(cli, "Unthrottling user %s", argv[i]);
1358 cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
1359 }
1360
1361 return CLI_OK;
1362 }
1363
1364 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1365 {
1366 int i;
1367
1368 if (CLI_HELP_REQUESTED)
1369 return cli_arg_help(cli, 1,
1370 "all", "Enable debugging for all except \"data\"",
1371 "critical", "", // FIXME: add descriptions
1372 "error", "",
1373 "warning", "",
1374 "info", "",
1375 "calls", "",
1376 "data", "",
1377 NULL);
1378
1379 if (!argc)
1380 {
1381 char *p = (char *) &debug_flags;
1382 for (i = 0; i < sizeof(debug_flags); i++)
1383 {
1384 if (p[i])
1385 {
1386 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1387 (debug_flags.critical) ? " critical" : "",
1388 (debug_flags.error) ? " error" : "",
1389 (debug_flags.warning) ? " warning" : "",
1390 (debug_flags.info) ? " info" : "",
1391 (debug_flags.calls) ? " calls" : "",
1392 (debug_flags.data) ? " data" : "");
1393
1394 return CLI_OK;
1395 }
1396 }
1397
1398 cli_print(cli, "Debugging off");
1399 return CLI_OK;
1400 }
1401
1402 for (i = 0; i < argc; i++)
1403 {
1404 int len = strlen(argv[i]);
1405
1406 if (argv[i][0] == 'c' && len < 2)
1407 len = 2; /* distinguish [cr]itical from [ca]lls */
1408
1409 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; }
1410 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; }
1411 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; }
1412 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; }
1413 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; }
1414 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; }
1415 if (!strncasecmp(argv[i], "all", len))
1416 {
1417 memset(&debug_flags, 1, sizeof(debug_flags));
1418 debug_flags.data = 0;
1419 continue;
1420 }
1421
1422 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1423 }
1424
1425 return CLI_OK;
1426 }
1427
1428 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1429 {
1430 int i;
1431
1432 if (CLI_HELP_REQUESTED)
1433 return cli_arg_help(cli, 1,
1434 "all", "Disable all debugging",
1435 "critical", "", // FIXME: add descriptions
1436 "error", "",
1437 "warning", "",
1438 "info", "",
1439 "calls", "",
1440 "data", "",
1441 NULL);
1442
1443 if (!argc)
1444 {
1445 memset(&debug_flags, 0, sizeof(debug_flags));
1446 return CLI_OK;
1447 }
1448
1449 for (i = 0; i < argc; i++)
1450 {
1451 int len = strlen(argv[i]);
1452
1453 if (argv[i][0] == 'c' && len < 2)
1454 len = 2; /* distinguish [cr]itical from [ca]lls */
1455
1456 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; }
1457 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; }
1458 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; }
1459 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; }
1460 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; }
1461 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; }
1462 if (!strncasecmp(argv[i], "all", len))
1463 {
1464 memset(&debug_flags, 0, sizeof(debug_flags));
1465 continue;
1466 }
1467
1468 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1469 }
1470
1471 return CLI_OK;
1472 }
1473
1474 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1475 {
1476 int i, firstfree = 0;
1477
1478 if (CLI_HELP_REQUESTED)
1479 return cli_arg_help(cli, argc > 1,
1480 "PLUGIN", "Name of plugin to load", NULL);
1481
1482 if (argc != 1)
1483 {
1484 cli_print(cli, "Specify a plugin to load");
1485 return CLI_OK;
1486 }
1487
1488 for (i = 0; i < MAXPLUGINS; i++)
1489 {
1490 if (!*config->plugins[i] && !firstfree)
1491 firstfree = i;
1492 if (strcmp(config->plugins[i], argv[0]) == 0)
1493 {
1494 cli_print(cli, "Plugin is already loaded");
1495 return CLI_OK;
1496 }
1497 }
1498
1499 if (firstfree)
1500 {
1501 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1502 config->reload_config = 1;
1503 cli_print(cli, "Loading plugin %s", argv[0]);
1504 }
1505
1506 return CLI_OK;
1507 }
1508
1509 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1510 {
1511 int i;
1512
1513 if (CLI_HELP_REQUESTED)
1514 return cli_arg_help(cli, argc > 1,
1515 "PLUGIN", "Name of plugin to unload", NULL);
1516
1517 if (argc != 1)
1518 {
1519 cli_print(cli, "Specify a plugin to remove");
1520 return CLI_OK;
1521 }
1522
1523 for (i = 0; i < MAXPLUGINS; i++)
1524 {
1525 if (strcmp(config->plugins[i], argv[0]) == 0)
1526 {
1527 config->reload_config = 1;
1528 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1529 return CLI_OK;
1530 }
1531 }
1532
1533 cli_print(cli, "Plugin is not loaded");
1534 return CLI_OK;
1535 }
1536
1537 char *duration(time_t secs)
1538 {
1539 static char *buf = NULL;
1540 int p = 0;
1541
1542 if (!buf) buf = calloc(64, 1);
1543
1544 if (secs >= 86400)
1545 {
1546 int days = secs / 86400;
1547 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1548 secs %= 86400;
1549 }
1550
1551 if (secs >= 3600)
1552 {
1553 int mins = secs / 60;
1554 int hrs = mins / 60;
1555
1556 mins %= 60;
1557 sprintf(buf + p, "%d:%02d", hrs, mins);
1558 }
1559 else if (secs >= 60)
1560 {
1561 int mins = secs / 60;
1562 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1563 }
1564 else
1565 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1566
1567 return buf;
1568 }
1569
1570 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1571 {
1572 FILE *fh;
1573 char buf[100], *p = buf, *loads[3];
1574 int i, num_sessions = 0;
1575
1576 if (CLI_HELP_REQUESTED)
1577 return CLI_HELP_NO_ARGS;
1578
1579 fh = fopen("/proc/loadavg", "r");
1580 fgets(buf, 100, fh);
1581 fclose(fh);
1582
1583 for (i = 0; i < 3; i++)
1584 loads[i] = strdup(strsep(&p, " "));
1585
1586 time(&time_now);
1587 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1588
1589 for (i = 1; i < MAXSESSION; i++)
1590 if (session[i].opened) num_sessions++;
1591
1592 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1593 buf,
1594 duration(time_now - config->start_time),
1595 num_sessions,
1596 loads[0], loads[1], loads[2]
1597 );
1598 for (i = 0; i < 3; i++)
1599 if (loads[i]) free(loads[i]);
1600
1601 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1602
1603 return CLI_OK;
1604 }
1605
1606 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1607 {
1608 int i;
1609
1610 if (CLI_HELP_REQUESTED)
1611 {
1612 switch (argc)
1613 {
1614 case 1:
1615 {
1616 int len = strlen(argv[0])-1;
1617 for (i = 0; config_values[i].key; i++)
1618 if (!len || !strncmp(argv[0], config_values[i].key, len))
1619 cli_print(cli, " %s", config_values[i].key);
1620 }
1621
1622 return CLI_OK;
1623
1624 case 2:
1625 return cli_arg_help(cli, 0,
1626 "VALUE", "Value for variable", NULL);
1627
1628 case 3:
1629 if (!argv[2][1])
1630 return cli_arg_help(cli, 1, NULL);
1631
1632 default:
1633 return CLI_OK;
1634 }
1635 }
1636
1637 if (argc != 2)
1638 {
1639 cli_print(cli, "Specify variable and value");
1640 return CLI_OK;
1641 }
1642
1643 for (i = 0; config_values[i].key; i++)
1644 {
1645 void *value = ((void *)config) + config_values[i].offset;
1646 if (strcmp(config_values[i].key, argv[0]) == 0)
1647 {
1648 // Found a value to set
1649 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1650 switch (config_values[i].type)
1651 {
1652 case STRING:
1653 strncpy((char *)value, argv[1], config_values[i].size - 1);
1654 break;
1655 case INT:
1656 *(int *)value = atoi(argv[1]);
1657 break;
1658 case UNSIGNED_LONG:
1659 *(unsigned long *)value = atol(argv[1]);
1660 break;
1661 case SHORT:
1662 *(short *)value = atoi(argv[1]);
1663 break;
1664 case IP:
1665 *(unsigned *)value = inet_addr(argv[1]);
1666 break;
1667 case BOOL:
1668 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1669 *(int *)value = 1;
1670 else
1671 *(int *)value = 0;
1672 break;
1673 default:
1674 cli_print(cli, "Unknown variable type");
1675 break;
1676 }
1677 config->reload_config = 1;
1678 return CLI_OK;
1679 }
1680 }
1681
1682 cli_print(cli, "Unknown variable \"%s\"", argv[0]);
1683 return CLI_OK;
1684 }
1685
1686 int regular_stuff(struct cli_def *cli)
1687 {
1688 int i = debug_rb_tail;
1689 int reprompt = 0;
1690
1691 #ifdef RINGBUFFER
1692 while (i != ringbuffer->tail)
1693 {
1694 int show_message = 0;
1695
1696 if (*ringbuffer->buffer[i].message)
1697 {
1698 // Always show messages if we are doing general debug
1699 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
1700 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
1701 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
1702 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
1703 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
1704 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
1705 }
1706
1707 if (show_message)
1708 {
1709 ipt address = htonl(ringbuffer->buffer[i].address);
1710 char *ipaddr;
1711 struct in_addr addr;
1712
1713 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
1714 ipaddr = inet_ntoa(addr);
1715
1716 cli_print(cli, "\r%s-%s-%u-%u %s",
1717 debug_levels[(int)ringbuffer->buffer[i].level],
1718 ipaddr,
1719 ringbuffer->buffer[i].tunnel,
1720 ringbuffer->buffer[i].session,
1721 ringbuffer->buffer[i].message);
1722
1723 reprompt = 1;
1724 }
1725
1726 if (++i == ringbuffer->tail) break;
1727 if (i == RINGBUFFER_SIZE) i = 0;
1728 }
1729
1730 debug_rb_tail = ringbuffer->tail;
1731 if (reprompt)
1732 cli_reprompt(cli);
1733 #endif
1734 return CLI_OK;
1735 }