[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PERL] Linux passwords



On Thu, 2 Mar 2000 18:10:25 +0100 (CET), Ole Tange <sslug@sslug> wrote:
> On 2 Mar 2000, Kristian Bjørnskov wrote:
> 
> > On Thu, 02 Mar 2000 14:33:13 +0100, Soren Schimkat <sslug@sslug>
> > wrote:
> > > Er der nogen der har en ide til hvordan man ændre en brugers password
> > > med perl?
> > 
> > Du kan gøre det nøjagtigt lige som i C. Du skal bruge getpwent,
> > setpwent og endpwent til at læse i passwd filen og putpwent til at
> > skrive i den. Se man til disse! Du skal ikke jogge meget i spinaten
> > for at gøre dit system ubrugeligt!
> 
> putpwent har jeg ikke fået til at virke med perl-5.005_03.
> 
> Har du et lille eksempel, der virker?

Der var jeg måske lige lidt for hurtig. Jeg har kigget i gammel kode, og jeg
kan godt se at jeg har programmeret det i c i sin tid. Det skulle dog ikke være
det helt store problem at kunne skrive sikkert i /etc/passwd. Jeg tror
nedenstående holder vand:

#!/usr/bin/perl -w

use FileHandle;
use strict;

my $PASSWD='/etc/passwd';
my $change=0;

die ("usage: $^X username password\n") unless ($#ARGV == 1);

$SIG{'ALRM'}=$SIG{'HUP'}=
  $SIG{'INT'}=$SIG{'QUIT'}=
  $SIG{'TERM'}=$SIG{'TSTP'}=
  $SIG{'TSTP'}=$SIG{'TTOU'}='IGNORE';

sysopen HANDLE, "$PASSWD.xx", O_WRONLY|O_CREAT|O_EXCL, 0644 
                   or die("No open: $!"); 
link "$PASSWD.xx","$PASSWD.x"
                   or die ("No link: $!");
unlink "$PASSWD.xx" or die ("No unlink: $!");
sysopen PASSWD, "$PASSWD", O_RDONLY|O_EXCL or die ("No RO open: $!");
while (<PASSWD>) {
  chop;
  my ($login,$passwd,$uid,$gid,$gcos, $home, $shell)=split /:/;
 
  if($login eq $ARGV[0]) { 
    srand(time()^($$+($$ <<15 )));
    my $salt=pack("cc",65+int(rand(60)),65+int(rand(60)));
    $passwd=crypt($ARGV[1],$salt);
    $change++;
  };

  print HANDLE "$login:$passwd:$uid:$gid:$gcos:$home:$shell\n";
}
unlink "$PASSWD.old";
link("$PASSWD","$PASSWD.OLD");
close(HANDLE);
rename "$PASSWD.x","$PASSWD";
close(PASSWD);
print "Changed $change password(s)\n";


-- 
Most kind regards,
Kristian Bjoernskov, <mailto:sslug@sslug>
Novo Nordisk IT, Network - internet.
Phone: +45 4442 3299. Fax: +45 4442 6061


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 19:52 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *