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