#!/usr/bin/perl
# 
# Script to take a standard Fysikum password file,
# reformat it for Netinfo on MacOS X and then load it into the Netinfo
# database. Only users on AFS will be loaded into the Netinfo database and
# local users like root and guest will be filtered out.
# If the user has set a local password instead of the dummy password in
# the password file, that will be kept in this progress.
#
# Author: Joakim Edsjo, edsjo@physto.se
# Date: May 20, 2003
# Version: 1.0
##################################################

# Define password file

$pwdfile="/afs/physto.se/common/uadmin/maps/passwd";

# First load the existing local Netinfo database into memory to check
# if a user has set a local password.

open (IN,"nidump passwd .|") || die "Can't get Netinfo data...\n";
while (defined($line=<IN>)) {
    ($user,$pwd,$rest)=split(/:/,$line);
    if ($pwd != '*' && $pwd != "x"){
        $localpwd{$user}=$pwd;
    }
}


# Now go through the password file and load it into Netinfo
open(IN,$pwdfile) || die "Can't open file $in for reading.\n";

while(defined($line=<IN>)) {
   ($user,$pwd,$uid,$grp,$name,$dir,$shell)=split(/:/,$line);
   if ($dir =~ /\/afs\//) {    # only keep AFS accounts
      next if ($user =~ "guest");   # skip guest account
      next if ($user =~ "root");    # skip root account
      next if ($user =~ "toor");    # skip toor account
      $pwd=$localpwd{$user} if (defined($localpwd{$user})); # use local if exists
      $nipwd = "${user}:${pwd}:${uid}:${grp}::0:0:${name}:${dir}:${shell}";
      system("niload -d passwd . <<EOF\n${nipwd}EOF\n");
      print "Syncing $user with NetInfo...\n";
   }
}
