#!/usr/bin/perl
# This Program was written by Lennart Hansen (lennart@edb.ihnykf.dk)
 
$user_file = "/usr/local/squid/etc/user");
$passwd_file = "/usr/local/squid/etc/passwd");
$squid = "/usr/local/squid/bin/squid";

# Modify the above lines to fit your own configuration.
#
# I hope you enjoy this program, please do NOT modify and code below, unless
# you got my premission, please send feedback to lennart@edb.ihnykf.dk          

$ver = "0.2";

if (-e "$squid") {
} else {
	print("Error !!!! Cannot locate ", $squid, "\n");
    print("Exiting...\n");
    exit(0); 
}
if (-e "$user_file") {
} else {
    print("Error !!!! Cannot locate ", $user_file, "\n");
    print("Exiting...\n");
    exit(0); 
}   
if (-e "$passwd_file") {
} else {
    print("Error !!!! Cannot locate ", $passwd_file, "\n");
    print("Exiting...\n");
    exit(0); 
}   


if ($#ARGV != 1) {
	print ("usage: $0 [user name] [password]\n");
	print ("Version: $ver by Lennart Hansen \(lennart\@edb.ihnykf.dk\)\n");
	exit(0);
}

$user = $ARGV[0];
$password = $ARGV[1];

@saltair= split
  //,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz0123456789./";

sub get_salt {
  srand();
  $iOff = int(rand($#saltair));
  $iOff2 = int(rand($#saltair));
  return join("",$saltair[$iOff],$saltair[$iOff2]);
}

$cpassword = crypt($password,get_salt());

if (-e "$user_file") {
	open(usr_file, ">>$user_file");
		print usr_file ("$user\n");
	close(usr_file);
} else {
	print("Cannot find : ", $user_file, "\n");
	print("Exiting..."); 
	exit(0);
}
if (-e "$passwd_file") {
	open(pas_file, ">>$passwd_file");
		print pas_file ("$user:$cpassword\n");
	close(pas_file);
} else {
	print("Cannot find : ", $passwd_file, "\n");
	print("Exiting..."); 
	exit(0);
}
if (-e "$squid") {
#	system("$squid -k reconfigure");
} else {
	print("Cannot find : ", $squid, "\n");
	print("Exiting...");
	exit(0);
}

