| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2012 Red Hat, Inc. (www.redhat.com) |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "evolution-ews-config.h" |
| 7 |
|
| 8 |
#include "e-mail-config-ews-autodiscover.h" |
| 9 |
|
| 10 |
#include <glib/gi18n-lib.h> |
| 11 |
|
| 12 |
#include <libedataserverui/libedataserverui.h> |
| 13 |
#include <shell/e-shell.h> |
| 14 |
#include <mail/e-mail-config-service-page.h> |
| 15 |
|
| 16 |
#include "common/e-ews-connection.h" |
| 17 |
#include "common/e-ews-connection-utils.h" |
| 18 |
|
| 19 |
typedef struct _AsyncContext AsyncContext; |
| 20 |
|
| 21 |
struct _EMailConfigEwsAutodiscoverPrivate { |
| 22 |
EMailConfigServiceBackend *backend; |
| 23 |
}; |
| 24 |
|
| 25 |
struct _AsyncContext { |
| 26 |
EMailConfigEwsAutodiscover *autodiscover; |
| 27 |
EActivity *activity; |
| 28 |
ESource *source; |
| 29 |
CamelEwsSettings *ews_settings; |
| 30 |
gchar *email_address; |
| 31 |
gchar *certificate_pem; |
| 32 |
GTlsCertificateFlags certificate_errors; |
| 33 |
}; |
| 34 |
|
| 35 |
enum { |
| 36 |
PROP_0, |
| 37 |
PROP_BACKEND |
| 38 |
}; |
| 39 |
|
| 40 |
G_DEFINE_DYNAMIC_TYPE_EXTENDED (EMailConfigEwsAutodiscover, e_mail_config_ews_autodiscover, GTK_TYPE_BUTTON, 0, |
| 41 |
G_ADD_PRIVATE_DYNAMIC (EMailConfigEwsAutodiscover)) |
| 42 |
|
| 43 |
static void |
| 44 |
async_context_free (gpointer ptr) |
| 45 |
{ |
| 46 |
AsyncContext *async_context = ptr; |
| 47 |
|
| 48 |
if (!async_context) |
| 49 |
return; |
| 50 |
|
| 51 |
g_clear_object (&async_context->autodiscover); |
| 52 |
g_clear_object (&async_context->activity); |
| 53 |
g_clear_object (&async_context->source); |
| 54 |
g_clear_object (&async_context->ews_settings); |
| 55 |
g_free (async_context->email_address); |
| 56 |
g_free (async_context->certificate_pem); |
| 57 |
|
| 58 |
g_slice_free (AsyncContext, async_context); |
| 59 |
} |
| 60 |
|
| 61 |
static gboolean |
| 62 |
mail_config_ews_autodiscover_finish (EMailConfigEwsAutodiscover *autodiscover, |
| 63 |
GAsyncResult *result, |
| 64 |
GError **error) |
| 65 |
{ |
| 66 |
g_return_val_if_fail (E_IS_MAIL_CONFIG_EWS_AUTODISCOVER (autodiscover), FALSE); |
| 67 |
g_return_val_if_fail (g_task_is_valid (result, autodiscover), FALSE); |
| 68 |
|
| 69 |
g_return_val_if_fail ( |
| 70 |
g_async_result_is_tagged ( |
| 71 |
result, mail_config_ews_autodiscover_finish), FALSE); |
| 72 |
|
| 73 |
return g_task_propagate_boolean (G_TASK (result), error); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
mail_config_ews_autodiscover_run (EMailConfigEwsAutodiscover *autodiscover); |
| 78 |
|
| 79 |
static void |
| 80 |
mail_config_ews_autodiscover_run_cb (GObject *source_object, |
| 81 |
GAsyncResult *result, |
| 82 |
gpointer user_data) |
| 83 |
{ |
| 84 |
AsyncContext *async_context = user_data; |
| 85 |
EMailConfigEwsAutodiscover *autodiscover; |
| 86 |
EAlertSink *alert_sink; |
| 87 |
GError *error = NULL; |
| 88 |
EMailConfigServiceBackend *backend; |
| 89 |
CamelSettings *settings; |
| 90 |
|
| 91 |
autodiscover = async_context->autodiscover; |
| 92 |
alert_sink = e_activity_get_alert_sink (async_context->activity); |
| 93 |
|
| 94 |
mail_config_ews_autodiscover_finish (E_MAIL_CONFIG_EWS_AUTODISCOVER (source_object), result, &error); |
| 95 |
|
| 96 |
backend = e_mail_config_ews_autodiscover_get_backend (autodiscover); |
| 97 |
settings = e_mail_config_service_backend_get_settings (backend); |
| 98 |
/* |
| 99 |
* And unstop since we are back to the main thread. |
| 100 |
*/ |
| 101 |
g_object_thaw_notify (G_OBJECT (settings)); |
| 102 |
|
| 103 |
if (e_activity_handle_cancellation (async_context->activity, error)) { |
| 104 |
/* Do nothing, just free the error below */ |
| 105 |
} else if (g_error_matches (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE) && |
| 106 |
async_context->certificate_pem && *async_context->certificate_pem && async_context->certificate_errors) { |
| 107 |
ETrustPromptResponse response; |
| 108 |
GtkWidget *parent; |
| 109 |
const gchar *host; |
| 110 |
|
| 111 |
parent = gtk_widget_get_toplevel (GTK_WIDGET (autodiscover)); |
| 112 |
if (!GTK_IS_WINDOW (parent)) |
| 113 |
parent = NULL; |
| 114 |
|
| 115 |
host = camel_network_settings_get_host (CAMEL_NETWORK_SETTINGS (settings)); |
| 116 |
|
| 117 |
response = e_trust_prompt_run_modal (parent ? GTK_WINDOW (parent) : NULL, |
| 118 |
E_SOURCE_EXTENSION_COLLECTION, _("Exchange Web Services"), |
| 119 |
host, async_context->certificate_pem, async_context->certificate_errors, |
| 120 |
error->message); |
| 121 |
|
| 122 |
g_clear_error (&error); |
| 123 |
|
| 124 |
if (response != E_TRUST_PROMPT_RESPONSE_UNKNOWN) { |
| 125 |
GTlsCertificate *certificate; |
| 126 |
|
| 127 |
certificate = g_tls_certificate_new_from_pem (async_context->certificate_pem, -1, &error); |
| 128 |
if (certificate) { |
| 129 |
ESourceWebdav *extension_webdav; |
| 130 |
|
| 131 |
extension_webdav = e_source_get_extension (async_context->source, E_SOURCE_EXTENSION_WEBDAV_BACKEND); |
| 132 |
|
| 133 |
e_source_webdav_update_ssl_trust (extension_webdav, host, certificate, response); |
| 134 |
|
| 135 |
g_object_unref (certificate); |
| 136 |
} |
| 137 |
|
| 138 |
if (error) { |
| 139 |
e_alert_submit ( |
| 140 |
alert_sink, |
| 141 |
"ews:autodiscovery-error", |
| 142 |
error->message, NULL); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (response == E_TRUST_PROMPT_RESPONSE_ACCEPT || |
| 147 |
response == E_TRUST_PROMPT_RESPONSE_ACCEPT_TEMPORARILY) { |
| 148 |
mail_config_ews_autodiscover_run (autodiscover); |
| 149 |
} |
| 150 |
} else if (error != NULL) { |
| 151 |
e_alert_submit ( |
| 152 |
alert_sink, |
| 153 |
"ews:autodiscovery-error", |
| 154 |
error->message, NULL); |
| 155 |
} |
| 156 |
|
| 157 |
gtk_widget_set_sensitive (GTK_WIDGET (autodiscover), TRUE); |
| 158 |
|
| 159 |
g_clear_error (&error); |
| 160 |
} |
| 161 |
|
| 162 |
static gboolean |
| 163 |
mail_config_ews_autodiscover_sync (ECredentialsPrompter *prompter, |
| 164 |
ESource *source, |
| 165 |
const ENamedParameters *credentials, |
| 166 |
gboolean *out_authenticated, |
| 167 |
gpointer user_data, |
| 168 |
GCancellable *cancellable, |
| 169 |
GError **error) |
| 170 |
{ |
| 171 |
AsyncContext *async_context = user_data; |
| 172 |
GError *local_error = NULL; |
| 173 |
gboolean res = TRUE; |
| 174 |
|
| 175 |
e_ews_autodiscover_ws_url_sync (source, |
| 176 |
async_context->ews_settings, async_context->email_address, |
| 177 |
credentials && e_named_parameters_get (credentials, E_SOURCE_CREDENTIAL_PASSWORD) ? |
| 178 |
e_named_parameters_get (credentials, E_SOURCE_CREDENTIAL_PASSWORD) : "", |
| 179 |
&async_context->certificate_pem, &async_context->certificate_errors, |
| 180 |
cancellable, &local_error); |
| 181 |
|
| 182 |
if (local_error == NULL) { |
| 183 |
*out_authenticated = TRUE; |
| 184 |
} else if (g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_UNAUTHORIZED)) { |
| 185 |
*out_authenticated = FALSE; |
| 186 |
g_error_free (local_error); |
| 187 |
} else { |
| 188 |
res = FALSE; |
| 189 |
g_propagate_error (error, local_error); |
| 190 |
} |
| 191 |
|
| 192 |
return res; |
| 193 |
} |
| 194 |
|
| 195 |
static void |
| 196 |
mail_config_ews_autodiscover_run_thread (GTask *task, |
| 197 |
gpointer source_object, |
| 198 |
gpointer task_data, |
| 199 |
GCancellable *cancellable) |
| 200 |
{ |
| 201 |
AsyncContext *async_context = task_data; |
| 202 |
GError *local_error = NULL; |
| 203 |
gboolean success = FALSE; |
| 204 |
|
| 205 |
if (!g_cancellable_set_error_if_cancelled (cancellable, &local_error) && !local_error) { |
| 206 |
gboolean without_password; |
| 207 |
|
| 208 |
without_password = e_ews_connection_utils_get_without_password (async_context->ews_settings); |
| 209 |
if (without_password) { |
| 210 |
success = e_ews_autodiscover_ws_url_sync (async_context->source, |
| 211 |
async_context->ews_settings, async_context->email_address, "", |
| 212 |
&async_context->certificate_pem, &async_context->certificate_errors, |
| 213 |
cancellable, &local_error); |
| 214 |
} |
| 215 |
|
| 216 |
if (!without_password || g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_UNAUTHORIZED)) { |
| 217 |
EShell *shell; |
| 218 |
|
| 219 |
e_ews_connection_utils_force_off_ntlm_auth_check (); |
| 220 |
g_clear_error (&local_error); |
| 221 |
|
| 222 |
shell = e_shell_get_default (); |
| 223 |
|
| 224 |
success = e_credentials_prompter_loop_prompt_sync (e_shell_get_credentials_prompter (shell), |
| 225 |
async_context->source, E_CREDENTIALS_PROMPTER_PROMPT_FLAG_ALLOW_SOURCE_SAVE, |
| 226 |
mail_config_ews_autodiscover_sync, async_context, cancellable, &local_error); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if (local_error != NULL) { |
| 231 |
g_task_return_error (task, local_error); |
| 232 |
} else { |
| 233 |
g_task_return_boolean (task, success); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static void |
| 238 |
mail_config_ews_autodiscover_run (EMailConfigEwsAutodiscover *autodiscover) |
| 239 |
{ |
| 240 |
EActivity *activity; |
| 241 |
EMailConfigServicePage *page; |
| 242 |
EMailConfigServiceBackend *backend; |
| 243 |
CamelSettings *settings; |
| 244 |
ESource *source; |
| 245 |
GCancellable *cancellable; |
| 246 |
AsyncContext *async_context; |
| 247 |
GTask *task; |
| 248 |
|
| 249 |
backend = e_mail_config_ews_autodiscover_get_backend (autodiscover); |
| 250 |
page = e_mail_config_service_backend_get_page (backend); |
| 251 |
source = e_mail_config_service_backend_get_collection (backend); |
| 252 |
settings = e_mail_config_service_backend_get_settings (backend); |
| 253 |
|
| 254 |
if (!source) |
| 255 |
source = e_mail_config_service_backend_get_source (backend); |
| 256 |
|
| 257 |
activity = e_mail_config_activity_page_new_activity (E_MAIL_CONFIG_ACTIVITY_PAGE (page)); |
| 258 |
cancellable = e_activity_get_cancellable (activity); |
| 259 |
|
| 260 |
e_activity_set_text (activity, _("Querying Autodiscover service")); |
| 261 |
|
| 262 |
gtk_widget_set_sensitive (GTK_WIDGET (autodiscover), FALSE); |
| 263 |
|
| 264 |
async_context = g_slice_new0 (AsyncContext); |
| 265 |
async_context->autodiscover = g_object_ref (autodiscover); |
| 266 |
async_context->activity = activity; /* takes ownership */ |
| 267 |
async_context->source = g_object_ref (source); |
| 268 |
async_context->ews_settings = CAMEL_EWS_SETTINGS (g_object_ref (settings)); |
| 269 |
async_context->email_address = g_strdup (e_mail_config_service_page_get_email_address (page)); |
| 270 |
async_context->certificate_pem = NULL; |
| 271 |
async_context->certificate_errors = 0; |
| 272 |
|
| 273 |
/* |
| 274 |
* The GTask will be run in a new thread, which will invoke |
| 275 |
* camel_ews_settings_set_{oaburl,hosturl}(), emiting signals that |
| 276 |
* are bound to GTK+ UI signals, causing GTK+ calls in this |
| 277 |
* secondary thread and consequently a crash. To avoid this, let's stop |
| 278 |
* the property changes notifications while we are not in the main thread. |
| 279 |
*/ |
| 280 |
g_object_freeze_notify (G_OBJECT (settings)); |
| 281 |
|
| 282 |
task = g_task_new (autodiscover, cancellable, mail_config_ews_autodiscover_run_cb, async_context); |
| 283 |
g_task_set_source_tag (task, mail_config_ews_autodiscover_finish); |
| 284 |
g_task_set_task_data (task, async_context, async_context_free); |
| 285 |
|
| 286 |
g_task_run_in_thread (task, mail_config_ews_autodiscover_run_thread); |
| 287 |
|
| 288 |
g_object_unref (task); |
| 289 |
} |
| 290 |
|
| 291 |
static void |
| 292 |
mail_config_ews_autodiscover_set_backend (EMailConfigEwsAutodiscover *autodiscover, |
| 293 |
EMailConfigServiceBackend *backend) |
| 294 |
{ |
| 295 |
g_return_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend)); |
| 296 |
g_return_if_fail (autodiscover->priv->backend == NULL); |
| 297 |
|
| 298 |
autodiscover->priv->backend = g_object_ref (backend); |
| 299 |
} |
| 300 |
|
| 301 |
static void |
| 302 |
mail_config_ews_autodiscover_set_property (GObject *object, |
| 303 |
guint property_id, |
| 304 |
const GValue *value, |
| 305 |
GParamSpec *pspec) |
| 306 |
{ |
| 307 |
switch (property_id) { |
| 308 |
case PROP_BACKEND: |
| 309 |
mail_config_ews_autodiscover_set_backend ( |
| 310 |
E_MAIL_CONFIG_EWS_AUTODISCOVER (object), |
| 311 |
g_value_get_object (value)); |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 316 |
} |
| 317 |
|
| 318 |
static void |
| 319 |
mail_config_ews_autodiscover_get_property (GObject *object, |
| 320 |
guint property_id, |
| 321 |
GValue *value, |
| 322 |
GParamSpec *pspec) |
| 323 |
{ |
| 324 |
switch (property_id) { |
| 325 |
case PROP_BACKEND: |
| 326 |
g_value_set_object ( |
| 327 |
value, |
| 328 |
e_mail_config_ews_autodiscover_get_backend ( |
| 329 |
E_MAIL_CONFIG_EWS_AUTODISCOVER (object))); |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 334 |
} |
| 335 |
|
| 336 |
static void |
| 337 |
mail_config_ews_autodiscover_dispose (GObject *object) |
| 338 |
{ |
| 339 |
EMailConfigEwsAutodiscover *autodiscover = E_MAIL_CONFIG_EWS_AUTODISCOVER (object); |
| 340 |
|
| 341 |
g_clear_object (&autodiscover->priv->backend); |
| 342 |
|
| 343 |
/* Chain up to parent's dispose() method. */ |
| 344 |
G_OBJECT_CLASS (e_mail_config_ews_autodiscover_parent_class)->dispose (object); |
| 345 |
} |
| 346 |
|
| 347 |
static void |
| 348 |
mail_config_ews_autodiscover_constructed (GObject *object) |
| 349 |
{ |
| 350 |
GtkButton *button; |
| 351 |
|
| 352 |
button = GTK_BUTTON (object); |
| 353 |
gtk_button_set_label (button, _("Fetch _URL")); |
| 354 |
gtk_button_set_use_underline (button, TRUE); |
| 355 |
|
| 356 |
/* Chain up tp parent's constructed() method. */ |
| 357 |
G_OBJECT_CLASS (e_mail_config_ews_autodiscover_parent_class)->constructed (object); |
| 358 |
} |
| 359 |
|
| 360 |
static void |
| 361 |
mail_config_ews_autodiscover_clicked (GtkButton *button) |
| 362 |
{ |
| 363 |
EMailConfigEwsAutodiscover *autodiscover; |
| 364 |
|
| 365 |
autodiscover = E_MAIL_CONFIG_EWS_AUTODISCOVER (button); |
| 366 |
|
| 367 |
mail_config_ews_autodiscover_run (autodiscover); |
| 368 |
} |
| 369 |
|
| 370 |
static void |
| 371 |
e_mail_config_ews_autodiscover_class_init (EMailConfigEwsAutodiscoverClass *class) |
| 372 |
{ |
| 373 |
GObjectClass *object_class; |
| 374 |
GtkButtonClass *button_class; |
| 375 |
|
| 376 |
object_class = G_OBJECT_CLASS (class); |
| 377 |
object_class->set_property = mail_config_ews_autodiscover_set_property; |
| 378 |
object_class->get_property = mail_config_ews_autodiscover_get_property; |
| 379 |
object_class->dispose = mail_config_ews_autodiscover_dispose; |
| 380 |
object_class->constructed = mail_config_ews_autodiscover_constructed; |
| 381 |
|
| 382 |
button_class = GTK_BUTTON_CLASS (class); |
| 383 |
button_class->clicked = mail_config_ews_autodiscover_clicked; |
| 384 |
|
| 385 |
g_object_class_install_property ( |
| 386 |
object_class, |
| 387 |
PROP_BACKEND, |
| 388 |
g_param_spec_object ( |
| 389 |
"backend", |
| 390 |
"Backend", |
| 391 |
"Mail configuration backend", |
| 392 |
E_TYPE_MAIL_CONFIG_SERVICE_BACKEND, |
| 393 |
G_PARAM_READWRITE | |
| 394 |
G_PARAM_CONSTRUCT_ONLY)); |
| 395 |
} |
| 396 |
|
| 397 |
static void |
| 398 |
e_mail_config_ews_autodiscover_class_finalize (EMailConfigEwsAutodiscoverClass *class) |
| 399 |
{ |
| 400 |
} |
| 401 |
|
| 402 |
static void |
| 403 |
e_mail_config_ews_autodiscover_init (EMailConfigEwsAutodiscover *autodiscover) |
| 404 |
{ |
| 405 |
autodiscover->priv = e_mail_config_ews_autodiscover_get_instance_private (autodiscover); |
| 406 |
} |
| 407 |
|
| 408 |
void |
| 409 |
e_mail_config_ews_autodiscover_type_register (GTypeModule *type_module) |
| 410 |
{ |
| 411 |
/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration |
| 412 |
* function, so we have to wrap it with a public function in |
| 413 |
* order to register types from a separate compilation unit. */ |
| 414 |
e_mail_config_ews_autodiscover_register_type (type_module); |
| 415 |
} |
| 416 |
|
| 417 |
GtkWidget * |
| 418 |
e_mail_config_ews_autodiscover_new (EMailConfigServiceBackend *backend) |
| 419 |
{ |
| 420 |
g_return_val_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend), NULL); |
| 421 |
|
| 422 |
return g_object_new ( |
| 423 |
E_TYPE_MAIL_CONFIG_EWS_AUTODISCOVER, |
| 424 |
"backend", backend, NULL); |
| 425 |
} |
| 426 |
|
| 427 |
EMailConfigServiceBackend * |
| 428 |
e_mail_config_ews_autodiscover_get_backend (EMailConfigEwsAutodiscover *autodiscover) |
| 429 |
{ |
| 430 |
g_return_val_if_fail ( |
| 431 |
E_IS_MAIL_CONFIG_EWS_AUTODISCOVER (autodiscover), NULL); |
| 432 |
|
| 433 |
return autodiscover->priv->backend; |
| 434 |
} |
| 435 |
|