Ask GpgME for path to gpg executable for manual key import.
[claws.git] / src / plugins / pgpcore / pgp_viewer.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 the Claws Mail team
4  * This file Copyright (C) 2006 Colin Leroy <colin@colino.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include <stddef.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <sys/types.h>
30 #ifndef G_OS_WIN32
31 #  include <sys/wait.h>
32 #endif
33 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
34 #  include <sys/signal.h>
35 #endif
36
37 #include "version.h"
38 #include "common/claws.h"
39 #include "mainwindow.h"
40 #include "mimeview.h"
41 #include "textview.h"
42 #include "sgpgme.h"
43 #include "prefs_common.h"
44 #include "prefs_gpg.h"
45 #include "alertpanel.h"
46 #include "plugin.h"
47
48 typedef struct _PgpViewer PgpViewer;
49
50 static MimeViewerFactory pgp_viewer_factory;
51
52 struct _PgpViewer
53 {
54         MimeViewer       mimeviewer;
55         TextView        *textview;      
56 };
57
58 static gchar *content_types[] = 
59         {"application/pgp-signature", NULL};
60
61 static GtkWidget *pgp_get_widget(MimeViewer *_viewer)
62 {
63         PgpViewer *viewer = (PgpViewer *) _viewer;
64
65         debug_print("pgp_get_widget\n");
66
67         return GTK_WIDGET(viewer->textview->vbox);
68 }
69
70 static gchar *_get_gpg_executable_name()
71 {
72         gpgme_engine_info_t e;
73
74         if (!gpgme_get_engine_info(&e)) {
75                 while (e != NULL) {
76                         if (e->protocol == GPGME_PROTOCOL_OpenPGP
77                                         && e->file_name != NULL) {
78                                 debug_print("Found gpg executable: '%s'\n", e->file_name);
79                                 return e->file_name;
80                         }
81                 }
82         }
83
84         return NULL;
85 }
86
87 static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
88 {
89         GtkTextView *text;
90         GtkTextBuffer *buffer;
91         GtkTextIter iter;
92         gpgme_data_t sigdata = NULL;
93         gpgme_verify_result_t sigstatus = NULL;
94         gpgme_ctx_t ctx = NULL;
95         gpgme_key_t key = NULL;
96         gpgme_signature_t sig = NULL;
97         gpgme_error_t err = 0;
98         if (!partinfo) return;
99
100         
101         textview_set_font(textview, NULL);
102         textview_clear(textview);
103
104         text = GTK_TEXT_VIEW(textview->text);
105         buffer = gtk_text_view_get_buffer(text);
106         gtk_text_buffer_get_start_iter(buffer, &iter);
107
108         err = gpgme_new (&ctx);
109         if (err) {
110                 debug_print("err : %s\n", gpgme_strerror(err));
111                 textview_show_mime_part(textview, partinfo);
112                 return;
113         }
114         
115         sigdata = sgpgme_data_from_mimeinfo(partinfo);
116         if (!sigdata) {
117                 g_warning("no sigdata");
118                 textview_show_mime_part(textview, partinfo);
119                 return;
120         }
121
122         /* Here we do not care about what data we attempt to verify with the
123          * signature, or about result of the verification - all we care about
124          * is that we find out ID of the key used to make this signature. */
125         sigstatus = sgpgme_verify_signature(ctx, sigdata, NULL, sigdata);
126         if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
127                 g_warning("no sigstatus");
128                 textview_show_mime_part(textview, partinfo);
129                 return;
130         }
131         sig = sigstatus->signatures;
132         if (!sig) {
133                 g_warning("no sig");
134                 textview_show_mime_part(textview, partinfo);
135                 return;
136         }
137         gpgme_get_key(ctx, sig->fpr, &key, 0);
138         if (!key) {
139                 gchar *gpgbin = _get_gpg_executable_name();
140                 gchar *cmd = g_strdup_printf("\"%s\" --no-tty --recv-keys %s",
141                                 (gpgbin ? gpgbin : "gpg"), sig->fpr);
142                 AlertValue val = G_ALERTDEFAULT;
143                 if (!prefs_common_get_prefs()->work_offline) {
144                         val = alertpanel(_("Key import"),
145                                 _("This key is not in your keyring. Do you want "
146                                   "Claws Mail to try and import it from a "
147                                   "keyserver?"),
148                                   GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
149                         GTK_EVENTS_FLUSH();
150                 }
151                 if (val == G_ALERTDEFAULT) {
152                         TEXTVIEW_INSERT(_("\n  Key ID "));
153                         TEXTVIEW_INSERT(sig->fpr);
154                         TEXTVIEW_INSERT(":\n\n");
155                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
156                         TEXTVIEW_INSERT(_("   It should be possible to import it "));
157                         if (prefs_common_get_prefs()->work_offline)
158                                 TEXTVIEW_INSERT(_("when working online,\n   or "));
159                         TEXTVIEW_INSERT(_("with the following command: \n\n     "));
160                         TEXTVIEW_INSERT(cmd);
161                 } else {
162 #ifndef G_OS_WIN32
163                         int res = 0;
164                         pid_t pid = 0;
165         
166                         TEXTVIEW_INSERT(_("\n  Importing key ID "));
167                         TEXTVIEW_INSERT(sig->fpr);
168                         TEXTVIEW_INSERT(":\n\n");
169
170                         main_window_cursor_wait(mainwindow_get_mainwindow());
171                         textview_cursor_wait(textview);
172                         GTK_EVENTS_FLUSH();
173
174                         pid = fork();
175                         if (pid == -1) {
176                                 res = -1;
177                         } else if (pid == 0) {
178                                 /* son */
179                                 gchar **argv;
180                                 argv = strsplit_with_quote(cmd, " ", 0);
181                                 res = execvp(argv[0], argv);
182                                 exit(255);
183                         } else {
184                                 int status = 0;
185                                 time_t start_wait = time(NULL);
186                                 res = -1;
187                                 do {
188                                         if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
189                                                 usleep(200000);
190                                         } else {
191                                                 res = WEXITSTATUS(status);
192                                                 break;
193                                         }
194                                         if (time(NULL) - start_wait > 9) {
195                                                 debug_print("SIGTERM'ing gpg %d\n", pid);
196                                                 kill(pid, SIGTERM);
197                                         }
198                                         if (time(NULL) - start_wait > 10) {
199                                                 debug_print("SIGKILL'ing gpg %d\n", pid);
200                                                 kill(pid, SIGKILL);
201                                                 break;
202                                         }
203                                 } while(1);
204                         }
205                         main_window_cursor_normal(mainwindow_get_mainwindow());
206                         textview_cursor_normal(textview);
207                         debug_print("res %d\n", res);
208                         if (res == 0) {
209                                 TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
210                         } else {
211                                 TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
212                                 TEXTVIEW_INSERT(_("   Key servers are sometimes slow.\n"));
213                                 TEXTVIEW_INSERT(_("   You can try to import it manually with the command:\n\n     "));
214                                 TEXTVIEW_INSERT(cmd);
215                         }
216 #else
217                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
218                         TEXTVIEW_INSERT(_("   Key import isn't implemented in Windows.\n"));
219 #endif
220                 }
221                 g_free(cmd);
222                 return;
223         } else {
224                 TEXTVIEW_INSERT(_("\n  Key ID "));
225                 TEXTVIEW_INSERT(sig->fpr);
226                 TEXTVIEW_INSERT(":\n\n");
227                 TEXTVIEW_INSERT(_("   This key is in your keyring.\n"));
228         }
229         gpgme_data_release(sigdata);
230         gpgme_release(ctx);
231         textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
232 }
233
234
235 static void pgp_show_mimepart(MimeViewer *_viewer,
236                                 const gchar *infile,
237                                 MimeInfo *partinfo)
238 {
239         PgpViewer *viewer = (PgpViewer *)_viewer;
240         debug_print("pgp_show_mimepart\n");
241         viewer->textview->messageview = _viewer->mimeview->messageview;
242         pgpview_show_mime_part(viewer->textview, partinfo);
243 }
244
245 static void pgp_clear_viewer(MimeViewer *_viewer)
246 {
247         PgpViewer *viewer = (PgpViewer *)_viewer;
248         debug_print("pgp_clear_viewer\n");
249         textview_clear(viewer->textview);
250 }
251
252 static void pgp_destroy_viewer(MimeViewer *_viewer)
253 {
254         PgpViewer *viewer = (PgpViewer *)_viewer;
255         debug_print("pgp_destroy_viewer\n");
256         textview_destroy(viewer->textview);
257 }
258
259 static MimeViewer *pgp_viewer_create(void)
260 {
261         PgpViewer *viewer;
262
263         debug_print("pgp_viewer_create\n");
264         
265         viewer = g_new0(PgpViewer, 1);
266         viewer->mimeviewer.factory = &pgp_viewer_factory;
267         viewer->mimeviewer.get_widget = pgp_get_widget;
268         viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
269         viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
270         viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer; 
271         viewer->mimeviewer.get_selection = NULL;
272         viewer->textview = textview_create();
273         textview_init(viewer->textview);
274
275         gtk_widget_show_all(viewer->textview->vbox);
276
277         return (MimeViewer *) viewer;
278 }
279
280 static MimeViewerFactory pgp_viewer_factory =
281 {
282         content_types,  
283         0,
284
285         pgp_viewer_create,
286 };
287
288 void pgp_viewer_init(void)
289 {
290         mimeview_register_viewer_factory(&pgp_viewer_factory);
291 }
292
293 void pgp_viewer_done(void)
294 {
295         mimeview_unregister_viewer_factory(&pgp_viewer_factory);
296
297 }