#!/bin/sh ########################################################################################################################## # adduser # # Gaurang K. Pandya [gaubrig at yahoo.com] # ########################################################################################################################## # # # This script reads sAMAccounName from Microsoft Active Directory Services and adds them to local unix /etc/passwd file # # # ########################################################################################################################## # ::NOTE:: # # This scripts does not expect domain name to be in FQDN format. That is if domain name is domain.com then enter just # # "domain" here in script, and the users that get created with this script will have "/domain" as their shell. # ########################################################################################################################## # ::DISCLAIMER:: # # This script has been provided "AS IS" and for free. Use it at YOUR own risk. Works well for me, # # your's *MAY* be different!!!. You are expected to test it before using it in live production server. # ########################################################################################################################## # Latest copy of this script can be found at # # # # http://www.geocities.com/gaurangpandya/scripts/adduser.htm # # # # Let me know your commants/suggesions about the script and if you make any changes to it. # # # # You may not like the way I am adding users with out checking if the already exist or not but it does not make # # any difference (I think) because if it already exist it will not add otherwise add it. There is one limitation to it. # # that is it will not add users if username is longer than 16 characters. I have add it because most *nix OS has # # that limitation. If yours is different case, please change it accordingly. # # # ########################################################################################################################## ################################### Change this according to your environment ############################################ sam_file="/etc/sam" w2kdomain="domain" logfile="/var/log/addusr.log" w2kusrname="domain\\username" w2kpasswd="password" dchost="my.ads.server" base="dc=example,dc=com" ########################################################################################################################## ldapsearch -w $w2kpasswd -D $w2kusrname -x -h $dchost -b $base "(&(objectCategory=person)(objectClass=user)(sAMAccountName=*))" sAMAccountName | grep sAMAccountName | sed -e "/sAMAccountName/s///" | sed -e "/:/s///" | sed -e "/ /s///" | grep -v \# | grep -v \\$ | tr "[:upper:]" "[:lower:]" | sort > $sam_file file_length=`wc -l $sam_file | cut -c1-8` file_length=`expr $file_length + 0` while [ $file_length -ne 0 ] do line=`head -$file_length $sam_file | tail -1` ################### Core things Here :) ################# length=`echo "$line" | wc -c | cut -c1-8` length=`expr $length - 1` if [ $length -lt 17 ] then pw useradd -q -n $line -m -g mailusers -s /$w2kdomain else echo "$0:`date`: can not add user $line, user name exceeds 16 characters" >> $logfile fi #################### End of Core things ################ file_length=`expr $file_length - 1` done rm $sam_file ####################################################### This is specific to qmail ######################################## #/var/qmail/bin/qmail-pw2u < /etc/passwd >/var/qmail/users/assign #/var/qmail/bin/qmail-newu ##########################################################################################################################