kadmin 0.7.2

Rust bindings for the Kerberos administration interface (kadm5)
Documentation
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Bindings to various kadm5 libraries

use std::ffi::OsStr;

use dlopen2::wrapper::{Container, WrapperApi};
#[cfg(feature = "python")]
use pyo3::prelude::*;

use crate::error::Result;

/// kadm5 library variant
///
/// Represent a kadm5 library to use. This struct will determine which C library kadmin will link
/// against. The list of currently supported options consist of the enum variants.
///
/// Depending on how kadmin was compiled, not all variants may be supported on your system. Refer
/// to the crate documentation on how to compile for all possible options.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[allow(clippy::exhaustive_enums)]
#[repr(u32)]
#[cfg_attr(feature = "python", pyclass(from_py_object, eq, eq_int))]
pub enum KAdm5Variant {
    #[cfg(mit_client)]
    /// MIT krb5 client-side
    MitClient,
    #[cfg(mit_server)]
    /// MIT krb5 server-side
    MitServer,
    #[cfg(heimdal_client)]
    /// Heimdal client-side
    HeimdalClient,
    #[cfg(heimdal_server)]
    /// Heimdal server-side
    HeimdalServer,
}

impl KAdm5Variant {
    /// Check if this [`KAdm5Variant`] is for MIT krb5
    pub fn is_mit(&self) -> bool {
        match self {
            #[cfg(mit_client)]
            Self::MitClient => true,
            #[cfg(mit_server)]
            Self::MitServer => true,
            #[allow(unreachable_patterns)]
            _ => false,
        }
    }

    /// Check if this [`KAdm5Variant`] is for Heimdal
    pub fn is_heimdal(&self) -> bool {
        match self {
            #[cfg(heimdal_client)]
            Self::HeimdalClient => true,
            #[cfg(heimdal_server)]
            Self::HeimdalServer => true,
            #[allow(unreachable_patterns)]
            _ => false,
        }
    }

    /// Check if this [`KAdm5Variant`] is for client-side usage
    pub fn is_client(&self) -> bool {
        match self {
            #[cfg(mit_client)]
            Self::MitClient => true,
            #[cfg(heimdal_client)]
            Self::HeimdalClient => true,
            _ => false,
        }
    }

    /// Check if this [`KAdm5Variant`] is for server-side usage
    pub fn is_server(&self) -> bool {
        match self {
            #[cfg(mit_server)]
            Self::MitServer => true,
            #[cfg(heimdal_server)]
            Self::HeimdalServer => true,
            _ => false,
        }
    }
}

/// Bindings to a kadm5 library
#[allow(clippy::exhaustive_enums)]
pub enum Library {
    /// Bindings for the MIT krb5 client-side library
    #[cfg(mit_client)]
    MitClient(Container<mit_client::Api>),
    /// Bindings for the MIT krb5 server-side library
    #[cfg(mit_server)]
    MitServer(Container<mit_server::Api>),
    /// Bindings for the Heimdal client-side library
    #[cfg(heimdal_client)]
    HeimdalClient(Container<heimdal_client::Api>),
    /// Bindings for the Heimdal server-side library
    #[cfg(heimdal_server)]
    HeimdalServer(Container<heimdal_server::Api>),
}

impl Library {
    /// Which [`KAdm5Variant`] this library implements
    pub fn variant(&self) -> KAdm5Variant {
        match self {
            #[cfg(mit_client)]
            Self::MitClient(_) => KAdm5Variant::MitClient,
            #[cfg(mit_server)]
            Self::MitServer(_) => KAdm5Variant::MitServer,
            #[cfg(heimdal_client)]
            Self::HeimdalClient(_) => KAdm5Variant::HeimdalClient,
            #[cfg(heimdal_server)]
            Self::HeimdalServer(_) => KAdm5Variant::HeimdalServer,
        }
    }

