Skip to main content

wildboar_ldap/
lib.rs

1#![doc = include_str!("../README.md")]
2#![allow(non_upper_case_globals)]
3#![allow(non_snake_case)]
4pub mod ldap_asn1;
5pub use ldap_asn1::*;
6
7/// Result Codes
8pub mod result_codes {
9    pub const success: usize = 0;
10    pub const operationsError: usize = 1;
11    pub const protocolError: usize = 2;
12    pub const timeLimitExceeded: usize = 3;
13    pub const sizeLimitExceeded: usize = 4;
14    pub const compareFalse: usize = 5;
15    pub const compareTrue: usize = 6;
16    pub const authMethodNotSupported: usize = 7;
17    pub const strongerAuthRequired: usize = 8;
18    pub const partialResults: usize = 9;
19    pub const referral: usize = 10;
20    pub const adminLimitExceeded: usize = 11;
21    pub const unavailableCriticalExtension: usize = 12;
22    pub const confidentialityRequired: usize = 13;
23    pub const saslBindInProgress: usize = 14;
24    pub const noSuchAttribute: usize = 16;
25    pub const undefinedAttributeType: usize = 17;
26    pub const inappropriateMatching: usize = 18;
27    pub const constraintViolation: usize = 19;
28    pub const attributeOrValueExists: usize = 20;
29    pub const invalidAttributeSyntax: usize = 21;
30    pub const noSuchObject: usize = 32;
31    pub const aliasProblem: usize = 33;
32    pub const invalidDNSyntax: usize = 34;
33    pub const isLeaf: usize = 35;
34    pub const aliasDereferencingProblem: usize = 36;
35    pub const inappropriateAuthentication: usize = 48;
36    pub const invalidCredentials: usize = 49;
37    pub const insufficientAccessRights: usize = 50;
38    pub const busy: usize = 51;
39    pub const unavailable: usize = 52;
40    pub const unwillingToPerform: usize = 53;
41    pub const loopDetect: usize = 54;
42    pub const namingViolation: usize = 64;
43    pub const objectClassViolation: usize = 65;
44    pub const notAllowedOnNonLeaf: usize = 66;
45    pub const notAllowedOnRDN: usize = 67;
46    pub const entryAlreadyExists: usize = 68;
47    pub const objectClassModsProhibited: usize = 69;
48    pub const resultsTooLarge: usize = 70;
49    pub const other: usize = 80;
50    pub const lcupResourcesExhausted: usize = 113;
51    pub const lcupSecurityViolation: usize = 114;
52    pub const lcupInvalidData: usize = 115;
53    pub const lcupUnsupportedScheme: usize = 116;
54    pub const lcupReloadRequired: usize = 117;
55    pub const canceled: usize = 118;
56    pub const noSuchOperation: usize = 119;
57    pub const tooLate: usize = 120;
58    pub const cannotCancel: usize = 121;
59    pub const assertionFailed: usize = 122;
60    pub const authorizationDenied: usize = 123;
61}
62
63/// Control Object Identifiers
64pub mod control_oids {
65    use wildboar_asn1::{OBJECT_IDENTIFIER, oid};
66    /// Matched Values Control [RFC3876](https://datatracker.ietf.org/doc/html/rfc3876)
67    #[inline]
68    pub fn matchedValues() -> OBJECT_IDENTIFIER {
69        oid!(1, 2, 826, 0, 1, 3344810, 2, 3)
70    }
71
72    /// LDAP Simple Paged Results Control [RFC2696](https://datatracker.ietf.org/doc/html/rfc2696)
73    #[inline]
74    pub fn simpledPagedResults() -> OBJECT_IDENTIFIER {
75        oid!(1, 2, 840, 113556, 1, 4, 319)
76    }
77
78    /// Sort Request [RFC2891](https://datatracker.ietf.org/doc/html/rfc2891)
79    #[inline]
80    pub fn sortRequest() -> OBJECT_IDENTIFIER {
81        oid!(1, 2, 840, 113556, 1, 4, 473)
82    }
83
84    /// Sort Response [RFC2891](https://datatracker.ietf.org/doc/html/rfc2891)
85    #[inline]
86    pub fn sortResponse() -> OBJECT_IDENTIFIER {
87        oid!(1, 2, 840, 113556, 1, 4, 474)
88    }
89
90    /// LCUP Sync Request Control [RFC3928](https://datatracker.ietf.org/doc/html/rfc3928)
91    #[inline]
92    pub fn lcupSyncRequest() -> OBJECT_IDENTIFIER {
93        oid!(1, 3, 6, 1, 1, 7, 1)
94    }
95
96    /// LCUP Sync Update Control [RFC3928](https://datatracker.ietf.org/doc/html/rfc3928)
97    #[inline]
98    pub fn lcupSyncUpdate() -> OBJECT_IDENTIFIER {
99        oid!(1, 3, 6, 1, 1, 7, 2)
100    }
101
102    /// LCUP Sync Done Control [RFC3928](https://datatracker.ietf.org/doc/html/rfc3928)
103    #[inline]
104    pub fn lcupSyncDone() -> OBJECT_IDENTIFIER {
105        oid!(1, 3, 6, 1, 1, 7, 3)
106    }
107
108    /// Assertion Control [RFC4528](https://datatracker.ietf.org/doc/html/rfc4528)  
109    #[inline]
110    pub fn assertion() -> OBJECT_IDENTIFIER {
111        oid!(1, 3, 6, 1, 1, 12)
112    }
113
114    /// LDAP Pre-read Control [RFC4527](https://datatracker.ietf.org/doc/html/rfc4527)
115    #[inline]
116    pub fn preread() -> OBJECT_IDENTIFIER {
117        oid!(1, 3, 6, 1, 1, 13, 1)
118    }
119
120    /// LDAP Post-read Control [RFC4527](https://datatracker.ietf.org/doc/html/rfc4527)
121    #[inline]
122    pub fn postread() -> OBJECT_IDENTIFIER {
123        oid!(1, 3, 6, 1, 1, 13, 2)
124    }
125
126    /// Transaction Specification Control [RFC5805](https://datatracker.ietf.org/doc/html/rfc5805)
127    #[inline]
128    pub fn transactionSpecification() -> OBJECT_IDENTIFIER {
129        oid!(1, 3, 6, 1, 1, 21, 2)
130    }
131
132    /// LDAP Content Synchronization Control [RFC4533](https://datatracker.ietf.org/doc/html/rfc4533)
133    #[inline]
134    pub fn contentSynchronization() -> OBJECT_IDENTIFIER {
135        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 9, 1, 1)
136    }
137
138    /// Subentries [RFC3672](https://datatracker.ietf.org/doc/html/rfc3672)
139    #[inline]
140    pub fn subentries() -> OBJECT_IDENTIFIER {
141        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 10, 1)
142    }
143
144    /// ManageDsaIT [RFC3296](https://datatracker.ietf.org/doc/html/rfc3296)
145    #[inline]
146    pub fn managedDSAIT() -> OBJECT_IDENTIFIER {
147        oid!(2, 16, 840, 1, 113730, 3, 4, 2)
148    }
149
150    /// Authorization Identity Response Control [RFC3829](https://datatracker.ietf.org/doc/html/rfc3829)
151    #[inline]
152    pub fn authorizationIdentityResponse() -> OBJECT_IDENTIFIER {
153        oid!(2, 16, 840, 1, 113730, 3, 4, 15)
154    }
155
156    /// Authorization Identity Request Control [RFC3829](https://datatracker.ietf.org/doc/html/rfc3829)
157    #[inline]
158    pub fn authorizationIdentityRequest() -> OBJECT_IDENTIFIER {
159        oid!(2, 16, 840, 1, 113730, 3, 4, 16)
160    }
161
162    /// Proxy Authorization Control [RFC4370](https://datatracker.ietf.org/doc/html/rfc4370)
163    #[inline]
164    pub fn proxyAuthorization() -> OBJECT_IDENTIFIER {
165        oid!(2, 16, 840, 1, 113730, 3, 4, 18)
166    }
167
168    /// LDAP Don't Use Copy Control [RFC6171](https://datatracker.ietf.org/doc/html/rfc6171)
169    #[inline]
170    pub fn dontUseCopy() -> OBJECT_IDENTIFIER {
171        oid!(1, 3, 6, 1, 1, 22)
172    }
173
174    /// IANA-Registered LDAP Controls as of 2025
175    pub const IANA_REGISTERED_CONTROLS_BY_2025: [fn() -> OBJECT_IDENTIFIER; 18] = [
176        matchedValues,
177        simpledPagedResults,
178        sortRequest,
179        sortResponse,
180        lcupSyncRequest,
181        lcupSyncUpdate,
182        lcupSyncDone,
183        assertion,
184        preread,
185        postread,
186        transactionSpecification,
187        contentSynchronization,
188        subentries,
189        managedDSAIT,
190        authorizationIdentityResponse,
191        authorizationIdentityRequest,
192        proxyAuthorization,
193        dontUseCopy,
194    ];
195}
196
197/// Extension Object Identifiers
198pub mod extension_oids {
199    use wildboar_asn1::{OBJECT_IDENTIFIER, oid};
200    /// Cancel Operation [RFC3909](https://datatracker.ietf.org/doc/html/rfc3909)
201    #[inline]
202    pub fn cancel() -> OBJECT_IDENTIFIER {
203        oid!(1, 3, 6, 1, 1, 8)
204    }
205
206    /// Start Transaction Extended Request [RFC5805](https://datatracker.ietf.org/doc/html/rfc5805)
207    #[inline]
208    pub fn startTransactionExtendedRequest() -> OBJECT_IDENTIFIER {
209        oid!(1, 3, 6, 1, 1, 21, 1)
210    }
211
212    /// End Transaction Extended Request [RFC5805](https://datatracker.ietf.org/doc/html/rfc5805)
213    #[inline]
214    pub fn endTransactionExtendedRequest() -> OBJECT_IDENTIFIER {
215        oid!(1, 3, 6, 1, 1, 21, 3)
216    }
217
218    /// Dynamic Refresh [RFC2589](https://datatracker.ietf.org/doc/html/rfc2589)
219    #[inline]
220    pub fn dynamicRefresh() -> OBJECT_IDENTIFIER {
221        oid!(1, 3, 6, 1, 4, 1, 1466, 101, 119, 1)
222    }
223
224    /// StartTLS [RFC4511](https://datatracker.ietf.org/doc/html/rfc4511)
225    #[inline]
226    pub fn startTLS() -> OBJECT_IDENTIFIER {
227        oid!(1, 3, 6, 1, 4, 1, 1466, 20037)
228    }
229
230    /// Modify Password [RFC3062](https://datatracker.ietf.org/doc/html/rfc3062)
231    #[inline]
232    pub fn modifyPassword() -> OBJECT_IDENTIFIER {
233        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 11, 1)
234    }
235
236    /// Who am I? [RFC4532](https://datatracker.ietf.org/doc/html/rfc4532)
237    #[inline]
238    pub fn whoAmI() -> OBJECT_IDENTIFIER {
239        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 11, 3)
240    }
241
242    /// StartLBURPRequest LDAP ExtendedRequest message [RFC4373](https://datatracker.ietf.org/doc/html/rfc4373)
243    #[inline]
244    pub fn startLBURPRequest() -> OBJECT_IDENTIFIER {
245        oid!(1, 3, 6, 1, 1, 17, 1)
246    }
247
248    /// StartLBURPResponse LDAP ExtendedResponse message [RFC4373](https://datatracker.ietf.org/doc/html/rfc4373)
249    #[inline]
250    pub fn startLBURPResponse() -> OBJECT_IDENTIFIER {
251        oid!(1, 3, 6, 1, 1, 17, 2)
252    }
253
254    /// EndLBURPRequest LDAP ExtendedRequest message [RFC4373](https://datatracker.ietf.org/doc/html/rfc4373)
255    #[inline]
256    pub fn endLBURPRequest() -> OBJECT_IDENTIFIER {
257        oid!(1, 3, 6, 1, 1, 17, 3)
258    }
259
260    /// EndLBURPResponse LDAP ExtendedResponse message [RFC4373](https://datatracker.ietf.org/doc/html/rfc4373) 
261    #[inline]
262    pub fn endLBURPResponse() -> OBJECT_IDENTIFIER {
263        oid!(1, 3, 6, 1, 1, 17, 4)
264    }
265
266    /// LBURPUpdateRequest LDAP ExtendedRequest message [RFC4373](https://datatracker.ietf.org/doc/html/rfc4373)
267    #[inline]
268    pub fn lburpUpdateRequest() -> OBJECT_IDENTIFIER {
269        oid!(1, 3, 6, 1, 1, 17, 5)
270    }
271
272    /// LBURPUpdateResponse LDAP ExtendedResponse message [RFC4373](https://datatracker.ietf.org/doc/html/rfc4373)
273    #[inline]
274    pub fn lburpUpdateResponse() -> OBJECT_IDENTIFIER {
275        oid!(1, 3, 6, 1, 1, 17, 6)
276    }
277
278    /// LDAP Turn Operation [RFC4531](https://datatracker.ietf.org/doc/html/rfc4531)
279    #[inline]
280    pub fn turn() -> OBJECT_IDENTIFIER {
281        oid!(1, 3, 6, 1, 1, 19)
282    }
283
284    /// IANA-Registered LDAP Extensions as of 2025
285    pub const IANA_REGISTERED_EXTENSIONS_BY_2025: [fn() -> OBJECT_IDENTIFIER; 14] = [
286        cancel,
287        startTransactionExtendedRequest,
288        endTransactionExtendedRequest,
289        dynamicRefresh,
290        startTLS,
291        modifyPassword,
292        whoAmI,
293        startLBURPRequest,
294        startLBURPResponse,
295        endLBURPRequest,
296        endLBURPResponse,
297        lburpUpdateRequest,
298        lburpUpdateResponse,
299        turn,
300    ];
301}
302
303/// Feature Object Identifiers
304pub mod feature_oids {
305    use wildboar_asn1::{OBJECT_IDENTIFIER, oid};
306    /// Modify-Increment [RFC4525](https://datatracker.ietf.org/doc/html/rfc4525)
307    #[inline]
308    pub fn modifyIncrement() -> OBJECT_IDENTIFIER {
309        oid!(1, 3, 6, 1, 1, 14)
310    }
311
312    /// All Op Attrs [RFC3673](https://datatracker.ietf.org/doc/html/rfc3673)
313    #[inline]
314    pub fn allOpAttrs() -> OBJECT_IDENTIFIER {
315        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 5, 1)
316    }
317
318    /// OC AD Lists [RFC4529](https://datatracker.ietf.org/doc/html/rfc4529)
319    #[inline]
320    pub fn ocadLists() -> OBJECT_IDENTIFIER {
321        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 5, 2)
322    }
323
324    /// True/False filters [RFC4526](https://datatracker.ietf.org/doc/html/rfc4526)
325    #[inline]
326    pub fn trueFalseFilters() -> OBJECT_IDENTIFIER {
327        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 5, 3)
328    }
329
330    /// Language Tag Options [RFC3866](https://datatracker.ietf.org/doc/html/rfc3866)
331    #[inline]
332    pub fn languageTag() -> OBJECT_IDENTIFIER {
333        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 5, 4)
334    }
335
336    /// Language Range Options [RFC3866](https://datatracker.ietf.org/doc/html/rfc3866)
337    #[inline]
338    pub fn languageRange() -> OBJECT_IDENTIFIER {
339        oid!(1, 3, 6, 1, 4, 1, 4203, 1, 5, 5)
340    }
341
342    /// LBURP Incremental Update style OID [RFC4373](https://datatracker.ietf.org/doc/html/rfc4373)
343    #[inline]
344    pub fn lburpIncrementalUpdateStyle() -> OBJECT_IDENTIFIER {
345        oid!(1, 3, 6, 1, 1, 17, 7)
346    }
347
348    /// IANA-Registered LDAP Features as of 2025
349    pub const IANA_REGISTERED_FEATURES_BY_2025: [fn() -> OBJECT_IDENTIFIER; 7] = [
350        modifyIncrement,
351        allOpAttrs,
352        ocadLists,
353        trueFalseFilters,
354        languageTag,
355        languageRange,
356        lburpIncrementalUpdateStyle,
357    ];
358}
359
360/// Notice Object Identifiers
361pub mod notice_oids {
362    use wildboar_asn1::{OBJECT_IDENTIFIER, oid};
363
364    /// Aborted Transaction Notice [RFC5805](https://datatracker.ietf.org/doc/html/rfc5805)
365    #[inline]
366    pub fn abortedTransaction() -> OBJECT_IDENTIFIER {
367        oid!(1, 3, 6, 1, 1, 21, 4)
368    }
369
370    /// IANA-Registered LDAP Notices as of 2025
371    pub const IANA_REGISTERED_NOTICES_BY_2025: [fn() -> OBJECT_IDENTIFIER; 1] = [abortedTransaction];
372
373}
374
375/// Check if two LDAP attribute descriptors are the same attribute type
376///
377/// ## Returns
378/// - `true` if the attribute descriptions are equivalent, `false` otherwise
379///
380/// ## Examples
381/// ```rust
382/// use wildboar_ldap::compare_attribute_types;
383///
384/// // Same OID, different case
385/// assert!(compare_attribute_types(b"cn", b"CN"));
386///
387/// // Same OID with options
388/// assert!(compare_attribute_types(b"cn;lang-en", b"CN;lang-fr"));
389///
390/// // Different OIDs
391/// assert!(!compare_attribute_types(b"cn", b"sn"));
392/// ```
393pub fn compare_attribute_types(a: &[u8], b: &[u8]) -> bool {
394    const SEMI_COLON: u8 = b';';
395
396    // Find semicolon positions
397    let a_semi_index = a.iter().position(|&byte| byte == SEMI_COLON);
398    let b_semi_index = b.iter().position(|&byte| byte == SEMI_COLON);
399
400    // Extract OID portions (everything before semicolon, or entire string if no semicolon)
401    let a_oid = match a_semi_index {
402        Some(index) => &a[..index],
403        None => a,
404    };
405
406    let b_oid = match b_semi_index {
407        Some(index) => &b[..index],
408        None => b,
409    };
410
411    // If OIDs have different lengths, they can't be equal
412    if a_oid.len() != b_oid.len() {
413        return false;
414    }
415
416    // Compare OIDs case-insensitively
417    a_oid
418        .iter()
419        .zip(b_oid.iter())
420        .all(|(&a_byte, &b_byte)| a_byte.eq_ignore_ascii_case(&b_byte))
421}