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 G_DEFINE_TYPE (StrongswanConnection
, strongswan_connection
, G_TYPE_OBJECT
);
48 strongswan_connection_get_property (GObject
*object
,
53 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
57 g_value_set_string (value
, priv
->name
);
60 g_value_set_string (value
, priv
->host
);
63 g_value_set_string (value
, priv
->cert
);
66 g_value_set_string (value
, priv
->user
);
69 g_value_set_string (value
, priv
->pass
);
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
78 strongswan_connection_set_property (GObject
*object
,
83 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
88 priv
->name
= g_value_dup_string (value
);
91 priv
->host
= g_value_dup_string (value
);
95 priv
->cert
= g_value_dup_string (value
);
99 priv
->user
= g_value_dup_string (value
);
103 priv
->pass
= g_value_dup_string (value
);
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
112 strongswan_connection_init (StrongswanConnection
*connection
)
114 connection
->priv
= STRONGSWAN_CONNECTION_GET_PRIVATE (connection
);
118 strongswan_connection_dispose (GObject
*object
)
120 G_OBJECT_CLASS (strongswan_connection_parent_class
)->dispose (object
);
124 strongswan_connection_finalize (GObject
*object
)
126 StrongswanConnectionPrivate
*priv
= STRONGSWAN_CONNECTION (object
)->priv
;
127 g_free (priv
->orig_name
);
133 G_OBJECT_CLASS (strongswan_connection_parent_class
)->finalize (object
);
137 strongswan_connection_class_init (StrongswanConnectionClass
*klass
)
139 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
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
;
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",
150 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
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",
156 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
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",
162 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
164 g_object_class_install_property (object_class
, PROP_USER
,
165 g_param_spec_string ("user", "Username",
166 "The username for EAP authentication",
168 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
170 g_object_class_install_property (object_class
, PROP_PASS
,
171 g_param_spec_string ("pass", "Password",
172 "The password for EAP authentication",
174 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
176 g_type_class_add_private (klass
, sizeof (StrongswanConnectionPrivate
));
179 static inline gchar
*
180 get_string_from_key_file (GKeyFile
*key_file
,
184 GError
*error
= NULL
;
186 value
= g_key_file_get_string (key_file
, name
, key
, &error
);
189 g_warning ("Failed to read %s/%s from key file: %s",
190 name
, key
, error
->message
);
191 g_error_free (error
);
197 strongswan_connection_update_from_key_file (GKeyFile
*key_file
,
198 StrongswanConnection
*connection
)
200 StrongswanConnectionPrivate
*priv
= connection
->priv
;
201 gchar
*name
= priv
->name
;
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");
210 StrongswanConnection
*
211 strongswan_connection_new (const gchar
*name
)
213 StrongswanConnection
*conn
;
214 conn
= g_object_new (STRONGSWAN_TYPE_CONNECTION
,
217 g_return_val_if_fail (conn
->priv
!= NULL
, NULL
);
221 StrongswanConnection
*
222 strongswan_connection_new_from_key_file (GKeyFile
*key_file
,
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
);
232 strongswan_connection_save_to_key_file (GKeyFile
*key_file
,
233 StrongswanConnection
*connection
)
235 StrongswanConnectionPrivate
*priv
= connection
->priv
;
236 gchar
*name
= priv
->name
;
238 if (priv
->orig_name
&& strcmp (name
, priv
->orig_name
))
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
);
247 g_key_file_set_string (key_file
, name
, "host", priv
->host
);
251 g_key_file_set_string (key_file
, name
, "cert", priv
->cert
);
255 g_key_file_set_string (key_file
, name
, "user", priv
->user
);
259 g_key_file_set_string (key_file
, name
, "pass", priv
->pass
);