summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/ldap/LdapPublishModule.java
blob: 97297de48f554b83f6a89d80e36c869f4b952aed (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// --- 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.cmscore.ldap;

import java.io.IOException;
import java.math.BigInteger;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.Hashtable;

import netscape.ldap.LDAPConnection;
import netscape.security.x509.X500Name;
import netscape.security.x509.X509CRLImpl;
import netscape.security.x509.X509CertImpl;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.authority.ICertAuthority;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.ISubsystem;
import com.netscape.certsrv.base.MetaInfo;
import com.netscape.certsrv.ca.ICertificateAuthority;
import com.netscape.certsrv.dbs.Modification;
import com.netscape.certsrv.dbs.ModificationSet;
import com.netscape.certsrv.dbs.certdb.ICertRecord;
import com.netscape.certsrv.dbs.certdb.ICertificateRepository;
import com.netscape.certsrv.ldap.ELdapException;
import com.netscape.certsrv.ldap.ILdapConnFactory;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.publish.ILdapCrlMapper;
import com.netscape.certsrv.publish.ILdapMapper;
import com.netscape.certsrv.publish.ILdapPlugin;
import com.netscape.certsrv.publish.ILdapPublishModule;
import com.netscape.certsrv.publish.ILdapPublisher;
import com.netscape.certsrv.publish.IPublisherProcessor;
import com.netscape.certsrv.request.IRequest;
import com.netscape.certsrv.request.IRequestListener;
import com.netscape.cmscore.dbs.CertRecord;
import com.netscape.cmscore.ldapconn.LdapBoundConnFactory;
import com.netscape.cmscore.util.Debug;

public class LdapPublishModule implements ILdapPublishModule {
    protected IConfigStore mConfig = null;
    protected LdapBoundConnFactory mLdapConnFactory = null;
    protected ILogger mLogger = CMS.getLogger();
    private boolean mInited = false;
    protected ICertAuthority mAuthority = null;

    /**
     * hashtable of cert types to cert mappers and publishers.
     * cert types are client, server, ca, subca, ra, crl, etc.
     * XXX the cert types need to be consistently used.
     * for each, the mapper may be null, in which case the full subject
     * name is used to map the cert.
     * for crl, if the mapper is null the ca mapper is used. if that
     * is null, the full issuer name is used.
     * XXX if we support crl issuing points the issuing point should be used
     * to publish the crl.
     * When publishers are null, the certs are not published.
     */
    protected Hashtable mMappers = new Hashtable();

    /**
     * handlers for request types (events)
     * values implement IRequestListener
     */
    protected Hashtable mEventHandlers = new Hashtable();

    /**
     * instantiate connection factory.
     */
    public static final String ATTR_LDAPPUBLISH_STATUS = "LdapPublishStatus";
    public static final String PROP_LDAP = "ldap";
    public static final String PROP_MAPPER = "mapper";
    public static final String PROP_PUBLISHER = "publisher";
    public static final String PROP_CLASS = "class";
    public static final String PROP_TYPE = "type";
    public static final String PROP_TYPE_CA = "ca";
    public static final String PROP_TYPE_CLIENT = "client";
    public static final String PROP_TYPE_CRL = "crl";

    public LdapPublishModule() {
    }

    public void init(ISubsystem sub, IConfigStore config) throws EBaseException {
    }

    public void set(String name, String val) {
    }

    public LdapPublishModule(LdapBoundConnFactory factory) {
        mLdapConnFactory = factory;
        mInited = true;
    }

    protected IPublisherProcessor mPubProcessor;

    public void init(ICertAuthority authority, IPublisherProcessor p,
            IConfigStore config)
            throws EBaseException {
        if (mInited)
            return;

        mAuthority = authority;
        mPubProcessor = p;
        mConfig = config;
        mLdapConnFactory = new LdapBoundConnFactory();
        mLdapConnFactory.init(mConfig.getSubStore("ldap"));

        // initMappers(config);
        initHandlers();

        mAuthority.registerRequestListener(this);
    }

    public void init(ICertAuthority authority, IConfigStore config)
            throws EBaseException {
        if (mInited)
            return;

        mAuthority = authority;
        mConfig = config;
        mLdapConnFactory = new LdapBoundConnFactory();
        mLdapConnFactory.init(mConfig.getSubStore("ldap"));

        initMappers(config);
        initHandlers();

        mAuthority.registerRequestListener(this);
    }

    /**
     * Returns the internal ldap connection factory.
     * This can be useful to get a ldap connection to the
     * ldap publishing directory without having to get it again from the
     * config file. Note that this means sharing a ldap connection pool
     * with the ldap publishing module so be sure to return connections to pool.
     * Use ILdapConnFactory.getConn() to get a Ldap connection to the ldap
     * publishing directory.
     * Use ILdapConnFactory.returnConn() to return the connection.
     * 
     * @see com.netscape.certsrv.ldap.ILdapBoundConnFactory
     * @see com.netscape.certsrv.ldap.ILdapConnFactory
     */
    public ILdapConnFactory getLdapConnFactory() {
        return mLdapConnFactory;
    }

    /**
     * Returns the connection factory to the publishing directory.
     * Must return the connection once you return
     */

    protected LdapMappers getMappers(String certType) {
        LdapMappers mappers = null;

        if (certType == null) {
            mappers = (LdapMappers) mMappers.get(PROP_TYPE_CLIENT);
        } else {
            mappers = (LdapMappers) mMappers.get(certType);
        }
        return mappers;
    }

    protected void initMappers(IConfigStore config)
            throws EBaseException {
        IConfigStore types = mConfig.getSubStore(PROP_TYPE);

        if (types == null || types.size() <= 0) {
            // nothing configured.
            if (Debug.ON)
                System.out.println("No ldap publishing configurations.");
            return;
        }
        Enumeration<String> substores = types.getSubStoreNames();

        while (substores.hasMoreElements()) {
            String certType = substores.nextElement();
            IConfigStore current = types.getSubStore(certType);

            if (current == null || current.size() <= 0) {
                CMS.debug(
                        "No ldap publish configuration for " + certType + " found.");
                continue;
            }
            ILdapPlugin mapper = null, publisher = null;
            IConfigStore mapperConf = null, publisherConf = null;
            String mapperClassName = null, publisherClassName = null;

            try {
                mapperConf = current.getSubStore(PROP_MAPPER);
                mapperClassName = mapperConf.getString(PROP_CLASS, null);
                if (mapperClassName != null && mapperClassName.length() > 0) {
                    CMS.debug(
                            "mapper " + mapperClassName + " for " + certType);
                    mapper = (ILdapPlugin)
                            Class.forName(mapperClassName).newInstance();
                    mapper.init(mapperConf);
                }
                publisherConf = current.getSubStore(PROP_PUBLISHER);
                publisherClassName = publisherConf.getString(PROP_CLASS, null);
                if (publisherClassName != null &&
                        publisherClassName.length() > 0) {
                    CMS.debug(
                            "publisher " + publisherClassName + " for " + certType);
                    publisher = (ILdapPlugin)
                            Class.forName(publisherClassName).newInstance();
                    publisher.init(publisherConf);
                }
                mMappers.put(certType, new LdapMappers(mapper, publisher));
            } catch (ClassNotFoundException e) {
                String missingClass = mapperClassName +
                        ((publisherClassName == null) ? "" :
                                (" or " + publisherClassName));

                log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_FIND_CLASS", missingClass));
                throw new ELdapException(
                        CMS.getUserMessage("CMS_LDAP_CLASS_NOT_FOUND", missingClass));
            } catch (InstantiationException e) {
                String badInstance = mapperClassName +
                        ((publisherClassName == null) ? "" :
                                (" or " + publisherClassName));

                log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_INST_CLASS",
                                badInstance, certType));
                throw new ELdapException(
                        CMS.getUserMessage("CMS_LDAP_INSTANTIATING_CLASS_FAILED", badInstance));
            } catch (IllegalAccessException e) {
                String badInstance = mapperClassName +
                        ((publisherClassName == null) ? "" :
                                (" or " + publisherClassName));

                log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_INSUFFICIENT_CREDENTIALS", badInstance, certType));
                throw new ELdapException(
                        CMS.getUserMessage("CMS_LDAP_INSUFFICIENT_CREDENTIALS", certType));
            } catch (EBaseException e) {
                log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_INIT_ERROR", certType, e.toString()));
                throw e;
            }
        }
        mInited = true;
    }

    protected void initHandlers() {
        mEventHandlers.put(IRequest.ENROLLMENT_REQUEST,
                new HandleEnrollment(this));
        mEventHandlers.put(IRequest.RENEWAL_REQUEST,
                new HandleRenewal(this));
        mEventHandlers.put(IRequest.REVOCATION_REQUEST,
                new HandleRevocation(this));
        mEventHandlers.put(IRequest.UNREVOCATION_REQUEST,
                new HandleUnrevocation(this));
    }

    public void accept(IRequest r) {
        String type = r.getRequestType();
        boolean error = false;

        IRequestListener handler = (IRequestListener) mEventHandlers.get(type);

        if (handler == null) {
            CMS.debug(
                    "Nothing to publish for request type " + type);
            return;
        }
        handler.accept(r);
    }

    public void publish(String certType, X509Certificate cert)
            throws ELdapException {
        // get mapper and publisher for cert type.
        LdapMappers mappers = getMappers(certType);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug("publisher for " + certType + " is null");
            return;
        }
        publish((ILdapMapper) mappers.mapper,
                (ILdapPublisher) mappers.publisher, cert);

        // set the ldap published flag.
        setPublishedFlag(cert.getSerialNumber(), true);
    }

    public void unpublish(String certType, X509Certificate cert)
            throws ELdapException {
        // get mapper and publisher for cert type.
        LdapMappers mappers = getMappers(certType);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug("publisher for " + certType + " is null");
            return;
        }
        unpublish((ILdapMapper) mappers.mapper,
                (ILdapPublisher) mappers.publisher, cert);

        // set the ldap published flag.
        setPublishedFlag(cert.getSerialNumber(), false);
    }

    /**
     * set published flag - true when published, false when unpublished.
     * not exist means not published.
     */
    public void setPublishedFlag(BigInteger serialNo, boolean published) {
        if (!(mAuthority instanceof ICertificateAuthority))
            return;
        ICertificateAuthority ca = (ICertificateAuthority) mAuthority;

        try {
            ICertificateRepository certdb = (ICertificateRepository) ca.getCertificateRepository();
            ICertRecord certRec = (ICertRecord) certdb.readCertificateRecord(serialNo);
            MetaInfo metaInfo = certRec.getMetaInfo();

            if (metaInfo == null) {
                metaInfo = new MetaInfo();
            }
            metaInfo.set(
                    CertRecord.META_LDAPPUBLISH, String.valueOf(published));
            ModificationSet modSet = new ModificationSet();

            modSet.add(ICertRecord.ATTR_META_INFO,
                    Modification.MOD_REPLACE, metaInfo);
            certdb.modifyCertificateRecord(serialNo, modSet);
        } catch (EBaseException e) {
            // not fatal. just log warning.
            log(ILogger.LL_WARN,
                    "Cannot mark cert 0x" + serialNo.toString(16) + " published as " + published +
                            " in the ldap directory. Cert Record not found. Error: " +
                            e.getMessage());
        }
    }

    public LDAPConnection getConn() throws ELdapException {
        return mLdapConnFactory.getConn();
    }

    public void returnConn(LDAPConnection conn) throws ELdapException {
        mLdapConnFactory.returnConn(conn);
    }

    public void publish(ILdapMapper mapper, ILdapPublisher publisher,
            X509Certificate cert)
            throws ELdapException {
        LDAPConnection conn = null;

        try {
            String dirdn = null;
            String result = null;

            conn = mLdapConnFactory.getConn();
            if (mapper == null) { // use the cert's subject name exactly
                dirdn = cert.getSubjectDN().toString();
                CMS.debug(
                        "no mapper found. Using subject name exactly." +
                                cert.getSubjectDN());
            } else {
                result = mapper.map(conn, cert);
                dirdn = result;
                if (dirdn == null) {
                    log(ILogger.LL_FAILURE,
                            CMS.getLogMessage("CMSCORE_LDAP_PUBLISH_NOT_MATCH",
                                    cert.getSerialNumber().toString(16),
                                    cert.getSubjectDN().toString()));
                    throw new ELdapException(CMS.getUserMessage("CMS_LDAP_NO_MATCH",
                            cert.getSubjectDN().toString()));
                }
            }
            publisher.publish(conn, dirdn, cert);
        } finally {
            if (conn != null) {
                mLdapConnFactory.returnConn(conn);
            }
        }
    }

    public void unpublish(ILdapMapper mapper, ILdapPublisher publisher,
            X509Certificate cert)
            throws ELdapException {
        LDAPConnection conn = null;

        try {
            String dirdn = null;
            String result = null;

            conn = mLdapConnFactory.getConn();
            if (mapper == null) { // use the cert's subject name exactly
                dirdn = cert.getSubjectDN().toString();
            } else {
                result = mapper.map(conn, cert);
                dirdn = result;
                if (dirdn == null) {
                    log(ILogger.LL_FAILURE,
                            CMS.getLogMessage("CMSCORE_LDAP_PUBLISH_NOT_MATCH",
                                    cert.getSerialNumber().toString(16),
                                    cert.getSubjectDN().toString()));
                    throw new ELdapException(CMS.getUserMessage("CMS_LDAP_NO_MATCH",
                            cert.getSubjectDN().toString()));
                }
            }
            publisher.unpublish(conn, dirdn, cert);
        } finally {
            if (conn != null) {
                mLdapConnFactory.returnConn(conn);
            }
        }
    }

    /**
     * publishes a crl by mapping the issuer name in the crl to an entry
     * and publishing it there. entry must be a certificate authority.
     */
    public void publish(X509CRLImpl crl)
            throws ELdapException {
        ILdapCrlMapper mapper = null;
        ILdapPublisher publisher = null;

        LdapMappers mappers = getMappers(PROP_TYPE_CRL);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug("publisher for crl is null");
            return;
        }

        LDAPConnection conn = null;
        String dn = null;

        try {
            String result = null;

            conn = mLdapConnFactory.getConn();
            if (mappers.mapper == null) {
                dn = ((X500Name) crl.getIssuerDN()).toLdapDNString();
            } else {
                result = ((ILdapMapper) mappers.mapper).map(conn, crl);
                dn = result;
                if (dn == null) {
                    log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_LDAP_CRL_NOT_MATCH"));
                    throw new ELdapException(CMS.getUserMessage("CMS_LDAP_NO_MATCH",
                            crl.getIssuerDN().toString()));
                }
            }
            ((ILdapPublisher) mappers.publisher).publish(conn, dn, crl);
        } catch (ELdapException e) {
            //e.printStackTrace();
            CMS.debug(
                    "Error publishing CRL to " + dn + ": " + e);
            throw e;
        } catch (IOException e) {
            CMS.debug("Error publishing CRL to " + dn + ": " + e);
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_GET_ISSUER_FROM_CRL_FAILED", (String) ""));
        } finally {
            if (conn != null) {
                mLdapConnFactory.returnConn(conn);
            }
        }
    }

    /**
     * publishes a crl by mapping the issuer name in the crl to an entry
     * and publishing it there. entry must be a certificate authority.
     */
    public void publish(String dn, X509CRL crl)
            throws ELdapException {
        LdapMappers mappers = getMappers(PROP_TYPE_CRL);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug("publisher for crl is null");
            return;
        }

        LDAPConnection conn = null;

        try {
            conn = mLdapConnFactory.getConn();
            ((ILdapPublisher) mappers.publisher).publish(conn, dn, crl);
        } catch (ELdapException e) {
            CMS.debug(
                    "Error publishing CRL to " + dn + ": " + e.toString());
            throw e;
        } finally {
            if (conn != null) {
                mLdapConnFactory.returnConn(conn);
            }
        }
    }

    public void log(int level, String msg) {
        mLogger.log(ILogger.EV_SYSTEM, null, ILogger.S_LDAP, level,
                "LdapPublishModule: " + msg);
    }

}

