6 * Copyright (C) 2011-2013 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
24 * Specification references:
26 * - [MS-SIPAE]: http://msdn.microsoft.com/en-us/library/cc431510.aspx
27 * - [MS-OCAUTHWS]: http://msdn.microsoft.com/en-us/library/ff595592.aspx
28 * - MS Tech-Ed Europe 2010 "UNC310: Microsoft Lync 2010 Technology Explained"
29 * http://ecn.channel9.msdn.com/o9/te/Europe/2010/pptx/unc310.pptx
37 #include "sipe-common.h"
38 #include "sipe-backend.h"
39 #include "sipe-core.h"
40 #include "sipe-core-private.h"
41 #include "sipe-http.h"
44 #include "sipe-utils.h"
48 /* forward declaration */
50 typedef void (svc_callback
)(struct sipe_core_private
*sipe_private
,
51 struct svc_request
*data
,
56 svc_callback
*internal_cb
;
57 sipe_svc_callback
*cb
;
59 struct sipe_http_request
*request
;
64 GSList
*pending_requests
;
67 struct sipe_svc_session
{
68 struct sipe_http_session
*session
;
71 static void sipe_svc_request_free(struct sipe_core_private
*sipe_private
,
72 struct svc_request
*data
)
75 sipe_http_request_cancel(data
->request
);
77 /* Callback: aborted */
78 (*data
->cb
)(sipe_private
, NULL
, NULL
, NULL
, data
->cb_data
);
83 void sipe_svc_free(struct sipe_core_private
*sipe_private
)
85 struct sipe_svc
*svc
= sipe_private
->svc
;
89 if (svc
->pending_requests
) {
90 GSList
*entry
= svc
->pending_requests
;
92 sipe_svc_request_free(sipe_private
, entry
->data
);
95 g_slist_free(svc
->pending_requests
);
99 sipe_private
->svc
= NULL
;
102 static void sipe_svc_init(struct sipe_core_private
*sipe_private
)
104 if (sipe_private
->svc
)
107 sipe_private
->svc
= g_new0(struct sipe_svc
, 1);
110 struct sipe_svc_session
*sipe_svc_session_start(void)
112 struct sipe_svc_session
*session
= g_new0(struct sipe_svc_session
, 1);
113 session
->session
= sipe_http_session_start();
117 void sipe_svc_session_close(struct sipe_svc_session
*session
)
120 sipe_http_session_close(session
->session
);
125 static void sipe_svc_https_response(struct sipe_core_private
*sipe_private
,
127 SIPE_UNUSED_PARAMETER GSList
*headers
,
129 gpointer callback_data
)
131 struct svc_request
*data
= callback_data
;
132 struct sipe_svc
*svc
= sipe_private
->svc
;
134 SIPE_DEBUG_INFO("sipe_svc_https_response: code %d", status
);
135 data
->request
= NULL
;
137 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
138 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
139 /* Internal callback: success */
140 (*data
->internal_cb
)(sipe_private
, data
, body
, xml
);
143 /* Internal callback: failed */
144 (*data
->internal_cb
)(sipe_private
, data
, NULL
, NULL
);
147 /* Internal callback has already called this */
150 svc
->pending_requests
= g_slist_remove(svc
->pending_requests
,
152 sipe_svc_request_free(sipe_private
, data
);
156 * Send GET request when @c body is NULL, otherwise send POST request
158 * @param content_type MIME type for body content (ignored when body is @c NULL)
159 * @param soap_action SOAP action header value (ignored when body is @c NULL)
160 * @param body body contents (may be @c NULL)
162 static gboolean
sipe_svc_https_request(struct sipe_core_private
*sipe_private
,
163 struct sipe_svc_session
*session
,
165 const gchar
*content_type
,
166 const gchar
*soap_action
,
168 svc_callback
*internal_callback
,
169 sipe_svc_callback
*callback
,
170 gpointer callback_data
)
172 struct svc_request
*data
= g_new0(struct svc_request
, 1);
173 struct sipe_http_request
*request
;
176 gchar
*headers
= g_strdup_printf("SOAPAction: \"%s\"\r\n",
179 request
= sipe_http_request_post(sipe_private
,
184 sipe_svc_https_response
,
189 request
= sipe_http_request_get(sipe_private
,
192 sipe_svc_https_response
,
197 data
->internal_cb
= internal_callback
;
199 data
->cb_data
= callback_data
;
200 data
->request
= request
;
201 data
->uri
= g_strdup(uri
);
203 sipe_http_request_session(request
, session
->session
);
205 sipe_svc_init(sipe_private
);
206 sipe_private
->svc
->pending_requests
= g_slist_prepend(sipe_private
->svc
->pending_requests
,
209 SIPE_DEBUG_ERROR("failed to create HTTP connection to %s", uri
);
213 return(request
!= NULL
);
216 static gboolean
sipe_svc_wsdl_request(struct sipe_core_private
*sipe_private
,
217 struct sipe_svc_session
*session
,
219 const gchar
*additional_ns
,
220 const gchar
*soap_action
,
221 const gchar
*wsse_security
,
222 const gchar
*soap_body
,
223 const gchar
*content_type
,
224 svc_callback
*internal_callback
,
225 sipe_svc_callback
*callback
,
226 gpointer callback_data
)
228 /* Only generate SOAP header if we have a security token */
229 gchar
*soap_header
= wsse_security
?
230 g_strdup_printf("<soap:Header>"
231 " <wsa:To>%s</wsa:To>"
233 " <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>"
235 " <wsa:Action>%s</wsa:Action>"
236 " <wsse:Security>%s</wsse:Security>"
242 gchar
*body
= g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
244 " xmlns:auth=\"http://schemas.xmlsoap.org/ws/2006/12/authorization\""
245 " xmlns:wsa=\"http://www.w3.org/2005/08/addressing\""
246 " xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\""
247 " xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\""
248 " xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\""
251 " <soap:Body>%s</soap:Body>"
257 gboolean ret
= sipe_svc_https_request(sipe_private
,
260 content_type
? content_type
: "text/xml",
272 static gboolean
new_soap_req(struct sipe_core_private
*sipe_private
,
273 struct sipe_svc_session
*session
,
275 const gchar
*soap_action
,
276 const gchar
*wsse_security
,
277 const gchar
*soap_body
,
278 svc_callback
*internal_callback
,
279 sipe_svc_callback
*callback
,
280 gpointer callback_data
)
282 return(sipe_svc_wsdl_request(sipe_private
,
285 "xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\" "
286 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
287 "xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512\"",
297 static void sipe_svc_wsdl_response(struct sipe_core_private
*sipe_private
,
298 struct svc_request
*data
,
303 /* Callback: success */
304 (*data
->cb
)(sipe_private
, data
->uri
, raw
, xml
, data
->cb_data
);
306 /* Callback: failed */
307 (*data
->cb
)(sipe_private
, data
->uri
, NULL
, NULL
, data
->cb_data
);
311 gboolean
sipe_svc_get_and_publish_cert(struct sipe_core_private
*sipe_private
,
312 struct sipe_svc_session
*session
,
314 const gchar
*wsse_security
,
315 const gchar
*certreq
,
316 sipe_svc_callback
*callback
,
317 gpointer callback_data
)
319 struct sipe_tls_random id
;
322 gchar
*uuid
= get_uuid(sipe_private
);
326 /* random request ID */
327 sipe_tls_fill_random(&id
, 256);
328 id_base64
= g_base64_encode(id
.buffer
, id
.length
);
329 sipe_tls_free_random(&id
);
330 id_uuid
= generateUUIDfromEPID(id_base64
);
333 soap_body
= g_strdup_printf("<GetAndPublishCert"
334 " xmlns=\"http://schemas.microsoft.com/OCS/AuthWebServices/\""
335 " xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512/\""
336 " xmlns:wstep=\"http://schemas.microsoft.com/windows/pki/2009/01/enrollment\""
340 " <wst:RequestSecurityToken>"
341 " <wst:TokenType>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3</wst:TokenType>"
342 " <wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>"
343 " <wsse:BinarySecurityToken"
344 " ValueType=\"http://schemas.microsoft.com/OCS/AuthWebServices.xsd#PKCS10\""
345 " EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd#Base64Binary\""
346 " >\r\n%s</wsse:BinarySecurityToken>"
347 " <wstep:RequestID>%s</wstep:RequestID>"
348 " </wst:RequestSecurityToken>"
349 "</GetAndPublishCert>",
351 sipe_private
->username
,
357 ret
= new_soap_req(sipe_private
,
360 "http://schemas.microsoft.com/OCS/AuthWebServices/GetAndPublishCert",
363 sipe_svc_wsdl_response
,
371 gboolean
sipe_svc_ab_entry_request(struct sipe_core_private
*sipe_private
,
372 struct sipe_svc_session
*session
,
374 const gchar
*wsse_security
,
378 sipe_svc_callback
*callback
,
379 gpointer callback_data
)
382 gchar
*soap_body
= g_strdup_printf("<SearchAbEntry"
383 " xmlns=\"DistributionListExpander\""
384 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
385 " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\""
388 " <ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
392 " <FromDialPad>false</FromDialPad>"
393 " <MaxResultNum>%d</MaxResultNum>"
394 " <ReturnList>displayName,msRTCSIP-PrimaryUserAddress,title,telephoneNumber,homePhone,mobile,otherTelephone,mail,company,country,photoRelPath,photoSize,photoHash</ReturnList>"
402 ret
= new_soap_req(sipe_private
,
405 "DistributionListExpander/IAddressBook/SearchAbEntry",
408 sipe_svc_wsdl_response
,
416 /* Requests to login.microsoftonline.com & ADFS */
417 static gboolean
request_passport(struct sipe_core_private
*sipe_private
,
418 struct sipe_svc_session
*session
,
419 const gchar
*service_uri
,
420 const gchar
*auth_uri
,
421 const gchar
*wsse_security
,
422 const gchar
*content_type
,
423 const gchar
*request_extension
,
424 sipe_svc_callback
*callback
,
425 gpointer callback_data
)
427 gchar
*soap_body
= g_strdup_printf("<wst:RequestSecurityToken>"
428 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
430 " <wsa:EndpointReference>"
431 " <wsa:Address>%s</wsa:Address>"
432 " </wsa:EndpointReference>"
435 "</wst:RequestSecurityToken>",
437 request_extension
? request_extension
: "");
439 gboolean ret
= sipe_svc_wsdl_request(sipe_private
,
442 "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" "
443 "xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"",
444 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue",
448 sipe_svc_wsdl_response
,
456 static gboolean
request_user_password(struct sipe_core_private
*sipe_private
,
457 struct sipe_svc_session
*session
,
458 const gchar
*service_uri
,
459 const gchar
*auth_uri
,
460 const gchar
*authuser
,
461 const gchar
*content_type
,
462 const gchar
*request_extension
,
463 sipe_svc_callback
*callback
,
464 gpointer callback_data
)
466 /* Only cleartext passwords seem to be accepted... */
467 gchar
*wsse_security
= g_strdup_printf("<wsse:UsernameToken>"
468 " <wsse:Username>%s</wsse:Username>"
469 " <wsse:Password>%s</wsse:Password>"
470 "</wsse:UsernameToken>",
472 sipe_private
->password
? sipe_private
->password
: "");
474 gboolean ret
= request_passport(sipe_private
,
483 g_free(wsse_security
);
488 gboolean
sipe_svc_webticket_adfs(struct sipe_core_private
*sipe_private
,
489 struct sipe_svc_session
*session
,
490 const gchar
*adfs_uri
,
491 sipe_svc_callback
*callback
,
492 gpointer callback_data
)
494 return(request_user_password(sipe_private
,
496 "urn:federation:MicrosoftOnline",
498 sipe_private
->username
,
499 /* ADFS is special, *sigh* */
500 "application/soap+xml; charset=utf-8",
501 "<wst:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</wst:KeyType>",
506 #define LMC_URI "https://login.microsoftonline.com:443/RST2.srf"
508 gboolean
sipe_svc_webticket_lmc(struct sipe_core_private
*sipe_private
,
509 struct sipe_svc_session
*session
,
510 const gchar
*service_uri
,
511 sipe_svc_callback
*callback
,
512 gpointer callback_data
)
514 return(request_user_password(sipe_private
,
518 sipe_private
->authuser
? sipe_private
->authuser
: sipe_private
->username
,
525 gboolean
sipe_svc_webticket_lmc_federated(struct sipe_core_private
*sipe_private
,
526 struct sipe_svc_session
*session
,
527 const gchar
*wsse_security
,
528 const gchar
*service_uri
,
529 sipe_svc_callback
*callback
,
530 gpointer callback_data
)
532 return(request_passport(sipe_private
,
543 gboolean
sipe_svc_webticket(struct sipe_core_private
*sipe_private
,
544 struct sipe_svc_session
*session
,
546 const gchar
*wsse_security
,
547 const gchar
*service_uri
,
548 const struct sipe_tls_random
*entropy
,
549 sipe_svc_callback
*callback
,
550 gpointer callback_data
)
552 gchar
*uuid
= get_uuid(sipe_private
);
553 gchar
*secret
= g_base64_encode(entropy
->buffer
, entropy
->length
);
554 gchar
*soap_body
= g_strdup_printf("<wst:RequestSecurityToken Context=\"%s\">"
555 " <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>"
556 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
558 " <wsa:EndpointReference>"
559 " <wsa:Address>%s</wsa:Address>"
560 " </wsa:EndpointReference>"
562 " <wst:Claims Dialect=\"urn:component:Microsoft.Rtc.WebAuthentication.2010:authclaims\">"
563 " <auth:ClaimType Uri=\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/uri\" Optional=\"false\">"
564 " <auth:Value>sip:%s</auth:Value>"
568 " <wst:BinarySecret>%s</wst:BinarySecret>"
570 " <wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</wst:KeyType>"
571 "</wst:RequestSecurityToken>",
574 sipe_private
->username
,
577 gboolean ret
= new_soap_req(sipe_private
,
580 "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue",
583 sipe_svc_wsdl_response
,
593 static void sipe_svc_metadata_response(struct sipe_core_private
*sipe_private
,
594 struct svc_request
*data
,
599 /* Callback: success */
600 (*data
->cb
)(sipe_private
, data
->uri
, raw
, xml
, data
->cb_data
);
602 /* Callback: failed */
603 (*data
->cb
)(sipe_private
, data
->uri
, NULL
, NULL
, data
->cb_data
);
607 gboolean
sipe_svc_realminfo(struct sipe_core_private
*sipe_private
,
608 struct sipe_svc_session
*session
,
609 sipe_svc_callback
*callback
,
610 gpointer callback_data
)
612 gchar
*realminfo_uri
= g_strdup_printf("https://login.microsoftonline.com/getuserrealm.srf?login=%s&xml=1",
613 sipe_private
->username
);
614 gboolean ret
= sipe_svc_https_request(sipe_private
,
620 sipe_svc_metadata_response
,
623 g_free(realminfo_uri
);
627 gboolean
sipe_svc_metadata(struct sipe_core_private
*sipe_private
,
628 struct sipe_svc_session
*session
,
630 sipe_svc_callback
*callback
,
631 gpointer callback_data
)
633 gchar
*mex_uri
= g_strdup_printf("%s/mex", uri
);
634 gboolean ret
= sipe_svc_https_request(sipe_private
,
640 sipe_svc_metadata_response
,