summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/request/ProcessReq.java
blob: 899f4c309305653da0a56abc5e546a8883a521cd (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
// --- 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.request;


import com.netscape.cms.servlet.common.*;
import com.netscape.cms.servlet.base.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.security.*;
import javax.servlet.*;
import javax.servlet.http.*;
import netscape.security.x509.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.apps.*;
import com.netscape.certsrv.ca.*;
import com.netscape.certsrv.ra.*;
import com.netscape.certsrv.authority.*;
import com.netscape.certsrv.dbs.*;
import com.netscape.cms.servlet.*;
import com.netscape.certsrv.dbs.keydb.*;
import com.netscape.certsrv.authentication.*;
import com.netscape.certsrv.authorization.*;
import com.netscape.certsrv.request.*;
import com.netscape.certsrv.policy.*; 
import com.netscape.certsrv.logging.*;


/**
 * Display Generic Request detail to the user.
 *
 * @version $Revision$, $Date$
 */
public class ProcessReq extends CMSServlet {

    private final static String INFO = "processReq";
    private final static String SEQNUM = "seqNum";
    private final static String DO_ASSIGN = "doAssign";
    private final static String TPL_FILE = "processReq.template";
    private final static String OUT_ERROR = "errorDetails";
    private final static String PROP_PARSER = "parser";

    private IRequestQueue mQueue = null;
    private String mFormPath = null;
    private IReqParser mParser = null;
    private String[] mSigningAlgorithms = null;

    private static String[] DEF_SIGNING_ALGORITHMS = new String[] 
        {"SHA1withRSA", "SHA256withRSA", "SHA512withRSA", "SHA1withDSA", "MD5withRSA", "MD2withRSA"};

    /**
     * Process request.
     */
    public ProcessReq() {
        super();
    }

    /**
     * initialize the servlet. This servlet uses the template file
     * "processReq.template" to process the response.
     * The initialization parameter 'parser' is read from the
     * servlet configration, and is used to set the type of request.
     * The value of this parameter can be:
     * <UL><LI><B>CertReqParser.NODETAIL_PARSER</B> - Show certificate Summary
     *  <LI><B>CertReqParser.DETAIL_PARSER</B> - Show certificate detail
     *  <LI><B>KeyReqParser.PARSER</B> - Show key archival detail
	 * </UL>
     *
     * @param sc servlet configuration, read from the web.xml file
     */
    public void init(ServletConfig sc) throws ServletException {
        super.init(sc);
        mQueue = mAuthority.getRequestQueue();
        mFormPath = "/" + mAuthority.getId() + "/" + TPL_FILE;

        String tmp = sc.getInitParameter(PROP_PARSER);

        if (tmp != null) {
            if (tmp.trim().equals("CertReqParser.NODETAIL_PARSER"))
                mParser = CertReqParser.NODETAIL_PARSER;
            else if (tmp.trim().equals("CertReqParser.DETAIL_PARSER"))
                mParser = CertReqParser.DETAIL_PARSER;
            else if (tmp.trim().equals("KeyReqParser.PARSER"))
                mParser = KeyReqParser.PARSER;
        }			

        // override success and error templates to null - 
        // handle templates locally.
        mTemplates.remove(CMSRequest.SUCCESS);
        mTemplates.remove(CMSRequest.ERROR);
        if (mOutputTemplatePath != null) 
            mFormPath = mOutputTemplatePath;
    }

    /**
     * Process the HTTP request.
     * <ul>
     * <li>http.param seqNum
     * <li>http.param doAssign reassign request. Value can be reassignToMe
     *       reassignToNobody
     * </ul>
     *
     * @param cmsReq the object holding the request and response information
     */
    public void process(CMSRequest cmsReq) throws EBaseException {
        int seqNum = -1;

        HttpServletRequest req = cmsReq.getHttpReq();
        HttpServletResponse resp = cmsReq.getHttpResp();

        IAuthToken authToken = authenticate(cmsReq);

        IArgBlock header = CMS.createArgBlock();
        IArgBlock fixed = CMS.createArgBlock();
        CMSTemplateParams argSet = new CMSTemplateParams(header, fixed);

        String doAssign = null;
        EBaseException error = null;

        CMSTemplate form = null, errorForm = null;
        Locale[] locale = new Locale[1];

        try {
            form = getTemplate(mFormPath, req, locale);
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, 
                "Error getting template " + mFormPath + " Error " + e);
            throw new ECMSGWException(
              CMS.getUserMessage("CMS_GW_DISPLAY_TEMPLATE_ERROR"));
        }

        try {
            if (req.getParameter(SEQNUM) != null) {
                seqNum = Integer.parseInt(req.getParameter(SEQNUM));
            }
            doAssign = req.getParameter(DO_ASSIGN);

            if (seqNum > -1) {
                // start authorization
                AuthzToken authzToken = null;

                try {
                    if (doAssign == null) {
                        authzToken = authorize(mAclMethod, authToken,
                                    mAuthzResourceName, "read");
                    } else if (doAssign.equals("toMe") || 
                        doAssign.equals("reassignToMe")) {
                        authzToken = authorize(mAclMethod, authToken,
                                    mAuthzResourceName, "assign");
                    } else if (doAssign.equals("reassignToNobody")) {
                        authzToken = authorize(mAclMethod, authToken,
                                    mAuthzResourceName, "unassign");
                    }
                } catch (EAuthzAccessDenied e) {
                    log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("ADMIN_SRVLT_AUTH_FAILURE", e.toString()));
                } catch (Exception e) {
                    log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("ADMIN_SRVLT_AUTH_FAILURE", e.toString()));
                }

                if (authzToken == null) {
                    cmsReq.setStatus(CMSRequest.UNAUTHORIZED);
                    return;
                }

                process(argSet, header, seqNum, req, resp, 
                    doAssign, locale[0]);
            } else {
                log(ILogger.LL_FAILURE, "Invalid sequence number " + seqNum);
                error = new ECMSGWException(
                  CMS.getUserMessage("CMS_GW_INVALID_REQUEST_ID",
                            String.valueOf(seqNum)));
            }
        } catch (EBaseException e) {
            error = e;
        } catch (NumberFormatException e) {
            error = new EBaseException(CMS.getUserMessage(locale[0], "CMS_BASE_INVALID_NUMBER_FORMAT"));
        } 

        try {
            ServletOutputStream out = resp.getOutputStream();

            if (error == null) {
                String xmlOutput = req.getParameter("xml");
                if (xmlOutput != null && xmlOutput.equals("true")) {
                  outputXML(resp, argSet);
                } else {
                  String output = form.getOutput(argSet);
                  resp.setContentType("text/html");
                  form.renderOutput(out, argSet);
                  cmsReq.setStatus(CMSRequest.SUCCESS);
                }
            } else {
                cmsReq.setError(error);
                cmsReq.setStatus(CMSRequest.ERROR);
            }
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, 
                "Error getting servlet output stream for rendering template. " +
                "Error " + e);
            throw new ECMSGWException(
              CMS.getUserMessage("CMS_GW_DISPLAY_TEMPLATE_ERROR"));
        }
        return;
    }

    /**
     * Sends request information to the calller. 
     * returns whether there was an error or not.
     */
    private void process(CMSTemplateParams argSet, IArgBlock header,
        int seqNum, HttpServletRequest req,
        HttpServletResponse resp, 
        String doAssign, Locale locale)
        throws EBaseException {

        header.addIntegerValue("seqNum", seqNum);

        IRequest r = 
            mQueue.findRequest(new RequestId(Integer.toString(seqNum)));

        if (r != null) {
            if (doAssign != null) {
                if ((doAssign.equals("toMe"))
                    || (doAssign.equals("reassignToMe"))) {
                    SessionContext ctx = SessionContext.getContext();
                    String id = (String) ctx.get(SessionContext.USER_ID);

                    r.setRequestOwner(id);
                    mQueue.updateRequest(r);
                } else if (doAssign.equals("reassignToNobody")) {
                    r.setRequestOwner(null);
                    mQueue.updateRequest(r);
                }
            }

            // add authority names to know what privileges can be requested.	
            if (CMS.getSubsystem("kra") != null) 
                header.addStringValue("localkra", "yes");
            if (CMS.getSubsystem("ca") != null) 
                header.addStringValue("localca", "yes");
            if (CMS.getSubsystem("ra") != null) 
                header.addStringValue("localra", "yes");

                // DONT NEED TO DO THIS FOR DRM
            if (mAuthority instanceof ICertAuthority) {
                // Check/set signing algorithms dynamically.
                // In RA mSigningAlgorithms could be null at startup if CA is not 
                // up and set later when CA comes back up. 
                // Once it's set assumed that it won't change.
                String[] allAlgorithms = mSigningAlgorithms;

                if (allAlgorithms == null) {
                    allAlgorithms = mSigningAlgorithms = 
                                    ((ICertAuthority) mAuthority).getCASigningAlgorithms();
                    if (allAlgorithms == null) {
                        CMS.debug(
                            "ProcessReq: signing algorithms set to All algorithms");
                        allAlgorithms = AlgorithmId.ALL_SIGNING_ALGORITHMS;
                    } else 
                        CMS.debug(
                            "ProcessReq: First signing algorithms is " + allAlgorithms[0]);
                }
                String validAlgorithms = null;
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < allAlgorithms.length; i++) {
                    if (i > 0) {
                        sb.append("+");
                        sb.append(allAlgorithms[i]);
                    } else {
                        sb.append(allAlgorithms[i]);
                    }
                }
                validAlgorithms = sb.toString();
                if (validAlgorithms != null)
                    header.addStringValue("validAlgorithms", validAlgorithms);
                if (mAuthority instanceof ICertificateAuthority) {
                    String signingAlgorithm = ((ICertificateAuthority) mAuthority).getDefaultAlgorithm();

                    if (signingAlgorithm != null)
                        header.addStringValue("caSigningAlgorithm", signingAlgorithm);
                    header.addLongValue("defaultValidityLength",
                        ((ICertificateAuthority) mAuthority).getDefaultValidity() / 1000);
                } else if (mAuthority instanceof IRegistrationAuthority) {
                    header.addLongValue("defaultValidityLength",
                        ((IRegistrationAuthority) mAuthority).getDefaultValidity() / 1000);
                }
                X509CertImpl caCert = ((ICertAuthority) mAuthority).getCACert();

                if (caCert != null) {
                    int caPathLen = caCert.getBasicConstraints();

                    header.addIntegerValue("caPathLen", caPathLen);
                }
            }

            mParser.fillRequestIntoArg(locale, r, argSet, header);
        } else {
            log(ILogger.LL_FAILURE, "Invalid sequence number " + seqNum);
            throw new ECMSGWException(
                  CMS.getUserMessage("CMS_GW_INVALID_REQUEST_ID",
                    String.valueOf(seqNum)));
        }

        return;
    }
}