~ubuntu-branches/ubuntu/precise/zentyal-ca/precise

« back to all changes in this revision

Viewing changes to src/EBox/CA/Certificates.pm

  • Committer: Package Import Robot
  • Author(s): Jorge Salamero Sanz
  • Date: 2012-03-13 19:07:40 UTC
  • Revision ID: package-import@ubuntu.com-20120313190740-8sw68w6j5v7tu6tw
Tags: 2.3.3
* Add transitional dummy package for ebox-ca
* Add Breaks header

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009-2012 eBox Technologies S.L.
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License, version 2, as
 
5
# published by the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 
 
16
# Class: EBox::CA::Certificates
 
17
#
 
18
#
 
19
 
 
20
package EBox::CA::Certificates;
 
21
 
 
22
use base qw(EBox::CA::Observer);
 
23
 
 
24
use strict;
 
25
use warnings;
 
26
 
 
27
use EBox::Gettext;
 
28
use EBox::Global;
 
29
 
 
30
use File::Temp qw(tempfile);
 
31
 
 
32
# Group: Public methods
 
33
 
 
34
# Constructor: new
 
35
#
 
36
#      Create the new CA Certificates model
 
37
#
 
38
# Returns:
 
39
#
 
40
#      <EBox::CA::Certificates> - the recently created model
 
41
#
 
42
sub new
 
43
{
 
44
    my $class = shift;
 
45
 
 
46
    my $self = {};
 
47
 
 
48
    bless($self, $class);
 
49
 
 
50
    return $self;
 
51
}
 
52
 
 
53
# Method: genCerts
 
54
#
 
55
#      Generates all the certificates requested by all the services
 
56
#
 
57
sub genCerts
 
58
{
 
59
    my ($self) = @_;
 
60
 
 
61
    my @srvscerts = @{$self->srvsCerts()};
 
62
    foreach my $srvcert (@srvscerts) {
 
63
        $self->_genCert($srvcert);
 
64
    }
 
65
}
 
66
 
 
67
# Method: certificateRevoked
 
68
#
 
69
# Overrides:
 
70
#
 
71
#      <EBox::CA::Observer::certificateRevoked>
 
72
#
 
73
sub certificateRevoked
 
74
{
 
75
    my ($self, $commonName, $isCACert) = @_;
 
76
 
 
77
    my $ca = EBox::Global->modInstance('ca');
 
78
    my $model = $ca->model('Certificates');
 
79
 
 
80
    return $model->certUsedByService($commonName);
 
81
}
 
82
 
 
83
# Method: certificateRenewed
 
84
#
 
85
# Overrides:
 
86
#
 
87
#      <EBox::CA::Observer::certificateRenewed>
 
88
#
 
89
sub certificateRenewed
 
90
{
 
91
    my ($self) = @_;
 
92
 
 
93
    $self->genCerts(); #FIXME only regen renewed certs
 
94
}
 
95
 
 
96
# Method: certificateExpired
 
97
#
 
98
# Overrides:
 
99
#
 
100
#      <EBox::CA::Observer::certificateExpired>
 
101
#
 
102
sub certificateExpired
 
103
{
 
104
    my ($self, $commonName, $isCACert) = @_;
 
105
 
 
106
    my $ca = EBox::Global->modInstance('ca');
 
107
    my $model = $ca->model('Certificates');
 
108
 
 
109
    my @srvscerts = @{$self->srvsCerts()};
 
110
    foreach my $srvcert (@srvscerts) {
 
111
        my $service = $srvcert->{'service'};
 
112
        my $cn = $model->cnByService($service);
 
113
        if ($cn eq $commonName) {
 
114
            $model->disableService($service);
 
115
        }
 
116
    }
 
117
}
 
118
 
 
119
# Method: freeCertificate
 
120
#
 
121
# Overrides:
 
122
#
 
123
#      <EBox::CA::Observer::freeCertificate>
 
124
#
 
125
sub freeCertificate
 
126
{
 
127
    my ($self, $commonName) = @_;
 
128
 
 
129
    my $ca = EBox::Global->modInstance('ca');
 
130
    my $model = $ca->model('Certificates');
 
131
 
 
132
    my @srvscerts = @{$self->srvsCerts()};
 
133
    foreach my $srvcert (@srvscerts) {
 
134
        my $service = $srvcert->{'service'};
 
135
        my $cn = $model->cnByService($service);
 
136
        if ($cn eq $commonName) {
 
137
            $model->disableService($service);
 
138
        }
 
139
    }
 
140
}
 
