Saturday 27 March 2010

Who needs help with the Haddock!

Hello it's your friendly neighbourhood techhie again!
Well a passionate one at any rate.
So one of the issues which I am sure everyone who is a sys admin on AD hits at some point scripting in their life is that darn long LDAP address you need to modify objects.
Remind you of how annoying it is to take that entire object string from the AD Object in AD Users and Computers (or as I lovingly call it ADUC pron: Haddock without the 'H')
Well here is a tool you can write very simply using the wonderful Visual C# Express Edition 2009.
Now before you all go arrrrgggghhhhh progamming or the other word argh another programming language. Everyone just take a deep breath and quote the words from the "Hitchhikers Guide to the Galaxy." which is "Don't Panic!"
First things first, download and install the Visual C# Express Edition 2008 from the Microsoft site. Install it with all the typical settings.
Once this is done create a new project using Windows Form Application template and give it a name something like AD2LDAP.

This form will need two text boxes and two command buttons.
One of the Text boxes will be called Username where you will enter the username of the user.
The second Text Box will be called LDAPPathText which will display the full path of the user using the standard LDAP display format.

Now to start searching the Active Directory you need to add a reference to the DirectoryServices Assembly. You do this by clicking on Project...Add Reference.
So now you now have a form To look something like this. I have added some niceties to this like some labels but the application should work as long as you have the components listed above.
We now need to confirm that the “System.DirectoryServices” assembly has been added to the form for it to use the searching abilities.
You can do this by looking at the associated cs file for the form. The easiest way is to double click on any of the buttons we have on the form.
What you should see is the source code associated with the form.
Over here at the top you can see the line
using System.DirectoryServices;

This basically states that the Directory Services Assembly is now available for use in this application. If you do not see it there just go ahead and type it.
Now what we are going to do is to create a generic search function which checks if the object exists in the local Active Directory. We will call this function IsExistInAD which will return a boolen response (i.e yes or no, true or false) where it will take an normal string argument (i.e. a word, sentence, etc) and search for it in the local Active Directory structure and say yes it exists or no it doesn’t.
What I will show you a sample code here and then explain it in detail.
bool IsExistInAD(string loginName)
{
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(SAMAccountName={0})", loginName);
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

if (result == null)
{
return false;
}

return true;
}

What you have to do is create a DirectorySearcher Component called search which will have all the characteristics and attributes of a DirectorySearcher component.
DirectorySearcher search = new DirectorySearcher();

We will then set it up to search for only the username object in Active Directory.
search.Filter = String.Format("(SAMAccountName={0})", loginName);
Out of it we only want it to return the canonical name (“cn”) of the object which in other words is the LDAP string we are after.
search.PropertiesToLoad.Add("cn");
We will then use the search function to locate the username and see if it exists in the current domain an return the result to an object of type SearchResult.
SearchResult result = search.FindOne();
We shall now see if the result of this has been successful in finding the canonical name of this user. If it hasn’t been successful then the SearchResult object result should be empty. If it is empty then the user has not been found so we can return a false result to say the user doesn’t exist.
if (result == null)
{
return false;
}

Otherwise in all other instances we can return the user exists.
return true;

So this allows us to test to see if the user exists in AD.
Let us now look at how we locate the LDAP path for the user using one of the buttons.
private void button1_Click(object sender, EventArgs e)
{

if (UserName.Text.Length > 0)
{
if (!IsExistInAD(UserName.Text.ToString()))
{
UserName.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("User Not Found! Please enter the exact username of the user. ", "User Check");
}
else
{
UserName.ForeColor = System.Drawing.Color.Blue;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(SAMAccountName={0})", UserName.Text.ToString());
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
LDAPPathText.Text = result.Path.ToString();
}
}
else
{
MessageBox.Show("User Name not Entered! Please enter the exact username of the user. ", "User Check");
}
}

As before in the function to check if the user exists in AD we use the same method however we use the generic function IsExistInAD to check if the user exists before we continue to return LDAP Path in one of our text boxes to use.
if (!IsExistInAD(UserName.Text.ToString()))
{
UserName.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("User Not Found! Please enter the exact username of the user. ", "User Check");
}

So in general what we want is for the application to return a error message if the user doesn’t exist and to validate for conditions when nothing is entered in the username text box and all other conditions before we perform the search.
So this is a simple form of searching for users. Of course this is not what you might call very secure in the sense of handling usernames and passwords, defining the domain to search etc. But it is a good platform to start writing your own applications using the power of Active Directory and c#

Here is the source code in case you need it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;


namespace ADtoLDAP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

bool IsExistInAD(string loginName)
{
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(SAMAccountName={0})", loginName);
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

if (result == null)
{
return false;
}

return true;
}


private void button1_Click(object sender, EventArgs e)
{

if (UserName.Text.Length > 0)
{
if (!IsExistInAD(UserName.Text.ToString()))
{
UserName.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("User Not Found! Please enter the exact username of the user. ", "User Check");
}
else
{
UserName.ForeColor = System.Drawing.Color.Blue;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(SAMAccountName={0})", UserName.Text.ToString());
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
LDAPPathText.Text = result.Path.ToString();
}
}
else
{
MessageBox.Show("User Name not Entered! Please enter the exact username of the user. ", "User Check");
}
}


private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}

Sunday 7 March 2010

Where are you from?

This is a question that most people get asked at some point in their lives. The context they get asked however can be quite varying...

Ranging from local Chav's asking the question near a job centre followed in the same sentence by the usual statement "Go Home Paki!" to the more curious question trying to understand where you got here.

There used to be a time when those words "Go Home Paki!" used to hurt but then the more I thought about it the more I laugh...because seriously for me to do that I would have to go back in time to when my ancestors left Baluchistan several hundred or even millennia before. If anything I am sufficiently glad that they ask the question "Where are you from?" it shows that in their world things have moved on just a tad bit!

So moving on I came across this article in the Guardian this morning and immediately got thinking hmm how would I explain my history and how I got here...

Well bear with me a bit and also since a lot of this has been lost through the mists of time it may not be a fact the same as it might not all be fiction.

So my ancestors basically to escape a God in Human form who took to not liking the particular caste my ancestors belonged to went on a huge killing spree! To escape from this God who was on a killing spree my ancestors sought refuge from a local goddess in a temple who said she will protect my ancestors if they give up the sword for a needle and thread. Still with me so far?

If not then here is the long version of it here.

Somewhere along the way the descendants of my ancestors in what is now Modern Day Pakistan ended up joining the rulers of the new Maratha Confederacy whose aim was to oust the Mughal Empire and establish a separate state from all external influence. Well lets just say that but for a brief period of time this meritocracy didn't quite work out and eventually people got scattered all over Pre 1947 India.

Some time during this my ancestors had settled down south and basically hung about as things were better or they got bored moving or whatever their reasons were they got integrated with the local culture and hey presto it leads to me and my parents where the gene that makes us want to move got activated again and hey presto here I am.

So really it gets really complicated when some chav decides to tell me "Go Home Paki" as it would mean me travelling through time and space for me to reach a physical location but then the State of Pakistan did not actually exist in that time so how can I?

Tuesday 2 March 2010

Aaromale Lyrics

Ok A huge diversion. A.R.Rahman has released some really good music for the film Vinnaithandi Varuvaiya (Tamil - 2010)

A particular tune has been stuck in my head for like a very long time now so I thought I would share the lyrics. Very nice song in the Malayalam Language!

The Lyrics are here now (Thanks Rajmohan Kanthaswamy & Vijay Ganesh Subramanian for the lyrics and translation)
Listen here: http://www.tunewiki.com/lyrics/alphons-joseph/aaromale-s2118982.aspx
Also remember that things may sound strange or as I like to call it get lost in translation. For the moment enjoy this superb tune!

Mamalayeri Varum Thennal
(A gentle breeze, blowing in through the mountains.)
Puthu Manavalan Thennal, ...
( A tender breeze, decked up like a bridegroom)
Palli Medaye Thottu Thalodi Kurushil Thozhuthu Varumbol,
(When it arrives after worshipping the cross at the altar..)
Varavelpinu MalayalaKara Manasammatham Choriyum,
( This land of Kerala will grant permission for a warm welcome )

Aaromalae, Aaromalae, Aaromalae, Aaromalae....
(O Beloved....O Beloved...O Beloved.....O Beloved...)

Swasthi Swasthi Su Muhurtham,
( Auspicious is this solemn occasion )
Sumungali Bhava, Manavatti
(O Bride, may you be blessed with a long wedded life)
Swasthi Swasthi Su Muhurtham,
( Auspicious is this solemn occasion )
Sumungali Bhava Manavatti
(O Bride, May you be blessed with a long wedded life)

Shyama Rathri Than Aramanayil,
( In the inner sanctum of the dark night )
Mari Nilkayo Tharakame,
( O Star..why are you staying away ? )
Pulari Manjillae Kathiroliyay,
( Like a ray of light in the morning mist,)
Akalae Nilkayo Penmaname,
( Are you standing afar, O Lady ? )

Chanju Nilkuma Chillayil Nee, Chila Chilambiyo Poonkuyilae
(On the leaning bough, O Cuckoo bird, did you sing so sweetly ? )
Manchiragile, Marayoliyae Thediyathiyo Poorangal
(Did the festivities arrive in search of the earthern lamp's flame ?)

Swasthi Swasthi Su Muhurtham,
( Auspicious is this solemn occasion )
Sumungali Bhava Manavatti
(O Bride, May you be blessed with a long wedded life)

Aaromalae... Aaromalae....
(O Beloved....O Beloved....)


Kadalinu Karayodiniyum Padan Sneham Undo ?
(Does the sea still possess the love to serenade the shore ?)
Mezhukuthurikalayi Urukan Iniyum Pranayam Manasil Undo ?
(Is there still love in the mind..to be melted like candle wax ?)

Aaromalae.. Aaromalaeee.. Aaromalaee
(O Beloved....O Beloved...O Beloved.....O Beloved...)
Aaromalae.. Ohh.. Ho ! - (O Beloved....)