** Fix for bug #543943
[platform/upstream/evolution-data-server.git] / camel / camel.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *           Bertrand Guiheneuf <bertrand@helixcode.com>
5  *
6  *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  */
23
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <signal.h>
30
31 #ifdef HAVE_NSS
32 #include <nspr.h>
33 #include <prthread.h>
34 #include "nss.h"      /* Don't use <> here or it will include the system nss.h instead */
35 #include <ssl.h>
36 #endif /* HAVE_NSS */
37
38 #include <glib.h>
39 #include <glib/gi18n-lib.h>
40
41 #include "camel.h"
42 #include "camel-certdb.h"
43 #include "camel-debug.h"
44 #include "camel-provider.h"
45 #include "camel-private.h"
46
47 static int initialised = FALSE;
48
49 int camel_application_is_exiting = FALSE;
50
51 static void
52 camel_shutdown (void)
53 {
54         CamelCertDB *certdb;
55
56         if (!initialised)
57                 return;
58
59         certdb = camel_certdb_get_default ();
60         if (certdb) {
61                 camel_certdb_save (certdb);
62                 camel_object_unref (certdb);
63         }
64
65         /* These next calls must come last. */
66
67 #if defined (HAVE_NSS)
68         NSS_Shutdown ();
69         PR_Cleanup ();
70 #endif /* HAVE_NSS */
71
72         initialised = FALSE;
73 }
74
75 int
76 camel_init (const char *configdir, gboolean nss_init)
77 {
78         CamelCertDB *certdb;
79         char *path;
80         
81         if (initialised)
82                 return 0;
83
84         bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
85         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
86
87         camel_debug_init();
88
89         /* initialise global camel_object_type */
90         camel_object_get_type();
91
92 #ifdef HAVE_NSS
93         if (nss_init) {
94                 char *nss_configdir;
95                 PRUint16 indx;
96
97                 PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
98                 
99 #ifndef G_OS_WIN32
100                 nss_configdir = g_strdup (configdir);
101 #else
102                 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
103 #endif
104
105                 if (NSS_InitReadWrite (nss_configdir) == SECFailure) {
106                         /* fall back on using volatile dbs? */
107                         if (NSS_NoDB_Init (nss_configdir) == SECFailure) {
108                                 g_free (nss_configdir);
109                                 g_warning ("Failed to initialize NSS");
110                                 return -1;
111                         }
112                 }
113
114                 NSS_SetDomesticPolicy ();
115                 /* we must enable all ciphersuites */
116                 for (indx = 0; indx < SSL_NumImplementedCiphers; indx++) {
117                         if (!SSL_IS_SSL2_CIPHER(SSL_ImplementedCiphers[indx]))
118                                 SSL_CipherPrefSetDefault (SSL_ImplementedCiphers[indx], PR_TRUE);
119                 }       
120                 
121                 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
122                 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
123                 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
124                 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
125
126                 g_free (nss_configdir);
127         }
128 #endif /* HAVE_NSS */
129         
130         path = g_strdup_printf ("%s/camel-cert.db", configdir);
131         certdb = camel_certdb_new ();
132         camel_certdb_set_filename (certdb, path);
133         g_free (path);
134         
135         /* if we fail to load, who cares? it'll just be a volatile certdb */
136         camel_certdb_load (certdb);
137         
138         /* set this certdb as the default db */
139         camel_certdb_set_default (certdb);
140         
141         camel_object_unref (certdb);
142         
143         g_atexit (camel_shutdown);
144         
145         initialised = TRUE;
146         
147         return 0;
148 }