Use static plugin features in libcharon to define essential dependencies
[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 #define CAP_DAC_OVERRIDE 1
26
27 typedef struct nm_backend_t nm_backend_t;
28
29 /**
30 * Data for the NetworkManager backend.
31 */
32 struct nm_backend_t {
33
34 /**
35 * NetworkManager service (VPNPlugin)
36 */
37 NMStrongswanPlugin *plugin;
38
39 /**
40 * Glib main loop for a thread, handles DBUS calls
41 */
42 GMainLoop *loop;
43
44 /**
45 * credential set registered at the daemon
46 */
47 nm_creds_t *creds;
48
49 /**
50 * attribute handler regeisterd at the daemon
51 */
52 nm_handler_t *handler;
53 };
54
55 /**
56 * Global (but private) instance of the NM backend.
57 */
58 static nm_backend_t *nm_backend = NULL;
59
60 /**
61 * NM plugin processing routine, creates and handles NMVPNPlugin
62 */
63 static job_requeue_t run(nm_backend_t *this)
64 {
65 this->loop = g_main_loop_new(NULL, FALSE);
66 g_main_loop_run(this->loop);
67 return JOB_REQUEUE_NONE;
68 }
69
70 /**
71 * Cancel the GLib Main Event Loop
72 */
73 static bool cancel(nm_backend_t *this)
74 {
75 if (this->loop)
76 {
77 if (g_main_loop_is_running(this->loop))
78 {
79 g_main_loop_quit(this->loop);
80 }
81 g_main_loop_unref(this->loop);
82 }
83 return TRUE;
84 }
85
86 /**
87 * Deinitialize NetworkManager backend
88 */
89 static void nm_backend_deinit()
90 {
91 nm_backend_t *this = nm_backend;
92
93 if (!this)
94 {
95 return;
96 }
97 if (this->plugin)
98 {
99 g_object_unref(this->plugin);
100 }
101 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
102 hydra->attributes->remove_handler(hydra->attributes, &this->handler->handler);
103 this->creds->destroy(this->creds);
104 this->handler->destroy(this->handler);
105 free(this);
106
107 nm_backend = NULL;
108 }
109
110 /**
111 * Initialize NetworkManager backend
112 */
113 static bool nm_backend_init()
114 {
115 nm_backend_t *this;
116
117 g_type_init ();
118 if (!g_thread_supported())
119 {
120 g_thread_init(NULL);
121 }
122
123 INIT(this,
124 .creds = nm_creds_create(),
125 .handler = nm_handler_create(),
126 );
127 this->plugin = nm_strongswan_plugin_new(this->creds, this->handler);
128 nm_backend = this;
129
130 hydra->attributes->add_handler(hydra->attributes, &this->handler->handler);
131 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
132 if (!this->plugin)
133 {
134 DBG1(DBG_CFG, "DBUS binding failed");
135 nm_backend_deinit();
136 return FALSE;
137 }
138
139 /* bypass file permissions to read from users ssh-agent */
140 charon->keep_cap(charon, CAP_DAC_OVERRIDE);
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 };
172 lib->plugins->add_static_features(lib->plugins, "nm-backend", features,
173 countof(features), TRUE);
174 }