[NTLUG:Discuss] Sendmail/setting up user accounts. Mail server change..

Jonathan Miller betaray at kludge.org
Wed Oct 13 13:33:07 CDT 1999


If you want an example of how to make passwd and shadow files solely from perl then the following will be of intrest:

(My implementation has some certain confusing things that I've removed to make it a more general script..)

----------------------------
print "New password file, shadow file, and home directories  being built...\n";

# Open files that will be used with mkpl #
open(PASSOUT,">passwd.new") || die "Can't open password output file: $!\n";
open(SHADOUT,">shadow.new") || die "Can't open shadow output file: $!\n";

# Generate from template #
open(PASSTMP,"passwd.tmp") || die "Can't open password input file: $!\n";
open(SHADTMP,"shadow.tmp") || die "Can't open shadow input file: $!\n";
while (<PASSTMP>)
{
  print PASSOUT;
}
close(PASSTMP);

while (<SHADTMP>)
{
  print SHADOUT;
}
close(SHADTMP);

#-- Generate passwd file --#

# Select account data #

# Print that data out to the appropriate files #
while (($username, $password, $lname, $fname, $uid, $gid, $shell) = <INPUTFILE>) { #of course INPUT File has to be opened and it's gotta have the info in the right format etc.
        $homedir = "/home/" . substr($username,0,1) . "/" . $username;
        if (!(-e $homedir)) {
                # system("mdkir $homedir");


        $num = rand(69) + 46;
        if (($num > 58) && ($num < 65)) {
                $num += 7;
        } elsif ($num > 90) {
                $num += 7;
        }
        $salt = chr($num);

        $num = rand(69) + 46;
        if (($num > 58) && ($num < 65)) {
                $num += 7;
        } elsif ($num > 90) {
                $num += 7;
        }
        $salt = $salt . chr($num);
        print $salt, "\n";

        $cryptpass = crypt($password, $salt);

        print PASSOUT "$username:x:$uid:$gid:$fname $lname,,,:$homedir:$shell\n";
        print SHADOUT "$username:$cryptpass:0:0:99999::::\n";
}

# Close opened files #
close(PASSOUT);
close(SHADOUT);

-----------------
Yes I know I have the same 7 lines twice and I could have made a function or a loop or something. But you know what? I didn't.

Also passwd.tmp and shadow.tmp are templates that contain the default accounts for the system and generally anything that's not in your datafile.

This is intended just to be a sample, and not anything I would want you to copy and paste on into a file and expect to work.

-- Jonathan




More information about the Discuss mailing list