6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010, 2009 pier11 <pier11@operamail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 For communication with Exchange 2007/2010 Web Server/Web Services:
28 1) Autodiscover (HTTPS POST request). With redirect support. XML content.
29 1.1) DNS SRV record _autodiscover._tcp.<domain> may also be resolved.
30 2) Availability Web service (SOAP = HTTPS POST + XML) call.
31 3) Out of Office (OOF) Web Service (SOAP = HTTPS POST + XML) call.
32 4) Web server authentication required - NTLM and/or Negotiate (Kerberos).
34 Note: ews - EWS stands for Exchange Web Services.
36 It will be able to retrieve our Calendar information (FreeBusy, WorkingHours,
37 Meetings Subject and Location, Is_Meeting) as well as our Out of Office (OOF) note
38 from Exchange Web Services for subsequent publishing.
40 Ref. for more implementation details:
41 http://sourceforge.net/projects/sipe/forums/forum/688535/topic/3403462
43 Similar functionality for Lotus Notes/Domino, iCalendar/CalDAV/Google would
44 be great to implement too.
52 #include "sipe-backend.h"
53 #include "sipe-common.h"
55 #include "sipe-core.h"
56 #include "sipe-core-private.h"
58 #include "sipe-http.h"
59 #include "sipe-utils.h"
63 * Autodiscover request for Exchange Web Services
64 * @param email (%s) Ex.: alice@cosmo.local
66 #define SIPE_EWS_AUTODISCOVER_REQUEST \
67 "<?xml version=\"1.0\"?>"\
68 "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">"\
70 "<EMailAddress>%s</EMailAddress>"\
71 "<AcceptableResponseSchema>"\
72 "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"\
73 "</AcceptableResponseSchema>"\
78 * GetUserOofSettingsRequest SOAP request to Exchange Web Services
79 * to obtain our Out-of-office (OOF) information.
80 * @param email (%s) Ex.: alice@cosmo.local
82 #define SIPE_EWS_USER_OOF_SETTINGS_REQUEST \
83 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
84 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"\
86 "<GetUserOofSettingsRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">"\
87 "<Mailbox xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
88 "<Address>%s</Address>"\
90 "</GetUserOofSettingsRequest>"\
95 * GetUserAvailabilityRequest SOAP request to Exchange Web Services
96 * to obtain our Availability (FreeBusy, WorkingHours, Meetings) information.
97 * @param email (%s) Ex.: alice@cosmo.local
98 * @param start_time (%s) Ex.: 2009-12-06T00:00:00
99 * @param end_time (%s) Ex.: 2009-12-09T23:59:59
101 #define SIPE_EWS_USER_AVAILABILITY_REQUEST \
102 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
103 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""\
104 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""\
105 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""\
106 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
108 "<GetUserAvailabilityRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\""\
109 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
110 "<t:TimeZone xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
114 "<Time>00:00:00</Time>"\
115 "<DayOrder>0</DayOrder>"\
117 "<DayOfWeek>Sunday</DayOfWeek>"\
121 "<Time>00:00:00</Time>"\
122 "<DayOrder>0</DayOrder>"\
124 "<DayOfWeek>Sunday</DayOfWeek>"\
127 "<MailboxDataArray>"\
130 "<t:Address>%s</t:Address>"\
132 "<t:AttendeeType>Required</t:AttendeeType>"\
133 "<t:ExcludeConflicts>false</t:ExcludeConflicts>"\
135 "</MailboxDataArray>"\
136 "<t:FreeBusyViewOptions>"\
138 "<t:StartTime>%s</t:StartTime>"\
139 "<t:EndTime>%s</t:EndTime>"\
141 "<t:MergedFreeBusyIntervalInMinutes>15</t:MergedFreeBusyIntervalInMinutes>"\
142 "<t:RequestedView>DetailedMerged</t:RequestedView>"\
143 "</t:FreeBusyViewOptions>"\
144 "</GetUserAvailabilityRequest>"\
148 #define SIPE_EWS_STATE_NONE 0
149 #define SIPE_EWS_STATE_AUTODISCOVER_SUCCESS 1
150 #define SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE -1
151 #define SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE -2
152 #define SIPE_EWS_STATE_AUTODISCOVER_3_FAILURE -3
153 #define SIPE_EWS_STATE_AVAILABILITY_SUCCESS 4
154 #define SIPE_EWS_STATE_AVAILABILITY_FAILURE -4
155 #define SIPE_EWS_STATE_OOF_SUCCESS 5
156 #define SIPE_EWS_STATE_OOF_FAILURE -5
159 sipe_ews_get_oof_note(struct sipe_calendar
*cal
)
161 time_t now
= time(NULL
);
163 if (!cal
|| !cal
->oof_state
) return NULL
;
165 if (sipe_strequal(cal
->oof_state
, "Enabled") ||
166 (sipe_strequal(cal
->oof_state
, "Scheduled") && now
>= cal
->oof_start
&& now
<= cal
->oof_end
))
168 return cal
->oof_note
;
177 sipe_ews_run_state_machine(struct sipe_calendar
*cal
);
179 static void sipe_ews_process_avail_response(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
181 SIPE_UNUSED_PARAMETER GSList
*headers
,
185 struct sipe_calendar
*cal
= data
;
187 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_avail_response: cb started.");
191 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
192 const sipe_xml
*node
;
193 const sipe_xml
*resp
;
194 /** ref: [MS-OXWAVLS] */
195 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
197 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/ResponseMessage@ResponseClass="Success"
198 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/MergedFreeBusy
199 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/CalendarEventArray/CalendarEvent
200 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/WorkingHours
202 resp
= sipe_xml_child(xml
, "Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse");
203 if (!resp
) return; /* rather soap:Fault */
204 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
205 return; /* Error response */
209 g_free(cal
->free_busy
);
210 cal
->free_busy
= sipe_xml_data(sipe_xml_child(resp
, "FreeBusyView/MergedFreeBusy"));
213 node
= sipe_xml_child(resp
, "FreeBusyView/WorkingHours");
214 g_free(cal
->working_hours_xml_str
);
215 cal
->working_hours_xml_str
= sipe_xml_stringify(node
);
216 SIPE_DEBUG_INFO("sipe_ews_process_avail_response: cal->working_hours_xml_str:\n%s",
217 cal
->working_hours_xml_str
? cal
->working_hours_xml_str
: "");
219 sipe_cal_events_free(cal
->cal_events
);
220 cal
->cal_events
= NULL
;
222 for (node
= sipe_xml_child(resp
, "FreeBusyView/CalendarEventArray/CalendarEvent");
224 node
= sipe_xml_twin(node
))
229 <StartTime>2009-12-07T13:30:00</StartTime>
230 <EndTime>2009-12-07T14:30:00</EndTime>
231 <BusyType>Busy</BusyType>
232 <CalendarEventDetails>
234 <Subject>Lunch</Subject>
235 <Location>Cafe</Location>
236 <IsMeeting>false</IsMeeting>
237 <IsRecurring>true</IsRecurring>
238 <IsException>false</IsException>
239 <IsReminderSet>true</IsReminderSet>
240 <IsPrivate>false</IsPrivate>
241 </CalendarEventDetails>
244 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
245 cal
->cal_events
= g_slist_append(cal
->cal_events
, cal_event
);
247 tmp
= sipe_xml_data(sipe_xml_child(node
, "StartTime"));
248 cal_event
->start_time
= sipe_utils_str_to_time(tmp
);
251 tmp
= sipe_xml_data(sipe_xml_child(node
, "EndTime"));
252 cal_event
->end_time
= sipe_utils_str_to_time(tmp
);
255 tmp
= sipe_xml_data(sipe_xml_child(node
, "BusyType"));
256 if (sipe_strequal("Free", tmp
)) {
257 cal_event
->cal_status
= SIPE_CAL_FREE
;
258 } else if (sipe_strequal("Tentative", tmp
)) {
259 cal_event
->cal_status
= SIPE_CAL_TENTATIVE
;
260 } else if (sipe_strequal("Busy", tmp
)) {
261 cal_event
->cal_status
= SIPE_CAL_BUSY
;
262 } else if (sipe_strequal("OOF", tmp
)) {
263 cal_event
->cal_status
= SIPE_CAL_OOF
;
265 cal_event
->cal_status
= SIPE_CAL_NO_DATA
;
269 cal_event
->subject
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Subject"));
270 cal_event
->location
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Location"));
272 tmp
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/IsMeeting"));
273 cal_event
->is_meeting
= tmp
? sipe_strequal(tmp
, "true") : TRUE
;
279 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_SUCCESS
;
280 sipe_ews_run_state_machine(cal
);
283 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_FAILURE
;
284 sipe_ews_run_state_machine(cal
);
288 static void sipe_ews_process_oof_response(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
290 SIPE_UNUSED_PARAMETER GSList
*headers
,
294 struct sipe_calendar
*cal
= data
;
296 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_oof_response: cb started.");
300 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
302 const sipe_xml
*resp
;
303 const sipe_xml
*xn_duration
;
304 /** ref: [MS-OXWOOF] */
305 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
306 /* Envelope/Body/GetUserOofSettingsResponse/ResponseMessage@ResponseClass="Success"
307 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/OofState=Enabled
308 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/StartTime
309 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/EndTime
310 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/InternalReply/Message
312 resp
= sipe_xml_child(xml
, "Body/GetUserOofSettingsResponse");
313 if (!resp
) return; /* rather soap:Fault */
314 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
315 return; /* Error response */
318 g_free(cal
->oof_state
);
319 cal
->oof_state
= sipe_xml_data(sipe_xml_child(resp
, "OofSettings/OofState"));
321 old_note
= cal
->oof_note
;
322 cal
->oof_note
= NULL
;
323 if (!sipe_strequal(cal
->oof_state
, "Disabled")) {
324 char *tmp
= sipe_xml_data(
325 sipe_xml_child(resp
, "OofSettings/InternalReply/Message"));
328 /* UTF-8 encoded BOM (0xEF 0xBB 0xBF) as a signature to mark the beginning of a UTF-8 file */
329 if (g_str_has_prefix(tmp
, "\xEF\xBB\xBF")) {
330 html
= g_strdup(tmp
+3);
332 html
= g_strdup(tmp
);
335 tmp
= g_strstrip(sipe_backend_markup_strip_html(html
));
337 cal
->oof_note
= g_markup_escape_text(tmp
, -1);
341 if (sipe_strequal(cal
->oof_state
, "Scheduled")
342 && (xn_duration
= sipe_xml_child(resp
, "OofSettings/Duration")))
344 char *tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "StartTime"));
345 cal
->oof_start
= sipe_utils_str_to_time(tmp
);
348 tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "EndTime"));
349 cal
->oof_end
= sipe_utils_str_to_time(tmp
);
353 if (!sipe_strequal(old_note
, cal
->oof_note
)) { /* oof note changed */
354 cal
->updated
= time(NULL
);
355 cal
->published
= FALSE
;
361 cal
->state
= SIPE_EWS_STATE_OOF_SUCCESS
;
362 sipe_ews_run_state_machine(cal
);
365 cal
->state
= SIPE_EWS_STATE_OOF_FAILURE
;
366 sipe_ews_run_state_machine(cal
);
370 static void sipe_ews_process_autodiscover(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
372 SIPE_UNUSED_PARAMETER GSList
*headers
,
376 struct sipe_calendar
*cal
= data
;
378 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_autodiscover: cb started.");
382 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
383 const sipe_xml
*node
;
384 /** ref: [MS-OXDSCLI] */
385 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
387 /* Autodiscover/Response/User/LegacyDN (trim()) */
388 cal
->legacy_dn
= sipe_xml_data(sipe_xml_child(xml
, "Response/User/LegacyDN"));
389 cal
->legacy_dn
= cal
->legacy_dn
? g_strstrip(cal
->legacy_dn
) : NULL
;
392 for (node
= sipe_xml_child(xml
, "Response/Account/Protocol");
394 node
= sipe_xml_twin(node
))
396 char *type
= sipe_xml_data(sipe_xml_child(node
, "Type"));
397 if (sipe_strequal("EXCH", type
)) {
398 cal
->as_url
= sipe_xml_data(sipe_xml_child(node
, "ASUrl"));
399 cal
->oof_url
= sipe_xml_data(sipe_xml_child(node
, "OOFUrl"));
400 cal
->oab_url
= sipe_xml_data(sipe_xml_child(node
, "OABUrl"));
402 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:as_url %s",
403 cal
->as_url
? cal
->as_url
: "");
404 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oof_url %s",
405 cal
->oof_url
? cal
->oof_url
: "");
406 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oab_url %s",
407 cal
->oab_url
? cal
->oab_url
: "");
419 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
420 sipe_ews_run_state_machine(cal
);
422 } else if ((status
== SIPE_HTTP_STATUS_CLIENT_FORBIDDEN
) && cal
->retry
) {
424 * Authentication succeeded but we still weren't allowed to view the page.
425 * At least at our work place this error is temporary, i.e. the next access
426 * with the exact same authentication succeeds. Simply retry once.
428 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_autodiscover: failed, let's retry once.");
429 sipe_ews_run_state_machine(cal
);
433 switch (cal
->auto_disco_method
) {
435 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
; break;
437 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
; break;
439 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_3_FAILURE
; break;
441 sipe_ews_run_state_machine(cal
);
445 static void sipe_ews_send_http_request(struct sipe_calendar
*cal
)
448 sipe_cal_http_authentication(cal
);
449 sipe_http_request_allow_redirect(cal
->request
);
450 sipe_http_request_ready(cal
->request
);
454 static void sipe_ews_do_autodiscover(struct sipe_calendar
*cal
,
455 const gchar
*autodiscover_url
)
459 SIPE_DEBUG_INFO("sipe_ews_do_autodiscover: going autodiscover url=%s",
460 autodiscover_url
? autodiscover_url
: "");
462 body
= g_strdup_printf(SIPE_EWS_AUTODISCOVER_REQUEST
, cal
->email
);
463 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
468 sipe_ews_process_autodiscover
,
472 sipe_ews_send_http_request(cal
);
475 static void sipe_ews_do_avail_request(struct sipe_calendar
*cal
)
480 time_t now
= time(NULL
);
485 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_avail_request: going Availability req.");
487 now_tm
= gmtime(&now
);
488 /* start -1 day, 00:00:00 */
492 cal
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
493 cal
->fb_start
-= 24*60*60;
494 /* end = start + 4 days - 1 sec */
495 end
= cal
->fb_start
+ SIPE_FREE_BUSY_PERIOD_SEC
- 1;
497 start_str
= sipe_utils_time_to_str(cal
->fb_start
);
498 end_str
= sipe_utils_time_to_str(end
);
500 body
= g_strdup_printf(SIPE_EWS_USER_AVAILABILITY_REQUEST
, cal
->email
, start_str
, end_str
);
501 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
505 "text/xml; charset=UTF-8",
506 sipe_ews_process_avail_response
,
512 sipe_ews_send_http_request(cal
);
516 static void sipe_ews_do_oof_request(struct sipe_calendar
*cal
)
521 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_oof_request: going OOF req.");
523 body
= g_strdup_printf(SIPE_EWS_USER_OOF_SETTINGS_REQUEST
, cal
->email
);
524 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
528 "text/xml; charset=UTF-8",
529 sipe_ews_process_oof_response
,
533 sipe_ews_send_http_request(cal
);
538 sipe_ews_run_state_machine(struct sipe_calendar
*cal
)
540 switch (cal
->state
) {
541 case SIPE_EWS_STATE_NONE
:
543 char *maildomain
= strstr(cal
->email
, "@") + 1;
544 char *autodisc_url
= g_strdup_printf("https://Autodiscover.%s/Autodiscover/Autodiscover.xml", maildomain
);
547 cal
->auto_disco_method
= 1;
549 sipe_ews_do_autodiscover(cal
, autodisc_url
);
551 g_free(autodisc_url
);
554 case SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
:
556 char *maildomain
= strstr(cal
->email
, "@") + 1;
557 char *autodisc_url
= g_strdup_printf("http://Autodiscover.%s/Autodiscover/Autodiscover.xml", maildomain
);
560 cal
->auto_disco_method
= 2;
562 sipe_ews_do_autodiscover(cal
, autodisc_url
);
564 g_free(autodisc_url
);
567 case SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
:
569 char *maildomain
= strstr(cal
->email
, "@") + 1;
570 char *autodisc_url
= g_strdup_printf("https://%s/Autodiscover/Autodiscover.xml", maildomain
);
573 cal
->auto_disco_method
= 3;
575 sipe_ews_do_autodiscover(cal
, autodisc_url
);
577 g_free(autodisc_url
);
580 case SIPE_EWS_STATE_AUTODISCOVER_3_FAILURE
:
581 case SIPE_EWS_STATE_AVAILABILITY_FAILURE
:
582 case SIPE_EWS_STATE_OOF_FAILURE
:
583 cal
->is_ews_disabled
= TRUE
;
585 case SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
:
586 sipe_ews_do_avail_request(cal
);
588 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS
:
589 sipe_ews_do_oof_request(cal
);
591 case SIPE_EWS_STATE_OOF_SUCCESS
:
593 struct sipe_core_private
*sipe_private
= cal
->sipe_private
;
595 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
596 cal
->is_updated
= TRUE
;
597 sipe_cal_presence_publish(sipe_private
, TRUE
);
604 sipe_ews_update_calendar(struct sipe_core_private
*sipe_private
)
606 //char *autodisc_srv = g_strdup_printf("_autodiscover._tcp.%s", maildomain);
609 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: started.");
611 if (sipe_cal_calendar_init(sipe_private
, &has_url
)) {
613 sipe_private
->calendar
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
617 if (sipe_private
->calendar
->is_ews_disabled
) {
618 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: disabled, exiting.");
622 sipe_ews_run_state_machine(sipe_private
->calendar
);
624 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: finished.");