#!/usr/bin/perl -w

# $Id: smbldap-qmail-userdel,v 0.3-2 2004/09/16 01:15:06 Jun Futagawa Exp $
#
#  This code was developped by Jun Futagawa, and based on smblda-tools
#  developped by IDEALX (http://IDEALX.org/) and contributors
#  (their names can be found in that CONTRIBUTORS file).
#
#               Copyright (C) 2004 Jun Futagawa
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.

# Purpose of smbldap-qmail-userdel : delete an attribute for qmail-ldap entry

use vars qw(@ARGV);

use strict;
use FindBin;
use FindBin qw($RealBin);
use Getopt::Std;
use IO::Handle;
use IO::File;
use lib "$RealBin/";

use smbldap_tools;
use smbldap_qmail_tools;

sub deleteOptionValue($$$);

#####################

my $fout = new IO::Handle();
$fout->fdopen(fileno(STDOUT), "w") or die "cannot open STDOUT";

my %Options;
my $ok = getopts('dugmqcsHSRDCA:a:f:M:P:h?', \%Options);
if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
	$fout->printf('%s',
		"Version 0.3-2\n".
		"Usage: $0 [-dugmafqcsHSRMPDCAh?] username\n".
		"  -d	homeDirectory\n".
		"  -u	qmailUID\n".
		"  -g	qmailGID\n".
		"  -m	mail\n".
		"  -a	mailAlternateAddress (multiple attribute)\n".
		"  -f	mailForwardingAddress (multiple attribute)\n".
		"  -q	mailQuotaSize\n".
		"  -c	mailQuotaCount\n".
		"  -s	mailSizeMax\n".
		"  -H	mailHost\n".
		"  -S	mailMessageStore\n".
		"  -R	mailReplyText\n".
		"  -M	deliveryMode: (multiple attribute)\n".
		"  -P	deliveryProgramPath (multiple attribute)\n".
		"  -D	qmailDotMode\n".
		"  -C	qmailAccountPurge\n".
		"  -A	accountStatus\n".
		"  -h,?	show this help message\n"
	);
	exit(1);
}

# check user
if ($< != 0) {
	$fout->printf('%s', "You must be root to modify an user\n");
	exit(1);
}

# read only first @ARGV
my $userName = $ARGV[0];

# untaint $userName (can finish with one or two $)
if ($userName =~ /^([\w -.]+\$?)$/) {
	$userName = $1;
} else {
	$fout->printf('%s', "$0: illegal username\n");
	exit (1);
}

# check and add objectClass qmailUser on user entry
if (!isQmailUser($userName)) {
	$fout->printf('%s', "$0: user $userName doesn't have objectClass qmailUser\n");
	exit(1);
}

# read user data
my $userEntry = readUserEntry($userName);
if (!defined($userEntry)) {
	$fout->printf('%s', "$0: user $userName doesn't exist\n");
	exit(1);
}

# get the dn of the user
my $dn = $userEntry->dn();
my @dels;

# all delete
if (keys(%Options) == 0) {
	my $ldap_slave=connect_ldap_slave();
	if (is_unix_user($userName)) {
		# delete attributes for qmailUser
		deleteQmailUserAttribute($userName);
	} else {
		# delete virtual user entry
		deleteUserEntry($dn);
	}
	$ldap_slave->unbind;
	exit(1);
}

# delete option value
deleteOptionValue(\@dels, 'd', 'homeDirectory');
deleteOptionValue(\@dels, 'u', 'qmailUID');
deleteOptionValue(\@dels, 'g', 'qmailGID');
deleteOptionValue(\@dels, 'm', 'mail');
deleteOptionValue(\@dels, 'a', 'mailAlternateAddress');
deleteOptionValue(\@dels, 'f', 'mailForwardingAddress');
deleteOptionValue(\@dels, 'q', 'mailQuotaSize');
deleteOptionValue(\@dels, 'c', 'mailQuotaCount');
deleteOptionValue(\@dels, 's', 'mailSizeMax');
deleteOptionValue(\@dels, 'H', 'mailHost');
deleteOptionValue(\@dels, 'S', 'mailMessageStore');
deleteOptionValue(\@dels, 'R', 'mailReplyText');
deleteOptionValue(\@dels, 'M', 'deliveryMode');
deleteOptionValue(\@dels, 'P', 'deliveryProgramPath');
deleteOptionValue(\@dels, 'D', 'qmailDotMode');
deleteOptionValue(\@dels, 'C', 'qmailAccountPurge');
deleteOptionValue(\@dels, 'A', 'accountStatus');

# connect to the directory
my $ldap_master=connect_ldap_master();
my $result;

# delete attributes
if (@dels > 0) {
	$result = $ldap_master->modify ("$dn", 'delete' => { @dels });
	$result->code && warn "failed to modify entry: ", $result->error;
}

# close session
$ldap_master->unbind;

# delete option value
sub deleteOptionValue($$$) {
	my ($dels, $optionName, $attributeName) = @_;
	my $tmp;
	if (defined($tmp = $Options{$optionName})) {
		if ($tmp eq '1' || $tmp eq "") {
			push(@$dels, $attributeName => []);
		} else {
			push(@$dels, $attributeName => $tmp);
		}
	}
}

########################################

=head1 NAME

    smbldap-qmail-userdel - delete attribute for qmail-ldap entry

=head1 SYNOPSIS

    smbldap-qmailmod
        [-v]
        [-d homeDirectory]
        [-u qmailUID]
        [-g qmailGID]
        [-m mail]
        [-a mailAlternateAddress]
        [-f mailForwardingAddress]
        [-q mailQuotaSize]
        [-c mailQuotaCount]
        [-s mailSizeMax]
        [-H mailHost]
        [-S mailMessageStore]
        [-R mailReplyText]
        [-M deliveryMode]
        [-P deliveryProgramPath]
        [-D qmailDotMode]
        [-C qmailAccountPurge]
        [-A accountStatus]
        username

=head1 DESCRIPTION

    The smbldap-qmail-userdel command deletes an attribute for qmail-ldap entry using
    the values specified on the command line and the default values
    from the system. The options which apply to the smbldap-qmailmod command are

    -u qmailUID The value of system uid for qmail-ldap.

    -g qmailGID The value of system gid for qmail-ldap.

    -m mail The value of the mail address.

    -a mailAlternateAddress The value of the alternate mail address.

    -d homeDirectory The value of the home directory.

    -f mailForwardingAddress The value of the forwarding mail address.

    -q mailQuotaSize The value of the max mailbox size.

    -c mailQuotaCount The value of the max amount of mail.

    -s mailSizeMax The value of the max one mail size.

    -H mailHost The value of the mail host address.

    -S mailMessageStore The value of the mail store directory.

    -R mailReplyText The value of the reply message.

    -M deliveryMode The value of the mail delivery mode. [(none) noforward nolocal noprogram reply]

    -P deliveryProgramPath The value of the program path.

    -D qmailDotMode The value of the .qmail mode. [both dotonly ldaponly ldapwithprog none].

    -C qmailAccountPurge The value of the epoch time.

    -A accountStatus The value of the forwarding mail address. [active noaccess disabled deleted].

=cut

#'

