~network-manager/network-manager/ubuntu.hardy.07

« back to all changes in this revision

Viewing changes to system-settings/plugins/keyfile/nm-keyfile-connection.c

* merge 0.7~~svn20080905t025540+eni0 snapshot to hardy branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
2
 
3
3
#include <string.h>
4
4
#include <glib/gstdio.h>
5
5
#include <NetworkManager.h>
 
6
#include <nm-setting-connection.h>
 
7
#include <nm-utils.h>
 
8
 
 
9
#include "nm-dbus-glib-types.h"
6
10
#include "nm-keyfile-connection.h"
7
11
#include "reader.h"
8
12
#include "writer.h"
28
32
        g_return_val_if_fail (filename != NULL, NULL);
29
33
 
30
34
        return (NMKeyfileConnection *) g_object_new (NM_TYPE_KEYFILE_CONNECTION,
31
 
                                                                                NM_KEYFILE_CONNECTION_FILENAME, filename,
32
 
                                                                                NULL);
 
35
                                                     NM_KEYFILE_CONNECTION_FILENAME, filename,
 
36
                                                     NULL);
33
37
}
34
38
 
35
39
const char *
46
50
        return nm_connection_to_hash (nm_exported_connection_get_connection (exported));
47
51
}
48
52
 
49
 
static const char *
50
 
get_id (NMExportedConnection *exported)
51
 
{
52
 
        return NM_KEYFILE_CONNECTION_GET_PRIVATE (exported)->filename;
 
53
static GValue *
 
54
string_to_gvalue (const char *str)
 
55
{
 
56
        GValue *val;
 
57
 
 
58
        val = g_slice_new0 (GValue);
 
59
        g_value_init (val, G_TYPE_STRING);
 
60
        g_value_set_string (val, str);
 
61
 
 
62
        return val;
 
63
}
 
64
 
 
65
static void
 
66
copy_one_secret (gpointer key, gpointer value, gpointer user_data)
 
67
{
 
68
        g_hash_table_insert ((GHashTable *) user_data,
 
69
                             g_strdup ((char *) key),
 
70
                             string_to_gvalue (value));
 
71
}
 
72
 
 
73
static void
 
74
add_secrets (NMSetting *setting,
 
75
             const char *key,
 
76
             const GValue *value,
 
77
             gboolean secret,
 
78
             gpointer user_data)
 
79
{
 
80
        GHashTable *secrets = user_data;
 
81
 
 
82
        if (!secret)
 
83
                return;
 
84
 
 
85
        if (G_VALUE_HOLDS_STRING (value)) {
 
86
                g_hash_table_insert (secrets, g_strdup (key), string_to_gvalue (g_value_get_string (value)));
 
87
        } else if (G_VALUE_HOLDS (value, DBUS_TYPE_G_MAP_OF_STRING)) {
 
88
                /* Flatten the string hash by pulling its keys/values out */
 
89
                g_hash_table_foreach (g_value_get_boxed (value), copy_one_secret, secrets);
 
90
        } else
 
91
                g_message ("%s: unhandled secret %s type %s", __func__, key, G_VALUE_TYPE_NAME (value));
 
92
}
 
93
 
 
94
static void
 
95
destroy_gvalue (gpointer data)
 
96
{
 
97
        GValue *value = (GValue *) data;
 
98
 
 
99
        g_value_unset (value);
 
100
        g_slice_free (GValue, value);
 
101
}
 
102
 
 
103
static GHashTable *
 
104
extract_secrets (NMKeyfileConnection *exported,
 
105
                 const char *setting_name,
 
106
                 GError **error)
 
107
{
 
108
        NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (exported);
 
109
        NMConnection *tmp;
 
110
        GHashTable *secrets;
 
111
        NMSetting *setting;
 
112
 
 
113
        tmp = connection_from_file (priv->filename, TRUE);
 
114
        if (!tmp) {
 
115
                g_set_error (error, NM_SETTINGS_ERROR, 1,
 
116
                             "%s.%d - Could not read secrets from file %s.",
 
117
                             __FILE__, __LINE__, priv->filename);
 
118
                return NULL;
 
119
        }
 
120
 
 
121
        setting = nm_connection_get_setting_by_name (tmp, setting_name);
 
122
        if (!setting) {
 
123
                g_object_unref (tmp);
 
124
                g_set_error (error, NM_SETTINGS_ERROR, 1,
 
125
                             "%s.%d - Could not read secrets from file %s.",
 
126
                             __FILE__, __LINE__, priv->filename);
 
127
                return NULL;
 
128
        }
 
129
 
 
130
        /* Add the secrets from this setting to the secrets hash */
 
131
        secrets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_gvalue);
 
132
        nm_setting_enumerate_values (setting, add_secrets, secrets);
 
133
 
 
134
        g_object_unref (tmp);
 
135
 
 
136
        return secrets;
 
137
}
 
138
 
 
139
static void
 
