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