ews autodiscovery script proposal

Asked by Mike Usik

Hi folk.

It is quite hard for new user to configure evolution-ews plugin and start reading mail from MS Exchange.
I tried to start using evolution-ews in big organization and realized that there is no any autodiscovery mechanism.
So my main goal is Outlook-like user expirence for new users.
Users computers are joined to AD domain with samba+winbind.

I wrote script, and put it into autostart folder in /etc/skel/.config/autostart-scripts/ (in KDE environment)

My script can be found there: https://wiki.autosys.tk/doku.php?id=linux_faq:evolution_preconfigure
So I want to know - may be it is usefull for community...

--------------------------------------------------------------------------------
#! /bin/bash

export GIO_USE_NETWORK_MONITOR=base
DOMAINNAME=`hostname -d`
########################################################################
### Check if Evolution EWS source files already exist. If exist - exit 0
#######################################################################
for f in ~/.config/evolution/sources/*$HOSTNAME.source; do [ -e "$f" ] && exit 0 ; break; done

##########################################
## Check if connected to AD
##########################################
if ! wbinfo -P; then
echo "NETLOGON test failed" >> ~/.ews_setup.log
else
echo "NETLOGON test OK" >> ~/.ews_setup.log

CURRENT_DC=`wbinfo -P | awk '{print $9}' | awk -F "\"" '{print $2}'`
FULL_NAME=`wbinfo -i $USER | awk -F ":" '{print $5}'`
BASEDN=`echo $CURRENT_DC | sed s/"\."/,dc=/g | sed -r 's!^[^dc=]+!!'`
MAIL=`ldapsearch -h $CURRENT_DC -b "$BASEDN" "sAMAccountName=$USER" | grep mail: | awk '{print $2 }'`
NAME_PREFIX=`date +%s.%N | cut -c 1-16`

#######################################################################
### MS Exchange autodiscovery
#### Joined AD with samba/winbind and have package gss-ntlmssp
#######################################################################
AUTOD_URL="https://autodiscover.`echo $MAIL | sed 's/^.*@//'`"/autodiscover/autodiscover.xml
REQUEST=$(cat <<EOF
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006">
<Request>
<EMailAddress>$MAIL</EMailAddress>
<AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>
</Request>
</Autodiscover>
EOF
)

bash -c "curl -k -d '$REQUEST' --header \"Content-Type: text/xml\" -s --negotiate -u : $AUTOD_URL" > ~/.autodiscover.xml
OABUrl=$(cat ~/.autodiscover.xml | grep -m 1 OABUrl | awk -F '[<>]' '{ print $3 }')oab.xml
EwsUrl=$(cat ~/.autodiscover.xml | grep -m 1 EwsUrl | awk -F '[<>]' '{ print $3 }')
EwsHost=$(echo $EwsUrl | awk -F '/' '{ print $3 }')
rm ~/.autodiscover.xml

echo CURRENT_DC - $CURRENT_DC > ~/.ews_setup.log
echo FULL_NAME - $FULL_NAME >> ~/.ews_setup.log
echo BASEDN - $BASEDN >> ~/.ews_setup.log
echo MAIL - $MAIL >> ~/.ews_setup.log
echo DOMAINNAME - $DOMAINNAME >> ~/.ews_setup.log
echo OABUrl - $OABUrl >> ~/.ews_setup.log
echo EwsUrl - $EwsUrl >> ~/.ews_setup.log
echo EwsHost - $EwsHost >> ~/.ews_setup.log
################################################################
### Check URLs format
################################################################
echo $OABUrl | grep -E '(https|http)://(([[:alnum:]]|-|_|\.|~|!|\*|;|:|@|&|=|+|$|,|/|\?|%|#|\[|\]])*/(oab|OAB)([[:alnum:]]|-|_|\.|~|!|\*|;|:|@|&|=|+|$|,|/|\?|%|#|\[|\]])*/oab.xml)'
OAB_URL_Check=$?
echo $EwsUrl | grep -E '(https|http)://(([[:alnum:]]|-|_|\.|~|!|\*|;|:|@|&|=|+|$|,|/|\?|%|#|\[|\]])*/(ews|EWS)([[:alnum:]]|-|_|\.|~|!|\*|;|:|@|&|=|+|$|,|/|\?|%|#|\[|\]])*/exchange.asmx)'
EWS_URL_Check=$?

if [ $OAB_URL_Check != 0 ] || [ $EWS_URL_Check != 0 ]; then
echo "OAB and EWS URLs check failed... Exit..." >> ~/.ews_setup.log
else
echo "OAB and EWS URLs check OK" >> ~/.ews_setup.log

######################################################################
### CleaningUp and creating evolution source files
######################################################################
killall evolution-source-registry
rm -Rf ~/.config/evolution/sources
mkdir --parents ~/.config/evolution/sources

#####################################################################################
cat <<EOF > ~/.config/evolution/sources/$NAME_PREFIX.1@$HOSTNAME.source
[Data Source]
DisplayName=$MAIL
Enabled=true
Parent=

[Offline]
StaySynchronized=true

[Authentication]
Host=$EwsHost
Method=GSSAPI
Port=443
ProxyUid=system-proxy
RememberPassword=true
User=$USER
CredentialName=

[Collection]
BackendName=ews
CalendarEnabled=true
ContactsEnabled=true
Identity=$USER
MailEnabled=true

[Security]
Method=none

[Ews Backend]
FilterInbox=true
StoreChangesInterval=3
CheckAll=true
ListenNotifications=true
Email=$MAIL
FilterJunk=true
FilterJunkInbox=false
FoldersInitialized=true
GalUid=$NAME_PREFIX.0@$HOSTNAME
Hosturl=$EwsUrl
Oaburl=$OABUrl
OabOffline=true
OalSelected=
Timeout=300
UseImpersonation=false
ImpersonateUser=
EOF

######################################################################
cat <<EOF > ~/.config/evolution/sources/$NAME_PREFIX.0@$HOSTNAME.source
[Data Source]
DisplayName=$MAIL
Enabled=true
Parent=$NAME_PREFIX.1@$HOSTNAME

[Mail Composition]
Bcc=
Cc=
DraftsFolder=folder://$NAME_PREFIX.3%40$HOSTNAME/%d0%a7%d0%b5%d1%80%d0%bd%d0%be%d0%b2%d0%b8%d0%ba%d0%b8
SignImip=true
TemplatesFolder=folder://local/Templates

[Mail Identity]
Address=$MAIL
Name=$FULL_NAME
Organization=
ReplyTo=
SignatureUid=$NAME_PREFIX.33@$HOSTNAME

[Mail Submission]
SentFolder=folder://$NAME_PREFIX.3%40$HOSTNAME/%d0%9e%d1%82%d0%bf%d1%80%d0%b0%d0%b2%d0%bb%d0%b5%d0%bd%d0%bd%d1%8b%d0%b5
TransportUid=$NAME_PREFIX.13@$HOSTNAME
RepliesToOriginFolder=false
EOF

######################################################################
cat <<EOF > ~/.config/evolution/sources/$NAME_PREFIX.3@$HOSTNAME.source
[Data Source]
DisplayName=$MAIL
Enabled=true
Parent=$NAME_PREFIX.1@$HOSTNAME

[Refresh]
Enabled=true
IntervalMinutes=3

[Mail Account]
BackendName=ews
IdentityUid=$NAME_PREFIX.0@$HOSTNAME
ArchiveFolder=
EOF

###############################################################################
cat <<EOF > ~/.config/evolution/sources/$NAME_PREFIX.13@$HOSTNAME.source
[Data Source]
DisplayName=$MAIL
Enabled=true
Parent=$NAME_PREFIX.1@$HOSTNAME

[Mail Transport]
BackendName=ews
EOF

################################################################################
cat <<EOF > ~/.config/evolution/sources/local.source
# Special built-in mail store.
[Data Source]
DisplayName=On This Computer
Enabled=false
Parent=

[Mail Account]
BackendName=maildir
IdentityUid=self
ArchiveFolder=

[Maildir Backend]
FilterInbox=true
Path=$HOME/.local/share/evolution/mail/local
EOF

########################################################################
cat <<EOF > ~/.config/evolution/sources/vfolder.source
# Special built-in mail store.

[Data Source]
DisplayName=Search Folders
Enabled=false
Parent=

[Mail Account]
BackendName=vfolder
IdentityUid=self
ArchiveFolder=

[Vfolder Backend]
FilterInbox=true
EOF

##########################################################################
mkdir --parents ~/.config/evolution/mail/
cat <<EOF > ~/.config/evolution/mail/state.ini
[GlobalFolder]
GroupByThreads=false
PreviewVisible=true

[Store $NAME_PREFIX.3@$HOSTNAME]
Expanded=true

[Search Bar]
SearchScope=mail-scope-current-folder
SearchOption=mail-search-subject-or-addresses-contain

[Folder Tree]
Selected=folder://$NAME_PREFIX.3%40$HOSTNAME/%d0%92%d1%85%d0%be%d0%b4%d1%8f%d1%89%d0%b8%d0%b5

[Folder folder://$NAME_PREFIX.3%40$HOSTNAME/%d0%92%d1%85%d0%be%d0%b4%d1%8f%d1%89%d0%b8%d0%b5]
GroupByThreads=false
PreviewVisible=true
Expanded=true

EOF

######################################################################
### Create Signature
cat <<EOF > ~/.config/evolution/sources/$NAME_PREFIX.33@$HOSTNAME.source
[Data Source]
DisplayName=$MAIL
Enabled=true
Parent=

[Mail Signature]
MimeType=text/html
EOF

mkdir --parents ~/.config/evolution/signatures/
cat <<EOF > ~/.config/evolution/signatures/$NAME_PREFIX.33@$HOSTNAME
<html><head></head><body>SIGNATURE</body></html>
EOF

fi
fi

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu evolution-ews Edit question
Assignee:
No assignee Edit question
Solved by:
actionparsnip
Solved:
Last query:
Last reply:
Revision history for this message
Best actionparsnip (andrew-woodhead666) said :
#1

I suggest you report a bug. Add your script as a fix

Revision history for this message
Mike Usik (mike.obninsk) said :
#2

Thanks actionparsnip, that solved my question.

Revision history for this message
Glyn M Burton (modiford) said :
#3

Not wishing to start a new thread unnecessarily, this had "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a" in reference to "EWS", matching the issue I am having.

Ubuntu 18.04 using the Gnome Online Accounts settings to add Microsoft Exchange.
First the Connect states "Error connecting to Microsoft Exchange server: Invalid certificate", giving the option to Ignore. The certificate is a Comodo one.

Pressing Ignore states "Error connecting to Microsoft Exchange server: Code 8: Unexpected response from server". Clicking Try Again loops through from the above again.

Running 'gnome-control-center' with -vvvvv gives indication of a HTTP/500 error and some output, in particular, "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a". If connected to in a browser states "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." (presumably with a status of 500).

"
(gnome-control-center:32255): GoaBackend-WARNING **: 17:36:10.485: goa_ews_client_autodiscover() failed: 500 — Internal Server Error
(gnome-control-center:32255): GoaBackend-WARNING **: 17:36:34.727: goa_ews_client_autodiscover() failed: 500 — Internal Server Error
(gnome-control-center:32255): GoaBackend-WARNING **: 17:36:34.978: goa_ews_client_autodiscover() failed: 8 — Message Corrupt
"

I was unable to find out if the Microsoft Schema site is retired, moved or what the alternative is.

Revision history for this message
Glyn M Burton (modiford) said :
#4

After further researching, I understand 'EWS' is the new 'ActiveSync' and they are very different protocols.

Unfortunately for me, a Kopano and Z-Push owner, there is as yet no EWS equivalent.

Perhaps the configuration utility could be more specific in it's error reporting? "No EWS compatible service found" may be more useful.