1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
4 * Bertrand Guiheneuf <bertrand@helixcode.com>
6 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
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.
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.
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.
33 #include "nss.h" /* Don't use <> here or it will include the system nss.h instead */
37 #include <glib/gi18n-lib.h>
40 #include "camel-certdb.h"
41 #include "camel-debug.h"
42 #include "camel-provider.h"
43 #include "camel-win32.h"
46 /* To protect NSS initialization and shutdown. This prevents
47 concurrent calls to shutdown() and init() by different threads */
48 PRLock *nss_initlock = NULL;
50 /* Whether or not Camel has initialized the NSS library. We cannot
51 unconditionally call NSS_Shutdown() if NSS was initialized by other
52 library before. This boolean ensures that we only perform a cleanup
53 if and only if Camel is the one that previously initialized NSS */
54 volatile gboolean nss_initialized = FALSE;
57 static gint initialised = FALSE;
59 gint camel_application_is_exiting = FALSE;
62 camel_init (const gchar *configdir, gboolean nss_init)
70 bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
71 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
77 gchar *nss_configdir = NULL;
78 gchar *nss_sql_configdir = NULL;
82 if (nss_initlock == NULL) {
83 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
84 nss_initlock = PR_NewLock();
86 PR_Lock (nss_initlock);
88 if (NSS_IsInitialized ())
92 nss_configdir = g_strdup (configdir);
94 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
97 /* XXX Currently we store the new shared NSS database in the
98 * same location we kept the original NSS databases in,
99 * but at least we have safe shared access between Camel
100 * and Evolution's S/MIME. Once freedesktop.org comes
101 * up with a user-wide shared location, we should use
103 nss_sql_configdir = g_strconcat ("sql:", nss_configdir, NULL);
105 #if NSS_VMAJOR > 3 || (NSS_VMAJOR == 3 && NSS_VMINOR >= 12)
106 /* See: https://wiki.mozilla.org/NSS_Shared_DB,
107 * particularly "Mode 3A". Note that the target
108 * directory MUST EXIST. */
109 status = NSS_InitWithMerge (
110 nss_sql_configdir, /* dest dir */
111 "", "", /* new DB name prefixes */
112 SECMOD_DB, /* secmod name */
113 nss_configdir, /* old DB dir */
114 "", "", /* old DB name prefixes */
115 nss_configdir, /* unique ID for old DB */
116 "Evolution S/MIME", /* UI name for old DB */
119 if (status == SECFailure) {
120 g_free (nss_configdir);
121 g_free (nss_sql_configdir);
122 g_warning ("Failed to initialize NSS");
123 PR_Unlock (nss_initlock);
127 /* Support old versions of libnss, pre-sqlite support. */
128 status = NSS_InitReadWrite (nss_configdir);
129 if (status == SECFailure) {
130 /* Fall back to using volatile dbs? */
131 status = NSS_NoDB_Init (nss_config);
132 if (status == SECFailure) {
133 g_free (nss_configdir);
134 g_free (nss_sql_configdir);
135 g_warning ("Failed to initialize NSS");
136 PR_Unlock (nss_initlock);
142 nss_initialized = TRUE;
145 NSS_SetDomesticPolicy ();
147 PR_Unlock(nss_initlock);
149 /* we must enable all ciphersuites */
150 for (indx = 0; indx < SSL_NumImplementedCiphers; indx++) {
151 if (!SSL_IS_SSL2_CIPHER(SSL_ImplementedCiphers[indx]))
152 SSL_CipherPrefSetDefault (SSL_ImplementedCiphers[indx], PR_TRUE);
155 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
156 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
157 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
158 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
160 g_free (nss_configdir);
161 g_free (nss_sql_configdir);
163 #endif /* HAVE_NSS */
165 path = g_strdup_printf ("%s/camel-cert.db", configdir);
166 certdb = camel_certdb_new ();
167 camel_certdb_set_filename (certdb, path);
170 /* if we fail to load, who cares? it'll just be a volatile certdb */
171 camel_certdb_load (certdb);
173 /* set this certdb as the default db */
174 camel_certdb_set_default (certdb);
176 g_object_unref (certdb);
189 camel_shutdown (void)
196 certdb = camel_certdb_get_default ();
198 camel_certdb_save (certdb);
199 camel_certdb_set_default (NULL);
202 /* These next calls must come last. */
204 #if defined (HAVE_NSS)
205 if (nss_initlock != NULL) {
206 PR_Lock(nss_initlock);
209 PR_Unlock(nss_initlock);
211 #endif /* HAVE_NSS */