home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / mvids < prev    next >
Encoding:
Text File  |  1990-08-12  |  4.6 KB  |  202 lines

  1. #!/usr/bin/perl
  2. #
  3. # mvids - "moves" uids and gids
  4. # Tom Christiansen <tchrist@convex.com>
  5. #
  6. # usage: mvids [-n] [-f howtomvfile] [starting-dir]
  7. #    
  8. # Takes list of new user and group ids.
  9. # Fixes passwd and group files.
  10. # Traverses local file system starting with starting-dir
  11. #      updating all changed ids
  12. #
  13. # -n means don't really change anything 
  14. # -f is if you don't like the default description file name of howtomv
  15.  
  16.  
  17. # read descriptions from howtomv file with format: 
  18. #    type name number
  19. # e.g.:
  20. #    user tom 1023
  21. #    group staff 200
  22.  
  23.  
  24. $| = 1;
  25. $oops = 0;
  26.  
  27. do 'getopts.pl' || warn "problem doing getopts.pl: $!\n";
  28.  
  29. do Getopts('dnf:');
  30.  
  31. $FILE = $opt_f || "howtomv"; 
  32.  
  33. $DIR = $opt_d ? "." : "/etc";
  34.  
  35. $topdir = shift || '/';
  36.  
  37. die "usage: $0 [-n] [-f howtomv] [starting-dir]\n"     if $#ARGV > -1;
  38.  
  39. die "$topdir: Not a directory"    unless -d $topdir;
  40.  
  41. open FILE || die "Can't open directions file \"$FILE\": $!\n";
  42. while (<FILE>) {
  43.     s/\s*#.*//;
  44.     next if /^$/;
  45.     unless (/^(user|group)\s+(\w+)\s+(\d+)/) {
  46.     print STDERR "malformed line at line $. of $FILE: $_";
  47.     $oops++; next;
  48.     } 
  49.     if ($3 > 32000) {
  50.     print STDERR "$1 $2 has id that's too big ($3)\n";
  51.     $oops++; next;
  52.     } 
  53.     if ($3 == 0) {
  54.     print STDERR "Too dangerous to move $1 $2 to 0\n";
  55.     $oops++; next;
  56.     } 
  57.     if ($2 eq 'root') {
  58.     print STDERR "You don't really want to move root\n";
  59.     $oops++; $next;
  60.     } 
  61.     if ($1 eq 'user') {
  62.     if (defined $n_pwn2i{$2}) {
  63.         print STDERR "Saw user $2 again at line $. of $FILE\n";
  64.         $oops++; next;
  65.     }
  66.     if (defined $n_pwi2n{$3}) {
  67.         print STDERR "Saw uid $3 again at line $. of $FILE\n";
  68.         $oops++; next;
  69.     }
  70.     $uids++;
  71.     $n_pwn2i{$2} = $3;
  72.     $n_pwi2n{$3} = $2;
  73.     } else {
  74.     if (defined $n_grn2i{$2}) {
  75.         print STDERR "Saw group $2 again at line $. of $FILE\n";
  76.         $oops++; next;
  77.     } 
  78.     if (defined $n_gri2n{$3}) {
  79.         print STDERR "Saw gid $3 again at line $. of $FILE\n";
  80.         $oops++; next;
  81.     }
  82.     $gids++;
  83.     $n_grn2i{$2} = $3;
  84.     $n_gri2n{$3} = $2;
  85.     } 
  86.  
  87. $PWD  = "$DIR/passwd";
  88. $NPWD = "$PWD.new";
  89.  
  90. if ($uids) {
  91.     open PWD             || die "Can't open $PWD: $!\n";
  92.     open (NPWD, ">$NPWD")     || die "Can't create $NPWD: $!\n";
  93.  
  94.     while (<PWD>) {
  95.     ((($name,$uid) = /^(\w+):[^:]*:(\d+):/)) 
  96.         || die "Bad passwd entry at line $.\n";
  97.     if (defined $n_pwi2n{$uid} && !defined $n_pwn2i{$name}) {
  98.         printf STDERR "Can't move user %s to uid %d -- %s already has it\n",
  99.             $n_pwi2n{$uid}, $uid, $name;
  100.         $oops++;
  101.         next;
  102.     } 
  103.     $pwn2i{$name} = $uid;
  104.     s/:$uid:/:$n_pwn2i{$name}:/    if defined $n_pwn2i{$name};
  105.     print NPWD;
  106.     }
  107.     close PWD;
  108.     close NPWD;
  109.  
  110.     foreach $user (keys %pwnam) {
  111.     unless (defined $pwn2i{$user}) {
  112.         print STDERR "Can't move non-existent user $user\n";
  113.         $oops++;
  114.     } 
  115.     } 
  116.  
  117. }
  118.  
  119. if ($gids) {
  120.     $GRP = "$DIR/group";
  121.     $NGRP = "$GRP.new";
  122.     open GRP             || die "Can't open $GRP: $!\n";
  123.     open (NGRP , ">$NGRP")     || die "Can't create $NGRP: $!\n";
  124.  
  125.     while (<GRP>) {
  126.     ((($name,$gid) = /^(\w+):[^:]*:(\d+):/)) 
  127.         || die "Bad group entry at line $.\n";
  128.     if (defined $n_gri2n{$gid} && !defined $n_grn2i{$name}) {
  129.         printf STDERR "Can't move gid %s to %d -- %s already has it\n",
  130.             $n_gri2n{$gid}, $gid, $name;
  131.         $oops++;
  132.         next;
  133.     } 
  134.     $grn2i{$name} = $gid;
  135.     s/:$gid:/:$n_grn2i{$name}:/    if defined $n_grn2i{$name};
  136.     print NGRP;
  137.     } 
  138.     close GRP;
  139.     close NGRP;
  140.  
  141.     foreach $group (keys %grnam) {
  142.     unless (defined $grn2i{$group}) {
  143.         print STDERR "Can't move non-existent group $group\n";
  144.         $oops++;
  145.     } 
  146.     } 
  147.  
  148. }
  149.  
  150. die "$0: $oops error" . ($oops > 1 ? "s" : "").
  151.     " in remapping directions.\n"        if $oops;
  152.  
  153.  
  154. die "$0: no ids to move\n" unless $uids || $gids;
  155.  
  156. # ok, now do it
  157.  
  158. open(FIND, "find $topdir \\( -fstype nfs -prune \\) -o -ls |")
  159.     || die "Can't open find pipe";
  160.  
  161. while (<FIND>) {
  162.     split;
  163.     $uid = $gid = -1;
  164.     ($file, $user, $group) = ($_[11], $_[5], $_[6]);
  165.  
  166.     if (defined $n_pwn2i{$user}) {
  167.     $uid = $n_pwn2i{$user};
  168.     print "changing owner $user of $file from ",
  169.         "$pwn2i{$user} to $n_pwn2i{$user}\n";
  170.     }
  171.     if (defined $n_grn2i{$group}) {
  172.     $gid = $n_grn2i{$group};
  173.     print "changing group $group of $file from ",
  174.         "$grn2i{$group} to $n_grn2i{$group}\n";
  175.     }
  176.  
  177.     if (!$opt_n && ($uid != -1 || $gid != -1)) {
  178.     if (!chown $uid, $gid, $file) {
  179.         printf STDERR "couldn't chown $file to $uid.$gid: $!\n";
  180.         $oops++;
  181.     } 
  182.     } 
  183.  
  184. unless ($opt_n) {
  185.     if ($uids) {
  186.     rename($PWD, "$PWD.bak")     
  187.         || die "Can't mv $PWD to $PWD.bak: $!\n";
  188.     rename($NPWD, $PWD)        
  189.         || die "Can't mv $NPWD to $PWD: $!\n";
  190.     }
  191.     if ($gids) {
  192.     rename($GRP, "$GRP.bak")
  193.         || die "Can't mv $GRP to $GRP.bak: $!\n";
  194.     rename($NGRP, $GRP)
  195.         || die "Can't mv $NGRP to $GRP: $!\n";
  196.     }
  197.  
  198. exit ($oops != 0);
  199.