Sort of a solution for people with a dynamic IP...

The problem

You have a dynamic IP but telling people to go to http://foo.dyndns.org/bar/ just plain sucks. You also have lots of domains (like we all do) and might want to use them (www.r0x0r.org) instead.

Why would I want such a thing?

Sometimes, you want people to be able to access some services like HTTP of your computer but that's it. No sniffing around, no... nothing! But giving them your IP might trigger stupid things. Men are stupid by design.

And what's the... uh... solution?

Not long ago, a customer wanted a subdomainservice, so I wrote a really dirty combination of some scripts which I want to publish here. They are an initial subdomain-creator, a script to update a subdomain's IP, a .htaccess and an index.php to do the rewriting.

<?

$sSub = substr($_SERVER["HTTP_HOST"], 0, -10);

$oDB = dbmopen("/www/reidel.info/webseiten/reidel.cc/subdomains.db", "r");
$sTarget = dbmfetch($oDB, $sSub);
list($sTarget, $sPassword) = split(" ", $sTarget);
if ( $sTarget ) {
  @readfile("$sTarget".$_SERVER["PATH_INFO"]);
  exit;
}

echo("Subdomain ".$_SERVER["HTTP_HOST"]." not defined yet");

?>

The .htaccess

RewriteEngine On
RewriteRule !^index.php /index.php%{REQUEST_URI} [L]

The index.php

<?

$sDomain = "reidel.cc"
$sSub = substr($_SERVER["HTTP_HOST"], 0, -(strlen($sDomain)+1));

$oDB = dbmopen("/path/to/dbm/subdomains.db", "r");
$sTarget = dbmfetch($oDB, $sSub); 

if ( $sTarget ) {
  list($sTarget, $sPassword) = split(" ", $sTarget);
  @readfile("$sTarget".$_SERVER["PATH_INFO"]);
  exit;
}

echo("Subdomain ".$_SERVER["HTTP_HOST"]." not defined yet");

?>

A sample subdomains.map file

mark    http://1.2.3.4/r0x0r    markspass
frank   http://5.6.7.8/uh/yeah  frankspassword
jack    http://www.kernel.org   yeahandjacks

A sample converter to get your subdomain.db created out of the subdomain.map

<?

$oMap = fopen("/path/to/dbm/subdomains.map", "r");
$oDB = dbmopen("/path/to/dbm/subdomains.db", "n");
while ($sLine = fgets($oMap)) {
  list($sKey, $sValue, $sPassword) = preg_split("/\s+/", $sLine);
  echo("Inserting $sKey:$sValue<br>\n");
  dbminsert($oDB, $sKey, "$sValue $sPassword");
}
dbmclose($oDB);
fclose($oMap);

?>

A possible script to call when the IP has changed:

<?

$oDB = dbmopen("/path/to/dbm/subdomains.db", "rw");
$sTarget = dbmfetch($oDB, $_REQUEST["subdomain"]); 

if ( !$sTarget ) {
  sleep 10;
  die();
}
list($sTarget, $sPassword) = split(" ", $sTarget);
if ( $_REQUEST["password"] != $sPassword ) {
  sleep 10;
  die();
}
dbmreplace($oDB, $_REQUEST["subdomain"],
           ($_REQUEST["url"]
               ? (preg_match("/http:\/\//", $_REQUEST["url"])
                   ? $_REQUEST["url"]
                   : "http://".$_REQUEST["url"])
               : "http://".$_SERVER["REMOTE_ADDR"]).
           " $sPassword");
dbmclose($oDB);
echo("Changes successful.<br>");

?>


Last modified: 26.07.2006 [01:38:23]    042418122817@mark.reidel.info