Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 130 lines (105 sloc) 4.369 kB
c900e15 start off with what was in eighthbit-website
James S authored
1 (defpackage #:nisp.ldap
ab17ba9 move 8b stuff to its own package
James S authored
2 (:use :cl)
695e8d7 @nixeagle shadow delete and search and export my generic methods that might not be
authored
3 (:shadow :delete :search)
1a07d66 add one-line-ldif
James S authored
4 (:export :*connections* #:with-ldap #:get-single-entry
695e8d7 @nixeagle shadow delete and search and export my generic methods that might not be
authored
5 #:one-line-ldif #:make-ldap #:describing-ldif-search
6
7 ;; Generics to add your own "hooks" onto. These are there
8 ;; for convience only, and likely will not exist in the same
9 ;; form 3 months from now as the way these are implemented is
10 ;; far from ideal. (These are done as around methods
11 ;; specializing on (T T), which means nobody else can write
12 ;; an around method to specialize on (T T). I'd vastly prefer
13 ;; adding another method selection :hook or similar.
14 #:compute-ldap #:compute-filter
15 ))
c900e15 start off with what was in eighthbit-website
James S authored
16
17 (in-package :nisp.ldap)
18
4410cb0 move while out of my way
James S authored
19 ;;; Needs to be moved to a seperate utility file or something.
20 (defmacro while (test &body body)
21 "While, like in most other languages."
22 `(do ()
23 ((not ,test))
24 ,@body))
25
c900e15 start off with what was in eighthbit-website
James S authored
26
b72cbcf defparameter instead of defvar
James S authored
27 (defvar *connections* '()
f27e4e7 add *connections* to hold a list of connections for 8b
James S authored
28 "Property list of connections to LDAP.")
29
c900e15 start off with what was in eighthbit-website
James S authored
30 ;;; Load an optional config file, the lack of this should not cause this
31 ;;; program to become unusable.
7b8e767 define 8b related ldap objects in their own file as well as get the
James S authored
32
176f1f9 @nixeagle add abstract-filter
authored
33 (defclass abstract-filter ()
34 ()
35 ;; This feels right, but not positive how subclassing is going to work
36 ;; yet.
37 (:documentation "Base of all ldap filters."))
a99a82d add make-ldap, given a keyword or an ldap:ldap object, return an
James S authored
38
39 (defun make-ldap (ldap-or-keyword
40 &optional (connections *connections*))
41 "Return an LDAP object, so long as input is an ldap object or a
42 keyword referincing an ldap object."
43 (if (typep ldap-or-keyword 'ldap:ldap)
44 ldap-or-keyword
45 (getf connections ldap-or-keyword)))
46
ab17ba9 move 8b stuff to its own package
James S authored
47
c900e15 start off with what was in eighthbit-website
James S authored
48
abb5d46 fix up with-ldap to make use of make-ldap
James S authored
49 (defmacro with-ldap (ldap-or-keyword &body body)
c900e15 start off with what was in eighthbit-website
James S authored
50 "Execute BODY in the context of LDAP bound to the ldap server."
abb5d46 fix up with-ldap to make use of make-ldap
James S authored
51 ;; I think this is fine to do instead of using gensyms. I'll consult
52 ;; on lisp on this later.
53 (let ((ldap (gensym)))
54 `(let ((,ldap (make-ldap ,ldap-or-keyword)))
55 (prog2
56 (ldap:bind ,ldap)
57 (progn ,@body)
58 (ldap:unbind ,ldap)))))
c900e15 start off with what was in eighthbit-website
James S authored
59
0a1202a add describing-ldif-search
James S authored
60 (defun describing-ldif-search (search-string &optional (ldap :anon))
61 "Development helper that prints a description of all search matches to
62 standard output in ldif form."
6797e72 @nixeagle fix search to use my list results rather then dosearch macro.
authored
63 (mapc #'(lambda (x)
64 (princ (ldap:ldif x)))
65 (list-search-results search-string (make-ldap ldap))))
0a1202a add describing-ldif-search
James S authored
66
67
c900e15 start off with what was in eighthbit-website
James S authored
68 (defun strip-newlines (string &optional (replace-char nil))
69 "Given a string, remove all newlines.
70
71 This is very irc specific where lines need to be all on one line.
72
73 Note that the newline is not replaced by a space!"
74 (coerce
75 (loop for char in (coerce string 'list)
76 when (and replace-char (eq char #\Newline)) collect replace-char
77 unless (eq char #\Newline) collect char)
78 'string))
79
775ace2 split print-single-entry up so we have get-single-entry
James S authored
80 (defun get-single-entry (search-string &key (ldap :anon)
81 attrs)
82 "Get a single trivial-ldap:entry object by binding and searching."
83 (with-ldap ldap
84 (ldap:search (make-ldap ldap) search-string
85 :attributes attrs)
86 (ldap:next-search-result (make-ldap ldap))))
87
1a07d66 add one-line-ldif
James S authored
88 (defgeneric one-line-ldif (entry)
89 (:documentation "ldif on one line.")
90 (:method ((entry ldap:entry))
91 (strip-newlines (ldap:ldif entry) #\ )))
92
02adada we should also default to the keyword :anon
James S authored
93 (defun print-single-entry (search-string &key (ldap :anon)
c900e15 start off with what was in eighthbit-website
James S authored
94 attrs)
95 (strip-newlines
96 (ldap:ldif
959fa93 lets use make-ldap to ensure we pass an ldap object.
James S authored
97 (get-single-entry search-string :ldap (make-ldap ldap) :attrs attrs))
c900e15 start off with what was in eighthbit-website
James S authored
98 #\ ))
99
ab17ba9 move 8b stuff to its own package
James S authored
100 (defun list-search-results (search-string &optional (ldap :anon))
c900e15 start off with what was in eighthbit-website
James S authored
101 "List of entries from a search."
2b80476 @nixeagle make sure we have an ldap object
authored
102 (let ((ldap (make-ldap ldap)))
103 (with-ldap ldap
104 (ldap:search ldap search-string)
105 (let (result)
106 (while (ldap:results-pending-p ldap)
107 (push (ldap:next-search-result ldap) result))
52cc8c0 @nixeagle (compute-filter): Calculate the string to search by given arguments
authored
108 (nreverse (cdr result))))))
109
110 (defgeneric compute-filter (type &rest args)
111 (:documentation "Compute an LDAP filter to do searches with.")
112 (:method (type &key)
113 "Return TYPE unchanged ignoring ARGS."
114 type))
115
116 (defmethod compute-filter ((filter (eql :all)) &key)
117 "Matches all objects."
255f095 @nixeagle (compute-ldap): Create or select an LDAP object.
authored
118 "(objectClass=*)")
119
120 (defgeneric compute-ldap (ldap &rest args)
121 (:documentation "Make or select LDAP.")
122 (:method (ldap &rest args)
123 (declare (ignore args))
124 "Return LDAP ignoring ARGS."
95350db @nixeagle (trivial-ldap:search): Add around method to check arguments for sensi…
authored
125 ldap))
126
127 (defmethod trivial-ldap:search :around ((ldap t) (filter t) &key)
128 "Search LDAP for all records under specified base."
129 (call-next-method (compute-ldap ldap) (compute-filter filter)))
Something went wrong with that request. Please try again.