Thursday, December 22, 2005

Postmaster NDR resubmit Tool

Hot on the heels of last weeks resubmit tool I decided to rework this a little so it could also be used to resubmit messages from an NDR notification that may have been wrongly addressed. If you have configured your SMTP virtual servers to send a copy of Non delivery Recipients to a mailbox you’ll get a copy of all the NDR’s your server produces. Now in a large environment this is probably next to useless because of the number of messages you may receive but in small companies it may be a reasonable thing to do to allow you catch mis-addressed emails. But once you have received a badly addressed email if you try to resubmit that email from the NDR using Outlook (Resend) the sender address will be set to your email address which may confuse replies. So what this tool does it firstly extracts the original message from the NDR (if possible) and then allows you to specify the proper email address you want to submit it to and it will then resubmit the email so when it arrives at the recipients mailbox it will have the correct sender address.

Technically this works the same as the post from last week it uses two ASP pages and Exoledb in the background to query a folder for NDR messages. And then it use CDOEX to extract the original message from the NDR and then it uses the stream interface to submit the message to the pick directory.

Using the tool

The two asp pages that make up this tool use Exoledb as the underlying mechanism to access the Exchange Store. This means that the pages must be run locally on an Exchange server where the postmaster or notifcation mailbox is located. To run the tool it needs to be put into a directory usually under the default website (eg create a directory under wwwroot called ndrresub) that has NTLM authentication enabled (via IIS admin). Because this was a really simple tool I’ve also hard coded the Pickup directory in the ASP file so if you have the pick directory set to any other directory other then “c:\Program Files\Exchsrvr\Mailroot\vsi 1\PickUp” you need to change the following line in expresub.asp file

dim rec,oCon,Href,msgobj,resub,Subject,Toaddress,pickupdirectory,rfcmsg
pickupdirectory = "c:\Program Files\Exchsrvr\Mailroot\vsi 1\PickUp"

This tool is desinged to be used against the postmasters mailbox so you should have the pages located on the server where the postmaster mailbox is and then to access the mailbox type the OWA url to the inbox eg http://servername/exchange/administrator/inbox. The main page of this application is called ndrmain.asp so if you have placed the files in a directory called ndrresub under wwwroot you should be able to access the tool by using http://servername/ndrresub/ndrmain.asp .

Download

I’ve put a donwloadable copy of the 2 asp pages here


The following code segment illustrates the main differences.

im rec,oCon,Href,msgobj,msgobj1,resub,Subject,Toaddress,pickupdirectory,rfcmsg
pickupdirectory = "c:\Program Files\Exchsrvr\Mailroot\vsi 1\PickUp"
Resub = request.form("resub")
Subject = request.form("Subject")
Toaddress = request.form("Toaddress")
set rec = createobject("ADODB.Record")
Set oCon = CreateObject("ADODB.Connection")
Href = request.querystring("Href")
oCon.ConnectionString = Href
oCon.Provider = "ExOledb.Datasource"
oCon.Open
set msgobj = createobject("CDO.Message")
msgobj.datasource.open Href,oCon,3
set objattachments = msgobj.attachments
for each objattachment in objattachments
if objAttachment.ContentMediaType = "message/rfc822" then
set msgobj1 = createobject("cdo.message")
msgobj1.datasource.OpenObject objattachment, "ibodypart"
exit for
end if
next
if Resub = "Yes" then
response.write "Message Resubmitted to :" & Toaddress
msgobj1.fields("urn:schemas:mailheader:subject") = Subject
msgobj1.fields.update
set stm = msgobj1.getstream
stm.type = 2
stm.Charset = "x-ansi"
rfcmsg = stm.readtext
rfcmsg = "x-sender: " & msgobj1.fields("urn:schemas:httpmail:fromemail") & vbcrlf & rfcmsg
rfcmsg = "x-receiver: " & Toaddress & vbcrlf & rfcmsg
stm.position = 0
stm.writetext = rfcmsg
Randomize ' Initialize random-number generator.
rndval = Int((20000000000 * Rnd) + 1)
stm.savetofile pickupdirectory & "\" & day(now) & month(now) & year(now) & hour(now) & minute(now) & rndval & ".eml"
else

