22e9caefc4fdc28b9a5e0de8ea40a417a6fe094f
[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 #else
33 #  include <pthread.h>
34 #  include <windows.h>
35 #endif
36 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
37 #  include <sys/signal.h>
38 #endif
39
40 #include "version.h"
41 #include "common/claws.h"
42 #include "mainwindow.h"
43 #include "mimeview.h"
44 #include "textview.h"
45 #include "sgpgme.h"
46 #include "prefs_common.h"
47 #include "prefs_gpg.h"
48 #include "alertpanel.h"
49 #include "plugin.h"
50
51 typedef struct _PgpViewer PgpViewer;
52
53 static MimeViewerFactory pgp_viewer_factory;
54
55 struct _PgpViewer
56 {
57         MimeViewer       mimeviewer;
58         TextView        *textview;      
59 };
60
61 static gchar *content_types[] = 
62         {"application/pgp-signature", NULL};
63
64 static GtkWidget *pgp_get_widget(MimeViewer *_viewer)
65 {
66         PgpViewer *viewer = (PgpViewer *) _viewer;
67
68         debug_print("pgp_get_widget\n");
69
70         return GTK_WIDGET(viewer->textview->vbox);
71 }
72
73 static gchar *_get_gpg_executable_name()
74 {
75         gpgme_engine_info_t e;
76
77         if (!gpgme_get_engine_info(&e)) {
78                 while (e != NULL) {
79                         if (e->protocol == GPGME_PROTOCOL_OpenPGP
80                                         && e->file_name != NULL) {
81                                 debug_print("Found gpg executable: '%s'\n", e->file_name);
82                                 return e->file_name;
83                         }
84                 }
85         }
86
87         return NULL;
88 }
89
90 #ifdef G_OS_WIN32
91 struct _ImportCtx {
92         gboolean done;
93         gchar *cmd;
94         DWORD exitcode;
95 };
96
97 static void *_import_threaded(void *arg)
98 {
99         struct _ImportCtx *ctx = (struct _ImportCtx *)arg;
100         gboolean result;
101
102         PROCESS_INFORMATION pi = {0};
103         STARTUPINFO si = {0};
104
105         result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
106                         NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
107                         NULL, NULL, &si, &pi);
108
109         if (!result) {
110                 debug_print("Couldn't execute '%s'\n", ctx->cmd);
111         } else {
112                 WaitForSingleObject(pi.hProcess, 10000);
113                 result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
114                 if (ctx->exitcode == STILL_ACTIVE) {
115                         debug_print("Process still running, terminating it.\n");
116                         TerminateProcess(pi.hProcess, 255);
117                 }
118
119                 CloseHandle(pi.hProcess);
120                 CloseHandle(pi.hThread);
121
122                 if (!result) {
123                         debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
124                 }
125         }
126
127         ctx->done = TRUE;
128         return NULL;
129 }
130 #endif
131
132 static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
133 {
134         GtkTextView *text;
135         GtkTextBuffer *buffer;
136         GtkTextIter iter;
137         gpgme_data_t sigdata = NULL;
138         gpgme_verify_result_t sigstatus = NULL;
139         gpgme_ctx_t ctx = NULL;
140         gpgme_key_t key = NULL;
141         gpgme_signature_t sig = NULL;
142         gpgme_error_t err = 0;
143         gboolean imported = FALSE;
144
145         if (!partinfo) return;
146         
147         textview_set_font(textview, NULL);
148         textview_clear(textview);
149
150         text = GTK_TEXT_VIEW(textview->text);
151         buffer = gtk_text_view_get_buffer(text);
152         gtk_text_buffer_get_start_iter(buffer, &iter);
153
154         err = gpgme_new (&ctx);
155         if (err) {
156                 debug_print("err : %s\n", gpgme_strerror(err));
157                 textview_show_mime_part(textview, partinfo);
158                 return;
159         }
160         
161         sigdata = sgpgme_data_from_mimeinfo(partinfo);
162         if (!sigdata) {
163                 g_warning("no sigdata");
164                 textview_show_mime_part(textview, partinfo);
165                 return;
166         }
167
168         /* Here we do not care about what data we attempt to verify with the
169          * signature, or about result of the verification - all we care about
170          * is that we find out ID of the key used to make this signature. */
171         sigstatus = sgpgme_verify_signature(ctx, sigdata, NULL, sigdata);
172         if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
173                 g_warning("no sigstatus");
174                 textview_show_mime_part(textview, partinfo);
175                 return;
176         }
177         sig = sigstatus->signatures;
178         if (!sig) {
179                 g_warning("no sig");
180                 textview_show_mime_part(textview, partinfo);
181                 return;
182         }
183         gpgme_get_key(ctx, sig->fpr, &key, 0);
184         if (!key) {
185                 gchar *gpgbin = _get_gpg_executable_name();
186                 gchar *cmd = g_strdup_printf("\"%s\" --no-tty --recv-keys %s",
187                                 (gpgbin ? gpgbin : "gpg"), sig->fpr);
188                 AlertValue val = G_ALERTDEFAULT;
189                 if (!prefs_common_get_prefs()->work_offline) {
190                         val = alertpanel(_("Key import"),
191                                 _("This key is not in your keyring. Do you want "
192                                   "Claws Mail to try and import it from a "
193                                   "keyserver?"),
194                                   GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
195                         GTK_EVENTS_FLUSH();
196                 }
197                 if (val == G_ALERTDEFAULT) {
198                         TEXTVIEW_INSERT(_("\n  Key ID "));
199                         TEXTVIEW_INSERT(sig->fpr);
200                         TEXTVIEW_INSERT(":\n\n");
201                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
202                         TEXTVIEW_INSERT(_("   It should be possible to import it "));
203                         if (prefs_common_get_prefs()->work_offline)
204                                 TEXTVIEW_INSERT(_("when working online,\n   or "));
205                         TEXTVIEW_INSERT(_("with the following command: \n\n     "));
206                         TEXTVIEW_INSERT(cmd);
207                 } else {
208                         TEXTVIEW_INSERT(_("\n  Importing key ID "));
209                         TEXTVIEW_INSERT(sig->fpr);
210                         TEXTVIEW_INSERT(":\n\n");
211
212                         main_window_cursor_wait(mainwindow_get_mainwindow());
213                         textview_cursor_wait(textview);
214                         GTK_EVENTS_FLUSH();
215
216 #ifndef G_OS_WIN32
217                         int res = 0;
218                         pid_t pid = 0;
219
220                         pid = fork();
221                         if (pid == -1) {
222                                 res = -1;
223                         } else if (pid == 0) {
224                                 /* son */
225                                 gchar **argv;
226                                 argv = strsplit_with_quote(cmd, " ", 0);
227                                 res = execvp(argv[0], argv);
228                                 exit(255);
229                         } else {
230                                 int status = 0;
231                                 time_t start_wait = time(NULL);
232                                 res = -1;
233                                 do {
234                                         if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
235                                                 usleep(200000);
236                                         } else {
237                                                 res = WEXITSTATUS(status);
238                                                 break;
239                                         }
240                                         if (time(NULL) - start_wait > 9) {
241                                                 debug_print("SIGTERM'ing gpg %d\n", pid);
242                                                 kill(pid, SIGTERM);
243                                         }
244                                         if (time(NULL) - start_wait > 10) {
245                                                 debug_print("SIGKILL'ing gpg %d\n", pid);
246                                                 kill(pid, SIGKILL);
247                                                 break;
248                                         }
249                                 } while(1);
250                         }
251                         debug_print("res %d\n", res);
252                         if (res == 0)
253                                 imported = TRUE;
254 #else
255                         /* We need to call gpg in a separate thread, so that waiting for
256                          * it to finish does not block the UI. */
257                         pthread_t pt;
258                         struct _ImportCtx *ctx = malloc(sizeof(struct _ImportCtx));
259
260                         ctx->done = FALSE;
261                         ctx->exitcode = STILL_ACTIVE;
262                         ctx->cmd = cmd;
263
264                         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
265                                                 _import_threaded, (void *)ctx) != 0) {
266                                 debug_print("Couldn't create thread, continuing unthreaded.\n");
267                                 _import_threaded(ctx);
268                         } else {
269                                 debug_print("Thread created, waiting for it to finish...\n");
270                                 while (!ctx->done)
271                                         claws_do_idle();
272                         }
273
274                         debug_print("Thread finished.\n");
275                         pthread_join(pt, NULL);
276
277                         if (ctx->exitcode == 0) {
278                                 imported = TRUE;
279                         }
280                         g_free(ctx);
281 #endif
282                         main_window_cursor_normal(mainwindow_get_mainwindow());
283                         textview_cursor_normal(textview);
284                         if (imported) {
285                                 TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
286                         } else {
287                                 TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
288                                 TEXTVIEW_INSERT(_("   Key servers are sometimes slow.\n"));
289                                 TEXTVIEW_INSERT(_("   You can try to import it manually with the command:\n\n     "));
290                                 TEXTVIEW_INSERT(cmd);
291                         }
292                 }
293                 g_free(cmd);
294                 return;
295         } else {
296                 TEXTVIEW_INSERT(_("\n  Key ID "));
297                 TEXTVIEW_INSERT(sig->fpr);
298                 TEXTVIEW_INSERT(":\n\n");
299                 TEXTVIEW_INSERT(_("   This key is in your keyring.\n"));
300         }
301         gpgme_data_release(sigdata);
302         gpgme_release(ctx);
303         textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
304 }
305
306
307 static void pgp_show_mimepart(MimeViewer *_viewer,
308                                 const gchar *infile,
309                                 MimeInfo *partinfo)
310 {
311         PgpViewer *viewer = (PgpViewer *)_viewer;
312         debug_print("pgp_show_mimepart\n");
313         viewer->textview->messageview = _viewer->mimeview->messageview;
314         pgpview_show_mime_part(viewer->textview, partinfo);
315 }
316
317 static void pgp_clear_viewer(MimeViewer *_viewer)
318 {
319         PgpViewer *viewer = (PgpViewer *)_viewer;
320         debug_print("pgp_clear_viewer\n");
321         textview_clear(viewer->textview);
322 }
323
324 static void pgp_destroy_viewer(MimeViewer *_viewer)
325 {
326         PgpViewer *viewer = (PgpViewer *)_viewer;
327         debug_print("pgp_destroy_viewer\n");
328         textview_destroy(viewer->textview);
329 }
330
331 static MimeViewer *pgp_viewer_create(void)
332 {
333         PgpViewer *viewer;
334
335         debug_print("pgp_viewer_create\n");
336         
337         viewer = g_new0(PgpViewer, 1);
338         viewer->mimeviewer.factory = &pgp_viewer_factory;
339         viewer->mimeviewer.get_widget = pgp_get_widget;
340         viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
341         viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
342         viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer; 
343         viewer->mimeviewer.get_selection = NULL;
344         viewer->textview = textview_create();
345         textview_init(viewer->textview);
346
347         gtk_widget_show_all(viewer->textview->vbox);
348
349         return (MimeViewer *) viewer;
350 }
351
352 static MimeViewerFactory pgp_viewer_factory =
353 {
354         content_types,  
355         0,
356
357         pgp_viewer_create,
358 };
359
360 void pgp_viewer_init(void)
361 {
362         mimeview_register_viewer_factory(&pgp_viewer_factory);
363 }
364
365 void pgp_viewer_done(void)
366 {
367         mimeview_unregister_viewer_factory(&pgp_viewer_factory);
368
369 }