summaryrefslogtreecommitdiffstats
path: root/RPM4/lib/RPM4/Sign.pm
blob: b1bc39946bdf1801df3467be3500e6e3c9617411 (plain)
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
##- the Free Software Foundation; either version 2, or (at your option)
##- any later version.
##-
##- This program is distributed in the hope that it will be useful,
##- but WITHOUT ANY WARRANTY; without even the implied warranty of
##- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##- GNU General Public License for more details.
##-
##- You should have received a copy of the GNU General Public License
##- along with this program; if not, write to the Free Software
##- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# $Id$

package RPM4::Sign;

use strict;
use warnings;

use RPM4;

sub new {
    my ($class, %options) = @_;

    my $Sign;
    $Sign = {
        _signature => undef,
        name => undef,
        path => undef,
        checkrpms => 1,
        passphrase => undef,

        password_file => undef,

        log => sub {
            my ($m, @v) = @_;
            printf STDERR "$m\n", @v;
        },

    };

    foreach (keys %$Sign) {
        defined($options{$_}) and $Sign->{$_} = $options{$_};
    }
    
    bless($Sign, $class);
    $Sign->getpubkey();
    $Sign->getpasswdfile();
    $Sign;
}

sub getpasswdfile {
    my ($self) = @_;
    $self->{password_file} or return 1;
    open(my $hpass, "<", $self->{password_file}) or return 0;
    $self->{passphrase} = <$hpass>;
    chomp($self->{passphrase});
    close($hpass);
    1;
}

sub adjustmacro {
    my ($self) = @_;

    defined($self->{_signature}) and RPM4::add_macro("_signature $self->{_signature}");

    foreach my $macro (qw(_gpg_name _pgp_name)) {
        RPM4::add_macro("$macro $self->{name}") if (defined($self->{name}));
    }
    
    foreach my $macro (qw(_gpg_path _pgp_path)) {
        RPM4::add_macro("$macro $self->{path}") if (defined($self->{path}));
    }
}

sub restoremacro {
    my ($self) = @_;

    if (defined($self->{_signature})) { RPM4::del_macro('_signature'); }
    
    if (defined($self->{name})) {
        RPM4::del_macro('_gpg_name');
        RPM4::del_macro('_pgp_name');
    }

    if (defined($self->{path})) {
        RPM4::del_macro('_gpg_path');
        RPM4::del_macro('_pgp_path');
    }
}

sub getpubkey {
    my ($self) = @_;
    $self->adjustmacro();
    my $gpgcmd;
    if (RPM4::expand("%_signature") eq "gpg") {
        $gpgcmd = '%__gpg --homedir %_gpg_path --list-public-keys --with-colons \'%_gpg_name\'';
    }
    open(my $hgpg, RPM4::expand($gpgcmd) .'|') or return undef;
    while (my $l = <$hgpg>) {
        chomp($l);
        my @v = split(':', $l);
        if ($v[0] eq 'pub') {
           $self->{keyid} = $v[4];
           last;
        }
    }
    close($hgpg);
    $self->restoremacro();
}

sub rpmsign {
    my ($self, $rpm, $header) = @_;
    my $need = 1;

    $header or return -1;
    
    if (RPM4::expand("_signature") || "" eq "gpg") {
        my $sigid = $header->queryformat("%{SIGGPG:pgpsig}");
        ($sigid) = $sigid =~ m/Key ID (\S+)/;
        if ($sigid && lc($sigid) eq lc($self->{keyid} || "")) { $need = 0 }
    }
    
    if ($need > 0) {
        $self->adjustmacro();
        rpmresign($self->{passphrase}, $rpm) and $need = -1;
        $self->restoremacro();
    }

    $need;
}

sub rpmssign {
    my ($self, @rpms) = @_;

    RPM4::parserpms(
        rpms => [ @rpms ],
        checkrpms => $self->{checkrpms},
        callback => sub {
            my (%arg) = @_;
            defined($arg{header}) or do {
                $self->{log}->("bad rpm %s", $arg{rpm});
                return;
            };
            my $res = $self->rpmsign($arg{rpm}, $arg{header});
            if ($res > 0) { $self->{log}->("%s has been resigned", $arg{rpm}); 
            } elsif ($res < 0) { $self->{log}->("Can't resign %s", $arg{rpm}); }
        },
    );
}

1;

__END__

=head1 NAME

RPM4::Sign

=head1 SYNOPSIS

A container to massively resign packages

=head1 DESCRIPTION

This object retains gpg options and provides functions to easilly sign or
resign packages. It does not resign packages having already the proper
signature.

=head1 METHODS

=head2 new(%options)

Create a new RPM4::Sign object.

Options are:

=over 4

=item name

The gpg key identity to use

=item path

the gpg homedir where keys are located

=item password_file

Use passphrase contains in this files

=item passphrase

Use this passphrase to unlock the key

=item checkrpms

Set to 0 remove the signature checking on packages

=back

=head2 rpmssign(@filelist)

Sign or resign the packages passed are arguments

=head1 SEE ALSO

L<RPM4>
L<RPM4::Header>

=cut