Archive for January, 2008

Talking to SMTP / Mail via telnet to diagnoze mail problems on a server

Mailserver diagnoses can be tricky: I personally find the easiest way to do it still using telnet in combination with the server logs to find out what exactly is wrong.

It is very easy to do:

telnet thehost.com 25

helo: myhost.com
mail from: me@myhost.com
rcpt to: someone@thehost.com
data

… type some data …
.

quit

That is all; it will help to exactly determine where it goes wrong.

The case we had today;

– postfix didn’t send mail ; it gave connection refused
– thoughts; firewall, network, dns etc.
– we tried the above and it worked fine, meaning postfix was the one refusing, not the network/firewall etc
– we fixed the postfix config and all was fine

PHP Posting to ASP.NET (ASPX) pages / sites

If you want to post anything (for instance a login/password), you need to take the viewstate in order to be able to post anything to any aspx page, so request the page for the first time, in the same session:


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, array());
$data = curl_exec($ch);

Then you collect the viewstate;


$vars = array("login"=>$user,
"password"=>$pass);

$i = strpos($data, "__VIEWSTATE");
$i = strpos($data, "value=", $i);
$i += 7;
$j = strpos($data, """, $i+1);
$viewstate = substr($data, $i, $j-$i);
$vars['__VIEWSTATE'] = $viewstate;

After that you can safely login to the site;


curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
$data = curl_exec($ch)