New source file implementing a very basic certificate database. This is
[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 /* 
4  *
5  * Author : 
6  *  Bertrand Guiheneuf <bertrand@helixcode.com>
7  *
8  * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
9  *
10  * This program is free software; you can redistribute it and/or 
11  * modify it under the terms of version 2 of the GNU General Public 
12  * License as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
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 #ifdef HAVE_NSS
45 static void
46 camel_shutdown (void)
47 {
48         CamelCertDB *certdb;
49         
50         NSS_Shutdown ();
51         
52         PR_Cleanup ();
53         
54         certdb = camel_certdb_get_default ();
55         if (certdb) {
56                 camel_certdb_save (certdb);
57                 camel_object_unref (certdb);
58         }
59 }
60 #endif /* HAVE_NSS */
61
62 gint
63 camel_init (const char *configdir, gboolean nss_init)
64 {
65         CamelCertDB *certdb;
66         char *path;
67         
68 #ifdef ENABLE_THREADS
69 #ifdef G_THREADS_ENABLED        
70         /*g_thread_init (NULL);*/
71 #else /* G_THREADS_ENABLED */
72         g_warning ("Threads are not supported by your version of glib");
73 #endif /* G_THREADS_ENABLED */
74 #endif /* ENABLE_THREADS */
75         
76         if (getenv ("CAMEL_VERBOSE_DEBUG"))
77                 camel_verbose_debug = TRUE;
78
79         /* initialise global camel_object_type */
80         camel_object_get_type();
81
82         camel_mime_utils_init();
83         
84 #ifdef HAVE_NSS
85         if (nss_init) {
86                 PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 10);
87                 
88                 if (NSS_InitReadWrite (configdir) == SECFailure) {
89                         /* fall back on using volatile dbs? */
90                         if (NSS_NoDB_Init (configdir) == SECFailure) {
91                                 g_warning ("Failed to initialize NSS");
92                                 return -1;
93                         }
94                 }
95                 
96                 NSS_SetDomesticPolicy ();
97                 
98                 g_atexit (camel_shutdown);
99         }
100         
101         SSL_OptionSetDefault (SSL_ENABLE_SSL2, PR_TRUE);
102         SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
103         SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
104         SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
105 #endif /* HAVE_NSS */
106         
107         path = g_strdup_printf ("%s/camel-cert.db", configdir);
108         certdb = camel_certdb_new ();
109         camel_certdb_set_filename (certdb, path);
110         g_free (path);
111         
112         /* if we fail to load, who cares? it'll just be a volatile certdb */
113         camel_certdb_load (certdb);
114         
115         /* set this certdb as the default db */
116         camel_certdb_set_default (certdb);
117         
118         camel_object_unref (certdb);
119         
120         return 0;
121 }