Share a common helper function for restarting sched_scan
[mech_eap.git] / hostapd / hostapd_cli.c
index 2a45ca4..5e62542 100644 (file)
@@ -15,6 +15,7 @@
 #include "utils/eloop.h"
 #include "utils/edit.h"
 #include "common/version.h"
+#include "common/cli.h"
 
 #ifndef CONFIG_NO_CTRL_IFACE
 
@@ -22,42 +23,6 @@ static const char *const hostapd_cli_version =
 "hostapd_cli v" VERSION_STR "\n"
 "Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi> and contributors";
 
-
-static const char *const hostapd_cli_license =
-"This software may be distributed under the terms of the BSD license.\n"
-"See README for more details.\n";
-
-static const char *const hostapd_cli_full_license =
-"This software may be distributed under the terms of the BSD license.\n"
-"\n"
-"Redistribution and use in source and binary forms, with or without\n"
-"modification, are permitted provided that the following conditions are\n"
-"met:\n"
-"\n"
-"1. Redistributions of source code must retain the above copyright\n"
-"   notice, this list of conditions and the following disclaimer.\n"
-"\n"
-"2. Redistributions in binary form must reproduce the above copyright\n"
-"   notice, this list of conditions and the following disclaimer in the\n"
-"   documentation and/or other materials provided with the distribution.\n"
-"\n"
-"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
-"   names of its contributors may be used to endorse or promote products\n"
-"   derived from this software without specific prior written permission.\n"
-"\n"
-"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
-"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
-"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
-"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
-"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
-"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
-"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
-"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
-"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
-"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
-"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
-"\n";
-
 static struct wpa_ctrl *ctrl_conn;
 static int hostapd_cli_quit = 0;
 static int hostapd_cli_attached = 0;
@@ -73,9 +38,13 @@ static const char *pid_file = NULL;
 static const char *action_file = NULL;
 static int ping_interval = 5;
 static int interactive = 0;
+static int event_handler_registered = 0;
+
+static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
 
 static void print_help(FILE *stream, const char *cmd);
 static char ** list_cmd_list(void);
+static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx);
 
 
 static void usage(void)
@@ -105,27 +74,27 @@ static void usage(void)
 }
 
 
-static int get_cmd_arg_num(const char *str, int pos)
+static void register_event_handler(struct wpa_ctrl *ctrl)
 {
-       int arg = 0, i;
-
-       for (i = 0; i <= pos; i++) {
-               if (str[i] != ' ') {
-                       arg++;
-                       while (i <= pos && str[i] != ' ')
-                               i++;
-               }
+       if (!ctrl_conn)
+               return;
+       if (interactive) {
+               event_handler_registered =
+                       !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
+                                                 hostapd_cli_receive,
+                                                 NULL, NULL);
        }
-
-       if (arg > 0)
-               arg--;
-       return arg;
 }
 
 
-static int str_starts(const char *src, const char *match)
+static void unregister_event_handler(struct wpa_ctrl *ctrl)
 {
-       return os_strncmp(src, match, os_strlen(match)) == 0;
+       if (!ctrl_conn)
+               return;
+       if (interactive && event_handler_registered) {
+               eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
+               event_handler_registered = 0;
+       }
 }
 
 
@@ -168,6 +137,7 @@ static void hostapd_cli_close_connection(void)
        if (ctrl_conn == NULL)
                return;
 
+       unregister_event_handler(ctrl_conn);
        if (hostapd_cli_attached) {
                wpa_ctrl_detach(ctrl_conn);
                hostapd_cli_attached = 0;
@@ -217,36 +187,6 @@ static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
 }
 
 
