~dylanmccall/ubuntu/oneiric/network-manager-applet/lp852961-disable-autostart-for-gnome-shell

« back to all changes in this revision

Viewing changes to src/applet-vpn-request.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2011-05-30 13:25:18 UTC
  • mto: This revision was merged to the branch mainline in revision 68.
  • Revision ID: james.westby@ubuntu.com-20110530132518-ya5i5mcrl8szsmoj
Tags: upstream-0.8.9997+git.20110529t170033.9ec4c5d
ImportĀ upstreamĀ versionĀ 0.8.9997+git.20110529t170033.9ec4c5d

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
/* NetworkManager Wireless Applet -- Display wireless access points and allow user control
 
3
 *
 
4
 * Dan Williams <dcbw@redhat.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License along
 
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
19
 *
 
20
 * (C) Copyright 2004 - 2011 Red Hat, Inc.
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <sys/types.h>
 
28
#include <sys/wait.h>
 
29
#include <glib.h>
 
30
#include <unistd.h>
 
31
#include <errno.h>
 
32
 
 
33
#include <glib-object.h>
 
34
 
 
35
#include "applet-vpn-request.h"
 
36
#include "nma-marshal.h"
 
37
#include <nm-connection.h>
 
38
#include <nm-setting-connection.h>
 
39
#include <nm-setting-vpn.h>
 
40
#include <nm-secret-agent.h>
 
41
 
 
42
#define APPLET_TYPE_VPN_REQUEST            (applet_vpn_request_get_type ())
 
43
#define APPLET_VPN_REQUEST(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), APPLET_TYPE_VPN_REQUEST, AppletVpnRequest))
 
44
#define APPLET_VPN_REQUEST_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), APPLET_TYPE_VPN_REQUEST, AppletVpnRequestClass))
 
45
#define APPLET_IS_VPN_REQUEST(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), APPLET_TYPE_VPN_REQUEST))
 
46
#define APPLET_IS_VPN_REQUEST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), APPLET_TYPE_VPN_REQUEST))
 
47
#define APPLET_VPN_REQUEST_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), APPLET_TYPE_VPN_REQUEST, AppletVpnRequestClass))
 
48
 
 
49
typedef struct {
 
50
        GObject parent;
 
51
} AppletVpnRequest;
 
52
 
 
53
typedef struct {
 
54
        GObjectClass parent;
 
55
} AppletVpnRequestClass;
 
56
 
 
57
GType applet_vpn_request_get_type (void);
 
58
 
 
59
G_DEFINE_TYPE (AppletVpnRequest, applet_vpn_request, G_TYPE_OBJECT)
 
60
 
 
61
#define APPLET_VPN_REQUEST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
 
62
                                           APPLET_TYPE_VPN_REQUEST, \
 
63
                                           AppletVpnRequestPrivate))
 
64
 
 
65
typedef struct {
 
66
        gboolean disposed;
 
67
 
 
68
        char *uuid;
 
69
        char *id;
 
70
        char *service_type;
 
71
 
 
72
        guint watch_id;
 
73
        GPid pid;
 
74
 
 
75
        GSList *lines;
 
76
        int child_stdin;
 
77
        int child_stdout;
 
78
        int num_newlines;
 
79
        GIOChannel *channel;
 
80
        guint channel_eventid;
 
81
} AppletVpnRequestPrivate;
 
82
 
 
83
/****************************************************************/
 
84
 
 
85
typedef struct {
 
86
        SecretsRequest req;
 
87
        AppletVpnRequest *vpn;
 
88
} VpnSecretsInfo;
 
89
 
 
90
#define DBUS_TYPE_G_MAP_OF_STRING (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING))
 
91
 
 
92
static void 
 
93
child_finished_cb (GPid pid, gint status, gpointer user_data)
 
94
{
 
95
        SecretsRequest *req = user_data;
 
96
        VpnSecretsInfo *info = (VpnSecretsInfo *) req;
 
97
        AppletVpnRequest *self = info->vpn;
 
98
        AppletVpnRequestPrivate *priv = APPLET_VPN_REQUEST_GET_PRIVATE (self);
 
99
        GError *error = NULL;
 
100
        GHashTable *settings = NULL;
 
101
 
 
102
        if (status == 0) {
 
103
                GHashTable *vpn, *secrets;
 
104
                GValue val = { 0 };
 
105
                GSList *iter;
 
106
 
 
107
                settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_hash_table_destroy);
 
108
 
 
109
                vpn = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_value_unset);
 
110
                g_hash_table_insert (settings, NM_SETTING_VPN_SETTING_NAME, vpn);
 
111
 
 
112
                secrets = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
 
113
                g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING);
 
114
                g_value_take_boxed (&val, secrets);
 
115
                g_hash_table_insert (vpn, NM_SETTING_VPN_SECRETS, &val);
 
116
 
 
117
                /* The length of 'lines' must be divisible by 2 since it must contain
 
118
                 * key:secret pairs with the key on one line and the associated secret
 
119
                 * on the next line.
 
120
                 */
 
121
                for (iter = priv->lines; iter; iter = g_slist_next (iter)) {
 
122
                        if (!iter->next)
 
123
                                break;
 
124
                        g_hash_table_insert (secrets, (char *) iter->data, (char *) iter->next->data);
 
125
                        iter = iter->next;
 
126
                }
 
127
        } else {
 
128
                error = g_error_new (NM_SECRET_AGENT_ERROR,
 
129
                                     NM_SECRET_AGENT_ERROR_USER_CANCELED,
 
130
                                     "%s.%d (%s): canceled", __FILE__, __LINE__, __func__);
 
131
        }
 
132
 
 
133
        /* Complete the secrets request */
 
134
        applet_secrets_request_complete (req, settings, error);
 
135
        applet_secrets_request_free (req);
 
136
 
 
137
        if (settings)
 
138
                g_hash_table_destroy (settings);
 
139
        g_clear_error (&error);
 
140
}
 
141
 
 
142
static gboolean 
 
143
child_stdout_data_cb (GIOChannel *source, GIOCondition condition, gpointer user_data)
 
