2 * @file sipe-ews-autodiscover.c
6 * Copyright (C) 2013-2014 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Specification references:
25 * - POX: plain old XML autodiscover
26 * - [MS-OXDSCLI]: http://msdn.microsoft.com/en-us/library/cc463896.aspx
27 * - Autdiscover for Exchange:
28 * http://msdn.microsoft.com/en-us/library/office/jj900169.aspx
29 * - POX autodiscover: http://msdn.microsoft.com/en-us/library/office/aa581522.aspx
30 * - POX redirect: http://msdn.microsoft.com/en-us/library/office/dn467392.aspx
37 #include "sipe-backend.h"
38 #include "sipe-common.h"
39 #include "sipe-core.h"
40 #include "sipe-core-private.h"
41 #include "sipe-ews-autodiscover.h"
42 #include "sipe-http.h"
43 #include "sipe-utils.h"
46 struct sipe_ews_autodiscover_cb
{
47 sipe_ews_autodiscover_callback
*cb
;
51 struct autodiscover_method
{
52 const gchar
*template;
56 struct sipe_ews_autodiscover
{
57 struct sipe_ews_autodiscover_data
*data
;
58 struct sipe_http_request
*request
;
61 const struct autodiscover_method
*method
;
66 static void sipe_ews_autodiscover_complete(struct sipe_core_private
*sipe_private
,
67 struct sipe_ews_autodiscover_data
*ews_data
)
69 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
70 GSList
*entry
= sea
->callbacks
;
73 struct sipe_ews_autodiscover_cb
*sea_cb
= entry
->data
;
74 sea_cb
->cb(sipe_private
, ews_data
, sea_cb
->cb_data
);
78 g_slist_free(sea
->callbacks
);
79 sea
->callbacks
= NULL
;
80 sea
->completed
= TRUE
;
83 static void sipe_ews_autodiscover_request(struct sipe_core_private
*sipe_private
,
84 gboolean next_method
);
85 static gboolean
sipe_ews_autodiscover_url(struct sipe_core_private
*sipe_private
,
87 static void sipe_ews_autodiscover_parse(struct sipe_core_private
*sipe_private
,
90 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
91 struct sipe_ews_autodiscover_data
*ews_data
= sea
->data
=
92 g_new0(struct sipe_ews_autodiscover_data
, 1);
93 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
94 const sipe_xml
*account
= sipe_xml_child(xml
, "Response/Account");
95 gboolean complete
= TRUE
;
97 /* valid POX autodiscover response? */
101 /* POX autodiscover settings? */
102 if ((node
= sipe_xml_child(account
, "Protocol")) != NULL
) {
104 /* Autodiscover/Response/User/LegacyDN (requires trimming) */
105 gchar
*tmp
= sipe_xml_data(sipe_xml_child(xml
,
106 "Response/User/LegacyDN"));
108 ews_data
->legacy_dn
= g_strstrip(tmp
);
110 /* extract settings */
111 for (; node
; node
= sipe_xml_twin(node
)) {
112 gchar
*type
= sipe_xml_data(sipe_xml_child(node
,
115 if (sipe_strequal("EXCH", type
)) {
118 #define _URL(name, field) \
120 ews_data->field = sipe_xml_data(sipe_xml_child(node, #name)); \
121 SIPE_DEBUG_INFO("sipe_ews_autodiscover_parse: " #field " = '%s'", \
122 ews_data->field ? ews_data->field : "<NOT FOUND>"); \
126 _URL(EwsUrl
, ews_url
);
127 _URL(OABUrl
, oab_url
);
128 _URL(OOFUrl
, oof_url
);
137 /* POX autodiscover redirect to new email address? */
138 } else if ((node
= sipe_xml_child(account
, "RedirectAddr")) != NULL
) {
139 gchar
*addr
= sipe_xml_data(node
);
142 * Sanity checks for new email address:
143 * - must contain a "@" character
144 * - must be different from current address
146 if (addr
&& strchr(addr
, '@') &&
147 !sipe_strequal(sea
->email
, addr
)) {
150 addr
= NULL
; /* sea takes ownership */
152 SIPE_DEBUG_INFO("sipe_ews_autodiscover_parse: restarting with email address '%s'",
155 /* restart process with new email address */
158 sipe_ews_autodiscover_request(sipe_private
,
163 /* POX autodiscover redirect to new URL? */
164 } else if ((node
= sipe_xml_child(account
, "RedirectUrl")) != NULL
) {
165 gchar
*url
= sipe_xml_data(node
);
167 if (!is_empty(url
)) {
168 SIPE_DEBUG_INFO("sipe_ews_autodiscover_parse: redirected to URL '%s'",
170 complete
= !sipe_ews_autodiscover_url(sipe_private
,
175 /* ignore all other POX autodiscover responses */
177 SIPE_DEBUG_ERROR_NOFORMAT("sipe_ews_autodiscover_parse: unknown response detected");
183 sipe_ews_autodiscover_complete(sipe_private
, ews_data
);
186 static void sipe_ews_autodiscover_response(struct sipe_core_private
*sipe_private
,
188 SIPE_UNUSED_PARAMETER GSList
*headers
,
192 struct sipe_ews_autodiscover
*sea
= data
;
197 case SIPE_HTTP_STATUS_OK
:
199 sipe_ews_autodiscover_parse(sipe_private
, body
);
201 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
204 case SIPE_HTTP_STATUS_CLIENT_FORBIDDEN
:
206 * Authentication succeeded but we still weren't allowed to
207 * view the page. At least at our work place this error is
208 * temporary, i.e. the next access with the exact same
209 * authentication succeeds.
211 * Let's try again, but only once...
213 sipe_ews_autodiscover_request(sipe_private
, !sea
->retry
);
216 case SIPE_HTTP_STATUS_ABORTED
:
217 /* we are not allowed to generate new requests */
221 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
226 static gboolean
sipe_ews_autodiscover_url(struct sipe_core_private
*sipe_private
,
229 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
230 gchar
*body
= g_strdup_printf("<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">"
232 " <EMailAddress>%s</EMailAddress>"
233 " <AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>"
238 SIPE_DEBUG_INFO("sipe_ews_autodiscover_url: trying '%s'", url
);
240 sea
->request
= sipe_http_request_post(sipe_private
,
245 sipe_ews_autodiscover_response
,
250 sipe_core_email_authentication(sipe_private
,
252 sipe_http_request_allow_redirect(sea
->request
);
253 sipe_http_request_ready(sea
->request
);
260 static void sipe_ews_autodiscover_redirect_response(struct sipe_core_private
*sipe_private
,
263 SIPE_UNUSED_PARAMETER
const gchar
*body
,
266 struct sipe_ews_autodiscover
*sea
= data
;
267 gboolean failed
= TRUE
;
271 /* Start attempt with URL from redirect (3xx) response */
272 if ((status
>= SIPE_HTTP_STATUS_REDIRECTION
) &&
273 (status
< SIPE_HTTP_STATUS_CLIENT_ERROR
)) {
274 const gchar
*location
= sipe_utils_nameval_find_instance(headers
,
278 failed
= !sipe_ews_autodiscover_url(sipe_private
,
283 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
286 static gboolean
sipe_ews_autodiscover_redirect(struct sipe_core_private
*sipe_private
,
289 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
291 SIPE_DEBUG_INFO("sipe_ews_autodiscover_redirect: trying '%s'", url
);
293 sea
->request
= sipe_http_request_get(sipe_private
,
296 sipe_ews_autodiscover_redirect_response
,
300 sipe_http_request_ready(sea
->request
);
307 static void sipe_ews_autodiscover_request(struct sipe_core_private
*sipe_private
,
308 gboolean next_method
)
310 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
311 static const struct autodiscover_method
const methods
[] = {
312 { "https://Autodiscover.%s/Autodiscover/Autodiscover.xml", FALSE
},
313 { "http://Autodiscover.%s/Autodiscover/Autodiscover.xml", TRUE
},
314 { "http://Autodiscover.%s/Autodiscover/Autodiscover.xml", FALSE
},
315 { "https://%s/Autodiscover/Autodiscover.xml", FALSE
},
319 sea
->retry
= next_method
;
324 sea
->method
= methods
;
326 if (sea
->method
->template) {
327 gchar
*url
= g_strdup_printf(sea
->method
->template,
328 strstr(sea
->email
, "@") + 1);
330 if (!(sea
->method
->redirect
?
331 sipe_ews_autodiscover_redirect(sipe_private
, url
) :
332 sipe_ews_autodiscover_url(sipe_private
, url
)))
333 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
338 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_autodiscover_request: no more methods to try!");
339 sipe_ews_autodiscover_complete(sipe_private
, NULL
);
343 void sipe_ews_autodiscover_start(struct sipe_core_private
*sipe_private
,
344 sipe_ews_autodiscover_callback
*callback
,
345 gpointer callback_data
)
347 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
349 if (sea
->completed
) {
350 (*callback
)(sipe_private
, sea
->data
, callback_data
);
352 struct sipe_ews_autodiscover_cb
*sea_cb
= g_new(struct sipe_ews_autodiscover_cb
, 1);
353 sea_cb
->cb
= callback
;
354 sea_cb
->cb_data
= callback_data
;
355 sea
->callbacks
= g_slist_prepend(sea
->callbacks
, sea_cb
);
358 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
362 void sipe_ews_autodiscover_init(struct sipe_core_private
*sipe_private
)
364 struct sipe_ews_autodiscover
*sea
= g_new0(struct sipe_ews_autodiscover
, 1);
366 sea
->email
= g_strdup(sipe_private
->email
);
368 sipe_private
->ews_autodiscover
= sea
;
371 void sipe_ews_autodiscover_free(struct sipe_core_private
*sipe_private
)
373 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
374 struct sipe_ews_autodiscover_data
*ews_data
= sea
->data
;
375 sipe_ews_autodiscover_complete(sipe_private
, NULL
);
377 g_free((gchar
*)ews_data
->as_url
);
378 g_free((gchar
*)ews_data
->ews_url
);
379 g_free((gchar
*)ews_data
->legacy_dn
);
380 g_free((gchar
*)ews_data
->oab_url
);
381 g_free((gchar
*)ews_data
->oof_url
);