wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
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
const ldap = require('../lib/index')
 
/// --- Shared handlers
 
function authorize (req, res, next) {
  /* Any user may search after bind, only cn=root has full power */
  const isSearch = (req instanceof ldap.SearchRequest)
  if (!req.connection.ldap.bindDN.equals('cn=root') && !isSearch) { return next(new ldap.InsufficientAccessRightsError()) }
 
  return next()
}
 
/// --- Globals
 
const SUFFIX = 'o=smartdc'
const db = {}
const server = ldap.createServer()
 
server.bind('cn=root', function (req, res, next) {
  if (req.dn.toString() !== 'cn=root' || req.credentials !== 'secret') { return next(new ldap.InvalidCredentialsError()) }
 
  res.end()
  return next()
})
 
server.add(SUFFIX, authorize, function (req, res, next) {
  const dn = req.dn.toString()
 
  if (db[dn]) { return next(new ldap.EntryAlreadyExistsError(dn)) }
 
  db[dn] = req.toObject().attributes
  res.end()
  return next()
})
 
server.bind(SUFFIX, function (req, res, next) {
  const dn = req.dn.toString()
  if (!db[dn]) { return next(new ldap.NoSuchObjectError(dn)) }
 
  if (!db[dn].userpassword) { return next(new ldap.NoSuchAttributeError('userPassword')) }
 
  if (db[dn].userpassword.indexOf(req.credentials) === -1) { return next(new ldap.InvalidCredentialsError()) }
 
  res.end()
  return next()
})
 
server.compare(SUFFIX, authorize, function (req, res, next) {
  const dn = req.dn.toString()
  if (!db[dn]) { return next(new ldap.NoSuchObjectError(dn)) }
 
  if (!db[dn][req.attribute]) { return next(new ldap.NoSuchAttributeError(req.attribute)) }
 
  let matches = false
  const vals = db[dn][req.attribute]
  for (let i = 0; i < vals.length; i++) {
    if (vals[i] === req.value) {
      matches = true
      break
    }
  }
 
  res.end(matches)
  return next()
})
 
server.del(SUFFIX, authorize, function (req, res, next) {
  const dn = req.dn.toString()
  if (!db[dn]) { return next(new ldap.NoSuchObjectError(dn)) }
 
  delete db[dn]
 
  res.end()
  return next()
})
 
server.modify(SUFFIX, authorize, function (req, res, next) {
  const dn = req.dn.toString()
  if (!req.changes.length) { return next(new ldap.ProtocolError('changes required')) }
  if (!db[dn]) { return next(new ldap.NoSuchObjectError(dn)) }
 
  const entry = db[dn]
 
  let mod
  for (let i = 0; i < req.changes.length; i++) {
    mod = req.changes[i].modification
    switch (req.changes[i].operation) {
      case 'replace':
        if (!entry[mod.type]) { return next(new ldap.NoSuchAttributeError(mod.type)) }
 
        if (!mod.vals || !mod.vals.length) {
          delete entry[mod.type]
        } else {
          entry[mod.type] = mod.vals
        }
 
        break
 
      case 'add':
        if (!entry[mod.type]) {
          entry[mod.type] = mod.vals
        } else {
          mod.vals.forEach(function (v) {
            if (entry[mod.type].indexOf(v) === -1) { entry[mod.type].push(v) }
          })
        }
 
        break
 
      case 'delete':
        if (!entry[mod.type]) { return next(new ldap.NoSuchAttributeError(mod.type)) }
 
        delete entry[mod.type]
 
        break
    }
  }
 
  res.end()
  return next()
})
 
server.search(SUFFIX, authorize, function (req, res, next) {
  const dn = req.dn.toString()
  if (!db[dn]) { return next(new ldap.NoSuchObjectError(dn)) }
 
  let scopeCheck
 
  switch (req.scope) {
    case 'base':
      if (req.filter.matches(db[dn])) {
        res.send({
          dn,
          attributes: db[dn]
        })
      }
 
      res.end()
      return next()
 
    case 'one':
      scopeCheck = function (k) {
        if (req.dn.equals(k)) { return true }
 
        const parent = ldap.parseDN(k).parent()
        return (parent ? parent.equals(req.dn) : false)
      }
      break
 
    case 'sub':
      scopeCheck = function (k) {
        return (req.dn.equals(k) || req.dn.parentOf(k))
      }
 
      break
  }
 
  Object.keys(db).forEach(function (key) {
    if (!scopeCheck(key)) { return }
 
    if (req.filter.matches(db[key])) {
      res.send({
        dn: key,
        attributes: db[key]
      })
    }
  })
 
  res.end()
  return next()
})
 
/// --- Fire it up
 
server.listen(1389, function () {
  console.log('LDAP server up at: %s', server.url)
})