package openwebmail::auth_home;
use strict;

use Home;

#
# auth_home.pl - authenticate user with libhome from pll project
# (http://pll.sourceforge.net/
#
# 2003/07/20 lfrigault@agneau.org for GalacSYS http://www.galacsys.net/
#

# Specific for galacsys hashing of directory
# $INTERMUID owner uid of internediate directory use by hashing
# $INTERMGID owner gid of internediate directory use by hashing
# $INTERMMODE mode  of internediate directory use by hashing
#
my $INTERMUID = 110;
my $INTERMGID = 110;
my $INTERMMODE = oct(750);
sub createhomedir($$$$);

################### No configuration required from here ###################

#  0 : ok
# -2 : parameter format error
# -3 : authentication system/internal error
# -4 : user doesn't exist
sub get_userinfo {
   my ($r_config, $user)=@_;
   my ($uid, $gid, $realname, $homedir);

   return(-2, 'User is null') if (!$user);
   ($uid, $gid, $realname, $homedir)= (&Home::getpwnam($user))[2,3,6,7];

   return(-4, "User $user doesn't exist") if ($uid eq "");

   # use first field only
   $realname=(split(/,/, $realname))[0];

   # Specific for galacsys
   &createhomedir($r_config,$homedir,$uid,$gid);

   return(0, "", $realname, $uid, $gid, $homedir);
}


#  0 : ok
# -1 : function not supported
# -3 : authentication system/internal error
sub get_userlist {	# only used by openwebmail-tool.pl -a
   my $r_config=$_[0];

   my @userlist=();
   return(0, "", @userlist);
}


#  0 : ok
# -2 : parameter format error
# -3 : authentication system/internal error
# -4 : password incorrect
sub check_userpassword {
   my ($r_config, $user, $password)=@_;
   return (-2, "User or password is null") if (!$user||!$password);

   my ($u, $p) = (&Home::getpwnam($user))[0,1];
   return (0, "") if ($u eq $user && $password eq $p);
   return(-4,"login/pass incorrect");

}


#  0 : ok
# -1 : function not supported
# -2 : parameter format error
# -3 : authentication system/internal error
# -4 : password incorrect
sub change_userpassword {
   my ($r_config, $user, $oldpassword, $newpassword)=@_;
   my ($u, $p, $misc, $encrypted);
   my ($content, $line);
   return (-2, "User or password is null") if (!$user||!$oldpassword||!$newpassword);
   return (-2, "Password too short") if (length($newpassword)<${$r_config}{'passwd_minlen'});

   return (-1, "function not supported");
}

## CREATE HOME DIR 
# Specific for galacsys which use a particular hashing directory structure
sub createhomedir($$$$)
{
	my ($r_config,$homedir,$uid,$gid) = @_;

	# create the user's home directory if necessary.
	# this must be done before changing to the user's uid.
	if (${$r_config}{'create_syshomedir'} && !-d $homedir )
	{
		my $dir='';
		foreach my $d (split (/\//,$homedir))
		{
			if ($dir eq '/')
			{
				$dir = "/$d";
			}
			else
			{
				$dir .= "/$d";
			};
			if (!-d $dir)
			{
				if ($dir eq $homedir)
				{
					mkdir ($dir,oct(700));
					chown($uid, $gid, $dir);
				}
				else
				{
					# Intermediate directory
					mkdir ($dir,$INTERMMODE);
					chown($INTERMUID, $INTERMGID, $dir);
				};
			};
		};
	}
};

1;
