added .gitignore files to NM applet
[strongswan.git] / src / charon / plugins / nm / nm_plugin.c
1 /*
2 * Copyright (C) 2008-2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "nm_plugin.h"
17 #include "nm_service.h"
18 #include "nm_creds.h"
19 #include "nm_handler.h"
20
21 #include <daemon.h>
22 #include <processing/jobs/callback_job.h>
23
24 #define CAP_DAC_OVERRIDE 1
25
26 typedef struct private_nm_plugin_t private_nm_plugin_t;
27
28 /**
29 * private data of nm plugin
30 */
31 struct private_nm_plugin_t {
32
33 /**
34 * implements plugin interface
35 */
36 nm_plugin_t public;
37
38 /**
39 * NetworkManager service (VPNPlugin)
40 */
41 NMStrongswanPlugin *plugin;
42
43 /**
44 * Glib main loop for a thread, handles DBUS calls
45 */
46 GMainLoop *loop;
47
48 /**
49 * credential set registered at the daemon
50 */
51 nm_creds_t *creds;
52
53 /**
54 * attribute handler regeisterd at the daemon
55 */
56 nm_handler_t *handler;
57 };
58
59 /**
60 * NM plugin processing routine, creates and handles NMVPNPlugin
61 */
62 static job_requeue_t run(private_nm_plugin_t *this)
63 {
64 this->loop = g_main_loop_new(NULL, FALSE);
65 g_main_loop_run(this->loop);
66 g_main_loop_unref(this->loop);
67
68 return JOB_REQUEUE_NONE;
69 }
70
71 /**
72 * Implementation of plugin_t.destroy
73 */
74 static void destroy(private_nm_plugin_t *this)
75 {
76 if (this->loop)
77 {
78 g_main_loop_quit(this->loop);
79 }
80 if (this->plugin)
81 {
82 g_object_unref(this->plugin);
83 }
84 charon->credentials->remove_set(charon->credentials, &this->creds->set);
85 this->creds->destroy(this->creds);
86 charon->attributes->remove_handler(charon->attributes, &this->handler->handler);
87 this->handler->destroy(this->handler);
88 free(this);
89 }
90
91 /*
92 * see header file
93 */
94 plugin_t *plugin_create()
95 {
96 private_nm_plugin_t *this = malloc_thing(private_nm_plugin_t);
97
98 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
99
100 this->loop = NULL;
101 g_type_init ();
102 if (!g_thread_supported())
103 {
104 g_thread_init(NULL);
105 }
106
107 this->creds = nm_creds_create();
108 this->handler = nm_handler_create();
109 charon->credentials->add_set(charon->credentials, &this->creds->set);
110 charon->attributes->add_handler(charon->attributes, &this->handler->handler);
111 this->plugin = nm_strongswan_plugin_new(this->creds, this->handler);
112 if (!this->plugin)
113 {
114 DBG1(DBG_CFG, "DBUS binding failed");
115 destroy(this);
116 return NULL;
117 }
118
119 /* bypass file permissions to read from users ssh-agent */
120 charon->keep_cap(charon, CAP_DAC_OVERRIDE);
121
122 charon->processor->queue_job(charon->processor,
123 (job_t*)callback_job_create((callback_job_cb_t)run, this, NULL, NULL));
124
125 return &this->public.plugin;
126 }
127