lib/Ius/Dav/Htpasswd.pm
branchfoerste
changeset 22 f664783b4160
parent 16 44cdd9d6fd8c
child 23 18aaf612a61e
equal deleted inserted replaced
21:c9dcdb710c52 22:f664783b4160
    37 
    37 
    38     # set the version for version checking
    38     # set the version for version checking
    39     $VERSION = 0.1;
    39     $VERSION = 0.1;
    40 
    40 
    41     @ISA    = qw(Exporter);
    41     @ISA    = qw(Exporter);
    42     @EXPORT_OK = qw(readconfig mkpasswd useradd userdel usage);
    42     @EXPORT_OK = qw(readconfig mkpasswd useradd userdel userexpiry usage);
    43 }
    43 }
    44 
    44 
    45 sub usage {
    45 sub usage {
    46 
    46 
    47     use Pod::Usage;
    47     use Pod::Usage;
    94     }
    94     }
    95 
    95 
    96     $expiry = $conf->{expiry} unless defined $expiry and $expiry ne '';
    96     $expiry = $conf->{expiry} unless defined $expiry and $expiry ne '';
    97     die 'Invalid input' unless validate $conf, $user, $expiry;
    97     die 'Invalid input' unless validate $conf, $user, $expiry;
    98 
    98 
    99     my $at_cmd = "at now + " . 24 * 60 * $expiry . " minutes";
       
   100     open AT, "|$at_cmd"
       
   101         or die "Can't open AT, '|$at_cmd': $!";
       
   102     print AT "ius-dav-htuserdel";
       
   103     close AT;
       
   104 
       
   105     my $user_dir = "$conf->{dav_base}/$user";
    99     my $user_dir = "$conf->{dav_base}/$user";
   106     mkdir "$user_dir" or die "Can't mkdir '$user_dir': $!";
   100     mkdir "$user_dir" or die "Can't mkdir '$user_dir': $!";
   107 
   101 
   108     my ($www_user, $www_group) = @{$conf}{qw(www_user www_group)};
   102     my ($www_user, $www_group) = @{$conf}{qw(www_user www_group)};
   109     my $www_uid = getpwnam $www_user or die "Can't getpwnam '$www_user'";
   103     my $www_uid = getpwnam $www_user or die "Can't getpwnam '$www_user'";
   113     my $htpasswd_file = $conf->{htpasswd};
   107     my $htpasswd_file = $conf->{htpasswd};
   114     unless (-e $htpasswd_file ) {
   108     unless (-e $htpasswd_file ) {
   115         open H, '>>', $htpasswd_file or die "Can't create '$htpasswd_file': $!";
   109         open H, '>>', $htpasswd_file or die "Can't create '$htpasswd_file': $!";
   116         close H;
   110         close H;
   117     }
   111     }
       
   112 
   118     my $htpasswd = new Apache::Htpasswd $htpasswd_file;
   113     my $htpasswd = new Apache::Htpasswd $htpasswd_file;
   119     $htpasswd->htpasswd($user, $pass)
   114     $htpasswd->htpasswd($user, $pass)
       
   115         or die $htpasswd->error;
       
   116     $htpasswd->writeInfo($user, time + 24 * 60 * 60 * $expiry)
   120         or die $htpasswd->error;
   117         or die $htpasswd->error;
   121 
   118 
   122     my $master_user = $conf->{master_user};
   119     my $master_user = $conf->{master_user};
   123     my $conf_file = "$conf->{conf_d}/$user.conf";
   120     my $conf_file = "$conf->{conf_d}/$user.conf";
   124     open C, '>', $conf_file or die "Can't open '$conf_file': $!";
   121     open C, '>', $conf_file or die "Can't open '$conf_file': $!";
   170 
   167 
   171     my $conf_file = "$conf->{conf_d}/$user.conf";
   168     my $conf_file = "$conf->{conf_d}/$user.conf";
   172     unlink $conf_file
   169     unlink $conf_file
   173         or $rc = -1 and warn "Can't unlink '$conf_file': $!";
   170         or $rc = -1 and warn "Can't unlink '$conf_file': $!";
   174 
   171 
   175     # maybe TODO: remove at job if it still exists (record job# during #
       
   176     # 'useradd'?)
       
   177 
       
   178     0 == system qw(apache2ctl graceful)
   172     0 == system qw(apache2ctl graceful)
   179         or $rc =-1 and warn "Can't 'apache2ctl graceful'!";
   173         or $rc =-1 and warn "Can't 'apache2ctl graceful'!";
   180 
   174 
   181 }
   175 }
   182 
   176 
       
   177 sub userexpiry {
       
   178 
       
   179     my ($conf) = @_;
       
   180 
       
   181     for (qw(htpasswd)) {
       
   182         die "Can't determine '$_' - please check configuration"
       
   183             unless defined $conf->{$_};
       
   184     }
       
   185 
       
   186     my $htpasswd_file = $conf->{htpasswd};
       
   187     my $htpasswd = new Apache::Htpasswd $htpasswd_file;
       
   188     my @users = $htpasswd->fetchUsers
       
   189         or die "Can't fetch htuser list: ", $htpasswd->error;
       
   190     my $now = time;
       
   191 
       
   192     for my $u (@users) {
       
   193         if (my $e = $htpasswd->fetchInfo($u)) {
       
   194             userdel($conf, $u) or warn "Can't 'userdel $conf, $u'\n" if $now >= $e;
       
   195         } else {
       
   196             warn "Can't get expiry for '$u': ", $htpasswd->error, "\n";
       
   197         }
       
   198     }
       
   199 
       
   200 }
       
   201 
   183 1;
   202 1;
   184 
   203 
   185 __END__
   204 __END__
   186 
   205 
   187 =pod
   206 =pod
   192 
   211 
   193 dav-useradd.cgi
   212 dav-useradd.cgi
   194 
   213 
   195 dav-userdel
   214 dav-userdel
   196 
   215 
   197 Ius::Dav::Htpasswd - Add dav users to htpasswd and remove them automatically
   216 dav-userexpiry
   198 after expiration or manually.
   217 
       
   218 Ius::Dav::Htpasswd - Add dav users to htpasswd and remove them after
       
   219 expiration.
   199 
   220 
   200 =head1 SYNOPSIS
   221 =head1 SYNOPSIS
   201 
   222 
   202 dav-useradd  -u|--user user
   223 dav-useradd  -u|--user user
   203             [-e|--expiry expiry]
   224             [-e|--expiry expiry]
   204 
   225 
   205 dav-userdel -u|--user user
   226 dav-userdel -u|--user user
   206 
   227 
       
   228 dav-userexpiry
       
   229 
   207 common options
   230 common options
   208 
   231 
   209             -m|--man
   232             -m|--man
   210             -h|--help
   233             -h|--help
   211 
   234 
   212 =head1 DESCRIPTION
   235 =head1 DESCRIPTION
   213 
   236 
   214 =head2 dav-useradd
   237 =head2 dav-useradd
   215 
   238 
   216 Add an at job to remove the user later. Make a directory for the user. Chown
   239 Make a directory for the user. Chown that directory to the webserver user and
   217 that directory to the webserver user and group. Add the user to an htpasswd
   240 group. Add the user to an htpasswd file. Add expiry information to that
   218 file. Place a config snippet for the users directory inside a directory (which
   241 htpasswd file. Place a config snippet for the users directory inside a
   219 is included from the apache config). Reload apache (or maybe restart is
   242 directory (which is included from the apache config). Reload apache (or maybe
   220 required).
   243 restart is required).
   221 
   244 
   222 =head2 dav-useradd.cgi
   245 =head2 dav-useradd.cgi
   223 
   246 
   224 Is supposed to do the same as dav-useradd.
   247 Is supposed to do the same as dav-useradd.
   225 
   248 
   226 =head2 dav-userdel
   249 =head2 dav-userdel
   227 
   250 
   228 Removes the directory of the user. Removes the user from the htpasswd file.
   251 Removes the directory of the user. Removes the user from the htpasswd file.
   229 Removes the config snippet for the users directory. Removes the at job that is
   252 Removes the config snippet for the users directory. Reload apache (or maybe
   230 supposed to remove the user if it still exists. Reload apache (or maybe restart
   253 restart is required).
   231 is required).
   254 
       
   255 =head2 dav-userexpiry
       
   256 
       
   257 Check the htpasswd file and run deletion for any expired users found.
   232 
   258 
   233 =head1 OPTIONS
   259 =head1 OPTIONS
   234 
   260 
   235 =over
   261 =over
   236 
   262 
   260 
   286 
   261 F</etc/apache2/dav.d>
   287 F</etc/apache2/dav.d>
   262 
   288 
   263 =head1 REQUIRES
   289 =head1 REQUIRES
   264 
   290 
   265 at from the 'at' job scheduler package. Several perl modules (should be installed automatically).
   291 Several perl modules (should be installed automatically). Some kind of cron
       
   292 daemon to run the user expiry is recommended.
   266 
   293 
   267 =head1 AUTHOR
   294 =head1 AUTHOR
   268 
   295 
   269 Matthias Förste <foerste@schlittermann.de>
   296 Matthias Förste <foerste@schlittermann.de>
   270 
   297