Ignore:
Timestamp:
12/02/11 11:42:17 (13 years ago)
Author:
nanardon
Message:
  • reimport missing files from previous svn
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm

    r818 r861  
    1212 
    1313@ISA = qw(Exporter); 
    14 @EXPORT = qw(to_ascii exec_command); 
    15 @EXPORT_OK = qw(to_ascii exec_command); 
     14@EXPORT = qw(to_ascii exec_command switch_user run_via_sudo); 
     15@EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo); 
    1616 
    1717sub to_ascii { 
     
    2121    $text =~ s/œ/oe/g; 
    2222    $text =~ s/Ê/ae/g; 
    23     $text =~ tr {uàâÀÂÄÀçéÚêëÉÈÊËïîÏÎÞöÎÖÔÌûÛÜ} 
    24                 {uaaaAAAceeeeEEEEiiIIoooOOuuUU}; 
     23    $text =~ tr {uàâÀÂÄÀçéÚêëÉÈÊËïîÏÎÞöÎÖÔÌûÛÜć} 
     24                {uaaaAAAceeeeEEEEiiIIoooOOuuUUc}; 
    2525    $text =~ s/([^[:ascii:]])/_/g; 
    2626    $text 
     
    2929sub exec_command { 
    3030    my ($command, $env) = @_; 
     31    my $rout = undef; 
     32    $rout = \$_[2] if(@_ > 2); 
    3133 
    3234    my @exec = ref $command 
     
    3537    la_log(LA_DEBUG, 'running command `%s\'', join(' ', @exec)); 
    3638 
     39    pipe(my $rh, my $wh); 
    3740    my $pid = fork; 
    3841    if (!defined($pid)) { 
     
    4043    } elsif ($pid) { 
    4144        # Father 
     45        close($wh); 
     46        my $header; 
     47        while (<$rh>) { 
     48            if ($rout) { 
     49                $$rout .= $_; 
     50            } else { 
     51                chomp; 
     52                if (!$header) { 
     53                    $header = 1; 
     54                    la_log(LA_NOTICE, "exec `%s'", join(' ', @exec)); 
     55                } 
     56                la_log(LA_NOTICE, "output: %s", $_); 
     57            } 
     58        } 
    4259        waitpid($pid, 0); 
    4360        if (my $exitstatus = $?) { 
     
    5067    } else { 
    5168        # Child 
     69        close($rh); 
     70        ( $ENV{LA_MODULE} ) = caller(); 
    5271        foreach (keys %{ $env || {} }) { 
    5372            $ENV{"LA_$_"} = $env->{$_}; 
    5473        } 
     74        open(STDOUT, ">&=" . fileno($wh)); 
     75        open(STDERR, ">&=" . fileno($wh)); 
    5576        exec(@exec); 
    5677        exit($!); 
     
    7899            } 
    79100        } else { 
    80             $attributes{$attr} = $value || undef; 
    81             $attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1; 
     101            $attributes{$attr} = $value eq '' ? undef : $value; 
     102            # Don't remember why this is here 
     103            #$attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1; 
    82104        } 
    83105    } 
     
    129151        if ($name !~ /^[a-z]/); 
    130152    return "must contain only a-z,0-9" 
    131         if ($name !~ /^[a-z,0-9]+$/); 
     153        if ($name !~ /^[a-z,0-9,_,-]+$/); 
    132154 
    133155    return check_oid_validity($name); 
    134156} 
    135157 
     158sub switch_user { 
     159    my ($runas) = @_; 
     160 
     161    if ($< == 0 || $> == 0) { 
     162        my @info = getpwnam($runas) or do { 
     163            warn "Can find user $runas"; 
     164            return; 
     165        }; 
     166        $> = $info[3]; 
     167        return; 
     168    } else { 
     169        warn "we are not root"; 
     170    } 
     171} 
     172 
     173sub run_via_sudo { 
     174    my ($runas) = @_; 
     175 
     176    my @info = getpwnam($runas) or do { 
     177        warn "Can find user $runas"; 
     178        return; 
     179    };  
     180    if ($< != $info[3]) { 
     181        exec('sudo', '-u', $runas, $0, @ARGV) or "Can run $!"; 
     182    } 
     183} 
     184 
    1361851; 
Note: See TracChangeset for help on using the changeset viewer.