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
#![deny(warnings)]
#![warn(unused_extern_crates)]
#![deny(clippy::todo)]
#![deny(clippy::unimplemented)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::panic)]
#![deny(clippy::unreachable)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::needless_pass_by_value)]
#![deny(clippy::trivially_copy_pass_by_ref)]
// We allow expect since it forces good error messages at the least.
#![allow(clippy::expect_used)]
use clap::Parser;
use ldap3_client::proto::LdapFilter;
use ldap3_client::*;
include!("./ldap_opt.rs");
#[tokio::main(flavor = "current_thread")]
async fn main() {
let opt = LdapOpt::from_args();
ldap3_cli_common::start_tracing(opt.verbose);
info!("ldap command line utility");
let (bind_dn, bind_passwd) = if let Some(dn) = opt.bind_dn {
if let Some(pw) = opt.bind_passwd {
(dn.clone(), pw.clone())
} else if opt.json {
let e = LdapError::PasswordNotFound;
println!(
"{}",
serde_json::to_string_pretty(&e).expect("CRITICAL: Serialisation Fault")
);
std::process::exit(e as i32);
} else {
let pw = match rpassword::prompt_password(&format!("Enter password for {}: ", dn)) {
Ok(p) => p,
Err(e) => {
error!("Failed to get bind password - {}", e);
std::process::exit(LdapError::PasswordNotFound as i32);
}
};
(dn.clone(), pw)
}
} else {
if opt.bind_passwd.is_some() {
let e = LdapError::AnonymousInvalidState;
if opt.json {
println!(
"{}",
serde_json::to_string_pretty(&e).expect("CRITICAL: Serialisation Fault")
);
} else {
error!("Anonymous does not take a password - {}", e);
}
std::process::exit(e as i32);
} else {
("".to_string(), "".to_string())
}
};
let mut client = match LdapClientBuilder::new(&opt.url).build().await {
Ok(c) => c,
Err(e) => {
if opt.json {
println!(
"{}",
serde_json::to_string_pretty(&e).expect("CRITICAL: Serialisation Fault")
)
} else {
error!("Failed to create ldap client - {}", e);
}
std::process::exit(e as i32);
}
};
// The first message after connect is always a bind.
if let Err(e) = client.bind(bind_dn, bind_passwd).await {
if opt.json {
println!(
"{}",
serde_json::to_string_pretty(&e).expect("CRITICAL: Serialisation Fault")
)
} else {
error!("Failed to create ldap client - {}", e);
}
std::process::exit(e as i32);
};
match opt.action {
LdapAction::Search { basedn, filter } => {
let filter = ldap3_client::filter::parse_ldap_filter_str(&filter)
.map_err(|e| {
error!(?e);
})
.expect("Invalid filter");
match client.search(basedn, filter).await {
Ok(search_result) => {
if opt.json {
} else {
for ent in &search_result.entries {
println!("dn: {}", ent.dn);
for (attr, vals) in &ent.attrs {
for val in vals {
println!("{}: {}", attr, val);
}
}
println!("");
}
}
}
Err(e) => {
if opt.json {
println!(
"{}",
serde_json::to_string_pretty(&e)
.expect("CRITICAL: Serialisation Fault")
)
} else {
error!("Failed to send syncrepl request - {}", e);
}
std::process::exit(e as i32);
}
}
}
LdapAction::Whoami => match client.whoami().await {
Ok(Some(dn)) => {
if dn.is_empty() {
println!("dn: anonymous");
} else {
println!("dn: {}", dn);
}
}
Ok(None) => {
println!("dn: <N/A>");
}
Err(e) => {
if opt.json {
println!(
"{}",
serde_json::to_string_pretty(&e).expect("CRITICAL: Serialisation Fault")
)
} else {
error!("Failed to send whoami request - {}", e);
}
std::process::exit(e as i32);
}
},
/*
LdapAction::FilterHelper { filter, filter_comp } => {
let filter = ldap3_client::filter::parse_ldap_filter_str(&filter)
.map_err(|e| {
error!(?e);
})
.expect("Invalid filter");
if let Some(filter_comp) = filter_comp {
let filter_comp = ldap3_client::filter::parse_ldap_filter_str(&filter_comp)
.map_err(|e| {
error!(?e);
})
.expect("Invalid filter");
println!("compare: {}", filter == filter_comp);
}
}
*/
LdapAction::Syncrepl {
basedn,
cookie,
// mode,
} => {
/*
let mode = match mode {
SyncRequestMode::RefreshOnly => ldapcli::proto::SyncRequestMode::RefreshOnly,
SyncRequestMode::RefreshAndPersist => {
ldapcli::proto::SyncRequestMode::RefreshAndPersist
}
};
*/
let mode = proto::SyncRequestMode::RefreshOnly;
let cookie = if let Some(cookie) = cookie {
match base64::decode_config(&cookie, base64::STANDARD_NO_PAD) {
Ok(c) => Some(c),
Err(e) => {
error!(?e, "Failed to parse cookie");
return;
}
}
} else {
None
};
let filter = LdapFilter::Present("objectClass".to_string());
match client.syncrepl(basedn, filter, cookie, mode).await {
Ok(LdapSyncRepl::RefreshRequired) => {
error!("Cookie has been invalidated - a full refresh is required");
}
Ok(LdapSyncRepl::Success {
cookie,
refresh_deletes,
entries,
delete_uuids,
present_uuids,
}) => {
// what do?
for ent in &entries {
println!("entryuuid: {}", ent.entry_uuid);
println!("syncstate: {:?}", ent.state);
println!("dn: {}", ent.entry.dn);
for (attr, vals) in &ent.entry.attrs {
for val in vals {
println!("{}: {}", attr, val);
}
}
println!("");
}
for entry_uuid in &delete_uuids {
println!("delete entryuuid: {}", entry_uuid);
}
if !present_uuids.is_empty() {
println!("");
}
for entry_uuid in &present_uuids {
println!("present entryuuid: {}", entry_uuid);
println!("");
}
println!("");
println!("refresh_deletes: {}", refresh_deletes);
println!(
"cookie: {}",
cookie
.map(|bin| base64::encode_config(&bin, base64::STANDARD_NO_PAD))
.unwrap_or_else(|| "NONE".to_string())
);
}
Err(e) => {
if opt.json {
println!(
"{}",
serde_json::to_string_pretty(&e)
.expect("CRITICAL: Serialisation Fault")
)
} else {
error!("Failed to send syncrepl request - {}", e);
}
std::process::exit(e as i32);
}
}
}
LdapAction::AdDirsync { basedn, cookie } => {
let cookie = if let Some(cookie) = cookie {
match base64::decode_config(&cookie, base64::STANDARD_NO_PAD) {
Ok(c) => Some(c),
Err(e) => {
error!(?e, "Failed to parse cookie");
return;
}
}
} else {
None
};
match client.ad_dirsync(basedn, cookie).await {
Ok(sync_repl) => {
// what do?
for ent in &sync_repl.entries {
// println!("entryuuid: {}", ent.entry_uuid);
println!("dn: {}", ent.entry.dn);
for (attr, vals) in &ent.entry.attrs {
for val in vals {
println!("{}: {}", attr, val);
}
}
println!("");
}
for entry_uuid in &sync_repl.delete_uuids {
println!("delete entryuuid: {}", entry_uuid);
}
if !sync_repl.present_uuids.is_empty() {
println!("");
}
for entry_uuid in &sync_repl.present_uuids {
println!("delete entryuuid: {}", entry_uuid);
println!("");
}
println!("");
println!(
"cookie: {}",
sync_repl
.cookie
.map(|bin| base64::encode_config(&bin, base64::STANDARD_NO_PAD))
.unwrap_or_else(|| "NONE".to_string())
);
}
Err(e) => {
if opt.json {
println!(
"{}",
serde_json::to_string_pretty(&e)
.expect("CRITICAL: Serialisation Fault")
)
} else {
error!("Failed to send syncrepl request - {}", e);
}
std::process::exit(e as i32);
}
}
}
}
}