-static int write_cmd(char *buf, size_t buflen, const char *cmd, int argc,
-                    char *argv[])
-{
-       int i, res;
-       char *pos, *end;
-
-       pos = buf;
-       end = buf + buflen;
-
-       res = os_snprintf(pos, end - pos, "%s", cmd);
-       if (os_snprintf_error(end - pos, res))
-               goto fail;
-       pos += res;
-
-       for (i = 0; i < argc; i++) {
-               res = os_snprintf(pos, end - pos, " %s", argv[i]);
-               if (os_snprintf_error(end - pos, res))
-                       goto fail;
-               pos += res;
-       }
-
-       buf[buflen - 1] = '\0';
-       return 0;
-
-fail:
-       printf("Too long command\n");
-       return -1;
-}
-
-
 static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
                           int min_args, int argc, char *argv[])
 {
@@ -378,6 +318,21 @@ static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static char ** hostapd_complete_deauthenticate(const char *str, int pos)
+{
+       int arg = get_cmd_arg_num(str, pos);
+       char **res = NULL;
+
+       switch (arg) {
+       case 1:
+               res = cli_txt_list_array(&stations);
+               break;
+       }
+
+       return res;
+}
+
+
 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
                                        char *argv[])
 {
@@ -396,6 +351,37 @@ static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static char ** hostapd_complete_disassociate(const char *str, int pos)
+{
+       int arg = get_cmd_arg_num(str, pos);
+       char **res = NULL;
+
+       switch (arg) {
+       case 1:
+               res = cli_txt_list_array(&stations);
+               break;
+       }
+
+       return res;
+}
+
+
+#ifdef CONFIG_TAXONOMY
+static int hostapd_cli_cmd_signature(struct wpa_ctrl *ctrl, int argc,
+                                    char *argv[])
+{
+       char buf[64];
+
+       if (argc != 1) {
+               printf("Invalid 'signature' command - exactly one argument, STA address, is required.\n");
+               return -1;
+       }
+       os_snprintf(buf, sizeof(buf), "SIGNATURE %s", argv[0]);
+       return wpa_ctrl_command(ctrl, buf);
+}
+#endif /* CONFIG_TAXONOMY */
+
+
 #ifdef CONFIG_IEEE80211W
 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
                                    char *argv[])
@@ -791,7 +777,7 @@ static char ** hostapd_cli_complete_help(const char *str, int pos)
 static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
                                   char *argv[])
 {
-       printf("%s\n\n%s\n", hostapd_cli_version, hostapd_cli_full_license);
+       printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
        return 0;
 }
 
@@ -902,6 +888,28 @@ static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
 }
 
 
