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
,
341 * This functions encodes what the Microsoft Lync client does for
342 * Office365 accounts. It will most definitely fail for internal Lync
343 * installation that use TLS-DSK instead of NTLM.
345 * But for those anonymous authentication should already have succeeded.
346 * I guess we'll have to see what happens in real life...
348 gboolean
sipe_svc_webticket_lmc(struct sipe_core_private
*sipe_private
,
349 const gchar
*service_uri
,
350 sipe_svc_callback
*callback
,
351 gpointer callback_data
)
353 /* login.microsoftonline.com seems only to accept cleartext passwords :/ */
354 gchar
*security
= g_strdup_printf("<wsse:UsernameToken>"
355 " <wsse:Username>%s</wsse:Username>"
356 " <wsse:Password>%s</wsse:Password>"
357 "</wsse:UsernameToken>",
358 sipe_private
->username
,
359 sipe_private
->password
);
361 gchar
*soap_body
= g_strdup_printf("<ps:RequestMultipleSecurityTokens>"
362 " <wst:RequestSecurityToken>"
363 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
365 " <wsa:EndpointReference>"
366 " <wsa:Address>%s</wsa:Address>"
367 " </wsa:EndpointReference>"
369 " </wst:RequestSecurityToken>"
370 "</ps:RequestMultipleSecurityTokens>",
373 gboolean ret
= sipe_svc_wsdl_request(sipe_private
,
374 "https://login.microsoftonline.com:443/RST2.srf",
375 "xmlns:ps=\"http://schemas.microsoft.com/Passport/SoapServices/PPCRL\" "
376 "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" "
377 "xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"",
378 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue",
381 sipe_svc_wsdl_response
,
390 gboolean
sipe_svc_webticket(struct sipe_core_private
*sipe_private
,
392 const gchar
*wsse_security
,
393 const gchar
*service_uri
,
394 const struct sipe_tls_random
*entropy
,
395 sipe_svc_callback
*callback
,
396 gpointer callback_data
)
398 gchar
*uuid
= get_uuid(sipe_private
);
399 gchar
*security
= NULL
;
400 gchar
*secret
= g_base64_encode(entropy
->buffer
, entropy
->length
);
401 gchar
*soap_body
= g_strdup_printf("<wst:RequestSecurityToken Context=\"%s\">"
402 " <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>"
403 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
405 " <wsa:EndpointReference>"
406 " <wsa:Address>%s</wsa:Address>"
407 " </wsa:EndpointReference>"
409 " <wst:Claims Dialect=\"urn:component:Microsoft.Rtc.WebAuthentication.2010:authclaims\">"
410 " <auth:ClaimType Uri=\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/uri\" Optional=\"false\">"
411 " <auth:Value>sip:%s</auth:Value>"
415 " <wst:BinarySecret>%s</wst:BinarySecret>"
417 " <wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</wst:KeyType>"
418 "</wst:RequestSecurityToken>",
421 sipe_private
->username
,
424 gboolean ret
= new_soap_req(sipe_private
,
426 "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue",
429 sipe_svc_wsdl_response
,
440 static void sipe_svc_metadata_response(struct svc_request
*data
,
445 /* Callback: success */
446 (*data
->cb
)(data
->sipe_private
, data
->uri
, raw
, xml
, data
->cb_data
);
448 /* Callback: failed */
449 (*data
->cb
)(data
->sipe_private
, data
->uri
, NULL
, NULL
, data
->cb_data
);
453 gboolean
sipe_svc_metadata(struct sipe_core_private
*sipe_private
,
455 sipe_svc_callback
*callback
,
456 gpointer callback_data
)
458 gchar
*mex_uri
= g_strdup_printf("%s/mex", uri
);
459 gboolean ret
= sipe_svc_https_request(sipe_private
,
465 sipe_svc_metadata_response
,