Posted By

lasavior on 05/22/11


Tagged

DNS ip hostname lock


Versions (?)

Hostname class


 / Published in: PHP
 

PHP class to restrict a script to only run on a set ip address. This takes into account a static hostname (via one of the freely available such as dyndns.org) and a travelling ip. See the description and instructions for more information.

  1. <?php
  2.  
  3. //NOTE: YOU SHOULD HAVE A SESSION RUNNING BEFORE CALLING THIS AS A CLASS
  4.  
  5. /*-+-------------------------------------------------------
  6.  |/ Hostname Check
  7.  | Version 2
  8.  | Created: lasavior - 5/22/2011, So-NIK.com
  9.  | Last Modified: 6/3/2011
  10.  |
  11.  | DESCRIPTION:
  12.  | "Limit access of any script to certain IP addresses."
  13.  |
  14.  | For example: i use this script in combination with
  15.  | dyndns.org. I have my residence setup on a static
  16.  | hostname (example.dyndns.org) that way i can always
  17.  | access the script from my place of business. I also
  18.  | have a traveling IP so i can still gain access to
  19.  | the script while im away. I update the database with
  20.  | the traveling IP and revoke it when i leave.
  21.  |
  22.  | LEGAL:
  23.  | This program is free software; you can redistribute
  24.  | it and/or modify it under the terms of the GNU
  25.  | General Public License as published by the Free
  26.  | Software Foundation; either version 2 of the License,
  27.  | or (at your option) any later version.
  28.  |
  29.  | This program is distributed in the hope that it
  30.  | will be useful, but WITHOUT ANY WARRANTY; without
  31.  | even the implied warranty of MERCHANTABILITY or
  32.  | FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33.  | General Public License for more details.
  34.  |
  35.  | You should have received a copy of the GNU General
  36.  | Public License along with this program; if not, write
  37.  | to the Free Software Foundation, Inc., 51 Franklin St,
  38.  | Fifth Floor, Boston, MA 02110-1301 USA
  39.  |\
  40.  *-+-------------------------------------------------------
  41.   :
  42.   : INSTRUCTIONS:
  43.   : ---------------------------
  44.   : 1) Change any settings needed on line 114
  45.   : 2) Add any static host's on line 121
  46.   : 3) For running as a single-file script, see line 86
  47.   :
  48.   : NOTE: It is your responsibility to write a script to
  49.   : update the database with the new traveling IP.
  50.   : Using the examples below, i suggest creating a
  51.   : separate file under password protection to
  52.   : update and block the IP for automation.
  53.   : See: http://snipplr.com/view/54875/updatednsphp/
  54.   :
  55.   :
  56.   : USAGE EXAMPLES:
  57.   : ---------------------------
  58.   : For checking the hostname, use the following example:
  59.   :
  60.   : require_once($_SERVER['DOCUMENT_ROOT'].'/hostname.class.php');
  61.   : $hostname = new hostname();
  62.   : $hostname->checkHostname();
  63.   :
  64.   : For updating the hostname, use the following example:
  65.   :
  66.   : require_once($_SERVER['DOCUMENT_ROOT'].'/hostname.class.php');
  67.   : $hostname = new hostname();
  68.   : $hostname->putCache($_SERVER['REMOTE_ADDR']);
  69.   :
  70.   :
  71.   : SETTINGS: (besides the static settings, these enable changes per instance)
  72.   : ---------------------------
  73.   : $hostname->changeSetting('dbtable', 'SQLite_table_name'); //Note: if the table cant be found, it will be created but not populated
  74.   : $hostname->changeSetting('dbfilename', 'database_filename.extension'); //Changes the SQLite database file location
  75.   : $hostname->changeSetting('useDatabase', 'no'); //Disables using a database. Be sure to populate the ipaddresses!
  76.   : $hostname->changeSetting('static_dns', 'clear'); //Erase all static hostnames. Can be used to create a fresh hostname list for a particular instance
  77.   : $hostname->changeSetting('addHost', 'hostname.dyndns.org'); //Adds new static hostname (does not have to be dyndns.org)
  78.   : $hostname->changeSetting('addHost', '74.125.115.99'); //Adds new static ip to the access list
  79.   : $hostname_error = ($hostname->lastError != '') ? "Error occurred" : NULL; //A simple way to check if a soft error occurred in the hostname script
  80.   :
  81.   : Note: It is recommended that you place the hostname database file in a
  82.   : location that cant be accessed from the outside. Use of an htaccess
  83.   : file with deny privileges to '*.sqdb' files works nicely for me.
  84.   :
  85.   :
  86.   : RUNNING AS A SCRIPT:
  87.   : ---------------------------
  88.   : This script was also built so it can be run as a single-file
  89.   : with no need to setup and use a class. The setup is the same
  90.   : (you still need to change any settings in the script) but
  91.   : you call it a little different. Use the following example:
  92.   :
  93.   : $hostname_runas = 'script';
  94.   : require_once($_SERVER['DOCUMENT_ROOT'].'/hostname.class.php');
  95.   : ---------------------------
  96.   :
  97.   : Thats all! Hope you enjoy this.
  98.   : If you need help, email php.hostname@ my domain
  99.  */
  100.  
  101. class hostname {
  102.  
  103. // initialize class variables
  104. public $lastError = '';
  105. public $errorLog = array();
  106. protected $_SETT = array();
  107. protected $static_users = array();
  108. protected $ip_users = array();
  109.  
  110. // initiate class
  111. public function hostname(){
  112.  
  113. // define initial settings
  114. $this->_SETT = array(
  115. 'dbtable' => 'hostname',
  116. 'dbfilename' => $_SERVER['DOCUMENT_ROOT'].'/hostnames.sqdb',
  117. 'useDatabase' => 'yes'
  118. );
  119.  
  120. // define static hostnames that will always be accessible from
  121. $this->static_users = array(
  122. 'INSERT_DYNDNS_HERE', //Note: not required to use dyndns.org, any hostname will do
  123. 'INSERT_DYNDNS_HERE'
  124. );
  125. } //END Function hostname
  126.  
  127. public function validIP($ip){
  128.  
  129. //Validates an ipaddress
  130.  
  131. if (preg_match("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}^", $ip)):
  132.  
  133. return TRUE;
  134. else:
  135.  
  136. return FALSE;
  137. endif;
  138. } //END Function validIP
  139.  
  140. public function addHost($host){
  141.  
  142. //Adds an ipaddress or hostname to the access list
  143.  
  144. if ($this->validIP($host)):
  145.  
  146. $this->ip_users[] = $host;
  147. else:
  148.  
  149. $this->static_users[] = $host;
  150. endif;
  151.  
  152. } //END Function addHost
  153.  
  154. public function changeSetting($property, $value){
  155.  
  156. // Allows a user to change the settings and modify hostnames
  157.  
  158. if ($property == 'static_dns'): //1
  159.  
  160. if ($value == 'clear'): //1.1
  161.  
  162. // Erases all the previously set static hostnames
  163. $this->static_users = array();
  164. else: //1.1-2
  165.  
  166. $this->returnError("Failed to set 'static_dns', only option is 'clear'", 1);
  167. endif; //1.1
  168. elseif (array_key_exists($property, $this->_SETT)): //1-2
  169.  
  170. // Makes sure its a real setting then changes it
  171. $this->_SETT[$property] = $value;
  172. else: //1-3
  173.  
  174. $this->returnError("Failed to set '$property', not a valid setting (case sensative)", 0);
  175. return FALSE;
  176. endif; //1
  177.  
  178. return TRUE;
  179. } //END Fcuntion changeSetting
  180.  
  181. protected function returnError($errorText, $killScript = 0){
  182.  
  183. // Error handling
  184.  
  185. if ($killScript > 0): //1
  186.  
  187. if ($this->lastError != ''): //1.1
  188.  
  189. // loop & output errorLog then die with lastError
  190. echo "\n The following errors have occurred:";
  191. foreach ($this->errorLog as $error): //1.1.1
  192.  
  193. echo "\n ", $error;
  194. endforeach; //1.1.1
  195. echo "\n\n Last error to occur:\n ", $errorText;
  196. die();
  197. else: //1.1-2
  198.  
  199. die("The following error has occurred: ".$errorText);
  200. endif; //1.1
  201. else: //1-2
  202.  
  203. $this->errorLog[] = $this->lastError = $errorText;
  204. endif; //1
  205. } //END Function returnError
  206.  
  207. public function putCache($ipaddress = NULL){
  208.  
  209. //An alias to $this->openDatabase. Makes calling it easier to read
  210. return $this->openDatabase('putCache', $ipaddress);
  211. } //END Function putCache
  212.  
  213. protected function openDatabase($sqFunction = 'getCache', $ipaddress = NULL){
  214.  
  215. /* Opens the database and grabs the cached IP address
  216.   * Also capable of opening the database and inserting the new ip address
  217.   */
  218.  
  219. if ($hostname_database = sqlite_open($this->_SETT['dbfilename'], 0666, $sqlerror)): //1
  220.  
  221. switch ($sqFunction): //1.1
  222.  
  223. case ('putCache'): //1.1-2
  224.  
  225. $newdnsip = ($ipaddress == NULL) ? '255.255.255.255' : $ipaddress;
  226. $put_cache_query = "UPDATE ".$this->_SETT['dbtable']." SET ipaddress='$newdnsip' WHERE uniqueid='travelip'";
  227. @sqlite_exec($hostname_database, $put_cache_query);
  228. return TRUE;
  229. break;
  230. case ('getCache'): //1.1-3
  231. default: //1.1-4
  232.  
  233. $get_cache_query = "SELECT ipaddress FROM ".$this->_SETT['dbtable']." WHERE uniqueid='travelip'";
  234. $cache_file = @sqlite_single_query($hostname_database, $get_cache_query, true);
  235. endswitch; //1.1
  236.  
  237. if(sqlite_last_error($hostname_database)): //1.2
  238.  
  239. $this->returnError('Database table does not exist. Creating table, inserting blank ip address.');
  240. $create_table_query = "CREATE TABLE ".$this->_SETT['dbtable']."(uniqueid TEXT, ipaddress TEXT)";
  241. @sqlite_exec($hostname_database, $create_table_query);
  242. $insert_table_query = "INSERT INTO ".$this->_SETT['dbtable']." (uniqueid, ipaddress) VALUES ('travelip', '1')";
  243. @sqlite_exec($hostname_database, $insert_table_query);
  244. $cache_file = ($sqFunction = 'putCache' ? "Database created, run script again to populate ip address" : NULL);
  245. endif; //1.2
  246. else: //1-2
  247.  
  248. $this->returnError("SQLite failed to initialize: ".$sqlerror);
  249. sqlite_close($hostname_database);
  250. return NULL;
  251. endif; //1
  252.  
  253. sqlite_close($hostname_database);
  254.  
  255. return $cache_file;
  256. } //END Function openDatabase
  257.  
  258. public function checkHostname(){
  259.  
  260. // Open database, grab cache, approve ip address
  261.  
  262. if (!isset($_SESSION['remoteaddok'])): //1
  263.  
  264. $ip_users = array();
  265. $ip_static_users = array();
  266.  
  267. if ($this->_SETT['useDatabase'] != 'no'): //1.1
  268.  
  269. $ip_users[] = $this->openDatabase('getCache');
  270. if ($ip_users[0]{0} == 'D'): //1.1.1
  271.  
  272. $this->returnError($ip_users[0], 1);
  273. endif; //1.1.1
  274. endif; //1.1
  275. if (!empty($this->ip_users)): //1.2
  276.  
  277. //Additional ipaddress were manually set.
  278. //This combines them into the user list
  279. $ip_users = array_merge($ip_users, $this->ip_users);
  280. endif; //1.2
  281. if (!isset($_SESSION['travelingIP'])): //1.3
  282.  
  283. foreach ($this->static_users as $static_hostname): //1.3.1
  284.  
  285. if ($this->validIP($static_hostname)): //1.3.1.1
  286.  
  287. $ip_users[] = $ip_static_users[] = $static_hostname;
  288. else: //1.3.1.1-2
  289.  
  290. $ip_users[] = $ip_static_users[] = gethostbyname($static_hostname);
  291. endif; //1.3.1.1
  292. endforeach; //1.3.1
  293. endif; //1.3
  294. // Now we check against the servers with the current ip
  295. if (!in_array($_SERVER['REMOTE_ADDR'],$ip_users)): //1.4
  296.  
  297. die("You are not on the correct network to view this page");
  298. endif; //1.4
  299. if (in_array($_SERVER['REMOTE_ADDR'],$ip_static_users)): //1.5
  300.  
  301. /* After a user authenicates themself on a static ip,
  302.   * this will keep the script from constantly pinging
  303.   * the DYNDNS server causing needless load time.
  304.   */
  305. $_SESSION['remoteaddok'] = TRUE;
  306. else: //1.5-2
  307.  
  308. /* If the user is on a traveling ip, this makes it
  309.   * check the ip address every time as SQLite load
  310.   * time is small enough to not cause delay.
  311.   */
  312. $_SESSION['travelingIP'] = TRUE;
  313. endif; //1.5
  314. endif; //1
  315. } //END Function checkHostname
  316. } // END Class
  317.  
  318. /*-+----------------------------------
  319.  * | From here on is the actual script
  320.  */
  321.  
  322. if(!isset($hostname_runas)):
  323.  
  324. /* This sets the default action of this file.
  325.   *
  326.   * Set to 'class' to default to running as a class
  327.   * and will require the user to setup the object
  328.   * and run the hostname commands.
  329.   *
  330.   * Setting to 'script' will only need a 'require_once'
  331.   * command and it will take care of the rest
  332.   */
  333.  
  334. $hostname_runas = 'class'; //Options are 'class' and 'script'
  335. endif;
  336. switch ($hostname_runas):
  337.  
  338. case 'script':
  339.  
  340. $hostname = new hostname();
  341. $hostname->checkHostname();
  342.  
  343. break;
  344. case 'class':
  345. default:
  346.  
  347. endswitch;
  348. ?>

Report this snippet  

You need to login to post a comment.