| 1 | # | ||||||||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2010-2020 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | Chj::xhome | ||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | Get the user's home directory in a safe manner. | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =over 4 | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | =item xHOME () | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | Just the $HOME env var, dieing if not set, and also checked against a | ||||||||||||||||||||||||||||||||||
| 25 | couple assertments. | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | In taint mode, is tainted. | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | =item xeffectiveuserhome () | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | Just the getpwuid setting. Throws unimplemented exception on Windows | ||||||||||||||||||||||||||||||||||
| 32 | (Raspberry, not Cygwin Perl). | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | Is not tainted. | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | =item xsafehome () | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | Always take xeffectiveuserhome (unless on Windows, in which case this | ||||||||||||||||||||||||||||||||||
| 39 | is currently the same as xhome), but is asserting that HOME is the | ||||||||||||||||||||||||||||||||||
| 40 | same if set. | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | Is untainted except on Windows where it's tainted. | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | =item xhome () | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | Tries $ENV{HOME} then glob "~" if exists (with assertments), otherwise | ||||||||||||||||||||||||||||||||||
| 47 | xeffectiveuserhome. | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | In taint mode, can be (and usually is) tainted. | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | =back | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | =head1 NOTE | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||||||||
| 56 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | package Chj::xhome; | ||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | use Exporter "import"; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 2 | our @EXPORT = qw(xhome); | |||||||||||||||||||||||||||||||||
| 67 | 2 | our @EXPORT_OK = qw(xHOME | |||||||||||||||||||||||||||||||||
| 68 | xeffectiveuserhome | ||||||||||||||||||||||||||||||||||
| 69 | xsafehome); | ||||||||||||||||||||||||||||||||||
| 70 | 2 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | use FP::Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | use FP::Docstring; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | # use File::HomeDir qw(home); | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | # But File::HomeDir is not installed with either Cygwin or Strawberry | ||||||||||||||||||||||||||||||||||
| 78 | # Perl. Also, HomeDir's `home` returns undef for non-existing paths. | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub xcheck_home { | ||||||||||||||||||||||||||||||||
|
Subroutine
xcheck_home: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 81 | 0 | my ($home) = @_; | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | length($home) or die "environment variable HOME is the empty string"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $home or die "environment variable HOME is false"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $home =~ m|^[a-z]+:|i # XX correct letter syntax? | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | or die | ||||||||||||||||||||||||||||||||||
| 87 | "environment variable HOME does not start with a drive designator: '$home'"; | ||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $home =~ m|^/| | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | or die | ||||||||||||||||||||||||||||||||||
| 91 | "environment variable HOME does not start with a slash: '$home'"; | ||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub xHOME { | ||||||||||||||||||||||||||||||||
|
Subroutine
xHOME: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | @_ == 0 or fp_croak_arity 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | defined(my $home = $ENV{HOME}) | ||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | or die "environment variable HOME is not set"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 99 | 0 | xcheck_home $home; | |||||||||||||||||||||||||||||||||
| 100 | 0 | $home | |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub xeffectiveuserhome { | ||||||||||||||||||||||||||||||||
|
Subroutine
xeffectiveuserhome: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | @_ == 0 or fp_croak_arity 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 0 | __ "is untainted"; | |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | # (Don't bother about caching, premature opt & dangerous.) | ||||||||||||||||||||||||||||||||||
| 109 | 0 | my $uid = $>; | |||||||||||||||||||||||||||||||||
| 110 | my ( | ||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $name, $passwd, $_uid, $gid, $quota, | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 112 | $comment, $gcos, $dir, $shell, $expire | ||||||||||||||||||||||||||||||||||
| 113 | ) | ||||||||||||||||||||||||||||||||||
| 114 | = getpwuid $uid | ||||||||||||||||||||||||||||||||||
| 115 | or die "unknown user for uid $uid"; | ||||||||||||||||||||||||||||||||||
| 116 | 0 | $dir | |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub xsafehome { | ||||||||||||||||||||||||||||||||
|
Subroutine
xsafehome: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | @_ == 0 or fp_croak_arity 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | 0 | __ "is untainted except on Windows where it's tainted"; | |||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | # XX or how to look it up on Windows again? If implemented, update pod. | ||||||||||||||||||||||||||||||||||
| 125 | 0 | xhome() | |||||||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||||||||
| 127 | 0 | my $effectiveuserhome = xeffectiveuserhome; | |||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if (my $e = $ENV{HOME}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $e eq $effectiveuserhome | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 130 | or die "HOME environment variable is set to something other " | ||||||||||||||||||||||||||||||||||
| 131 | . "than the effective user home: '$e' vs. '$effectiveuserhome'"; | ||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | $effectiveuserhome | ||||||||||||||||||||||||||||||||||
| 134 | 0 | } | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 2 | our $warned = 0; | |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub xchecked_home { | ||||||||||||||||||||||||||||||||
|
Subroutine
xchecked_home: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 141 | 0 | my ($home, $what) = @_; | |||||||||||||||||||||||||||||||||
| 142 | 0 | xcheck_home $home; | |||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if (-d $home) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 144 | 0 | $home | |||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | warn "$what: dir '$home' does not exist, falling back to getpwuid" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | unless $warned++; | ||||||||||||||||||||||||||||||||||
| 148 | undef | ||||||||||||||||||||||||||||||||||
| 149 | 0 | } | |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub maybe_HOME { | ||||||||||||||||||||||||||||||||
|
Subroutine
maybe_HOME: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 153 | 0 | __ "is tainted"; | |||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if (my $home = $ENV{HOME}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 155 | 0 | xchecked_home $home, '$ENV{HOME}' | |||||||||||||||||||||||||||||||||
| 156 | } else { | ||||||||||||||||||||||||||||||||||
| 157 | undef | ||||||||||||||||||||||||||||||||||
| 158 | 0 | } | |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub maybe_globhome { | ||||||||||||||||||||||||||||||||
|
Subroutine
maybe_globhome: not called CC=2 SCAR=12.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 162 | 0 | __ "is tainted"; | |||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my ($home) = glob "~"; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if (defined $home) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 165 | 0 | xchecked_home $home, "glob '~'"; | |||||||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||||||||
| 167 | undef | ||||||||||||||||||||||||||||||||||
| 168 | 0 | } | |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub xhome { | ||||||||||||||||||||||||||||||||
|
Subroutine
xhome: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | @_ == 0 or fp_croak_arity 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | maybe_HOME() // maybe_globhome() // xeffectiveuserhome() | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
(maybe_HOME() // maybe_globhome()) // xeffectiveuserhome()
maybe_HOME() // maybe_globhome()
MC/DC0%0/3
(maybe_HOME() // maybe_globhome()) // xeffectiveuserhome()
maybe_HOME()
maybe_globhome()
xeffectiveuserhome()
Truth table0%0/4
A // B // C
A= maybe_HOME()B= maybe_globhome()C= xeffectiveuserhome()
| |||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | 2 | 1 | |||||||||||||||||||||||||||||||||