Chris@909: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@1115: require 'iconv' Chris@0: require 'net/ldap' Chris@1115: require 'net/ldap/dn' Chris@1115: require 'timeout' Chris@0: Chris@909: class AuthSourceLdap < AuthSource Chris@0: validates_presence_of :host, :port, :attr_login Chris@245: validates_length_of :name, :host, :maximum => 60, :allow_nil => true Chris@1115: validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true Chris@0: validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true Chris@0: validates_numericality_of :port, :only_integer => true Chris@1115: validates_numericality_of :timeout, :only_integer => true, :allow_blank => true Chris@1115: validate :validate_filter Chris@909: Chris@0: before_validation :strip_ldap_attributes Chris@909: Chris@1115: def initialize(attributes=nil, *args) Chris@1115: super Chris@0: self.port = 389 if self.port == 0 Chris@0: end Chris@909: Chris@0: def authenticate(login, password) Chris@0: return nil if login.blank? || password.blank? Chris@909: Chris@1115: with_timeout do Chris@1115: attrs = get_user_dn(login, password) Chris@1115: if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password) Chris@1115: logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? Chris@1115: return attrs.except(:dn) Chris@1115: end Chris@0: end Chris@1115: rescue Net::LDAP::LdapError => e Chris@1115: raise AuthSourceException.new(e.message) Chris@0: end Chris@0: Chris@0: # test the connection to the LDAP Chris@0: def test_connection Chris@1115: with_timeout do Chris@1115: ldap_con = initialize_ldap_con(self.account, self.account_password) Chris@1115: ldap_con.open { } Chris@1115: end Chris@1115: rescue Net::LDAP::LdapError => e Chris@1115: raise AuthSourceException.new(e.message) Chris@0: end Chris@909: Chris@0: def auth_method_name Chris@0: "LDAP" Chris@0: end Chris@909: Chris@0: private Chris@909: Chris@1115: def with_timeout(&block) Chris@1115: timeout = self.timeout Chris@1115: timeout = 20 unless timeout && timeout > 0 Chris@1115: Timeout.timeout(timeout) do Chris@1115: return yield Chris@1115: end Chris@1115: rescue Timeout::Error => e Chris@1115: raise AuthSourceTimeoutException.new(e.message) Chris@1115: end Chris@1115: Chris@1115: def ldap_filter Chris@1115: if filter.present? Chris@1115: Net::LDAP::Filter.construct(filter) Chris@1115: end Chris@1115: rescue Net::LDAP::LdapError Chris@1115: nil Chris@1115: end Chris@1115: Chris@1115: def validate_filter Chris@1115: if filter.present? && ldap_filter.nil? Chris@1115: errors.add(:filter, :invalid) Chris@1115: end Chris@1115: end Chris@1115: Chris@0: def strip_ldap_attributes Chris@0: [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| Chris@0: write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? Chris@0: end Chris@0: end Chris@909: Chris@0: def initialize_ldap_con(ldap_user, ldap_password) Chris@0: options = { :host => self.host, Chris@0: :port => self.port, Chris@0: :encryption => (self.tls ? :simple_tls : nil) Chris@0: } Chris@0: options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? Chris@0: Net::LDAP.new options Chris@0: end Chris@0: Chris@0: def get_user_attributes_from_ldap_entry(entry) Chris@0: { Chris@0: :dn => entry.dn, Chris@0: :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), Chris@0: :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), Chris@0: :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), Chris@0: :auth_source_id => self.id Chris@0: } Chris@0: end Chris@0: Chris@0: # Return the attributes needed for the LDAP search. It will only Chris@0: # include the user attributes if on-the-fly registration is enabled Chris@0: def search_attributes Chris@0: if onthefly_register? Chris@0: ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] Chris@0: else Chris@0: ['dn'] Chris@0: end Chris@0: end Chris@0: Chris@0: # Check if a DN (user record) authenticates with the password Chris@0: def authenticate_dn(dn, password) Chris@0: if dn.present? && password.present? Chris@0: initialize_ldap_con(dn, password).bind Chris@0: end Chris@0: end Chris@0: Chris@0: # Get the user's dn and any attributes for them, given their login Chris@1115: def get_user_dn(login, password) Chris@1115: ldap_con = nil Chris@1115: if self.account && self.account.include?("$login") Chris@1115: ldap_con = initialize_ldap_con(self.account.sub("$login", Net::LDAP::DN.escape(login)), password) Chris@1115: else Chris@1115: ldap_con = initialize_ldap_con(self.account, self.account_password) Chris@1115: end Chris@909: login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) Chris@909: object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) Chris@0: attrs = {} Chris@909: Chris@1115: search_filter = object_filter & login_filter Chris@1115: if f = ldap_filter Chris@1115: search_filter = search_filter & f Chris@1115: end Chris@1115: Chris@909: ldap_con.search( :base => self.base_dn, Chris@1115: :filter => search_filter, Chris@0: :attributes=> search_attributes) do |entry| Chris@0: Chris@0: if onthefly_register? Chris@0: attrs = get_user_attributes_from_ldap_entry(entry) Chris@0: else Chris@0: attrs = {:dn => entry.dn} Chris@0: end Chris@0: Chris@0: logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug? Chris@0: end Chris@0: Chris@0: attrs Chris@0: end Chris@909: Chris@0: def self.get_attr(entry, attr_name) Chris@0: if !attr_name.blank? Chris@0: entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] Chris@0: end Chris@0: end Chris@0: end