1 // L2TPNS Command Line Interface
2 // $Id: cli.c,v 1.2 2004-03-05 00:09:03 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
, 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
[];
39 extern struct Tringbuffer
*ringbuffer
;
42 char *rcs_id
= "$Id: cli.c,v 1.2 2004-03-05 00:09:03 fred_nerk Exp $";
44 char *debug_levels
[] = {
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
);
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");
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");
119 cli_register_command(cli
, c
, "counters", cmd_show_counters
, "Display all the internal counters and running totals");
121 c
= cli_register_command(cli
, NULL
, "clear", NULL
, NULL
);
122 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, "Clear internal counters");
125 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, "Show uptime and bandwidth utilisation");
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");
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");
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");
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");
144 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, "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");
152 c
= cli_register_command(cli
, NULL
, "load", NULL
, NULL
);
153 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, "Load a plugin");
155 c
= cli_register_command(cli
, NULL
, "remove", NULL
, NULL
);
156 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, "Remove a plugin");
158 cli_register_command(cli
, NULL
, "set", cmd_set
, "Set a configuration variable");
160 // Enable regular processing
161 cli_regular(cli
, regular_stuff
);
163 if (!(f
= fopen(CLIUSERS
, "r")))
165 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
169 while (fgets(buf
, 4096, f
))
172 if (*buf
== '#') continue;
173 if ((p
= strchr(buf
, '\r'))) *p
= 0;
174 if ((p
= strchr(buf
, '\n'))) *p
= 0;
176 if (!(p
= strchr((char *)buf
, ':'))) continue;
178 cli_allow_user(cli
, buf
, p
);
179 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
184 memset(&addr
, 0, sizeof(addr
));
185 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
186 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
189 // Set cli fd as non-blocking
190 flags
= fcntl(clifd
, F_GETFL
, 0);
191 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
193 addr
.sin_family
= AF_INET
;
194 addr
.sin_port
= htons(23);
195 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
197 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
203 void cli_do(int sockfd
)
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;
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
);
224 log(3, 0, 0, 0, "Accepted connection to CLI\n");
229 debug_rb_tail
= ringbuffer
->tail
;
231 memset(&debug_flags
, 0, sizeof(debug_flags
));
232 debug_flags
.critical
= 1;
236 snprintf(prompt
, 1005, "%s> ", hostname
);
237 cli_loop(cli
, sockfd
, prompt
);
241 log(3, 0, 0, 0, "Closed CLI connection\n");
245 void cli_print_log(struct cli_def
*cli
, char *string
)
247 log(3, 0, 0, 0, "%s\n", string
);
250 void cli_do_file(FILE *fh
)
252 log(3, 0, 0, 0, "Reading configuration file\n");
253 cli_print_callback(cli
, cli_print_log
);
255 cli_print_callback(cli
, NULL
);
258 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
266 // Show individual session
267 for (i
= 0; i
< argc
; i
++)
271 if (!s
|| s
> MAXSESSION
)
273 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
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
, " Servicenet: %s", session
[s
].servicenet
? "YES" : "no");
294 cli_print(cli
, " Filter Bucket: %s", session
[s
].tbf
? filter_buckets
[session
[s
].tbf
].handle
: "none");
300 cli_print(cli
, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
314 for (i
= 1; i
< MAXSESSION
; i
++)
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",
323 session
[i
].user
[0] ? session
[i
].user
: "*",
325 (session
[i
].snoop
) ? "Y" : "N",
326 (session
[i
].throttle
) ? "Y" : "N",
327 (session
[i
].servicenet
) ? "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
)),
333 session
[i
].calling
[0] ? session
[i
].calling
: "*");
334 if (userip
) free(userip
);
335 if (tunnelip
) free(tunnelip
);
340 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
342 int i
, x
, show_all
= 0;
354 if (strcmp(argv
[0], "all") == 0)
360 // Show individual tunnel
361 for (i
= 0; i
< argc
; i
++)
366 if (!t
|| t
> MAXTUNNEL
)
368 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
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
));
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
);
391 // Show tunnel summary
392 cli_print(cli
, "%s %s %s %s %s",
398 for (i
= 1; i
< MAXTUNNEL
; i
++)
401 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
|| !tunnel
[i
].hostname
[0])) continue;
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",
406 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
407 inet_toa(htonl(tunnel
[i
].ip
)),
408 states
[tunnel
[i
].state
],
414 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
417 for (i
= 0; i
< MAXSESSION
; i
++)
419 if (!session
[i
].opened
) continue;
420 if (!session
[i
].user
[0]) continue;
427 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
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
));
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
),
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
));
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
));
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
));
507 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
509 cli_print(cli
, "L2TPNS %s", VERSION
);
510 cli_print(cli
, "ID: %s", rcs_id
);
514 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
517 int used
= 0, free
= 0, show_all
= 0;
520 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
524 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
525 for (i
= 0; i
< MAXIPPOOL
; i
++)
527 if (!ip_address_pool
[i
].address
) continue;
528 if (ip_address_pool
[i
].assigned
)
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
);
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
);
543 cli_print(cli
, "%-15s N", inet_toa(ip_address_pool
[i
].address
));
550 cli_print(cli
, "(Not displaying unused addresses)");
552 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
556 void print_save_config(struct cli_def
*cli
, char *string
)
559 fprintf(save_config_fh
, "%s\n", string
);
562 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
564 if ((save_config_fh
= fopen(config
->config_file
, "w")))
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
);
575 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
580 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
584 cli_print(cli
, "# Current configuration:");
586 for (i
= 0; config_values
[i
].key
; i
++)
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
);
603 cli_print(cli
, "# Plugins");
604 for (i
= 0; i
< MAXPLUGINS
; i
++)
606 if (*config
->plugins
[i
])
608 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
612 cli_print(cli
, "# end");
616 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
627 int i
, free
= 0, used
= 0, show_all
= 0;
630 cli_print(cli
, "%6s%6s%9s%9s%4s", "Radius", "State", "Session", "Retry", "Try");
634 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
637 for (i
= 1; i
< MAXRADIUS
; i
++)
639 if (radius
[i
].state
== RADIUSNULL
)
644 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
646 cli_print(cli
, "%6d%6s%9d%9u%4d",
648 states
[radius
[i
].state
],
654 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
659 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
662 cli_print(cli
, "Plugins currently loaded:");
663 for (i
= 0; i
< MAXPLUGINS
; i
++)
665 if (*config
->plugins
[i
])
667 cli_print(cli
, " %s", config
->plugins
[i
]);
673 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
675 cli_print(cli
, " _\n"
681 " ( \\ `. `-. _,.-:\\\n"
682 " \\ \\ `. `-._ __..--' ,-';/\n"
683 " \\ `. `-. `-..___..---' _.--' ,'/\n"
684 " `. `. `-._ __..--' ,' /\n"
685 " `. `-_ ``--..'' _.-' ,'\n"
686 " `-_ `-.___ __,--' ,'\n"
687 " `-.__ `----\"\"\" __.-'\n"
688 "hh `--..____..--'");
693 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
695 cli_print(cli
, "Counters cleared");
696 SET_STAT(last_reset
, time(NULL
));
700 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
707 cli_print(cli
, "Specify a user to drop");
710 for (i
= 0; i
< argc
; i
++)
712 if (strchr(argv
[i
], '?'))
714 cli_print(cli
, "username ...");
719 for (i
= 0; i
< argc
; i
++)
721 if (!(s
= sessionbyuser(argv
[i
])))
723 cli_print(cli
, "User %s is not connected", argv
[i
]);
727 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
731 cli_print(cli
, "Dropping user %s", session
[s
].user
);
732 for (x
= 0; x
< MAXSESSION
; x
++)
734 if (!cli_session_kill
[x
])
736 cli_session_kill
[x
] = s
;
746 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
753 cli_print(cli
, "Specify a tunnel to drop");
756 for (i
= 0; i
< argc
; i
++)
758 if (strchr(argv
[i
], '?'))
760 cli_print(cli
, "tunnel_id ...");
765 for (i
= 0; i
< argc
; i
++)
769 if ((tid
= atol(argv
[i
])) <= 0 || (tid
> MAXTUNNEL
))
771 cli_print(cli
, "Invalid tunnel ID (%d - %d)", 0, MAXTUNNEL
);
777 cli_print(cli
, "Tunnel %d is not connected", tid
);
783 cli_print(cli
, "Tunnel %d is already being shut down", tid
);
787 for (x
= 0; x
< MAXTUNNEL
; x
++)
789 if (!cli_tunnel_kill
[x
])
791 cli_tunnel_kill
[x
] = tid
;
792 cli_print(cli
, "Tunnel %d shut down (%s)", tid
, tunnel
[tid
].hostname
);
801 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
808 cli_print(cli
, "Specify a session id to drop");
811 for (i
= 0; i
< argc
; i
++)
813 if (strchr(argv
[i
], '?'))
815 cli_print(cli
, "session_id ...");
820 for (i
= 0; i
< argc
; i
++)
822 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
824 cli_print(cli
, "Invalid session ID (%d - %d)", 0, MAXSESSION
);
828 if (session
[s
].opened
&& !session
[s
].die
)
831 for (x
= 0; x
< MAXSESSION
; x
++)
833 if (!cli_session_kill
[x
])
835 cli_session_kill
[x
] = s
;
839 cli_print(cli
, "Dropping session %d", s
);
843 cli_print(cli
, "Session %d is not active.", s
);
850 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
857 cli_print(cli
, "Specify a user");
860 for (i
= 0; i
< argc
; i
++)
862 if (strchr(argv
[i
], '?'))
864 cli_print(cli
, "username ...");
869 for (i
= 0; i
< argc
; i
++)
871 if (!(s
= sessionbyuser(argv
[i
])))
873 cli_print(cli
, "User %s is not connected", argv
[i
]);
876 session
[s
].snoop
= 1;
878 cli_print(cli
, "Snooping user %s", argv
[i
]);
883 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
890 cli_print(cli
, "Specify a user");
893 for (i
= 0; i
< argc
; i
++)
895 if (strchr(argv
[i
], '?'))
897 cli_print(cli
, "username ...");
902 for (i
= 0; i
< argc
; i
++)
904 if (!(s
= sessionbyuser(argv
[i
])))
906 cli_print(cli
, "User %s is not connected", argv
[i
]);
909 session
[s
].snoop
= 0;
911 cli_print(cli
, "Not snooping user %s", argv
[i
]);
916 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
923 cli_print(cli
, "Specify a user");
926 for (i
= 0; i
< argc
; i
++)
928 if (strchr(argv
[i
], '?'))
930 cli_print(cli
, "username ...");
935 for (i
= 0; i
< argc
; i
++)
937 if (!(s
= sessionbyuser(argv
[i
])))
939 cli_print(cli
, "User %s is not connected", argv
[i
]);
942 throttle_session(s
, 1);
944 cli_print(cli
, "throttling user %s", argv
[i
]);
949 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
956 cli_print(cli
, "Specify a user");
959 for (i
= 0; i
< argc
; i
++)
961 if (strchr(argv
[i
], '?'))
963 cli_print(cli
, "username ...");
968 for (i
= 0; i
< argc
; i
++)
970 if (!(s
= sessionbyuser(argv
[i
])))
972 cli_print(cli
, "User %s is not connected", argv
[i
]);
975 throttle_session(s
, 0);
977 cli_print(cli
, "unthrottling user %s", argv
[i
]);
982 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
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 ");
999 for (i
= 0; i
< argc
; i
++)
1001 if (*argv
[i
] == '?')
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");
1014 for (i
= 0; i
< argc
; i
++)
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)
1024 memset(&debug_flags
, 1, sizeof(debug_flags
));
1025 debug_flags
.data
= 0;
1032 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1036 for (i
= 0; i
< argc
; i
++)
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
));
1050 int cmd_watch_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1056 cli_print(cli
, "Specify a single session to debug (0 to disable)");
1062 cli_print(cli
, "No longer debugging session %d", debug_session
);
1064 if (s
) cli_print(cli
, "Debugging session %d.", s
);
1070 int cmd_watch_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1076 cli_print(cli
, "Specify a single tunnel to debug (0 to disable)");
1082 cli_print(cli
, "No longer debugging tunnel %d", debug_tunnel
);
1084 if (s
) cli_print(cli
, "Debugging tunnel %d.", s
);
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
;