summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/publish/publishers/LdapCrlPublisher.java
blob: 6327291ed71b9a1c0a3f6e70df8b7853e7ede89e (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
// --- 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.publish.publishers;


import java.io.*;
import java.util.*;
import java.security.cert.*;
import netscape.ldap.*;
import com.netscape.certsrv.logging.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.apps.*;

import com.netscape.certsrv.ldap.*;
import com.netscape.certsrv.publish.*;


/**
 * For publishing master or global CRL. 
 * Publishes (replaces) the CRL in the CA's LDAP entry.
 * 
 * @version $Revision$, $Date$
 */
public class LdapCrlPublisher implements ILdapPublisher, IExtendedPluginInfo {
    private ILogger mLogger = CMS.getLogger();
    protected IConfigStore mConfig = null;
    boolean mInited = false;

    public static final String 
        LDAP_CRL_ATTR = "certificateRevocationList;binary";

    String mCrlAttr = LDAP_CRL_ATTR;

    /**
     * constructs ldap crl publisher with default values
     */
    public LdapCrlPublisher() {
    }

    public String getImplName() {
        return "LdapCrlPublisher";
    }

    public String getDescription() {
        return "LdapCrlPublisher";
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                "crlAttr;string;Name of Ldap attribute in which to store the CRL",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-ldappublish-publisher-crlpublisher",
                IExtendedPluginInfo.HELP_TEXT +
                ";This plugin knows how to publish CRL's to an LDAP directory"
            };

        return params;
    }

    public Vector getInstanceParams() {
        Vector v = new Vector();

        v.addElement("crlAttr=" + mCrlAttr);
        return v;
    }

    public Vector getDefaultParams() {
        Vector v = new Vector();

        v.addElement("crlAttr=" + mCrlAttr);
        return v;
    }

    public IConfigStore getConfigStore() {
        return mConfig;
    }

    public void init(IConfigStore config) 
        throws EBaseException {
        if (mInited)
            return;
        mConfig = config;
        mCrlAttr = mConfig.getString("crlAttr", LDAP_CRL_ATTR);
        mInited = true;
    }

    public LdapCrlPublisher(String crlAttr) {
        mCrlAttr = crlAttr;
    }

    /**
     * Replaces the CRL in the certificateRevocationList attribute.
     * CRL's are published as a DER encoded blob.
     */
    public void publish(LDAPConnection conn, String dn, Object crlObj)
        throws ELdapException {
        if (conn == null) {
            log(ILogger.LL_INFO, "publish CRL: no LDAP connection");
            return;
        }

        // Bugscape #56124 - support multiple publishing directory
        // see if we should create local connection
        LDAPConnection altConn = null;
        try {
          String host = mConfig.getString("host", null);
          String port = mConfig.getString("port", null);
          if (host != null && port != null) {
            int portVal = Integer.parseInt(port);
            int version = Integer.parseInt(mConfig.getString("version", "2"));
            String cert_nick = mConfig.getString("clientCertNickname", null);
            LDAPSSLSocketFactoryExt sslSocket = null;
            if (cert_nick != null) {
                sslSocket = CMS.getLdapJssSSLSocketFactory(cert_nick);
            }
            String mgr_dn = mConfig.getString("bindDN", null);
            String mgr_pwd = mConfig.getString("bindPWD", null);

            altConn = CMS.getBoundConnection(host, portVal,
               version,
               sslSocket, mgr_dn, mgr_pwd);
            conn = altConn;
          }
        } catch (LDAPException e) {
          CMS.debug("Failed to create alt connection " + e);
        } catch (EBaseException e) {
          CMS.debug("Failed to create alt connection " + e);
        }

        try {
            byte[] crlEnc = ((X509CRL) crlObj).getEncoded();

            log(ILogger.LL_INFO, "publish CRL: " + dn);
            conn.modify(dn, new LDAPModification(LDAPModification.REPLACE,
                    new LDAPAttribute(mCrlAttr, crlEnc)));
        } catch (CRLException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_PUBLISH_ERROR", e.toString()));
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_PUBLISH_CRL_ERROR", e.toString()));
        } catch (LDAPException e) {
            if (e.getLDAPResultCode() == LDAPException.UNAVAILABLE) {
                // need to intercept this because message from LDAP is
                // "DSA is unavailable" which confuses with DSA PKI.
                log(ILogger.LL_FAILURE,
                    CMS.getLogMessage("PUBLISH_NO_LDAP_SERVER"));
                throw new ELdapServerDownException(CMS.getUserMessage("CMS_LDAP_SERVER_UNAVAILABLE", conn.getHost(), "" + conn.getPort()));
            } else {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_PUBLISH_ERROR", e.toString()));
                throw new ELdapException(CMS.getUserMessage("CMS_LDAP_PUBLISH_CRL_ERROR", e.toString()));
            }
        } finally {
          if (altConn != null) {
             try {
                altConn.disconnect();
             } catch (LDAPException e) {
                // safely ignored
             }
          }
        }

    }

    /**
     * There shouldn't be a need to call this. 
     * CRLs are always replaced but this is implemented anyway in case 
     * there is ever a reason to remove a global CRL.
     */
    public void unpublish(LDAPConnection conn, String dn, Object crlObj)
        throws ELdapException {
        try {
            byte[] crlEnc = ((X509CRL) crlObj).getEncoded();

            LDAPSearchResults res = conn.search(dn, LDAPv2.SCOPE_BASE,
                    "(objectclass=*)", new String[] { mCrlAttr }, false);
            LDAPEntry e = res.next();
            LDAPAttribute crls = e.getAttribute(mCrlAttr);

            if (!LdapUserCertPublisher.ByteValueExists(crls, crlEnc)) {
                log(ILogger.LL_INFO, 
                    "unpublish: " + dn + " already has not CRL");
                return;
            }
            conn.modify(dn, new LDAPModification(LDAPModification.DELETE,
                    new LDAPAttribute(mCrlAttr, crlEnc)));
        } catch (CRLException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_UNPUBLISH_ERROR", e.toString()));
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_PUBLISH_CRL_ERROR", e.toString()));
        } catch (LDAPException e) {
            if (e.getLDAPResultCode() == LDAPException.UNAVAILABLE) {
                // need to intercept this because message from LDAP is
                // "DSA is unavailable" which confuses with DSA PKI.
                log(ILogger.LL_FAILURE,
                    CMS.getLogMessage("PUBLISH_NO_LDAP_SERVER"));
                throw new ELdapServerDownException(CMS.getUserMessage("CMS_LDAP_SERVER_UNAVAILABLE", conn.getHost(), "" + conn.getPort()));
            } else {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_UNPUBLISH_ERROR", e.toString()));
                throw new ELdapException(CMS.getUserMessage("CMS_LDAP_UNPUBLISH_CRL_ERROR", e.toString()));
            }
        }
        return;
    }

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