##// END OF EJS Templates
Require net/ldap....
Jean-Philippe Lang -
r8787:97075c85e851
parent child
Show More
@@ -1,130 +1,131
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 'iconv'
18 require 'iconv'
19 require 'net/ldap'
19
20
20 class AuthSourceLdap < AuthSource
21 class AuthSourceLdap < AuthSource
21 validates_presence_of :host, :port, :attr_login
22 validates_presence_of :host, :port, :attr_login
22 validates_length_of :name, :host, :maximum => 60, :allow_nil => true
23 validates_length_of :name, :host, :maximum => 60, :allow_nil => true
23 validates_length_of :account, :account_password, :base_dn, :maximum => 255, :allow_nil => true
24 validates_length_of :account, :account_password, :base_dn, :maximum => 255, :allow_nil => true
24 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
25 validates_numericality_of :port, :only_integer => true
26 validates_numericality_of :port, :only_integer => true
26
27
27 before_validation :strip_ldap_attributes
28 before_validation :strip_ldap_attributes
28
29
29 def initialize(attributes=nil, *args)
30 def initialize(attributes=nil, *args)
30 super
31 super
31 self.port = 389 if self.port == 0
32 self.port = 389 if self.port == 0
32 end
33 end
33
34
34 def authenticate(login, password)
35 def authenticate(login, password)
35 return nil if login.blank? || password.blank?
36 return nil if login.blank? || password.blank?
36 attrs = get_user_dn(login)
37 attrs = get_user_dn(login)
37
38
38 if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password)
39 if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password)
39 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
40 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
40 return attrs.except(:dn)
41 return attrs.except(:dn)
41 end
42 end
42 rescue Net::LDAP::LdapError => text
43 rescue Net::LDAP::LdapError => text
43 raise "LdapError: " + text
44 raise "LdapError: " + text
44 end
45 end
45
46
46 # test the connection to the LDAP
47 # test the connection to the LDAP
47 def test_connection
48 def test_connection
48 ldap_con = initialize_ldap_con(self.account, self.account_password)
49 ldap_con = initialize_ldap_con(self.account, self.account_password)
49 ldap_con.open { }
50 ldap_con.open { }
50 rescue Net::LDAP::LdapError => text
51 rescue Net::LDAP::LdapError => text
51 raise "LdapError: " + text
52 raise "LdapError: " + text
52 end
53 end
53
54
54 def auth_method_name
55 def auth_method_name
55 "LDAP"
56 "LDAP"
56 end
57 end
57
58
58 private
59 private
59
60
60 def strip_ldap_attributes
61 def strip_ldap_attributes
61 [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
62 [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
62 write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
63 write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
63 end
64 end
64 end
65 end
65
66
66 def initialize_ldap_con(ldap_user, ldap_password)
67 def initialize_ldap_con(ldap_user, ldap_password)
67 options = { :host => self.host,
68 options = { :host => self.host,
68 :port => self.port,
69 :port => self.port,
69 :encryption => (self.tls ? :simple_tls : nil)
70 :encryption => (self.tls ? :simple_tls : nil)
70 }
71 }
71 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
72 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
72 Net::LDAP.new options
73 Net::LDAP.new options
73 end
74 end
74
75
75 def get_user_attributes_from_ldap_entry(entry)
76 def get_user_attributes_from_ldap_entry(entry)
76 {
77 {
77 :dn => entry.dn,
78 :dn => entry.dn,
78 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
79 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
79 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
80 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
80 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
81 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
81 :auth_source_id => self.id
82 :auth_source_id => self.id
82 }
83 }
83 end
84 end
84
85
85 # Return the attributes needed for the LDAP search. It will only
86 # Return the attributes needed for the LDAP search. It will only
86 # include the user attributes if on-the-fly registration is enabled
87 # include the user attributes if on-the-fly registration is enabled
87 def search_attributes
88 def search_attributes
88 if onthefly_register?
89 if onthefly_register?
89 ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]
90 ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]
90 else
91 else
91 ['dn']
92 ['dn']
92 end
93 end
93 end
94 end
94
95
95 # Check if a DN (user record) authenticates with the password
96 # Check if a DN (user record) authenticates with the password
96 def authenticate_dn(dn, password)
97 def authenticate_dn(dn, password)
97 if dn.present? && password.present?
98 if dn.present? && password.present?
98 initialize_ldap_con(dn, password).bind
99 initialize_ldap_con(dn, password).bind
99 end
100 end
100 end
101 end
101
102
102 # Get the user's dn and any attributes for them, given their login
103 # Get the user's dn and any attributes for them, given their login
103 def get_user_dn(login)
104 def get_user_dn(login)
104 ldap_con = initialize_ldap_con(self.account, self.account_password)
105 ldap_con = initialize_ldap_con(self.account, self.account_password)
105 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
106 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
106 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
107 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
107 attrs = {}
108 attrs = {}
108
109
109 ldap_con.search( :base => self.base_dn,
110 ldap_con.search( :base => self.base_dn,
110 :filter => object_filter & login_filter,
111 :filter => object_filter & login_filter,
111 :attributes=> search_attributes) do |entry|
112 :attributes=> search_attributes) do |entry|
112
113
113 if onthefly_register?
114 if onthefly_register?
114 attrs = get_user_attributes_from_ldap_entry(entry)
115 attrs = get_user_attributes_from_ldap_entry(entry)
115 else
116 else
116 attrs = {:dn => entry.dn}
117 attrs = {:dn => entry.dn}
117 end
118 end
118
119
119 logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug?
120 logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug?
120 end
121 end
121
122
122 attrs
123 attrs
123 end
124 end
124
125
125 def self.get_attr(entry, attr_name)
126 def self.get_attr(entry, attr_name)
126 if !attr_name.blank?
127 if !attr_name.blank?
127 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
128 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
128 end
129 end
129 end
130 end
130 end
131 end
General Comments 0
You need to be logged in to leave comments. Login now