# HG changeset patch # User Matthias Förste # Date 1378468617 -7200 # Node ID 2b8cf6a91d0b41d6c21212ba7a054f0889a46b05 # Parent eea8bbbf3818fde4942111f29b18bc88b9c032dc [savepoint] diff -r eea8bbbf3818 -r 2b8cf6a91d0b bin/ius-dav-htuseradd --- a/bin/ius-dav-htuseradd Fri Jul 22 09:38:29 2011 +0200 +++ b/bin/ius-dav-htuseradd Fri Sep 06 13:56:57 2013 +0200 @@ -20,7 +20,7 @@ use strict; use warnings; -use Ius::Dav::Htpasswd qw(mkpasswd readconfig useradd usage); +use Ius::Dav::Htpasswd; use Getopt::Long; use Pod::Usage; @@ -30,9 +30,9 @@ GetOptions( 'u|user=s' => \$user, 'e|expiry=i' => \$expiry, - 'h|help' => sub { usage( -exit => 0, -verbose => 1 ) }, + 'h|help' => sub { Ius::Dav::Htpasswd::usage( -exit => 0, -verbose => 1 ) }, 'm|man' => sub { - usage( + Ius::Dav::Htpasswd::usage( -exit => 0, # "system('perldoc -V &>/dev/null')" appears shorter, but may not @@ -44,7 +44,9 @@ }, ) and defined $user - or usage; + or Ius::Dav::Htpasswd::usage; -$pass = useradd readconfig, $user, mkpasswd, $expiry; +my $h = Ius::Dav::Htpasswd->new; +$h->readconfig or die "Can't readconfig"; +$pass = $h->useradd($user, Ius::Dav::Htpasswd::mkpasswd, $expiry); print "$pass\n"; diff -r eea8bbbf3818 -r 2b8cf6a91d0b bin/ius-dav-htuserdel --- a/bin/ius-dav-htuserdel Fri Jul 22 09:38:29 2011 +0200 +++ b/bin/ius-dav-htuserdel Fri Sep 06 13:56:57 2013 +0200 @@ -21,15 +21,15 @@ use warnings; use Getopt::Long; -use Ius::Dav::Htpasswd qw(readconfig userdel usage); +use Ius::Dav::Htpasswd; my $user; GetOptions( 'u|user=s' => \$user, - "h|help" => sub { usage( -verbose => 0, -exitval => 0 ) }, + "h|help" => sub { Ius::Dav::Htpasswd::usage( -verbose => 0, -exitval => 0 ) }, "m|man" => sub { - usage( + Ius::Dav::Htpasswd::usage( -verbose => 2, -exitval => 0, -noperldoc => ( `perldoc -V 2>/dev/null`, $? != 0 )[-1] @@ -37,6 +37,8 @@ }, ) and defined $user - or usage(); + or Ius::Dav::Htpasswd::usage(); -exit userdel readconfig, $user; +my $h = Ius::Dav::Htpasswd->new; +$h->readconfig or die "Can't readconfig"; +exit $h->userdel($user); diff -r eea8bbbf3818 -r 2b8cf6a91d0b bin/ius-dav-htuserexpiry --- a/bin/ius-dav-htuserexpiry Fri Jul 22 09:38:29 2011 +0200 +++ b/bin/ius-dav-htuserexpiry Fri Sep 06 13:56:57 2013 +0200 @@ -20,15 +20,15 @@ use strict; use warnings; -use Ius::Dav::Htpasswd qw(readconfig userexpiry usage); +use Ius::Dav::Htpasswd; use Getopt::Long; use Pod::Usage; GetOptions( - 'h|help' => sub { usage( -exit => 0, -verbose => 1 ) }, + 'h|help' => sub { Ius::Dav::Htpasswd::usage( -exit => 0, -verbose => 1 ) }, 'm|man' => sub { - usage( + Ius::Dav::Htpasswd::usage( -exit => 0, # "system('perldoc -V &>/dev/null')" appears shorter, but may not @@ -38,6 +38,8 @@ -verbose => 2 ); }, -) or usage; +) or Ius::Dav::Htpasswd::usage; -exit userexpiry readconfig; +my $h = Ius::Dav::Htpasswd->new; +$h->readconfig or die "Can't readconfig"; +exit $h->userexpiry; diff -r eea8bbbf3818 -r 2b8cf6a91d0b cgi-bin/ius-dav-htuseradd.cgi --- a/cgi-bin/ius-dav-htuseradd.cgi Fri Jul 22 09:38:29 2011 +0200 +++ b/cgi-bin/ius-dav-htuseradd.cgi Fri Sep 06 13:56:57 2013 +0200 @@ -23,7 +23,7 @@ # restarting apache when everything else works # use CGI::Fast; use CGI; -use Ius::Dav::Htpasswd qw(mkpasswd readconfig useradd); +use Ius::Dav::Htpasswd; my $css = <new; +$h->readconfig or die "Can't readconfig"; if ( defined $p->{add} and $p->{add} ne '' ) { @@ -79,7 +80,7 @@ if ( my $pass = qx(@cmd) ) { - my $url = "$conf->{dav_base_remote}/$p->{user}"; + my $url = $h->conf->{dav_base_remote} . "/$p->{user}"; chomp $pass; diff -r eea8bbbf3818 -r 2b8cf6a91d0b lib/Ius/Dav/Htpasswd.pm --- a/lib/Ius/Dav/Htpasswd.pm Fri Jul 22 09:38:29 2011 +0200 +++ b/lib/Ius/Dav/Htpasswd.pm Fri Sep 06 13:56:57 2013 +0200 @@ -42,6 +42,29 @@ @EXPORT_OK = qw(readconfig mkpasswd useradd userdel userexpiry usage); } +use constant { + + E_USER_NONALNUM => 1, + E_EXPIRY_NONNUM => 2, + E_EXPIRY_TOOSHORT => 3, + E_EXPIRY_TOOLONG => 4, + +}; + +# can't use constants in the declaration in which they are defined +use constant { + ERRSTR => { + + E_USER_NONALNUM, 'Invalid character in username.', + E_EXPIRY_NONNUM, 'Expiry does not look like a positive integer.', + E_EXPIRY_TOOSHORT, 'Expiry too short.', + E_EXPIRY_TOOLONG, 'Expiry too long.' + + } +}; + + + sub usage { use Pod::Usage; @@ -51,9 +74,26 @@ } +sub new { + + my ($class, $self) = (shift, {}); + $self->{ERRNO} = undef; + bless $self, $class; + return $self; +} + +sub conf { + + my $self = shift; + return { $self->{CONF}->varlist('.') }; + +} + sub readconfig { - my $conf = new AppConfig( + my $self = shift; + + $self->{CONF} = new AppConfig( qw( expiry=i expiry_min=i @@ -66,24 +106,42 @@ www_group=s master_user=s) ) or die 'Failed to read config!'; - $conf->file($_) + $self->{CONF}->file($_) for grep -e, map "$_/ius-dav-htpasswd.conf", qw(/etc/ius-dav-htpasswd /usr/local/etc/ius-dav-htpasswd ~/.ius-dav-htpasswd .); - return { $conf->varlist('.') }; + + return 1; + +} + +sub seterrno { + + my ($self, $e) = @_; + + $self->{ERRNO} = $e; + return; + +} + +sub errstr { + + my $self = shift; + return ERRSTR->{$self->{ERRNO}}; } sub validate { - my ( $conf, $user, $expiry ) = @_; + my ( $self, $user, $expiry ) = @_; + my $conf = $self->conf; - return unless $user =~ /^[[:alnum:]_]+$/; + return $self->seterrno(E_USER_NONALNUM) unless $user =~ /^[[:alnum:]_]+$/; if ( defined $expiry ) { - return unless $expiry =~ /^[0-9]+$/; - return - unless $expiry >= $conf->{expiry_min} - and $expiry <= $conf->{expiry_max}; + + return $self->seterrno(E_EXPIRY_NONNUM) unless $expiry =~ /^[0-9]+$/; + return $self->seterrno(E_EXPIRY_TOOSHORT) unless $expiry >= $conf->{expiry_min}; + return $self->seterrno(E_EXPIRY_TOOLONG) unless $expiry <= $conf->{expiry_max}; } return 1; @@ -92,7 +150,8 @@ sub useradd { - my ( $conf, $user, $pass, $expiry ) = @_; + my ( $self, $user, $pass, $expiry ) = @_; + my $conf = $self->conf; for ( qw(expiry expiry_min expiry_max dav_base_local htpasswd conf_d www_user www_group) @@ -103,7 +162,7 @@ } $expiry = $conf->{expiry} unless defined $expiry and $expiry ne ''; - die 'Invalid input' unless validate $conf, $user, $expiry; + die $self->errstr unless $self->validate($user, $expiry); my $user_dir = "$conf->{dav_base_local}/$user"; mkdir "$user_dir" or die "Can't mkdir '$user_dir': $!"; @@ -165,7 +224,8 @@ sub userdel { - my ( $conf, $user ) = @_; + my ( $self, $user ) = @_; + my $conf = $self->conf; my $rc = 0; @@ -201,7 +261,8 @@ sub userexpiry { - my ($conf) = @_; + my $self = shift; + my $conf = $self->conf; my $rc = 0; @@ -218,7 +279,7 @@ for my $u (@users) { if ( my $e = $htpasswd->fetchInfo($u) ) { - userdel( $conf, $u ) + $self->userdel( $u ) and warn "Error(s) occured during 'userdel $conf, $u'\n" if $now >= $e; }