sequoia-sq 1.4.0

Command-line frontends for Sequoia
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Command-line parser for `sq keyring`.

use std::path::PathBuf;

use clap::{Args, Parser, Subcommand};

use sequoia_openpgp as openpgp;
use openpgp::KeyHandle;

use crate::cli::examples::*;
use crate::cli::types::ClapData;
use crate::cli::types::FileOrStdin;
use crate::cli::types::FileOrStdout;

#[derive(Parser, Debug)]
#[clap(
    name = "keyring",
    about = "Manage collections of keys or certs",
    long_about =
"Manage collections of keys or certs

Collections of keys or certificates (also known as \"keyrings\" when \
they contain secret key material, and \"certrings\" when they don't) are \
any number of concatenated certificates.  This subcommand provides \
tools to list, split, merge, and filter keyrings.

Note: In the documentation of this subcommand, we sometimes use the \
terms keys and certs interchangeably.
",
    subcommand_required = true,
    arg_required_else_help = true,
    disable_help_subcommand = true,
)]
pub struct Command {
    #[clap(subcommand)]
    pub subcommand: Subcommands,
}

#[derive(Debug, Subcommand)]
pub enum Subcommands {
    List(ListCommand),
    Split(SplitCommand),
    Merge(MergeCommand),
    Filter(FilterCommand),
}

#[derive(Debug, Args)]
#[clap(
    about = "Join keys into a keyring applying a filter",
    long_about =
"Join keys into a keyring applying a filter

This can be used to filter keys based on given predicates, \
e.g. whether they have a user id containing an email address with a \
certain domain.  Additionally, the keys can be pruned to only include \
components matching the predicates.

If no filters are supplied, everything matches.

If multiple predicates are given, they are or'ed, i.e., a key matches \
if any of the predicates match.  To require all predicates to match, \
chain multiple invocations of this command.  See EXAMPLES for \
inspiration.

Note: this command is considered experimental and may change in future \
releases.  To acknowledge this, you must give the `--experimental` \
flag when invoking this command.
",
    after_help = FILTER_EXAMPLES,
)]
pub struct FilterCommand {
    #[clap(
        long = "experimental",
        required = true,
        help = "Opt-in to using an experimental feature",
        long_help = "\
Opt-in to using an experimental feature

This command is considered experimental and may change in future \
releases.  To acknowledge this, you must give the `--experimental` \
flag when invoking this command.

In the future, we may stabilize this command.  When that happens, \
`--experimental` will no longer be required, but will be ignored \
silently.
",
    )]
    pub _experimental: bool,

    #[clap(value_name = "FILE", help = "Read from FILE or stdin if omitted")]
    pub input: Vec<PathBuf>,
    #[clap(
        default_value_t = FileOrStdout::default(),
        help = FileOrStdout::HELP_OPTIONAL,
        long,
        value_name = FileOrStdout::VALUE_NAME,
    )]
    pub output: FileOrStdout,
    #[clap(
        long = "userid",
        value_name = "USERID",
        help = "Match on USERID",
        long_help = "Match on USERID

Case-sensitively matches on the \
user ID, requiring an exact match.",
    )]
    pub userid: Vec<String>,
    #[clap(
        long = "name",
        value_name = "NAME",
        help = "Match on NAME",
        long_help = "Match on NAME

Parse user IDs into name and email \
and case-sensitively matches on the \
name, requiring an exact match.",
    )]
    pub name: Vec<String>,
    #[clap(
        long = "email",
        value_name = "ADDRESS",
        help = "Match on email ADDRESS",
        long_help = "Match on email ADDRESS

Parse user IDs into name and email \
address and case-sensitively matches \
on the email address, requiring an exact match.",
    )]
    pub email: Vec<String>,
    #[clap(
        long = "domain",
        value_name = "FQDN",
        help = "Match on email domain FQDN",
        long_help =
            "Match on email domain FQDN

Parse user IDs into name and email \
address and case-sensitively matches \
on the domain of the email address, \
requiring an exact match.",
    )]
    pub domain: Vec<String>,

    #[clap(
        long = "cert",
        value_name = "FINGERPRINT|KEYID",
        help = "Match on certificate fingerprints and key IDs",
        long_help =
            "Match on certificate fingerprints and key IDs

Match on primary keys, \
including those certificates that match the \
given fingerprint or key ID.",
    )]
    pub cert: Vec<KeyHandle>,

    #[clap(
        long = "key",
        value_name = "FINGERPRINT|KEYID",
        help = "Match on (sub)key fingerprints and key IDs",
        long_help =
            "Match on (sub)key fingerprints and key IDs

Match on both primary keys and subkeys, \
including those certificates that match the \
given fingerprint or key ID.",
    )]
    pub key: Vec<KeyHandle>,

    #[clap(
        long = "prune-certs",
        help = "Remove certificate components not matching the filter",
    )]
    pub prune_certs: bool,

    #[clap(
        long = "to-cert",
        help = "Convert any keys in the input to certificates",
        long_help =
            "Convert any keys in the input to certificates

Converting a key to a \
certificate removes secret key material \
from the key thereby turning it into \
a certificate.",
    )]
    pub to_certificate: bool,
}

