This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpDirAuth.php
2435 lines (2138 loc) · 105 KB
/
wpDirAuth.php
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* wpDirAuth: WordPress Directory Authentication (LDAP/LDAPS).
*
* Works with most LDAP enabled directory services, such as OpenLDAP,
* Apache Directory, Microsoft Active Directory, Novell eDirectory,
* Sun Java System Directory Server, etc.
*
* Please note that wpDirAuth will start in safe mode if it detects that
* another plugin is in conflict, by detecting if the wp_authenticate and
* wp_setcookie functions have already been overwritten. It cannot,
* on the other hand, detect plugins that might want to overwrite these
* functions after wpDirAuth has been loaded.
*
* Originally forked from a patched version of wpLDAP.
*
* @package wpDirAuth
* @version 1.9.4
* @see http://wpdirauth.gilzow.com/
* @license GPLv2 or later <https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html>
*
* Copyrights are listed in chronological order, by contributions.
*
* wpDirAuth: WordPress Directory Authentication, original author
* Copyright (c) 2007 Stephane Daury - http://stephane.daury.org/
*
* wpDirAuth and wpLDAP Patch Contributions
* Copyright (c) 2007 PKR Internet, LLC - http://www.pkrinternet.com/
*
* wpDirAuth Patch Contributions
* Copyright (c) 2007 Todd Beverly
*
* wpLDAP: WordPress LDAP Authentication
* Copyright (c) 2007 Ashay Suresh Manjure - http://ashay.org/
*
* wpDirAuth Patch Contribution and current maintainer
* Copyright (c) 2010, 2011, 2012 Paul Gilzow - http://gilzow.com/
*
* wpDirAuth is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation.
*
* wpDirAuth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* @todo Always stay on top of security and user input validation while
* staying backwards compatible enough until PHP4 support is dropped in
* WP (serious patches welcomed, please see code). Note that we do
* heavily rely on WP's admin ACL scheme, by necessity.
*/
/*
PLUGIN META INFO FOR WORDPRESS LISTINGS
Plugin Name: wpDirAuth-PosixGroup
Plugin URI: http://wpdirauth.gilzow.com/
Description: WordPress Directory Authentication (LDAP/LDAPS).
Works with most LDAP enabled directory services, such as OpenLDAP,
Apache Directory, Microsoft Active Directory, Novell eDirectory,
Sun Java System Directory Server, etc.
Originally revived and upgraded from a patched version of wpLDAP.
Version: 1.9.4
Author: Paul Gilzow
Author URI: http://www.gilzow.com/
*/
/**
* Prevent direct access. Technically, they'd get a white screen anyway since the bulk of this plugin is function
* definitions, but let's make sure. In addition, we'll give them a 404
*/
if(!defined('ABSPATH')){
http_response_code(404);
exit;
}
/**
* wpDirAuth version.
*/
define('WPDIRAUTH_VERSION', '1.9.4');
/**
* wpDirAuth signature.
*/
define('WPDIRAUTH_SIGNATURE', '<a href="https://wordpress.org/plugins/wpdirauth/">wpDirAuth</a> '.WPDIRAUTH_VERSION);
/**
* Default LDAP field to search against when locating the user's profile.
*/
define('WPDIRAUTH_DEFAULT_FILTER', 'samAccountName');
/**
* Default login screen message.
*/
define('WPDIRAUTH_DEFAULT_LOGINSCREENMSG', '%s members can login directly using their institutional password.');
/**
* Default password change message.
*/
define('WPDIRAUTH_DEFAULT_CHANGEPASSMSG', 'To change a %s password, please refer to the official institutional password policy.');
/**
* Allowed HTML (messages)
*/
define('WPDIRAUTH_ALLOWED_TAGS', '<a><strong><em><p><ul><ol><li>');
define('WPDIRAUTH_ERROR_TITLE',__('<strong>wpDirAuth Directory Authentication Error</strong>: '));
define('WPDIRAUTH_LDAP_RETURN_KEYS',serialize(array('sn', 'givenname', 'mail')));
//
define('WPDIRAUTH_EMAIL_NEWUSER_NOTIFY','You have been added to the site %s as %s %s. You may login to the site using your institution\'s %s (%s) and password at the following address: %s');
//meta key used for determing if we have checked a directory-auth'ed users password
define('WPDIRAUTH_UPASSWORD_CHECKED','wpdirauth_prnd');
//default length of time, IN HOURS, the cookie should be set for when a directory-authed user logs in
define('WPDIRAUTH_COOKIE_EXPIRE_TIME_DEFAULT', 1);
/**
*My fail-safe method for determing if we are running in multisite mode
*/
define('WPDIRAUTH_MULTISITE',(defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE && function_exists('switch_to_blog')) ? TRUE : FALSE);
/**
*List of option keys we store in wp_sitemeta/wp_options
*/
define('WPDIRAUTH_OPTIONS', serialize(array(
'dirAuthEnable',
'dirAuthRequiresSsl',
'dirAuthTOS',
'dirAuthUseGroups',
'dirAuthEnableSsl',
'dirAuthControllers',
'dirAuthBaseDn',
'dirAuthPreBindUser',
'dirAuthAccountSuffix',
'dirAuthFilter',
'dirAuthInstitution',
'dirAuthGroups',
'dirAuthMarketingSSOID',
'dirAuthLoginScreenMsg',
'dirAuthChangePassMsg',
'dirAuthCookieExpire',
)));
if(function_exists('add_filter')){
add_filter('authenticate','wpDirAuth_authenticate',10,3);
if(function_exists('wp_authenticate')){
//we need to at least warn them that the plugin might not work correctly. total hack until we rewrite
define('WPDIRAUTH_PLUGGABLE_WARN',true);
}
} elseif(!function_exists('wp_authenticate')) {
function wp_authenticate($strUserName,$strPassword){
return wpDirAuth_authenticate(null,$strUserName,$strPassword);
}
} else {
//no hooks, and something has overridden the wp_authenticate menu
$boolAuthOverridden = true;
}
if (!function_exists('ldap_connect') || (isset($boolAuthOverridden) && $boolAuthOverridden) ) {
/**
* SAFE MODE
*/
/**
* SAFE MODE: wpDirAuth plugin configuration panel.
* Processes and outputs the wpDirAuth configuration form, with a conflict message.
*
* @return void
*/
function wpDirAuth_safeConflictMessage()
{
$wpDARef = WPDIRAUTH_SIGNATURE;
if (!function_exists('ldap_connect')) {
$message = <<<________EOS
<h3>Sorry, but your PHP install does not seem to have access to the LDAP features.</h3>
<p>
wpDirAuth is now running in safe mode.'
</p>
<p>
Quote from the <a href="http://php.net/ldap#ldap.installation">PHP manual LDAP section</a>:
<blockquote>
LDAP support in PHP is not enabled by default. You will need to use the
--with-ldap[=DIR] configuration option when compiling PHP to enable LDAP
support. DIR is the LDAP base install directory. To enable SASL support,
be sure --with-ldap-sasl[=DIR] is used, and that sasl.h exists on the system.
</blockquote>
</p>
________EOS;
} else {
$message = <<<________EOS
<h3>Sorry, but another plugin seems to be conflicting with wpDirAuth.</h3>
<p>
wpDirAuth is now running in safe mode as to not impair the other plugin's operations.'
</p>
<p>
You are running an older version of WordPress that does not support <a href="https://codex.wordpress.org/Plugin_API/Hooks">Hooks</a>
and another plugin has overridden the wp_authenticate <a href="http://codex.wordpress.org/Pluggable_Functions">pluggable function</a>.
wpDirAuth cannot provide directory authentication without having access to this function.
</p>
<p>
Please disable any WP plugins that deal with authentication in order to use wpDirAuth, or upgrade your instance
of WordPress to one that supports Hooks. Unfortunately, we cannot provide you with more info as to which plugin is in conflict.
</p>
________EOS;
}
echo <<<________EOS
<div class="wrap">
<h2>wpDirAuth Directory Authentication Options: Plugin Conflict</h2>
$message
<p>$wpDARef</p>
</div>
________EOS;
}
/**
* SAFE MODE: Adds the `wpDirAuth` menu entry in the Wordpress Admin section.
* Also activates the wpDirAuth config panel, with a conflict message, as a callback function.
*
* @uses wpDirAuth_safeConflictMessage
*/
function wpDirAuth_safeAddMenu()
{
if (function_exists('add_options_page')) {
add_options_page(
'wpDirAuth Directory Authentication Options: Plugin Conflict',
'!! wpDirAuth !!',
'manage_options',
basename(__FILE__),
'wpDirAuth_safeConflictMessage'
);
}
}
/**
* SAFE MODE: Add custom WordPress actions.
*
* @uses wpDirAuth_safeAddMenu
*/
if (function_exists('add_action')) {
add_action('admin_menu', 'wpDirAuth_safeAddMenu');
}
}
else {
/**
* STANDARD MODE
*/
/**
* @param $objUser WP_User
* @param $strPassword string
* @return void
*/
function wpDirAuth_check_oldpassword($objUser,$strPassword)
{
if(!wpdirauth_already_changed_password($objUser)){
//does their current directory password match the one in wordpress?
if(wp_check_password($strPassword,$objUser->data->user_pass,$objUser->ID)){
//it does, so let's give them a new random one
wp_set_password(wp_generate_password(24));
}
wpDirAuth_mark_password_as_checked($objUser->ID);
}
}
/**
* Have we already checked a user's password at some point?
*
* @param $objUser WP_User
* @return bool
*/
function wpDirAuth_already_changed_password($objUser)
{
return (1 == get_user_meta($objUser->ID,WPDIRAUTH_UPASSWORD_CHECKED,true) ? true : false);
}
/**
* @param $objUser
* @return void
*/
function wpDirAuth_mark_password_as_checked($intUserID)
{
add_user_meta($intUserID,WPDIRAUTH_UPASSWORD_CHECKED,1,true);
}
/**
* Cookie marker.
* Generates a random string to be used as salt for the password
* hash cookie checks in wp_setcookie and wp_authenticate
*
* @return string 55 chars-long salty goodness (md5 + uniqid)
*/
function wpDirAuth_makeCookieMarker()
{
$cookieMarker = md5(
$_SERVER['SERVER_SIGNATURE']
.$_SERVER['HTTP_USER_AGENT']
.$_SERVER['REMOTE_ADDR']
).uniqid(microtime(),true);
update_site_option("dirAuthCookieMarker",$cookieMarker);
return $cookieMarker;
}
/**
* LDAP bind test
* Tries two different documented method of php-based ldap binding.
* Note: passing params by reference, no need for copies (unlike in
* wpDirAuth_auth where it is desirable).
*
* @param resource &$connection LDAP connection
* @param string &$username LDAP username
* @param string &$password LDAP password
* @param string $baseDn
* @return boolean Binding status
* */
function wpDirAuth_bindTest(&$connection, &$username, &$password,$baseDn)
{
//$password = strtr($password, array("\'"=>"'"));
/**
* Why stripslashes on the password? Because wordpress.
* @see: https://codex.wordpress.org/Function_Reference/stripslashes_deep#Good_Coding_Practice
*/
$password = stripslashes_deep($password);
if ( ($isBound = @ldap_bind($connection, $username, $password)) === false ) {
// @see http://weblogs.valsania.it/andreav/2008/07/24/wpdirauth-14-patch/
$isBound = @ldap_bind($connection,"uid=$username,$baseDn", $password);
}
return $isBound;
}
/**
* put your comment there...
*
* @param string $dc name of domain controller to connect to
* @param integer $enableSsl ssl config option
* @return resource|WP_Error
*/
function wpDirAuth_establishConnection($dc,$enableSsl){
/**
* Only setup protocol value if ldaps is required to help with older AD
* @see http://groups.google.com/group/wpdirauth-support/browse_thread/thread/7b744c7ad66a4829
*/
$protocol = ($enableSsl) ? 'ldaps://' : '';
/**
* Scan for and use alternate server port, but only if ssl is disabled.
* @see Parameters constraint at http://ca.php.net/ldap_connect
*/
if (strstr($dc, ':')) list($dc, $port) = explode(':', $dc);
switch($enableSsl){
case 1:
$connection = ldap_connect($protocol.$dc);
break;
case 2:
case 0:
default:
if(isset($port)){
$connection = ldap_connect($dc,$port);
} else {
$connection = ldap_connect($dc);
}
break;
}
/**
* Copes with W2K3/AD issue.
* @see http://bugs.php.net/bug.php?id=30670
*/
if (@ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3)) {
@ldap_set_option($connection, LDAP_OPT_REFERRALS, 0);
}
//they want to start TLS
if($enableSsl == 2){
if(!ldap_start_tls($connection)){
return new WP_Error('tls_failed_to_start',__('wpDirAuth error: tls failed to start'));
}
}
return $connection;
}
/**
* put your comment there...
*
* @param array $controllers list of domain controllers to connect to
* @return mixed array of shuffled controllers or WP_Error
*/
function wpDirAuth_shuffleControllers($controllers){
if (count($controllers) > 1) {
// shuffle the domain controllers for pseudo load balancing and fault tolerance.
shuffle($controllers);
} elseif (count($controllers) == 0) {
return new WP_Error('no_controllers',__(' wpDirAuth config error: no domain controllers specified.'));
}
return $controllers;
}
/**
* Custom LDAP authentication module.
* The returned keys are in the same format used by WP for
* the wp_insert_user and wp_update_user functions.
*
* @param string $username LDAP username
* @param string $password LDAP password
* @return WP_Error|array WP_Error object or array of Directory email, last_name and first_name
*
* @uses WPDIRAUTH_DEFAULT_FILTER
* @uses WPDIRAUTH_ERROR_TITLE
* @uses wpDirAuth_bindTest
* @uses wpDirAuth_retrieveUserDetails
* @uses wpDirAuth_shuffleControllers
* @uses wpDirAuth_establishConnection
*/
function wpDirAuth_auth($username, $password)
{
global $error, $pwd;
$errorTitle = WPDIRAUTH_ERROR_TITLE;
$isInDirectory = false;
$results = false;
$controllers = explode(',', get_site_option('dirAuthControllers'));
$baseDn = get_site_option('dirAuthBaseDn');
$preBindUser = get_site_option('dirAuthPreBindUser');
$preBindPassword = get_site_option('dirAuthPreBindPassword');
$accountSuffix = get_site_option('dirAuthAccountSuffix');
$filter = get_site_option('dirAuthFilter');
$enableSsl = get_site_option('dirAuthEnableSsl');
$boolUseGroups = get_site_option('dirAuthUseGroups');
if($boolUseGroups == 1){
$strAuthGroups = get_site_option('dirAuthGroups');
}
$returnKeys = wpDirAuth_retrieveReturnFilterKeys();
$isBound = $isPreBound = $isLoggedIn = false;
if ($accountSuffix) $username .= $accountSuffix;
if (!$filter) $filter = WPDIRAUTH_DEFAULT_FILTER;
$filterQuery = "($filter=$username)";
$filterQuery = apply_filters('wpdirauth_filterquery',$filterQuery,$filter,$username);
$controllers = wpDirAuth_shuffleControllers($controllers);
if(is_wp_error($controllers)){
return $controllers;
}
// Connection pool loop - Haha, PooL LooP
foreach ($controllers as $dc) {
$connection = wpDirAuth_establishConnection($dc,$enableSsl);
if(is_wp_error($connection)){
return $connection;
}
if ($preBindUser && $preBindPassword) {
/**
* Use case 1: Servers requiring pre-binding with admin defined
* credentials to search for the user's full DN before attempting
* to login.
* @see http://dev.wp-plugins.org/ticket/681
*/
if ( $isPreBound = wpDirAuth_bindTest($connection, $preBindUser, $preBindPassword,$baseDn) === true ) {
if ( ($results = @ldap_search($connection, $baseDn, $filterQuery, $returnKeys)) !== false ) {
if ( ($userDn = @ldap_get_dn($connection, ldap_first_entry($connection, $results))) !== false ) {
if ( ($isBound = wpDirAuth_bindTest($connection, $userDn, $password,$baseDn)) === true ) {
$isLoggedIn = true; // valid server, valid login, move on
break; // valid server, valid login, move on
}
}
}
}
}
elseif ( ($isBound = wpDirAuth_bindTest($connection, $username, $password,$baseDn)) === true ) {
/**
* Use case 2: Servers that will not let you bind anonymously
* but will let the end user bind directly.
* @see http://groups.google.com/group/wpdirauth-support/browse_thread/thread/8fd16c05266fc832
*/
$isLoggedIn = true;
break; // valid server, valid login, move on
}
elseif ( ($isBound = @ldap_bind($connection)) === true ) {
/**
* Use case 3: Servers that might require a full user DN to
* actually login and therefore let you bind anonymously first .
* Try ldap_search + ldap_get_dn before attempting a login.
* @see http://wordpress.org/support/topic/129814?replies=34#post-603644
*/
if ( ($results = @ldap_search($connection, $baseDn, $filterQuery, $returnKeys)) !== false ) {
if ( ($userDn = @ldap_get_dn($connection, ldap_first_entry($connection, $results))) !== false ) {
$isInDirectory = true; // account exists in directory
if ( ($isBound = wpDirAuth_bindTest($connection, $userDn, $password,$baseDn)) === true ) {
$isLoggedIn = true; // valid server, valid login, move on
break; // valid server, valid login, move on
}
}
}
}
}
if ( ($preBindUser && $preBindPassword) && ( ! $isPreBound ) ) {
return new WP_Error ('no_directory_or_prebinding', $errorTitle
. __(' wpDirAuth config error: No directory server available for authentication, OR pre-binding credentials denied.'));
}
elseif ( ( $isInDirectory ) && ( ! $isBound ) ) {
return new WP_Error ('could_not_bind_as_user', $errorTitle
. __(' Incorrect password.'));
}
elseif ( ! $isBound && ! $isPreBound ) {
return new WP_Error ('no_directory_available', $errorTitle
. __(' wpDirAuth config error: No directory server available for authentication.'));
}
elseif ( ! $isLoggedIn) {
/**
* @desc wp-hack was echo'ing out $username verbatim which allowed a XSS vulnerability. Encoded $username before echoing'
*/
return new WP_Error ('could_not_authenticate', $errorTitle
. __(' Could not authenticate user. Please check your credentials.')
. " [" . htmlentities($username,ENT_QUOTES,'UTF-8') . "]");
}
else {
if($boolUseGroups == 1){
//the user is authenticated, but we want to make sure they are a member of the groups given
/**
* We need to get the DN's for each Authentication Group CN that was given to us.
*/
$aryAuthGroupsDN = array();
$aryAuthGroups = explode(',',$strAuthGroups);
$aryAttribs = array('distinguishedname');
foreach($aryAuthGroups as $strAuthGroup){
$strAuthGroup = 'cn='.$strAuthGroup;
$rscLDAPSearch = ldap_search($connection,$baseDn,$strAuthGroup,$aryAttribs);
$arySearchResults = ldap_get_entries($connection,$rscLDAPSearch);
if(isset($arySearchResults[0]['dn'])){
$aryAuthGroupsDN[] = $strAuthGroup;
}
}
if(count($aryAuthGroupsDN) == 0){
return new WP_Error('no_auth_groups_found',$errorTitle.__('No Authentication Groups found based on given group CN'));
}
$memberFilterQuery = "(memberUid=".$username.")";
$strFilterQuery = '(&'.$memberFilterQuery.'(|';
foreach($aryAuthGroupsDN as $strAuthGroupDN){
$strFilterQuery .= "(".$strAuthGroupDN.")";
}
$strFilterQuery .= '))';
if(($rscLDAPSearchGroupMember = ldap_search($connection,$baseDn,$strFilterQuery)) !== false){
$arySearchResultsMember = ldap_get_entries($connection,$rscLDAPSearchGroupMember);
if($arySearchResultsMember['count'] !== 1){
return new WP_Error('not_member_of_auth_group',$errorTitle
. __('User authenticated but is not a member of an Authentication Group(s)'));
}
}
}
/**
* Search for profile, if still needed.
* $result is set to false by default. reset to a resource or false again in lines 403-437
* wpDirAuth_retrieveUserDetails() now checks the value of $results before continuing
*/
return wpDirAuth_retrieveUserDetails($connection,$baseDn,$filterQuery,$results);
}
}
/**
* Runs stripslashes, html_entity_decode, then strip_tags with
* allowed html if requested.
*
* No input sashimi for us (hopefully).
*
* @param string $value Value to `sanitize`
* @param boolean $allowed Set to true for WPDIRAUTH_ALLOWED_TAGS
* @return string Cleaner value.
*
* @uses WPDIRAUTH_ALLOWED_TAGS
*/
function wpDirAuth_sanitize($value, $allowed = false)
{
$allowed = ($allowed) ? WPDIRAUTH_ALLOWED_TAGS : '';
return strip_tags(html_entity_decode(stripslashes($value)), $allowed);
}
/**
* wpDirAuth plugin configuration panel.
* Processes and outputs the wpDirAuth configuration form.
*
* @return void
*
* @uses WPDIRAUTH_DEFAULT_FILTER
* @uses WPDIRAUTH_DEFAULT_LOGINSCREENMSG
* @uses WPDIRAUTH_DEFAULT_CHANGEPASSMSG
* @uses WPDIRAUTH_ALLOWED_TAGS
* @uses wpDirAuth_makeCookieMarker
* @uses wpDirAuth_sanitize
*/
function wpDirAuth_optionsPanel()
{
global $userdata;
$wpDARef = WPDIRAUTH_SIGNATURE;
$allowedHTML = htmlentities(WPDIRAUTH_ALLOWED_TAGS);
$curUserIsDirUser = get_user_meta($userdata->ID, 'wpDirAuthFlag',true);
if ($curUserIsDirUser) {
echo <<<____________EOS
<div class="wrap">
<h2>wpDirAuth Directory Authentication Options</h2>
<p>
Because any changes made to directory authentication
options can adversly affect your session when logged in
as a directory user, you must be logged in as a
WordPress-only administrator user to update these settings.
</p>
<p>
If such a user no longer exists in the database, please
<a href="./users.php#add-new-user">create a new one</a>
using the appropriate WordPress admin tool.
</p>
<p>$wpDARef</p>
</div>
____________EOS;
return;
}
if ($_POST) {
$enableSsl = 0; //default
$boolUseGroups = 0; //default
// Booleans
$enable = intval($_POST['dirAuthEnable']) == 1 ? 1 : 0;
$requireSsl = intval($_POST['dirAuthRequireSsl']) == 1 ? 1 : 0;
$TOS = intval($_POST['dirAuthTOS']) == 1 ? 1 : 0;
$boolAutoRegister = intval($_POST['dirAuthAutoRegistration'])== 1 ? 1 : 0;
//integers
if(intval($_POST['dirAuthEnableSsl']) == 1 || intval($_POST['dirAuthEnableSsl']) == 2){
$enableSsl = intval($_POST['dirAuthEnableSsl']);
}
// Strings, no HTML
$controllers = wpDirAuth_sanitize($_POST['dirAuthControllers']);
$baseDn = wpDirAuth_sanitize($_POST['dirAuthBaseDn']);
$preBindUser = wpDirAuth_sanitize($_POST['dirAuthPreBindUser']);
$preBindPassword = wpDirAuth_sanitize($_POST['dirAuthPreBindPassword']);
$preBindPassCheck = wpDirAuth_sanitize($_POST['dirAuthPreBindPassCheck']);
$accountSuffix = wpDirAuth_sanitize($_POST['dirAuthAccountSuffix']);
$filter = wpDirAuth_sanitize($_POST['dirAuthFilter']);
$institution = wpDirAuth_sanitize($_POST['dirAuthInstitution']);
$strAuthGroups = wpDirAuth_sanitize($_POST['dirAuthGroups']);
$strMarketingSSOID= wpDirAuth_sanitize(($_POST['dirAuthMarketingSSOID']));
if($strAuthGroups != ''){
$boolUseGroups = 1;
if(1 == $boolAutoRegister){
//they are using authgroups, but they also want us to autoregister. sorry, cant do that.
$boolAutoRegister = 0;
}
}
// Have to be allowed to contain some HTML
$loginScreenMsg = wpDirAuth_sanitize($_POST['dirAuthLoginScreenMsg'], true);
$changePassMsg = wpDirAuth_sanitize($_POST['dirAuthChangePassMsg'], true);
$fltCookieExpire = (is_numeric($_POST['dirAuthCookieExpire'])) ? floatval($_POST['dirAuthCookieExpire']) : WPDIRAUTH_COOKIE_EXPIRE_TIME_DEFAULT;
update_site_option('dirAuthEnable', $enable);
update_site_option('dirAuthEnableSsl', $enableSsl);
update_site_option('dirAuthRequireSsl', $requireSsl);
update_site_option('dirAuthAutoRegister', $boolAutoRegister);
update_site_option('dirAuthControllers', $controllers);
update_site_option('dirAuthBaseDn', $baseDn);
update_site_option('dirAuthPreBindUser', $preBindUser);
update_site_option('dirAuthAccountSuffix', $accountSuffix);
update_site_option('dirAuthFilter', $filter);
update_site_option('dirAuthInstitution', $institution);
update_site_option('dirAuthLoginScreenMsg', $loginScreenMsg);
update_site_option('dirAuthChangePassMsg', $changePassMsg);
update_site_option('dirAuthTOS', $TOS);
update_site_option('dirAuthUseGroups', $boolUseGroups);
update_site_option('dirAuthGroups', $strAuthGroups);
update_site_option('dirAuthMarketingSSOID', $strMarketingSSOID);
update_site_option('dirAuthCookieExpire', $fltCookieExpire);
// Only store/override the value if a new one is being sent a bind user is set.
if ( $preBindUser && $preBindPassword && ($preBindPassCheck == $preBindPassword) )
update_site_option('dirAuthPreBindPassword', $preBindPassword);
// Clear the stored password if the Bind DN is null
elseif ( ! $preBindUser)
update_site_option('dirAuthPreBindPassword', '');
if (get_site_option('dirAuthEnable') && !get_site_option('dirAuthCookieMarker'))
wpDirAuth_makeCookieMarker();
echo '<div id="message" class="updated fade"><p>Your new settings were saved successfully.</p></div>';
// Be sure to clear $preBindPassword, not to be displayed onscreen or in source
unset($preBindPassword);
} else {
// Booleans
$enable = intval(get_site_option('dirAuthEnable')) == 1 ? 1 : 0;
$requireSsl = intval(get_site_option('dirAuthRequireSsl')) == 1 ? 1 : 0;
$TOS = intval(get_site_option('dirAuthTOS')) == 1 ? 1 : 0;
$boolUseGroups = intval(get_site_option('dirAuthUseGroups')) == 1 ? 1 : 0;
$boolAutoRegister= intval(get_site_option('dirAuthAutoRegister'))== 1 ? 1 : 0;
//integers
$enableSsl = intval(get_site_option('dirAuthEnableSsl',0));
// Strings, no HTML
$controllers = wpDirAuth_sanitize(get_site_option('dirAuthControllers'));
$baseDn = wpDirAuth_sanitize(get_site_option('dirAuthBaseDn'));
$preBindUser = wpDirAuth_sanitize(get_site_option('dirAuthPreBindUser'));
$accountSuffix = wpDirAuth_sanitize(get_site_option('dirAuthAccountSuffix'));
$filter = wpDirAuth_sanitize(get_site_option('dirAuthFilter'));
$institution = wpDirAuth_sanitize(get_site_option('dirAuthInstitution'));
$strAuthGroups = wpDirAuth_sanitize((get_site_option('dirAuthGroups')));
$strMarketingSSOID = wpDirAuth_sanitize((get_site_option('dirAuthMarketingSSOID')));
//Floats
$fltCookieExpire = floatval(get_site_option('dirAuthCookieExpire'));
// Have to be allowed to contain some HTML
$loginScreenMsg = wpDirAuth_sanitize(get_site_option('dirAuthLoginScreenMsg'), true);
$changePassMsg = wpDirAuth_sanitize(get_site_option('dirAuthChangePassMsg'), true);
}
$controllers = htmlspecialchars($controllers);
$baseDn = htmlspecialchars($baseDn);
$preBindUser = htmlspecialchars($preBindUser);
$accountSuffix = htmlspecialchars($accountSuffix);
$filter = htmlspecialchars($filter);
$institution = htmlspecialchars($institution);
$loginScreenMsg = htmlspecialchars($loginScreenMsg);
$changePassMsg = htmlspecialchars($changePassMsg);
$strAuthGroups = htmlspecialchars($strAuthGroups);
$tEnable = $fEnable = $tWpSsl = $fWpSsl = $tTOS = $fTOS = '';
if ($enable) {
$tEnable = "checked";
}
else {
$fEnable = "checked";
}
$defaultFilter = WPDIRAUTH_DEFAULT_FILTER;
if (!$filter) {
$filter = $defaultFilter;
}
if (!$institution) {
$institution = '[YOUR INSTITUTION]';
}
if (!$loginScreenMsg) {
$loginScreenMsg = sprintf(WPDIRAUTH_DEFAULT_LOGINSCREENMSG, $institution);
}
if (!$changePassMsg) {
$changePassMsg = sprintf(WPDIRAUTH_DEFAULT_CHANGEPASSMSG, $institution);
}
/**
* If they are using authentication groups we will not automaticaly register authed users that dont already have an account
*/
if('' != $strAuthGroups && $boolAutoRegister == 1){
$boolAutoRegister = 0;
}
/*
if ($enableSsl) {
$tSsl = "checked";
}
else {
$fSsl = "checked";
}
*/
$strNoSSL ='';
$strSSL = '';
$strTLS = '';
$strOptionSelected = 'selected="selected"';
switch($enableSsl){
case 1:
$strSSL = $strOptionSelected;
break;
case 2:
$strTLS = $strOptionSelected;
break;
case 0:
default:
$strNoSSL = $strOptionSelected;
break;
}
if ($requireSsl) {
$tWpSsl = "checked";
}
else {
$fWpSsl = "checked";
}
if ($TOS) {
$tTOS = "checked";
}
else {
$fTOS = "checked";
}
$strYesAutoRegister = '';
$strNoAutoRegister = '';
if($boolAutoRegister){
$strYesAutoRegister = 'checked';
} else {
$strNoAutoRegister = 'checked';
}
/**
* you have to cast the 0 to a float because the zero you get back from above has been cast to a float
*/
if(floatval(0) === $fltCookieExpire){
$fltCookieExpire = WPDIRAUTH_COOKIE_EXPIRE_TIME_DEFAULT;
}
//we are running in filtered mode, but some other plugin has defined wp_authenticate
if(defined('WPDIRAUTH_PLUGGABLE_WARN') && WPDIRAUTH_PLUGGABLE_WARN){
$strPluggableWarn = '<div class="error">'.PHP_EOL;
$strPluggableWarn .= '<p>Just FYI: another plugin has overridden the wp_authenticate <a href="http://codex.wordpress.org/Pluggable_Functions">pluggable function</a>.';
$strPluggableWarn .= ' If that plugin doesn\'t apply the \'authenticate\' filter, then wpDirAuth will be unable to function.</p>'.PHP_EOL;
$strPluggableWarn .= '</div>'.PHP_EOL;
} else {
$strPluggableWarn = '';
}
$strPHPWarn = '';
if(version_compare( PHP_VERSION, '5.3', '<' )){
$strPHPWarn = <<<____PHPWARN
<div class="error">
<h3>PHP 5.2.X WARNING</h3>
<p>Unfortunately, trying to support newer versions of PHP (the upcoming release of 7.2) as well as older version of PHP has become unsustainable. As of wpDirAuth version 2.0
I will no longer be able to support versions of PHP less than 5.3.X. I feel an obligation to strongly suggest you upgrade your PHP install considering 5.2 was released
eleven years ago and has been without support for over six years.
</div>
____PHPWARN;
}
/**
* @todo seems like we should loop through these yes/nos are something...
*/
$wpDAV = WPDIRAUTH_VERSION;
echo <<<________EOS
<style>
#wpdirauth fieldset + fieldset { margin-top:40px; }
#wpdirauth legend { font-weight:600; font-size:1.8em;}
#wpdirauth label { font-weight:bold; }
</style>
<div id="wpdirauth" class="wrap">
<h2>wpDirAuth Directory Authentication Options</h2>
$strPluggableWarn
$strPHPWarn
<form method="post" id="dir_auth_options">
<p class="submit"><input type="submit" name="dirAuthOptionsSave" value="Update Options »" /></p>
<fieldset class="options">
<legend>WordPress Settings</legend>
<ul>
<li>
<label for="dirAuthEnable">Enable Directory Authentication?</label>
<br />
<input type="radio" name="dirAuthEnable" value="1" $tEnable /> Yes
<input type="radio" name="dirAuthEnable" value="0" $fEnable /> No
<br />
<strong>Note 1</strong>: Users created in WordPress are not affected by your directory authentication settings.
<br />
<strong>Note 2</strong>: You will still be able to login with standard WP users if the LDAP server(s) go offline.
</li>
<li>
<label for="dirAuthRequireSsl">Require SSL Login?</label>
<br />
<input type="radio" name="dirAuthRequireSsl" value="1" $tWpSsl/> Yes
<input type="radio" name="dirAuthRequireSsl" value="0" $fWpSsl/> No
<br />
<em>Force the WordPress login screen to require encryption (SSL, https:// URL)?</em>
</li>
<li>
<label for="dirAuthAutoRegistration">Automatically Register Authenticated Users?</label>
<p style="max-width:800px; font-style: italic; margin: 0 0 5px 0;">If a user authenticates successfully, but does not already have an account for the site, should wpDirAuth automatically create a new user
account for the authenticated user, and assign them to the lowest possible role? Note that this setting has no affect if you are using
<a href="#dirAuthGroups">Authentication Groups</a>.</p>
<input type="radio" name="dirAuthAutoRegistration" value="1" $strYesAutoRegister /> Yes
<input type="radio" name="dirAuthAutoRegistration" value="0" $strNoAutoRegister /> No
</li>
</ul>
</fieldset>
<fieldset class="options">
<legend>Directory Settings</legend>
<ul>
<li>
<label for="dirAuthEnableSsl">Enable SSL Connectivity?</label>
<br />
<select id="dirAuthEnableSsl" name="dirAuthEnableSsl">
<option value="0" $strNoSSL>No SSL Connectivity</option>
<option value="1" $strSSL>Use SSL (ldaps)</option>
<option value="2" $strTLS>Use TLS</option>
</select>
<br />
<em>Use encryption (TLS, SSL, ldaps:// URL) when WordPress connects to the directory server(s)?</em>
</li>
<li>
<label for="dirAuthControllers">Directory Servers (Domain Controllers)</label>
<br />
<input type="text" name="dirAuthControllers" value="$controllers" size="40"/><br />
<em>The DNS name or IP address of the directory server(s).</em><br />
<strong>NOTE:</strong> Separate multiple entries by a comma and/or alternate ports with a colon (eg: my.server1.org, my.server2.edu:387).
Unfortunately, alternate ports will be ignored when using LDAP/SSL, because of <a href="http://ca3.php.net/ldap_connect">the way</a> PHP handles the protocol.
</li>
<li>
<label for="dirAuthFilter">Account Filter</label>
<br />
<input type="text" name="dirAuthFilter" value="$filter" size="40"/>
(Defaults to <em>$defaultFilter</em>)
<br />
<em>What LDAP field should we search the username against to locate the user's profile after successful login?</em>
</li>
<li>
<label for="dirAuthAccountSuffix">Account Suffix</label>
<br />
<input type="text" name="dirAuthAccountSuffix" value="$accountSuffix" size="40" /><br />
<em>Suffix to be automatically appended to the username if desired. e.g. @domain.com</em><br />
<strong>NOTE:</strong> Changing this value will cause your existing directory users to have new accounts created the next time they login.
</li>
<li>
<label for="dirAuthBaseDn">Base DN</label>
<br />
<input type="text" name="dirAuthBaseDn" value="$baseDn" size="40"/><br />
<em>The base DN for carrying out LDAP searches.</em>
</li>
<li>
<label for="dirAuthPreBindUser">Bind DN</label>
<br />
<input type="text" name="dirAuthPreBindUser" value="$preBindUser" size="40"/><br />
<em>Enter a valid user account/DN to pre-bind with if your LDAP server does not allow anonymous profile searches, or requires a user with specific privileges to search.</em>
</li>
<li>
<label for="dirAuthPreBindPassword">Bind Password</label>
<br />
<input type="password" name="dirAuthPreBindPassword" value="" size="40"/><br />
<em>Enter a password for the above Bind DN if a value is needed.</em><br />
<strong>Note 1</strong>: this value will be stored in clear text in your WordPress database.<br />
<strong>Note 2</strong>: Simply clear the Bind DN value if you wish to delete the stored password altogether.
</li>
<li>
<label for="dirAuthPreBindPassCheck">Confirm Password</label>
<br />
<input type="password" name="dirAuthPreBindPassCheck" value="" size="40"/><br />
<em>Confirm the above Bind Password if you are setting a new value.</em>
</li>
<li>
<label for="dirAuthGroups">Authentication Groups</label><br />