a24b6e0f16356d6a5436f4d846d8dd33d4b7036a
[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 void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
71 {
72         GtkTextView *text;
73         GtkTextBuffer *buffer;
74         GtkTextIter iter;
75         gpgme_data_t sigdata = NULL;
76         gpgme_verify_result_t sigstatus = NULL;
77         gpgme_ctx_t ctx = NULL;
78         gpgme_key_t key = NULL;
79         gpgme_signature_t sig = NULL;
80         gpgme_error_t err = 0;
81         if (!partinfo) return;
82
83         
84         textview_set_font(textview, NULL);
85         textview_clear(textview);
86
87         text = GTK_TEXT_VIEW(textview->text);
88         buffer = gtk_text_view_get_buffer(text);
89         gtk_text_buffer_get_start_iter(buffer, &iter);
90
91         err = gpgme_new (&ctx);
92         if (err) {
93                 debug_print("err : %s\n", gpgme_strerror(err));
94                 textview_show_mime_part(textview, partinfo);
95                 return;
96         }
97         
98         sigdata = sgpgme_data_from_mimeinfo(partinfo);
99         if (!sigdata) {
100                 g_warning("no sigdata");
101                 textview_show_mime_part(textview, partinfo);
102                 return;
103         }
104
105         /* Here we do not care about what data we attempt to verify with the
106          * signature, or about result of the verification - all we care about
107          * is that we find out ID of the key used to make this signature. */
108         sigstatus = sgpgme_verify_signature(ctx, sigdata, NULL, sigdata);
109         if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
110                 g_warning("no sigstatus");
111                 textview_show_mime_part(textview, partinfo);
112                 return;
113         }
114         sig = sigstatus->signatures;
115         if (!sig) {
116                 g_warning("no sig");
117                 textview_show_mime_part(textview, partinfo);
118                 return;
119         }
120         gpgme_get_key(ctx, sig->fpr, &key, 0);
121         if (!key) {
122                 gchar *cmd = g_strdup_printf("gpg --no-tty --recv-keys %s", sig->fpr);
123                 AlertValue val = G_ALERTDEFAULT;
124                 if (!prefs_common_get_prefs()->work_offline) {
125                         val = alertpanel(_("Key import"),
126                                 _("This key is not in your keyring. Do you want "
127                                   "Claws Mail to try and import it from a "
128                                   "keyserver?"),
129                                   GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
130                         GTK_EVENTS_FLUSH();
131                 }
132                 if (val == G_ALERTDEFAULT) {
133                         TEXTVIEW_INSERT(_("\n  Key ID "));
134                         TEXTVIEW_INSERT(sig->fpr);
135                         TEXTVIEW_INSERT(":\n\n");
136                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
137                         TEXTVIEW_INSERT(_("   It should be possible to import it "));
138                         if (prefs_common_get_prefs()->work_offline)
139                                 TEXTVIEW_INSERT(_("when working online,\n   or "));
140                         TEXTVIEW_INSERT(_("with the following command: \n\n     "));
141                         TEXTVIEW_INSERT(cmd);
142                 } else {
143 #ifndef G_OS_WIN32
144                         int res = 0;
145                         pid_t pid = 0;
146         
147                         TEXTVIEW_INSERT(_("\n  Importing key ID "));
148                         TEXTVIEW_INSERT(sig->fpr);
149                         TEXTVIEW_INSERT(":\n\n");
150
151                         main_window_cursor_wait(mainwindow_get_mainwindow());
152                         textview_cursor_wait(textview);
153                         GTK_EVENTS_FLUSH();
154
155                         pid = fork();
156                         if (pid == -1) {
157                                 res = -1;
158                         } else if (pid == 0) {
159                                 /* son */
160                                 gchar **argv;
161                                 argv = strsplit_with_quote(cmd, " ", 0);
162                                 res = execvp(argv[0], argv);
163                                 exit(255);
164                         } else {
165                                 int status = 0;
166                                 time_t start_wait = time(NULL);
167                                 res = -1;
168                                 do {
169                                         if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
170                                                 usleep(200000);
171                                         } else {
172                                                 res = WEXITSTATUS(status);
173                                                 break;
174                                         }
175                                         if (time(NULL) - start_wait > 9) {
176                                                 debug_print("SIGTERM'ing gpg %d\n", pid);
177                                                 kill(pid, SIGTERM);
178                                         }
179                                         if (time(NULL) - start_wait > 10) {
180                                                 debug_print("SIGKILL'ing gpg %d\n", pid);
181                                                 kill(pid, SIGKILL);
182                                                 break;
183                                         }
184                                 } while(1);
185                         }
186                         main_window_cursor_normal(mainwindow_get_mainwindow());
187                         textview_cursor_normal(textview);
188                         debug_print("res %d\n", res);
189                         if (res == 0) {
190                                 TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
191                         } else {
192                                 TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
193                                 TEXTVIEW_INSERT(_("   Key servers are sometimes slow.\n"));
194                                 TEXTVIEW_INSERT(_("   You can try to import it manually with the command:\n\n     "));
195                                 TEXTVIEW_INSERT(cmd);
196                         }
197 #else
198                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
199                         TEXTVIEW_INSERT(_("   Key import isn't implemented in Windows.\n"));
200 #endif
201                 }
202                 g_free(cmd);
203                 return;
204         } else {
205                 TEXTVIEW_INSERT(_("\n  Key ID "));
206                 TEXTVIEW_INSERT(sig->fpr);
207                 TEXTVIEW_INSERT(":\n\n");
208                 TEXTVIEW_INSERT(_("   This key is in your keyring.\n"));
209         }
210         gpgme_data_release(sigdata);
211         gpgme_release(ctx);
212         textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
213 }
214
215
216 static void pgp_show_mimepart(MimeViewer *_viewer,
217                                 const gchar *infile,
218                                 MimeInfo *partinfo)
219 {
220         PgpViewer *viewer = (PgpViewer *)_viewer;
221         debug_print("pgp_show_mimepart\n");
222         viewer->textview->messageview = _viewer->mimeview->messageview;
223         pgpview_show_mime_part(viewer->textview, partinfo);
224 }
225
226 static void pgp_clear_viewer(MimeViewer *_viewer)
227 {
228         PgpViewer *viewer = (PgpViewer *)_viewer;
229         debug_print("pgp_clear_viewer\n");
230         textview_clear(viewer->textview);
231 }
232
233 static void pgp_destroy_viewer(MimeViewer *_viewer)
234 {
235         PgpViewer *viewer = (PgpViewer *)_viewer;
236         debug_print("pgp_destroy_viewer\n");
237         textview_destroy(viewer->textview);
238 }
239
240 static MimeViewer *pgp_viewer_create(void)
241 {
242         PgpViewer *viewer;
243
244         debug_print("pgp_viewer_create\n");
245         
246         viewer = g_new0(PgpViewer, 1);
247         viewer->mimeviewer.factory = &pgp_viewer_factory;
248         viewer->mimeviewer.get_widget = pgp_get_widget;
249         viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
250         viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
251         viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer; 
252         viewer->mimeviewer.get_selection = NULL;
253         viewer->textview = textview_create();
254         textview_init(viewer->textview);
255
256         gtk_widget_show_all(viewer->textview->vbox);
257
258         return (MimeViewer *) viewer;
259 }
260
261 static MimeViewerFactory pgp_viewer_factory =
262 {
263         content_types,  
264         0,
265
266         pgp_viewer_create,
267 };
268
269 void pgp_viewer_init(void)
270 {
271         mimeview_register_viewer_factory(&pgp_viewer_factory);
272 }
273
274 void pgp_viewer_done(void)
275 {
276         mimeview_unregister_viewer_factory(&pgp_viewer_factory);
277
278 }