home *** CD-ROM | disk | FTP | other *** search
- #! /usr/local/bin/suidperl
- #
- # Change user's full name in password file
- # Must be suid root
- #
- # Usage: chfn new-full-name
- # Author: Dave Mack, csu@alembic.ACS.COM
- # Notes:
- # Changes only the first occurence of the entry in the passwd
- # file with the same uid as the invoking process.
- #
- # This code is hereby placed in the public domain and may be
- # used for any purpose whatsoever, but if you use it, you do
- # so at your own risk.
-
- die "Usage: chfn new-full-name\n" if ( $#ARGV != 0 );
-
- umask(0600); # Don't want folks messing with the tmp file
-
- open(PW,"</etc/passwd") || die "Can\'t open /etc/passwd: $!\n";
-
- # Using a world-writeable directory for the temp file opens a
- # huge security hole. Use /etc instead.
- open(NEWPW,">/etc/pw$$") || die "Can\'t open temp file: $!\n";
-
- # pump the entire passwd file into an array. May be bad idea on systems
- # with lots of users.
- @passlines = <PW>;
- close(PW);
-
- $changed = 0;
- for ( $i = 0; $i <= $#passlines; $i++ ) {
- ($login,$password,$uid,$gid,$fullname,$homedir,$shell) = split(/:/,$passlines[$i]);
- # only change the first instance of entry w/ same uid
- if ($uid eq $< && ! $changed) {
- print "Changing fullname for $login from $fullname to $ARGV[0]\n";
- $fullname = $ARGV[0];
- $changed = 1;
- }
- print NEWPW join(':',$login,$password,$uid,$gid,$fullname,$homedir,$shell);
- }
- # trash the temp file if we had a problem
- if ( ! $changed ) {
- close(NEWPW);
- unlink(NEWPW);
- die "You don't seem to exist. Sorry.\n";
- }
-
- rename('/etc/passwd','/etc/passwd.bak');
- rename("/etc/pw$$","/etc/passwd");
- # make sure /etc/passwd is world-readable
- chmod 0644,'/etc/passwd';
- exit;
-