    /// Check if this [`Library`] is for MIT krb5
    pub fn is_mit(&self) -> bool {
        match self {
            #[cfg(mit_client)]
            Self::MitClient(_) => true,
            #[cfg(mit_server)]
            Self::MitServer(_) => true,
            #[allow(unreachable_patterns)]
            _ => false,
        }
    }

    /// Check if this [`Library`] is for Heimdal
    pub fn is_heimdal(&self) -> bool {
        match self {
            #[cfg(heimdal_client)]
            Self::HeimdalClient(_) => true,
            #[cfg(heimdal_server)]
            Self::HeimdalServer(_) => true,
            #[allow(unreachable_patterns)]
            _ => false,
        }
    }

    /// Check if this [`Library`] is for client-side usage
    pub fn is_client(&self) -> bool {
        match self {
            #[cfg(mit_client)]
            Self::MitClient(_) => true,
            #[cfg(heimdal_client)]
            Self::HeimdalClient(_) => true,
            _ => false,
        }
    }

    /// Check if this [`Library`] is for server-side usage
    pub fn is_server(&self) -> bool {
        match self {
            #[cfg(mit_server)]
            Self::MitServer(_) => true,
            #[cfg(heimdal_server)]
            Self::HeimdalServer(_) => true,
            _ => false,
        }
    }

    fn find_library<T: WrapperApi>(
        library_paths: Vec<&'static str>,
        libraries: Vec<&'static str>,
        sonames: Vec<&'static str>,
    ) -> Option<Container<T>> {
        let try_load = |full_path: &str| -> Option<Container<T>> {
            #[cfg(feature = "log")]
            log::trace!("Trying to load library at path {full_path}");
            let load = unsafe { Container::load(full_path) };
            load.inspect(|_| {
                #[cfg(feature = "log")]
                log::trace!("Successfully loaded library at {full_path}");
            })
            .inspect_err(|_err| {
                #[cfg(feature = "log")]
                log::trace!("Loading library at path {full_path} resulted in an error: {_err}");
            })
            .ok()
        };

        for soname in &sonames {
            if let Some(cont) = try_load(soname) {
                return Some(cont);
            }
        }

        for path in &library_paths {
            for library in &libraries {
                if let Some(cont) = try_load(&format!("{path}/lib{library}.so")) {
                    return Some(cont);
                }
            }
        }
        #[cfg(feature = "log")]
        log::trace!("Couldn't find a built-in library, trying a generic one");
        None
    }

    /// Create a new [`Library`] instance from a [`KAdm5Variant`]
    pub fn from_variant(variant: KAdm5Variant) -> Result<Self> {
        Ok(match variant {
            #[cfg(mit_client)]
            KAdm5Variant::MitClient => {
                if let Some(cont) = Self::find_library(
                    mit_client::library_paths(),
                    mit_client::libraries(),
                    mit_client::sonames(),
                ) {
                    Library::MitClient(cont)
                } else {
                    Library::MitClient(unsafe { Container::load("libkadm5clnt_mit.so") }?)
                }
            }
            #[cfg(mit_server)]
            KAdm5Variant::MitServer => {
                if let Some(cont) = Self::find_library(
                    mit_server::library_paths(),
                    mit_server::libraries(),
                    mit_server::sonames(),
                ) {
                    Library::MitServer(cont)
                } else {
                    Library::MitServer(unsafe { Container::load("libkadm5srv_mit.so") }?)
                }
            }
            #[cfg(heimdal_client)]
            KAdm5Variant::HeimdalClient => {
                if let Some(cont) = Self::find_library(
                    heimdal_client::library_paths(),
                    heimdal_client::libraries(),
                    heimdal_client::sonames(),
                ) {
                    Library::HeimdalClient(cont)
                } else {
                    Library::HeimdalClient(unsafe { Container::load("libkadm5clnt.so") }?)
                }
            }
            #[cfg(heimdal_server)]
            KAdm5Variant::HeimdalServer => {
                if let Some(cont) = Self::find_library(
                    heimdal_server::library_paths(),
                    heimdal_server::libraries(),
                    heimdal_server::sonames(),
                ) {
                    Library::HeimdalServer(cont)
                } else {
                    Library::HeimdalServer(unsafe { Container::load("libkadm5srv.so") }?)
                }
            }
        })
    }

