Don't pass NULL to PR_Lock() during camel_shutdown().
[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 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <signal.h>
29
30 #ifdef HAVE_NSS
31 #include <nspr.h>
32 #include <prthread.h>
33 #include "nss.h"      /* Don't use <> here or it will include the system nss.h instead */
34 #include <ssl.h>
35 #endif /* HAVE_NSS */
36
37 #include <glib.h>
38 #include <glib/gi18n-lib.h>
39
40 #include "camel.h"
41 #include "camel-certdb.h"
42 #include "camel-debug.h"
43 #include "camel-provider.h"
44 #include "camel-private.h"
45
46 #ifdef HAVE_NSS
47 /* To protect NSS initialization and shutdown. This prevents
48    concurrent calls to shutdown() and init() by different threads */
49 PRLock *nss_initlock = NULL;
50
51 /* Whether or not Camel has initialized the NSS library. We cannot
52    unconditionally call NSS_Shutdown() if NSS was initialized by other
53    library before. This boolean ensures that we only perform a cleanup
54    if and only if Camel is the one that previously initialized NSS */
55 volatile gboolean nss_initialized = FALSE;
56 #endif
57
58 static gint initialised = FALSE;
59
60 gint camel_application_is_exiting = FALSE;
61
62 gint
63 camel_init (const gchar *configdir, gboolean nss_init)
64 {
65         CamelCertDB *certdb;
66         gchar *path;
67
68         if (initialised)
69                 return 0;
70
71         bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
72         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
73
74         camel_debug_init();
75
76         /* initialise global camel_object_type */
77         camel_object_get_type();
78
79 #ifdef HAVE_NSS
80         if (nss_init) {
81                 gchar *nss_configdir;
82                 PRUint16 indx;
83
84                 if (nss_initlock == NULL) {
85                         PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
86                         nss_initlock = PR_NewLock();
87                 }
88                 PR_Lock (nss_initlock);
89
90 #ifndef G_OS_WIN32
91                 nss_configdir = g_strdup (configdir);
92 #else
93                 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
94 #endif
95
96                 if (!NSS_IsInitialized()) {
97                         nss_initialized = 1;
98
99                         if (NSS_InitReadWrite (nss_configdir) == SECFailure) {
100                                 /* fall back on using volatile dbs? */
101                                 if (NSS_NoDB_Init (nss_configdir) == SECFailure) {
102                                         g_free (nss_configdir);
103                                         g_warning ("Failed to initialize NSS");
104                                         nss_initialized = 0;
105                                         PR_Unlock(nss_initlock);
106                                         return -1;
107                                 }
108                         }
109                 }
110
111                 NSS_SetDomesticPolicy ();
112
113                 PR_Unlock(nss_initlock);
114
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         initialised = TRUE;
144
145         return 0;
146 }
147
148 void
149 camel_shutdown (void)
150 {
151         CamelCertDB *certdb;
152
153         if (!initialised)
154                 return;
155
156         certdb = camel_certdb_get_default ();
157         if (certdb) {
158                 camel_certdb_save (certdb);
159                 camel_certdb_set_default (NULL);
160         }
161
162         /* These next calls must come last. */
163
164 #if defined (HAVE_NSS)
165         if (nss_initlock != NULL) {
166                 PR_Lock(nss_initlock);
167                 if (nss_initialized)
168                         NSS_Shutdown ();
169                 PR_Unlock(nss_initlock);
170         }
171 #endif /* HAVE_NSS */
172
173         initialised = FALSE;
174 }