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