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.
34 #include "nss.h" /* Don't use <> here or it will include the system nss.h instead */
39 #include <glib/gi18n-lib.h>
42 #include "camel-certdb.h"
43 #include "camel-debug.h"
44 #include "camel-provider.h"
45 #include "camel-private.h"
48 /* To protect NSS initialization and shutdown. This prevents
49 concurrent calls to shutdown() and init() by different threads */
50 PRLock *nss_initlock = NULL;
52 /* Whether or not Camel has initialized the NSS library. We cannot
53 unconditionally call NSS_Shutdown() if NSS was initialized by other
54 library before. This boolean ensures that we only perform a cleanup
55 if and only if Camel is the one that previously initialized NSS */
56 volatile gboolean nss_initialized = FALSE;
59 static gint initialised = FALSE;
61 gint camel_application_is_exiting = FALSE;
64 camel_init (const gchar *configdir, gboolean nss_init)
72 bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
73 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
77 /* initialise global camel_object_type */
78 camel_object_get_type();
85 if (nss_initlock == NULL) {
86 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
87 nss_initlock = PR_NewLock();
89 PR_Lock (nss_initlock);
92 nss_configdir = g_strdup (configdir);
94 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
97 if (!NSS_IsInitialized()) {
100 if (NSS_InitReadWrite (nss_configdir) == SECFailure) {
101 /* fall back on using volatile dbs? */
102 if (NSS_NoDB_Init (nss_configdir) == SECFailure) {
103 g_free (nss_configdir);
104 g_warning ("Failed to initialize NSS");
106 PR_Unlock(nss_initlock);
112 NSS_SetDomesticPolicy ();
114 PR_Unlock(nss_initlock);
116 /* we must enable all ciphersuites */
117 for (indx = 0; indx < SSL_NumImplementedCiphers; indx++) {
118 if (!SSL_IS_SSL2_CIPHER(SSL_ImplementedCiphers[indx]))
119 SSL_CipherPrefSetDefault (SSL_ImplementedCiphers[indx], PR_TRUE);
122 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
123 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
124 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
125 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
127 g_free (nss_configdir);
129 #endif /* HAVE_NSS */
131 path = g_strdup_printf ("%s/camel-cert.db", configdir);
132 certdb = camel_certdb_new ();
133 camel_certdb_set_filename (certdb, path);
136 /* if we fail to load, who cares? it'll just be a volatile certdb */
137 camel_certdb_load (certdb);
139 /* set this certdb as the default db */
140 camel_certdb_set_default (certdb);
142 camel_object_unref (certdb);
150 camel_shutdown (void)
157 certdb = camel_certdb_get_default ();
159 camel_certdb_save (certdb);
160 camel_certdb_set_default (NULL);
163 /* These next calls must come last. */
165 #if defined (HAVE_NSS)
166 PR_Lock(nss_initlock);
169 PR_Unlock(nss_initlock);
170 #endif /* HAVE_NSS */