2 * @file sipe-lync-autodiscover.c
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
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"
42 #include "sipe-webticket.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
;
51 gpointer id
; /* != NULL for active request */
52 struct sipe_http_request
*request
;
53 struct sipe_svc_session
*session
;
54 const gchar
*protocol
;
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) \
67 GSList *entry = sipe_private->lync_autodiscover->pending_requests; \
69 struct lync_autodiscover_request *lar = entry->data; \
70 entry = entry->next; \
71 if (lar->id == id) { \
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
);
85 sipe_http_request_cancel(request
->request
);
87 /* Callback: aborted */
88 (*request
->cb
)(sipe_private
, NULL
, request
->cb_data
);
89 sipe_svc_session_close(request
->session
);
94 static void sipe_lync_autodiscover_cb(struct sipe_core_private
*sipe_private
,
98 gpointer callback_data
);
99 static void lync_request(struct sipe_core_private
*sipe_private
,
100 struct lync_autodiscover_request
*request
,
102 const gchar
*headers
)
104 request
->request
= sipe_http_request_get(sipe_private
,
106 headers
? headers
: LYNC_AUTODISCOVER_ACCEPT_HEADER
,
107 sipe_lync_autodiscover_cb
,
111 static GSList
*sipe_lync_autodiscover_add(GSList
*servers
,
112 const sipe_xml
*node
,
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
);
130 GSList
*sipe_lync_autodiscover_pop(GSList
*servers
)
133 struct sipe_lync_autodiscover_data
*lync_data
= servers
->data
;
134 servers
= g_slist_remove(servers
, lync_data
);
137 g_free((gchar
*) lync_data
->server
);
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
,
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");
159 node
= sipe_xml_twin(node
)) {
160 const gchar
*token
= sipe_xml_attribute(node
, "token");
161 const gchar
*uri
= sipe_xml_attribute(node
, "href");
165 if (sipe_strcase_equal(token
, "Redirect")) {
166 SIPE_DEBUG_INFO("sipe_lync_autodiscover_parse: redirect to %s",
168 lync_request(sipe_private
, request
, uri
, NULL
);
173 } else if (sipe_strcase_equal(token
, "User")) {
174 SIPE_DEBUG_INFO("sipe_lync_autodiscover_parse: user %s",
177 /* remember URI for authentication failure */
178 request
->uri
= g_strdup(uri
);
180 lync_request(sipe_private
, request
, uri
, NULL
);
185 SIPE_DEBUG_INFO("sipe_lync_autodiscover_parse: unknown token %s",
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? */
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
,
207 "SipClientExternalAccess");
210 if (!access_location
||
211 sipe_strcase_equal(access_location
, "internal")) {
212 servers
= sipe_lync_autodiscover_add(servers
,
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( \
228 /* Request completed */
230 sipe_lync_autodiscover_request_free(sipe_private
, request
);
231 /* request is invalid */
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
;
250 /* Extract SAML Assertion from WSSE Security XML text */
252 ((saml
= sipe_xml_extract_raw(wsse_security
,
255 gchar
*base64
= g_base64_encode((const guchar
*) saml
,
257 gchar
*headers
= g_strdup_printf(LYNC_AUTODISCOVER_ACCEPT_HEADER
258 "X-MS-WebTicket: opaque=%s\r\n",
262 SIPE_DEBUG_INFO("sipe_lync_autodiscover_webticket: got ticket for Auth URI %s",
266 lync_request(sipe_private
, request
, auth_uri
, headers
);
270 sipe_lync_autodiscover_queue_request(sipe_private
, request
);
273 static void sipe_lync_autodiscover_cb(struct sipe_core_private
*sipe_private
,
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
;
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
);
292 sipe_lync_autodiscover_queue_request(sipe_private
, request
);
295 case SIPE_HTTP_STATUS_FAILED
:
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
,
307 sipe_lync_autodiscover_webticket
,
309 sipe_lync_autodiscover_queue_request(sipe_private
, request
);
311 sipe_lync_autodiscover_queue_request(sipe_private
, request
);
315 case SIPE_HTTP_STATUS_ABORTED
:
316 /* we are not allowed to generate new requests */
317 sipe_lync_autodiscover_request_free(sipe_private
, request
);
321 sipe_lync_autodiscover_queue_request(sipe_private
, request
);
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? */
336 static const gchar
*methods
[] = {
337 "%s://LyncDiscoverInternal.%s/?sipuri=%s",
338 "%s://LyncDiscover.%s/?sipuri=%s",
342 request
->is_pending
= TRUE
;
347 request
->method
= methods
;
349 if (*request
->method
) {
350 gchar
*uri
= g_strdup_printf(*request
->method
,
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
);
363 /* Count entries with the same request ID */
364 FOR_ALL_REQUESTS_WITH_SAME_ID( \
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 */
385 sipe_lync_autodiscover_request_free(sipe_private
, request
);
386 /* request is invalid */
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
) \
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
, \
419 static gpointer
sipe_lync_autodiscover_create(struct sipe_core_private
*sipe_private
,
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 */
432 request
->protocol
= protocol
;
433 request
->cb
= callback
;
434 request
->cb_data
= callback_data
;
436 request
->session
= sipe_svc_session_start();
438 sla
->pending_requests
= g_slist_prepend(sla
->pending_requests
,
441 sipe_lync_autodiscover_request(sipe_private
, request
);
446 void sipe_lync_autodiscover_start(struct sipe_core_private
*sipe_private
,
447 sipe_lync_autodiscover_callback
*callback
,
448 gpointer callback_data
)
452 #define CREATE(protocol) \
453 id = sipe_lync_autodiscover_create(sipe_private, \
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
);
478 sipe_private
->lync_autodiscover
= NULL
;