summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/csadmin/ConfigHSMServlet.java
blob: 9eb1462948fcdd666aaf9e1066f15a82c089b44e (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
// --- 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.servlet.csadmin;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.mozilla.jss.CryptoManager;
import org.mozilla.jss.crypto.CryptoToken;
import org.mozilla.jss.crypto.TokenException;
import org.mozilla.jss.pkcs11.PK11Module;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.cmsutil.crypto.Module;

public class ConfigHSMServlet extends ConfigBaseServlet {
    /**
     *
     */
    private static final long serialVersionUID = -330521231753992202L;
    private CryptoManager mCryptoManager = null;
    private Vector<Module> mSupportedModules = null;
    private Vector<Module> mOtherModules = null;
    private String mDefaultTok = null;
    private Hashtable<String, PK11Module> mCurrModTable = new Hashtable<String, PK11Module>();

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void loadCurrModTable() {
        try {
            // getting existing modules
            mCryptoManager = CryptoManager.getInstance();
            @SuppressWarnings("unchecked")
            Enumeration<PK11Module> modules = mCryptoManager.getModules();

            while (modules.hasMoreElements()) {
                PK11Module mod = modules.nextElement();

                CMS.debug("ConfigHSMServlet: got module " + mod.getName());
                mCurrModTable.put(mod.getName(), mod);
            } // while
        } catch (Exception e) {
            CMS.debug(
                    "ConfigHSMServlet: Exception caught in loadCurrModTable: "
                            + e.toString());
            System.err.println("Exception caught: " + e.toString());
        }
    }

    /*
     * Modules not listed as supported modules
     */
    public void loadOtherModules() {
        Enumeration<PK11Module> m = mCurrModTable.elements();

        mOtherModules = new Vector<Module>();
        while (m.hasMoreElements()) {
            PK11Module mod = m.nextElement();
            Enumeration<Module> s = mSupportedModules.elements();
            boolean found = false;

            while (s.hasMoreElements()) {
                Module sm = s.nextElement();

                if (mod.getName().equals(sm.getCommonName())) {
                    found = true;
                    break;
                } else {
                    found = false;
                }
            }// while
            if (!found) {
                // unsupported, use common name as user friendly name
                Module module = new Module(mod.getName(), mod.getName());

                loadModTokens(module, mod);
                module.setFound(true);
                mOtherModules.addElement(module);
                break;
            }
        }// while
    }

    /*
     * find all tokens belonging to a module and load the Module
     */
    public void loadModTokens(Module module, PK11Module mod) {
        @SuppressWarnings("unchecked")
        Enumeration<CryptoToken> tokens = mod.getTokens();

        while (tokens.hasMoreElements()) {
            try {
                CryptoToken token = tokens.nextElement();

                CMS.debug("ConfigHSMServlet: token nick name=" + token.getName());
                CMS.debug(
                        "ConfigHSMServlet: token logged in?"
                                + token.isLoggedIn());
                CMS.debug(
                        "ConfigHSMServlet: token is present?"
                                + token.isPresent());
                if (!token.getName().equals("Internal Crypto Services Token")) {
                    module.addToken(token);
                } else {
                    CMS.debug(
                            "ConfigHSMServlet: token " + token.getName()
                                    + " not to be added");
                }

            } catch (TokenException ex) {
                CMS.debug("ConfigHSMServlet:" + ex.toString());
            }
        }
    }

    /*
     * Modules unsupported by the system will not be included
     */
    public void loadSupportedModules() {

        // getting supported security modules
        // a Vectgor of Modules
        mSupportedModules = new Vector<Module>();
        // read from conf store all supported modules
        try {
            int count = CMS.getConfigStore().getInteger(
                    "preop.configModules.count");

            CMS.debug("ConfigHSMServlet: supported modules count= " + count);
            for (int i = 0; i < count; i++) {
                String cn = CMS.getConfigStore().getString(
                        "preop.configModules.module" + i + ".commonName");
                String pn = CMS.getConfigStore().getString(
                        "preop.configModules.module" + i + ".userFriendlyName");
                String img = CMS.getConfigStore().getString(
                        "preop.configModules.module" + i + ".imagePath");

                if ((cn == null) || (cn.equals(""))) {
                    break;
                }

                CMS.debug("ConfigHSMServlet: got from config module: " + cn);
                // create a Module object
                Module module = new Module(cn, pn, img);

                if (mCurrModTable.containsKey(cn)) {
                    CMS.debug("ConfigHSMServlet: module found: " + cn);
                    module.setFound(true);
                    // add token info to module vector
                    PK11Module m = mCurrModTable.get(cn);

                    loadModTokens(module, m);
                }

                CMS.debug("ConfigHSMServlet: adding module " + cn);
                // add module to set
                if (!mSupportedModules.contains(module)) {
                    mSupportedModules.addElement(module);
                }
            }// for

        } catch (Exception e) {
            CMS.debug(
                    "ConfigHSMServlet: Exception caught in loadSupportedModules(): "
                            + e.toString());
            System.err.println("Exception caught: " + e.toString());
        }
    }

    public boolean isDisplayMode(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        String choice = request.getParameter("choice");

        if (choice == null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isPanelModified(IConfigStore cs) {
        String modified = "";

        try {
            modified = cs.getString("preop.configModules.modified", "");
        } catch (Exception e) {
            return false;
        }

        if (modified.equals("true")) {
            return true;
        } else {
            return false;
        }
    }

    public void display(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        CMS.debug("ConfigHSMServlet: in display()");

        loadCurrModTable();
        loadSupportedModules();
        loadOtherModules();
        // getting default token selection
        try {
            mDefaultTok = CMS.getConfigStore().getString(
                    "preop.configModules.defaultTok",
                    "Internal Key Storage Token");
        } catch (Exception e) {
            CMS.debug("ConfigHSMServlet: Exception caught: " + e.toString());
            System.err.println("Exception caught: " + e.toString());
        }
        if (mSupportedModules == null) {
            CMS.debug("ConfigHSMServlet: mSupportedModules not loaded");
        } else {
            CMS.debug("ConfigHSMServlet: mSupportedModules loaded");
        }

        context.put("status", "display");
        context.put("oms", mOtherModules);
        context.put("sms", mSupportedModules);
        context.put("defTok", mDefaultTok);
    }

    public void update(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {

        IConfigStore cs = CMS.getConfigStore();

        CMS.debug("ConfigHSMServlet: in update()");

        if (mSupportedModules == null) {
            CMS.debug("ConfigHSMServlet: mSupportedModules not loaded");
        } else {
            CMS.debug("ConfigHSMServlet: mSupportedModules loaded");
        }

        String select = request.getParameter("choice");

        if (select == null) {
            CMS.debug("ConfigHSMServlet: choice not found");
            // throw new IOException("choice not found");
        }

        try {
            CMS.debug("ConfigHSMServlet: choice =" + select);
            cs.putString("preop.configModules.defaultTok", select);
            cs.commit(false);
        } catch (Exception e) {
            CMS.debug("ConfigHSMServlet: Exception caught: " + e.toString());
            System.err.println("Exception caught: " + e.toString());
        }
        context.put("status", "update");
        context.put("error", "");

    }

    public Template getTemplate(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        try {
            return Velocity.getTemplate("admin/console/config/config_hsm.vm");
        } catch (Exception e) {
        }
        return null;
    }
}