2
2
* camel-network-service.c
4
* This library is free software you can redistribute it and/or modify it
4
* This library is free software: you can redistribute it and/or modify it
5
5
* under the terms of the GNU Lesser General Public License as published by
6
6
* the Free Software Foundation.
8
8
* This library is distributed in the hope that it will be useful, but
9
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11
11
* for more details.
13
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this library; if not, see <http://www.gnu.org/licenses/>.
14
* along with this library. If not, see <http://www.gnu.org/licenses/>.
66
67
camel_network_service,
67
68
CAMEL_TYPE_SERVICE)
70
network_service_get_cert_dir (void)
72
static gchar *cert_dir = NULL;
74
if (G_UNLIKELY (cert_dir == NULL)) {
75
const gchar *data_dir;
76
const gchar *home_dir;
79
home_dir = g_get_home_dir ();
80
data_dir = g_get_user_data_dir ();
82
cert_dir = g_build_filename (data_dir, "camel_certs", NULL);
84
/* Move the old certificate directory if present. */
85
old_dir = g_build_filename (home_dir, ".camel_certs", NULL);
86
if (g_file_test (old_dir, G_FILE_TEST_IS_DIR)) {
87
if (g_rename (old_dir, cert_dir) == -1) {
88
g_warning ("%s: Failed to rename '%s' to '%s': %s", G_STRFUNC, old_dir, cert_dir, g_strerror (errno));
93
g_mkdir_with_parents (cert_dir, 0700);
100
71
network_service_generate_fingerprint (GTlsCertificate *certificate)
139
110
return g_string_free (fingerprint, FALSE);
143
network_service_load_cert_file (const gchar *fingerprint,
146
GBytes *bytes = NULL;
147
gchar *contents = NULL;
150
const gchar *cert_dir;
152
cert_dir = network_service_get_cert_dir ();
153
filename = g_build_filename (cert_dir, fingerprint, NULL);
155
if (g_file_get_contents (filename, &contents, &length, error))
156
bytes = g_bytes_new_take (contents, length);
164
network_service_save_cert_file (GTlsCertificate *certificate,
168
GBytes *bytes = NULL;
170
GFileOutputStream *output_stream;
173
const gchar *cert_dir;
175
/* XXX No accessor function for this property. */
176
g_object_get (certificate, "certificate", &der, NULL);
177
g_return_val_if_fail (der != NULL, NULL);
179
fingerprint = network_service_generate_fingerprint (certificate);
180
g_return_val_if_fail (fingerprint != NULL, NULL);
182
cert_dir = network_service_get_cert_dir ();
183
filename = g_build_filename (cert_dir, fingerprint, NULL);
184
file = g_file_new_for_path (filename);
186
output_stream = g_file_replace (
188
G_FILE_CREATE_REPLACE_DESTINATION,
191
g_object_unref (file);
194
if (output_stream != NULL) {
197
/* XXX Treat GByteArray as though its data is owned by
198
* GTlsCertificate. That means avoiding functions
199
* like g_byte_array_free_to_bytes() that alter or
200
* reset the GByteArray. */
201
bytes = g_bytes_new (der->data, der->len);
203
/* XXX Not handling partial writes, but GIO does not make
204
* it easy. Need a g_output_stream_write_all_bytes().
205
* (see: https://bugzilla.gnome.org/708838) */
206
n_written = g_output_stream_write_bytes (
207
G_OUTPUT_STREAM (output_stream),
211
g_bytes_unref (bytes);
216
g_byte_array_unref (der);
217
g_free (fingerprint);
222
113
static CamelCert *
223
114
network_service_certdb_lookup (CamelCertDB *certdb,
224
115
GTlsCertificate *certificate,
239
130
if (cert->rawcert == NULL) {
240
131
GError *local_error = NULL;
242
cert->rawcert = network_service_load_cert_file (
243
fingerprint, &local_error);
133
camel_cert_load_cert_file (cert, &local_error);
245
135
/* Sanity check. */
286
176
GTlsCertificate *certificate)
178
GByteArray *der = NULL;
288
179
GError *local_error = NULL;
290
cert->rawcert = network_service_save_cert_file (
291
certificate, &local_error);
181
g_object_get (certificate, "certificate", &der, NULL);
182
g_return_if_fail (der != NULL);
184
camel_cert_save_cert_file (cert, der, &local_error);
186
g_byte_array_unref (der);
293
188
/* Sanity check. */
415
313
session = camel_service_ref_session (CAMEL_SERVICE (service));
417
camel_session_idle_add (
418
session, G_PRIORITY_DEFAULT_IDLE,
419
network_service_notify_host_reachable_cb,
420
g_object_ref (service),
421
(GDestroyNotify) g_object_unref);
316
camel_session_idle_add (
317
session, G_PRIORITY_DEFAULT_IDLE,
318
network_service_notify_host_reachable_cb,
319
g_object_ref (service),
320
(GDestroyNotify) g_object_unref);
423
g_object_unref (session);
322
g_object_unref (session);
642
545
if (method == CAMEL_NETWORK_SECURITY_METHOD_SSL_ON_ALTERNATE_PORT)
643
546
g_socket_client_set_tls (client, TRUE);
645
g_object_bind_property (
548
camel_binding_bind_property (
646
549
service, "proxy-resolver",
647
550
client, "proxy-resolver",
648
551
G_BINDING_SYNC_CREATE);