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 * - POX autodiscover: http://msdn.microsoft.com/en-us/library/office/aa581522.aspx
28 * - POX redirect: http://msdn.microsoft.com/en-us/library/office/dn467392.aspx
35 #include "sipe-backend.h"
36 #include "sipe-common.h"
37 #include "sipe-core.h"
38 #include "sipe-core-private.h"
39 #include "sipe-ews-autodiscover.h"
40 #include "sipe-http.h"
41 #include "sipe-utils.h"
44 struct sipe_ews_autodiscover_cb
{
45 sipe_ews_autodiscover_callback
*cb
;
49 struct sipe_ews_autodiscover
{
50 struct sipe_ews_autodiscover_data
*data
;
51 struct sipe_http_request
*request
;
54 const gchar
* const *method
;
59 static void sipe_ews_autodiscover_complete(struct sipe_core_private
*sipe_private
,
60 struct sipe_ews_autodiscover_data
*ews_data
)
62 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
63 GSList
*entry
= sea
->callbacks
;
66 struct sipe_ews_autodiscover_cb
*sea_cb
= entry
->data
;
67 sea_cb
->cb(sipe_private
, ews_data
, sea_cb
->cb_data
);
71 g_slist_free(sea
->callbacks
);
72 sea
->callbacks
= NULL
;
73 sea
->completed
= TRUE
;
76 static void sipe_ews_autodiscover_request(struct sipe_core_private
*sipe_private
,
77 gboolean next_method
);
78 static void sipe_ews_autodiscover_parse(struct sipe_core_private
*sipe_private
,
81 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
82 struct sipe_ews_autodiscover_data
*ews_data
= sea
->data
=
83 g_new0(struct sipe_ews_autodiscover_data
, 1);
84 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
85 const sipe_xml
*account
= sipe_xml_child(xml
, "Response/Account");
86 gboolean complete
= TRUE
;
88 /* valid POX autodiscover response? */
90 const sipe_xml
*node
= sipe_xml_child(account
, "Protocol");
95 /* Autodiscover/Response/User/LegacyDN (requires trimming) */
96 gchar
*tmp
= sipe_xml_data(sipe_xml_child(xml
,
97 "Response/User/LegacyDN"));
99 ews_data
->legacy_dn
= g_strstrip(tmp
);
101 /* extract settings */
102 for (; node
; node
= sipe_xml_twin(node
)) {
103 gchar
*type
= sipe_xml_data(sipe_xml_child(node
,
106 if (sipe_strequal("EXCH", type
)) {
109 #define _URL(name, field) \
111 ews_data->field = sipe_xml_data(sipe_xml_child(node, #name)); \
112 SIPE_DEBUG_INFO("sipe_ews_autodiscover_parse: " #field " = '%s'", \
113 ews_data->field ? ews_data->field : "<NOT FOUND>"); \
117 _URL(EwsUrl
, ews_url
);
118 _URL(OABUrl
, oab_url
);
119 _URL(OOFUrl
, oof_url
);
130 * POX autodiscover redirect email address?
131 * Make sure email address contains a "@" character.
132 * Make sure email address is different from current one.
134 gchar
*tmp
= sipe_xml_data(sipe_xml_child(account
,
136 if (tmp
&& strchr(tmp
, '@') &&
137 !sipe_strequal(sea
->email
, tmp
)) {
140 tmp
= NULL
; /* sea takes ownership */
142 SIPE_DEBUG_INFO("sipe_ews_autodiscover_parse: restarting with email address '%s'",
145 /* restart process with new email address */
148 sipe_ews_autodiscover_request(sipe_private
,
157 sipe_ews_autodiscover_complete(sipe_private
, ews_data
);
160 static void sipe_ews_autodiscover_response(struct sipe_core_private
*sipe_private
,
162 SIPE_UNUSED_PARAMETER GSList
*headers
,
166 struct sipe_ews_autodiscover
*sea
= data
;
171 case SIPE_HTTP_STATUS_OK
:
173 sipe_ews_autodiscover_parse(sipe_private
, body
);
175 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
178 case SIPE_HTTP_STATUS_CLIENT_FORBIDDEN
:
180 * Authentication succeeded but we still weren't allowed to
181 * view the page. At least at our work place this error is
182 * temporary, i.e. the next access with the exact same
183 * authentication succeeds.
185 * Let's try again, but only once...
187 sipe_ews_autodiscover_request(sipe_private
, !sea
->retry
);
190 case SIPE_HTTP_STATUS_ABORTED
:
191 /* we are not allowed to generate new requests */
195 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
200 static gboolean
sipe_ews_autodiscover_url(struct sipe_core_private
*sipe_private
,
203 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
204 gchar
*body
= g_strdup_printf("<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">"
206 " <EMailAddress>%s</EMailAddress>"
207 " <AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>"
212 SIPE_DEBUG_INFO("sipe_ews_autodiscover_url: trying '%s'", url
);
214 sea
->request
= sipe_http_request_post(sipe_private
,
219 sipe_ews_autodiscover_response
,
224 sipe_core_email_authentication(sipe_private
,
226 sipe_http_request_allow_redirect(sea
->request
);
227 sipe_http_request_ready(sea
->request
);
234 static void sipe_ews_autodiscover_request(struct sipe_core_private
*sipe_private
,
235 gboolean next_method
)
237 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
238 static const gchar
* const methods
[] = {
239 "https://Autodiscover.%s/Autodiscover/Autodiscover.xml",
240 "http://Autodiscover.%s/Autodiscover/Autodiscover.xml",
241 "https://%s/Autodiscover/Autodiscover.xml",
245 sea
->retry
= next_method
;
250 sea
->method
= methods
;
253 gchar
*url
= g_strdup_printf(*sea
->method
,
254 strstr(sea
->email
, "@") + 1);
256 if (!sipe_ews_autodiscover_url(sipe_private
, url
))
257 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
262 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_autodiscover_request: no more methods to try!");
263 sipe_ews_autodiscover_complete(sipe_private
, NULL
);
267 void sipe_ews_autodiscover_start(struct sipe_core_private
*sipe_private
,
268 sipe_ews_autodiscover_callback
*callback
,
269 gpointer callback_data
)
271 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
273 if (sea
->completed
) {
274 (*callback
)(sipe_private
, sea
->data
, callback_data
);
276 struct sipe_ews_autodiscover_cb
*sea_cb
= g_new(struct sipe_ews_autodiscover_cb
, 1);
277 sea_cb
->cb
= callback
;
278 sea_cb
->cb_data
= callback_data
;
279 sea
->callbacks
= g_slist_prepend(sea
->callbacks
, sea_cb
);
282 sipe_ews_autodiscover_request(sipe_private
, TRUE
);
286 void sipe_ews_autodiscover_init(struct sipe_core_private
*sipe_private
)
288 struct sipe_ews_autodiscover
*sea
= g_new0(struct sipe_ews_autodiscover
, 1);
290 sea
->email
= g_strdup(sipe_private
->email
);
292 sipe_private
->ews_autodiscover
= sea
;
295 void sipe_ews_autodiscover_free(struct sipe_core_private
*sipe_private
)
297 struct sipe_ews_autodiscover
*sea
= sipe_private
->ews_autodiscover
;
298 struct sipe_ews_autodiscover_data
*ews_data
= sea
->data
;
299 sipe_ews_autodiscover_complete(sipe_private
, NULL
);
301 g_free((gchar
*)ews_data
->as_url
);
302 g_free((gchar
*)ews_data
->ews_url
);
303 g_free((gchar
*)ews_data
->legacy_dn
);
304 g_free((gchar
*)ews_data
->oab_url
);
305 g_free((gchar
*)ews_data
->oof_url
);