blib/lib/Web/Passwd.pm

filter: Statementstmt 16.3% 23 / 141 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 37.5% 6 / 16 Podpod 20.0% 2 / 10 totaltotal 11.1% 31 / 279 CC 53 SCAR 48.7
CC
53
Cov
10%
load_config
342.0
display_status
132.0
user_op
72.0
CRAP 130.9
↑ Summary
1 # $Date: 2007-02-07 15:54:57 -0600 (Wed, 07 Feb 2007) $
2 # $Revision: 19 $
3
4 package Web::Passwd;
5 1 ▶use base 'CGI::Application';
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 # set up application framework, including mode parameter and dispatch table
13 0 ▶sub setup {
Subroutine setup: not called CC=1 SCAR=6.9
Pod documented
14 0 my $self = shift;
15 0 $self->start_mode('index');
16 0 $self->mode_param('mode');
17 0 $self->run_modes(
18 'index' => 'display_index',
19 'view' => 'display_htfile',
20 'adduser' => 'user_op',
21 'changepw' => 'user_op',
22 'deluser' => 'user_op',
23 );
24
25 # return just to be tidy
26 0 return;
27 }
28
29 # perform final actions after all other processing
30 0 ▶sub teardown {
Subroutine teardown: not called CC=1 SCAR=6.9
Pod documented
31 # trigger the printing of any warnings to browser as HTML comments
32 0 warningsToBrowser(1);
33
34 # return just to be tidy
35 0 return;
36 }
37
38 # display index page with no actions to be performed
39 0 ▶sub display_index {
Subroutine display_index: not called CC=4 SCAR=30.0
Pod undocumented
40 0 my $self = shift;
41
42 # load configuration as a hash ref
43 0 $self->param('act_config', load_config( $self->param('config') ) );
44
45 # load template file with HTML::Template
46 0 my $tmpl_obj;
47 0 ▶ if(-e $self->param('act_config')->{'_'}->{'tmpl_path'} . 'index.tmpl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->param('act_config')->{'_'}{'tmpl_path'} . 'index.tmpl') { } 0 0 0
48 0 $tmpl_obj = $self->load_tmpl( $self->param('act_config')->{'_'}->{'tmpl_path'} . 'index.tmpl' );
49 }
50 else {
51 0 $tmpl_obj = $self->load_tmpl( \$Web::Passwd::INDEX_TEMPLATE );
52 }
53
54 # get list of htpasswd config blocks
55 0 my @htfiles;
56 0 for my $key (keys %{$self->param('act_config')}) {
57 0 ▶ if($key ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "_") 0 0 0
58 0 push(@htfiles, {'TITLE' => $key});
59 }
60 }
61
62 # pass template parameters
63 $tmpl_obj->param(
64 'HTFILES' => \@htfiles,
65 'IS_WARNINGS' => $#CGI::Carp::WARNINGS + 1,
66 0 'FORM_METHOD' => $self->param('act_config')->{'_'}->{'form_method'},
67 );
68
69 # return template-generated output
70 0 return $tmpl_obj->output;
71 }
72
73 # display page to view/manage a specific htpasswd file
74 0 ▶sub display_htfile {
Subroutine display_htfile: not called CC=3 SCAR=24.8
Pod undocumented
75 0 my $self = shift;
76
77 # get CGI query object
78 0 my $query_obj = $self->query();
79
80 # load configuration as a hash ref
81 0 $self->param('act_config', load_config( $self->param('config') ) );
82
83 # load template file with HTML::Template
84 0 my $tmpl_obj;
85 0 ▶ if(-e $self->param('act_config')->{'_'}->{'tmpl_path'} . 'view.tmpl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->param('act_config')->{'_'}{'tmpl_path'} . 'view.tmpl') { } 0 0 0
86 0 $tmpl_obj = $self->load_tmpl( $self->param('act_config')->{'_'}->{'tmpl_path'} . 'view.tmpl' );
87 }
88 else {
89 0 $tmpl_obj = $self->load_tmpl( \$Web::Passwd::VIEW_TEMPLATE );
90 }
91
92 # get user list and format for template processing
93 0 my @users;
94 0 for my $user ( htfile_listusers( $self->param('act_config')->{ $query_obj->param('htfile') }->{'path'} ) ) {
95 0 push(@users, {'USERNAME' => $user});
96 }
97
98 # pass template parameters
99 $tmpl_obj->param(
100 'HTFILENAME' => $query_obj->param('htfile'),
101 'USER_LOOP' => \@users,
102 'IS_WARNINGS' => $#CGI::Carp::WARNINGS + 1,
103 0 'FORM_METHOD' => $self->param('act_config')->{'_'}->{'form_method'},
104 );
105
106 # return template-generated output
107 0 return $tmpl_obj->output;
108 }
109
110 # display the status of an operation
111 0 ▶sub display_status {
Subroutine display_status: not called CC=11 SCAR=48.8
Pod undocumented
112 0 my($self, $mode, $htfile, @users) = @_;
113
114 # load template file with HTML::Template
115 0 my $tmpl_obj;
116 0 ▶ if(-d $self->param('act_config')->{'_'}->{'tmpl_path'}.'status.tmpl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->param('act_config')->{'_'}{'tmpl_path'} . 'status.tmpl') { } 0 0 0
117 0 $tmpl_obj = $self->load_tmpl( $self->param('act_config')->{'_'}->{'tmpl_path'}.'status.tmpl' );
118 }
119 else {
120 0 $tmpl_obj = $self->load_tmpl( \$Web::Passwd::STATUS_TEMPLATE );
121 }
122
123 # build action status header
124 0 ▶ my $act_stat = ($mode eq 'adduser') ? "Addition Successful"
Branch: 3 branches
BranchTrueFalseTotal
$mode eq 'adduser' ? : 0 0 0
$mode eq 'changepw' ? : 0 0 0
$mode eq 'deluser' ? : 0 0 0
125 : ($mode eq 'changepw') ? "Modification Successful"
126 : ($mode eq 'deluser') ? "Deletion Successful"
127 : "Action Successful";
128
129 # build action message
130 0 ▶ my $act_msg = ($mode eq 'adduser') ? sprintf("User '%s' added.", $users[0])
Branch: 4 branches
BranchTrueFalseTotal
$mode eq 'adduser' ? : 0 0 0
$mode eq 'changepw' ? : 0 0 0
$mode eq 'deluser' && $#users == 0 ? : 0 0 0
$mode eq 'deluser' && $#users > 0 ? : 0 0 0
Condition0%0/6
$mode eq "deluser" and $#users == 0
!ll&&!rl&&r
000
$mode eq "deluser" and $#users > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$mode eq "deluser" and $#users == 0
$mode eq "deluser" $#users == 0
MC/DC0%0/2
$mode eq "deluser" and $#users > 0
$mode eq "deluser" $#users > 0
Truth table0%0/3
A and B
A= $mode eq "deluser"B= $#users == 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $mode eq "deluser"B= $#users > 0
ABresult
0X0
100
111
131 : ($mode eq 'changepw') ? sprintf("Password changed for user '%s'.", $users[0])
132 : ($mode eq 'deluser' && $#users == 0) ? sprintf("User '%s' deleted.", $users[0])
133 : ($mode eq 'deluser' && $#users > 0) ? sprintf("Users '%s' deleted.", join "','", @users)
134 : 'Unknown operation...Check error logs.';
135
136 # pass template parameters
137 $tmpl_obj->param(
138 'ACTION_STATUS' => $act_stat,
139 'ACTION_MESSAGE' => $act_msg,
140 'HTFILENAME' => $htfile,
141 'IS_WARNINGS' => $#CGI::Carp::WARNINGS + 1,
142 0 'FORM_METHOD' => $self->param('act_config')->{'_'}->{'form_method'},
143 );
144
145 # return template-generated output
146 0 return $tmpl_obj->output;
147 }
148
149 # perform an operation
150 0 ▶sub user_op {
Subroutine user_op: not called CC=8 SCAR=42.8
Pod undocumented
151 0 my $self = shift;
152
153 # get CGI query object
154 0 my $query_obj = $self->query();
155
156 # create lexical copy of mode
157 0 my $user_mode = lc $query_obj->param('mode');
158
159 # if adding or modifying user, check that passwords match
160 0 ▶ if($user_mode eq 'adduser' || $user_mode eq 'changepw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_mode eq "adduser" or $user_mode eq "changepw") 0 0 0
Condition0%0/3
$user_mode eq "adduser" or $user_mode eq "changepw"
l!l&&r!l&&!r
000
MC/DC0%0/2
$user_mode eq "adduser" or $user_mode eq "changepw"
$user_mode eq "adduser" $user_mode eq "changepw"
Truth table0%0/3
A or B
A= $user_mode eq "adduser"B= $user_mode eq "changepw"
ABresult
1X1
011
000
161 0 ▶ if($query_obj->param('pass') ne $query_obj->param('pass_confirm')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_obj->param("pass") ne $query_obj->param("pass_confirm")) 0 0 0
162 0 die 'passwords did not match';
163 }
164 }
165
166 # load configuration as a hash ref
167 0 $self->param('act_config', load_config( $self->param('config') ) );
168
169 # add new or change existing user/pass
170 0 my @users = $query_obj->param('user');
171 0 ▶ if($user_mode eq 'adduser' || $user_mode eq 'changepw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($user_mode eq 'adduser' or $user_mode eq 'changepw') { } 0 0 0
elsif ($user_mode eq 'deluser') { } 0 0 0
Condition0%0/3
$user_mode eq "adduser" or $user_mode eq "changepw"
l!l&&r!l&&!r
000
MC/DC0%0/2
$user_mode eq "adduser" or $user_mode eq "changepw"
$user_mode eq "adduser" $user_mode eq "changepw"
Truth table0%0/3
A or B
A= $user_mode eq "adduser"B= $user_mode eq "changepw"
ABresult
1X1
011
000
172 htfile_moduser(
173 $self->param('act_config')->{'_'}->{'htpasswd_command'},
174 $self->param('act_config')->{ $query_obj->param('htfile') }->{'path'},
175 $users[0],
176 $query_obj->param('pass'),
177 0 $self->param('act_config')->{ $query_obj->param('htfile') }->{'algorithm'}
178 );
179 }
180 # or delete existing user(s)
181 elsif($user_mode eq 'deluser') {
182 0 for my $user (@users) {
183 htfile_deluser(
184 $self->param('act_config')->{'_'}->{'htpasswd_command'},
185 0 $self->param('act_config')->{ $query_obj->param('htfile') }->{'path'},
186 $user
187 );
188 }
189 }
190
191 # generate operation status page from template
192 0 my $tmpl_output = display_status( $self, $user_mode, $query_obj->param('htfile'), @users );
193
194 # return template-generated output
195 0 return $tmpl_output;
196 }
197
198 # load the app configuration, returning a hash reference
199 0 ▶sub load_config {
Subroutine load_config: not called CC=18 SCAR=58.3
Pod undocumented
200 0 my $conf_file = shift;
201
202 # if custom configuration not provided, search for a default config file
203 0 ▶ if(!defined $conf_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf_file) 0 0 0
204 # expected filename of the config
205 0 my $CONFIG_FILENAME = 'webpasswd.conf';
206
207 # search for config file in current, parent, and /etc directories
208 0 ▶ $conf_file = (-e "./$CONFIG_FILENAME") ? "./$CONFIG_FILENAME"
Branch: 3 branches
BranchTrueFalseTotal
-e "./$CONFIG_FILENAME" ? : 0 0 0
-e "../$CONFIG_FILENAME" ? : 0 0 0
-e "/etc/$CONFIG_FILENAME" ? : 0 0 0
209 : (-e "../$CONFIG_FILENAME") ? "../$CONFIG_FILENAME"
210 : (-e "/etc/$CONFIG_FILENAME") ? "/etc/$CONFIG_FILENAME"
211 : undef;
212
213 # die if config file was not found
214 0 ▶ if(! defined $conf_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf_file) 0 0 0
215 0 die "configuration file not found"
216 }
217 }
218
219 # load configuration, or die on error
220 0 ▶ my $config_obj = Config::Tiny->read($conf_file) or die Config::Tiny::errstr();
Branch: 1 branch
BranchTrueFalseTotal
my $config_obj = "Config::Tiny"->read($conf_file) or die(Config::Tiny::errstr()) 0 0 0
221
222 # if no htpasswd command supplied, default to 'htpasswd'
223 0 ▶ if(!exists $config_obj->{'_'}->{'htpasswd_command'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config_obj->{"_"}{"htpasswd_command"}) 0 0 0
224 0 $config_obj->{'_'}->{'htpasswd_command'} = 'htpasswd';
225 0 warn "missing 'htpasswd_command' configuration option, using default of 'htpasswd'";
226 }
227
228 # if template path doesnt exist, try root path
229 0 ▶ if(!exists $config_obj->{'_'}->{'tmpl_path'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config_obj->{"_"}{"tmpl_path"}) 0 0 0
230 0 $config_obj->{'_'}->{'tmpl_path'} = '/';
231 }
232
233 # if template path doesnt end with a fore-slash, append one
234 0 ▶ if(substr($config_obj->{'_'}->{'tmpl_path'}, -1) ne '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($config_obj->{"_"}{"tmpl_path"}, -1) ne "/") 0 0 0
235 0 $config_obj->{'_'}->{'tmpl_path'} .= '/';
236 }
237
238 # if form method not provided or not GET, default to POST
239 0 ▶ if(!exists $config_obj->{'_'}->{'form_method'} || uc($config_obj->{'_'}->{'form_method'}) ne 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $config_obj->{"_"}{"form_method"} or uc $config_obj->{"_"}{"form_method"} ne "GET") 0 0 0
Condition0%0/3
not exists $config_obj->{"_"}{"form_method"} or uc $config_obj->{"_"}{"form_method"} ne "GET"
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $config_obj->{"_"}{"form_method"} or uc $config_obj->{"_"}{"form_method"} ne "GET"
not exists $config_obj->{"_"}{"form_method"} uc $config_obj->{"_"}{"form_method"} ne "GET"
Truth table0%0/3
not(A) or B
A= not exists $config_obj->{"_"}{"form_method"}B= uc $config_obj->{"_"}{"form_method"} ne "GET"
ABresult
1X1
011
000
240 0 $config_obj->{'_'}->{'form_method'} = 'POST';
241 }
242
243 # ensure valid attributes for each configured section
244 0 for my $section (keys %{$config_obj}) {
245 # if not root section
246 0 ▶ if($section ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($section ne "_") 0 0 0
247 # if missing path, remove from active config and issue warning
248 0 my $file_path = $config_obj->{$section}->{'path'};
249 0 ▶ if(!defined $file_path || $file_path =~ m/\A\s*\z/ || ! -e $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file_path or $file_path =~ /\A\s*\z/ or not -e $file_path) 0 0 0
Condition0%0/6
not defined $file_path or $file_path =~ /\A\s*\z/ or not -e $file_path
l!l&&r!l&&!r
000
not defined $file_path or $file_path =~ /\A\s*\z/
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $file_path or $file_path =~ /\A\s*\z/ or not -e $file_path
not defined $file_path $file_path =~ /\A\s*\z/ not -e $file_path
Truth table0%0/4
not(A) or B or not(C)
A= not defined $file_pathB= $file_path =~ /\A\s*\z/C= not -e $file_path
ABCresult
1XX1
01X1
0011
0000
250 0 delete $config_obj->{$section};
251 0 warn "invalid path for config block [$section]";
252 0 next;
253 }
254
255 # if missing or invalid algorithm, default to 'crypt' and issue warning
256 0 my $pass_alg = lc $config_obj->{$section}->{'algorithm'};
257 0 ▶ if($pass_alg !~ m/\s*(?:crypt|md5|sha|plain)\s*/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass_alg =~ /\s*(?:crypt|md5|sha|plain)\s*/i) 0 0 0
258 0 warn "invalid password algorithm '$pass_alg' for config block [$section], using 'crypt' instead";
259 0 $pass_alg = 'crypt';
260 }
261 0 $config_obj->{$section}->{'algorithm'} = $pass_alg;
262 }
263 }
264
265 # return config
266 0 return $config_obj;
267 }
268
269 # list the users in a given htfile
270 0 ▶sub htfile_listusers {
Subroutine htfile_listusers: not called CC=3 SCAR=24.8
Pod undocumented
271 0 my $htfile = shift;
272
273 # declare array to hold usernames
274 0 my @users;
275
276 # read htfile in as text
277 0 ▶ open(my $HTFILE, '<', $htfile) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $HTFILE, "<", $htfile or die($!) 0 0 0
278 0 my @file = <$HTFILE>;
279 0 close($HTFILE);
280
281 # parse off usernames, add to array
282 0 for my $line (@file) {
283 0 my($user) = split /:/, $line, 2;
284 0 push @users, $user;
285 }
286
287 # return username array
288 0 return @users;
289 }
290
291 # add/modify a user in a given htfile
292 0 ▶sub htfile_moduser {
Subroutine htfile_moduser: not called CC=6 SCAR=37.4
Pod undocumented
293 0 my($htcmd,$htfile,$user,$pass,$algorithm) = @_;
294
295 # translate algorithm to appropriate flag
296 0 ▶ $algorithm = ($algorithm eq 'plain') ? 'p'
Branch: 4 branches
BranchTrueFalseTotal
$algorithm eq 'plain' ? : 0 0 0
$algorithm eq 'md5' ? : 0 0 0
$algorithm eq 'sha' ? : 0 0 0
$algorithm eq 'crypt' ? : 0 0 0
297 : ($algorithm eq 'md5') ? 'm'
298 : ($algorithm eq 'sha') ? 's'
299 : ($algorithm eq 'crypt') ? 'd'
300 : '';
301
302 # assemble command
303 0 my $command = sprintf "%s -b%s %s %s %s",
304 $htcmd,
305 $algorithm,
306 $htfile,
307 $user,
308 $pass;
309
310 # execute, or die on unsuccessful return value
311 0 ▶ if(system($command) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system($command) != 0) 0 0 0
312 0 die "htpasswd command failed: $?";
313 }
314
315 # return just to be tidy
316 0 return;
317 }
318
319 # delete a user in a given htfile
320 0 ▶sub htfile_deluser {
Subroutine htfile_deluser: not called CC=7 SCAR=37.6
Pod undocumented
321 0 my($htcmd,$htfile,$user) = @_;
322
323 # assemble command
324 0 my $command = sprintf "%s -D %s %s",
325 $htcmd,
326 $htfile,
327 $user;
328
329 # try to make htpasswd do the work with the -D flag (apache 2.x)
330 0 ▶ if(system($command) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system($command) == 0) { } 0 0 0
331 0 return 1;
332 }
333 # otherwise, do the damn thing by hand (apache 1.3.x)
334 else {
335 # read in htfile contents
336 0 ▶ open(my $HTIN, '<', $htfile) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $HTIN, "<", $htfile or die($!) 0 0 0
337 0 my @file = <$HTIN>;
338 0 close($HTIN);
339
340 # search for, and remove, offending user line
341 0 my $deleted = 0;
342 0 for my $ln (0..$#file) {
343 1 ▶ no warnings; # bypass a puzzling warning of uninitialized value in m//
Subroutine BEGIN: called CC=1 SCAR=0.0
344 0 ▶ if($file[$ln] =~ m/\A$user\:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file[$ln] =~ /\A$user\:/) 0 0 0
345 0 splice @file, $ln, 1;
346 0 $deleted++;
347 }
348 }
349
350 # write changes back to htfile
351 0 ▶ open(my $HTOUT, '>', $htfile) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $HTOUT, ">", $htfile or die($!) 0 0 0
352 0 print {$HTOUT} @file;
353 0 close($HTOUT);
354
355 # set error string
356 0 ▶ $! = ($deleted) ? undef : "remove of line '$user' failed";
Branch: 1 branch
BranchTrueFalseTotal
$deleted ? : 0 0 0
357
358 0 return $deleted;
359 }
360 }
361
362
363 1 $Web::Passwd::INDEX_TEMPLATE = <<'HTML_CODE';
364 <html>
365
366 <head>
367 <title>Web Htpasswd Management</title>
368 <style>
369 body { font-family: Arial }
370 table { font-family: Arial }
371 </style>
372 </head>
373
374 <body>
375
376 <center>
377
378 <h1>Web Htpasswd Management</h1>
379
380 <hr>
381
382 <p>
383 <form method="<TMPL_VAR NAME="FORM_METHOD" DEFAULT="POST">">
384 <input type="hidden" name="mode" value="view">
385
386 Select Htpasswd File:&nbsp;&nbsp;
387
388 <select name="htfile">
389 <TMPL_LOOP NAME="HTFILES">
390 <option value="<TMPL_VAR NAME="TITLE">"><TMPL_VAR NAME="TITLE"></option>
391 </TMPL_LOOP>
392 </select>
393
394 <input type="submit" value="Manage File">
395
396 </form>
397 </p>
398
399 <TMPL_IF NAME="IS_WARNINGS">
400 <hr>
401 <span style="color: red">Warnings were encountered...Please check error log.</span>
402 </TMPL_IF>
403
404 </center>
405
406 </body>
407
408 </html>
409 <!--
410 <TMPL_VAR NAME="DEBUG_DUMP" DEFAULT="">
411 -->
412 HTML_CODE
413
414 1 $Web::Passwd::VIEW_TEMPLATE = <<'HTML_CODE';
415 <html>
416
417 <head>
418 <title>Web Htpasswd Management</title>
419 <style>
420 body { font-family: Arial }
421 table { font-family: Arial }
422 </style>
423 </head>
424
425 <body>
426
427 <center>
428
429 <h1>Managing Htpasswd File:</h1>
430 <h3><TMPL_VAR NAME="HTFILENAME"></h3>
431
432 <hr>
433
434 <p>
435 <form method="<TMPL_VAR NAME="FORM_METHOD" DEFAULT="POST">">
436 <input type="hidden" name="htfile" value="<TMPL_VAR NAME="HTFILENAME">">
437 <input type="hidden" name="mode" value="adduser">
438 <fieldset style="width: 400">
439 <legend style="font-weight: bold">Add User</legend>
440 <table width="350">
441 <tr>
442 <td width="150">Username:&nbsp;&nbsp;</td>
443 <td width="200"><input type="text" name="user" size=255 style="width: 200"></td>
444 </tr>
445 <tr>
446 <td>Password:&nbsp;&nbsp;</td>
447 <td><input type="password" name="pass" size=255 style="width: 200"></td>
448 </tr>
449 <tr>
450 <td>Retype Password:&nbsp;&nbsp;</td>
451 <td><input type="password" name="pass_confirm" size=255 style="width: 200"></td>
452 </tr>
453 <tr>
454 <td></td>
455 <td><input type="submit" value="Add User"></td>
456 </tr>
457 </table>
458 </fieldset>
459 </form>
460 </p>
461
462 <p>
463 <form method="<TMPL_VAR NAME="FORM_METHOD" DEFAULT="POST">">
464 <input type="hidden" name="htfile" value="<TMPL_VAR NAME="HTFILENAME">">
465 <input type="hidden" name="mode" value="changepw">
466 <fieldset style="width: 400">
467 <legend style="font-weight: bold">Modify User</legend>
468 <table width="350">
469 <tr>
470 <td width="150">Username:&nbsp;&nbsp;</td>
471 <td width="200">
472 <select name="user" style="width: 200">
473 <TMPL_LOOP NAME="USER_LOOP">
474 <option value="<TMPL_VAR NAME="USERNAME">"><TMPL_VAR NAME="USERNAME"></option>
475 </TMPL_LOOP>
476 </select>
477 </td>
478 </tr>
479 <tr>
480 <td>Password:&nbsp;&nbsp;</td>
481 <td><input type="password" name="pass" size=255 style="width: 200"></td>
482 </tr>
483 <tr>
484 <td>Retype Password:&nbsp;&nbsp;</td>
485 <td><input type="password" name="pass_confirm" size=255 style="width: 200"></td>
486 </tr>
487 <tr>
488 <td></td>
489 <td><input type="submit" value="Change Password"></td>
490 </tr>
491 </table>
492 </fieldset>
493 </form>
494 </p>
495
496 <p>
497 <form method="<TMPL_VAR NAME="FORM_METHOD" DEFAULT="POST">">
498 <input type="hidden" name="htfile" value="<TMPL_VAR NAME="HTFILENAME">">
499 <input type="hidden" name="mode" value="deluser">
500 <fieldset style="width: 400">
501 <legend style="font-weight: bold">Delete Users</legend>
502 <table width="350">
503 <tr>
504 <td width="150" valign="top">Usernames:&nbsp;&nbsp;</td>
505 <td width="200">
506 <select name="user" size="5" multiple style="width: 200px">
507 <TMPL_LOOP NAME="USER_LOOP">
508 <option value="<TMPL_VAR NAME="USERNAME">"><TMPL_VAR NAME="USERNAME"></option>
509 </TMPL_LOOP>
510 </select>
511 </td>
512 </tr>
513 <tr>
514 <td></td>
515 <td><input type="submit" value="Delete User"></td>
516 </tr>
517 </table>
518 </fieldset>
519 </form>
520 </p>
521
522 <hr>
523 <a href="?">Back to Main</a>
524 <TMPL_IF NAME="IS_WARNINGS">
525 <hr>
526 <span style="color: red">Warnings were encountered...Please check error log.</span>
527 </TMPL_IF>
528
529 </center>
530
531 </body>
532
533 </html>
534 <!--
535 <TMPL_VAR NAME="DEBUG_DUMP" DEFAULT="">
536 -->
537 HTML_CODE
538
539 1 $Web::Passwd::STATUS_TEMPLATE = <<'HTML_CODE';
540 <html>
541
542 <head>
543 <title>Web Htpasswd Management</title>
544 <style>
545 body { font-family: Arial }
546 table { font-family: Arial }
547 </style>
548 </head>
549
550 <body>
551
552 <center>
553
554 <h1><TMPL_VAR NAME="ACTION_STATUS"></h1>
555
556 <p>
557 <form method="<TMPL_VAR NAME="FORM_METHOD" DEFAULT="POST">">
558 <TMPL_VAR NAME="ACTION_MESSAGE"><br>&nbsp;<br>
559 <input type="hidden" name="htfile" value="<TMPL_VAR NAME="HTFILENAME">">
560 <input type="hidden" name="mode" value="view">
561 <input type="submit" value="Go Back">
562 </form>
563 </p>
564
565 <TMPL_IF NAME="IS_WARNINGS">
566 <hr>
567 <span style="color: red">Warnings were encountered...Please check error log.</span>
568 </TMPL_IF>
569
570 </center>
571
572 </body>
573
574 </html>
575 <!--
576 <TMPL_VAR NAME="DEBUG_DUMP" DEFAULT="">
577 -->
578 HTML_CODE
579
580 =head1 NAME
581
582 Web::Passwd - Web-based htpasswd Management
583
584 =head1 VERSION
585
586 Version 0.03
587
588 =cut
589 1 our $VERSION = "0.03";
590
591 =head1 SYNOPSIS
592
593 Web::Passwd is a web-based utility for managing Apache C<htpasswd> files. It uses the L<CGI::Application|CGI::Application> framework, so functionality is encapsulated in the module and very little code is required to create an instance:
594
595 use Web::Passwd;
596
597 my $webapp = Web::Passwd->new();
598 $webapp->run();
599
600 That's it. Drop that script in a web-accessible cgi directory and give it execute permissions, and (assuming a default config file is found), you're good to go. If you'd rather explicity define a configuration file to use, you can pass it through an extra parameter:
601
602 my $webapp = Web::Passwd->new( PARAMS => { config => '/home/evan/custom_webpasswd.conf' } );
603
604 =head1 CONFIGURATION
605
606 If not explicitly provided, a configuration file will be searched for in the following locations (in order). If a valid configuration file is not found, the script will die with errors.
607
608 ./webpasswd.conf (the current directory)
609 ../webpasswd.conf (the parent directory)
610 /etc/webpasswd.conf
611
612 The configuration file can be used to specify a directory of templates in the L<HTML::Template|HTML::Template> format. If no templates are found, default templates are used (see the C</example/templates> directory of the distribution).
613
614 tmpl_path = /var/www/cgi-bin/webpasswd/
615
616 The C<htpasswd> command can also be specified. If no C<htpasswd> command is provided, the default is used. Note that, on some systems, you must specify the I<absolute> path to the C<htpasswd> binary.
617
618 htpasswd_command = htpasswd
619
620 The configuration file can specify whether to use the C<GET> (data encoded into the URL) or C<POST> (data encoded into the message body) form request method. Defaults to using the generally more secure C<POST>.
621
622 form_method = POST
623
624 The configuration file should also contain a section for each htpasswd file it will be used to maintain, using the following format:
625
626 [Descriptive Name]
627 path = /system/path/to/passwdfile
628 algorithm = {crypt|md5|sha|plain}
629
630 B<TECHNICAL NOTE:> The default algorithm Apache uses is C<crypt> under Linux, and C<MD5> under Windows.
631
632 B<PITFALL:> Enclosing values in quotes within the config file does not have the expected effect! It simply includes the literal quote characters in the config value.
633
634 =head1 SECURITY
635
636 It is *imperitive* that the Web::Passwd instance script itself be htpasswd protected, as it includes no access control mechanism.
637
638 Understand that putting the ability to manage htpasswd files via a web-based utility carries an inherent security risk, in that anyone who gains access to the utility is potentially given access to any of the managed htpasswd-protected resources.
639
640 Any htpasswd files to be managed with this utility MUST be owned by whatever user apache runs as. Usually, this is 'apache' or 'nobody'.
641
642 =head1 COMPATABILITY
643
644 This was written expressly for Apache webserver 1.3 or higher running under Linux. However, there is nothing as far as I am aware that would prevent execution on a higher version of Apache, or on Apache under Windows.
645
646 =head1 DEPENDENCIES
647
648 A Perl version of 5.6.1 or higher is recommended, and the following modules are required:
649
650 CGI::Application
651 Config::Tiny
652 HTML::Template
653
654 =head1 AUTHOR
655
656 Evan Kaufman, C<< <evank at cpan.org> >>
657
658 =head1 SUPPORT
659
660 You can find documentation for this module with the perldoc command.
661
662 perldoc Web::Passwd
663
664 =head1 ACKNOWLEDGEMENTS
665
666 Written for BCD Music Group.
667
668 =head1 COPYRIGHT & LICENSE
669
670 Copyright 2007 Evan Kaufman, all rights reserved.
671
672 This program is free software; you can redistribute it and/or modify it
673 under the same terms as Perl itself.
674
675 =cut
676
677 # we're a good little module
678 1 1;
↑ Summary