class LdapMappers {
    public LdapMappers(ILdapPlugin aMapper, ILdapPlugin aPublisher) {
        mapper = aMapper;
        publisher = aPublisher;
    }

    public ILdapPlugin mapper = null;
    public ILdapPlugin publisher = null;
}

class HandleEnrollment implements IRequestListener {
    LdapPublishModule mModule = null;

    public HandleEnrollment(LdapPublishModule module) {
        mModule = module;
    }

    public void set(String name, String val) {
    }

    public void init(ISubsystem sub, IConfigStore config) throws EBaseException {
    }

    public void accept(IRequest r) {
        CMS.debug(
                "handling publishing for enrollment request id " +
                        r.getRequestId());

        // in case it's not meant for us
        if (r.getExtDataInInteger(IRequest.RESULT) == null)
            return;

        // check if request failed.
        if ((r.getExtDataInInteger(IRequest.RESULT)).equals(IRequest.RES_ERROR)) {
            CMS.debug("Request errored. " +
                    "Nothing to publish for enrollment request id " +
                    r.getRequestId());
            return;
        }
        CMS.debug("Checking publishing for request " +
                r.getRequestId());
        // check if issued certs is set.
        X509CertImpl[] certs = r.getExtDataInCertArray(IRequest.ISSUED_CERTS);

        if (certs == null || certs.length == 0 || certs[0] == null) {
            CMS.debug(
                    "No certs to publish for request id " + r.getRequestId());
            return;
        }

        // get mapper and publisher for client certs.
        LdapMappers mappers =
                mModule.getMappers(LdapPublishModule.PROP_TYPE_CLIENT);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug(
                    "In publishing: No publisher for type " +
                            LdapPublishModule.PROP_TYPE_CLIENT);
            return;
        }

