X-Git-Url: http://git.sthu.org/?a=blobdiff_plain;f=caff%2Fcaff;h=35be431932aa20e283b390fb3599820552896a9b;hb=e201631c7543dfd60a38d08c7328bc13f147e149;hp=5c75332df26fe58e66189c534f362dc08f1620da;hpb=271b3606c951690b5f1bf0c85ac9de8a572fe472;p=pgp-tools.git diff --git a/caff/caff b/caff/caff index 5c75332..35be431 100755 --- a/caff/caff +++ b/caff/caff @@ -85,6 +85,10 @@ Do not sign the keys. Select the key that is used for signing, in case you have more than one key. +=item B<--key-file> I + +Import keys from file. Can be supplied more than once. + =back =head1 FILES @@ -169,6 +173,10 @@ Keyserver to download keys from. Default: B. If true, then skip the step of fetching keys from the keyserver. Default: B<0>. +=item B [list of files] + +A list of files containing keys to be imported. + =head2 Signing settings =item B [boolean] @@ -258,9 +266,62 @@ my $REVISION = '$Rev$'; my ($REVISION_NUMER) = $REVISION =~ /(\d+)/; my $VERSION = "0.0.0.$REVISION_NUMER"; +sub generate_config() { + die "Error: \$LOGNAME is not set.\n" unless $ENV{LOGNAME}; + my $gecos = (getpwnam($ENV{LOGNAME}))[6]; + $gecos =~ s/,.*//; + + my $gpg = GnuPG::Interface->new(); + $gpg->call( 'gpg' ); + $gpg->options->hash_init( + 'extra_args' => [ qw{ --no-auto-check-trustdb --trust-model=always --with-colons --fixed-list-mode } ] ); + $gpg->options->meta_interactive( 0 ); + my ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds(); + my $pid = $gpg->list_public_keys(handles => $handles, command_args => [ $gecos ]); + my ($stdout, $stderr, $status) = readwrite_gpg('', $inputfd, $stdoutfd, $stderrfd, $statusfd); + waitpid $pid, 0; + + if ($stdout eq '') { + warn ("No data from gpg for list-key\n"); # There should be at least 'tru:' everywhere. + }; + + my @keys; + unless (@keys = ($stdout =~ /^pub:[^r:]*:(?:[^:]*:){2,2}([^:]+):/mg)) { + die "Error: No keys were found using \"gpg --list-public-keys '$gecos'\".\n"; + } + unless ($stdout =~ /^uid:.*<(.+@.+)>.*:/m) { + die "Error: No email address was found using \"gpg --list-public-keys '$gecos'\".\n"; + } + my $email = $1; + + return < +# +# if you have a v4 key, it will simply be the last 16 digits of +# your fingerprint. + +\$CONFIG{'keyid'} = [ qw{@keys} ]; +EOT +}; + sub load_config() { my $config = $ENV{'HOME'} . '/.caffrc'; - -f $config or die "No file $config present. See caff(1).\n"; + unless (-f $config) { + print "No configfile $config present, I will use this template:\n"; + my $template = generate_config(); + print "$template\nPress enter to continue."; + ; + open F, ">$config" or die "$config: $!"; + print F $template; + close F; + } unless (scalar eval `cat $config`) { die "Couldn't parse $config: $EVAL_ERROR\n" if $EVAL_ERROR; }; @@ -282,6 +343,7 @@ sub load_config() { $CONFIG{'secret-keyring'} = $ENV{'HOME'}.'/.gnupg/secring.gpg' unless defined $CONFIG{'secret-keyring'}; $CONFIG{'no-download'} = 0 unless defined $CONFIG{'no-download'}; $CONFIG{'no-sign'} = 0 unless defined $CONFIG{'no-sign'}; + $CONFIG{'key-files'} = () unless defined $CONFIG{'key-files'}; $CONFIG{'mail-template'} = <<'EOM' unless defined $CONFIG{'mail-template'}; Hi, @@ -439,8 +501,9 @@ sub readwrite_gpg($$$$$%) { sub ask($$;$$) { my ($question, $default, $forceyes, $forceno) = @_; my $answer; + my $yn = $default ? '[Y/n]' : '[y/N]'; while (1) { - print $question,' ',($default ? '[Y/n]' : '[y/N]'), ' '; + print $question,' ',$yn, ' '; if ($forceyes && $forceno) { print "$default (from config/command line)\n"; return $default; @@ -455,9 +518,17 @@ sub ask($$;$$) { }; $answer = ; + if (!defined $answer) { + $OUTPUT_AUTOFLUSH = 1; + die "\n\n". + "End of STDIN reached. Are you using xargs? Caff wants to read from STDIN,\n". + "so you can't really use it with xargs. A patch against caff to read from\n". + "the terminal would be appreciated.\n". + "For now instead of cat keys | xargs caff do caff `cat keys`\n"; + }; chomp $answer; - last if ((defined $answer) && (length $answer <= 1)); - print "grrrrrr.\n"; + last if ((length $answer == 0) || ($answer =~ m/^[yYnN]$/) ); + print "What about $yn is so hard to understand?\nAnswer with either 'n' or 'y' or just press enter for the default.\n"; sleep 1; }; my $result = $default; @@ -717,6 +788,7 @@ if (!GetOptions ( '--no-download' => \$params->{'no-download'}, '-S' => \$params->{'no-sign'}, '--no-sign' => \$params->{'no-sign'}, + '--key-file=s@' => \$params->{'key-files'}, )) { usage(\*STDERR, 1); }; @@ -758,6 +830,7 @@ $CONFIG{'no-download'} = $params->{'no-download'} if defined $params->{'no-downl $CONFIG{'no-mail'} = $params->{'no-mail'} if defined $params->{'no-mail'}; $CONFIG{'mail'} = $params->{'mail'} if defined $params->{'mail'}; $CONFIG{'no-sign'} = $params->{'no-sign'} if defined $params->{'no-sign'}; +push @{$CONFIG{'key-files'}}, @{$params->{'key-files'}} if defined $params->{'key-files'}; ################# @@ -793,6 +866,24 @@ for my $keyid (@{$CONFIG{'keyid'}}) { } } +######################## +# import keys from files +######################## +foreach my $keyfile (@{$CONFIG{'key-files'}}) { + my $gpg = GnuPG::Interface->new(); + $gpg->call( $CONFIG{'gpg'} ); + $gpg->options->hash_init('homedir' => $GNUPGHOME); + $gpg->options->meta_interactive( 0 ); + my ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds(); + my $pid = $gpg->import_keys(handles => $handles, command_args => $keyfile); + my ($stdout, $stderr, $status) = readwrite_gpg('', $inputfd, $stdoutfd, $stderrfd, $statusfd); + info ("Importing keys from $keyfile"); + waitpid $pid, 0; + if ($status !~ /^\[GNUPG:\] IMPORT_OK/m) { + warn $stderr; + } +} + ############################# # receive keys from keyserver #############################