144
{
 
145
        VpnSecretsInfo *info = user_data;
 
146
        AppletVpnRequest *self = info->vpn;
 
147
        AppletVpnRequestPrivate *priv = APPLET_VPN_REQUEST_GET_PRIVATE (self);
 
148
        const char *buf = "QUIT\n\n";
 
149
        char *str;
 
150
        int len;
 
151
 
 
152
        if (!(condition & G_IO_IN))
 
153
                return TRUE;
 
154
 
 
155
        if (g_io_channel_read_line (source, &str, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
 
156
                len = strlen (str);
 
157
                if (len == 1 && str[0] == '\n') {
 
158
                        /* on second line with a newline newline */
 
159
                        if (++priv->num_newlines == 2) {
 
160
                                /* terminate the child */
 
161
                                if (write (priv->child_stdin, buf, strlen (buf)) == -1)
 
162
                                        return TRUE;
 
163
                        }
 
164
                } else if (len > 0) {
 
165
                        /* remove terminating newline */
 
166
                        str[len - 1] = '\0';
 
167
                        priv->lines = g_slist_append (priv->lines, str);
 
168
                }
 
169
        }
 
170
        return TRUE;
 
171
}
 
172
 
 
173
static char *
 
174
find_auth_dialog_binary (const char *service, GError **error)
 
175
{
 
176
        GDir *dir;
 
177
        char *prog = NULL;
 
178
        const char *f;
 
179
 
 
180
        dir = g_dir_open (VPN_NAME_FILES_DIR, 0, NULL);
 
181
        if (!dir) {
 
182
                g_set_error (error,
 
183
                             NM_SECRET_AGENT_ERROR,
 
184
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 
185
                             "Failed to open VPN plugin file configuration directory " VPN_NAME_FILES_DIR);
 
186
                return NULL;
 
187
        }
 
188
 
 
189
        while (prog == NULL && (f = g_dir_read_name (dir)) != NULL) {
 
190
                char *path;
 
191
                GKeyFile *keyfile;
 
192
 
 
193
                if (!g_str_has_suffix (f, ".name"))
 
194
                        continue;
 
195
 
 
196
                path = g_strdup_printf ("%s/%s", VPN_NAME_FILES_DIR, f);
 
197
 
 
198
                keyfile = g_key_file_new ();
 
199
                if (g_key_file_load_from_file (keyfile, path, 0, NULL)) {
 
200
                        char *thisservice;
 
201
 
 
202
                        thisservice = g_key_file_get_string (keyfile, "VPN Connection", "service", NULL);
 
203
                        if (g_strcmp0 (thisservice, service) == 0)
 
204
                                prog = g_key_file_get_string (keyfile, "GNOME", "auth-dialog", NULL);
 
205
                        g_free (thisservice);
 
206
                }
 
207
                g_key_file_free (keyfile);
 
208
                g_free (path);
 
209
        }
 
210
        g_dir_close (dir);
 
211
 
 
212
        if (prog == NULL) {
 
213
                g_set_error (error,
 
214
                             NM_SECRET_AGENT_ERROR,
 
215
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 
216
                             "Could not find the authentication dialog for VPN connection type '%s'",
 
217
                             service);
 
218
        } else {
 
219
                char *prog_basename;
 
220
 
 
221
                /* Remove any path component, then reconstruct path to the auth
 
222
                 * dialog in LIBEXECDIR.
 
223
                 */
 
224
                prog_basename = g_path_get_basename (prog);
 
225
                g_free (prog);
 
226
                prog = g_strdup_printf ("%s/%s", LIBEXECDIR, prog_basename);
 
227
                g_free (prog_basename);
 
228
        }
 
229
 
 
230
        return prog;
 
231
}
 
232
 
 
233
static void
 
234
free_vpn_secrets_info (SecretsRequest *req)
 
235
{
 
236
        VpnSecretsInfo *info = (VpnSecretsInfo *) req;
 
237
 
 
238
        if (info->vpn)
 
239
                g_object_unref (info->vpn);
 
240
}
 
241
 
 
242
size_t
 
243
applet_vpn_request_get_secrets_size (void)
 
244
{
 
245
        return sizeof (VpnSecretsInfo);
 
246
}
 
247
 
 
248
typedef struct {
 
249
        int fd;
 
250
        gboolean secret;
 
251
        GError **error;
 
252
} WriteItemInfo;
 
253
 
 
254
static const char *data_key_tag = "DATA_KEY=";
 
255
static const char *data_val_tag = "DATA_VAL=";
 
256
static const char *secret_key_tag = "SECRET_KEY=";
 
257
static const char *secret_val_tag = "SECRET_VAL=";
 
258
 
 
259
static gboolean
 
260
write_item (int fd, const char *item, GError **error)
 
261
{
 
262
        size_t item_len = strlen (item);
 
263
 
 
264
        errno = 0;
 
265
        if (write (fd, item, item_len) != item_len) {
 
266
                g_set_error (error,
 
267
                                 NM_SECRET_AGENT_ERROR,
 
268
                                 NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 
269
                                 "Failed to write connection to VPN UI: errno %d", errno);
 
270
                return FALSE;
 
271
        }
 
272
        return TRUE;
 
273
}
 
274
 
 
275
static void
 
276
write_one_key_val (const char *key, const char *value, gpointer user_data)
 
277
{
 
278
        WriteItemInfo *info = user_data;
 
279
        const char *tag;
 
280
 
 
281
        if (info->error && *(info->error))
 
282
                return;
 
283
 
 
284
        /* Write the key name */
 
285
        tag = info->secret ? secret_key_tag : data_key_tag;
 
286
        if (!write_item (info->fd, tag, info->error))
 
287
                return;
 
288
        if (!write_item (info->fd, key, info->error))
 
289
                return;
 
290
        if (!write_item (info->fd, "\n", info->error))
 
291
                return;
 
292
 
 
293
        /* Write the key value */
 
294
        tag = info->secret ? secret_val_tag : data_val_tag;
 
295
        if (!write_item (info->fd, tag, info->error))
 
296
                return;
 
297
        if (!write_item (info->fd, value ? value : "", info->error))
 
298
                return;
 
299
        if (!write_item (info->fd, "\n\n", info->error))
 
300
                return;
 
301
}
 
302
 
 
303
static gboolean
 
304
write_connection_to_child (int fd, NMConnection *connection, GError **error)
 
305
{
 
306
        NMSettingVPN *s_vpn;
 
307
        WriteItemInfo info = { .fd = fd, .secret = FALSE, .error = error };
 
308
 
 
309
        s_vpn = (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
 
310
        if (!s_vpn) {
 
311
                g_set_error_literal (error,
 
312
                                     NM_SECRET_AGENT_ERROR,
 
313
                                     NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 
314
                                     "Connection had no VPN setting");
 
315
                return FALSE;
 
316
        }
 
317
 
 
318
        nm_setting_vpn_foreach_data_item (s_vpn, write_one_key_val, &info);
 
319
        if (error && *error)
 
320
                return FALSE;
 
321
 
 
322
        info.secret = TRUE;
 
323
        nm_setting_vpn_foreach_secret (s_vpn, write_one_key_val, &info);
 
324
        if (error && *error)
 
325
                return FALSE;
 
326
 
 
327
        if (!write_item (fd, "DONE\n\n", error))
 
328
                return FALSE;
 
329
 
 
330
        return TRUE;
 
331
}
 
332
 
 
333
static void
 
334
vpn_child_setup (gpointer user_data G_GNUC_UNUSED)
 
335
{
 
336
        /* We are in the child process at this point */
 
337
        pid_t pid = getpid ();
 
338
        setpgid (pid, pid);
 
339
}
 
340
 
 
341
gboolean
 
342
applet_vpn_request_get_secrets (SecretsRequest *req, GError **error)
 
343
{
 
344
        VpnSecretsInfo *info = (VpnSecretsInfo *) req;
 
345
        AppletVpnRequestPrivate *priv;
 
346
        NMSettingConnection *s_con;
 
347
        NMSettingVPN *s_vpn;
 
348
        const char *connection_type;
 
349
        const char *service_type;
 
350
        char *bin_path;
 
351
        const char *argv[10];
 
352
        gboolean success = FALSE;
 
353
        guint i = 0;
 
354
 
 
355
        applet_secrets_request_set_free_func (req, free_vpn_secrets_info);
 
356
 
 
357
        s_con = (NMSettingConnection *) nm_connection_get_setting (req->connection, NM_TYPE_SETTING_CONNECTION);
 
358
        g_return_val_if_fail (s_con != NULL, FALSE);
 
359
 
 
360
        connection_type = nm_setting_connection_get_connection_type (s_con);
 
361
        g_return_val_if_fail (connection_type != NULL, FALSE);
 
362
        g_return_val_if_fail (strcmp (connection_type, NM_SETTING_VPN_SETTING_NAME) == 0, FALSE);
 
363
 
 
364
        s_vpn = NM_SETTING_VPN (nm_connection_get_setting (req->connection, NM_TYPE_SETTING_VPN));
 
365
        g_return_val_if_fail (s_vpn != NULL, FALSE);
 
366
 
 
367
        service_type = nm_setting_vpn_get_service_type (s_vpn);
 
368
        g_return_val_if_fail (service_type != NULL, FALSE);
 
369
 
 
370
        /* find the auth-dialog binary */
 
371
        bin_path = find_auth_dialog_binary (service_type, error);
 
372
        if (!bin_path)
 
373
                return FALSE;
 
374
 
 
375
        info->vpn = (AppletVpnRequest *) g_object_new (APPLET_TYPE_VPN_REQUEST, NULL);
 
376
        if (!info->vpn) {
 
377
                g_set_error_literal (error,
 
378
                                     NM_SECRET_AGENT_ERROR,
 
379
                                     NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 
380
                                     "Could not create VPN secrets request object");
 
381
                goto out;
 
382
        }
 
383
 
 
384
        priv = APPLET_VPN_REQUEST_GET_PRIVATE (info->vpn);
 
385
 
 
386
        memset (argv, 0, sizeof (argv));
 
387
        argv[i++] = bin_path;
 
388
        argv[i++] = "-u";
 
389
        argv[i++] = nm_setting_connection_get_uuid (s_con);
 
390
        argv[i++] = "-n";
 
391
        argv[i++] = nm_setting_connection_get_id (s_con);
 
392
        argv[i++] = "-s";
 
393
        argv[i++] = service_type;
 
394
        if (req->flags & NM_SECRET_AGENT_GET_SECRETS_FLAG_ALLOW_INTERACTION)
 
395
                argv[i++] = "-i";
 
396
        if (req->flags & NM_SECRET_AGENT_GET_SECRETS_FLAG_REQUEST_NEW)
 
397
                argv[i++] = "-r";
 
398
 
 
399
        if (!g_spawn_async_with_pipes (NULL,                       /* working_directory */
 
400
                                       (gchar **) argv,            /* argv */
 
401
                                       NULL,                       /* envp */
 
402
                                       G_SPAWN_DO_NOT_REAP_CHILD,  /* flags */
 
403
                                       vpn_child_setup,            /* child_setup */
 
404
                                       NULL,                       /* user_data */
 
405
                                       &priv->pid,                 /* child_pid */
 
406
                                       &priv->child_stdin,         /* standard_input */
 
407
                                       &priv->child_stdout,        /* standard_output */
 
408
                                       NULL,                       /* standard_error */
 
409
                                       error))                     /* error */
 
410
                goto out;
 
411
 
 
412
        /* catch when child is reaped */
 
413
        priv->watch_id = g_child_watch_add (priv->pid, child_finished_cb, info);
 
414
 
 
415
        /* listen to what child has to say */
 
416
        priv->channel = g_io_channel_unix_new (priv->child_stdout);
 
417
        priv->channel_eventid = g_io_add_watch (priv->channel, G_IO_IN, child_stdout_data_cb, info);
 
418
        g_io_channel_set_encoding (priv->channel, NULL, NULL);
 
419
 
 
420
        /* Dump parts of the connection to the child */
 
421
        success = write_connection_to_child (priv->child_stdin, req->connection, error);
 
422
 
 
423
out:
 
424
        g_free (bin_path);
 
425
        return success;
 
426
}
 
427
 
 
428
static void
 
429
applet_vpn_request_init (AppletVpnRequest *self)
 
430
{
 
431
}
 
432
 
 
433
static gboolean
 
434
ensure_killed (gpointer data)
 
435
{
 
436
        pid_t pid = GPOINTER_TO_INT (data);
 
437
 
 
438
        if (kill (pid, 0) == 0)
 
439
                kill (pid, SIGKILL);
 
440
        /* ensure the child is reaped */
 
441
        waitpid (pid, NULL, 0);
 
442
        return FALSE;
 
443
}
 
444
 
 
445
static void
 
446
dispose (GObject *object)
 
447
{
 
448
        AppletVpnRequest *self = APPLET_VPN_REQUEST (object);
 
449
        AppletVpnRequestPrivate *priv = APPLET_VPN_REQUEST_GET_PRIVATE (self);
 
450
 
 
451
        if (priv->disposed)
 
452
                goto done;
 
453
 
 
454
        priv->disposed = TRUE;
 
455
 
 
456
        g_free (priv->uuid);
 
457
        g_free (priv->id);
 
458
        g_free (priv->service_type);
 
459
 
 
460
        if (priv->watch_id)
 
461
                g_source_remove (priv->watch_id);
 
462
 
 
463
        if (priv->channel_eventid)
 
464
                g_source_remove (priv->channel_eventid);
 
465
        if (priv->channel)
 
466
                g_io_channel_unref (priv->channel);
 
467
 
 
468
        if (priv->pid) {
 
469
                g_spawn_close_pid (priv->pid);
 
470
                if (kill (priv->pid, SIGTERM) == 0)
 
471
                        g_timeout_add_seconds (2, ensure_killed, GINT_TO_POINTER (priv->pid));
 
472
                else {
 
473
                        kill (priv->pid, SIGKILL);
 
474
                        /* ensure the child is reaped */
 
475
                        waitpid (priv->pid, NULL, 0);
 
476
                }
 
477
        }
 
478
 
 
479
        g_slist_foreach (priv->lines, (GFunc) g_free, NULL);
 
480
        g_slist_free (priv->lines);
 
481
 
 
482
done:
 
483
        G_OBJECT_CLASS (applet_vpn_request_parent_class)->dispose (object);
 
484
}
 
485
 
 
486
static void
 
487
applet_vpn_request_class_init (AppletVpnRequestClass *req_class)
 
488
{
 
489
        GObjectClass *object_class = G_OBJECT_CLASS (req_class);
 
490
 
 
491
        g_type_class_add_private (req_class, sizeof (AppletVpnRequestPrivate));
 
492
 
 
493
        /* virtual methods */
 
494
        object_class->dispose = dispose;
 
495
}
 
496