        // publish
        Integer results[] = new Integer[certs.length];
        X509CertImpl cert;

        for (int i = 0; i < certs.length; i++) {
            try {
                if (certs[i] == null)
                    continue;
                mModule.publish((ILdapMapper) mappers.mapper,
                        (ILdapPublisher) mappers.publisher, certs[i]);
                results[i] = IRequest.RES_SUCCESS;
                CMS.debug(
                        "Published cert serial no 0x" + certs[i].getSerialNumber().toString(16));
                mModule.setPublishedFlag(certs[i].getSerialNumber(), true);
            } catch (ELdapException e) {
                mModule.log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_CERT_NOT_PUBLISH",
                                certs[i].getSerialNumber().toString(16), e.toString()));
                results[i] = IRequest.RES_ERROR;
            }
            r.setExtData("ldapPublishStatus", results);
        }
    }
}

class HandleRenewal implements IRequestListener {
    private LdapPublishModule mModule = null;

    public HandleRenewal(LdapPublishModule module) {
        mModule = module;
    }

    public void init(ISubsystem sub, IConfigStore config) throws EBaseException {
    }

    public void set(String name, String val) {
    }

    public void accept(IRequest r) {
        // Note we do not remove old certs from directory during renewal 
        X509CertImpl[] certs = r.getExtDataInCertArray(IRequest.ISSUED_CERTS);

        if (certs == null || certs.length == 0) {
            CMS.debug("no certs to publish for renewal " +
                    "request " + r.getRequestId());
            return;
        }
        Integer results[] = new Integer[certs.length];
        X509CertImpl cert = null;

        // get mapper and publisher for cert type.
        LdapMappers mappers =
                mModule.getMappers(LdapPublishModule.PROP_TYPE_CLIENT);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug(
                    "publisher for " + LdapPublishModule.PROP_TYPE_CLIENT + " is null");
            return;
        }

