[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); |
---|
[1151] | 10 | use Crypt::Cracklib; |
---|
[175] | 11 | |
---|
| 12 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
| 13 | |
---|
[1023] | 14 | =head1 NAME |
---|
| 15 | |
---|
| 16 | LATMOS::Accounts::Utils - Utils functions |
---|
| 17 | |
---|
| 18 | =head1 FUNCTIONS |
---|
| 19 | |
---|
| 20 | =cut |
---|
| 21 | |
---|
[175] | 22 | @ISA = qw(Exporter); |
---|
[1378] | 23 | @EXPORT = qw(to_ascii exec_command switch_user run_via_sudo yesno); |
---|
| 24 | @EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo yesno); |
---|
[175] | 25 | |
---|
[1023] | 26 | =head2 to_ascii($text) |
---|
| 27 | |
---|
| 28 | Replace in C<$text> non ascii caracters from iso-8859-15 table to ascii |
---|
| 29 | equivalent caracter. |
---|
| 30 | |
---|
| 31 | =cut |
---|
| 32 | |
---|
[175] | 33 | sub to_ascii { |
---|
| 34 | my ($text) = @_; |
---|
[203] | 35 | return $text unless(defined($text)); |
---|
[175] | 36 | utf8::decode($text) unless(utf8::is_utf8($text)); |
---|
[377] | 37 | $text =~ s/Å/oe/g; |
---|
| 38 | $text =~ s/Ê/ae/g; |
---|
[861] | 39 | $text =~ tr {uà âÀÃÃÃçéÚêëÃÃÃÃïîÃÃÞöÎÃÃÌûÃÃÄ} |
---|
| 40 | {uaaaAAAceeeeEEEEiiIIoooOOuuUUc}; |
---|
[660] | 41 | $text =~ s/([^[:ascii:]])/_/g; |
---|
[175] | 42 | $text |
---|
| 43 | } |
---|
| 44 | |
---|
[1023] | 45 | =head2 exec_command($command, $env) |
---|
| 46 | |
---|
| 47 | Execute C<$command> and redirect output to log system. |
---|
| 48 | |
---|
| 49 | C<$env> is a hashref containing environment variable to set, all variables are |
---|
| 50 | prefixed by 'LA_'. |
---|
| 51 | |
---|
| 52 | =cut |
---|
| 53 | |
---|
[261] | 54 | sub exec_command { |
---|
| 55 | my ($command, $env) = @_; |
---|
[861] | 56 | my $rout = undef; |
---|
| 57 | $rout = \$_[2] if(@_ > 2); |
---|
[261] | 58 | |
---|
| 59 | my @exec = ref $command |
---|
| 60 | ? (@$command) |
---|
| 61 | : ($command); |
---|
[267] | 62 | la_log(LA_DEBUG, 'running command `%s\'', join(' ', @exec)); |
---|
[261] | 63 | |
---|
[861] | 64 | pipe(my $rh, my $wh); |
---|
[261] | 65 | my $pid = fork; |
---|
| 66 | if (!defined($pid)) { |
---|
[267] | 67 | la_log(LA_ERR, "Can't launch script: cannot fork"); |
---|
[261] | 68 | } elsif ($pid) { |
---|
| 69 | # Father |
---|
[861] | 70 | close($wh); |
---|
| 71 | my $header; |
---|
| 72 | while (<$rh>) { |
---|
| 73 | if ($rout) { |
---|
| 74 | $$rout .= $_; |
---|
| 75 | } else { |
---|
| 76 | chomp; |
---|
| 77 | if (!$header) { |
---|
| 78 | $header = 1; |
---|
| 79 | la_log(LA_NOTICE, "exec `%s'", join(' ', @exec)); |
---|
| 80 | } |
---|
| 81 | la_log(LA_NOTICE, "output: %s", $_); |
---|
| 82 | } |
---|
| 83 | } |
---|
[261] | 84 | waitpid($pid, 0); |
---|
| 85 | if (my $exitstatus = $?) { |
---|
| 86 | la_log(LA_ERR, 'command %s exit with status %d', |
---|
| 87 | join(' ', @exec), $exitstatus); |
---|
[818] | 88 | return; |
---|
| 89 | } else { |
---|
| 90 | return 1; |
---|
[261] | 91 | } |
---|
| 92 | } else { |
---|
| 93 | # Child |
---|
[861] | 94 | close($rh); |
---|
| 95 | ( $ENV{LA_MODULE} ) = caller(); |
---|
[261] | 96 | foreach (keys %{ $env || {} }) { |
---|
| 97 | $ENV{"LA_$_"} = $env->{$_}; |
---|
| 98 | } |
---|
[861] | 99 | open(STDOUT, ">&=" . fileno($wh)); |
---|
| 100 | open(STDERR, ">&=" . fileno($wh)); |
---|
[261] | 101 | exec(@exec); |
---|
| 102 | exit($!); |
---|
| 103 | } |
---|
| 104 | 1 |
---|
| 105 | } |
---|
| 106 | |
---|
[1023] | 107 | =head2 parse_obj_file($handle) |
---|
| 108 | |
---|
| 109 | Read file content from C<$handle> and return hash containing parsed attributes |
---|
| 110 | |
---|
| 111 | =cut |
---|
| 112 | |
---|
[334] | 113 | sub parse_obj_file { |
---|
| 114 | my ($handle) = @_; |
---|
[261] | 115 | |
---|
[334] | 116 | my %attributes; |
---|
| 117 | while (my $line = <$handle>) { |
---|
| 118 | $line =~ /^#/ and next; |
---|
| 119 | chomp($line); |
---|
[399] | 120 | my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or |
---|
| 121 | die "Malformed input file\n"; |
---|
[382] | 122 | $value =~ s/\s*$//; |
---|
[400] | 123 | $value =~ s/\\n/\n/g; |
---|
[334] | 124 | if ($attributes{$attr}) { |
---|
| 125 | if (ref $attributes{$attr}) { |
---|
| 126 | push(@{ $attributes{$attr} }, $value); |
---|
| 127 | } else { |
---|
| 128 | my $temp = $attributes{$attr}; |
---|
| 129 | $attributes{$attr} = [ $temp, $value ]; |
---|
| 130 | } |
---|
| 131 | } else { |
---|
[861] | 132 | $attributes{$attr} = $value eq '' ? undef : $value; |
---|
| 133 | # Don't remember why this is here |
---|
| 134 | #$attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1; |
---|
[334] | 135 | } |
---|
| 136 | } |
---|
| 137 | %attributes |
---|
| 138 | } |
---|
| 139 | |
---|
[1023] | 140 | =head2 dump_read_temp_file($writecb, $readcb) |
---|
| 141 | |
---|
| 142 | Create a temporary file, call C<$writecb()> function, run editor and if file get |
---|
| 143 | modified call C<$readcb>. |
---|
| 144 | |
---|
| 145 | =cut |
---|
| 146 | |
---|
[334] | 147 | sub dump_read_temp_file { |
---|
| 148 | my ($writecb, $readcb) = @_; |
---|
| 149 | |
---|
| 150 | my ($fh, $filename) = tempfile(CLEANUP => 0); |
---|
| 151 | $writecb->($fh) or return; |
---|
| 152 | $fh = undef; # closing file |
---|
| 153 | my $res; |
---|
| 154 | |
---|
| 155 | my @stat = stat($filename); |
---|
| 156 | while (1) { |
---|
| 157 | my $cmd = ($ENV{EDITOR} || 'vi') . " $filename"; |
---|
| 158 | warn "Running $cmd\n"; |
---|
| 159 | if (system($cmd) == -1 ) { |
---|
| 160 | warn "Cannot run editor $!\n"; |
---|
| 161 | last; |
---|
| 162 | } |
---|
| 163 | if ((stat($filename))[9] == $stat[9]) { |
---|
| 164 | warn "No change existing\n"; |
---|
| 165 | last; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | open($fh, '<', $filename) or return; |
---|
| 169 | $res = $readcb->($fh); |
---|
| 170 | $fh = undef; # closing again |
---|
| 171 | $res < 2 and last; |
---|
| 172 | } |
---|
| 173 | unlink($filename); |
---|
| 174 | $res; |
---|
| 175 | } |
---|
[400] | 176 | |
---|
[1023] | 177 | =head2 check_oid_validity($name) |
---|
| 178 | |
---|
| 179 | Check C<$name> is suitable to be used as object identifier. Return the error |
---|
| 180 | text, undef if no error. |
---|
| 181 | |
---|
| 182 | =cut |
---|
| 183 | |
---|
[674] | 184 | sub check_oid_validity { |
---|
| 185 | my ($name) = @_; |
---|
| 186 | return "leadind space" if ($name =~ /^\s/); |
---|
| 187 | return "trailing space" if ($name =~ /\s$/); |
---|
| 188 | return "containing space" if ($name =~ /\s/); |
---|
| 189 | |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | |
---|
[1023] | 193 | =head2 check_ug_validity($name) |
---|
| 194 | |
---|
| 195 | Check C<$name> is suitable to used as user or group identifier. |
---|
| 196 | |
---|
| 197 | =cut |
---|
| 198 | |
---|
[674] | 199 | sub check_ug_validity { |
---|
| 200 | my ($name) = @_; |
---|
[959] | 201 | return "Empty name is not a valid name !" |
---|
| 202 | if (!$name); |
---|
[674] | 203 | return "first caractere must be a-z" |
---|
| 204 | if ($name !~ /^[a-z]/); |
---|
| 205 | return "must contain only a-z,0-9" |
---|
[1492] | 206 | if ($name !~ /^[a-z,0-9,_,\-,\.]+$/); |
---|
[674] | 207 | |
---|
| 208 | return check_oid_validity($name); |
---|
| 209 | } |
---|
| 210 | |
---|
[1023] | 211 | =head2 switch_user($runas) |
---|
| 212 | |
---|
| 213 | Switch effective id of the process to user named C<$runas> |
---|
| 214 | |
---|
| 215 | =cut |
---|
| 216 | |
---|
[861] | 217 | sub switch_user { |
---|
| 218 | my ($runas) = @_; |
---|
| 219 | |
---|
| 220 | if ($< == 0 || $> == 0) { |
---|
| 221 | my @info = getpwnam($runas) or do { |
---|
| 222 | warn "Can find user $runas"; |
---|
| 223 | return; |
---|
| 224 | }; |
---|
| 225 | $> = $info[3]; |
---|
| 226 | return; |
---|
| 227 | } else { |
---|
| 228 | warn "we are not root"; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
[1023] | 232 | =head2 run_via_sudo($runas) |
---|
| 233 | |
---|
| 234 | Rerun current programme as C<$runas> user using sudo |
---|
| 235 | |
---|
| 236 | =cut |
---|
| 237 | |
---|
[861] | 238 | sub run_via_sudo { |
---|
| 239 | my ($runas) = @_; |
---|
| 240 | |
---|
| 241 | my @info = getpwnam($runas) or do { |
---|
| 242 | warn "Can find user $runas"; |
---|
| 243 | return; |
---|
| 244 | }; |
---|
| 245 | if ($< != $info[3]) { |
---|
| 246 | exec('sudo', '-u', $runas, $0, @ARGV) or "Can run $!"; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
[1378] | 250 | =head2 genpassword(%options) |
---|
[1151] | 251 | |
---|
[1378] | 252 | Generate a random password, options are: |
---|
| 253 | |
---|
[1379] | 254 | =over 4 |
---|
[1378] | 255 | |
---|
| 256 | =item length |
---|
| 257 | |
---|
| 258 | The minimum password length (default is 6) |
---|
| 259 | |
---|
| 260 | =item nonalpha |
---|
| 261 | |
---|
| 262 | Include non alpha-numeric caracters |
---|
| 263 | |
---|
| 264 | =item syllables |
---|
| 265 | |
---|
| 266 | Use a set of syllables instead letter |
---|
| 267 | |
---|
| 268 | =back |
---|
| 269 | |
---|
[1151] | 270 | =cut |
---|
| 271 | |
---|
| 272 | sub genpassword { |
---|
| 273 | my (%options) = @_; |
---|
| 274 | my @non_alpha = (qw$; : / ! ( ) [ ] { } + = @ - " ' % * & . ? < >$, ',', '$'); |
---|
| 275 | my @letters = ('a' .. 'z', 'A' .. 'Z', 0 .. 9); |
---|
| 276 | |
---|
| 277 | my @consonants = qw(b d f g j k l m n r s t v x z ch); |
---|
| 278 | my @vowels = qw(a e i o u ou oi io ia iu); |
---|
| 279 | |
---|
[1770] | 280 | $options{length} ||= 8 + int(rand(3)); |
---|
[1151] | 281 | |
---|
| 282 | while (1) { |
---|
| 283 | if ($options{syllables}) { |
---|
| 284 | $options{length} = int($options{length} / 2); |
---|
| 285 | $options{length} = 3 if ($options{length} < 3); |
---|
| 286 | } else { |
---|
| 287 | $options{length} = 6 if ($options{length} < 6); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | my @chars; |
---|
| 291 | |
---|
| 292 | if ($options{nonalpha}) { |
---|
| 293 | for(0 .. (0 + int(rand(3)))) { |
---|
| 294 | push(@chars, (@non_alpha[rand(scalar(@non_alpha))])); |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | foreach (1 .. ($options{length} - scalar(@chars))) { |
---|
| 299 | if ($options{syllables}) { |
---|
| 300 | my $c = @consonants[rand(scalar(@consonants))]; |
---|
| 301 | my $v = @vowels[rand(scalar(@vowels))]; |
---|
| 302 | push(@chars, "$c$v"); |
---|
| 303 | } else { |
---|
| 304 | push(@chars, (@letters[rand(scalar(@letters))])); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | my $pass = join('', sort { rand() <=> rand() } @chars); |
---|
| 310 | if (length($pass) >= 6 && fascist_check($pass) eq 'ok') { |
---|
| 311 | return $pass; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
[1378] | 316 | =head2 yesno($value, $default) |
---|
| 317 | |
---|
| 318 | Check is a paramter mean yes or no |
---|
| 319 | |
---|
| 320 | =cut |
---|
| 321 | |
---|
| 322 | sub yesno { |
---|
| 323 | my ($value, $default) = @_; |
---|
| 324 | |
---|
| 325 | if ($value =~ /^(yes|1|true)$/i) { |
---|
| 326 | return 1; |
---|
| 327 | } |
---|
| 328 | if ($value =~ /^(no|0|false)$/i) { |
---|
| 329 | return 0; |
---|
| 330 | } |
---|
| 331 | if ($value =~ /^\d+$/ and $value != 0) { |
---|
| 332 | return 1; |
---|
| 333 | } |
---|
| 334 | return $default || $value; |
---|
| 335 | } |
---|
| 336 | |
---|
[175] | 337 | 1; |
---|
[1023] | 338 | |
---|
| 339 | __END__ |
---|
| 340 | |
---|
| 341 | =head1 SEE ALSO |
---|
| 342 | |
---|
| 343 | L<sudo> |
---|
| 344 | |
---|
| 345 | =head1 AUTHOR |
---|
| 346 | |
---|
| 347 | Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt> |
---|
| 348 | |
---|
| 349 | =head1 COPYRIGHT AND LICENSE |
---|
| 350 | |
---|
| 351 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
| 352 | |
---|
| 353 | This library is free software; you can redistribute it and/or modify |
---|
| 354 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 355 | at your option, any later version of Perl 5 you may have available. |
---|
| 356 | |
---|
| 357 | =cut |
---|