"Master password" is now called "master passphrase".
[claws.git] / src / password_gtk.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2016 The Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #ifndef PASSWORD_CRYPTO_OLD
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 #include "common/utils.h"
32 #include "gtk/manage_window.h"
33 #include "gtk/gtkutils.h"
34 #include "account.h"
35 #include "alertpanel.h"
36 #include "password.h"
37 #include "prefs_common.h"
38
39 static void entry_activated(GtkEntry *entry, gpointer user_data)
40 {
41         const gchar *text = gtk_entry_get_text(entry);
42
43         if (strlen(text) > 0)
44                 gtk_widget_grab_focus(GTK_WIDGET(user_data));
45 }
46
47 struct _ctx {
48         gboolean done;
49         GtkWidget *dialog;
50         GtkWidget *entry_old;
51         GtkWidget *entry_new1;
52         GtkWidget *entry_new2;
53 };
54
55 static void ok_button_clicked(GtkButton *button, gpointer user_data)
56 {
57         struct _ctx *ctx = (struct _ctx *)user_data;
58         const gchar *old = NULL;
59         const gchar *new1 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new1));
60         const gchar *new2 = gtk_entry_get_text(GTK_ENTRY(ctx->entry_new2));
61
62         debug_print("OK button activated\n");
63
64         /* Now we check the new passphrase - same in both entries. */
65         if (strcmp(new1, new2)) {
66                 debug_print("passphrases do not match\n");
67                 alertpanel_warning(_("New passwphrases do not match, try again."));
68                 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new1), "");
69                 gtk_entry_set_text(GTK_ENTRY(ctx->entry_new2), "");
70                 gtk_widget_grab_focus(ctx->entry_new1);
71                 return;
72         }
73
74         /* If there is an existing master passphrase, check for its correctness
75          * in entry_old. */
76         if (master_passphrase_is_set()
77                         && ((old = gtk_entry_get_text(GTK_ENTRY(ctx->entry_old))) == NULL
78                                 || strlen(old) == 0 || !master_passphrase_is_correct(old))) {
79                 debug_print("old passphrase incorrect\n");
80                 alertpanel_warning(_("Incorrect old master passphrase entered, try again."));
81                 gtk_entry_set_text(GTK_ENTRY(ctx->entry_old), "");
82                 gtk_widget_grab_focus(ctx->entry_old);
83                 return;
84         }
85
86         master_passphrase_change(old, new1);
87
88         ctx->done = TRUE;
89         gtk_widget_destroy(ctx->dialog);
90         ctx->dialog = NULL;
91 }
92
93 static void cancel_button_clicked(GtkButton *button, gpointer user_data)
94 {
95         struct _ctx *ctx = (struct _ctx *)user_data;
96         ctx->done = TRUE;
97         gtk_widget_destroy(ctx->dialog);
98         ctx->dialog = NULL;
99 }
100
101 static void dialog_destroy(GtkWidget *widget, gpointer user_data)
102 {
103         struct _ctx *ctx = (struct _ctx *)user_data;
104         ctx->done = TRUE;
105         ctx->dialog = NULL;
106 }
107
108 void master_passphrase_change_dialog()
109 {
110         static PangoFontDescription *font_desc;
111         GtkWidget *dialog;
112         GtkWidget *vbox, *hbox;
113         GtkWidget *icon, *table, *label;
114         GtkWidget *msg_title;
115         GtkWidget *entry_old, *entry_new1, *entry_new2;
116         GtkWidget *confirm_area;
117         GtkWidget *ok_button, *cancel_button;
118         struct _ctx *ctx;
119
120         dialog = gtk_dialog_new();
121
122         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
123         gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
124         gtk_window_set_title(GTK_WINDOW(dialog), "");
125
126         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
127
128         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
129         gtk_box_set_spacing(GTK_BOX(vbox), 14);
130         hbox = gtk_hbox_new(FALSE, 12);
131         gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
132         gtk_widget_show(hbox);
133         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
134
135         icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
136                         GTK_ICON_SIZE_DIALOG);
137         gtk_misc_set_alignment(GTK_MISC(icon), 0.5, 0.0);
138         gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, FALSE, 0);
139
140         vbox = gtk_vbox_new(FALSE, 12);
141         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
142         gtk_widget_show(vbox);
143
144         msg_title = gtk_label_new(_("Changing master passphrase"));
145         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
146         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
147         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
148         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
149         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
150         if (!font_desc) {
151                 gint size;
152
153                 size = pango_font_description_get_size
154                         (gtk_widget_get_style(msg_title)->font_desc);
155                 font_desc = pango_font_description_new();
156                 pango_font_description_set_weight
157                         (font_desc, PANGO_WEIGHT_BOLD);
158                 pango_font_description_set_size
159                         (font_desc, size * PANGO_SCALE_LARGE);
160         }
161         if (font_desc)
162                 gtk_widget_modify_font(msg_title, font_desc);
163
164         label = gtk_label_new(
165         _("If a master passphrase is currently active, it\n"
166         "needs to be entered.")
167         );
168         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
169         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
170         gtk_widget_show(label);
171
172         table = gtk_table_new(4, 2, FALSE);
173
174         /* Old passphrase */
175         label = gtk_label_new(_("Old passphrase:"));
176         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
177         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
178                         GTK_EXPAND | GTK_FILL, 0, 0, 0);
179
180         entry_old = gtk_entry_new();
181         gtk_entry_set_visibility(GTK_ENTRY(entry_old), FALSE);
182         gtk_table_attach(GTK_TABLE(table), entry_old, 1, 2, 0, 1,
183                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
184
185         /* Separator */
186         gtk_table_attach(GTK_TABLE(table),
187                         gtk_hseparator_new(), 0, 2, 1, 2,
188                         GTK_FILL | GTK_EXPAND, 0, 0, 5);
189
190         /* New passphrase */
191         label = gtk_label_new(_("New passphrase:"));
192         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
193         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
194                         GTK_EXPAND | GTK_FILL, 0, 0, 0);
195
196         entry_new1 = gtk_entry_new();
197         gtk_entry_set_visibility(GTK_ENTRY(entry_new1), FALSE);
198         gtk_table_attach(GTK_TABLE(table), entry_new1, 1, 2, 2, 3,
199                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
200
201         /* New passphrase again */
202         label = gtk_label_new(_("Confirm passphrase:"));
203         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
204         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
205                         GTK_EXPAND | GTK_FILL, 0, 0, 0);
206
207         entry_new2 = gtk_entry_new();
208         gtk_entry_set_visibility(GTK_ENTRY(entry_new2), FALSE);
209         gtk_table_attach(GTK_TABLE(table), entry_new2, 1, 2, 3, 4,
210                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
211
212         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
213
214         /* Dialog buttons */
215         gtkut_stock_button_set_create(&confirm_area,
216                         &cancel_button, GTK_STOCK_CANCEL,
217                         &ok_button, GTK_STOCK_OK,
218                         NULL, NULL);
219
220         gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dialog))),
221                         confirm_area, FALSE, FALSE, 0);
222         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
223
224         gtk_widget_grab_default(ok_button);
225
226         /* If no master passphrase is set, disable the "old passphrase" entry */
227         if (!master_passphrase_is_set())
228                 gtk_widget_set_sensitive(entry_old, FALSE);
229
230         g_signal_connect(G_OBJECT(entry_old), "activate",
231                         G_CALLBACK(entry_activated), entry_new1);
232         g_signal_connect(G_OBJECT(entry_new1), "activate",
233                         G_CALLBACK(entry_activated), entry_new2);
234         gtk_entry_set_activates_default(GTK_ENTRY(entry_new2), TRUE);
235
236         ctx = g_new(struct _ctx, 1);
237         ctx->done = FALSE;
238         ctx->dialog = dialog;
239         ctx->entry_old = entry_old;
240         ctx->entry_new1 = entry_new1;
241         ctx->entry_new2 = entry_new2;
242
243         g_signal_connect(G_OBJECT(ok_button), "clicked",
244                         G_CALLBACK(ok_button_clicked), ctx);
245         g_signal_connect(G_OBJECT(cancel_button), "clicked",
246                         G_CALLBACK(cancel_button_clicked), ctx);
247
248         g_signal_connect(G_OBJECT(dialog), "destroy",
249                         G_CALLBACK(dialog_destroy), ctx);
250
251         gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
252         gtk_window_present(GTK_WINDOW(dialog));
253
254         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
255         manage_window_set_transient(GTK_WINDOW(dialog));
256
257         while (!ctx->done)
258                 gtk_main_iteration();
259
260         if (ctx->dialog != NULL)
261                 gtk_widget_destroy(ctx->dialog);
262
263         GTK_EVENTS_FLUSH();
264
265         g_free(ctx);
266 }
267
268 #endif /* !PASSWORD_CRYPTO_OLD */