123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Claws Mail team
- *
- * 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
- * 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.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- #include <glib.h>
- #include <glib/gi18n.h>
- #include "privacy.h"
- #include "procmime.h"
- static GSList *systems = NULL;
- static gchar *privacy_last_error = NULL;
- void privacy_set_error(const gchar *format, ...)
- {
- va_list args;
- gchar buf[BUFSIZ];
- va_start(args, format);
- g_vsnprintf(buf, BUFSIZ, format, args);
- va_end(args);
- g_free(privacy_last_error);
- privacy_last_error = g_strdup(buf);
- }
- static gchar tmp_privacy_error[BUFSIZ];
- void privacy_reset_error(void)
- {
- g_free(privacy_last_error);
- privacy_last_error = NULL;
- }
- gboolean privacy_peek_error(void)
- {
- return (privacy_last_error != NULL);
- }
- const gchar *privacy_get_error (void)
- {
- if (privacy_last_error) {
- strncpy2(tmp_privacy_error, privacy_last_error, BUFSIZ-1);
- privacy_reset_error();
- return tmp_privacy_error;
- } else {
- return _("Unknown error");
- }
- }
- PrivacySystem *privacy_data_get_system(PrivacyData *data)
- {
- /* Make sure the cached system is still registered */
- if (data->system && g_slist_find(systems, data->system))
- return data->system;
- else
- return NULL;
- }
- /**
- * Register a new Privacy System
- *
- * \param system The Privacy System that should be registered
- */
- void privacy_register_system(PrivacySystem *system)
- {
- systems = g_slist_append(systems, system);
- }
- /**
- * Unregister a new Privacy System. The system must not be in
- * use anymore when it is unregistered.
- *
- * \param system The Privacy System that should be unregistered
- */
- void privacy_unregister_system(PrivacySystem *system)
- {
- systems = g_slist_remove(systems, system);
- }
- /**
- * Free a PrivacyData of a PrivacySystem
- *
- * \param privacydata The data to free
- */
- void privacy_free_privacydata(PrivacyData *privacydata)
- {
- PrivacySystem *system = NULL;
-
- g_return_if_fail(privacydata != NULL);
- system = privacy_data_get_system(privacydata);
- if (!system)
- return;
- system->free_privacydata(privacydata);
- }
- /**
- * Check if a MimeInfo is signed with one of the available
- * privacy system. If a privacydata is set in the MimeInfo
- * it will directory return the return value by the system
- * set in the privacy data or check all available privacy
- * systems otherwise.
- *
- * \return True if the MimeInfo has a signature
- */
- gboolean privacy_mimeinfo_is_signed(MimeInfo *mimeinfo)
- {
- GSList *cur;
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
- if (mimeinfo->privacy != NULL) {
- PrivacySystem *system =
- privacy_data_get_system(mimeinfo->privacy);
- if (system == NULL) {
- mimeinfo->privacy = NULL;
- goto try_others;
- }
- if (system->is_signed != NULL)
- return system->is_signed(mimeinfo);
- else
- return FALSE;
- }
- try_others:
- for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
- PrivacySystem *system = (PrivacySystem *) cur->data;
- if(system->is_signed != NULL && system->is_signed(mimeinfo))
- return TRUE;
- }
- return FALSE;
- }
- /**
- * Check the signature of a MimeInfo. privacy_mimeinfo_is_signed
- * should be called before otherwise it is done by this function.
- * If the MimeInfo is not signed an error code will be returned.
- *
- * \return Error code indicating the result of the check,
- * < 0 if an error occured
- */
- gint privacy_mimeinfo_check_signature(MimeInfo *mimeinfo)
- {
- PrivacySystem *system;
- g_return_val_if_fail(mimeinfo != NULL, -1);
- if (mimeinfo->privacy == NULL)
- privacy_mimeinfo_is_signed(mimeinfo);
-
- if (mimeinfo->privacy == NULL)
- return -1;
-
- system = privacy_data_get_system(mimeinfo->privacy);
- if (system == NULL)
- return -1;
- if (system->check_signature == NULL)
- return -1;
-
- return system->check_signature(mimeinfo);
- }
- SignatureStatus privacy_mimeinfo_get_sig_status(MimeInfo *mimeinfo)
- {
- PrivacySystem *system;
- g_return_val_if_fail(mimeinfo != NULL, -1);
- if (mimeinfo->privacy == NULL)
- privacy_mimeinfo_is_signed(mimeinfo);
-
- if (mimeinfo->privacy == NULL)
- return SIGNATURE_UNCHECKED;
-
- system = privacy_data_get_system(mimeinfo->privacy);
- if (system == NULL)
- return SIGNATURE_UNCHECKED;
- if (system->get_sig_status == NULL)
- return SIGNATURE_UNCHECKED;
-
- return system->get_sig_status(mimeinfo);
- }
- gchar *privacy_mimeinfo_sig_info_short(MimeInfo *mimeinfo)
- {
- PrivacySystem *system;
- g_return_val_if_fail(mimeinfo != NULL, NULL);
- if (mimeinfo->privacy == NULL)
- privacy_mimeinfo_is_signed(mimeinfo);
-
- if (mimeinfo->privacy == NULL)
- return g_strdup(_("No signature found"));
-
- system = privacy_data_get_system(mimeinfo->privacy);
- if (system == NULL)
- return g_strdup(_("No signature found"));
- if (system->get_sig_info_short == NULL)
- return g_strdup(_("No information available"));
-
- return system->get_sig_info_short(mimeinfo);
- }
- gchar *privacy_mimeinfo_sig_info_full(MimeInfo *mimeinfo)
- {
- PrivacySystem *system;
- g_return_val_if_fail(mimeinfo != NULL, NULL);
- if (mimeinfo->privacy == NULL)
- privacy_mimeinfo_is_signed(mimeinfo);
-
- if (mimeinfo->privacy == NULL)
- return g_strdup(_("No signature found"));
-
- system = privacy_data_get_system(mimeinfo->privacy);
- if (system == NULL)
- return g_strdup(_("No signature found"));
- if (system->get_sig_info_full == NULL)
- return g_strdup(_("No information available"));
-
- return system->get_sig_info_full(mimeinfo);
- }
- gboolean privacy_mimeinfo_is_encrypted(MimeInfo *mimeinfo)
- {
- GSList *cur;
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
- for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
- PrivacySystem *system = (PrivacySystem *) cur->data;
- if(system->is_encrypted != NULL && system->is_encrypted(mimeinfo))
- return TRUE;
- }
- return FALSE;
- }
- static gint decrypt(MimeInfo *mimeinfo, PrivacySystem *system)
- {
- MimeInfo *decryptedinfo, *parentinfo;
- gint childnumber;
-
- g_return_val_if_fail(system->decrypt != NULL, -1);
-
- decryptedinfo = system->decrypt(mimeinfo);
- if (decryptedinfo == NULL)
- return -1;
- parentinfo = procmime_mimeinfo_parent(mimeinfo);
- childnumber = g_node_child_index(parentinfo->node, mimeinfo);
-
- procmime_mimeinfo_free_all(mimeinfo);
- g_node_insert(parentinfo->node, childnumber, decryptedinfo->node);
- return 0;
- }
- gint privacy_mimeinfo_decrypt(MimeInfo *mimeinfo)
- {
- GSList *cur;
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
- for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
- PrivacySystem *system = (PrivacySystem *) cur->data;
- if(system->is_encrypted != NULL && system->is_encrypted(mimeinfo))
- return decrypt(mimeinfo, system);
- }
- return -1;
- }
- GSList *privacy_get_system_ids()
- {
- GSList *cur;
- GSList *ret = NULL;
- for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
- PrivacySystem *system = (PrivacySystem *) cur->data;
- ret = g_slist_append(ret, g_strdup(system->id));
- }
- return ret;
- }
- static PrivacySystem *privacy_get_system(const gchar *id)
- {
- GSList *cur;
- g_return_val_if_fail(id != NULL, NULL);
- for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
- PrivacySystem *system = (PrivacySystem *) cur->data;
- if(strcmp(id, system->id) == 0)
- return system;
- }
- return NULL;
- }
- const gchar *privacy_system_get_name(const gchar *id)
- {
- PrivacySystem *system;
- g_return_val_if_fail(id != NULL, NULL);
- system = privacy_get_system(id);
- if (system == NULL)
- return NULL;
- return system->name;
- }
- gboolean privacy_system_can_sign(const gchar *id)
- {
- PrivacySystem *system;
- g_return_val_if_fail(id != NULL, FALSE);
- system = privacy_get_system(id);
- if (system == NULL)
- return FALSE;
- return system->can_sign;
- }
- gboolean privacy_system_can_encrypt(const gchar *id)
- {
- PrivacySystem *system;
- g_return_val_if_fail(id != NULL, FALSE);
- system = privacy_get_system(id);
- if (system == NULL)
- return FALSE;
- return system->can_encrypt;
- }
- gboolean privacy_sign(const gchar *id, MimeInfo *target, PrefsAccount *account)
- {
- PrivacySystem *system;
- g_return_val_if_fail(id != NULL, FALSE);
- g_return_val_if_fail(target != NULL, FALSE);
- system = privacy_get_system(id);
- if (system == NULL)
- return FALSE;
- if (!system->can_sign)
- return FALSE;
- if (system->sign == NULL)
- return FALSE;
- return system->sign(target, account);
- }
- gchar *privacy_get_encrypt_data(const gchar *id, GSList *recp_names)
- {
- PrivacySystem *system;
- g_return_val_if_fail(id != NULL, NULL);
- g_return_val_if_fail(recp_names != NULL, NULL);
- system = privacy_get_system(id);
- if (system == NULL)
- return NULL;
- if (!system->can_encrypt)
- return NULL;
- if (system->get_encrypt_data == NULL)
- return NULL;
- return system->get_encrypt_data(recp_names);
- }
- gboolean privacy_encrypt(const gchar *id, MimeInfo *mimeinfo, const gchar *encdata)
- {
- PrivacySystem *system;
- g_return_val_if_fail(id != NULL, FALSE);
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
- if (encdata == NULL) {
- privacy_set_error(_("No recipient keys defined."));
- return FALSE;
- }
- system = privacy_get_system(id);
- if (system == NULL)
- return FALSE;
- if (!system->can_encrypt)
- return FALSE;
- if (system->encrypt == NULL)
- return FALSE;
- return system->encrypt(mimeinfo, encdata);
- }
|