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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Search operation adapters.
//!
//! A Search operation can return various objects in addition to directory entries, such as
//! referrals or intermediate messages, which may or may not be of interest to the user invoking
//! the operation. Some search operations will effectively span several discrete Search protocol
//! exchanges, as is the case for searches using the Paged Results control, or distributed
//! searches with referral chasing. Search adapters provide a mechanism to hand control of the
//! operation to user-defined code capable of handling such use cases, while presenting the invoker
//! with the same result-gathering interface.
//!
//! An adapter is a struct implementing the [`Adapter`](trait.Adapter.html) trait. A single adapter
//! struct or a vector of `Adapter` trait objects can be passed to the
//! [`streaming_search_with()`](../struct.Ldap.html#method.streaming_search_with) method on the `Ldap`
//! handle along with regular Search parameters to create an adapted search. Calling the stream
//! methods on the returned handle will execute the chain of `Adapter` methods from each adapter in
//! turn, ending with the direct call of the regular stream method.
//!
//! Adapters must be written with async calls, but work equally well for both async and sync versions of the API
//! because the sync API is just a blocking façade for the async one.

use std::fmt::Debug;
use std::marker::PhantomData;

use crate::controls::{self, Control, ControlType};
use crate::ldap::Ldap;
use crate::result::{LdapError, LdapResult, Result};
use crate::search::parse_refs;
use crate::search::{ResultEntry, Scope, SearchStream};

use async_trait::async_trait;

