6 * Copyright (C) 2011 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 "http-conn.h"
39 #include "sipe-backend.h"
40 #include "sipe-core.h"
41 #include "sipe-core-private.h"
44 #include "sipe-utils.h"
48 /* forward declaration */
50 typedef void (svc_callback
)(struct svc_request
*data
,
55 struct sipe_core_private
*sipe_private
;
56 svc_callback
*internal_cb
;
57 sipe_svc_callback
*cb
;
66 GSList
*pending_requests
;
69 static void sipe_svc_request_free(struct svc_request
*data
)
72 http_conn_free(data
->conn
);
74 /* Callback: aborted */
75 (*data
->cb
)(data
->sipe_private
, NULL
, NULL
, NULL
, data
->cb_data
);
76 g_free(data
->soap_action
);
81 void sipe_svc_free(struct sipe_core_private
*sipe_private
)
83 struct sipe_svc
*svc
= sipe_private
->svc
;
87 if (svc
->pending_requests
) {
88 GSList
*entry
= svc
->pending_requests
;
90 sipe_svc_request_free(entry
->data
);
93 g_slist_free(svc
->pending_requests
);
97 sipe_private
->svc
= NULL
;
100 static void sipe_svc_init(struct sipe_core_private
*sipe_private
)
102 if (sipe_private
->svc
)
105 sipe_private
->svc
= g_new0(struct sipe_svc
, 1);
108 static void sipe_svc_https_response(int return_code
,
110 SIPE_UNUSED_PARAMETER
const gchar
*content_type
,
114 struct svc_request
*data
= callback_data
;
115 struct sipe_svc
*svc
= data
->sipe_private
->svc
;
117 SIPE_DEBUG_INFO("sipe_svc_https_response: code %d", return_code
);
118 http_conn_set_close(conn
);
121 if ((return_code
== 200) && body
) {
122 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
123 /* Internal callback: success */
124 (*data
->internal_cb
)(data
, body
, xml
);
127 /* Internal callback: failed */
128 (*data
->internal_cb
)(data
, NULL
, NULL
);
131 /* Internal callback has already called this */
134 svc
->pending_requests
= g_slist_remove(svc
->pending_requests
,
136 sipe_svc_request_free(data
);
139 static gboolean
sipe_svc_https_request(struct sipe_core_private
*sipe_private
,
142 const gchar
*content_type
,
143 const gchar
*soap_action
,
145 svc_callback
*internal_callback
,
146 sipe_svc_callback
*callback
,
147 gpointer callback_data
)
149 struct svc_request
*data
= g_new0(struct svc_request
, 1);
150 gboolean ret
= FALSE
;
152 data
->sipe_private
= sipe_private
;
153 data
->uri
= g_strdup(uri
);
156 data
->soap_action
= g_strdup_printf("SOAPAction: \"%s\"\r\n",
159 /* re-use SIP credentials */
160 data
->auth
.domain
= sipe_private
->authdomain
;
161 data
->auth
.user
= sipe_private
->authuser
? sipe_private
->authuser
: sipe_private
->username
;
162 data
->auth
.password
= sipe_private
->password
;
164 data
->conn
= http_conn_create(SIPE_CORE_PUBLIC
,
165 NULL
, /* HttpSession */
168 HTTP_CONN_NO_REDIRECT
,
174 sipe_svc_https_response
,
178 data
->internal_cb
= internal_callback
;
180 data
->cb_data
= callback_data
;
181 sipe_svc_init(sipe_private
);
182 sipe_private
->svc
->pending_requests
= g_slist_prepend(sipe_private
->svc
->pending_requests
,
186 SIPE_DEBUG_ERROR("failed to create HTTP connection to %s", uri
);
187 sipe_svc_request_free(data
);
193 static gboolean
sipe_svc_wsdl_request(struct sipe_core_private
*sipe_private
,
195 const gchar
*additional_ns
,
196 const gchar
*soap_action
,
197 const gchar
*wsse_security
,
198 const gchar
*soap_body
,
199 svc_callback
*internal_callback
,
200 sipe_svc_callback
*callback
,
201 gpointer callback_data
)
203 /* Only generate SOAP header if we have a security token */
204 gchar
*soap_header
= wsse_security
?
205 g_strdup_printf("<soap:Header>"
206 " <wsa:To>%s</wsa:To>"
208 " <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>"
210 " <wsa:Action>%s</wsa:Action>"
211 " <wsse:Security>%s</wsse:Security>"
217 gchar
*body
= g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
219 " xmlns:auth=\"http://schemas.xmlsoap.org/ws/2006/12/authorization\""
220 " xmlns:wsa=\"http://www.w3.org/2005/08/addressing\""
221 " xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\""
222 " xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\""
223 " xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\""
226 " <soap:Body>%s</soap:Body>"
232 gboolean ret
= sipe_svc_https_request(sipe_private
,
247 static gboolean
new_soap_req(struct sipe_core_private
*sipe_private
,
249 const gchar
*soap_action
,
250 const gchar
*wsse_security
,
251 const gchar
*soap_body
,
252 svc_callback
*internal_callback
,
253 sipe_svc_callback
*callback
,
254 gpointer callback_data
)
256 return(sipe_svc_wsdl_request(sipe_private
,
258 "xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\" "
259 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
260 "xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512\"",
269 static void sipe_svc_wsdl_response(struct svc_request
*data
,
274 /* Callback: success */
275 (*data
->cb
)(data
->sipe_private
, data
->uri
, raw
, xml
, data
->cb_data
);
277 /* Callback: failed */
278 (*data
->cb
)(data
->sipe_private
, data
->uri
, NULL
, NULL
, data
->cb_data
);
282 gboolean
sipe_svc_get_and_publish_cert(struct sipe_core_private
*sipe_private
,
284 const gchar
*wsse_security
,
285 const gchar
*certreq
,
286 sipe_svc_callback
*callback
,
287 gpointer callback_data
)
289 struct sipe_tls_random id
;
292 gchar
*uuid
= get_uuid(sipe_private
);
296 /* random request ID */
297 sipe_tls_fill_random(&id
, 256);
298 id_base64
= g_base64_encode(id
.buffer
, id
.length
);
299 sipe_tls_free_random(&id
);
300 id_uuid
= generateUUIDfromEPID(id_base64
);
303 soap_body
= g_strdup_printf("<GetAndPublishCert"
304 " xmlns=\"http://schemas.microsoft.com/OCS/AuthWebServices/\""
305 " xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512/\""
306 " xmlns:wstep=\"http://schemas.microsoft.com/windows/pki/2009/01/enrollment\""
310 " <wst:RequestSecurityToken>"
311 " <wst:TokenType>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3</wst:TokenType>"
312 " <wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>"
313 " <wsse:BinarySecurityToken"
314 " ValueType=\"http://schemas.microsoft.com/OCS/AuthWebServices.xsd#PKCS10\""
315 " EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd#Base64Binary\""
316 " >\r\n%s</wsse:BinarySecurityToken>"
317 " <wstep:RequestID>%s</wstep:RequestID>"
318 " </wst:RequestSecurityToken>"
319 "</GetAndPublishCert>",
321 sipe_private
->username
,
327 ret
= new_soap_req(sipe_private
,
329 "http://schemas.microsoft.com/OCS/AuthWebServices/GetAndPublishCert",
332 sipe_svc_wsdl_response
,
340 gboolean
sipe_svc_ab_entry_request(struct sipe_core_private
*sipe_private
,
342 const gchar
*wsse_security
,
344 sipe_svc_callback
*callback
,
345 gpointer callback_data
)
348 gchar
*soap_body
= g_strdup_printf("<SearchAbEntry"
349 " xmlns=\"DistributionListExpander\""
350 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
351 " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\""
354 " <ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[1]\">"
355 " <AbEntryRequest.ChangeSearchQuery>"
357 " </AbEntryRequest.ChangeSearchQuery>"
360 " <FromDialPad>false</FromDialPad>"
361 " <MaxResultNum>1</MaxResultNum>"
362 " <ReturnList>displayName,msRTCSIP-PrimaryUserAddress,telephoneNumber,homePhone,mobile,otherTelephone,custom1Phone,mail,company,country</ReturnList>"
368 ret
= new_soap_req(sipe_private
,
370 "DistributionListExpander/IAddressBook/SearchAbEntry",
373 sipe_svc_wsdl_response
,
382 * This functions encodes what the Microsoft Lync client does for
383 * Office365 accounts. It will most definitely fail for internal Lync
384 * installation that use TLS-DSK instead of NTLM.
386 * But for those anonymous authentication should already have succeeded.
387 * I guess we'll have to see what happens in real life...
389 gboolean
sipe_svc_webticket_lmc(struct sipe_core_private
*sipe_private
,
390 const gchar
*service_uri
,
391 sipe_svc_callback
*callback
,
392 gpointer callback_data
)
394 /* login.microsoftonline.com seems only to accept cleartext passwords :/ */
395 gchar
*security
= g_strdup_printf("<wsse:UsernameToken>"
396 " <wsse:Username>%s</wsse:Username>"
397 " <wsse:Password>%s</wsse:Password>"
398 "</wsse:UsernameToken>",
399 sipe_private
->username
,
400 sipe_private
->password
);
402 gchar
*soap_body
= g_strdup_printf("<ps:RequestMultipleSecurityTokens>"
403 " <wst:RequestSecurityToken>"
404 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
406 " <wsa:EndpointReference>"
407 " <wsa:Address>%s</wsa:Address>"
408 " </wsa:EndpointReference>"
410 " </wst:RequestSecurityToken>"
411 "</ps:RequestMultipleSecurityTokens>",
414 gboolean ret
= sipe_svc_wsdl_request(sipe_private
,
415 "https://login.microsoftonline.com:443/RST2.srf",
416 "xmlns:ps=\"http://schemas.microsoft.com/Passport/SoapServices/PPCRL\" "
417 "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" "
418 "xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"",
419 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue",
422 sipe_svc_wsdl_response
,
431 gboolean
sipe_svc_webticket(struct sipe_core_private
*sipe_private
,
433 const gchar
*wsse_security
,
434 const gchar
*service_uri
,
435 const struct sipe_tls_random
*entropy
,
436 sipe_svc_callback
*callback
,
437 gpointer callback_data
)
439 gchar
*uuid
= get_uuid(sipe_private
);
440 gchar
*security
= NULL
;
441 gchar
*secret
= g_base64_encode(entropy
->buffer
, entropy
->length
);
442 gchar
*soap_body
= g_strdup_printf("<wst:RequestSecurityToken Context=\"%s\">"
443 " <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>"
444 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
446 " <wsa:EndpointReference>"
447 " <wsa:Address>%s</wsa:Address>"
448 " </wsa:EndpointReference>"
450 " <wst:Claims Dialect=\"urn:component:Microsoft.Rtc.WebAuthentication.2010:authclaims\">"
451 " <auth:ClaimType Uri=\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/uri\" Optional=\"false\">"
452 " <auth:Value>sip:%s</auth:Value>"
456 " <wst:BinarySecret>%s</wst:BinarySecret>"
458 " <wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</wst:KeyType>"
459 "</wst:RequestSecurityToken>",
462 sipe_private
->username
,
465 gboolean ret
= new_soap_req(sipe_private
,
467 "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue",
470 sipe_svc_wsdl_response
,
481 static void sipe_svc_metadata_response(struct svc_request
*data
,
486 /* Callback: success */
487 (*data
->cb
)(data
->sipe_private
, data
->uri
, raw
, xml
, data
->cb_data
);
489 /* Callback: failed */
490 (*data
->cb
)(data
->sipe_private
, data
->uri
, NULL
, NULL
, data
->cb_data
);
494 gboolean
sipe_svc_metadata(struct sipe_core_private
*sipe_private
,
496 sipe_svc_callback
*callback
,
497 gpointer callback_data
)
499 gchar
*mex_uri
= g_strdup_printf("%s/mex", uri
);
500 gboolean ret
= sipe_svc_https_request(sipe_private
,
506 sipe_svc_metadata_response
,