Debian Codegraph @

contrib/auth_ticket.inc.php

raw
1 <?php
2
3 //####################################################################
4 //
5 // File: auth_ticket.inc.php
6 //
7 // By: Luc Germain, STI, Universite de Sherbrooke
8 // Date: 2004-02-17
9 //
10 //#####################################################################
11 //
12 //#####################################################################
13 //
14 // This file defines functions to generate cookie tickets compatible
15 // with the "mod_auth_tkt" apache module.
16 //
17 //#####################################################################
18
19 // Configuration
20
21 // Weather we encrypt the data or not
22 $ENCRYPT_COOKIE = true;
23
24 // File name where the secret key is stored
25 $SECRET_KEY_FILE = "/path/to/file.txt";
26
27 // Initial seed for calls to rand
28 if( !isset($SeedIsDone) ) {
29 srand((double)microtime()*1000000);
30 $SeedIsDone = true;
31 }
32
33 //---------------------------------------------------------------
34 // Functions
35 //---------------------------------------------------------------
36
37 //---------------------------------------------------------------
38 // $result = getSecretKey( );
39 //---------------------------------------------------------------
40 //
41 // Returns a string that contains the secret key used to sign the
42 // cookie. Read from the secret key file.
43 //
44 //---------------------------------------------------------------
45 function getSecretKey() {
46 global $SECRET_KEY_FILE;
47 $keyword = "TktAuthSecret";
48 $minKeyLength = 10;
49 $matches = array();
50 $secretKey = "";
51
52 $content = file_get_contents( $SECRET_KEY_FILE );
53 if( $content === FALSE ){
54 // Cannot read key file
55 stopOnError( "F0006" );
56 }
57
58 if( preg_match( "/^\s*$keyword\s+\"(.*?)\"/m",
59 $content, $matches ) ) {
60 $secretKey = $matches[1];
61 }
62
63 if( strlen( $secretKey ) < $minKeyLength ) {
64 // Key invalid or not found
65 stopOnError( "F0007" );
66 }
67
68 return( $secretKey );
69 }
70
71 //---------------------------------------------------------------
72 // $result = stopOnError( $code );
73 //---------------------------------------------------------------
74 //
75 // Display an error code in html and exit
76 //
77 //---------------------------------------------------------------
78 function stopOnError( $code ) {
79 echo " <p>The program encountered an unexpected error.</p>
80 <p>Error code: $code</p>";
81 exit( );
82 }
83
84 //---------------------------------------------------------------
85 // $result = getTKTHash( $ip, $user, $tokens, $data, $key,
86 // [, $base64 [, $ts]] );
87 //---------------------------------------------------------------
88 //
89 // Returns a string that contains the signed cookie.
90 //
91 // The cookie includes the ip address of the user, the user UID, the
92 // tokens, the user data and a time stamp. The cookie can be
93 // optionnally base64 encoded. The data is also crypted with the
94 // encode() function.
95 //
96 //---------------------------------------------------------------
97 function getTKTHash( $ip, $user, $tokens, $data, $key, $base64 = false, $ts = "" ) {
98
99 // set the timestamp to now
100 // unless a time is specified
101 if( $ts == "" ) {
102 $ts = time();
103 }
104 $ipts = pack( "NN", ip2long($ip), $ts );
105
106 // make the cookie signature
107 $digest0 = md5( $ipts . $key . $user . "\0" . $tokens . "\0" . $data );
108 $digest = md5( $digest0 . $key );
109
110 if( $tokens ){
111 $tkt = sprintf( "%s%08x%s!%s!%s", $digest, $ts,
112 encode( $user, $ts, 0 ),
113 encode( $tokens, $ts, 4 ),
114 encode( $data, $ts, 8 ) );
115 } else {
116 $tkt = sprintf( "%s%08x%s!%s", $digest, $ts,
117 encode( $user, $ts, 0 ),
118 encode( $data, $ts, 8 ) );
119 }
120 if( $base64 ) {
121 return( base64_encode( $tkt ) );
122 } else {
123 return( $tkt );
124 }
125 }
126
127 //---------------------------------------------------------------
128 // $result = encode( $data, $timestamp, $offset );
129 //---------------------------------------------------------------
130 //
131 // Returns a "crypted" version of the data. The length of the data is
132 // unchanged.
133 //
134 // The encryption is deactivated (the function simply returns the
135 // string unencrypted) if the configuration variable $ENCRYPT_COOKIE
136 // is not set to TRUE.
137 //
138 // The function implements a encryption algorithm that substitutes
139 // each character for another one using a key to compute the shift
140 // value. The key is generated from a hash of the timestamp of the
141 // cookie and the secret key. This key is used from the offset
142 // specified. This algorithm is reversed in the mod_auth_tkt apache
143 // module before using the data. This may not be strictly
144 // cryptographically secure, but should provide sufficient protection
145 // for the personnal data included in the cookie.
146 //
147 //---------------------------------------------------------------
148 function encode( $data, $timestamp, $offset ) {
149 $CHARS_TO_ENCODE = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.:";
150 $LENGTH = strlen( $CHARS_TO_ENCODE );
151 $md5key = md5( $timestamp . getSecretKey() );
152 $encoded = "";
153
154 global $ENCRYPT_COOKIE;
155
156 // check if encryption is activated
157 if( ! $ENCRYPT_COOKIE ) {
158 return $data;
159 }
160
161 // encode the data one caracter at a time
162 for( $i = 0; $i < strlen($data); $i++ ) {
163
164 $pos = strpos( $CHARS_TO_ENCODE, $data{$i} );
165 if( $pos === FALSE ) {
166 // skip characters that are not in list to encode
167 $encoded .= $data{$i};
168 } else {
169 $newPos = ($pos + (hexdec( $md5key{($offset + $i)%strlen($md5key)} )*7)) % $LENGTH;
170 $encoded .= $CHARS_TO_ENCODE{$newPos};
171 // print $data{$i} . " -> $newPos " . $CHARS_TO_ENCODE{$newPos} . "<br>";
172 }
173 }
174 // print "<br>md5key = $md5key<br>data = $data<br>encoded = $encoded";
175 return $encoded;
176 }
177
178 //---------------------------------------------------------------
179 // $result = file_get_contents( $filename, $use_include_path = 0 );
180 //---------------------------------------------------------------
181 //
182 // Returns the content of the file in a string, or false if there is
183 // an error.
184 //
185 // This function exists in PHP >= 4.3.0 only
186 //
187 //---------------------------------------------------------------
188 if( !function_exists( 'file_get_contents' ) ) {
189 function file_get_contents( $filename, $use_include_path = 0 ) {
190 $data = '';
191 $file = @fopen( $filename, "rb", $use_include_path );
192 if( $file ) {
193 while( !feof( $file ) ) {
194 $data .= fread( $file, 1024 );
195 }
196 fclose($file);
197 } else {
198 /* There was a problem opening the file. */
199 return FALSE;
200 }
201 return $data;
202 }
203 }
204
205
206
207 ?>
208
209
210