Matt
12-15-2009, 10:49 PM
Following is code that we have used to automate the management of e-mail accounts on FutureQuest. Additional information in a follow-up post.
<?php
// Include settings
include_once("../settings.php");
global $root;
global $domain;
global $content;
global $cnc_user;
global $cnc_pass;
global $base_path;
global $url_path;
global $cnc_url;
global $used_pops;
global $total_pops;
// ----------------------------------------------------------
// Functions responsible for checking password validity
// ----------------------------------------------------------
function isAlphaAndNumeric($input)
{
if (preg_match('/[a-zA-Z]/', $input) && preg_match('/[0-9]/', $input))
return 1;
else
return 0;
}
function isEmailValid($input)
{
if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $input))
return 0;
else
return 1;
}
function isPasswordValid($input)
{
// Verify password is valid length
if (strlen($input)<8 || strlen($input)>20)
{
return "Password must be 8 to 20 characters in length";
}
// New passwords cannot have leading or trailing spaces
if ($input != trim($input))
{
return "Password cannot contain leading or trailing spaces";
}
// If password is 8 characters, cannot contain spaces
if ((strlen($input)==8) && (strstr($input," ")))
{
return "Eight character passwords cannot contain spaces";
}
// Password cannot contain two or more consecutive spaces
if (strstr($input," "))
{
return "Password cannot contain two or more consecutive spaces";
}
// Check that the new password contains both letters and digits
if (!isAlphaAndNumeric($input))
{
return "Password must contain letters and at least one number";
}
// Control characters and `|() not allowed in password
if (!preg_match('/[\x00-\x1F\x7F`|()]/', '', $input))
{
return "valid";
}
else
{
return "Password contains invalid characters";
}
}
function getErrorString($data)
{
// Errors are delimted as follows: <blockquote>Some error text</blockquote>
$find_str = '<blockquote>';
$str_len = strlen($find_str);
$location = strpos($data, $find_str);
if ($location === false)
{
return "An error occurred";
}
$counter = 0;
$char = $data[$location + $str_len + $counter];
$value = '';
while ($char != '<')
{
$value .= $char;
$counter++;
$char = $data[$location + $str_len + $counter];
}
return $value;
}
function initializeEmail()
{
global $root;
global $domain;
global $base_path;
global $cnc_url;
global $content;
global $cnc_user;
global $cnc_pass;
global $url_path;
global $used_pops;
global $total_pops;
$errors = '';
//echo "cnc_user = $cnc_user <br />";
//echo "cnc_pass = $cnc_pass <br />";
//echo "url_path = $url_path <br />";
$cnc_url = 'http://' . $cnc_user . ':' . $cnc_pass . '@' . $url_path . '/CNC/emailmgr.cgi';
//echo "cnc_url = $cnc_url <br />";
$fp = @fopen($cnc_url, 'r')
or die("Error opening CNC");
while ($line = @fgets($fp, 1024))
{
$content .= "$line";
}
fclose($fp);
$content = strtolower(strip_tags($content));
// Initialize # of POP3 accounts available and taken
// Delimiters.. these may change if FQ changes CNC text
$begin = "pop3 mailboxes (";
$end = " allowed)";
$separator = " of ";
// \Q = quote (disable) pattern metacharacters till \E
if (preg_match("/\Q" . $begin . "\E.*?\Q" . $end . "\E/", $content, $array))
{
// Retrieve the string: pop3 mailboxes (x of y allowed
//echo $array[0] . '<br />';
$pop3_str = $array[0];
$pop3_str = str_replace($begin, "", $pop3_str);
//echo $pop3_str . '<br />';
$pop3_str = str_replace($end, "", $pop3_str);
//echo $pop3_str . '<br />';
list($used_pops, $total_pops) = split($separator, $pop3_str);
//echo "Used: $used <br />";
//echo "Available: $available <br />";
return $errors;
}
}
function getFormValue($data, $var, $type="hidden")
{
// <input type="hidden" name="uniqueid" value="PsALcD@XcAMAABJ4Uv0228561052773233">
if ($type == "hidden")
{
$find_str = '<input type="hidden" name="' . $var . '" value="';
}
elseif ($type == "text")
{
$find_str = '<input type="text" class="text" name="' . $var . '" value="';
}
else
{
// Unknown type specified
return "";
}
$str_len = strlen($find_str);
$location = strpos($data, $find_str);
if ($location === false)
{
return '';
}
$counter = 0;
$char = $data[$location + $str_len + $counter];
$value = '';
while ($char != '"')
{
$value .= $char;
$counter++;
$char = $data[$location + $str_len + $counter];
}
return $value;
}
function getEmailUsername()
{
global $cnc_user;
return $cnc_user;
}
function getPOPAccountsTotal()
{
global $total_pops;
return $total_pops;
}
function getPOPAccountsTaken()
{
global $used_pops;
return $used_pops;
}
function getPOPAccountsAvailable()
{
global $total_pops;
global $used_pops;
return $total_pops - $used_pops;
}
function getPOP3ServerName()
{
global $domain;
return 'pop.' . $domain;
}
function getSMTPServerName()
{
global $domain;
return 'mail.' . $domain;
}
function doCreatePOPAccount($user, $pass1, $pass2)
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// Verify that requested username is valid
if (!isUsernameValid($user))
{
return "Username contains invalid characters";
}
// Verify that passwords are valid
if ($pass1 != $pass2)
{
return "Passwords do not match";
}
$result = isPasswordValid($pass1);
if ($result != "valid")
{
return $result;
}
// Step 1: Request new POP user
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("username" => $cnc_user,
"name" => $user,
"root" => $root,
"script" => "emailmgr",
"version" => "3.0",
"domain" => $domain,
"xdom" => 'x' . $cnc_user,
"referer" => $cnc_url,
"do" => "addpop");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 2: Check whether the requested username is available
if (strpos($data, "You already have an account set up for") === false)
{
// Username is available, so get identifier from:
// <input type="hidden" name="uniqueid" value="PsALcD@XcAMAABJ4Uv0228561052773233">
$identifier = getFormValue($data, "uniqueid", "hidden");
if ($identifier <= '')
{
return "Couldn't retrieve uniqueid";
}
}
else
{
return "This username has been taken";
}
// Step 3: Attempt to create account
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("name" => $user,
"pass1" => $pass1,
"pass2" => $pass2,
"uniqueid" => $identifier,
"do" => "createpop");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 4: Verify that account was successfully created
if (strpos($data, "Mailbox Created") === false)
{
return "Mailbox creation unsuccessful";
}
else
{
return "success";
}
}
function doCreateForwardingAccount($user, $email1='', $email2='', $email3='', $email4='', $email5='', $email6='', $email7='', $email8='', $email9='', $email10='')
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// Verify that requested username is valid
if (!isUsernameValid($user))
{
return "Username contains invalid characters";
}
if (((strlen($email1) > 0) && (!isEmailValid($email1))) || ((strlen($email2) > 0) && (!isEmailValid($email2))) || ((strlen($email3) > 0) && (!isEmailValid($email3))) || ((strlen($email4) > 0) && (!isEmailValid($email4))) || ((strlen($email5) > 0) && (!isEmailValid($email5))) || ((strlen($email6) > 0) && (!isEmailValid($email6))) || ((strlen($email7) > 0) && (!isEmailValid($email7))) || ((strlen($email8) > 0) && (!isEmailValid($email8))) || ((strlen($email9) > 0) && (!isEmailValid($email9))) || ((strlen($email10) > 0) && (!isEmailValid($email10))))
{
return "E-mail address(es) invalid.";
}
// Step 1: Request new forwarding user
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("username" => $cnc_user,
"name" => $user,
"root" => $root,
"script" => "emailmgr",
"version" => "3.0",
"domain" => $domain,
"xdom" => 'x' . $cnc_user,
"referer" => $cnc_url,
"do" => "addalias");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 2: Check whether the requested username is available
if (strpos($data, "You already have an account set up for") === false)
{
// Username is available, so get identifier from:
// <input type="hidden" name="uniqueid" value="PsALcD@XcAMAABJ4Uv0228561052773233">
$identifier = getFormValue($data, "uniqueid", "hidden");
if ($identifier <= '')
{
return "Couldn't retrieve uniqueid";
}
}
else
{
return "This username has been taken";
}
// Step 3: Attempt to create account
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("name" => $user,
"uniqueid" => $identifier,
"do" => "createalias",
"TO_1" => "$email1",
"TO_2" => "$email2",
"TO_3" => "$email3",
"TO_4" => "$email4",
"TO_5" => "$email5",
"TO_6" => "$email6",
"TO_7" => "$email7",
"TO_8" => "$email8",
"TO_9" => "$email9",
"TO_10" => "$email10",
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 4: Verify that account was successfully created
if (strpos($data, "Alias Created") === false)
{
return "Mailbox creation unsuccessful";
//echo $data;
}
else
{
return "success";
}
}
function doRemovePOPAccount($user, $email='')
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// Step 1: Load delete form
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("name" => $user,
"DELETE" => " Delete Box ",
"do" => "edit");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
// echo $data;
$identifier = getFormValue($data, "uniqueid", "hidden");
if ($identifier <= '')
{
die("Couldn't retrieve uniqueid");
}
// Step 2: Attempt to delete account
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
if (strlen($email) > 0)
{
$forward = '1';
}
else
{
$forward = '0';
}
$submit = array("name" => $user,
"CONFIRM" => "1",
"uniqueid" => $identifier,
"forward_existing" => $forward,
"forward" => $email,
"do" => "delete");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
//echo $data;
// Step 4: Verify that account was successfully created
if (strpos($data, "Invalid forwarding address."))
{
die("Invalid forwarding address");
}
if (strpos($data, "Mailbox Deleted") || strpos($data, "Alias Deleted"))
{
return "success";
}
else
{
die("Error deleting mailbox");
}
}
function doChangePOPPass($accountname, $oldpass, $newpass, $newpass2)
{
// Verify that passwords are valid
if ($newpass != $newpass2)
{
return "Passwords do not match";
}
$result = isPasswordValid($newpass);
if ($result != "valid")
{
return $result;
}
// ----------------------------------------------------------
// Now verify that username and old password authenticate
// ----------------------------------------------------------
$command = "vauthenticate '$accountname'";
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$process = proc_open($command, $descriptorspec, $pipes);
if (is_resource($process))
{
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fwrite($pipes[0], $oldpass . "\n");
fclose($pipes[0]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
if ($return_value)
{
return "Password stored in database is corrupted. Password update failed.";
}
}
// ----------------------------------------------------------
// Username/ old password authenticated; change to new password
// ----------------------------------------------------------
$command = "vpasswd '$accountname'";
$process = proc_open($command, $descriptorspec, $pipes);
if (is_resource($process))
{
fwrite($pipes[0], $newpass . "\n");
fclose($pipes[0]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
if (!$return_value)
{
return "success";
}
else
{
return "Password change unsuccessful";
}
}
}
function doFilterExecutables($user, $filter)
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $filter is either 1=true or 0=false
// If true, enable Executable Filter
// If false, disable
// Desired action: delete, bounce, or forward
$action = "bounce";
$bounce_msg = "";
$recipient = "";
if ($action == "bounce")
{
$bounce_msg = "The specified recipient does not accept executable attachments";
}
elseif ($action == "forward")
{
// Specify desired recipient here:
$recipient = "";
}
// Step 1: Load executable filter form
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("name" => $user,
"filter" => "ea",
"do" => "editfilterbuiltin");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
//echo $data;
// Step 2: Attempt to delete account
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "savefilterbuiltin",
"filter" => "ea",
"name" => $user,
"filterenable" => $filter,
"action" => $action,
"bounce" => $bounce_msg,
"redirect" => $recipient);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
//echo $data;
// Step 4: Verify that account was successfully created
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
die("Error setting filter");
}
}
function doSpamProtection($user, $filter, $action="tag")
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $filter is either 1=true or 0=false
// If true, enable Executable Filter
// If false, disable
// Currently only support "tag" and "delete"
// $action can be "delete", "bounce", "redirect", or "tag",
// Step 1: Load spam assassin filter form
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "editfiltersa",
"name" => $user);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
$subject_tag = getFormValue($data, "subject_tag", "text");
$score = getFormValue($data, "filter_4", "text");
//echo $data;
// Step 2: Attempt to activate/ deactivate spam assassin
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "savefiltersa",
"name" => $user,
"type_1" => $filter,
"action" => $action,
"subject_tag" => $subject_tag,
"filter_4" => $score);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
if (strpos($data, "an error has occurred"))
{
echo getErrorString($data);
}
//echo $data;
// Step 4: Verify that requested settings were made
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
if ($action=="delete")
{
// FQ has added a confirmation step here that we have to deal with
// Get all the invisible values from confirmation form:
$uniqueid = getFormValue($data, "uniqueid", "hidden");
$name = getFormValue($data, "name", "hidden");
$type_1 = getFormValue($data, "type_1", "hidden");
$do = getFormValue($data, "do", "hidden");
$filter_1 = getFormValue($data, "filter_1", "hidden");
$filter_2 = getFormValue($data, "filter_2", "hidden");
$filter_3 = getFormValue($data, "filter_3", "hidden");
$filter_4 = getFormValue($data, "filter_4", "hidden");
$subject_tag = getFormValue($data, "subject_tag", "hidden");
$action = getFormValue($data, "action", "hidden");
$numberofboxes = getFormValue($data, "numberofboxes", "hidden");
// Now fill array with values
$submit = array("uniqueid" => $uniqueid,
"name" => $name,
"type_1" => $type_1,
"do" => $do,
"filter_1" => $filter_1,
"filter_2" => $filter_2,
"filter_3" => $filter_3,
"filter_4" => $filter_4,
"subject_tag" => $subject_tag,
"action" => $action,
"numberofboxes" => $numberofboxes);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
}
function doVirusProtection($user, $filter, $action="tag")
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $filter is either 1=true or 0=false
// If true, enable Executable Filter
// If false, disable
if (($filter != 0) && ($filter != 1))
$filter = 0;
// Currently support "tag" and "delete" for $action
if (($action != "tag") && ($action != "delete"))
$action = "tag";
// Step 1: Submit virus protection preferences
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "savefilterbuiltin",
"filter" => "virus",
"name" => $user,
"filterenable" => $filter,
"action" => $action,
"includelines" => "",
"prefixsubject" => "*{virus detected}*");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
if (strpos($data, "an error has occurred"))
{
echo getErrorString($data);
}
//echo $data;
// Verify that requested settings were made
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
if ($action=="delete")
{
// FQ has added a confirmation step here that we have to deal with
// Get all the invisible values from confirmation form:
$uniqueid = getFormValue($data, "uniqueid", "hidden");
$name = getFormValue($data, "name", "hidden");
$do = getFormValue($data, "do", "hidden");
$filterenable = getFormValue($data, "filterenable", "hidden");
$action = getFormValue($data, "action", "hidden");
$filter = getFormValue($data, "filter", "hidden");
// Now fill array with values
$submit = array("uniqueid" => $uniqueid,
"name" => $name,
"do" => $do,
"filterenable" => $filterenable,
"action" => $action,
"filter" => $filter);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
}
function doEmailForwarding($user, $enable, $recipient)
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $enable is either 1=true or 0=false
// If true, enable forwarding
// If false, disable
if (($enable != 0) && ($enable != 1))
$enable = 0;
}
?>
<?php
// Include settings
include_once("../settings.php");
global $root;
global $domain;
global $content;
global $cnc_user;
global $cnc_pass;
global $base_path;
global $url_path;
global $cnc_url;
global $used_pops;
global $total_pops;
// ----------------------------------------------------------
// Functions responsible for checking password validity
// ----------------------------------------------------------
function isAlphaAndNumeric($input)
{
if (preg_match('/[a-zA-Z]/', $input) && preg_match('/[0-9]/', $input))
return 1;
else
return 0;
}
function isEmailValid($input)
{
if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $input))
return 0;
else
return 1;
}
function isPasswordValid($input)
{
// Verify password is valid length
if (strlen($input)<8 || strlen($input)>20)
{
return "Password must be 8 to 20 characters in length";
}
// New passwords cannot have leading or trailing spaces
if ($input != trim($input))
{
return "Password cannot contain leading or trailing spaces";
}
// If password is 8 characters, cannot contain spaces
if ((strlen($input)==8) && (strstr($input," ")))
{
return "Eight character passwords cannot contain spaces";
}
// Password cannot contain two or more consecutive spaces
if (strstr($input," "))
{
return "Password cannot contain two or more consecutive spaces";
}
// Check that the new password contains both letters and digits
if (!isAlphaAndNumeric($input))
{
return "Password must contain letters and at least one number";
}
// Control characters and `|() not allowed in password
if (!preg_match('/[\x00-\x1F\x7F`|()]/', '', $input))
{
return "valid";
}
else
{
return "Password contains invalid characters";
}
}
function getErrorString($data)
{
// Errors are delimted as follows: <blockquote>Some error text</blockquote>
$find_str = '<blockquote>';
$str_len = strlen($find_str);
$location = strpos($data, $find_str);
if ($location === false)
{
return "An error occurred";
}
$counter = 0;
$char = $data[$location + $str_len + $counter];
$value = '';
while ($char != '<')
{
$value .= $char;
$counter++;
$char = $data[$location + $str_len + $counter];
}
return $value;
}
function initializeEmail()
{
global $root;
global $domain;
global $base_path;
global $cnc_url;
global $content;
global $cnc_user;
global $cnc_pass;
global $url_path;
global $used_pops;
global $total_pops;
$errors = '';
//echo "cnc_user = $cnc_user <br />";
//echo "cnc_pass = $cnc_pass <br />";
//echo "url_path = $url_path <br />";
$cnc_url = 'http://' . $cnc_user . ':' . $cnc_pass . '@' . $url_path . '/CNC/emailmgr.cgi';
//echo "cnc_url = $cnc_url <br />";
$fp = @fopen($cnc_url, 'r')
or die("Error opening CNC");
while ($line = @fgets($fp, 1024))
{
$content .= "$line";
}
fclose($fp);
$content = strtolower(strip_tags($content));
// Initialize # of POP3 accounts available and taken
// Delimiters.. these may change if FQ changes CNC text
$begin = "pop3 mailboxes (";
$end = " allowed)";
$separator = " of ";
// \Q = quote (disable) pattern metacharacters till \E
if (preg_match("/\Q" . $begin . "\E.*?\Q" . $end . "\E/", $content, $array))
{
// Retrieve the string: pop3 mailboxes (x of y allowed
//echo $array[0] . '<br />';
$pop3_str = $array[0];
$pop3_str = str_replace($begin, "", $pop3_str);
//echo $pop3_str . '<br />';
$pop3_str = str_replace($end, "", $pop3_str);
//echo $pop3_str . '<br />';
list($used_pops, $total_pops) = split($separator, $pop3_str);
//echo "Used: $used <br />";
//echo "Available: $available <br />";
return $errors;
}
}
function getFormValue($data, $var, $type="hidden")
{
// <input type="hidden" name="uniqueid" value="PsALcD@XcAMAABJ4Uv0228561052773233">
if ($type == "hidden")
{
$find_str = '<input type="hidden" name="' . $var . '" value="';
}
elseif ($type == "text")
{
$find_str = '<input type="text" class="text" name="' . $var . '" value="';
}
else
{
// Unknown type specified
return "";
}
$str_len = strlen($find_str);
$location = strpos($data, $find_str);
if ($location === false)
{
return '';
}
$counter = 0;
$char = $data[$location + $str_len + $counter];
$value = '';
while ($char != '"')
{
$value .= $char;
$counter++;
$char = $data[$location + $str_len + $counter];
}
return $value;
}
function getEmailUsername()
{
global $cnc_user;
return $cnc_user;
}
function getPOPAccountsTotal()
{
global $total_pops;
return $total_pops;
}
function getPOPAccountsTaken()
{
global $used_pops;
return $used_pops;
}
function getPOPAccountsAvailable()
{
global $total_pops;
global $used_pops;
return $total_pops - $used_pops;
}
function getPOP3ServerName()
{
global $domain;
return 'pop.' . $domain;
}
function getSMTPServerName()
{
global $domain;
return 'mail.' . $domain;
}
function doCreatePOPAccount($user, $pass1, $pass2)
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// Verify that requested username is valid
if (!isUsernameValid($user))
{
return "Username contains invalid characters";
}
// Verify that passwords are valid
if ($pass1 != $pass2)
{
return "Passwords do not match";
}
$result = isPasswordValid($pass1);
if ($result != "valid")
{
return $result;
}
// Step 1: Request new POP user
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("username" => $cnc_user,
"name" => $user,
"root" => $root,
"script" => "emailmgr",
"version" => "3.0",
"domain" => $domain,
"xdom" => 'x' . $cnc_user,
"referer" => $cnc_url,
"do" => "addpop");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 2: Check whether the requested username is available
if (strpos($data, "You already have an account set up for") === false)
{
// Username is available, so get identifier from:
// <input type="hidden" name="uniqueid" value="PsALcD@XcAMAABJ4Uv0228561052773233">
$identifier = getFormValue($data, "uniqueid", "hidden");
if ($identifier <= '')
{
return "Couldn't retrieve uniqueid";
}
}
else
{
return "This username has been taken";
}
// Step 3: Attempt to create account
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("name" => $user,
"pass1" => $pass1,
"pass2" => $pass2,
"uniqueid" => $identifier,
"do" => "createpop");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 4: Verify that account was successfully created
if (strpos($data, "Mailbox Created") === false)
{
return "Mailbox creation unsuccessful";
}
else
{
return "success";
}
}
function doCreateForwardingAccount($user, $email1='', $email2='', $email3='', $email4='', $email5='', $email6='', $email7='', $email8='', $email9='', $email10='')
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// Verify that requested username is valid
if (!isUsernameValid($user))
{
return "Username contains invalid characters";
}
if (((strlen($email1) > 0) && (!isEmailValid($email1))) || ((strlen($email2) > 0) && (!isEmailValid($email2))) || ((strlen($email3) > 0) && (!isEmailValid($email3))) || ((strlen($email4) > 0) && (!isEmailValid($email4))) || ((strlen($email5) > 0) && (!isEmailValid($email5))) || ((strlen($email6) > 0) && (!isEmailValid($email6))) || ((strlen($email7) > 0) && (!isEmailValid($email7))) || ((strlen($email8) > 0) && (!isEmailValid($email8))) || ((strlen($email9) > 0) && (!isEmailValid($email9))) || ((strlen($email10) > 0) && (!isEmailValid($email10))))
{
return "E-mail address(es) invalid.";
}
// Step 1: Request new forwarding user
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("username" => $cnc_user,
"name" => $user,
"root" => $root,
"script" => "emailmgr",
"version" => "3.0",
"domain" => $domain,
"xdom" => 'x' . $cnc_user,
"referer" => $cnc_url,
"do" => "addalias");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 2: Check whether the requested username is available
if (strpos($data, "You already have an account set up for") === false)
{
// Username is available, so get identifier from:
// <input type="hidden" name="uniqueid" value="PsALcD@XcAMAABJ4Uv0228561052773233">
$identifier = getFormValue($data, "uniqueid", "hidden");
if ($identifier <= '')
{
return "Couldn't retrieve uniqueid";
}
}
else
{
return "This username has been taken";
}
// Step 3: Attempt to create account
$ch = curl_init($cnc_url);
if (!$ch)
{
return "Couldn't make connection to script";
}
else
{
$submit = array("name" => $user,
"uniqueid" => $identifier,
"do" => "createalias",
"TO_1" => "$email1",
"TO_2" => "$email2",
"TO_3" => "$email3",
"TO_4" => "$email4",
"TO_5" => "$email5",
"TO_6" => "$email6",
"TO_7" => "$email7",
"TO_8" => "$email8",
"TO_9" => "$email9",
"TO_10" => "$email10",
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
return "No data returned";
}
}
// Step 4: Verify that account was successfully created
if (strpos($data, "Alias Created") === false)
{
return "Mailbox creation unsuccessful";
//echo $data;
}
else
{
return "success";
}
}
function doRemovePOPAccount($user, $email='')
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// Step 1: Load delete form
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("name" => $user,
"DELETE" => " Delete Box ",
"do" => "edit");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
// echo $data;
$identifier = getFormValue($data, "uniqueid", "hidden");
if ($identifier <= '')
{
die("Couldn't retrieve uniqueid");
}
// Step 2: Attempt to delete account
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
if (strlen($email) > 0)
{
$forward = '1';
}
else
{
$forward = '0';
}
$submit = array("name" => $user,
"CONFIRM" => "1",
"uniqueid" => $identifier,
"forward_existing" => $forward,
"forward" => $email,
"do" => "delete");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
//echo $data;
// Step 4: Verify that account was successfully created
if (strpos($data, "Invalid forwarding address."))
{
die("Invalid forwarding address");
}
if (strpos($data, "Mailbox Deleted") || strpos($data, "Alias Deleted"))
{
return "success";
}
else
{
die("Error deleting mailbox");
}
}
function doChangePOPPass($accountname, $oldpass, $newpass, $newpass2)
{
// Verify that passwords are valid
if ($newpass != $newpass2)
{
return "Passwords do not match";
}
$result = isPasswordValid($newpass);
if ($result != "valid")
{
return $result;
}
// ----------------------------------------------------------
// Now verify that username and old password authenticate
// ----------------------------------------------------------
$command = "vauthenticate '$accountname'";
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$process = proc_open($command, $descriptorspec, $pipes);
if (is_resource($process))
{
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fwrite($pipes[0], $oldpass . "\n");
fclose($pipes[0]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
if ($return_value)
{
return "Password stored in database is corrupted. Password update failed.";
}
}
// ----------------------------------------------------------
// Username/ old password authenticated; change to new password
// ----------------------------------------------------------
$command = "vpasswd '$accountname'";
$process = proc_open($command, $descriptorspec, $pipes);
if (is_resource($process))
{
fwrite($pipes[0], $newpass . "\n");
fclose($pipes[0]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
if (!$return_value)
{
return "success";
}
else
{
return "Password change unsuccessful";
}
}
}
function doFilterExecutables($user, $filter)
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $filter is either 1=true or 0=false
// If true, enable Executable Filter
// If false, disable
// Desired action: delete, bounce, or forward
$action = "bounce";
$bounce_msg = "";
$recipient = "";
if ($action == "bounce")
{
$bounce_msg = "The specified recipient does not accept executable attachments";
}
elseif ($action == "forward")
{
// Specify desired recipient here:
$recipient = "";
}
// Step 1: Load executable filter form
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("name" => $user,
"filter" => "ea",
"do" => "editfilterbuiltin");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
//echo $data;
// Step 2: Attempt to delete account
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "savefilterbuiltin",
"filter" => "ea",
"name" => $user,
"filterenable" => $filter,
"action" => $action,
"bounce" => $bounce_msg,
"redirect" => $recipient);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
//echo $data;
// Step 4: Verify that account was successfully created
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
die("Error setting filter");
}
}
function doSpamProtection($user, $filter, $action="tag")
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $filter is either 1=true or 0=false
// If true, enable Executable Filter
// If false, disable
// Currently only support "tag" and "delete"
// $action can be "delete", "bounce", "redirect", or "tag",
// Step 1: Load spam assassin filter form
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "editfiltersa",
"name" => $user);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
$subject_tag = getFormValue($data, "subject_tag", "text");
$score = getFormValue($data, "filter_4", "text");
//echo $data;
// Step 2: Attempt to activate/ deactivate spam assassin
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "savefiltersa",
"name" => $user,
"type_1" => $filter,
"action" => $action,
"subject_tag" => $subject_tag,
"filter_4" => $score);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
if (strpos($data, "an error has occurred"))
{
echo getErrorString($data);
}
//echo $data;
// Step 4: Verify that requested settings were made
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
if ($action=="delete")
{
// FQ has added a confirmation step here that we have to deal with
// Get all the invisible values from confirmation form:
$uniqueid = getFormValue($data, "uniqueid", "hidden");
$name = getFormValue($data, "name", "hidden");
$type_1 = getFormValue($data, "type_1", "hidden");
$do = getFormValue($data, "do", "hidden");
$filter_1 = getFormValue($data, "filter_1", "hidden");
$filter_2 = getFormValue($data, "filter_2", "hidden");
$filter_3 = getFormValue($data, "filter_3", "hidden");
$filter_4 = getFormValue($data, "filter_4", "hidden");
$subject_tag = getFormValue($data, "subject_tag", "hidden");
$action = getFormValue($data, "action", "hidden");
$numberofboxes = getFormValue($data, "numberofboxes", "hidden");
// Now fill array with values
$submit = array("uniqueid" => $uniqueid,
"name" => $name,
"type_1" => $type_1,
"do" => $do,
"filter_1" => $filter_1,
"filter_2" => $filter_2,
"filter_3" => $filter_3,
"filter_4" => $filter_4,
"subject_tag" => $subject_tag,
"action" => $action,
"numberofboxes" => $numberofboxes);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
}
function doVirusProtection($user, $filter, $action="tag")
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $filter is either 1=true or 0=false
// If true, enable Executable Filter
// If false, disable
if (($filter != 0) && ($filter != 1))
$filter = 0;
// Currently support "tag" and "delete" for $action
if (($action != "tag") && ($action != "delete"))
$action = "tag";
// Step 1: Submit virus protection preferences
$ch = curl_init($cnc_url);
if (!$ch)
{
die ("Couldn't make connection to script");
}
else
{
$submit = array("do" => "savefilterbuiltin",
"filter" => "virus",
"name" => $user,
"filterenable" => $filter,
"action" => $action,
"includelines" => "",
"prefixsubject" => "*{virus detected}*");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (!$data)
{
die("No data returned");
}
}
if (strpos($data, "an error has occurred"))
{
echo getErrorString($data);
}
//echo $data;
// Verify that requested settings were made
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
if ($action=="delete")
{
// FQ has added a confirmation step here that we have to deal with
// Get all the invisible values from confirmation form:
$uniqueid = getFormValue($data, "uniqueid", "hidden");
$name = getFormValue($data, "name", "hidden");
$do = getFormValue($data, "do", "hidden");
$filterenable = getFormValue($data, "filterenable", "hidden");
$action = getFormValue($data, "action", "hidden");
$filter = getFormValue($data, "filter", "hidden");
// Now fill array with values
$submit = array("uniqueid" => $uniqueid,
"name" => $name,
"do" => $do,
"filterenable" => $filterenable,
"action" => $action,
"filter" => $filter);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (strpos($data, "Settings Saved"))
{
return "success";
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
else
{
reportError($data);
die("Error setting filter. Problem has been reported! <a href=\"index.php\">Back to Module</a>");
}
}
}
function doEmailForwarding($user, $enable, $recipient)
{
global $cnc_url;
global $cnc_user;
global $cnc_pass;
global $root;
global $domain;
// $enable is either 1=true or 0=false
// If true, enable forwarding
// If false, disable
if (($enable != 0) && ($enable != 1))
$enable = 0;
}
?>