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