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