2005-12-09 [cleroy] 1.9.100cvs79
[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                 switch (gpg_err_code(sig->validity)) {
94                 case GPGME_VALIDITY_NEVER:
95                         return SIGNATURE_INVALID;
96                 case GPGME_VALIDITY_UNKNOWN:
97                 case GPGME_VALIDITY_UNDEFINED:
98                 case GPGME_VALIDITY_MARGINAL:
99                 case GPGME_VALIDITY_FULL:
100                 case GPGME_VALIDITY_ULTIMATE:
101                         return SIGNATURE_OK;
102                 default:
103                         return SIGNATURE_CHECK_FAILED;
104                 }
105         case GPG_ERR_SIG_EXPIRED:
106         case GPG_ERR_KEY_EXPIRED:
107                 return SIGNATURE_WARN;
108         case GPG_ERR_BAD_SIGNATURE:
109                 return SIGNATURE_INVALID;
110         case GPG_ERR_NO_PUBKEY:
111                 return SIGNATURE_CHECK_FAILED;
112         default:
113                 return SIGNATURE_CHECK_FAILED;
114         }
115         return SIGNATURE_CHECK_FAILED;
116 }
117
118 static const gchar *get_validity_str(unsigned long validity)
119 {
120         switch (gpg_err_code(validity)) {
121         case GPGME_VALIDITY_UNKNOWN:
122                 return _("Unknown");
123         case GPGME_VALIDITY_UNDEFINED:
124                 return _("Undefined");
125         case GPGME_VALIDITY_NEVER:
126                 return _("Never");
127         case GPGME_VALIDITY_MARGINAL:
128                 return _("Marginal");
129         case GPGME_VALIDITY_FULL:
130                 return _("Full");
131         case GPGME_VALIDITY_ULTIMATE:
132                 return _("Ultimate");
133         default:
134                 return _("Error");
135         }
136 }
137
138 static gchar *extract_name(const char *uid)
139 {
140         if (uid == NULL)
141                 return NULL;
142         if (!strncmp(uid, "CN=", 3)) {
143                 gchar *result = g_strdup(uid+3);
144                 if (strstr(result, ","))
145                         *(strstr(result, ",")) = '\0';
146                 return result;
147         } else if (strstr(uid, ",CN=")) {
148                 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
149                 if (strstr(result, ","))
150                         *(strstr(result, ",")) = '\0';
151                 return result;
152         } else {
153                 return g_strdup(uid);
154         }
155 }
156 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
157 {
158         gpgme_signature_t sig = NULL;
159         gchar *uname = NULL;
160         gpgme_key_t key;
161         gchar *result = NULL;
162
163         if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
164                 return g_strdup(_("The signature can't be checked - GPG error."));
165         }
166
167         if (status == NULL) {
168                 return g_strdup(_("The signature has not been checked."));
169         }
170         sig = status->signatures;
171         if (sig == NULL) {
172                 return g_strdup(_("The signature has not been checked."));
173         }
174
175         gpgme_get_key(ctx, sig->fpr, &key, 0);
176         if (key)
177                 uname = extract_name(key->uids->uid);
178         else
179                 uname = g_strdup("<?>");
180         switch (gpg_err_code(sig->status)) {
181         case GPG_ERR_NO_ERROR:
182                 switch (gpg_err_code(sig->validity)) {
183                 case GPGME_VALIDITY_MARGINAL:
184                 case GPGME_VALIDITY_FULL:
185                 case GPGME_VALIDITY_ULTIMATE:
186                         result = g_strdup_printf(_("Good signature from %s."), uname);
187                         break;
188                 case GPGME_VALIDITY_UNKNOWN:
189                 case GPGME_VALIDITY_UNDEFINED:
190                 case GPGME_VALIDITY_NEVER:
191                 default:
192                         result = g_strdup_printf(_("Good signature (untrusted) from %s."), uname);
193                         break;
194                 }
195                 break;
196         case GPG_ERR_SIG_EXPIRED:
197                 result = g_strdup_printf(_("Expired signature from %s."), uname);
198                 break;
199         case GPG_ERR_KEY_EXPIRED:
200                 result = g_strdup_printf(_("Expired key from %s."), uname);
201                 break;
202         case GPG_ERR_BAD_SIGNATURE:
203                 result = g_strdup_printf(_("Bad signature from %s."), uname);
204                 break;
205         case GPG_ERR_NO_PUBKEY: {
206                 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
207                 result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
208                 g_free(id);
209                 break;
210                 }
211         default:
212                 result = g_strdup(_("The signature has not been checked."));
213                 break;
214         }
215         if (result == NULL)
216                 result = g_strdup(_("Error"));
217         g_free(uname);
218         return result;
219 }
220
221 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
222 {
223         gint i = 0;
224         gchar *ret;
225         GString *siginfo;
226         gpgme_signature_t sig = status->signatures;
227         
228         siginfo = g_string_sized_new(64);
229         while (sig) {
230                 gpgme_user_id_t user = NULL;
231                 gpgme_key_t key;
232
233                 const gchar *keytype, *keyid, *uid;
234                 
235                 gpgme_get_key(ctx, sig->fpr, &key, 0);
236
237                 if (key) {
238                         user = key->uids;
239                         keytype = gpgme_pubkey_algo_name(
240                                         key->subkeys->pubkey_algo);
241                         keyid = key->subkeys->keyid;
242                         uid = user->uid;
243                 } else {
244                         keytype = "?";
245                         keyid = "?";
246                         uid = "?";
247                 }
248                 g_string_append_printf(siginfo,
249                         _("Signature made using %s key ID %s\n"),
250                         keytype, keyid);
251                 
252                 switch (gpg_err_code(sig->status)) {
253                 case GPG_ERR_NO_ERROR:
254                 case GPG_ERR_KEY_EXPIRED:
255                         g_string_append_printf(siginfo,
256                                 _("Good signature from \"%s\" (Trust: %s)\n"),
257                                 uid, get_validity_str(sig->validity));
258                         break;
259                 case GPG_ERR_SIG_EXPIRED:
260                         g_string_append_printf(siginfo,
261                                 _("Expired signature from \"%s\"\n"),
262                                 uid);
263                         break;
264                 case GPG_ERR_BAD_SIGNATURE:
265                         g_string_append_printf(siginfo,
266                                 _("BAD signature from \"%s\"\n"),
267                                 uid);
268                         break;
269                 default:
270                         break;
271                 }
272                 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
273                         gint j = 1;
274                         user = user ? user->next : NULL;
275                         while (user != NULL) {
276                                 g_string_append_printf(siginfo,
277                                         _("                aka \"%s\"\n"),
278                                         user->uid);
279                                 j++;
280                                 user = user->next;
281                         }
282                         g_string_append_printf(siginfo,
283                                 _("Primary key fingerprint: %s\n"), 
284                                 sig ? sig->fpr: "?");
285                 }
286                 
287                 g_string_append(siginfo, "\n");
288                 i++;
289                 sig = sig->next;
290         }
291
292         ret = siginfo->str;
293         g_string_free(siginfo, FALSE);
294         return ret;
295 }
296
297 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
298 {
299         gpgme_data_t data = NULL;
300         gpgme_error_t err;
301         FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
302         gchar *tmp_file = NULL;
303
304         if (!fp) 
305                 return NULL;
306
307         tmp_file = get_tmp_file();
308         copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
309         fclose(fp);
310         fp = g_fopen(tmp_file, "rb");
311         debug_print("tmp file %s\n", tmp_file);
312         if (!fp) 
313                 return NULL;
314         
315         err = gpgme_data_new_from_file(&data, tmp_file, 1);
316         g_unlink(tmp_file);
317         g_free(tmp_file);
318
319         debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
320         if (err) {
321                 debug_print ("gpgme_data_new_from_file failed: %s\n",
322                              gpgme_strerror (err));
323                 return NULL;
324         }
325         return data;
326 }
327
328 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
329 {
330         struct passphrase_cb_info_s info;
331         gpgme_data_t plain;
332         gpgme_error_t err;
333
334         memset (&info, 0, sizeof info);
335         
336         if (gpgme_data_new(&plain) != GPG_ERR_NO_ERROR) {
337                 gpgme_release(ctx);
338                 return NULL;
339         }
340         
341         if (!getenv("GPG_AGENT_INFO")) {
342                 info.c = ctx;
343                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
344         }
345
346         err = gpgme_op_decrypt_verify(ctx, cipher, plain);
347         if (err != GPG_ERR_NO_ERROR) {
348                 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
349                 gpgmegtk_free_passphrase();
350                 gpgme_data_release(plain);
351                 return NULL;
352         }
353
354         err = gpgme_data_rewind(plain);
355         if (err) {
356                 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
357         }
358
359         debug_print("decrypted.\n");
360         *status = gpgme_op_verify_result (ctx);
361
362         return plain;
363 }
364
365 gchar *sgpgme_get_encrypt_data(GSList *recp_names)
366 {
367         SelectionResult result = KEY_SELECTION_CANCEL;
368         gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result);
369         gchar *ret = NULL;
370         int i = 0;
371
372         if (!keys) {
373                 if (result == KEY_SELECTION_DONT)
374                         return g_strdup("_DONT_ENCRYPT_");
375                 else
376                         return NULL;
377         }
378         while (keys[i]) {
379                 gpgme_subkey_t skey = keys[i]->subkeys;
380                 gchar *fpr = skey->fpr;
381                 gchar *tmp = NULL;
382                 debug_print("adding %s\n", fpr);
383                 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
384                 g_free(ret);
385                 ret = tmp;
386                 i++;
387         }
388         return ret;
389 }
390
391 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
392 {
393         GPGAccountConfig *config;
394
395         gpgme_signers_clear(ctx);
396
397         config = prefs_gpg_account_get_config(account);
398
399         if (config->sign_key != SIGN_KEY_DEFAULT) {
400                 gchar *keyid;
401                 gpgme_key_t key;
402
403                 if (config->sign_key == SIGN_KEY_BY_FROM)
404                         keyid = account->address;
405                 else if (config->sign_key == SIGN_KEY_CUSTOM)
406                         keyid = config->sign_key_id;
407                 else
408                         return FALSE;
409
410                 gpgme_op_keylist_start(ctx, keyid, 1);
411                 while (!gpgme_op_keylist_next(ctx, &key)) {
412                         gpgme_signers_add(ctx, key);
413                         gpgme_key_release(key);
414                 }
415                 gpgme_op_keylist_end(ctx);
416         }
417
418         prefs_gpg_account_free_config(config);
419
420         return TRUE;
421 }
422
423 void sgpgme_init()
424 {
425         gpgme_engine_info_t engineInfo;
426         if (gpgme_check_version("0.4.5")) {
427                 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
428                 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
429                 if (!gpgme_get_engine_info(&engineInfo)) {
430                         while (engineInfo) {
431                                 debug_print("GpgME Protocol: %s\n      Version: %s\n",
432                                         gpgme_get_protocol_name(engineInfo->protocol),
433                                         engineInfo->version);
434                                 engineInfo = engineInfo->next;
435                         }
436                 }
437         } else {
438                 sgpgme_disable_all();
439
440                 if (prefs_gpg_get_config()->gpg_warning) {
441                         AlertValue val;
442
443                         val = alertpanel_full
444                                 (_("Warning"),
445                                  _("GnuPG is not installed properly, or needs "
446                                  "to be upgraded.\n"
447                                  "OpenPGP support disabled."),
448                                  GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
449                                  ALERT_WARNING, G_ALERTDEFAULT);
450                         if (val & G_ALERTDISABLE)
451                                 prefs_gpg_get_config()->gpg_warning = FALSE;
452                 }
453         }
454 }
455
456 void sgpgme_done()
457 {
458         gpgmegtk_free_passphrase();
459 }
460
461 #endif /* USE_GPGME */