Friday, December 16, 2005

Public folder Mail Re-Submit Tool

I came up with this little application this week to solve one annoying problem I have with a small client that uses an anti-spam application that submits any SPAM it finds to a public folder. As with everything the cheaper the anti-spam software the more chance there will be false positives so occasionally there is a need to go though this public folder and forward any false positives on to the intended receiver. Using the normal Outlook or OWA method will change the reply address’s and also the subject line which is usually also altered by the anti spam program. So what I came up with was two simple ASP pages that could be used to firstly query all the mail in a public folder and then allow an export (which is sometimes useful for checking the X-headers added by this spam program) and also resubmit an email to a desired email address (and allow any fix-up to the subject) buy firstly getting the RFC822 stream of the message via CDOEX and then saving this stream with an added x:sender and x:receiver field to the pickup directory on the server.

The Resubmit Trick

I picked up this resubmit trick by basically copying what the IMF and IMF archive managers do. If you have UCE Archiving enabled with IMF it saves a copy of any SPAM it detects to a directory and writes the sender and reciver address’s as x:sender and x:receiver at the top of the file. If these two fields are at the top of the file when you put a mail into the pickup directory Exchange will process these as the envelope fields and only attempt to deliver the message to the x:receiver address (and not the To and CC fields of the message). I started out thinking I could do this by adding the X-header’s via the fields collection in CDOEX like I usually do which works but created a problem that because using the procedure for some reason added another field to the top of the file before x:sender and x:receiver when exchange processed the email that was placed in the pickup directory it would also try and resend the email to any of the address’s that where in the To and CC field of the message (which is a really bad thing).To fix this problem I ended up getting the stream of the message appending to the front of the steam and then writing it to disk which made sure that these two fields where always at the top of the file

Using the tool

The two asp pages that make up this tool use Exoledb as the underlying mechanism to access the Exchange Store. This means that the pages must be run locally on an Exchange server that has an instance of the public folder you want to access. To run the tool it needs to be put into a directory usually under the default website (eg create a directory under wwwroot called resub) that has NTLM authentication enabled (via IIS admin). Because this was a really simple tool I’ve also hard coded the Pickup directory in the ASP file so if you have the pick directory set to any other directory other then “c:\Program Files\Exchsrvr\Mailroot\vsi 1\PickUp” you need to change the following line in expresub.asp file

dim rec,oCon,Href,msgobj,resub,Subject,Toaddress,pickupdirectory,rfcmsg
pickupdirectory = "c:\Program Files\Exchsrvr\Mailroot\vsi 1\PickUp"

This tool is not just limited to public folders if you have a mailbox you want to re-submit messages from it will also work just enter in the OWA url to the inbox you want (eg: http://servername/exchange/username/inbox ) and press submit. The main page of this application is called resubmain.asp so if you have placed the files in a directory called resub under wwwroot you should be able to access the tool by using http://servername/resub/resubmain.asp .

Limitations

One limitation is that you can only submit a message to one email address and you should only resubmit messages to internal address resubmitting to external address may cause unexpected results.

Changing Export format

Currently the export format is set to .txt files this is mainly because I wanted to be able to view the message in notepad. If you wanted to export the message as a .eml just change the extension in the following line in resubmail.asp and you will then be able to open the message in Mail client such as Outlook Express

Response.AddHeader "Content-Disposition","attachment;filename=export.txt"

Change to

Response.AddHeader "Content-Disposition","attachment;filename=export.eml"

Download

I’ve put a download of the ASP pages here

Sunday, December 11, 2005

Mind the Overlap –2006 Commonwealth Games timezone patch for Daylight Saving and how it affects Exchange and Outlook

This is one for the Aussie’s and anyone who is managing a server on the East coast of Australia. Because of the Commonwealth games next year the date daylight saving starts on is being brought forward 1 week. If anyone remembers back a similar thing was done for the Olympics in 2000. First thing first is what is the overlap period? this is the period between when the new daylight saving starts and the period where it usually starts. This is between the dates 26/03/2006 and 2/04/2006. The long and short of it is that if your users currently have appointments created within this period including recurring appointments (no patches applied) these appointments are going to be out by 1 hour during this overlap period. Microsoft has released a patch that adds a new timezone to the registry but this patch doesn’t retrospectively fix appointments that already exist only those created after the patch has been applied. Because Timezone information isn’t year specific you now run into the problem if your users create appointment in the overlap period in 2007 with the patch applied.

The current KB and Advice from Microsoft can be found at

http://www.microsoft.com/australia/timezone/2005.aspx
http://support.microsoft.com/kb/907494/
http://support.microsoft.com/kb/909915

Here’s my take on this which is absolutely unofficial based on my testing and knowledge, I strongly encourage you to do your own testing to test for yourself the affects of these patches and setting, Also any of the scripts mentioned in this post have had very little real testing. Because I’m a Sydney sider this affects me directly I’ll try to keep this page up to date with any corrections as I learn more.
Depending on your environment you have four areas you may need to worry about with this patch. Desktop, OWA, locally on Exchange Server and Handhelds. Additionally if your using WebDAV or CDOEX to do any appointment creation/modifications you need to apply a hotfix and check your code especially if you’re using hard coded CDO timezone enums.

Desktops- Getting all the desktops your users may be using to access Outlook and OWA updated with this patch (and then removing) it after provides the greatest logistical challenge. The desktop patch affects both Outlook and OWA.
How does the Desktop Patch effect OWA – This is an interesting one because you would think that OWA would only really be affected by the patches on the server well no. When a user tries to create an appointment in Exchange via OWA the browser uses a client side control that pulls information from the time zone on the local machine and the date and the time that is posted to the server is actually the UTC time for the appointment based on the time zone offset from the client. So this means although you may have everything in your company patched if a user walked up to a kiosk machine that isn’t patched and creates an appointment via OWA during the overlap period this appointment would be created 1 hour out no matter what the time zone settings where in OWA. The server-side patch’s for OWA is still equally important however because OWA still does a lot of server side actions that are going to require the right Timezone data. These include free-busy time calculation and also expansion of reoccurring appointments. One example of this is if you have patched your desktops and patched your servers and even applied the hot fix for CDO and OWA if you haven’t updated the timezone for the user in OWA options when OWA goes to expand a reoccurring appointment that has an occurrence in the overlap period it will expand 1 hour out. So to get OWA working properly in the Overlap period with Exchange 2003

1. Update any Desktops that are going to be used to connect to Outlook or OWA
2 Update the Server with the new timezone and enable
3. Apply the hotfix from http://support.microsoft.com/default.aspx?scid=kb;en-us;909933&sd=rss&spid=1773 . Note if you don’t apply the hotfix and try changing the Timezone in OWA my experience was that OWA would no longer create appointments at all so this hotfix is import.
4. Adjust the timezone in OWA – Options

Doing a Bulk Update of OWA Timezone Options
Once you have updated the timezone on the server and applied the hotfix another task that must be done is to update the user’s OWA options. The first time a user connect to OWA its sets the http://schemas.microsoft.com/exchange/timezone property. This is the timezone you see if you go into options in OWA. If you don’t want to rely on your users going in and manually changing this setting you might want to look at a script to do a bulk update of this property. The timezone property itself lives on the root mailbox folder I’ve come up with a ADSI / WebDAV script that will connect to every mailbox on a server and update the timezone setting if it is currently set to one of the 3 affected timezones. This script is written for 2003 so I’ve used the email address in the WebDAV connection string which I find the most reliable way for 2003. If you want to run the script on 2000 you could try using the mailnickname instead. This script takes the name of the server you want to run it against as a commandline parameter and then does a ADSI query to find all the users on that server (not hidden from the addresslist) and then constructs a URL to connect to each user's mailbox and does a propfind on the http://schemas.microsoft.com/exchange/timezone property to determine what the current timezone is and then does a proppatch with the new value if needed. The script needs to be run with a user that has full rights to all mailboxes on a server see http://support.microsoft.com/default.aspx?scid=kb;en-us;262054 for more details. I’ve included the script in the download for this post here

CDOEX /Exoledb and WebDAV

Because CDO maintains an internal table to track time zone information you need a hotfix (for 2003 see) to make sure that any of your CDOEX applications will handle date and times correctly during the overlap period. The CdoTimeZoneId Enum id is stored with any appointments created via OWA and CDO and used by CDO to render the time correctly. What that hotfix does is creates some new CdoTimeZoneId Enum values (and supporting code) to cater for the overlap period. For example the CdoTimeZoneId Enum for Sydney normally is 57 the CdoTimeZoneId Enum the hotfix introduces for Sydney during this period is 78. Again having the correct CdoTimeZoneid Enum set is going to be important if you do an expansion query on a reoccurring appointment with Exoledb or when you go to display an appointment use CDOEX during the overlap period.

Auditing currently affected Appointments

What about appointments that have already been created without that patch being applied in the overlap period. Currently Microsoft recommends that you do an export and import for any affected appointments. Before you do this you might want to identify which users are affected eg those that have appointments created in the Overlap period. For this some Exoldb or WebDAV code can come in handy. I’ve wrote a script that takes the servername or the server you want to analyze as a command-line parameter then uses Exoledb to query that server for any appointments in the overlap period. Because this script uses Exoledb it must be run locally on a Exchange server and must be run with an account that has rights to the users calendar see kb . This script produces a CSV file that lists all the users that have appointments in the overlap period and the details of those appointments. To use the script there is one change you must make before using it which is you must add your primary SMTP domain name in the domainname variable in the script this is used for the Exoledb Connection string. I’ve included a copy of the script in the download for this post here.

Can you identify if an appointment has been created with the patch? Yes and no if the appointment is created with Outlook you can check the following Mapi property http://schemas.microsoft.com/mapi/id/{00062002-0000-0000-C000-000000000046}/0x8234 to see what timezone the appointment was created with. If the appointment was created with OWA then it’s a little more difficult. If the timezone patch and hotfix has been applied to the server and the OWA timezone is set then you can use the urn:schemas:calendar:timezoneid property. For example if your using the new Sydney 2006 timezone when the patch is applied the timezoneid property should be set to 78. The problem is if you have the desktop patch applied but the server unpatched an appointment may be created okay in the overlap period but the CdoTimeZoneId will still be set to 57. Also expanded reoccurring appointments may show nothing depending on the method that has been use to expand them.

Can you update the appointment in the overlap period programmatically? I haven’t quite got to that yet as I’m still in the detection and update period. But in theory it should be possible you’d want to make sure that you where only going to affect appointments created in the overlap period. For example if you wanted to adjust reoccurring appointments that may have been expanded wrong in the overlap period you would need to update the timezone information and them make a change that would cause the appointment to be re-expanded.

Friday, December 02, 2005

Auditing Send-as and Receive As rights via script

A while ago I wrote this script to audit the Mailbox DACL in a reverse fashion so instead of the normal way where you would see these people have rights to this mailbox it did this person has rights to the following mailbox’s. The one thing this script didn’t check was for Send-as and receive-as rights. These particular rights are extended rights there a good description of what an extended right is here. The bit that is needed for this script is (quoted from that article)

“Extended rights are not defined by an access mask. Instead, each extended right is identified by a globally unique identifier (GUID). This GUID corresponds to a controlAccessRight object that is stored in the Extended-Rights container within a forest's Configuration container. An ACE that grants an extended right specifies a GUID corresponding to a particular controlAccessRight object.”

The ACE’s themselves that pertain to send-as and receive-as are added to the Active Directory User object Security descriptor and not the mailbox’s security descriptor (msExchMailboxSecurityDescriptor).
So adapting the script that is used before was pretty easy it’s a matter of changing it from checking the mailbox security descriptor via CDOEXM to just using ADSI to check the user’s security descriptor. Go though all the ACE’s on the DACL of that user and look for any GUID’s that match the send-as and receive as rights see this for a list of all the extended right’s GUIDS. Look for allows which have an AceFlags enum of 5 (6 is for deny) then using the same data-shaping logic I used previously display the result to the commandline.
I’ve put a download of this script here the script itself looks like

Set objSystemInfo = CreateObject("ADSystemInfo")
strdname = objSystemInfo.DomainShortName
set conn1 = createobject("ADODB.Connection")
strConnString = "Data Provider=NONE; Provider=MSDataShape"
conn1.Open strConnString
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strNameingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
Query = "<LDAP://" & strNameingContext & ">;(&(&(& (mailnickname=*) (|
(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*)))

))));samaccountname,displayname,distinguishedName;subtree"
Com.ActiveConnection = Conn
Com.CommandText = Query
Com.Properties("Page Size") = 1000
set objParentRS = createobject("adodb.recordset")
set objChildRS = createobject("adodb.recordset")
strSQL = "SHAPE APPEND" & _
" NEW adVarChar(255) AS UOADDisplayName, " & _
" NEW adVarChar(255) AS UOADTrusteeName, " & _
" ((SHAPE APPEND " & _
" NEW adVarChar(255) AS MRmbox, " & _
" NEW adVarChar(255) AS MRTrusteeName, " & _
" NEW adVarChar(255) AS MRRights, " & _
" NEW adVarChar(255) AS MRAceflags) " & _
" RELATE UOADTrusteeName TO MRTrusteeName) AS rsUOMR"
objParentRS.LockType = 3
objParentRS.Open strSQL, conn1

