SUMMARY: How to create a class list

From: system PRIVILEGED account <root_at_alpha1.msvu.ca>
Date: Tue, 1 Oct 1996 17:17:39 -0300 (ADT)

Hi, everyone:

My orignal question is:

 I would like to create a group of users with users' name as
cs1151001, cs1511002, etc and hopefully, the same password as users' name.
Is there any script which allows me to do this?

Thanks to Tom Rioux <rioux_at_ip6480nl.ce.utexas.edu> who suggests that:

look at ftp.ce.utexas.edu:/unix/nis_utils/nis_utils.tar;
look for add_nis_class, add_nis_classu, chdir_nis_user, com_nis_utils,
dis_nis_user, ena_nis_user, mail_nis_user, mvdir_nis_user, paswd_nis_user,
prt_nis_user, and rm_nis_class. you will also need gen_passwd.c and makefile.

And thanks to Kent R Arnott <karnott_at_falcon.tamucc.edu> who provides me the
following perl script. I used Kent's script and it works well!

Deming

#!/usr/bin/perl
########################################################################
# NAME
# addusers - add a list of students to the system
# SYNOPSIS
# addusers [-h home] [-s shell] [-q quota_type] [-p password] [file...]
# DESCRIPTION
# Addusers reads a list of lines of the form "userid username"
# from the input files (stdin if not specified) and creates
# user accounts in the passwd file for DEC UNIX 3.2c on TAMU-CC
# machines. The -h option can be used to specify the target
# for users home directories (default /home/stu). The -s option
# specifies the shell to be used for each user (default /bin/ksh).
# MODIFIED
# 1/14/96 - this was taken from addusers on old falcon (DECstation
# 5240) and modified to work only with c2 security.
#
# 1/14/96 - to work with Digital Unix 3.2c. C2 security has been
# implemented therefore we need to update the auth files when adding
# a user.
#
# 5/18/94 - added -q option to generate quotas for each account.
# The default prototype quota is stu-norm.
#
# 4/22/94 - added -p option to generate passwords for each account.
# The default password is "changeit".
#
# 1/25/93 - Addusers creates a file in the current directory that
# contains those entries where the userid already exists in the
# passwd file (./addusers.dup).
#
# 5/1/96 - added variables expiret and lifet...and subroutine
# addutime. This will calculate the expiration and lifetime of
# a password on the day it is created. The values for u_exp and
# u_life is the number of seconds since January 1970.
#
# 5/28/96 - removed variables expiret and lifet...only need the
# var now to change the succhg expire and life are constant
########################################################################
require 'getopts.pl';
&Getopts('h:s:q:p:');

######################################
# defaults for everyones account
$BASEDIR= $opt_h || "/home/fac";
$SHELL = $opt_s || "/bin/ksh";
$QUOTA= $opt_q || "fac-norm";
$PASSWORD= $opt_p || "changeit";
######################################
$gid="15"; # users
$min_uid="1001";
$max_uid="9998";

chop($PWD=`pwd`);
$DUP="$PWD/addusers.dup";
$PASSWD="/etc/passwd";
$PTMP="/etc/ptmp";
$PTMPTMP="/etc/ptmp$$";
$AUTHBASE="/tcb/files/auth";
$pword=$PASSWORD;

open(DUP,">$DUP") || die "$0: can't write $DUP: $!\n";
while (<>) {
   chop;
# cyn
   #($login,$pword,$uname) = split(' ',$_,3);
   # ($login,$pword,$uname,$SHELL) = split(/:/,$_,4);
   ($login,$uname) = split(' ',$_,2);
        # print "you need to edit this L()()K!!\n";
        # exit 0;
   next unless $login && $uname;
   &CryPw($pword);
   $homedir="$BASEDIR/$login";
   $FirstL=substr($login,0,1);
   $temp="$login:$homedir";
   push(_at_users,$temp);
#cyn
   #&AddUser($login,$uname,$BASEDIR);
   &AddUser($login,$uname,$BASEDIR,$SHELL);
   &CreateAuth($FirstL,$login);
}
close(DUP);

&ChgPerms(_at_users);

if (-z $DUP) { # if it's empty, get rid of it.
    unlink $DUP;
} else {
    print "\nCheck ./addusers.dup for duplicate login id's (if any)\n";
    system("cat $DUP");
}
# --done--

