-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap-browser.el
242 lines (214 loc) · 10.2 KB
/
ldap-browser.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
;;; ldap-browser.el --- LDAP utility based on ldapsearch
;;
;; Copyright(c) 2014 Sylvain Chouleur <[email protected]>
;; Authors: Sylvain Chouleur <[email protected]>
;; Keywords: code
;; Licence: GPLv2
;; Version 0.1
;; Requires: ldapsearch
;; Usage:
;; - customize 'ldap-servers variable
;; - Be sure to populate your ~/.authinfo file to provide login and
;; password for each of the servers in 'ldap-servers
;; - load this script using (require 'ldap-browser)
;; - M-x ldap-browser-search-name
;; Provide a name to search, using regexp syntax (e.g: *name*)
;; Results will be displayed in *ldap-browser* buffer
;; - M-x ldap-browser-insert-mail
;; Provide a name to search, the result's mail will be inserted
;; in current buffer if unique. If case of multiple results,
;; *ldap-browser* buffer is opened to let the user select the
;; good one. Hit RET to insert the email of selected contact.
;;
;; *ldap-browser* navigation:
;; - "v" : open contact card and see all LDAP fields available
;; - "g" : refresh buffer
;; - "a" : add contact to purple buddies
;; - "RET" : apply current action on contact. If no action available, do as "v"
;; This file is *NOT* part of GNU Emacs.
;; This file is distributed under the same terms as GNU Emacs.
;;
;; 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., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;
;; http://www.fsf.org/copyleft/gpl.html
(defgroup ldap-browser nil
"LDAP-browser group"
:group 'network)
(defcustom ldap-servers nil
"'((\"ldap1.example.com\" . \"ou=Workers,dc=ldap1,dc=example,dc=com\")
(\"ldap1.example.com\" . \"ou=Workers,dc=ldap2,dc=example,dc=com\"))"
:group 'ldap-browser)
(defvar ldap-search-args "-LLL -o ldif-wrap=no -z none -T /tmp/ldapsearch")
(defvar ldap-result-buffer-format "*ldap-result<%s>*")
(defvar ldap-contact-buffer "*ldap-contact<%s>*")
(defvar ldap-browser-buffer "*ldap-browser*")
(defvar ldap-browser-cols '(("cn" . ("Name" 29 t))
("mail" . ("Email" 40 t))
("uid" . ("Nickname" 10 t))))
(defvar ldap-browser-search-history '())
(defsubst curry (function &rest arguments)
(lexical-let ((function function)
(arguments arguments))
(lambda (&rest more) (apply function (append arguments more)))))
(defsubst icurry (function &rest arguments)
(lexical-let ((function function)
(arguments arguments))
(lambda (&rest more) (interactive) (apply function (append arguments more)))))
(defvar ldap-browser-mode-map
(let ((map (make-sparse-keymap))
(menu-map (make-sparse-keymap)))
(set-keymap-parent map tabulated-list-mode-map)
(define-key map "v" (icurry 'ldap-browser-action 'ldap-browser-view-contact))
(define-key map "g" 'ldap-browser-update)
(define-key map "a" (icurry 'ldap-browser-action 'ldap-browser-add-purple-buddy-callback))
(define-key map (kbd "RET") 'ldap-browser-action)
map))
(define-derived-mode ldap-browser-mode tabulated-list-mode "Ldap Browser"
"Major mode for ldap browser."
(use-local-map ldap-browser-mode-map))
(defun ldap-browser-get-contact (&optional id)
"Return specified contact, or current one if not specified"
(with-current-buffer (get-buffer ldap-browser-buffer)
(let ((id (or id (tabulated-list-get-id)))
(entries ldap-browser-entries))
(find id entries :key (lambda(x)(assoc-default "dn" x)) :test 'equal))))
(defun ascii-clear (string)
"If string terminates by two '=' characters, returns base64 decoded string.
Otherwise returns original string."
(if (string-suffix-p "==" string)
(shell-command-to-string (format "echo %s | base64 -d" string))
string))
(defun ldap-browser-view-contact (contact)
"View contact details in a dedicated buffer"
(pop-to-buffer (get-buffer-create (format ldap-contact-buffer (assoc-default "dn" contact))))
(let ((inhibit-read-only t))
(erase-buffer)
(mapcar (lambda(x)(insert (format "%s = %s\n" (car x) (ascii-clear (cdr x))))) contact)
(align-regexp (point-min) (point-max) "\\(\\s-*\\) = " 1 1)
(goto-char (point-min))
(view-mode)))
(defun ldap-browser-action (&optional action)
"Try to call in order (first existing wins)
- action if non-nil
- `ldap-browser-callback' if a valid function
- `ldap-browser-view'"
(interactive)
(let ((action (or action
(and (functionp ldap-browser-callback) ldap-browser-callback)
'ldap-browser-view-contact)))
(funcall action (ldap-browser-get-contact))))
(defun ldap-browser-clear ()
"Flush ldap-browser entries"
(interactive)
(with-current-buffer (get-create-ldap-browser-buffer)
(setq-local progress (make-progress-reporter "LDAP searching:" 0 (length ldap-servers)))
(setq ldap-browser-entries nil)
(ldap-browser-update)))
(defun ldap-browser-set-callback (callback)
"set variable ldap-browser-callback"
(with-current-buffer (get-create-ldap-browser-buffer)
(setq-local ldap-browser-callback callback)))
(defun ldap-browser-update ()
"Populates browser with found entries"
(interactive)
(with-current-buffer (get-create-ldap-browser-buffer)
(setq tabulated-list-format (vconcat (mapcar 'cdr ldap-browser-cols)))
(funcall tabulated-list-entries)
(tabulated-list-print)
(tabulated-list-init-header)))
(defun ldap-browser-entry-to-tab-entry (entry)
"Output a tabulated-list-entries format of entry"
(list (assoc-default "dn" entry)
(vconcat (mapcar (lambda(col)(or (assoc-default (car col) entry) "-")) ldap-browser-cols))))
(defun ldap-browser-fill-entries ()
"Populate tabulated-list-entries variable"
(mapcar 'ldap-browser-entry-to-tab-entry ldap-browser-entries))
(defun get-create-ldap-browser-buffer ()
(unless (get-buffer ldap-browser-buffer)
(with-current-buffer (get-buffer-create ldap-browser-buffer)
(ldap-browser-mode)
(setq-local tabulated-list-entries 'ldap-browser-fill-entries)
(make-local-variable 'ldap-browser-entries)
(setq tabulated-list-format (vconcat (mapcar 'cdr ldap-browser-cols)))
(tabulated-list-init-header)))
(get-buffer ldap-browser-buffer))
(defun ldap-browser-add-entry (entry)
"Add entry to ldap browser"
(with-current-buffer (get-create-ldap-browser-buffer)
(or (and ldap-browser-entries (nconc ldap-browser-entries (list entry)))
(setq ldap-browser-entries (list entry)))))
(defun ldap-browser-remaining-requests ()
"Return the amount of on-going requests"
(let ((ongoing 0))
(dolist (server ldap-servers ongoing)
(let ((buf (get-buffer (format ldap-result-buffer-format (car server)))))
(and buf (get-buffer-process buf) (setq ongoing (+ ongoing 1)))))))
(defun ldap-browser-may-callback ()
"Call the `ldap-browser-callback' function if not nil and no more ldap requests remaining"
(with-current-buffer (get-buffer ldap-browser-buffer)
(when (functionp ldap-browser-callback)
(if (= 1 (length ldap-browser-entries))
(funcall ldap-browser-callback (ldap-browser-get-contact))
(pop-to-buffer ldap-browser-buffer)))))
(defun ldap-parse-results (add-func)
"Parse ldapsearch output to find entries and call add-func for each found entry"
(goto-char (point-min))
(while (search-forward-regexp "^dn: " nil t)
(let ((entry `(("dn" . ,(buffer-substring-no-properties (point) (point-at-eol)))))
(bound (save-excursion (search-forward-regexp "^$"))))
(while (search-forward-regexp "^\\(\\w+\\):+<? \\(.*\\)" bound t)
(nconc entry (list (cons (match-string 1) (ascii-clear (match-string 2))))))
(funcall add-func entry))))
(defun ldap-search-sentinel (process change)
(with-current-buffer (process-buffer process)
(ldap-parse-results 'ldap-browser-add-entry))
(with-current-buffer (get-buffer ldap-browser-buffer)
(let ((remaining (ldap-browser-remaining-requests)))
(progress-reporter-update progress (- (length ldap-servers) remaining))
(ldap-browser-update)
(when (= 0 remaining)
(ldap-browser-may-callback)
(when (= 0 (length ldap-browser-entries))
(error "ldap-browser: No results")))
(unless ldap-browser-callback
(pop-to-buffer ldap-browser-buffer))
(unless (equal change "finished\n")
(error "ldapsearch: %s\n See buffer %s for details" change (process-buffer process))))))
(defun ldap-browser-search-fields (pattern fields &optional callback)
"Fetch ldap entries filtered by FILTER on displayName.
You can use * as wildcard, but ensure that it's placed at the end of the string.
For obscure reasons, with a star at the beginning of the string the ldap query fails with a timeout..."
(ldap-browser-clear)
(ldap-browser-set-callback callback)
(dolist (server ldap-servers)
(with-current-buffer (get-buffer-create (format ldap-result-buffer-format (car server)))
(erase-buffer)
(let* ((filter (mapconcat (lambda(x)(format "(%s=%s)" x pattern)) fields ""))
(ldap-username (auth-source-user-or-password "login" (car server) "ldap"))
(ldap-password (auth-source-user-or-password "password" (car server) "ldap"))
(cmd (format "%s -h %s -b %s %s %s (|%s)"
ldap-search-args
(car server)
(cdr server)
(if ldap-username (format "-D %s" ldap-username) "")
(if ldap-password (format "-w %s" ldap-password) "-x")
filter)))
(set-process-sentinel (apply 'start-process "ldapsearch" (current-buffer) "ldapsearch" (split-string cmd)) 'ldap-search-sentinel)))))
(defun ldap-browser-search-name (name &optional callback)
"Search pattern in fields \"displayName\" and \"sn\""
(interactive (list (read-string "Name: " nil 'ldap-browser-search-history)))
(ldap-browser-search-fields name '("mail" "sn" "givenName" "uid") callback))
(provide 'ldap-browser)