"agent auto" will register the agent normally but will also
automatically accept all peer authorization requests that either
require no input or that can be accepted with just an empty WPS
string (pushbutton mode).

This makes testing peer connections much faster and less stressful.
---
 client/agent.c    | 58 +++++++++++++++++++++++++++++++++++++++----------------
 client/agent.h    |  2 +-
 client/commands.c | 56 ++++++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 85 insertions(+), 31 deletions(-)

diff --git a/client/agent.c b/client/agent.c
index d020889..ac7a07e 100644
--- a/client/agent.c
+++ b/client/agent.c
@@ -54,6 +54,7 @@ struct agent_data {
        struct agent_input_data *input;
        char *interface;
        bool registered;
+       bool auto_accept;
        DBusMessage *message;
        DBusMessage *reply;
        DBusMessageIter iter;
@@ -589,27 +590,36 @@ static DBusMessage *agent_request_input(DBusConnection 
*connection,
        return NULL;
 }
 
+static void send_authorization_reply(struct agent_data *request, bool 
include_empty_wps)
+{
+       request->reply = dbus_message_new_method_return(request->message);
+       dbus_message_iter_init_append(request->reply, &request->iter);
+
+       dbus_message_iter_open_container(&request->iter,
+                       DBUS_TYPE_ARRAY,
+                       DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+                       DBUS_TYPE_STRING_AS_STRING
+                       DBUS_TYPE_VARIANT_AS_STRING
+                       DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
+                       &request->dict);
+
+       if (include_empty_wps) {
+               request_input_append(request, "WPS", "");
+               request->input[WPS].requested = false;
+       }
+
+       dbus_message_iter_close_container(&request->iter, &request->dict);
+       g_dbus_send_message(agent_connection, request->reply);
+       request->reply = NULL;
+}
+
 static void request_authorization_return(char *input, void *user_data)
 {
        struct agent_data *request = user_data;
 
        switch (confirm_input(input)) {
        case 1:
-               request->reply = dbus_message_new_method_return(
-                                                       request->message);
-               dbus_message_iter_init_append(request->reply, &request->iter);
-
-               dbus_message_iter_open_container(&request->iter,
-                               DBUS_TYPE_ARRAY,
-                               DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
-                               DBUS_TYPE_STRING_AS_STRING
-                               DBUS_TYPE_VARIANT_AS_STRING
-                               DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
-                               &request->dict);
-               dbus_message_iter_close_container(&request->iter,
-                                                       &request->dict);
-               g_dbus_send_message(agent_connection, request->reply);
-               request->reply = NULL;
+               send_authorization_reply(request, FALSE);
                break;
        case 0:
                 g_dbus_send_error(agent_connection, request->message,
@@ -632,7 +642,7 @@ agent_request_peer_authorization(DBusConnection *connection,
        struct agent_data *request = user_data;
        DBusMessageIter iter, dict;
        char *peer, *str;
-       bool input;
+       bool input, is_wps;
        int i;
 
        if (handle_message(message, request, agent_request_peer_authorization)
@@ -657,10 +667,21 @@ agent_request_peer_authorization(DBusConnection 
*connection,
        for (input = false, i = 0; request->input[i].attribute; i++) {
                if (request->input[i].requested == true) {
                        input = true;
+                       is_wps = (i == WPS);
                        break;
                }
        }
 
+       if (request->auto_accept && (!input || is_wps)) {
+               request->message = dbus_message_ref(message);
+               send_authorization_reply(request, is_wps);
+               fprintf(stdout, "Connection accepted\n");
+               pending_message_remove(request);
+               pending_command_complete("");
+
+               return NULL;
+       }
+
        if (!input) {
                request->message = dbus_message_ref(message);
                __connmanctl_agent_mode("Accept connection (yes/no)? ",
@@ -735,11 +756,14 @@ static void append_path(DBusMessageIter *iter, void 
*user_data)
        dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
 }
 
-int __connmanctl_agent_register(DBusConnection *connection)
+int __connmanctl_agent_register(DBusConnection *connection,
+               bool auto_accept)
 {
        char *path = agent_path();
        int result;
 
+       agent_request.auto_accept = auto_accept;
+
        if (agent_request.registered == true) {
                fprintf(stderr, "Agent already registered\n");
                return -EALREADY;
diff --git a/client/agent.h b/client/agent.h
index 669a593..10773dc 100644
--- a/client/agent.h
+++ b/client/agent.h
@@ -29,7 +29,7 @@
 extern "C" {
 #endif
 
-int __connmanctl_agent_register(DBusConnection *connection);
+int __connmanctl_agent_register(DBusConnection *connection, bool auto_accept);
 int __connmanctl_agent_unregister(DBusConnection *connection);
 
 int __connmanctl_vpn_agent_register(DBusConnection *connection);
diff --git a/client/commands.c b/client/commands.c
index 9208016..6e37141 100644
--- a/client/commands.c
+++ b/client/commands.c
@@ -1332,20 +1332,26 @@ static int cmd_agent(char *args[], int num, struct 
connman_option *options)
        if (num < 2)
                return -EINVAL;
 
-       switch(parse_boolean(args[1])) {
-       case 0:
-               __connmanctl_agent_unregister(connection);
-               break;
-
-       case 1:
-               if (__connmanctl_agent_register(connection) == -EINPROGRESS)
+       if(strcasecmp(args[1], "auto") == 0) {
+               if (__connmanctl_agent_register(connection, TRUE) == 
-EINPROGRESS)
                        return -EINPROGRESS;
 
-               break;
+       } else {
+               switch(parse_boolean(args[1])) {
+               case 0:
+                       __connmanctl_agent_unregister(connection);
+                       break;
 
-       default:
-               return -EINVAL;
-               break;
+               case 1:
+                       if (__connmanctl_agent_register(connection, FALSE) == 
-EINPROGRESS)
+                               return -EINPROGRESS;
+
+                       break;
+
+               default:
+                       return -EINVAL;
+                       break;
+               }
        }
 
        return 0;
@@ -1983,6 +1989,30 @@ static char *lookup_on_off(const char *text, int state)
        return NULL;
 }
 
+static char *lookup_on_off_auto(const char *text, int state)
+{
+       char *onoffauto[] = { "on", "off", "auto", NULL };
+       static int idx = 0;
+       static int len = 0;
+
+       char *str;
+
+       if (!state) {
+               idx = 0;
+               len = strlen(text);
+       }
+
+       while (onoffauto[idx]) {
+               str = onoffauto[idx];
+               idx++;
+
+               if (!strncmp(text, str, len))
+                       return strdup(str);
+       }
+
+       return NULL;
+}
+
 static char *lookup_tether(const char *text, int state)
 {
        int level;
@@ -2006,7 +2036,7 @@ static char *lookup_agent(const char *text, int state)
                return NULL;
        }
 
-       return lookup_on_off(text, state);
+       return lookup_on_off_auto(text, state);
 }
 
 static struct connman_option service_options[] = {
@@ -2402,7 +2432,7 @@ static const struct {
          "Set service configuration options", lookup_config },
        { "monitor",      "[off]",        monitor_options, cmd_monitor,
          "Monitor signals from interfaces", lookup_monitor },
-       { "agent", "on|off",              NULL,            cmd_agent,
+       { "agent", "on|off|auto",         NULL,            cmd_agent,
          "Agent mode", lookup_agent },
        {"vpnconnections", "[<connection>]", NULL,         cmd_vpnconnections,
         "Display VPN connections", NULL },
-- 
2.1.4

_______________________________________________
connman mailing list
[email protected]
https://lists.connman.net/mailman/listinfo/connman

Reply via email to