File tree Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change @@ -378,3 +378,38 @@ pub fn keyctl_dh_compute(
378
378
}
379
379
. map ( size)
380
380
}
381
+
382
+ pub enum Restriction < ' a > {
383
+ AllLinks ,
384
+ ByType {
385
+ type_ : & ' a str ,
386
+ restriction : & ' a str ,
387
+ } ,
388
+ }
389
+
390
+ pub fn keyctl_restrict_keyring ( keyring : KeyringSerial , restriction : Restriction ) -> Result < ( ) > {
391
+ let type_cstr;
392
+ let restriction_cstr;
393
+
394
+ let ( type_ptr, restriction_ptr) = match restriction {
395
+ Restriction :: AllLinks => ( ptr:: null ( ) , ptr:: null ( ) ) ,
396
+ Restriction :: ByType {
397
+ type_,
398
+ restriction,
399
+ } => {
400
+ type_cstr = cstring ( type_) ;
401
+ restriction_cstr = cstring ( restriction) ;
402
+
403
+ ( type_cstr. as_ptr ( ) , restriction_cstr. as_ptr ( ) )
404
+ } ,
405
+ } ;
406
+ unsafe {
407
+ keyctl ! (
408
+ libc:: KEYCTL_RESTRICT_KEYRING ,
409
+ keyring. get( ) ,
410
+ type_ptr,
411
+ restriction_ptr,
412
+ )
413
+ }
414
+ . map ( ignore)
415
+ }
Original file line number Diff line number Diff line change @@ -403,6 +403,32 @@ impl Keyring {
403
403
keyctl_setperm ( self . id , perms)
404
404
}
405
405
406
+ /// Restrict all links into the keyring.
407
+ ///
408
+ /// Requires the `setattr` permission on the keyring and the SysAdmin capability to change it to
409
+ /// anything other than the current user.
410
+ pub fn restrict_all ( & mut self ) -> Result < ( ) > {
411
+ keyctl_restrict_keyring ( self . id , Restriction :: AllLinks )
412
+ }
413
+
414
+ /// Restrict links into the keyring.
415
+ ///
416
+ /// Requires the `setattr` permission on the keyring and the SysAdmin capability to change it to
417
+ /// anything other than the current user.
418
+ pub fn restrict_by_type < K , R > ( & mut self , restriction : R ) -> Result < ( ) >
419
+ where
420
+ K : RestrictableKeyType ,
421
+ R : Borrow < K :: Restriction > ,
422
+ {
423
+ keyctl_restrict_keyring (
424
+ self . id ,
425
+ Restriction :: ByType {
426
+ type_ : K :: name ( ) ,
427
+ restriction : & restriction. borrow ( ) . restriction ( ) ,
428
+ } ,
429
+ )
430
+ }
431
+
406
432
fn description_raw ( & self ) -> Result < String > {
407
433
// Get the size of the description.
408
434
let mut sz = keyctl_describe ( self . id , None ) ?;
Original file line number Diff line number Diff line change @@ -90,3 +90,27 @@ impl KeyPayload for Vec<u8> {
90
90
Cow :: Borrowed ( & self )
91
91
}
92
92
}
93
+
94
+ /// A key which may be restricted into being added to a keyring.
95
+ pub trait RestrictableKeyType : KeyType {
96
+ /// The type for representing a restriction for adding keys of this type.
97
+ type Restriction : KeyRestriction + ?Sized ;
98
+ }
99
+
100
+ /// A restriction for a key.
101
+ pub trait KeyRestriction {
102
+ /// The restriction string of the key.
103
+ fn restriction ( & self ) -> Cow < str > ;
104
+ }
105
+
106
+ impl KeyRestriction for str {
107
+ fn restriction ( & self ) -> Cow < str > {
108
+ Cow :: Borrowed ( & self )
109
+ }
110
+ }
111
+
112
+ impl KeyRestriction for String {
113
+ fn restriction ( & self ) -> Cow < str > {
114
+ Cow :: Borrowed ( & self )
115
+ }
116
+ }
You can’t perform that action at this time.
0 commit comments