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
43 #ifndef USE_DYNAMIC_TYPES
44 G_DEFINE_TYPE (StrongswanConnection
, strongswan_connection
, G_TYPE_OBJECT
);
46 G_DEFINE_DYNAMIC_TYPE (StrongswanConnection
, strongswan_connection
, G_TYPE_OBJECT
);
47 void strongswan_connection_register (GTypeModule
*type_module
)
49 strongswan_connection_register_type (type_module
);
54 strongswan_connection_get_property (GObject
*object
,
59 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
63 g_value_set_string (value
, priv
->name
);
66 g_value_set_string (value
, priv
->host
);
69 g_value_set_string (value
, priv
->cert
);
72 g_value_set_string (value
, priv
->user
);
75 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
81 strongswan_connection_set_property (GObject
*object
,
86 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
91 priv
->name
= g_value_dup_string (value
);
94 priv
->host
= g_value_dup_string (value
);
98 priv
->cert
= g_value_dup_string (value
);
102 priv
->user
= g_value_dup_string (value
);
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
111 strongswan_connection_init (StrongswanConnection
*connection
)
113 connection
->priv
= STRONGSWAN_CONNECTION_GET_PRIVATE (connection
);
117 strongswan_connection_dispose (GObject
*object
)
119 G_OBJECT_CLASS (strongswan_connection_parent_class
)->dispose (object
);
123 strongswan_connection_finalize (GObject
*object
)
125 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
126 g_free (priv
->orig_name
);
131 G_OBJECT_CLASS (strongswan_connection_parent_class
)->finalize (object
);
135 strongswan_connection_class_init (StrongswanConnectionClass
*klass
)
137 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
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
;
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",
148 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
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",
154 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
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",
160 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
162 g_object_class_install_property (object_class
, PROP_USER
,
163 g_param_spec_string ("user", "Username",
164 "The username for EAP authentication",
166 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
168 g_type_class_add_private (klass
, sizeof (StrongswanConnectionPrivate
));
171 #ifdef USE_DYNAMIC_TYPES
173 strongswan_connection_class_finalize (StrongswanConnectionClass
*klass
)
178 static inline gchar
*
179 get_string_from_key_file (GKeyFile
*key_file
,
183 GError
*error
= NULL
;
185 value
= g_key_file_get_string (key_file
, name
, key
, &error
);
188 g_warning ("Failed to read %s/%s from key file: %s",
189 name
, key
, error
->message
);
190 g_error_free (error
);
196 strongswan_connection_update_from_key_file (GKeyFile
*key_file
,
197 StrongswanConnection
*connection
)
199 StrongswanConnectionPrivate
*priv
= connection
->priv
;
200 gchar
*name
= priv
->name
;
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");
208 StrongswanConnection
*
209 strongswan_connection_new (const gchar
*name
)
211 StrongswanConnection
*conn
;
212 conn
= g_object_new (STRONGSWAN_TYPE_CONNECTION
,
215 g_return_val_if_fail (conn
->priv
!= NULL
, NULL
);
219 StrongswanConnection
*
220 strongswan_connection_new_from_key_file (GKeyFile
*key_file
,
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
);
230 strongswan_connection_save_to_key_file (GKeyFile
*key_file
,
231 StrongswanConnection
*connection
)
233 StrongswanConnectionPrivate
*priv
= connection
->priv
;
234 gchar
*name
= priv
->name
;
236 if (priv
->orig_name
&& strcmp (name
, priv
->orig_name
))
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
);
245 g_key_file_set_string (key_file
, name
, "host", priv
->host
);
249 g_key_file_set_string (key_file
, name
, "cert", priv
->cert
);
253 g_key_file_remove_key (key_file
, name
, "cert", NULL
);
257 g_key_file_set_string (key_file
, name
, "user", priv
->user
);