summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/admin/KRAAdminServlet.java
blob: eaa5a95c4e8b2fc1491681faad701425675285ed (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
// --- 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.admin;

import java.io.IOException;
import java.util.Enumeration;

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

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.common.Constants;
import com.netscape.certsrv.common.NameValuePairs;
import com.netscape.certsrv.common.OpDef;
import com.netscape.certsrv.common.ScopeDef;
import com.netscape.certsrv.kra.IKeyRecoveryAuthority;
import com.netscape.certsrv.logging.ILogger;

/**
 * A class representings an administration servlet for Key
 * Recovery Authority. This servlet is responsible to serve
 * KRA administrative operation such as configuration
 * parameter updates.
 * 
 * @version $Revision$, $Date$
 */
public class KRAAdminServlet extends AdminServlet {
    /**
     *
     */
    private static final long serialVersionUID = -5794220348195666729L;

    protected static final String PROP_ENABLED = "enabled";

    private final static String INFO = "KRAAdminServlet";

    private IKeyRecoveryAuthority mKRA = null;

    private final static String LOGGING_SIGNED_AUDIT_CONFIG_DRM =
            "LOGGING_SIGNED_AUDIT_CONFIG_DRM_3";

    /**
     * Constructs KRA servlet.
     */
    public KRAAdminServlet() {
        super();
    }

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        mKRA = (IKeyRecoveryAuthority) CMS.getSubsystem(CMS.SUBSYSTEM_KRA);
    }

    /**
     * Returns serlvet information.
     * 
     * @return name of this servlet
     */
    public String getServletInfo() {
        return INFO;
    }

    /**
     * Serves HTTP admin request.
     * 
     * @param req HTTP request
     * @param resp HTTP response
     */
    public void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.service(req, resp);

        super.authenticate(req);
        String scope = req.getParameter(Constants.OP_SCOPE);

        if (scope == null) {
            sendResponse(ERROR,
                    CMS.getUserMessage(getLocale(req), "CMS_ADMIN_SRVLT_INVALID_OP_SCOPE"),
                    null, resp);
            return;
        }
        String op = req.getParameter(Constants.OP_TYPE);

        if (op == null) {
            sendResponse(ERROR,
                    CMS.getUserMessage(getLocale(req), "CMS_ADMIN_SRVLT_INVALID_OP_TYPE", op),
                    null, resp);
            return;
        }

        try {
            AUTHZ_RES_NAME = "certServer.kra.configuration";
            if (op.equals(OpDef.OP_READ)) {
                mOp = "read";
                if ((mToken = super.authorize(req)) == null) {
                    sendResponse(ERROR,
                            CMS.getUserMessage(getLocale(req), "CMS_ADMIN_SRVLT_AUTHZ_FAILED"),
                            null, resp);
                    return;
                }
                /* Functions not implemented in console
                if (scope.equals(ScopeDef.SC_AUTO_RECOVERY)) {
                    readAutoRecoveryConfig(req, resp);
                    return;
                } else if (scope.equals(ScopeDef.SC_RECOVERY)) {
                    readRecoveryConfig(req, resp);
                    return;
                } else if (scope.equals(ScopeDef.SC_NOTIFICATION_RIQ)) {
                    getNotificationRIQConfig(req, resp);
                    return;
                } else
                */
                if (scope.equals(ScopeDef.SC_GENERAL)) {
                    getGeneralConfig(req, resp);
                    return;
                }
            } else if (op.equals(OpDef.OP_MODIFY)) {
                mOp = "modify";
                if ((mToken = super.authorize(req)) == null) {
                    sendResponse(ERROR,
                            CMS.getUserMessage(getLocale(req), "CMS_ADMIN_SRVLT_AUTHZ_FAILED"),
                            null, resp);
                    return;
                }
                /* Functions not implemented in console
                if (scope.equals(ScopeDef.SC_AUTO_RECOVERY)) {
                    modifyAutoRecoveryConfig(req, resp);
                    return;
                } else if (scope.equals(ScopeDef.SC_AGENT_PWD)) {
                    changeAgentPwd(req, resp);
                    return;
                } else if (scope.equals(ScopeDef.SC_MNSCHEME)) {
                    changeMNScheme(req, resp);
                    return;
                } else if (scope.equals(ScopeDef.SC_NOTIFICATION_RIQ)) {
                    setNotificationRIQConfig(req, resp);
                    return;
                } else 
                */
                if (scope.equals(ScopeDef.SC_GENERAL)) {
                    setGeneralConfig(req, resp);
                }
            }
        } catch (EBaseException e) {
            // convert exception into locale-specific message
            sendResponse(ERROR, e.toString(getLocale(req)),
                    null, resp);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        sendResponse(ERROR,
                CMS.getUserMessage(getLocale(req), "CMS_ADMIN_SRVLT_INVALID_PROTOCOL"),
                null, resp);
    }

    private void getGeneralConfig(HttpServletRequest req,
            HttpServletResponse resp) throws ServletException,
            IOException, EBaseException {

        NameValuePairs params = new NameValuePairs();
        int value = 1;

        value = mKRA.getNoOfRequiredAgents();
        params.put(Constants.PR_NO_OF_REQUIRED_RECOVERY_AGENTS, Integer.toString(value));

        sendResponse(SUCCESS, null, params, resp);
    }

    private void setGeneralConfig(HttpServletRequest req,
            HttpServletResponse resp) throws ServletException,
            IOException, EBaseException {
        @SuppressWarnings("unchecked")
        Enumeration<String> enum1 = req.getParameterNames();
        boolean restart = false;

        String auditMessage = null;
        String auditSubjectID = auditSubjectID();

        while (enum1.hasMoreElements()) {
            String key = enum1.nextElement();
            String value = req.getParameter(key);

            if (key.equals(Constants.PR_NO_OF_REQUIRED_RECOVERY_AGENTS)) {
                try {
                    int number = Integer.parseInt(value);
                    mKRA.setNoOfRequiredAgents(number);
                } catch (NumberFormatException e) {
                    auditMessage = CMS.getLogMessage(
                            LOGGING_SIGNED_AUDIT_CONFIG_DRM,
                            auditSubjectID,
                            ILogger.FAILURE,
                            auditParams(req));

                    audit(auditMessage);
                    throw new EBaseException("Number of agents must be an integer");
                }
            }
        }

        commit(true);

        auditMessage = CMS.getLogMessage(
                LOGGING_SIGNED_AUDIT_CONFIG_DRM,
                auditSubjectID,
                ILogger.SUCCESS,
                auditParams(req));

        audit(auditMessage);

        if (restart)
            sendResponse(RESTART, null, null, resp);
        else
            sendResponse(SUCCESS, null, null, resp);
    }
}