Thread Passwort ändern (3 answers)
Opened by KCobain at 2011-12-25 21:47

KCobain
 2011-12-25 21:47
#155067 #155067
User since
2011-10-03
34 Artikel
BenutzerIn
[default_avatar]
Moin,
ich möchte mittels Perl ein Passwort eines Nutzers ändern. passwd kann man ja nicht nutzn, weil man da das passwort selbst eingeben muss. habe folgende funktion gefunden:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
sub change_password # (user, newpass){
  # Gegebenenfalls sind die Pfade von 'cp' und 'sed' anzupassen!
  #
  # can only be done as root
  # Errorcodes:
  #   0  O.K.
  #   1  User does not exist
  #   2  More than one user
  #   3  Error while copying $passwd to $ptmp
  #   4  Changing $ptmp failed
  #   5  Error while copying $ptmp to $passwd

  my $user = @_[0];
  my $newpass = @_[1];
  my @saltset = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '.', '/');
  my $ptmp = '/etc/ptmp';
  my $passwd = '/etc/shadow';
  my @entry = ();
  my @rest=();
  my $name = '';
  my $oldcrypt = '';
  my $newcrypt = '';
  my $salt = '';
  my $ok = '';
  my $line = '';
  
  srand(time|$$);
  open(PASSWD,'<' . $passwd);
  @entry = grep(/^$user:/,<PASSWD>);
  close PASSWD;
  return(1) if ($#entry == -1); 
  return(2) if ($#entry != 0); 
  $line = shift(@entry);
  ($name, $oldcrypt, @rest) = split(/:/, $line);
  $salt = $saltset[rand(62)] . $saltset[rand(62)];    # form new salt
  $newcrypt = crypt($newpass,$salt);    # form new encrypted password
  
  sleep(2) while ( -f $ptmp ); 
  $ok = system("/bin/cp $passwd $ptmp");
  return(3) if ($ok != 0); 
  $ok = system("/usr/bin/sed -e 's/^$user:$oldcrypt:/$user:$newcrypt:/' $passwd > $ptmp");
  if ($ok != 0) 
    {
    unlink($ptmp);
    return(4);
    }
  $ok = system("/bin/cp $ptmp $passwd");
  if ($ok != 0) 
    {
    print "RED ALERT! Couldn't update $passwd. Backup file is $ptmp\n";
    return(5);
    }
  unlink($ptmp);
  return(0);    
  } 


Funzt nicht, bekomme folgende meldung:
Quote
/usr/bin/sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'

View full thread Passwort ändern