@@ -75,7 +75,16 @@ pub struct Keyring {
75
75
}
76
76
77
77
impl Keyring {
78
- fn new ( id : KeyringSerial ) -> Self {
78
+ /// Instantiate a keyring from an ID.
79
+ ///
80
+ /// This is unsafe because no keyring is known to exist with the given ID.
81
+ pub unsafe fn new ( id : KeyringSerial ) -> Self {
82
+ Keyring {
83
+ id : id,
84
+ }
85
+ }
86
+
87
+ fn new_impl ( id : KeyringSerial ) -> Self {
79
88
Keyring {
80
89
id : id,
81
90
}
@@ -99,7 +108,7 @@ impl Keyring {
99
108
pub fn request < D > ( description : D ) -> Result < Self >
100
109
where D : AsRef < str > ,
101
110
{
102
- Keyring :: new ( 0 ) . request_keyring ( description)
111
+ Keyring :: new_impl ( 0 ) . request_keyring ( description)
103
112
}
104
113
105
114
/// Requests a keyring with the given description by searching the thread, process, and session
@@ -111,12 +120,12 @@ impl Keyring {
111
120
where D : AsRef < str > ,
112
121
I : AsRef < str > ,
113
122
{
114
- Keyring :: new ( 0 ) . request_keyring_with_fallback ( description, info)
123
+ Keyring :: new_impl ( 0 ) . request_keyring_with_fallback ( description, info)
115
124
}
116
125
117
126
fn get_keyring ( id : SpecialKeyring , create : bool ) -> Result < Keyring > {
118
127
let res = unsafe { keyctl_get_keyring_ID ( id. serial ( ) , create as libc:: c_int ) } ;
119
- check_call ( res as libc:: c_long , Keyring :: new ( res) )
128
+ check_call ( res as libc:: c_long , Keyring :: new_impl ( res) )
120
129
}
121
130
122
131
/// Attach to a special keyring. Fails if the keyring does not already exist.
@@ -132,7 +141,7 @@ impl Keyring {
132
141
/// Create a new anonymous keyring and set it as the session keyring.
133
142
pub fn join_anonymous_session ( ) -> Result < Self > {
134
143
let res = unsafe { keyctl_join_session_keyring ( ptr:: null ( ) ) } ;
135
- check_call ( res as libc:: c_long , Keyring :: new ( res) )
144
+ check_call ( res as libc:: c_long , Keyring :: new_impl ( res) )
136
145
}
137
146
138
147
/// Attached to a named session keyring.
@@ -144,7 +153,7 @@ impl Keyring {
144
153
{
145
154
let name_cstr = CString :: new ( name. as_ref ( ) ) . unwrap ( ) ;
146
155
let res = unsafe { keyctl_join_session_keyring ( name_cstr. as_ptr ( ) ) } ;
147
- check_call ( res as libc:: c_long , Keyring :: new ( res) )
156
+ check_call ( res as libc:: c_long , Keyring :: new_impl ( res) )
148
157
}
149
158
150
159
/// Clears the contents of the keyring.
@@ -201,7 +210,7 @@ impl Keyring {
201
210
where D : AsRef < str > ,
202
211
{
203
212
let res = self . search_impl ( "user" , description. as_ref ( ) ) ?;
204
- check_call ( res, Key :: new ( res as key_serial_t ) )
213
+ check_call ( res, Key :: new_impl ( res as key_serial_t ) )
205
214
}
206
215
207
216
/// Recursively search the keyring for a keyring with the matching description.
@@ -214,7 +223,7 @@ impl Keyring {
214
223
where D : AsRef < str > ,
215
224
{
216
225
let res = self . search_impl ( "keyring" , description. as_ref ( ) ) ?;
217
- check_call ( res, Keyring :: new ( res as key_serial_t ) )
226
+ check_call ( res, Keyring :: new_impl ( res as key_serial_t ) )
218
227
}
219
228
220
229
/// Return all immediate children of the keyring.
@@ -231,12 +240,12 @@ impl Keyring {
231
240
} ) ?;
232
241
unsafe { buffer. set_len ( ( actual_sz as usize ) / mem:: size_of :: < KeyringSerial > ( ) ) } ;
233
242
let keys = buffer. iter ( )
234
- . map ( |& id| Key :: new ( id) )
243
+ . map ( |& id| Key :: new_impl ( id) )
235
244
. partition ( |key| key. description ( ) . unwrap ( ) . type_ == "keyring" ) ;
236
245
Ok ( ( keys. 1 ,
237
246
keys. 0
238
247
. iter ( )
239
- . map ( |key| Keyring :: new ( key. id ) )
248
+ . map ( |key| Keyring :: new_impl ( key. id ) )
240
249
. collect :: < Vec < _ > > ( ) ) )
241
250
}
242
251
@@ -245,7 +254,7 @@ impl Keyring {
245
254
/// If one does not exist, it will be created. Requires `write` permission on the keyring.
246
255
pub fn attach_persistent ( & mut self ) -> Result < Self > {
247
256
let res = unsafe { keyctl_get_persistent ( !0 , self . id ) } ;
248
- check_call ( res, Keyring :: new ( res as key_serial_t ) )
257
+ check_call ( res, Keyring :: new_impl ( res as key_serial_t ) )
249
258
}
250
259
251
260
/// Adds a key of a specific type to the keyring.
@@ -276,7 +285,7 @@ impl Keyring {
276
285
payload. len ( ) ,
277
286
self . id )
278
287
} ;
279
- check_call ( res as libc:: c_long , Key :: new ( res) )
288
+ check_call ( res as libc:: c_long , Key :: new_impl ( res) )
280
289
}
281
290
282
291
/// Adds a keyring to the current keyring.
@@ -287,7 +296,7 @@ impl Keyring {
287
296
where D : Borrow < <keytypes:: Keyring as KeyType >:: Description > ,
288
297
{
289
298
let key = self . add_key :: < keytypes:: Keyring , _ , _ > ( description, ( ) ) ?;
290
- Ok ( Keyring :: new ( key. id ) )
299
+ Ok ( Keyring :: new_impl ( key. id ) )
291
300
}
292
301
293
302
fn request_impl ( & self , type_ : & str , description : & str ) -> Result < KeyringSerial > {
@@ -306,7 +315,7 @@ impl Keyring {
306
315
where D : AsRef < str > ,
307
316
{
308
317
let res = self . request_impl ( "user" , description. as_ref ( ) ) ?;
309
- check_call ( res as libc:: c_long , Key :: new ( res) )
318
+ check_call ( res as libc:: c_long , Key :: new_impl ( res) )
310
319
}
311
320
312
321
/// Requests a keyring with the given description by searching the thread, process, and session
@@ -317,7 +326,7 @@ impl Keyring {
317
326
where D : AsRef < str > ,
318
327
{
319
328
let res = self . request_impl ( "keyring" , description. as_ref ( ) ) ?;
320
- check_call ( res as libc:: c_long , Keyring :: new ( res) )
329
+ check_call ( res as libc:: c_long , Keyring :: new_impl ( res) )
321
330
}
322
331
323
332
fn request_fallback_impl ( & self , type_ : & str , description : & str , info : & str ) -> Result < KeyringSerial > {
@@ -343,7 +352,7 @@ impl Keyring {
343
352
I : AsRef < str > ,
344
353
{
345
354
let res = self . request_fallback_impl ( "user" , description. as_ref ( ) , info. as_ref ( ) ) ?;
346
- check_call ( res as libc:: c_long , Key :: new ( res) )
355
+ check_call ( res as libc:: c_long , Key :: new_impl ( res) )
347
356
}
348
357
349
358
/// Requests a keyring with the given description by searching the thread, process, and session
@@ -357,7 +366,7 @@ impl Keyring {
357
366
I : AsRef < str > ,
358
367
{
359
368
let res = self . request_fallback_impl ( "keyring" , description. as_ref ( ) , info. as_ref ( ) ) ?;
360
- check_call ( res as libc:: c_long , Keyring :: new ( res) )
369
+ check_call ( res as libc:: c_long , Keyring :: new_impl ( res) )
361
370
}
362
371
363
372
/// Revokes the keyring.
@@ -449,7 +458,14 @@ pub struct Key {
449
458
}
450
459
451
460
impl Key {
452
- fn new ( id : KeyringSerial ) -> Self {
461
+ /// Instantiate a key from an ID.
462
+ ///
463
+ /// This is unsafe because no key is known to exist with the given ID.
464
+ pub unsafe fn new ( id : KeyringSerial ) -> Self {
465
+ Self :: new_impl ( id)
466
+ }
467
+
468
+ fn new_impl ( id : KeyringSerial ) -> Self {
453
469
Key {
454
470
id : id,
455
471
}
@@ -459,15 +475,15 @@ impl Key {
459
475
/// keyrings.
460
476
pub fn request_key_auth_key ( create : bool ) -> Result < Self > {
461
477
let res = unsafe { keyctl_get_keyring_ID ( KEY_SPEC_REQKEY_AUTH_KEY , create as libc:: c_int ) } ;
462
- check_call ( res as libc:: c_long , Key :: new ( res) )
478
+ check_call ( res as libc:: c_long , Key :: new_impl ( res) )
463
479
}
464
480
465
481
/// Requests a key with the given description by searching the thread, process, and session
466
482
/// keyrings.
467
483
pub fn request < D > ( description : D ) -> Result < Self >
468
484
where D : AsRef < str > ,
469
485
{
470
- Keyring :: new ( 0 ) . request_key ( description)
486
+ Keyring :: new_impl ( 0 ) . request_key ( description)
471
487
}
472
488
473
489
/// Requests a key with the given description by searching the thread, process, and session
@@ -479,7 +495,7 @@ impl Key {
479
495
where D : AsRef < str > ,
480
496
I : AsRef < str > ,
481
497
{
482
- Keyring :: new ( 0 ) . request_key_with_fallback ( description, info)
498
+ Keyring :: new_impl ( 0 ) . request_key_with_fallback ( description, info)
483
499
}
484
500
485
501
/// Update the payload in the key.
@@ -495,31 +511,31 @@ impl Key {
495
511
496
512
/// Revokes the key. Requires `write` permission on the key.
497
513
pub fn revoke ( self ) -> Result < ( ) > {
498
- Keyring :: new ( self . id ) . revoke ( )
514
+ Keyring :: new_impl ( self . id ) . revoke ( )
499
515
}
500
516
501
517
/// Change the user which owns the key.
502
518
///
503
519
/// Requires the `setattr` permission on the key and the SysAdmin capability to change it to
504
520
/// anything other than the current user.
505
521
pub fn chown ( & mut self , uid : libc:: uid_t ) -> Result < ( ) > {
506
- Keyring :: new ( self . id ) . chown ( uid)
522
+ Keyring :: new_impl ( self . id ) . chown ( uid)
507
523
}
508
524
509
525
/// Change the group which owns the key.
510
526
///
511
527
/// Requires the `setattr` permission on the key and the SysAdmin capability to change it to
512
528
/// anything other than a group of which the current user is a member.
513
529
pub fn chgrp ( & mut self , gid : libc:: gid_t ) -> Result < ( ) > {
514
- Keyring :: new ( self . id ) . chgrp ( gid)
530
+ Keyring :: new_impl ( self . id ) . chgrp ( gid)
515
531
}
516
532
517
533
/// Set the permissions on the key.
518
534
///
519
535
/// Requires the `setattr` permission on the key and the SysAdmin capability if the current
520
536
/// user does not own the key.
521
537
pub fn set_permissions ( & mut self , perms : KeyPermissions ) -> Result < ( ) > {
522
- Keyring :: new ( self . id ) . set_permissions ( perms)
538
+ Keyring :: new_impl ( self . id ) . set_permissions ( perms)
523
539
}
524
540
525
541
/// Retrieve metadata about the key.
@@ -528,7 +544,7 @@ impl Key {
528
544
///
529
545
/// If the kernel returns malformed data, the parser will panic.
530
546
pub fn description ( & self ) -> Result < Description > {
531
- Keyring :: new ( self . id ) . description ( )
547
+ Keyring :: new_impl ( self . id ) . description ( )
532
548
}
533
549
534
550
/// Read the payload of the key. Requires `read` permissions on the key.
@@ -548,27 +564,27 @@ impl Key {
548
564
///
549
565
/// A timeout of `0` means "no expiration". Requires the `setattr` permission on the key.
550
566
pub fn set_timeout ( & mut self , timeout : u32 ) -> Result < ( ) > {
551
- Keyring :: new ( self . id ) . set_timeout ( timeout)
567
+ Keyring :: new_impl ( self . id ) . set_timeout ( timeout)
552
568
}
553
569
554
570
/// The security context of the key.
555
571
///
556
572
/// Depends on the security manager loaded into the kernel (e.g., SELinux or AppArmor).
557
573
pub fn security ( & self ) -> Result < String > {
558
- Keyring :: new ( self . id ) . security ( )
574
+ Keyring :: new_impl ( self . id ) . security ( )
559
575
}
560
576
561
577
/// Invalidates the key and schedules it for removal.
562
578
///
563
579
/// Requires the `search` permission on the key.
564
580
pub fn invalidate ( self ) -> Result < ( ) > {
565
- Keyring :: new ( self . id ) . invalidate ( )
581
+ Keyring :: new_impl ( self . id ) . invalidate ( )
566
582
}
567
583
568
584
/// Create an object to manage a key request.
569
585
pub fn manage ( & mut self ) -> Result < KeyManager > {
570
586
check_call ( unsafe { keyctl_assume_authority ( self . id ) } ,
571
- KeyManager :: new ( Key :: new ( self . id ) ) )
587
+ KeyManager :: new ( Key :: new_impl ( self . id ) ) )
572
588
}
573
589
574
590
/// Compute a Diffie-Hellman prime for use as a shared secret or public key.
0 commit comments