source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm @ 2374

Last change on this file since 2374 was 2352, checked in by nanardon, 4 years ago

Add basic template features

  • Property svn:keywords set to Id Rev
File size: 11.8 KB
Line 
1package LATMOS::Accounts::Utils;
2use 5.010000;
3use strict;
4use warnings;
5use Exporter ();
6use vars qw(@ISA @EXPORT_OK @EXPORT);
7use utf8;
8use LATMOS::Accounts::Log;
9use File::Temp qw(tempfile);
10use Crypt::Cracklib;
11use Text::CSV;
12
13our $VERSION = (q$Rev: 2073 $ =~ /^Rev: (\d+) /)[0];
14
15=head1 NAME
16
17LATMOS::Accounts::Utils - Utils functions
18
19=head1 FUNCTIONS
20
21=cut
22
23@ISA = qw(Exporter);
24@EXPORT = qw(to_ascii exec_command switch_user run_via_sudo buildLogin loadCSV yesno);
25@EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo buildLogin loadCSV yesno);
26
27=head2 to_ascii($text)
28
29Replace in C<$text> non ascii caracters from iso-8859-15 table to ascii
30equivalent caracter.
31
32=cut
33
34sub to_ascii {
35    my ($text) = @_;
36    return $text unless(defined($text));
37    utf8::decode($text) unless(utf8::is_utf8($text));
38    $text =~ s/œ/oe/g;
39    $text =~ s/Ê/ae/g;
40    $text =~ tr {uàâÀÂÄÀçéÚêëÉÈÊËïîÏÎÞöÎÖÔÌûÛÜć}
41                {uaaaAAAceeeeEEEEiiIIoooOOuuUUc};
42    $text =~ s/([^[:ascii:]])/_/g;
43    $text
44} 
45
46=head2 exec_command($command, $env)
47
48Execute C<$command> and redirect output to log system.
49
50C<$env> is a hashref containing environment variable to set, all variables are
51prefixed by 'LA_'.
52
53=cut
54
55sub exec_command {
56    my ($command, $env) = @_;
57    my $rout = undef;
58    $rout = \$_[2] if(@_ > 2);
59
60    my @exec = ref $command
61        ? (@$command)
62        : ($command);
63    la_log(LA_DEBUG, 'running command `%s\'', join(' ', @exec));
64
65    pipe(my $rh, my $wh);
66    my $pid = fork;
67    if (!defined($pid)) {
68        la_log(LA_ERR, "Can't launch script: cannot fork");
69    } elsif ($pid) {
70        # Father
71        close($wh);
72        my $header;
73        while (<$rh>) {
74            if ($rout) {
75                $$rout .= $_;
76            } else {
77                chomp;
78                if (!$header) {
79                    $header = 1;
80                    la_log(LA_NOTICE, "exec `%s'", join(' ', @exec));
81                }
82                la_log(LA_NOTICE, "output: %s", $_);
83            }
84        }
85        waitpid($pid, 0);
86        if (my $exitstatus = $?) {
87            la_log(LA_ERR, 'command %s exit with status %d',
88                join(' ', @exec), $exitstatus);
89            return;
90        } else {
91            return 1;
92        }
93    } else {
94        # Child
95        close($rh);
96        ( $ENV{LA_MODULE} ) = caller();
97        foreach (keys %{ $env || {} }) {
98            $ENV{"LA_$_"} = $env->{$_};
99        }
100        open(STDOUT, ">&=" . fileno($wh));
101        open(STDERR, ">&=" . fileno($wh));
102        exec(@exec);
103        exit($!);
104    }
105    1
106}
107
108sub _parse_line {
109    my ( $line, $attributes ) = @_;
110
111    $line =~ /^#/ and return;
112    # Empty line are skipped (or space only)
113    $line =~ /^\s*$/ and return;
114    chomp($line);
115    my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or
116        die "Malformed input file\n";
117    $value =~ s/\s*$//;
118    $value =~ s/\\n/\n/g;
119    if ($attributes->{$attr}) {
120        if (ref $attributes->{$attr}) {
121            push(@{ $attributes->{$attr} }, $value);
122        } else {
123            my $temp = $attributes->{$attr};
124            $attributes->{$attr} = [ $temp, $value ];
125        }
126    } else {
127        $attributes->{$attr} = $value eq '' ? undef : $value;
128        # Don't remember why this is here
129        #$attr eq 'exported' && !defined $attributes->{$attr} and $attributes->{$attr} = 1;
130    }
131}
132
133=head2 parse_obj_file($handle)
134
135Read file content from C<$handle> and return hash containing parsed attributes
136
137=cut
138
139sub parse_obj_file {
140    my ($handle) = @_;
141
142    my $attributes = {};
143    while (my $line = <$handle>) {
144        _parse_line( $line, $attributes );
145    }
146    %$attributes
147}
148
149=head2 parse_obj_text($text)
150
151=cut
152
153sub parse_obj_text {
154    my ( $Text ) = @_;
155
156    my $attributes = {};
157
158    foreach (split(/\n/, $Text)) {
159        _parse_line( $_, $attributes );
160    }
161
162    %$attributes
163}
164
165=head2 dump_read_temp_file($writecb, $readcb)
166
167Create a temporary file, call C<$writecb()> function, run editor and if file get
168modified call C<$readcb>.
169
170=cut
171
172sub dump_read_temp_file {
173    my ($writecb, $readcb) = @_;
174
175    my ($fh, $filename) = tempfile(CLEANUP => 0);
176    $writecb->($fh) or return;
177    $fh = undef; # closing file
178    my $res;
179   
180    my @stat = stat($filename);
181    while (1) {
182        my $cmd = ($ENV{EDITOR} || 'vi') . " $filename";
183        warn "Running $cmd\n";
184        if (system($cmd) == -1 ) {
185            warn "Cannot run editor $!\n";
186            last;
187        }
188        if ((stat($filename))[9] == $stat[9]) {
189            warn "No change existing\n";
190            last;
191        }
192
193        open($fh, '<', $filename) or return;
194        $res = $readcb->($fh);
195        $fh = undef; # closing again
196        $res < 2 and last;
197    }
198    unlink($filename);
199    $res;
200}
201
202=head2 loadCSV($fh, $callback, $initcallback)
203
204Parse CVS files and return an array for each parsed line.
205
206C<%options> may contains
207
208=over 4
209
210=item C<initcb> An function call after parsing first line
211
212=item C<cb> A function call for each line
213
214=back
215
216=cut
217
218sub loadCSV {
219    my ($fh, %opt) = @_;
220
221    my $csv = Text::CSV->new({
222            blank_is_undef => 1,
223            binary => 1,
224    });
225
226    binmode($fh, ":encoding(UTF-8)");
227
228    # First line contains attribute
229    my $columns = $csv->getline( $fh );
230
231    $csv->column_names($columns);
232
233    if ($opt{initcb}) {
234        $opt{initcb}->($csv);
235    }
236
237    my $all = [];
238    my $linecount = 1;
239    while ( my $row = $csv->getline_hr( $fh ) ) {
240        $linecount++;
241        if ($opt{cb}) {
242            if (! $opt{cb}->($row, $linecount)) {
243                return;
244            }
245        }
246        push(@{ $all }, $row);
247    }
248    $csv->eof () or do {
249        return;
250    };
251
252    return $all;
253}
254
255=head2 check_oid_validity($name)
256
257Check C<$name> is suitable to be used as object identifier. Return the error
258text, undef if no error.
259
260=cut
261
262sub check_oid_validity {
263    my ($name) = @_;
264    return "leadind space" if ($name =~ /^\s/);
265    return "trailing space" if ($name =~ /\s$/);
266    return "containing space" if ($name =~ /\s/);
267
268    return;
269}
270
271=head2 check_ug_validity($name)
272
273Check C<$name> is suitable to used as user or group identifier.
274
275=cut
276
277sub check_ug_validity {
278    my ($name) = @_;
279    return "Empty name is not a valid name !"
280        if (!$name);
281    return "first caractere must be a-z"
282        if ($name !~ /^[a-z]/);
283    return "must contain only a-z,0-9"
284        if ($name !~ /^[a-z,0-9,_,\-,\.]+$/);
285    return "lenght must be < 19 characters"
286        if (length($name) >= 20);
287
288    return check_oid_validity($name);
289}
290
291=head2 switch_user($runas)
292
293Switch effective id of the process to user named C<$runas>
294
295=cut
296
297sub switch_user {
298    my ($runas) = @_;
299
300    if ($< == 0 || $> == 0) {
301        my @info = getpwnam($runas) or do {
302            warn "Can find user $runas";
303            return;
304        };
305        $> = $info[3];
306        return;
307    } else {
308        warn "we are not root";
309    }
310}
311
312=head2 run_via_sudo($runas)
313
314Rerun current programme as C<$runas> user using sudo
315
316=cut
317
318sub run_via_sudo {
319    my ($runas) = @_;
320
321    my @info = getpwnam($runas) or do {
322        warn "Can find user $runas";
323        return;
324    }; 
325    if ($< != $info[3]) {
326        exec('sudo', '-u', $runas, $0, @ARGV) or "Can run $!";
327    }
328}
329
330=head2 genpassword(%options)
331
332Generate a random password, options are:
333
334=over 4
335
336=item length
337
338The minimum password length (default is 6)
339
340=item nonalpha
341
342Include non alpha-numeric caracters
343
344=item syllables
345
346Use a set of syllables instead letter
347
348=item checkpassword
349
350A sub reference to check if generated password is valid
351
352=back
353
354=cut
355
356sub genpassword {
357    my (%options) = @_;
358
359    $options{checkpassword} ||= sub {
360        my ($cpass) = @_;
361        return fascist_check($cpass) eq 'ok';
362    };
363
364    my @non_alpha = (qw$; : / ! ( ) [ ] { } + = @ - " ' % * & . ? < >$, ',', '$');
365    my @letters = ('a' .. 'z', 'A' .. 'Z', 0 .. 9);
366
367    my @consonants = qw(b d f g j k l m n r s t v x z ch);
368    my @vowels = qw(a e i o u ou oi io ia iu);
369
370    $options{length} ||= 8 + int(rand(3));
371
372    while (1) {
373        if ($options{syllables}) {
374            $options{length} = int($options{length} / 2);
375            $options{length} = 3 if ($options{length} < 3);
376        } else {
377            $options{length} = 6 if ($options{length} < 6);
378        }
379
380        my @chars;
381
382        if ($options{nonalpha}) {
383            for(0 .. (0 + int(rand(3)))) {
384                push(@chars, (@non_alpha[rand(scalar(@non_alpha))]));
385            }
386        }
387
388        foreach (1 .. ($options{length} - scalar(@chars))) {
389            if ($options{syllables}) {
390                my $c = @consonants[rand(scalar(@consonants))];
391                my $v = @vowels[rand(scalar(@vowels))];
392                push(@chars, "$c$v");
393            } else {
394                push(@chars, (@letters[rand(scalar(@letters))]));
395            }
396        }
397
398
399        my $pass = join('', sort { rand() <=> rand() } @chars);
400        if (length($pass) >= 6 && $options{checkpassword}->($pass)) {
401            return $pass;
402        }
403    }
404}
405
406=head2 Crypt($password, $method)
407
408Build an encrypted password using standard crypt(), $method is the encrypted method to use:
409
410=over 4
411
412=item DES: the old DES method, do not use
413
414=item 1 or md5
415
416=item 5 or sha-256
417
418=item 6 or sha-512
419
420=back
421
422=cut
423
424sub Crypt {
425    my ($clearpassword, $method) = @_;
426
427    $method ||= '';
428    my $methNumber = {
429        'des'     => -1,
430        'md5'     =>  1,
431        'sha-256' =>  5,
432        'sha-512' =>  6,
433    }->{lc($method)} || $method || 1;
434
435
436    if ($methNumber > 0) {
437        # Good we're secure
438        my @salt_char = (('a' .. 'z'), ('A' .. 'Z'), (0 .. 9), '/', '.');
439        my $salt = join('', map { $salt_char[rand(scalar(@salt_char))] } (1 .. 8));
440
441        return crypt($clearpassword, '$' . $methNumber . '$' . $salt);
442    } else {
443        # Grumpf DES
444        my @salt_char = (('a' .. 'z'), ('A' .. 'Z'), (0 .. 9));
445        my $salt = join('', map { $salt_char[rand(scalar(@salt_char))] } (1,2));
446
447        return crypt($clearpassword, $salt);
448    }
449}
450
451=head2 buildLogin([$cb, ] @names)
452
453Try to find a proper login from @names.
454
455Optionnal $cb is a callback to check the solution over exiting data. If return false, another solution is tried
456
457Example:
458
459    buildLogin(sub { $_[0] ? 1 : 0 }, "sn", "givenName")
460
461=cut
462
463sub buildLogin {
464    my (@names) = @_;
465    my $cb = undef;
466
467    if (ref $names[0]) {
468        $cb = shift(@names);
469    } else {
470        $cb = sub { 1 }; # always validating
471    }
472
473    # Cleaning names:
474    foreach (@names) {
475        $_ ||= '';
476        $_ = lc(to_ascii($_));
477        s/[^\w]//g;
478    }
479
480    @names = grep { $_ } @names;
481    my $base = shift(@names) or return;
482    my $rest = join('', grep { $_ } @names);
483
484    if ((my $l = length($base)) > 10) {
485        my $len = 8;
486        while ($len < $l) {
487            my $try  = substr($base, 0, $len);
488            if ($cb->($try)) {
489                return $try;
490            }
491            $len++;
492        }
493    } elsif ($cb->($base)) {
494        return $base;
495    }
496
497    my $try = $base;
498    while (my $len = length($try) - length($base) + 1) {
499        if ($len > length($rest)) {
500            last;
501        }
502        $try = $base . substr($rest, 0, $len);
503        if ($cb->($try)) {
504            return $try;
505        }
506    }
507    return;
508}
509
510=head2 yesno($value, $default)
511
512Check is a paramter mean yes or no
513
514=cut
515
516sub yesno {
517    my ($value, $default) = @_;
518
519    if ($value =~ /^(yes|1|true)$/i) {
520        return 1;
521    }
522    if ($value =~ /^(no|0|false)$/i) {
523        return 0;
524    }
525    if ($value =~ /^\d+$/ and $value != 0) {
526        return 1;
527    }
528    return $default || $value;
529}
530
5311;
532
533__END__
534
535=head1 SEE ALSO
536
537L<sudo>
538
539=head1 AUTHOR
540
541Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>
542
543=head1 COPYRIGHT AND LICENSE
544
545Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
546
547This library is free software; you can redistribute it and/or modify
548it under the same terms as Perl itself, either Perl version 5.10.0 or,
549at your option, any later version of Perl 5 you may have available.
550
551=cut
Note: See TracBrowser for help on using the repository browser.