blib/lib/Doit/User.pm

filter: Statementstmt 35.3% 53 / 150 Branchbran 14.0% 16 / 114 Conditioncond 10.5% 4 / 38 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 35.7% 5 / 14 Podpod 0.0% 0 / 5 totaltotal 23.3% 79 / 339 SCAR 77.3
CC
71
Cov
24%
user_account
2652.0
user_add_user_to_group
42.0
_get_user_groups
20.0
CRAP 2268.9
« lib/Doit.pm ↑ Summary blib/lib/Doit/Locale.pm »
1 # -*- perl -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2017 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # Mail: slaven@rezic.de
11 # WWW: http://www.rezic.de/eserte/
12 #
13  
14 package Doit::User;
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 our $VERSION = '0.032';
19  
20 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our @EXPORT_OK = qw(as_user);
22  
23 1 ▶use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 {
26 1 my(%uid_cache, %gid_cache, %homedir_cache);
27  
28 sub as_user (&$;@) {
29 7 ▶ my($code, $user, %opts) = @_;
Subroutine as_user: called CC=13 SCAR=26.0
Pod undocumented
30 7 ▶ my $cache = exists $opts{cache} ? delete $opts{cache} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'cache'} ? : 3 4 7
31 7 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 6 7
32  
33 6 my($uid, $gid, $homedir);
34 6 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 5 1 6
35 5 ($uid, $gid, $homedir) = ($uid_cache{$user}, $gid_cache{$user}, $homedir_cache{$user});
36 }
37 6 ▶ if (!defined $uid || !defined $gid || !defined $homedir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid and defined $gid and defined $homedir) 0 2 2
Condition66%4/6
defined $uid and defined $gid and defined $homedir
!ll&&!rl&&r
402
defined $uid and defined $gid
!ll&&!rl&&r
402
MC/DC33%1/3
defined $uid and defined $gid and defined $homedir
defined $uid defined $gid defined $homedir
Truth table50%2/4
A and B and C
A= defined $uidB= defined $gidC= defined $homedir
ABCresult
0XX0
10X0
1100
1111
38 4 ($uid, $gid, $homedir) = ((getpwnam $user)[2,3,7]);
39 4 ▶ if (!defined $uid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid) 1 3 4
40 1 error "Cannot get uid of user '$user'";
41 }
42 3 ($gid) = $gid =~ m{^(\d+)}; # only the first one
43 3 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 2 1 3
44 2 $uid_cache{$user} = $uid;
45 2 $gid_cache{$user} = $gid;
46 2 $homedir_cache{$user} = $homedir;
47 }
48 }
49  
50 # change first the gid, then the uid!
51 5 ▶ local $( = $gid; { my $errno = $!; if ($( != $gid) { error "Can't set real group id (wanted: $gid, is: $(): $errno" } }
Branch: 1 branch
BranchTrueFalseTotal
if ($( != $gid) 1 4 5
52 4 ▶ local $) = $gid; { my $errno = $!; if ($) != $gid) { error "Can't set effective group id (wanted: $gid, is: $)): $errno" } }
Branch: 1 branch
BranchTrueFalseTotal
if ($) != $gid) 0 4 4
53 4 ▶ local $< = $uid; { my $errno = $!; if ($< != $uid) { error "Can't set real user id (wanted: $uid, is: $<) : $errno" } }
Branch: 1 branch
BranchTrueFalseTotal
if ($< != $uid) 0 4 4
54 4 ▶ local $> = $uid; { my $errno = $!; if ($> != $uid) { error "Can't set effective user id (wanted: $uid, is: $>): $errno" } }
Branch: 1 branch
BranchTrueFalseTotal
if ($> != $uid) 0 4 4
55 4 local $ENV{HOME} = $homedir;
56 4 local $ENV{USER} = $user;
57 4 local $ENV{LOGNAME} = $user;
58  
59 4 $code->();
60 }
61 }
62  
63 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=1.2
Pod undocumented
64 0 ▶sub functions { qw(user_account user_add_user_to_group) }
Subroutine functions: not called CC=1 SCAR=6.9
Pod undocumented
65  
66 sub user_account {
67 0 ▶ my($self, %opts) = @_;
Subroutine user_account: not called CC=51 SCAR=78.8
Pod undocumented
68  
69 0 ▶ error "Only supported for linux and freebsd" if $^O !~ /^(linux|freebsd)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /^(linux|freebsd)$/ 0 0 0
70  
71 0 my $username = delete $opts{username};
72 0 ▶ if (!defined $username) { error "'username' is mandatory" }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username) 0 0 0
73 0 ▶ my $ensure = delete $opts{ensure} || 'present';
Condition0%0/2
delete $opts{'ensure'} || 'present'
l!l
00
MC/DC0%0/1
delete $opts{'ensure'} || 'present'
delete $opts{'ensure'}
Truth table0%0/2
A
A= delete $opts{'ensure'}
Aresult
11
00
74 0 my $uid = delete $opts{uid};
75 0 ▶ my @groups = @{ delete $opts{groups} || [] };
Branch: 1 branch
BranchTrueFalseTotal
delete $opts{"groups"} or [] 0 0 0
76 0 my $home = delete $opts{home};
77 0 my $shell = delete $opts{shell};
78 0 ▶ my @ssh_keys = @{ delete $opts{ssh_keys} || [] };
Branch: 1 branch
BranchTrueFalseTotal
delete $opts{"ssh_keys"} or [] 0 0 0
79 ## XXX maybe support some day (taken from Rex):
80 # expire - Date when the account will expire. Format: YYYY-MM-DD
81 # password - Cleartext password for the user.
82 # crypt_password - Crypted password for the user. Available on Linux, OpenBSD and NetBSD.
83 # system - Create a system user.
84 # create_home - If the home directory should be created. Valid parameters are TRUE, FALSE.
85 # comment
86 0 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
87  
88 0 my($got_username, $got_passwd, $got_uid, $got_gid, $got_quota,
89 $got_comment, $got_gcos, $got_home, $got_shell, $got_expire) = getpwnam($username);
90  
91 0 ▶ if ($ensure eq 'absent') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ensure eq 'absent') { } 0 0 0
elsif ($ensure ne 'present') { } 0 0 0
92 0 ▶ if (defined $got_username) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $got_username) 0 0 0
93 my $sys_userdel =
94 0 ▶ ( $^O eq 'linux' ? sub { $self->system('userdel', $username) } # XXX what about --remove?
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 ▶ : $^O eq 'freebsd' ? sub { $self->system('pw', 'userdel', $username) } # XXX what about -r?
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 ▶ : sub { error "userdel NYI for $^O" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'linux' ? : 0 0 0
$^O eq 'freebsd' ? : 0 0 0
98 0 $sys_userdel->();
99 }
100 } elsif ($ensure ne 'present') {
101 0 error "Valid values for 'ensure': 'absent', 'present' (got: '$ensure')\n";
102 } else {
103 0 my($cmd, @args);
104 0 ▶ if (defined $got_username) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $got_username) { } 0 0 0
105 0 $cmd = 'usermod';
106 } else {
107 0 $cmd = 'useradd';
108 }
109 0 ▶ if (defined $uid &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uid and defined $got_uid && $got_uid != $uid || !defined($got_uid)) 0 0 0
Condition0%0/9
defined $uid and defined $got_uid && $got_uid != $uid || !defined($got_uid)
!ll&&!rl&&r
000
defined $got_uid and $got_uid != $uid or not defined $got_uid
l!l&&r!l&&!r
000
defined $got_uid and $got_uid != $uid
!ll&&!rl&&r
000
MC/DC0%0/4
defined $uid and defined $got_uid && $got_uid != $uid || !defined($got_uid)
defined $uid defined $got_uid $got_uid != $uid not defined $got_uid
Truth table0%0/6
A and B and C or not(D)
A= defined $uidB= defined $got_uidC= $got_uid != $uidD= not defined $got_uid
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
110 (
111 (defined $got_uid && $got_uid != $uid)
112 || (!defined $got_uid)
113 )
114 ) {
115 0 ▶ push @args, ($^O eq 'linux' ? '--uid' : $^O eq 'freebsd' ? '-u' : error "NYI"), $uid;
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'linux' ? : 0 0 0
$^O eq 'freebsd' ? : 0 0 0
116 }
117 0 ▶ if ($cmd eq 'useradd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "useradd") 0 0 0
118 0 ▶ if ($^O eq 'linux') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'linux') { } 0 0 0
elsif ($^O eq 'freebsd') { } 0 0 0
119 0 push @args, '--user-group';
120 } elsif ($^O eq 'freebsd') {
121 # done automatically
122 }
123 }
124 ## XXX?
125 #if (defined $uid &&
126 # (
127 # (defined $got_gid && $got_gid != $uid)
128 # || (!defined $got_gid)
129 # )
130 # ) {
131 # push @args, '--gid', $uid; # XXX what if uid should be != gid?
132 #}
133 0 ▶ if (defined $home &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $home and defined $got_home && $got_home ne $home || !defined($got_home)) { } 0 0 0
elsif ($cmd eq 'useradd') { } 0 0 0
Condition0%0/9
defined $home and defined $got_home && $got_home ne $home || !defined($got_home)
!ll&&!rl&&r
000
defined $got_home and $got_home ne $home or not defined $got_home
l!l&&r!l&&!r
000
defined $got_home and $got_home ne $home
!ll&&!rl&&r
000
MC/DC0%0/4
defined $home and defined $got_home && $got_home ne $home || !defined($got_home)
defined $home defined $got_home $got_home ne $home not defined $got_home
Truth table0%0/6
A and B and C or not(D)
A= defined $homeB= defined $got_homeC= $got_home ne $homeD= not defined $got_home
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
134 (
135 (defined $got_home && $got_home ne $home)
136 || (!defined $got_home)
137 )
138 ) {
139 0 ▶ push @args, ($^O eq 'linux' ? ('--home', $home, ($cmd eq 'usermod' ? '--move-home' : '--create-home')) :
Branch: 3 branches
BranchTrueFalseTotal
$^O eq 'linux' ? : 0 0 0
$cmd eq 'usermod' ? : 0 0 0
$^O eq 'freebsd' ? : 0 0 0
140 $^O eq 'freebsd' ? ('-d', $home, '-m') :
141 error "NYI");
142 } elsif ($cmd eq 'useradd') {
143 0 ▶ push @args, ($^O eq 'linux' ? ($cmd eq 'usermod' ? '--move-home' : '--create-home') :
Branch: 3 branches
BranchTrueFalseTotal
$^O eq 'linux' ? : 0 0 0
$cmd eq 'usermod' ? : 0 0 0
$^O eq 'freebsd' ? : 0 0 0
144 $^O eq 'freebsd' ? ('-m') :
145 error "NYI");
146 }
147 0 ▶ if (defined $shell &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $shell and defined $got_shell && $got_shell ne $shell || !defined($got_shell)) 0 0 0
Condition0%0/9
defined $shell and defined $got_shell && $got_shell ne $shell || !defined($got_shell)
!ll&&!rl&&r
000
defined $got_shell and $got_shell ne $shell or not defined $got_shell
l!l&&r!l&&!r
000
defined $got_shell and $got_shell ne $shell
!ll&&!rl&&r
000
MC/DC0%0/4
defined $shell and defined $got_shell && $got_shell ne $shell || !defined($got_shell)
defined $shell defined $got_shell $got_shell ne $shell not defined $got_shell
Truth table0%0/6
A and B and C or not(D)
A= defined $shellB= defined $got_shellC= $got_shell ne $shellD= not defined $got_shell
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
148 (
149 (defined $got_shell && $got_shell ne $shell)
150 || (!defined $got_shell)
151 )
152 ) {
153 0 ▶ push @args, ($^O eq 'linux' ? '--shell' :
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'linux' ? : 0 0 0
$^O eq 'freebsd' ? : 0 0 0
154 $^O eq 'freebsd' ? '-s' :
155 error "NYI"), $shell;
156 }
157 0 ▶ if (@groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@groups) 0 0 0
158 0 my @got_groups = sort _get_user_groups($username);
159 0 my @want_groups = sort @groups;
160 0 ▶ if ("@want_groups" ne "@got_groups") {
Branch: 1 branch
BranchTrueFalseTotal
if (join($", @want_groups) ne join($", @got_groups)) 0 0 0
161 0 ▶ push @args, ($^O eq 'linux' ? '--groups' :
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'linux' ? : 0 0 0
$^O eq 'freebsd' ? : 0 0 0
162 $^O eq 'freebsd' ? '-G' :
163 error "NYI"), join(",", @groups);
164 }
165 }
166 0 ▶ if ($cmd eq 'useradd' || @args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "useradd" or @args) 0 0 0
Condition0%0/3
$cmd eq "useradd" or @args
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd eq "useradd" or @args
$cmd eq "useradd" @args
Truth table0%0/3
A or B
A= $cmd eq "useradd"B= @args
ABresult
1X1
011
000
167 0 local $ENV{PATH} = "/usr/sbin:$ENV{PATH}";
168 0 ▶ if ($^O eq 'linux') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'linux') { } 0 0 0
elsif ($^O eq 'freebsd') { } 0 0 0
169 0 $self->system($cmd, @args, $username);
170 } elsif ($^O eq 'freebsd') {
171 0 $self->system('pw', $cmd, @args, '-n', $username);
172 }
173 }
174  
175 0 ▶ if (!$self->is_dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_dry_run) { } 0 0 0
176 0 ($got_username, $got_passwd, $got_uid, $got_gid, $got_quota,
177 $got_comment, $got_gcos, $got_home, $got_shell, $got_expire) = getpwnam($username);
178 0 ▶ if (!defined $got_username) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $got_username) 0 0 0
179 0 error "Something went wrong: $cmd did not fail, but user '$username' does not exist";
180 }
181 } else {
182 0 ▶ if (defined $home) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $home) { } 0 0 0
183 0 $got_home = $home;
184 } else {
185 0 $got_home = "/home/$username";
186 }
187 }
188  
189 0 ▶ if (@ssh_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ssh_keys) 0 0 0
190 0 $self->mkdir("$got_home/.ssh");
191 0 $self->chmod(0700, "$got_home/.ssh");
192 0 $self->chown($username, $username, "$got_home/.ssh");
193 0 $self->create_file_if_nonexisting("$got_home/.ssh/authorized_keys");
194 0 $self->chmod(0600, "$got_home/.ssh/authorized_keys");
195 0 $self->chown($username, $username, "$got_home/.ssh/authorized_keys");
196 $self->change_file("$got_home/.ssh/authorized_keys",
197 0 (map { +{ add_if_missing => $_ } } @ssh_keys),
198 );
199 }
200 }
201 }
202  
203 sub user_add_user_to_group {
204 0 ▶ my($self, %opts) = @_;
Subroutine user_add_user_to_group: not called CC=6 SCAR=37.4
Pod undocumented
205 0 my $username = delete $opts{username};
206 0 ▶ if (!defined $username) { error "username is mandatory" }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username) 0 0 0
207 0 my $group = delete $opts{group};
208 0 ▶ if (!defined $group) { error "group is mandatory" }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $group) 0 0 0
209 0 my %user_groups = map{($_,1)} _get_user_groups($username);
210 0 my $changes = 0;
211 0 ▶ if (!$user_groups{$group}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_groups{$group}) 0 0 0
212 0 ▶ if ($^O eq 'linux') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'linux') { } 0 0 0
elsif ($^O eq 'freebsd') { } 0 0 0
213 0 $self->system('usermod', '--append', '--groups', $group, $username);
214 0 $changes = 1;
215 } elsif ($^O eq 'freebsd') {
216 0 $self->system('pw', 'groupmod', '-m', $username, '-n', $group);
217 0 $changes = 1;
218 } else {
219 0 error "user_add_user_to_group NYI for $^O";
220 }
221 }
222 0 $changes;
223 }
224  
225 sub _get_user_groups {
226 0 ▶ my $username = shift;
Subroutine _get_user_groups: not called CC=4 SCAR=30.0
227 0 my @groups;
228 0 require POSIX;
229 0 require List::Util;
230 0 ▶ while (my($gname,undef,undef,$members) = getgrent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($gname, undef, undef, $members) = getgrent) 0 0 0
231 0 ▶ next if $gname eq $username; # don't deal with primary groups
Branch: 1 branch
BranchTrueFalseTotal
if $gname eq $username 0 0 0
232 0 ▶ if (List::Util::first(sub { $_ eq $username }, split /\s+/, $members)) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&List::Util::first(sub { $_ eq $username; } , split(/\s+/, $members, 0))) 0 0 0
233 0 push @groups, $gname;
234 }
235 }
236 0 endgrent;
237 0 @groups;
238 }
239  
240 1 1;
241  
242 __END__
« lib/Doit.pm ↑ Summary blib/lib/Doit/Locale.pm »