#!/usr/bin/perl -w #WINNT #!d:\perl\bin\perl.exe -w # # Purpose: # Convert a certnumb,lastname,email .csv to .htpasswd and .htgroup # # Summary: # The "user name" will be the certificate number, and the "password" will be # the email address. The last name is used to salt the crypt() function. # # Notes: # * The email (and last name) may be used by more than one member of a # family, so it can not be considered unique. The cert. numb. is, so we # neccessarily must use it for the user name. # * A half-hearted attempt at guessing the input list file's format will be # made. Supported formats are 3 (certnumb,lastname,email), 4 (same, but with # firstname in there) and 58 (standard newXXX.dat format). # # Usage : csv2htpasswd.pl inputfile # Questions : charger67@lycos.com # Revision : 15 October 2001 - remove all " from all input fields # # Copyright : 2001 - ITCom, United States Power Squadrons, Inc. # # ---------------------------------------------------------------------- use strict; require 5.001; # ---------------------------------------------------------------------- my( $inpfile, $htpasswdfile, $htgroupfile, ); $htpasswdfile = "./.htpasswd"; $htgroupfile = "./.htgroup"; # ---------------------------------------------------------------------- # check to see if the correct number of arguments was passed in if (@ARGV == 1) { $inpfile = $ARGV[0]; } else { print("usage: $0 inputfile\n"); exit; } # open the certnum,lastname,email .csv file for reading open(INPFILE, "<$inpfile") || die("can not read $inpfile"); # open the htpasswd file open(PWDFILE, ">$htpasswdfile") || die("can not write $htpasswdfile"); # open the htgroup file open(GRPFILE, ">$htgroupfile") || die("can not write $htgroupfile"); # the allowed group name (must match what's in .htaccess) print(GRPFILE "squadronmembers: "); # while there are lines in the input file... while () { chomp; # clean up # local vars my($certnumb, $lastname, $emailadd); # split the fields up at the comma (,) # maybe add a TAB, maybe something else my(@elements) = split/,/; # figure out what kind of file was put in place # certnumb, lastname, emailadd if (@elements == 3) { $certnumb = $elements[0]; $lastname = $elements[1]; $emailadd = $elements[2]; } # certnumb, firstname, lastname, emailadd elsif (@elements == 4) { $certnumb = $elements[0]; $lastname = $elements[2]; $emailadd = $elements[3]; } # standard USPS format (newXXX.dat) elsif (@elements == 58) { $certnumb = $elements[0]; $lastname = $elements[1]; $emailadd = $elements[25]; } # none of the above, choke on it else { print("ERROR: line $elements[0] appears to be unsupported\n"); $certnumb = "-garbage-"; $lastname = "-garbage-"; $emailadd = "-garbage-"; } $certnumb =~ s/\"//g; $lastname =~ s/\"//g; $emailadd =~ s/\"//g; # check to see if the email address is valid (@ and . in right place) if ($emailadd =~ /\w+@\w+\.\w+/) { # now fix the data $certnumb = uc($certnumb); # cert. numb. is always uppercase $lastname = uc($lastname); # last name is now uppercase $emailadd = lc($emailadd); # email is always lowercase # write it out - encrypted print(PWDFILE $certnumb . ":" . crypt($emailadd, $lastname) . "\n"); # tack on the user name to the allowed group print(GRPFILE $certnumb . " "); # NO \n here # say something... print("ADDED: username=$certnumb password=$emailadd\n"); } # if valid email } # while # close htgroup file print(GRPFILE "\n\n"); close(GRPFILE); # close htpasswd file print(PWDFILE "\n"); close(PWDFILE); # close input file close(INPFILE); # all done exit; #EndOfFile