#!/usr/bin/perl -w
#
###############################################################################
#
# by Ray Schneider 07/05/2000  ****a very quick hack, but it should work ****
# 
# This script reads the file /etc/passwd and grabs the userid values and
# name values of each user. The script then attempts to create a ldif file
# for the ldap server to have fed into the database.
#
# Some things to note:
#
#	- the root dn of the ldif file is set by the initial variables
#         set by the user. These are hardcoded in the script and the
#	  beginings of the ldif file are started with this information.
#
# 	- The usual system type entries in the passwd file are ignored.
#	  It is unlikely however possible that the script may generate
#	  some entries that are not wanted due to new entries in the 
# 	  passwd file representing new services offered by a more recent
# 	  version of GNU/Linux OS. This is treatable, via the modification
#	  of a couple lines of code.
#
#       ****************************************************************
#
#	Disclaimer: This script is use at your own risk...I accept no
#	      responsiblity for any headaches or what have you that it
#             may cause...;-)
#            
#     
#       ****************************************************************
#
###############################################################################

# set the below or adjust the script slightly for your own situation ;-)

# set the dn root values.
# dn: dc=yourdomain, dc=com
# dc: yourdomain
# objectclass: organization
# objectclass: dcObject
$dnline="dc=yourdomain, dc=com";
$dcline="yourdomain";
$objectclass1="organization";
$objectclass2="dcObject";

# set organization email postfix
$emailpostfix="yourdomain.com";

# set variables for read files
$passwdfile="/etc/passwd";

#uid value to start parsing from in passwd file
$uidvalue=499;

# set variable for write files
$ldiffile="yourdomainsldif";

# create ldiffile stub with the appropriate dn root set above.
open(LDIFH,">$ldiffile");
print LDIFH "dn: $dnline\n";
print LDIFH "dc: $dcline\n";
print LDIFH "objectclass: $objectclass1\n";
print LDIFH "objectclass: $objectclass2\n";
print LDIFH "\n";
close(LDIFH);

# open files to read from and written to
open (RFH, "<$passwdfile");
open (WFH, ">>$ldiffile");

# values to ignore in the /etc/passwd file (ie. system stuff)
# ignore lines with values: root,bin,daemon,adm,lp,sync,shutdown,halt,mail,
# news,uucp,operator,games,gopher,ftp,nobody,xfs,named,gdm,piranha,postgres,
# pvm,squid, in our case we also want to ignore cyrus, esquire.
# While reading the file, grab the login value and the name values
# insert these into the building ldif file.

while(<RFH>){
	chop; #takes care of trailing newline character
	($login,$passwd,$uid,$gid,$gcos,$home,$shell)=split/:/;
	#print STDOUT "Login: $login Name: $gcos\n";
	if ($uid >$uidvalue){
			
		#split up gcos value into first and last names
		($first,$last)=split(/ /, $gcos);
		#build the ldif file appropriately for our setting
		print WFH "dn: cn=$gcos, dc=$dcline, dc=com\n";
		print WFH "cn: $gcos\n";
		print WFH "sn: $last\n";
		print WFH "mail: $login\@$emailpostfix\n";
		print WFH "objectclass: person\n";
		print WFH "\n";
		
		
	}

}

