On Win32, NSS wants the directory name in system codepage, not UTF-8. No
[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 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., 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-debug.h"
41 #include "camel-i18n.h"
42 #include "camel-provider.h"
43 #include "camel-private.h"
44
45 static int initialised = FALSE;
46
47 static void
48 camel_shutdown (void)
49 {
50         CamelCertDB *certdb;
51         
52         if (!initialised)
53                 return;
54         
55 #if defined (HAVE_NSS) && !defined (G_OS_WIN32)
56         /* For some reason we get into trouble on Win32 if we call these.
57          * But they shouldn't be necessary as the process is exiting anywy?
58          */
59         NSS_Shutdown ();
60         
61         PR_Cleanup ();
62 #endif /* HAVE_NSS */
63         
64         certdb = camel_certdb_get_default ();
65         if (certdb) {
66                 camel_certdb_save (certdb);
67                 camel_object_unref (certdb);
68         }
69         
70         initialised = FALSE;
71 }
72
73 int
74 camel_init (const char *configdir, gboolean nss_init)
75 {
76         CamelCertDB *certdb;
77         char *path;
78         
79         if (initialised)
80                 return 0;
81
82         bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
83         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
84
85         camel_debug_init();
86
87         /* initialise global camel_object_type */
88         camel_object_get_type();
89
90 #ifdef HAVE_NSS
91         if (nss_init) {
92                 char *nss_configdir;
93
94                 PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
95                 
96 #ifndef G_OS_WIN32
97                 nss_configdir = g_strdup (configdir);
98 #else
99                 nss_configdir = g_win32_locale_filename_from_utf8 (configdir);
100 #endif
101
102                 if (NSS_InitReadWrite (nss_configdir) == SECFailure) {
103                         /* fall back on using volatile dbs? */
104                         if (NSS_NoDB_Init (nss_configdir) == SECFailure) {
105                                 g_warning ("Failed to initialize NSS");
106                                 return -1;
107                         }
108                 }
109
110                 NSS_SetDomesticPolicy ();
111                 
112                 SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
113                 SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
114                 SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
115                 SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
116         }
117 #endif /* HAVE_NSS */
118         
119         path = g_strdup_printf ("%s/camel-cert.db", configdir);
120         certdb = camel_certdb_new ();
121         camel_certdb_set_filename (certdb, path);
122         g_free (path);
123         
124         /* if we fail to load, who cares? it'll just be a volatile certdb */
125         camel_certdb_load (certdb);
126         
127         /* set this certdb as the default db */
128         camel_certdb_set_default (certdb);
129         
130         camel_object_unref (certdb);
131         
132         g_atexit (camel_shutdown);
133         
134         initialised = TRUE;
135         
136         return 0;
137 }