        boolean error = false;

        for (int i = 0; i < certs.length; i++) {
            cert = (X509CertImpl) certs[i];
            if (cert == null)
                continue; // there was an error issuing this cert.
            try {
                mModule.publish((ILdapMapper) mappers.mapper,
                        (ILdapPublisher) mappers.publisher, cert);
                results[i] = IRequest.RES_SUCCESS;
                mModule.log(ILogger.LL_INFO,
                        "Published cert serial no 0x" + cert.getSerialNumber().toString(16));
            } catch (ELdapException e) {
                error = true;
                mModule.log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_CERT_NOT_PUBLISH",
                                cert.getSerialNumber().toString(16), e.getMessage()));
                results[i] = IRequest.RES_ERROR;
            }
        }
        r.setExtData("ldapPublishStatus", results);
        r.setExtData("ldapPublishOverAllStatus",
                (error == true ? IRequest.RES_ERROR : IRequest.RES_SUCCESS));
    }
}

class HandleRevocation implements IRequestListener {
    private LdapPublishModule mModule = null;

    public HandleRevocation(LdapPublishModule module) {
        mModule = module;
    }

    public void init(ISubsystem sub, IConfigStore config) throws EBaseException {
    }

    public void set(String name, String val) {
    }

    public void accept(IRequest r) {
        CMS.debug(
                "Handle publishing for revoke request id " + r.getRequestId());

        // get fields in request.
        X509CertImpl[] revcerts = r.getExtDataInCertArray(IRequest.OLD_CERTS);

        if (revcerts == null || revcerts.length == 0 || revcerts[0] == null) {
            // no certs in revoke.
            CMS.debug(
                    "Nothing to unpublish for revocation " +
                            "request " + r.getRequestId());
            return;
        }

        // get mapper and publisher for cert type.
        LdapMappers mappers =
                mModule.getMappers(LdapPublishModule.PROP_TYPE_CLIENT);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug(
                    "publisher for " + LdapPublishModule.PROP_TYPE_CLIENT + " is null");
            return;
        }

