This repository was archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauthenticate.scm.in
More file actions
145 lines (122 loc) · 5.57 KB
/
Copy pathauthenticate.scm.in
File metadata and controls
145 lines (122 loc) · 5.57 KB
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
;;; Copyright © 2018 Roel Janssen <roel@gnu.org>
;;;
;;; 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 3 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, see <http://www.gnu.org/licenses/>.
(define-module (ldap authenticate)
#:use-module (ice-9 format)
#:use-module (srfi srfi-1)
#:use-module (system foreign)
#:use-module (rnrs bytevectors)
#:use-module (logger)
#:use-module (www config)
#:re-export (%null-pointer parse-c-struct int size_t * pointer->string)
#:export (may-access?
ldap-is-available?))
;;
;; LIBRARY HANDLE
;; ----------------------------------------------------------------------------
(define %ldap-handle %null-pointer)
(define (ldap-link-library)
(catch 'misc-error
(lambda _
(set! %ldap-handle (dynamic-link (if (string= "@LIBLDAP@" "")
"libldap"
"@LIBLDAP@"))))
(lambda (key . args)
(set! %ldap-handle %null-pointer)
#f))
(dynamic-object? %ldap-handle))
(define (ldap-unlink-library)
(dynamic-unlink %ldap-handle))
(ldap-link-library)
(define (ldap-is-available?)
(dynamic-object? %ldap-handle))
;;
;; CONVENIENCE MACRO
;; ----------------------------------------------------------------------------
(define-syntax-rule
(define-foreign-function symbol handle return-type c-function args)
(define symbol
(if (eq? handle %null-pointer)
#f
(pointer->procedure return-type
(dynamic-func c-function handle) args))))
;;
;; LIBLDAP FUNCTIONS
;; ----------------------------------------------------------------------------
(define-foreign-function ldap-initialize
%ldap-handle int "ldap_initialize" '(* *))
(define-foreign-function ldap-simple-bind-synchronous
%ldap-handle int "ldap_simple_bind_s" '(* * *))
(define-foreign-function ldap-set-option
%ldap-handle int "ldap_set_option" `(* ,int *))
(define-foreign-function ldap-error->string
%ldap-handle '* "ldap_err2string" (list int))
(define-foreign-function ldap-unbind
%ldap-handle int "ldap_unbind" '(*))
;;
;; LIBLDAP CONSTANTS
;; ----------------------------------------------------------------------------
(define LDAP_VERSION3 (make-c-struct (list int) (list 3)))
(define LDAP_OPT_PROTOCOL_VERSION 17)
(define LDAP_OPT_X_TLS_CACERTFILE 24578)
(define LDAP_OPT_X_TLS_CACERTDIR 24579)
;;
;; AUTHENTICATION FUNCTIONS
;; ----------------------------------------------------------------------------
(define (may-access? ldap-server-uri common-name organizational-unit domain
username password)
(catch 'ldap-error
(lambda _
(let* ((connection (bytevector->pointer
(make-bytevector (sizeof size_t))))
(bind-dn (string-append "cn=" username
(if common-name
(format #f "~{,~a~}"
(map (lambda (cn)
(string-append "cn=" cn))
(string-split common-name #\.)))
"")
(if organizational-unit
(format #f ",ou=~a" organizational-unit)
"")
(format #f "~{,dc=~a~}"
(string-split domain #\.)))))
(when (ldap-ssl-certificate-directory)
(unless (zero? (ldap-set-option (dereference-pointer connection)
LDAP_OPT_X_TLS_CACERTDIR
(string->pointer
(ldap-ssl-certificate-directory))))
(throw 'ldap-error "Cannot set SSL certificate directory.")))
(when (ldap-ssl-certificate-file)
(unless (zero? (ldap-set-option (dereference-pointer connection)
LDAP_OPT_X_TLS_CACERTFILE
(string->pointer
(ldap-ssl-certificate-file))))
(throw 'ldap-error "Cannot set SSL certificate file.")))
(if (zero? (ldap-initialize connection (string->pointer ldap-server-uri)))
(begin
(unless (zero? (ldap-set-option (dereference-pointer connection)
LDAP_OPT_PROTOCOL_VERSION
LDAP_VERSION3))
(throw 'ldap-error "Cannot set protocol version."))
(let [(result (ldap-simple-bind-synchronous
(dereference-pointer connection)
(string->pointer bind-dn)
(string->pointer password)))]
(ldap-unbind (dereference-pointer connection))
(zero? result)))
(throw 'ldap-error "Couldn't connect to ~s" ldap-server-uri))))
(lambda (key . args)
(log-error "may-access?" "~s" args)
#f)))