/// Adapter interface to a Search.
///
/// Structs implementing this trait:
///
/// * Must additionally implement `Clone` and `Debug`;
///
/// * Must be `Send` and `Sync`.
///
/// The trait is parametrized with `'a`, the lifetime bound propagated to trait objects, `S`, used in the `start()`
/// method as the generic type for attribute names, and `A`, the vector of attribute names. (They appear here
/// because of object safety; `A` enables initialization with owned or borrowed attribute lists.) When implementing the trait,
/// `S` must be constrained to `AsRef<str> + Send + Sync + 'a`, and `A` to `AsRef<[S]> + Send + Sync + 'a`.
/// To use a bare instance of a struct implementing this trait in the call to `streaming_search_with()`, the struct
/// must also implement [`SoloMarker`](trait.SoloMarker.html).
///
/// There are three points where an adapter can hook into a Search:
///
/// 1. Initialization, which can be used to capture the parameters of the operation
///    itself and the underlying `Ldap` handle, as well as to prepare the internal adapter state.
///    This is done in the [`start()`](#tymethod.start) method.
///
/// 2. Entry iteration, where each fetched entry can be examined, transformed, or discarded.
///    The [`next()`](#tymethod.next) method serves this purpose.
///
/// 3. Termination, which can examine and transform the result, or invoke further operations
///    to terminate other active connections or operations, if necessary. The [`finish()`](#tymethod.finish)
///    method is used for this.
///
/// All three methods are called in an async context, so they are marked as `async` and implemented using the
/// `async_trait` proc macro from the `async-trait` crate. To make chaining work, all trait methods must call
/// the corresponding method on the passed stream handle.
///
/// Additional details of the calling structure are provided in the documentation of the
/// [`StreamState`](../enum.StreamState.html) enum.
///
/// ## Example: the `EntriesOnly` adapter
///
/// This adapter discards intermediate messages and collects all referreals in the result of the Search
/// operation. The (slightly simplified) source is annotated by comments pointing out the notable
/// details of the implementation.
///
/// ```rust,no_run
/// # use async_trait::async_trait;
/// # use ldap3::adapters::{Adapter, SoloMarker};
/// # use ldap3::{ResultEntry, Scope, SearchStream};
/// # use ldap3::result::{LdapResult, Result};
/// # use ldap3::parse_refs;
/// // An adapter must implement Clone and Debug
/// //
/// // The slightly awkward Option-wrapping lets us move the vector
/// // out of the struct in finish()
/// #[derive(Clone, Debug)]
/// pub struct EntriesOnly {
///     refs: Option<Vec<String>>,
/// }
///
/// // This impl enables the use of a bare struct instance
/// // when invoking a Search
/// impl SoloMarker for EntriesOnly {}
///
/// // Adapter impl must be derived with the async_trait proc macro
/// // until Rust supports async fns in traits directly
/// #[async_trait]
/// impl<'a, S, A> Adapter<'a, S, A> for EntriesOnly
/// where
///     // The S and A generic parameters must have these bounds
///     S: AsRef<str> + Send + Sync + 'a,
///     A: AsRef<[S]> + Send + Sync + 'a,
/// {
///     // The start() method doesn't do much
///     async fn start(
///         &mut self,
///         stream: &mut SearchStream<'a, S, A>,
///         base: &str,
///         scope: Scope,
///         filter: &str,
///         attrs: A,
///     ) -> Result<()> {
///         self.refs.as_mut().expect("refs").clear();
///         // Call up the adapter chain
///         stream.start(base, scope, filter, attrs).await
///     }
///
///     // Multiple calls up the chain are possible before
///     // a single result entry is returned
///     async fn next(
///         &mut self,
///         stream: &mut SearchStream<'a, S, A>
///     ) -> Result<Option<ResultEntry>> {
///         loop {
///             // Call up the adapter chain
///             return match stream.next().await {
///                 Ok(None) => Ok(None),
///                 Ok(Some(re)) => {
///                     if re.is_intermediate() {
///                         continue;
///                     } else if re.is_ref() {
///                         self.refs.as_mut().expect("refs").extend(parse_refs(re.0));
///                         continue;
///                     } else {
///                         Ok(Some(re))
///                     }
///                 }
///                 Err(e) => Err(e),
///             };
///         }
///     }
///
///     // The result returned from the upcall is modified by our values
///     async fn finish(&mut self, stream: &mut SearchStream<'a, S, A>) -> LdapResult {
///         // Call up the adapter chain
///         let mut res = stream.finish().await;
///         res.refs.extend(self.refs.take().expect("refs"));
///         res
///     }
/// }
#[async_trait]
pub trait Adapter<'a, S, A>: AdapterClone<'a, S, A> + Debug + Send + Sync + 'a {
    /// Initialize the stream.
    async fn start(
        &mut self,
        stream: &mut SearchStream<'a, S, A>,
        base: &str,
        scope: Scope,
        filter: &str,
        attrs: A,
    ) -> Result<()>;

    /// Fetch the next entry from the stream.
    async fn next(&mut self, stream: &mut SearchStream<'a, S, A>) -> Result<Option<ResultEntry>>;

    /// Return the result from the stream.
    async fn finish(&mut self, stream: &mut SearchStream<'a, S, A>) -> LdapResult;
}

/// Helper trait to enforce `Clone` on `Adapter` implementors.
pub trait AdapterClone<'a, S, A> {
    fn box_clone(&self) -> Box<dyn Adapter<'a, S, A> + 'a>;
}

impl<'a, S, A, T> AdapterClone<'a, S, A> for T
where
    T: Adapter<'a, S, A> + Clone + 'a,
{
    fn box_clone(&self) -> Box<dyn Adapter<'a, S, A> + 'a> {
        Box::new(self.clone())
    }
}

/// Marker trait for convenient single-adapter searches.
///
/// If a struct implements this trait in addition to `Adapter`, its bare instance can appear
/// as the first argument of [`streaming_search_with()`](../struct.Ldap.html#method.streaming_search_with)
/// without the need for constructing a single-element vector containing the boxed trait object derived
/// from the instance.
pub trait SoloMarker {}

/// Helper trait for `Adapter` instance/chain conversions.
pub trait IntoAdapterVec<'a, S, A> {
    fn into(self) -> Vec<Box<dyn Adapter<'a, S, A> + 'a>>;
}

impl<'a, S, A> IntoAdapterVec<'a, S, A> for Vec<Box<dyn Adapter<'a, S, A> + 'a>> {
    fn into(self) -> Vec<Box<dyn Adapter<'a, S, A> + 'a>> {
        self
    }
}

