[175] | 1 | package LATMOS::Accounts::Utils; |
---|
| 2 | use 5.010000; |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use Exporter (); |
---|
| 6 | use vars qw(@ISA @EXPORT_OK @EXPORT); |
---|
| 7 | use utf8; |
---|
[261] | 8 | use LATMOS::Accounts::Log; |
---|
[334] | 9 | use File::Temp qw(tempfile); |
---|
[175] | 10 | |
---|
| 11 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
| 12 | |
---|
[1023] | 13 | =head1 NAME |
---|
| 14 | |
---|
| 15 | LATMOS::Accounts::Utils - Utils functions |
---|
| 16 | |
---|
| 17 | =head1 FUNCTIONS |
---|
| 18 | |
---|
| 19 | =cut |
---|
| 20 | |
---|
[175] | 21 | @ISA = qw(Exporter); |
---|
[861] | 22 | @EXPORT = qw(to_ascii exec_command switch_user run_via_sudo); |
---|
| 23 | @EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo); |
---|
[175] | 24 | |
---|
[1023] | 25 | =head2 to_ascii($text) |
---|
| 26 | |
---|
| 27 | Replace in C<$text> non ascii caracters from iso-8859-15 table to ascii |
---|
| 28 | equivalent caracter. |
---|
| 29 | |
---|
| 30 | =cut |
---|
| 31 | |
---|
[175] | 32 | sub to_ascii { |
---|
| 33 | my ($text) = @_; |
---|
[203] | 34 | return $text unless(defined($text)); |
---|
[175] | 35 | utf8::decode($text) unless(utf8::is_utf8($text)); |
---|
[377] | 36 | $text =~ s/Å/oe/g; |
---|
| 37 | $text =~ s/Ê/ae/g; |
---|
[861] | 38 | $text =~ tr {uà âÀÃÃÃçéÚêëÃÃÃÃïîÃÃÞöÎÃÃÌûÃÃÄ} |
---|
| 39 | {uaaaAAAceeeeEEEEiiIIoooOOuuUUc}; |
---|
[660] | 40 | $text =~ s/([^[:ascii:]])/_/g; |
---|
[175] | 41 | $text |
---|
| 42 | } |
---|
| 43 | |
---|
[1023] | 44 | =head2 exec_command($command, $env) |
---|
| 45 | |
---|
| 46 | Execute C<$command> and redirect output to log system. |
---|
| 47 | |
---|
| 48 | C<$env> is a hashref containing environment variable to set, all variables are |
---|
| 49 | prefixed by 'LA_'. |
---|
| 50 | |
---|
| 51 | =cut |
---|
| 52 | |
---|
[261] | 53 | sub exec_command { |
---|
| 54 | my ($command, $env) = @_; |
---|
[861] | 55 | my $rout = undef; |
---|
| 56 | $rout = \$_[2] if(@_ > 2); |
---|
[261] | 57 | |
---|
| 58 | my @exec = ref $command |
---|
| 59 | ? (@$command) |
---|
| 60 | : ($command); |
---|
[267] | 61 | la_log(LA_DEBUG, 'running command `%s\'', join(' ', @exec)); |
---|
[261] | 62 | |
---|
[861] | 63 | pipe(my $rh, my $wh); |
---|
[261] | 64 | my $pid = fork; |
---|
| 65 | if (!defined($pid)) { |
---|
[267] | 66 | la_log(LA_ERR, "Can't launch script: cannot fork"); |
---|
[261] | 67 | } elsif ($pid) { |
---|
| 68 | # Father |
---|
[861] | 69 | close($wh); |
---|
| 70 | my $header; |
---|
| 71 | while (<$rh>) { |
---|
| 72 | if ($rout) { |
---|
| 73 | $$rout .= $_; |
---|
| 74 | } else { |
---|
| 75 | chomp; |
---|
| 76 | if (!$header) { |
---|
| 77 | $header = 1; |
---|
| 78 | la_log(LA_NOTICE, "exec `%s'", join(' ', @exec)); |
---|
| 79 | } |
---|
| 80 | la_log(LA_NOTICE, "output: %s", $_); |
---|
| 81 | } |
---|
| 82 | } |
---|
[261] | 83 | waitpid($pid, 0); |
---|
| 84 | if (my $exitstatus = $?) { |
---|
| 85 | la_log(LA_ERR, 'command %s exit with status %d', |
---|
| 86 | join(' ', @exec), $exitstatus); |
---|
[818] | 87 | return; |
---|
| 88 | } else { |
---|
| 89 | return 1; |
---|
[261] | 90 | } |
---|
| 91 | } else { |
---|
| 92 | # Child |
---|
[861] | 93 | close($rh); |
---|
| 94 | ( $ENV{LA_MODULE} ) = caller(); |
---|
[261] | 95 | foreach (keys %{ $env || {} }) { |
---|
| 96 | $ENV{"LA_$_"} = $env->{$_}; |
---|
| 97 | } |
---|
[861] | 98 | open(STDOUT, ">&=" . fileno($wh)); |
---|
| 99 | open(STDERR, ">&=" . fileno($wh)); |
---|
[261] | 100 | exec(@exec); |
---|
| 101 | exit($!); |
---|
| 102 | } |
---|
| 103 | 1 |
---|
| 104 | } |
---|
| 105 | |
---|
[1023] | 106 | =head2 parse_obj_file($handle) |
---|
| 107 | |
---|
| 108 | Read file content from C<$handle> and return hash containing parsed attributes |
---|
| 109 | |
---|
| 110 | =cut |
---|
| 111 | |
---|
[334] | 112 | sub parse_obj_file { |
---|
| 113 | my ($handle) = @_; |
---|
[261] | 114 | |
---|
[334] | 115 | my %attributes; |
---|
| 116 | while (my $line = <$handle>) { |
---|
| 117 | $line =~ /^#/ and next; |
---|
| 118 | chomp($line); |
---|
[399] | 119 | my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or |
---|
| 120 | die "Malformed input file\n"; |
---|
[382] | 121 | $value =~ s/\s*$//; |
---|
[400] | 122 | $value =~ s/\\n/\n/g; |
---|
[334] | 123 | if ($attributes{$attr}) { |
---|
| 124 | if (ref $attributes{$attr}) { |
---|
| 125 | push(@{ $attributes{$attr} }, $value); |
---|
| 126 | } else { |
---|
| 127 | my $temp = $attributes{$attr}; |
---|
| 128 | $attributes{$attr} = [ $temp, $value ]; |
---|
| 129 | } |
---|
| 130 | } else { |
---|
[861] | 131 | $attributes{$attr} = $value eq '' ? undef : $value; |
---|
| 132 | # Don't remember why this is here |
---|
| 133 | #$attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1; |
---|
[334] | 134 | } |
---|
| 135 | } |
---|
| 136 | %attributes |
---|
| 137 | } |
---|
| 138 | |
---|
[1023] | 139 | =head2 dump_read_temp_file($writecb, $readcb) |
---|
| 140 | |
---|
| 141 | Create a temporary file, call C<$writecb()> function, run editor and if file get |
---|
| 142 | modified call C<$readcb>. |
---|
| 143 | |
---|
| 144 | =cut |
---|
| 145 | |
---|
[334] | 146 | sub dump_read_temp_file { |
---|
| 147 | my ($writecb, $readcb) = @_; |
---|
| 148 | |
---|
| 149 | my ($fh, $filename) = tempfile(CLEANUP => 0); |
---|
| 150 | $writecb->($fh) or return; |
---|
| 151 | $fh = undef; # closing file |
---|
| 152 | my $res; |
---|
| 153 | |
---|
| 154 | my @stat = stat($filename); |
---|
| 155 | while (1) { |
---|
| 156 | my $cmd = ($ENV{EDITOR} || 'vi') . " $filename"; |
---|
| 157 | warn "Running $cmd\n"; |
---|
| 158 | if (system($cmd) == -1 ) { |
---|
| 159 | warn "Cannot run editor $!\n"; |
---|
| 160 | last; |
---|
| 161 | } |
---|
| 162 | if ((stat($filename))[9] == $stat[9]) { |
---|
| 163 | warn "No change existing\n"; |
---|
| 164 | last; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | open($fh, '<', $filename) or return; |
---|
| 168 | $res = $readcb->($fh); |
---|
| 169 | $fh = undef; # closing again |
---|
| 170 | $res < 2 and last; |
---|
| 171 | } |
---|
| 172 | unlink($filename); |
---|
| 173 | $res; |
---|
| 174 | } |
---|
[400] | 175 | |
---|
[1023] | 176 | =head2 check_oid_validity($name) |
---|
| 177 | |
---|
| 178 | Check C<$name> is suitable to be used as object identifier. Return the error |
---|
| 179 | text, undef if no error. |
---|
| 180 | |
---|
| 181 | =cut |
---|
| 182 | |
---|
[674] | 183 | sub check_oid_validity { |
---|
| 184 | my ($name) = @_; |
---|
| 185 | return "leadind space" if ($name =~ /^\s/); |
---|
| 186 | return "trailing space" if ($name =~ /\s$/); |
---|
| 187 | return "containing space" if ($name =~ /\s/); |
---|
| 188 | |
---|
| 189 | return; |
---|
| 190 | } |
---|
| 191 | |
---|
[1023] | 192 | =head2 check_ug_validity($name) |
---|
| 193 | |
---|
| 194 | Check C<$name> is suitable to used as user or group identifier. |
---|
| 195 | |
---|
| 196 | =cut |
---|
| 197 | |
---|
[674] | 198 | sub check_ug_validity { |
---|
| 199 | my ($name) = @_; |
---|
[959] | 200 | return "Empty name is not a valid name !" |
---|
| 201 | if (!$name); |
---|
[674] | 202 | return "first caractere must be a-z" |
---|
| 203 | if ($name !~ /^[a-z]/); |
---|
| 204 | return "must contain only a-z,0-9" |
---|
[861] | 205 | if ($name !~ /^[a-z,0-9,_,-]+$/); |
---|
[674] | 206 | |
---|
| 207 | return check_oid_validity($name); |
---|
| 208 | } |
---|
| 209 | |
---|
[1023] | 210 | =head2 switch_user($runas) |
---|
| 211 | |
---|
| 212 | Switch effective id of the process to user named C<$runas> |
---|
| 213 | |
---|
| 214 | =cut |
---|
| 215 | |
---|
[861] | 216 | sub switch_user { |
---|
| 217 | my ($runas) = @_; |
---|
| 218 | |
---|
| 219 | if ($< == 0 || $> == 0) { |
---|
| 220 | my @info = getpwnam($runas) or do { |
---|
| 221 | warn "Can find user $runas"; |
---|
| 222 | return; |
---|
| 223 | }; |
---|
| 224 | $> = $info[3]; |
---|
| 225 | return; |
---|
| 226 | } else { |
---|
| 227 | warn "we are not root"; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
[1023] | 231 | =head2 run_via_sudo($runas) |
---|
| 232 | |
---|
| 233 | Rerun current programme as C<$runas> user using sudo |
---|
| 234 | |
---|
| 235 | =cut |
---|
| 236 | |
---|
[861] | 237 | sub run_via_sudo { |
---|
| 238 | my ($runas) = @_; |
---|
| 239 | |
---|
| 240 | my @info = getpwnam($runas) or do { |
---|
| 241 | warn "Can find user $runas"; |
---|
| 242 | return; |
---|
| 243 | }; |
---|
| 244 | if ($< != $info[3]) { |
---|
| 245 | exec('sudo', '-u', $runas, $0, @ARGV) or "Can run $!"; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
[175] | 249 | 1; |
---|
[1023] | 250 | |
---|
| 251 | __END__ |
---|
| 252 | |
---|
| 253 | =head1 SEE ALSO |
---|
| 254 | |
---|
| 255 | L<sudo> |
---|
| 256 | |
---|
| 257 | =head1 AUTHOR |
---|
| 258 | |
---|
| 259 | Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt> |
---|
| 260 | |
---|
| 261 | =head1 COPYRIGHT AND LICENSE |
---|
| 262 | |
---|
| 263 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
| 264 | |
---|
| 265 | This library is free software; you can redistribute it and/or modify |
---|
| 266 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 267 | at your option, any later version of Perl 5 you may have available. |
---|
| 268 | |
---|
| 269 | =cut |
---|