Bug #615776 - Drop camel-private.h and offer a public alternative for locks
[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         /* initialise global camel_object_type */
76         camel_object_get_type ();
77
78 #ifdef HAVE_NSS
79         if (nss_init) {
80                 gchar *nss_configdir;
81                 PRUint16 indx;
82
83                 if (nss_initlock == NULL) {
84                         PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
85                         nss_initlock = PR_NewLock();
86                 }
87                 PR_Lock (nss_initlock);
88
89 #ifndef G_OS_WIN32
90                 nss_configdir = g_strdup (configdir);
91 #else
92                 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
93 #endif
94
95                 if (!NSS_IsInitialized()) {
96                         nss_initialized = 1;
97
98                         if (NSS_InitReadWrite (nss_configdir) == SECFailure) {
99                                 /* fall back on using volatile dbs? */
100                                 if (NSS_NoDB_Init (nss_configdir) == SECFailure) {
101                                         g_free (nss_configdir);
102                                         g_warning ("Failed to initialize NSS");
103                                         nss_initialized = 0;
104                                         PR_Unlock(nss_initlock);
105                                         return -1;
106                                 }
107                         }
108                 }
109
110                 NSS_SetDomesticPolicy ();
111
112                 PR_Unlock(nss_initlock);
113
114                 /* we must enable all ciphersuites */
115                 for (indx = 0; indx < SSL_NumImplementedCiphers; indx++) {
116                         if (!SSL_IS_SSL2_CIPHER(SSL_ImplementedCiphers[indx]))
117                                 SSL_CipherPrefSetDefault (SSL_ImplementedCiphers[indx], PR_TRUE);
118                 }
119
120                 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
121                 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
122                 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
123                 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
124
125                 g_free (nss_configdir);
126         }
127 #endif /* HAVE_NSS */
128
129         path = g_strdup_printf ("%s/camel-cert.db", configdir);
130         certdb = camel_certdb_new ();
131         camel_certdb_set_filename (certdb, path);
132         g_free (path);
133
134         /* if we fail to load, who cares? it'll just be a volatile certdb */
135         camel_certdb_load (certdb);
136
137         /* set this certdb as the default db */
138         camel_certdb_set_default (certdb);
139
140         camel_object_unref (certdb);
141
142         initialised = TRUE;
143
144         return 0;
145 }
146
147 /**
148  * camel_shutdown:
149  *
150  * Since: 2.24
151  **/
152 void
153 camel_shutdown (void)
154 {
155         CamelCertDB *certdb;
156
157         if (!initialised)
158                 return;
159
160         certdb = camel_certdb_get_default ();
161         if (certdb) {
162                 camel_certdb_save (certdb);
163                 camel_certdb_set_default (NULL);
164         }
165
166         /* These next calls must come last. */
167
168 #if defined (HAVE_NSS)
169         if (nss_initlock != NULL) {
170                 PR_Lock(nss_initlock);
171                 if (nss_initialized)
172                         NSS_Shutdown ();
173                 PR_Unlock(nss_initlock);
174         }
175 #endif /* HAVE_NSS */
176
177         initialised = FALSE;
178 }