home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / authticket.php < prev    next >
Encoding:
PHP Script  |  2001-07-02  |  7.5 KB  |  214 lines

  1. //**************************************
  2.     //     
  3.     // Name: authticket
  4.     // Description:This code generates an MD
  5.     //     5 protected string, which can be used to
  6.     //     hand off to other web pages, or even oth
  7.     //     er sites. If someone can read the ticket
  8.     //     , they can use it, so this works best ov
  9.     //     er encrypted connections, but since the 
  10.     //     ticket only lasts for a short time (2 ho
  11.     //     urs, definable) I think it's better than
  12.     //     sending a password around. By Michael Gr
  13.     //     aff.
  14.     // By: PHP Code Exchange
  15.     //**************************************
  16.     //     
  17.     
  18.     <?php
  19.     // $Id: authticket.phl,v 1.3 1998/02/11 
  20.     //     16:45:34 explorer Exp $
  21.     //
  22.     // Copyright (c) 1998 Michael Graff <
  23.     //     explorer@flame.org>
  24.     // All rights reserved.
  25.     //
  26.     // Redistribution and use in source and 
  27.     //     binary forms, with or without
  28.     // modification, are permitted provided 
  29.     //     that the following conditions
  30.     // are met:
  31.     // 1. Redistributions of source code mus
  32.     //     t retain the above copyright
  33.     //notice, this list of conditions and th
  34.     //     e following disclaimer.
  35.     // 2. Redistributions in binary form mus
  36.     //     t reproduce the above copyright
  37.     //notice, this list of conditions and th
  38.     //     e following disclaimer in the
  39.     //documentation and/or other materials p
  40.     //     rovided with the distribution.
  41.     // 3. Neither the name of author nor the
  42.     //     names of its contributors may be
  43.     //used to endorse or promote products de
  44.     //     rived from this software
  45.     //without specific prior written permiss
  46.     //     ion.
  47.     //
  48.     // THIS SOFTWARE IS PROVIDED BY THE AUTH
  49.     //     OR AND CONTRIBUTORS ``AS IS'' AND ANY
  50.     // EXPRESS OR IMPLIED WARRANTIES, INCLUD
  51.     //     ING, BUT NOT LIMITED TO, THE IMPLIED
  52.     // WARRANTIES OF MERCHANTABILITY AND FIT
  53.     //     NESS FOR A PARTICULAR PURPOSE ARE
  54.     // DISCLAIMED. IN NO EVENT SHALL THE FOU
  55.     //     NDATION OR CONTRIBUTORS BE LIABLE
  56.     // FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57.     //     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  58.     // DAMAGES (INCLUDING, BUT NOT LIMITED T
  59.     //     O, PROCUREMENT OF SUBSTITUTE GOODS OR
  60.     // SERVICES; LOSS OF USE, DATA, OR PROFI
  61.     //     TS; OR BUSINESS INTERRUPTION) HOWEVER
  62.     // CAUSED AND ON ANY THEORY OF LIABILITY
  63.     //     , WHETHER IN CONTRACT, STRICT
  64.     // LIABILITY, OR TORT (INCLUDING NEGLIGE
  65.     //     NCE OR OTHERWISE) ARISING IN ANY WAY
  66.     // OUT OF THE USE OF THIS SOFTWARE, EVEN
  67.     //     IF ADVISED OF THE POSSIBILITY OF
  68.     // SUCH DAMAGE.
  69.     //
  70.     class authticket {
  71.         var $secret = "setme";        // you WILL want to change this
  72.         var $realm = "";        // the realm of this identity
  73.         var $lifetime = 2 * 60 *60;    // tickets good for 2 hours
  74.         var $authenticated = 0;        // the data here is valid iff non-zero
  75.         var $identity;        // the remote identity, if decoded correctly
  76.         var $issue;        // the time the ticket was issued
  77.         var $remote_addr;    // the remote address of the client
  78.         var $hash;        // the hash value. Probably of little use.
  79.         var $autherr;        // if verification faild, this contains why
  80.         //
  81.         // helper function which just zeros out the ticket data
  82.         //
  83.         function zerodata()
  84.         {
  85.             $this->authenticated = 0;
  86.             $this->identity = "";
  87.             $this->issue = 0;
  88.             $this->remote_addr = "";
  89.             $this->hash = "";
  90.             $this->autherr = "";
  91.         }
  92.         //
  93.         // Take a string ($identity) and a time ($time) and the internal
  94.         // secret value, and generate a string that can be used to verify
  95.         // that the remote user is known to us. The result of this function
  96.         // is a single string, that can be passed along in a hidden form
  97.         // or even a cookie.
  98.         //
  99.         // If ($time) is 0, the current time is used instead.
  100.         //
  101.         // ($identity) _cannot_ contain a ``:'' character. If you need
  102.         // one in there, you will have to change it to some sort of escape
  103.         // sequence.
  104.         //
  105.         // Some care should be used. I recommend using this only over SSL,
  106.         // unless the actual ticket contents are encrypted using something
  107.         // stronger than XOR.
  108.         //
  109.         function makeauth($identity, $time)
  110.         {
  111.             global $REMOTE_ADDR;
  112.             $this->zerodata();
  113.             if ($time == 0)
  114.                 $time = time();
  115.             $ticket_items[] = (string)$time;
  116.             $ticket_items[] = $this->realm;
  117.             $ticket_items[] = $REMOTE_ADDR;
  118.             $ticket_items[] = $identity;
  119.             $ticket = implode($ticket_items, ":");
  120.             $hash = md5($this->secret . $ticket);
  121.             $ticket = $hash . ':' . $ticket;
  122.             $this->identity = $identity;
  123.             $this->issue = $time;
  124.             $this->remote_addr = $REMOTE_ADDR;
  125.             $this->hash = $hash;
  126.             $this->authenticated = 1; /* data is valid */
  127.             $this->autherr = "";
  128.             return $ticket;
  129.         }
  130.         //
  131.         // Take a ($ticket) string generated by makeauth(), and a ($time),
  132.         // and verify that the ticket is valid and not expired.
  133.         //
  134.         // If ($time) is 0, the current time will be used.
  135.         //
  136.         // On error, the function returns the empty string "",
  137.         // $authenticated is 0, and $autherr contains the reason
  138.         // the authentication failed.
  139.         //
  140.         // On success, the identity encoded in the ticket is returned,
  141.         // $authenticated is non-zero, and $autherr is to be ignored.
  142.         //
  143.         function checkauth($ticket, $time)
  144.         {
  145.             global $REMOTE_ADDR;
  146.             $this->zerodata();
  147.             if ($time == 0)
  148.                 $time = time();
  149.             /*
  150.              * Item order: hash time realm remote_addr identity
  151.              */
  152.             $ticket_items = explode(":", $ticket);
  153.             /*
  154.              * if the remote address doesn't match the one in the ticket,
  155.              * drop them.
  156.              */
  157.             if ($ticket_items[3] != $REMOTE_ADDR) {
  158.                 $this->autherr = "Address mismatch";
  159.                 return "";
  160.             }
  161.             //
  162.             // if we are supposed to check for expired tickets, do that
  163.             // here.
  164.             //
  165.             if ($this->lifetime != 0)
  166.                 if ($time > (int)$ticket_items[1] + $this->lifetime) {
  167.                     $this->autherr = "Ticket expired";
  168.                     return "";
  169.                 }
  170.             //
  171.             // make certain that the ticket is not being used before
  172.             // it was issued.
  173.             //
  174.             if ($time < (int)$ticket_items[1]) {
  175.                 $this->autherr = "Ticket used before issued";
  176.                 return "";
  177.             }
  178.             //
  179.             // verify that the realms match
  180.             //
  181.             if ($this->realm != $ticket_items[2]) {
  182.                 $this->autherr = "Realm mismatch";
  183.                 return "";
  184.             }
  185.             //
  186.             // This could be done better... Reassemble the components
  187.             // of the ticket passed to us, and rehash. Compare this
  188.             // to the hash we were sent.
  189.             //
  190.             $tmp_items[] = $ticket_items[1];
  191.             $tmp_items[] = $ticket_items[2];
  192.             $tmp_items[] = $ticket_items[3];
  193.             $tmp_items[] = $ticket_items[4];
  194.             $tmp_ticket = implode($tmp_items, ":");
  195.             $hash = md5($this->secret . $tmp_ticket);
  196.             if ($hash != $ticket_items[0]) {
  197.                 $this->autherr = "Integrity check failed";
  198.                 return "";
  199.             }
  200.             //
  201.             // well, it all checks out. Might as well claim we know
  202.             // who this person is.
  203.             //
  204.             $this->hash = $hash;
  205.             $this->issue = $ticket_items[1];
  206.             $this->remote_addr = $ticket_items[3];
  207.             $this->identity = $ticket_items[4];
  208.             $this->authenticated = 1;
  209.             return $this->identity;
  210.         }
  211.     };
  212.     ?>
  213.  
  214.