sub CryPw {
    $salt=substr($pword,0,2);
    $password=crypt($pword,$salt);
}

sub AddUser {
#cyn
   #local($login,$uname,$BASEDIR) = _at__;
   local($login,$uname,$BASEDIR,$SHELL) = _at__;
   print "$0: creating password entry for $login\n";
   open(PTMPTMP,">$PTMPTMP") || die "$0: can't create tmp password file: $!\n";
   close(PTMPTMP);
   $locked = link($PTMPTMP,$PTMP);
   unlink($PTMPTMP);
   $locked || die "$0: password file busy...try again later\n";
   open(PASSWD) || die "$0: can't open $PASSWD: $!\n";
   open(PTMP,">$PTMP") || die "$0: can't write $PTMP: $!\n";
   while (<PASSWD>) {
      ($pwlogin,$pwuid) = (split(/:/,$_))[0,2];
      if ($pwlogin eq $login) {
         warn "$0: entry already exists for $login...skipping to next\n";
         close(PASSWD);
             close(PTMP);
         unlink($PTMP);
         return 0;
      }
      print PTMP $_;
   }
   close(PASSWD);
   
   &GetUid;

   print PTMP "$login:*:$uid:$gid:$uname:$homedir:$SHELL\n" ||
      do { unlink($PTMP); die "$0: can't write $PTMP: $!\n"; };
   close(PTMP);

   rename($PASSWD,"$PASSWD.old");
   &Cmd("mv $PTMP $PASSWD") ||
      die "$0: couldn't install new password file: $!\n";
   chdir("/etc");
   &Cmd("/usr/sbin/mkpasswd $PASSWD");
}

sub CreateAuth {
    local($FirstL,$login) = _at__;
    $AUTHDIR="$AUTHBASE/$FirstL";
    if ( -e "$AUTHDIR/$login") {
            print "$0: AUTH entry already exists for $login...please fix/modify \n";
            print "$0:",`ls -l $AUTHDIR/$login`;
            exit 0;
    }
        open(IT,">$AUTHDIR/$login");
# print "EXIT.....neeed to edit tcb file pw is expiring....\n";
# exit 0;
        &addutime;
        print IT <<EOF;
$login:u_name=$login:u_id#$uid:\\
        :u_pwd=$password:\\
        :u_exp#7257600:u_life#8467200:u_succhg#$now:u_restrict:\\
        :u_maxtries#7:u_unlock#7200:\\
        :u_pwdict=Nologin:\\
        :u_lock_at_:chkent:
EOF
close(IT);

&Cmd("/sbin/chown auth $AUTHDIR/$login");
&Cmd("/sbin/chgrp auth $AUTHDIR/$login");
&Cmd("/sbin/chmod 660 $AUTHDIR/$login");
# gotta make sure and fix /etc/passwd file...
&Cmd("/sbin/chmod 644 $PASSWD");

}

sub ChgPerms {
   foreach $temp (_at_users) {
   ($login,$homedir)=split(/:/,$temp,2);
   ($name,$passwd,$uid,$trash)=getpwnam($login);
        
   print "\t *** next newuser is $login \n";
   &Cmd("/sbin/cp -r /usr/skel/ $homedir") || die "$0: can't copy /usr/skel: $!\n";
   &Cmd("/sbin/chown -R $uid $homedir") || die "$0: can't change owner: $!\n";
   &Cmd("chgrp -R users $homedir") || die "$0: can't change group: $!\n";
   &Cmd("chmod 0711 $homedir") || die "$0: can't change permissions: $!\n";
#cyn
   #&Cmd("/usr/sbin/edquota -p $QUOTA $login");

   }
   return 1;
}

sub Cmd {
   print "\t_at__\n";
   system("_at__");
   !$?;
}

sub GetUid {
   open(PASSWD) || die "$0: can't open $PASSWD: $!\n";
   while(<PASSWD>) {
        ($us,$pw,$pid)=split(/:/,$_);
        if (($pid >= $min_uid) && ($pid <= $max_uid)) {
                push(_at_auids,$pid);
        }
   }
   close(PASSWD);
   _at_sauids = sort bynumber _at_auids;
   $tmp=pop(_at_sauids);
   $uid=$tmp+1;
}

sub bynumber { $a <=> $b; }


sub addutime {
        $now=time;
        utime $now $now, _at_ARGV;
}
Received on Tue Oct 01 1996 - 22:54:04 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:47 NZDT