values not set yet
[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.22 2004/11/04 23:41:54 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, "\tUser:\t\t%s", session[s].user[0] ? session[s].user : "none");
353 cli_print(cli, "\tCalling Num:\t%s", session[s].calling);
354 cli_print(cli, "\tCalled Num:\t%s", session[s].called);
355 cli_print(cli, "\tTunnel ID:\t%d", session[s].tunnel);
356 cli_print(cli, "\tIP address:\t%s", inet_toa(htonl(session[s].ip)));
357 cli_print(cli, "\tUnique SID:\t%lu", session[s].unique_id);
358 cli_print(cli, "\tIdle time:\t%u seconds", abs(time_now - session[s].last_packet));
359 cli_print(cli, "\tNext Recv:\t%u", session[s].nr);
360 cli_print(cli, "\tNext Send:\t%u", session[s].ns);
361 cli_print(cli, "\tBytes In/Out:\t%lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin);
362 cli_print(cli, "\tPkts In/Out:\t%lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin);
363 cli_print(cli, "\tMRU:\t\t%d", session[s].mru);
364 cli_print(cli, "\tRadius Session:\t%u", session[s].radius);
365 cli_print(cli, "\tRx Speed:\t%lu", session[s].rx_connect_speed);
366 cli_print(cli, "\tTx Speed:\t%lu", session[s].tx_connect_speed);
367 if (session[s].snoop_ip && session[s].snoop_port)
368 cli_print(cli, "\tIntercepted:\t%s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port);
369 else
370 cli_print(cli, "\tIntercepted:\tno");
371
372 cli_print(cli, "\tWalled Garden:\t%s", session[s].walled_garden ? "YES" : "no");
373 {
374 int t = (session[s].throttle_in || session[s].throttle_out);
375 cli_print(cli, "\tThrottled:\t%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, "\t\t\t%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, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
392 b_in,
393 (filter_list[b_in].next ? "*" : " "),
394 (b_in < 100 ? "\t" : ""),
395 filter_list[b_in].rate * 8,
396 filter_list[b_in].credit,
397 filter_list[b_in].queued,
398 filter_list[b_in].b_queued,
399 filter_list[b_in].p_queued,
400 filter_list[b_in].b_sent,
401 filter_list[b_in].p_sent,
402 filter_list[b_in].p_dropped,
403 filter_list[b_in].p_delayed);
404
405 if (b_out)
406 cli_print(cli, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
407 b_out,
408 (filter_list[b_out].next ? "*" : " "),
409 (b_out < 100 ? "\t" : ""),
410 filter_list[b_out].rate * 8,
411 filter_list[b_out].credit,
412 filter_list[b_out].queued,
413 filter_list[b_out].b_queued,
414 filter_list[b_out].p_queued,
415 filter_list[b_out].b_sent,
416 filter_list[b_out].p_sent,
417 filter_list[b_out].p_dropped,
418 filter_list[b_out].p_delayed);
419
420 }
421 return CLI_OK;
422 }
423
424 // Show Summary
425 cli_print(cli, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
426 "SID",
427 "TID",
428 "Username",
429 "IP",
430 "I",
431 "T",
432 "G",
433 "opened",
434 "downloaded",
435 "uploaded",
436 "idle",
437 "LAC",
438 "CLI");
439
440 for (i = 1; i < MAXSESSION; i++)
441 {
442 char *userip, *tunnelip;
443 if (!session[i].opened) continue;
444 userip = strdup(inet_toa(htonl(session[i].ip)));
445 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
446 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
447 i,
448 session[i].tunnel,
449 session[i].user[0] ? session[i].user : "*",
450 userip,
451 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
452 (session[i].throttle_in || session[i].throttle_out) ? "Y" : "N",
453 (session[i].walled_garden) ? "Y" : "N",
454 abs(time_now - (unsigned long)session[i].opened),
455 (unsigned long)session[i].total_cout,
456 (unsigned long)session[i].total_cin,
457 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
458 tunnelip,
459 session[i].calling[0] ? session[i].calling : "*");
460 if (userip) free(userip);
461 if (tunnelip) free(tunnelip);
462 }
463 return CLI_OK;
464 }
465
466 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
467 {
468 int i, x, show_all = 0;
469 char *states[] = {
470 "Free",
471 "Open",
472 "Closing",
473 "Opening",
474 };
475
476 if (CLI_HELP_REQUESTED)
477 {
478 if (argc > 1)
479 return cli_arg_help(cli, 1,
480 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
481 NULL);
482
483 return cli_arg_help(cli, 1,
484 "all", "Show all tunnels, including unused",
485 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
486 NULL);
487 }
488
489 time(&time_now);
490 if (argc > 0)
491 {
492 if (strcmp(argv[0], "all") == 0)
493 {
494 show_all = 1;
495 }
496 else
497 {
498 // Show individual tunnel
499 for (i = 0; i < argc; i++)
500 {
501 char s[65535] = {0};
502 unsigned int t;
503 t = atoi(argv[i]);
504 if (t <= 0 || t >= MAXTUNNEL)
505 {
506 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
507 continue;
508 }
509 cli_print(cli, "\r\nTunnel %d:", t);
510 cli_print(cli, "\tState:\t\t%s", states[tunnel[t].state]);
511 cli_print(cli, "\tHostname:\t%s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
512 cli_print(cli, "\tRemote IP:\t%s", inet_toa(htonl(tunnel[t].ip)));
513 cli_print(cli, "\tRemote Port:\t%d", tunnel[t].port);
514 cli_print(cli, "\tRx Window:\t%u", tunnel[t].window);
515 cli_print(cli, "\tNext Recv:\t%u", tunnel[t].nr);
516 cli_print(cli, "\tNext Send:\t%u", tunnel[t].ns);
517 cli_print(cli, "\tQueue Len:\t%u", tunnel[t].controlc);
518 cli_print(cli, "\tLast Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
519
520 for (x = 0; x < MAXSESSION; x++)
521 if (session[x].tunnel == t && session[x].opened && !session[x].die)
522 sprintf(s, "%s%u ", s, x);
523
524 cli_print(cli, "\tSessions:\t%s", s);
525 }
526 return CLI_OK;
527 }
528 }
529
530 // Show tunnel summary
531 cli_print(cli, "%4s %20s %20s %6s %s",
532 "TID",
533 "Hostname",
534 "IP",
535 "State",
536 "Sessions");
537
538 for (i = 1; i < MAXTUNNEL; i++)
539 {
540 int sessions = 0;
541 if (!show_all && (!tunnel[i].ip || tunnel[i].die || !tunnel[i].hostname[0])) continue;
542
543 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
544 cli_print(cli, "%4d %20s %20s %6s %6d",
545 i,
546 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
547 inet_toa(htonl(tunnel[i].ip)),
548 states[tunnel[i].state],
549 sessions);
550 }
551
552 return CLI_OK;
553 }
554
555 int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
556 {
557 char sid[32][8];
558 char *sargv[32];
559 int sargc = 0;
560 int i;
561
562 if (CLI_HELP_REQUESTED)
563 return cli_arg_help(cli, 1,
564 "USER", "Show details for specific username",
565 NULL);
566
567 for (i = 0; i < MAXSESSION; i++)
568 {
569 if (!session[i].opened) continue;
570 if (!session[i].user[0]) continue;
571 if (argc > 0)
572 {
573 int j;
574 for (j = 0; j < argc && sargc < 32; j++)
575 {
576 if (strcmp(argv[j], session[i].user) == 0)
577 {
578 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
579 sargv[sargc] = sid[sargc];
580 sargc++;
581 }
582 }
583
584 continue;
585 }
586
587 cli_print(cli, "%s", session[i].user);
588 }
589
590 if (sargc > 0)
591 return cmd_show_session(cli, "users", sargv, sargc);
592
593 return CLI_OK;
594 }
595
596 int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
597 {
598 if (CLI_HELP_REQUESTED)
599 return CLI_HELP_NO_ARGS;
600
601 cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
602 cli_print(cli, "%-10s %8lu %8lu %8lu", "RX",
603 GET_STAT(tun_rx_bytes),
604 GET_STAT(tun_rx_packets),
605 GET_STAT(tun_rx_errors));
606 cli_print(cli, "%-10s %8lu %8lu %8lu", "TX",
607 GET_STAT(tun_tx_bytes),
608 GET_STAT(tun_tx_packets),
609 GET_STAT(tun_tx_errors));
610 cli_print(cli, "");
611
612 cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
613 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX",
614 GET_STAT(tunnel_rx_bytes),
615 GET_STAT(tunnel_rx_packets),
616 GET_STAT(tunnel_rx_errors),
617 0L);
618 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX",
619 GET_STAT(tunnel_tx_bytes),
620 GET_STAT(tunnel_tx_packets),
621 GET_STAT(tunnel_tx_errors),
622 GET_STAT(tunnel_retries));
623 cli_print(cli, "");
624
625 cli_print(cli, "%-30s%-10s", "Counter", "Value");
626 cli_print(cli, "-----------------------------------------");
627 cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries));
628 cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent));
629 cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped));
630 cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created));
631 cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created));
632 cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout));
633 cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout));
634 cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout));
635 cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow));
636 cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow));
637 cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow));
638 cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated));
639 cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed));
640 cli_print(cli, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded));
641 cli_print(cli, "%-30s%lu", "recv_forward", GET_STAT(recv_forward));
642
643
644 #ifdef STATISTICS
645 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
646 cli_print(cli, "-----------------------------------------");
647 cli_print(cli, "%-30s%lu", "call_processtun", GET_STAT(call_processtun));
648 cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout));
649 cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp));
650 cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap));
651 cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap));
652 cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp));
653 cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp));
654 cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin));
655 cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp));
656 cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad));
657 cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp));
658 cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp));
659 cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap));
660 cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip));
661 cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
662 cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend));
663 cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill));
664 cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
665 cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill));
666 cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
667 cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup));
668 cli_print(cli, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address));
669 cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address));
670 cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
671 cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend));
672 cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry));
673 #endif
674 return CLI_OK;
675 }
676
677 int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
678 {
679 int tag = 0;
680 int file = 0;
681 int i = 0;
682
683 if (CLI_HELP_REQUESTED)
684 return cli_arg_help(cli, 1,
685 "tag", "Include CVS release tag",
686 "file", "Include file versions",
687 NULL);
688
689 for (i = 0; i < argc; i++)
690 if (!strcmp(argv[i], "tag"))
691 tag++;
692 else if (!strcmp(argv[i], "file"))
693 file++;
694
695 cli_print(cli, "L2TPNS %s", VERSION);
696 if (tag)
697 {
698 char const *p = strchr(cvs_name, ':');
699 char const *e;
700 if (p)
701 {
702 p++;
703 while (isspace(*p))
704 p++;
705 }
706
707 if (!p || *p == '$')
708 p = "HEAD";
709
710 e = strpbrk(p, " \t$");
711 cli_print(cli, "Tag: %.*s", e ? e - p + 1 : strlen(p), p);
712 }
713
714 if (file)
715 {
716 extern linked_list *loaded_plugins;
717 void *p;
718
719 cli_print(cli, "Files:");
720 cli_print(cli, " %s", cvs_id_arp);
721 #ifdef BGP
722 cli_print(cli, " %s", cvs_id_bgp);
723 #endif /* BGP */
724 cli_print(cli, " %s", cvs_id_cli);
725 cli_print(cli, " %s", cvs_id_cluster);
726 cli_print(cli, " %s", cvs_id_constants);
727 cli_print(cli, " %s", cvs_id_control);
728 cli_print(cli, " %s", cvs_id_icmp);
729 cli_print(cli, " %s", cvs_id_l2tpns);
730 cli_print(cli, " %s", cvs_id_ll);
731 cli_print(cli, " %s", cvs_id_md5);
732 cli_print(cli, " %s", cvs_id_ppp);
733 cli_print(cli, " %s", cvs_id_radius);
734 cli_print(cli, " %s", cvs_id_tbf);
735 cli_print(cli, " %s", cvs_id_util);
736
737 ll_reset(loaded_plugins);
738 while ((p = ll_next(loaded_plugins)))
739 {
740 char const **id = dlsym(p, "cvs_id");
741 if (id)
742 cli_print(cli, " %s", *id);
743 }
744 }
745
746 return CLI_OK;
747 }
748
749 int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
750 {
751 int i;
752 int used = 0, free = 0, show_all = 0;
753
754 if (!config->cluster_iam_master)
755 {
756 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
757 return CLI_OK;
758 }
759
760 if (CLI_HELP_REQUESTED)
761 {
762 if (argc > 1)
763 return cli_arg_help(cli, 1, NULL);
764
765 return cli_arg_help(cli, 1,
766 "all", "Show all pool addresses, including unused",
767 NULL);
768 }
769
770 if (argc > 0 && strcmp(argv[0], "all") == 0)
771 show_all = 1;
772
773 time(&time_now);
774 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
775 for (i = 0; i < MAXIPPOOL; i++)
776 {
777 if (!ip_address_pool[i].address) continue;
778 if (ip_address_pool[i].assigned)
779 {
780 cli_print(cli, "%-15s\tY %8d %s",
781 inet_toa(htonl(ip_address_pool[i].address)), ip_address_pool[i].session, session[ip_address_pool[i].session].user);
782
783 used++;
784 }
785 else
786 {
787 if (ip_address_pool[i].last)
788 cli_print(cli, "%-15s\tN %8s [%s] %ds",
789 inet_toa(htonl(ip_address_pool[i].address)), "",
790 ip_address_pool[i].user, time_now - ip_address_pool[i].last);
791 else if (show_all)
792 cli_print(cli, "%-15s\tN", inet_toa(htonl(ip_address_pool[i].address)));
793
794 free++;
795 }
796 }
797
798 if (!show_all)
799 cli_print(cli, "(Not displaying unused addresses)");
800
801 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
802 return CLI_OK;
803 }
804
805 void print_save_config(struct cli_def *cli, char *string)
806 {
807 if (save_config_fh)
808 fprintf(save_config_fh, "%s\n", string);
809 }
810
811 int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
812 {
813 if (CLI_HELP_REQUESTED)
814 return CLI_HELP_NO_ARGS;
815
816 if ((save_config_fh = fopen(config->config_file, "w")))
817 {
818 cli_print(cli, "Writing configuration");
819 cli_print_callback(cli, print_save_config);
820 cmd_show_run(cli, command, argv, argc);
821 cli_print_callback(cli, NULL);
822 fclose(save_config_fh);
823 }
824 else
825 {
826 cli_print(cli, "Error writing configuration: %s", strerror(errno));
827 }
828 return CLI_OK;
829 }
830
831 int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
832 {
833 int i;
834
835 if (CLI_HELP_REQUESTED)
836 return CLI_HELP_NO_ARGS;
837
838 cli_print(cli, "# Current configuration:");
839
840 for (i = 0; config_values[i].key; i++)
841 {
842 void *value = ((void *)config) + config_values[i].offset;
843 if (config_values[i].type == STRING)
844 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value);
845 else if (config_values[i].type == IP)
846 cli_print(cli, "set %s %s", config_values[i].key, inet_toa(*(unsigned *)value));
847 else if (config_values[i].type == SHORT)
848 cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value);
849 else if (config_values[i].type == BOOL)
850 cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no");
851 else if (config_values[i].type == INT)
852 cli_print(cli, "set %s %d", config_values[i].key, *(int *)value);
853 else if (config_values[i].type == UNSIGNED_LONG)
854 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value);
855 else if (config_values[i].type == MAC)
856 cli_print(cli, "set %s %02x%02x.%02x%02x.%02x%02x", config_values[i].key,
857 *(unsigned short *)(value + 0),
858 *(unsigned short *)(value + 1),
859 *(unsigned short *)(value + 2),
860 *(unsigned short *)(value + 3),
861 *(unsigned short *)(value + 4),
862 *(unsigned short *)(value + 5));
863 }
864
865 cli_print(cli, "# Plugins");
866 for (i = 0; i < MAXPLUGINS; i++)
867 {
868 if (*config->plugins[i])
869 {
870 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
871 }
872 }
873
874 cli_print(cli, "# end");
875 return CLI_OK;
876 }
877
878 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
879 {
880 int i, free = 0, used = 0, show_all = 0;
881 char *states[] = {
882 "NULL",
883 "CHAP",
884 "AUTH",
885 "IPCP",
886 "START",
887 "STOP",
888 "WAIT",
889 };
890
891 if (CLI_HELP_REQUESTED)
892 {
893 if (argc > 1)
894 return cli_arg_help(cli, 1, NULL);
895
896 return cli_arg_help(cli, 1,
897 "all", "Show all RADIUS sessions, including unused",
898 NULL);
899 }
900
901 cli_print(cli, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
902
903 time(&time_now);
904
905 if (argc > 0 && strcmp(argv[0], "all") == 0)
906 show_all = 1;
907
908 for (i = 1; i < MAXRADIUS; i++)
909 {
910 if (radius[i].state == RADIUSNULL)
911 free++;
912 else
913 used++;
914
915 if (!show_all && radius[i].state == RADIUSNULL) continue;
916
917 cli_print(cli, "%6d%6d%5d%6s%9d%9u%4d",
918 i,
919 i >> RADIUS_SHIFT,
920 i & RADIUS_MASK,
921 states[radius[i].state],
922 radius[i].session,
923 radius[i].retry,
924 radius[i].try);
925 }
926
927 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
928
929 return CLI_OK;
930 }
931
932 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
933 {
934 int i;
935
936 if (CLI_HELP_REQUESTED)
937 return CLI_HELP_NO_ARGS;
938
939 cli_print(cli, "Plugins currently loaded:");
940 for (i = 0; i < MAXPLUGINS; i++)
941 if (*config->plugins[i])
942 cli_print(cli, " %s", config->plugins[i]);
943
944 return CLI_OK;
945 }
946
947 int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc)
948 {
949 int i;
950
951 if (CLI_HELP_REQUESTED)
952 return CLI_HELP_NO_ARGS;
953
954 cli_print(cli, "%5s %4s %-32s %7s %6s %6s %6s",
955 "SID",
956 "TID",
957 "Username",
958 "Rate In",
959 "Out",
960 "TBFI",
961 "TBFO");
962
963 for (i = 0; i < MAXSESSION; i++)
964 {
965 if (session[i].throttle_in || session[i].throttle_out)
966 cli_print(cli, "%5d %4d %-32s %6d %6d %6d %6d",
967 i,
968 session[i].tunnel,
969 session[i].user,
970 session[i].throttle_in,
971 session[i].throttle_out,
972 session[i].tbf_in,
973 session[i].tbf_out);
974 }
975
976 return CLI_OK;
977 }
978
979 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
980 {
981 if (CLI_HELP_REQUESTED)
982 return CLI_HELP_NO_ARGS;
983
984 cli_print(cli, " _\n"
985 "//\\\n"
986 "V \\\n"
987 " \\ \\_\n"
988 " \\,'.`-.\n"
989 " |\\ `. `.\n"
990 " ( \\ `. `-. _,.-:\\\n"
991 " \\ \\ `. `-._ __..--' ,-';/\n"
992 " \\ `. `-. `-..___..---' _.--' ,'/\n"
993 " `. `. `-._ __..--' ,' /\n"
994 " `. `-_ ``--..'' _.-' ,'\n"
995 " `-_ `-.___ __,--' ,'\n"
996 " `-.__ `----\"\"\" __.-'\n"
997 "hh `--..____..--'");
998
999 return CLI_OK;
1000 }
1001
1002 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
1003 {
1004 if (CLI_HELP_REQUESTED)
1005 return CLI_HELP_NO_ARGS;
1006
1007 cli_print(cli, "Counters cleared");
1008 SET_STAT(last_reset, time(NULL));
1009 return CLI_OK;
1010 }
1011
1012 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
1013 {
1014 int i;
1015 sessionidt s;
1016
1017 if (CLI_HELP_REQUESTED)
1018 return cli_arg_help(cli, argc > 1,
1019 "USER", "Username of session to drop", NULL);
1020
1021 if (!config->cluster_iam_master)
1022 {
1023 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1024 return CLI_OK;
1025 }
1026
1027 if (!argc)
1028 {
1029 cli_print(cli, "Specify a user to drop");
1030 return CLI_OK;
1031 }
1032
1033 for (i = 0; i < argc; i++)
1034 {
1035 if (!(s = sessionbyuser(argv[i])))
1036 {
1037 cli_print(cli, "User %s is not connected", argv[i]);
1038 continue;
1039 }
1040
1041 if (session[s].ip && session[s].opened && !session[s].die)
1042 {
1043 cli_print(cli, "Dropping user %s", session[s].user);
1044 cli_session_actions[s].action |= CLI_SESS_KILL;
1045 }
1046 }
1047
1048 return CLI_OK;
1049 }
1050
1051 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1052 {
1053 int i;
1054 tunnelidt t;
1055
1056 if (CLI_HELP_REQUESTED)
1057 return cli_arg_help(cli, argc > 1,
1058 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1059
1060 if (!config->cluster_iam_master)
1061 {
1062 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1063 return CLI_OK;
1064 }
1065
1066 if (!argc)
1067 {
1068 cli_print(cli, "Specify a tunnel to drop");
1069 return CLI_OK;
1070 }
1071
1072 for (i = 0; i < argc; i++)
1073 {
1074 if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL))
1075 {
1076 cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1077 continue;
1078 }
1079
1080 if (!tunnel[t].ip)
1081 {
1082 cli_print(cli, "Tunnel %d is not connected", t);
1083 continue;
1084 }
1085
1086 if (tunnel[t].die)
1087 {
1088 cli_print(cli, "Tunnel %d is already being shut down", t);
1089 continue;
1090 }
1091
1092 cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname);
1093 cli_tunnel_actions[t].action |= CLI_TUN_KILL;
1094 }
1095
1096 return CLI_OK;
1097 }
1098
1099 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1100 {
1101 int i;
1102 sessionidt s;
1103
1104 if (CLI_HELP_REQUESTED)
1105 return cli_arg_help(cli, argc > 1,
1106 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1107
1108 if (!config->cluster_iam_master)
1109 {
1110 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1111 return CLI_OK;
1112 }
1113
1114 if (!argc)
1115 {
1116 cli_print(cli, "Specify a session id to drop");
1117 return CLI_OK;
1118 }
1119
1120 for (i = 0; i < argc; i++)
1121 {
1122 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1123 {
1124 cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1125 continue;
1126 }
1127
1128 if (session[s].ip && session[s].opened && !session[s].die)
1129 {
1130 cli_print(cli, "Dropping session %d", s);
1131 cli_session_actions[s].action |= CLI_SESS_KILL;
1132 }
1133 else
1134 {
1135 cli_print(cli, "Session %d is not active.", s);
1136 }
1137 }
1138
1139 return CLI_OK;
1140 }
1141
1142 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1143 {
1144 ipt ip;
1145 u16 port;
1146 sessionidt s;
1147
1148 if (CLI_HELP_REQUESTED)
1149 {
1150 switch (argc)
1151 {
1152 case 1:
1153 return cli_arg_help(cli, 0,
1154 "USER", "Username of session to snoop", NULL);
1155
1156 case 2:
1157 return cli_arg_help(cli, 0,
1158 "A.B.C.D", "IP address of snoop destination", NULL);
1159
1160 case 3:
1161 return cli_arg_help(cli, 0,
1162 "N", "Port of snoop destination", NULL);
1163
1164 case 4:
1165 if (!argv[3][1])
1166 return cli_arg_help(cli, 1, NULL);
1167
1168 default:
1169 return CLI_OK;
1170 }
1171 }
1172
1173 if (!config->cluster_iam_master)
1174 {
1175 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1176 return CLI_OK;
1177 }
1178
1179 if (argc < 3)
1180 {
1181 cli_print(cli, "Specify username, ip and port");
1182 return CLI_OK;
1183 }
1184
1185 if (!(s = sessionbyuser(argv[0])))
1186 {
1187 cli_print(cli, "User %s is not connected", argv[0]);
1188 return CLI_OK;
1189 }
1190
1191 ip = inet_addr(argv[1]);
1192 if (!ip || ip == INADDR_NONE)
1193 {
1194 cli_print(cli, "Cannot parse IP \"%s\"", argv[1]);
1195 return CLI_OK;
1196 }
1197
1198 port = atoi(argv[2]);
1199 if (!port)
1200 {
1201 cli_print(cli, "Invalid port %s", argv[2]);
1202 return CLI_OK;
1203 }
1204
1205 cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(ip), port);
1206 cli_session_actions[s].snoop_ip = ip;
1207 cli_session_actions[s].snoop_port = port;
1208 cli_session_actions[s].action |= CLI_SESS_SNOOP;
1209
1210 return CLI_OK;
1211 }
1212
1213 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1214 {
1215 int i;
1216 sessionidt s;
1217
1218 if (CLI_HELP_REQUESTED)
1219 return cli_arg_help(cli, argc > 1,
1220 "USER", "Username of session to unsnoop", NULL);
1221
1222 if (!config->cluster_iam_master)
1223 {
1224 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1225 return CLI_OK;
1226 }
1227
1228 if (!argc)
1229 {
1230 cli_print(cli, "Specify a user to unsnoop");
1231 return CLI_OK;
1232 }
1233
1234 for (i = 0; i < argc; i++)
1235 {
1236 if (!(s = sessionbyuser(argv[i])))
1237 {
1238 cli_print(cli, "User %s is not connected", argv[i]);
1239 continue;
1240 }
1241
1242 cli_print(cli, "Not snooping user %s", argv[i]);
1243 cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
1244 }
1245
1246 return CLI_OK;
1247 }
1248
1249 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1250 {
1251 int rate_in = 0;
1252 int rate_out = 0;
1253 sessionidt s;
1254
1255 /*
1256 throttle USER - throttle in/out to default rate
1257 throttle USER RATE - throttle in/out to default rate
1258 throttle USER in RATE - throttle input only
1259 throttle USER out RATE - throttle output only
1260 throttle USER in RATE out RATE - throttle both
1261 */
1262
1263 if (CLI_HELP_REQUESTED)
1264 {
1265 switch (argc)
1266 {
1267 case 1:
1268 return cli_arg_help(cli, 0,
1269 "USER", "Username of session to throttle", NULL);
1270
1271 case 2:
1272 return cli_arg_help(cli, 1,
1273 "RATE", "Rate in kbps (in and out)",
1274 "in", "Select incoming rate",
1275 "out", "Select outgoing rate", NULL);
1276
1277 case 4:
1278 return cli_arg_help(cli, 1,
1279 "in", "Select incoming rate",
1280 "out", "Select outgoing rate", NULL);
1281
1282 case 3:
1283 if (isdigit(argv[1][0]))
1284 return cli_arg_help(cli, 1, NULL);
1285
1286 case 5:
1287 return cli_arg_help(cli, 0, "RATE", "Rate in kbps", NULL);
1288
1289 default:
1290 return cli_arg_help(cli, argc > 1, NULL);
1291 }
1292 }
1293
1294 if (!config->cluster_iam_master)
1295 {
1296 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1297 return CLI_OK;
1298 }
1299
1300 if (argc == 0)
1301 {
1302 cli_print(cli, "Specify a user to throttle");
1303 return CLI_OK;
1304 }
1305
1306 if (!(s = sessionbyuser(argv[0])))
1307 {
1308 cli_print(cli, "User %s is not connected", argv[0]);
1309 return CLI_OK;
1310 }
1311
1312 if (argc == 1)
1313 {
1314 rate_in = rate_out = config->rl_rate;
1315 }
1316 else if (argc == 2)
1317 {
1318 rate_in = rate_out = atoi(argv[1]);
1319 if (rate_in < 1)
1320 {
1321 cli_print(cli, "Invalid rate \"%s\"", argv[1]);
1322 return CLI_OK;
1323 }
1324 }
1325 else if (argc == 3 || argc == 5)
1326 {
1327 int i;
1328 for (i = 1; i < argc - 1; i += 2)
1329 {
1330 int len = strlen(argv[i]);
1331 int r = 0;
1332 if (!strncasecmp(argv[i], "in", len))
1333 r = rate_in = atoi(argv[i+1]);
1334 else if (!strncasecmp(argv[i], "out", len))
1335 r = rate_out = atoi(argv[i+1]);
1336
1337 if (r < 1)
1338 {
1339 cli_print(cli, "Invalid rate specification \"%s %s\"", argv[i], argv[i+1]);
1340 return CLI_OK;
1341 }
1342 }
1343 }
1344 else
1345 {
1346 cli_print(cli, "Invalid arguments");
1347 return CLI_OK;
1348 }
1349
1350 if ((rate_in && session[s].throttle_in) || (rate_out && session[s].throttle_out))
1351 {
1352 cli_print(cli, "User %s already throttled, unthrottle first", argv[0]);
1353 return CLI_OK;
1354 }
1355
1356 cli_session_actions[s].throttle_in = cli_session_actions[s].throttle_out = -1;
1357 if (rate_in && session[s].throttle_in != rate_in)
1358 cli_session_actions[s].throttle_in = rate_in;
1359
1360 if (rate_out && session[s].throttle_out != rate_out)
1361 cli_session_actions[s].throttle_out = rate_out;
1362
1363 if (cli_session_actions[s].throttle_in == -1 &&
1364 cli_session_actions[s].throttle_out == -1)
1365 {
1366 cli_print(cli, "User %s already throttled at this rate", argv[0]);
1367 return CLI_OK;
1368 }
1369
1370 cli_print(cli, "Throttling user %s", argv[0]);
1371 cli_session_actions[s].action |= CLI_SESS_THROTTLE;
1372
1373 return CLI_OK;
1374 }
1375
1376 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1377 {
1378 int i;
1379 sessionidt s;
1380
1381 if (CLI_HELP_REQUESTED)
1382 return cli_arg_help(cli, argc > 1,
1383 "USER", "Username of session to unthrottle", NULL);
1384
1385 if (!config->cluster_iam_master)
1386 {
1387 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1388 return CLI_OK;
1389 }
1390
1391 if (!argc)
1392 {
1393 cli_print(cli, "Specify a user to unthrottle");
1394 return CLI_OK;
1395 }
1396
1397 for (i = 0; i < argc; i++)
1398 {
1399 if (!(s = sessionbyuser(argv[i])))
1400 {
1401 cli_print(cli, "User %s is not connected", argv[i]);
1402 continue;
1403 }
1404
1405 if (session[s].throttle_in || session[s].throttle_out)
1406 {
1407 cli_print(cli, "Unthrottling user %s", argv[i]);
1408 cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
1409 }
1410 else
1411 {
1412 cli_print(cli, "User %s not throttled", argv[i]);
1413 }
1414 }
1415
1416 return CLI_OK;
1417 }
1418
1419 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1420 {
1421 int i;
1422
1423 if (CLI_HELP_REQUESTED)
1424 return cli_arg_help(cli, 1,
1425 "all", "Enable debugging for all except \"data\"",
1426 "critical", "", // FIXME: add descriptions
1427 "error", "",
1428 "warning", "",
1429 "info", "",
1430 "calls", "",
1431 "data", "",
1432 NULL);
1433
1434 if (!argc)
1435 {
1436 char *p = (char *) &debug_flags;
1437 for (i = 0; i < sizeof(debug_flags); i++)
1438 {
1439 if (p[i])
1440 {
1441 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1442 (debug_flags.critical) ? " critical" : "",
1443 (debug_flags.error) ? " error" : "",
1444 (debug_flags.warning) ? " warning" : "",
1445 (debug_flags.info) ? " info" : "",
1446 (debug_flags.calls) ? " calls" : "",
1447 (debug_flags.data) ? " data" : "");
1448
1449 return CLI_OK;
1450 }
1451 }
1452
1453 cli_print(cli, "Debugging off");
1454 return CLI_OK;
1455 }
1456
1457 for (i = 0; i < argc; i++)
1458 {
1459 int len = strlen(argv[i]);
1460
1461 if (argv[i][0] == 'c' && len < 2)
1462 len = 2; /* distinguish [cr]itical from [ca]lls */
1463
1464 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; }
1465 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; }
1466 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; }
1467 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; }
1468 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; }
1469 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; }
1470 if (!strncasecmp(argv[i], "all", len))
1471 {
1472 memset(&debug_flags, 1, sizeof(debug_flags));
1473 debug_flags.data = 0;
1474 continue;
1475 }
1476
1477 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1478 }
1479
1480 return CLI_OK;
1481 }
1482
1483 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1484 {
1485 int i;
1486
1487 if (CLI_HELP_REQUESTED)
1488 return cli_arg_help(cli, 1,
1489 "all", "Disable all debugging",
1490 "critical", "", // FIXME: add descriptions
1491 "error", "",
1492 "warning", "",
1493 "info", "",
1494 "calls", "",
1495 "data", "",
1496 NULL);
1497
1498 if (!argc)
1499 {
1500 memset(&debug_flags, 0, sizeof(debug_flags));
1501 return CLI_OK;
1502 }
1503
1504 for (i = 0; i < argc; i++)
1505 {
1506 int len = strlen(argv[i]);
1507
1508 if (argv[i][0] == 'c' && len < 2)
1509 len = 2; /* distinguish [cr]itical from [ca]lls */
1510
1511 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; }
1512 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; }
1513 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; }
1514 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; }
1515 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; }
1516 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; }
1517 if (!strncasecmp(argv[i], "all", len))
1518 {
1519 memset(&debug_flags, 0, sizeof(debug_flags));
1520 continue;
1521 }
1522
1523 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1524 }
1525
1526 return CLI_OK;
1527 }
1528
1529 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1530 {
1531 int i, firstfree = 0;
1532
1533 if (CLI_HELP_REQUESTED)
1534 return cli_arg_help(cli, argc > 1,
1535 "PLUGIN", "Name of plugin to load", NULL);
1536
1537 if (argc != 1)
1538 {
1539 cli_print(cli, "Specify a plugin to load");
1540 return CLI_OK;
1541 }
1542
1543 for (i = 0; i < MAXPLUGINS; i++)
1544 {
1545 if (!*config->plugins[i] && !firstfree)
1546 firstfree = i;
1547 if (strcmp(config->plugins[i], argv[0]) == 0)
1548 {
1549 cli_print(cli, "Plugin is already loaded");
1550 return CLI_OK;
1551 }
1552 }
1553
1554 if (firstfree)
1555 {
1556 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1557 config->reload_config = 1;
1558 cli_print(cli, "Loading plugin %s", argv[0]);
1559 }
1560
1561 return CLI_OK;
1562 }
1563
1564 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1565 {
1566 int i;
1567
1568 if (CLI_HELP_REQUESTED)
1569 return cli_arg_help(cli, argc > 1,
1570 "PLUGIN", "Name of plugin to unload", NULL);
1571
1572 if (argc != 1)
1573 {
1574 cli_print(cli, "Specify a plugin to remove");
1575 return CLI_OK;
1576 }
1577
1578 for (i = 0; i < MAXPLUGINS; i++)
1579 {
1580 if (strcmp(config->plugins[i], argv[0]) == 0)
1581 {
1582 config->reload_config = 1;
1583 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1584 return CLI_OK;
1585 }
1586 }
1587
1588 cli_print(cli, "Plugin is not loaded");
1589 return CLI_OK;
1590 }
1591
1592 char *duration(time_t secs)
1593 {
1594 static char *buf = NULL;
1595 int p = 0;
1596
1597 if (!buf) buf = calloc(64, 1);
1598
1599 if (secs >= 86400)
1600 {
1601 int days = secs / 86400;
1602 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1603 secs %= 86400;
1604 }
1605
1606 if (secs >= 3600)
1607 {
1608 int mins = secs / 60;
1609 int hrs = mins / 60;
1610
1611 mins %= 60;
1612 sprintf(buf + p, "%d:%02d", hrs, mins);
1613 }
1614 else if (secs >= 60)
1615 {
1616 int mins = secs / 60;
1617 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1618 }
1619 else
1620 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1621
1622 return buf;
1623 }
1624
1625 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1626 {
1627 FILE *fh;
1628 char buf[100], *p = buf, *loads[3];
1629 int i, num_sessions = 0;
1630
1631 if (CLI_HELP_REQUESTED)
1632 return CLI_HELP_NO_ARGS;
1633
1634 fh = fopen("/proc/loadavg", "r");
1635 fgets(buf, 100, fh);
1636 fclose(fh);
1637
1638 for (i = 0; i < 3; i++)
1639 loads[i] = strdup(strsep(&p, " "));
1640
1641 time(&time_now);
1642 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1643
1644 for (i = 1; i < MAXSESSION; i++)
1645 if (session[i].opened) num_sessions++;
1646
1647 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1648 buf,
1649 duration(time_now - config->start_time),
1650 num_sessions,
1651 loads[0], loads[1], loads[2]
1652 );
1653 for (i = 0; i < 3; i++)
1654 if (loads[i]) free(loads[i]);
1655
1656 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1657
1658 return CLI_OK;
1659 }
1660
1661 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1662 {
1663 int i;
1664
1665 if (CLI_HELP_REQUESTED)
1666 {
1667 switch (argc)
1668 {
1669 case 1:
1670 {
1671 int len = strlen(argv[0])-1;
1672 for (i = 0; config_values[i].key; i++)
1673 if (!len || !strncmp(argv[0], config_values[i].key, len))
1674 cli_print(cli, " %s", config_values[i].key);
1675 }
1676
1677 return CLI_OK;
1678
1679 case 2:
1680 return cli_arg_help(cli, 0,
1681 "VALUE", "Value for variable", NULL);
1682
1683 case 3:
1684 if (!argv[2][1])
1685 return cli_arg_help(cli, 1, NULL);
1686
1687 default:
1688 return CLI_OK;
1689 }
1690 }
1691
1692 if (argc != 2)
1693 {
1694 cli_print(cli, "Specify variable and value");
1695 return CLI_OK;
1696 }
1697
1698 for (i = 0; config_values[i].key; i++)
1699 {
1700 void *value = ((void *)config) + config_values[i].offset;
1701 if (strcmp(config_values[i].key, argv[0]) == 0)
1702 {
1703 // Found a value to set
1704 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1705 switch (config_values[i].type)
1706 {
1707 case STRING:
1708 strncpy((char *)value, argv[1], config_values[i].size - 1);
1709 break;
1710 case INT:
1711 *(int *)value = atoi(argv[1]);
1712 break;
1713 case UNSIGNED_LONG:
1714 *(unsigned long *)value = atol(argv[1]);
1715 break;
1716 case SHORT:
1717 *(short *)value = atoi(argv[1]);
1718 break;
1719 case IP:
1720 *(unsigned *)value = inet_addr(argv[1]);
1721 break;
1722 case MAC:
1723 parsemac(argv[1], (char *)value);
1724 break;
1725 case BOOL:
1726 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1727 *(int *)value = 1;
1728 else
1729 *(int *)value = 0;
1730 break;
1731 default:
1732 cli_print(cli, "Unknown variable type");
1733 break;
1734 }
1735 config->reload_config = 1;
1736 return CLI_OK;
1737 }
1738 }
1739
1740 cli_print(cli, "Unknown variable \"%s\"", argv[0]);
1741 return CLI_OK;
1742 }
1743
1744 int regular_stuff(struct cli_def *cli)
1745 {
1746 int i = debug_rb_tail;
1747 int reprompt = 0;
1748
1749 #ifdef RINGBUFFER
1750 while (i != ringbuffer->tail)
1751 {
1752 int show_message = 0;
1753
1754 if (*ringbuffer->buffer[i].message)
1755 {
1756 // Always show messages if we are doing general debug
1757 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
1758 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
1759 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
1760 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
1761 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
1762 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
1763 }
1764
1765 if (show_message)
1766 {
1767 ipt address = htonl(ringbuffer->buffer[i].address);
1768 char *ipaddr;
1769 struct in_addr addr;
1770
1771 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
1772 ipaddr = inet_ntoa(addr);
1773
1774 cli_print(cli, "\r%s-%s-%u-%u %s",
1775 debug_levels[(int)ringbuffer->buffer[i].level],
1776 ipaddr,
1777 ringbuffer->buffer[i].tunnel,
1778 ringbuffer->buffer[i].session,
1779 ringbuffer->buffer[i].message);
1780
1781 reprompt = 1;
1782 }
1783
1784 if (++i == ringbuffer->tail) break;
1785 if (i == RINGBUFFER_SIZE) i = 0;
1786 }
1787
1788 debug_rb_tail = ringbuffer->tail;
1789 if (reprompt)
1790 cli_reprompt(cli);
1791 #endif
1792 return CLI_OK;
1793 }
1794
1795 // Convert a string in the form of abcd.ef12.3456 into char[6]
1796 void parsemac(char *string, char mac[6])
1797 {
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 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)
1801 return;
1802 memset(mac, 0, 6);
1803 }