buddy: fix use after free in process_buddy_photo_response()
[siplcs.git] / src / core / sipe-svc.c
blobad73a14e6c9756b04ee19cc8b41458b5bdfe85db
1 /**
2 * @file sipe-svc.c
4 * pidgin-sipe
6 * Copyright (C) 2011-12 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
32 #include <stdlib.h>
33 #include <string.h>
35 #include <glib.h>
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"
42 #include "sipe-svc.h"
43 #include "sipe-tls.h"
44 #include "sipe-utils.h"
45 #include "sipe-xml.h"
46 #include "uuid.h"
48 /* forward declaration */
49 struct svc_request;
50 typedef void (svc_callback)(struct svc_request *data,
51 const gchar *raw,
52 sipe_xml *xml);
54 struct svc_request {
55 struct sipe_core_private *sipe_private;
56 svc_callback *internal_cb;
57 sipe_svc_callback *cb;
58 gpointer *cb_data;
59 HttpConn *conn;
60 HttpConnAuth auth;
61 gchar *uri;
62 gchar *soap_action;
65 struct sipe_svc {
66 GSList *pending_requests;
69 struct sipe_svc_session {
70 HttpSession *session;
73 static void sipe_svc_request_free(struct svc_request *data)
75 if (data->conn)
76 http_conn_free(data->conn);
77 if (data->cb)
78 /* Callback: aborted */
79 (*data->cb)(data->sipe_private, NULL, NULL, NULL, data->cb_data);
80 g_free(data->soap_action);
81 g_free(data->uri);
82 g_free(data);
85 void sipe_svc_free(struct sipe_core_private *sipe_private)
87 struct sipe_svc *svc = sipe_private->svc;
88 if (!svc)
89 return;
91 if (svc->pending_requests) {
92 GSList *entry = svc->pending_requests;
93 while (entry) {
94 sipe_svc_request_free(entry->data);
95 entry = entry->next;
97 g_slist_free(svc->pending_requests);
100 g_free(svc);
101 sipe_private->svc = NULL;
104 static void sipe_svc_init(struct sipe_core_private *sipe_private)
106 if (sipe_private->svc)
107 return;
109 sipe_private->svc = g_new0(struct sipe_svc, 1);
112 struct sipe_svc_session *sipe_svc_session_start(void)
114 struct sipe_svc_session *session = g_new0(struct sipe_svc_session, 1);
115 session->session = http_conn_session_create();
116 return(session);
119 void sipe_svc_session_close(struct sipe_svc_session *session)
121 if (session) {
122 http_conn_session_free(session->session);
123 g_free(session);
127 static void sipe_svc_https_response(int return_code,
128 const gchar *body,
129 SIPE_UNUSED_PARAMETER GSList *headers,
130 HttpConn *conn,
131 void *callback_data)
133 struct svc_request *data = callback_data;
134 struct sipe_svc *svc = data->sipe_private->svc;
136 SIPE_DEBUG_INFO("sipe_svc_https_response: code %d", return_code);
137 http_conn_set_close(conn);
138 data->conn = NULL;
140 if ((return_code == 200) && body) {
141 sipe_xml *xml = sipe_xml_parse(body, strlen(body));
142 /* Internal callback: success */
143 (*data->internal_cb)(data, body, xml);
144 sipe_xml_free(xml);
145 } else {
146 /* Internal callback: failed */
147 (*data->internal_cb)(data, NULL, NULL);
150 /* Internal callback has already called this */
151 data->cb = NULL;
153 svc->pending_requests = g_slist_remove(svc->pending_requests,
154 data);
155 sipe_svc_request_free(data);
158 static gboolean sipe_svc_https_request(struct sipe_core_private *sipe_private,
159 const gchar *method,
160 struct sipe_svc_session *session,
161 const gchar *uri,
162 const gchar *content_type,
163 const gchar *soap_action,
164 const gchar *body,
165 svc_callback *internal_callback,
166 sipe_svc_callback *callback,
167 gpointer callback_data)
169 struct svc_request *data = g_new0(struct svc_request, 1);
170 gboolean ret = FALSE;
172 data->sipe_private = sipe_private;
173 data->uri = g_strdup(uri);
175 if (soap_action)
176 data->soap_action = g_strdup_printf("SOAPAction: \"%s\"\r\n",
177 soap_action);
179 /* re-use SIP credentials */
180 data->auth.domain = sipe_private->authdomain;
181 data->auth.user = sipe_private->authuser ? sipe_private->authuser : sipe_private->username;
182 data->auth.password = sipe_private->password;
184 data->conn = http_conn_create(SIPE_CORE_PUBLIC,
185 session->session,
186 method,
187 HTTP_CONN_SSL,
188 HTTP_CONN_NO_REDIRECT,
189 uri,
190 body,
191 content_type,
192 data->soap_action,
193 &data->auth,
194 sipe_svc_https_response,
195 data);
197 if (data->conn) {
198 data->internal_cb = internal_callback;
199 data->cb = callback;
200 data->cb_data = callback_data;
201 sipe_svc_init(sipe_private);
202 sipe_private->svc->pending_requests = g_slist_prepend(sipe_private->svc->pending_requests,
203 data);
204 ret = TRUE;
205 } else {
206 SIPE_DEBUG_ERROR("failed to create HTTP connection to %s", uri);
207 sipe_svc_request_free(data);
210 return(ret);
213 static gboolean sipe_svc_wsdl_request(struct sipe_core_private *sipe_private,
214 struct sipe_svc_session *session,
215 const gchar *uri,
216 const gchar *additional_ns,
217 const gchar *soap_action,
218 const gchar *wsse_security,
219 const gchar *soap_body,
220 const gchar *content_type,
221 svc_callback *internal_callback,
222 sipe_svc_callback *callback,
223 gpointer callback_data)
225 /* Only generate SOAP header if we have a security token */
226 gchar *soap_header = wsse_security ?
227 g_strdup_printf("<soap:Header>"
228 " <wsa:To>%s</wsa:To>"
229 " <wsa:ReplyTo>"
230 " <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>"
231 " </wsa:ReplyTo>"
232 " <wsa:Action>%s</wsa:Action>"
233 " <wsse:Security>%s</wsse:Security>"
234 "</soap:Header>",
235 uri,
236 soap_action,
237 wsse_security) :
238 g_strdup("");
239 gchar *body = g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
240 "<soap:Envelope %s"
241 " xmlns:auth=\"http://schemas.xmlsoap.org/ws/2006/12/authorization\""
242 " xmlns:wsa=\"http://www.w3.org/2005/08/addressing\""
243 " xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\""
244 " xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\""
245 " xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\""
246 " >"
247 "%s"
248 " <soap:Body>%s</soap:Body>"
249 "</soap:Envelope>",
250 additional_ns,
251 soap_header,
252 soap_body);
254 gboolean ret = sipe_svc_https_request(sipe_private,
255 HTTP_CONN_POST,
256 session,
257 uri,
258 content_type ? content_type : "text/xml",
259 soap_action,
260 body,
261 internal_callback,
262 callback,
263 callback_data);
264 g_free(soap_header);
265 g_free(body);
267 return(ret);
270 static gboolean new_soap_req(struct sipe_core_private *sipe_private,
271 struct sipe_svc_session *session,
272 const gchar *uri,
273 const gchar *soap_action,
274 const gchar *wsse_security,
275 const gchar *soap_body,
276 svc_callback *internal_callback,
277 sipe_svc_callback *callback,
278 gpointer callback_data)
280 return(sipe_svc_wsdl_request(sipe_private,
281 session,
282 uri,
283 "xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\" "
284 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
285 "xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512\"",
286 soap_action,
287 wsse_security,
288 soap_body,
289 NULL,
290 internal_callback,
291 callback,
292 callback_data));
295 static void sipe_svc_wsdl_response(struct svc_request *data,
296 const gchar *raw,
297 sipe_xml *xml)
299 if (xml) {
300 /* Callback: success */
301 (*data->cb)(data->sipe_private, data->uri, raw, xml, data->cb_data);
302 } else {
303 /* Callback: failed */
304 (*data->cb)(data->sipe_private, data->uri, NULL, NULL, data->cb_data);
308 gboolean sipe_svc_get_and_publish_cert(struct sipe_core_private *sipe_private,
309 struct sipe_svc_session *session,
310 const gchar *uri,
311 const gchar *wsse_security,
312 const gchar *certreq,
313 sipe_svc_callback *callback,
314 gpointer callback_data)
316 struct sipe_tls_random id;
317 gchar *id_base64;
318 gchar *id_uuid;
319 gchar *uuid = get_uuid(sipe_private);
320 gchar *soap_body;
321 gboolean ret;
323 /* random request ID */
324 sipe_tls_fill_random(&id, 256);
325 id_base64 = g_base64_encode(id.buffer, id.length);
326 sipe_tls_free_random(&id);
327 id_uuid = generateUUIDfromEPID(id_base64);
328 g_free(id_base64);
330 soap_body = g_strdup_printf("<GetAndPublishCert"
331 " xmlns=\"http://schemas.microsoft.com/OCS/AuthWebServices/\""
332 " xmlns:wst=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512/\""
333 " xmlns:wstep=\"http://schemas.microsoft.com/windows/pki/2009/01/enrollment\""
334 " DeviceId=\"{%s}\""
335 " Entity=\"%s\""
337 " <wst:RequestSecurityToken>"
338 " <wst:TokenType>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3</wst:TokenType>"
339 " <wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>"
340 " <wsse:BinarySecurityToken"
341 " ValueType=\"http://schemas.microsoft.com/OCS/AuthWebServices.xsd#PKCS10\""
342 " EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd#Base64Binary\""
343 " >\r\n%s</wsse:BinarySecurityToken>"
344 " <wstep:RequestID>%s</wstep:RequestID>"
345 " </wst:RequestSecurityToken>"
346 "</GetAndPublishCert>",
347 uuid,
348 sipe_private->username,
349 certreq,
350 id_uuid);
351 g_free(id_uuid);
352 g_free(uuid);
354 ret = new_soap_req(sipe_private,
355 session,
356 uri,
357 "http://schemas.microsoft.com/OCS/AuthWebServices/GetAndPublishCert",
358 wsse_security,
359 soap_body,
360 sipe_svc_wsdl_response,
361 callback,
362 callback_data);
363 g_free(soap_body);
365 return(ret);
368 gboolean sipe_svc_ab_entry_request(struct sipe_core_private *sipe_private,
369 struct sipe_svc_session *session,
370 const gchar *uri,
371 const gchar *wsse_security,
372 const gchar *search,
373 guint entries,
374 guint max_returns,
375 sipe_svc_callback *callback,
376 gpointer callback_data)
378 gboolean ret;
379 gchar *soap_body = g_strdup_printf("<SearchAbEntry"
380 " xmlns=\"DistributionListExpander\""
381 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
382 " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\""
384 " <AbEntryRequest>"
385 " <ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
386 " %s"
387 " </ChangeSearch>"
388 " <Metadata>"
389 " <FromDialPad>false</FromDialPad>"
390 " <MaxResultNum>%d</MaxResultNum>"
391 " <ReturnList>displayName,msRTCSIP-PrimaryUserAddress,title,telephoneNumber,homePhone,mobile,otherTelephone,mail,company,country,photoRelPath,photoSize,photoHash</ReturnList>"
392 " </Metadata>"
393 " </AbEntryRequest>"
394 "</SearchAbEntry>",
395 entries,
396 search,
397 max_returns);
399 ret = new_soap_req(sipe_private,
400 session,
401 uri,
402 "DistributionListExpander/IAddressBook/SearchAbEntry",
403 wsse_security,
404 soap_body,
405 sipe_svc_wsdl_response,
406 callback,
407 callback_data);
408 g_free(soap_body);
410 return(ret);
413 /* Requests to login.microsoftonline.com & ADFS */
414 static gboolean request_passport(struct sipe_core_private *sipe_private,
415 struct sipe_svc_session *session,
416 const gchar *service_uri,
417 const gchar *auth_uri,
418 const gchar *wsse_security,
419 const gchar *content_type,
420 const gchar *request_extension,
421 sipe_svc_callback *callback,
422 gpointer callback_data)
424 gchar *soap_body = g_strdup_printf("<wst:RequestSecurityToken>"
425 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
426 " <wsp:AppliesTo>"
427 " <wsa:EndpointReference>"
428 " <wsa:Address>%s</wsa:Address>"
429 " </wsa:EndpointReference>"
430 " </wsp:AppliesTo>"
431 " %s"
432 "</wst:RequestSecurityToken>",
433 service_uri,
434 request_extension ? request_extension : "");
436 gboolean ret = sipe_svc_wsdl_request(sipe_private,
437 session,
438 auth_uri,
439 "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" "
440 "xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"",
441 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue",
442 wsse_security,
443 soap_body,
444 content_type,
445 sipe_svc_wsdl_response,
446 callback,
447 callback_data);
448 g_free(soap_body);
450 return(ret);
453 static gboolean request_user_password(struct sipe_core_private *sipe_private,
454 struct sipe_svc_session *session,
455 const gchar *service_uri,
456 const gchar *auth_uri,
457 const gchar *content_type,
458 const gchar *request_extension,
459 sipe_svc_callback *callback,
460 gpointer callback_data)
462 /* Only cleartext passwords seem to be accepted... */
463 gchar *wsse_security = g_strdup_printf("<wsse:UsernameToken>"
464 " <wsse:Username>%s</wsse:Username>"
465 " <wsse:Password>%s</wsse:Password>"
466 "</wsse:UsernameToken>",
467 sipe_private->username,
468 sipe_private->password);
470 gboolean ret = request_passport(sipe_private,
471 session,
472 service_uri,
473 auth_uri,
474 wsse_security,
475 content_type,
476 request_extension,
477 callback,
478 callback_data);
479 g_free(wsse_security);
481 return(ret);
484 gboolean sipe_svc_webticket_adfs(struct sipe_core_private *sipe_private,
485 struct sipe_svc_session *session,
486 const gchar *adfs_uri,
487 sipe_svc_callback *callback,
488 gpointer callback_data)
490 return(request_user_password(sipe_private,
491 session,
492 "urn:federation:MicrosoftOnline",
493 adfs_uri,
494 /* ADFS is special, *sigh* */
495 "application/soap+xml; charset=utf-8",
496 "<wst:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</wst:KeyType>",
497 callback,
498 callback_data));
501 #define LMC_URI "https://login.microsoftonline.com:443/RST2.srf"
503 gboolean sipe_svc_webticket_lmc(struct sipe_core_private *sipe_private,
504 struct sipe_svc_session *session,
505 const gchar *service_uri,
506 sipe_svc_callback *callback,
507 gpointer callback_data)
509 return(request_user_password(sipe_private,
510 session,
511 service_uri,
512 LMC_URI,
513 NULL,
514 NULL,
515 callback,
516 callback_data));
519 gboolean sipe_svc_webticket_lmc_federated(struct sipe_core_private *sipe_private,
520 struct sipe_svc_session *session,
521 const gchar *wsse_security,
522 const gchar *service_uri,
523 sipe_svc_callback *callback,
524 gpointer callback_data)
526 return(request_passport(sipe_private,
527 session,
528 service_uri,
529 LMC_URI,
530 wsse_security,
531 NULL,
532 NULL,
533 callback,
534 callback_data));
537 gboolean sipe_svc_webticket(struct sipe_core_private *sipe_private,
538 struct sipe_svc_session *session,
539 const gchar *uri,
540 const gchar *wsse_security,
541 const gchar *service_uri,
542 const struct sipe_tls_random *entropy,
543 sipe_svc_callback *callback,
544 gpointer callback_data)
546 gchar *uuid = get_uuid(sipe_private);
547 gchar *secret = g_base64_encode(entropy->buffer, entropy->length);
548 gchar *soap_body = g_strdup_printf("<wst:RequestSecurityToken Context=\"%s\">"
549 " <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>"
550 " <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>"
551 " <wsp:AppliesTo>"
552 " <wsa:EndpointReference>"
553 " <wsa:Address>%s</wsa:Address>"
554 " </wsa:EndpointReference>"
555 " </wsp:AppliesTo>"
556 " <wst:Claims Dialect=\"urn:component:Microsoft.Rtc.WebAuthentication.2010:authclaims\">"
557 " <auth:ClaimType Uri=\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/uri\" Optional=\"false\">"
558 " <auth:Value>sip:%s</auth:Value>"
559 " </auth:ClaimType>"
560 " </wst:Claims>"
561 " <wst:Entropy>"
562 " <wst:BinarySecret>%s</wst:BinarySecret>"
563 " </wst:Entropy>"
564 " <wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</wst:KeyType>"
565 "</wst:RequestSecurityToken>",
566 uuid,
567 service_uri,
568 sipe_private->username,
569 secret);
571 gboolean ret = new_soap_req(sipe_private,
572 session,
573 uri,
574 "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue",
575 wsse_security,
576 soap_body,
577 sipe_svc_wsdl_response,
578 callback,
579 callback_data);
580 g_free(soap_body);
581 g_free(secret);
582 g_free(uuid);
584 return(ret);
587 static void sipe_svc_metadata_response(struct svc_request *data,
588 const gchar *raw,
589 sipe_xml *xml)
591 if (xml) {
592 /* Callback: success */
593 (*data->cb)(data->sipe_private, data->uri, raw, xml, data->cb_data);
594 } else {
595 /* Callback: failed */
596 (*data->cb)(data->sipe_private, data->uri, NULL, NULL, data->cb_data);
600 gboolean sipe_svc_realminfo(struct sipe_core_private *sipe_private,
601 struct sipe_svc_session *session,
602 sipe_svc_callback *callback,
603 gpointer callback_data)
605 gchar *realminfo_uri = g_strdup_printf("https://login.microsoftonline.com/getuserrealm.srf?login=%s&xml=1",
606 sipe_private->username);
607 gboolean ret = sipe_svc_https_request(sipe_private,
608 HTTP_CONN_GET,
609 session,
610 realminfo_uri,
611 "text",
612 NULL,
613 NULL,
614 sipe_svc_metadata_response,
615 callback,
616 callback_data);
617 g_free(realminfo_uri);
618 return(ret);
621 gboolean sipe_svc_metadata(struct sipe_core_private *sipe_private,
622 struct sipe_svc_session *session,
623 const gchar *uri,
624 sipe_svc_callback *callback,
625 gpointer callback_data)
627 gchar *mex_uri = g_strdup_printf("%s/mex", uri);
628 gboolean ret = sipe_svc_https_request(sipe_private,
629 HTTP_CONN_GET,
630 session,
631 mex_uri,
632 "text",
633 NULL,
634 NULL,
635 sipe_svc_metadata_response,
636 callback,
637 callback_data);
638 g_free(mex_uri);
639 return(ret);
643 Local Variables:
644 mode: c
645 c-file-style: "bsd"
646 indent-tabs-mode: t
647 tab-width: 8
648 End: