c056a5a192d4803e4e76e7e9385b357ea633f4eb
[claws.git] / src / plugins / pgpcore / sgpgme.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19  
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23  
24 #ifdef USE_GPGME
25
26 #include <time.h>
27 #include <gtk/gtk.h>
28 #include <gpgme.h>
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #if HAVE_LOCALE_H
34 #  include <locale.h>
35 #endif
36
37 #include "sgpgme.h"
38 #include "privacy.h"
39 #include "prefs_common.h"
40 #include "utils.h"
41 #include "alertpanel.h"
42 #include "passphrase.h"
43 #include "prefs_gpg.h"
44 #include "select-keys.h"
45
46 static void sgpgme_disable_all(void)
47 {
48     /* FIXME: set a flag, so that we don't bother the user with failed
49      * gpgme messages */
50 }
51
52 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig, 
53                                         gpgme_data_t plain, gpgme_data_t dummy)
54 {
55         gpgme_verify_result_t status = NULL;
56         gpgme_error_t err;
57
58         if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
59                 debug_print("op_verify err %s\n", gpgme_strerror(err));
60                 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
61                 
62         }
63         status = gpgme_op_verify_result(ctx);
64
65         return status;
66 }
67
68 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
69 {
70         unsigned long validity = 0;
71         gpgme_signature_t sig = NULL;
72         
73         if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
74                 debug_print("system error\n");
75                 return SIGNATURE_CHECK_FAILED;
76         }
77
78         if (status == NULL) {
79                 debug_print("status == NULL\n");
80                 return SIGNATURE_UNCHECKED;
81         }
82         sig = status->signatures;
83
84         if (sig == NULL) {
85                 debug_print("sig == NULL\n");
86                 return SIGNATURE_UNCHECKED;
87         }
88         validity = sig->validity;
89
90         debug_print("err code %d\n", gpg_err_code(sig->status));
91         switch (gpg_err_code(sig->status)) {
92         case GPG_ERR_NO_ERROR:
93                 return SIGNATURE_OK;
94         case GPG_ERR_SIG_EXPIRED:
95         case GPG_ERR_KEY_EXPIRED:
96                 return SIGNATURE_WARN;
97         case GPG_ERR_BAD_SIGNATURE:
98                 return SIGNATURE_INVALID;
99         case GPG_ERR_NO_PUBKEY:
100                 return SIGNATURE_CHECK_FAILED;
101         default:
102                 return SIGNATURE_CHECK_FAILED;
103         }
104         return SIGNATURE_CHECK_FAILED;
105 }
106
107 static const gchar *get_validity_str(unsigned long validity)
108 {
109         switch (gpg_err_code(validity)) {
110         case GPGME_VALIDITY_UNKNOWN:
111                 return _("Unknown");
112         case GPGME_VALIDITY_UNDEFINED:
113                 return _("Undefined");
114         case GPGME_VALIDITY_NEVER:
115                 return _("Never");
116         case GPGME_VALIDITY_MARGINAL:
117                 return _("Marginal");
118         case GPGME_VALIDITY_FULL:
119                 return _("Full");
120         case GPGME_VALIDITY_ULTIMATE:
121                 return _("Ultimate");
122         default:
123                 return _("Error");
124         }
125 }
126
127 static gchar *extract_name(const char *uid)
128 {
129         if (uid == NULL)
130                 return NULL;
131         if (!strncmp(uid, "CN=", 3)) {
132                 gchar *result = g_strdup(uid+3);
133                 if (strstr(result, ","))
134                         *(strstr(result, ",")) = '\0';
135                 return result;
136         } else if (strstr(uid, ",CN=")) {
137                 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
138                 if (strstr(result, ","))
139                         *(strstr(result, ",")) = '\0';
140                 return result;
141         } else {
142                 return g_strdup(uid);
143         }
144 }
145 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
146 {
147         gpgme_signature_t sig = NULL;
148         gchar *uname = NULL;
149         gpgme_key_t key;
150         gchar *result = NULL;
151
152         if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
153                 return g_strdup(_("The signature can't be checked - GPG error."));
154         }
155
156         if (status == NULL) {
157                 return g_strdup(_("The signature has not been checked."));
158         }
159         sig = status->signatures;
160         if (sig == NULL) {
161                 return g_strdup(_("The signature has not been checked."));
162         }
163
164         gpgme_get_key(ctx, sig->fpr, &key, 0);
165         if (key)
166                 uname = extract_name(key->uids->uid);
167         else
168                 uname = g_strdup("<?>");
169         switch (gpg_err_code(sig->status)) {
170         case GPG_ERR_NO_ERROR:
171                 result = g_strdup_printf(_("Good signature from %s."),
172                         uname);
173                 break;
174         case GPG_ERR_SIG_EXPIRED:
175                 result = g_strdup_printf(_("Expired signature from %s."), uname);
176                 break;
177         case GPG_ERR_KEY_EXPIRED:
178                 result = g_strdup_printf(_("Expired key from %s."), uname);
179                 break;
180         case GPG_ERR_BAD_SIGNATURE:
181                 result = g_strdup_printf(_("Bad signature from %s."), uname);
182                 break;
183         case GPG_ERR_NO_PUBKEY: {
184                 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
185                 result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
186                 g_free(id);
187                 break;
188                 }
189         default:
190                 result = g_strdup(_("The signature has not been checked."));
191                 break;
192         }
193         if (result == NULL)
194                 result = g_strdup(_("Error"));
195         g_free(uname);
196         return result;
197 }
198
199 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
200 {
201         gint i = 0;
202         gchar *ret;
203         GString *siginfo;
204         gpgme_signature_t sig = status->signatures;
205         
206         siginfo = g_string_sized_new(64);
207         while (sig) {
208                 gpgme_user_id_t user = NULL;
209                 gpgme_key_t key;
210
211                 const gchar *keytype, *keyid, *uid;
212                 
213                 gpgme_get_key(ctx, sig->fpr, &key, 0);
214
215                 if (key) {
216                         user = key->uids;
217                         keytype = gpgme_pubkey_algo_name(
218                                         key->subkeys->pubkey_algo);
219                         keyid = key->subkeys->keyid;
220                         uid = user->uid;
221                 } else {
222                         keytype = "?";
223                         keyid = "?";
224                         uid = "?";
225                 }
226                 g_string_append_printf(siginfo,
227                         _("Signature made using %s key ID %s\n"),
228                         keytype, keyid);
229                 
230                 switch (gpg_err_code(sig->status)) {
231                 case GPG_ERR_NO_ERROR:
232                 case GPG_ERR_KEY_EXPIRED:
233                         g_string_append_printf(siginfo,
234                                 _("Good signature from \"%s\" (Trust: %s)\n"),
235                                 uid, get_validity_str(sig->validity));
236                         break;
237                 case GPG_ERR_SIG_EXPIRED:
238                         g_string_append_printf(siginfo,
239                                 _("Expired signature from \"%s\"\n"),
240                                 uid);
241                         break;
242                 case GPG_ERR_BAD_SIGNATURE:
243                         g_string_append_printf(siginfo,
244                                 _("BAD signature from \"%s\"\n"),
245                                 uid);
246                         break;
247                 default:
248                         break;
249                 }
250                 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
251                         gint j = 1;
252                         user = user ? user->next : NULL;
253                         while (user != NULL) {
254                                 g_string_append_printf(siginfo,
255                                         _("                aka \"%s\"\n"),
256                                         user->uid);
257                                 j++;
258                                 user = user->next;
259                         }
260                         g_string_append_printf(siginfo,
261                                 _("Primary key fingerprint: %s\n"), 
262                                 sig ? sig->fpr: "?");
263                 }
264                 
265                 g_string_append(siginfo, "\n");
266                 i++;
267                 sig = sig->next;
268         }
269
270         ret = siginfo->str;
271         g_string_free(siginfo, FALSE);
272         return ret;
273 }
274
275 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
276 {
277         gpgme_data_t data = NULL;
278         gpgme_error_t err;
279         FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
280         gchar *tmp_file = NULL;
281
282         if (!fp) 
283                 return NULL;
284
285         tmp_file = get_tmp_file();
286         copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
287         fclose(fp);
288         fp = g_fopen(tmp_file, "rb");
289         debug_print("tmp file %s\n", tmp_file);
290         if (!fp) 
291                 return NULL;
292         
293         err = gpgme_data_new_from_file(&data, tmp_file, 1);
294         g_unlink(tmp_file);
295         g_free(tmp_file);
296
297         debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
298         if (err) {
299                 debug_print ("gpgme_data_new_from_file failed: %s\n",
300                              gpgme_strerror (err));
301                 return NULL;
302         }
303         return data;
304 }
305
306 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
307 {
308         struct passphrase_cb_info_s info;
309         gpgme_data_t plain;
310         gpgme_error_t err;
311
312         memset (&info, 0, sizeof info);
313         
314         if (gpgme_data_new(&plain) != GPG_ERR_NO_ERROR) {
315                 gpgme_release(ctx);
316                 return NULL;
317         }
318         
319         if (!getenv("GPG_AGENT_INFO")) {
320                 info.c = ctx;
321                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
322         }
323
324         err = gpgme_op_decrypt_verify(ctx, cipher, plain);
325         if (err != GPG_ERR_NO_ERROR) {
326                 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
327                 gpgmegtk_free_passphrase();
328                 gpgme_data_release(plain);
329                 return NULL;
330         }
331
332         err = gpgme_data_rewind(plain);
333         if (err) {
334                 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
335         }
336
337         debug_print("decrypted.\n");
338         *status = gpgme_op_verify_result (ctx);
339
340         return plain;
341 }
342
343 gchar *sgpgme_get_encrypt_data(GSList *recp_names)
344 {
345         SelectionResult result = KEY_SELECTION_CANCEL;
346         gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result);
347         gchar *ret = NULL;
348         int i = 0;
349
350         if (!keys) {
351                 if (result == KEY_SELECTION_DONT)
352                         return g_strdup("_DONT_ENCRYPT_");
353                 else
354                         return NULL;
355         }
356         while (keys[i]) {
357                 gpgme_subkey_t skey = keys[i]->subkeys;
358                 gchar *fpr = skey->fpr;
359                 gchar *tmp = NULL;
360                 debug_print("adding %s\n", fpr);
361                 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
362                 g_free(ret);
363                 ret = tmp;
364                 i++;
365         }
366         return ret;
367 }
368
369 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
370 {
371         GPGAccountConfig *config;
372
373         gpgme_signers_clear(ctx);
374
375         config = prefs_gpg_account_get_config(account);
376
377         if (config->sign_key != SIGN_KEY_DEFAULT) {
378                 gchar *keyid;
379                 gpgme_key_t key;
380
381                 if (config->sign_key == SIGN_KEY_BY_FROM)
382                         keyid = account->address;
383                 else if (config->sign_key == SIGN_KEY_CUSTOM)
384                         keyid = config->sign_key_id;
385                 else
386                         return FALSE;
387
388                 gpgme_op_keylist_start(ctx, keyid, 1);
389                 while (!gpgme_op_keylist_next(ctx, &key)) {
390                         gpgme_signers_add(ctx, key);
391                         gpgme_key_release(key);
392                 }
393                 gpgme_op_keylist_end(ctx);
394         }
395
396         prefs_gpg_account_free_config(config);
397
398         return TRUE;
399 }
400
401 void sgpgme_init()
402 {
403         gpgme_engine_info_t engineInfo;
404         if (gpgme_check_version("0.4.5")) {
405                 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
406                 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
407                 if (!gpgme_get_engine_info(&engineInfo)) {
408                         while (engineInfo) {
409                                 debug_print("GpgME Protocol: %s\n      Version: %s\n",
410                                         gpgme_get_protocol_name(engineInfo->protocol),
411                                         engineInfo->version);
412                                 engineInfo = engineInfo->next;
413                         }
414                 }
415         } else {
416                 sgpgme_disable_all();
417
418                 if (prefs_gpg_get_config()->gpg_warning) {
419                         AlertValue val;
420
421                         val = alertpanel_full
422                                 (_("Warning"),
423                                  _("GnuPG is not installed properly, or needs "
424                                  "to be upgraded.\n"
425                                  "OpenPGP support disabled."),
426                                  GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
427                                  ALERT_WARNING, G_ALERTDEFAULT);
428                         if (val & G_ALERTDISABLE)
429                                 prefs_gpg_get_config()->gpg_warning = FALSE;
430                 }
431         }
432 }
433
434 void sgpgme_done()
435 {
436         gpgmegtk_free_passphrase();
437 }
438
439 #endif /* USE_GPGME */