agent: Keep CAP_DAC_OVERRIDE to connect to ssh-agent socket
[strongswan.git] / src / charon-nm / nm / nm_backend.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2008-2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "nm_service.h"
18 #include "nm_creds.h"
19 #include "nm_handler.h"
20
21 #include <hydra.h>
22 #include <daemon.h>
23 #include <processing/jobs/callback_job.h>
24
25 typedef struct nm_backend_t nm_backend_t;
26
27 /**
28 * Data for the NetworkManager backend.
29 */
30 struct nm_backend_t {
31
32 /**
33 * NetworkManager service (VPNPlugin)
34 */
35 NMStrongswanPlugin *plugin;
36
37 /**
38 * Glib main loop for a thread, handles DBUS calls
39 */
40 GMainLoop *loop;
41
42 /**
43 * credential set registered at the daemon
44 */
45 nm_creds_t *creds;
46
47 /**
48 * attribute handler regeisterd at the daemon
49 */
50 nm_handler_t *handler;
51 };
52
53 /**
54 * Global (but private) instance of the NM backend.
55 */
56 static nm_backend_t *nm_backend = NULL;
57
58 /**
59 * NM plugin processing routine, creates and handles NMVPNPlugin
60 */
61 static job_requeue_t run(nm_backend_t *this)
62 {
63 this->loop = g_main_loop_new(NULL, FALSE);
64 g_main_loop_run(this->loop);
65 return JOB_REQUEUE_NONE;
66 }
67
68 /**
69 * Cancel the GLib Main Event Loop
70 */
71 static bool cancel(nm_backend_t *this)
72 {
73 if (this->loop)
74 {
75 if (g_main_loop_is_running(this->loop))
76 {
77 g_main_loop_quit(this->loop);
78 }
79 g_main_loop_unref(this->loop);
80 }
81 return TRUE;
82 }
83
84 /**
85 * Deinitialize NetworkManager backend
86 */
87 static void nm_backend_deinit()
88 {
89 nm_backend_t *this = nm_backend;
90
91 if (!this)
92 {
93 return;
94 }
95 if (this->plugin)
96 {
97 g_object_unref(this->plugin);
98 }
99 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
100 hydra->attributes->remove_handler(hydra->attributes, &this->handler->handler);
101 this->creds->destroy(this->creds);
102 this->handler->destroy(this->handler);
103 free(this);
104
105 nm_backend = NULL;
106 }
107
108 /**
109 * Initialize NetworkManager backend
110 */
111 static bool nm_backend_init()
112 {
113 nm_backend_t *this;
114
115 #if !GLIB_CHECK_VERSION(2,36,0)
116 g_type_init ();
117 #endif
118
119 #if !GLIB_CHECK_VERSION(2,23,0)
120 if (!g_thread_supported())
121 {
122 g_thread_init(NULL);
123 }
124 #endif
125
126 INIT(this,
127 .creds = nm_creds_create(),
128 .handler = nm_handler_create(),
129 );
130 this->plugin = nm_strongswan_plugin_new(this->creds, this->handler);
131 nm_backend = this;
132
133 hydra->attributes->add_handler(hydra->attributes, &this->handler->handler);
134 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
135 if (!this->plugin)
136 {
137 DBG1(DBG_CFG, "DBUS binding failed");
138 nm_backend_deinit();
139 return FALSE;
140 }
141
142 lib->processor->queue_job(lib->processor,
143 (job_t*)callback_job_create_with_prio((callback_job_cb_t)run, this,
144 NULL, (callback_job_cancel_t)cancel, JOB_PRIO_CRITICAL));
145 return TRUE;
146 }
147
148 /**
149 * Initialize/deinitialize NetworkManager backend
150 */
151 static bool nm_backend_cb(void *plugin,
152 plugin_feature_t *feature, bool reg, void *data)
153 {
154 if (reg)
155 {
156 return nm_backend_init();
157 }
158 nm_backend_deinit();
159 return TRUE;
160 }
161
162 /*
163 * see header file
164 */
165 void nm_backend_register()
166 {
167 static plugin_feature_t features[] = {
168 PLUGIN_CALLBACK((plugin_feature_callback_t)nm_backend_cb, NULL),
169 PLUGIN_PROVIDE(CUSTOM, "NetworkManager backend"),
170 PLUGIN_DEPENDS(CUSTOM, "libcharon"),
171 PLUGIN_SDEPEND(PRIVKEY, KEY_RSA),
172 PLUGIN_SDEPEND(PRIVKEY, KEY_ECDSA),
173 PLUGIN_SDEPEND(CERT_DECODE, CERT_ANY),
174 PLUGIN_SDEPEND(CERT_DECODE, CERT_X509),
175 };
176 lib->plugins->add_static_features(lib->plugins, "nm-backend", features,
177 countof(features), TRUE);
178 }