File: initial-dialog.cpp

package info (click to toggle)
gobby 0.6.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,872 kB
  • sloc: cpp: 19,417; ansic: 12,094; sh: 4,162; makefile: 417; xml: 42
file content (293 lines) | stat: -rw-r--r-- 8,691 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Gobby - GTK-based collaborative text editor
 * Copyright (C) 2008-2015 Armin Burgmeier <armin@arbur.net>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "dialogs/initial-dialog.hpp"

#include "core/credentialsgenerator.hpp"

#include "util/file.hpp"
#include "util/i18n.hpp"
#include "features.hpp"

#include <glibmm/markup.h>
#include <gtkmm/alignment.h>

namespace
{
	class InitialCertGenerator
	{
	public:
		InitialCertGenerator(Gobby::CertificateManager& cert_manager,
		                     Gobby::StatusBar& status_bar,
		                     const std::string& key_filename,
		                     const std::string& cert_filename):
			m_cert_manager(cert_manager),
			m_status_bar(status_bar),
			m_key_filename(key_filename),
			m_cert_filename(cert_filename),
			m_key(NULL),
			m_status_handle(m_status_bar.invalid_handle())
		{
			m_key_handle = Gobby::create_key(
				GNUTLS_PK_RSA,
				2048,
				sigc::mem_fun(
					*this,
					&InitialCertGenerator::
						on_key_generated));

			m_status_handle = m_status_bar.add_info_message(
				Gobby::_("Generating 2048-bit "
				         "RSA private key..."));
		}

		~InitialCertGenerator()
		{
			if(m_key != NULL)
				gnutls_x509_privkey_deinit(m_key);
			if(m_status_handle != m_status_bar.invalid_handle())
				m_status_bar.remove_message(m_status_handle);
		}

	private:
		void on_key_generated(const Gobby::KeyGeneratorHandle* handle,
		                      gnutls_x509_privkey_t key,
		                      const GError* error)
		{
			m_key_handle.reset(NULL);
			m_key = key;

			if(error != NULL)
			{
				m_status_bar.add_error_message(
					Gobby::_("Failed to generate "
					         "private key"),
					Glib::ustring::compose(
						Gobby::_("%1\n\nYou can try "
						         "again to create a "
						         "key in the "
						         "Security tab of "
						         "the preferences "
						         "dialog."),
						error->message));

				m_cert_manager.set_private_key(
					NULL, m_key_filename.c_str(), error);

				done();
				return;
			}

			m_crt_handle = Gobby::create_self_signed_certificate(
				m_key,
				sigc::mem_fun(
					*this,
					&InitialCertGenerator::
						on_cert_generated));
		}

		void on_cert_generated(
			const Gobby::CertificateGeneratorHandle* hndl,
			gnutls_x509_crt_t cert,
			const GError* error)
		{
			m_status_bar.remove_message(m_status_handle);
			m_status_handle = m_status_bar.invalid_handle();
			m_crt_handle.reset(NULL);

			m_cert_manager.set_private_key(
				m_key, m_key_filename.c_str(), NULL);
			m_key = NULL;

			if(error != NULL)
			{
				m_status_bar.add_error_message(
					Gobby::_("Failed to generate "
					         "self-signed certificate"),
					Glib::ustring::compose(
						Gobby::_("%1\n\nYou can try "
						         "again to create a "
						         "certificate in the "
						         "Security tab of "
						         "the preferences "
						         "dialog."),
						error->message));
			}
			else
			{
				// Note this needs to be allocated with
				// g_malloc, since it is directly passed and
				// freed by libinfinity
				gnutls_x509_crt_t* crt_array =
					static_cast<gnutls_x509_crt_t*>(
						g_malloc(sizeof(
							gnutls_x509_crt_t)));
				crt_array[0] = cert;

				m_cert_manager.set_certificates(
					crt_array, 1,
					m_cert_filename.c_str(), NULL);
			}

			done();
		}

		void done()
		{
			delete this;
		}

		Gobby::CertificateManager& m_cert_manager;
		Gobby::StatusBar& m_status_bar;
		const std::string m_key_filename;
		const std::string m_cert_filename;

		gnutls_x509_privkey_t m_key;
		std::unique_ptr<Gobby::KeyGeneratorHandle> m_key_handle;
		std::unique_ptr<Gobby::CertificateGeneratorHandle> m_crt_handle;
		Gobby::StatusBar::MessageHandle m_status_handle;
	};
}

