1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
<?php
require_once('libdb.php');
function libglue_sess_db($dbtype = 'local')
{
if($dbtype === 'sso')
{
$dbh = libglue_param(libglue_param('sso_dbh_name'));
if(is_resource($dbh)) return $dbh;
$dbh_name = libglue_param('sso_dbh_name');
$host = libglue_param('sso_host');
$db = libglue_param('sso_db');
$user = libglue_param('sso_username');
$pass = libglue_param('sso_pass');
$name = libglue_param('sso_dbh_name');
}
elseif($dbtype === 'local')
{
$dbh = libglue_param(libglue_param('local_dbh_name'));
if(is_resource($dbh)) return $dbh;
$dbh_name = libglue_param('local_dbh_name');
$host = libglue_param('local_host');
$db = libglue_param('local_db');
$user = libglue_param('local_username');
$pass = libglue_param('local_pass');
$name = libglue_param('local_dhb_name');
}
$dbh = db_connect($host,$user,$pass);
db_select($db);
libglue_param(array($dbh_name=>$dbh));
if(is_resource($dbh)) return $dbh;
else die("Could not connect to $dbtype database for session management");
}
/* This function is public */
function check_session($id=null)
{
if(is_null($id))
{
//From Karl Vollmer, vollmerk@net.orst.edu:
// naming the session and starting it is sufficient
// to retrieve the cookie
$name = libglue_param('sess_name');
if(!empty($name)) session_name($name);
session_start();
$id = strip_tags($_COOKIE[$name]);
}
// Now what we have a session id, let's verify it:
if(libglue_sso_mode())
{
// if sso mode, we must have a valid sso session already
$sso_sess = libglue_sso_check($id);
if(!is_null($sso_sess))
{
// if sso is valid, it's okay to create a new local session
if($local_sess = libglue_local_check($id))
{
return true;
}
else
{
libglue_local_create($id,
$sso_sess[libglue_param('sso_username_col')],
'sso',
$sso_sess[libglue_param('sso_expire_col')]);
return true;
}
}
else
// libglue_sso_check failed
{
libglue_sess_destroy($id);
return false;
}
}
else
{
//if not in sso mode, there must be a local session
if($local_sess = libglue_local_check($id))
{
return true;
}
else
{
//you're gone buddy
libglue_sess_destroy($id);
return false;
}
}
}
// private function, don't ever use this:
function libglue_sso_mode()
{
$auth_methods = libglue_param('auth_methods');
if(!is_array($auth_methods)) $auth_methods = array($auth_methods);
return (in_array('sso',$auth_methods))?true:false;
}
function libglue_sso_check($sess_id)
{
// Read the sso info from the config file:
$sso_table = libglue_param('sso_table');
$sso_sid = libglue_param('sso_sessid_col');
$sso_expire_col = libglue_param('sso_expire_col');
$sso_length = libglue_param('sso_length');
$sso_username_col = libglue_param('sso_username_col');
$sso_dbh = libglue_sess_db('sso');
$sql = "SELECT * FROM $sso_table WHERE $sso_sid='$sess_id' AND $sso_expire_col > UNIX_TIMESTAMP()";
$db_result = db_query($sql, $sso_dbh);
if(is_resource($db_result)) $sso_session = db_fetch($db_result);
else $sso_session = null;
$retval = (is_array($sso_session))?$sso_session:null;
return $retval;
}
function libglue_local_check($sess_id)
{
static $retval = -1;
if($retval != -1) return $retval;
$local_table = libglue_param('local_table');
$local_sid = libglue_param('local_sid');
$local_expirecol = libglue_param('local_expirecol');
$local_length = libglue_param('local_length');
$local_usercol = libglue_param('local_usercol');
$local_datacol = libglue_param('local_datacol');
$local_typecol = libglue_param('local_typecol');
$local_dbh = libglue_sess_db('local');
$sql = "SELECT $local_datacol FROM $local_table WHERE $local_sid='$sess_id' AND $local_expirecol > UNIX_TIMESTAMP()";
$db_result = db_query($sql, $local_dbh);
if(is_resource($db_result)) $session = db_fetch($db_result);
else $session = null;
if(is_array($session))
{
$retval = $session[$local_datacol];
}
else $retval = null;
return $retval;
}
function libglue_local_create($sess_id, $username, $type, $expire)
{
if($type === "sso" || $type === "ldap")
$userdata = get_ldap_user($username);
else if($type === "mysql")
$userdata = get_mysql_user($username);
$data = array(libglue_param('user_data_name')=>$userdata);
// It seems we have to set $_SESSION manually, or it gets blasted
// by php's session write handler
$_SESSION = $data;
$db_data = serialize($data);
$local_dbh = libglue_sess_db('local');
// Local parameters we need:
$local_table = libglue_param('local_table');
$local_sid = libglue_param('local_sid');
$local_usercol = libglue_param('local_usercol');
$local_datacol = libglue_param('local_datacol');
$local_expirecol = libglue_param('local_expirecol');
$local_typecol = libglue_param('local_typecol');
// session data will be saved when the script terminates,
// but not the rest of this fancy info
$sql= "INSERT INTO $local_table ".
" ($local_sid,$local_usercol,$local_datacol,$local_expirecol,$local_typecol)".
" VALUES ('$sess_id','$username','$db_data','$expire','$type')";
$db_result = db_query($sql, $local_dbh);
if(!$db_result) die("Died trying to create local session: <pre><br>$sql</pre>");
}
function sess_open()
{
if(libglue_sso_mode())
{
if(!is_resource(libglue_sess_db('sso')))
{
die("<!-- Unable to connect to the SSO server in order to " .
"use the session database. Perhaps the database is not ".
"running, or perhaps the admin needs to change a few variables in ".
"modules/include/global_settings in order to point to the correct ".
"database.-->\n");
return false;
}
}
if(!is_resource(libglue_sess_db('local')))
{
die("<!-- Unable to connect to local server in order to " .
"use the session database. Perhaps the database is not ".
"running, or perhaps the admin needs to change a few variables in ".
"the config file in order to point to the correct ".
"database.-->\n");
return false;
}
return true;
}
function sess_close(){ return true; }
function sess_write($sess_id, $sess_data)
{
$local_dbh = libglue_sess_db('local');
$local_datacol = libglue_param('local_datacol');
$local_table = libglue_param('local_table');
$local_sid = libglue_param('local_sid');
$auth_methods = libglue_param('auth_methods');
$local_expire = libglue_param('local_expirecol');
$local_length = libglue_param('local_length');
$time = $local_length+time();
// If not using sso, we now need to update the expire time
$local_expire = libglue_param('local_expirecol');
$local_length = libglue_param('local_length');
$time = $local_length+time();
$sql = "UPDATE $local_table SET $local_datacol='$sess_data',$local_expire='$time'".
" WHERE $local_sid = '$sess_id'";
db_query($sql, $local_dbh);
if(libglue_sso_mode())
{
$sso_table = libglue_param('sso_table');
$sso_expire_col = libglue_param('sso_expire_col');
$sso_sess_length = libglue_param('sso_length_col');
$sso_sess_id = libglue_param('sso_sessid_col');
$time = time();
$sql = "UPDATE $sso_table SET $sso_expire_col = $sso_sess_length + UNIX_TIMESTAMP() WHERE $sso_sess_id = '$sess_id'";
$sso_dbh = libglue_sess_db('sso');
db_query($sql, $sso_dbh);
}
return true;
}
//
// This function is called with random frequency
// to remove expired session data
//
function sess_gc($maxlifetime)
{
if(libglue_sso_mode())
{
//Delete old sessions from SSO
// We do 'where length' so we don't accidentally blast
// another app's sessions
$sso_expirecol = libglue_param('sso_expire_col');
$sso_table = libglue_param('sso_table');
$sso_length = libglue_param('sso_length_col');
$local_length = libglue_param('local_length');
$sso_dbh = libglue_sess_db('sso');
$time = time();
$sql = "DELETE FROM $sso_table WHERE $sso_expirecol < $time".
" AND $sso_length = '$local_length'";
$result = db_query($sql, $sso_dbh);
}
$local_expire = libglue_param('local_expire');
$local_table = libglue_param('local_table');
$time = time();
$local_dbh = libglue_sess_db('local');
$sql = "DELETE FROM $local_table WHERE $local_expire < $time";
$result = db_query($sql, $local_dbh);
return true;
}
function libglue_sess_destroy($id=null)
{
if(is_null($id))
{
//From Karl Vollmer, vollmerk@net.orst.edu:
// naming the session and starting it is sufficient
// to retrieve the cookie
$name = libglue_param('sess_name');
if(!empty($name)) session_name($name);
session_start();
$id = strip_tags($_COOKIE[$name]);
}
if(libglue_sso_mode())
{
$sso_sid = libglue_param('sso_sessid_col');
$sso_table = libglue_param('sso_table');
$sso_dbh = libglue_sess_db('sso');
$sql = "DELETE FROM $sso_table WHERE $sso_sid = '$id' LIMIT 1";
$result = db_query($sql, $sso_dbh);
}
$local_sid = libglue_param('local_sid');
$local_table = libglue_param('local_table');
$local_dbh = libglue_sess_db('local');
$sql = "DELETE FROM $local_table WHERE $local_sid = '$id' LIMIT 1";
$result = db_query($sql, $local_dbh);
// It is very important we destroy our current session cookie,
// because if we don't, a person won't be able to log in again
// without closing their browser - SSO doesn't respect
//
// Code from http://php.oregonstate.edu/manual/en/function.session-destroy.php,
// Written by powerlord@spamless.vgmusic.com, 18-Nov-2002 08:41
//
$cookie = session_get_cookie_params();
if ((empty($cookie['domain'])) && (empty($cookie['secure'])) )
{
setcookie(session_name(), '', time()-3600, $cookie['path']);
} elseif (empty($CookieInfo['secure'])) {
setcookie(session_name(), '', time()-3600, $cookie['path'], $cookie['domain']);
} else {
setcookie(session_name(), '', time()-3600, $cookie['path'], $cookie['domain'], $cookie['secure']);
}
// end powerloard
unset($_SESSION);
unset($_COOKIE[session_name()]);
return TRUE;
}
function logout($id=null)
{
libglue_sess_destroy($id);
$login_page = libglue_param('login_page');
header("Location: $login_page");
die();
return true; //because why not?
}
//
// Register all our cool session handling functions
//
session_set_save_handler(
"sess_open",
"sess_close",
"libglue_local_check",
"sess_write",
"libglue_sess_destroy",
"sess_gc");
?>
|