summaryrefslogtreecommitdiff
path: root/src/vpn/nm-vpn-manager.c
blob: d518b082298a26d866a48fcb6d72682a0634afce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2005 - 2012 Red Hat, Inc.
 * Copyright (C) 2006 - 2008 Novell, Inc.
 */

#include "nm-default.h"

#include "nm-vpn-manager.h"

#include "nm-vpn-plugin-info.h"
#include "nm-vpn-connection.h"
#include "nm-setting-vpn.h"
#include "nm-vpn-dbus-interface.h"
#include "nm-core-internal.h"

typedef struct {
    GSList *      plugins;
    GFileMonitor *monitor_etc;
    GFileMonitor *monitor_lib;
    gulong        monitor_id_etc;
    gulong        monitor_id_lib;

    /* This is only used for services that don't support multiple
	 * connections, to guard access to them. */
    GHashTable *active_services;
} NMVpnManagerPrivate;

struct _NMVpnManager {
    GObject             parent;
    NMVpnManagerPrivate _priv;
};

struct _NMVpnManagerClass {
    GObjectClass parent;
};

G_DEFINE_TYPE(NMVpnManager, nm_vpn_manager, G_TYPE_OBJECT)

#define NM_VPN_MANAGER_GET_PRIVATE(self) _NM_GET_PRIVATE(self, NMVpnManager, NM_IS_VPN_MANAGER)

/*****************************************************************************/

static void
vpn_state_changed(NMVpnConnection *vpn, GParamSpec *pspec, NMVpnManager *manager)
{
    NMVpnManagerPrivate *   priv  = NM_VPN_MANAGER_GET_PRIVATE(manager);
    NMActiveConnectionState state = nm_active_connection_get_state(NM_ACTIVE_CONNECTION(vpn));
    const char *            service_name = nm_vpn_connection_get_service(vpn);

    if (state == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED) {
        g_hash_table_remove(priv->active_services, service_name);
        g_signal_handlers_disconnect_by_func(vpn, vpn_state_changed, manager);
        g_object_unref(manager);
    }
}

gboolean
nm_vpn_manager_activate_connection(NMVpnManager *manager, NMVpnConnection *vpn, GError **error)
{
    NMVpnManagerPrivate *priv;
    NMVpnPluginInfo *    plugin_info;
    const char *         service_name;
    NMDevice *           device;

    g_return_val_if_fail(NM_IS_VPN_MANAGER(manager), FALSE);
    g_return_val_if_fail(NM_IS_VPN_CONNECTION(vpn), FALSE);
    g_return_val_if_fail(!error || !*error, FALSE);

    priv   = NM_VPN_MANAGER_GET_PRIVATE(manager);
    device = nm_active_connection_get_device(NM_ACTIVE_CONNECTION(vpn));
    g_assert(device);
    if (nm_device_get_state(device) != NM_DEVICE_STATE_ACTIVATED
        && nm_device_get_state(device) != NM_DEVICE_STATE_SECONDARIES) {
        g_set_error_literal(error,
                            NM_MANAGER_ERROR,
                            NM_MANAGER_ERROR_DEPENDENCY_FAILED,
                            "The base device for the VPN connection was not active.");
        return FALSE;
    }

    service_name = nm_vpn_connection_get_service(vpn);

    plugin_info = nm_vpn_plugin_info_list_find_by_service(priv->plugins, service_name);
    if (!plugin_info) {
        g_set_error(error,
                    NM_MANAGER_ERROR,
                    NM_MANAGER_ERROR_CONNECTION_NOT_AVAILABLE,
                    "The VPN service '%s' was not installed.",
                    service_name);
        return FALSE;
    }

    if (!nm_vpn_plugin_info_supports_multiple(plugin_info)
        && g_hash_table_contains(priv->active_services, service_name)) {
        g_set_error(error,
                    NM_MANAGER_ERROR,
                    NM_MANAGER_ERROR_CONNECTION_NOT_AVAILABLE,
                    "The '%s' plugin only supports a single active connection.",
                    nm_vpn_plugin_info_get_name(plugin_info));
        return FALSE;
    }

    nm_vpn_connection_activate(vpn, plugin_info);

    if (!nm_vpn_plugin_info_supports_multiple(plugin_info)) {
        /* Block activations of the connections of the same service type. */
        g_hash_table_add(priv->active_services, g_strdup(service_name));
        g_signal_connect(vpn,
                         "notify::" NM_ACTIVE_CONNECTION_STATE,
                         G_CALLBACK(vpn_state_changed),
                         g_object_ref(manager));
    }

    return TRUE;
}

/*****************************************************************************/

static void
try_add_plugin(NMVpnManager *self, NMVpnPluginInfo *plugin_info)
{
    NMVpnManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE(self);
    const char *         program;

    program = nm_vpn_plugin_info_get_program(plugin_info);
    if (!program || !*program)
        return;

    /* Make sure we don't add dupes.
	 * We don't really allow reload of the same file. What we do allow is however to
	 * delete a file and re-add it. */
    if (nm_vpn_plugin_info_list_find_by_filename(priv->plugins,
                                                 nm_vpn_plugin_info_get_filename(plugin_info)))
        return;
    if (!nm_vpn_plugin_info_list_add(&priv->plugins, plugin_info, NULL))
        return;
}

