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