Fix CID 1491401 and 1491402: (possible) modulo by zero.
[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-2021 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         MsgInfo *msginfo = textview->messageview->msginfo;
128
129         if (!partinfo) return;
130         
131         textview_set_font(textview, NULL);
132         textview_clear(textview);
133
134         text = GTK_TEXT_VIEW(textview->text);
135         buffer = gtk_text_view_get_buffer(text);
136         gtk_text_buffer_get_start_iter(buffer, &iter);
137
138         err = gpgme_new (&ctx);
139         if (err) {
140                 debug_print("err : %s\n", gpgme_strerror(err));
141                 textview_show_mime_part(textview, partinfo);
142                 return;
143         }
144         
145         sigdata = sgpgme_data_from_mimeinfo(partinfo);
146         if (!sigdata) {
147                 g_warning("no sigdata");
148                 textview_show_mime_part(textview, partinfo);
149                 return;
150         }
151
152         /* Here we do not care about what data we attempt to verify with the
153          * signature, or about result of the verification - all we care about
154          * is that we find out ID of the key used to make this signature. */
155         sigstatus = sgpgme_verify_signature(ctx, sigdata, NULL, sigdata);
156         if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
157                 g_warning("no sigstatus");
158                 textview_show_mime_part(textview, partinfo);
159                 return;
160         }
161         sig = sigstatus->signatures;
162         if (!sig) {
163                 g_warning("no sig");
164                 textview_show_mime_part(textview, partinfo);
165                 return;
166         }
167         gpgme_get_key(ctx, sig->fpr, &key, 0);
168         if (!key) {
169                 gchar *gpgbin = get_gpg_executable_name();
170                 gchar *from_addr = g_strdup(msginfo->from);
171                 extract_address(from_addr);
172                 gchar *cmd_ks = g_strdup_printf("\"%s\" --batch --no-tty --recv-keys %s",
173                                 (gpgbin ? gpgbin : "gpg2"), sig->fpr);
174                 gchar *cmd_wkd = g_strdup_printf("\"%s\" --batch --no-tty --locate-keys \"%s\"",
175                                 (gpgbin ? gpgbin : "gpg2"), from_addr);
176
177                 AlertValue val = G_ALERTDEFAULT;
178                 if (!prefs_common_get_prefs()->work_offline) {
179                         val = alertpanel(_("Key import"),
180                                 _("This key is not in your keyring. Do you want "
181                                   "Claws Mail to try to import it?"),
182                                   _("_No"), _("from keyserver"), _("from Web Key Directory"),
183                                   ALERTFOCUS_SECOND);
184                         GTK_EVENTS_FLUSH();
185                 }
186                 if (val == G_ALERTDEFAULT) {
187                         TEXTVIEW_INSERT(_("\n  Key ID "));
188                         TEXTVIEW_INSERT(sig->fpr);
189                         TEXTVIEW_INSERT(":\n\n");
190                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
191                         TEXTVIEW_INSERT(_("   It should be possible to import it "));
192                         if (prefs_common_get_prefs()->work_offline)
193                                 TEXTVIEW_INSERT(_("when working online,\n   or "));
194                         TEXTVIEW_INSERT(_("with either of the following commands: \n\n     "));
195                         TEXTVIEW_INSERT(cmd_ks);
196                         TEXTVIEW_INSERT("\n\n");
197                         TEXTVIEW_INSERT(cmd_wkd);
198                 } else if (val == G_ALERTALTERNATE || val == G_ALERTOTHER) {
199                         TEXTVIEW_INSERT(_("\n  Importing key ID "));
200                         TEXTVIEW_INSERT(sig->fpr);
201                         TEXTVIEW_INSERT(":\n\n");
202
203                         main_window_cursor_wait(mainwindow_get_mainwindow());
204                         textview_cursor_wait(textview);
205                         GTK_EVENTS_FLUSH();
206
207 #ifndef G_OS_WIN32
208                         int res = 0;
209                         pid_t pid = 0;
210
211                         pid = fork();
212                         if (pid == -1) {
213                                 res = -1;
214                         } else if (pid == 0) {
215                                 /* son */
216                                 gchar **argv;
217                                 if (val == G_ALERTOTHER)
218                                         argv = strsplit_with_quote(cmd_wkd, " ", 0);
219                                 else
220                                         argv = strsplit_with_quote(cmd_ks, " ", 0);
221                                 res = execvp(argv[0], argv);
222                                 perror("execvp");
223                                 exit(255);
224                         } else {
225                                 int status = 0;
226                                 time_t start_wait = time(NULL);
227                                 res = -1;
228                                 do {
229                                         if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
230                                                 usleep(200000);
231                                         } else {
232                                                 res = WEXITSTATUS(status);
233                                                 break;
234                                         }
235                                         if (time(NULL) - start_wait > 9) {
236                                                 debug_print("SIGTERM'ing gpg %d\n", pid);
237                                                 kill(pid, SIGTERM);
238                                         }
239                                         if (time(NULL) - start_wait > 10) {
240                                                 debug_print("SIGKILL'ing gpg %d\n", pid);
241                                                 kill(pid, SIGKILL);
242                                                 break;
243                                         }
244                                 } while(1);
245                         }
246                         debug_print("res %d\n", res);
247                         if (res == 0)
248                                 imported = TRUE;
249 #else
250                         /* We need to call gpg in a separate thread, so that waiting for
251                          * it to finish does not block the UI. */
252                         pthread_t pt;
253                         struct _ImportCtx *ctx = malloc(sizeof(struct _ImportCtx));
254
255                         ctx->done = FALSE;
256                         ctx->exitcode = STILL_ACTIVE;
257                         ctx->cmd = (val == G_ALERTOTHER)? cmd_wkd : cmd_ks;
258
259                         if (pthread_create(&pt, NULL,
260                                                 _import_threaded, (void *)ctx) != 0) {
261                                 debug_print("Couldn't create thread, continuing unthreaded.\n");
262                                 _import_threaded(ctx);
263                         } else {
264                                 debug_print("Thread created, waiting for it to finish...\n");
265                                 while (!ctx->done)
266                                         claws_do_idle();
267                         }
268
269                         debug_print("Thread finished.\n");
270                         pthread_join(pt, NULL);
271
272                         if (ctx->exitcode == 0) {
273                                 imported = TRUE;
274                         }
275                         g_free(ctx);
276 #endif
277                         main_window_cursor_normal(mainwindow_get_mainwindow());
278                         textview_cursor_normal(textview);
279                         if (imported) {
280                                 TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
281                         } else {
282                                 TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
283                                 TEXTVIEW_INSERT(_("   Key servers are sometimes slow.\n"));
284                                 TEXTVIEW_INSERT(_("   You can try to import it manually with the command:"));
285                                 TEXTVIEW_INSERT("\n\n     ");
286                                 TEXTVIEW_INSERT(cmd_ks);
287                                 TEXTVIEW_INSERT("\n\n     ");    
288                                 TEXTVIEW_INSERT(_("or"));
289                                 TEXTVIEW_INSERT("\n\n     ");
290                                 TEXTVIEW_INSERT(cmd_wkd);
291                         }
292                 }
293                 g_free(cmd_ks);
294                 g_free(cmd_wkd);
295                 g_free(from_addr);
296         } else {
297                 TEXTVIEW_INSERT(_("\n  Key ID "));
298
299 #if defined GPGME_VERSION_NUMBER && GPGME_VERSION_NUMBER >= 0x010700
300                 TEXTVIEW_INSERT(key->fpr);
301 #else
302                 TEXTVIEW_INSERT(sig->fpr);
303 #endif
304
305                 TEXTVIEW_INSERT(":\n\n");
306                 TEXTVIEW_INSERT(_("   This key is in your keyring.\n"));
307         }
308         gpgme_data_release(sigdata);
309         gpgme_release(ctx);
310         textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
311 }
312
313
314 static void pgp_show_mimepart(MimeViewer *_viewer,
315                                 const gchar *infile,
316                                 MimeInfo *partinfo)
317 {
318         PgpViewer *viewer = (PgpViewer *)_viewer;
319         debug_print("pgp_show_mimepart\n");
320         viewer->textview->messageview = _viewer->mimeview->messageview;
321         pgpview_show_mime_part(viewer->textview, partinfo);
322 }
323
324 static void pgp_clear_viewer(MimeViewer *_viewer)
325 {
326         PgpViewer *viewer = (PgpViewer *)_viewer;
327         debug_print("pgp_clear_viewer\n");
328         textview_clear(viewer->textview);
329 }
330
331 static void pgp_destroy_viewer(MimeViewer *_viewer)
332 {
333         PgpViewer *viewer = (PgpViewer *)_viewer;
334         debug_print("pgp_destroy_viewer\n");
335         textview_destroy(viewer->textview);
336 }
337
338 static MimeViewer *pgp_viewer_create(void)
339 {
340         PgpViewer *viewer;
341
342         debug_print("pgp_viewer_create\n");
343         
344         viewer = g_new0(PgpViewer, 1);
345         viewer->mimeviewer.factory = &pgp_viewer_factory;
346         viewer->mimeviewer.get_widget = pgp_get_widget;
347         viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
348         viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
349         viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer; 
350         viewer->mimeviewer.get_selection = NULL;
351         viewer->textview = textview_create();
352         textview_init(viewer->textview);
353
354         gtk_widget_show_all(viewer->textview->vbox);
355
356         return (MimeViewer *) viewer;
357 }
358
359 static MimeViewerFactory pgp_viewer_factory =
360 {
361         content_types,  
362         0,
363
364         pgp_viewer_create,
365 };
366
367 void pgp_viewer_init(void)
368 {
369         mimeview_register_viewer_factory(&pgp_viewer_factory);
370 }
371
372 void pgp_viewer_done(void)
373 {
374         mimeview_unregister_viewer_factory(&pgp_viewer_factory);
375
376 }