0d096cbe02e898ce6cb837280b0b04c2bf26251b
[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.6 2004/06/28 02:43:13 fred_nerk Exp $";
6
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <sys/file.h>
10 #include <sys/stat.h>
11 #include <syslog.h>
12 #include <malloc.h>
13 #include <sched.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <dlfcn.h>
25 #include <libcli.h>
26 #include "l2tpns.h"
27 #include "util.h"
28 #include "cluster.h"
29 #include "tbf.h"
30 #include "ll.h"
31 #ifdef BGP
32 #include "bgp.h"
33 #endif
34
35 extern tunnelt *tunnel;
36 extern sessiont *session;
37 extern radiust *radius;
38 extern ippoolt *ip_address_pool;
39 extern struct Tstats *_statistics;
40 struct cli_def *cli = NULL;
41 int cli_quit = 0;
42 extern int clifd, udpfd, 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_EXEC, NULL);
175 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Load a plugin");
176
177 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
178 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Remove a plugin");
179
180 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_EXEC, "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);
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 if (!argc)
1016 {
1017 cli_print(cli, "Specify a user to drop");
1018 return CLI_OK;
1019 }
1020
1021 for (i = 0; i < argc; i++)
1022 {
1023 if (!(s = sessionbyuser(argv[i])))
1024 {
1025 cli_print(cli, "User %s is not connected", argv[i]);
1026 continue;
1027 }
1028
1029 if (session[s].ip && session[s].opened && !session[s].die)
1030 {
1031 int x;
1032
1033 cli_print(cli, "Dropping user %s", session[s].user);
1034 for (x = 0; x < MAXSESSION; x++)
1035 {
1036 if (!cli_session_kill[x])
1037 {
1038 cli_session_kill[x] = s;
1039 break;
1040 }
1041 }
1042 }
1043 }
1044
1045 return CLI_OK;
1046 }
1047
1048 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1049 {
1050 int i;
1051 tunnelidt tid;
1052
1053 if (CLI_HELP_REQUESTED)
1054 return cli_arg_help(cli, argc > 1,
1055 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1056
1057 if (!config->cluster_iam_master)
1058 {
1059 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1060 return CLI_OK;
1061 }
1062 if (!argc)
1063 {
1064 cli_print(cli, "Specify a tunnel to drop");
1065 return CLI_OK;
1066 }
1067
1068 for (i = 0; i < argc; i++)
1069 {
1070 int x;
1071
1072 if ((tid = atol(argv[i])) <= 0 || (tid >= MAXTUNNEL))
1073 {
1074 cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1075 continue;
1076 }
1077
1078 if (!tunnel[tid].ip)
1079 {
1080 cli_print(cli, "Tunnel %d is not connected", tid);
1081 continue;
1082 }
1083
1084 if (tunnel[tid].die)
1085 {
1086 cli_print(cli, "Tunnel %d is already being shut down", tid);
1087 continue;
1088 }
1089
1090 for (x = 0; x < MAXTUNNEL; x++)
1091 {
1092 if (!cli_tunnel_kill[x])
1093 {
1094 cli_tunnel_kill[x] = tid;
1095 cli_print(cli, "Tunnel %d shut down (%s)", tid, tunnel[tid].hostname);
1096 break;
1097 }
1098 }
1099 }
1100
1101 return CLI_OK;
1102 }
1103
1104 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1105 {
1106 int i;
1107 sessionidt s;
1108
1109 if (CLI_HELP_REQUESTED)
1110 return cli_arg_help(cli, argc > 1,
1111 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1112
1113 if (!config->cluster_iam_master)
1114 {
1115 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1116 return CLI_OK;
1117 }
1118 if (!argc)
1119 {
1120 cli_print(cli, "Specify a session id to drop");
1121 return CLI_OK;
1122 }
1123
1124 for (i = 0; i < argc; i++)
1125 {
1126 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1127 {
1128 cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1129 continue;
1130 }
1131
1132 if (session[s].opened && !session[s].die)
1133 {
1134 int x;
1135 for (x = 0; x < MAXSESSION; x++)
1136 {
1137 if (!cli_session_kill[x])
1138 {
1139 cli_session_kill[x] = s;
1140 break;
1141 }
1142 }
1143 cli_print(cli, "Dropping session %d", s);
1144 }
1145 else
1146 {
1147 cli_print(cli, "Session %d is not active.", s);
1148 }
1149 }
1150
1151 return CLI_OK;
1152 }
1153
1154 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1155 {
1156 ipt ip;
1157 u16 port;
1158 sessionidt s;
1159
1160 if (CLI_HELP_REQUESTED)
1161 {
1162 switch (argc)
1163 {
1164 case 1:
1165 return cli_arg_help(cli, 0,
1166 "USER", "Username of session to snoop", NULL);
1167
1168 case 2:
1169 return cli_arg_help(cli, 0,
1170 "A.B.C.D", "IP address of snoop destination", NULL);
1171
1172 case 3:
1173 return cli_arg_help(cli, 0,
1174 "N", "Port of snoop destination", NULL);
1175
1176 case 4:
1177 if (!argv[3][1])
1178 return cli_arg_help(cli, 1, NULL);
1179
1180 default:
1181 return CLI_OK;
1182 }
1183 }
1184
1185 if (!config->cluster_iam_master)
1186 {
1187 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1188 return CLI_OK;
1189 }
1190
1191 if (argc < 3)
1192 {
1193 cli_print(cli, "Specify username ip port");
1194 return CLI_OK;
1195 }
1196
1197 if (!(s = sessionbyuser(argv[0])))
1198 {
1199 cli_print(cli, "User %s is not connected", argv[0]);
1200 return CLI_OK;
1201 }
1202
1203 ip = inet_addr(argv[1]);
1204 if (!ip || ip == INADDR_NONE)
1205 {
1206 cli_print(cli, "Cannot parse IP \"%s\"", argv[1]);
1207 return CLI_OK;
1208 }
1209
1210 port = atoi(argv[2]);
1211 if (!port)
1212 {
1213 cli_print(cli, "Invalid port %s", argv[2]);
1214 return CLI_OK;
1215 }
1216
1217 session[s].snoop_ip = ip;
1218 session[s].snoop_port = port;
1219
1220 cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(session[s].snoop_ip), session[s].snoop_port);
1221 return CLI_OK;
1222 }
1223
1224 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1225 {
1226 int i;
1227 sessionidt s;
1228
1229 if (CLI_HELP_REQUESTED)
1230 return cli_arg_help(cli, argc > 1,
1231 "USER", "Username of session to un-snoop", NULL);
1232
1233 if (!config->cluster_iam_master)
1234 {
1235 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1236 return CLI_OK;
1237 }
1238
1239 if (!argc)
1240 {
1241 cli_print(cli, "Specify a user");
1242 return CLI_OK;
1243 }
1244
1245 for (i = 0; i < argc; i++)
1246 {
1247 if (!(s = sessionbyuser(argv[i])))
1248 {
1249 cli_print(cli, "User %s is not connected", argv[i]);
1250 continue;
1251 }
1252 session[s].snoop_ip = 0;
1253 session[s].snoop_port = 0;
1254
1255 cli_print(cli, "Not snooping user %s", argv[i]);
1256 }
1257 return CLI_OK;
1258 }
1259
1260 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1261 {
1262 int i;
1263 sessionidt s;
1264
1265 if (CLI_HELP_REQUESTED)
1266 return cli_arg_help(cli, argc > 1,
1267 "USER", "Username of session to throttle", NULL);
1268
1269 if (!config->cluster_iam_master)
1270 {
1271 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1272 return CLI_OK;
1273 }
1274 if (!argc)
1275 {
1276 cli_print(cli, "Specify a user");
1277 return CLI_OK;
1278 }
1279
1280 for (i = 0; i < argc; i++)
1281 {
1282 if (!(s = sessionbyuser(argv[i])))
1283 {
1284 cli_print(cli, "User %s is not connected", argv[i]);
1285 continue;
1286 }
1287 if (!throttle_session(s, config->rl_rate))
1288 cli_print(cli, "Error throttling %s", argv[i]);
1289 else
1290 cli_print(cli, "Throttling user %s", argv[i]);
1291 }
1292 return CLI_OK;
1293 }
1294
1295 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1296 {
1297 int i;
1298 sessionidt s;
1299
1300 if (CLI_HELP_REQUESTED)
1301 return cli_arg_help(cli, argc > 1,
1302 "USER", "Username of session to un-throttle", NULL);
1303
1304 if (!config->cluster_iam_master)
1305 {
1306 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1307 return CLI_OK;
1308 }
1309 if (!argc)
1310 {
1311 cli_print(cli, "Specify a user");
1312 return CLI_OK;
1313 }
1314
1315 for (i = 0; i < argc; i++)
1316 {
1317 if (!(s = sessionbyuser(argv[i])))
1318 {
1319 cli_print(cli, "User %s is not connected", argv[i]);
1320 continue;
1321 }
1322 throttle_session(s, 0);
1323
1324 cli_print(cli, "Unthrottling user %s", argv[i]);
1325 }
1326
1327 return CLI_OK;
1328 }
1329
1330 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1331 {
1332 int i;
1333
1334 if (CLI_HELP_REQUESTED)
1335 return cli_arg_help(cli, 1,
1336 "all", "Enable debugging for all except \"data\"",
1337 "critical", "", // FIXME: add descriptions
1338 "error", "",
1339 "warning", "",
1340 "info", "",
1341 "calls", "",
1342 "data", "",
1343 NULL);
1344
1345 if (!argc)
1346 {
1347 char *p = (char *) &debug_flags;
1348 for (i = 0; i < sizeof(debug_flags); i++)
1349 {
1350 if (p[i])
1351 {
1352 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1353 (debug_flags.critical) ? " critical" : "",
1354 (debug_flags.error) ? " error" : "",
1355 (debug_flags.warning) ? " warning" : "",
1356 (debug_flags.info) ? " info" : "",
1357 (debug_flags.calls) ? " calls" : "",
1358 (debug_flags.data) ? " data" : "");
1359
1360 return CLI_OK;
1361 }
1362 }
1363
1364 cli_print(cli, "Debugging off");
1365 return CLI_OK;
1366 }
1367
1368 for (i = 0; i < argc; i++)
1369 {
1370 int len = strlen(argv[i]);
1371
1372 if (argv[i][0] == 'c' && len < 2)
1373 len = 2; /* distinguish [cr]itical from [ca]lls */
1374
1375 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; }
1376 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; }
1377 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; }
1378 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; }
1379 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; }
1380 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; }
1381 if (!strncasecmp(argv[i], "all", len))
1382 {
1383 memset(&debug_flags, 1, sizeof(debug_flags));
1384 debug_flags.data = 0;
1385 continue;
1386 }
1387
1388 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1389 }
1390
1391 return CLI_OK;
1392 }
1393
1394 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1395 {
1396 int i;
1397
1398 if (CLI_HELP_REQUESTED)
1399 return cli_arg_help(cli, 1,
1400 "all", "Disable all debugging",
1401 "critical", "", // FIXME: add descriptions
1402 "error", "",
1403 "warning", "",
1404 "info", "",
1405 "calls", "",
1406 "data", "",
1407 NULL);
1408
1409 if (!argc)
1410 {
1411 memset(&debug_flags, 0, sizeof(debug_flags));
1412 return CLI_OK;
1413 }
1414
1415 for (i = 0; i < argc; i++)
1416 {
1417 int len = strlen(argv[i]);
1418
1419 if (argv[i][0] == 'c' && len < 2)
1420 len = 2; /* distinguish [cr]itical from [ca]lls */
1421
1422 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; }
1423 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; }
1424 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; }
1425 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; }
1426 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; }
1427 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; }
1428 if (!strncasecmp(argv[i], "all", len))
1429 {
1430 memset(&debug_flags, 0, sizeof(debug_flags));
1431 continue;
1432 }
1433
1434 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1435 }
1436
1437 return CLI_OK;
1438 }
1439
1440 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1441 {
1442 int i, firstfree = 0;
1443
1444 if (CLI_HELP_REQUESTED)
1445 return cli_arg_help(cli, argc > 1,
1446 "PLUGIN", "Name of plugin to load", NULL);
1447
1448 if (argc != 1)
1449 {
1450 cli_print(cli, "Specify a plugin to load");
1451 return CLI_OK;
1452 }
1453
1454 for (i = 0; i < MAXPLUGINS; i++)
1455 {
1456 if (!*config->plugins[i] && !firstfree)
1457 firstfree = i;
1458 if (strcmp(config->plugins[i], argv[0]) == 0)
1459 {
1460 cli_print(cli, "Plugin is already loaded");
1461 return CLI_OK;
1462 }
1463 }
1464
1465 if (firstfree)
1466 {
1467 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1468 config->reload_config = 1;
1469 cli_print(cli, "Loading plugin %s", argv[0]);
1470 }
1471
1472 return CLI_OK;
1473 }
1474
1475 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1476 {
1477 int i;
1478
1479 if (CLI_HELP_REQUESTED)
1480 return cli_arg_help(cli, argc > 1,
1481 "PLUGIN", "Name of plugin to unload", NULL);
1482
1483 if (argc != 1)
1484 {
1485 cli_print(cli, "Specify a plugin to remove");
1486 return CLI_OK;
1487 }
1488
1489 for (i = 0; i < MAXPLUGINS; i++)
1490 {
1491 if (strcmp(config->plugins[i], argv[0]) == 0)
1492 {
1493 config->reload_config = 1;
1494 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1495 return CLI_OK;
1496 }
1497 }
1498
1499 cli_print(cli, "Plugin is not loaded");
1500 return CLI_OK;
1501 }
1502
1503 char *duration(time_t secs)
1504 {
1505 static char *buf = NULL;
1506 int p = 0;
1507
1508 if (!buf) buf = calloc(64, 1);
1509
1510 if (secs >= 86400)
1511 {
1512 int days = secs / 86400;
1513 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1514 secs %= 86400;
1515 }
1516
1517 if (secs >= 3600)
1518 {
1519 int mins = secs / 60;
1520 int hrs = mins / 60;
1521
1522 mins %= 60;
1523 sprintf(buf + p, "%d:%02d", hrs, mins);
1524 }
1525 else if (secs >= 60)
1526 {
1527 int mins = secs / 60;
1528 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1529 }
1530 else
1531 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1532
1533 return buf;
1534 }
1535
1536 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1537 {
1538 FILE *fh;
1539 char buf[100], *p = buf, *loads[3];
1540 int i, num_sessions = 0;
1541 time_t time_now;
1542
1543 if (CLI_HELP_REQUESTED)
1544 return CLI_HELP_NO_ARGS;
1545
1546 fh = fopen("/proc/loadavg", "r");
1547 fgets(buf, 100, fh);
1548 fclose(fh);
1549
1550 for (i = 0; i < 3; i++)
1551 loads[i] = strdup(strsep(&p, " "));
1552
1553 time(&time_now);
1554 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1555
1556 for (i = 1; i < MAXSESSION; i++)
1557 if (session[i].opened) num_sessions++;
1558
1559 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1560 buf,
1561 duration(time_now - config->start_time),
1562 num_sessions,
1563 loads[0], loads[1], loads[2]
1564 );
1565 for (i = 0; i < 3; i++)
1566 if (loads[i]) free(loads[i]);
1567
1568 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1569
1570 return CLI_OK;
1571 }
1572
1573 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1574 {
1575 int i;
1576
1577 if (CLI_HELP_REQUESTED)
1578 {
1579 switch (argc)
1580 {
1581 case 1:
1582 {
1583 int len = strlen(argv[0])-1;
1584 for (i = 0; config_values[i].key; i++)
1585 if (!len || !strncmp(argv[0], config_values[i].key, len))
1586 cli_print(cli, " %s", config_values[i].key);
1587 }
1588
1589 return CLI_OK;
1590
1591 case 2:
1592 return cli_arg_help(cli, 0,
1593 "VALUE", "Value for variable", NULL);
1594
1595 case 3:
1596 if (!argv[2][1])
1597 return cli_arg_help(cli, 1, NULL);
1598
1599 default:
1600 return CLI_OK;
1601 }
1602 }
1603
1604 if (argc != 2)
1605 {
1606 cli_print(cli, "Specify variable and value");
1607 return CLI_OK;
1608 }
1609
1610 for (i = 0; config_values[i].key; i++)
1611 {
1612 void *value = ((void *)config) + config_values[i].offset;
1613 if (strcmp(config_values[i].key, argv[0]) == 0)
1614 {
1615 // Found a value to set
1616 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1617 switch (config_values[i].type)
1618 {
1619 case STRING:
1620 strncpy((char *)value, argv[1], config_values[i].size - 1);
1621 break;
1622 case INT:
1623 *(int *)value = atoi(argv[1]);
1624 break;
1625 case UNSIGNED_LONG:
1626 *(unsigned long *)value = atol(argv[1]);
1627 break;
1628 case SHORT:
1629 *(short *)value = atoi(argv[1]);
1630 break;
1631 case IP:
1632 *(unsigned *)value = inet_addr(argv[1]);
1633 break;
1634 case BOOL:
1635 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1636 *(int *)value = 1;
1637 else
1638 *(int *)value = 0;
1639 break;
1640 default:
1641 cli_print(cli, "Unknown variable type");
1642 break;
1643 }
1644 config->reload_config = 1;
1645 return CLI_OK;
1646 }
1647 }
1648
1649 cli_print(cli, "Unknown variable \"%s\"", argv[0]);
1650 return CLI_OK;
1651 }
1652
1653 int regular_stuff(struct cli_def *cli)
1654 {
1655 int i = debug_rb_tail;
1656 int reprompt = 0;
1657
1658 #ifdef RINGBUFFER
1659 while (i != ringbuffer->tail)
1660 {
1661 int show_message = 0;
1662
1663 if (*ringbuffer->buffer[i].message)
1664 {
1665 // Always show messages if we are doing general debug
1666 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
1667 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
1668 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
1669 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
1670 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
1671 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
1672 }
1673
1674 if (show_message)
1675 {
1676 ipt address = htonl(ringbuffer->buffer[i].address);
1677 char *ipaddr;
1678 struct in_addr addr;
1679
1680 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
1681 ipaddr = inet_ntoa(addr);
1682
1683 cli_print(cli, "\r%s-%s-%u-%u %s",
1684 debug_levels[(int)ringbuffer->buffer[i].level],
1685 ipaddr,
1686 ringbuffer->buffer[i].tunnel,
1687 ringbuffer->buffer[i].session,
1688 ringbuffer->buffer[i].message);
1689
1690 reprompt = 1;
1691 }
1692
1693 if (++i == ringbuffer->tail) break;
1694 if (i == RINGBUFFER_SIZE) i = 0;
1695 }
1696
1697 debug_rb_tail = ringbuffer->tail;
1698 if (reprompt)
1699 cli_reprompt(cli);
1700 #endif
1701 return CLI_OK;
1702 }