WE/Util/Htpasswd.pm

filter: Statementstmt 22.9% 14 / 61 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 4 / 9 Podpod 75.0% 3 / 4 totaltotal 22.8% 21 / 92 CC 12 SCAR 31.0
CC
12
Cov
18%
htpasswd_exe
42.0
create
30.0
add_user
12.0
CRAP 22.3
« WebEditor/OldFeatures/KeyDB.pm ↑ Summary WE/DB/Base.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Htpasswd.pm,v 1.8 2004/04/14 14:42:54 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2002 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE::Util::Htpasswd;
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use vars qw($VERSION $HTPASSWD_EXE);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
22  
23 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use WE::Util::Functions qw(is_in_path _save_pwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 WE::Util::Htpasswd - create apache .htpasswd files from a user database
29  
30 =head1 SYNOPSIS
31  
32 use WE::Util::Htpasswd;
33 WE::Util::Htpasswd::create("/var/www/.htpasswd", $complex_user_db);
34  
35 =head1 DESCRIPTION
36  
37 Create apache C<.htpasswd> files from a WE_Framework user database.
38  
39 =head2 FUNCTIONS
40  
41 =over 4
42  
43 =item create($dest_file, $user_db, %args);
44  
45 Create the .htpasswd as C<$dest_file> from the (complex) user database
46 object C<$user_db>. Note that the user database should use the "none"
47 C<CryptMode> (that is, store plain text passwords).
48  
49 =cut
50  
51 0 ▶sub create {
Subroutine create: not called CC=5 SCAR=34.0
Pod documented
52 0 my($dest_file, $user_db, %args) = @_;
53 0 ▶ if ($user_db->CryptMode ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_db->CryptMode ne "none") 0 0 0
54 0 die "CryptMode of the user database should be none";
55 }
56 0 unlink $dest_file;
57 0 ▶ my $devnull = File::Spec->can("devnull") ? File::Spec->devnull : "/dev/null";
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->can('devnull') ? : 0 0 0
58 #my $devnull = "/tmp/htpasswd-debug.log";
59 0 my $htpasswd = htpasswd_exe();
60 0 my @args = ('-c'); # first time: create htpasswd
61 0 foreach my $uid ($user_db->get_all_users) {
62 0 my $u = $user_db->get_user_object($uid);
63 0 my $p = $u->Password;
64  
65 0 *OLDERR = *OLDERR;
66 0 open(OLDERR, ">&STDERR");
67 0 open(STDERR, ">" . $devnull);
68 0 my @cmd = ($htpasswd, @args, "-b", $dest_file, $uid, $p);
69 0 ▶ _save_pwd {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 # htpasswd seems to use the current directory as temporary
71 # directory, so help here for a better location:
72 0 chdir "/tmp";
73 0 system @cmd;
74 0 };
75 0 close STDERR;
76 0 open(STDERR, ">&OLDERR");
77  
78 0 ▶ if ($?/256!=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? / 256 != 0) 0 0 0
79 0 die "htpasswd for file $dest_file and uid $uid returned " . ($?/256) . "\nCommand line was: @cmd\nPATH was $ENV{PATH}";
80 }
81 0 @args = ();
82 }
83 0 1;
84 }
85  
86 =item add_user($dest_file, $user_object, %args);
87  
88 Add the entry for a user to the C<.htpasswd> file C<$dest_file>. The
89 user object should be a C<WE::UserObj> object as created in
90 C<WE::DB::ComplexUser>.
91  
92 =cut
93  
94 0 ▶sub add_user {
Subroutine add_user: not called CC=3 SCAR=24.8
Pod documented
95 0 my($dest_file, $u, %args) = @_;
96 0 my $uid = $u->Username;
97 0 my $p = $u->Password;
98 0 my @args;
99 0 ▶ if (!-e $dest_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dest_file) 0 0 0
100 0 push @args, "-c";
101 }
102 0 my $htpasswd = htpasswd_exe();
103 0 my @cmd = ($htpasswd, @args, "-b", $dest_file, $uid, $p);
104 0 system @cmd;
105 0 ▶ if ($?/256!=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? / 256 != 0) 0 0 0
106 0 die "htpasswd for file $dest_file and uid $uid returned " . ($?/256) . "\nCommand line was: @cmd";
107 }
108 0 1;
109 }
110  
111 =item invalid_chars
112  
113 Return a string of invalid characters for htpasswd usernames. This is handy
114 for using in C<ComplexUser>:
115  
116 new WE::DB::ComplexUser(..., ...,
117 -crypt => "none",
118 -invalidchars => WE::Util::Htpasswd::invalid_chars(),
119 -invalidgroupchars => WE::Util::Htgroup::invalid_chars())
120  
121 =cut
122  
123 0 ▶sub invalid_chars {
Subroutine invalid_chars: not called CC=1 SCAR=6.9
Pod documented
124 0 ":";
125 }
126  
127 0 ▶sub htpasswd_exe {
Subroutine htpasswd_exe: not called CC=6 SCAR=37.4
Pod undocumented
128 TRY: {
129 0 ▶ if (!defined $HTPASSWD_EXE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $WE::Util::Htpasswd::HTPASSWD_EXE) 0 0 0
130 0 $HTPASSWD_EXE = is_in_path("htpasswd");
131 0 ▶ last TRY if defined $HTPASSWD_EXE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $WE::Util::Htpasswd::HTPASSWD_EXE 0 0 0
132 0 for my $exe (qw(/usr/local/bin/htpasswd
133 /usr/local/apache/bin/htpasswd)) {
134 0 ▶ if (-x $exe) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $exe) 0 0 0
135 0 $HTPASSWD_EXE = $exe;
136 0 last TRY;
137 }
138 }
139 }
140 }
141 0 ▶ if (!defined $HTPASSWD_EXE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $WE::Util::Htpasswd::HTPASSWD_EXE) 0 0 0
142 0 die "Cannot find htpasswd binary in $ENV{PATH}";
143 }
144 0 $HTPASSWD_EXE;
145 }
146  
147 1 1;
148  
149 __END__
« WebEditor/OldFeatures/KeyDB.pm ↑ Summary WE/DB/Base.pm »