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 47-56 my $anonymous_bind = 1; Link Here
47
my $desired_authentication_result = 'success';
47
my $desired_authentication_result = 'success';
48
my $desired_connection_result     = 'error';
48
my $desired_connection_result     = 'error';
49
my $desired_admin_bind_result     = 'error';
49
my $desired_admin_bind_result     = 'error';
50
my $desired_compare_result        = 'error';
51
my $desired_search_result         = 'error';
50
my $desired_search_result         = 'error';
52
my $desired_count_result          = 1;
51
my $desired_count_result          = 1;
53
my $desired_bind_result           = 'error';
52
my $desired_bind_result           = 'error';
53
my $remaining_entry = 1;
54
my $ret;
54
my $ret;
55
55
56
# Mock the context module
56
# Mock the context module
Lines 267-279 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: er Link Here
267
        $anonymous_bind            = 1;
267
        $anonymous_bind            = 1;
268
        $desired_admin_bind_result = 'success';
268
        $desired_admin_bind_result = 'success';
269
        $desired_bind_result = 'error';
269
        $desired_bind_result = 'error';
270
        $desired_search_result = 'success';
271
        $desired_count_result = 1;
270
        reload_ldap_module();
272
        reload_ldap_module();
271
273
272
        warning_like {
274
        warning_like {
273
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
275
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
274
                password => 'hey' );
276
                password => 'hey' );
275
        }
277
        }
276
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
278
qr/LDAP Auth rejected : invalid password for user 'hola'./,
277
          'checkpw_ldap prints correct warning if LDAP bind fails';
279
          'checkpw_ldap prints correct warning if LDAP bind fails';
278
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
280
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
279
281
Lines 300-306 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: er Link Here
300
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
302
            $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
301
                password => 'hey' );
303
                password => 'hey' );
302
        }
304
        }
303
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
305
qr/LDAP Auth rejected : invalid password for user 'hola'./,
304
          'checkpw_ldap prints correct warning if LDAP bind fails';
306
          'checkpw_ldap prints correct warning if LDAP bind fails';
305
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
307
        is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
306
308
Lines 309-315 qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_n Link Here
309
311
310
subtest 'search_method tests' => sub {
312
subtest 'search_method tests' => sub {
311
313
312
    plan tests => 5;
314
    plan tests => 3;
313
315
314
    my $ldap = mock_net_ldap();
316
    my $ldap = mock_net_ldap();
315
317
Lines 329-343 subtest 'search_method tests' => sub { Link Here
329
        qr/LDAP search failed to return object : 1/,
331
        qr/LDAP search failed to return object : 1/,
330
'search_method prints correct warning when db->search returns error code'
332
'search_method prints correct warning when db->search returns error code'
331
    );
333
    );
332
333
    $desired_search_result = 'success';
334
    $desired_count_result  = 2;
335
    reload_ldap_module();
336
    warning_like { $ret = C4::Auth_with_ldap::search_method( $ldap, '123' ) }
337
    qr/^LDAP Auth rejected \: \(uid\=123\) gets 2 hits/,
338
      'search_method prints correct warning when hits count is not 1';
339
    is( $ret, 0, 'search_method returns 0 when hits count is not 1' );
340
341
};
334
};
342
335
343
# Function that mocks the call to C4::Context->config(param)
336
# Function that mocks the call to C4::Context->config(param)
Lines 412-417 sub mock_net_ldap { Link Here
412
        'search',
405
        'search',
413
        sub {
406
        sub {
414
407
408
            $remaining_entry = 1
409
415
            return mock_net_ldap_search(
410
            return mock_net_ldap_search(
416
                {
411
                {
417
                    count => ($desired_count_result)
412
                    count => ($desired_count_result)
Lines 454-460 sub mock_net_ldap_search { Link Here
454
    $mocked_search->mock( 'error',       sub { return $error; } );
449
    $mocked_search->mock( 'error',       sub { return $error; } );
455
    $mocked_search->mock( 'error_name',  sub { return $error_name; } );
450
    $mocked_search->mock( 'error_name',  sub { return $error_name; } );
456
    $mocked_search->mock( 'error_text',  sub { return $error_text; } );
451
    $mocked_search->mock( 'error_text',  sub { return $error_text; } );
457
    $mocked_search->mock( 'shift_entry', sub { return $shift_entry; } );
452
    $mocked_search->mock( 'shift_entry', sub {
453
        if ($remaining_entry) {
454
            $remaining_entry--;
455
            return $shift_entry;
456
        }
457
        return '';
458
    });
458
459
459
    return $mocked_search;
460
    return $mocked_search;
460
}
461
}
461
- 

Return to bug 6979