28c8f1366b84981cea5f2af08d004e6deda1afaa
[strongswan.git] / src / frontends / maemo / src / strongswan-connection.c
1 /*
2 * Copyright (C) 2010 Tobias Brunner
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 <string.h>
17
18 #include "strongswan-connection.h"
19
20 #define STRONGSWAN_CONNECTION_GET_PRIVATE(object) \
21 (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
22 STRONGSWAN_TYPE_CONNECTION, \
23 StrongswanConnectionPrivate))
24
25 struct _StrongswanConnectionPrivate
26 {
27 gchar *orig_name;
28 gchar *name;
29 gchar *host;
30 gchar *cert;
31 gchar *user;
32 gchar *pass;
33 };
34
35 enum
36 {
37 PROP_0,
38 PROP_NAME,
39 PROP_HOST,
40 PROP_CERT,
41 PROP_USER,
42 PROP_PASS,
43 };
44
45 #ifndef USE_DYNAMIC_TYPES
46 G_DEFINE_TYPE (StrongswanConnection, strongswan_connection, G_TYPE_OBJECT);
47 #else
48 G_DEFINE_DYNAMIC_TYPE (StrongswanConnection, strongswan_connection, G_TYPE_OBJECT);
49 void strongswan_connection_register (GTypeModule *type_module)
50 {
51 strongswan_connection_register_type (type_module);
52 }
53 #endif
54
55 static void
56 strongswan_connection_get_property (GObject *object,
57 guint prop_id,
58 GValue *value,
59 GParamSpec *pspec)
60 {
61 StrongswanConnectionPrivate *priv = STRONGSWAN_CONNECTION (object)->priv;
62 switch (prop_id)
63 {
64 case PROP_NAME:
65 g_value_set_string (value, priv->name);
66 break;
67 case PROP_HOST:
68 g_value_set_string (value, priv->host);
69 break;
70 case PROP_CERT:
71 g_value_set_string (value, priv->cert);
72 break;
73 case PROP_USER:
74 g_value_set_string (value, priv->user);
75 break;
76 case PROP_PASS:
77 g_value_set_string (value, priv->pass);
78 break;
79 default:
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
81 break;
82 }
83 }
84
85 static void
86 strongswan_connection_set_property (GObject *object,
87 guint prop_id,
88 const GValue *value,
89 GParamSpec *pspec)
90 {
91 StrongswanConnectionPrivate *priv = STRONGSWAN_CONNECTION (object)->priv;
92 switch (prop_id)
93 {
94 case PROP_NAME:
95 g_free (priv->name);
96 priv->name = g_value_dup_string (value);
97 case PROP_HOST:
98 g_free (priv->host);
99 priv->host = g_value_dup_string (value);
100 break;
101 case PROP_CERT:
102 g_free (priv->cert);
103 priv->cert = g_value_dup_string (value);
104 break;
105 case PROP_USER:
106 g_free (priv->user);
107 priv->user = g_value_dup_string (value);
108 break;
109 case PROP_PASS:
110 g_free (priv->pass);
111 priv->pass = g_value_dup_string (value);
112 break;
113 default:
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
115 break;
116 }
117 }
118
119 static void
120 strongswan_connection_init (StrongswanConnection *connection)
121 {
122 connection->priv = STRONGSWAN_CONNECTION_GET_PRIVATE (connection);
123 }
124
125 static void
126 strongswan_connection_dispose (GObject *object)
127 {
128 G_OBJECT_CLASS (strongswan_connection_parent_class)->dispose (object);
129 }
130
131 static void
132 strongswan_connection_finalize (GObject *object)
133 {
134 StrongswanConnectionPrivate *priv = STRONGSWAN_CONNECTION (object)->priv;
135 g_free (priv->orig_name);
136 g_free (priv->name);
137 g_free (priv->host);
138 g_free (priv->cert);
139 g_free (priv->user);
140 g_free (priv->pass);
141 G_OBJECT_CLASS (strongswan_connection_parent_class)->finalize (object);
142 }
143
144 static void
145 strongswan_connection_class_init (StrongswanConnectionClass *klass)
146 {
147 GObjectClass *object_class = G_OBJECT_CLASS (klass);
148
149 object_class->get_property = strongswan_connection_get_property;
150 object_class->set_property = strongswan_connection_set_property;
151 object_class->dispose = strongswan_connection_dispose;
152 object_class->finalize = strongswan_connection_finalize;
153
154 g_object_class_install_property (object_class, PROP_NAME,
155 g_param_spec_string ("name", "Connection name",
156 "The unique name of a connection",
157 NULL,
158 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159
160 g_object_class_install_property (object_class, PROP_HOST,
161 g_param_spec_string ("host", "Hostname or IP address",
162 "The hostname or IP address of the Gateway",
163 NULL,
164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165
166 g_object_class_install_property (object_class, PROP_CERT,
167 g_param_spec_string ("cert", "Gateway or CA certificate",
168 "The certificate of the gateway or the CA",
169 NULL,
170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171
172 g_object_class_install_property (object_class, PROP_USER,
173 g_param_spec_string ("user", "Username",
174 "The username for EAP authentication",
175 NULL,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177
178 g_object_class_install_property (object_class, PROP_PASS,
179 g_param_spec_string ("pass", "Password",
180 "The password for EAP authentication",
181 NULL,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183
184 g_type_class_add_private (klass, sizeof (StrongswanConnectionPrivate));
185 }
186
187 #ifdef USE_DYNAMIC_TYPES
188 static void
189 strongswan_connection_class_finalize (StrongswanConnectionClass *klass)
190 {
191 }
192 #endif
193
194 static inline gchar *
195 get_string_from_key_file (GKeyFile *key_file,
196 const gchar *name,
197 const gchar *key)
198 {
199 GError *error = NULL;
200 gchar *value;
201 value = g_key_file_get_string (key_file, name, key, &error);
202 if (error)
203 {
204 g_warning ("Failed to read %s/%s from key file: %s",
205 name, key, error->message);
206 g_error_free (error);
207 }
208 return value;
209 }
210
211 static void
212 strongswan_connection_update_from_key_file (GKeyFile *key_file,
213 StrongswanConnection *connection)
214 {
215 StrongswanConnectionPrivate *priv = connection->priv;
216 gchar *name = priv->name;
217
218 priv->orig_name = g_strdup (name);
219 priv->host = get_string_from_key_file (key_file, name, "host");
220 priv->cert = get_string_from_key_file (key_file, name, "cert");
221 priv->user = get_string_from_key_file (key_file, name, "user");
222 priv->pass = get_string_from_key_file (key_file, name, "pass");
223 }
224
225 StrongswanConnection *
226 strongswan_connection_new (const gchar *name)
227 {
228 StrongswanConnection *conn;
229 conn = g_object_new (STRONGSWAN_TYPE_CONNECTION,
230 "name", name,
231 NULL);
232 g_return_val_if_fail (conn->priv != NULL, NULL);
233 return conn;
234 }
235
236 StrongswanConnection *
237 strongswan_connection_new_from_key_file (GKeyFile *key_file,
238 const gchar *name)
239 {
240 StrongswanConnection *conn = strongswan_connection_new (name);
241 g_return_val_if_fail (conn != NULL, NULL);
242 strongswan_connection_update_from_key_file (key_file, conn);
243 return conn;
244 }
245
246 void
247 strongswan_connection_save_to_key_file (GKeyFile *key_file,
248 StrongswanConnection *connection)
249 {
250 StrongswanConnectionPrivate *priv = connection->priv;
251 gchar *name = priv->name;
252
253 if (priv->orig_name && strcmp (name, priv->orig_name))
254 {
255 g_key_file_remove_group (key_file, priv->orig_name, NULL);
256 g_free (priv->orig_name);
257 priv->orig_name = g_strdup (name);
258 }
259
260 if (priv->host)
261 {
262 g_key_file_set_string (key_file, name, "host", priv->host);
263 }
264 if (priv->cert)
265 {
266 g_key_file_set_string (key_file, name, "cert", priv->cert);
267 }
268 if (priv->user)
269 {
270 g_key_file_set_string (key_file, name, "user", priv->user);
271 }
272 if (priv->pass)
273 {
274 g_key_file_set_string (key_file, name, "pass", priv->pass);
275 }
276 }
277