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