settings: return username of agent providing secrets
authorDan Williams <dcbw@redhat.com>
Thu, 31 Mar 2011 23:39:09 +0000 (18:39 -0500)
committerDan Williams <dcbw@redhat.com>
Thu, 31 Mar 2011 23:39:09 +0000 (18:39 -0500)
Mainly for VPN connections to grab the default username from, like
0.8 did when the applet provided the username to NM for user
connections.

src/nm-activation-request.c
src/settings/nm-agent-manager.c
src/settings/nm-agent-manager.h
src/settings/nm-settings-connection.c
src/settings/nm-settings-connection.h
src/vpn-manager/nm-vpn-connection.c

index 92ffbee..fb42de6 100644 (file)
@@ -106,6 +106,7 @@ typedef struct {
 static void
 get_secrets_cb (NMSettingsConnection *connection,
                 guint32 call_id,
+                const char *agent_username,
                 const char *setting_name,
                 GError *error,
                 gpointer user_data)
index aae6f85..dbc29ca 100644 (file)
@@ -21,6 +21,7 @@
 #include <config.h>
 #include <string.h>
 #include <ctype.h>
+#include <pwd.h>
 
 #include <glib.h>
 #include <dbus/dbus-glib.h>
@@ -328,6 +329,7 @@ done:
 typedef void (*RequestCompleteFunc) (Request *req,
                                      GHashTable *secrets,
                                      const char *agent_dbus_owner,
+                                     const char *agent_username,
                                      gboolean agent_has_modify,
                                      GError *error,
                                      gpointer user_data);
@@ -466,6 +468,28 @@ request_free (Request *req)
        g_free (req);
 }
 
