#!/usr/bin/perl -w
use strict;

my $aclfile = '/etc/usermin/webmin.acl';

my ($op, $modname) = @ARGV;

#
#  Do help operation
#
if (defined($op) && $op eq 'help')
{
  system('/usr/bin/perldoc', $0) or die "help: $!\n";
  exit;
}

#
# If add or remove must also have a modulename
#
unless (defined($modname))
{
  die "Not enough operands\n";
}

if ($op eq 'add')
{
  my @acl;
  # Update ACL for the root user, or the first user in the ACL file
  open(ACL, $aclfile);
  chop(@acl = <ACL>);
  close(ACL);
  my $found = 0;
  foreach my $a (@acl)
  {
    if ($a =~ /^(\S+):/ && ($1 eq 'root' || $1 eq 'admin'))
    {
      $a .= " $modname";
      $found = 1;
      last;
    }
  }
  if (!$found)
  {
    $acl[0] .= " $modname";
  }
  open(ACL, "> $aclfile");
  foreach my $a (@acl)
  {
    print ACL "$a\n";
  }
  close(ACL);
}
elsif ($op eq 'remove')
{
  my @acl;
  open(ACL, $aclfile);
  chop(@acl = <ACL>);
  close(ACL);
  foreach my $a (@acl)
  {
    $a =~ s/ ?$modname//g;
  }
  open(ACL, "> $aclfile");
  foreach my $a (@acl)
  {
    print ACL "$a\n";
  }
  close(ACL);
}
else
{
  die "Invalid operation\n";
}


__END__


=pod

=head1 NAME

update-usermin -- Adds or removes a module from the usermin ACL

=head1 SYNOPSIS

B<update-usermin> add modulename

B<update-usermin> remove modulename

B<update-usermin> help

=head1 DESCRIPTION

This script should be run by usermin modules to add or remove themselves from
the access control list.  It behaves differently based on the operation
requested.

=head2 add

The module is added to the ACLs' of any user called root or admin.  If neither
of these are found, it is added to the ACL of the first user in the ACL file.
A module should call this in its' postinst script.

=head2 remove

The module is removed from all users ACLs.  A module should call this in its'
prerm script.

=head2 help

Displays this manual page.

=head1 AUTHOR

Jaldhar H. Vyas E<lt>jaldhar@@debian.orgE<gt>

=head1 LICENSE

This code is free software under the Crowley Public License ("Do what
thou wilt shall be the whole of the license")

=head1 VERSION

1.0 -- Mar 19, 2002

=cut

