purple: work around broken dbus-server.h
[siplcs.git] / src / core / sipe-lync-autodiscover.c
blob3b632d667afdb4c917ef60028a74da1d24c48200
1 /**
2 * @file sipe-lync-autodiscover.c
4 * pidgin-sipe
6 * Copyright (C) 2016-2017 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 * - [MS-OCDISCWS]: https://msdn.microsoft.com/en-us/library/hh623245.aspx
26 * - Understanding Autodiscover in Lync Server 2013
27 * https://technet.microsoft.com/en-us/library/jj945654.aspx
30 #include <string.h>
32 #include <glib.h>
34 #include "sipe-common.h"
35 #include "sipe-backend.h"
36 #include "sipe-core.h"
37 #include "sipe-core-private.h"
38 #include "sipe-http.h"
39 #include "sipe-lync-autodiscover.h"
40 #include "sipe-utils.h"
41 #include "sipe-svc.h"
42 #include "sipe-webticket.h"
43 #include "sipe-xml.h"
45 #define LYNC_AUTODISCOVER_ACCEPT_HEADER \
46 "Accept: application/vnd.microsoft.rtc.autodiscover+xml;v=1\r\n"
48 struct lync_autodiscover_request {
49 sipe_lync_autodiscover_callback *cb;
50 gpointer cb_data;
51 gpointer id; /* != NULL for active request */
52 struct sipe_http_request *request;
53 struct sipe_svc_session *session;
54 const gchar *protocol;
55 const gchar **method;
56 gchar *uri;
57 gboolean is_pending;
60 struct sipe_lync_autodiscover {
61 GSList *pending_requests;
64 /* Use "lar" inside the code fragment */
65 #define FOR_ALL_REQUESTS_WITH_SAME_ID(code) \
66 { \
67 GSList *entry = sipe_private->lync_autodiscover->pending_requests; \
68 while (entry) { \
69 struct lync_autodiscover_request *lar = entry->data; \
70 entry = entry->next; \
71 if (lar->id == id) { \
72 code; \
73 } \
74 } \
77 static void sipe_lync_autodiscover_request_free(struct sipe_core_private *sipe_private,
78 struct lync_autodiscover_request *request)
80 struct sipe_lync_autodiscover *sla = sipe_private->lync_autodiscover;
82 sla->pending_requests = g_slist_remove(sla->pending_requests, request);
84 if (request->request)
85 sipe_http_request_cancel(request->request);
86 if (request->cb)
87 /* Callback: aborted */
88 (*request->cb)(sipe_private, NULL, request->cb_data);
89 sipe_svc_session_close(request->session);
90 g_free(request->uri);
91 g_free(request);
94 static void sipe_lync_autodiscover_cb(struct sipe_core_private *sipe_private,
95 guint status,
96 GSList *headers,
97 const gchar *body,
98 gpointer callback_data);
99 static void lync_request(struct sipe_core_private *sipe_private,
100 struct lync_autodiscover_request *request,
101 const gchar *uri,
102 const gchar *headers)
104 request->request = sipe_http_request_get(sipe_private,
105 uri,
106 headers ? headers : LYNC_AUTODISCOVER_ACCEPT_HEADER,
107 sipe_lync_autodiscover_cb,
108 request);
111 static GSList *sipe_lync_autodiscover_add(GSList *servers,
112 const sipe_xml *node,
113 const gchar *name)
115 const sipe_xml *child = sipe_xml_child(node, name);
116 const gchar *fqdn = sipe_xml_attribute(child, "fqdn");
117 guint port = sipe_xml_int_attribute(child, "port", 0);
119 /* Add new entry to head of list */
120 if (fqdn && (port != 0)) {
121 struct sipe_lync_autodiscover_data *lync_data = g_new0(struct sipe_lync_autodiscover_data, 1);
122 lync_data->server = g_strdup(fqdn);
123 lync_data->port = port;
124 servers = g_slist_prepend(servers, lync_data);
127 return(servers);
130 GSList *sipe_lync_autodiscover_pop(GSList *servers)
132 if (servers) {
133 struct sipe_lync_autodiscover_data *lync_data = servers->data;
134 servers = g_slist_remove(servers, lync_data);
136 if (lync_data) {
137 g_free((gchar *) lync_data->server);
138 g_free(lync_data);
142 return(servers);
145 static void sipe_lync_autodiscover_queue_request(struct sipe_core_private *sipe_private,
146 struct lync_autodiscover_request *request);
147 static void sipe_lync_autodiscover_parse(struct sipe_core_private *sipe_private,
148 struct lync_autodiscover_request *request,
149 const gchar *body)
151 sipe_xml *xml = sipe_xml_parse(body, strlen(body));
152 const sipe_xml *node;
153 gboolean next = TRUE;
154 const gchar *access_location;
156 /* Root: resources exposed by this server */
157 for (node = sipe_xml_child(xml, "Root/Link");
158 node;
159 node = sipe_xml_twin(node)) {
160 const gchar *token = sipe_xml_attribute(node, "token");
161 const gchar *uri = sipe_xml_attribute(node, "href");
163 if (token && uri) {
164 /* Redirect? */
165 if (sipe_strcase_equal(token, "Redirect")) {
166 SIPE_DEBUG_INFO("sipe_lync_autodiscover_parse: redirect to %s",
167 uri);
168 lync_request(sipe_private, request, uri, NULL);
169 next = FALSE;
170 break;
172 /* User? */
173 } else if (sipe_strcase_equal(token, "User")) {
174 SIPE_DEBUG_INFO("sipe_lync_autodiscover_parse: user %s",
175 uri);
177 /* remember URI for authentication failure */
178 request->uri = g_strdup(uri);
180 lync_request(sipe_private, request, uri, NULL);
181 next = FALSE;
182 break;
184 } else
185 SIPE_DEBUG_INFO("sipe_lync_autodiscover_parse: unknown token %s",
186 token);
190 access_location = sipe_xml_attribute(xml, "AccessLocation");
192 /* User: topology information of the user’s home server */
193 if ((node = sipe_xml_child(xml, "User")) != NULL) {
194 gpointer id = request->id;
196 /* Active request? */
197 if (id) {
198 GSList *servers;
200 /* List is reversed, i.e. internal will be tried first */
201 servers = g_slist_prepend(NULL, NULL);
203 if (!access_location ||
204 sipe_strcase_equal(access_location, "external")) {
205 servers = sipe_lync_autodiscover_add(servers,
206 node,
207 "SipClientExternalAccess");
210 if (!access_location ||
211 sipe_strcase_equal(access_location, "internal")) {
212 servers = sipe_lync_autodiscover_add(servers,
213 node,
214 "SipClientInternalAccess");
217 /* Callback takes ownership of servers list */
218 (*request->cb)(sipe_private, servers, request->cb_data);
220 /* We're done with requests for this callback */
221 FOR_ALL_REQUESTS_WITH_SAME_ID( \
222 lar->cb = NULL; \
223 lar->id = NULL \
228 /* Request completed */
229 next = FALSE;
230 sipe_lync_autodiscover_request_free(sipe_private, request);
231 /* request is invalid */
234 sipe_xml_free(xml);
236 if (next)
237 sipe_lync_autodiscover_queue_request(sipe_private, request);
240 static void sipe_lync_autodiscover_webticket(struct sipe_core_private *sipe_private,
241 SIPE_UNUSED_PARAMETER const gchar *base_uri,
242 const gchar *auth_uri,
243 const gchar *wsse_security,
244 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
245 gpointer callback_data)
247 struct lync_autodiscover_request *request = callback_data;
248 gchar *saml;
250 /* Extract SAML Assertion from WSSE Security XML text */
251 if (wsse_security &&
252 ((saml = sipe_xml_extract_raw(wsse_security,
253 "Assertion",
254 TRUE)) != NULL)) {
255 gchar *base64 = g_base64_encode((const guchar *) saml,
256 strlen(saml));
257 gchar *headers = g_strdup_printf(LYNC_AUTODISCOVER_ACCEPT_HEADER
258 "X-MS-WebTicket: opaque=%s\r\n",
259 base64);
260 g_free(base64);
262 SIPE_DEBUG_INFO("sipe_lync_autodiscover_webticket: got ticket for Auth URI %s",
263 auth_uri);
264 g_free(saml);
266 lync_request(sipe_private, request, auth_uri, headers);
267 g_free(headers);
269 } else
270 sipe_lync_autodiscover_queue_request(sipe_private, request);
273 static void sipe_lync_autodiscover_cb(struct sipe_core_private *sipe_private,
274 guint status,
275 GSList *headers,
276 const gchar *body,
277 gpointer callback_data)
279 struct lync_autodiscover_request *request = callback_data;
280 const gchar *type = sipe_utils_nameval_find(headers, "Content-Type");
281 gchar *uri = request->uri;
283 request->request = NULL;
284 request->uri = NULL;
286 switch (status) {
287 case SIPE_HTTP_STATUS_OK:
288 /* only accept Autodiscover XML responses */
289 if (body && g_str_has_prefix(type, "application/vnd.microsoft.rtc.autodiscover+xml"))
290 sipe_lync_autodiscover_parse(sipe_private, request, body);
291 else
292 sipe_lync_autodiscover_queue_request(sipe_private, request);
293 break;
295 case SIPE_HTTP_STATUS_FAILED:
297 if (uri) {
298 /* check for authentication failure */
299 const gchar *webticket_uri = sipe_utils_nameval_find(headers,
300 "X-MS-WebTicketURL");
302 if (!(webticket_uri &&
303 sipe_webticket_request_with_auth(sipe_private,
304 request->session,
305 webticket_uri,
306 uri, /* Auth URI */
307 sipe_lync_autodiscover_webticket,
308 request)))
309 sipe_lync_autodiscover_queue_request(sipe_private, request);
310 } else
311 sipe_lync_autodiscover_queue_request(sipe_private, request);
313 break;
315 case SIPE_HTTP_STATUS_ABORTED:
316 /* we are not allowed to generate new requests */
317 sipe_lync_autodiscover_request_free(sipe_private, request);
318 break;
320 default:
321 sipe_lync_autodiscover_queue_request(sipe_private, request);
322 break;
325 g_free(uri);
328 /* Proceed to next method for request */
329 static void sipe_lync_autodiscover_request(struct sipe_core_private *sipe_private,
330 struct lync_autodiscover_request *request)
332 gpointer id = request->id;
334 /* Active request? */
335 if (id) {
336 static const gchar *methods[] = {
337 "%s://LyncDiscoverInternal.%s/?sipuri=%s",
338 "%s://LyncDiscover.%s/?sipuri=%s",
339 NULL
342 request->is_pending = TRUE;
344 if (request->method)
345 request->method++;
346 else
347 request->method = methods;
349 if (*request->method) {
350 gchar *uri = g_strdup_printf(*request->method,
351 request->protocol,
352 SIPE_CORE_PUBLIC->sip_domain,
353 sipe_private->username);
355 SIPE_DEBUG_INFO("sipe_lync_autodiscover_request: trying '%s'", uri);
357 lync_request(sipe_private, request, uri, NULL);
358 g_free(uri);
360 } else {
361 guint count = 0;
363 /* Count entries with the same request ID */
364 FOR_ALL_REQUESTS_WITH_SAME_ID( \
365 count++; \
368 if (count == 1) {
370 * This is the last pending request for this
371 * ID, i.e. autodiscover has failed. Create
372 * empty server list and return it.
374 GSList *servers = g_slist_prepend(NULL, NULL);
376 /* All methods tried, indicate failure to caller */
377 SIPE_DEBUG_INFO_NOFORMAT("sipe_lync_autodiscover_request: no more methods to try!");
379 /* Callback takes ownership of servers list */
380 (*request->cb)(sipe_private, servers, request->cb_data);
383 /* Request completed */
384 request->cb = NULL;
385 sipe_lync_autodiscover_request_free(sipe_private, request);
386 /* request is invalid */
388 } else {
389 /* Inactive request, callback already NULL */
390 sipe_lync_autodiscover_request_free(sipe_private, request);
391 /* request is invalid */
395 /* Proceed to next method for all requests */
396 static void sipe_lync_autodiscover_queue_request(struct sipe_core_private *sipe_private,
397 struct lync_autodiscover_request *request)
399 gpointer id = request->id;
401 /* This request is ready to proceed to next method */
402 request->is_pending = FALSE;
404 /* Is any request for the same ID still pending? */
405 FOR_ALL_REQUESTS_WITH_SAME_ID( \
406 if (lar->is_pending) \
407 return \
410 SIPE_DEBUG_INFO_NOFORMAT("sipe_lync_autodiscover_queue_request: proceed in lockstep");
412 /* No, proceed to next method for all requests */
413 FOR_ALL_REQUESTS_WITH_SAME_ID( \
414 sipe_lync_autodiscover_request(sipe_private, \
415 lar) \
419 static gpointer sipe_lync_autodiscover_create(struct sipe_core_private *sipe_private,
420 gpointer id,
421 const gchar *protocol,
422 sipe_lync_autodiscover_callback *callback,
423 gpointer callback_data)
425 struct sipe_lync_autodiscover *sla = sipe_private->lync_autodiscover;
426 struct lync_autodiscover_request *request = g_new0(struct lync_autodiscover_request, 1);
428 /* use address of first request structure as unique ID */
429 if (id == NULL)
430 id = request;
432 request->protocol = protocol;
433 request->cb = callback;
434 request->cb_data = callback_data;
435 request->id = id;
436 request->session = sipe_svc_session_start();
438 sla->pending_requests = g_slist_prepend(sla->pending_requests,
439 request);
441 sipe_lync_autodiscover_request(sipe_private, request);
443 return(id);
446 void sipe_lync_autodiscover_start(struct sipe_core_private *sipe_private,
447 sipe_lync_autodiscover_callback *callback,
448 gpointer callback_data)
450 gpointer id = NULL;
452 #define CREATE(protocol) \
453 id = sipe_lync_autodiscover_create(sipe_private, \
454 id, \
455 #protocol, \
456 callback, \
457 callback_data)
458 CREATE(http);
459 CREATE(https);
462 void sipe_lync_autodiscover_init(struct sipe_core_private *sipe_private)
464 struct sipe_lync_autodiscover *sla = g_new0(struct sipe_lync_autodiscover, 1);
466 sipe_private->lync_autodiscover = sla;
469 void sipe_lync_autodiscover_free(struct sipe_core_private *sipe_private)
471 struct sipe_lync_autodiscover *sla = sipe_private->lync_autodiscover;
473 while (sla->pending_requests)
474 sipe_lync_autodiscover_request_free(sipe_private,
475 sla->pending_requests->data);
477 g_free(sla);
478 sipe_private->lync_autodiscover = NULL;
482 Local Variables:
483 mode: c
484 c-file-style: "bsd"
485 indent-tabs-mode: t
486 tab-width: 8
487 End: