Usually people manages Linux boxes using Windows clients but sometimes, someone (like me, for example) needs to manage a Windows server from a Linux host (it could be a normal client or another server which wants to talk to Windows).
IMO, Active Directory is one of the best product from Microsoft, since it’s based on a well known standard like X.500 (aka LDAP) and it has a good interoperabilty (although it could be better, see all the problems Samba people had in the past). So, even if there are tools like PHPLdapAdmin which are pretty good, if you need to automate users and groups management, there’s nothing better than a command line tool. Enter adtool.
adtool is very simple to use, but it’s not so simple to have it up&running, because this involves, amongst other things, to activate Secure LDAP in your Active Directory installation. To do this, you can follow this guide which will lead you through all the steps you have to do to enable LDAPS in Windows Server 2003. It may look scary but it works indeed, I used it myself.
Then, install adtool. In Debian/Ubuntu
# aptitude install adtool
Probably adtool is already present in your distribution’s repositories, so use your package manager. In the case it’s not present, simply download the adtool tarball from its homepage and do the usual
$ tar xzvf adtool-1.3.tar.gz
$ cd adtool-1.3
# make install
It should be quite straightforward.
Now we have everything installed, so we can configure adtool.
/etc/adtool.cfg or, even better
$HOME/.adtool.cfg because it will contain sensitive information, so lock it up to the user you’re willing to employ to modify Active Directory.
Put this in the config file (adapt to your needs)
As you can see we are using LDAPS here, because otherwise some adtool features like changing users’ passwords wouldn’t be available.
You don’t necessarily have to use the Administrator account, you can use whatever account you want, it just needs to have the right permissions (create user, change passwords etc).
So you can start poking your AD from the Linux command line, like this:
# create a new user with a dn like cn=$NAME,ou=$DEP,dc=domain,dc=tld
$ adtool usercreate "$NAME" "ou=$DEP,dc=domain,dc=tld"
# set user logon password
$ adtool setpass "$NAME" $my_secret_password
# to unlock the account (locked by default)
$ adtool userunlock "$NAME"
# to disable all the "account options" in the user's account tabe. Amongst them the "Password never expires" which is again enabled by default
$ adtool attributereplace "$NAME" userAccountControl 512
# to set user's mail address
$ adtool attributeadd "$NAME" mail firstname.lastname@example.org
# add the user to a group of users
$ adtool groupadduser $my_group "$NAME"
This could be very useful for user scripting and system integration if you’re in a mixed environment, just like we are.