static void
vpn_dir_changed(GFileMonitor *    monitor,
                GFile *           file,
                GFile *           other_file,
                GFileMonitorEvent event_type,
                gpointer          user_data)
{
    NMVpnManager *       self = NM_VPN_MANAGER(user_data);
    NMVpnManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE(self);
    NMVpnPluginInfo *    plugin_info;
    gs_free char *       path  = NULL;
    GError *             error = NULL;

    path = g_file_get_path(file);
    if (!nm_vpn_plugin_info_validate_filename(path))
        return;

    switch (event_type) {
    case G_FILE_MONITOR_EVENT_DELETED:
        plugin_info = nm_vpn_plugin_info_list_find_by_filename(priv->plugins, path);
        if (!plugin_info)
            break;

        nm_log_dbg(LOGD_VPN, "vpn: service file %s deleted", path);
        nm_vpn_plugin_info_list_remove(&priv->plugins, plugin_info);
        break;
    case G_FILE_MONITOR_EVENT_CREATED:
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
        plugin_info = nm_vpn_plugin_info_list_find_by_filename(priv->plugins, path);
        if (plugin_info) {
            /* we don't support reloading an existing plugin. You can only remove the file
			 * and re-add it. By reloading we want to support the use case of installing
			 * a VPN plugin after NM started. No need to burden ourself with a complete
			 * reload. */
            break;
        }

        if (!_nm_vpn_plugin_info_check_file(path, TRUE, TRUE, 0, NULL, NULL, &error)) {
            nm_log_dbg(LOGD_VPN, "vpn: ignore changed service file %s (%s)", path, error->message);
            g_clear_error(&error);
            break;
        }
        plugin_info = nm_vpn_plugin_info_new_from_file(path, &error);
        if (!plugin_info) {
            nm_log_dbg(LOGD_VPN,
                       "vpn: ignore changed service file %s due to invalid content (%s)",
                       path,
                       error->message);
            g_clear_error(&error);
            break;
        }

        nm_log_dbg(LOGD_VPN, "vpn: service file %s created or modified", path);
        try_add_plugin(self, plugin_info);
        g_object_unref(plugin_info);
        break;
    default:
        nm_log_dbg(LOGD_VPN, "vpn: service file %s change event %d", path, event_type);
        break;
    }
}

/*****************************************************************************/

NM_DEFINE_SINGLETON_GETTER(NMVpnManager, nm_vpn_manager_get, NM_TYPE_VPN_MANAGER);

static void
nm_vpn_manager_init(NMVpnManager *self)
{
    NMVpnManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE(self);
    GFile *              file;
    GSList *             infos, *info;
    const char *         conf_dir_etc = _nm_vpn_plugin_info_get_default_dir_etc();
    const char *         conf_dir_lib = _nm_vpn_plugin_info_get_default_dir_lib();

    /* Watch the VPN directory for changes */
    file              = g_file_new_for_path(conf_dir_lib);
    priv->monitor_lib = g_file_monitor_directory(file, G_FILE_MONITOR_NONE, NULL, NULL);
    g_object_unref(file);
    if (priv->monitor_lib) {
        priv->monitor_id_lib =
            g_signal_connect(priv->monitor_lib, "changed", G_CALLBACK(vpn_dir_changed), self);
    }

    file              = g_file_new_for_path(conf_dir_etc);
    priv->monitor_etc = g_file_monitor_directory(file, G_FILE_MONITOR_NONE, NULL, NULL);
    g_object_unref(file);
    if (priv->monitor_etc) {
        priv->monitor_id_etc =
            g_signal_connect(priv->monitor_etc, "changed", G_CALLBACK(vpn_dir_changed), self);
    }

    /* first read conf_dir_lib. The name files are not really user configuration, but
	 * plugin configuration. Hence we expect ~newer~ plugins to install their files
	 * in /usr/lib/NetworkManager. We want to prefer those files.
	 * In case of no-conflict, the order doesn't matter. */
    infos = _nm_vpn_plugin_info_list_load_dir(conf_dir_lib, TRUE, 0, NULL, NULL);
    for (info = infos; info; info = info->next)
        try_add_plugin(self, info->data);
    g_slist_free_full(infos, g_object_unref);

    infos = _nm_vpn_plugin_info_list_load_dir(conf_dir_etc, TRUE, 0, NULL, NULL);
    for (info = infos; info; info = info->next)
        try_add_plugin(self, info->data);
    g_slist_free_full(infos, g_object_unref);

    priv->active_services = g_hash_table_new_full(nm_str_hash, g_str_equal, g_free, NULL);
}

static void
dispose(GObject *object)
{
    NMVpnManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE(object);

    if (priv->monitor_etc) {
        if (priv->monitor_id_etc)
            g_signal_handler_disconnect(priv->monitor_etc, priv->monitor_id_etc);
        g_file_monitor_cancel(priv->monitor_etc);
        g_clear_object(&priv->monitor_etc);
    }

    if (priv->monitor_lib) {
        if (priv->monitor_id_lib)
            g_signal_handler_disconnect(priv->monitor_lib, priv->monitor_id_lib);
        g_file_monitor_cancel(priv->monitor_lib);
        g_clear_object(&priv->monitor_lib);
    }

    while (priv->plugins)
        nm_vpn_plugin_info_list_remove(&priv->plugins, priv->plugins->data);

    g_hash_table_unref(priv->active_services);

    G_OBJECT_CLASS(nm_vpn_manager_parent_class)->dispose(object);
}

static void
nm_vpn_manager_class_init(NMVpnManagerClass *manager_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(manager_class);

    object_class->dispose = dispose;
}