# useradmin_update.pl - script for synchronizing unix accounts with pptp-server accounts
# more details at: http://patchlog.com/webmin/webmin-pptp-user-synchronization/
#
# Copyright 2007-2008 Mihai Secasiu  - http://patchlog.com 
# This script is free for use,modification and redistribution
#  under the terms of GPLv2 or later at your choice
#
do 'pptp-server-lib.pl';

# useradmin_create_user(&details)
sub useradmin_create_user
{
	local %sec;
	if($config{'sync_add'}){
		&lock_file($config{'pap_file'});
		$sec{'server'} = &get_ppp_hostname();
		$sec{'client'} = $_[0]->{'user'};
		$sec{'secret'}=&opt_crypt($_[0]->{'plainpass'});
		$sec{'ips'} = [ "*" ];
		&create_secret(\%sec);
		&unlock_file($config{'pap_file'});
	}
}

# useradmin_delete_user(&details)
sub useradmin_delete_user
{
	if($config{'sync_delete'}){
		&lock_file($config{'pap_file'});
		local %sec = &get_user($_[0]->{'user'});
	        &delete_secret(\%sec);
        	&unlock_file($config{'pap_file'});
	}
}

# useradmin_modify_user(&details)
# Update a samba user
sub useradmin_modify_user
{

	if($config{'sync_change'}){
		&lock_file($config{'pap_file'});
        	local %sec = &get_user($_[0]->{'olduser'});
		#local %sec=%{$s};
        	if ($_[0]->{'passmode'} == 4) {
	                # Not changing password
        	}
	        elsif ($_[0]->{'passmode'} == 3) {
        	        # Setting new password
			$sec{'secret'}=&opt_crypt($_[0]->{'plainpass'});
	        }
        	elsif ($_[0]->{'passmode'} == 0) {
                	# No password
			$sec{'secret'}='';
        	}
	        else {
        	        # Assume locked
			$sec{'secret'}='*';
        	}	
	        if ($_[0]->{'user'} ne $_[0]->{'olduser'}) {
			$sec{'client'}=$_[0]->{'user'};	
        	}
		&change_secret(\%sec);
		&unlock_file($config{'pap_file'});
	}
}

sub get_user()
{
        local @ulist = &list_secrets();
        local $u;
        foreach $u (@ulist) {
                local %t=%{$u};
                return %t if ($t{'client'} eq $_[0]);
        }
        return undef;
}

1
