2010-12-10 12:38:19 -06:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
|
|
|
|
/* NetworkManager -- Network link manager
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*
|
2011-01-18 14:57:44 -06:00
|
|
|
* Copyright (C) 2010 - 2011 Red Hat, Inc.
|
2010-12-10 12:38:19 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <dbus/dbus-glib.h>
|
|
|
|
|
#include <dbus/dbus-glib-lowlevel.h>
|
|
|
|
|
|
|
|
|
|
#include "NetworkManager.h"
|
2010-12-10 13:32:45 -06:00
|
|
|
#include "nm-logging.h"
|
2010-12-10 12:38:19 -06:00
|
|
|
#include "nm-agent-manager.h"
|
|
|
|
|
#include "nm-secret-agent.h"
|
|
|
|
|
#include "nm-manager-auth.h"
|
2010-12-14 15:34:34 -06:00
|
|
|
#include "nm-dbus-glib-types.h"
|
2010-12-10 12:38:19 -06:00
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (NMAgentManager, nm_agent_manager, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
|
|
#define NM_AGENT_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
|
|
|
|
|
NM_TYPE_AGENT_MANAGER, \
|
|
|
|
|
NMAgentManagerPrivate))
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
gboolean disposed;
|
|
|
|
|
|
|
|
|
|
NMDBusManager *dbus_mgr;
|
|
|
|
|
NMSessionMonitor *session_monitor;
|
|
|
|
|
|
|
|
|
|
/* Hashed by owner name, not identifier, since two agents in different
|
|
|
|
|
* sessions can use the same identifier.
|
|
|
|
|
*/
|
|
|
|
|
GHashTable *agents;
|
2010-12-13 13:11:51 -06:00
|
|
|
|
|
|
|
|
GHashTable *requests;
|
2010-12-10 12:38:19 -06:00
|
|
|
} NMAgentManagerPrivate;
|
|
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
typedef struct _Request Request;
|
|
|
|
|
|
|
|
|
|
static void request_add_agent (Request *req,
|
|
|
|
|
NMSecretAgent *agent,
|
|
|
|
|
NMSessionMonitor *session_monitor);
|
|
|
|
|
|
|
|
|
|
static void request_remove_agent (Request *req, NMSecretAgent *agent);
|
2010-12-10 12:38:19 -06:00
|
|
|
|
|
|
|
|
static void impl_agent_manager_register (NMAgentManager *self,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
DBusGMethodInvocation *context);
|
|
|
|
|
|
|
|
|
|
static void impl_agent_manager_unregister (NMAgentManager *self,
|
|
|
|
|
DBusGMethodInvocation *context);
|
|
|
|
|
|
|
|
|
|
#include "nm-agent-manager-glue.h"
|
|
|
|
|
|
|
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
|
|
#define NM_AGENT_MANAGER_ERROR (nm_agent_manager_error_quark ())
|
|
|
|
|
#define NM_TYPE_AGENT_MANAGER_ERROR (nm_agent_manager_error_get_type ())
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_SENDER_UNKNOWN = 0,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_PERMISSION_DENIED,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_SESSION_NOT_FOUND,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_NOT_REGISTERED,
|
2010-12-14 15:34:34 -06:00
|
|
|
NM_AGENT_MANAGER_ERROR_INTERNAL_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_NO_SECRETS
|
2010-12-10 12:38:19 -06:00
|
|
|
} NMAgentManagerError;
|
|
|
|
|
|
|
|
|
|
static GQuark
|
|
|
|
|
nm_agent_manager_error_quark (void)
|
|
|
|
|
{
|
|
|
|
|
static GQuark ret = 0;
|
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (ret == 0))
|
|
|
|
|
ret = g_quark_from_static_string ("nm-agent-manager-error");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
|
|
|
|
|
|
|
|
|
|
static GType
|
|
|
|
|
nm_agent_manager_error_get_type (void)
|
|
|
|
|
{
|
|
|
|
|
static GType etype = 0;
|
|
|
|
|
|
|
|
|
|
if (etype == 0) {
|
|
|
|
|
static const GEnumValue values[] = {
|
|
|
|
|
/* Unable to determine caller's sender or UID */
|
|
|
|
|
ENUM_ENTRY (NM_AGENT_MANAGER_ERROR_SENDER_UNKNOWN, "SenderUnknown"),
|
|
|
|
|
/* Permission for some operation was denied */
|
|
|
|
|
ENUM_ENTRY (NM_AGENT_MANAGER_ERROR_PERMISSION_DENIED, "PermissionDenied"),
|
|
|
|
|
/* The caller's session could not be determined */
|
|
|
|
|
ENUM_ENTRY (NM_AGENT_MANAGER_ERROR_SESSION_NOT_FOUND, "SessionNotFound"),
|
|
|
|
|
/* The identifier was invalid */
|
|
|
|
|
ENUM_ENTRY (NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER, "InvalidIdentifier"),
|
|
|
|
|
/* Request was not from a registered agent */
|
|
|
|
|
ENUM_ENTRY (NM_AGENT_MANAGER_ERROR_NOT_REGISTERED, "NotRegistered"),
|
|
|
|
|
/* Some internal error occurred */
|
|
|
|
|
ENUM_ENTRY (NM_AGENT_MANAGER_ERROR_INTERNAL_ERROR, "InternalError"),
|
2010-12-14 15:34:34 -06:00
|
|
|
/* No secrets were available */
|
|
|
|
|
ENUM_ENTRY (NM_AGENT_MANAGER_ERROR_NO_SECRETS, "NoSecrets"),
|
2010-12-10 12:38:19 -06:00
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
etype = g_enum_register_static ("NMAgentManagerError", values);
|
|
|
|
|
}
|
|
|
|
|
return etype;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
remove_agent (NMAgentManager *self, const char *owner)
|
|
|
|
|
{
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
NMSecretAgent *agent;
|
2010-12-14 15:34:34 -06:00
|
|
|
GHashTableIter iter;
|
|
|
|
|
gpointer data;
|
2010-12-10 12:38:19 -06:00
|
|
|
|
|
|
|
|
g_return_val_if_fail (owner != NULL, FALSE);
|
|
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
/* Make sure this agent has already registered */
|
2010-12-10 12:38:19 -06:00
|
|
|
agent = g_hash_table_lookup (priv->agents, owner);
|
|
|
|
|
if (!agent)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent unregistered",
|
|
|
|
|
nm_secret_agent_get_description (agent));
|
2010-12-10 12:38:19 -06:00
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
/* Remove this agent to any in-progress secrets requests */
|
|
|
|
|
g_hash_table_iter_init (&iter, priv->requests);
|
|
|
|
|
while (g_hash_table_iter_next (&iter, NULL, &data))
|
|
|
|
|
request_remove_agent ((Request *) data, agent);
|
2010-12-10 13:32:45 -06:00
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
/* And dispose of the agent */
|
2010-12-10 12:38:19 -06:00
|
|
|
g_hash_table_remove (priv->agents, owner);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
validate_identifier (const char *identifier, GError **error)
|
|
|
|
|
{
|
|
|
|
|
const char *p = identifier;
|
|
|
|
|
size_t id_len;
|
|
|
|
|
|
|
|
|
|
if (!identifier) {
|
|
|
|
|
g_set_error_literal (error,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER,
|
|
|
|
|
"No identifier was given");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Length between 3 and 255 characters inclusive */
|
|
|
|
|
id_len = strlen (identifier);
|
|
|
|
|
if (id_len < 3 || id_len > 255) {
|
|
|
|
|
g_set_error_literal (error,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER,
|
|
|
|
|
"Identifier length not between 3 and 255 characters (inclusive)");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((identifier[0] == '.') || (identifier[id_len - 1] == '.')) {
|
|
|
|
|
g_set_error_literal (error,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER,
|
|
|
|
|
"Identifier must not start or end with '.'");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* FIXME: do complete validation here */
|
|
|
|
|
while (p && *p) {
|
2010-12-14 11:19:39 -06:00
|
|
|
if (!isalnum (*p) && (*p != '_') && (*p != '-') && (*p != '.')) {
|
2010-12-10 12:38:19 -06:00
|
|
|
g_set_error (error,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER,
|
|
|
|
|
"Identifier contains invalid character '%c'", *p);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((*p == '.') && (*(p + 1) == '.')) {
|
|
|
|
|
g_set_error_literal (error,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER,
|
|
|
|
|
"Identifier contains two '.' characters in sequence");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2010-12-14 11:14:24 -06:00
|
|
|
p++;
|
2010-12-10 12:38:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
impl_agent_manager_register (NMAgentManager *self,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
DBusGMethodInvocation *context)
|
|
|
|
|
{
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
char *error_desc = NULL, *sender = NULL;
|
|
|
|
|
gulong sender_uid = G_MAXULONG;
|
|
|
|
|
GError *error = NULL, *local = NULL;
|
|
|
|
|
NMSecretAgent *agent;
|
2010-12-14 15:34:34 -06:00
|
|
|
GHashTableIter iter;
|
|
|
|
|
gpointer data;
|
2010-12-10 12:38:19 -06:00
|
|
|
|
|
|
|
|
if (!nm_auth_get_caller_uid (context,
|
|
|
|
|
priv->dbus_mgr,
|
|
|
|
|
&sender_uid,
|
|
|
|
|
&error_desc)) {
|
|
|
|
|
error = g_error_new_literal (NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_SENDER_UNKNOWN,
|
|
|
|
|
error_desc);
|
|
|
|
|
g_free (error_desc);
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!nm_session_monitor_uid_has_session (priv->session_monitor,
|
|
|
|
|
sender_uid,
|
|
|
|
|
NULL,
|
|
|
|
|
&local)) {
|
|
|
|
|
error = g_error_new_literal (NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_SESSION_NOT_FOUND,
|
|
|
|
|
local && local->message ? local->message : "Session not found");
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sender = dbus_g_method_get_sender (context);
|
|
|
|
|
if (!sender) {
|
|
|
|
|
error = g_error_new_literal (NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_SENDER_UNKNOWN,
|
|
|
|
|
"Failed to get D-Bus request sender");
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Validate the identifier */
|
|
|
|
|
if (!validate_identifier (identifier, &error))
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
|
|
/* Success, add the new agent */
|
2010-12-14 15:34:34 -06:00
|
|
|
agent = nm_secret_agent_new (priv->dbus_mgr, sender, identifier, sender_uid);
|
2010-12-10 12:38:19 -06:00
|
|
|
if (!agent) {
|
|
|
|
|
error = g_error_new_literal (NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_INTERNAL_ERROR,
|
|
|
|
|
"Failed to initialize the agent");
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_hash_table_insert (priv->agents, g_strdup (sender), agent);
|
2010-12-14 15:34:34 -06:00
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent registered",
|
|
|
|
|
nm_secret_agent_get_description (agent));
|
2010-12-10 12:38:19 -06:00
|
|
|
dbus_g_method_return (context);
|
|
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
/* Add this agent to any in-progress secrets requests */
|
|
|
|
|
g_hash_table_iter_init (&iter, priv->requests);
|
|
|
|
|
while (g_hash_table_iter_next (&iter, NULL, &data))
|
|
|
|
|
request_add_agent ((Request *) data, agent, priv->session_monitor);
|
|
|
|
|
|
2010-12-10 12:38:19 -06:00
|
|
|
done:
|
|
|
|
|
if (error)
|
|
|
|
|
dbus_g_method_return_error (context, error);
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
g_clear_error (&local);
|
|
|
|
|
g_free (sender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
impl_agent_manager_unregister (NMAgentManager *self,
|
|
|
|
|
DBusGMethodInvocation *context)
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
char *sender = NULL;
|
|
|
|
|
|
|
|
|
|
sender = dbus_g_method_get_sender (context);
|
|
|
|
|
if (!sender) {
|
|
|
|
|
error = g_error_new_literal (NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_SENDER_UNKNOWN,
|
|
|
|
|
"Failed to get D-Bus request sender");
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Found the agent, unregister and remove it */
|
|
|
|
|
if (!remove_agent (self, sender)) {
|
|
|
|
|
error = g_error_new_literal (NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_AGENT_MANAGER_ERROR_NOT_REGISTERED,
|
|
|
|
|
"Caller is not registered as an Agent");
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_g_method_return (context);
|
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
if (error)
|
|
|
|
|
dbus_g_method_return_error (context, error);
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
g_free (sender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
2010-12-13 13:11:51 -06:00
|
|
|
typedef void (*RequestCompleteFunc) (Request *req,
|
|
|
|
|
GHashTable *secrets,
|
|
|
|
|
GError *error,
|
|
|
|
|
gpointer user_data);
|
2011-01-31 23:10:33 -06:00
|
|
|
typedef void (*RequestNextFunc) (Request *req);
|
|
|
|
|
typedef void (*RequestCancelFunc) (Request *req);
|
2010-12-13 13:11:51 -06:00
|
|
|
|
|
|
|
|
struct _Request {
|
|
|
|
|
guint32 reqid;
|
|
|
|
|
|
|
|
|
|
NMConnection *connection;
|
2011-01-26 18:36:08 -06:00
|
|
|
gboolean filter_by_uid;
|
|
|
|
|
gulong uid_filter;
|
2010-12-13 13:11:51 -06:00
|
|
|
char *setting_name;
|
2011-01-18 14:57:44 -06:00
|
|
|
guint32 flags;
|
2010-12-13 13:11:51 -06:00
|
|
|
char *hint;
|
|
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
/* Current agent being asked for secrets */
|
|
|
|
|
NMSecretAgent *current;
|
2011-01-18 13:19:29 -06:00
|
|
|
gconstpointer current_call_id;
|
2010-12-14 15:34:34 -06:00
|
|
|
|
|
|
|
|
/* Stores the sorted list of NMSecretAgents which will be
|
|
|
|
|
* asked for secrets.
|
|
|
|
|
*/
|
|
|
|
|
GSList *pending;
|
|
|
|
|
|
|
|
|
|
/* Stores the list of NMSecretAgent hashes that we've already
|
|
|
|
|
* asked for secrets, so that we don't ask the same agent twice
|
|
|
|
|
* if it quits and re-registers during this secrets request.
|
|
|
|
|
*/
|
|
|
|
|
GSList *asked;
|
|
|
|
|
|
2010-12-13 13:11:51 -06:00
|
|
|
guint32 idle_id;
|
|
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
GHashTable *existing_secrets;
|
2010-12-22 15:12:12 -06:00
|
|
|
|
2010-12-13 13:11:51 -06:00
|
|
|
NMAgentSecretsResultFunc callback;
|
|
|
|
|
gpointer callback_data;
|
|
|
|
|
gpointer other_data2;
|
|
|
|
|
gpointer other_data3;
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
RequestCancelFunc cancel_callback;
|
|
|
|
|
RequestNextFunc next_callback;
|
2010-12-13 13:11:51 -06:00
|
|
|
RequestCompleteFunc complete_callback;
|
2010-12-14 15:34:34 -06:00
|
|
|
gpointer complete_callback_data;
|
2010-12-13 13:11:51 -06:00
|
|
|
};
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
static guint32 next_req_id = 1;
|
|
|
|
|
|
2010-12-13 13:11:51 -06:00
|
|
|
static Request *
|
2011-01-31 23:10:33 -06:00
|
|
|
request_new_get (NMConnection *connection,
|
|
|
|
|
gboolean filter_by_uid,
|
|
|
|
|
gulong uid_filter,
|
|
|
|
|
GHashTable *existing_secrets,
|
|
|
|
|
const char *setting_name,
|
|
|
|
|
guint32 flags,
|
|
|
|
|
const char *hint,
|
|
|
|
|
NMAgentSecretsResultFunc callback,
|
|
|
|
|
gpointer callback_data,
|
|
|
|
|
gpointer other_data2,
|
|
|
|
|
gpointer other_data3,
|
|
|
|
|
RequestCompleteFunc complete_callback,
|
|
|
|
|
gpointer complete_callback_data,
|
|
|
|
|
RequestNextFunc next_callback,
|
|
|
|
|
RequestCancelFunc cancel_callback)
|
2010-12-13 13:11:51 -06:00
|
|
|
{
|
|
|
|
|
Request *req;
|
|
|
|
|
|
|
|
|
|
req = g_malloc0 (sizeof (Request));
|
2011-01-31 23:10:33 -06:00
|
|
|
req->reqid = next_req_id++;
|
2010-12-13 13:11:51 -06:00
|
|
|
req->connection = g_object_ref (connection);
|
2011-01-26 18:36:08 -06:00
|
|
|
req->filter_by_uid = filter_by_uid;
|
|
|
|
|
req->uid_filter = uid_filter;
|
2011-01-31 23:10:33 -06:00
|
|
|
if (existing_secrets)
|
|
|
|
|
req->existing_secrets = g_hash_table_ref (existing_secrets);
|
2010-12-13 13:11:51 -06:00
|
|
|
req->setting_name = g_strdup (setting_name);
|
2011-01-18 14:57:44 -06:00
|
|
|
req->flags = flags;
|
2010-12-13 13:11:51 -06:00
|
|
|
req->hint = g_strdup (hint);
|
|
|
|
|
req->callback = callback;
|
|
|
|
|
req->callback_data = callback_data;
|
|
|
|
|
req->other_data2 = other_data2;
|
|
|
|
|
req->other_data3 = other_data3;
|
2010-12-14 15:34:34 -06:00
|
|
|
req->complete_callback = complete_callback;
|
|
|
|
|
req->complete_callback_data = complete_callback_data;
|
2011-01-31 23:10:33 -06:00
|
|
|
req->next_callback = next_callback;
|
|
|
|
|
req->cancel_callback = cancel_callback;
|
|
|
|
|
|
|
|
|
|
return req;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Request *
|
|
|
|
|
request_new_save (NMConnection *connection,
|
|
|
|
|
gboolean filter_by_uid,
|
|
|
|
|
gulong uid_filter,
|
|
|
|
|
RequestCompleteFunc complete_callback,
|
|
|
|
|
gpointer complete_callback_data,
|
|
|
|
|
RequestNextFunc next_callback)
|
|
|
|
|
{
|
|
|
|
|
Request *req;
|
|
|
|
|
|
|
|
|
|
req = g_malloc0 (sizeof (Request));
|
|
|
|
|
req->reqid = next_req_id++;
|
|
|
|
|
req->connection = g_object_ref (connection);
|
|
|
|
|
req->filter_by_uid = filter_by_uid;
|
|
|
|
|
req->uid_filter = uid_filter;
|
|
|
|
|
req->complete_callback = complete_callback;
|
|
|
|
|
req->complete_callback_data = complete_callback_data;
|
|
|
|
|
req->next_callback = next_callback;
|
2010-12-13 13:11:51 -06:00
|
|
|
|
|
|
|
|
return req;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
request_free (Request *req)
|
|
|
|
|
{
|
|
|
|
|
if (req->idle_id)
|
|
|
|
|
g_source_remove (req->idle_id);
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
req->cancel_callback (req);
|
2010-12-14 15:34:34 -06:00
|
|
|
|
|
|
|
|
g_slist_free (req->pending);
|
|
|
|
|
g_slist_free (req->asked);
|
2010-12-13 13:11:51 -06:00
|
|
|
g_object_unref (req->connection);
|
|
|
|
|
g_free (req->setting_name);
|
|
|
|
|
g_free (req->hint);
|
2011-01-27 10:41:02 -06:00
|
|
|
if (req->existing_secrets)
|
|
|
|
|
g_hash_table_unref (req->existing_secrets);
|
2010-12-13 13:11:51 -06:00
|
|
|
memset (req, 0, sizeof (Request));
|
|
|
|
|
g_free (req);
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
static gint
|
|
|
|
|
agent_compare_func (NMSecretAgent *a, NMSecretAgent *b, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
NMSessionMonitor *session_monitor = NM_SESSION_MONITOR (user_data);
|
|
|
|
|
gboolean a_active, b_active;
|
|
|
|
|
|
|
|
|
|
if (a && !b)
|
|
|
|
|
return -1;
|
|
|
|
|
else if (a == b)
|
|
|
|
|
return 0;
|
|
|
|
|
else if (!a && b)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
/* Prefer agents in active sessions */
|
|
|
|
|
a_active = nm_session_monitor_uid_active (session_monitor,
|
|
|
|
|
nm_secret_agent_get_owner_uid (a),
|
|
|
|
|
NULL);
|
|
|
|
|
b_active = nm_session_monitor_uid_active (session_monitor,
|
|
|
|
|
nm_secret_agent_get_owner_uid (b),
|
|
|
|
|
NULL);
|
|
|
|
|
if (a_active && !b_active)
|
|
|
|
|
return -1;
|
|
|
|
|
else if (a_active == b_active)
|
|
|
|
|
return 0;
|
|
|
|
|
else if (!a_active && b_active)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2010-12-14 15:34:34 -06:00
|
|
|
|
|
|
|
|
static void
|
2011-01-31 23:10:33 -06:00
|
|
|
request_add_agent (Request *req,
|
|
|
|
|
NMSecretAgent *agent,
|
|
|
|
|
NMSessionMonitor *session_monitor)
|
|
|
|
|
{
|
|
|
|
|
uid_t agent_uid;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (req != NULL);
|
|
|
|
|
g_return_if_fail (agent != NULL);
|
|
|
|
|
|
|
|
|
|
if (g_slist_find (req->asked, GUINT_TO_POINTER (nm_secret_agent_get_hash (agent))))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Ensure the caller's username exists in the connection's permissions,
|
|
|
|
|
* or that the permissions is empty (ie, visible by everyone).
|
|
|
|
|
*/
|
|
|
|
|
agent_uid = nm_secret_agent_get_owner_uid (agent);
|
|
|
|
|
if (0 != agent_uid) {
|
|
|
|
|
if (!nm_auth_uid_in_acl (req->connection, session_monitor, agent_uid, NULL)) {
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent ignored for secrets request %p/%s (not in ACL)",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name);
|
|
|
|
|
/* Connection not visible to this agent's user */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* Caller is allowed to manipulate this connection */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If the request should filter agents by UID, do that now */
|
|
|
|
|
if (req->filter_by_uid && (agent_uid != req->uid_filter)) {
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent ignored for secrets request %p/%s "
|
|
|
|
|
"(uid %ld not required %ld)",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name, agent_uid, req->uid_filter);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent allowed for secrets request %p/%s",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name);
|
|
|
|
|
|
|
|
|
|
/* Add this agent to the list, preferring active sessions */
|
|
|
|
|
req->pending = g_slist_insert_sorted_with_data (req->pending,
|
|
|
|
|
agent,
|
|
|
|
|
(GCompareDataFunc) agent_compare_func,
|
|
|
|
|
session_monitor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
request_add_agents (NMAgentManager *self, Request *req)
|
|
|
|
|
{
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
GHashTableIter iter;
|
|
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, priv->agents);
|
|
|
|
|
while (g_hash_table_iter_next (&iter, NULL, &data))
|
|
|
|
|
request_add_agent (req, NM_SECRET_AGENT (data), priv->session_monitor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
request_remove_agent (Request *req, NMSecretAgent *agent)
|
|
|
|
|
{
|
|
|
|
|
gboolean try_next = FALSE;
|
|
|
|
|
const char *detail = "";
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (req != NULL);
|
|
|
|
|
g_return_if_fail (agent != NULL);
|
|
|
|
|
|
|
|
|
|
/* If this agent is being asked right now, cancel the request */
|
|
|
|
|
if (agent == req->current) {
|
|
|
|
|
req->cancel_callback (req);
|
|
|
|
|
req->current = NULL;
|
|
|
|
|
req->current_call_id = NULL;
|
|
|
|
|
try_next = TRUE;
|
|
|
|
|
detail = " current";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s)%s agent removed from secrets request %p/%s",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
detail, req, req->setting_name);
|
|
|
|
|
|
|
|
|
|
req->pending = g_slist_remove (req->pending, agent);
|
|
|
|
|
|
|
|
|
|
if (try_next) {
|
|
|
|
|
/* If the agent serving the in-progress secrets request went away then
|
|
|
|
|
* we need to send the request to the next agent.
|
|
|
|
|
*/
|
|
|
|
|
req->next_callback (req);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
next_generic (Request *req, const char *detail)
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gboolean success = FALSE;
|
|
|
|
|
|
|
|
|
|
if (req->pending == NULL) {
|
|
|
|
|
/* No more secret agents are available to fulfill this secrets request */
|
|
|
|
|
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, error, req->complete_callback_data);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
} else {
|
|
|
|
|
/* Send a secrets request to the next agent */
|
|
|
|
|
req->current = req->pending->data;
|
|
|
|
|
req->pending = g_slist_remove (req->pending, req->current);
|
|
|
|
|
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent %s secrets for request %p/%s",
|
|
|
|
|
nm_secret_agent_get_description (req->current),
|
|
|
|
|
detail, req, req->setting_name);
|
|
|
|
|
success = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
get_done_cb (NMSecretAgent *agent,
|
|
|
|
|
gconstpointer call_id,
|
|
|
|
|
GHashTable *secrets,
|
|
|
|
|
GError *error,
|
|
|
|
|
gpointer user_data)
|
2010-12-14 15:34:34 -06:00
|
|
|
{
|
|
|
|
|
Request *req = user_data;
|
2011-01-27 10:41:02 -06:00
|
|
|
GHashTable *setting_secrets;
|
2010-12-14 15:34:34 -06:00
|
|
|
|
2011-01-18 13:19:29 -06:00
|
|
|
g_return_if_fail (call_id == req->current_call_id);
|
2010-12-14 15:34:34 -06:00
|
|
|
|
|
|
|
|
req->current = NULL;
|
|
|
|
|
req->current_call_id = NULL;
|
|
|
|
|
|
2011-01-18 13:19:29 -06:00
|
|
|
if (error) {
|
2010-12-14 15:34:34 -06:00
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent failed secrets request %p/%s: (%d) %s",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name,
|
|
|
|
|
error ? error->code : -1,
|
|
|
|
|
(error && error->message) ? error->message : "(unknown)");
|
|
|
|
|
|
|
|
|
|
/* Try the next agent */
|
2011-01-31 23:10:33 -06:00
|
|
|
req->next_callback (req);
|
2010-12-14 15:34:34 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Ensure the setting we wanted secrets for got returned and has something in it */
|
|
|
|
|
setting_secrets = g_hash_table_lookup (secrets, req->setting_name);
|
|
|
|
|
if (!setting_secrets || !g_hash_table_size (setting_secrets)) {
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent returned no secrets for request %p/%s",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name);
|
|
|
|
|
|
|
|
|
|
/* Try the next agent */
|
2011-01-31 23:10:33 -06:00
|
|
|
req->next_callback (req);
|
2010-12-14 15:34:34 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent returned secrets for request %p/%s",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name);
|
|
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
req->complete_callback (req, secrets, NULL, req->complete_callback_data);
|
2010-12-14 15:34:34 -06:00
|
|
|
}
|
|
|
|
|
|
2010-12-13 13:11:51 -06:00
|
|
|
static void
|
2011-01-31 23:10:33 -06:00
|
|
|
get_next_cb (Request *req)
|
2010-12-13 13:11:51 -06:00
|
|
|
{
|
2011-01-31 23:10:33 -06:00
|
|
|
if (!next_generic (req, "getting"))
|
2010-12-14 15:34:34 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
req->current_call_id = nm_secret_agent_get_secrets (NM_SECRET_AGENT (req->current),
|
|
|
|
|
req->connection,
|
|
|
|
|
req->setting_name,
|
|
|
|
|
req->hint,
|
2011-01-18 14:57:44 -06:00
|
|
|
req->flags,
|
2011-01-31 23:10:33 -06:00
|
|
|
get_done_cb,
|
2010-12-14 15:34:34 -06:00
|
|
|
req);
|
|
|
|
|
if (req->current_call_id == NULL) {
|
|
|
|
|
/* Shouldn't hit this, but handle it anyway */
|
|
|
|
|
g_warn_if_fail (req->current_call_id != NULL);
|
|
|
|
|
req->current = NULL;
|
2011-01-31 23:10:33 -06:00
|
|
|
req->next_callback (req);
|
2010-12-14 15:34:34 -06:00
|
|
|
}
|
2010-12-13 13:11:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2011-01-31 23:10:33 -06:00
|
|
|
get_start (gpointer user_data)
|
2010-12-13 13:11:51 -06:00
|
|
|
{
|
|
|
|
|
Request *req = user_data;
|
2011-01-27 10:41:02 -06:00
|
|
|
GHashTable *setting_secrets = NULL;
|
2010-12-13 13:11:51 -06:00
|
|
|
|
|
|
|
|
req->idle_id = 0;
|
|
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
/* Check if there are any existing secrets */
|
|
|
|
|
if (req->existing_secrets)
|
|
|
|
|
setting_secrets = g_hash_table_lookup (req->existing_secrets, req->setting_name);
|
2010-12-22 15:12:12 -06:00
|
|
|
|
|
|
|
|
if (setting_secrets && g_hash_table_size (setting_secrets)) {
|
|
|
|
|
NMConnection *tmp;
|
2011-01-27 10:41:02 -06:00
|
|
|
GError *error = NULL;
|
2010-12-22 15:12:12 -06:00
|
|
|
|
|
|
|
|
/* The connection already had secrets; check if any more are required.
|
|
|
|
|
* If no more are required, we're done. If secrets are still needed,
|
|
|
|
|
* ask a secret agent for more. This allows admins to provide generic
|
|
|
|
|
* secrets but allow additional user-specific ones as well.
|
|
|
|
|
*/
|
|
|
|
|
tmp = nm_connection_duplicate (req->connection);
|
|
|
|
|
g_assert (tmp);
|
|
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
if (!nm_connection_update_secrets (tmp, req->setting_name, req->existing_secrets, &error)) {
|
2010-12-22 15:12:12 -06:00
|
|
|
req->complete_callback (req, NULL, error, req->complete_callback_data);
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
} else {
|
|
|
|
|
/* Do we have everything we need? */
|
|
|
|
|
/* FIXME: handle second check for VPN connections */
|
|
|
|
|
if (nm_connection_need_secrets (tmp, NULL) == NULL) {
|
2011-01-19 16:58:26 -06:00
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%p/%s) system settings secrets sufficient",
|
|
|
|
|
req, req->setting_name);
|
|
|
|
|
|
2010-12-22 15:12:12 -06:00
|
|
|
/* Got everything, we're done */
|
2011-01-27 10:41:02 -06:00
|
|
|
req->complete_callback (req, req->existing_secrets, NULL, req->complete_callback_data);
|
2010-12-22 15:12:12 -06:00
|
|
|
} else {
|
2011-01-19 16:58:26 -06:00
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%p/%s) system settings secrets insufficient, asking agents",
|
|
|
|
|
req, req->setting_name);
|
|
|
|
|
|
2010-12-22 15:12:12 -06:00
|
|
|
/* We don't, so ask some agents for additional secrets */
|
2011-01-31 23:10:33 -06:00
|
|
|
req->next_callback (req);
|
2010-12-22 15:12:12 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_object_unref (tmp);
|
2010-12-14 15:34:34 -06:00
|
|
|
} else {
|
2010-12-13 13:11:51 -06:00
|
|
|
/* Couldn't get secrets from system settings, so now we ask the
|
|
|
|
|
* agents for secrets. Let the Agent Manager handle which agents
|
|
|
|
|
* we'll ask and in which order.
|
|
|
|
|
*/
|
2011-01-31 23:10:33 -06:00
|
|
|
req->next_callback (req);
|
2010-12-13 13:11:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2011-01-31 23:10:33 -06:00
|
|
|
get_complete_cb (Request *req,
|
|
|
|
|
GHashTable *secrets,
|
|
|
|
|
GError *error,
|
|
|
|
|
gpointer user_data)
|
2010-12-13 13:11:51 -06:00
|
|
|
{
|
|
|
|
|
NMAgentManager *self = NM_AGENT_MANAGER (user_data);
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
/* Send secrets back to the requesting object */
|
2010-12-13 13:11:51 -06:00
|
|
|
req->callback (self,
|
|
|
|
|
req->reqid,
|
2011-01-26 13:32:25 -06:00
|
|
|
req->setting_name,
|
2011-01-27 10:41:02 -06:00
|
|
|
error ? NULL : secrets,
|
|
|
|
|
error,
|
2010-12-13 13:11:51 -06:00
|
|
|
req->callback_data,
|
|
|
|
|
req->other_data2,
|
|
|
|
|
req->other_data3);
|
|
|
|
|
|
|
|
|
|
g_hash_table_remove (priv->requests, GUINT_TO_POINTER (req->reqid));
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
static void
|
|
|
|
|
get_cancel_cb (Request *req)
|
|
|
|
|
{
|
|
|
|
|
if (req->current && req->current_call_id)
|
|
|
|
|
nm_secret_agent_cancel_secrets (req->current, req->current_call_id);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-13 13:11:51 -06:00
|
|
|
guint32
|
|
|
|
|
nm_agent_manager_get_secrets (NMAgentManager *self,
|
|
|
|
|
NMConnection *connection,
|
2011-01-26 18:36:08 -06:00
|
|
|
gboolean filter_by_uid,
|
|
|
|
|
gulong uid_filter,
|
2011-01-27 10:41:02 -06:00
|
|
|
GHashTable *existing_secrets,
|
2010-12-13 13:11:51 -06:00
|
|
|
const char *setting_name,
|
2011-01-18 14:57:44 -06:00
|
|
|
guint32 flags,
|
2010-12-13 13:11:51 -06:00
|
|
|
const char *hint,
|
|
|
|
|
NMAgentSecretsResultFunc callback,
|
|
|
|
|
gpointer callback_data,
|
|
|
|
|
gpointer other_data2,
|
|
|
|
|
gpointer other_data3)
|
|
|
|
|
{
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
Request *req;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, 0);
|
|
|
|
|
g_return_val_if_fail (connection != NULL, 0);
|
2011-01-27 10:41:02 -06:00
|
|
|
g_return_val_if_fail (NM_IS_CONNECTION (connection), 0);
|
2010-12-13 13:11:51 -06:00
|
|
|
g_return_val_if_fail (callback != NULL, 0);
|
|
|
|
|
|
2010-12-14 15:34:34 -06:00
|
|
|
nm_log_dbg (LOGD_SETTINGS,
|
|
|
|
|
"Secrets requested for connection %s (%s)",
|
|
|
|
|
nm_connection_get_path (connection),
|
|
|
|
|
setting_name);
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
req = request_new_get (connection,
|
|
|
|
|
filter_by_uid,
|
|
|
|
|
uid_filter,
|
|
|
|
|
existing_secrets,
|
|
|
|
|
setting_name,
|
|
|
|
|
flags,
|
|
|
|
|
hint,
|
|
|
|
|
callback,
|
|
|
|
|
callback_data,
|
|
|
|
|
other_data2,
|
|
|
|
|
other_data3,
|
|
|
|
|
get_complete_cb,
|
|
|
|
|
self,
|
|
|
|
|
get_next_cb,
|
|
|
|
|
get_cancel_cb);
|
2010-12-13 13:11:51 -06:00
|
|
|
g_hash_table_insert (priv->requests, GUINT_TO_POINTER (req->reqid), req);
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
/* Kick off the request */
|
|
|
|
|
request_add_agents (self, req);
|
|
|
|
|
req->idle_id = g_idle_add (get_start, req);
|
2010-12-13 13:11:51 -06:00
|
|
|
|
|
|
|
|
return req->reqid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nm_agent_manager_cancel_secrets (NMAgentManager *self,
|
|
|
|
|
guint32 request_id)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
g_return_if_fail (request_id > 0);
|
|
|
|
|
|
|
|
|
|
g_hash_table_remove (NM_AGENT_MANAGER_GET_PRIVATE (self)->requests,
|
|
|
|
|
GUINT_TO_POINTER (request_id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
2011-01-31 23:10:33 -06:00
|
|
|
static void
|
|
|
|
|
save_done_cb (NMSecretAgent *agent,
|
|
|
|
|
gconstpointer call_id,
|
|
|
|
|
GHashTable *secrets,
|
|
|
|
|
GError *error,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
Request *req = user_data;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (call_id == req->current_call_id);
|
|
|
|
|
|
|
|
|
|
req->current = NULL;
|
|
|
|
|
req->current_call_id = NULL;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent failed save secrets request %p/%s: (%d) %s",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name,
|
|
|
|
|
error ? error->code : -1,
|
|
|
|
|
(error && error->message) ? error->message : "(unknown)");
|
|
|
|
|
|
|
|
|
|
/* Try the next agent */
|
|
|
|
|
req->next_callback (req);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nm_log_dbg (LOGD_AGENTS, "(%s) agent saved secrets for request %p/%s",
|
|
|
|
|
nm_secret_agent_get_description (agent),
|
|
|
|
|
req, req->setting_name);
|
|
|
|
|
|
|
|
|
|
req->complete_callback (req, NULL, NULL, req->complete_callback_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
save_next_cb (Request *req)
|
|
|
|
|
{
|
|
|
|
|
if (!next_generic (req, "saving"))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
req->current_call_id = nm_secret_agent_save_secrets (NM_SECRET_AGENT (req->current),
|
|
|
|
|
req->connection,
|
|
|
|
|
save_done_cb,
|
|
|
|
|
req);
|
|
|
|
|
if (req->current_call_id == NULL) {
|
|
|
|
|
/* Shouldn't hit this, but handle it anyway */
|
|
|
|
|
g_warn_if_fail (req->current_call_id != NULL);
|
|
|
|
|
req->current = NULL;
|
|
|
|
|
req->next_callback (req);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
save_complete_cb (Request *req,
|
|
|
|
|
GHashTable *secrets,
|
|
|
|
|
GError *error,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
NMAgentManager *self = NM_AGENT_MANAGER (user_data);
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
|
|
|
|
|
g_hash_table_remove (priv->requests, GUINT_TO_POINTER (req->reqid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
save_start (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
Request *req = user_data;
|
|
|
|
|
|
|
|
|
|
req->idle_id = 0;
|
|
|
|
|
req->next_callback (req);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guint32
|
|
|
|
|
nm_agent_manager_save_secrets (NMAgentManager *self,
|
|
|
|
|
NMConnection *connection,
|
|
|
|
|
gboolean filter_by_uid,
|
|
|
|
|
gulong uid_filter)
|
|
|
|
|
{
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
Request *req;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, 0);
|
|
|
|
|
g_return_val_if_fail (connection != NULL, 0);
|
|
|
|
|
g_return_val_if_fail (NM_IS_CONNECTION (connection), 0);
|
|
|
|
|
|
|
|
|
|
nm_log_dbg (LOGD_SETTINGS,
|
|
|
|
|
"Saving secrets for connection %s",
|
|
|
|
|
nm_connection_get_path (connection));
|
|
|
|
|
|
|
|
|
|
req = request_new_save (connection,
|
|
|
|
|
filter_by_uid,
|
|
|
|
|
uid_filter,
|
|
|
|
|
save_complete_cb,
|
|
|
|
|
self,
|
|
|
|
|
save_next_cb);
|
|
|
|
|
g_hash_table_insert (priv->requests, GUINT_TO_POINTER (req->reqid), req);
|
|
|
|
|
|
|
|
|
|
/* Kick off the request */
|
|
|
|
|
request_add_agents (self, req);
|
|
|
|
|
req->idle_id = g_idle_add (save_start, req);
|
|
|
|
|
|
|
|
|
|
return req->reqid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
2010-12-10 12:38:19 -06:00
|
|
|
static void
|
|
|
|
|
name_owner_changed_cb (NMDBusManager *dbus_mgr,
|
|
|
|
|
const char *name,
|
|
|
|
|
const char *old_owner,
|
|
|
|
|
const char *new_owner,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
if (old_owner) {
|
|
|
|
|
/* The agent quit, so remove it and let interested clients know */
|
|
|
|
|
remove_agent (NM_AGENT_MANAGER (user_data), old_owner);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
|
|
NMAgentManager *
|
2011-01-27 10:41:02 -06:00
|
|
|
nm_agent_manager_get (void)
|
2010-12-10 12:38:19 -06:00
|
|
|
{
|
2011-01-27 10:41:02 -06:00
|
|
|
static NMAgentManager *singleton = NULL;
|
2010-12-10 12:38:19 -06:00
|
|
|
NMAgentManagerPrivate *priv;
|
|
|
|
|
DBusGConnection *connection;
|
|
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
if (singleton)
|
|
|
|
|
return g_object_ref (singleton);
|
2010-12-10 12:38:19 -06:00
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
singleton = (NMAgentManager *) g_object_new (NM_TYPE_AGENT_MANAGER, NULL);
|
|
|
|
|
g_assert (singleton);
|
2010-12-10 12:38:19 -06:00
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
priv = NM_AGENT_MANAGER_GET_PRIVATE (singleton);
|
|
|
|
|
priv->session_monitor = nm_session_monitor_get ();
|
|
|
|
|
priv->dbus_mgr = nm_dbus_manager_get ();
|
2010-12-10 12:38:19 -06:00
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
connection = nm_dbus_manager_get_connection (priv->dbus_mgr);
|
|
|
|
|
dbus_g_connection_register_g_object (connection,
|
|
|
|
|
NM_DBUS_PATH_AGENT_MANAGER,
|
|
|
|
|
G_OBJECT (singleton));
|
|
|
|
|
|
|
|
|
|
g_signal_connect (priv->dbus_mgr,
|
|
|
|
|
NM_DBUS_MANAGER_NAME_OWNER_CHANGED,
|
|
|
|
|
G_CALLBACK (name_owner_changed_cb),
|
|
|
|
|
singleton);
|
2010-12-10 12:38:19 -06:00
|
|
|
|
2011-01-27 10:41:02 -06:00
|
|
|
return singleton;
|
2010-12-10 12:38:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_agent_manager_init (NMAgentManager *self)
|
|
|
|
|
{
|
2010-12-13 13:11:51 -06:00
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
|
|
|
|
|
priv->agents = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
|
|
|
|
|
priv->requests = g_hash_table_new_full (g_direct_hash,
|
|
|
|
|
g_direct_equal,
|
|
|
|
|
NULL,
|
|
|
|
|
(GDestroyNotify) request_free);
|
2010-12-10 12:38:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
dispose (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
NMAgentManagerPrivate *priv = NM_AGENT_MANAGER_GET_PRIVATE (object);
|
|
|
|
|
|
|
|
|
|
if (priv->disposed)
|
|
|
|
|
return;
|
|
|
|
|
priv->disposed = TRUE;
|
|
|
|
|
|
|
|
|
|
g_object_unref (priv->session_monitor);
|
|
|
|
|
g_object_unref (priv->dbus_mgr);
|
|
|
|
|
|
|
|
|
|
g_hash_table_destroy (priv->agents);
|
2010-12-13 13:11:51 -06:00
|
|
|
g_hash_table_destroy (priv->requests);
|
2010-12-10 12:38:19 -06:00
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (nm_agent_manager_parent_class)->dispose (object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-12-14 11:12:56 -06:00
|
|
|
nm_agent_manager_class_init (NMAgentManagerClass *agent_manager_class)
|
2010-12-10 12:38:19 -06:00
|
|
|
{
|
2010-12-14 11:12:56 -06:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (agent_manager_class);
|
2010-12-10 12:38:19 -06:00
|
|
|
|
2010-12-14 11:12:56 -06:00
|
|
|
g_type_class_add_private (agent_manager_class, sizeof (NMAgentManagerPrivate));
|
2010-12-10 12:38:19 -06:00
|
|
|
|
|
|
|
|
/* virtual methods */
|
|
|
|
|
object_class->dispose = dispose;
|
|
|
|
|
|
2010-12-14 11:12:56 -06:00
|
|
|
dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (agent_manager_class),
|
|
|
|
|
&dbus_glib_nm_agent_manager_object_info);
|
|
|
|
|
|
2010-12-10 12:38:19 -06:00
|
|
|
dbus_g_error_domain_register (NM_AGENT_MANAGER_ERROR,
|
|
|
|
|
NM_DBUS_INTERFACE_AGENT_MANAGER,
|
|
|
|
|
NM_TYPE_AGENT_MANAGER_ERROR);
|
|
|
|
|
}
|