1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-30 11:09:23 +00:00
emacs/lisp/net/eudcb-ldap.el

212 lines
6.8 KiB
EmacsLisp
Raw Normal View History

2000-01-12 20:50:20 +00:00
;;; eudcb-ldap.el --- Emacs Unified Directory Client - LDAP Backend
2006-12-09 04:06:06 +00:00
;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
2009-01-05 03:18:22 +00:00
;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
2000-01-12 20:50:20 +00:00
;; Author: Oscar Figueiredo <oscar@cpe.fr>
;; Maintainer: Pavel Jan<61>k <Pavel@Janik.cz>
2002-01-06 16:37:36 +00:00
;; Keywords: comm
2000-01-12 20:50:20 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
2000-01-12 20:50:20 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
2000-01-12 20:50:20 +00:00
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
2000-01-12 20:50:20 +00:00
;;; Commentary:
2002-01-06 15:08:06 +00:00
;; This library provides specific LDAP protocol support for the
2000-01-12 20:50:20 +00:00
;; Emacs Unified Directory Client package
;;; Installation:
;; Install EUDC first. See EUDC documentation.
;;; Code:
(require 'eudc)
(require 'ldap)
;;{{{ Internal cooking
(eval-and-compile
(if (fboundp 'ldap-get-host-parameter)
(fset 'eudc-ldap-get-host-parameter 'ldap-get-host-parameter)
(defun eudc-ldap-get-host-parameter (host parameter)
"Get the value of PARAMETER for HOST in `ldap-host-parameters-alist'."
(plist-get (cdr (assoc host ldap-host-parameters-alist))
parameter))))
(defvar eudc-ldap-attributes-translation-alist
'((name . sn)
(firstname . givenname)
(email . mail)
(phone . telephonenumber))
"Alist mapping EUDC attribute names to LDAP names.")
2002-01-06 15:08:06 +00:00
(eudc-protocol-set 'eudc-query-function 'eudc-ldap-simple-query-internal
2000-01-12 20:50:20 +00:00
'ldap)
(eudc-protocol-set 'eudc-list-attributes-function 'eudc-ldap-get-field-list
'ldap)
2002-01-06 15:08:06 +00:00
(eudc-protocol-set 'eudc-protocol-attributes-translation-alist
2000-01-12 20:50:20 +00:00
'eudc-ldap-attributes-translation-alist 'ldap)
2002-01-06 15:08:06 +00:00
(eudc-protocol-set 'eudc-bbdb-conversion-alist
'eudc-ldap-bbdb-conversion-alist
2000-01-12 20:50:20 +00:00
'ldap)
(eudc-protocol-set 'eudc-protocol-has-default-query-attributes nil 'ldap)
2002-01-06 15:08:06 +00:00
(eudc-protocol-set 'eudc-attribute-display-method-alist
2000-01-12 20:50:20 +00:00
'(("jpegphoto" . eudc-display-jpeg-inline)
("labeledurl" . eudc-display-url)
("audio" . eudc-display-sound)
("labeleduri" . eudc-display-url)
("mail" . eudc-display-mail)
2002-01-06 15:08:06 +00:00
("url" . eudc-display-url))
2000-01-12 20:50:20 +00:00
'ldap)
2002-01-06 15:08:06 +00:00
(eudc-protocol-set 'eudc-switch-to-server-hook
'(eudc-ldap-check-base)
2000-01-12 20:50:20 +00:00
'ldap)
(defun eudc-ldap-cleanup-record-simple (record)
"Do some cleanup in a RECORD to make it suitable for EUDC."
2002-01-06 15:08:06 +00:00
(mapcar
(function
2000-01-12 20:50:20 +00:00
(lambda (field)
(cons (intern (car field))
(if (cdr (cdr field))
(cdr field)
(car (cdr field))))))
record))
(defun eudc-filter-$ (string)
(mapconcat 'identity (split-string string "\\$") "\n"))
;; Cleanup a LDAP record to make it suitable for EUDC:
;; Make the record a cons-cell instead of a list if it is single-valued
2000-01-12 20:50:20 +00:00
;; Filter the $ character in addresses into \n if not done by the LDAP lib
(defun eudc-ldap-cleanup-record-filtering-addresses (record)
2002-01-06 15:08:06 +00:00
(mapcar
(function
2000-01-12 20:50:20 +00:00
(lambda (field)
(let ((name (intern (car field)))
(value (cdr field)))
(if (memq name '(postaladdress registeredaddress))
(setq value (mapcar 'eudc-filter-$ value)))
(cons name
(if (cdr value)
value
(car value))))))
record))
(defun eudc-ldap-simple-query-internal (query &optional return-attrs)
"Query the LDAP server with QUERY.
2002-01-06 15:08:06 +00:00
QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid
LDAP attribute names.
RETURN-ATTRS is a list of attributes to return, defaulting to
2000-01-12 20:50:20 +00:00
`eudc-default-return-attributes'."
(let ((result (ldap-search (eudc-ldap-format-query-as-rfc1558 query)
eudc-server
(if (listp return-attrs)
(mapcar 'symbol-name return-attrs))))
final-result)
(if (or (not (boundp 'ldap-ignore-attribute-codings))
ldap-ignore-attribute-codings)
2002-01-06 15:08:06 +00:00
(setq result
2000-01-12 20:50:20 +00:00
(mapcar 'eudc-ldap-cleanup-record-filtering-addresses result))
(setq result (mapcar 'eudc-ldap-cleanup-record-simple result)))
(if (and eudc-strict-return-matches
return-attrs
(not (eq 'all return-attrs)))
(setq result (eudc-filter-partial-records result return-attrs)))
;; Apply eudc-duplicate-attribute-handling-method
(if (not (eq 'list eudc-duplicate-attribute-handling-method))
(mapc
2000-01-12 20:50:20 +00:00
(function (lambda (record)
2002-01-06 15:08:06 +00:00
(setq final-result
2000-01-12 20:50:20 +00:00
(append (eudc-filter-duplicate-attributes record)
final-result))))
result))
final-result))
(defun eudc-ldap-get-field-list (dummy &optional objectclass)
"Return a list of valid attribute names for the current server.
OBJECTCLASS is the LDAP object class for which the valid
attribute names are returned. Default to `person'"
(interactive)
(or eudc-server
(call-interactively 'eudc-set-server))
2002-01-06 15:08:06 +00:00
(let ((ldap-host-parameters-alist
2000-01-12 20:50:20 +00:00
(list (cons eudc-server
'(scope subtree sizelimit 1)))))
(mapcar 'eudc-ldap-cleanup-record-simple
2002-01-06 15:08:06 +00:00
(ldap-search
(eudc-ldap-format-query-as-rfc1558
2000-01-12 20:50:20 +00:00
(list (cons "objectclass"
(or objectclass
"person"))))
eudc-server nil t))))
(defun eudc-ldap-escape-query-special-chars (string)
"Value is STRING with characters forbidden in LDAP queries escaped."
2002-01-06 15:08:06 +00:00
;; Note that * should also be escaped but in most situations I suppose
2000-01-12 20:50:20 +00:00
;; the user doesn't want this
(eudc-replace-in-string
(eudc-replace-in-string
(eudc-replace-in-string
2002-01-06 15:08:06 +00:00
(eudc-replace-in-string
string
2000-01-12 20:50:20 +00:00
"\\\\" "\\5c")
"(" "\\28")
")" "\\29")
(char-to-string ?\0) "\\00"))
(defun eudc-ldap-format-query-as-rfc1558 (query)
"Format the EUDC QUERY list as a RFC1558 LDAP search filter."
2002-01-06 15:08:06 +00:00
(format "(&%s)"
(apply 'concat
2000-01-12 20:50:20 +00:00
(mapcar '(lambda (item)
2002-01-06 15:08:06 +00:00
(format "(%s=%s)"
(car item)
2000-01-12 20:50:20 +00:00
(eudc-ldap-escape-query-special-chars (cdr item))))
query))))
2002-01-06 15:08:06 +00:00
;;}}}
2000-01-12 20:50:20 +00:00
;;{{{ High-level interfaces (interactive functions)
(defun eudc-ldap-customize ()
"Customize the EUDC LDAP support."
(interactive)
(customize-group 'eudc-ldap))
(defun eudc-ldap-check-base ()
"Check if the current LDAP server has a configured search base."
(unless (or (eudc-ldap-get-host-parameter eudc-server 'base)
ldap-default-base
(null (y-or-n-p "No search base defined. Configure it now? ")))
2000-01-12 20:50:20 +00:00
;; If the server is not in ldap-host-parameters-alist we add it for the
;; user
(if (null (assoc eudc-server ldap-host-parameters-alist))
2002-01-06 15:08:06 +00:00
(setq ldap-host-parameters-alist
2000-01-12 20:50:20 +00:00
(cons (list eudc-server) ldap-host-parameters-alist)))
(customize-variable 'ldap-host-parameters-alist)))
2002-01-06 15:08:06 +00:00
;;}}}
2000-01-12 20:50:20 +00:00
(eudc-register-protocol 'ldap)
(provide 'eudcb-ldap)
;; arch-tag: 0f254dc0-7378-4fd4-ae26-18666184e96b
2000-01-12 20:50:20 +00:00
;;; eudcb-ldap.el ends here