#!/usr/sbin/perl -w use Socket; # Order modules are declared is important... use strict; use diagnostics; # Must run as root! ($> == 0) || die("\n\n\nNot Authorized: Can only be run by root user. Goodbye...\n\n"); my $buffer = ''; my $remoteServer = 'fccjvm.fccj.cc.fl.us'; my $proto = getprotobyname('tcp') || 6; my $port = getservbyname('jester', 'tcp') || 1937; my $serverAddr = (gethostbyname($remoteServer))[4]; my $code = ''; my $userid = ''; my $password = ''; my $erc; my $y; my ($acct, $pswd, $uid, $gid, $quota, $cmt, $gcos, $home, $shell); my $testID; my @fields; my $valid_data; # WARNING - WARNING ... # If Testing: addtst; else if production, use addusr... my $cmd = "addusr"; my $requestor = "wcjones"; # Set to your Profs ID prior to executing... my $testing = 0; # Set to 0 for production... #### ... #### my $flag = ''; # If set then next... ++$|; $\ = "\n"; open (MFADD, ">ADD") or die "Cannot open: $! $?"; print MFADD "[User]"; while () { chomp; # no newline... s/#.*//; # no comments... s/^\s+//; # no leading whitespace... s/\s+$//; # no trailing whitespace... next unless length; # anything to process? # Don't forget to validate the data stream! @fields = (split('\|', $_)); unless ($#fields == 5) { # MUST be EXACTLY 6 minus 1 fields (5...) print "\n\n...Invalid or Missing Fields in $_ -$#fields- Not Processed...\n\n"; next; } # Test which fields WHICH MUST have entries and WHAT type or LENGTH is allowed... $flag .= "\nFatal:\nid missing " unless ($fields[0] ne ""); # ID requested missing... $flag .= $fields[0] . " invalid length " unless (length($fields[0]) <= 8 && length($fields[0]) >= 3); # ID requested wrong size (!between 3-8 chars.) $flag .= " fname missing " unless ($fields[1] ne ""); # First Name missing... $flag .= " mi invalid length " unless (length($fields[2]) < 2); # Middle Initial - Blank Allowed... $flag .= " lname missing " unless ($fields[3] ne ""); # Last Name missing... $flag .= " campus missing " unless ($fields[4] ne ""); # Campus missing... $flag .= $fields[4] . " invalid length " unless (length($fields[4]) == 3); # Campus must be 3 chars... $flag .= " ssn missing " unless ($fields[5] ne ""); # SSN missing... $fields[5] =~ s/-//g; # Try to strip dashes, if they are there... $flag .= $fields[5] . " invalid length " unless (length($fields[5]) == 9); # SSN must be 9 chars... if ($flag) { print "\n\nDATA Invalid or Missing: $flag \nin $_ -$#fields- Not Processed...\n\n"; $flag = ''; # Reset it for the next line of data... next; } # See if the ID requested exists in the MOREINFO.DATA file: open (MOREINFO, "moreinfo.data") or die "$? Cannot read file: MOREINFO $!"; my $mi_rec; my @id_rec; while(defined($mi_rec = )) { # print "Looking at record: $mi_rec\n"; @id_rec = split(/\|/, $mi_rec); next if $fields[0] ne lc($id_rec[5]); $y = 1; print "\n ERC! LOGIN $id_rec[5] ($fields[0]) already exists in MOREINFO.DATA... Verify...\n"; last; } # Finally, see if the ID requested exists on the local system... $testID = (split('\|', $_))[0]; setpwent(); # Reset so we can re-read the /etc/passwd file... while (($acct, $pswd, $uid, $gid, $quota, $cmt, $gcos, $home, $shell) = getpwent()) { next if $acct ne $testID; $y = 1; last; } if ($y) { print "\n ERC! LOGIN $testID ($acct) already exists in /etc/passwd... Skipping!\n"; $y = 0; # Reset for next time... } else { # Otherwise we have valid data - Hopefully... :) $valid_data = $fields[0] . '|' . $fields[1] . '|' .$fields[2] . '|' . $fields[3] . '|' . $fields[4] . '|' . $fields[5] . '|' . $requestor; &xAddUsr("$cmd $valid_data"); } } sub xAddUsr { my $xaddusr = shift; print "\n\n======================================================================\nSending: $xaddusr ..."; socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die("fccj: !Socket: $!"); my($packFormat) = 'S n a4 x8'; connect(SOCKET, pack($packFormat, AF_INET(), $port, $serverAddr)) or die("fccj: !Connection: $!"); send(SOCKET, $xaddusr, 0); recv(SOCKET, $buffer, 132, 0); close(SOCKET); print "\nResponse? $buffer ... "; $code = (split(' ', $buffer))[0]; if ($code eq "1000") { $userid = (split(' ', $buffer))[1]; $password = (split(' ', $buffer))[2]; # Ex: sneex,Bill Jones Test Acct,FCCJ1965,,,, # userid|First|M|Last|MCC|SSN|requester print MFADD lc($userid) . ',' . (split('\|', $xaddusr))[1] . ' ' . (split('\|', $xaddusr))[3] . ',' . lc($password) . ',,,,'; } else { print "\n\nAn ERROR occurred during AddUsr;\ngot CODE: $code on BUFFER: $buffer\nfor SENT: $xaddusr ...\n\n"; } } print MFADD "[Global]\n,,,\n[Local]\n,,,"; close (MFADD) or die "Cannot close: $! $?"; $erc = system("cronAcctAD.cgi ADD") / 256 unless $testing; print "An error $! $? occurred during processing of /drv2/home/mainframe/WINNT.ADD ..." if $erc; exit; # Old files, and data formats, used for this & related scripts: # /export/home/bill/cronAcctAD.cgi /drv2/home/mainframe/WINNT.ADD # [User] # sneex,Bill Jones Test Acct,FCCJ1965,,,, # [Global] # ,,, # [Local] # ,,, __DATA__ # Ex: userid|First|M|Last|MCC|SSN # NOTE: Correctly handles HYPHENED Last Names, but NOT Embedded Spaces! # wcjones|William|C|Bruning-Jones|DWC|123456789 # Template: #2345678| - IDs are limited to 8 chars... # -----------------------------------------------------------------------------