| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2013 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 <string.h> |
| 9 |
|
| 10 |
#include <glib.h> |
| 11 |
#include <glib/gi18n-lib.h> |
| 12 |
#include <glib/gstdio.h> |
| 13 |
|
| 14 |
#include "e-ews-connection-utils.h" |
| 15 |
#include "camel-ews-settings.h" |
| 16 |
|
| 17 |
void |
| 18 |
e_ews_connection_utils_set_user_agent_header (SoupMessage *message, |
| 19 |
CamelEwsSettings *settings) |
| 20 |
{ |
| 21 |
g_return_if_fail (SOUP_IS_MESSAGE (message)); |
| 22 |
g_return_if_fail (CAMEL_IS_EWS_SETTINGS (settings)); |
| 23 |
|
| 24 |
if (camel_ews_settings_get_override_user_agent (settings)) { |
| 25 |
gchar *user_agent; |
| 26 |
|
| 27 |
user_agent = camel_ews_settings_dup_user_agent (settings); |
| 28 |
|
| 29 |
if (user_agent && *user_agent) { |
| 30 |
soup_message_headers_replace (soup_message_get_request_headers (message), "User-Agent", user_agent); |
| 31 |
} |
| 32 |
|
| 33 |
g_free (user_agent); |
| 34 |
} else { |
| 35 |
soup_message_headers_replace (soup_message_get_request_headers (message), "User-Agent", "Evolution/" VERSION); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static gpointer |
| 40 |
ews_unref_in_thread_func (gpointer data) |
| 41 |
{ |
| 42 |
g_object_unref (G_OBJECT (data)); |
| 43 |
|
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
e_ews_connection_utils_unref_in_thread (gpointer object) |
| 49 |
{ |
| 50 |
GThread *thread; |
| 51 |
|
| 52 |
g_return_if_fail (G_IS_OBJECT (object)); |
| 53 |
|
| 54 |
thread = g_thread_new (NULL, ews_unref_in_thread_func, object); |
| 55 |
g_thread_unref (thread); |
| 56 |
} |
| 57 |
|
| 58 |
/* Do not call this directly; use E_EWS_CONNECTION_UTILS_CHECK_ELEMENT macro instead. */ |
| 59 |
gboolean |
| 60 |
e_ews_connection_utils_check_element (const gchar *function_name, |
| 61 |
const gchar *element_name, |
| 62 |
const gchar *expected_name) |
| 63 |
{ |
| 64 |
g_return_val_if_fail (function_name != NULL, FALSE); |
| 65 |
g_return_val_if_fail (element_name != NULL, FALSE); |
| 66 |
g_return_val_if_fail (expected_name != NULL, FALSE); |
| 67 |
|
| 68 |
if (!g_str_equal (element_name, expected_name)) { |
| 69 |
g_warning ( |
| 70 |
"%s: Expected <%s> but got <%s>", |
| 71 |
function_name, expected_name, element_name); |
| 72 |
return FALSE; |
| 73 |
} |
| 74 |
|
| 75 |
return TRUE; |
| 76 |
} |
| 77 |
|
| 78 |
static gboolean force_off_ntlm_auth_check = FALSE; |
| 79 |
|
| 80 |
static gboolean |
| 81 |
ews_connect_check_ntlm_available (void) |
| 82 |
{ |
| 83 |
#ifndef G_OS_WIN32 |
| 84 |
const gchar *helper; |
| 85 |
CamelStream *stream; |
| 86 |
const gchar *cp; |
| 87 |
const gchar *user; |
| 88 |
gchar buf[1024]; |
| 89 |
gsize s; |
| 90 |
gchar *command; |
| 91 |
gint ret; |
| 92 |
|
| 93 |
if (force_off_ntlm_auth_check) |
| 94 |
return FALSE; |
| 95 |
|
| 96 |
/* We are attempting to predict what libsoup will do. */ |
| 97 |
helper = g_getenv ("SOUP_NTLM_AUTH_DEBUG"); |
| 98 |
if (!helper) |
| 99 |
helper = "/usr/bin/ntlm_auth"; |
| 100 |
else if (!helper[0]) |
| 101 |
return FALSE; |
| 102 |
|
| 103 |
if (g_access (helper, X_OK)) |
| 104 |
return FALSE; |
| 105 |
|
| 106 |
user = g_getenv ("NTLMUSER"); |
| 107 |
if (!user) |
| 108 |
user = g_get_user_name(); |
| 109 |
|
| 110 |
cp = strpbrk (user, "\\/"); |
| 111 |
if (cp != NULL) { |
| 112 |
command = g_strdup_printf ( |
| 113 |
"%s --helper-protocol ntlmssp-client-1 " |
| 114 |
"--use-cached-creds --username '%s' " |
| 115 |
"--domain '%.*s'", helper, |
| 116 |
cp + 1, (gint)(cp - user), user); |
| 117 |
} else { |
| 118 |
command = g_strdup_printf ( |
| 119 |
"%s --helper-protocol ntlmssp-client-1 " |
| 120 |
"--use-cached-creds --username '%s'", |
| 121 |
helper, user); |
| 122 |
} |
| 123 |
|
| 124 |
stream = camel_stream_process_new (); |
| 125 |
|
| 126 |
ret = camel_stream_process_connect (CAMEL_STREAM_PROCESS (stream), |
| 127 |
command, NULL, NULL); |
| 128 |
|
| 129 |
g_free (command); |
| 130 |
|
| 131 |
if (ret) { |
| 132 |
g_object_unref (stream); |
| 133 |
return FALSE; |
| 134 |
} |
| 135 |
|
| 136 |
if (camel_stream_write_string (stream, "YR\n", NULL, NULL) < 0) { |
| 137 |
g_object_unref (stream); |
| 138 |
return FALSE; |
| 139 |
} |
| 140 |
|
| 141 |
s = camel_stream_read (stream, buf, sizeof (buf), NULL, NULL); |
| 142 |
if (s < 4) { |
| 143 |
g_object_unref (stream); |
| 144 |
return FALSE; |
| 145 |
} |
| 146 |
|
| 147 |
if (buf[0] != 'Y' || buf[1] != 'R' || buf[2] != ' ' || buf[s - 1] != '\n') { |
| 148 |
g_object_unref (stream); |
| 149 |
return FALSE; |
| 150 |
} |
| 151 |
|
| 152 |
g_object_unref (stream); |
| 153 |
|
| 154 |
return TRUE; |
| 155 |
#else |
| 156 |
/* Win32 should be able to use SSPI here. */ |
| 157 |
return FALSE; |
| 158 |
#endif |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
e_ews_connection_utils_force_off_ntlm_auth_check (void) |
| 163 |
{ |
| 164 |
force_off_ntlm_auth_check = TRUE; |
| 165 |
} |
| 166 |
|
| 167 |
/* Should we bother to attempt a connection without a password? Remember, |
| 168 |
* this is *purely* an optimisation to avoid that extra round-trip if we |
| 169 |
* *KNOW* it's going to fail. So if unsure, return TRUE to avoid pestering |
| 170 |
* the user for a password which might not even get used. |
| 171 |
* |
| 172 |
* We *have* to handle the case where the passwordless attempt fails |
| 173 |
* and we have to fall back to asking for a password anyway. */ |
| 174 |
gboolean |
| 175 |
e_ews_connection_utils_get_without_password (CamelEwsSettings *ews_settings) |
| 176 |
{ |
| 177 |
switch (camel_ews_settings_get_auth_mechanism (ews_settings)) { |
| 178 |
case EWS_AUTH_TYPE_GSSAPI: |
| 179 |
case EWS_AUTH_TYPE_OAUTH2: |
| 180 |
return TRUE; |
| 181 |
|
| 182 |
case EWS_AUTH_TYPE_NTLM: |
| 183 |
return ews_connect_check_ntlm_available (); |
| 184 |
|
| 185 |
case EWS_AUTH_TYPE_BASIC: |
| 186 |
return FALSE; |
| 187 |
|
| 188 |
/* No default: case (which should never be used anyway). That |
| 189 |
* means the compiler will warn if we ever add a new mechanism |
| 190 |
* to the enum and don't handle it here. */ |
| 191 |
} |
| 192 |
|
| 193 |
return FALSE; |
| 194 |
} |
| 195 |
|
| 196 |
void |
| 197 |
e_ews_connection_utils_expired_password_to_error (const gchar *service_url, |
| 198 |
GError **error) |
| 199 |
{ |
| 200 |
if (!error) |
| 201 |
return; |
| 202 |
|
| 203 |
if (service_url) { |
| 204 |
g_set_error (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_PASSWORDEXPIRED, |
| 205 |
_("Password expired. Change password at “%s”."), service_url); |
| 206 |
} else { |
| 207 |
g_set_error_literal (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_PASSWORDEXPIRED, |
| 208 |
_("Password expired.")); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
gboolean |
| 213 |
e_ews_connection_utils_check_x_ms_credential_headers (SoupMessage *message, |
| 214 |
gint *out_expire_in_days, |
| 215 |
gboolean *out_expired, |
| 216 |
gchar **out_service_url) |
| 217 |
{ |
| 218 |
SoupMessageHeaders *response_headers; |
| 219 |
gboolean any_found = FALSE; |
| 220 |
const gchar *header; |
| 221 |
|
| 222 |
if (!message) |
| 223 |
return FALSE; |
| 224 |
|
| 225 |
response_headers = soup_message_get_response_headers (message); |
| 226 |
|
| 227 |
if (!response_headers) |
| 228 |
return FALSE; |
| 229 |
|
| 230 |
header = soup_message_headers_get_list (response_headers, "X-MS-Credential-Service-CredExpired"); |
| 231 |
if (header && g_ascii_strcasecmp (header, "true") == 0) { |
| 232 |
any_found = TRUE; |
| 233 |
|
| 234 |
if (out_expired) |
| 235 |
*out_expired = TRUE; |
| 236 |
} |
| 237 |
|
| 238 |
header = soup_message_headers_get_list (response_headers, "X-MS-Credentials-Expire"); |
| 239 |
if (header) { |
| 240 |
gint in_days; |
| 241 |
|
| 242 |
in_days = g_ascii_strtoll (header, NULL, 10); |
| 243 |
if (in_days <= 30 && in_days >= 0) { |
| 244 |
any_found = TRUE; |
| 245 |
|
| 246 |
if (out_expire_in_days) |
| 247 |
*out_expire_in_days = in_days; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if (any_found && out_service_url) { |
| 252 |
header = soup_message_headers_get_list (response_headers, "X-MS-Credential-Service-Url"); |
| 253 |
|
| 254 |
*out_service_url = g_strdup (header); |
| 255 |
} |
| 256 |
|
| 257 |
return any_found; |
| 258 |
} |