@@ -66,6 +66,7 @@ fn into_serial(res: libc::c_long) -> KeyringSerial {
66
66
KeyringSerial :: new ( res as i32 ) . unwrap ( )
67
67
}
68
68
69
+ /// Request a key from the kernel.
69
70
fn request_impl < K : KeyType > (
70
71
description : & str ,
71
72
info : Option < & str > ,
@@ -122,31 +123,24 @@ impl Keyring {
122
123
123
124
/// Requests a keyring with the given description by searching the thread, process, and session
124
125
/// keyrings.
125
- pub fn request < D > ( description : D ) -> Result < Self >
126
+ ///
127
+ /// If it is not found, the `info` string (if provided) will be handed off to
128
+ /// `/sbin/request-key` to generate the key.
129
+ ///
130
+ /// If `target` is given, the found keyring will be linked into it. If `target` is not given
131
+ /// and a new key is constructed due to the request, it will be linked into the default
132
+ /// keyring (see `Keyring::set_default`).
133
+ pub fn request < ' a , D , I , IS , T > ( description : D , info : I , target : T ) -> Result < Self >
126
134
where
127
135
D : AsRef < str > ,
136
+ I : Into < Option < IS > > ,
137
+ IS : AsRef < str > ,
138
+ T : Into < Option < TargetKeyring < ' a > > > ,
128
139
{
129
140
check_call_keyring ( request_impl :: < keytypes:: Keyring > (
130
141
description. as_ref ( ) ,
131
- None ,
132
- None ,
133
- ) )
134
- }
135
-
136
- /// Requests a keyring with the given description by searching the thread, process, and session
137
- /// keyrings.
138
- ///
139
- /// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
140
- /// the key.
141
- pub fn request_with_fallback < D , I > ( description : D , info : I ) -> Result < Self >
142
- where
143
- D : Borrow < <keytypes:: Keyring as KeyType >:: Description > ,
144
- I : AsRef < str > ,
145
- {
146
- check_call_keyring ( request_impl :: < keytypes:: Keyring > (
147
- & description. borrow ( ) . description ( ) ,
148
- Some ( info. as_ref ( ) ) ,
149
- None ,
142
+ info. into ( ) . as_ref ( ) . copied ( ) ,
143
+ target. into ( ) . map ( TargetKeyring :: serial) ,
150
144
) )
151
145
}
152
146
@@ -371,74 +365,6 @@ impl Keyring {
371
365
check_call_keyring ( self . add_key_impl :: < keytypes:: Keyring > ( description. borrow ( ) , & ( ) ) )
372
366
}
373
367
374
- /// Requests a key with the given description by searching the thread, process, and session
375
- /// keyrings.
376
- ///
377
- /// If it is found, it is attached to the keyring.
378
- pub fn request_key < K , D > ( & self , description : D ) -> Result < Key >
379
- where
380
- K : KeyType ,
381
- D : Borrow < K :: Description > ,
382
- {
383
- check_call_key ( request_impl :: < K > (
384
- & description. borrow ( ) . description ( ) ,
385
- None ,
386
- Some ( self . id ) ,
387
- ) )
388
- }
389
-
390
- /// Requests a keyring with the given description by searching the thread, process, and session
391
- /// keyrings.
392
- ///
393
- /// If it is found, it is attached to the keyring.
394
- pub fn request_keyring < D > ( & self , description : D ) -> Result < Self >
395
- where
396
- D : Borrow < <keytypes:: Keyring as KeyType >:: Description > ,
397
- {
398
- check_call_keyring ( request_impl :: < keytypes:: Keyring > (
399
- & description. borrow ( ) . description ( ) ,
400
- None ,
401
- Some ( self . id ) ,
402
- ) )
403
- }
404
-
405
- /// Requests a key with the given description by searching the thread, process, and session
406
- /// keyrings.
407
- ///
408
- /// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
409
- /// the key. If found, it will be attached to the current keyring. Requires `write` permission
410
- /// to the keyring.
411
- pub fn request_key_with_fallback < K , D , I > ( & self , description : D , info : I ) -> Result < Key >
412
- where
413
- K : KeyType ,
414
- D : Borrow < K :: Description > ,
415
- I : AsRef < str > ,
416
- {
417
- check_call_key ( request_impl :: < K > (
418
- & description. borrow ( ) . description ( ) ,
419
- Some ( info. as_ref ( ) ) ,
420
- Some ( self . id ) ,
421
- ) )
422
- }
423
-
424
- /// Requests a keyring with the given description by searching the thread, process, and session
425
- /// keyrings.
426
- ///
427
- /// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
428
- /// the key. If found, it will be attached to the current keyring. Requires `write` permission
429
- /// to the keyring.
430
- pub fn request_keyring_with_fallback < D , I > ( & self , description : D , info : I ) -> Result < Self >
431
- where
432
- D : Borrow < <keytypes:: Keyring as KeyType >:: Description > ,
433
- I : AsRef < str > ,
434
- {
435
- check_call_keyring ( request_impl :: < keytypes:: Keyring > (
436
- & description. borrow ( ) . description ( ) ,
437
- Some ( info. as_ref ( ) ) ,
438
- Some ( self . id ) ,
439
- ) )
440
- }
441
-
442
368
/// Revokes the keyring.
443
369
///
444
370
/// Requires `write` permission on the keyring.
@@ -561,35 +487,27 @@ impl Key {
561
487
self . id
562
488
}
563
489
564
- /// Requests a key with the given description by searching the thread, process, and session
565
- /// keyrings.
566
- pub fn request < K , D > ( description : D ) -> Result < Self >
567
- where
568
- K : KeyType ,
569
- D : Borrow < K :: Description > ,
570
- {
571
- check_call_key ( request_impl :: < K > (
572
- & description. borrow ( ) . description ( ) ,
573
- None ,
574
- None ,
575
- ) )
576
- }
577
-
578
- /// Requests a key with the given description by searching the thread, process, and session
579
- /// keyrings.
490
+ /// Requests a key with the given type and description by searching the thread, process, and
491
+ /// session keyrings.
580
492
///
581
- /// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
582
- /// the key.
583
- pub fn request_with_fallback < K , D , I > ( description : D , info : I ) -> Result < Self >
493
+ /// If it is not found, the `info` string (if provided) will be handed off to
494
+ /// `/sbin/request-key` to generate the key.
495
+ ///
496
+ /// If `target` is given, the found keyring will be linked into it. If `target` is not given
497
+ /// and a new key is constructed due to the request, it will be linked into the default
498
+ /// keyring (see `Keyring::set_default`).
499
+ pub fn request < ' a , K , D , I , IS , T > ( description : D , info : I , target : T ) -> Result < Self >
584
500
where
585
501
K : KeyType ,
586
502
D : Borrow < K :: Description > ,
587
- I : AsRef < str > ,
503
+ I : Into < Option < IS > > ,
504
+ IS : AsRef < str > ,
505
+ T : Into < Option < TargetKeyring < ' a > > > ,
588
506
{
589
- check_call_key ( request_impl :: < keytypes :: Keyring > (
507
+ check_call_key ( request_impl :: < K > (
590
508
& description. borrow ( ) . description ( ) ,
591
- Some ( info. as_ref ( ) ) ,
592
- None ,
509
+ info. into ( ) . as_ref ( ) . copied ( ) ,
510
+ target . into ( ) . map ( TargetKeyring :: serial ) ,
593
511
) )
594
512
}
595
513
@@ -911,30 +829,3 @@ impl KeyManager {
911
829
} )
912
830
}
913
831
}
914
-
915
- #[ cfg( test) ]
916
- mod tests {
917
- use super :: * ;
918
- use crate :: tests:: utils;
919
-
920
- #[ test]
921
- fn test_request_key ( ) {
922
- let mut keyring = utils:: new_test_keyring ( ) ;
923
-
924
- // Create the key.
925
- let description = "test:rust-keyutils:request_key" ;
926
- let payload = "payload" ;
927
- let key = keyring
928
- . add_key :: < keytypes:: User , _ , _ > ( description, payload. as_bytes ( ) )
929
- . unwrap ( ) ;
930
-
931
- let found_key = keyring
932
- . request_key :: < keytypes:: User , _ > ( description)
933
- . unwrap ( ) ;
934
- assert_eq ! ( found_key, key) ;
935
-
936
- // Clean up.
937
- keyring. unlink_key ( & key) . unwrap ( ) ;
938
- keyring. invalidate ( ) . unwrap ( ) ;
939
- }
940
- }
0 commit comments