1
1
//! Common helpers for CLI binaries.
2
2
3
- use pinentry:: { MessageDialog , PassphraseInput } ;
3
+ use pinentry:: PassphraseInput ;
4
4
use rand:: {
5
5
distributions:: { Distribution , Uniform } ,
6
6
rngs:: OsRng ,
@@ -11,33 +11,45 @@ use std::fs::File;
11
11
use std:: io:: { self , BufReader } ;
12
12
use subtle:: ConstantTimeEq ;
13
13
14
- use crate :: { fl, identity:: IdentityFile , Callbacks , Identity } ;
15
-
16
- #[ cfg( feature = "plugin" ) ]
17
- use crate :: plugin;
14
+ use crate :: { armor:: ArmoredReader , fl, identity:: IdentityFile , Callbacks , Identity } ;
18
15
19
16
pub mod file_io;
20
17
21
18
const BIP39_WORDLIST : & str = include_str ! ( "../assets/bip39-english.txt" ) ;
22
19
23
20
/// Reads identities from the provided files if given, or the default system
24
21
/// locations if no files are given.
25
- pub fn read_identities < E , G > (
22
+ pub fn read_identities < E , G , H > (
26
23
filenames : Vec < String > ,
24
+ max_work_factor : Option < u8 > ,
27
25
file_not_found : G ,
26
+ identity_encrypted_without_passphrase : H ,
28
27
#[ cfg( feature = "ssh" ) ] unsupported_ssh : impl Fn ( String , crate :: ssh:: UnsupportedKey ) -> E ,
29
28
) -> Result < Vec < Box < dyn Identity > > , E >
30
29
where
31
30
E : From < crate :: DecryptError > ,
32
31
E : From < io:: Error > ,
33
32
G : Fn ( String ) -> E ,
33
+ H : Fn ( String ) -> E ,
34
34
{
35
35
let mut identities: Vec < Box < dyn Identity > > = vec ! [ ] ;
36
36
37
- #[ cfg( feature = "plugin" ) ]
38
- let mut plugin_identities: Vec < plugin:: Identity > = vec ! [ ] ;
39
-
40
37
for filename in filenames {
38
+ // Try parsing as an encrypted age identity.
39
+ if let Ok ( identity) = crate :: encrypted:: Identity :: from_buffer (
40
+ ArmoredReader :: new ( BufReader :: new ( File :: open ( & filename) ?) ) ,
41
+ Some ( filename. clone ( ) ) ,
42
+ UiCallbacks ,
43
+ max_work_factor,
44
+ ) {
45
+ if let Some ( identity) = identity {
46
+ identities. push ( Box :: new ( identity) ) ;
47
+ continue ;
48
+ } else {
49
+ return Err ( identity_encrypted_without_passphrase ( filename) ) ;
50
+ }
51
+ }
52
+
41
53
// Try parsing as a single multi-line SSH identity.
42
54
#[ cfg( feature = "ssh" ) ]
43
55
match crate :: ssh:: Identity :: from_buffer (
59
71
_ => e. into ( ) ,
60
72
} ) ?;
61
73
62
- #[ cfg( feature = "plugin" ) ]
63
- let ( new_ids, mut new_plugin_ids) = identity_file. split_into ( ) ;
64
-
65
- #[ cfg( not( feature = "plugin" ) ) ]
66
- let new_ids = identity_file. into_identities ( ) ;
67
-
68
- identities. extend (
69
- new_ids
70
- . into_iter ( )
71
- . map ( |i| Box :: new ( i) as Box < dyn Identity > ) ,
72
- ) ;
73
-
74
- #[ cfg( feature = "plugin" ) ]
75
- plugin_identities. append ( & mut new_plugin_ids) ;
76
- }
77
-
78
- #[ cfg( feature = "plugin" ) ]
79
- {
80
- // Collect the names of the required plugins.
81
- let mut plugin_names = plugin_identities
82
- . iter ( )
83
- . map ( |r| r. plugin ( ) )
84
- . collect :: < Vec < _ > > ( ) ;
85
- plugin_names. sort_unstable ( ) ;
86
- plugin_names. dedup ( ) ;
87
-
88
- // Find the required plugins.
89
- for plugin_name in plugin_names {
90
- identities. push ( Box :: new ( crate :: plugin:: IdentityPluginV1 :: new (
91
- plugin_name,
92
- & plugin_identities,
93
- UiCallbacks ,
94
- ) ?) )
74
+ for entry in identity_file. into_identities ( ) {
75
+ identities. push ( entry. into_identity ( UiCallbacks ) ?) ;
95
76
}
96
77
}
97
78
@@ -162,18 +143,11 @@ pub fn read_secret(
162
143
}
163
144
164
145
/// Implementation of age callbacks that makes requests to the user via the UI.
146
+ #[ derive( Clone , Copy ) ]
165
147
pub struct UiCallbacks ;
166
148
167
149
impl Callbacks for UiCallbacks {
168
- fn prompt ( & self , message : & str ) {
169
- if let Some ( dialog) = MessageDialog :: with_default_binary ( ) {
170
- // pinentry binary is available!
171
- if dialog. show_message ( message) . is_ok ( ) {
172
- return ;
173
- }
174
- }
175
-
176
- // Fall back to CLI interface.
150
+ fn display_message ( & self , message : & str ) {
177
151
eprintln ! ( "{}" , message) ;
178
152
}
179
153
0 commit comments