~ubuntu-branches/ubuntu/wily/evolution-data-server/wily

« back to all changes in this revision

Viewing changes to camel/camel-network-service.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2015-07-20 13:34:59 UTC
  • mfrom: (1.1.126) (1.2.48 sid)
  • Revision ID: package-import@ubuntu.com-20150720133459-g6y46hnu5ewtoz08
Tags: 3.16.4-0ubuntu2
debian/patches/0001-Bug-752373-Monthly-events-do-not-recur-correctly.patch:
Cherry-pick patch from upstream to fix events not recurring correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * camel-network-service.c
3
3
 *
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.
7
7
 *
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.
12
12
 *
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/>.
15
15
 *
16
16
 */
17
17
#if HAVE_CONFIG_H
24
24
#include <glib/gstdio.h>
25
25
#include <glib/gi18n-lib.h>
26
26
 
 
27
#include "camel.h"
27
28
#include <camel/camel-enumtypes.h>
28
29
#include <camel/camel-network-settings.h>
29
30
#include <camel/camel-service.h>
66
67
        camel_network_service,
67
68
        CAMEL_TYPE_SERVICE)
68
69
 
69
 
static const gchar *
70
 
network_service_get_cert_dir (void)
71
 
{
72
 
        static gchar *cert_dir = NULL;
73
 
 
74
 
        if (G_UNLIKELY (cert_dir == NULL)) {
75
 
                const gchar *data_dir;
76
 
                const gchar *home_dir;
77
 
                gchar *old_dir;
78
 
 
79
 
                home_dir = g_get_home_dir ();
80
 
                data_dir = g_get_user_data_dir ();
81
 
 
82
 
                cert_dir = g_build_filename (data_dir, "camel_certs", NULL);
83
 
 
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));
89
 
                        }
90
 
                }
91
 
                g_free (old_dir);
92
 
 
93
 
                g_mkdir_with_parents (cert_dir, 0700);
94
 
        }
95
 
 
96
 
        return cert_dir;
97
 
}
98
 
 
99
70
static gchar *
100
71
network_service_generate_fingerprint (GTlsCertificate *certificate)
101
72
{
139
110
        return g_string_free (fingerprint, FALSE);
140
111
}
141
112
 
142
 
static GBytes *
143
 
network_service_load_cert_file (const gchar *fingerprint,
144
 
                                GError **error)
145
 
{
146
 
        GBytes *bytes = NULL;
147
 
        gchar *contents = NULL;
148
 
        gchar *filename;
149
 
        gsize length;
150
 
        const gchar *cert_dir;
151
 
 
152
 
        cert_dir = network_service_get_cert_dir ();
153
 
        filename = g_build_filename (cert_dir, fingerprint, NULL);
154
 
 
155
 
        if (g_file_get_contents (filename, &contents, &length, error))
156
 
                bytes = g_bytes_new_take (contents, length);
157
 
 
158
 
        g_free (filename);
159
 
 
160
 
        return bytes;
161
 
}
162
 
 
163
 
static GBytes *
164
 
network_service_save_cert_file (GTlsCertificate *certificate,
165
 
                                GError **error)
166
 
{
167
 
        GByteArray *der;
168
 
        GBytes *bytes = NULL;
169
 
        GFile *file;
170
 
        GFileOutputStream *output_stream;
171
 
        gchar *filename;
172
 
        gchar *fingerprint;
173
 
        const gchar *cert_dir;
174
 
 
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);
178
 
 
179
 
        fingerprint = network_service_generate_fingerprint (certificate);
180
 
        g_return_val_if_fail (fingerprint != NULL, NULL);
181
 
 
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);
185
 
 
186
 
        output_stream = g_file_replace (
187
 
                file, NULL, FALSE,
188
 
                G_FILE_CREATE_REPLACE_DESTINATION,
189
 
                NULL, error);
190
 
 
191
 
        g_object_unref (file);
192
 
        g_free (filename);
193
 
 
194
 
        if (output_stream != NULL) {
195
 
                gssize n_written;
196
 
 
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);
202
 
 
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),
208
 
                        bytes, NULL, error);
209
 
 
210
 
                if (n_written < 0) {
211
 
                        g_bytes_unref (bytes);
212
 
                        bytes = NULL;
213
 
                }
214
 
        }
215
 
 
216
 
        g_byte_array_unref (der);
217
 
        g_free (fingerprint);
218
 
 
219
 
        return bytes;
220
 
}
221
 
 
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;
241
132
 
242
 
                cert->rawcert = network_service_load_cert_file (
243
 
                        fingerprint, &local_error);
 
133
                camel_cert_load_cert_file (cert, &local_error);
244
134
 
245
135
                /* Sanity check. */
246
136
                g_warn_if_fail (
285
175
                              CamelCert *cert,
286
176
                              GTlsCertificate *certificate)
287
177
{
 
178
        GByteArray *der = NULL;
288
179
        GError *local_error = NULL;
289
180
 
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);
 
183
 
 
184
        camel_cert_save_cert_file (cert, der, &local_error);
 
185
 
 
186
        g_byte_array_unref (der);
292
187
 
293
188
        /* Sanity check. */
294
189
        g_warn_if_fail (
320
215
        gchar *host;
321
216
 
322
217
        session = camel_service_ref_session (CAMEL_SERVICE (service));
 
218
        if (!session)
 
219
                return FALSE;
 
220
 
323
221
        settings = camel_service_ref_settings (CAMEL_SERVICE (service));
324
222
 
325
223
        network_settings = CAMEL_NETWORK_SETTINGS (settings);
414
312
 
415
313
        session = camel_service_ref_session (CAMEL_SERVICE (service));
416
314
 
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);
 
315
        if (session) {
 
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);
422
321
 
423
 
        g_object_unref (session);
 
322
                g_object_unref (session);
 
323
        }
424
324
}
425
325
 
426
326
static void
520
420
        priv = CAMEL_NETWORK_SERVICE_GET_PRIVATE (service);
521
421
 
522
422
        session = camel_service_ref_session (CAMEL_SERVICE (service));
 
423
        if (!session)
 
424
                return;
 
425
 
523
426
        if (!camel_session_get_online (session)) {
524
427
                g_object_unref (session);
525
428
                return;
642
545
        if (method == CAMEL_NETWORK_SECURITY_METHOD_SSL_ON_ALTERNATE_PORT)
643
546
                g_socket_client_set_tls (client, TRUE);
644
547
 
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);
659
562
                GSocket *socket;
660
563
 
661
564
                socket = g_socket_connection_get_socket (connection);
662
 
                if (socket)
 
565
                if (socket) {
663
566
                        g_socket_set_timeout (socket, 90);
 
567
                        g_socket_set_keepalive (socket, TRUE);
 
568
                }
664
569
        }
665
570
 
666
571
        return (connection != NULL) ? G_IO_STREAM (connection) : NULL;