Gobby::InitialDialog::InitialDialog(
	GtkDialog* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
:
	Gtk::Dialog(cobject), m_status_bar(NULL), m_preferences(NULL),
	m_cert_manager(NULL)
{
	builder->get_widget("name-entry", m_name_entry);
	builder->get_widget_derived("color-button", m_color_button);

	builder->get_widget("allow-remote-connections",
	                    m_remote_allow_connections);
	builder->get_widget("ask-password", m_remote_require_password);
	builder->get_widget("password", m_remote_password_entry);
	builder->get_widget("create-self-signed", m_remote_auth_self);
	builder->get_widget("use-existing-certificate",
	                    m_remote_auth_external);
	builder->get_widget("private-key-file",
	                    m_remote_auth_external_keyfile);
	builder->get_widget("certificate-file",
	                    m_remote_auth_external_certfile);

	builder->get_widget("remote-connections-grid",
	                    m_remote_connections_grid);
	builder->get_widget("password-grid",
	                    m_password_grid);
	builder->get_widget("certificate-grid",
	                    m_certificate_grid);

	m_remote_allow_connections->signal_toggled().connect(
		sigc::mem_fun(*this,
			&Gobby::InitialDialog::
				on_remote_allow_connections_toggled));
	m_remote_require_password->signal_toggled().connect(
		sigc::mem_fun(*this,
			&Gobby::InitialDialog::
				on_remote_require_password_toggled));
	m_remote_auth_external->signal_toggled().connect(
		sigc::mem_fun(*this,
			&Gobby::InitialDialog::
				on_remote_auth_external_toggled));

	add_button(_("_Close"), Gtk::RESPONSE_CLOSE);
}

std::unique_ptr<Gobby::InitialDialog>
Gobby::InitialDialog::create(Gtk::Window& parent,
                             StatusBar& status_bar,
                             Preferences& preferences,
                             CertificateManager& cert_manager)
{
	Glib::RefPtr<Gtk::Builder> builder =
		Gtk::Builder::create_from_resource(
			"/de/0x539/gobby/ui/initial-dialog.ui");

	InitialDialog* dialog_ptr;
	builder->get_widget_derived("InitialDialog", dialog_ptr);
	std::unique_ptr<InitialDialog> dialog(dialog_ptr);

	dialog->set_transient_for(parent);

	dialog->m_status_bar = &status_bar;
	dialog->m_preferences = &preferences;
	dialog->m_cert_manager = &cert_manager;

	// Set initial values
	dialog->m_name_entry->set_text(preferences.user.name);
	dialog->m_color_button->set_hue(preferences.user.hue);
	dialog->m_remote_allow_connections->set_active(
		preferences.user.allow_remote_access);
	dialog->m_remote_require_password->set_active(
		preferences.user.require_password);
	dialog->m_remote_password_entry->set_text(
		static_cast<std::string>(preferences.user.password));

	// Set initial sensitivity
	dialog->on_remote_allow_connections_toggled();
	dialog->on_remote_require_password_toggled();
	dialog->on_remote_auth_external_toggled();

	return dialog;
}

void Gobby::InitialDialog::on_response(int id)
{
	m_preferences->user.name = m_name_entry->get_text();
	m_preferences->user.hue = m_color_button->get_hue();
	m_preferences->user.allow_remote_access =
		m_remote_allow_connections->get_active();
	m_preferences->user.require_password =
		m_remote_require_password->get_active();
	m_preferences->user.password =
		m_remote_password_entry->get_text();
	m_preferences->security.authentication_enabled = true;
	if(m_remote_auth_self->get_active())
	{
		// The certificate generator takes care of its own:
		new InitialCertGenerator(
			*m_cert_manager, *m_status_bar,
			config_filename("key.pem"),
			config_filename("cert.pem"));
	}
	else
	{
		m_preferences->security.certificate_file =
			m_remote_auth_external_certfile->get_filename();
		m_preferences->security.key_file =
			m_remote_auth_external_keyfile->get_filename();
	}

	hide();
}

void Gobby::InitialDialog::on_remote_allow_connections_toggled()
{
	m_remote_connections_grid->set_sensitive(
		m_remote_allow_connections->get_active());
}

void Gobby::InitialDialog::on_remote_require_password_toggled()
{
	m_password_grid->set_sensitive(
		m_remote_require_password->get_active());
}

void Gobby::InitialDialog::on_remote_auth_external_toggled()
{
	m_certificate_grid->set_sensitive(
		m_remote_auth_external->get_active());
}