2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2008-2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include "nm_service.h"
19 #include "nm_handler.h"
23 #include <processing/jobs/callback_job.h>
25 #ifndef CAP_DAC_OVERRIDE
26 #define CAP_DAC_OVERRIDE 1
29 typedef struct nm_backend_t nm_backend_t
;
32 * Data for the NetworkManager backend.
37 * NetworkManager service (VPNPlugin)
39 NMStrongswanPlugin
*plugin
;
42 * Glib main loop for a thread, handles DBUS calls
47 * credential set registered at the daemon
52 * attribute handler regeisterd at the daemon
54 nm_handler_t
*handler
;
58 * Global (but private) instance of the NM backend.
60 static nm_backend_t
*nm_backend
= NULL
;
63 * NM plugin processing routine, creates and handles NMVPNPlugin
65 static job_requeue_t
run(nm_backend_t
*this)
67 this->loop
= g_main_loop_new(NULL
, FALSE
);
68 g_main_loop_run(this->loop
);
69 return JOB_REQUEUE_NONE
;
73 * Cancel the GLib Main Event Loop
75 static bool cancel(nm_backend_t
*this)
79 if (g_main_loop_is_running(this->loop
))
81 g_main_loop_quit(this->loop
);
83 g_main_loop_unref(this->loop
);
89 * Deinitialize NetworkManager backend
91 static void nm_backend_deinit()
93 nm_backend_t
*this = nm_backend
;
101 g_object_unref(this->plugin
);
103 lib
->credmgr
->remove_set(lib
->credmgr
, &this->creds
->set
);
104 hydra
->attributes
->remove_handler(hydra
->attributes
, &this->handler
->handler
);
105 this->creds
->destroy(this->creds
);
106 this->handler
->destroy(this->handler
);
113 * Initialize NetworkManager backend
115 static bool nm_backend_init()
120 if (!g_thread_supported())
126 .creds
= nm_creds_create(),
127 .handler
= nm_handler_create(),
129 this->plugin
= nm_strongswan_plugin_new(this->creds
, this->handler
);
132 hydra
->attributes
->add_handler(hydra
->attributes
, &this->handler
->handler
);
133 lib
->credmgr
->add_set(lib
->credmgr
, &this->creds
->set
);
136 DBG1(DBG_CFG
, "DBUS binding failed");
141 /* bypass file permissions to read from users ssh-agent */
142 charon
->caps
->keep(charon
->caps
, CAP_DAC_OVERRIDE
);
144 lib
->processor
->queue_job(lib
->processor
,
145 (job_t
*)callback_job_create_with_prio((callback_job_cb_t
)run
, this,
146 NULL
, (callback_job_cancel_t
)cancel
, JOB_PRIO_CRITICAL
));
151 * Initialize/deinitialize NetworkManager backend
153 static bool nm_backend_cb(void *plugin
,
154 plugin_feature_t
*feature
, bool reg
, void *data
)
158 return nm_backend_init();
167 void nm_backend_register()
169 static plugin_feature_t features
[] = {
170 PLUGIN_CALLBACK((plugin_feature_callback_t
)nm_backend_cb
, NULL
),
171 PLUGIN_PROVIDE(CUSTOM
, "NetworkManager backend"),
172 PLUGIN_DEPENDS(CUSTOM
, "libcharon"),
174 lib
->plugins
->add_static_features(lib
->plugins
, "nm-backend", features
,
175 countof(features
), TRUE
);