/*
* Authors: Jeffrey Stedfast <fejj@ximian.com>
*
- * Copyright 2002-2003 Ximian, Inc. (www.ximian.com)
+ * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
* This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
+ * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
+ * You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
-
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
-#include <glib.h>
+#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <pthread.h>
#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include "camel-certdb.h"
-#include "camel-private.h"
-
-#include <camel/camel-file-utils.h>
-
-#include <e-util/e-memory.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <glib/gstdio.h>
-#define CAMEL_CERTDB_GET_CLASS(db) ((CamelCertDBClass *) CAMEL_OBJECT_GET_CLASS (db))
+#include "camel-certdb.h"
+#include "camel-file-utils.h"
+#include "camel-win32.h"
#define CAMEL_CERTDB_VERSION 0x100
-static void camel_certdb_class_init (CamelCertDBClass *klass);
-static void camel_certdb_init (CamelCertDB *certdb);
-static void camel_certdb_finalize (CamelObject *obj);
+#define CAMEL_CERTDB_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE \
+ ((obj), CAMEL_TYPE_CERTDB, CamelCertDBPrivate))
+
+struct _CamelCertDBPrivate {
+ GMutex *db_lock; /* for the db hashtable/array */
+ GMutex *io_lock; /* load/save lock, for access to saved_count, etc */
+ GMutex *alloc_lock; /* for setting up and using allocators */
+ GMutex *ref_lock; /* for reffing/unreffing certs */
+};
-static int certdb_header_load (CamelCertDB *certdb, FILE *istream);
-static int certdb_header_save (CamelCertDB *certdb, FILE *ostream);
+static gint certdb_header_load (CamelCertDB *certdb, FILE *istream);
+static gint certdb_header_save (CamelCertDB *certdb, FILE *ostream);
static CamelCert *certdb_cert_load (CamelCertDB *certdb, FILE *istream);
-static int certdb_cert_save (CamelCertDB *certdb, CamelCert *cert, FILE *ostream);
+static gint certdb_cert_save (CamelCertDB *certdb, CamelCert *cert, FILE *ostream);
static CamelCert *certdb_cert_new (CamelCertDB *certdb);
static void certdb_cert_free (CamelCertDB *certdb, CamelCert *cert);
-static const char *cert_get_string (CamelCertDB *certdb, CamelCert *cert, int string);
-static void cert_set_string (CamelCertDB *certdb, CamelCert *cert, int string, const char *value);
+static const gchar *cert_get_string (CamelCertDB *certdb, CamelCert *cert, gint string);
+static void cert_set_string (CamelCertDB *certdb, CamelCert *cert, gint string, const gchar *value);
+G_DEFINE_TYPE (CamelCertDB, camel_certdb, CAMEL_TYPE_OBJECT)
-static CamelObjectClass *parent_class = NULL;
+static gboolean
+certdb_str_equal_casecmp (gconstpointer str1,
+ gconstpointer str2)
+{
+ if (!str1 || !str2)
+ return str1 == str2;
+ return g_ascii_strcasecmp (str1, str2) == 0;
+}
-CamelType
-camel_certdb_get_type (void)
+static void
+certdb_finalize (GObject *object)
{
- static CamelType type = CAMEL_INVALID_TYPE;
-
- if (type == CAMEL_INVALID_TYPE) {
- type = camel_type_register (camel_object_get_type (),
- "CamelCertDB",
- sizeof (CamelCertDB),
- sizeof (CamelCertDBClass),
- (CamelObjectClassInitFunc) camel_certdb_class_init,
- NULL,
- (CamelObjectInitFunc) camel_certdb_init,
- (CamelObjectFinalizeFunc) camel_certdb_finalize);
- }
-
- return type;
-}
+ CamelCertDB *certdb = CAMEL_CERTDB (object);
+ CamelCertDBPrivate *priv;
+
+ priv = CAMEL_CERTDB_GET_PRIVATE (object);
+ if (certdb->flags & CAMEL_CERTDB_DIRTY)
+ camel_certdb_save (certdb);
+
+ camel_certdb_clear (certdb);
+ g_ptr_array_free (certdb->certs, TRUE);
+ g_hash_table_destroy (certdb->cert_hash);
+
+ g_free (certdb->filename);
+
+ if (certdb->cert_chunks)
+ camel_memchunk_destroy (certdb->cert_chunks);
+
+ g_mutex_free (priv->db_lock);
+ g_mutex_free (priv->io_lock);
+ g_mutex_free (priv->alloc_lock);
+ g_mutex_free (priv->ref_lock);
+
+ /* Chain up to parent's finalize() method. */
+ G_OBJECT_CLASS (camel_certdb_parent_class)->finalize (object);
+}
static void
-camel_certdb_class_init (CamelCertDBClass *klass)
+camel_certdb_class_init (CamelCertDBClass *class)
{
- parent_class = camel_type_get_global_classfuncs (camel_object_get_type ());
-
- klass->header_load = certdb_header_load;
- klass->header_save = certdb_header_save;
-
- klass->cert_new = certdb_cert_new;
- klass->cert_load = certdb_cert_load;
- klass->cert_save = certdb_cert_save;
- klass->cert_free = certdb_cert_free;
- klass->cert_get_string = cert_get_string;
- klass->cert_set_string = cert_set_string;
+ GObjectClass *object_class;
+
+ g_type_class_add_private (class, sizeof (CamelCertDBPrivate));
+
+ object_class = G_OBJECT_CLASS (class);
+ object_class->finalize = certdb_finalize;
+
+ class->header_load = certdb_header_load;
+ class->header_save = certdb_header_save;
+
+ class->cert_new = certdb_cert_new;
+ class->cert_load = certdb_cert_load;
+ class->cert_save = certdb_cert_save;
+ class->cert_free = certdb_cert_free;
+ class->cert_get_string = cert_get_string;
+ class->cert_set_string = cert_set_string;
}
static void
camel_certdb_init (CamelCertDB *certdb)
{
- certdb->priv = g_malloc (sizeof (struct _CamelCertDBPrivate));
-
+ certdb->priv = CAMEL_CERTDB_GET_PRIVATE (certdb);
+
certdb->filename = NULL;
certdb->version = CAMEL_CERTDB_VERSION;
certdb->saved_certs = 0;
-
+
certdb->cert_size = sizeof (CamelCert);
-
+
certdb->cert_chunks = NULL;
-
+
certdb->certs = g_ptr_array_new ();
- certdb->cert_hash = g_hash_table_new (g_str_hash, g_str_equal);
-
+ certdb->cert_hash = g_hash_table_new (g_str_hash, certdb_str_equal_casecmp);
+
certdb->priv->db_lock = g_mutex_new ();
certdb->priv->io_lock = g_mutex_new ();
certdb->priv->alloc_lock = g_mutex_new ();
certdb->priv->ref_lock = g_mutex_new ();
}
-static void
-camel_certdb_finalize (CamelObject *obj)
-{
- CamelCertDB *certdb = (CamelCertDB *) obj;
- struct _CamelCertDBPrivate *p;
-
- p = certdb->priv;
-
- if (certdb->flags & CAMEL_CERTDB_DIRTY)
- camel_certdb_save (certdb);
-
- camel_certdb_clear (certdb);
- g_ptr_array_free (certdb->certs, TRUE);
- g_hash_table_destroy (certdb->cert_hash);
-
- g_free (certdb->filename);
-
- if (certdb->cert_chunks)
- e_memchunk_destroy (certdb->cert_chunks);
-
- g_mutex_free (p->db_lock);
- g_mutex_free (p->io_lock);
- g_mutex_free (p->alloc_lock);
- g_mutex_free (p->ref_lock);
-
- g_free (p);
-}
-
-
CamelCertDB *
camel_certdb_new (void)
{
- return (CamelCertDB *) camel_object_new (camel_certdb_get_type ());
+ return g_object_new (CAMEL_TYPE_CERTDB, NULL);
}
-
static CamelCertDB *default_certdb = NULL;
-static pthread_mutex_t default_certdb_lock = PTHREAD_MUTEX_INITIALIZER;
-
+static GStaticMutex default_certdb_lock = G_STATIC_MUTEX_INIT;
void
camel_certdb_set_default (CamelCertDB *certdb)
{
- pthread_mutex_lock (&default_certdb_lock);
-
+ g_static_mutex_lock (&default_certdb_lock);
+
if (default_certdb)
- camel_object_unref (default_certdb);
-
+ g_object_unref (default_certdb);
+
if (certdb)
- camel_object_ref (certdb);
-
+ g_object_ref (certdb);
+
default_certdb = certdb;
-
- pthread_mutex_unlock (&default_certdb_lock);
-}
+ g_static_mutex_unlock (&default_certdb_lock);
+}
CamelCertDB *
camel_certdb_get_default (void)
{
CamelCertDB *certdb;
-
- pthread_mutex_lock (&default_certdb_lock);
-
+
+ g_static_mutex_lock (&default_certdb_lock);
+
if (default_certdb)
- camel_object_ref (default_certdb);
-
+ g_object_ref (default_certdb);
+
certdb = default_certdb;
-
- pthread_mutex_unlock (&default_certdb_lock);
-
+
+ g_static_mutex_unlock (&default_certdb_lock);
+
return certdb;
}
-
void
-camel_certdb_set_filename (CamelCertDB *certdb, const char *filename)
+camel_certdb_set_filename (CamelCertDB *certdb,
+ const gchar *filename)
{
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
g_return_if_fail (filename != NULL);
-
- CAMEL_CERTDB_LOCK (certdb, db_lock);
-
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_DB_LOCK);
+
g_free (certdb->filename);
certdb->filename = g_strdup (filename);
-
- CAMEL_CERTDB_UNLOCK (certdb, db_lock);
-}
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_DB_LOCK);
+}
-static int
-certdb_header_load (CamelCertDB *certdb, FILE *istream)
+static gint
+certdb_header_load (CamelCertDB *certdb,
+ FILE *istream)
{
if (camel_file_util_decode_uint32 (istream, &certdb->version) == -1)
return -1;
if (camel_file_util_decode_uint32 (istream, &certdb->saved_certs) == -1)
return -1;
-
+
return 0;
}
static CamelCert *
-certdb_cert_load (CamelCertDB *certdb, FILE *istream)
+certdb_cert_load (CamelCertDB *certdb,
+ FILE *istream)
{
CamelCert *cert;
-
+
cert = camel_certdb_cert_new (certdb);
if (camel_file_util_decode_string (istream, &cert->issuer) == -1)
goto error;
if (camel_file_util_decode_uint32 (istream, &cert->trust) == -1)
goto error;
-
+
+ /* unset temporary trusts on load */
+ if (cert->trust == CAMEL_CERT_TRUST_TEMPORARY)
+ cert->trust = CAMEL_CERT_TRUST_UNKNOWN;
+
return cert;
-
+
error:
-
+
camel_certdb_cert_unref (certdb, cert);
-
+
return NULL;
}
-int
+gint
camel_certdb_load (CamelCertDB *certdb)
{
+ CamelCertDBClass *class;
CamelCert *cert;
FILE *in;
- int i;
-
+ gint i;
+
g_return_val_if_fail (CAMEL_IS_CERTDB (certdb), -1);
g_return_val_if_fail (certdb->filename, -1);
- in = fopen (certdb->filename, "r");
+ in = g_fopen (certdb->filename, "rb");
if (in == NULL)
return -1;
-
- CAMEL_CERTDB_LOCK (certdb, io_lock);
- if (CAMEL_CERTDB_GET_CLASS (certdb)->header_load (certdb, in) == -1)
+
+ class = CAMEL_CERTDB_GET_CLASS (certdb);
+ if (!class->header_load || !class->cert_load) {
+ fclose (in);
+ in = NULL;
+ }
+ g_return_val_if_fail (class->header_load != NULL, -1);
+ g_return_val_if_fail (class->cert_load != NULL, -1);
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_IO_LOCK);
+ if (class->header_load (certdb, in) == -1)
goto error;
-
+
for (i = 0; i < certdb->saved_certs; i++) {
- cert = CAMEL_CERTDB_GET_CLASS (certdb)->cert_load (certdb, in);
-
+ cert = class->cert_load (certdb, in);
+
if (cert == NULL)
goto error;
-
- camel_certdb_add (certdb, cert);
+
+ /* NOTE: If we are upgrading from an evolution-data-server version
+ * prior to the change to look up certs by hostname (bug 606181),
+ * this "put" will result in duplicate entries for the same
+ * hostname being dropped. The change will become permanent on
+ * disk the next time the certdb is dirtied for some reason and
+ * has to be saved. */
+ camel_certdb_put (certdb, cert);
}
-
- CAMEL_CERTDB_UNLOCK (certdb, io_lock);
-
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_IO_LOCK);
+
if (fclose (in) != 0)
return -1;
-
+
certdb->flags &= ~CAMEL_CERTDB_DIRTY;
-
+
return 0;
-
+
error:
-
- g_warning ("Cannot load certificate database: %s", strerror (ferror (in)));
-
- CAMEL_CERTDB_UNLOCK (certdb, io_lock);
-
+
+ g_warning ("Cannot load certificate database: %s", g_strerror (ferror (in)));
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_IO_LOCK);
+
fclose (in);
-
+
return -1;
}
-static int
-certdb_header_save (CamelCertDB *certdb, FILE *ostream)
+static gint
+certdb_header_save (CamelCertDB *certdb,
+ FILE *ostream)
{
if (camel_file_util_encode_uint32 (ostream, certdb->version) == -1)
return -1;
if (camel_file_util_encode_uint32 (ostream, certdb->saved_certs) == -1)
return -1;
-
+
return 0;
}
-static int
-certdb_cert_save (CamelCertDB *certdb, CamelCert *cert, FILE *ostream)
+static gint
+certdb_cert_save (CamelCertDB *certdb,
+ CamelCert *cert,
+ FILE *ostream)
{
if (camel_file_util_encode_string (ostream, cert->issuer) == -1)
return -1;
return -1;
if (camel_file_util_encode_uint32 (ostream, cert->trust) == -1)
return -1;
-
+
return 0;
}
-int
+gint
camel_certdb_save (CamelCertDB *certdb)
{
+ CamelCertDBClass *class;
CamelCert *cert;
- char *filename;
- int fd, i;
+ gchar *filename;
+ gint fd, i;
FILE *out;
-
+
g_return_val_if_fail (CAMEL_IS_CERTDB (certdb), -1);
g_return_val_if_fail (certdb->filename, -1);
-
+
+ /* no change, nothing new to save, simply return success */
+ if ((certdb->flags & CAMEL_CERTDB_DIRTY) == 0)
+ return 0;
+
filename = alloca (strlen (certdb->filename) + 4);
sprintf (filename, "%s~", certdb->filename);
-
- fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+
+ fd = g_open (filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
if (fd == -1)
return -1;
-
- out = fdopen (fd, "w");
+
+ out = fdopen (fd, "wb");
if (out == NULL) {
i = errno;
close (fd);
- unlink (filename);
+ g_unlink (filename);
errno = i;
return -1;
}
-
- CAMEL_CERTDB_LOCK (certdb, io_lock);
-
+
+ class = CAMEL_CERTDB_GET_CLASS (certdb);
+ if (!class->header_save || !class->cert_save) {
+ fclose (out);
+ out = NULL;
+ }
+ g_return_val_if_fail (class->header_save != NULL, -1);
+ g_return_val_if_fail (class->cert_save != NULL, -1);
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_IO_LOCK);
+
certdb->saved_certs = certdb->certs->len;
- if (CAMEL_CERTDB_GET_CLASS (certdb)->header_save (certdb, out) == -1)
+ if (class->header_save (certdb, out) == -1)
goto error;
-
+
for (i = 0; i < certdb->saved_certs; i++) {
cert = (CamelCert *) certdb->certs->pdata[i];
-
- if (CAMEL_CERTDB_GET_CLASS (certdb)->cert_save (certdb, cert, out) == -1)
+
+ if (class->cert_save (certdb, cert, out) == -1)
goto error;
}
-
- CAMEL_CERTDB_UNLOCK (certdb, io_lock);
-
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_IO_LOCK);
+
if (fflush (out) != 0 || fsync (fileno (out)) == -1) {
i = errno;
fclose (out);
- unlink (filename);
+ g_unlink (filename);
errno = i;
return -1;
}
-
+
if (fclose (out) != 0) {
i = errno;
- unlink (filename);
+ g_unlink (filename);
errno = i;
return -1;
}
-
- if (rename (filename, certdb->filename) == -1) {
+
+ if (g_rename (filename, certdb->filename) == -1) {
i = errno;
- unlink (filename);
+ g_unlink (filename);
errno = i;
return -1;
}
-
+
certdb->flags &= ~CAMEL_CERTDB_DIRTY;
-
+
return 0;
-
+
error:
-
- g_warning ("Cannot save certificate database: %s", strerror (ferror (out)));
-
- CAMEL_CERTDB_UNLOCK (certdb, io_lock);
-
+
+ g_warning ("Cannot save certificate database: %s", g_strerror (ferror (out)));
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_IO_LOCK);
+
i = errno;
fclose (out);
- unlink (filename);
+ g_unlink (filename);
errno = i;
-
+
return -1;
}
camel_certdb_touch (CamelCertDB *certdb)
{
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
-
+
certdb->flags |= CAMEL_CERTDB_DIRTY;
}
CamelCert *
-camel_certdb_get_cert (CamelCertDB *certdb, const char *fingerprint)
+camel_certdb_get_host (CamelCertDB *certdb,
+ const gchar *hostname)
{
CamelCert *cert;
-
+
g_return_val_if_fail (CAMEL_IS_CERTDB (certdb), NULL);
-
- CAMEL_CERTDB_LOCK (certdb, db_lock);
-
- cert = g_hash_table_lookup (certdb->cert_hash, fingerprint);
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_DB_LOCK);
+
+ cert = g_hash_table_lookup (certdb->cert_hash, hostname);
if (cert)
camel_certdb_cert_ref (certdb, cert);
-
- CAMEL_CERTDB_UNLOCK (certdb, db_lock);
-
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_DB_LOCK);
+
return cert;
}
void
-camel_certdb_add (CamelCertDB *certdb, CamelCert *cert)
+camel_certdb_put (CamelCertDB *certdb,
+ CamelCert *cert)
{
+ CamelCert *old_cert;
+
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
-
- CAMEL_CERTDB_LOCK (certdb, db_lock);
-
- if (g_hash_table_lookup (certdb->cert_hash, cert->fingerprint)) {
- CAMEL_CERTDB_UNLOCK (certdb, db_lock);
- return;
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_DB_LOCK);
+
+ /* Replace an existing entry with the same hostname. */
+ old_cert = g_hash_table_lookup (certdb->cert_hash, cert->hostname);
+ if (old_cert) {
+ g_hash_table_remove (certdb->cert_hash, cert->hostname);
+ g_ptr_array_remove (certdb->certs, old_cert);
+ camel_certdb_cert_unref (certdb, old_cert);
}
-
+
camel_certdb_cert_ref (certdb, cert);
g_ptr_array_add (certdb->certs, cert);
- g_hash_table_insert (certdb->cert_hash, cert->fingerprint, cert);
-
+ g_hash_table_insert (certdb->cert_hash, cert->hostname, cert);
+
certdb->flags |= CAMEL_CERTDB_DIRTY;
-
- CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_DB_LOCK);
}
void
-camel_certdb_remove (CamelCertDB *certdb, CamelCert *cert)
+camel_certdb_remove_host (CamelCertDB *certdb,
+ const gchar *hostname)
{
+ CamelCert *cert;
+
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
-
- CAMEL_CERTDB_LOCK (certdb, db_lock);
-
- if (g_hash_table_lookup (certdb->cert_hash, cert->fingerprint)) {
- g_hash_table_remove (certdb->cert_hash, cert->fingerprint);
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_DB_LOCK);
+
+ cert = g_hash_table_lookup (certdb->cert_hash, hostname);
+ if (cert) {
+ g_hash_table_remove (certdb->cert_hash, hostname);
g_ptr_array_remove (certdb->certs, cert);
camel_certdb_cert_unref (certdb, cert);
-
+
certdb->flags |= CAMEL_CERTDB_DIRTY;
}
-
- CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_DB_LOCK);
}
static CamelCert *
certdb_cert_new (CamelCertDB *certdb)
{
CamelCert *cert;
-
+
if (certdb->cert_chunks)
- cert = e_memchunk_alloc0 (certdb->cert_chunks);
+ cert = camel_memchunk_alloc0 (certdb->cert_chunks);
else
cert = g_malloc0 (certdb->cert_size);
-
+
cert->refcount = 1;
-
+
return cert;
}
CamelCert *
camel_certdb_cert_new (CamelCertDB *certdb)
{
+ CamelCertDBClass *class;
CamelCert *cert;
-
+
g_return_val_if_fail (CAMEL_IS_CERTDB (certdb), NULL);
-
- CAMEL_CERTDB_LOCK (certdb, alloc_lock);
-
- cert = CAMEL_CERTDB_GET_CLASS (certdb)->cert_new (certdb);
-
- CAMEL_CERTDB_UNLOCK (certdb, alloc_lock);
-
+
+ class = CAMEL_CERTDB_GET_CLASS (certdb);
+ g_return_val_if_fail (class->cert_new != NULL, NULL);
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_ALLOC_LOCK);
+
+ cert = class->cert_new (certdb);
+
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_ALLOC_LOCK);
+
return cert;
}
void
-camel_certdb_cert_ref (CamelCertDB *certdb, CamelCert *cert)
+camel_certdb_cert_ref (CamelCertDB *certdb,
+ CamelCert *cert)
{
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
g_return_if_fail (cert != NULL);
-
- CAMEL_CERTDB_LOCK (certdb, ref_lock);
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_REF_LOCK);
cert->refcount++;
- CAMEL_CERTDB_UNLOCK (certdb, ref_lock);
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_REF_LOCK);
}
static void
-certdb_cert_free (CamelCertDB *certdb, CamelCert *cert)
+certdb_cert_free (CamelCertDB *certdb,
+ CamelCert *cert)
{
g_free (cert->issuer);
g_free (cert->subject);
g_free (cert->hostname);
g_free (cert->fingerprint);
if (cert->rawcert)
- g_byte_array_free(cert->rawcert, TRUE);
+ g_byte_array_free (cert->rawcert, TRUE);
}
void
-camel_certdb_cert_unref (CamelCertDB *certdb, CamelCert *cert)
+camel_certdb_cert_unref (CamelCertDB *certdb,
+ CamelCert *cert)
{
+ CamelCertDBClass *class;
+
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
g_return_if_fail (cert != NULL);
-
- CAMEL_CERTDB_LOCK (certdb, ref_lock);
-
+
+ class = CAMEL_CERTDB_GET_CLASS (certdb);
+ g_return_if_fail (class->cert_free != NULL);
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_REF_LOCK);
+
if (cert->refcount <= 1) {
- CAMEL_CERTDB_GET_CLASS (certdb)->cert_free (certdb, cert);
+ class->cert_free (certdb, cert);
if (certdb->cert_chunks)
- e_memchunk_free (certdb->cert_chunks, cert);
+ camel_memchunk_free (certdb->cert_chunks, cert);
else
g_free (cert);
} else {
cert->refcount--;
}
-
- CAMEL_CERTDB_UNLOCK (certdb, ref_lock);
-}
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_REF_LOCK);
+}
static gboolean
-cert_remove (gpointer key, gpointer value, gpointer user_data)
+cert_remove (gpointer key,
+ gpointer value,
+ gpointer user_data)
{
return TRUE;
}
camel_certdb_clear (CamelCertDB *certdb)
{
CamelCert *cert;
- int i;
-
+ gint i;
+
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
-
- CAMEL_CERTDB_LOCK (certdb, db_lock);
-
+
+ camel_certdb_lock (certdb, CAMEL_CERTDB_DB_LOCK);
+
g_hash_table_foreach_remove (certdb->cert_hash, cert_remove, NULL);
for (i = 0; i < certdb->certs->len; i++) {
cert = (CamelCert *) certdb->certs->pdata[i];
camel_certdb_cert_unref (certdb, cert);
}
-
+
certdb->saved_certs = 0;
g_ptr_array_set_size (certdb->certs, 0);
certdb->flags |= CAMEL_CERTDB_DIRTY;
-
- CAMEL_CERTDB_UNLOCK (certdb, db_lock);
-}
+ camel_certdb_unlock (certdb, CAMEL_CERTDB_DB_LOCK);
+}
-static const char *
-cert_get_string (CamelCertDB *certdb, CamelCert *cert, int string)
+static const gchar *
+cert_get_string (CamelCertDB *certdb,
+ CamelCert *cert,
+ gint string)
{
switch (string) {
case CAMEL_CERT_STRING_ISSUER:
}
}
-
-const char *
-camel_cert_get_string (CamelCertDB *certdb, CamelCert *cert, int string)
+const gchar *
+camel_cert_get_string (CamelCertDB *certdb,
+ CamelCert *cert,
+ gint string)
{
+ CamelCertDBClass *class;
+
g_return_val_if_fail (CAMEL_IS_CERTDB (certdb), NULL);
g_return_val_if_fail (cert != NULL, NULL);
-
+
+ class = CAMEL_CERTDB_GET_CLASS (certdb);
+ g_return_val_if_fail (class->cert_get_string != NULL, NULL);
+
/* FIXME: do locking? */
-
- return CAMEL_CERTDB_GET_CLASS (certdb)->cert_get_string (certdb, cert, string);
+
+ return class->cert_get_string (certdb, cert, string);
}
static void
-cert_set_string (CamelCertDB *certdb, CamelCert *cert, int string, const char *value)
+cert_set_string (CamelCertDB *certdb,
+ CamelCert *cert,
+ gint string,
+ const gchar *value)
{
switch (string) {
case CAMEL_CERT_STRING_ISSUER:
}
}
-
void
-camel_cert_set_string (CamelCertDB *certdb, CamelCert *cert, int string, const char *value)
+camel_cert_set_string (CamelCertDB *certdb,
+ CamelCert *cert,
+ gint string,
+ const gchar *value)
{
+ CamelCertDBClass *class;
+
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
g_return_if_fail (cert != NULL);
-
+
+ class = CAMEL_CERTDB_GET_CLASS (certdb);
+ g_return_if_fail (class->cert_set_string != NULL);
+
/* FIXME: do locking? */
-
- CAMEL_CERTDB_GET_CLASS (certdb)->cert_set_string (certdb, cert, string, value);
-}
+ class->cert_set_string (certdb, cert, string, value);
+}
CamelCertTrust
-camel_cert_get_trust (CamelCertDB *certdb, CamelCert *cert)
+camel_cert_get_trust (CamelCertDB *certdb,
+ CamelCert *cert)
{
g_return_val_if_fail (CAMEL_IS_CERTDB (certdb), CAMEL_CERT_TRUST_UNKNOWN);
g_return_val_if_fail (cert != NULL, CAMEL_CERT_TRUST_UNKNOWN);
-
+
return cert->trust;
}
-
void
-camel_cert_set_trust (CamelCertDB *certdb, CamelCert *cert, CamelCertTrust trust)
+camel_cert_set_trust (CamelCertDB *certdb,
+ CamelCert *cert,
+ CamelCertTrust trust)
{
g_return_if_fail (CAMEL_IS_CERTDB (certdb));
g_return_if_fail (cert != NULL);
-
+
cert->trust = trust;
}
+
+/**
+ * camel_certdb_lock:
+ * @certdb: a #CamelCertDB
+ * @lock: lock type to lock
+ *
+ * Locks @certdb's @lock. Unlock it with camel_certdb_unlock().
+ *
+ * Since: 2.32
+ **/
+void
+camel_certdb_lock (CamelCertDB *certdb,
+ CamelCertDBLock lock)
+{
+ g_return_if_fail (CAMEL_IS_CERTDB (certdb));
+
+ switch (lock) {
+ case CAMEL_CERTDB_DB_LOCK:
+ g_mutex_lock (certdb->priv->db_lock);
+ break;
+ case CAMEL_CERTDB_IO_LOCK:
+ g_mutex_lock (certdb->priv->io_lock);
+ break;
+ case CAMEL_CERTDB_ALLOC_LOCK:
+ g_mutex_lock (certdb->priv->alloc_lock);
+ break;
+ case CAMEL_CERTDB_REF_LOCK:
+ g_mutex_lock (certdb->priv->ref_lock);
+ break;
+ default:
+ g_return_if_reached ();
+ }
+}
+
+/**
+ * camel_certdb_unlock:
+ * @certdb: a #CamelCertDB
+ * @lock: lock type to unlock
+ *
+ * Unlocks @certdb's @lock, previously locked with camel_certdb_lock().
+ *
+ * Since: 2.32
+ **/
+void
+camel_certdb_unlock (CamelCertDB *certdb,
+ CamelCertDBLock lock)
+{
+ g_return_if_fail (CAMEL_IS_CERTDB (certdb));
+
+ switch (lock) {
+ case CAMEL_CERTDB_DB_LOCK:
+ g_mutex_unlock (certdb->priv->db_lock);
+ break;
+ case CAMEL_CERTDB_IO_LOCK:
+ g_mutex_unlock (certdb->priv->io_lock);
+ break;
+ case CAMEL_CERTDB_ALLOC_LOCK:
+ g_mutex_unlock (certdb->priv->alloc_lock);
+ break;
+ case CAMEL_CERTDB_REF_LOCK:
+ g_mutex_unlock (certdb->priv->ref_lock);
+ break;
+ default:
+ g_return_if_reached ();
+ }
+}