140
get_secrets (NMExportedConnection *exported,
 
141
             const gchar *setting_name,
 
142
             const gchar **hints,
 
143
             gboolean request_new,
 
144
             DBusGMethodInvocation *context)
 
145
{
 
146
        NMConnection *connection;
 
147
        GError *error = NULL;
 
148
        GHashTable *settings = NULL;
 
149
        GHashTable *secrets = NULL;
 
150
        NMSetting *setting;
 
151
 
 
152
        connection = nm_exported_connection_get_connection (exported);
 
153
        setting = nm_connection_get_setting_by_name (connection, setting_name);
 
154
        if (!setting) {
 
155
                g_set_error (&error, NM_SETTINGS_ERROR, 1,
 
156
                             "%s.%d - Connection didn't have requested setting '%s'.",
 
157
                             __FILE__, __LINE__, setting_name);
 
158
                goto error;
 
159
        }
 
160
 
 
161
        /* Returned secrets are a{sa{sv}}; this is the outer a{s...} hash that
 
162
         * will contain all the individual settings hashes.
 
163
         */
 
164
        settings = g_hash_table_new_full (g_str_hash, g_str_equal,
 
165
                                          g_free, (GDestroyNotify) g_hash_table_destroy);
 
166
 
 
167
        /* Read in a temporary connection and just extract the secrets */
 
168
        secrets = extract_secrets (NM_KEYFILE_CONNECTION (exported), setting_name, &error);
 
169
        if (!secrets)
 
170
                goto error;
 
171
 
 
172
        g_hash_table_insert (settings, g_strdup (setting_name), secrets);
 
173
 
 
174
        dbus_g_method_return (context, settings);
 
175
        g_hash_table_destroy (settings);
 
176
        return;
 
177
 
 
178
error:
 
179
        nm_warning ("%s", error->message);
 
180
        dbus_g_method_return_error (context, error);
 
181
        g_error_free (error);
53
182
}
54
183
 
55
184
static gboolean
56
185
update (NMExportedConnection *exported,
57
 
           GHashTable *new_settings,
58
 
           GError **error)
 
186
        GHashTable *new_settings,
 
187
        GError **error)
59
188
{
 
189
        NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (exported);
60
190
        gboolean success;
61
191
 
62
192
        success = NM_EXPORTED_CONNECTION_CLASS (nm_keyfile_connection_parent_class)->update (exported, new_settings, error);
63
 
        if (success)
64
 
                success = write_connection (nm_exported_connection_get_connection (exported), error);
 
193
        if (success) {
 
194
                NMConnection *connection;
 
195
                char *filename = NULL;
 
196
 
 
197
                connection = nm_exported_connection_get_connection (exported);
 
198
                success = write_connection (connection, &filename, error);
 
199
                if (success && filename && strcmp (priv->filename, filename)) {
 
200
                        /* Update the filename if it changed */
 
201
                        g_free (priv->filename);
 
202
                        priv->filename = filename;
 
203
                } else
 
204
                        g_free (filename);
 
205
        }
65
206
 
66
207
        return success;
67
208
}
95
236
        GObject *object;
96
237
        NMKeyfileConnectionPrivate *priv;
97
238
        NMConnection *wrapped;
 
239
        NMSettingConnection *s_con;
98
240
 
99
241
        object = G_OBJECT_CLASS (nm_keyfile_connection_parent_class)->constructor (type, n_construct_params, construct_params);
100
242
 
108
250
                goto err;
109
251
        }
110
252
 
111
 
        wrapped = connection_from_file (priv->filename);
 
253
        wrapped = connection_from_file (priv->filename, FALSE);
112
254
        if (!wrapped)
113
255
                goto err;
114
256
 
 
257
        /* if for some reason the connection didn't have a UUID, add one */
 
258
        s_con = (NMSettingConnection *) nm_connection_get_setting (wrapped, NM_TYPE_SETTING_CONNECTION);
 
259
        if (s_con && !s_con->uuid) {
 
260
                GError *error = NULL;
 
261
 
 
262
                s_con->uuid = nm_utils_uuid_generate ();
 
263
                if (!write_connection (wrapped, NULL, &error)) {
 
264
                        g_warning ("Couldn't update connection %s with a UUID: (%d) %s",
 
265
                                   s_con->id, error ? error->code : 0,
 
266
                                   error ? error->message : "unknown");
 
267
                        g_error_free (error);
 
268
                }
 
269
        }
 
270
 
115
271
        g_object_set (object, NM_EXPORTED_CONNECTION_CONNECTION, wrapped, NULL);
116
272
        g_object_unref (wrapped);
117
273
 
181
337
        object_class->finalize     = finalize;
182
338
 
183
339
        connection_class->get_settings = get_settings;
184
 
        connection_class->get_id       = get_id;
 
340
        connection_class->get_secrets  = get_secrets;
185
341
        connection_class->update       = update;
186
342
        connection_class->delete       = delete;
187
343