+static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
+                                      struct dl_list *interfaces)
+{
+       struct dirent *dent;
+       DIR *dir;
+
+       if (!ctrl || !interfaces)
+               return;
+       dir = opendir(ctrl_iface_dir);
+       if (dir == NULL)
+               return;
+
+       while ((dent = readdir(dir))) {
+               if (strcmp(dent->d_name, ".") == 0 ||
+                   strcmp(dent->d_name, "..") == 0)
+                       continue;
+               cli_txt_list_add(interfaces, dent->d_name);
+       }
+       closedir(dir);
+}
+
+
 static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
 {
        struct dirent *dent;
@@ -943,6 +951,7 @@ static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
                printf("Connected to interface '%s.\n", ctrl_ifname);
                if (wpa_ctrl_attach(ctrl_conn) == 0) {
                        hostapd_cli_attached = 1;
+                       register_event_handler(ctrl_conn);
                } else {
                        printf("Warning: Failed to attach to "
                               "hostapd.\n");
@@ -955,6 +964,24 @@ static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static char ** hostapd_complete_interface(const char *str, int pos)
+{
+       int arg = get_cmd_arg_num(str, pos);
+       char **res = NULL;
+       DEFINE_DL_LIST(interfaces);
+
+       switch (arg) {
+       case 1:
+               hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
+               res = cli_txt_list_array(&interfaces);
+               cli_txt_list_flush(&interfaces);
+               break;
+       }
+
+       return res;
+}
+
+
 static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
 {
        char cmd[256];
@@ -1254,10 +1281,16 @@ static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
           "= get MIB variables for all stations" },
        { "new_sta", hostapd_cli_cmd_new_sta, NULL,
          "<addr> = add a new station" },
-       { "deauthenticate", hostapd_cli_cmd_deauthenticate, NULL,
+       { "deauthenticate", hostapd_cli_cmd_deauthenticate,
+         hostapd_complete_deauthenticate,
          "<addr> = deauthenticate a station" },
-       { "disassociate", hostapd_cli_cmd_disassociate, NULL,
+       { "disassociate", hostapd_cli_cmd_disassociate,
+         hostapd_complete_disassociate,
          "<addr> = disassociate a station" },
+#ifdef CONFIG_TAXONOMY
+       { "signature", hostapd_cli_cmd_signature, NULL,
+         "<addr> = get taxonomy signature for a station" },
+#endif /* CONFIG_TAXONOMY */
 #ifdef CONFIG_IEEE80211W
        { "sa_query", hostapd_cli_cmd_sa_query, NULL,
          "<addr> = send SA Query to a station" },
@@ -1295,7 +1328,7 @@ static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
          "= show current configuration" },
        { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
          "= show this usage help" },
-       { "interface", hostapd_cli_cmd_interface, NULL,
+       { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
          "[ifname] = show interfaces/select interface" },
 #ifdef CONFIG_FST
        { "fst", hostapd_cli_cmd_fst, NULL, NULL },
@@ -1403,6 +1436,34 @@ static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
 }
 
 
+static void cli_event(const char *str)
+{
+       const char *start, *s;
+
+       start = os_strchr(str, '>');
+       if (start == NULL)
+               return;
+
+       start++;
+
+       if (str_starts(start, AP_STA_CONNECTED)) {
+               s = os_strchr(start, ' ');
+               if (s == NULL)
+                       return;
+               cli_txt_list_add(&stations, s + 1);
+               return;
+       }
+
+       if (str_starts(start, AP_STA_DISCONNECTED)) {
+               s = os_strchr(start, ' ');
+               if (s == NULL)
+                       return;
+               cli_txt_list_del_addr(&stations, s + 1);
+               return;
+       }
+}
+
+
 static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
                                     int action_monitor)
 {
@@ -1417,6 +1478,7 @@ static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
                        if (action_monitor)
                                hostapd_cli_action_process(buf, len);
                        else {
+                               cli_event(buf);
                                if (in_read && first)
                                        printf("\n");
                                first = 0;
@@ -1430,35 +1492,9 @@ static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
 }
 
 
-#define max_args 10
-
-static int tokenize_cmd(char *cmd, char *argv[])
+static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
 {
-       char *pos;
-       int argc = 0;
-
-       pos = cmd;
-       for (;;) {
-               while (*pos == ' ')
-                       pos++;
-               if (*pos == '\0')
-                       break;
-               argv[argc] = pos;
-               argc++;
-               if (argc == max_args)
-                       break;
-               if (*pos == '"') {
-                       char *pos2 = os_strrchr(pos, '"');
-                       if (pos2)
-                               pos = pos2 + 1;
-               }
-               while (*pos != '\0' && *pos != ' ')
-                       pos++;
-               if (*pos == ' ')
-                       *pos++ = '\0';
-       }
-
-       return argc;
+       hostapd_cli_recv_pending(ctrl_conn, 0, 0);
 }
 
 
@@ -1474,6 +1510,7 @@ static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
                        printf("Connection to hostapd re-established\n");
                        if (wpa_ctrl_attach(ctrl_conn) == 0) {
                                hostapd_cli_attached = 1;
+                               register_event_handler(ctrl_conn);
                        } else {
                                printf("Warning: Failed to attach to "
                                       "hostapd.\n");
@@ -1583,6 +1620,7 @@ static void hostapd_cli_interactive(void)
 
        eloop_run();
 
+       cli_txt_list_flush(&stations);
        edit_deinit(NULL, NULL);
        eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
 }
@@ -1686,8 +1724,7 @@ int main(int argc, char *argv[])
        interactive = (argc == optind) && (action_file == NULL);
 
        if (interactive) {
-               printf("%s\n\n%s\n\n", hostapd_cli_version,
-                      hostapd_cli_license);
+               printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
        }
 
        if (eloop_init())
@@ -1735,6 +1772,7 @@ int main(int argc, char *argv[])
        if (interactive || action_file) {
                if (wpa_ctrl_attach(ctrl_conn) == 0) {
                        hostapd_cli_attached = 1;
+                       register_event_handler(ctrl_conn);
                } else {
                        printf("Warning: Failed to attach to hostapd.\n");
                        if (action_file)
@@ -1752,6 +1790,7 @@ int main(int argc, char *argv[])
        else
                wpa_request(ctrl_conn, argc - optind, &argv[optind]);
 
+       unregister_event_handler(ctrl_conn);
        os_free(ctrl_ifname);
        eloop_destroy();
        hostapd_cli_cleanup();