| 1 | |||
| 2 | package Slackware::Slackget::GPG; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use Slackware::Slackget::GPG::Signature ; | ||
| 7 | use constant { | ||
| 8 | SIG_GOOD => 'GOOD', | ||
| 9 | SIG_BAD => 'BAD', | ||
| 10 | SIG_UNKNOW => 'UNKNOW', | ||
| 11 | }; | ||
| 12 | |||
| 13 | =head1 NOM | ||
| 14 | |||
| 15 | Slackware::Slackget::GPG - A simple wrapper class to the gpg binary | ||
| 16 | |||
| 17 | =head1 VERSION | ||
| 18 | |||
| 19 | Version 0.4 | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | our $VERSION = '0.4'; | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | A simple class to verify files signatures with gpg. | ||
| 28 | |||
| 29 | use Slackware::Slackget::GPG; | ||
| 30 | |||
| 31 | my $slackware_slackget_gpg_object = Slackware::Slackget::GPG->new(); | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | =head1 CONSTRUCTOR | ||
| 36 | |||
| 37 | new() : The constructor take the followings arguments : | ||
| 38 | |||
| 39 | - gpg_binary : where we can find a valid gpg binary (default: /usr/bin/gpg) | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub new | ||
| 46 | { | ||
| 47 | my ($class,%args) = @_ ; | ||
| 48 | my $self={}; | ||
| 49 | $self->{DATA}->{gpg_binary} = '/usr/bin/gpg' ; | ||
| 50 | $self->{DATA}->{gpg_binary} = $args{gpg_binary} if(exists($args{gpg_binary}) && defined($args{gpg_binary})); | ||
| 51 | bless($self,$class); | ||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | =head1 METHODS | ||
| 56 | |||
| 57 | =head2 verify_file | ||
| 58 | |||
| 59 | take a file and a signature as parameter and verify the signature of the file. Return a Slackware::Slackget::GPG::Signature object. If the status is UNKNOW, the warnings() accessor may return some interesting data. | ||
| 60 | |||
| 61 | my $sig = $gpg->verify("/usr/local/slack-get-1.0.0-alpha1/update/signature-cache/gcc-g++-3.3.4-i486-1.tgz","/usr/local/slack-get-1.0.0-alpha1/update/package-cache/gcc-g++-3.3.4-i486-1.tgz.asc"); | ||
| 62 | die "Signature doesn't match.\n" if(!$sig->is_good) ; | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub verify_file | ||
| 67 | { | ||
| 68 | my ($self,$file,$sig1) = @_; | ||
| 69 | my @out = `2>&1 LC_ALL=C $self->{DATA}->{gpg_binary} --verify $sig1 $file`; | ||
| 70 | |||
| 71 | # gpg: CRC error; 040b69 - 24a901 | ||
| 72 | # gpg: packet(3) with unknown version 3 | ||
| 73 | # | ||
| 74 | # gpg: Signature made Mon 14 Jun 2004 09:23:24 AM CEST using DSA key ID 40102233 | ||
| 75 | # gpg: Good signature from "Slackware Linux Project <security@slackware.com>" | ||
| 76 | # gpg: WARNING: This key is not certified with a trusted signature! | ||
| 77 | # gpg: There is no indication that the signature belongs to the owner. | ||
| 78 | |||
| 79 | # gpg: Signature made Mon 16 Feb 2004 07:53:35 AM CET using DSA key ID 40102233 | ||
| 80 | # gpg: BAD signature from "Slackware Linux Project <security@slackware.com>" | ||
| 81 | |||
| 82 | my $sig = new Slackware::Slackget::GPG::Signature; | ||
| 83 | foreach (@out) | ||
| 84 | { | ||
| 85 | # print "[DEBUG::GPG] $_\n"; | ||
| 86 | chomp; | ||
| 87 | if($_ =~ /gpg: Signature made (.*) using DSA key ID (.*)/) | ||
| 88 | { | ||
| 89 | $sig->date($1); | ||
| 90 | $sig->key_id($2); | ||
| 91 | } | ||
| 92 | if($_ =~ /gpg: CRC error;.*/) | ||
| 93 | { | ||
| 94 | $sig->status('BAD'); | ||
| 95 | } | ||
| 96 | if($_ =~ /gpg: Good signature from "([^"]*)"/) | ||
| 97 | { | ||
| 98 | $sig->status('GOOD'); | ||
| 99 | $sig->emitter($1); | ||
| 100 | } | ||
| 101 | if($_ =~ /gpg: BAD signature/) | ||
| 102 | { | ||
| 103 | $sig->status('BAD'); | ||
| 104 | } | ||
| 105 | if($_ =~ /gpg: BAD signature from "([^"]*)"/) | ||
| 106 | { | ||
| 107 | $sig->status('BAD'); | ||
| 108 | $sig->emitter($1); | ||
| 109 | } | ||
| 110 | if($_=~ /gpg: WARNING: (.*)/) | ||
| 111 | { | ||
| 112 | $sig->warnings([@{$sig->warnings()},$1]); | ||
| 113 | } | ||
| 114 | if($_=~ /Primary key fingerprint: ([0-9A-F\s]*)/) | ||
| 115 | { | ||
| 116 | $sig->fingerprint($1); | ||
| 117 | } | ||
| 118 | if($_=~ /gpg: verify signatures failed: (.*)/) | ||
| 119 | { | ||
| 120 | $sig->status('UNKNOW'); | ||
| 121 | $sig->warnings([@{$sig->warnings()},$1]); | ||
| 122 | } | ||
| 123 | if($_=~ /gpg: can't hash datafile: (.*)/) | ||
| 124 | { | ||
| 125 | $sig->status('UNKNOW'); | ||
| 126 | $sig->warnings([@{$sig->warnings()},"can't hash datafile",$1]); | ||
| 127 | } | ||
| 128 | |||
| 129 | } | ||
| 130 | $sig->status('UNKNOW') unless($sig->status); | ||
| 131 | return $sig; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head2 import_key | ||
| 135 | |||
| 136 | Import a key file passed in parameter. | ||
| 137 | |||
| 138 | $gpg->import_key('update/GPG-KEY') or die "unable to import official Slackware GnuPG key.\n"; | ||
| 139 | |||
| 140 | Return a Slackware::Slackget::Signature object. | ||
| 141 | |||
| 142 | The returned object is set with the status (which represent in this case, the status of the import). | ||
| 143 | |||
| 144 | On successfull import, it also set teh key_id and the emitter. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub import_key | ||
| 149 | { | ||
| 150 | my ($self,$key) = @_ ; | ||
| 151 | my @out = `2>&1 LC_ALL=C $self->{DATA}->{gpg_binary} --import $key`; | ||
| 152 | my $sig = new Slackware::Slackget::GPG::Signature; | ||
| 153 | $sig->status('BAD'); | ||
| 154 | foreach (@out){ | ||
| 155 | # key 40102233: public key "Slackware Linux Project <security@slackware.com>" imported | ||
| 156 | if(/gpg: key ([^:]+): public key "([^"]+)" imported/){ | ||
| 157 | $sig->status('GOOD'); | ||
| 158 | $sig->key_id($1); | ||
| 159 | $sig->emitter($2); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | return $sig; | ||
| 164 | } | ||
| 165 | |||
| 166 | =head2 in_keyring | ||
| 167 | |||
| 168 | Return the number of keys in the keyring that match the given string. | ||
| 169 | |||
| 170 | $gpg->in_keyring('Slackware Linux Project') or die "The GPG signature of the Slackware Linux project cannot be found in your keyring.\n"; | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub in_keyring { | ||
| 175 | my ($self, $string) = @_ ; | ||
| 176 | my @r = (); | ||
| 177 | foreach my $key ( $self->list_keys ){ | ||
| 178 | foreach (@{$key->{uid}}){ | ||
| 179 | push @r, $key if(/$string/); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | return scalar(@r); | ||
| 183 | } | ||
| 184 | |||
| 185 | =head2 list_keys | ||
| 186 | |||
| 187 | Return the list of keys in the current user's keyring. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | # Put the next line in the pod when the *info methods are coded. | ||
| 192 | # To retrieve all information for a given key, use the key_info() method. | ||
| 193 | |||
| 194 | sub list_keys { | ||
| 195 | my $self = shift; | ||
| 196 | my @list = (); | ||
| 197 | my @out = `2>&1 LC_ALL=C $self->{DATA}->{gpg_binary} --list-keys`; | ||
| 198 | #pub 1024D/61BD09B3 2005-07-02 | ||
| 199 | foreach (@out){ | ||
| 200 | chomp; | ||
| 201 | if(/^pub\s+[^\/]+\/([^\s]+)\s+.*$/){ | ||
| 202 | push @list, {key => $1, uid => []}; | ||
| 203 | } | ||
| 204 | elsif(/^uid\s+(.+)$/){ | ||
| 205 | push @{ $list[$#list]->{uid} },$1; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | return @list; | ||
| 209 | } | ||
| 210 | |||
| 211 | =head2 list_sigs | ||
| 212 | |||
| 213 | Return the list of signatures in the current user's keyring. | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | # Put the next line in the pod when the *info methods are coded. | ||
| 218 | # To retrieve all information for a given key, use the sig_info() method. | ||
| 219 | |||
| 220 | sub list_sigs { | ||
| 221 | my $self = shift; | ||
| 222 | my @list = (); | ||
| 223 | my @out = `2>&1 LC_ALL=C $self->{DATA}->{gpg_binary} --list-sigs`; | ||
| 224 | foreach (@out){ | ||
| 225 | chomp; | ||
| 226 | if(/^pub\s+[^\/]+\/([^\s]+)\s+.*$/){ | ||
| 227 | push @list, $1; | ||
| 228 | } | ||
| 229 | elsif(/^uid\s+(.+)$/){ | ||
| 230 | push @{ $list[$#list]->{uid} },$1; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | return @list; | ||
| 234 | } | ||
| 235 | |||
| 236 | # =head2 sig_info | ||
| 237 | # | ||
| 238 | # Retrieve info on one of the user's keyring signature. | ||
| 239 | # | ||
| 240 | # This method takes a uid (or a significant part of it) as parameter. | ||
| 241 | # | ||
| 242 | # If the uid is not unique enough to select one signature, this method return undef. | ||
| 243 | # | ||
| 244 | # =cut | ||
| 245 | # # TODO: it sucks => list_* should return a list of key id and *_info return the rest of info !! | ||
| 246 | # sub sig_info { | ||
| 247 | # my ($self,$uid) = @_; | ||
| 248 | # my @out = `2>&1 LC_ALL=C $self->{DATA}->{gpg_binary} --list-sigs`; | ||
| 249 | # my $data = {}; | ||
| 250 | # foreach (@out){ | ||
| 251 | # chomp; | ||
| 252 | # if(/^uid\s+$uid/){ | ||
| 253 | # $data->{uid} = $uid; | ||
| 254 | # }elsif(defined($data->{uid}) && $data->{uid} eq $uid ){ # you are never to cautious with test... | ||
| 255 | # if(//) | ||
| 256 | # } | ||
| 257 | # } | ||
| 258 | # } | ||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | =head1 ACCESSORS | ||
| 263 | |||
| 264 | =head2 gpg_binary | ||
| 265 | |||
| 266 | Get/set the path to the gpg binary. | ||
| 267 | |||
| 268 | die "Cannot find gpg : $!\n" unless( -e $gpg->gpg_binary()); | ||
| 269 | |||
| 270 | =cut | ||
| 271 | |||
| 272 | sub gpg_binary | ||
| 273 | { | ||
| 274 | return $_[1] ? $_[0]->{DATA}->{gpg_binary}=$_[1] : $_[0]->{DATA}->{gpg_binary}; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | =head1 AUTHOR | ||
| 280 | |||
| 281 | DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >> | ||
| 282 | |||
| 283 | =head1 BUGS | ||
| 284 | |||
| 285 | Please report any bugs or feature requests to | ||
| 286 | C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at | ||
| 287 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>. | ||
| 288 | I will be notified, and then you'll automatically be notified of progress on | ||
| 289 | your bug as I make changes. | ||
| 290 | |||
| 291 | =head1 SUPPORT | ||
| 292 | |||
| 293 | You can find documentation for this module with the perldoc command. | ||
| 294 | |||
| 295 | perldoc Slackware::Slackget | ||
| 296 | |||
| 297 | |||
| 298 | You can also look for information at: | ||
| 299 | |||
| 300 | =over 4 | ||
| 301 | |||
| 302 | =item * Infinity Perl website | ||
| 303 | |||
| 304 | L<http://www.infinityperl.org/category/slack-get> | ||
| 305 | |||
| 306 | =item * slack-get specific website | ||
| 307 | |||
| 308 | L<http://slackget.infinityperl.org> | ||
| 309 | |||
| 310 | =item * RT: CPAN's request tracker | ||
| 311 | |||
| 312 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget> | ||
| 313 | |||
| 314 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 315 | |||
| 316 | L<http://annocpan.org/dist/Slackware-Slackget> | ||
| 317 | |||
| 318 | =item * CPAN Ratings | ||
| 319 | |||
| 320 | L<http://cpanratings.perl.org/d/Slackware-Slackget> | ||
| 321 | |||
| 322 | =item * Search CPAN | ||
| 323 | |||
| 324 | L<http://search.cpan.org/dist/Slackware-Slackget> | ||
| 325 | |||
| 326 | =back | ||
| 327 | |||
| 328 | =head1 ACKNOWLEDGEMENTS | ||
| 329 | |||
| 330 | Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation. | ||
| 331 | |||
| 332 | =head1 SEE ALSO | ||
| 333 | |||
| 334 | =head1 COPYRIGHT & LICENSE | ||
| 335 | |||
| 336 | Copyright 2005 DUPUIS Arnaud, All Rights Reserved. | ||
| 337 | |||
| 338 | This program is free software; you can redistribute it and/or modify it | ||
| 339 | under the same terms as Perl itself. | ||
| 340 | |||
| 341 | =cut | ||
| 342 | |||
| 343 | 1; # Fin de Slackware::Slackget::GPG | ||
| 344 |