ews: add 4th autodiscover type
[siplcs.git] / src / core / sipe-ews-autodiscover.c
blob25b1665d9f862655401decd1a0a9b68bec38d9b3
1 /**
2 * @file sipe-ews-autodiscover.c
4 * pidgin-sipe
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
33 #include <string.h>
35 #include <glib.h>
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"
44 #include "sipe-xml.h"
46 struct sipe_ews_autodiscover_cb {
47 sipe_ews_autodiscover_callback *cb;
48 gpointer cb_data;
51 struct autodiscover_method {
52 const gchar *template;
53 gboolean redirect;
56 struct sipe_ews_autodiscover {
57 struct sipe_ews_autodiscover_data *data;
58 struct sipe_http_request *request;
59 GSList *callbacks;
60 gchar *email;
61 const struct autodiscover_method *method;
62 gboolean retry;
63 gboolean completed;
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;
72 while (entry) {
73 struct sipe_ews_autodiscover_cb *sea_cb = entry->data;
74 sea_cb->cb(sipe_private, ews_data, sea_cb->cb_data);
75 g_free(sea_cb);
76 entry = entry->next;
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,
86 const gchar *url);
87 static void sipe_ews_autodiscover_parse(struct sipe_core_private *sipe_private,
88 const gchar *body)
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? */
98 if (account) {
99 const sipe_xml *node;
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"));
107 if (tmp)
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,
113 "Type"));
115 if (sipe_strequal("EXCH", type)) {
116 g_free(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>"); \
125 _URL(ASUrl, as_url);
126 _URL(EwsUrl, ews_url);
127 _URL(OABUrl, oab_url);
128 _URL(OOFUrl, oof_url);
129 #undef _URL
131 break;
134 g_free(type);
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)) {
148 g_free(sea->email);
149 sea->email = addr;
150 addr = NULL; /* sea takes ownership */
152 SIPE_DEBUG_INFO("sipe_ews_autodiscover_parse: restarting with email address '%s'",
153 sea->email);
155 /* restart process with new email address */
156 sea->method = NULL;
157 complete = FALSE;
158 sipe_ews_autodiscover_request(sipe_private,
159 TRUE);
161 g_free(addr);
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'",
169 url);
170 complete = !sipe_ews_autodiscover_url(sipe_private,
171 url);
173 g_free(url);
175 /* ignore all other POX autodiscover responses */
176 } else {
177 SIPE_DEBUG_ERROR_NOFORMAT("sipe_ews_autodiscover_parse: unknown response detected");
180 sipe_xml_free(xml);
182 if (complete)
183 sipe_ews_autodiscover_complete(sipe_private, ews_data);
186 static void sipe_ews_autodiscover_response(struct sipe_core_private *sipe_private,
187 guint status,
188 SIPE_UNUSED_PARAMETER GSList *headers,
189 const gchar *body,
190 gpointer data)
192 struct sipe_ews_autodiscover *sea = data;
194 sea->request = NULL;
196 switch (status) {
197 case SIPE_HTTP_STATUS_OK:
198 if (body)
199 sipe_ews_autodiscover_parse(sipe_private, body);
200 else
201 sipe_ews_autodiscover_request(sipe_private, TRUE);
202 break;
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);
214 break;
216 case SIPE_HTTP_STATUS_ABORTED:
217 /* we are not allowed to generate new requests */
218 break;
220 default:
221 sipe_ews_autodiscover_request(sipe_private, TRUE);
222 break;
226 static gboolean sipe_ews_autodiscover_url(struct sipe_core_private *sipe_private,
227 const gchar *url)
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\">"
231 " <Request>"
232 " <EMailAddress>%s</EMailAddress>"
233 " <AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>"
234 " </Request>"
235 "</Autodiscover>",
236 sea->email);
238 SIPE_DEBUG_INFO("sipe_ews_autodiscover_url: trying '%s'", url);
240 sea->request = sipe_http_request_post(sipe_private,
241 url,
242 NULL,
243 body,
244 "text/xml",
245 sipe_ews_autodiscover_response,
246 sea);
247 g_free(body);
249 if (sea->request) {
250 sipe_core_email_authentication(sipe_private,
251 sea->request);
252 sipe_http_request_allow_redirect(sea->request);
253 sipe_http_request_ready(sea->request);
254 return(TRUE);
257 return(FALSE);
260 static void sipe_ews_autodiscover_redirect_response(struct sipe_core_private *sipe_private,
261 guint status,
262 GSList *headers,
263 SIPE_UNUSED_PARAMETER const gchar *body,
264 gpointer data)
266 struct sipe_ews_autodiscover *sea = data;
267 gboolean failed = TRUE;
269 sea->request = NULL;
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,
275 "Location",
277 if (location)
278 failed = !sipe_ews_autodiscover_url(sipe_private,
279 location);
282 if (failed)
283 sipe_ews_autodiscover_request(sipe_private, TRUE);
286 static gboolean sipe_ews_autodiscover_redirect(struct sipe_core_private *sipe_private,
287 const gchar *url)
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,
294 url,
295 NULL,
296 sipe_ews_autodiscover_redirect_response,
297 sea);
299 if (sea->request) {
300 sipe_http_request_ready(sea->request);
301 return(TRUE);
304 return(FALSE);
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 },
316 { NULL, FALSE },
319 sea->retry = next_method;
320 if (sea->method) {
321 if (next_method)
322 sea->method++;
323 } else
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);
335 g_free(url);
337 } else {
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);
351 } else {
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);
357 if (!sea->method)
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);
376 if (ews_data) {
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);
382 g_free(ews_data);
384 g_free(sea->email);
385 g_free(sea);
389 Local Variables:
390 mode: c
391 c-file-style: "bsd"
392 indent-tabs-mode: t
393 tab-width: 8
394 End: