@@ -1,113 +1,110 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require 'net/ldap' |
|
18 | require 'net/ldap' | |
19 | require 'iconv' |
|
19 | require 'iconv' | |
20 |
|
20 | |||
21 | class AuthSourceLdap < AuthSource |
|
21 | class AuthSourceLdap < AuthSource | |
22 | validates_presence_of :host, :port, :attr_login |
|
22 | validates_presence_of :host, :port, :attr_login | |
23 | validates_length_of :name, :host, :account_password, :maximum => 60, :allow_nil => true |
|
23 | validates_length_of :name, :host, :account_password, :maximum => 60, :allow_nil => true | |
24 | validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true |
|
24 | validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true | |
25 | validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true |
|
25 | validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true | |
26 | validates_numericality_of :port, :only_integer => true |
|
26 | validates_numericality_of :port, :only_integer => true | |
27 |
|
27 | |||
28 | before_validation :strip_ldap_attributes |
|
28 | before_validation :strip_ldap_attributes | |
29 |
|
29 | |||
30 | def after_initialize |
|
30 | def after_initialize | |
31 | self.port = 389 if self.port == 0 |
|
31 | self.port = 389 if self.port == 0 | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | def authenticate(login, password) |
|
34 | def authenticate(login, password) | |
35 | return nil if login.blank? || password.blank? |
|
35 | return nil if login.blank? || password.blank? | |
36 | attrs = [] |
|
36 | attrs = [] | |
37 | # get user's DN |
|
37 | # get user's DN | |
38 | ldap_con = initialize_ldap_con(self.account, self.account_password) |
|
38 | ldap_con = initialize_ldap_con(self.account, self.account_password) | |
39 | login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) |
|
39 | login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) | |
40 | object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) |
|
40 | object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) | |
41 | dn = String.new |
|
41 | dn = String.new | |
42 | ldap_con.search( :base => self.base_dn, |
|
42 | ldap_con.search( :base => self.base_dn, | |
43 | :filter => object_filter & login_filter, |
|
43 | :filter => object_filter & login_filter, | |
44 | # only ask for the DN if on-the-fly registration is disabled |
|
44 | # only ask for the DN if on-the-fly registration is disabled | |
45 | :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry| |
|
45 | :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry| | |
46 | dn = entry.dn |
|
46 | dn = entry.dn | |
47 | attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register? |
|
47 | attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register? | |
48 | logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug? |
|
48 | logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug? | |
49 |
|
49 | |||
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | if authenticate_dn(dn, password) |
|
52 | if authenticate_dn(dn, password) | |
53 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? |
|
53 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? | |
54 | return attrs |
|
54 | return attrs | |
55 | else |
|
|||
56 | return nil |
|
|||
57 | end |
|
55 | end | |
58 | rescue Net::LDAP::LdapError => text |
|
56 | rescue Net::LDAP::LdapError => text | |
59 | raise "LdapError: " + text |
|
57 | raise "LdapError: " + text | |
60 | end |
|
58 | end | |
61 |
|
59 | |||
62 | # test the connection to the LDAP |
|
60 | # test the connection to the LDAP | |
63 | def test_connection |
|
61 | def test_connection | |
64 | ldap_con = initialize_ldap_con(self.account, self.account_password) |
|
62 | ldap_con = initialize_ldap_con(self.account, self.account_password) | |
65 | ldap_con.open { } |
|
63 | ldap_con.open { } | |
66 | rescue Net::LDAP::LdapError => text |
|
64 | rescue Net::LDAP::LdapError => text | |
67 | raise "LdapError: " + text |
|
65 | raise "LdapError: " + text | |
68 | end |
|
66 | end | |
69 |
|
67 | |||
70 | def auth_method_name |
|
68 | def auth_method_name | |
71 | "LDAP" |
|
69 | "LDAP" | |
72 | end |
|
70 | end | |
73 |
|
71 | |||
74 | private |
|
72 | private | |
75 |
|
73 | |||
76 | def strip_ldap_attributes |
|
74 | def strip_ldap_attributes | |
77 | [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| |
|
75 | [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| | |
78 | write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? |
|
76 | write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? | |
79 | end |
|
77 | end | |
80 | end |
|
78 | end | |
81 |
|
79 | |||
82 | def initialize_ldap_con(ldap_user, ldap_password) |
|
80 | def initialize_ldap_con(ldap_user, ldap_password) | |
83 | options = { :host => self.host, |
|
81 | options = { :host => self.host, | |
84 | :port => self.port, |
|
82 | :port => self.port, | |
85 | :encryption => (self.tls ? :simple_tls : nil) |
|
83 | :encryption => (self.tls ? :simple_tls : nil) | |
86 | } |
|
84 | } | |
87 | options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? |
|
85 | options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? | |
88 | Net::LDAP.new options |
|
86 | Net::LDAP.new options | |
89 | end |
|
87 | end | |
90 |
|
88 | |||
91 | def get_user_attributes_from_ldap_entry(entry) |
|
89 | def get_user_attributes_from_ldap_entry(entry) | |
92 | [ |
|
90 | [ | |
93 | :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), |
|
91 | :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), | |
94 | :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), |
|
92 | :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), | |
95 | :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), |
|
93 | :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), | |
96 | :auth_source_id => self.id |
|
94 | :auth_source_id => self.id | |
97 | ] |
|
95 | ] | |
98 | end |
|
96 | end | |
99 |
|
97 | |||
100 | # Check if a DN (user record) authenticates with the password |
|
98 | # Check if a DN (user record) authenticates with the password | |
101 | def authenticate_dn(dn, password) |
|
99 | def authenticate_dn(dn, password) | |
102 | return nil if dn.empty? |
|
100 | if dn.present? && password.present? | |
103 |
|
101 | initialize_ldap_con(dn, password).bind | ||
104 | ldap_con = initialize_ldap_con(dn, password) |
|
102 | end | |
105 | return ldap_con.bind |
|
|||
106 | end |
|
103 | end | |
107 |
|
104 | |||
108 | def self.get_attr(entry, attr_name) |
|
105 | def self.get_attr(entry, attr_name) | |
109 | if !attr_name.blank? |
|
106 | if !attr_name.blank? | |
110 | entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] |
|
107 | entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] | |
111 | end |
|
108 | end | |
112 | end |
|
109 | end | |
113 | end |
|
110 | end |
General Comments 0
You need to be logged in to leave comments.
Login now