home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1327 / chfn
Text File  |  1990-12-28  |  2KB  |  54 lines

  1. #! /usr/local/bin/suidperl 
  2. #
  3. # Change user's full name in password file
  4. # Must be suid root
  5. #
  6. # Usage: chfn new-full-name
  7. # Author: Dave Mack, csu@alembic.ACS.COM
  8. # Notes:
  9. # Changes only the first occurence of the entry in the passwd
  10. # file with the same uid as the invoking process.
  11. #
  12. # This code is hereby placed in the public domain and may be
  13. # used for any purpose whatsoever, but if you use it, you do
  14. # so at your own risk.
  15.  
  16. die "Usage: chfn new-full-name\n" if ( $#ARGV != 0 );
  17.  
  18. umask(0600); # Don't want folks messing with the tmp file
  19.  
  20. open(PW,"</etc/passwd") || die "Can\'t open /etc/passwd: $!\n";
  21.  
  22. # Using a world-writeable directory for the temp file opens a
  23. # huge security hole. Use /etc instead. 
  24. open(NEWPW,">/etc/pw$$") || die "Can\'t open temp file: $!\n";
  25.  
  26. # pump the entire passwd file into an array. May be bad idea on systems
  27. # with lots of users.
  28. @passlines = <PW>;
  29. close(PW);
  30.  
  31. $changed = 0;
  32. for ( $i = 0; $i <= $#passlines; $i++ ) {
  33.     ($login,$password,$uid,$gid,$fullname,$homedir,$shell) = split(/:/,$passlines[$i]);
  34.     # only change the first instance of entry w/ same uid
  35.     if ($uid eq $< && ! $changed) {
  36.         print "Changing fullname for $login from $fullname to $ARGV[0]\n";
  37.         $fullname = $ARGV[0];
  38.         $changed = 1;
  39.     }
  40.     print NEWPW join(':',$login,$password,$uid,$gid,$fullname,$homedir,$shell);
  41. }
  42. # trash the temp file if we had a problem
  43. if ( ! $changed ) {
  44.     close(NEWPW);
  45.     unlink(NEWPW);
  46.     die "You don't seem to exist. Sorry.\n";
  47. }
  48.  
  49. rename('/etc/passwd','/etc/passwd.bak');
  50. rename("/etc/pw$$","/etc/passwd");
  51. # make sure /etc/passwd is world-readable
  52. chmod 0644,'/etc/passwd';
  53. exit;
  54.