3aafb107cbb3ff95181611e94af34688fe8b54ef
[l2tpns.git] / cli.c
1 // L2TPNS Command Line Interface
2 // $Id: cli.c,v 1.1 2003-12-16 07:07:39 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 <malloc.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
16 #include <signal.h>
17 #include <unistd.h>
18 #include <libcli.h>
19 #include "l2tpns.h"
20 #include "util.h"
21 #include "config.h"
22
23 #ifdef HAVE_LIBCLI
24
25 extern tunnelt *tunnel;
26 extern sessiont *session;
27 extern radiust *radius;
28 extern ippoolt *ip_address_pool;
29 extern struct Tstats *_statistics;
30 extern int cli_pid;
31 struct cli_def *cli = NULL;
32 int cli_quit = 0;
33 extern int clifd, udpfd, tapfd, snoopfd, radfd, ifrfd, cluster_sockfd;
34 extern sessionidt *cli_session_kill;
35 extern tunnelidt *cli_tunnel_kill;
36 extern tbft *filter_buckets;
37 #ifdef RINGBUFFER
38 extern struct Tringbuffer *ringbuffer;
39 #endif
40
41 char *rcs_id = "$Id: cli.c,v 1.1 2003-12-16 07:07:39 fred_nerk Exp $";
42
43 char *debug_levels[] = {
44 "CRIT",
45 "ERROR",
46 "WARN",
47 "INFO",
48 "CALL",
49 "DATA",
50 };
51
52 struct
53 {
54 char critical;
55 char error;
56 char warning;
57 char info;
58 char calls;
59 char data;
60 } debug_flags;
61
62 int debug_session;
63 int debug_tunnel;
64 int debug_rb_tail;
65
66 int cmd_show_session(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
67 int cmd_show_tunnels(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
68 int cmd_show_users(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
69 int cmd_show_counters(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
70 int cmd_show_version(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
71 int cmd_show_pool(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
72 int cmd_show_banana(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
73 int cmd_clear_counters(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
74 int cmd_drop_user(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
75 int cmd_drop_tunnel(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
76 int cmd_drop_session(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
77 int cmd_snoop(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
78 int cmd_no_snoop(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
79 int cmd_throttle(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
80 int cmd_no_throttle(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
81 int cmd_debug(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
82 int cmd_no_debug(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
83 int cmd_watch_session(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
84 int cmd_watch_tunnel(struct cli_def *cli, FILE *w, char *command, char **argv, int argc);
85 int regular_stuff(struct cli_def *cli, FILE *w);
86
87 void init_cli()
88 {
89 FILE *f;
90 char buf[4096];
91 struct cli_command *c;
92 int on = 1;
93 struct sockaddr_in addr;
94
95 cli = cli_init();
96
97 c = cli_register_command(cli, NULL, "show", NULL, NULL);
98 cli_register_command(cli, c, "session", cmd_show_session, "Show a list of sessions");
99 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, NULL);
100 cli_register_command(cli, c, "users", cmd_show_users, NULL);
101 cli_register_command(cli, c, "version", cmd_show_version, NULL);
102 cli_register_command(cli, c, "banana", cmd_show_banana, "Show a banana");
103 cli_register_command(cli, c, "pool", cmd_show_pool, NULL);
104
105 #ifdef STATISTICS
106 cli_register_command(cli, c, "counters", cmd_show_counters, NULL);
107
108 c = cli_register_command(cli, NULL, "clear", NULL, NULL);
109 cli_register_command(cli, c, "counters", cmd_clear_counters, NULL);
110 #endif
111
112 cli_register_command(cli, NULL, "snoop", cmd_snoop, NULL);
113 cli_register_command(cli, NULL, "throttle", cmd_throttle, NULL);
114
115 c = cli_register_command(cli, NULL, "no", NULL, NULL);
116 cli_register_command(cli, c, "snoop", cmd_no_snoop, NULL);
117 cli_register_command(cli, c, "throttle", cmd_no_throttle, NULL);
118 cli_register_command(cli, c, "debug", cmd_no_debug, NULL);
119
120 c = cli_register_command(cli, NULL, "drop", NULL, NULL);
121 cli_register_command(cli, c, "user", cmd_drop_user, NULL);
122 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, NULL);
123 cli_register_command(cli, c, "session", cmd_drop_session, NULL);
124
125 cli_register_command(cli, NULL, "debug", cmd_debug, "Specify a debugging level");
126
127 c = cli_register_command(cli, NULL, "watch", NULL, NULL);
128 cli_register_command(cli, c, "session", cmd_watch_session, "Dump logs for a tunnel");
129 cli_register_command(cli, c, "tunnel", cmd_watch_tunnel, "Dump logs for a tunnel");
130
131 // Enable regular processing
132 cli_regular(cli, regular_stuff);
133
134 if (!(f = fopen(CLIUSERS, "r")))
135 {
136 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
137 }
138 else
139 {
140 while (fgets(buf, 4096, f))
141 {
142 char *p;
143 if (*buf == '#') continue;
144 if ((p = strchr(buf, '\r'))) *p = 0;
145 if ((p = strchr(buf, '\n'))) *p = 0;
146 if (!*buf) continue;
147 if (!(p = strchr((char *)buf, ':'))) continue;
148 *p++ = 0;
149 cli_allow_user(cli, buf, p);
150 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
151 }
152 fclose(f);
153 }
154
155 memset(&addr, 0, sizeof(addr));
156 clifd = socket(PF_INET, SOCK_STREAM, 6);
157 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
158 {
159 int flags;
160 // Set cli fd as non-blocking
161 flags = fcntl(clifd, F_GETFL, 0);
162 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
163 }
164 addr.sin_family = AF_INET;
165 addr.sin_port = htons(23);
166 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
167 {
168 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
169 return;
170 }
171 listen(clifd, 10);
172 }
173
174 void cli_do(int sockfd)
175 {
176 if ((cli_pid = fork())) return;
177
178 // Close sockets
179 if (udpfd) close(udpfd); udpfd = 0;
180 if (tapfd) close(tapfd); tapfd = 0;
181 if (snoopfd) close(snoopfd); snoopfd = 0;
182 if (radfd) close(radfd); radfd = 0;
183 if (ifrfd) close(ifrfd); ifrfd = 0;
184 if (cluster_sockfd) close(cluster_sockfd); cluster_sockfd = 0;
185 if (clifd) close(clifd); clifd = 0;
186
187 signal(SIGPIPE, SIG_DFL);
188 signal(SIGCHLD, SIG_DFL);
189 signal(SIGHUP, SIG_DFL);
190 signal(SIGUSR1, SIG_DFL);
191 signal(SIGQUIT, SIG_DFL);
192 signal(SIGKILL, SIG_DFL);
193 signal(SIGALRM, SIG_DFL);
194
195 log(3, 0, 0, 0, "Accepted connection to CLI\n");
196
197 debug_session = 0;
198 debug_tunnel = 0;
199 #ifdef RINGBUFFER
200 debug_rb_tail = ringbuffer->tail;
201 #endif
202 memset(&debug_flags, 0, sizeof(debug_flags));
203 debug_flags.critical = 1;
204
205 cli_loop(cli, sockfd, "l2tpns> ");
206
207 close(sockfd);
208 log(3, 0, 0, 0, "Closed CLI connection\n");
209 exit(0);
210 }
211
212 int cmd_show_session(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
213 {
214 int i;
215 time_t time_now;
216
217 time(&time_now);
218 if (argc > 0)
219 {
220 // Show individual session
221 for (i = 0; i < argc; i++)
222 {
223 unsigned int s;
224 s = atoi(argv[i]);
225 if (!s || s > MAXSESSION)
226 {
227 fprintf(w, "Invalid session id \"%s\"\r\n", argv[i]);
228 continue;
229 }
230 fprintf(w, "\r\nSession %d:\r\n", s);
231 fprintf(w, " User: %s\r\n", session[s].user[0] ? session[s].user : "none");
232 fprintf(w, " Calling Num: %s\r\n", session[s].calling);
233 fprintf(w, " Called Num: %s\r\n", session[s].called);
234 fprintf(w, " Tunnel ID: %d\r\n", session[s].tunnel);
235 fprintf(w, " IP address: %s\r\n", inet_toa(htonl(session[s].ip)));
236 fprintf(w, " HSD sid: %lu\r\n", session[s].sid);
237 fprintf(w, " Idle time: %u seconds\r\n", abs(time_now - session[s].last_packet));
238 fprintf(w, " Next Recv: %u\r\n", session[s].nr);
239 fprintf(w, " Next Send: %u\r\n", session[s].ns);
240 fprintf(w, " Bytes In/Out: %lu/%lu\r\n", (unsigned long)session[s].cin, (unsigned long)session[s].cout);
241 fprintf(w, " Pkts In/Out: %lu/%lu\r\n", (unsigned long)session[s].pin, (unsigned long)session[s].pout);
242 fprintf(w, " Radius Session: %u\r\n", session[s].radius);
243 fprintf(w, " Rx Speed: %lu\r\n", session[s].rx_connect_speed);
244 fprintf(w, " Tx Speed: %lu\r\n", session[s].tx_connect_speed);
245 fprintf(w, " Intercepted: %s\r\n", session[s].snoop ? "YES" : "no");
246 fprintf(w, " Throttled: %s\r\n", session[s].throttle ? "YES" : "no");
247 fprintf(w, " Walled Garden: %s\r\n", session[s].walled_garden ? "YES" : "no");
248 fprintf(w, " Filter Bucket: %s\r\n", session[s].tbf ? filter_buckets[session[s].tbf].handle : "none");
249 }
250 return CLI_OK;
251 }
252
253 // Show Summary
254 fprintf(w, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s\r\n",
255 "SID",
256 "TID",
257 "Username",
258 "IP",
259 "I",
260 "T",
261 "S",
262 "opened",
263 "downloaded",
264 "uploaded",
265 "idle",
266 "LAC",
267 "CLI");
268 for (i = 0; i < MAXSESSION; i++)
269 {
270 char *userip, *tunnelip;
271 if (!session[i].opened) continue;
272 userip = strdup(inet_toa(htonl(session[i].ip)));
273 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
274 fprintf(w, "%5d %4d %-32s %-15s %s %s %s %10lu %10lu %10lu %4u %-15s %s\r\n",
275 i,
276 session[i].tunnel,
277 session[i].user[0] ? session[i].user : "*",
278 userip,
279 (session[i].snoop) ? "Y" : "N",
280 (session[i].throttle) ? "Y" : "N",
281 (session[i].walled_garden) ? "Y" : "N",
282 (unsigned long)session[i].opened,
283 (unsigned long)session[i].cout,
284 (unsigned long)session[i].cin,
285 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
286 tunnelip,
287 session[i].calling[0] ? session[i].calling : "*");
288 if (userip) free(userip);
289 if (tunnelip) free(tunnelip);
290 }
291 return CLI_OK;
292 }
293
294 int cmd_show_tunnels(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
295 {
296 int i, x;
297 time_t time_now;
298
299 time(&time_now);
300 if (argc > 0)
301 {
302 // Show individual tunnel
303 for (i = 0; i < argc; i++)
304 {
305 char s[65535] = {0};
306 unsigned int t;
307 t = atoi(argv[i]);
308 if (!t || t > MAXTUNNEL)
309 {
310 fprintf(w, "Invalid tunnel id \"%s\"\r\n", argv[i]);
311 continue;
312 }
313 fprintf(w, "\r\nTunnel %d:\r\n", t);
314 fprintf(w, " Hostname: %s\r\n", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
315 fprintf(w, " Remote IP: %s\r\n", inet_toa(htonl(tunnel[t].ip)));
316 fprintf(w, " Remote Port: %d\r\n", tunnel[t].port);
317 fprintf(w, " Rx Window: %u\r\n", tunnel[t].window);
318 fprintf(w, " Next Recv: %u\r\n", tunnel[t].nr);
319 fprintf(w, " Next Send: %u\r\n", tunnel[t].ns);
320 fprintf(w, " Queue Len: %u\r\n", tunnel[t].controlc);
321 fprintf(w, " Last Packet Age:%u\r\n", (unsigned)(time_now - tunnel[t].last));
322
323 for (x = 0; x < MAXSESSION; x++)
324 if (session[x].tunnel == t && session[x].opened && !session[x].die)
325 sprintf(s, "%s%u ", s, x);
326 fprintf(w, " Sessions: %s\r\n", s);
327 }
328 return CLI_OK;
329 }
330
331 // Show tunnel summary
332 fprintf(w, "%s %s %s %s\r\n",
333 "TID",
334 "Hostname",
335 "IP",
336 "Sessions");
337 for (i = 0; i < MAXTUNNEL; i++)
338 {
339 int sessions = 0;
340 if (!tunnel[i].ip || tunnel[i].die || !tunnel[i].hostname[0]) continue;
341
342 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
343 fprintf(w, "%d %s %s %d\r\n",
344 i,
345 tunnel[i].hostname,
346 inet_toa(htonl(tunnel[i].ip)),
347 sessions);
348 }
349 return CLI_OK;
350 }
351
352 int cmd_show_users(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
353 {
354 int i;
355 for (i = 0; i < MAXSESSION; i++)
356 {
357 if (!session[i].opened) continue;
358 if (!session[i].user[0]) continue;
359 fprintf(w, "%s\r\n",
360 session[i].user);
361 }
362 return CLI_OK;
363 }
364
365 int cmd_show_counters(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
366 {
367 fprintf(w, "%-10s %-8s %-10s %-8s\r\n", "Ethernet", "Bytes", "Packets", "Errors");
368 fprintf(w, "%-10s %8lu %8lu %8lu\r\n", "RX",
369 GET_STAT(tap_rx_bytes),
370 GET_STAT(tap_rx_packets),
371 GET_STAT(tap_rx_errors));
372 fprintf(w, "%-10s %8lu %8lu %8lu\r\n", "TX",
373 GET_STAT(tap_tx_bytes),
374 GET_STAT(tap_tx_packets),
375 GET_STAT(tap_tx_errors));
376 fprintf(w, "\r\n");
377
378 fprintf(w, "%-10s %-8s %-10s %-8s %-8s\r\n", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
379 fprintf(w, "%-10s %8lu %8lu %8lu %8lu\r\n", "RX",
380 GET_STAT(tunnel_rx_bytes),
381 GET_STAT(tunnel_rx_packets),
382 GET_STAT(tunnel_rx_errors),
383 0L);
384 fprintf(w, "%-10s %8lu %8lu %8lu %8lu\r\n", "TX",
385 GET_STAT(tunnel_tx_bytes),
386 GET_STAT(tunnel_tx_packets),
387 GET_STAT(tunnel_rx_errors),
388 GET_STAT(tunnel_retries));
389 fprintf(w, "\r\n");
390
391 fprintf(w, "%-30s%-10s\r\n", "Counter", "Value");
392 fprintf(w, "-----------------------------------------\r\n");
393 fprintf(w, "%-30s%lu\r\n", "radius_retries", GET_STAT(radius_retries));
394 fprintf(w, "%-30s%lu\r\n", "arp_errors", GET_STAT(arp_errors));
395 fprintf(w, "%-30s%lu\r\n", "arp_replies", GET_STAT(arp_replies));
396 fprintf(w, "%-30s%lu\r\n", "arp_discarded", GET_STAT(arp_discarded));
397 fprintf(w, "%-30s%lu\r\n", "arp_sent", GET_STAT(arp_sent));
398 fprintf(w, "%-30s%lu\r\n", "arp_recv", GET_STAT(arp_recv));
399 fprintf(w, "%-30s%lu\r\n", "packets_snooped", GET_STAT(packets_snooped));
400 fprintf(w, "%-30s%lu\r\n", "tunnel_created", GET_STAT(tunnel_created));
401 fprintf(w, "%-30s%lu\r\n", "session_created", GET_STAT(session_created));
402 fprintf(w, "%-30s%lu\r\n", "tunnel_timeout", GET_STAT(tunnel_timeout));
403 fprintf(w, "%-30s%lu\r\n", "session_timeout", GET_STAT(session_timeout));
404 fprintf(w, "%-30s%lu\r\n", "radius_timeout", GET_STAT(radius_timeout));
405 fprintf(w, "%-30s%lu\r\n", "radius_overflow", GET_STAT(radius_overflow));
406 fprintf(w, "%-30s%lu\r\n", "tunnel_overflow", GET_STAT(tunnel_overflow));
407 fprintf(w, "%-30s%lu\r\n", "session_overflow", GET_STAT(session_overflow));
408 fprintf(w, "%-30s%lu\r\n", "ip_allocated", GET_STAT(ip_allocated));
409 fprintf(w, "%-30s%lu\r\n", "ip_freed", GET_STAT(ip_freed));
410
411 #ifdef STAT_CALLS
412 fprintf(w, "\n%-30s%-10s\r\n", "Counter", "Value");
413 fprintf(w, "-----------------------------------------\r\n");
414 fprintf(w, "%-30s%lu\r\n", "call_processtap", GET_STAT(call_processtap));
415 fprintf(w, "%-30s%lu\r\n", "call_processarp", GET_STAT(call_processarp));
416 fprintf(w, "%-30s%lu\r\n", "call_processipout", GET_STAT(call_processipout));
417 fprintf(w, "%-30s%lu\r\n", "call_processudp", GET_STAT(call_processudp));
418 fprintf(w, "%-30s%lu\r\n", "call_processpap", GET_STAT(call_processpap));
419 fprintf(w, "%-30s%lu\r\n", "call_processchap", GET_STAT(call_processchap));
420 fprintf(w, "%-30s%lu\r\n", "call_processlcp", GET_STAT(call_processlcp));
421 fprintf(w, "%-30s%lu\r\n", "call_processipcp", GET_STAT(call_processipcp));
422 fprintf(w, "%-30s%lu\r\n", "call_processipin", GET_STAT(call_processipin));
423 fprintf(w, "%-30s%lu\r\n", "call_processccp", GET_STAT(call_processccp));
424 fprintf(w, "%-30s%lu\r\n", "call_processrad", GET_STAT(call_processrad));
425 fprintf(w, "%-30s%lu\r\n", "call_sendarp", GET_STAT(call_sendarp));
426 fprintf(w, "%-30s%lu\r\n", "call_sendipcp", GET_STAT(call_sendipcp));
427 fprintf(w, "%-30s%lu\r\n", "call_sendchap", GET_STAT(call_sendchap));
428 fprintf(w, "%-30s%lu\r\n", "call_sessionbyip", GET_STAT(call_sessionbyip));
429 fprintf(w, "%-30s%lu\r\n", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
430 fprintf(w, "%-30s%lu\r\n", "call_tunnelsend", GET_STAT(call_tunnelsend));
431 fprintf(w, "%-30s%lu\r\n", "call_tunnelkill", GET_STAT(call_tunnelkill));
432 fprintf(w, "%-30s%lu\r\n", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
433 fprintf(w, "%-30s%lu\r\n", "call_sessionkill", GET_STAT(call_sessionkill));
434 fprintf(w, "%-30s%lu\r\n", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
435 fprintf(w, "%-30s%lu\r\n", "call_sessionsetup", GET_STAT(call_sessionsetup));
436 fprintf(w, "%-30s%lu\r\n", "call_assign_ip_address",GET_STAT(call_assign_ip_address));
437 fprintf(w, "%-30s%lu\r\n", "call_free_ip_address", GET_STAT(call_free_ip_address));
438 fprintf(w, "%-30s%lu\r\n", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
439 fprintf(w, "%-30s%lu\r\n", "call_radiussend", GET_STAT(call_radiussend));
440 fprintf(w, "%-30s%lu\r\n", "call_radiusretry", GET_STAT(call_radiusretry));
441 #endif
442 return CLI_OK;
443 }
444
445 int cmd_show_version(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
446 {
447 fprintf(w, "L2TPNS %s\r\n", VERSION);
448 fprintf(w, "ID: %s\r\n", rcs_id);
449 return CLI_OK;
450 }
451 int cmd_show_pool(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
452 {
453 int i;
454 int used = 0, free = 0;
455
456 fprintf(w, "%-15s %4s %8s %s\r\n", "IP Address", "Used", "Session", "User");
457 for (i = 0; i < MAXIPPOOL; i++)
458 {
459 sessionidt s = 0;
460
461 if (!ip_address_pool[i].address) continue;
462 if (ip_address_pool[i].assigned)
463 {
464 used++;
465 s = sessionbyip(ip_address_pool[i].address);
466 }
467 else
468 {
469 free++;
470 }
471
472 fprintf(w, "%-15s %4s %8d %s\r\n",
473 inet_toa(ip_address_pool[i].address),
474 (s) ? "Y" : "N",
475 s,
476 session[s].user);
477 }
478 fprintf(w, "\r\nFree: %d\r\nUsed: %d\r\n", free, used);
479 return CLI_OK;
480 }
481 int cmd_show_banana(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
482 {
483 fputs(" _\r\n"
484 "//\\\r\n"
485 "V \\\r\n"
486 " \\ \\_\r\n"
487 " \\,'.`-.\r\n"
488 " |\\ `. `.\r\n"
489 " ( \\ `. `-. _,.-:\\\r\n"
490 " \\ \\ `. `-._ __..--' ,-';/\r\n"
491 " \\ `. `-. `-..___..---' _.--' ,'/\r\n"
492 " `. `. `-._ __..--' ,' /\r\n"
493 " `. `-_ ``--..'' _.-' ,'\r\n"
494 " `-_ `-.___ __,--' ,'\r\n"
495 " `-.__ `----\"\"\" __.-'\r\n"
496 "hh `--..____..--'\r\n", w);
497
498 return CLI_OK;
499 }
500
501 int cmd_clear_counters(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
502 {
503 fprintf(w, "Counters cleared\r\n");
504 SET_STAT(last_reset, time(NULL));
505 return CLI_OK;
506 }
507
508 int cmd_drop_user(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
509 {
510 int i;
511 sessionidt s;
512
513 if (!argc)
514 {
515 fprintf(w, "Specify a user to drop\r\n");
516 return CLI_OK;
517 }
518 for (i = 0; i < argc; i++)
519 {
520 if (strchr(argv[i], '?'))
521 {
522 fprintf(w, "username ...");
523 return CLI_OK;
524 }
525 }
526
527 for (i = 0; i < argc; i++)
528 {
529 if (!(s = sessionbyuser(argv[i])))
530 {
531 fprintf(w, "User %s is not connected\r\n", argv[i]);
532 continue;
533 }
534
535 if (session[s].ip && session[s].opened && !session[s].die)
536 {
537 int x;
538
539 fprintf(w, "Dropping user %s\r\n", session[s].user);
540 for (x = 0; x < MAXSESSION; x++)
541 {
542 if (!cli_session_kill[x])
543 {
544 cli_session_kill[x] = s;
545 break;
546 }
547 }
548 }
549 }
550
551 return CLI_OK;
552 }
553
554 int cmd_drop_tunnel(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
555 {
556 int i;
557 tunnelidt tid;
558
559 if (!argc)
560 {
561 fprintf(w, "Specify a tunnel to drop\r\n");
562 return CLI_OK;
563 }
564 for (i = 0; i < argc; i++)
565 {
566 if (strchr(argv[i], '?'))
567 {
568 fprintf(w, "tunnel_id ...");
569 return CLI_OK;
570 }
571 }
572
573 for (i = 0; i < argc; i++)
574 {
575 int x;
576
577 if ((tid = atol(argv[i])) <= 0 || (tid > MAXTUNNEL))
578 {
579 fprintf(w, "Invalid tunnel ID (%d - %d)\r\n", 0, MAXTUNNEL);
580 continue;
581 }
582
583 if (!tunnel[tid].ip)
584 {
585 fprintf(w, "Tunnel %d is not connected\r\n", tid);
586 continue;
587 }
588
589 if (tunnel[tid].die)
590 {
591 fprintf(w, "Tunnel %d is already being shut down\r\n", tid);
592 continue;
593 }
594
595 for (x = 0; x < MAXTUNNEL; x++)
596 {
597 if (!cli_tunnel_kill[x])
598 {
599 cli_tunnel_kill[x] = tid;
600 fprintf(w, "Tunnel %d shut down (%s)\r\n", tid, tunnel[tid].hostname);
601 break;
602 }
603 }
604 }
605
606 return CLI_OK;
607 }
608
609 int cmd_drop_session(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
610 {
611 int i;
612 sessionidt s;
613
614 if (!argc)
615 {
616 fprintf(w, "Specify a session id to drop\r\n");
617 return CLI_OK;
618 }
619 for (i = 0; i < argc; i++)
620 {
621 if (strchr(argv[i], '?'))
622 {
623 fprintf(w, "session_id ...");
624 return CLI_OK;
625 }
626 }
627
628 for (i = 0; i < argc; i++)
629 {
630 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
631 {
632 fprintf(w, "Invalid session ID (%d - %d)\r\n", 0, MAXSESSION);
633 continue;
634 }
635
636 if (session[s].ip && session[s].opened && !session[s].die)
637 {
638 int x;
639 for (x = 0; x < MAXSESSION; x++)
640 {
641 if (!cli_session_kill[x])
642 {
643 cli_session_kill[x] = s;
644 break;
645 }
646 }
647 fprintf(w, "Dropping session %d\r\n", s);
648 }
649 else
650 {
651 fprintf(w, "Session %d is not active.\r\n", s);
652 }
653 }
654
655 return CLI_OK;
656 }
657
658 int cmd_snoop(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
659 {
660 int i;
661 sessionidt s;
662
663 if (!argc)
664 {
665 fprintf(w, "Specify a user\r\n");
666 return CLI_OK;
667 }
668 for (i = 0; i < argc; i++)
669 {
670 if (strchr(argv[i], '?'))
671 {
672 fprintf(w, "username ...");
673 return CLI_OK;
674 }
675 }
676
677 for (i = 0; i < argc; i++)
678 {
679 if (!(s = sessionbyuser(argv[i])))
680 {
681 fprintf(w, "User %s is not connected\r\n", argv[i]);
682 continue;
683 }
684 session[s].snoop = 1;
685
686 fprintf(w, "Snooping user %s\r\n", argv[i]);
687 }
688 return CLI_OK;
689 }
690
691 int cmd_no_snoop(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
692 {
693 int i;
694 sessionidt s;
695
696 if (!argc)
697 {
698 fprintf(w, "Specify a user\r\n");
699 return CLI_OK;
700 }
701 for (i = 0; i < argc; i++)
702 {
703 if (strchr(argv[i], '?'))
704 {
705 fprintf(w, "username ...");
706 return CLI_OK;
707 }
708 }
709
710 for (i = 0; i < argc; i++)
711 {
712 if (!(s = sessionbyuser(argv[i])))
713 {
714 fprintf(w, "User %s is not connected\r\n", argv[i]);
715 continue;
716 }
717 session[s].snoop = 0;
718
719 fprintf(w, "Not snooping user %s\r\n", argv[i]);
720 }
721 return CLI_OK;
722 }
723
724 int cmd_throttle(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
725 {
726 int i;
727 sessionidt s;
728
729 if (!argc)
730 {
731 fprintf(w, "Specify a user\r\n");
732 return CLI_OK;
733 }
734 for (i = 0; i < argc; i++)
735 {
736 if (strchr(argv[i], '?'))
737 {
738 fprintf(w, "username ...");
739 return CLI_OK;
740 }
741 }
742
743 for (i = 0; i < argc; i++)
744 {
745 if (!(s = sessionbyuser(argv[i])))
746 {
747 fprintf(w, "User %s is not connected\r\n", argv[i]);
748 continue;
749 }
750 throttle_session(s, 1);
751
752 fprintf(w, "throttling user %s\r\n", argv[i]);
753 }
754 return CLI_OK;
755 }
756
757 int cmd_no_throttle(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
758 {
759 int i;
760 sessionidt s;
761
762 if (!argc)
763 {
764 fprintf(w, "Specify a user\r\n");
765 return CLI_OK;
766 }
767 for (i = 0; i < argc; i++)
768 {
769 if (strchr(argv[i], '?'))
770 {
771 fprintf(w, "username ...");
772 return CLI_OK;
773 }
774 }
775
776 for (i = 0; i < argc; i++)
777 {
778 if (!(s = sessionbyuser(argv[i])))
779 {
780 fprintf(w, "User %s is not connected\r\n", argv[i]);
781 continue;
782 }
783 throttle_session(s, 0);
784
785 fprintf(w, "unthrottling user %s\r\n", argv[i]);
786 }
787 return CLI_OK;
788 }
789
790 int cmd_debug(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
791 {
792 int i;
793
794 if (!argc)
795 {
796 fprintf(w, "Currently debugging: ");
797 if (debug_flags.critical) fprintf(w, "critical ");
798 if (debug_flags.error) fprintf(w, "error ");
799 if (debug_flags.warning) fprintf(w, "warning ");
800 if (debug_flags.info) fprintf(w, "info ");
801 if (debug_flags.calls) fprintf(w, "calls ");
802 if (debug_flags.data) fprintf(w, "data ");
803 fprintf(w, "\r\n");
804 return CLI_OK;
805 }
806
807 for (i = 0; i < argc; i++)
808 {
809 if (*argv[i] == '?')
810 {
811 fprintf(w, "Possible debugging states are:\r\n");
812 fprintf(w, " critical\r\n");
813 fprintf(w, " error\r\n");
814 fprintf(w, " warning\r\n");
815 fprintf(w, " info\r\n");
816 fprintf(w, " calls\r\n");
817 fprintf(w, " data\r\n");
818 return CLI_OK;
819 }
820 }
821
822 for (i = 0; i < argc; i++)
823 {
824 if (strcasecmp(argv[i], "critical") == 0) debug_flags.critical = 1;
825 if (strcasecmp(argv[i], "error") == 0) debug_flags.error = 1;
826 if (strcasecmp(argv[i], "warning") == 0) debug_flags.warning = 1;
827 if (strcasecmp(argv[i], "info") == 0) debug_flags.info = 1;
828 if (strcasecmp(argv[i], "calls") == 0) debug_flags.calls = 1;
829 if (strcasecmp(argv[i], "data") == 0) debug_flags.data = 1;
830 if (strcasecmp(argv[i], "all") == 0)
831 {
832 memset(&debug_flags, 1, sizeof(debug_flags));
833 debug_flags.data = 0;
834 }
835 }
836
837 return CLI_OK;
838 }
839
840 int cmd_no_debug(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
841 {
842 int i;
843
844 for (i = 0; i < argc; i++)
845 {
846 if (strcasecmp(argv[i], "critical") == 0) debug_flags.critical = 0;
847 if (strcasecmp(argv[i], "error") == 0) debug_flags.error = 0;
848 if (strcasecmp(argv[i], "warning") == 0) debug_flags.warning = 0;
849 if (strcasecmp(argv[i], "info") == 0) debug_flags.info = 0;
850 if (strcasecmp(argv[i], "calls") == 0) debug_flags.calls = 0;
851 if (strcasecmp(argv[i], "data") == 0) debug_flags.data = 0;
852 if (strcasecmp(argv[i], "all") == 0) memset(&debug_flags, 0, sizeof(debug_flags));
853 }
854
855 return CLI_OK;
856 }
857
858 int cmd_watch_session(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
859 {
860 sessionidt s;
861
862 if (argc != 1)
863 {
864 fprintf(w, "Specify a single session to debug (0 to disable)\r\n");
865 return CLI_OK;
866 }
867 s = atoi(argv[0]);
868
869 if (debug_session)
870 fprintf(w, "No longer debugging session %d\r\n", debug_session);
871
872 if (s) fprintf(w, "Debugging session %d.\r\n", s);
873 debug_session = s;
874
875 return CLI_OK;
876 }
877
878 int cmd_watch_tunnel(struct cli_def *cli, FILE *w, char *command, char **argv, int argc)
879 {
880 tunnelidt s;
881
882 if (argc != 1)
883 {
884 fprintf(w, "Specify a single tunnel to debug (0 to disable)\r\n");
885 return CLI_OK;
886 }
887 s = atoi(argv[0]);
888
889 if (debug_tunnel)
890 fprintf(w, "No longer debugging tunnel %d\r\n", debug_tunnel);
891
892 if (s) fprintf(w, "Debugging tunnel %d.\r\n", s);
893 debug_tunnel = s;
894
895 return CLI_OK;
896 }
897
898 int regular_stuff(struct cli_def *cli, FILE *w)
899 {
900 int i = debug_rb_tail;
901
902 #ifdef RINGBUFFER
903 while (i != ringbuffer->tail)
904 {
905 int show_message = 0;
906
907 if (*ringbuffer->buffer[i].message)
908 {
909 // Always show messages if we are doing general debug
910 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
911 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
912 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
913 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
914 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
915 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
916 }
917
918 if (show_message)
919 {
920 ipt address = ntohl(ringbuffer->buffer[i].address);
921 char *ipaddr;
922 struct in_addr addr;
923
924 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
925 ipaddr = inet_ntoa(addr);
926
927 fprintf(w, "%s-%s-%u-%u %s\r",
928 debug_levels[(int)ringbuffer->buffer[i].level],
929 ipaddr,
930 ringbuffer->buffer[i].tunnel,
931 ringbuffer->buffer[i].session,
932 ringbuffer->buffer[i].message);
933 }
934
935 if (++i == ringbuffer->tail) break;
936 if (i == RINGBUFFER_SIZE) i = 0;
937 }
938
939 debug_rb_tail = ringbuffer->tail;
940 #endif
941 return CLI_OK;
942 }
943
944 #endif