impl<'a, Ad, S, A> IntoAdapterVec<'a, S, A> for Ad
where
    Ad: Adapter<'a, S, A> + SoloMarker,
    S: AsRef<str> + Send + Sync + 'a,
    A: AsRef<[S]> + Send + Sync + 'a,
{
    fn into(self) -> Vec<Box<dyn Adapter<'a, S, A> + 'a>> {
        vec![Box::new(self)]
    }
}

/// Adapter which returns just the directory entries.
///
/// This adapter mimics the earlier behavior of the crate, where referrals were collected
/// and returned in the overall result of the Search, and nothing but directory entries
/// were returned to the users.
///
/// To invoke a streaming Search with this adapter on the `ldap` handle, use
///
/// ```rust,no_run
/// # use ldap3::adapters::EntriesOnly;
/// # use ldap3::{LdapConn, Scope};
/// # let mut ldap = LdapConn::new("ldapi://ldapi").unwrap();
/// let mut stream = ldap.streaming_search_with(
///     EntriesOnly::new(),
///     "",
///     Scope::Base,
///     "(objectClass=*)",
///     vec!["+"]
/// );
/// # let _ = stream;
/// ```
#[derive(Clone, Debug)]
pub struct EntriesOnly {
    refs: Option<Vec<String>>,
}

/// Create a new adapter instance.
#[allow(clippy::new_without_default)]
impl EntriesOnly {
    pub fn new() -> Self {
        Self { refs: Some(vec![]) }
    }
}

impl SoloMarker for EntriesOnly {}

#[async_trait]
impl<'a, S, A> Adapter<'a, S, A> for EntriesOnly
where
    S: AsRef<str> + Send + Sync + 'a,
    A: AsRef<[S]> + Send + Sync + 'a,
{
    async fn start(
        &mut self,
        stream: &mut SearchStream<'a, S, A>,
        base: &str,
        scope: Scope,
        filter: &str,
        attrs: A,
    ) -> Result<()> {
        self.refs.as_mut().expect("refs").clear();
        stream.start(base, scope, filter, attrs).await
    }

    async fn next(&mut self, stream: &mut SearchStream<'a, S, A>) -> Result<Option<ResultEntry>> {
        loop {
            return match stream.next().await {
                Ok(None) => Ok(None),
                Ok(Some(re)) => {
                    if re.is_intermediate() {
                        continue;
                    } else if re.is_ref() {
                        self.refs.as_mut().expect("refs").extend(parse_refs(re.0));
                        continue;
                    } else {
                        Ok(Some(re))
                    }
                }
                Err(e) => Err(e),
            };
        }
    }

    async fn finish(&mut self, stream: &mut SearchStream<'a, S, A>) -> LdapResult {
        let mut res = stream.finish().await;
        res.refs.extend(self.refs.take().expect("refs"));
        res
    }
}

/// Adapter which fetches Search results with a Paged Results control.
///
/// The adapter adds a Paged Results control with the user-supplied page size to
/// a Search operation. The operation must not already contain a Paged Results
/// control; if it does, an error is reported. If the complete result set is not
/// retrieved in the first protocol operation, the adapter will automatically issue
/// further Searches until the whole search is done.
#[derive(Clone, Debug)]
pub struct PagedResults<S: AsRef<str>, A> {
    page_size: i32,
    ldap: Option<Ldap>,
    base: String,
    scope: Scope,
    filter: String,
    attrs: Option<A>,
    _s: PhantomData<S>,
}

impl<S, A> SoloMarker for PagedResults<S, A>
where
    S: AsRef<str> + Send + Sync,
    A: AsRef<[S]> + Send + Sync,
{
}

impl<S, A> PagedResults<S, A>
where
    S: AsRef<str> + Send + Sync,
    A: AsRef<[S]> + Send + Sync,
{
    /// Construct a new adapter instance with the requested page size.
    pub fn new(page_size: i32) -> Self {
        Self {
            page_size,
            ldap: None,
            base: String::from(""),
            scope: Scope::Base,
            filter: String::from(""),
            attrs: None,
            _s: PhantomData,
        }
    }
}

