1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CILogon\Service; |
4
|
|
|
|
5
|
|
|
use CILogon\Service\CSRF; |
6
|
|
|
use CILogon\Service\Loggit; |
7
|
|
|
use CILogon\Service\IdpList; |
8
|
|
|
use CILogon\Service\DBService; |
9
|
|
|
use CILogon\Service\SessionMgr; |
10
|
|
|
use CILogon\Service\Skin; |
11
|
|
|
use CILogon\Service\TimeIt; |
12
|
|
|
use CILogon\Service\PortalCookie; |
13
|
|
|
use PEAR; |
14
|
|
|
use DB; |
15
|
|
|
use Config; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Util |
19
|
|
|
* |
20
|
|
|
* This class contains a bunch of static (class) utility |
21
|
|
|
* methods, for example getting and setting server environment |
22
|
|
|
* variables and handling cookies. See the header for each function for |
23
|
|
|
* detailed description. |
24
|
|
|
*/ |
25
|
|
|
class Util |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array $ini_array Read the cilogon.ini file into an array |
29
|
|
|
*/ |
30
|
|
|
public static $ini_array = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TimeIt $timeit Initialize by calling static::startTiming() in |
34
|
|
|
* init(). |
35
|
|
|
*/ |
36
|
|
|
public static $timeit; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var IdPList $idplist A 'global' IdpList object since dplist.xml is |
40
|
|
|
* large and expensive to create multiple times. |
41
|
|
|
*/ |
42
|
|
|
public static $idplist = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var CSRF $csrf A 'global' CSRF token object to set the CSRF cookie |
46
|
|
|
* and print the hidden CSRF form element. Needs to be set only once |
47
|
|
|
* to keep the same CSRF value through the session. |
48
|
|
|
*/ |
49
|
|
|
public static $csrf = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Skin $skin A 'global' Skin object for skin configuration. |
53
|
|
|
*/ |
54
|
|
|
public static $skin = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array $oauth2idps An array of OAuth2 Identity Providers. |
58
|
|
|
*/ |
59
|
|
|
public static $oauth2idps = ['Google', 'GitHub', 'ORCID']; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* getIdPList |
64
|
|
|
* |
65
|
|
|
* This function initializes the class $idplist object (if not yet |
66
|
|
|
* created) and returns it. This allows for a single 'global' |
67
|
|
|
* $idplist to be used by other classes (since creating an IdPList |
68
|
|
|
* object is expensive). |
69
|
|
|
* |
70
|
|
|
* @return IdPList|null The class instantiated IdPList object. |
71
|
|
|
**/ |
72
|
|
|
public static function getIdpList() |
73
|
|
|
{ |
74
|
|
|
if (is_null(static::$idplist)) { |
75
|
|
|
static::$idplist = new IdpList(); |
76
|
|
|
} |
77
|
|
|
return static::$idplist; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* getCsrf |
82
|
|
|
* |
83
|
|
|
* This function initializes the class $csrf object (if not yet |
84
|
|
|
* created) and returns it. This allows for a single 'global' |
85
|
|
|
* $csrf to be used by other classes (since we want the CSRV value |
86
|
|
|
* to be consistent for the current page load). |
87
|
|
|
* |
88
|
|
|
* @return CSRF|null The class instantiated CSRF object. |
89
|
|
|
*/ |
90
|
|
|
public static function getCsrf() |
91
|
|
|
{ |
92
|
|
|
if (is_null(static::$csrf)) { |
93
|
|
|
static::$csrf = new CSRF(); |
94
|
|
|
} |
95
|
|
|
return static::$csrf; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* getSkin |
100
|
|
|
* |
101
|
|
|
* This function initializes the class $skin object (if not yet |
102
|
|
|
* created) and returns it. This allows for a single 'global' |
103
|
|
|
* $skin to be used by other classes (since loading the skin is |
104
|
|
|
* potentially expensive). |
105
|
|
|
* |
106
|
|
|
* @return Skin|null The class instantiated Skin object. |
107
|
|
|
*/ |
108
|
|
|
public static function getSkin() |
109
|
|
|
{ |
110
|
|
|
if (is_null(static::$skin)) { |
111
|
|
|
static::$skin = new Skin(); |
112
|
|
|
} |
113
|
|
|
return static::$skin; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* startTiming |
118
|
|
|
* |
119
|
|
|
* This function initializes the class variable $timeit which is |
120
|
|
|
* used for timing/benchmarking purposes. |
121
|
|
|
*/ |
122
|
|
|
public static function startTiming() |
123
|
|
|
{ |
124
|
|
|
static::$timeit = new TimeIt(TimeIt::DEFAULTFILENAME, true); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* getServerVar |
129
|
|
|
* |
130
|
|
|
* This function queries a given $_SERVER variable (which is set |
131
|
|
|
* by the Apache server) and returns the value. |
132
|
|
|
* |
133
|
|
|
* @param string $serv The $_SERVER variable to query. |
134
|
|
|
* @return string The value of the $_SERVER variable or empty string |
135
|
|
|
* if that variable is not set. |
136
|
|
|
*/ |
137
|
|
|
public static function getServerVar($serv) |
138
|
|
|
{ |
139
|
|
|
$retval = ''; |
140
|
|
|
if (isset($_SERVER[$serv])) { |
141
|
|
|
$retval = $_SERVER[$serv]; |
142
|
|
|
} |
143
|
|
|
return $retval; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* getGetVar |
148
|
|
|
* |
149
|
|
|
* This function queries a given $_GET parameter (which is set in |
150
|
|
|
* the URL via a '?parameter=value' parameter) and returns the |
151
|
|
|
* value. |
152
|
|
|
* |
153
|
|
|
* @param string $get The $_GET variable to query. |
154
|
|
|
* @return string The value of the $_GET variable or empty string if |
155
|
|
|
* that variable is not set. |
156
|
|
|
*/ |
157
|
|
|
public static function getGetVar($get) |
158
|
|
|
{ |
159
|
|
|
$retval = ''; |
160
|
|
|
if (isset($_GET[$get])) { |
161
|
|
|
$retval = $_GET[$get]; |
162
|
|
|
} |
163
|
|
|
return $retval; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* getPostVar |
168
|
|
|
* |
169
|
|
|
* This function queries a given $_POST variable (which is set when |
170
|
|
|
* the user submits a form, for example) and returns the value. |
171
|
|
|
* |
172
|
|
|
* @param string $post The $_POST variable to query. |
173
|
|
|
* @return string The value of the $_POST variable or empty string if |
174
|
|
|
* that variable is not set. |
175
|
|
|
*/ |
176
|
|
|
public static function getPostVar($post) |
177
|
|
|
{ |
178
|
|
|
$retval = ''; |
179
|
|
|
if (isset($_POST[$post])) { |
180
|
|
|
$retval = $_POST[$post]; |
181
|
|
|
} |
182
|
|
|
return $retval; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* getGetOrPostVar |
187
|
|
|
* |
188
|
|
|
* This function looks for a $_GET or $_POST variable, with |
189
|
|
|
* preference given to $_GET if both are present. |
190
|
|
|
* |
191
|
|
|
* @param string $var The $_GET or $_POST variable to query. |
192
|
|
|
* @return string The value of the $_GET or $_POST variable |
193
|
|
|
* if present. Empty string if variable is not set. |
194
|
|
|
*/ |
195
|
|
|
public static function getGetOrPostVar($var) |
196
|
|
|
{ |
197
|
|
|
$retval = static::getGetVar($var); |
198
|
|
|
if (empty($retval)) { |
199
|
|
|
$retval = static::getPostVar($var); |
200
|
|
|
} |
201
|
|
|
return $retval; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* getCookieVar |
206
|
|
|
* |
207
|
|
|
* This function returns the value of a given cookie. |
208
|
|
|
* |
209
|
|
|
* @param string $cookie he $_COOKIE variable to query. |
210
|
|
|
* @return string The value of the $_COOKIE variable or empty string |
211
|
|
|
* if that variable is not set. |
212
|
|
|
*/ |
213
|
|
|
public static function getCookieVar($cookie) |
214
|
|
|
{ |
215
|
|
|
$retval = ''; |
216
|
|
|
if (isset($_COOKIE[$cookie])) { |
217
|
|
|
$retval = $_COOKIE[$cookie]; |
218
|
|
|
} |
219
|
|
|
return $retval; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* setCookieVar |
224
|
|
|
* |
225
|
|
|
* This function sets a cookie. |
226
|
|
|
* |
227
|
|
|
* @param string $cookie The name of the cookie to set. |
228
|
|
|
* @param string $value (Optional) The value to set for the cookie. |
229
|
|
|
* Defaults to empty string. |
230
|
|
|
* @param int $exp The future expiration time (in seconds) of the |
231
|
|
|
* cookie. Defaults to 1 year from now. If set to 0, |
232
|
|
|
* the cookie expires at the end of the session. |
233
|
|
|
*/ |
234
|
|
|
public static function setCookieVar($cookie, $value = '', $exp = 31536000) |
235
|
|
|
{ |
236
|
|
|
if ($exp > 0) { |
237
|
|
|
$exp += time(); |
238
|
|
|
} |
239
|
|
|
setcookie($cookie, $value, $exp, '/', '.' . static::getDN(), true); |
240
|
|
|
$_COOKIE[$cookie] = $value; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* unsetCookieVar |
245
|
|
|
* |
246
|
|
|
* This function unsets a cookie. Strictly speaking, the cookie is |
247
|
|
|
* not removed, rather it is set to an empty value with an expired |
248
|
|
|
* time. |
249
|
|
|
* |
250
|
|
|
* @param string $cookie The name of the cookie to unset (delete). |
251
|
|
|
*/ |
252
|
|
|
public static function unsetCookieVar($cookie) |
253
|
|
|
{ |
254
|
|
|
setcookie($cookie, '', 1, '/', '.' . static::getDN(), true); |
255
|
|
|
unset($_COOKIE[$cookie]); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* getPortalOrNormalCookieVar |
260
|
|
|
* |
261
|
|
|
* This is a convenience function which first checks if there is a |
262
|
|
|
* OAuth 1.0a ('delegate') or OIDC ('authorize') session active. |
263
|
|
|
* If so, it attempts to get the requested cookie from the |
264
|
|
|
* associated portalcookie. If there is not an OAuth/OIDC session |
265
|
|
|
* active, it looks for a 'normal' cookie. If you need a |
266
|
|
|
* portalcookie object to do multiple get/set method calls from |
267
|
|
|
* one function, it is probably better NOT to use this method since |
268
|
|
|
* creating the portalcookie object is potentially expensive. |
269
|
|
|
* |
270
|
|
|
* @param string $cookie The name of the cookie to get. |
271
|
|
|
* @return string The cookie value from either the portalcookie |
272
|
|
|
* (in the case of an active OAuth session) or the |
273
|
|
|
* 'normal' cookie. Return empty string if no matching |
274
|
|
|
* cookie in either place. |
275
|
|
|
*/ |
276
|
|
|
public static function getPortalOrNormalCookieVar($cookie) |
277
|
|
|
{ |
278
|
|
|
$retval = ''; |
279
|
|
|
$pc = new PortalCookie(); |
280
|
|
|
$pn = $pc->getPortalName(); |
281
|
|
|
if (strlen($pn) > 0) { |
282
|
|
|
$retval = $pc->get($cookie); |
283
|
|
|
} else { |
284
|
|
|
$retval = static::getCookieVar($cookie); |
285
|
|
|
} |
286
|
|
|
return $retval; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* getSessionVar |
291
|
|
|
* |
292
|
|
|
* This function returns the value of a given PHP Session variable. |
293
|
|
|
* |
294
|
|
|
* @param string $sess The $_SESSION variable to query. |
295
|
|
|
* @return string The value of the $_SESSION variable or empty string |
296
|
|
|
* if that variable is not set. |
297
|
|
|
*/ |
298
|
|
|
public static function getSessionVar($sess) |
299
|
|
|
{ |
300
|
|
|
$retval = ''; |
301
|
|
|
if (isset($_SESSION[$sess])) { |
302
|
|
|
$retval = $_SESSION[$sess]; |
303
|
|
|
} |
304
|
|
|
return $retval; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* setSessionVar |
309
|
|
|
* |
310
|
|
|
* This function can set or unset a given PHP session variable. |
311
|
|
|
* The first parameter is the PHP session variable to set/unset. |
312
|
|
|
* If the second parameter is the empty string, then the session |
313
|
|
|
* variable is unset. Otherwise, the session variable is set to |
314
|
|
|
* the second parameter. The function returns true if the session |
315
|
|
|
* variable was set to a non-empty value, false otherwise. |
316
|
|
|
* Normally, the return value can be ignored. |
317
|
|
|
* |
318
|
|
|
* @param string $key The name of the PHP session variable to set |
319
|
|
|
* (or unset). |
320
|
|
|
* @param string $value (Optional) The value of the PHP session variable |
321
|
|
|
* (to set), or empty string (to unset). Defaults to empty |
322
|
|
|
* string (implies unset the session variable). |
323
|
|
|
* @return bool True if the PHP session variable was set to a |
324
|
|
|
* non-empty string, false if variable was unset or if |
325
|
|
|
* the specified session variable was not previously set. |
326
|
|
|
*/ |
327
|
|
|
public static function setSessionVar($key, $value = '') |
328
|
|
|
{ |
329
|
|
|
$retval = false; // Assume we want to unset the session variable |
330
|
|
|
if (strlen($key) > 0) { // Make sure session var name was passed in |
331
|
|
|
if (strlen($value) > 0) { |
332
|
|
|
$_SESSION[$key] = $value; |
333
|
|
|
$retval = true; |
334
|
|
|
} else { |
335
|
|
|
static::unsetSessionVar($key); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
return $retval; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* unsetSessionVar |
343
|
|
|
* |
344
|
|
|
* This function clears the given PHP session variable by first |
345
|
|
|
* setting it to null and then unsetting it entirely. |
346
|
|
|
* |
347
|
|
|
* @param string $sess The $_SESSION variable to erase. |
348
|
|
|
*/ |
349
|
|
|
public static function unsetSessionVar($sess) |
350
|
|
|
{ |
351
|
|
|
if (isset($_SESSION[$sess])) { |
352
|
|
|
$_SESSION[$sess] = null; |
353
|
|
|
unset($_SESSION[$sess]); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* removeShibCookies |
359
|
|
|
* |
360
|
|
|
* This function removes all '_shib*' cookies currently in the |
361
|
|
|
* user's browser session. In effect, this logs the user out of |
362
|
|
|
* any IdP. Note that you must call this before you output any |
363
|
|
|
* HTML. Strictly speaking, the cookies are not removed, rather |
364
|
|
|
* they are set to empty values with expired times. |
365
|
|
|
*/ |
366
|
|
|
public static function removeShibCookies() |
367
|
|
|
{ |
368
|
|
|
foreach ($_COOKIE as $key => $value) { |
369
|
|
|
if (strncmp($key, '_shib', strlen('_shib')) == 0) { |
370
|
|
|
static::unsetCookieVar($key); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* startPHPSession |
377
|
|
|
* |
378
|
|
|
* This function starts a secure PHP session and should be called |
379
|
|
|
* at the beginning of each script before any HTML is output. It |
380
|
|
|
* does a trick of setting a 'lastaccess' time so that the |
381
|
|
|
* $_SESSION variable does not expire without warning. |
382
|
|
|
* |
383
|
|
|
* @param string $storetype (Optional) Storage location of the PHP |
384
|
|
|
* session data, one of 'file' or 'mysql'. Defaults to null, |
385
|
|
|
* which means use the value of STORAGE_PHPSESSIONS from the |
386
|
|
|
* config.php file, or 'file' if no such parameter configured. |
387
|
|
|
*/ |
388
|
|
|
public static function startPHPSession($storetype = null) |
|
|
|
|
389
|
|
|
{ |
390
|
|
|
// No parameter given? Use the value read in from cilogon.ini file. |
391
|
|
|
// If STORAGE_PHPSESSIONS == 'mysqli', create a sessionmgr(). |
392
|
|
|
$storetype = STORAGE_PHPSESSIONS; |
|
|
|
|
393
|
|
|
|
394
|
|
|
if (preg_match('/^mysql/', $storetype)) { |
395
|
|
|
$sessionmgr = new SessionMgr(); |
|
|
|
|
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
ini_set('session.cookie_secure', true); |
|
|
|
|
399
|
|
|
ini_set('session.cookie_domain', '.' . static::getDN()); |
400
|
|
|
session_start(); |
401
|
|
|
if ( |
402
|
|
|
(!isset($_SESSION['lastaccess']) || |
403
|
|
|
(time() - $_SESSION['lastaccess']) > 60) |
404
|
|
|
) { |
405
|
|
|
$_SESSION['lastaccess'] = time(); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* getScriptDir |
411
|
|
|
* |
412
|
|
|
* This function returns the directory (or full url) of the script |
413
|
|
|
* that is currently running. The returned directory/url is |
414
|
|
|
* terminated by a '/' character (unless the second parameter is |
415
|
|
|
* set to true). This function is useful for those scripts named |
416
|
|
|
* index.php where we don't want to actually see 'index.php' in the |
417
|
|
|
* address bar (again, unless the second parameter is set to true). |
418
|
|
|
* |
419
|
|
|
* @param bool $prependhttp (Optional) Boolean to prepend 'http(s)://' to |
420
|
|
|
* the script name. Defaults to false. |
421
|
|
|
* @param bool $stripfile (Optional) Boolean to strip off the trailing |
422
|
|
|
* filename (e.g. index.php) from the path. |
423
|
|
|
* Defaults to true (i.e., defaults to directory |
424
|
|
|
* only without the trailing filename). |
425
|
|
|
* @return string The directory or url of the current script, with or |
426
|
|
|
* without the trailing .php filename. |
427
|
|
|
*/ |
428
|
|
|
public static function getScriptDir($prependhttp = false, $stripfile = true) |
429
|
|
|
{ |
430
|
|
|
$retval = static::getServerVar('SCRIPT_NAME'); |
431
|
|
|
if ($stripfile) { |
432
|
|
|
$retval = dirname($retval); |
433
|
|
|
} |
434
|
|
|
if ($retval == '.') { |
435
|
|
|
$retval = ''; |
436
|
|
|
} |
437
|
|
|
if ( |
438
|
|
|
(strlen($retval) == 0) || |
439
|
|
|
($stripfile && ($retval[strlen($retval) - 1] != '/')) |
440
|
|
|
) { |
441
|
|
|
$retval .= '/'; // Append a slash if necessary |
442
|
|
|
} |
443
|
|
|
if ($prependhttp) { // Prepend http(s)://hostname |
444
|
|
|
$retval = 'http' . |
445
|
|
|
((strtolower(static::getServerVar('HTTPS')) == 'on') ? 's' : '') . |
446
|
|
|
'://' . static::getServerVar('HTTP_HOST') . $retval; |
447
|
|
|
} |
448
|
|
|
return $retval; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* tempDir |
453
|
|
|
* |
454
|
|
|
* This function creates a temporary subdirectory within the |
455
|
|
|
* specified subdirectory. The new directory name is composed of |
456
|
|
|
* 16 hexadecimal letters, plus any prefix if you specify one. The |
457
|
|
|
* full path of the the newly created directory is returned. |
458
|
|
|
* |
459
|
|
|
* @param string $dir The full path to the containing directory. |
460
|
|
|
* @param string $prefix (Optional) A prefix for the new temporary |
461
|
|
|
* directory. Defaults to empty string. |
462
|
|
|
* @param int $mode (Optional) Access permissions for the new |
463
|
|
|
* temporary directory. Defaults to 0775. |
464
|
|
|
* @return string Full path to the newly created temporary directory. |
465
|
|
|
*/ |
466
|
|
|
public static function tempDir($dir, $prefix = '', $mode = 0775) |
467
|
|
|
{ |
468
|
|
|
if (substr($dir, -1) != '/') { |
469
|
|
|
$dir .= '/'; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
$path = ''; |
|
|
|
|
473
|
|
|
do { |
474
|
|
|
$path = $dir . $prefix . sprintf("%08X%08X", mt_rand(), mt_rand()); |
475
|
|
|
} while (!mkdir($path, $mode, true)); |
476
|
|
|
|
477
|
|
|
return $path; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* deleteDir |
482
|
|
|
* |
483
|
|
|
* This function deletes a directory and all of its contents. |
484
|
|
|
* |
485
|
|
|
* @param string $dir The (possibly non-empty) directory to delete. |
486
|
|
|
* @param bool $shred (Optional) Shred the file before deleting? |
487
|
|
|
* Defaults to false. |
488
|
|
|
*/ |
489
|
|
|
public static function deleteDir($dir, $shred = false) |
490
|
|
|
{ |
491
|
|
|
if (is_dir($dir)) { |
492
|
|
|
$objects = scandir($dir); |
493
|
|
|
foreach ($objects as $object) { |
494
|
|
|
if ($object != "." && $object != "..") { |
495
|
|
|
if (filetype($dir . "/" . $object) == "dir") { |
496
|
|
|
static::deleteDir($dir . "/" . $object); |
497
|
|
|
} else { |
498
|
|
|
if ($shred) { |
499
|
|
|
@exec('/bin/env /usr/bin/shred -u -z ' . $dir . "/" . $object); |
|
|
|
|
500
|
|
|
} else { |
501
|
|
|
@unlink($dir . "/" . $object); |
|
|
|
|
502
|
|
|
} |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
reset($objects); |
|
|
|
|
507
|
|
|
@rmdir($dir); |
|
|
|
|
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* htmlent |
513
|
|
|
* |
514
|
|
|
* This method is necessary since htmlentities() does not seem to |
515
|
|
|
* obey the default arguments as documented in the PHP manual, and |
516
|
|
|
* instead encodes accented characters incorrectly. By specifying |
517
|
|
|
* the flags and encoding, the problem is solved. |
518
|
|
|
* |
519
|
|
|
* @param string $str : A string to process with htmlentities(). |
520
|
|
|
* @return string The input string processed by htmlentities with |
521
|
|
|
* specific options. |
522
|
|
|
*/ |
523
|
|
|
public static function htmlent($str) |
524
|
|
|
{ |
525
|
|
|
return htmlentities($str, ENT_COMPAT | ENT_HTML401, 'UTF-8'); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* sendErrorAlert |
530
|
|
|
* |
531
|
|
|
* Use this function to send an error message. The $summary should |
532
|
|
|
* be a short description of the error since it is placed in the |
533
|
|
|
* subject of the email. Put a more verbose description of the |
534
|
|
|
* error in the $detail parameter. Any session variables available |
535
|
|
|
* are appended to the body of the message. |
536
|
|
|
* |
537
|
|
|
* @param string $summary A brief summary of the error (in email subject) |
538
|
|
|
* @param string $detail A detailed description of the error (in the |
539
|
|
|
* email body) |
540
|
|
|
* @param string $mailto (Optional) The destination email address. |
541
|
|
|
* Defaults to '[email protected]'. |
542
|
|
|
*/ |
543
|
|
|
public static function sendErrorAlert( |
544
|
|
|
$summary, |
545
|
|
|
$detail, |
546
|
|
|
$mailto = '[email protected]' |
547
|
|
|
) { |
548
|
|
|
$sessionvars = array( |
549
|
|
|
'idp' => 'IdP ID', |
550
|
|
|
'idpname' => 'IdP Name', |
551
|
|
|
'uid' => 'Database UID', |
552
|
|
|
'dn' => 'Cert DN', |
553
|
|
|
'firstname' => 'First Name', |
554
|
|
|
'lastname' => 'Last Name', |
555
|
|
|
'displayname' => 'Display Name', |
556
|
|
|
'ePPN' => 'ePPN', |
557
|
|
|
'ePTID' => 'ePTID', |
558
|
|
|
'openID' => 'OpenID ID', |
559
|
|
|
'oidcID' => 'OIDC ID', |
560
|
|
|
'loa' => 'LOA', |
561
|
|
|
'affiliation' => 'Affiliation', |
562
|
|
|
'ou' => 'OU', |
563
|
|
|
'memberof' => 'MemberOf', |
564
|
|
|
'acr' => 'AuthnContextClassRef', |
565
|
|
|
'entitlement' => 'Entitlement', |
566
|
|
|
'itrustuin' => 'iTrustUIN', |
567
|
|
|
'cilogon_skin' => 'Skin Name', |
568
|
|
|
'authntime' => 'Authn Time' |
569
|
|
|
); |
570
|
|
|
|
571
|
|
|
$remoteaddr = static::getServerVar('REMOTE_ADDR'); |
572
|
|
|
$remotehost = gethostbyaddr($remoteaddr); |
573
|
|
|
$mailfrom = 'From: [email protected]' . "\r\n" . |
574
|
|
|
'X-Mailer: PHP/' . phpversion(); |
575
|
|
|
$mailsubj = 'CILogon Service on ' . php_uname('n') . |
576
|
|
|
' - ' . $summary; |
577
|
|
|
$mailmsg = ' |
578
|
|
|
CILogon Service - ' . $summary . ' |
579
|
|
|
----------------------------------------------------------- |
580
|
|
|
' . $detail . ' |
581
|
|
|
|
582
|
|
|
Session Variables |
583
|
|
|
----------------- |
584
|
|
|
Timestamp = ' . date(DATE_ATOM) . ' |
585
|
|
|
Server Host = ' . static::getHN() . ' |
586
|
|
|
Remote Address= ' . $remoteaddr . ' |
587
|
|
|
' . (($remotehost !== false) ? "Remote Host = $remotehost" : '') . ' |
588
|
|
|
'; |
589
|
|
|
|
590
|
|
|
foreach ($sessionvars as $svar => $sname) { |
591
|
|
|
if (strlen($val = static::getSessionVar($svar)) > 0) { |
592
|
|
|
$mailmsg .= sprintf("%-14s= %s\n", $sname, $val); |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
mail($mailto, $mailsubj, $mailmsg, $mailfrom); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* getFirstAndLastName |
601
|
|
|
* |
602
|
|
|
* This function attempts to get the first and last name of a user |
603
|
|
|
* extracted from the 'full name' (displayName) of the user. |
604
|
|
|
* Simply pass in all name info (full, first, and last) and the |
605
|
|
|
* function first tries to break up the full name into first/last. |
606
|
|
|
* If this is not sufficient, the function checks first and last |
607
|
|
|
* name. Finally, if either first or last is blank, the function |
608
|
|
|
* duplicates first <=> last so both names have the same value. |
609
|
|
|
* Note that even with all this, you still need to check if the |
610
|
|
|
* returned (first,last) names are blank. |
611
|
|
|
* |
612
|
|
|
* @param string $full The 'full name' of the user |
613
|
|
|
* @param string $first (Optional) The 'first name' of the user |
614
|
|
|
* @param string $last (Optional) The 'last name' of the user |
615
|
|
|
* @return array An array 'list(firstname,lastname)' |
616
|
|
|
*/ |
617
|
|
|
public static function getFirstAndLastName($full, $first = '', $last = '') |
618
|
|
|
{ |
619
|
|
|
$firstname = ''; |
620
|
|
|
$lastname = ''; |
621
|
|
|
|
622
|
|
|
# Try to split the incoming $full name into first and last names |
623
|
|
|
if (strlen($full) > 0) { |
624
|
|
|
$names = preg_split('/\s+/', $full, 2); |
625
|
|
|
$firstname = @$names[0]; |
626
|
|
|
$lastname = @$names[1]; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
# If either first or last name blank, then use incoming $first and $last |
630
|
|
|
if (strlen($firstname) == 0) { |
631
|
|
|
$firstname = $first; |
632
|
|
|
} |
633
|
|
|
if (strlen($lastname) == 0) { |
634
|
|
|
$lastname = $last; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
# Finally, if only a single name, copy first name <=> last name |
638
|
|
|
if (strlen($lastname) == 0) { |
639
|
|
|
$lastname = $firstname; |
640
|
|
|
} |
641
|
|
|
if (strlen($firstname) == 0) { |
642
|
|
|
$firstname = $lastname; |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
# Return both names as an array (i.e., use list($first,last)=...) |
646
|
|
|
return array($firstname,$lastname); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* getHN |
651
|
|
|
* |
652
|
|
|
* This function calculates and returns the 'hostname' for the |
653
|
|
|
* server. It first checks HTTP_HOST. If not set, it returns |
654
|
|
|
* DEFAULT_HOSTNAME. This is needed by command line scripts. |
655
|
|
|
* |
656
|
|
|
* @return string The 'Hostname' for the web server. |
657
|
|
|
*/ |
658
|
|
|
public static function getHN() |
659
|
|
|
{ |
660
|
|
|
$thehostname = static::getServerVar('HTTP_HOST'); |
661
|
|
|
if (strlen($thehostname) == 0) { |
662
|
|
|
$thehostname = DEFAULT_HOSTNAME; |
|
|
|
|
663
|
|
|
} |
664
|
|
|
return $thehostname; |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* getDN |
669
|
|
|
* |
670
|
|
|
* This function calculates and returns the 'domainname' for the |
671
|
|
|
* server. It uses the hostname value calculated by getHN() and |
672
|
|
|
* uses the last two segments. |
673
|
|
|
* |
674
|
|
|
* @return string The 'Domainname' for the web server. |
675
|
|
|
*/ |
676
|
|
|
public static function getDN() |
677
|
|
|
{ |
678
|
|
|
$thedomainname = static::getHN(); |
679
|
|
|
if (preg_match('/[^\.]+\.[^\.]+$/', $thedomainname, $matches)) { |
680
|
|
|
$thedomainname = $matches[0]; |
681
|
|
|
} |
682
|
|
|
return $thedomainname; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* getAuthzUrl |
687
|
|
|
* |
688
|
|
|
* This funtion takes in the name of an IdP (e.g., 'Google') and |
689
|
|
|
* returns the assoicated OAuth2 authorization URL. |
690
|
|
|
* |
691
|
|
|
* @param string $idp The name of an OAuth2 Identity Provider. |
692
|
|
|
* @return string The authorization URL for the given IdP. |
693
|
|
|
*/ |
694
|
|
|
public static function getAuthzUrl($idp) |
695
|
|
|
{ |
696
|
|
|
$url = null; |
697
|
|
|
$idptourl = array( |
698
|
|
|
'Google' => 'https://accounts.google.com/o/oauth2/auth', |
699
|
|
|
'GitHub' => 'https://github.com/login/oauth/authorize', |
700
|
|
|
'ORCID' => 'https://orcid.org/oauth/authorize', |
701
|
|
|
); |
702
|
|
|
if (array_key_exists($idp, $idptourl)) { |
703
|
|
|
$url = $idptourl[$idp]; |
704
|
|
|
} |
705
|
|
|
return $url; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* getAuthzIdP |
710
|
|
|
* |
711
|
|
|
* This function takes in the OAuth2 authorization URL and returns |
712
|
|
|
* the associated pretty-print name of the IdP. |
713
|
|
|
* |
714
|
|
|
* @param string $url The authorization URL of an OAuth2 Identity Provider. |
715
|
|
|
* @return string The name of the IdP. |
716
|
|
|
*/ |
717
|
|
|
public static function getAuthzIdP($url) |
718
|
|
|
{ |
719
|
|
|
$idp = null; |
720
|
|
|
$urltoidp = array( |
721
|
|
|
'https://accounts.google.com/o/oauth2/auth' => 'Google', |
722
|
|
|
'https://github.com/login/oauth/authorize' => 'GitHub', |
723
|
|
|
'https://orcid.org/oauth/authorize' => 'ORCID', |
724
|
|
|
); |
725
|
|
|
if (array_key_exists($url, $urltoidp)) { |
726
|
|
|
$idp = $urltoidp[$url]; |
727
|
|
|
} |
728
|
|
|
return $idp; |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
/** |
732
|
|
|
* gotUserAttributes |
733
|
|
|
* |
734
|
|
|
* This function returns true if the PHP session contains all of the |
735
|
|
|
* necessary user/IdP attributes to fetch an X.509 certificate. This |
736
|
|
|
* means that at least one of (remoteuser, ePPN, ePTID, openidID, |
737
|
|
|
* oidcID) must be set, as well as idp (entityId), idpname, firstname, |
738
|
|
|
* lastname, and emailaddr. Also, the emailaddr must conform to valid |
739
|
|
|
* email formatting. |
740
|
|
|
* |
741
|
|
|
* @return bool True if all user/IdP attributes necessary to form the |
742
|
|
|
* distinguished name (DN) for X.509 certificates are |
743
|
|
|
* present in the PHP session. False otherwise. |
744
|
|
|
*/ |
745
|
|
|
public static function gotUserAttributes() |
746
|
|
|
{ |
747
|
|
|
$retval = false; // Assume we don't have all user attributes |
748
|
|
|
if ( |
749
|
|
|
((strlen(Util::getSessionVar('remoteuser')) > 0) || |
750
|
|
|
(strlen(Util::getSessionVar('ePPN')) > 0) || |
751
|
|
|
(strlen(Util::getSessionVar('ePTID')) > 0) || |
752
|
|
|
(strlen(Util::getSessionVar('openidID')) > 0) || |
753
|
|
|
(strlen(Util::getSessionVar('oidcID')) > 0)) && |
754
|
|
|
(strlen(Util::getSessionVar('idp')) > 0) && |
755
|
|
|
(strlen(Util::getSessionVar('idpname')) > 0) && |
756
|
|
|
(strlen(Util::getSessionVar('firstname')) > 0) && |
757
|
|
|
(strlen(Util::getSessionVar('lastname')) > 0) && |
758
|
|
|
(strlen(Util::getSessionVar('emailaddr')) > 0) && |
759
|
|
|
(filter_var(Util::getSessionVar('emailaddr'), FILTER_VALIDATE_EMAIL)) |
760
|
|
|
) { |
761
|
|
|
$retval = true; |
762
|
|
|
} |
763
|
|
|
return $retval; |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* saveUserToDataStore |
768
|
|
|
* |
769
|
|
|
* This function is called when a user logs on to save identity |
770
|
|
|
* information to the datastore. As it is used by both Shibboleth |
771
|
|
|
* and OpenID Identity Providers, some parameters passed in may |
772
|
|
|
* be blank (empty string). If the function verifies that the minimal |
773
|
|
|
* sets of parameters are valid, the dbservice servlet is called |
774
|
|
|
* to save the user info. Then various session variables are set |
775
|
|
|
* for use by the program later on. In case of error, an email |
776
|
|
|
* alert is sent showing the missing parameters. |
777
|
|
|
* |
778
|
|
|
* @param mixed $args Variable number of paramters ordered as follows: |
779
|
|
|
* remoteuser -The REMOTE_USER from HTTP headers |
780
|
|
|
* idp - The provider IdP Identifier / URL endpoint |
781
|
|
|
* idpname - The pretty print provider IdP name |
782
|
|
|
* firstname - The user's first name |
783
|
|
|
* lastname - The user's last name |
784
|
|
|
* displayname - The user's display name |
785
|
|
|
* emailaddr- The user's email address |
786
|
|
|
* loa - The level of assurance (e.g., openid/basic/silver) |
787
|
|
|
* ePPN - User's ePPN (for SAML IdPs) |
788
|
|
|
* ePTID - User's ePTID (for SAML IdPs) |
789
|
|
|
* openidID - User's OpenID 2.0 Identifier (Google deprecated) |
790
|
|
|
* oidcID - User's OpenID Connect Identifier |
791
|
|
|
* affiliation - User's affiliation |
792
|
|
|
* ou - User's organizational unit (OU) |
793
|
|
|
* memberof - User's isMemberOf group info |
794
|
|
|
* acr - Authentication Context Class Ref |
795
|
|
|
* entitlement - User's entitlement |
796
|
|
|
* itrustuin - User's univerity ID number |
797
|
|
|
*/ |
798
|
|
|
public static function saveUserToDataStore(...$args) |
799
|
|
|
{ |
800
|
|
|
$dbs = new DBService(); |
801
|
|
|
|
802
|
|
|
// Save the passed-in variables to the session for later use |
803
|
|
|
// (e.g., by the error handler in handleGotUser). Then get these |
804
|
|
|
// session variables into local vars for ease of use. |
805
|
|
|
static::setUserAttributeSessionVars(...$args); |
806
|
|
|
|
807
|
|
|
$remoteuser = static::getSessionVar('remoteuser'); |
808
|
|
|
$idp = static::getSessionVar('idp'); |
809
|
|
|
$idpname = static::getSessionVar('idpname'); |
810
|
|
|
$firstname = static::getSessionVar('firstname'); |
811
|
|
|
$lastname = static::getSessionVar('lastname'); |
812
|
|
|
$displayname = static::getSessionVar('displayname'); |
813
|
|
|
$emailaddr = static::getSessionvar('emailaddr'); |
814
|
|
|
$loa = static::getSessionVar('loa'); |
815
|
|
|
$ePPN = static::getSessionVar('ePPN'); |
816
|
|
|
$ePTID = static::getSessionVar('ePTID'); |
817
|
|
|
$openidID = static::getSessionVar('openidID'); |
818
|
|
|
$oidcID = static::getSessionVar('oidcID'); |
819
|
|
|
$affiliation = static::getSessionVar('affiliation'); |
820
|
|
|
$ou = static::getSessionVar('ou'); |
821
|
|
|
$memberof = static::getSessionVar('memberof'); |
822
|
|
|
$acr = static::getSessionVar('acr'); |
823
|
|
|
$entitlement = static::getSessionVar('entitlement'); |
824
|
|
|
$itrustuin = static::getSessionVar('itrustuin'); |
825
|
|
|
|
826
|
|
|
// Make sure parameters are not empty strings, and email is valid |
827
|
|
|
// Must have at least one of remoteuser/ePPN/ePTID/openidID/oidcID |
828
|
|
|
if (static::gotUserAttributes()) { |
829
|
|
|
// For the new Google OAuth 2.0 endpoint, we want to keep the |
830
|
|
|
// old Google OpenID endpoint URL in the database (so user does |
831
|
|
|
// not get a new certificate subject DN). Change the idp |
832
|
|
|
// and idpname to the old Google OpenID values. |
833
|
|
|
if ( |
834
|
|
|
($idpname == 'Google+') || |
835
|
|
|
($idp == static::getAuthzUrl('Google')) |
836
|
|
|
) { |
837
|
|
|
$idpname = 'Google'; |
838
|
|
|
$idp = 'https://www.google.com/accounts/o8/id'; |
839
|
|
|
} |
840
|
|
|
|
841
|
|
|
// In the database, keep a consistent ProviderId format: only |
842
|
|
|
// allow 'http' (not 'https') and remove any 'www.' prefix. |
843
|
|
|
if ($loa == 'openid') { |
844
|
|
|
$idp = preg_replace('%^https://(www\.)?%', 'http://', $idp); |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
$result = $dbs->getUser( |
848
|
|
|
$remoteuser, |
849
|
|
|
$idp, |
850
|
|
|
$idpname, |
851
|
|
|
$firstname, |
852
|
|
|
$lastname, |
853
|
|
|
$displayname, |
854
|
|
|
$emailaddr, |
855
|
|
|
$ePPN, |
856
|
|
|
$ePTID, |
857
|
|
|
$openidID, |
858
|
|
|
$oidcID, |
859
|
|
|
$affiliation, |
860
|
|
|
$ou, |
861
|
|
|
$memberof, |
862
|
|
|
$acr, |
863
|
|
|
$entitlement, |
864
|
|
|
$itrustuin |
865
|
|
|
); |
866
|
|
|
static::setSessionVar('uid', $dbs->user_uid); |
867
|
|
|
static::setSessionVar('dn', $dbs->distinguished_name); |
868
|
|
|
static::setSessionVar('status', $dbs->status); |
869
|
|
|
if (!$result) { |
870
|
|
|
static::sendErrorAlert( |
871
|
|
|
'dbService Error', |
872
|
|
|
'Error calling dbservice action "getUser" in ' . |
873
|
|
|
'saveUserToDatastore() method.' |
874
|
|
|
); |
875
|
|
|
} |
876
|
|
|
} else { // Missing one or more required attributes |
877
|
|
|
static::setSessionVar( |
878
|
|
|
'status', |
879
|
|
|
DBService::$STATUS['STATUS_MISSING_PARAMETER_ERROR'] |
880
|
|
|
); |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
// If 'status' is not STATUS_OK*, then send an error email |
884
|
|
|
$status = static::getSessionVar('status'); |
885
|
|
|
if ($status & 1) { // Bad status codes are odd |
886
|
|
|
// For missing parameter errors, log an error message |
887
|
|
|
if ( |
888
|
|
|
$status == |
889
|
|
|
DBService::$STATUS['STATUS_MISSING_PARAMETER_ERROR'] |
890
|
|
|
) { |
891
|
|
|
$log = new Loggit(); |
892
|
|
|
$log->error('STATUS_MISSING_PARAMETER_ERROR', true); |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
// For other dbservice errors OR for any error involving |
896
|
|
|
// LIGO (e.g., missing parameter error), send email alert. |
897
|
|
|
if ( |
898
|
|
|
($status != |
899
|
|
|
DBService::$STATUS['STATUS_MISSING_PARAMETER_ERROR']) || |
900
|
|
|
(preg_match('/ligo\.org/', $idp)) |
901
|
|
|
) { |
902
|
|
|
$mailto = '[email protected]'; |
903
|
|
|
|
904
|
|
|
// CIL-205 - Notify LIGO about IdP login errors. |
905
|
|
|
// Set DISABLE_LIGO_ALERTS to true in the top-level |
906
|
|
|
// config.php file to stop LIGO failures |
907
|
|
|
// from being sent to '[email protected]', but still |
908
|
|
|
// sent to '[email protected]'. |
909
|
|
|
if (preg_match('/ligo\.org/', $idp)) { |
910
|
|
|
if (DISABLE_LIGO_ALERTS) { |
|
|
|
|
911
|
|
|
$mailto = ''; |
912
|
|
|
} |
913
|
|
|
$mailto .= ((strlen($mailto) > 0) ? ',' : '') . |
914
|
|
|
'[email protected]'; |
915
|
|
|
} |
916
|
|
|
|
917
|
|
|
static::sendErrorAlert( |
918
|
|
|
'Failure in ' . |
919
|
|
|
(($loa == 'openid') ? '' : '/secure') . '/getuser/', |
920
|
|
|
'Remote_User = ' . ((strlen($remoteuser) > 0) ? |
921
|
|
|
$remoteuser : '<MISSING>') . "\n" . |
922
|
|
|
'IdP ID = ' . ((strlen($idp) > 0) ? |
923
|
|
|
$idp : '<MISSING>') . "\n" . |
924
|
|
|
'IdP Name = ' . ((strlen($idpname) > 0) ? |
925
|
|
|
$idpname : '<MISSING>') . "\n" . |
926
|
|
|
'First Name = ' . ((strlen($firstname) > 0) ? |
927
|
|
|
$firstname : '<MISSING>') . "\n" . |
928
|
|
|
'Last Name = ' . ((strlen($lastname) > 0) ? |
929
|
|
|
$lastname : '<MISSING>') . "\n" . |
930
|
|
|
'Display Name = ' . ((strlen($displayname) > 0) ? |
931
|
|
|
$displayname : '<MISSING>') . "\n" . |
932
|
|
|
'Email Address = ' . ((strlen($emailaddr) > 0) ? |
933
|
|
|
$emailaddr : '<MISSING>') . "\n" . |
934
|
|
|
'ePPN = ' . ((strlen($ePPN) > 0) ? |
935
|
|
|
$ePPN : '<MISSING>') . "\n" . |
936
|
|
|
'ePTID = ' . ((strlen($ePTID) > 0) ? |
937
|
|
|
$ePTID : '<MISSING>') . "\n" . |
938
|
|
|
'OpenID ID = ' . ((strlen($openidID) > 0) ? |
939
|
|
|
$openidID : '<MISSING>') . "\n" . |
940
|
|
|
'OIDC ID = ' . ((strlen($oidcID) > 0) ? |
941
|
|
|
$oidcID : '<MISSING>') . "\n" . |
942
|
|
|
'Affiliation = ' . ((strlen($affiliation) > 0) ? |
943
|
|
|
$affiliation : '<MISSING>') . "\n" . |
944
|
|
|
'OU = ' . ((strlen($ou) > 0) ? |
945
|
|
|
$ou : '<MISSING>') . "\n" . |
946
|
|
|
'MemberOf = ' . ((strlen($memberof) > 0) ? |
947
|
|
|
$memberof : '<MISSING>') . "\n" . |
948
|
|
|
'ACR = ' . ((strlen($acr) > 0) ? |
949
|
|
|
$acr : '<MISSING>') . "\n" . |
950
|
|
|
'Entitlement = ' . ((strlen($entitlement) > 0) ? |
951
|
|
|
$entitlement : '<MISSING>') . "\n" . |
952
|
|
|
'iTrustUIN = ' . ((strlen($itrustuin) > 0) ? |
953
|
|
|
$itrustuin : '<MISSING>') . "\n" . |
954
|
|
|
'Database UID = ' . ((strlen( |
955
|
|
|
$i = static::getSessionVar('uid') |
956
|
|
|
) > 0) ? $i : '<MISSING>') . "\n" . |
957
|
|
|
'Status Code = ' . ((strlen( |
|
|
|
|
958
|
|
|
$i = array_search( |
|
|
|
|
959
|
|
|
$status, |
960
|
|
|
DBService::$STATUS |
961
|
|
|
) |
962
|
|
|
) > 0) ? $i : '<MISSING>'), |
963
|
|
|
$mailto |
964
|
|
|
); |
965
|
|
|
} |
966
|
|
|
static::unsetSessionVar('authntime'); |
967
|
|
|
} |
968
|
|
|
} |
969
|
|
|
|
970
|
|
|
/** |
971
|
|
|
* setUserAttributeSessionVars |
972
|
|
|
* |
973
|
|
|
* This method is called by saveUserToDatastore to put the passsed-in |
974
|
|
|
* variables into the PHP session for later use. |
975
|
|
|
* |
976
|
|
|
* @param mixed $args Variable number of user attribute paramters |
977
|
|
|
* ordered as shown in the $attrs array below. |
978
|
|
|
*/ |
979
|
|
|
public static function setUserAttributeSessionVars(...$args) |
980
|
|
|
{ |
981
|
|
|
$attrs = array('remoteuser', 'idp', 'idpname', 'firstname', |
982
|
|
|
'lastname', 'displayname', 'emailaddr', |
983
|
|
|
'loa', 'ePPN', 'ePTID', 'openidID', 'oidcID', |
984
|
|
|
'affiliation', 'ou', 'memberof', 'acr', |
985
|
|
|
'entitlement', 'itrustuin'); |
986
|
|
|
$numargs = count($args); |
987
|
|
|
for ($i = 0; $i < $numargs; $i++) { |
988
|
|
|
static::setSessionVar($attrs[$i], $args[$i]); |
989
|
|
|
} |
990
|
|
|
|
991
|
|
|
// CACC-238 - Set loa to "silver" if the following are true: |
992
|
|
|
// (1) loa contains https://refeds.org/assurance/profile/cappuccino |
993
|
|
|
// (2) acr is either https://refeds.org/profile/sfa or |
994
|
|
|
// https://refeds.org/profile/mfa |
995
|
|
|
if ( |
996
|
|
|
(preg_match('%https://refeds.org/assurance/profile/cappuccino%', static::getSessionVar('loa'))) && |
997
|
|
|
(preg_match('%https://refeds.org/profile/[ms]fa%', static::getSessionVar('acr'))) |
998
|
|
|
) { |
999
|
|
|
static::setSessionVar('loa', 'http://incommonfederation.org/assurance/silver'); |
1000
|
|
|
} |
1001
|
|
|
|
1002
|
|
|
static::setSessionVar('status', '0'); |
1003
|
|
|
static::setSessionVar('submit', static::getSessionVar('responsesubmit')); |
1004
|
|
|
static::setSessionVar('authntime', time()); |
1005
|
|
|
static::unsetSessionVar('responsesubmit'); |
1006
|
|
|
static::getCsrf()->setCookieAndSession(); |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
|
|
/** |
1010
|
|
|
* unsetClientSessionVars |
1011
|
|
|
* |
1012
|
|
|
* This function removes all of the PHP session variables related to |
1013
|
|
|
* the client session. |
1014
|
|
|
*/ |
1015
|
|
|
public static function unsetClientSessionVars() |
1016
|
|
|
{ |
1017
|
|
|
static::unsetSessionVar('submit'); |
1018
|
|
|
|
1019
|
|
|
// Specific to 'Download Certificate' page |
1020
|
|
|
static::unsetSessionVar('p12'); |
1021
|
|
|
static::unsetSessionVar('p12lifetime'); |
1022
|
|
|
static::unsetSessionVar('p12multiplier'); |
1023
|
|
|
|
1024
|
|
|
// Specific to OAuth 1.0a flow |
1025
|
|
|
static::unsetSessionVar('portalstatus'); |
1026
|
|
|
static::unsetSessionVar('callbackuri'); |
1027
|
|
|
static::unsetSessionVar('successuri'); |
1028
|
|
|
static::unsetSessionVar('failureuri'); |
1029
|
|
|
static::unsetSessionVar('portalname'); |
1030
|
|
|
static::unsetSessionVar('tempcred'); |
1031
|
|
|
|
1032
|
|
|
// Specific to OIDC flow |
1033
|
|
|
static::unsetSessionVar('clientparams'); |
1034
|
|
|
} |
1035
|
|
|
|
1036
|
|
|
/** |
1037
|
|
|
* unsetUserSessionVars |
1038
|
|
|
* |
1039
|
|
|
* This function removes all of the PHP session variables related to |
1040
|
|
|
* the user's session. This will force the user to log on (again) |
1041
|
|
|
* with their IdP and call the 'getuser' script to repopulate the PHP |
1042
|
|
|
* session. |
1043
|
|
|
*/ |
1044
|
|
|
public static function unsetUserSessionVars() |
1045
|
|
|
{ |
1046
|
|
|
// Needed for verifyCurrentUserSession |
1047
|
|
|
static::unsetSessionVar('idp'); |
1048
|
|
|
static::unsetSessionVar('idpname'); |
1049
|
|
|
static::unsetSessionVar('status'); |
1050
|
|
|
static::unsetSessionVar('uid'); |
1051
|
|
|
static::unsetSessionVar('dn'); |
1052
|
|
|
static::unsetSessionVar('authntime'); |
1053
|
|
|
|
1054
|
|
|
// Variables set by getuser |
1055
|
|
|
static::unsetSessionVar('firstname'); |
1056
|
|
|
static::unsetSessionVar('lastname'); |
1057
|
|
|
static::unsetSessionVar('displayname'); |
1058
|
|
|
static::unsetSessionVar('emailaddr'); |
1059
|
|
|
static::unsetSessionVar('loa'); |
1060
|
|
|
static::unsetSessionVar('ePPN'); |
1061
|
|
|
static::unsetSessionVar('ePTID'); |
1062
|
|
|
static::unsetSessionVar('openidID'); |
1063
|
|
|
static::unsetSessionVar('oidcID'); |
1064
|
|
|
static::unsetSessionVar('affiliation'); |
1065
|
|
|
static::unsetSessionVar('ou'); |
1066
|
|
|
static::unsetSessionVar('memberof'); |
1067
|
|
|
static::unsetSessionVar('acr'); |
1068
|
|
|
static::unsetSessionVar('entitlement'); |
1069
|
|
|
static::unsetSessionVar('itrustuin'); |
1070
|
|
|
|
1071
|
|
|
// Current skin |
1072
|
|
|
static::unsetSessionVar('cilogon_skin'); |
1073
|
|
|
} |
1074
|
|
|
|
1075
|
|
|
/** |
1076
|
|
|
* unsetAllUserSessionVars |
1077
|
|
|
* |
1078
|
|
|
* This is a convenience method to clear all session variables related |
1079
|
|
|
* to the client and the user. |
1080
|
|
|
*/ |
1081
|
|
|
public static function unsetAllUserSessionVars() |
1082
|
|
|
{ |
1083
|
|
|
static::unsetClientSessionVars(); |
1084
|
|
|
static::unsetUserSessionVars(); |
1085
|
|
|
} |
1086
|
|
|
|
1087
|
|
|
/** |
1088
|
|
|
* verifySessionAndCall |
1089
|
|
|
* |
1090
|
|
|
* This function is a convenience method called by several cases in the |
1091
|
|
|
* main 'switch' call at the top of the index.php file. I noticed |
1092
|
|
|
* a pattern where verifyCurrentUserSession() was called to verify the |
1093
|
|
|
* current user session. Upon success, one or two functions were called |
1094
|
|
|
* to continue program, flow. Upon failure, cookies and session |
1095
|
|
|
* variables were cleared, and the main Logon page was printed. This |
1096
|
|
|
* function encapsulates that pattern. If the user's session is valid, |
1097
|
|
|
* the passed-in $func is called, possibly with parameters passed in as |
1098
|
|
|
* an array. The function returns true if the session is verified, so |
1099
|
|
|
* that other functions may be called upon return. |
1100
|
|
|
* |
1101
|
|
|
* @param callable $func The function to call if the current session is |
1102
|
|
|
* successfully verified. |
1103
|
|
|
* @param array $params (Optional) An array of parameters to pass to the |
1104
|
|
|
* function. Defaults to empty array, meaning zero parameters. |
1105
|
|
|
*/ |
1106
|
|
|
public static function verifySessionAndCall($func, $params = array()) |
1107
|
|
|
{ |
1108
|
|
|
$retval = false; |
1109
|
|
|
if (Content::verifyCurrentUserSession()) { // Verify PHP session is valid |
1110
|
|
|
$retval = true; |
1111
|
|
|
call_user_func_array($func, $params); |
1112
|
|
|
} else { |
1113
|
|
|
printLogonPage(true); // Clear cookies and session vars too |
|
|
|
|
1114
|
|
|
} |
1115
|
|
|
return $retval; |
1116
|
|
|
} |
1117
|
|
|
|
1118
|
|
|
/** |
1119
|
|
|
* isEduGAINAndGetCert |
1120
|
|
|
* |
1121
|
|
|
* This function checks to see if the current session IdP is an |
1122
|
|
|
* eduGAIN IdP (i.e., not Registered By InCommon) and the IdP does not |
1123
|
|
|
* have both the REFEDS R&S and SIRTFI extensions in metadata. If so, |
1124
|
|
|
* check to see if the transaction could be used to fetch a |
1125
|
|
|
* certificate. (The only time the transaction is not used to fetch |
1126
|
|
|
* a cert is during OIDC without the 'getcert' scope.) If all that is |
1127
|
|
|
* true, then return true. Otherwise return false. |
1128
|
|
|
* |
1129
|
|
|
* @param string $idp (optional) The IdP entityID. If empty, read value |
1130
|
|
|
* from PHP session. |
1131
|
|
|
* @param string $idpname (optional) The IdP display name. If empty, |
1132
|
|
|
* read value from PHP session. |
1133
|
|
|
* @return bool True if the current IdP is an eduGAIN IdP without |
1134
|
|
|
* both REFEDS R&S and SIRTFI, AND the session could be |
1135
|
|
|
* used to get a certificate. |
1136
|
|
|
*/ |
1137
|
|
|
public static function isEduGAINAndGetCert($idp = '', $idpname = '') |
1138
|
|
|
{ |
1139
|
|
|
$retval = false; // Assume not eduGAIN IdP and getcert |
1140
|
|
|
|
1141
|
|
|
// If $idp or $idpname not passed in, get from current session. |
1142
|
|
|
if (strlen($idp) == 0) { |
1143
|
|
|
$idp = static::getSessionVar('idp'); |
1144
|
|
|
} |
1145
|
|
|
if (strlen($idpname) == 0) { |
1146
|
|
|
$idpname = static::getSessionVar('idpname'); |
1147
|
|
|
} |
1148
|
|
|
|
1149
|
|
|
// Check if this was an OIDC transaction, and if the |
1150
|
|
|
// 'getcert' scope was requested. |
1151
|
|
|
$oidcscopegetcert = false; |
1152
|
|
|
$oidctrans = false; |
1153
|
|
|
$clientparams = json_decode(static::getSessionVar('clientparams'), true); |
1154
|
|
|
if (isset($clientparams['scope'])) { |
1155
|
|
|
$oidctrans = true; |
1156
|
|
|
if ( |
1157
|
|
|
preg_match( |
1158
|
|
|
'/edu\.uiuc\.ncsa\.myproxy\.getcert/', |
1159
|
|
|
$clientparams['scope'] |
1160
|
|
|
) |
1161
|
|
|
) { |
1162
|
|
|
$oidcscopegetcert = true; |
1163
|
|
|
} |
1164
|
|
|
} |
1165
|
|
|
|
1166
|
|
|
// First, make sure $idp was set and is not an OAuth2 IdP. |
1167
|
|
|
$idplist = static::getIdpList(); |
1168
|
|
|
if ( |
1169
|
|
|
((strlen($idp) > 0) && |
1170
|
|
|
(strlen($idpname) > 0) && |
1171
|
|
|
(!in_array($idpname, static::$oauth2idps))) && |
1172
|
|
|
( |
1173
|
|
|
// Next, check for eduGAIN without REFEDS R&S and SIRTFI |
1174
|
|
|
((!$idplist->isRegisteredByInCommon($idp)) && |
1175
|
|
|
((!$idplist->isREFEDSRandS($idp)) || |
1176
|
|
|
(!$idplist->isSIRTFI($idp)) |
1177
|
|
|
) |
1178
|
|
|
) && |
1179
|
|
|
// Next, check if user could get X509 cert, |
1180
|
|
|
// i.e., OIDC getcert scope, or a non-OIDC |
1181
|
|
|
// transaction such as PKCS12, JWS, or OAuth 1.0a |
1182
|
|
|
($oidcscopegetcert || !$oidctrans) |
1183
|
|
|
) |
1184
|
|
|
) { |
1185
|
|
|
$retval = true; |
1186
|
|
|
} |
1187
|
|
|
return $retval; |
1188
|
|
|
} |
1189
|
|
|
|
1190
|
|
|
/** |
1191
|
|
|
* setPortalOrCookie |
1192
|
|
|
* |
1193
|
|
|
* This is a convenience function for a set of operations that is done |
1194
|
|
|
* a few times in Content.php. It first checks if the name of the portal |
1195
|
|
|
* in the PortalCookie is empty. If not, then it sets the PortalCookie |
1196
|
|
|
* key/value pair. Otherwise, it sets the 'normal' cookie key/value |
1197
|
|
|
* pair. |
1198
|
|
|
* |
1199
|
|
|
* @param PortalCookie $pc The PortalCookie to read/write. If the portal |
1200
|
|
|
* name is empty, then use the 'normal' cookie instead. |
1201
|
|
|
* @param string $key The key of the PortalCookie or 'normal' cookie to |
1202
|
|
|
* set. |
1203
|
|
|
* @param string $value The value to set for the $key. |
1204
|
|
|
* @param bool $save (optional) If set to true, attempt to write the |
1205
|
|
|
* PortalCookie. Defaults to false. |
1206
|
|
|
*/ |
1207
|
|
|
public static function setPortalOrCookie($pc, $key, $value, $save = false) |
1208
|
|
|
{ |
1209
|
|
|
$pn = $pc->getPortalName(); |
1210
|
|
|
// If the portal name is valid, then set the PortalCookie key/value |
1211
|
|
|
if (strlen($pn) > 0) { |
1212
|
|
|
$pc->set($key, $value); |
1213
|
|
|
if ($save) { |
1214
|
|
|
$pc->write(); |
1215
|
|
|
} |
1216
|
|
|
} else { // If portal name is not valid, then use the 'normal' cookie |
1217
|
|
|
if (strlen($value) > 0) { |
1218
|
|
|
Util::setCookieVar($key, $value); |
1219
|
|
|
} else { // If $value is empty, then UNset the 'normal' cookie |
1220
|
|
|
Util::unsetCookieVar($key); |
1221
|
|
|
} |
1222
|
|
|
} |
1223
|
|
|
} |
1224
|
|
|
|
1225
|
|
|
/** |
1226
|
|
|
* getOIDCClientParams |
1227
|
|
|
* |
1228
|
|
|
* This function addresses CIL-618 and reads OIDC client information |
1229
|
|
|
* directly from the database. It is a replacement for |
1230
|
|
|
* $dbs->getClient($clientparams['client_id']) which calls |
1231
|
|
|
* '/dbService?action=getClient&client_id=...'. This gives the PHP |
1232
|
|
|
* '/authorize' endpoint access to additional OIDC client parameters |
1233
|
|
|
* without having to rewrite the '/dbService?action=getClient' endpoint. |
1234
|
|
|
* |
1235
|
|
|
* @param array $clientparams An array of client parameters which gets |
1236
|
|
|
* stored in the PHP session. The keys of the array are |
1237
|
|
|
* the column names of the 'client' table in the 'ciloa2' |
1238
|
|
|
* database, prefixed by 'client_'. |
1239
|
|
|
*/ |
1240
|
|
|
public static function getOIDCClientParams(&$clientparams) |
1241
|
|
|
{ |
1242
|
|
|
$retval = false; |
1243
|
|
|
if (strlen(@$clientparams['client_id']) > 0) { |
1244
|
|
|
$dsn = array( |
1245
|
|
|
'phptype' => 'mysqli', |
1246
|
|
|
'username' => MYSQLI_USERNAME, |
|
|
|
|
1247
|
|
|
'password' => MYSQLI_PASSWORD, |
|
|
|
|
1248
|
|
|
'database' => 'ciloa2', |
1249
|
|
|
'hostspec' => 'localhost' |
1250
|
|
|
); |
1251
|
|
|
|
1252
|
|
|
$opts = array( |
1253
|
|
|
'persistent' => true, |
1254
|
|
|
'portability' => DB_PORTABILITY_ALL |
1255
|
|
|
); |
1256
|
|
|
|
1257
|
|
|
$db = DB::connect($dsn, $opts); |
1258
|
|
|
if (!PEAR::isError($db)) { |
1259
|
|
|
$data = $db->getRow( |
1260
|
|
|
'SELECT * from clients WHERE client_id = ?', |
1261
|
|
|
array($clientparams['client_id']), |
1262
|
|
|
DB_FETCHMODE_ASSOC |
1263
|
|
|
); |
1264
|
|
|
if (!DB::isError($data)) { |
1265
|
|
|
if (!empty($data)) { |
1266
|
|
|
foreach ($data as $key => $value) { |
1267
|
|
|
$clientparams['client_' . $key] = $value; |
1268
|
|
|
} |
1269
|
|
|
$clientparams['clientstatus'] = DBService::$STATUS['STATUS_OK']; |
1270
|
|
|
$retval = true; |
1271
|
|
|
} |
1272
|
|
|
} |
1273
|
|
|
$db->disconnect(); |
1274
|
|
|
} |
1275
|
|
|
} |
1276
|
|
|
return $retval; |
1277
|
|
|
} |
1278
|
|
|
|
1279
|
|
|
/** |
1280
|
|
|
* getMinMaxLifetimes |
1281
|
|
|
* |
1282
|
|
|
* This function checks the skin's configuration to see if either or |
1283
|
|
|
* both of minlifetime and maxlifetime in the specified config.xml |
1284
|
|
|
* block have been set. If not, default to minlifetime of 1 (hour) and |
1285
|
|
|
* the specified defaultmaxlifetime. |
1286
|
|
|
* |
1287
|
|
|
* @param string $section The XML section block from which to read the |
1288
|
|
|
* minlifetime and maxlifetime values. Can be one of the |
1289
|
|
|
* following: 'pkcs12' or 'delegate'. |
1290
|
|
|
* @param int $defaultmaxlifetime Default maxlifetime (in hours) for the |
1291
|
|
|
* credential. |
1292
|
|
|
* @return array An array consisting of two entries: the minimum and |
1293
|
|
|
* maximum lifetimes (in hours) for a credential. |
1294
|
|
|
*/ |
1295
|
|
|
public static function getMinMaxLifetimes($section, $defaultmaxlifetime) |
1296
|
|
|
{ |
1297
|
|
|
$minlifetime = 1; // Default minimum lifetime is 1 hour |
1298
|
|
|
$maxlifetime = $defaultmaxlifetime; |
1299
|
|
|
$skin = Util::getSkin(); |
1300
|
|
|
$skinminlifetime = $skin->getConfigOption($section, 'minlifetime'); |
1301
|
|
|
// Read the skin's minlifetime value from the specified section |
1302
|
|
|
if ((!is_null($skinminlifetime)) && ((int)$skinminlifetime > 0)) { |
1303
|
|
|
$minlifetime = max($minlifetime, (int)$skinminlifetime); |
1304
|
|
|
// Make sure $minlifetime is less than $maxlifetime; |
1305
|
|
|
$minlifetime = min($minlifetime, $maxlifetime); |
1306
|
|
|
} |
1307
|
|
|
// Read the skin's maxlifetime value from the specified section |
1308
|
|
|
$skinmaxlifetime = $skin->getConfigOption($section, 'maxlifetime'); |
1309
|
|
|
if ((!is_null($skinmaxlifetime)) && ((int)$skinmaxlifetime) > 0) { |
1310
|
|
|
$maxlifetime = min($maxlifetime, (int)$skinmaxlifetime); |
1311
|
|
|
// Make sure $maxlifetime is greater than $minlifetime |
1312
|
|
|
$maxlifetime = max($minlifetime, $maxlifetime); |
1313
|
|
|
} |
1314
|
|
|
|
1315
|
|
|
return array($minlifetime, $maxlifetime); |
1316
|
|
|
} |
1317
|
|
|
} |
1318
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.