2 * Copyright (C) 2010 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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>.
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
18 #include "strongswan-connection.h"
20 #define STRONGSWAN_CONNECTION_GET_PRIVATE(object) \
21 (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
22 STRONGSWAN_TYPE_CONNECTION, \
23 StrongswanConnectionPrivate))
25 struct _StrongswanConnectionPrivate
45 #ifndef USE_DYNAMIC_TYPES
46 G_DEFINE_TYPE (StrongswanConnection
, strongswan_connection
, G_TYPE_OBJECT
);
48 G_DEFINE_DYNAMIC_TYPE (StrongswanConnection
, strongswan_connection
, G_TYPE_OBJECT
);
49 void strongswan_connection_register (GTypeModule
*type_module
)
51 strongswan_connection_register_type (type_module
);
56 strongswan_connection_get_property (GObject
*object
,
61 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
65 g_value_set_string (value
, priv
->name
);
68 g_value_set_string (value
, priv
->host
);
71 g_value_set_string (value
, priv
->cert
);
74 g_value_set_string (value
, priv
->user
);
77 g_value_set_string (value
, priv
->pass
);
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
86 strongswan_connection_set_property (GObject
*object
,
91 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
96 priv
->name
= g_value_dup_string (value
);
99 priv
->host
= g_value_dup_string (value
);
103 priv
->cert
= g_value_dup_string (value
);
107 priv
->user
= g_value_dup_string (value
);
111 priv
->pass
= g_value_dup_string (value
);
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
120 strongswan_connection_init (StrongswanConnection
*connection
)
122 connection
->priv
= STRONGSWAN_CONNECTION_GET_PRIVATE (connection
);
126 strongswan_connection_dispose (GObject
*object
)
128 G_OBJECT_CLASS (strongswan_connection_parent_class
)->dispose (object
);
132 strongswan_connection_finalize (GObject
*object
)
134 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
135 g_free (priv
->orig_name
);
141 G_OBJECT_CLASS (strongswan_connection_parent_class
)->finalize (object
);
145 strongswan_connection_class_init (StrongswanConnectionClass
*klass
)
147 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
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
;
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",
158 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
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",
164 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
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",
170 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
172 g_object_class_install_property (object_class
, PROP_USER
,
173 g_param_spec_string ("user", "Username",
174 "The username for EAP authentication",
176 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
178 g_object_class_install_property (object_class
, PROP_PASS
,
179 g_param_spec_string ("pass", "Password",
180 "The password for EAP authentication",
182 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
184 g_type_class_add_private (klass
, sizeof (StrongswanConnectionPrivate
));
187 #ifdef USE_DYNAMIC_TYPES
189 strongswan_connection_class_finalize (StrongswanConnectionClass
*klass
)
194 static inline gchar
*
195 get_string_from_key_file (GKeyFile
*key_file
,
199 GError
*error
= NULL
;
201 value
= g_key_file_get_string (key_file
, name
, key
, &error
);
204 g_warning ("Failed to read %s/%s from key file: %s",
205 name
, key
, error
->message
);
206 g_error_free (error
);
212 strongswan_connection_update_from_key_file (GKeyFile
*key_file
,
213 StrongswanConnection
*connection
)
215 StrongswanConnectionPrivate
*priv
= connection
->priv
;
216 gchar
*name
= priv
->name
;
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");
225 StrongswanConnection
*
226 strongswan_connection_new (const gchar
*name
)
228 StrongswanConnection
*conn
;
229 conn
= g_object_new (STRONGSWAN_TYPE_CONNECTION
,
232 g_return_val_if_fail (conn
->priv
!= NULL
, NULL
);
236 StrongswanConnection
*
237 strongswan_connection_new_from_key_file (GKeyFile
*key_file
,
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
);
247 strongswan_connection_save_to_key_file (GKeyFile
*key_file
,
248 StrongswanConnection
*connection
)
250 StrongswanConnectionPrivate
*priv
= connection
->priv
;
251 gchar
*name
= priv
->name
;
253 if (priv
->orig_name
&& strcmp (name
, priv
->orig_name
))
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
);
262 g_key_file_set_string (key_file
, name
, "host", priv
->host
);
266 g_key_file_set_string (key_file
, name
, "cert", priv
->cert
);
270 g_key_file_set_string (key_file
, name
, "user", priv
->user
);
274 g_key_file_set_string (key_file
, name
, "pass", priv
->pass
);