Bug 620522 - Build break: 'nss_config' undeclared
[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/gi18n-lib.h>
38
39 #include "camel.h"
40 #include "camel-certdb.h"
41 #include "camel-debug.h"
42 #include "camel-provider.h"
43 #include "camel-win32.h"
44
45 #ifdef HAVE_NSS
46 /* To protect NSS initialization and shutdown. This prevents
47    concurrent calls to shutdown() and init() by different threads */
48 PRLock *nss_initlock = NULL;
49
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;
55 #endif
56
57 static gint initialised = FALSE;
58
59 gint camel_application_is_exiting = FALSE;
60
61 gint
62 camel_init (const gchar *configdir, gboolean nss_init)
63 {
64         CamelCertDB *certdb;
65         gchar *path;
66
67         if (initialised)
68                 return 0;
69
70         bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
71         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
72
73         camel_debug_init();
74
75 #ifdef HAVE_NSS
76         if (nss_init) {
77                 gchar *nss_configdir = NULL;
78                 gchar *nss_sql_configdir = NULL;
79                 SECStatus status;
80                 PRUint16 indx;
81
82                 if (nss_initlock == NULL) {
83                         PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
84                         nss_initlock = PR_NewLock();
85                 }
86                 PR_Lock (nss_initlock);
87
88                 if (NSS_IsInitialized ())
89                         goto skip_nss_init;
90
91 #ifndef G_OS_WIN32
92                 nss_configdir = g_strdup (configdir);
93 #else
94                 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
95 #endif
96
97                 /* Create the configdir if it does not exist
98                  * This prevents camel from bailing out on first run */
99                 g_mkdir_with_parents (configdir, 0700);
100
101                 /* XXX Currently we store the new shared NSS database in the
102                  *     same location we kept the original NSS databases in,
103                  *     but at least we have safe shared access between Camel
104                  *     and Evolution's S/MIME.  Once freedesktop.org comes
105                  *     up with a user-wide shared location, we should use
106                  *     that instead. */
107                 nss_sql_configdir = g_strconcat ("sql:", nss_configdir, NULL);
108
109 #if NSS_VMAJOR > 3 || (NSS_VMAJOR == 3 && NSS_VMINOR >= 12)
110                 /* See: https://wiki.mozilla.org/NSS_Shared_DB,
111                  * particularly "Mode 3A".  Note that the target
112                  * directory MUST EXIST. */
113                 status = NSS_InitWithMerge (
114                         nss_sql_configdir,      /* dest dir */
115                         "", "",                 /* new DB name prefixes */
116                         SECMOD_DB,              /* secmod name */
117                         nss_configdir,          /* old DB dir */
118                         "", "",                 /* old DB name prefixes */
119                         nss_configdir,          /* unique ID for old DB */
120                         "Evolution S/MIME",     /* UI name for old DB */
121                         0);                     /* flags */
122
123                 if (status == SECFailure) {
124                         g_free (nss_configdir);
125                         g_free (nss_sql_configdir);
126                         g_warning ("Failed to initialize NSS");
127                         PR_Unlock (nss_initlock);
128                         return -1;
129                 }
130 #else
131                 /* Support old versions of libnss, pre-sqlite support. */
132                 status = NSS_InitReadWrite (nss_configdir);
133                 if (status == SECFailure) {
134                         /* Fall back to using volatile dbs? */
135                         status = NSS_NoDB_Init (nss_configdir);
136                         if (status == SECFailure) {
137                                 g_free (nss_configdir);
138                                 g_free (nss_sql_configdir);
139                                 g_warning ("Failed to initialize NSS");
140                                 PR_Unlock (nss_initlock);
141                                 return -1;
142                         }
143                 }
144 #endif
145
146                 nss_initialized = TRUE;
147 skip_nss_init:
148
149                 NSS_SetDomesticPolicy ();
150
151                 PR_Unlock(nss_initlock);
152
153                 /* we must enable all ciphersuites */
154                 for (indx = 0; indx < SSL_NumImplementedCiphers; indx++) {
155                         if (!SSL_IS_SSL2_CIPHER(SSL_ImplementedCiphers[indx]))
156                                 SSL_CipherPrefSetDefault (SSL_ImplementedCiphers[indx], PR_TRUE);
157                 }
158
159                 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
160                 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
161                 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
162                 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
163
164                 g_free (nss_configdir);
165                 g_free (nss_sql_configdir);
166         }
167 #endif /* HAVE_NSS */
168
169         path = g_strdup_printf ("%s/camel-cert.db", configdir);
170         certdb = camel_certdb_new ();
171         camel_certdb_set_filename (certdb, path);
172         g_free (path);
173
174         /* if we fail to load, who cares? it'll just be a volatile certdb */
175         camel_certdb_load (certdb);
176
177         /* set this certdb as the default db */
178         camel_certdb_set_default (certdb);
179
180         g_object_unref (certdb);
181
182         initialised = TRUE;
183
184         return 0;
185 }
186
187 /**
188  * camel_shutdown:
189  *
190  * Since: 2.24
191  **/
192 void
193 camel_shutdown (void)
194 {
195         CamelCertDB *certdb;
196
197         if (!initialised)
198                 return;
199
200         certdb = camel_certdb_get_default ();
201         if (certdb) {
202                 camel_certdb_save (certdb);
203                 camel_certdb_set_default (NULL);
204         }
205
206         /* These next calls must come last. */
207
208 #if defined (HAVE_NSS)
209         if (nss_initlock != NULL) {
210                 PR_Lock(nss_initlock);
211                 if (nss_initialized)
212                         NSS_Shutdown ();
213                 PR_Unlock(nss_initlock);
214         }
215 #endif /* HAVE_NSS */
216
217         initialised = FALSE;
218 }