Protect against multiple camel_init() calls. Remember if we've already
[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 1999-2003 Ximian, Inc. (www.ximian.com)
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU 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 General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
21  *
22  */
23
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <signal.h>
30
31 #ifdef HAVE_NSS
32 #include <nspr.h>
33 #include <prthread.h>
34 #include "nss.h"      /* Don't use <> here or it will include the system nss.h instead */
35 #include <ssl.h>
36 #endif /* HAVE_NSS */
37
38 #include "camel.h"
39 #include "camel-certdb.h"
40 #include "camel-mime-utils.h"
41
42 gboolean camel_verbose_debug = FALSE;
43
44 static int initialised = FALSE;
45
46 static void
47 camel_shutdown (void)
48 {
49         CamelCertDB *certdb;
50         
51         if (!initialised)
52                 return;
53         
54 #ifdef HAVE_NSS
55         NSS_Shutdown ();
56         
57         PR_Cleanup ();
58 #endif /* HAVE_NSS */
59         
60         certdb = camel_certdb_get_default ();
61         if (certdb) {
62                 camel_certdb_save (certdb);
63                 camel_object_unref (certdb);
64         }
65         
66         initialised = FALSE;
67 }
68
69 int
70 camel_init (const char *configdir, gboolean nss_init)
71 {
72         CamelCertDB *certdb;
73         char *path;
74         void camel_operation_init(void);
75         
76         if (initialised)
77                 return;
78         
79         if (getenv ("CAMEL_VERBOSE_DEBUG"))
80                 camel_verbose_debug = TRUE;
81         
82         /* initialise global camel_object_type */
83         camel_object_get_type ();
84         
85         camel_mime_utils_init ();
86         camel_operation_init();
87
88 #ifdef HAVE_NSS
89         if (nss_init) {
90                 PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
91                 
92                 if (NSS_InitReadWrite (configdir) == SECFailure) {
93                         /* fall back on using volatile dbs? */
94                         if (NSS_NoDB_Init (configdir) == SECFailure) {
95                                 g_warning ("Failed to initialize NSS");
96                                 return -1;
97                         }
98                 }
99                 
100                 NSS_SetDomesticPolicy ();
101                 
102                 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
103                 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
104                 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
105                 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
106         }
107 #endif /* HAVE_NSS */
108         
109         path = g_strdup_printf ("%s/camel-cert.db", configdir);
110         certdb = camel_certdb_new ();
111         camel_certdb_set_filename (certdb, path);
112         g_free (path);
113         
114         /* if we fail to load, who cares? it'll just be a volatile certdb */
115         camel_certdb_load (certdb);
116         
117         /* set this certdb as the default db */
118         camel_certdb_set_default (certdb);
119         
120         camel_object_unref (certdb);
121         
122         g_atexit (camel_shutdown);
123         
124         initialised = TRUE;
125         
126         return 0;
127 }