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