+static void
+req_complete_success (Request *req,
+                      GHashTable *secrets,
+                      const char *agent_dbus_owner,
+                      const char *agent_uname,
+                      gboolean agent_has_modify)
+{
+       req->complete_callback (req,
+                               secrets,
+                               agent_dbus_owner,
+                               agent_uname,
+                               agent_has_modify,
+                               NULL,
+                               req->complete_callback_data);
+}
+
+static void
+req_complete_error (Request *req, GError *error)
+{
+       req->complete_callback (req, NULL, NULL, NULL, FALSE, error, req->complete_callback_data);
+}
+
 static gint
 agent_compare_func (NMSecretAgent *a, NMSecretAgent *b, gpointer user_data)
 {
@@ -600,7 +624,7 @@ next_generic (Request *req, const char *detail)
                error = g_error_new_literal (NM_AGENT_MANAGER_ERROR,
                                             NM_AGENT_MANAGER_ERROR_NO_SECRETS,
                                             "No agents were available for this request.");
-               req->complete_callback (req, NULL, NULL, FALSE, error, req->complete_callback_data);
+               req_complete_error (req, error);
                g_error_free (error);
        } else {
                /* Send a secrets request to the next agent */
@@ -641,6 +665,8 @@ get_done_cb (NMSecretAgent *agent,
        GHashTable *setting_secrets;
        const char *agent_dbus_owner;
        gboolean agent_has_modify;
+       struct passwd *pw;
+       char *agent_uname = NULL;
 
        g_return_if_fail (call_id == req->current_call_id);
 
@@ -677,8 +703,17 @@ get_done_cb (NMSecretAgent *agent,
                    nm_secret_agent_get_description (agent),
                    req, req->setting_name);
 
+       /* Get the agent's username */
+       pw = getpwuid (nm_secret_agent_get_owner_uid (agent));
+       if (pw && strlen (pw->pw_name)) {
+               /* Needs to be UTF-8 valid since it may be pushed through D-Bus */
+               if (g_utf8_validate (pw->pw_name, -1, NULL))
+                       agent_uname = g_strdup (pw->pw_name);
+       }
+
        agent_dbus_owner = nm_secret_agent_get_dbus_owner (agent);
-       req->complete_callback (req, secrets, agent_dbus_owner, agent_has_modify, NULL, req->complete_callback_data);
+       req_complete_success (req, secrets, agent_dbus_owner, agent_uname, agent_has_modify);
+       g_free (agent_uname);
 }
 
 static void
@@ -914,7 +949,7 @@ get_start (gpointer user_data)
                g_assert (tmp);
 
                if (!nm_connection_update_secrets (tmp, req->setting_name, req->existing_secrets, &error)) {
-                       req->complete_callback (req, NULL, NULL, FALSE, error, req->complete_callback_data);
+                       req_complete_error (req, error);
                        g_clear_error (&error);
                } else {
                        /* Do we have everything we need? */
@@ -924,7 +959,7 @@ get_start (gpointer user_data)
                                            req, req->setting_name);
 
                                /* Got everything, we're done */
-                               req->complete_callback (req, req->existing_secrets, NULL, FALSE, NULL, req->complete_callback_data);
+                               req_complete_success (req, req->existing_secrets, NULL, NULL, FALSE);
                        } else {
                                nm_log_dbg (LOGD_AGENTS, "(%p/%s) system settings secrets insufficient, asking agents",
                                            req, req->setting_name);
@@ -949,6 +984,7 @@ static void
 get_complete_cb (Request *req,
                  GHashTable *secrets,
                  const char *agent_dbus_owner,
+                 const char *agent_username,
                  gboolean agent_has_modify,
                  GError *error,
                  gpointer user_data)
@@ -960,6 +996,7 @@ get_complete_cb (Request *req,
        req->callback (self,
                       req->reqid,
                       agent_dbus_owner,
+                      agent_username,
                       agent_has_modify,
                       req->setting_name,
                       req->flags,
@@ -1082,7 +1119,7 @@ save_done_cb (NMSecretAgent *agent,
                    req, req->setting_name);
 
        agent_dbus_owner = nm_secret_agent_get_dbus_owner (agent);
-       req->complete_callback (req, NULL, agent_dbus_owner, FALSE, NULL, req->complete_callback_data);
+       req_complete_success (req, NULL, NULL, agent_dbus_owner, FALSE);
 }
 
 static void
@@ -1107,6 +1144,7 @@ static void
 save_complete_cb (Request *req,
                   GHashTable *secrets,
                   const char *agent_dbus_owner,
+                  const char *agent_username,
                   gboolean agent_has_modify,
                   GError *error,
                   gpointer user_data)
@@ -1203,6 +1241,7 @@ static void
 delete_complete_cb (Request *req,
                     GHashTable *secrets,
                     const char *agent_dbus_owner,
+                    const char *agent_username,
                     gboolean agent_has_modify,
                     GError *error,
                     gpointer user_data)
index b17b91a..788a917 100644 (file)
@@ -49,6 +49,7 @@ NMAgentManager *nm_agent_manager_get (void);
 typedef void (*NMAgentSecretsResultFunc) (NMAgentManager *manager,
                                           guint32 call_id,
                                           const char *agent_dbus_owner,
+                                          const char *agent_uname,
                                           gboolean agent_has_modify,
                                           const char *setting_name,
                                           NMSettingsGetSecretsFlags flags,
index a088c4b..22aef71 100644 (file)
@@ -486,6 +486,7 @@ static void
 agent_secrets_done_cb (NMAgentManager *manager,
                        guint32 call_id,
                        const char *agent_dbus_owner,
+                       const char *agent_username,
                        gboolean agent_has_modify,
                        const char *setting_name,
                        NMSettingsGetSecretsFlags flags,
@@ -511,7 +512,7 @@ agent_secrets_done_cb (NMAgentManager *manager,
                            error->code,
                            error->message ? error->message : "(unknown)");
 
-               callback (self, call_id, setting_name, error, callback_data);
+               callback (self, call_id, NULL, setting_name, error, callback_data);
                return;
        }
 
@@ -519,7 +520,7 @@ agent_secrets_done_cb (NMAgentManager *manager,
                local = g_error_new (NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_SETTING,
                                     "%s.%d - Connection didn't have requested setting '%s'.",
                                     __FILE__, __LINE__, setting_name);
-               callback (self, call_id, setting_name, local, callback_data);
+               callback (self, call_id, NULL, setting_name, local, callback_data);
                g_clear_error (&local);
                return;
        }
@@ -631,7 +632,7 @@ agent_secrets_done_cb (NMAgentManager *manager,
                            (local && local->message) ? local->message : "(unknown)");
        }
 
-       callback (self, call_id, setting_name, local, callback_data);
+       callback (self, call_id, agent_username, setting_name, local, callback_data);
        g_clear_error (&local);
        if (hash)
                g_hash_table_destroy (hash);
@@ -1167,6 +1168,7 @@ impl_settings_connection_delete (NMSettingsConnection *self,
 static void
 dbus_get_agent_secrets_cb (NMSettingsConnection *self,
                            guint32 call_id,
+                           const char *agent_username,
                            const char *setting_name,
                            GError *error,
                            gpointer user_data)
index cb5fcb6..116bfdc 100644 (file)
@@ -94,6 +94,7 @@ void nm_settings_connection_delete (NMSettingsConnection *connection,
 
 typedef void (*NMSettingsConnectionSecretsFunc) (NMSettingsConnection *connection,
                                                  guint32 call_id,
+                                                 const char *agent_username,
                                                  const char *setting_name,
                                                  GError *error,
                                                  gpointer user_data);
index 74be317..c6f1407 100644 (file)
@@ -765,6 +765,7 @@ nm_vpn_connection_disconnect (NMVPNConnection *connection,
 static void
 vpn_secrets_cb (NMSettingsConnection *connection,
                 guint32 call_id,
+                const char *agent_username,
                 const char *setting_name,
                 GError *error,
                 gpointer user_data)
@@ -836,6 +837,7 @@ connection_need_secrets_cb  (DBusGProxy *proxy,
 static void
 existing_secrets_cb (NMSettingsConnection *connection,
                      guint32 call_id,
+                     const char *agent_username,
                      const char *setting_name,
                      GError *error,
                      gpointer user_data)