    /// Create a new [`Library`] instance from a [`KAdm5Variant`] and a custom library path
    pub fn from_path<S: AsRef<OsStr>>(variant: KAdm5Variant, path: S) -> Result<Self> {
        Ok(match variant {
            #[cfg(mit_client)]
            KAdm5Variant::MitClient => Library::MitClient(unsafe { Container::load(path) }?),
            #[cfg(mit_server)]
            KAdm5Variant::MitServer => Library::MitServer(unsafe { Container::load(path) }?),
            #[cfg(heimdal_client)]
            KAdm5Variant::HeimdalClient => {
                Library::HeimdalClient(unsafe { Container::load(path) }?)
            }
            #[cfg(heimdal_server)]
            KAdm5Variant::HeimdalServer => {
                Library::HeimdalServer(unsafe { Container::load(path) }?)
            }
        })
    }
}

/// MIT kadm5-client bindings
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
#[allow(clippy::exhaustive_structs)]
#[allow(clippy::too_many_arguments)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::unseparated_literal_suffix)]
#[cfg(mit_client)]
pub mod mit_client {
    pub fn library_paths() -> Vec<&'static str> {
        env!("KADMIN_BUILD_MIT_CLIENT_LIBRARY_PATHS")
            .split_whitespace()
            .collect()
    }

    pub fn libraries() -> Vec<&'static str> {
        env!("KADMIN_BUILD_MIT_CLIENT_LIBRARIES")
            .split_whitespace()
            .collect()
    }

    pub fn sonames() -> Vec<&'static str> {
        env!("KADMIN_BUILD_MIT_CLIENT_SONAMES")
            .split_whitespace()
            .collect()
    }

    include!(concat!(env!("OUT_DIR"), "/bindings_mit_client.rs"));
}

/// MIT kadm5-server bindings
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
#[allow(clippy::exhaustive_structs)]
#[allow(clippy::too_many_arguments)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::unseparated_literal_suffix)]
#[cfg(mit_server)]
pub mod mit_server {
    pub fn library_paths() -> Vec<&'static str> {
        env!("KADMIN_BUILD_MIT_SERVER_LIBRARY_PATHS")
            .split_whitespace()
            .collect()
    }

    pub fn libraries() -> Vec<&'static str> {
        env!("KADMIN_BUILD_MIT_SERVER_LIBRARIES")
            .split_whitespace()
            .collect()
    }

    pub fn sonames() -> Vec<&'static str> {
        env!("KADMIN_BUILD_MIT_SERVER_SONAMES")
            .split_whitespace()
            .collect()
    }

    include!(concat!(env!("OUT_DIR"), "/bindings_mit_server.rs"));
}

/// Heimdal kadm5-client bindings
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
#[allow(unused_qualifications)]
#[allow(clippy::exhaustive_structs)]
#[allow(clippy::missing_safety_doc)]
#[allow(clippy::ptr_offset_with_cast)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::too_many_arguments)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::unseparated_literal_suffix)]
#[allow(clippy::useless_transmute)]
#[cfg(heimdal_client)]
pub mod heimdal_client {
    pub fn library_paths() -> Vec<&'static str> {
        env!("KADMIN_BUILD_HEIMDAL_CLIENT_LIBRARY_PATHS")
            .split_whitespace()
            .collect()
    }

    pub fn libraries() -> Vec<&'static str> {
        env!("KADMIN_BUILD_HEIMDAL_CLIENT_LIBRARIES")
            .split_whitespace()
            .collect()
    }

    pub fn sonames() -> Vec<&'static str> {
        env!("KADMIN_BUILD_HEIMDAL_CLIENT_SONAMES")
            .split_whitespace()
            .collect()
    }

    include!(concat!(env!("OUT_DIR"), "/bindings_heimdal_client.rs"));
}