        boolean error = false;
        Integer results[] = new Integer[revcerts.length];

        for (int i = 0; i < revcerts.length; i++) {
            X509CertImpl cert = revcerts[i];

            results[i] = IRequest.RES_ERROR;
            try {
                mModule.unpublish((ILdapMapper) mappers.mapper,
                        (ILdapPublisher) mappers.publisher, cert);
                results[i] = IRequest.RES_SUCCESS;
                CMS.debug(
                        "Unpublished cert serial no 0x" + cert.getSerialNumber().toString(16));
            } catch (ELdapException e) {
                error = true;
                mModule.log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_CERT_NOT_UNPUBLISH",
                                cert.getSerialNumber().toString(16), e.getMessage()));
            }
        }
        r.setExtData("ldapPublishStatus", results);
        r.setExtData("ldapPublishOverAllStatus",
                (error == true ? IRequest.RES_ERROR : IRequest.RES_SUCCESS));
    }
}

class HandleUnrevocation implements IRequestListener {
    private LdapPublishModule mModule = null;

    public HandleUnrevocation(LdapPublishModule module) {
        mModule = module;
    }

    public void set(String name, String val) {
    }

    public void init(ISubsystem sub, IConfigStore config) throws EBaseException {
    }

    public void accept(IRequest r) {
        CMS.debug(
                "Handle publishing for unrevoke request id " + r.getRequestId());

        // get fields in request.
        X509CertImpl[] certs = r.getExtDataInCertArray(IRequest.OLD_CERTS);

        if (certs == null || certs.length == 0 || certs[0] == null) {
            // no certs in unrevoke.
            CMS.debug(
                    "Nothing to publish for unrevocation " +
                            "request " + r.getRequestId());
            return;
        }

        // get mapper and publisher for cert type.
        LdapMappers mappers =
                mModule.getMappers(LdapPublishModule.PROP_TYPE_CLIENT);

        if (mappers == null || mappers.publisher == null) {
            CMS.debug(
                    "publisher for " + LdapPublishModule.PROP_TYPE_CLIENT + " is null");
            return;
        }

        boolean error = false;
        Integer results[] = new Integer[certs.length];

        for (int i = 0; i < certs.length; i++) {
            results[i] = IRequest.RES_ERROR;
            try {
                mModule.publish((ILdapMapper) mappers.mapper,
                        (ILdapPublisher) mappers.publisher, certs[i]);
                results[i] = IRequest.RES_SUCCESS;
                CMS.debug(
                        "Unpublished cert serial no 0x" + certs[i].getSerialNumber().toString(16));
            } catch (ELdapException e) {
                error = true;
                mModule.log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_LDAP_CERT_NOT_UNPUBLISH",
                                certs[i].getSerialNumber().toString(16), e.getMessage()));
            }
        }
        r.setExtData("ldapPublishStatus", results);
        r.setExtData("ldapPublishOverAllStatus",
                (error == true ? IRequest.RES_ERROR : IRequest.RES_SUCCESS));
    }

}