blib/lib/Unix/SetUser.pm

filter: Statementstmt 22.1% 25 / 113 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 17.0% 33 / 194 CC 33 SCAR 68.1
CC
33
Cov
14%
set_user
1122.0
_warn_ids
2.0
BEGIN
1.0
CRAP 903.3
↑ Summary blib/lib/Unix/SavedIDs.pm »
1 # $Id$
2  
3 package Unix::SetUser;
4  
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Unix::SavedIDs 0.004002;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #warn "\@INC = ".join(" ",@INC)."\n";
12 #warn "\%INC = ".Dumper(\%INC)."\n";
13  
14 2 our $verbose = 0;
15  
16 2 my $warncount = 0;
17  
18 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 our ($VERSION,@ISA,@EXPORT);
21 2 @ISA = qw(Exporter);
22 2 @EXPORT = qw(set_user);
23 2 $VERSION = 0.004003;
24 }
25  
26 0 ▶sub set_user {
Subroutine set_user: not called CC=33 SCAR=70.2
Pod documented
27 ## Figure Out IDs to Set
28 0 my($user,$group,@sup_groups) = @_;
29 0 my($uid,$gid,%sup_gids);
30 0 my $is_int = qr/^(\d+)$/o;
31 0 ▶ if ( !defined($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 0 0
32 0 croak "set_user() called with no arguments";
33 }
34 # get uids
35 0 ▶ if ( $user =~ $is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user =~ /$is_int/) { } 0 0 0
36 0 $uid = $1;
37 }
38 else {
39 # get numeric uid if given non-numeric user name
40 0 $uid = getpwnam($user);
41 0 ▶ if ( !defined($uid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid) 0 0 0
42 0 croak "User '$user' does not exist";
43 }
44 0 ▶ if ( $uid !~ $is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uid =~ /$is_int/) 0 0 0
45 0 croak "User id for '$user' is not an int. "
46 ."This shouldn't ever happen";
47 }
48 0 $uid = $1;
49 }
50 # get primary gid
51 0 ▶ if ( defined($group) && $group =~ $is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group and $group =~ /$is_int/) { } 0 0 0
Condition0%0/3
defined $group and $group =~ /$is_int/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $group and $group =~ /$is_int/
defined $group $group =~ /$is_int/
Truth table0%0/3
A and B
A= defined $groupB= $group =~ /$is_int/
ABresult
0X0
100
111
52 0 $gid = $1;
53 0 ▶ if ( !getgrgid($gid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (getgrgid $gid) 0 0 0
54 0 croak "Primary group id '$gid' does not exist\n";
55 }
56 }
57 else {
58 # use user's primary group if no primary group specified
59 0 ▶ if ( !defined($group) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $group) { } 0 0 0
60 0 $gid = (getpwuid($uid))[3];
61 0 ▶ if ( !defined($gid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gid) 0 0 0
62 0 croak "Failed to get primary group ID for uid '$uid'";
63 }
64 }
65 else {
66 # get gid from group name
67 0 $gid = getgrnam($group);
68 0 ▶ if ( !defined($gid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gid) 0 0 0
69 0 croak "Primary group '$group' does not exist";
70 }
71  
72 }
73 0 ▶ if ( $gid !~ $is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gid =~ /$is_int/) 0 0 0
74 0 croak "Primary group ID for '$user' is not an int. "
75 ."This shouldn't ever happen";
76 }
77 }
78 # get supplemental groups
79 0 my @dont_exist;
80 0 foreach my $sup_group (@sup_groups) {
81 0 my $sup_gid;
82 0 ▶ if ( !defined($sup_group) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sup_group) 0 0 0
83 0 croak "Supplemental group list contains 'undef'";
84 }
85 # if ints, make sure they exist
86 0 ▶ if ( $sup_group =~ $is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sup_group =~ /$is_int/) { } 0 0 0
87 0 $sup_gid = $1;
88 0 ▶ if ( $sup_gid == $gid || defined($sup_gids{$sup_gid})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sup_gid == $gid or defined $sup_gids{$sup_gid}) 0 0 0
Condition0%0/3
$sup_gid == $gid or defined $sup_gids{$sup_gid}
l!l&&r!l&&!r
000
MC/DC0%0/2
$sup_gid == $gid or defined $sup_gids{$sup_gid}
$sup_gid == $gid defined $sup_gids{$sup_gid}
Truth table0%0/3
A or B
A= $sup_gid == $gidB= defined $sup_gids{$sup_gid}
ABresult
1X1
011
000
89 # remove duplicates
90 0 next;
91 }
92 0 ▶ if ( !getgrgid($sup_gid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (getgrgid $sup_gid) 0 0 0
93 0 push(@dont_exist,$sup_group);
94 0 next;
95 }
96 }
97 # if not ints, convert text usernames to ints which tests existance
98 else {
99 0 $sup_gid = getgrnam($sup_group);
100 0 ▶ if ( !defined($sup_gid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sup_gid) 0 0 0
101 0 push(@dont_exist,$sup_group);
102 0 next;
103 }
104 0 ▶ if ( $sup_gid !~ $is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sup_gid =~ /$is_int/) 0 0 0
105 0 croak "Supplemental group id for group '$sup_group' is "
106 ."not an int. This shouldn't ever happen";
107 }
108 0 ▶ if ( $sup_gid == $gid || defined($sup_gids{$sup_gid}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sup_gid == $gid or defined $sup_gids{$sup_gid}) 0 0 0
Condition0%0/3
$sup_gid == $gid or defined $sup_gids{$sup_gid}
l!l&&r!l&&!r
000
MC/DC0%0/2
$sup_gid == $gid or defined $sup_gids{$sup_gid}
$sup_gid == $gid defined $sup_gids{$sup_gid}
Truth table0%0/3
A or B
A= $sup_gid == $gidB= defined $sup_gids{$sup_gid}
ABresult
1X1
011
000
109 # duplicate
110 0 next;
111 }
112 }
113 0 $sup_gids{$sup_gid} = 1;
114 }
115 0 ▶ if ( @dont_exist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dont_exist) 0 0 0
116 0 croak "Specified supplemental group(s) '".join("', '",@dont_exist)
117 ."' do(es) not exist(s)";
118 }
119  
120 ## Set Ids
121  
122 # set gid, egid and sgid
123 0 setresgid($gid,$gid,$gid);
124 # set egid & supplimental gids
125 0 my $egid_string = $gid;
126 0 ▶ if ( keys(%sup_gids) == 0 ) { # $) lists primary group twice
Branch: 1 branch
BranchTrueFalseTotal
if (keys %sup_gids == 0) { } 0 0 0
127 0 $egid_string .= ' '.$gid; # if it's your only group
128 }
129 else {
130 0 $egid_string .= ' '.join(' ',sort(keys(%sup_gids)));
131 }
132 0 $) = $egid_string;
133 # set uid,euid & suid
134 #_warn_ids();
135 #warn "-- Setting uid,euid & suid setresuid($uid,$uid,$uid)\n";
136 0 setresuid($uid,$uid,$uid);
137 #_warn_ids();
138  
139 ## Make sure everything worked
140 0 my @errs = ();
141 # make sure uid change worked
142 0 ▶ if ( $< != $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($< != $uid) 0 0 0
143 0 push(@errs,"Failed to set uid: uid = '$<', not '$uid'");
144 }
145 # make sure set euid worked
146 0 ▶ if ( $> != $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($> != $uid) 0 0 0
147 0 push(@errs,"Failed to set euid: euid = '$>', not '$uid'");
148 }
149 # make sure saved uid got set correctly
150 0 my $suid = (getresuid())[2];
151 0 ▶ if ( $suid != $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suid != $uid) 0 0 0
152 0 push(@errs,'Failed to set saved uid: saved uid = '.$suid
153 .' not '.$uid);
154 }
155 #warn "about to check rgid\n";
156 #_warn_ids();
157 # make sure rgid is correct
158 0 ▶ if ( $( !~ /^(\d+)/o ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($( =~ /^(\d+)/o) 0 0 0
159 0 croak "GID is not an int!??!";
160 }
161 0 my $now_gid = $1;
162 0 ▶ if ( $now_gid != $gid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now_gid != $gid) 0 0 0
163 0 croak "Failed to set gid: gid is '$now_gid' not '$gid'\n";
164 }
165 # make sure set egid & supplimental gids worked
166 0 ▶ if ( $) ne $egid_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($) ne $egid_string) 0 0 0
167 # Order doesn't matter, so split, sort and rejoin to test.
168 0 my $now = join(' ',sort(split(' ',$))));
169 0 my $want = join(' ',sort(split(' ',$egid_string)));
170 0 ▶ if ( $now ne $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now ne $want) 0 0 0
171 0 push(@errs,"Failed to set egid: egid = '$)', not '$egid_string'");
172 }
173 }
174 # make sure sgid got set correctly
175 0 my $sgid = (getresgid())[2];
176 0 ▶ if ( $sgid != $gid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sgid != $gid) 0 0 0
177 0 push(@errs,'Failed to set saved gid: saved gid = '.$sgid
178 .' not '.$gid);
179 }
180 0 ▶ if ( @errs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errs) 0 0 0
181 0 croak join("\n",@errs);
182 }
183 #_warn_ids();
184 0 return undef;
185 }
186  
187 0 ▶sub _warn_ids {
Subroutine _warn_ids: not called CC=1 SCAR=6.9
188 0 $warncount++;
189 0 warn "count = $warncount\n";
190 0 warn "\$< = '$<'\n";
191 0 warn "\$> = '$>'\n";
192 0 warn "\$( = '$('\n";
193 0 warn "\$) = '$)'\n";
194 0 warn "geresuid = ".join(',',getresuid())."\n";
195 0 warn "geresgid = ".join(',',getresuid())."\n";
196 }
197  
198 2 1; # Magic true value required at end of module
199 __END__
↑ Summary blib/lib/Unix/SavedIDs.pm »