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
,
128 gpointer callback_data
)
130 struct svc_request
*data
= callback_data
;
131 struct sipe_svc
*svc
= sipe_private
->svc
;
133 SIPE_DEBUG_INFO("sipe_svc_https_response: code %d", status
);
134 data
->request
= NULL
;
136 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
137 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
138 /* Internal callback: success */
139 (*data
->internal_cb
)(sipe_private
, data
, body
, xml
);
142 /* Internal callback: failed */
143 (*data
->internal_cb
)(sipe_private
, data
, NULL
, NULL
);
146 /* Internal callback has already called this */
149 svc
->pending_requests
= g_slist_remove(svc
->pending_requests
,
151 sipe_svc_request_free(sipe_private
, data
);
155 * Send GET request when @c body is NULL, otherwise send POST request
157 * @param content_type MIME type for body content (ignored when body is @c NULL)
158 * @param soap_action SOAP action header value (ignored when body is @c NULL)
159 * @param body body contents (may be @c NULL)
161 static gboolean
sipe_svc_https_request(struct sipe_core_private
*sipe_private
,
162 struct sipe_svc_session
*session
,
164 const gchar
*content_type
,
165 const gchar
*soap_action
,
167 svc_callback
*internal_callback
,
168 sipe_svc_callback
*callback
,
169 gpointer callback_data
)
171 struct svc_request
*data
= g_new0(struct svc_request
, 1);
172 struct sipe_http_request
*request
;
175 gchar
*headers
= g_strdup_printf("SOAPAction: \"%s\"\r\n",
178 request
= sipe_http_request_post(sipe_private
,
183 sipe_svc_https_response
,
188 request
= sipe_http_request_get(sipe_private
,
191 sipe_svc_https_response
,
196 data
->internal_cb
= internal_callback
;
198 data
->cb_data
= callback_data
;
199 data
->request
= request
;
200 data
->uri
= g_strdup(uri
);
202 sipe_http_request_session(request
, session
->session
);
204 sipe_svc_init(sipe_private
);
205 sipe_private
->svc
->pending_requests
= g_slist_prepend(sipe_private
->svc
->pending_requests
,
208 SIPE_DEBUG_ERROR("failed to create HTTP connection to %s", uri
);
212 return(request
!= NULL
);
215 static gboolean
sipe_svc_wsdl_request(struct sipe_core_private
*sipe_private
,
216 struct sipe_svc_session
*session
,
218 const gchar
*additional_ns
,
219 const gchar
*soap_action
,
220 const gchar
*wsse_security
,
221 const gchar
*soap_body
,
222 const gchar
*content_type
,
223 svc_callback
*internal_callback
,
224 sipe_svc_callback
*callback
,
225 gpointer callback_data
)
227 /* Only generate SOAP header if we have a security token */
228 gchar
*soap_header
= wsse_security
?
229 g_strdup_printf("<soap:Header>"
230 " <wsa:To>%s</wsa:To>"
232 " <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>"
234 " <wsa:Action>%s</wsa:Action>"
235 " <wsse:Security>%s</wsse:Security>"
241 gchar
*body
= g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
243 " xmlns:auth=\"http://schemas.xmlsoap.org/ws/2006/12/authorization\""
244 " xmlns:wsa=\"http://www.w3.org/2005/08/addressing\""
245 " xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\""
246 " xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\""
247 " xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\""
250 " <soap:Body>%s</soap:Body>"
256 gboolean ret
= sipe_svc_https_request(sipe_private
,
259 content_type
? content_type
: "text/xml",
271 static gboolean
new_soap_req(struct sipe_core_private
*sipe_private
,
272 struct sipe_svc_session
*session
,
274 const gchar
*soap_action
,
275 const gchar
*wsse_security
,
276 const gchar
*soap_body
,
277 svc_callback
*internal_callback
,
278 sipe_svc_callback
*callback
,
279 gpointer callback_data
)
281 return(sipe_svc_wsdl_request(sipe_private
,
284 "xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\" "
285 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
286 "xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512\"",
296 static void sipe_svc_wsdl_response(struct sipe_core_private
*sipe_private
,
297 struct svc_request
*data
,
302 /* Callback: success */
303 (*data
->cb
)(sipe_private
, data
->uri
, raw
, xml
, data
->cb_data
);
305 /* Callback: failed */
306 (*data
->cb
)(sipe_private
, data
->uri
, NULL
, NULL
, data
->cb_data
);
310 gboolean
sipe_svc_get_and_publish_cert(struct sipe_core_private
*sipe_private
,
311 struct sipe_svc_session
*session
,
313 const gchar
*wsse_security
,
314 const gchar
*certreq
,
315 sipe_svc_callback
*callback
,
316 gpointer callback_data
)
318 struct sipe_tls_random id
;
321 gchar
*uuid
= get_uuid(sipe_private
);
325 /* random request ID */
326 sipe_tls_fill_random(&id
, 256);
327 id_base64
= g_base64_encode(id
.buffer
, id
.length
);
328 sipe_tls_free_random(&id
);
329 id_uuid
= generateUUIDfromEPID(id_base64
);
332 soap_body
= g_strdup_printf("<GetAndPublishCert"
333 " xmlns=\"http://schemas.microsoft.com/OCS/AuthWebServices/\""
334 " xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512/\""
335 " xmlns:wstep=\"http://schemas.microsoft.com/windows/pki/2009/01/enrollment\""
339 " <wst:RequestSecurityToken>"
340 " <wst:TokenType>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3</wst:TokenType>"
341 " <wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>"
342 " <wsse:BinarySecurityToken"
343 " ValueType=\"http://schemas.microsoft.com/OCS/AuthWebServices.xsd#PKCS10\""
344 " EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd#Base64Binary\""
345 " >\r\n%s</wsse:BinarySecurityToken>"
346 " <wstep:RequestID>%s</wstep:RequestID>"
347 " </wst:RequestSecurityToken>"
348 "</GetAndPublishCert>",
350 sipe_private
->username
,
356 ret
= new_soap_req(sipe_private
,
359 "http://schemas.microsoft.com/OCS/AuthWebServices/GetAndPublishCert",
362 sipe_svc_wsdl_response
,
370 gboolean
sipe_svc_ab_entry_request(struct sipe_core_private
*sipe_private
,
371 struct sipe_svc_session
*session
,
373 const gchar
*wsse_security
,
377 sipe_svc_callback
*callback
,
378 gpointer callback_data
)
381 gchar
*soap_body
= g_strdup_printf("<SearchAbEntry"
382 " xmlns=\"DistributionListExpander\""
383 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
384 " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\""
387 " <ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
391 " <FromDialPad>false</FromDialPad>"
392 " <MaxResultNum>%d</MaxResultNum>"
393 " <ReturnList>displayName,msRTCSIP-PrimaryUserAddress,title,telephoneNumber,homePhone,mobile,otherTelephone,mail,company,country,photoRelPath,photoSize,photoHash</ReturnList>"
401 ret
= new_soap_req(sipe_private
,
404 "DistributionListExpander/IAddressBook/SearchAbEntry",
407 sipe_svc_wsdl_response
,
415 /* Requests to login.microsoftonline.com & ADFS */
416 static gboolean
request_passport(struct sipe_core_private
*sipe_private
,
417 struct sipe_svc_session
*session
,
418 const gchar
*service_uri
,
419 const gchar
*auth_uri
,
420 const gchar
*wsse_security
,
421 const gchar
*content_type
,
422 const gchar
*request_extension
,
423 sipe_svc_callback
*callback
,
424 gpointer callback_data
)
426 gchar
*soap_body
= g_strdup_printf("<wst:RequestSecurityToken>"
427 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
429 " <wsa:EndpointReference>"
430 " <wsa:Address>%s</wsa:Address>"
431 " </wsa:EndpointReference>"
434 "</wst:RequestSecurityToken>",
436 request_extension
? request_extension
: "");
438 gboolean ret
= sipe_svc_wsdl_request(sipe_private
,
441 "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" "
442 "xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"",
443 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue",
447 sipe_svc_wsdl_response
,
455 static gboolean
request_user_password(struct sipe_core_private
*sipe_private
,
456 struct sipe_svc_session
*session
,
457 const gchar
*service_uri
,
458 const gchar
*auth_uri
,
459 const gchar
*content_type
,
460 const gchar
*request_extension
,
461 sipe_svc_callback
*callback
,
462 gpointer callback_data
)
464 /* Only cleartext passwords seem to be accepted... */
465 gchar
*wsse_security
= g_strdup_printf("<wsse:UsernameToken>"
466 " <wsse:Username>%s</wsse:Username>"
467 " <wsse:Password>%s</wsse:Password>"
468 "</wsse:UsernameToken>",
469 sipe_private
->username
,
470 sipe_private
->password
? sipe_private
->password
: "");
472 gboolean ret
= request_passport(sipe_private
,
481 g_free(wsse_security
);
486 gboolean
sipe_svc_webticket_adfs(struct sipe_core_private
*sipe_private
,
487 struct sipe_svc_session
*session
,
488 const gchar
*adfs_uri
,
489 sipe_svc_callback
*callback
,
490 gpointer callback_data
)
492 return(request_user_password(sipe_private
,
494 "urn:federation:MicrosoftOnline",
496 /* ADFS is special, *sigh* */
497 "application/soap+xml; charset=utf-8",
498 "<wst:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</wst:KeyType>",
503 #define LMC_URI "https://login.microsoftonline.com:443/RST2.srf"
505 gboolean
sipe_svc_webticket_lmc(struct sipe_core_private
*sipe_private
,
506 struct sipe_svc_session
*session
,
507 const gchar
*service_uri
,
508 sipe_svc_callback
*callback
,
509 gpointer callback_data
)
511 return(request_user_password(sipe_private
,
521 gboolean
sipe_svc_webticket_lmc_federated(struct sipe_core_private
*sipe_private
,
522 struct sipe_svc_session
*session
,
523 const gchar
*wsse_security
,
524 const gchar
*service_uri
,
525 sipe_svc_callback
*callback
,
526 gpointer callback_data
)
528 return(request_passport(sipe_private
,
539 gboolean
sipe_svc_webticket(struct sipe_core_private
*sipe_private
,
540 struct sipe_svc_session
*session
,
542 const gchar
*wsse_security
,
543 const gchar
*service_uri
,
544 const struct sipe_tls_random
*entropy
,
545 sipe_svc_callback
*callback
,
546 gpointer callback_data
)
548 gchar
*uuid
= get_uuid(sipe_private
);
549 gchar
*secret
= g_base64_encode(entropy
->buffer
, entropy
->length
);
550 gchar
*soap_body
= g_strdup_printf("<wst:RequestSecurityToken Context=\"%s\">"
551 " <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>"
552 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
554 " <wsa:EndpointReference>"
555 " <wsa:Address>%s</wsa:Address>"
556 " </wsa:EndpointReference>"
558 " <wst:Claims Dialect=\"urn:component:Microsoft.Rtc.WebAuthentication.2010:authclaims\">"
559 " <auth:ClaimType Uri=\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/uri\" Optional=\"false\">"
560 " <auth:Value>sip:%s</auth:Value>"
564 " <wst:BinarySecret>%s</wst:BinarySecret>"
566 " <wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</wst:KeyType>"
567 "</wst:RequestSecurityToken>",
570 sipe_private
->username
,
573 gboolean ret
= new_soap_req(sipe_private
,
576 "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue",
579 sipe_svc_wsdl_response
,
589 static void sipe_svc_metadata_response(struct sipe_core_private
*sipe_private
,
590 struct svc_request
*data
,
595 /* Callback: success */
596 (*data
->cb
)(sipe_private
, data
->uri
, raw
, xml
, data
->cb_data
);
598 /* Callback: failed */
599 (*data
->cb
)(sipe_private
, data
->uri
, NULL
, NULL
, data
->cb_data
);
603 gboolean
sipe_svc_realminfo(struct sipe_core_private
*sipe_private
,
604 struct sipe_svc_session
*session
,
605 sipe_svc_callback
*callback
,
606 gpointer callback_data
)
608 gchar
*realminfo_uri
= g_strdup_printf("https://login.microsoftonline.com/getuserrealm.srf?login=%s&xml=1",
609 sipe_private
->username
);
610 gboolean ret
= sipe_svc_https_request(sipe_private
,
616 sipe_svc_metadata_response
,
619 g_free(realminfo_uri
);
623 gboolean
sipe_svc_metadata(struct sipe_core_private
*sipe_private
,
624 struct sipe_svc_session
*session
,
626 sipe_svc_callback
*callback
,
627 gpointer callback_data
)
629 gchar
*mex_uri
= g_strdup_printf("%s/mex", uri
);
630 gboolean ret
= sipe_svc_https_request(sipe_private
,
636 sipe_svc_metadata_response
,