Set Rs = Com.Execute
While Not Rs.EOF
dn = "LDAP://" & replace(rs.Fields("distinguishedName").Value,"/","\/")
set objuser = getobject(dn)
Set oSecurityDescriptor = objuser.Get("ntSecurityDescriptor")
Set dacl = oSecurityDescriptor.DiscretionaryAcl
Set ace = CreateObject("AccessControlEntry")
objParentRS.addnew
objParentRS("UOADDisplayName") = rs.fields("displayname")
objParentRS("UOADTrusteeName") = strdname & "\" & rs.fields("samaccountname")
objParentRS.update
Set objChildRS = objParentRS("rsUOMR").Value
For Each ace In dacl
if lcase(ace.ObjectType) = "{ab721a54-1e2f-11d0-9819-00aa0040529b}" and
ace.AceType = 5 then
if ace.Trustee <> "NT AUTHORITY\SELF" and ace.AceFlags <> 6 then
objChildRS.addnew
objChildRS("MRmbox") = rs.fields("displayname")
objChildRS("MRTrusteeName") = ace.Trustee
objChildRS("MRRights") = "Send As"
objChildRS("MRAceflags") = ace.AceFlags
objChildRS.update
end if
end if
if lcase(ace.ObjectType) = "{ab721a56-1e2f-11d0-9819-00aa0040529b}" and
ace.AceType = 5 then
if ace.Trustee <> "NT AUTHORITY\SELF" and ace.AceFlags <> 6 then
objChildRS.addnew
objChildRS("MRmbox") = rs.fields("displayname")
objChildRS("MRTrusteeName") = ace.Trustee
objChildRS("MRRights") = "Recieve As"
objChildRS("MRAceflags") = ace.AceFlags
objChildRS.update
end if
end if
Next
rs.movenext
Wend
wscript.echo "Number of Mailboxes Checked " & objParentRS.recordcount
Wscript.echo
objParentRS.MoveFirst
Do While Not objParentRS.EOF
Set objChildRS = objParentRS("rsUOMR").Value
crec = 0
if objChildRS.recordcount <> 0 then wscript.echo objParentRS("UOADDisplayName")
Do While Not objChildRS.EOF
wscript.echo " " & objChildRS.fields("MRmbox")
wscript.echo " -" & objChildRS.fields("MRRights")
objChildRS.movenext
loop
objParentRS.MoveNext
loop