#[async_trait]
impl<'a, S, A> Adapter<'a, S, A> for PagedResults<S, A>
where
    S: AsRef<str> + Clone + Debug + Send + Sync + 'a,
    A: AsRef<[S]> + Clone + Debug + Send + Sync + 'a,
{
    async fn start(
        &mut self,
        stream: &mut SearchStream<'a, S, A>,
        base: &str,
        scope: Scope,
        filter: &str,
        attrs: A,
    ) -> Result<()> {
        let mut stream = stream;
        let stream_ldap = stream.ldap_handle();
        let mut ldap = stream_ldap.clone();
        ldap.timeout = stream_ldap.timeout;
        ldap.search_opts = stream_ldap.search_opts.clone();
        let empty_ctrls = vec![];
        let mut found_pr = false;
        let mut controls: Vec<_> = stream_ldap
            .controls
            .as_ref()
            .unwrap_or(&empty_ctrls)
            .iter()
            .filter(|c| {
                if c.ctype == "1.2.840.113556.1.4.319" {
                    found_pr = true;
                    false
                } else {
                    true
                }
            })
            .cloned()
            .collect();
        if found_pr {
            return Err(LdapError::AdapterInit(String::from(
                "found Paged Results control in op set",
            )));
        }
        ldap.controls = Some(controls.clone());
        controls.push(
            controls::PagedResults {
                size: self.page_size,
                cookie: vec![],
            }
            .into(),
        );
        // Not a typo for "stream_ldap", we're replacing Ldap controls.
        stream.ldap.controls = Some(controls);
        self.ldap = Some(ldap);
        self.base = String::from(base);
        self.scope = scope;
        self.filter = String::from(filter);
        self.attrs = Some(attrs.clone());
        stream.start(base, scope, filter, attrs).await
    }

    async fn next(&mut self, stream: &mut SearchStream<'a, S, A>) -> Result<Option<ResultEntry>> {
        'ent: loop {
            match stream.next().await {
                Ok(None) => {
                    let mut pr_index = None;
                    let ctrls = if let Some(res_ref) = stream.res.as_mut() {
                        &mut res_ref.ctrls
                    } else {
                        return Ok(None);
                    };
                    for (cno, ctrl) in ctrls.iter().enumerate() {
                        if let Control(Some(ControlType::PagedResults), ref raw) = *ctrl {
                            pr_index = Some(cno);
                            let pr: controls::PagedResults = raw.parse();
                            if pr.cookie.is_empty() {
                                break;
                            }
                            let ldap_ref = self.ldap.as_ref().expect("ldap_ref");
                            let mut ldap = ldap_ref.clone();
                            ldap.timeout = ldap_ref.timeout;
                            ldap.search_opts = ldap_ref.search_opts.clone();
                            let mut controls = ldap_ref.controls.clone().expect("saved ctrls");
                            controls.push(
                                controls::PagedResults {
                                    size: self.page_size,
                                    cookie: pr.cookie.clone(),
                                }
                                .into(),
                            );
                            ldap.controls = Some(controls);
                            let new_stream = match ldap
                                .streaming_search(
                                    &self.base,
                                    self.scope,
                                    &self.filter,
                                    self.attrs.as_ref().unwrap(),
                                )
                                .await
                            {
                                Ok(strm) => strm,
                                Err(e) => return Err(e),
                            };
                            // Again, we're replacing the innards of the original stream with
                            // the contents of the new one.
                            stream.ldap = new_stream.ldap;
                            stream.rx = new_stream.rx;
                            continue 'ent;
                        }
                    }
                    if let Some(pr_index) = pr_index {
                        ctrls.remove(pr_index);
                    }
                    return Ok(None);
                }
                any => return any,
            }
        }
    }

    async fn finish(&mut self, stream: &mut SearchStream<'a, S, A>) -> LdapResult {
        stream.finish().await
    }
}