141
 
 
142
# Method: srvsCerts
 
143
#
 
144
#      All services which request a certificate as provided
 
145
#      by EBox::Module::Service::certificates() plus the
 
146
#      module they are from.
 
147
#
 
148
# Returns:
 
149
#
 
150
#       A ref to array with all the services information
 
151
#
 
152
sub srvsCerts
 
153
{
 
154
    my ($self) = @_;
 
155
 
 
156
    my @srvscerts;
 
157
    my @mods = @{$self->_modsService()};
 
158
    for my $mod (@mods) {
 
159
        my @modsrvs = @{EBox::Global->modInstance($mod)->certificates()};
 
160
        next unless @modsrvs;
 
161
        for my $srv (@modsrvs) {
 
162
            $srv->{'module'} = $mod;
 
163
            push(@srvscerts, $srv);
 
164
        }
 
165
    }
 
166
    return \@srvscerts;
 
167
}
 
168
 
 
169
 
 
170
# Group: Public methods
 
171
 
 
172
# Method: _genCert
 
173
#
 
174
#      Generates the certificate for a service
 
175
#
 
176
sub _genCert
 
177
{
 
178
    my ($self, $srvcert) = @_;
 
179
 
 
180
    my $ca = EBox::Global->modInstance('ca');
 
181
 
 
182
    my $model = $ca->model('Certificates');
 
183
 
 
184
    my $service = $srvcert->{'service'};
 
185
    return undef unless ($model->isEnabledService($service));
 
186
 
 
187
    my $cn = $model->cnByService($service);
 
188
    return undef unless (defined($cn));
 
189
 
 
190
    my $certMD = $ca->getCertificateMetadata(cn => $cn);
 
191
    if ((not defined($certMD)) or ($certMD->{state} ne 'V')) {
 
192
        # Check the expiration date
 
193
        my $caMD = $ca->getCACertificateMetadata();
 
194
        $ca->issueCertificate(
 
195
            commonName => $cn,
 
196
            endDate => $caMD->{expiryDate},
 
197
        );
 
198
    }
 
199
 
 
200
    my $cert = $ca->getCertificateMetadata(cn => $cn)->{'path'};
 
201
    my $privkey = $ca->getKeys($cn)->{'privateKey'};
 
202
 
 
203
    my ($tempfile_fh, $tempfile) = tempfile(EBox::Config::tmp . "/ca_certificates_XXXXXX") or
 
204
        throw EBox::Exceptions::Internal("Could not create temporal file.");
 
205
 
 
206
    open(CERT, $cert) or throw EBox::Exceptions::Internal('Could not open certificate file.');
 
207
    my @certdata = <CERT>;
 
208
    close(CERT);
 
209
    open(KEY, $privkey) or throw EBox::Exceptions::Internal('Could not open certificate file.');
 
210
    my @privkeydata = <KEY>;
 
211
    close(KEY);
 
212
 
 
213
    print $tempfile_fh @certdata;
 
214
    print $tempfile_fh @privkeydata;
 
215
    close($tempfile_fh);
 
216
 
 
217
    my @commands;
 
218
 
 
219
    my $user = $srvcert->{'user'};
 
220
    my $group = $srvcert->{'group'};
 
221
    push (@commands, "/bin/chown $user:$group $tempfile");
 
222
 
 
223
    my $mode = $srvcert->{'mode'};
 
224
    push (@commands, "/bin/chmod $mode $tempfile");
 
225
 
 
226
    my $path = $srvcert->{'path'};
 
227
    push (@commands, "mkdir -p `dirname $path`");
 
228
    push (@commands, "mv -f $tempfile $path");
 
229
 
 
230
    EBox::Sudo::root(@commands);
 
231
}
 
232
 
 
233
# Method: _modsService
 
234
#
 
235
#      All configured service modules (EBox::Module::Service)
 
236
#      which could be implmenting the certificates method.
 
237
#
 
238
# Returns:
 
239
#
 
240
#       A ref to array with all the Module::Service names
 
241
#
 
242
sub _modsService
 
243
{
 
244
    my ($self) = @_;
 
245
 
 
246
    my @names = @{EBox::Global->modInstancesOfType('EBox::Module::Service')};
 
247
 
 
248
    my @mods;
 
249
    foreach my $name (@names) {
 
250
       $name->configured() or next;
 
251
       push (@mods, $name->name());
 
252
    }
 
253
    return \@mods;
 
254
}
 
255
 
 
256
1;