View | Details | Raw Unified | Return to bug 6979
Collapse All | Expand All

(-)a/C4/Auth_with_ldap.pm (-10 / +19 lines)
Lines 106-115 sub search_method { Link Here
106
		warn sprintf("LDAP Auth rejected : %s gets %d hits\n", $filter->as_string, $count) . description($search);
106
		warn sprintf("LDAP Auth rejected : %s gets %d hits\n", $filter->as_string, $count) . description($search);
107
		return 0;
107
		return 0;
108
	}
108
	}
109
	if ($count != 1) {
109
    if ($count == 0) {
110
		warn sprintf("LDAP Auth rejected : %s gets %d hits\n", $filter->as_string, $count);
110
        warn sprintf("LDAP Auth rejected : search with filter '%s' returns no hit\n", $filter->as_string);
111
		return 0;
111
        return 0;
112
	}
112
    }
113
    return $search;
113
    return $search;
114
}
114
}
115
115
Lines 183-197 sub checkpw_ldap { Link Here
183
			return 0;
183
			return 0;
184
		}
184
		}
185
        my $search = search_method($db, $userid) or return 0;   # warnings are in the sub
185
        my $search = search_method($db, $userid) or return 0;   # warnings are in the sub
186
        $userldapentry = $search->shift_entry;
186
        # Handle multiple branches. Same login exists several times in different branches.
187
        my $dn = $userldapentry->dn;
187
        my $bind_ok = 0;
188
        my $user_ldap_bind_ret = $db->bind($dn, password => $password);
188
        while (my $entry = $search->shift_entry) {
189
        if ($user_ldap_bind_ret->code) {
189
            my $user_ldap_bind_ret = $db->bind($entry->dn, password => $password);
190
            warn "LDAP Auth rejected : invalid password for user '$userid'. " . description($user_ldap_bind_ret);
190
            unless ($user_ldap_bind_ret->code) {
191
                $userldapentry = $entry;
192
                $bind_ok = 1;
193
                last;
194
            }
195
        }
196
197
        unless ($bind_ok) {
198
            warn "LDAP Auth rejected : invalid password for user '$userid'.";
191
            return -1;
199
            return -1;
192
        }
200
        }
193
201
194
	}
202
203
    }
195
204
196
    # To get here, LDAP has accepted our user's login attempt.
205
    # To get here, LDAP has accepted our user's login attempt.
197
    # But we still have work to do.  See perldoc below for detailed breakdown.
206
    # But we still have work to do.  See perldoc below for detailed breakdown.
(-)a/t/db_dependent/Auth_with_ldap.t (-15 / +15 lines)
Lines 42-51 my $anonymous_bind = 1; Link Here
42
my $desired_authentication_result = 'success';
42
my $desired_authentication_result = 'success';
43
my $desired_connection_result     = 'error';
43
my $desired_connection_result     = 'error';
44
my $desired_admin_bind_result     = 'error';
44
my $desired_admin_bind_result     = 'error';
45
my $desired_compare_result        = 'error';
46
my $desired_search_result         = 'error';
45
my $desired_search_result         = 'error';
47
my $desired_count_result          = 1;
46
my $desired_count_result          = 1;
48
my $desired_bind_result           = 'error';
47
my $desired_bind_result           = 'error';
48
my $remaining_entry = 1;
49
my $ret;
49
my $ret;
50
50
51
# Mock the context module
51
# Mock the context module
Lines 242-254 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: er Link Here
242
        $anonymous_bind            = 1;
242
        $anonymous_bind            = 1;
243
        $desired_admin_bind_result = 'success';
243
        $desired_admin_bind_result = 'success';
244
        $desired_bind_result = 'error';
244
        $desired_bind_result = 'error';
245
        $desired_search_result = 'success';
246
        $desired_count_result = 1;
245
        reload_ldap_module();
247
        reload_ldap_module();
246
248
247
        warning_like {
249
        warning_like {
248
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
250
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
249
                password => 'hey' );
251
                password => 'hey' );
250
        }
252
        }
251
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
253
qr/LDAP Auth rejected : invalid password for user 'hola'./,
252
          'checkpw_ldap prints correct warning if LDAP bind fails';
254
          'checkpw_ldap prints correct warning if LDAP bind fails';
253
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
255
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
254
256
Lines 275-281 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: er Link Here
275
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
277
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
276
                password => 'hey' );
278
                password => 'hey' );
277
        }
279
        }
278
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
280
qr/LDAP Auth rejected : invalid password for user 'hola'./,
279
          'checkpw_ldap prints correct warning if LDAP bind fails';
281
          'checkpw_ldap prints correct warning if LDAP bind fails';
280
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
282
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
281
283
Lines 284-290 qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_n Link Here
284
286
285
subtest 'search_method tests' => sub {
287
subtest 'search_method tests' => sub {
286
288
287
    plan tests => 5;
289
    plan tests => 3;
288
290
289
    my $ldap = mock_net_ldap();
291
    my $ldap = mock_net_ldap();
290
292
Lines 304-318 subtest 'search_method tests' => sub { Link Here
304
        qr/LDAP search failed to return object : 1/,
306
        qr/LDAP search failed to return object : 1/,
305
'search_method prints correct warning when db->search returns error code'
307
'search_method prints correct warning when db->search returns error code'
306
    );
308
    );
307
308
    $desired_search_result = 'success';
309
    $desired_count_result  = 2;
310
    reload_ldap_module();
311
    warning_like { $ret = C4::Auth_with_ldap::search_method( $ldap, '123' ) }
312
    qr/^LDAP Auth rejected \: \(uid\=123\) gets 2 hits/,
313
      'search_method prints correct warning when hits count is not 1';
314
    is( $ret, 0, 'search_method returns 0 when hits count is not 1' );
315
316
};
309
};
317
310
318
# Function that mocks the call to C4::Context->config(param)
311
# Function that mocks the call to C4::Context->config(param)
Lines 387-392 sub mock_net_ldap { Link Here
387
        'search',
380
        'search',
388
        sub {
381
        sub {
389
382
383
            $remaining_entry = 1
384
390
            return mock_net_ldap_search(
385
            return mock_net_ldap_search(
391
                {
386
                {
392
                    count => ($desired_count_result)
387
                    count => ($desired_count_result)
Lines 429-435 sub mock_net_ldap_search { Link Here
429
    $mocked_search->mock( 'error',       sub { return $error; } );
424
    $mocked_search->mock( 'error',       sub { return $error; } );
430
    $mocked_search->mock( 'error_name',  sub { return $error_name; } );
425
    $mocked_search->mock( 'error_name',  sub { return $error_name; } );
431
    $mocked_search->mock( 'error_text',  sub { return $error_text; } );
426
    $mocked_search->mock( 'error_text',  sub { return $error_text; } );
432
    $mocked_search->mock( 'shift_entry', sub { return $shift_entry; } );
427
    $mocked_search->mock( 'shift_entry', sub {
428
        if ($remaining_entry) {
429
            $remaining_entry--;
430
            return $shift_entry;
431
        }
432
        return '';
433
    });
433
434
434
    return $mocked_search;
435
    return $mocked_search;
435
}
436
}
436
- 

Return to bug 6979