const FILTER_EXAMPLES: Actions = Actions {
    actions: &[
        Action::setup().command(&[
            "sq", "keyring", "merge",
            "--output=certs.pgp",
            "bob.pgp", "romeo.pgp",
        ]).build(),

        Action::example().comment(
            "Convert all keys to certificates (i.e. remove any secret key material).",
        ).command(&[
            "sq", "keyring", "filter",
            "--experimental",
            "--to-cert",
            "certs.pgp",
        ]).build(),

        Action::example().comment(
            "Get all certificates with a user ID on example.org.",
        ).command(&[
            "sq", "keyring", "filter",
            "--experimental",
            "--domain=example.org",
            "certs.pgp",
        ]).build(),

        Action::example().comment(
            "Get all certificates with a user ID on example.org or example.net.",
        ).command(&[
            "sq", "keyring", "filter",
            "--experimental",
            "--domain=example.org",
            "--domain=example.net",
            "certs.pgp",
        ]).build(),

        Action::example().comment(
            "Get all certificates with a name user ID matching Romeo.",
        ).command(&[
            "sq", "keyring", "filter",
            "--experimental",
            "--name=Romeo",
            "certs.pgp",
        ]).build(),

        Action::example().comment(
            "Get all certificates with a name user ID matching Romeo on example.org.",
        ).command(&[
            "sq", "keyring", "filter",
            "--experimental",
            "--domain=example.org",
            "certs.pgp",
            "|", "sq", "keyring", "filter",
            "--experimental",
            "--name=Romeo",
        ]).build(),

        Action::example().comment(
            "Get all certificates with a user ID on example.org, pruning other user IDs.",
        ).command(&[
            "sq", "keyring", "filter",
            "--experimental",
            "--domain=example.org",
            "--prune-certs",
            "certs.pgp",
        ]).build(),
    ],
};
test_examples!(sq_keyring_filter, FILTER_EXAMPLES);

#[derive(Debug, Args)]
#[clap(
    about = "Merge keys or keyrings into a single keyring",
    long_about =
"Merge keys or keyrings into a single keyring

Multiple \
versions of the same certificate are merged together.  Where data is \
replaced (e.g., secret key material), data from the later certificate \
is preferred.
",
    after_help = MERGE_EXAMPLES,
)]
pub struct MergeCommand {
    #[clap(value_name = "FILE", help = "Read from FILE or stdin if omitted")]
    pub input: Vec<PathBuf>,
    #[clap(
        default_value_t = FileOrStdout::default(),
        help = FileOrStdout::HELP_OPTIONAL,
        long,
        value_name = FileOrStdout::VALUE_NAME,
    )]
    pub output: FileOrStdout,
}

const MERGE_EXAMPLES: Actions = Actions {
    actions: &[
        Action::setup().command(&[
            "sq", "packet", "dearmor",
            "--output=bob-updates.pgp",
            "bob.pgp",
        ]).build(),

        Action::example().comment(
            "Merge certificate updates.",
        ).command(&[
            "sq", "keyring", "merge",
            "bob.pgp", "bob-updates.pgp",
        ]).build(),
    ],
};
test_examples!(sq_keyring_merge, MERGE_EXAMPLES);

#[derive(Debug, Args)]
#[clap(
    about = "List keys in a keyring",
    long_about =
"List keys in a keyring

Prints the fingerprint as well as the primary userid for every \
certificate encountered in the keyring.
",
    after_help = LIST_EXAMPLES,
)]
pub struct ListCommand {
    #[clap(
        default_value_t = FileOrStdin::default(),
        help = FileOrStdin::HELP_OPTIONAL,
        value_name = FileOrStdin::VALUE_NAME,
    )]
    pub input: FileOrStdin,
    #[clap(
        long = "all-userids",
        help = "List all user IDs",
        long_help = "List all user IDs

Include user IDs that are \
expired, revoked, or not valid under the \
standard policy.",
    )]
    pub all_userids: bool,
}

const LIST_EXAMPLES: Actions = Actions {
    actions: &[
        Action::setup().command(&[
            "sq", "keyring", "merge",
            "--output=certs.pgp",
            "bob.pgp", "romeo.pgp",
        ]).build(),

        Action::example().comment(
            "List all certificates.",
        ).command(&[
            "sq", "keyring", "list",
            "certs.pgp",
        ]).build(),

        Action::example().comment(
            "List all certificates with a user ID on example.org.",
        ).command(&[
            "sq", "keyring", "filter",
            "--experimental",
            "--domain=example.org",
            "certs.pgp",
            "|", "sq", "keyring", "list",
        ]).build(),
    ],
};
test_examples!(sq_keyring_list, LIST_EXAMPLES);

#[derive(Debug, Args)]
#[clap(
    about = "Split a keyring into individual keys",
    long_about =
"Split a keyring into individual keys

Splitting up a keyring into individual keys helps with curating a \
keyring.

The converse operation is `sq keyring merge`.
",
    after_help = SPLIT_EXAMPLES,
)]
pub struct SplitCommand {
    #[clap(
        default_value_t = FileOrStdin::default(),
        help = FileOrStdin::HELP_OPTIONAL,
        value_name = FileOrStdin::VALUE_NAME,
    )]
    pub input: FileOrStdin,
    #[clap(
        long = "prefix",
        value_name = "PREFIX",
        help = "Write to files with PREFIX \
            [defaults: `FILE-` if FILE is set, or `output-` if read from stdin]",
    )]
    pub prefix: Option<String>,
}

const SPLIT_EXAMPLES: Actions = Actions {
    actions: &[
        Action::setup().command(&[
            "sq", "keyring", "merge",
            "--output=certs.pgp",
            "bob.pgp", "romeo.pgp",
        ]).build(),

        Action::example().comment(
            "Split all certificates.",
        ).command(&[
            "sq", "keyring", "split",
            "certs.pgp",
        ]).build(),

        Action::example().comment(
            "Split all certificates, merging them first to avoid duplicates.",
        ).command(&[
            "sq", "keyring", "merge",
            "certs.pgp",
            "|", "sq", "keyring", "split",
        ]).build(),
    ],
};
test_examples!(sq_keyring_split, SPLIT_EXAMPLES);