summaryrefslogtreecommitdiffstats
path: root/pki/base/ra/lib/perl/PKI/Request/Plugin/EmailNotification.pm
blob: 95274bfa7eba137aa14093fba0497f027151bf38 (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
#!/usr/bin/perl
#
# --- BEGIN COPYRIGHT BLOCK ---
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2007 Red Hat, Inc.
# All rights reserved.
# --- END COPYRIGHT BLOCK ---
#
#
#
#

#######################################
# This plugins mails a notification
# to an email specified in the request.
#######################################
package PKI::Request::Plugin::EmailNotification;

use DBI;
use PKI::Base::TimeTool;

#######################################
# Instantiate this plugin
#######################################
sub new {
  my $self = {};
  bless ($self);
  return $self;
}

sub substitute {
  my ($self, $cfg, $queue, $prefix, $req, $line) = @_;

  my $mail_to = $cfg->get($prefix . ".mailTo");

  # if mail_to starts with $, retrieve value from request
  if ($mail_to =~ /^\$/) { 
    $mail_to =~ s/\$//g;
    $mail_to = $req->{$mail_to};
  }
  my $machineName = $cfg->get("service.machineName");
  my $securePort = $cfg->get("service.securePort");
  my $unsecurePort = $cfg->get("service.unsecurePort");
  my $nonClientAuthSecurePort = $cfg->get("service.non_clientauth_securePort");
  my $subject_dn = $req->{'subject_dn'};

  $line =~ s/\$mail_to/$mail_to/g;
  $line =~ s/\$request_id/$req->{'rowid'}/g;
  $line =~ s/\$machineName/$machineName/g;
  $line =~ s/\$securePort/$securePort/g;
  $line =~ s/\$unsecurePort/$unsecurePort/g;
  $line =~ s/\$subject_dn/$subject_dn/g;
  $line =~ s/\$nonClientAuthSecurePort/$nonClientAuthSecurePort/g;
  return $line;
}

#######################################
# Processes plugin
#######################################
sub process {
  my ($self, $cfg, $queue, $prefix, $req) = @_;
  my $queue = PKI::Request::Queue->new();
  $queue->open($cfg);
  my $ref = $queue->read_request($req->{rowid});

  my $req_err = $ref->{errorString};
  if ($req_err ne "0") {
    return;
  }

  my $mail_to = $cfg->get($prefix . ".mailTo");
  if ($mail_to eq "") {
    return;
  }

  my $template_dir = $cfg->get($prefix . ".templateDir");
  my $template_file = $cfg->get($prefix . ".templateFile");

  open(SENDMAIL, "|/usr/sbin/sendmail -t");
  open(F,"$template_dir/$template_file");
  while (<F>) {
    print SENDMAIL $self->substitute($cfg, $queue, $prefix, $ref, $_);
  }
  close(F);
  close(SENDMAIL);
}

1;