/// Heimdal kadm5-server bindings
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
#[allow(unused_qualifications)]
#[allow(clippy::exhaustive_structs)]
#[allow(clippy::missing_safety_doc)]
#[allow(clippy::ptr_offset_with_cast)]
#[allow(clippy::semicolon_if_nothing_returned)]
#[allow(clippy::too_many_arguments)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::unseparated_literal_suffix)]
#[allow(clippy::useless_transmute)]
#[cfg(heimdal_server)]
pub mod heimdal_server {
    pub fn library_paths() -> Vec<&'static str> {
        env!("KADMIN_BUILD_HEIMDAL_SERVER_LIBRARY_PATHS")
            .split_whitespace()
            .collect()
    }

    pub fn libraries() -> Vec<&'static str> {
        env!("KADMIN_BUILD_HEIMDAL_SERVER_LIBRARIES")
            .split_whitespace()
            .collect()
    }

    pub fn sonames() -> Vec<&'static str> {
        env!("KADMIN_BUILD_HEIMDAL_SERVER_SONAMES")
            .split_whitespace()
            .collect()
    }

    include!(concat!(env!("OUT_DIR"), "/bindings_heimdal_server.rs"));
}

macro_rules! library_match {
    ($expr:expr; |$cont:ident, $lib:ident| $code:expr) => {
        library_match!(
            $expr;
            mit_client => |$cont, $lib| $code,
            mit_server => |$cont, $lib| $code,
            heimdal_client => |$cont, $lib| $code,
            heimdal_server => |$cont, $lib| $code
        )
    };

    ($expr:expr; $($($libname:ident),+ => |$cont:ident, $lib:ident| $code:expr),+) => {
        match $expr {
            $(
                $(
                    #[cfg($libname)]
                    library_match!(@variant $libname, $cont) => {
                        macro_rules! $lib {
                            ($ty:ident) => { crate::sys::$libname::$ty };
                        }
                        $code
                    }
                )+
            )+
        }
    };

    (@variant mit_client, $cont:ident) => {
        crate::sys::Library::MitClient($cont)
    };
    (@variant mit_server, $cont:ident) => {
        crate::sys::Library::MitServer($cont)
    };
    (@variant heimdal_client, $cont:ident) => {
        crate::sys::Library::HeimdalClient($cont)
    };
    (@variant heimdal_server, $cont:ident) => {
        crate::sys::Library::HeimdalServer($cont)
    };
}
pub(crate) use library_match;

macro_rules! cfg_match {
    (|$lib:ident| $code:expr) => {
        cfg_match!(
            mit_client => |$lib| $code,
            mit_server => |$lib| $code,
            heimdal_client => |$lib| $code,
            heimdal_server => |$lib| $code
        )
    };

    ($($($libname:ident),+ => |$lib:ident| $code:expr),+) => {
        $(
            $(
                #[cfg($libname)]
                {
                    macro_rules! $lib {
                        ($ty:ident) => { crate::sys::$libname::$ty };
                    }
                    $code
                };
            )+
        )+
    };
}
pub(crate) use cfg_match;

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(mit_client)]
    #[test_log::test]
    #[serial_test::serial]
    fn library_load_mit_client() -> Result<()> {
        Library::from_variant(KAdm5Variant::MitClient)?;
        Ok(())
    }

    #[cfg(mit_server)]
    #[test_log::test]
    #[serial_test::serial]
    fn library_load_mit_server() -> Result<()> {
        Library::from_variant(KAdm5Variant::MitServer)?;
        Ok(())
    }

    #[cfg(heimdal_client)]
    #[test_log::test]
    #[serial_test::serial]
    fn library_load_heimdal_client() -> Result<()> {
        Library::from_variant(KAdm5Variant::HeimdalClient)?;
        Ok(())
    }

    #[cfg(heimdal_server)]
    #[test_log::test]
    #[serial_test::serial]
    fn library_load_heimdal_server() -> Result<()> {
        Library::from_variant(KAdm5Variant::HeimdalServer)?;
        Ok(())
    }
}