+ return CLI_OK;
+}
+
+static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ ipt ip;
+ u16 port;
+ sessionidt s;
+
+ if (CLI_HELP_REQUESTED)
+ {
+ switch (argc)
+ {
+ case 1:
+ return cli_arg_help(cli, 0,
+ "USER", "Username of session to snoop", NULL);
+
+ case 2:
+ return cli_arg_help(cli, 0,
+ "A.B.C.D", "IP address of snoop destination", NULL);
+
+ case 3:
+ return cli_arg_help(cli, 0,
+ "N", "Port of snoop destination", NULL);
+
+ case 4:
+ if (!argv[3][1])
+ return cli_arg_help(cli, 1, NULL);
+
+ default:
+ return CLI_OK;
+ }
+ }
+
+ if (!config->cluster_iam_master)
+ {
+ cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
+ return CLI_OK;
+ }
+
+ if (argc < 3)
+ {
+ cli_print(cli, "Specify username, ip and port");
+ return CLI_OK;
+ }
+
+ if (!(s = sessionbyuser(argv[0])))
+ {
+ cli_print(cli, "User %s is not connected", argv[0]);
+ return CLI_OK;
+ }
+
+ ip = inet_addr(argv[1]);
+ if (!ip || ip == INADDR_NONE)
+ {
+ cli_print(cli, "Cannot parse IP \"%s\"", argv[1]);
+ return CLI_OK;
+ }
+
+ port = atoi(argv[2]);
+ if (!port)
+ {
+ cli_print(cli, "Invalid port %s", argv[2]);
+ return CLI_OK;
+ }
+
+ cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(ip), port);
+ cli_session_actions[s].snoop_ip = ip;
+ cli_session_actions[s].snoop_port = port;
+ cli_session_actions[s].action |= CLI_SESS_SNOOP;
+
+ return CLI_OK;
+}
+
+static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ int i;
+ sessionidt s;
+
+ if (CLI_HELP_REQUESTED)
+ return cli_arg_help(cli, argc > 1,
+ "USER", "Username of session to unsnoop", NULL);
+
+ if (!config->cluster_iam_master)
+ {
+ cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
+ return CLI_OK;
+ }
+
+ if (!argc)
+ {
+ cli_print(cli, "Specify a user to unsnoop");
+ return CLI_OK;
+ }
+
+ for (i = 0; i < argc; i++)
+ {
+ if (!(s = sessionbyuser(argv[i])))
+ {
+ cli_print(cli, "User %s is not connected", argv[i]);
+ continue;
+ }
+
+ cli_print(cli, "Not snooping user %s", argv[i]);
+ cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
+ }
+
+ return CLI_OK;
+}
+
+static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ int rate_in = 0;
+ int rate_out = 0;
+ sessionidt s;
+
+ /*
+ throttle USER - throttle in/out to default rate
+ throttle USER RATE - throttle in/out to default rate
+ throttle USER in RATE - throttle input only
+ throttle USER out RATE - throttle output only
+ throttle USER in RATE out RATE - throttle both
+ */
+
+ if (CLI_HELP_REQUESTED)
+ {
+ switch (argc)
+ {
+ case 1:
+ return cli_arg_help(cli, 0,
+ "USER", "Username of session to throttle", NULL);
+
+ case 2:
+ return cli_arg_help(cli, 1,
+ "RATE", "Rate in kbps (in and out)",
+ "in", "Select incoming rate",
+ "out", "Select outgoing rate", NULL);
+
+ case 4:
+ return cli_arg_help(cli, 1,
+ "in", "Select incoming rate",
+ "out", "Select outgoing rate", NULL);
+
+ case 3:
+ if (isdigit(argv[1][0]))
+ return cli_arg_help(cli, 1, NULL);
+
+ case 5:
+ return cli_arg_help(cli, 0, "RATE", "Rate in kbps", NULL);
+
+ default:
+ return cli_arg_help(cli, argc > 1, NULL);
+ }
+ }
+
+ if (!config->cluster_iam_master)
+ {
+ cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
+ return CLI_OK;
+ }
+
+ if (argc == 0)
+ {
+ cli_print(cli, "Specify a user to throttle");
+ return CLI_OK;
+ }
+
+ if (!(s = sessionbyuser(argv[0])))
+ {
+ cli_print(cli, "User %s is not connected", argv[0]);
+ return CLI_OK;
+ }
+
+ if (argc == 1)
+ {
+ rate_in = rate_out = config->rl_rate;
+ }
+ else if (argc == 2)
+ {
+ rate_in = rate_out = atoi(argv[1]);
+ if (rate_in < 1)
+ {
+ cli_print(cli, "Invalid rate \"%s\"", argv[1]);
+ return CLI_OK;
+ }
+ }
+ else if (argc == 3 || argc == 5)
+ {
+ int i;
+ for (i = 1; i < argc - 1; i += 2)
+ {
+ int len = strlen(argv[i]);
+ int r = 0;
+ if (!strncasecmp(argv[i], "in", len))
+ r = rate_in = atoi(argv[i+1]);
+ else if (!strncasecmp(argv[i], "out", len))
+ r = rate_out = atoi(argv[i+1]);
+
+ if (r < 1)
+ {
+ cli_print(cli, "Invalid rate specification \"%s %s\"", argv[i], argv[i+1]);
+ return CLI_OK;
+ }
+ }
+ }
+ else
+ {
+ cli_print(cli, "Invalid arguments");
+ return CLI_OK;
+ }
+
+ if ((rate_in && session[s].throttle_in) || (rate_out && session[s].throttle_out))
+ {
+ cli_print(cli, "User %s already throttled, unthrottle first", argv[0]);
+ return CLI_OK;
+ }
+
+ cli_session_actions[s].throttle_in = cli_session_actions[s].throttle_out = -1;
+ if (rate_in && session[s].throttle_in != rate_in)
+ cli_session_actions[s].throttle_in = rate_in;
+
+ if (rate_out && session[s].throttle_out != rate_out)
+ cli_session_actions[s].throttle_out = rate_out;
+
+ if (cli_session_actions[s].throttle_in == -1 &&
+ cli_session_actions[s].throttle_out == -1)
+ {
+ cli_print(cli, "User %s already throttled at this rate", argv[0]);
+ return CLI_OK;
+ }
+
+ cli_print(cli, "Throttling user %s", argv[0]);
+ cli_session_actions[s].action |= CLI_SESS_THROTTLE;
+
+ return CLI_OK;
+}
+
+static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ int i;
+ sessionidt s;
+
+ if (CLI_HELP_REQUESTED)
+ return cli_arg_help(cli, argc > 1,
+ "USER", "Username of session to unthrottle", NULL);
+
+ if (!config->cluster_iam_master)
+ {
+ cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
+ return CLI_OK;
+ }
+
+ if (!argc)
+ {
+ cli_print(cli, "Specify a user to unthrottle");
+ return CLI_OK;
+ }
+
+ for (i = 0; i < argc; i++)
+ {
+ if (!(s = sessionbyuser(argv[i])))
+ {
+ cli_print(cli, "User %s is not connected", argv[i]);
+ continue;
+ }
+
+ if (session[s].throttle_in || session[s].throttle_out)
+ {
+ cli_print(cli, "Unthrottling user %s", argv[i]);
+ cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
+ }
+ else
+ {
+ cli_print(cli, "User %s not throttled", argv[i]);
+ }
+ }
+
+ return CLI_OK;
+}
+
+static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ int i;
+
+ if (CLI_HELP_REQUESTED)
+ return cli_arg_help(cli, 1,
+ "all", "Enable debugging for all except \"data\"",
+ "critical", "", // FIXME: add descriptions
+ "error", "",
+ "warning", "",
+ "info", "",
+ "calls", "",
+ "data", "",
+ NULL);
+
+ if (!argc)
+ {
+ char *p = (char *) &debug_flags;
+ for (i = 0; i < sizeof(debug_flags); i++)
+ {
+ if (p[i])
+ {
+ cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
+ (debug_flags.critical) ? " critical" : "",
+ (debug_flags.error) ? " error" : "",
+ (debug_flags.warning) ? " warning" : "",
+ (debug_flags.info) ? " info" : "",
+ (debug_flags.calls) ? " calls" : "",
+ (debug_flags.data) ? " data" : "");
+
+ return CLI_OK;
+ }
+ }
+
+ cli_print(cli, "Debugging off");
+ return CLI_OK;
+ }
+
+ for (i = 0; i < argc; i++)
+ {
+ int len = strlen(argv[i]);
+
+ if (argv[i][0] == 'c' && len < 2)
+ len = 2; /* distinguish [cr]itical from [ca]lls */
+
+ if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; }
+ if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; }
+ if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; }
+ if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; }
+ if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; }
+ if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; }
+ if (!strncasecmp(argv[i], "all", len))
+ {
+ memset(&debug_flags, 1, sizeof(debug_flags));
+ debug_flags.data = 0;
+ continue;
+ }
+
+ cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
+ }
+
+ return CLI_OK;
+}
+
+static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ int i;
+
+ if (CLI_HELP_REQUESTED)
+ return cli_arg_help(cli, 1,
+ "all", "Disable all debugging",
+ "critical", "", // FIXME: add descriptions
+ "error", "",
+ "warning", "",
+ "info", "",
+ "calls", "",
+ "data", "",
+ NULL);
+
+ if (!argc)
+ {
+ memset(&debug_flags, 0, sizeof(debug_flags));
+ return CLI_OK;
+ }
+
+ for (i = 0; i < argc; i++)
+ {
+ int len = strlen(argv[i]);
+
+ if (argv[i][0] == 'c' && len < 2)
+ len = 2; /* distinguish [cr]itical from [ca]lls */
+
+ if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; }
+ if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; }
+ if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; }
+ if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; }
+ if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; }
+ if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; }
+ if (!strncasecmp(argv[i], "all", len))
+ {
+ memset(&debug_flags, 0, sizeof(debug_flags));
+ continue;
+ }
+
+ cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
+ }
+
+ return CLI_OK;
+}
+
+static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ int i, firstfree = 0;
+
+ if (CLI_HELP_REQUESTED)
+ return cli_arg_help(cli, argc > 1,
+ "PLUGIN", "Name of plugin to load", NULL);
+
+ if (argc != 1)
+ {
+ cli_print(cli, "Specify a plugin to load");
+ return CLI_OK;
+ }
+
+ for (i = 0; i < MAXPLUGINS; i++)
+ {
+ if (!*config->plugins[i] && !firstfree)
+ firstfree = i;
+ if (strcmp(config->plugins[i], argv[0]) == 0)
+ {
+ cli_print(cli, "Plugin is already loaded");
+ return CLI_OK;
+ }
+ }
+
+ if (firstfree)
+ {
+ strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
+ config->reload_config = 1;
+ cli_print(cli, "Loading plugin %s", argv[0]);
+ }
+
+ return CLI_OK;
+}
+
+static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ int i;
+
+ if (CLI_HELP_REQUESTED)
+ return cli_arg_help(cli, argc > 1,
+ "PLUGIN", "Name of plugin to unload", NULL);
+
+ if (argc != 1)
+ {
+ cli_print(cli, "Specify a plugin to remove");
+ return CLI_OK;
+ }
+
+ for (i = 0; i < MAXPLUGINS; i++)
+ {
+ if (strcmp(config->plugins[i], argv[0]) == 0)
+ {
+ config->reload_config = 1;
+ memset(config->plugins[i], 0, sizeof(config->plugins[i]));
+ return CLI_OK;
+ }
+ }
+
+ cli_print(cli, "Plugin is not loaded");
+ return CLI_OK;
+}
+
+static char *duration(time_t secs)
+{
+ static char *buf = NULL;
+ int p = 0;
+
+ if (!buf) buf = calloc(64, 1);
+
+ if (secs >= 86400)
+ {
+ int days = secs / 86400;
+ p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
+ secs %= 86400;
+ }
+
+ if (secs >= 3600)
+ {
+ int mins = secs / 60;
+ int hrs = mins / 60;
+
+ mins %= 60;
+ sprintf(buf + p, "%d:%02d", hrs, mins);
+ }
+ else if (secs >= 60)
+ {
+ int mins = secs / 60;
+ sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
+ }
+ else
+ sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
+
+ return buf;
+}
+
+static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
+{
+ FILE *fh;
+ char buf[100], *p = buf, *loads[3];
+ int i, num_sessions = 0;
+
+ if (CLI_HELP_REQUESTED)
+ return CLI_HELP_NO_ARGS;