.
[l2tpns.git] / cli.c
1 // L2TPNS Command Line Interface
2 // $Id: cli.c,v 1.3 2004-03-05 00:22:06 fred_nerk Exp $
3 // vim: sw=4 ts=8
4
5 #include <stdio.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <syslog.h>
9 #include <malloc.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <arpa/inet.h>
14 #include <errno.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include <signal.h>
18 #include <unistd.h>
19 #include "l2tpns.h"
20 #include "libcli.h"
21 #include "util.h"
22
23 extern tunnelt *tunnel;
24 extern sessiont *session;
25 extern radiust *radius;
26 extern ippoolt *ip_address_pool;
27 extern struct Tstats *_statistics;
28 extern int cli_pid;
29 struct cli_def *cli = NULL;
30 int cli_quit = 0;
31 extern int clifd, udpfd, tapfd, snoopfd, radfd, ifrfd, cluster_sockfd;
32 extern sessionidt *cli_session_kill;
33 extern tunnelidt *cli_tunnel_kill;
34 extern tbft *filter_buckets;
35 extern struct configt *config;
36 extern struct config_descriptt config_values[];
37 extern char hostname[];
38 #ifdef RINGBUFFER
39 extern struct Tringbuffer *ringbuffer;
40 #endif
41
42 char *rcs_id = "$Id: cli.c,v 1.3 2004-03-05 00:22:06 fred_nerk Exp $";
43
44 char *debug_levels[] = {
45 "CRIT",
46 "ERROR",
47 "WARN",
48 "INFO",
49 "CALL",
50 "DATA",
51 };
52
53 struct
54 {
55 char critical;
56 char error;
57 char warning;
58 char info;
59 char calls;
60 char data;
61 } debug_flags;
62
63 int debug_session;
64 int debug_tunnel;
65 int debug_rb_tail;
66 FILE *save_config_fh;
67
68 int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
69 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
70 int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
71 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
72 int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
73 int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
74 int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
75 int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
76 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
77 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
78 int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
79 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
80 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
81 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
82 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
83 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
84 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
85 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
86 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
87 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
88 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
89 int cmd_watch_session(struct cli_def *cli, char *command, char **argv, int argc);
90 int cmd_watch_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
91 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
92 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
93 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
94 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
95 int regular_stuff(struct cli_def *cli);
96
97 void init_cli()
98 {
99 FILE *f;
100 char buf[4096];
101 struct cli_command *c;
102 int on = 1;
103 struct sockaddr_in addr;
104
105 cli = cli_init();
106
107 c = cli_register_command(cli, NULL, "show", NULL, NULL);
108 cli_register_command(cli, c, "session", cmd_show_session, "Show a list of sessions or details for a single session");
109 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, "Show a list of tunnels or details for a single tunnel");
110 cli_register_command(cli, c, "users", cmd_show_users, "Show a list of all connected users");
111 cli_register_command(cli, c, "version", cmd_show_version, "Show currently running software version");
112 cli_register_command(cli, c, "banana", cmd_show_banana, "Show a banana");
113 cli_register_command(cli, c, "pool", cmd_show_pool, "Show the IP address allocation pool");
114 cli_register_command(cli, c, "running-config", cmd_show_run, "Show the currently running configuration");
115 cli_register_command(cli, c, "radius", cmd_show_radius, "Show active radius queries");
116 cli_register_command(cli, c, "plugins", cmd_show_plugins, "List all installed plugins");
117
118 #ifdef STATISTICS
119 cli_register_command(cli, c, "counters", cmd_show_counters, "Display all the internal counters and running totals");
120
121 c = cli_register_command(cli, NULL, "clear", NULL, NULL);
122 cli_register_command(cli, c, "counters", cmd_clear_counters, "Clear internal counters");
123 #endif
124
125 cli_register_command(cli, NULL, "uptime", cmd_uptime, "Show uptime and bandwidth utilisation");
126
127 c = cli_register_command(cli, NULL, "write", NULL, NULL);
128 cli_register_command(cli, c, "memory", cmd_write_memory, "Save the running config to flash");
129 cli_register_command(cli, c, "terminal", cmd_show_run, "Show the running config");
130
131 cli_register_command(cli, NULL, "snoop", cmd_snoop, "Temporarily enable interception for a user");
132 cli_register_command(cli, NULL, "throttle", cmd_throttle, "Temporarily enable throttling for a user");
133
134 c = cli_register_command(cli, NULL, "no", NULL, NULL);
135 cli_register_command(cli, c, "snoop", cmd_no_snoop, "Temporarily disable interception for a user");
136 cli_register_command(cli, c, "throttle", cmd_no_throttle, "Temporarily disable throttling for a user");
137 cli_register_command(cli, c, "debug", cmd_no_debug, "Turn off logging of a certain level of debugging");
138
139 c = cli_register_command(cli, NULL, "drop", NULL, NULL);
140 cli_register_command(cli, c, "user", cmd_drop_user, "Disconnect a user");
141 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, "Disconnect a tunnel and all sessions on that tunnel");
142 cli_register_command(cli, c, "session", cmd_drop_session, "Disconnect a session");
143
144 cli_register_command(cli, NULL, "debug", cmd_debug, "Set the level of logging that is shown on the console");
145
146 /*
147 c = cli_register_command(cli, NULL, "watch", NULL, NULL);
148 cli_register_command(cli, c, "session", cmd_watch_session, "Dump logs for a session");
149 cli_register_command(cli, c, "tunnel", cmd_watch_tunnel, "Dump logs for a tunnel");
150 */
151
152 c = cli_register_command(cli, NULL, "load", NULL, NULL);
153 cli_register_command(cli, c, "plugin", cmd_load_plugin, "Load a plugin");
154
155 c = cli_register_command(cli, NULL, "remove", NULL, NULL);
156 cli_register_command(cli, c, "plugin", cmd_remove_plugin, "Remove a plugin");
157
158 cli_register_command(cli, NULL, "set", cmd_set, "Set a configuration variable");
159
160 // Enable regular processing
161 cli_regular(cli, regular_stuff);
162
163 if (!(f = fopen(CLIUSERS, "r")))
164 {
165 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
166 }
167 else
168 {
169 while (fgets(buf, 4096, f))
170 {
171 char *p;
172 if (*buf == '#') continue;
173 if ((p = strchr(buf, '\r'))) *p = 0;
174 if ((p = strchr(buf, '\n'))) *p = 0;
175 if (!*buf) continue;
176 if (!(p = strchr((char *)buf, ':'))) continue;
177 *p++ = 0;
178 cli_allow_user(cli, buf, p);
179 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
180 }
181 fclose(f);
182 }
183
184 memset(&addr, 0, sizeof(addr));
185 clifd = socket(PF_INET, SOCK_STREAM, 6);
186 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
187 {
188 int flags;
189 // Set cli fd as non-blocking
190 flags = fcntl(clifd, F_GETFL, 0);
191 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
192 }
193 addr.sin_family = AF_INET;
194 addr.sin_port = htons(23);
195 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
196 {
197 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
198 return;
199 }
200 listen(clifd, 10);
201 }
202
203 void cli_do(int sockfd)
204 {
205 if (fork()) return;
206
207 // Close sockets
208 if (udpfd) close(udpfd); udpfd = 0;
209 if (tapfd) close(tapfd); tapfd = 0;
210 if (snoopfd) close(snoopfd); snoopfd = 0;
211 if (radfd) close(radfd); radfd = 0;
212 if (ifrfd) close(ifrfd); ifrfd = 0;
213 if (cluster_sockfd) close(cluster_sockfd); cluster_sockfd = 0;
214 if (clifd) close(clifd); clifd = 0;
215
216 signal(SIGPIPE, SIG_DFL);
217 signal(SIGCHLD, SIG_DFL);
218 signal(SIGHUP, SIG_DFL);
219 signal(SIGUSR1, SIG_DFL);
220 signal(SIGQUIT, SIG_DFL);
221 signal(SIGKILL, SIG_DFL);
222 signal(SIGALRM, SIG_DFL);
223
224 log(3, 0, 0, 0, "Accepted connection to CLI\n");
225
226 debug_session = 0;
227 debug_tunnel = 0;
228 #ifdef RINGBUFFER
229 debug_rb_tail = ringbuffer->tail;
230 #endif
231 memset(&debug_flags, 0, sizeof(debug_flags));
232 debug_flags.critical = 1;
233
234 {
235 char prompt[1005];
236 snprintf(prompt, 1005, "%s> ", hostname);
237 cli_loop(cli, sockfd, prompt);
238 }
239
240 close(sockfd);
241 log(3, 0, 0, 0, "Closed CLI connection\n");
242 exit(0);
243 }
244
245 void cli_print_log(struct cli_def *cli, char *string)
246 {
247 log(3, 0, 0, 0, "%s\n", string);
248 }
249
250 void cli_do_file(FILE *fh)
251 {
252 log(3, 0, 0, 0, "Reading configuration file\n");
253 cli_print_callback(cli, cli_print_log);
254 cli_file(cli, fh);
255 cli_print_callback(cli, NULL);
256 }
257
258 int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
259 {
260 int i;
261 time_t time_now;
262
263 time(&time_now);
264 if (argc > 0)
265 {
266 // Show individual session
267 for (i = 0; i < argc; i++)
268 {
269 unsigned int s;
270 s = atoi(argv[i]);
271 if (!s || s > MAXSESSION)
272 {
273 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
274 continue;
275 }
276 cli_print(cli, "\r\nSession %d:", s);
277 cli_print(cli, " User: %s", session[s].user[0] ? session[s].user : "none");
278 cli_print(cli, " Calling Num: %s", session[s].calling);
279 cli_print(cli, " Called Num: %s", session[s].called);
280 cli_print(cli, " Tunnel ID: %d", session[s].tunnel);
281 cli_print(cli, " IP address: %s", inet_toa(htonl(session[s].ip)));
282 cli_print(cli, " HSD sid: %lu", session[s].sid);
283 cli_print(cli, " Idle time: %u seconds", abs(time_now - session[s].last_packet));
284 cli_print(cli, " Next Recv: %u", session[s].nr);
285 cli_print(cli, " Next Send: %u", session[s].ns);
286 cli_print(cli, " Bytes In/Out: %lu/%lu", (unsigned long)session[s].cin, (unsigned long)session[s].total_cout);
287 cli_print(cli, " Pkts In/Out: %lu/%lu", (unsigned long)session[s].pin, (unsigned long)session[s].pout);
288 cli_print(cli, " Radius Session: %u", session[s].radius);
289 cli_print(cli, " Rx Speed: %lu", session[s].rx_connect_speed);
290 cli_print(cli, " Tx Speed: %lu", session[s].tx_connect_speed);
291 cli_print(cli, " Intercepted: %s", session[s].snoop ? "YES" : "no");
292 cli_print(cli, " Throttled: %s", session[s].throttle ? "YES" : "no");
293 cli_print(cli, " Walled Garden: %s", session[s].walled_garden ? "YES" : "no");
294 cli_print(cli, " Filter Bucket: %s", session[s].tbf ? filter_buckets[session[s].tbf].handle : "none");
295 }
296 return CLI_OK;
297 }
298
299 // Show Summary
300 cli_print(cli, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
301 "SID",
302 "TID",
303 "Username",
304 "IP",
305 "I",
306 "T",
307 "G",
308 "opened",
309 "downloaded",
310 "uploaded",
311 "idle",
312 "LAC",
313 "CLI");
314 for (i = 1; i < MAXSESSION; i++)
315 {
316 char *userip, *tunnelip;
317 if (!session[i].opened) continue;
318 userip = strdup(inet_toa(htonl(session[i].ip)));
319 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
320 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
321 i,
322 session[i].tunnel,
323 session[i].user[0] ? session[i].user : "*",
324 userip,
325 (session[i].snoop) ? "Y" : "N",
326 (session[i].throttle) ? "Y" : "N",
327 (session[i].walled_garden) ? "Y" : "N",
328 abs(time_now - (unsigned long)session[i].opened),
329 (unsigned long)session[i].total_cout,
330 (unsigned long)session[i].total_cin,
331 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
332 tunnelip,
333 session[i].calling[0] ? session[i].calling : "*");
334 if (userip) free(userip);
335 if (tunnelip) free(tunnelip);
336 }
337 return CLI_OK;
338 }
339
340 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
341 {
342 int i, x, show_all = 0;
343 time_t time_now;
344 char *states[] = {
345 "Free",
346 "Open",
347 "Closing",
348 "Opening",
349 };
350
351 time(&time_now);
352 if (argc > 0)
353 {
354 if (strcmp(argv[0], "all") == 0)
355 {
356 show_all = 1;
357 }
358 else
359 {
360 // Show individual tunnel
361 for (i = 0; i < argc; i++)
362 {
363 char s[65535] = {0};
364 unsigned int t;
365 t = atoi(argv[i]);
366 if (!t || t > MAXTUNNEL)
367 {
368 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
369 continue;
370 }
371 cli_print(cli, "\r\nTunnel %d:", t);
372 cli_print(cli, " State: %s", states[tunnel[t].state]);
373 cli_print(cli, " Hostname: %s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
374 cli_print(cli, " Remote IP: %s", inet_toa(htonl(tunnel[t].ip)));
375 cli_print(cli, " Remote Port: %d", tunnel[t].port);
376 cli_print(cli, " Rx Window: %u", tunnel[t].window);
377 cli_print(cli, " Next Recv: %u", tunnel[t].nr);
378 cli_print(cli, " Next Send: %u", tunnel[t].ns);
379 cli_print(cli, " Queue Len: %u", tunnel[t].controlc);
380 cli_print(cli, " Last Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
381
382 for (x = 0; x < MAXSESSION; x++)
383 if (session[x].tunnel == t && session[x].opened && !session[x].die)
384 sprintf(s, "%s%u ", s, x);
385 cli_print(cli, " Sessions: %s", s);
386 }
387 return CLI_OK;
388 }
389 }
390
391 // Show tunnel summary
392 cli_print(cli, "%s %s %s %s %s",
393 "TID",
394 "Hostname",
395 "IP",
396 "State",
397 "Sessions");
398 for (i = 1; i < MAXTUNNEL; i++)
399 {
400 int sessions = 0;
401 if (!show_all && (!tunnel[i].ip || tunnel[i].die || !tunnel[i].hostname[0])) continue;
402
403 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
404 cli_print(cli, "%d %s %s %s %d",
405 i,
406 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
407 inet_toa(htonl(tunnel[i].ip)),
408 states[tunnel[i].state],
409 sessions);
410 }
411 return CLI_OK;
412 }
413
414 int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
415 {
416 int i;
417 for (i = 0; i < MAXSESSION; i++)
418 {
419 if (!session[i].opened) continue;
420 if (!session[i].user[0]) continue;
421 cli_print(cli, "%s",
422 session[i].user);
423 }
424 return CLI_OK;
425 }
426
427 int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
428 {
429 cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
430 cli_print(cli, "%-10s %8lu %8lu %8lu", "RX",
431 GET_STAT(tap_rx_bytes),
432 GET_STAT(tap_rx_packets),
433 GET_STAT(tap_rx_errors));
434 cli_print(cli, "%-10s %8lu %8lu %8lu", "TX",
435 GET_STAT(tap_tx_bytes),
436 GET_STAT(tap_tx_packets),
437 GET_STAT(tap_tx_errors));
438 cli_print(cli, "");
439
440 cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
441 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX",
442 GET_STAT(tunnel_rx_bytes),
443 GET_STAT(tunnel_rx_packets),
444 GET_STAT(tunnel_rx_errors),
445 0L);
446 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX",
447 GET_STAT(tunnel_tx_bytes),
448 GET_STAT(tunnel_tx_packets),
449 GET_STAT(tunnel_rx_errors),
450 GET_STAT(tunnel_retries));
451 cli_print(cli, "");
452
453 cli_print(cli, "%-30s%-10s", "Counter", "Value");
454 cli_print(cli, "-----------------------------------------");
455 cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries));
456 cli_print(cli, "%-30s%lu", "arp_errors", GET_STAT(arp_errors));
457 cli_print(cli, "%-30s%lu", "arp_replies", GET_STAT(arp_replies));
458 cli_print(cli, "%-30s%lu", "arp_discarded", GET_STAT(arp_discarded));
459 cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent));
460 cli_print(cli, "%-30s%lu", "arp_recv", GET_STAT(arp_recv));
461 cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped));
462 cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created));
463 cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created));
464 cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout));
465 cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout));
466 cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout));
467 cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow));
468 cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow));
469 cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow));
470 cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated));
471 cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed));
472
473 #ifdef STAT_CALLS
474 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
475 cli_print(cli, "-----------------------------------------");
476 cli_print(cli, "%-30s%lu", "call_processtap", GET_STAT(call_processtap));
477 cli_print(cli, "%-30s%lu", "call_processarp", GET_STAT(call_processarp));
478 cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout));
479 cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp));
480 cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap));
481 cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap));
482 cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp));
483 cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp));
484 cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin));
485 cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp));
486 cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad));
487 cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp));
488 cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp));
489 cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap));
490 cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip));
491 cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
492 cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend));
493 cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill));
494 cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
495 cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill));
496 cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
497 cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup));
498 cli_print(cli, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address));
499 cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address));
500 cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
501 cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend));
502 cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry));
503 #endif
504 return CLI_OK;
505 }
506
507 int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
508 {
509 cli_print(cli, "L2TPNS %s", VERSION);
510 cli_print(cli, "ID: %s", rcs_id);
511 return CLI_OK;
512 }
513
514 int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
515 {
516 int i;
517 int used = 0, free = 0, show_all = 0;
518 time_t time_now;
519
520 if (argc > 0 && strcmp(argv[0], "all") == 0)
521 show_all = 1;
522
523 time(&time_now);
524 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
525 for (i = 0; i < MAXIPPOOL; i++)
526 {
527 if (!ip_address_pool[i].address) continue;
528 if (ip_address_pool[i].assigned)
529 {
530 sessionidt s = sessionbyip(ip_address_pool[i].address);
531 cli_print(cli, "%-15s Y %8d %s",
532 inet_toa(ip_address_pool[i].address), s, session[s].user);
533
534 used++;
535 }
536 else
537 {
538 if (ip_address_pool[i].last)
539 cli_print(cli, "%-15s N %8s [%s] %ds",
540 inet_toa(ip_address_pool[i].address), "",
541 ip_address_pool[i].user, time_now - ip_address_pool[i].last);
542 else if (show_all)
543 cli_print(cli, "%-15s N", inet_toa(ip_address_pool[i].address));
544
545 free++;
546 }
547 }
548
549 if (!show_all)
550 cli_print(cli, "(Not displaying unused addresses)");
551
552 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
553 return CLI_OK;
554 }
555
556 void print_save_config(struct cli_def *cli, char *string)
557 {
558 if (save_config_fh)
559 fprintf(save_config_fh, "%s\n", string);
560 }
561
562 int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
563 {
564 if ((save_config_fh = fopen(config->config_file, "w")))
565 {
566 cli_print(cli, "Writing configuration");
567 cli_print_callback(cli, print_save_config);
568 cmd_show_run(cli, command, argv, argc);
569 cli_print_callback(cli, NULL);
570 fclose(save_config_fh);
571 sleep(1);
572 }
573 else
574 {
575 cli_print(cli, "Error writing configuration: %s", strerror(errno));
576 }
577 return CLI_OK;
578 }
579
580 int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
581 {
582 int i;
583
584 cli_print(cli, "# Current configuration:");
585
586 for (i = 0; config_values[i].key; i++)
587 {
588 void *value = ((void *)config) + config_values[i].offset;
589 if (config_values[i].type == STRING)
590 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value);
591 else if (config_values[i].type == IP)
592 cli_print(cli, "set %s %s", config_values[i].key, inet_toa(*(unsigned *)value));
593 else if (config_values[i].type == SHORT)
594 cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value);
595 else if (config_values[i].type == BOOL)
596 cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no");
597 else if (config_values[i].type == INT)
598 cli_print(cli, "set %s %d", config_values[i].key, *(int *)value);
599 else if (config_values[i].type == UNSIGNED_LONG)
600 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value);
601 }
602
603 cli_print(cli, "# Plugins");
604 for (i = 0; i < MAXPLUGINS; i++)
605 {
606 if (*config->plugins[i])
607 {
608 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
609 }
610 }
611
612 cli_print(cli, "# end");
613 return CLI_OK;
614 }
615
616 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
617 {
618 char *states[] = {
619 "NULL",
620 "CHAP",
621 "AUTH",
622 "IPCP",
623 "START",
624 "STOP",
625 "WAIT",
626 };
627 int i, free = 0, used = 0, show_all = 0;
628 time_t time_now;
629
630 cli_print(cli, "%6s%6s%9s%9s%4s", "Radius", "State", "Session", "Retry", "Try");
631
632 time(&time_now);
633
634 if (argc > 0 && strcmp(argv[0], "all") == 0)
635 show_all = 1;
636
637 for (i = 1; i < MAXRADIUS; i++)
638 {
639 if (radius[i].state == RADIUSNULL)
640 free++;
641 else
642 used++;
643
644 if (!show_all && radius[i].state == RADIUSNULL) continue;
645
646 cli_print(cli, "%6d%6s%9d%9u%4d",
647 i,
648 states[radius[i].state],
649 radius[i].session,
650 radius[i].retry,
651 radius[i].try);
652 }
653
654 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
655
656 return CLI_OK;
657 }
658
659 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
660 {
661 int i;
662 cli_print(cli, "Plugins currently loaded:");
663 for (i = 0; i < MAXPLUGINS; i++)
664 {
665 if (*config->plugins[i])
666 {
667 cli_print(cli, " %s", config->plugins[i]);
668 }
669 }
670 return CLI_OK;
671 }
672
673 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
674 {
675 cli_print(cli, " _\n"
676 "//\\\n"
677 "V \\\n"
678 " \\ \\_\n"
679 " \\,'.`-.\n"
680 " |\\ `. `.\n"
681 " ( \\ `. `-. _,.-:\\\n"
682 " \\ \\ `. `-._ __..--' ,-';/\n"
683 " \\ `. `-. `-..___..---' _.--' ,'/\n"
684 " `. `. `-._ __..--' ,' /\n"
685 " `. `-_ ``--..'' _.-' ,'\n"
686 " `-_ `-.___ __,--' ,'\n"
687 " `-.__ `----\"\"\" __.-'\n"
688 "hh `--..____..--'");
689
690 return CLI_OK;
691 }
692
693 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
694 {
695 cli_print(cli, "Counters cleared");
696 SET_STAT(last_reset, time(NULL));
697 return CLI_OK;
698 }
699
700 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
701 {
702 int i;
703 sessionidt s;
704
705 if (!argc)
706 {
707 cli_print(cli, "Specify a user to drop");
708 return CLI_OK;
709 }
710 for (i = 0; i < argc; i++)
711 {
712 if (strchr(argv[i], '?'))
713 {
714 cli_print(cli, "username ...");
715 return CLI_OK;
716 }
717 }
718
719 for (i = 0; i < argc; i++)
720 {
721 if (!(s = sessionbyuser(argv[i])))
722 {
723 cli_print(cli, "User %s is not connected", argv[i]);
724 continue;
725 }
726
727 if (session[s].ip && session[s].opened && !session[s].die)
728 {
729 int x;
730
731 cli_print(cli, "Dropping user %s", session[s].user);
732 for (x = 0; x < MAXSESSION; x++)
733 {
734 if (!cli_session_kill[x])
735 {
736 cli_session_kill[x] = s;
737 break;
738 }
739 }
740 }
741 }
742
743 return CLI_OK;
744 }
745
746 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
747 {
748 int i;
749 tunnelidt tid;
750
751 if (!argc)
752 {
753 cli_print(cli, "Specify a tunnel to drop");
754 return CLI_OK;
755 }
756 for (i = 0; i < argc; i++)
757 {
758 if (strchr(argv[i], '?'))
759 {
760 cli_print(cli, "tunnel_id ...");
761 return CLI_OK;
762 }
763 }
764
765 for (i = 0; i < argc; i++)
766 {
767 int x;
768
769 if ((tid = atol(argv[i])) <= 0 || (tid > MAXTUNNEL))
770 {
771 cli_print(cli, "Invalid tunnel ID (%d - %d)", 0, MAXTUNNEL);
772 continue;
773 }
774
775 if (!tunnel[tid].ip)
776 {
777 cli_print(cli, "Tunnel %d is not connected", tid);
778 continue;
779 }
780
781 if (tunnel[tid].die)
782 {
783 cli_print(cli, "Tunnel %d is already being shut down", tid);
784 continue;
785 }
786
787 for (x = 0; x < MAXTUNNEL; x++)
788 {
789 if (!cli_tunnel_kill[x])
790 {
791 cli_tunnel_kill[x] = tid;
792 cli_print(cli, "Tunnel %d shut down (%s)", tid, tunnel[tid].hostname);
793 break;
794 }
795 }
796 }
797
798 return CLI_OK;
799 }
800
801 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
802 {
803 int i;
804 sessionidt s;
805
806 if (!argc)
807 {
808 cli_print(cli, "Specify a session id to drop");
809 return CLI_OK;
810 }
811 for (i = 0; i < argc; i++)
812 {
813 if (strchr(argv[i], '?'))
814 {
815 cli_print(cli, "session_id ...");
816 return CLI_OK;
817 }
818 }
819
820 for (i = 0; i < argc; i++)
821 {
822 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
823 {
824 cli_print(cli, "Invalid session ID (%d - %d)", 0, MAXSESSION);
825 continue;
826 }
827
828 if (session[s].opened && !session[s].die)
829 {
830 int x;
831 for (x = 0; x < MAXSESSION; x++)
832 {
833 if (!cli_session_kill[x])
834 {
835 cli_session_kill[x] = s;
836 break;
837 }
838 }
839 cli_print(cli, "Dropping session %d", s);
840 }
841 else
842 {
843 cli_print(cli, "Session %d is not active.", s);
844 }
845 }
846
847 return CLI_OK;
848 }
849
850 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
851 {
852 int i;
853 sessionidt s;
854
855 if (!argc)
856 {
857 cli_print(cli, "Specify a user");
858 return CLI_OK;
859 }
860 for (i = 0; i < argc; i++)
861 {
862 if (strchr(argv[i], '?'))
863 {
864 cli_print(cli, "username ...");
865 return CLI_OK;
866 }
867 }
868
869 for (i = 0; i < argc; i++)
870 {
871 if (!(s = sessionbyuser(argv[i])))
872 {
873 cli_print(cli, "User %s is not connected", argv[i]);
874 continue;
875 }
876 session[s].snoop = 1;
877
878 cli_print(cli, "Snooping user %s", argv[i]);
879 }
880 return CLI_OK;
881 }
882
883 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
884 {
885 int i;
886 sessionidt s;
887
888 if (!argc)
889 {
890 cli_print(cli, "Specify a user");
891 return CLI_OK;
892 }
893 for (i = 0; i < argc; i++)
894 {
895 if (strchr(argv[i], '?'))
896 {
897 cli_print(cli, "username ...");
898 return CLI_OK;
899 }
900 }
901
902 for (i = 0; i < argc; i++)
903 {
904 if (!(s = sessionbyuser(argv[i])))
905 {
906 cli_print(cli, "User %s is not connected", argv[i]);
907 continue;
908 }
909 session[s].snoop = 0;
910
911 cli_print(cli, "Not snooping user %s", argv[i]);
912 }
913 return CLI_OK;
914 }
915
916 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
917 {
918 int i;
919 sessionidt s;
920
921 if (!argc)
922 {
923 cli_print(cli, "Specify a user");
924 return CLI_OK;
925 }
926 for (i = 0; i < argc; i++)
927 {
928 if (strchr(argv[i], '?'))
929 {
930 cli_print(cli, "username ...");
931 return CLI_OK;
932 }
933 }
934
935 for (i = 0; i < argc; i++)
936 {
937 if (!(s = sessionbyuser(argv[i])))
938 {
939 cli_print(cli, "User %s is not connected", argv[i]);
940 continue;
941 }
942 throttle_session(s, 1);
943
944 cli_print(cli, "throttling user %s", argv[i]);
945 }
946 return CLI_OK;
947 }
948
949 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
950 {
951 int i;
952 sessionidt s;
953
954 if (!argc)
955 {
956 cli_print(cli, "Specify a user");
957 return CLI_OK;
958 }
959 for (i = 0; i < argc; i++)
960 {
961 if (strchr(argv[i], '?'))
962 {
963 cli_print(cli, "username ...");
964 return CLI_OK;
965 }
966 }
967
968 for (i = 0; i < argc; i++)
969 {
970 if (!(s = sessionbyuser(argv[i])))
971 {
972 cli_print(cli, "User %s is not connected", argv[i]);
973 continue;
974 }
975 throttle_session(s, 0);
976
977 cli_print(cli, "unthrottling user %s", argv[i]);
978 }
979 return CLI_OK;
980 }
981
982 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
983 {
984 int i;
985
986 if (!argc)
987 {
988 cli_print(cli, "Currently debugging: ");
989 if (debug_flags.critical) cli_print(cli, "critical ");
990 if (debug_flags.error) cli_print(cli, "error ");
991 if (debug_flags.warning) cli_print(cli, "warning ");
992 if (debug_flags.info) cli_print(cli, "info ");
993 if (debug_flags.calls) cli_print(cli, "calls ");
994 if (debug_flags.data) cli_print(cli, "data ");
995 cli_print(cli, "");
996 return CLI_OK;
997 }
998
999 for (i = 0; i < argc; i++)
1000 {
1001 if (*argv[i] == '?')
1002 {
1003 cli_print(cli, "Possible debugging states are:");
1004 cli_print(cli, " critical");
1005 cli_print(cli, " error");
1006 cli_print(cli, " warning");
1007 cli_print(cli, " info");
1008 cli_print(cli, " calls");
1009 cli_print(cli, " data");
1010 return CLI_OK;
1011 }
1012 }
1013
1014 for (i = 0; i < argc; i++)
1015 {
1016 if (strcasecmp(argv[i], "critical") == 0) debug_flags.critical = 1;
1017 if (strcasecmp(argv[i], "error") == 0) debug_flags.error = 1;
1018 if (strcasecmp(argv[i], "warning") == 0) debug_flags.warning = 1;
1019 if (strcasecmp(argv[i], "info") == 0) debug_flags.info = 1;
1020 if (strcasecmp(argv[i], "calls") == 0) debug_flags.calls = 1;
1021 if (strcasecmp(argv[i], "data") == 0) debug_flags.data = 1;
1022 if (strcasecmp(argv[i], "all") == 0)
1023 {
1024 memset(&debug_flags, 1, sizeof(debug_flags));
1025 debug_flags.data = 0;
1026 }
1027 }
1028
1029 return CLI_OK;
1030 }
1031
1032 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1033 {
1034 int i;
1035
1036 for (i = 0; i < argc; i++)
1037 {
1038 if (strcasecmp(argv[i], "critical") == 0) debug_flags.critical = 0;
1039 if (strcasecmp(argv[i], "error") == 0) debug_flags.error = 0;
1040 if (strcasecmp(argv[i], "warning") == 0) debug_flags.warning = 0;
1041 if (strcasecmp(argv[i], "info") == 0) debug_flags.info = 0;
1042 if (strcasecmp(argv[i], "calls") == 0) debug_flags.calls = 0;
1043 if (strcasecmp(argv[i], "data") == 0) debug_flags.data = 0;
1044 if (strcasecmp(argv[i], "all") == 0) memset(&debug_flags, 0, sizeof(debug_flags));
1045 }
1046
1047 return CLI_OK;
1048 }
1049
1050 int cmd_watch_session(struct cli_def *cli, char *command, char **argv, int argc)
1051 {
1052 sessionidt s;
1053
1054 if (argc != 1)
1055 {
1056 cli_print(cli, "Specify a single session to debug (0 to disable)");
1057 return CLI_OK;
1058 }
1059 s = atoi(argv[0]);
1060
1061 if (debug_session)
1062 cli_print(cli, "No longer debugging session %d", debug_session);
1063
1064 if (s) cli_print(cli, "Debugging session %d.", s);
1065 debug_session = s;
1066
1067 return CLI_OK;
1068 }
1069
1070 int cmd_watch_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1071 {
1072 tunnelidt s;
1073
1074 if (argc != 1)
1075 {
1076 cli_print(cli, "Specify a single tunnel to debug (0 to disable)");
1077 return CLI_OK;
1078 }
1079 s = atoi(argv[0]);
1080
1081 if (debug_tunnel)
1082 cli_print(cli, "No longer debugging tunnel %d", debug_tunnel);
1083
1084 if (s) cli_print(cli, "Debugging tunnel %d.", s);
1085 debug_tunnel = s;
1086
1087 return CLI_OK;
1088 }
1089
1090 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1091 {
1092 int i, firstfree = 0;
1093 if (argc != 1)
1094 {
1095 cli_print(cli, "Specify a plugin to load");
1096 return CLI_OK;
1097 }
1098
1099 for (i = 0; i < MAXPLUGINS; i++)
1100 {
1101 if (!*config->plugins[i] && !firstfree)
1102 firstfree = i;
1103 if (strcmp(config->plugins[i], argv[0]) == 0)
1104 {
1105 cli_print(cli, "Plugin is already loaded");
1106 return CLI_OK;
1107 }
1108 }
1109
1110 if (firstfree)
1111 {
1112 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1113 config->reload_config = 1;
1114 cli_print(cli, "Loading plugin %s", argv[0]);
1115 }
1116
1117 return CLI_OK;
1118 }
1119
1120 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1121 {
1122 int i;
1123
1124 if (argc != 1)
1125 {
1126 cli_print(cli, "Specify a plugin to remove");
1127 return CLI_OK;
1128 }
1129
1130 for (i = 0; i < MAXPLUGINS; i++)
1131 {
1132 if (strcmp(config->plugins[i], argv[0]) == 0)
1133 {
1134 config->reload_config = 1;
1135 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1136 return CLI_OK;
1137 }
1138 }
1139
1140 cli_print(cli, "Plugin is not loaded");
1141 return CLI_OK;
1142 }
1143
1144 char *duration(time_t seconds)
1145 {
1146 static char *buf = NULL;
1147 if (!buf) buf = calloc(64, 1);
1148
1149 if (seconds > 86400)
1150 sprintf(buf, "%d days", (int)(seconds / 86400.0));
1151 else if (seconds > 60)
1152 sprintf(buf, "%02d:%02lu", (int)(seconds / 3600.0), seconds % 60);
1153 else
1154 sprintf(buf, "%lu sec", seconds);
1155 return buf;
1156 }
1157
1158 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1159 {
1160 FILE *fh;
1161 char buf[100], *p = buf, *loads[3];
1162 int i, num_sessions = 0;
1163 time_t time_now;
1164
1165 fh = fopen("/proc/loadavg", "r");
1166 fgets(buf, 100, fh);
1167 fclose(fh);
1168
1169 for (i = 0; i < 3; i++)
1170 loads[i] = strdup(strsep(&p, " "));
1171
1172 time(&time_now);
1173 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1174
1175 for (i = 1; i < MAXSESSION; i++)
1176 if (session[i].opened) num_sessions++;
1177
1178 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1179 buf,
1180 duration(abs(time_now - config->start_time)),
1181 num_sessions,
1182 loads[0], loads[1], loads[2]
1183 );
1184 for (i = 0; i < 3; i++)
1185 if (loads[i]) free(loads[i]);
1186
1187 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1188
1189 return CLI_OK;
1190 }
1191
1192 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1193 {
1194 int i;
1195
1196 if (argc != 2)
1197 {
1198 cli_print(cli, "Usage: set <variable> <value>");
1199 return CLI_OK;
1200 }
1201
1202 for (i = 0; config_values[i].key; i++)
1203 {
1204 void *value = ((void *)config) + config_values[i].offset;
1205 if (strcmp(config_values[i].key, argv[0]) == 0)
1206 {
1207 // Found a value to set
1208 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1209 switch (config_values[i].type)
1210 {
1211 case STRING:
1212 strncpy((char *)value, argv[1], config_values[i].size - 1);
1213 break;
1214 case INT:
1215 *(int *)value = atoi(argv[1]);
1216 break;
1217 case UNSIGNED_LONG:
1218 *(unsigned long *)value = atol(argv[1]);
1219 break;
1220 case SHORT:
1221 *(short *)value = atoi(argv[1]);
1222 break;
1223 case IP:
1224 *(unsigned *)value = inet_addr(argv[1]);
1225 break;
1226 case BOOL:
1227 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1228 *(int *)value = 1;
1229 else
1230 *(int *)value = 0;
1231 break;
1232 default:
1233 cli_print(cli, "Unknown variable type");
1234 break;
1235 }
1236 config->reload_config = 1;
1237 return CLI_OK;
1238 }
1239 }
1240
1241 cli_print(cli, "Unknown variable \"%s\"", argv[0]);
1242 return CLI_OK;
1243 }
1244
1245 int regular_stuff(struct cli_def *cli)
1246 {
1247 int i = debug_rb_tail;
1248 int reprompt = 0;
1249
1250 #ifdef RINGBUFFER
1251 while (i != ringbuffer->tail)
1252 {
1253 int show_message = 0;
1254
1255 if (*ringbuffer->buffer[i].message)
1256 {
1257 // Always show messages if we are doing general debug
1258 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
1259 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
1260 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
1261 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
1262 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
1263 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
1264 }
1265
1266 if (show_message)
1267 {
1268 ipt address = ntohl(ringbuffer->buffer[i].address);
1269 char *ipaddr;
1270 struct in_addr addr;
1271
1272 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
1273 ipaddr = inet_ntoa(addr);
1274
1275 cli_print(cli, "\r%s-%s-%u-%u %s",
1276 debug_levels[(int)ringbuffer->buffer[i].level],
1277 ipaddr,
1278 ringbuffer->buffer[i].tunnel,
1279 ringbuffer->buffer[i].session,
1280 ringbuffer->buffer[i].message);
1281
1282 reprompt = 1;
1283 }
1284
1285 if (++i == ringbuffer->tail) break;
1286 if (i == RINGBUFFER_SIZE) i = 0;
1287 }
1288
1289 debug_rb_tail = ringbuffer->tail;
1290 if (reprompt)
1291 cli_reprompt(cli);
1292 #endif
1293 return CLI_OK;
1294 }