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