1 // L2TPNS Command Line Interface
2 // $Id: cli.c,v 1.4 2004-05-24 04:12:02 fred_nerk Exp $
13 #include <arpa/inet.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
23 extern tunnelt
*tunnel
;
24 extern sessiont
*session
;
25 extern radiust
*radius
;
26 extern ippoolt
*ip_address_pool
;
27 extern struct Tstats
*_statistics
;
29 struct cli_def
*cli
= NULL
;
31 extern int clifd
, udpfd
, tapfd
, snoopfd
, ifrfd
, cluster_sockfd
;
33 extern sessionidt
*cli_session_kill
;
34 extern tunnelidt
*cli_tunnel_kill
;
35 extern tbft
*filter_buckets
;
36 extern struct configt
*config
;
37 extern struct config_descriptt config_values
[];
38 extern char hostname
[];
40 extern struct Tringbuffer
*ringbuffer
;
43 char *rcs_id
= "$Id: cli.c,v 1.4 2004-05-24 04:12:02 fred_nerk Exp $";
45 char *debug_levels
[] = {
69 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
70 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
71 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
72 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
73 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
74 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
75 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 int cmd_no_debug(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
);
101 struct cli_command
*c
;
103 struct sockaddr_in addr
;
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 or details of selected user");
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 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, "List all token bucket filters in use");
120 cli_register_command(cli
, c
, "counters", cmd_show_counters
, "Display all the internal counters and running totals");
122 c
= cli_register_command(cli
, NULL
, "clear", NULL
, NULL
);
123 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, "Clear internal counters");
126 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, "Show uptime and bandwidth utilisation");
128 c
= cli_register_command(cli
, NULL
, "write", NULL
, NULL
);
129 cli_register_command(cli
, c
, "memory", cmd_write_memory
, "Save the running config to flash");
130 cli_register_command(cli
, c
, "terminal", cmd_show_run
, "Show the running config");
132 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, "Temporarily enable interception for a user");
133 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, "Temporarily enable throttling for a user");
135 c
= cli_register_command(cli
, NULL
, "no", NULL
, NULL
);
136 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, "Temporarily disable interception for a user");
137 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, "Temporarily disable throttling for a user");
138 cli_register_command(cli
, c
, "debug", cmd_no_debug
, "Turn off logging of a certain level of debugging");
140 c
= cli_register_command(cli
, NULL
, "drop", NULL
, NULL
);
141 cli_register_command(cli
, c
, "user", cmd_drop_user
, "Disconnect a user");
142 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, "Disconnect a tunnel and all sessions on that tunnel");
143 cli_register_command(cli
, c
, "session", cmd_drop_session
, "Disconnect a session");
145 cli_register_command(cli
, NULL
, "debug", cmd_debug
, "Set the level of logging that is shown on the console");
147 c
= cli_register_command(cli
, NULL
, "load", NULL
, NULL
);
148 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, "Load a plugin");
150 c
= cli_register_command(cli
, NULL
, "remove", NULL
, NULL
);
151 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, "Remove a plugin");
153 cli_register_command(cli
, NULL
, "set", cmd_set
, "Set a configuration variable");
155 // Enable regular processing
156 cli_regular(cli
, regular_stuff
);
158 if (!(f
= fopen(CLIUSERS
, "r")))
160 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
164 while (fgets(buf
, 4096, f
))
167 if (*buf
== '#') continue;
168 if ((p
= strchr(buf
, '\r'))) *p
= 0;
169 if ((p
= strchr(buf
, '\n'))) *p
= 0;
171 if (!(p
= strchr((char *)buf
, ':'))) continue;
173 cli_allow_user(cli
, buf
, p
);
174 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
179 memset(&addr
, 0, sizeof(addr
));
180 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
181 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
184 // Set cli fd as non-blocking
185 flags
= fcntl(clifd
, F_GETFL
, 0);
186 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
188 addr
.sin_family
= AF_INET
;
189 addr
.sin_port
= htons(23);
190 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
192 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
198 void cli_do(int sockfd
)
205 if (udpfd
) close(udpfd
); udpfd
= 0;
206 if (tapfd
) close(tapfd
); tapfd
= 0;
207 if (snoopfd
) close(snoopfd
); snoopfd
= 0;
208 for (i
= 0; i
< config
->num_radfds
; i
++)
209 if (radfds
[i
]) close(radfds
[i
]);
210 if (ifrfd
) close(ifrfd
); ifrfd
= 0;
211 if (cluster_sockfd
) close(cluster_sockfd
); cluster_sockfd
= 0;
212 if (clifd
) close(clifd
); clifd
= 0;
214 signal(SIGPIPE
, SIG_DFL
);
215 signal(SIGCHLD
, SIG_DFL
);
216 signal(SIGHUP
, SIG_DFL
);
217 signal(SIGUSR1
, SIG_DFL
);
218 signal(SIGQUIT
, SIG_DFL
);
219 signal(SIGKILL
, SIG_DFL
);
220 signal(SIGALRM
, SIG_DFL
);
222 log(3, 0, 0, 0, "Accepted connection to CLI\n");
227 debug_rb_tail
= ringbuffer
->tail
;
229 memset(&debug_flags
, 0, sizeof(debug_flags
));
230 debug_flags
.critical
= 1;
234 snprintf(prompt
, 1005, "%s> ", hostname
);
235 cli_loop(cli
, sockfd
, prompt
);
239 log(3, 0, 0, 0, "Closed CLI connection\n");
243 void cli_print_log(struct cli_def
*cli
, char *string
)
245 log(3, 0, 0, 0, "%s\n", string
);
248 void cli_do_file(FILE *fh
)
250 log(3, 0, 0, 0, "Reading configuration file\n");
251 cli_print_callback(cli
, cli_print_log
);
253 cli_print_callback(cli
, NULL
);
256 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
264 // Show individual session
265 for (i
= 0; i
< argc
; i
++)
269 if (!s
|| s
> MAXSESSION
)
271 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
274 cli_print(cli
, "\r\nSession %d:", s
);
275 cli_print(cli
, " User: %s", session
[s
].user
[0] ? session
[s
].user
: "none");
276 cli_print(cli
, " Calling Num: %s", session
[s
].calling
);
277 cli_print(cli
, " Called Num: %s", session
[s
].called
);
278 cli_print(cli
, " Tunnel ID: %d", session
[s
].tunnel
);
279 cli_print(cli
, " IP address: %s", inet_toa(htonl(session
[s
].ip
)));
280 cli_print(cli
, " HSD sid: %lu", session
[s
].sid
);
281 cli_print(cli
, " Idle time: %u seconds", abs(time_now
- session
[s
].last_packet
));
282 cli_print(cli
, " Next Recv: %u", session
[s
].nr
);
283 cli_print(cli
, " Next Send: %u", session
[s
].ns
);
284 cli_print(cli
, " Bytes In/Out: %lu/%lu", (unsigned long)session
[s
].cin
, (unsigned long)session
[s
].total_cout
);
285 cli_print(cli
, " Pkts In/Out: %lu/%lu", (unsigned long)session
[s
].pin
, (unsigned long)session
[s
].pout
);
286 cli_print(cli
, " MRU: %d", session
[s
].mru
);
287 cli_print(cli
, " Radius Session: %u", session
[s
].radius
);
288 cli_print(cli
, " Rx Speed: %lu", session
[s
].rx_connect_speed
);
289 cli_print(cli
, " Tx Speed: %lu", session
[s
].tx_connect_speed
);
290 cli_print(cli
, " Intercepted: %s", session
[s
].snoop
? "YES" : "no");
291 cli_print(cli
, " Throttled: %s", session
[s
].throttle
? "YES" : "no");
292 cli_print(cli
, " Walled Garden: %s", session
[s
].walled_garden
? "YES" : "no");
293 cli_print(cli
, " Filter Bucket: %s", session
[s
].tbf
? filter_buckets
[session
[s
].tbf
].handle
: "none");
299 cli_print(cli
, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
313 for (i
= 1; i
< MAXSESSION
; i
++)
315 char *userip
, *tunnelip
;
316 if (!session
[i
].opened
) continue;
317 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
318 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
319 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
322 session
[i
].user
[0] ? session
[i
].user
: "*",
324 (session
[i
].snoop
) ? "Y" : "N",
325 (session
[i
].throttle
) ? "Y" : "N",
326 (session
[i
].walled_garden
) ? "Y" : "N",
327 abs(time_now
- (unsigned long)session
[i
].opened
),
328 (unsigned long)session
[i
].total_cout
,
329 (unsigned long)session
[i
].total_cin
,
330 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
332 session
[i
].calling
[0] ? session
[i
].calling
: "*");
333 if (userip
) free(userip
);
334 if (tunnelip
) free(tunnelip
);
339 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
341 int i
, x
, show_all
= 0;
353 if (strcmp(argv
[0], "all") == 0)
359 // Show individual tunnel
360 for (i
= 0; i
< argc
; i
++)
365 if (!t
|| t
> MAXTUNNEL
)
367 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
370 cli_print(cli
, "\r\nTunnel %d:", t
);
371 cli_print(cli
, " State: %s", states
[tunnel
[t
].state
]);
372 cli_print(cli
, " Hostname: %s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
373 cli_print(cli
, " Remote IP: %s", inet_toa(htonl(tunnel
[t
].ip
)));
374 cli_print(cli
, " Remote Port: %d", tunnel
[t
].port
);
375 cli_print(cli
, " Rx Window: %u", tunnel
[t
].window
);
376 cli_print(cli
, " Next Recv: %u", tunnel
[t
].nr
);
377 cli_print(cli
, " Next Send: %u", tunnel
[t
].ns
);
378 cli_print(cli
, " Queue Len: %u", tunnel
[t
].controlc
);
379 cli_print(cli
, " Last Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
381 for (x
= 0; x
< MAXSESSION
; x
++)
382 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
383 sprintf(s
, "%s%u ", s
, x
);
384 cli_print(cli
, " Sessions: %s", s
);
390 // Show tunnel summary
391 cli_print(cli
, "%s %s %s %s %s",
397 for (i
= 1; i
< MAXTUNNEL
; i
++)
400 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
|| !tunnel
[i
].hostname
[0])) continue;
402 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
403 cli_print(cli
, "%d %s %s %s %d",
405 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
406 inet_toa(htonl(tunnel
[i
].ip
)),
407 states
[tunnel
[i
].state
],
413 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
419 for (i
= 0; i
< MAXSESSION
; i
++)
421 if (!session
[i
].opened
) continue;
422 if (!session
[i
].user
[0]) continue;
426 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
428 if (strcmp(argv
[j
], session
[i
].user
) == 0)
430 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
431 sargv
[sargc
] = sid
[sargc
];
439 cli_print(cli
, "%s", session
[i
].user
);
443 return cmd_show_session(cli
, "users", sargv
, sargc
);
448 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
450 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
451 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
452 GET_STAT(tap_rx_bytes
),
453 GET_STAT(tap_rx_packets
),
454 GET_STAT(tap_rx_errors
));
455 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
456 GET_STAT(tap_tx_bytes
),
457 GET_STAT(tap_tx_packets
),
458 GET_STAT(tap_tx_errors
));
461 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
462 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
463 GET_STAT(tunnel_rx_bytes
),
464 GET_STAT(tunnel_rx_packets
),
465 GET_STAT(tunnel_rx_errors
),
467 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
468 GET_STAT(tunnel_tx_bytes
),
469 GET_STAT(tunnel_tx_packets
),
470 GET_STAT(tunnel_tx_errors
),
471 GET_STAT(tunnel_retries
));
474 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
475 cli_print(cli
, "-----------------------------------------");
476 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
477 cli_print(cli
, "%-30s%lu", "arp_errors", GET_STAT(arp_errors
));
478 cli_print(cli
, "%-30s%lu", "arp_replies", GET_STAT(arp_replies
));
479 cli_print(cli
, "%-30s%lu", "arp_discarded", GET_STAT(arp_discarded
));
480 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
481 cli_print(cli
, "%-30s%lu", "arp_recv", GET_STAT(arp_recv
));
482 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
483 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
484 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
485 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
486 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
487 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
488 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
489 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
490 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
491 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
492 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
495 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
496 cli_print(cli
, "-----------------------------------------");
497 cli_print(cli
, "%-30s%lu", "call_processtap", GET_STAT(call_processtap
));
498 cli_print(cli
, "%-30s%lu", "call_processarp", GET_STAT(call_processarp
));
499 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
500 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
501 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
502 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
503 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
504 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
505 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
506 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
507 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
508 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
509 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
510 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
511 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
512 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
513 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
514 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
515 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
516 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
517 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
518 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
519 cli_print(cli
, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address
));
520 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
521 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
522 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
523 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
528 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
530 cli_print(cli
, "L2TPNS %s", VERSION
);
531 cli_print(cli
, "ID: %s", rcs_id
);
535 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
538 int used
= 0, free
= 0, show_all
= 0;
541 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
545 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
546 for (i
= 0; i
< MAXIPPOOL
; i
++)
548 if (!ip_address_pool
[i
].address
) continue;
549 if (ip_address_pool
[i
].assigned
)
551 cli_print(cli
, "%-15s Y %8d %s",
552 inet_toa(ip_address_pool
[i
].address
), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
558 if (ip_address_pool
[i
].last
)
559 cli_print(cli
, "%-15s N %8s [%s] %ds",
560 inet_toa(ip_address_pool
[i
].address
), "",
561 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
563 cli_print(cli
, "%-15s N", inet_toa(ip_address_pool
[i
].address
));
570 cli_print(cli
, "(Not displaying unused addresses)");
572 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
576 void print_save_config(struct cli_def
*cli
, char *string
)
579 fprintf(save_config_fh
, "%s\n", string
);
582 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
584 if ((save_config_fh
= fopen(config
->config_file
, "w")))
586 cli_print(cli
, "Writing configuration");
587 cli_print_callback(cli
, print_save_config
);
588 cmd_show_run(cli
, command
, argv
, argc
);
589 cli_print_callback(cli
, NULL
);
590 fclose(save_config_fh
);
595 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
600 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
604 cli_print(cli
, "# Current configuration:");
606 for (i
= 0; config_values
[i
].key
; i
++)
608 void *value
= ((void *)config
) + config_values
[i
].offset
;
609 if (config_values
[i
].type
== STRING
)
610 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
611 else if (config_values
[i
].type
== IP
)
612 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
613 else if (config_values
[i
].type
== SHORT
)
614 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
615 else if (config_values
[i
].type
== BOOL
)
616 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
617 else if (config_values
[i
].type
== INT
)
618 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
619 else if (config_values
[i
].type
== UNSIGNED_LONG
)
620 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
623 cli_print(cli
, "# Plugins");
624 for (i
= 0; i
< MAXPLUGINS
; i
++)
626 if (*config
->plugins
[i
])
628 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
632 cli_print(cli
, "# end");
636 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
647 int i
, free
= 0, used
= 0, show_all
= 0;
650 cli_print(cli
, "%6s%5s%6s%9s%9s%4s", "Radius", "Sock", "State", "Session", "Retry", "Try");
654 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
657 for (i
= 1; i
< MAXRADIUS
; i
++)
659 if (radius
[i
].state
== RADIUSNULL
)
664 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
666 cli_print(cli
, "%6d%5d%6s%9d%9u%4d",
669 states
[radius
[i
].state
],
675 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
680 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
683 cli_print(cli
, "Plugins currently loaded:");
684 for (i
= 0; i
< MAXPLUGINS
; i
++)
686 if (*config
->plugins
[i
])
688 cli_print(cli
, " %s", config
->plugins
[i
]);
694 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
697 cli_print(cli
, "Token bucket filters:");
698 cli_print(cli
, "%-6s %8s %-4s", "ID", "Handle", "Used");
699 for (i
= 0; i
< MAXSESSION
; i
++)
701 if (!*filter_buckets
[i
].handle
)
704 cli_print(cli
, "%-6d %8s %c",
706 filter_buckets
[i
].handle
,
707 (filter_buckets
[i
].in_use
) ? 'Y' : 'N');
712 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
714 cli_print(cli
, " _\n"
720 " ( \\ `. `-. _,.-:\\\n"
721 " \\ \\ `. `-._ __..--' ,-';/\n"
722 " \\ `. `-. `-..___..---' _.--' ,'/\n"
723 " `. `. `-._ __..--' ,' /\n"
724 " `. `-_ ``--..'' _.-' ,'\n"
725 " `-_ `-.___ __,--' ,'\n"
726 " `-.__ `----\"\"\" __.-'\n"
727 "hh `--..____..--'");
732 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
734 cli_print(cli
, "Counters cleared");
735 SET_STAT(last_reset
, time(NULL
));
739 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
746 cli_print(cli
, "Specify a user to drop");
749 for (i
= 0; i
< argc
; i
++)
751 if (strchr(argv
[i
], '?'))
753 cli_print(cli
, "username ...");
758 for (i
= 0; i
< argc
; i
++)
760 if (!(s
= sessionbyuser(argv
[i
])))
762 cli_print(cli
, "User %s is not connected", argv
[i
]);
766 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
770 cli_print(cli
, "Dropping user %s", session
[s
].user
);
771 for (x
= 0; x
< MAXSESSION
; x
++)
773 if (!cli_session_kill
[x
])
775 cli_session_kill
[x
] = s
;
785 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
792 cli_print(cli
, "Specify a tunnel to drop");
795 for (i
= 0; i
< argc
; i
++)
797 if (strchr(argv
[i
], '?'))
799 cli_print(cli
, "tunnel_id ...");
804 for (i
= 0; i
< argc
; i
++)
808 if ((tid
= atol(argv
[i
])) <= 0 || (tid
> MAXTUNNEL
))
810 cli_print(cli
, "Invalid tunnel ID (%d - %d)", 0, MAXTUNNEL
);
816 cli_print(cli
, "Tunnel %d is not connected", tid
);
822 cli_print(cli
, "Tunnel %d is already being shut down", tid
);
826 for (x
= 0; x
< MAXTUNNEL
; x
++)
828 if (!cli_tunnel_kill
[x
])
830 cli_tunnel_kill
[x
] = tid
;
831 cli_print(cli
, "Tunnel %d shut down (%s)", tid
, tunnel
[tid
].hostname
);
840 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
847 cli_print(cli
, "Specify a session id to drop");
850 for (i
= 0; i
< argc
; i
++)
852 if (strchr(argv
[i
], '?'))
854 cli_print(cli
, "session_id ...");
859 for (i
= 0; i
< argc
; i
++)
861 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
863 cli_print(cli
, "Invalid session ID (%d - %d)", 0, MAXSESSION
);
867 if (session
[s
].opened
&& !session
[s
].die
)
870 for (x
= 0; x
< MAXSESSION
; x
++)
872 if (!cli_session_kill
[x
])
874 cli_session_kill
[x
] = s
;
878 cli_print(cli
, "Dropping session %d", s
);
882 cli_print(cli
, "Session %d is not active.", s
);
889 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
896 cli_print(cli
, "Specify a user");
899 for (i
= 0; i
< argc
; i
++)
901 if (strchr(argv
[i
], '?'))
903 cli_print(cli
, "username ...");
908 for (i
= 0; i
< argc
; i
++)
910 if (!(s
= sessionbyuser(argv
[i
])))
912 cli_print(cli
, "User %s is not connected", argv
[i
]);
915 session
[s
].snoop
= 1;
917 cli_print(cli
, "Snooping user %s", argv
[i
]);
922 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
929 cli_print(cli
, "Specify a user");
932 for (i
= 0; i
< argc
; i
++)
934 if (strchr(argv
[i
], '?'))
936 cli_print(cli
, "username ...");
941 for (i
= 0; i
< argc
; i
++)
943 if (!(s
= sessionbyuser(argv
[i
])))
945 cli_print(cli
, "User %s is not connected", argv
[i
]);
948 session
[s
].snoop
= 0;
950 cli_print(cli
, "Not snooping user %s", argv
[i
]);
955 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
962 cli_print(cli
, "Specify a user");
965 for (i
= 0; i
< argc
; i
++)
967 if (strchr(argv
[i
], '?'))
969 cli_print(cli
, "username ...");
974 for (i
= 0; i
< argc
; i
++)
976 if (!(s
= sessionbyuser(argv
[i
])))
978 cli_print(cli
, "User %s is not connected", argv
[i
]);
981 if (!throttle_session(s
, 1))
982 cli_print(cli
, "error throttling %s", argv
[i
]);
984 cli_print(cli
, "throttling user %s", argv
[i
]);
989 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
996 cli_print(cli
, "Specify a user");
999 for (i
= 0; i
< argc
; i
++)
1001 if (strchr(argv
[i
], '?'))
1003 cli_print(cli
, "username ...");
1008 for (i
= 0; i
< argc
; i
++)
1010 if (!(s
= sessionbyuser(argv
[i
])))
1012 cli_print(cli
, "User %s is not connected", argv
[i
]);
1015 throttle_session(s
, 0);
1017 cli_print(cli
, "unthrottling user %s", argv
[i
]);
1022 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1028 cli_print(cli
, "Currently debugging: ");
1029 if (debug_flags
.critical
) cli_print(cli
, "critical ");
1030 if (debug_flags
.error
) cli_print(cli
, "error ");
1031 if (debug_flags
.warning
) cli_print(cli
, "warning ");
1032 if (debug_flags
.info
) cli_print(cli
, "info ");
1033 if (debug_flags
.calls
) cli_print(cli
, "calls ");
1034 if (debug_flags
.data
) cli_print(cli
, "data ");
1039 for (i
= 0; i
< argc
; i
++)
1041 if (*argv
[i
] == '?')
1043 cli_print(cli
, "Possible debugging states are:");
1044 cli_print(cli
, " critical");
1045 cli_print(cli
, " error");
1046 cli_print(cli
, " warning");
1047 cli_print(cli
, " info");
1048 cli_print(cli
, " calls");
1049 cli_print(cli
, " data");
1054 for (i
= 0; i
< argc
; i
++)
1056 if (strcasecmp(argv
[i
], "critical") == 0) debug_flags
.critical
= 1;
1057 if (strcasecmp(argv
[i
], "error") == 0) debug_flags
.error
= 1;
1058 if (strcasecmp(argv
[i
], "warning") == 0) debug_flags
.warning
= 1;
1059 if (strcasecmp(argv
[i
], "info") == 0) debug_flags
.info
= 1;
1060 if (strcasecmp(argv
[i
], "calls") == 0) debug_flags
.calls
= 1;
1061 if (strcasecmp(argv
[i
], "data") == 0) debug_flags
.data
= 1;
1062 if (strcasecmp(argv
[i
], "all") == 0)
1064 memset(&debug_flags
, 1, sizeof(debug_flags
));
1065 debug_flags
.data
= 0;
1072 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1076 for (i
= 0; i
< argc
; i
++)
1078 if (strcasecmp(argv
[i
], "critical") == 0) debug_flags
.critical
= 0;
1079 if (strcasecmp(argv
[i
], "error") == 0) debug_flags
.error
= 0;
1080 if (strcasecmp(argv
[i
], "warning") == 0) debug_flags
.warning
= 0;
1081 if (strcasecmp(argv
[i
], "info") == 0) debug_flags
.info
= 0;
1082 if (strcasecmp(argv
[i
], "calls") == 0) debug_flags
.calls
= 0;
1083 if (strcasecmp(argv
[i
], "data") == 0) debug_flags
.data
= 0;
1084 if (strcasecmp(argv
[i
], "all") == 0) memset(&debug_flags
, 0, sizeof(debug_flags
));
1090 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1092 int i
, firstfree
= 0;
1095 cli_print(cli
, "Specify a plugin to load");
1099 for (i
= 0; i
< MAXPLUGINS
; i
++)
1101 if (!*config
->plugins
[i
] && !firstfree
)
1103 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1105 cli_print(cli
, "Plugin is already loaded");
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]);
1120 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1126 cli_print(cli
, "Specify a plugin to remove");
1130 for (i
= 0; i
< MAXPLUGINS
; i
++)
1132 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1134 config
->reload_config
= 1;
1135 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1140 cli_print(cli
, "Plugin is not loaded");
1144 char *duration(time_t seconds
)
1146 static char *buf
= NULL
;
1147 if (!buf
) buf
= calloc(64, 1);
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);
1154 sprintf(buf
, "%lu sec", seconds
);
1158 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1161 char buf
[100], *p
= buf
, *loads
[3];
1162 int i
, num_sessions
= 0;
1165 fh
= fopen("/proc/loadavg", "r");
1166 fgets(buf
, 100, fh
);
1169 for (i
= 0; i
< 3; i
++)
1170 loads
[i
] = strdup(strsep(&p
, " "));
1173 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1175 for (i
= 1; i
< MAXSESSION
; i
++)
1176 if (session
[i
].opened
) num_sessions
++;
1178 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1180 duration(abs(time_now
- config
->start_time
)),
1182 loads
[0], loads
[1], loads
[2]
1184 for (i
= 0; i
< 3; i
++)
1185 if (loads
[i
]) free(loads
[i
]);
1187 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1192 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1198 cli_print(cli
, "Usage: set <variable> <value>");
1202 for (i
= 0; config_values
[i
].key
; i
++)
1204 void *value
= ((void *)config
) + config_values
[i
].offset
;
1205 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1207 // Found a value to set
1208 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1209 switch (config_values
[i
].type
)
1212 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1215 *(int *)value
= atoi(argv
[1]);
1218 *(unsigned long *)value
= atol(argv
[1]);
1221 *(short *)value
= atoi(argv
[1]);
1224 *(unsigned *)value
= inet_addr(argv
[1]);
1227 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1233 cli_print(cli
, "Unknown variable type");
1236 config
->reload_config
= 1;
1241 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1245 int regular_stuff(struct cli_def
*cli
)
1247 int i
= debug_rb_tail
;
1251 while (i
!= ringbuffer
->tail
)
1253 int show_message
= 0;
1255 if (*ringbuffer
->buffer
[i
].message
)
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;
1268 ipt address
= ntohl(ringbuffer
->buffer
[i
].address
);
1270 struct in_addr addr
;
1272 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1273 ipaddr
= inet_ntoa(addr
);
1275 cli_print(cli
, "\r%s-%s-%u-%u %s",
1276 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1278 ringbuffer
->buffer
[i
].tunnel
,
1279 ringbuffer
->buffer
[i
].session
,
1280 ringbuffer
->buffer
[i
].message
);
1285 if (++i
== ringbuffer
->tail
) break;
1286 if (i
== RINGBUFFER_SIZE
) i
= 0;
1289 debug_rb_tail
= ringbuffer
->tail
;