unfsck 4->8 indenting change
[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.18 2004/10/28 03:31:11 bodea 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 void parsemac(char *string, char mac[6]);
106
107 void init_cli(char *hostname)
108 {
109 FILE *f;
110 char buf[4096];
111 struct cli_command *c;
112 struct cli_command *c2;
113 int on = 1;
114 struct sockaddr_in addr;
115
116 cli = cli_init();
117 if (hostname && *hostname)
118 cli_set_hostname(cli, hostname);
119 else
120 cli_set_hostname(cli, "l2tpns");
121
122 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
123 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
124 #ifdef BGP
125 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
126 #endif /* BGP */
127 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
128 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
129 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
130 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
131 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
132 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the currently running configuration");
133 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
134 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
135 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
136 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
137 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");
138 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
139
140 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
141 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
142 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
143
144 #ifdef STATISTICS
145 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
146
147 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
148 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
149 #endif
150
151 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
152
153 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
154 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
155 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
156
157 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable interception for a user");
158 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable throttling for a user");
159 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
160
161 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
162 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP peer");
163
164 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
165 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable interception for a user");
166 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable throttling for a user");
167 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
168 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
169 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP peer");
170
171 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
172 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
173 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
174 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
175
176 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
177 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
178
179 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
180 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
181
182 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
183 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
184
185 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
186
187 // Enable regular processing
188 cli_regular(cli, regular_stuff);
189
190 if (!(f = fopen(CLIUSERS, "r")))
191 {
192 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
193 }
194 else
195 {
196 while (fgets(buf, 4096, f))
197 {
198 char *p;
199 if (*buf == '#') continue;
200 if ((p = strchr(buf, '\r'))) *p = 0;
201 if ((p = strchr(buf, '\n'))) *p = 0;
202 if (!*buf) continue;
203 if (!(p = strchr((char *)buf, ':'))) continue;
204 *p++ = 0;
205 if (!strcmp(buf, "enable"))
206 {
207 cli_allow_enable(cli, p);
208 log(3, 0, 0, 0, "Setting enable password\n");
209 }
210 else
211 {
212 cli_allow_user(cli, buf, p);
213 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
214 }
215 }
216 fclose(f);
217 }
218
219 memset(&addr, 0, sizeof(addr));
220 clifd = socket(PF_INET, SOCK_STREAM, 6);
221 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
222 {
223 int flags;
224 // Set cli fd as non-blocking
225 flags = fcntl(clifd, F_GETFL, 0);
226 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
227 }
228 addr.sin_family = AF_INET;
229 addr.sin_port = htons(23);
230 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
231 {
232 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
233 return;
234 }
235 listen(clifd, 10);
236 }
237
238 void cli_do(int sockfd)
239 {
240 int i;
241 int require_auth = 1;
242 struct sockaddr_in addr;
243 int l = sizeof(addr);
244
245 if (fork()) return;
246 if (config->scheduler_fifo)
247 {
248 int ret;
249 struct sched_param params = {0};
250 params.sched_priority = 0;
251 if ((ret = sched_setscheduler(0, SCHED_OTHER, &params)) == 0)
252 {
253 log(3, 0, 0, 0, "Dropped FIFO scheduler\n");
254 }
255 else
256 {
257 log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno));
258 log(0, 0, 0, 0, "This is probably really really bad.\n");
259 }
260 }
261
262 signal(SIGPIPE, SIG_DFL);
263 signal(SIGCHLD, SIG_DFL);
264 signal(SIGHUP, SIG_DFL);
265 signal(SIGUSR1, SIG_DFL);
266 signal(SIGQUIT, SIG_DFL);
267 signal(SIGKILL, SIG_DFL);
268 signal(SIGALRM, SIG_DFL);
269 signal(SIGTERM, SIG_DFL);
270
271 // Close sockets
272 if (udpfd) close(udpfd); udpfd = 0;
273 if (tunfd) close(tunfd); tunfd = 0;
274 if (snoopfd) close(snoopfd); snoopfd = 0;
275 for (i = 0; i < config->num_radfds; i++)
276 if (radfds[i]) close(radfds[i]);
277 if (ifrfd) close(ifrfd); ifrfd = 0;
278 if (cluster_sockfd) close(cluster_sockfd); cluster_sockfd = 0;
279 if (clifd) close(clifd); clifd = 0;
280 #ifdef BGP
281 for (i = 0; i < BGP_NUM_PEERS; i++)
282 if (bgp_peers[i].sock != -1)
283 close(bgp_peers[i].sock);
284 #endif /* BGP */
285
286 if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0)
287 {
288 log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr.sin_addr.s_addr));
289 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
290 }
291 else
292 log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
293
294 if (require_auth)
295 {
296 log(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
297 if (!cli->users) /* paranoia */
298 {
299 log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
300 exit(0);
301 }
302 }
303 else
304 {
305 /* no username/pass required */
306 cli->users = 0;
307 }
308
309 debug_session = 0;
310 debug_tunnel = 0;
311 #ifdef RINGBUFFER
312 debug_rb_tail = ringbuffer->tail;
313 #endif
314 memset(&debug_flags, 0, sizeof(debug_flags));
315 debug_flags.critical = 1;
316
317 cli_loop(cli, sockfd);
318
319 close(sockfd);
320 log(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr.sin_addr.s_addr));
321 exit(0);
322 }
323
324 void cli_print_log(struct cli_def *cli, char *string)
325 {
326 log(3, 0, 0, 0, "%s\n", string);
327 }
328
329 void cli_do_file(FILE *fh)
330 {
331 log(3, 0, 0, 0, "Reading configuration file\n");
332 cli_print_callback(cli, cli_print_log);
333 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
334 cli_print_callback(cli, NULL);
335 }
336
337 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
338 {
339 va_list ap;
340 char *desc;
341 char buf[16];
342 char *p;
343
344 va_start(ap, entry);
345 while (entry)
346 {
347 /* allow one %d */
348 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
349 {
350 int v = va_arg(ap, int);
351 snprintf(buf, sizeof(buf), entry, v);
352 p = buf;
353 }
354 else
355 p = entry;
356
357 desc = va_arg(ap, char *);
358 if (desc && *desc)
359 cli_print(cli, " %-20s %s", p, desc);
360 else
361 cli_print(cli, " %s", p);
362
363 entry = desc ? va_arg(ap, char *) : 0;
364 }
365
366 va_end(ap);
367 if (cr_ok)
368 cli_print(cli, " <cr>");
369
370 return CLI_OK;
371 }
372
373 int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
374 {
375 int i;
376
377 if (CLI_HELP_REQUESTED)
378 return cli_arg_help(cli, 1,
379 "<1-%d>", MAXSESSION-1, "Show specific session by id",
380 NULL);
381
382 time(&time_now);
383 if (argc > 0)
384 {
385 // Show individual session
386 for (i = 0; i < argc; i++)
387 {
388 unsigned int s, b_in, b_out;
389 s = atoi(argv[i]);
390 if (s <= 0 || s >= MAXSESSION)
391 {
392 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
393 continue;
394 }
395 cli_print(cli, "\r\nSession %d:", s);
396 cli_print(cli, " User: %s", session[s].user[0] ? session[s].user : "none");
397 cli_print(cli, " Calling Num: %s", session[s].calling);
398 cli_print(cli, " Called Num: %s", session[s].called);
399 cli_print(cli, " Tunnel ID: %d", session[s].tunnel);
400 cli_print(cli, " IP address: %s", inet_toa(htonl(session[s].ip)));
401 cli_print(cli, " Unique SID: %lu", session[s].unique_id);
402 cli_print(cli, " Idle time: %u seconds", abs(time_now - session[s].last_packet));
403 cli_print(cli, " Next Recv: %u", session[s].nr);
404 cli_print(cli, " Next Send: %u", session[s].ns);
405 cli_print(cli, " Bytes In/Out: %lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin);
406 cli_print(cli, " Pkts In/Out: %lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin);
407 cli_print(cli, " MRU: %d", session[s].mru);
408 cli_print(cli, " Radius Session: %u", session[s].radius);
409 cli_print(cli, " Rx Speed: %lu", session[s].rx_connect_speed);
410 cli_print(cli, " Tx Speed: %lu", session[s].tx_connect_speed);
411 if (session[s].snoop_ip && session[s].snoop_port)
412 cli_print(cli, " Intercepted: %s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port);
413 else
414 cli_print(cli, " Intercepted: no");
415 cli_print(cli, " Throttled: %s", session[s].throttle ? "YES" : "no");
416 cli_print(cli, " Walled Garden: %s", session[s].walled_garden ? "YES" : "no");
417 b_in = session[s].tbf_in;
418 b_out = session[s].tbf_out;
419 if (b_in || b_out)
420 cli_print(cli, " %5s %6s %6s | %7s %7s %8s %8s %8s %8s",
421 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
422 "ByteSent", "PackSent", "PackDrop", "PackDelay");
423
424 if (b_in)
425 cli_print(cli, " TBFI#%d%1s %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
426 b_in,
427 (filter_list[b_in].next ? "*" : " "),
428 filter_list[b_in].rate * 8,
429 filter_list[b_in].credit,
430 filter_list[b_in].queued,
431 filter_list[b_in].b_queued,
432 filter_list[b_in].p_queued,
433 filter_list[b_in].b_sent,
434 filter_list[b_in].p_sent,
435 filter_list[b_in].p_dropped,
436 filter_list[b_in].p_delayed);
437
438 if (b_out)
439 cli_print(cli, " TBFO#%d%1s %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
440 b_out,
441 (filter_list[b_out].next ? "*" : " "),
442 filter_list[b_out].rate * 8,
443 filter_list[b_out].credit,
444 filter_list[b_out].queued,
445 filter_list[b_out].b_queued,
446 filter_list[b_out].p_queued,
447 filter_list[b_out].b_sent,
448 filter_list[b_out].p_sent,
449 filter_list[b_out].p_dropped,
450 filter_list[b_out].p_delayed);
451
452 }
453 return CLI_OK;
454 }
455
456 // Show Summary
457 cli_print(cli, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
458 "SID",
459 "TID",
460 "Username",
461 "IP",
462 "I",
463 "T",
464 "G",
465 "opened",
466 "downloaded",
467 "uploaded",
468 "idle",
469 "LAC",
470 "CLI");
471
472 for (i = 1; i < MAXSESSION; i++)
473 {
474 char *userip, *tunnelip;
475 if (!session[i].opened) continue;
476 userip = strdup(inet_toa(htonl(session[i].ip)));
477 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
478 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
479 i,
480 session[i].tunnel,
481 session[i].user[0] ? session[i].user : "*",
482 userip,
483 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
484 (session[i].throttle) ? "Y" : "N",
485 (session[i].walled_garden) ? "Y" : "N",
486 abs(time_now - (unsigned long)session[i].opened),
487 (unsigned long)session[i].total_cout,
488 (unsigned long)session[i].total_cin,
489 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
490 tunnelip,
491 session[i].calling[0] ? session[i].calling : "*");
492 if (userip) free(userip);
493 if (tunnelip) free(tunnelip);
494 }
495 return CLI_OK;
496 }
497
498 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
499 {
500 int i, x, show_all = 0;
501 char *states[] = {
502 "Free",
503 "Open",
504 "Closing",
505 "Opening",
506 };
507
508 if (CLI_HELP_REQUESTED)
509 {
510 if (argc > 1)
511 return cli_arg_help(cli, 1,
512 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
513 NULL);
514
515 return cli_arg_help(cli, 1,
516 "all", "Show all tunnels, including unused",
517 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
518 NULL);
519 }
520
521 time(&time_now);
522 if (argc > 0)
523 {
524 if (strcmp(argv[0], "all") == 0)
525 {
526 show_all = 1;
527 }
528 else
529 {
530 // Show individual tunnel
531 for (i = 0; i < argc; i++)
532 {
533 char s[65535] = {0};
534 unsigned int t;
535 t = atoi(argv[i]);
536 if (t <= 0 || t >= MAXTUNNEL)
537 {
538 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
539 continue;
540 }
541 cli_print(cli, "\r\nTunnel %d:", t);
542 cli_print(cli, " State: %s", states[tunnel[t].state]);
543 cli_print(cli, " Hostname: %s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
544 cli_print(cli, " Remote IP: %s", inet_toa(htonl(tunnel[t].ip)));
545 cli_print(cli, " Remote Port: %d", tunnel[t].port);
546 cli_print(cli, " Rx Window: %u", tunnel[t].window);
547 cli_print(cli, " Next Recv: %u", tunnel[t].nr);
548 cli_print(cli, " Next Send: %u", tunnel[t].ns);
549 cli_print(cli, " Queue Len: %u", tunnel[t].controlc);
550 cli_print(cli, " Last Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
551
552 for (x = 0; x < MAXSESSION; x++)
553 if (session[x].tunnel == t && session[x].opened && !session[x].die)
554 sprintf(s, "%s%u ", s, x);
555
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 else if (config_values[i].type == MAC)
886 cli_print(cli, "set %s %02x%02x.%02x%02x.%02x%02x", config_values[i].key,
887 *(unsigned short *)(value + 0),
888 *(unsigned short *)(value + 1),
889 *(unsigned short *)(value + 2),
890 *(unsigned short *)(value + 3),
891 *(unsigned short *)(value + 4),
892 *(unsigned short *)(value + 5));
893 }
894
895 cli_print(cli, "# Plugins");
896 for (i = 0; i < MAXPLUGINS; i++)
897 {
898 if (*config->plugins[i])
899 {
900 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
901 }
902 }
903
904 cli_print(cli, "# end");
905 return CLI_OK;
906 }
907
908 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
909 {
910 int i, free = 0, used = 0, show_all = 0;
911 char *states[] = {
912 "NULL",
913 "CHAP",
914 "AUTH",
915 "IPCP",
916 "START",
917 "STOP",
918 "WAIT",
919 };
920
921 if (CLI_HELP_REQUESTED)
922 {
923 if (argc > 1)
924 return cli_arg_help(cli, 1, NULL);
925
926 return cli_arg_help(cli, 1,
927 "all", "Show all RADIUS sessions, including unused",
928 NULL);
929 }
930
931 cli_print(cli, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
932
933 time(&time_now);
934
935 if (argc > 0 && strcmp(argv[0], "all") == 0)
936 show_all = 1;
937
938 for (i = 1; i < MAXRADIUS; i++)
939 {
940 if (radius[i].state == RADIUSNULL)
941 free++;
942 else
943 used++;
944
945 if (!show_all && radius[i].state == RADIUSNULL) continue;
946
947 cli_print(cli, "%6d%6d%5d%6s%9d%9u%4d",
948 i,
949 i >> RADIUS_SHIFT,
950 i & RADIUS_MASK,
951 states[radius[i].state],
952 radius[i].session,
953 radius[i].retry,
954 radius[i].try);
955 }
956
957 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
958
959 return CLI_OK;
960 }
961
962 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
963 {
964 int i;
965
966 if (CLI_HELP_REQUESTED)
967 return CLI_HELP_NO_ARGS;
968
969 cli_print(cli, "Plugins currently loaded:");
970 for (i = 0; i < MAXPLUGINS; i++)
971 {
972 if (*config->plugins[i])
973 {
974 cli_print(cli, " %s", config->plugins[i]);
975 }
976 }
977 return CLI_OK;
978 }
979
980 int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc)
981 {
982 int i;
983
984 if (CLI_HELP_REQUESTED)
985 return CLI_HELP_NO_ARGS;
986
987 cli_print(cli, "Token bucket filters:");
988 cli_print(cli, "%-6s %8s %-4s", "ID", "Handle", "Used");
989 for (i = 0; i < MAXSESSION; i++)
990 {
991 if (!session[i].throttle)
992 continue;
993
994 cli_print(cli, "%-6d %8d %8d",
995 i,
996 session[i].tbf_in,
997 session[i].tbf_out);
998 }
999 return CLI_OK;
1000 }
1001
1002 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
1003 {
1004 if (CLI_HELP_REQUESTED)
1005 return CLI_HELP_NO_ARGS;
1006
1007 cli_print(cli, " _\n"
1008 "//\\\n"
1009 "V \\\n"
1010 " \\ \\_\n"
1011 " \\,'.`-.\n"
1012 " |\\ `. `.\n"
1013 " ( \\ `. `-. _,.-:\\\n"
1014 " \\ \\ `. `-._ __..--' ,-';/\n"
1015 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1016 " `. `. `-._ __..--' ,' /\n"
1017 " `. `-_ ``--..'' _.-' ,'\n"
1018 " `-_ `-.___ __,--' ,'\n"
1019 " `-.__ `----\"\"\" __.-'\n"
1020 "hh `--..____..--'");
1021
1022 return CLI_OK;
1023 }
1024
1025 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
1026 {
1027 if (CLI_HELP_REQUESTED)
1028 return CLI_HELP_NO_ARGS;
1029
1030 cli_print(cli, "Counters cleared");
1031 SET_STAT(last_reset, time(NULL));
1032 return CLI_OK;
1033 }
1034
1035 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
1036 {
1037 int i;
1038 sessionidt s;
1039
1040 if (CLI_HELP_REQUESTED)
1041 return cli_arg_help(cli, argc > 1,
1042 "USER", "Username of session to drop", NULL);
1043
1044 if (!config->cluster_iam_master)
1045 {
1046 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1047 return CLI_OK;
1048 }
1049
1050 if (!argc)
1051 {
1052 cli_print(cli, "Specify a user to drop");
1053 return CLI_OK;
1054 }
1055
1056 for (i = 0; i < argc; i++)
1057 {
1058 if (!(s = sessionbyuser(argv[i])))
1059 {
1060 cli_print(cli, "User %s is not connected", argv[i]);
1061 continue;
1062 }
1063
1064 if (session[s].ip && session[s].opened && !session[s].die)
1065 {
1066 cli_print(cli, "Dropping user %s", session[s].user);
1067 cli_session_actions[s].action |= CLI_SESS_KILL;
1068 }
1069 }
1070
1071 return CLI_OK;
1072 }
1073
1074 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1075 {
1076 int i;
1077 tunnelidt t;
1078
1079 if (CLI_HELP_REQUESTED)
1080 return cli_arg_help(cli, argc > 1,
1081 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1082
1083 if (!config->cluster_iam_master)
1084 {
1085 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1086 return CLI_OK;
1087 }
1088
1089 if (!argc)
1090 {
1091 cli_print(cli, "Specify a tunnel to drop");
1092 return CLI_OK;
1093 }
1094
1095 for (i = 0; i < argc; i++)
1096 {
1097 if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL))
1098 {
1099 cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1100 continue;
1101 }
1102
1103 if (!tunnel[t].ip)
1104 {
1105 cli_print(cli, "Tunnel %d is not connected", t);
1106 continue;
1107 }
1108
1109 if (tunnel[t].die)
1110 {
1111 cli_print(cli, "Tunnel %d is already being shut down", t);
1112 continue;
1113 }
1114
1115 cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname);
1116 cli_tunnel_actions[t].action |= CLI_TUN_KILL;
1117 }
1118
1119 return CLI_OK;
1120 }
1121
1122 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1123 {
1124 int i;
1125 sessionidt s;
1126
1127 if (CLI_HELP_REQUESTED)
1128 return cli_arg_help(cli, argc > 1,
1129 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1130
1131 if (!config->cluster_iam_master)
1132 {
1133 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1134 return CLI_OK;
1135 }
1136
1137 if (!argc)
1138 {
1139 cli_print(cli, "Specify a session id to drop");
1140 return CLI_OK;
1141 }
1142
1143 for (i = 0; i < argc; i++)
1144 {
1145 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1146 {
1147 cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1148 continue;
1149 }
1150
1151 if (session[s].ip && session[s].opened && !session[s].die)
1152 {
1153 cli_print(cli, "Dropping session %d", s);
1154 cli_session_actions[s].action |= CLI_SESS_KILL;
1155 }
1156 else
1157 {
1158 cli_print(cli, "Session %d is not active.", s);
1159 }
1160 }
1161
1162 return CLI_OK;
1163 }
1164
1165 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1166 {
1167 ipt ip;
1168 u16 port;
1169 sessionidt s;
1170
1171 if (CLI_HELP_REQUESTED)
1172 {
1173 switch (argc)
1174 {
1175 case 1:
1176 return cli_arg_help(cli, 0,
1177 "USER", "Username of session to snoop", NULL);
1178
1179 case 2:
1180 return cli_arg_help(cli, 0,
1181 "A.B.C.D", "IP address of snoop destination", NULL);
1182
1183 case 3:
1184 return cli_arg_help(cli, 0,
1185 "N", "Port of snoop destination", NULL);
1186
1187 case 4:
1188 if (!argv[3][1])
1189 return cli_arg_help(cli, 1, NULL);
1190
1191 default:
1192 return CLI_OK;
1193 }
1194 }
1195
1196 if (!config->cluster_iam_master)
1197 {
1198 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1199 return CLI_OK;
1200 }
1201
1202 if (argc < 3)
1203 {
1204 cli_print(cli, "Specify username ip port");
1205 return CLI_OK;
1206 }
1207
1208 if (!(s = sessionbyuser(argv[0])))
1209 {
1210 cli_print(cli, "User %s is not connected", argv[0]);
1211 return CLI_OK;
1212 }
1213
1214 ip = inet_addr(argv[1]);
1215 if (!ip || ip == INADDR_NONE)
1216 {
1217 cli_print(cli, "Cannot parse IP \"%s\"", argv[1]);
1218 return CLI_OK;
1219 }
1220
1221 port = atoi(argv[2]);
1222 if (!port)
1223 {
1224 cli_print(cli, "Invalid port %s", argv[2]);
1225 return CLI_OK;
1226 }
1227
1228 cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(session[s].snoop_ip), session[s].snoop_port);
1229 cli_session_actions[s].snoop_ip = ip;
1230 cli_session_actions[s].snoop_port = port;
1231 cli_session_actions[s].action |= CLI_SESS_SNOOP;
1232
1233 return CLI_OK;
1234 }
1235
1236 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1237 {
1238 int i;
1239 sessionidt s;
1240
1241 if (CLI_HELP_REQUESTED)
1242 return cli_arg_help(cli, argc > 1,
1243 "USER", "Username of session to un-snoop", NULL);
1244
1245 if (!config->cluster_iam_master)
1246 {
1247 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1248 return CLI_OK;
1249 }
1250
1251 if (!argc)
1252 {
1253 cli_print(cli, "Specify a user");
1254 return CLI_OK;
1255 }
1256
1257 for (i = 0; i < argc; i++)
1258 {
1259 if (!(s = sessionbyuser(argv[i])))
1260 {
1261 cli_print(cli, "User %s is not connected", argv[i]);
1262 continue;
1263 }
1264
1265 cli_print(cli, "Not snooping user %s", argv[i]);
1266 cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
1267 }
1268 return CLI_OK;
1269 }
1270
1271 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1272 {
1273 int rate_in = 0;
1274 int rate_out = 0;
1275 sessionidt s;
1276
1277 if (CLI_HELP_REQUESTED)
1278 {
1279 switch (argc)
1280 {
1281 case 1:
1282 return cli_arg_help(cli, 0, "user", "Username of session to throttle", NULL);
1283
1284 case 2:
1285 return cli_arg_help(cli, 1, "rate", "Incoming rate in kb/s", NULL);
1286
1287 case 3:
1288 return cli_arg_help(cli, 1, "rate", "Outgoing rate in kb/s", NULL);
1289
1290 default:
1291 return cli_arg_help(cli, argc > 1, "user", "Username of session to throttle", NULL);
1292 }
1293 }
1294
1295 if (!config->cluster_iam_master)
1296 {
1297 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1298 return CLI_OK;
1299 }
1300
1301 if (argc == 0)
1302 {
1303 cli_print(cli, "You must specify at least a username");
1304 return CLI_OK;
1305 }
1306
1307 rate_in = rate_out = config->rl_rate;
1308 if (argc >= 2) rate_in = atoi(argv[1]);
1309 if (argc >= 3) rate_out = atoi(argv[2]);
1310
1311 if (!(s = sessionbyuser(argv[0])))
1312 {
1313 cli_print(cli, "User %s is not connected", argv[0]);
1314 return CLI_OK;
1315 }
1316
1317 if (session[s].throttle)
1318 {
1319 cli_print(cli, "User %s already throttled, unthrottle first", argv[0]);
1320 return CLI_OK;
1321 }
1322
1323 cli_print(cli, "Throttling user %s", argv[0]);
1324 cli_session_actions[s].throttle = rate_in << 16 | rate_out;
1325 cli_session_actions[s].action |= CLI_SESS_THROTTLE;
1326
1327 return CLI_OK;
1328 }
1329
1330 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1331 {
1332 int i;
1333 sessionidt s;
1334
1335 if (CLI_HELP_REQUESTED)
1336 return cli_arg_help(cli, argc > 1,
1337 "USER", "Username of session to un-throttle", NULL);
1338
1339 if (!config->cluster_iam_master)
1340 {
1341 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1342 return CLI_OK;
1343 }
1344
1345 if (!argc)
1346 {
1347 cli_print(cli, "Specify a user");
1348 return CLI_OK;
1349 }
1350
1351 for (i = 0; i < argc; i++)
1352 {
1353 if (!(s = sessionbyuser(argv[i])))
1354 {
1355 cli_print(cli, "User %s is not connected", argv[i]);
1356 continue;
1357 }
1358
1359 if (!session[s].throttle)
1360 {
1361 cli_print(cli, "User %s not throttled", argv[i]);
1362 continue;
1363 }
1364
1365 cli_print(cli, "Unthrottling user %s", argv[i]);
1366 cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
1367 }
1368
1369 return CLI_OK;
1370 }
1371
1372 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1373 {
1374 int i;
1375
1376 if (CLI_HELP_REQUESTED)
1377 return cli_arg_help(cli, 1,
1378 "all", "Enable debugging for all except \"data\"",
1379 "critical", "", // FIXME: add descriptions
1380 "error", "",
1381 "warning", "",
1382 "info", "",
1383 "calls", "",
1384 "data", "",
1385 NULL);
1386
1387 if (!argc)
1388 {
1389 char *p = (char *) &debug_flags;
1390 for (i = 0; i < sizeof(debug_flags); i++)
1391 {
1392 if (p[i])
1393 {
1394 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1395 (debug_flags.critical) ? " critical" : "",
1396 (debug_flags.error) ? " error" : "",
1397 (debug_flags.warning) ? " warning" : "",
1398 (debug_flags.info) ? " info" : "",
1399 (debug_flags.calls) ? " calls" : "",
1400 (debug_flags.data) ? " data" : "");
1401
1402 return CLI_OK;
1403 }
1404 }
1405
1406 cli_print(cli, "Debugging off");
1407 return CLI_OK;
1408 }
1409
1410 for (i = 0; i < argc; i++)
1411 {
1412 int len = strlen(argv[i]);
1413
1414 if (argv[i][0] == 'c' && len < 2)
1415 len = 2; /* distinguish [cr]itical from [ca]lls */
1416
1417 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; }
1418 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; }
1419 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; }
1420 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; }
1421 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; }
1422 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; }
1423 if (!strncasecmp(argv[i], "all", len))
1424 {
1425 memset(&debug_flags, 1, sizeof(debug_flags));
1426 debug_flags.data = 0;
1427 continue;
1428 }
1429
1430 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1431 }
1432
1433 return CLI_OK;
1434 }
1435
1436 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1437 {
1438 int i;
1439
1440 if (CLI_HELP_REQUESTED)
1441 return cli_arg_help(cli, 1,
1442 "all", "Disable all debugging",
1443 "critical", "", // FIXME: add descriptions
1444 "error", "",
1445 "warning", "",
1446 "info", "",
1447 "calls", "",
1448 "data", "",
1449 NULL);
1450
1451 if (!argc)
1452 {
1453 memset(&debug_flags, 0, sizeof(debug_flags));
1454 return CLI_OK;
1455 }
1456
1457 for (i = 0; i < argc; i++)
1458 {
1459 int len = strlen(argv[i]);
1460
1461 if (argv[i][0] == 'c' && len < 2)
1462 len = 2; /* distinguish [cr]itical from [ca]lls */
1463
1464 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; }
1465 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; }
1466 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; }
1467 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; }
1468 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; }
1469 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; }
1470 if (!strncasecmp(argv[i], "all", len))
1471 {
1472 memset(&debug_flags, 0, sizeof(debug_flags));
1473 continue;
1474 }
1475
1476 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1477 }
1478
1479 return CLI_OK;
1480 }
1481
1482 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1483 {
1484 int i, firstfree = 0;
1485
1486 if (CLI_HELP_REQUESTED)
1487 return cli_arg_help(cli, argc > 1,
1488 "PLUGIN", "Name of plugin to load", NULL);
1489
1490 if (argc != 1)
1491 {
1492 cli_print(cli, "Specify a plugin to load");
1493 return CLI_OK;
1494 }
1495
1496 for (i = 0; i < MAXPLUGINS; i++)
1497 {
1498 if (!*config->plugins[i] && !firstfree)
1499 firstfree = i;
1500 if (strcmp(config->plugins[i], argv[0]) == 0)
1501 {
1502 cli_print(cli, "Plugin is already loaded");
1503 return CLI_OK;
1504 }
1505 }
1506
1507 if (firstfree)
1508 {
1509 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1510 config->reload_config = 1;
1511 cli_print(cli, "Loading plugin %s", argv[0]);
1512 }
1513
1514 return CLI_OK;
1515 }
1516
1517 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1518 {
1519 int i;
1520
1521 if (CLI_HELP_REQUESTED)
1522 return cli_arg_help(cli, argc > 1,
1523 "PLUGIN", "Name of plugin to unload", NULL);
1524
1525 if (argc != 1)
1526 {
1527 cli_print(cli, "Specify a plugin to remove");
1528 return CLI_OK;
1529 }
1530
1531 for (i = 0; i < MAXPLUGINS; i++)
1532 {
1533 if (strcmp(config->plugins[i], argv[0]) == 0)
1534 {
1535 config->reload_config = 1;
1536 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1537 return CLI_OK;
1538 }
1539 }
1540
1541 cli_print(cli, "Plugin is not loaded");
1542 return CLI_OK;
1543 }
1544
1545 char *duration(time_t secs)
1546 {
1547 static char *buf = NULL;
1548 int p = 0;
1549
1550 if (!buf) buf = calloc(64, 1);
1551
1552 if (secs >= 86400)
1553 {
1554 int days = secs / 86400;
1555 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1556 secs %= 86400;
1557 }
1558
1559 if (secs >= 3600)
1560 {
1561 int mins = secs / 60;
1562 int hrs = mins / 60;
1563
1564 mins %= 60;
1565 sprintf(buf + p, "%d:%02d", hrs, mins);
1566 }
1567 else if (secs >= 60)
1568 {
1569 int mins = secs / 60;
1570 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1571 }
1572 else
1573 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1574
1575 return buf;
1576 }
1577
1578 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1579 {
1580 FILE *fh;
1581 char buf[100], *p = buf, *loads[3];
1582 int i, num_sessions = 0;
1583
1584 if (CLI_HELP_REQUESTED)
1585 return CLI_HELP_NO_ARGS;
1586
1587 fh = fopen("/proc/loadavg", "r");
1588 fgets(buf, 100, fh);
1589 fclose(fh);
1590
1591 for (i = 0; i < 3; i++)
1592 loads[i] = strdup(strsep(&p, " "));
1593
1594 time(&time_now);
1595 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1596
1597 for (i = 1; i < MAXSESSION; i++)
1598 if (session[i].opened) num_sessions++;
1599
1600 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1601 buf,
1602 duration(time_now - config->start_time),
1603 num_sessions,
1604 loads[0], loads[1], loads[2]
1605 );
1606 for (i = 0; i < 3; i++)
1607 if (loads[i]) free(loads[i]);
1608
1609 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1610
1611 return CLI_OK;
1612 }
1613
1614 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1615 {
1616 int i;
1617
1618 if (CLI_HELP_REQUESTED)
1619 {
1620 switch (argc)
1621 {
1622 case 1:
1623 {
1624 int len = strlen(argv[0])-1;
1625 for (i = 0; config_values[i].key; i++)
1626 if (!len || !strncmp(argv[0], config_values[i].key, len))
1627 cli_print(cli, " %s", config_values[i].key);
1628 }
1629
1630 return CLI_OK;
1631
1632 case 2:
1633 return cli_arg_help(cli, 0,
1634 "VALUE", "Value for variable", NULL);
1635
1636 case 3:
1637 if (!argv[2][1])
1638 return cli_arg_help(cli, 1, NULL);
1639
1640 default:
1641 return CLI_OK;
1642 }
1643 }
1644
1645 if (argc != 2)
1646 {
1647 cli_print(cli, "Specify variable and value");
1648 return CLI_OK;
1649 }
1650
1651 for (i = 0; config_values[i].key; i++)
1652 {
1653 void *value = ((void *)config) + config_values[i].offset;
1654 if (strcmp(config_values[i].key, argv[0]) == 0)
1655 {
1656 // Found a value to set
1657 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1658 switch (config_values[i].type)
1659 {
1660 case STRING:
1661 strncpy((char *)value, argv[1], config_values[i].size - 1);
1662 break;
1663 case INT:
1664 *(int *)value = atoi(argv[1]);
1665 break;
1666 case UNSIGNED_LONG:
1667 *(unsigned long *)value = atol(argv[1]);
1668 break;
1669 case SHORT:
1670 *(short *)value = atoi(argv[1]);
1671 break;
1672 case IP:
1673 *(unsigned *)value = inet_addr(argv[1]);
1674 break;
1675 case MAC:
1676 parsemac(argv[1], (char *)value);
1677 break;
1678 case BOOL:
1679 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1680 *(int *)value = 1;
1681 else
1682 *(int *)value = 0;
1683 break;
1684 default:
1685 cli_print(cli, "Unknown variable type");
1686 break;
1687 }
1688 config->reload_config = 1;
1689 return CLI_OK;
1690 }
1691 }
1692
1693 cli_print(cli, "Unknown variable \"%s\"", argv[0]);
1694 return CLI_OK;
1695 }
1696
1697 int regular_stuff(struct cli_def *cli)
1698 {
1699 int i = debug_rb_tail;
1700 int reprompt = 0;
1701
1702 #ifdef RINGBUFFER
1703 while (i != ringbuffer->tail)
1704 {
1705 int show_message = 0;
1706
1707 if (*ringbuffer->buffer[i].message)
1708 {
1709 // Always show messages if we are doing general debug
1710 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
1711 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
1712 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
1713 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
1714 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
1715 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
1716 }
1717
1718 if (show_message)
1719 {
1720 ipt address = htonl(ringbuffer->buffer[i].address);
1721 char *ipaddr;
1722 struct in_addr addr;
1723
1724 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
1725 ipaddr = inet_ntoa(addr);
1726
1727 cli_print(cli, "\r%s-%s-%u-%u %s",
1728 debug_levels[(int)ringbuffer->buffer[i].level],
1729 ipaddr,
1730 ringbuffer->buffer[i].tunnel,
1731 ringbuffer->buffer[i].session,
1732 ringbuffer->buffer[i].message);
1733
1734 reprompt = 1;
1735 }
1736
1737 if (++i == ringbuffer->tail) break;
1738 if (i == RINGBUFFER_SIZE) i = 0;
1739 }
1740
1741 debug_rb_tail = ringbuffer->tail;
1742 if (reprompt)
1743 cli_reprompt(cli);
1744 #endif
1745 return CLI_OK;
1746 }
1747
1748 // Convert a string in the form of abcd.ef12.3456 into char[6]
1749 void parsemac(char *string, char mac[6])
1750 {
1751 if (sscanf(string, "%02x%02x.%02x%02x.%02x%02x", (unsigned int *)&mac[0], (unsigned int *)&mac[1], (unsigned int *)&mac[2], (unsigned int *)&mac[3], (unsigned int *)&mac[4], (unsigned int *)&mac[5]) == 6)
1752 return;
1753 if (sscanf(string, "%02x%02x:%02x%02x:%02x%02x", (unsigned int *)&mac[0], (unsigned int *)&mac[1], (unsigned int *)&mac[2], (unsigned int *)&mac[3], (unsigned int *)&mac[4], (unsigned int *)&mac[5]) == 6)
1754 return;
1755 memset(mac, 0, 6);
1756 }