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.
32 #include "nss.h" /* Don't use <> here or it will include the system nss.h instead */
36 #include <glib/gi18n-lib.h>
39 #include "camel-certdb.h"
40 #include "camel-debug.h"
41 #include "camel-provider.h"
42 #include "camel-win32.h"
44 /* To protect NSS initialization and shutdown. This prevents
45 * concurrent calls to shutdown () and init () by different threads */
46 PRLock *nss_initlock = NULL;
48 /* Whether or not Camel has initialized the NSS library. We cannot
49 * unconditionally call NSS_Shutdown () if NSS was initialized by other
50 * library before. This boolean ensures that we only perform a cleanup
51 * if and only if Camel is the one that previously initialized NSS */
52 volatile gboolean nss_initialized = FALSE;
54 static gint initialised = FALSE;
56 gint camel_application_is_exiting = FALSE;
58 #define NSS_SYSTEM_DB "/etc/pki/nssdb"
61 nss_has_system_db (void)
68 f = fopen (NSS_SYSTEM_DB "/pkcs11.txt", "r");
72 /* Check whether the system NSS db is actually enabled */
73 while (fgets (buf, 80, f) && !found) {
74 if (!strcmp (buf, "library=libnsssysinit.so\n"))
83 camel_init (const gchar *configdir,
92 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
93 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
98 gchar *nss_configdir = NULL;
99 gchar *nss_sql_configdir = NULL;
100 SECStatus status = SECFailure;
103 if (nss_initlock == NULL) {
104 PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
105 nss_initlock = PR_NewLock ();
107 PR_Lock (nss_initlock);
109 if (NSS_IsInitialized ())
113 nss_configdir = g_strdup (configdir);
115 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
118 if (nss_has_system_db ()) {
119 nss_sql_configdir = g_strdup ("sql:" NSS_SYSTEM_DB );
121 /* On Windows, we use the Evolution configdir. On other
122 * operating systems we use ~/.pki/nssdb/, which is where
123 * the user-specific part of the "shared system db" is
124 * stored and is what Chrome uses too.
126 * We have to create the configdir if it does not exist,
127 * to prevent camel from bailing out on first run. */
129 g_mkdir_with_parents (configdir, 0700);
130 nss_sql_configdir = g_strconcat (
131 "sql:", nss_configdir, NULL);
133 gchar *user_nss_dir = g_build_filename (
134 g_get_home_dir (), ".pki/nssdb", NULL );
135 if (g_mkdir_with_parents (user_nss_dir, 0700))
137 "Failed to create SQL "
138 "database directory %s: %s\n",
139 user_nss_dir, strerror (errno));
141 nss_sql_configdir = g_strconcat (
142 "sql:", user_nss_dir, NULL);
143 g_free (user_nss_dir);
147 #if NSS_VMAJOR > 3 || (NSS_VMAJOR == 3 && NSS_VMINOR >= 12)
148 /* See: https://wiki.mozilla.org/NSS_Shared_DB,
149 * particularly "Mode 3A". Note that the target
150 * directory MUST EXIST. */
151 status = NSS_InitWithMerge (
152 nss_sql_configdir, /* dest dir */
153 "", "", /* new DB name prefixes */
154 SECMOD_DB, /* secmod name */
155 nss_configdir, /* old DB dir */
156 "", "", /* old DB name prefixes */
157 nss_configdir, /* unique ID for old DB */
158 "Evolution S/MIME", /* UI name for old DB */
161 if (status == SECFailure) {
163 "Failed to initialize NSS SQL database in %s: NSS error %d",
164 nss_sql_configdir, PORT_GetError ());
165 /* Fall back to opening the old DBM database */
168 /* Support old versions of libnss, pre-sqlite support. */
169 if (status == SECFailure)
170 status = NSS_InitReadWrite (nss_configdir);
171 if (status == SECFailure) {
172 /* Fall back to using volatile dbs? */
173 status = NSS_NoDB_Init (nss_configdir);
174 if (status == SECFailure) {
175 g_free (nss_configdir);
176 g_free (nss_sql_configdir);
177 g_warning ("Failed to initialize NSS");
178 PR_Unlock (nss_initlock);
183 nss_initialized = TRUE;
186 NSS_SetDomesticPolicy ();
188 PR_Unlock (nss_initlock);
190 /* we must enable all ciphersuites */
191 for (indx = 0; indx < SSL_NumImplementedCiphers; indx++) {
192 if (!SSL_IS_SSL2_CIPHER (SSL_ImplementedCiphers[indx]))
193 SSL_CipherPrefSetDefault (SSL_ImplementedCiphers[indx], PR_TRUE);
196 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
197 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
198 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
199 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
201 g_free (nss_configdir);
202 g_free (nss_sql_configdir);
205 path = g_strdup_printf ("%s/camel-cert.db", configdir);
206 certdb = camel_certdb_new ();
207 camel_certdb_set_filename (certdb, path);
210 /* if we fail to load, who cares? it'll just be a volatile certdb */
211 camel_certdb_load (certdb);
213 /* set this certdb as the default db */
214 camel_certdb_set_default (certdb);
216 g_object_unref (certdb);
229 camel_shutdown (void)
236 certdb = camel_certdb_get_default ();
238 camel_certdb_save (certdb);
239 camel_certdb_set_default (NULL);
242 /* These next calls must come last. */
244 if (nss_initlock != NULL) {
245 PR_Lock (nss_initlock);
248 PR_Unlock (nss_initlock);