summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/jobs/UnpublishExpiredJob.java
blob: 1813bd639629581cd532b23532129079d4cb3cfc (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// --- 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.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cms.jobs;


import java.util.*;
import java.io.*;
import java.text.DateFormat;
import java.security.*;
import java.security.cert.*;
import netscape.security.x509.*;
import com.netscape.certsrv.notification.*;
import com.netscape.certsrv.jobs.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.apps.*;
import com.netscape.certsrv.logging.*;
import com.netscape.certsrv.ca.*;
import com.netscape.certsrv.request.*;
import com.netscape.certsrv.dbs.certdb.*;
import com.netscape.certsrv.ldap.*;
import com.netscape.certsrv.publish.*;


/**
 * a job for the Jobs Scheduler.  This job checks in the internal ldap
 * db for certs that have expired and remove them from the ldap
 * publishing directory.
 * <p>
 * the $TOKENS that are available for the this jobs's summary outer form are:<br>
 * <UL>
 * $Status
 * $InstanceID
 * $SummaryItemList
 * $SummaryTotalNum
 * $SummaryTotalSuccess
 * $SummaryTotalfailure
 * $ExecutionTime
 * </UL>
 * and for the inner list items:
 * <UL>
 * $SerialNumber
 * $IssuerDN
 * $SubjectDN
 * $NotAfter
 * $NotBefore
 * $RequestorEmail
 * $CertType
 * </UL>
 *
 * @version $Revision$, $Date$
 */
public class UnpublishExpiredJob extends AJobBase
    implements IJob, Runnable, IExtendedPluginInfo {
	
    ICertificateAuthority mCa = null;
    IRequestQueue mReqQ = null;
    ICertificateRepository mRepository = null;
    IPublisherProcessor mPublisherProcessor = null;
    private boolean mSummary = false;

    /* Holds configuration parameters accepted by this implementation.
     * This list is passed to the configuration console so configuration
     * for instances of this implementation can be configured through the
     * console.
     */
    protected static String[] mConfigParams =
        new String[] { 
            "enabled",
            "cron",
            "summary.enabled",
            "summary.emailSubject",
            "summary.emailTemplate",
            "summary.itemTemplate",
            "summary.senderEmail",
            "summary.recipientEmail"
        };

    /* Vector of extendedPluginInfo strings */
    protected static Vector mExtendedPluginInfo = null;
    static {
        mExtendedPluginInfo = new Vector();
    };

    public String[] getExtendedPluginInfo(Locale locale) {
        String s[] = {
                IExtendedPluginInfo.HELP_TEXT +
                "; A job that checks for expired certificates in the " +
                "database, and removes them from the publishing " +
                "directory",
                "cron;string;Format: minute hour dayOfMonth month " +
                "dayOfWeek. Use '*' for 'every'. For dayOfWeek, 0 is Sunday",
                "summary.senderEmail;string;Specify the address to be used " +
                "as the email's 'sender'. Bounces go to this address.",
                "summary.recipientEmail;string;Who should receive summaries",
                "enabled;boolean;Enable this plugin",
                "summary.enabled;boolean;Enable the summary. You must enabled " +
                "this for the job to work.",
                "summary.emailSubject;string;Subject of summary email",
                "summary.emailTemplate;string;Fully qualified pathname of " +
                "template file of email to be sent",
                "summary.itemTemplate;string;Fully qualified pathname of " +
                "file containing template for each item",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-jobrules-unpublishexpiredjobs",
            };

        return s;
    }

    /**
     * initialize from the configuration file
     */
    public void init(ISubsystem owner, String id, String implName, IConfigStore config) throws
            EBaseException {
        mConfig = config;
        mId = id;
        mImplName = implName;

        mCa = (ICertificateAuthority)
                CMS.getSubsystem("ca");
        if (mCa == null) {
            return;
        }

        mReqQ = mCa.getRequestQueue();
        mRepository = (ICertificateRepository) mCa.getCertificateRepository();
        mPublisherProcessor = mCa.getPublisherProcessor();
		
        // read from the configuration file
        mCron = mConfig.getString(IJobCron.PROP_CRON);
        if (mCron == null) {
            return;
        }
		
        // parse cron string into a JobCron class
        IJobsScheduler scheduler = (IJobsScheduler) owner;

        mJobCron = scheduler.createJobCron(mCron);

        // initialize the summary related config info
        IConfigStore sc = mConfig.getSubStore(PROP_SUMMARY);

        if (sc.getBoolean(PROP_ENABLED, false)) {
            mSummary = true;
            mSummaryMailSubject = sc.getString(PROP_EMAIL_SUBJECT);
            mMailForm = sc.getString(PROP_EMAIL_TEMPLATE);
            mItemForm = sc.getString(PROP_ITEM_TEMPLATE);
            mSummarySenderEmail = sc.getString(PROP_SENDER_EMAIL);
            mSummaryReceiverEmail = sc.getString(PROP_RECEIVER_EMAIL);
        } else {
            mSummary = false;
        }
    }

    /**
     * look in the internal db for certificateRecords that are
     * expired.
     * remove them from ldap publishing directory
     * if remove successfully, mark <i>false</i> on the
     *		 <b>InLdapPublishDir</b> flag,
     *    else, if remove unsuccessfully, log it
     */
    public void run() {
        //		System.out.println("in ExpiredUnpublishJob "+
        //						   getId()+ " : run()");
        // get time now..."now" is before the loop
        Date date = CMS.getCurrentDate();
        long now = date.getTime();
        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        String nowString = dateFormat.format(date);

        // form filter
        String filter = "(&(x509Cert.notAfter<=" + now +
            ")(!(x509Cert.notAfter=" + now + "))" +
            "(" + "certMetainfo=" + ICertRecord.META_LDAPPUBLISH +
            ":true))";
        // a test for without CertRecord.META_LDAPPUBLISH
        //String filter = "(x509Cert.notAfter<="+ now +")";

        Enumeration expired = null;

        try {
            expired = mRepository.findCertRecs(filter);
            // bug 399150
            /*
             CertRecordList list = null;
             list = mRepository.findCertRecordsInList(filter,  null, "serialno", 5);
             int size = list.getSize();
             expired = list.getCertRecords(0, size - 1);
             */
        } catch (EBaseException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("OPERATION_ERROR", e.toString()));
        }

        int count = 0; // how many have been unpublished successfully
        int negCount = 0; // how many have NOT been unpublished successfully
        String contentForm = null;
        String itemForm = null;
        String itemListContent = null;

        if (mSummary == true) {
            contentForm = getTemplateContent(mMailForm);
            itemForm = getTemplateContent(mItemForm);
        }

        // unpublish them and unpublish() will set inLdapPublishDir flag
        while (expired != null && expired.hasMoreElements()) {
            ICertRecord rec = (ICertRecord) expired.nextElement();

            if (rec == null) break;
            X509CertImpl cert = rec.getCertificate();

            if (mSummary == true)
                buildItemParams(cert);

                // get request id from cert record MetaInfo
            MetaInfo minfo = null;

            try {
                minfo = (MetaInfo) rec.get(ICertRecord.ATTR_META_INFO);
            } catch (EBaseException e) {
                negCount += 1;
                if (mSummary == true)
                    buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                        STATUS_FAILURE);
                log(ILogger.LL_FAILURE,					
                    CMS.getLogMessage("JOBS_META_INFO_ERROR",
                        cert.getSerialNumber().toString(16) +
                        e.toString()));
            }

            String ridString = null;

            try {
                if (minfo != null)
                    ridString = (String) minfo.get(ICertRecord.META_REQUEST_ID); 
            } catch (EBaseException e) {
                negCount += 1;
                if (mSummary == true)
                    buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                        STATUS_FAILURE);
                log(ILogger.LL_FAILURE,					
                    CMS.getLogMessage("JOBS_META_REQUEST_ERROR",
                        cert.getSerialNumber().toString(16) +
                        e.toString()));
            } catch (NullPointerException e) {
                // no requestId in MetaInfo...skip to next record
                negCount += 1;
                if (mSummary == true)
                    buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                        STATUS_FAILURE);
                log(ILogger.LL_FAILURE,					
                    CMS.getLogMessage("JOBS_META_REQUEST_ERROR",
                        cert.getSerialNumber().toString(16) +
                        e.toString()));
            }

            if (ridString != null) {
                RequestId rid = new RequestId(ridString);
				
                // get request from request id
                IRequest req = null;

                try {
                    req = mReqQ.findRequest(rid);
                    if (req != null) {
                        if (mSummary == true)
                            buildItemParams(req);
                    }
                } catch (EBaseException e) {
                    negCount += 1;
                    if (mSummary == true)
                        buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                            STATUS_FAILURE);
                    log(ILogger.LL_FAILURE,					
                        CMS.getLogMessage("JOBS_FIND_REQUEST_ERROR",
                            cert.getSerialNumber().toString(16) +
                            e.toString()));
                }
                try {
                    if ((mPublisherProcessor != null) &&
                        mPublisherProcessor.enabled()) { 
                        mPublisherProcessor.unpublishCert((X509Certificate) cert, req);
                        if (mSummary == true)
                            buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                                STATUS_SUCCESS);
                        count += 1;
                    } else {
                        negCount += 1;
                    }
                } catch (Exception e) {
                    negCount += 1;
                    if (mSummary == true)
                        buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                            STATUS_FAILURE);
                    log(ILogger.LL_FAILURE,					
                        CMS.getLogMessage("JOBS_UNPUBLISH_ERROR",
                            cert.getSerialNumber().toString(16) +
                            e.toString()));
                }
            } // ridString != null
            else {
                try {
                    if ((mPublisherProcessor != null) &&
                        mPublisherProcessor.enabled()) { 
                        mPublisherProcessor.unpublishCert((X509Certificate) cert, null);
                        if (mSummary == true)
                            buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                                STATUS_SUCCESS);
                        count += 1;
                    } else {
                        negCount += 1;
                    }
                } catch (Exception e) {
                    negCount += 1;
                    if (mSummary == true)
                        buildItemParams(IEmailFormProcessor.TOKEN_STATUS,
                            STATUS_FAILURE);
                    log(ILogger.LL_FAILURE,					
                        CMS.getLogMessage("JOBS_UNPUBLISH_ERROR",
                            cert.getSerialNumber().toString(16) +
                            e.toString()));
                }
            } // ridString == null

            // inLdapPublishDir flag should have been set by the
            // unpublish() method

            // if summary is enabled, form the item content
            if (mSummary) {
                IEmailFormProcessor emailItemFormProcessor =
                    CMS.getEmailFormProcessor();
                String c = emailItemFormProcessor.getEmailContent(itemForm,
                        mItemParams);

                // add item content to the item list
                if (itemListContent == null) {
                    itemListContent = c;
                } else {
                    itemListContent += c;
                }
            }
        }

        // time for summary
        if (mSummary == true) {
            buildContentParams(IEmailFormProcessor.TOKEN_ID,
                mId);
            buildContentParams(IEmailFormProcessor.TOKEN_SUMMARY_ITEM_LIST,
                itemListContent);
            buildContentParams(IEmailFormProcessor.TOKEN_SUMMARY_TOTAL_NUM,
                String.valueOf(count + negCount));
            buildContentParams(IEmailFormProcessor.TOKEN_SUMMARY_SUCCESS_NUM,
                String.valueOf(count));
            buildContentParams(IEmailFormProcessor.TOKEN_SUMMARY_FAILURE_NUM,
                String.valueOf(negCount));
            buildContentParams(IEmailFormProcessor.TOKEN_EXECUTION_TIME,
                nowString);

            IEmailFormProcessor emailFormProcessor = CMS.getEmailFormProcessor();
            String mailContent =
                emailFormProcessor.getEmailContent(contentForm,
                    mContentParams);

            mailSummary(mailContent);
        }
    }

    /**
     * Returns a list of configuration parameter names. 
     * The list is passed to the configuration console so instances of 
     * this implementation can be configured through the console.
     *
     * @return String array of configuration parameter names.
     */
    public String[] getConfigParams() {
        return (mConfigParams);
    }
}