#!/usr/bin/perl -w
# chkvpopmail
# -----------------------------------------------------------------------------------------------------------------
# authenticate vpopmail users using the vchkpw binary from the distribution
#
# do whatever you want with it, made by fcu@no-way.org
#

use strict;

use FileHandle;
use IPC::Open2;

# &vpopmailAuth ($username, $password)
#   authenticates a user
#   returns empty string on login failure an the homedir on success
sub vpopmailAuth {
	my ($username, $passwd)=@_;
	my ($pid, $path);
	$pid = open2(*Reader, *Writer, "/usr/local/qmail/vpopmail/bin/vchkpw pwd 3<&0" );
	Writer->autoflush(); # default here, actually
	print Writer sprintf("%s\0%s\0\0",$username, $passwd);
	$path="";
	while (<Reader>){
		$path.=$_;
	}
	unless (defined $path && $path ne ""){
		$path="";
	}
	return $path;
}

# main demo
if (scalar(@ARGV)<1){
	die "Usage: chkvpopmail [user] [passwd]\n";
}

my $userdir=&vpopmailAuth($ARGV[0],$ARGV[1]);
unless ($userdir ne ""){
	print "login failed\n";
	exit 1;
}

print $userdir;
