| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | # redMine - project management software | 
					
						
							|  |  |  | # Copyright (C) 2006  Jean-Philippe Lang | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program 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; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							|  |  |  | #  | 
					
						
							|  |  |  | # This program 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, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'net/ldap' | 
					
						
							|  |  |  | require 'iconv' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AuthSourceLdap < AuthSource  | 
					
						
							|  |  |  |   validates_presence_of :host, :port, :attr_login | 
					
						
							| 
									
										
										
										
											2008-07-19 10:47:19 +00:00
										 |  |  |   validates_length_of :name, :host, :account_password, :maximum => 60, :allow_nil => true | 
					
						
							|  |  |  |   validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true | 
					
						
							|  |  |  |   validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true | 
					
						
							|  |  |  |   validates_numericality_of :port, :only_integer => true | 
					
						
							| 
									
										
										
										
											2007-08-14 09:14:33 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2008-09-21 13:28:12 +00:00
										 |  |  |   before_validation :strip_ldap_attributes | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   def after_initialize | 
					
						
							|  |  |  |     self.port = 389 if self.port == 0
 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def authenticate(login, password) | 
					
						
							| 
									
										
										
										
											2008-02-22 17:26:53 +00:00
										 |  |  |     return nil if login.blank? || password.blank? | 
					
						
							| 
									
										
										
										
											2010-02-19 17:00:49 +00:00
										 |  |  |     attrs = get_user_dn(login) | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-02-26 09:13:12 +00:00
										 |  |  |     if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password) | 
					
						
							| 
									
										
										
										
											2010-02-17 16:35:35 +00:00
										 |  |  |       logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? | 
					
						
							| 
									
										
										
										
											2010-02-26 09:13:12 +00:00
										 |  |  |       return attrs.except(:dn) | 
					
						
							| 
									
										
										
										
											2010-02-17 16:35:35 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   rescue  Net::LDAP::LdapError => text | 
					
						
							|  |  |  |     raise "LdapError: " + text | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # test the connection to the LDAP | 
					
						
							|  |  |  |   def test_connection | 
					
						
							|  |  |  |     ldap_con = initialize_ldap_con(self.account, self.account_password) | 
					
						
							|  |  |  |     ldap_con.open { } | 
					
						
							|  |  |  |   rescue  Net::LDAP::LdapError => text | 
					
						
							|  |  |  |     raise "LdapError: " + text | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |   def auth_method_name | 
					
						
							|  |  |  |     "LDAP" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2008-09-21 13:28:12 +00:00
										 |  |  |   private | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def strip_ldap_attributes | 
					
						
							|  |  |  |     [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| | 
					
						
							|  |  |  |       write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   def initialize_ldap_con(ldap_user, ldap_password) | 
					
						
							| 
									
										
										
										
											2008-03-05 12:43:14 +00:00
										 |  |  |     options = { :host => self.host, | 
					
						
							|  |  |  |                 :port => self.port, | 
					
						
							|  |  |  |                 :encryption => (self.tls ? :simple_tls : nil) | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2008-03-06 17:22:21 +00:00
										 |  |  |     options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? | 
					
						
							| 
									
										
										
										
											2008-03-05 12:43:14 +00:00
										 |  |  |     Net::LDAP.new options | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-02-16 17:03:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def get_user_attributes_from_ldap_entry(entry) | 
					
						
							| 
									
										
										
										
											2010-02-26 09:13:12 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-02-19 17:00:49 +00:00
										 |  |  |      :dn => entry.dn, | 
					
						
							| 
									
										
										
										
											2010-02-16 17:03:54 +00:00
										 |  |  |      :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), | 
					
						
							|  |  |  |      :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), | 
					
						
							|  |  |  |      :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), | 
					
						
							|  |  |  |      :auth_source_id => self.id | 
					
						
							| 
									
										
										
										
											2010-02-26 09:13:12 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-02-16 17:03:54 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-02-17 16:35:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 16:33:01 +00:00
										 |  |  |   # Return the attributes needed for the LDAP search.  It will only | 
					
						
							|  |  |  |   # include the user attributes if on-the-fly registration is enabled | 
					
						
							|  |  |  |   def search_attributes | 
					
						
							|  |  |  |     if onthefly_register? | 
					
						
							|  |  |  |       ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       ['dn'] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-17 16:35:35 +00:00
										 |  |  |   # Check if a DN (user record) authenticates with the password | 
					
						
							|  |  |  |   def authenticate_dn(dn, password) | 
					
						
							| 
									
										
										
										
											2010-02-18 16:55:10 +00:00
										 |  |  |     if dn.present? && password.present? | 
					
						
							|  |  |  |       initialize_ldap_con(dn, password).bind | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-02-17 16:35:35 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-02-19 17:00:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Get the user's dn and any attributes for them, given their login | 
					
						
							|  |  |  |   def get_user_dn(login) | 
					
						
							|  |  |  |     ldap_con = initialize_ldap_con(self.account, self.account_password) | 
					
						
							|  |  |  |     login_filter = Net::LDAP::Filter.eq( self.attr_login, login )  | 
					
						
							|  |  |  |     object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )  | 
					
						
							| 
									
										
										
										
											2010-02-26 09:13:12 +00:00
										 |  |  |     attrs = {} | 
					
						
							| 
									
										
										
										
											2010-02-19 17:00:49 +00:00
										 |  |  |      | 
					
						
							|  |  |  |     ldap_con.search( :base => self.base_dn,  | 
					
						
							|  |  |  |                      :filter => object_filter & login_filter,  | 
					
						
							|  |  |  |                      :attributes=> search_attributes) do |entry| | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if onthefly_register? | 
					
						
							|  |  |  |         attrs = get_user_attributes_from_ldap_entry(entry) | 
					
						
							|  |  |  |       else | 
					
						
							| 
									
										
										
										
											2010-02-26 09:13:12 +00:00
										 |  |  |         attrs = {:dn => entry.dn} | 
					
						
							| 
									
										
										
										
											2010-02-19 17:00:49 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 09:13:12 +00:00
										 |  |  |       logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug? | 
					
						
							| 
									
										
										
										
											2010-02-19 17:00:49 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     attrs | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   def self.get_attr(entry, attr_name) | 
					
						
							| 
									
										
										
										
											2008-11-25 19:33:41 +00:00
										 |  |  |     if !attr_name.blank? | 
					
						
							|  |  |  |       entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2006-07-29 09:32:58 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |