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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant};
use ldap3_proto::proto::*;
use uuid::Uuid;
use crate::data::*;
use crate::ldap::{LdapClient, LdapSchema};
use crate::profile::DsConfig;
use crate::{TargetServer, TargetServerBuilder};
#[derive(Debug)]
pub struct DirectoryServer {
ldap: LdapClient,
dm_pw: String,
}
impl DirectoryServer {
fn construct(uri: String, dm_pw: String, basedn: String) -> Result<Self, ()> {
let ldap = LdapClient::new(uri, basedn, LdapSchema::Rfc2307bis)?;
Ok(DirectoryServer { ldap, dm_pw })
}
pub fn build(uri: String, dm_pw: String, basedn: String) -> Result<TargetServer, ()> {
Self::construct(uri, dm_pw, basedn).map(TargetServer::DirSrv)
}
#[allow(clippy::new_ret_no_self)]
pub fn new(lconfig: &DsConfig) -> Result<TargetServer, ()> {
Self::construct(
lconfig.uri.clone(),
lconfig.dm_pw.clone(),
lconfig.base_dn.clone(),
)
.map(TargetServer::DirSrv)
}
pub fn info(&self) -> String {
format!("Directory Server Connection: {}", self.ldap.uri)
}
pub fn builder(&self) -> TargetServerBuilder {
TargetServerBuilder::DirSrv(
self.ldap.uri.clone(),
self.dm_pw.clone(),
self.ldap.basedn.clone(),
)
}
pub async fn open_admin_connection(&self) -> Result<(), ()> {
self.ldap.open_dm_connection(&self.dm_pw).await
}
pub async fn setup_admin_delete_uuids(&self, targets: &[Uuid]) -> Result<(), ()> {
let filter = LdapFilter::Or(
targets
.iter()
.map(|u| LdapFilter::Equality("cn".to_string(), u.to_string()))
.collect(),
);
print!("(|");
for u in targets.iter() {
print!("(cn={})", u);
}
println!(")");
let res = self.ldap.search(filter).await?;
for ent in res.iter() {
debug!("Deleting ... {}", ent.dn);
self.ldap.delete(ent.dn.clone()).await?;
}
Ok(())
}
pub async fn setup_admin_precreate_entities(
&self,
targets: &HashSet<Uuid>,
all_entities: &HashMap<Uuid, Entity>,
) -> Result<(), ()> {
let res = self
.ldap
.search(LdapFilter::Equality("ou".to_string(), "people".to_string()))
.await?;
if res.is_empty() {
info!("Creating ou=people");
let ou_people = LdapAddRequest {
dn: format!("ou=people,{}", self.ldap.basedn),
attributes: vec![
LdapAttribute {
atype: "objectClass".to_string(),
vals: vec![
"top".as_bytes().into(),
"organizationalUnit".as_bytes().into(),
],
},
LdapAttribute {
atype: "ou".to_string(),
vals: vec!["people".as_bytes().into()],
},
],
};
self.ldap.add(ou_people).await?;
}
let res = self
.ldap
.search(LdapFilter::Equality("ou".to_string(), "groups".to_string()))
.await?;
if res.is_empty() {
info!("Creating ou=groups");
let ou_groups = LdapAddRequest {
dn: format!("ou=groups,{}", self.ldap.basedn),
attributes: vec![
LdapAttribute {
atype: "objectClass".to_string(),
vals: vec![
"top".as_bytes().into(),
"organizationalUnit".as_bytes().into(),
],
},
LdapAttribute {
atype: "ou".to_string(),
vals: vec!["groups".as_bytes().into()],
},
],
};
self.ldap.add(ou_groups).await?;
}
for u in targets {
let res = self
.ldap
.search(LdapFilter::Equality("cn".to_string(), u.to_string()))
.await?;
if !res.is_empty() {
continue;
}
let e = all_entities.get(u).unwrap();
let dn = e.get_ds_ldap_dn(&self.ldap.basedn);
match e {
Entity::Account(a) => {
let account = LdapAddRequest {
dn,
attributes: vec![
LdapAttribute {
atype: "objectClass".to_string(),
vals: vec![
"top".as_bytes().into(),
"nsPerson".as_bytes().into(),
"nsAccount".as_bytes().into(),
"nsOrgPerson".as_bytes().into(),
"posixAccount".as_bytes().into(),
],
},
LdapAttribute {
atype: "cn".to_string(),
vals: vec![a.uuid.as_bytes().to_vec()],
},
LdapAttribute {
atype: "uid".to_string(),
vals: vec![a.name.as_bytes().into()],
},
LdapAttribute {
atype: "displayName".to_string(),
vals: vec![a.display_name.as_bytes().into()],
},
LdapAttribute {
atype: "userPassword".to_string(),
vals: vec![a.password.as_bytes().into()],
},
LdapAttribute {
atype: "homeDirectory".to_string(),
vals: vec![format!("/home/{}", a.uuid).as_bytes().into()],
},
LdapAttribute {
atype: "uidNumber".to_string(),
vals: vec!["1000".as_bytes().into()],
},
LdapAttribute {
atype: "gidNumber".to_string(),
vals: vec!["1000".as_bytes().into()],
},
],
};
self.ldap.add(account).await?;
}
Entity::Group(g) => {
let group = LdapAddRequest {
dn,
attributes: vec![
LdapAttribute {
atype: "objectClass".to_string(),
vals: vec![
"top".as_bytes().into(),
"groupOfNames".as_bytes().into(),
],
},
LdapAttribute {
atype: "cn".to_string(),
vals: vec![g.uuid.as_bytes().to_vec(), g.name.as_bytes().into()],
},
],
};
self.ldap.add(group).await?;
}
}
}
for g in targets.iter().filter_map(|u| {
let e = all_entities.get(u).unwrap();
match e {
Entity::Group(g) => Some(g),
_ => None,
}
}) {
let vals: Vec<Vec<u8>> = g
.members
.iter()
.map(|id| {
all_entities
.get(id)
.unwrap()
.get_ds_ldap_dn(&self.ldap.basedn)
.as_bytes()
.into()
})
.collect();
let req = LdapModifyRequest {
dn: g.get_ds_ldap_dn(&self.ldap.basedn),
changes: vec![LdapModify {
operation: LdapModifyType::Replace,
modification: LdapPartialAttribute {
atype: "member".to_string(),
vals,
},
}],
};
self.ldap.modify(req).await?;
}
Ok(())
}
pub async fn setup_access_controls(
&self,
access: &HashMap<Uuid, Vec<EntityType>>,
all_entities: &HashMap<Uuid, Entity>,
) -> Result<(), ()> {
let res = self
.ldap
.search(LdapFilter::Equality(
"cn".to_string(),
"priv_account_manage".to_string(),
))
.await?;
if res.is_empty() {
info!("Creating cn=priv_account_manage");
let group = LdapAddRequest {
dn: format!("cn=priv_account_manage,{}", self.ldap.basedn),
attributes: vec![
LdapAttribute {
atype: "objectClass".to_string(),
vals: vec!["top".as_bytes().into(), "groupOfNames".as_bytes().into()],
},
LdapAttribute {
atype: "cn".to_string(),
vals: vec!["priv_account_manage".as_bytes().into()],
},
],
};
self.ldap.add(group).await?;
}
let res = self
.ldap
.search(LdapFilter::Equality(
"cn".to_string(),
"priv_group_manage".to_string(),
))
.await?;
if res.is_empty() {
info!("Creating cn=priv_group_manage");
let group = LdapAddRequest {
dn: format!("cn=priv_group_manage,{}", self.ldap.basedn),
attributes: vec![
LdapAttribute {
atype: "objectClass".to_string(),
vals: vec!["top".as_bytes().into(), "groupOfNames".as_bytes().into()],
},
LdapAttribute {
atype: "cn".to_string(),
vals: vec!["priv_group_manage".as_bytes().into()],
},
],
};
self.ldap.add(group).await?;
}
let acimod = LdapModifyRequest {
dn: self.ldap.basedn.clone(),
changes: vec![
LdapModify {
operation: LdapModifyType::Replace,
modification: LdapPartialAttribute {
atype: "aci".to_string(),
vals: vec![
r#"(targetattr="dc || description || objectClass")(targetfilter="(objectClass=domain)")(version 3.0; acl "Enable anyone domain read"; allow (read, search, compare)(userdn="ldap:///anyone");)"#.as_bytes().into(),
r#"(targetattr="ou || objectClass")(targetfilter="(objectClass=organizationalUnit)")(version 3.0; acl "Enable anyone ou read"; allow (read, search, compare)(userdn="ldap:///anyone");)"#.as_bytes().into(),
r#"(targetattr="cn || member || gidNumber || nsUniqueId || description || objectClass")(targetfilter="(objectClass=groupOfNames)")(version 3.0; acl "Enable anyone group read"; allow (read, search, compare)(userdn="ldap:///anyone");)"#.as_bytes().into(),
format!(r#"(targetattr="cn || member || gidNumber || description || objectClass")(targetfilter="(objectClass=groupOfNames)")(version 3.0; acl "Enable group_admin to manage groups"; allow (write,add, delete)(groupdn="ldap:///cn=priv_group_manage,{}");)"#, self.ldap.basedn).as_bytes().into(),
r#"(targetattr="objectClass || description || nsUniqueId || uid || displayName || loginShell || uidNumber || gidNumber || gecos || homeDirectory || cn || memberOf || mail || nsSshPublicKey || nsAccountLock || userCertificate")(targetfilter="(objectClass=posixaccount)")(version 3.0; acl "Enable anyone user read"; allow (read, search, compare)(userdn="ldap:///anyone");)"#.as_bytes().into(),
r#"(targetattr="displayName || legalName || userPassword || nsSshPublicKey")(version 3.0; acl "Enable self partial modify"; allow (write)(userdn="ldap:///self");)"#.as_bytes().into(),
format!(r#"(targetattr="uid || description || displayName || loginShell || uidNumber || gidNumber || gecos || homeDirectory || cn || memberOf || mail || legalName || telephoneNumber || mobile")(targetfilter="(&(objectClass=nsPerson)(objectClass=nsAccount))")(version 3.0; acl "Enable user admin create"; allow (write, add, delete, read)(groupdn="ldap:///cn=priv_account_manage,{}");)"#, self.ldap.basedn).as_bytes().into(),
]
}
}
]
};
self.ldap.modify(acimod).await?;
let mut priv_account = Vec::new();
let mut priv_group = Vec::new();
for (id, list) in access.iter() {
let account = all_entities.get(id).unwrap();
let need_account = list
.iter()
.filter(|v| matches!(v, EntityType::Account(_)))
.count()
== 0;
let need_group = list
.iter()
.filter(|v| matches!(v, EntityType::Group(_)))
.count()
== 0;
if need_account {
priv_account.push(
account
.get_ds_ldap_dn(&self.ldap.basedn)
.as_bytes()
.to_vec(),
)
}
if need_group {
priv_group.push(
account
.get_ds_ldap_dn(&self.ldap.basedn)
.as_bytes()
.to_vec(),
)
}
}
priv_account.sort_unstable();
priv_group.sort_unstable();
priv_account.dedup();
priv_group.dedup();
info!("Setting up cn=priv_group_manage");
let req = LdapModifyRequest {
dn: format!("cn=priv_group_manage,{}", self.ldap.basedn),
changes: vec![LdapModify {
operation: LdapModifyType::Delete,
modification: LdapPartialAttribute {
atype: "member".to_string(),
vals: priv_group,
},
}],
};
let _ = self.ldap.modify(req).await;
info!("Setting up cn=priv_account_manage");
let req = LdapModifyRequest {
dn: format!("cn=priv_account_manage,{}", self.ldap.basedn),
changes: vec![LdapModify {
operation: LdapModifyType::Delete,
modification: LdapPartialAttribute {
atype: "member".to_string(),
vals: priv_account,
},
}],
};
let _ = self.ldap.modify(req).await;
Ok(())
}
pub async fn open_user_connection(
&self,
test_start: Instant,
name: &str,
pw: &str,
) -> Result<(Duration, Duration), ()> {
self.ldap.open_user_connection(test_start, name, pw).await
}
pub async fn close_connection(&self) {
self.ldap.close_connection().await;
}
pub async fn search(
&self,
test_start: Instant,
ids: &[String],
) -> Result<(Duration, Duration, usize), ()> {
self.ldap.search_name(test_start, ids).await
}
}