lib/Ius/Dav/Htpasswd.pm
branchfoerste
changeset 26 9edb61423498
parent 24 6426cf731f25
child 31 df24e0f202f1
--- a/lib/Ius/Dav/Htpasswd.pm	Fri Jul 15 14:10:40 2011 +0200
+++ b/lib/Ius/Dav/Htpasswd.pm	Fri Jul 15 14:22:21 2011 +0200
@@ -32,13 +32,13 @@
 
 BEGIN {
 
-    our ($VERSION, @ISA, @EXPORT_OK);
+    our ( $VERSION, @ISA, @EXPORT_OK );
     use Exporter;
 
     # set the version for version checking
     $VERSION = 0.1;
 
-    @ISA    = qw(Exporter);
+    @ISA       = qw(Exporter);
     @EXPORT_OK = qw(readconfig mkpasswd useradd userdel userexpiry usage);
 }
 
@@ -47,37 +47,42 @@
     use Pod::Usage;
     use Pod::Find qw(pod_where);
 
-    pod2usage( -input => pod_where({-inc => 1}, __PACKAGE__), @_ );
+    pod2usage( -input => pod_where( { -inc => 1 }, __PACKAGE__ ), @_ );
 
 }
 
 sub readconfig {
 
-    my $conf = new AppConfig(qw(
-        expiry=i
-        expiry_min=i
-        expiry_max=i
-        dav_base=s
-        htpasswd=s
-        conf_d=s
-        www_user=s
-        www_group=s
-        master_user=s)
+    my $conf = new AppConfig(
+        qw(
+          expiry=i
+          expiry_min=i
+          expiry_max=i
+          dav_base=s
+          htpasswd=s
+          conf_d=s
+          www_user=s
+          www_group=s
+          master_user=s)
     ) or die 'Failed to read config!';
-    $conf->file($_) for grep -e, map "$_/ius-dav-htpasswd.conf", qw(/etc/ius-dav-htpasswd /usr/local/etc/ius-dav-htpasswd ~/.ius-dav-htpasswd ./ius-dav-htpasswd);
+    $conf->file($_)
+      for grep -e, map "$_/ius-dav-htpasswd.conf",
+      qw(/etc/ius-dav-htpasswd /usr/local/etc/ius-dav-htpasswd ~/.ius-dav-htpasswd ./ius-dav-htpasswd);
     return { $conf->varlist('.') };
 
 }
 
 sub validate {
 
-    my ($conf, $user, $expiry) = @_;
+    my ( $conf, $user, $expiry ) = @_;
 
-    return unless $user =~ /^[[:alpha:]_]+$/; 
+    return unless $user =~ /^[[:alpha:]_]+$/;
 
-    if (defined $expiry) {
+    if ( defined $expiry ) {
         return unless $expiry =~ /^[0-9]+$/;
-        return unless $expiry >= $conf->{expiry_min} and $expiry <= $conf->{expiry_max};
+        return
+          unless $expiry >= $conf->{expiry_min}
+              and $expiry <= $conf->{expiry_max};
     }
 
     return 1;
@@ -86,11 +91,14 @@
 
 sub useradd {
 
-    my ($conf, $user, $pass, $expiry) = @_;
+    my ( $conf, $user, $pass, $expiry ) = @_;
 
-    for (qw(expiry expiry_min expiry_max dav_base htpasswd conf_d www_user www_group)) {
+    for (
+        qw(expiry expiry_min expiry_max dav_base htpasswd conf_d www_user www_group)
+      )
+    {
         die "Can't determine '$_' - please check configuration"
-            unless defined $conf->{$_};
+          unless defined $conf->{$_};
     }
 
     $expiry = $conf->{expiry} unless defined $expiry and $expiry ne '';
@@ -99,25 +107,26 @@
     my $user_dir = "$conf->{dav_base}/$user";
     mkdir "$user_dir" or die "Can't mkdir '$user_dir': $!";
 
-    my ($www_user, $www_group) = @{$conf}{qw(www_user www_group)};
-    my $www_uid = getpwnam $www_user or die "Can't getpwnam '$www_user'";
+    my ( $www_user, $www_group ) = @{$conf}{qw(www_user www_group)};
+    my $www_uid = getpwnam $www_user  or die "Can't getpwnam '$www_user'";
     my $www_gid = getgrnam $www_group or die "Can't getgrnam '$www_group'";
-    chown $www_uid, $www_gid, "$user_dir" or die "Can't chown, '$www_uid', '$www_gid', '$user_dir': $!";
+    chown $www_uid, $www_gid, "$user_dir"
+      or die "Can't chown, '$www_uid', '$www_gid', '$user_dir': $!";
 
     my $htpasswd_file = $conf->{htpasswd};
-    unless (-e $htpasswd_file ) {
+    unless ( -e $htpasswd_file ) {
         open H, '>>', $htpasswd_file or die "Can't create '$htpasswd_file': $!";
         close H;
     }
 
     my $htpasswd = new Apache::Htpasswd $htpasswd_file;
-    $htpasswd->htpasswd($user, $pass)
-        or die $htpasswd->error;
-    $htpasswd->writeInfo($user, time + 24 * 60 * 60 * $expiry)
-        or die $htpasswd->error;
+    $htpasswd->htpasswd( $user, $pass )
+      or die $htpasswd->error;
+    $htpasswd->writeInfo( $user, time + 24 * 60 * 60 * $expiry )
+      or die $htpasswd->error;
 
     my $master_user = $conf->{master_user};
-    my $conf_file = "$conf->{conf_d}/$user.conf";
+    my $conf_file   = "$conf->{conf_d}/$user.conf";
     open C, '>', $conf_file or die "Can't open '$conf_file': $!";
     print C <<EOC;
 <Directory "$user_dir">
@@ -134,7 +143,7 @@
     close C;
 
     0 == system qw(apache2ctl graceful)
-        or die "Can't 'apache2ctl graceful'!";
+      or die "Can't 'apache2ctl graceful'!";
 
     return $pass;
 
@@ -144,33 +153,35 @@
 
 sub userdel {
 
-    my ($conf, $user) = @_;
+    my ( $conf, $user ) = @_;
 
     my $rc;
 
     for (qw(dav_base htpasswd conf_d)) {
         die "Can't determine '$_' - please check configuration"
-            unless defined $conf->{$_};
+          unless defined $conf->{$_};
     }
 
     # avoid 'Found = in conditional, should be ==' warnings
     no warnings qw(syntax);
     my $user_dir = "$conf->{dav_base}/$user";
     my $err;
-    rmtree($user_dir, error => $err)
-        or $rc = -1 and defined $err and warn "Errors occurred during rmtree '$user_dir': ", @{$err};
+    rmtree( $user_dir, error => $err )
+      or $rc = -1
+      and defined $err
+      and warn "Errors occurred during rmtree '$user_dir': ", @{$err};
 
     my $htpasswd_file = $conf->{htpasswd};
-    my $htpasswd = new Apache::Htpasswd $htpasswd_file;
+    my $htpasswd      = new Apache::Htpasswd $htpasswd_file;
     $htpasswd->htDelete($user)
-        or $rc = -1 and warn "Can't htdelete '$user': ", $htpasswd->error;
+      or $rc = -1 and warn "Can't htdelete '$user': ", $htpasswd->error;
 
     my $conf_file = "$conf->{conf_d}/$user.conf";
     unlink $conf_file
-        or $rc = -1 and warn "Can't unlink '$conf_file': $!";
+      or $rc = -1 and warn "Can't unlink '$conf_file': $!";
 
     0 == system qw(apache2ctl graceful)
-        or $rc =-1 and warn "Can't 'apache2ctl graceful'!";
+      or $rc = -1 and warn "Can't 'apache2ctl graceful'!";
 
 }
 
@@ -180,19 +191,22 @@
 
     for (qw(htpasswd)) {
         die "Can't determine '$_' - please check configuration"
-            unless defined $conf->{$_};
+          unless defined $conf->{$_};
     }
 
     my $htpasswd_file = $conf->{htpasswd};
-    my $htpasswd = new Apache::Htpasswd $htpasswd_file;
-    my @users = $htpasswd->fetchUsers
-        or die "Can't fetch htuser list: ", $htpasswd->error;
+    my $htpasswd      = new Apache::Htpasswd $htpasswd_file;
+    my @users         = $htpasswd->fetchUsers
+      or die "Can't fetch htuser list: ", $htpasswd->error;
     my $now = time;
 
     for my $u (@users) {
-        if (my $e = $htpasswd->fetchInfo($u)) {
-            userdel($conf, $u) or warn "Can't 'userdel $conf, $u'\n" if $now >= $e;
-        } else {
+        if ( my $e = $htpasswd->fetchInfo($u) ) {
+            userdel( $conf, $u )
+              or warn "Can't 'userdel $conf, $u'\n"
+              if $now >= $e;
+        }
+        else {
             warn "Can't get expiry for '$u': ", $htpasswd->error, "\n";
         }
     }