#!/usr/bin/perl
# Script to migrate unixauth databse to nwauth databse
# By: M.Weehuizen - NetWin - http://netwinsite.com  	December 2002

# Uncomment and change this to have accounts transferred to nwauth with fully specified paths
$domain='@mydomain.com';

# Change following if required
$surgemailpath = "/usr/local/surgemail";
$passwdfile = "passwd";
$shadowfile = "shadow";

# Should not have to change the following

$param = $ARGV[0];
if ($param eq "-apply"){
	printf "Adding users in $passwdfile to $surgemailpath/nwauth.add \n";
	$outputfile = ">>nwauth.add";
} else {
	printf "Running in verify mode (changes written to nwauth.tmp), use \" -apply \" to apply to nwauth.add\n";
	$outputfile = ">nwauth.tmp";
}


open(IPFILE, "<$passwdfile") or die "Cannot open $passwdfile file";
open(OPFILE, "$outputfile") or die "Cannot open $outputfile file";

foreach $line ( <IPFILE> )
{
	$line =~ m/^(.*?):(.*?):(.*?):(.*?):(.*?):/;
	$user = $1;
	$password = $2;
	$fullname = $5;
	
	if ($password eq "x" ) {
		open(IPFILE2, "<$shadowfile") or die "Cannot open $shadowfile file";

		foreach $line2 ( <IPFILE2> ){
			if ($line2 =~ m/^$user:(.*?):.*/) {
				$password = $1 ;
			}
		}
		close IPFILE2;
	}	
	if (($password ne "*") && ($password ne "||") && ($password ne "!!")) {
		printf "Found user:$user password(crypt):$password fullname:\"$fullname\"\n";
		printf OPFILE "${user}${domain}:${password}: full_name=\"${fullname}\" \n"; 
	}
}

close IPFILE;
close OPFILE;

