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

Last change on this file since 2441 was 2390, checked in by nanardon, 4 years ago

Add nethost name test to allow utf8

  • Property svn:keywords set to Id Rev
File size: 12.2 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_host_validity($name)
272
273Check C<$name> is suitable to be used as nethost identifier. Return the error
274text, undef if no error.
275
276=cut
277
278sub check_host_validity {
279    my ($name) = @_;
280    return "leadind space" if ($name =~ /^\s/);
281    return "trailing space" if ($name =~ /\s$/);
282    return "containing space" if ($name =~ /\s/);
283    return "containing underscore" if ($name =~ /_/);
284
285    return;
286}
287
288=head2 check_ug_validity($name)
289
290Check C<$name> is suitable to used as user or group identifier.
291
292=cut
293
294sub check_ug_validity {
295    my ($name) = @_;
296    return "Empty name is not a valid name !"
297        if (!$name);
298    return "first caractere must be a-z"
299        if ($name !~ /^[a-z]/);
300    return "must contain only a-z,0-9"
301        if ($name !~ /^[a-z,0-9,_,\-,\.]+$/);
302    return "lenght must be < 19 characters"
303        if (length($name) >= 20);
304
305    return check_oid_validity($name);
306}
307
308=head2 switch_user($runas)
309
310Switch effective id of the process to user named C<$runas>
311
312=cut
313
314sub switch_user {
315    my ($runas) = @_;
316
317    if ($< == 0 || $> == 0) {
318        my @info = getpwnam($runas) or do {
319            warn "Can find user $runas";
320            return;
321        };
322        $> = $info[3];
323        return;
324    } else {
325        warn "we are not root";
326    }
327}
328
329=head2 run_via_sudo($runas)
330
331Rerun current programme as C<$runas> user using sudo
332
333=cut
334
335sub run_via_sudo {
336    my ($runas) = @_;
337
338    my @info = getpwnam($runas) or do {
339        warn "Can find user $runas";
340        return;
341    }; 
342    if ($< != $info[3]) {
343        exec('sudo', '-u', $runas, $0, @ARGV) or "Can run $!";
344    }
345}
346
347=head2 genpassword(%options)
348
349Generate a random password, options are:
350
351=over 4
352
353=item length
354
355The minimum password length (default is 6)
356
357=item nonalpha
358
359Include non alpha-numeric caracters
360
361=item syllables
362
363Use a set of syllables instead letter
364
365=item checkpassword
366
367A sub reference to check if generated password is valid
368
369=back
370
371=cut
372
373sub genpassword {
374    my (%options) = @_;
375
376    $options{checkpassword} ||= sub {
377        my ($cpass) = @_;
378        return fascist_check($cpass) eq 'ok';
379    };
380
381    my @non_alpha = (qw$; : / ! ( ) [ ] { } + = @ - " ' % * & . ? < >$, ',', '$');
382    my @letters = ('a' .. 'z', 'A' .. 'Z', 0 .. 9);
383
384    my @consonants = qw(b d f g j k l m n r s t v x z ch);
385    my @vowels = qw(a e i o u ou oi io ia iu);
386
387    $options{length} ||= 8 + int(rand(3));
388
389    while (1) {
390        if ($options{syllables}) {
391            $options{length} = int($options{length} / 2);
392            $options{length} = 3 if ($options{length} < 3);
393        } else {
394            $options{length} = 6 if ($options{length} < 6);
395        }
396
397        my @chars;
398
399        if ($options{nonalpha}) {
400            for(0 .. (0 + int(rand(3)))) {
401                push(@chars, (@non_alpha[rand(scalar(@non_alpha))]));
402            }
403        }
404
405        foreach (1 .. ($options{length} - scalar(@chars))) {
406            if ($options{syllables}) {
407                my $c = @consonants[rand(scalar(@consonants))];
408                my $v = @vowels[rand(scalar(@vowels))];
409                push(@chars, "$c$v");
410            } else {
411                push(@chars, (@letters[rand(scalar(@letters))]));
412            }
413        }
414
415
416        my $pass = join('', sort { rand() <=> rand() } @chars);
417        if (length($pass) >= 6 && $options{checkpassword}->($pass)) {
418            return $pass;
419        }
420    }
421}
422
423=head2 Crypt($password, $method)
424
425Build an encrypted password using standard crypt(), $method is the encrypted method to use:
426
427=over 4
428
429=item DES: the old DES method, do not use
430
431=item 1 or md5
432
433=item 5 or sha-256
434
435=item 6 or sha-512
436
437=back
438
439=cut
440
441sub Crypt {
442    my ($clearpassword, $method) = @_;
443
444    $method ||= '';
445    my $methNumber = {
446        'des'     => -1,
447        'md5'     =>  1,
448        'sha-256' =>  5,
449        'sha-512' =>  6,
450    }->{lc($method)} || $method || 1;
451
452
453    if ($methNumber > 0) {
454        # Good we're secure
455        my @salt_char = (('a' .. 'z'), ('A' .. 'Z'), (0 .. 9), '/', '.');
456        my $salt = join('', map { $salt_char[rand(scalar(@salt_char))] } (1 .. 8));
457
458        return crypt($clearpassword, '$' . $methNumber . '$' . $salt);
459    } else {
460        # Grumpf DES
461        my @salt_char = (('a' .. 'z'), ('A' .. 'Z'), (0 .. 9));
462        my $salt = join('', map { $salt_char[rand(scalar(@salt_char))] } (1,2));
463
464        return crypt($clearpassword, $salt);
465    }
466}
467
468=head2 buildLogin([$cb, ] @names)
469
470Try to find a proper login from @names.
471
472Optionnal $cb is a callback to check the solution over exiting data. If return false, another solution is tried
473
474Example:
475
476    buildLogin(sub { $_[0] ? 1 : 0 }, "sn", "givenName")
477
478=cut
479
480sub buildLogin {
481    my (@names) = @_;
482    my $cb = undef;
483
484    if (ref $names[0]) {
485        $cb = shift(@names);
486    } else {
487        $cb = sub { 1 }; # always validating
488    }
489
490    # Cleaning names:
491    foreach (@names) {
492        $_ ||= '';
493        $_ = lc(to_ascii($_));
494        s/[^\w]//g;
495    }
496
497    @names = grep { $_ } @names;
498    my $base = shift(@names) or return;
499    my $rest = join('', grep { $_ } @names);
500
501    if ((my $l = length($base)) > 10) {
502        my $len = 8;
503        while ($len < $l) {
504            my $try  = substr($base, 0, $len);
505            if ($cb->($try)) {
506                return $try;
507            }
508            $len++;
509        }
510    } elsif ($cb->($base)) {
511        return $base;
512    }
513
514    my $try = $base;
515    while (my $len = length($try) - length($base) + 1) {
516        if ($len > length($rest)) {
517            last;
518        }
519        $try = $base . substr($rest, 0, $len);
520        if ($cb->($try)) {
521            return $try;
522        }
523    }
524    return;
525}
526
527=head2 yesno($value, $default)
528
529Check is a paramter mean yes or no
530
531=cut
532
533sub yesno {
534    my ($value, $default) = @_;
535
536    if ($value =~ /^(yes|1|true)$/i) {
537        return 1;
538    }
539    if ($value =~ /^(no|0|false)$/i) {
540        return 0;
541    }
542    if ($value =~ /^\d+$/ and $value != 0) {
543        return 1;
544    }
545    return $default || $value;
546}
547
5481;
549
550__END__
551
552=head1 SEE ALSO
553
554L<sudo>
555
556=head1 AUTHOR
557
558Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>
559
560=head1 COPYRIGHT AND LICENSE
561
562Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
563
564This library is free software; you can redistribute it and/or modify
565it under the same terms as Perl itself, either Perl version 5.10.0 or,
566at your option, any later version of Perl 5 you may have available.
567
568=cut
Note: See TracBrowser for help on using the repository browser.