summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cms/servlet/csadmin/AdminPanel.java
blob: 579c54df027f981011de0e65dcc3cd92b2515a5a (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
// --- 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.io.IOException;

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

import org.apache.velocity.context.Context;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.ISubsystem;
import com.netscape.certsrv.property.Descriptor;
import com.netscape.certsrv.property.IDescriptor;
import com.netscape.certsrv.property.PropertySet;
import com.netscape.certsrv.usrgrp.IUGSubsystem;
import com.netscape.certsrv.util.HttpInput;
import com.netscape.cms.servlet.wizard.WizardServlet;

public class AdminPanel extends WizardPanelBase {

    private static final String ADMIN_UID = "admin";

    public AdminPanel() {
    }

    /**
     * Initializes this panel.
     */
    public void init(ServletConfig config, int panelno)
            throws ServletException {
        setPanelNo(panelno);
        setName("Administrator");
    }

    public void init(WizardServlet servlet, ServletConfig config, int panelno, String id) {
        setPanelNo(panelno);
        setName("Administrator");
        setId(id);
    }

    public void cleanUp() throws IOException {
        IConfigStore cs = CMS.getConfigStore();
        cs.putString("preop.admin.email", "");
    }

    public boolean isPanelDone() {
        IConfigStore cs = CMS.getConfigStore();
        try {
            String s = cs.getString("preop.admin.email", "");
            if (s == null || s.equals("")) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }

    public PropertySet getUsage() {
        PropertySet set = new PropertySet();

        Descriptor emailDesc = new Descriptor(IDescriptor.STRING, null, /* no constraint */
                null, /* no default parameter */
                "Email address for an administrator");

        set.add("admin_email", emailDesc);

        Descriptor pwdDesc = new Descriptor(IDescriptor.STRING, null, /* no constraint */
                null, /* no default parameter */
                "Administrator's password");

        set.add("pwd", pwdDesc);

        Descriptor pwdAgainDesc = new Descriptor(IDescriptor.STRING, null, /* no constraint */
                null, /* no default parameter */
                "Administrator's password again");

        set.add("admin_password_again", pwdAgainDesc);
        return set;
    }

    /**
     * Display the panel.
     */
    public void display(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        CMS.debug("AdminPanel: display");

        IConfigStore cs = CMS.getConfigStore();
        String session_id = request.getParameter("session_id");
        if (session_id != null) {
            CMS.debug("NamePanel setting session id.");
            CMS.setConfigSDSessionId(session_id);
        }

        String type = "";
        String info = "";
        context.put("import", "true");

        try {
            type = cs.getString("preop.ca.type", "");
        } catch (Exception e) {
        }

        if (isPanelDone()) {
            try {
                context.put("admin_email", cs.getString("preop.admin.email"));
                context.put("admin_name", cs.getString("preop.admin.name"));
                context.put("admin_pwd", "");
                context.put("admin_pwd_again", "");
                context.put("admin_uid", cs.getString("preop.admin.uid"));
            } catch (Exception e) {
            }
        } else {
            String def_admin_name = "";
            try {
                def_admin_name = cs.getString("cs.type") + " Administrator of Instance " + cs.getString("instanceId");
            } catch (EBaseException e) {
            }
            context.put("admin_name", def_admin_name);
            context.put("admin_email", "");
            context.put("admin_pwd", "");
            context.put("admin_pwd_again", "");
            context.put("admin_uid", ADMIN_UID);
        }
        ISubsystem ca = CMS.getSubsystem("ca");

        if (ca == null) {
            context.put("ca", "false");
        } else {
            context.put("ca", "true");
        }
        context.put("caType", type);

        String domainname = "";
        try {
            domainname = cs.getString("securitydomain.name", "");
        } catch (EBaseException e1) {
        }
        context.put("securityDomain", domainname);
        context.put("title", "Administrator");
        context.put("panel", "admin/console/config/adminpanel.vm");
        context.put("errorString", "");
        context.put("info", info);

    }

    /**
     * Checks if the given parameters are valid.
     */
    public void validate(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
        String pwd = HttpInput.getPassword(request, "__pwd");
        String pwd_again = HttpInput.getPassword(request, "__admin_password_again");
        String email = HttpInput.getEmail(request, "email");
        String name = HttpInput.getName(request, "name");
        String uid = HttpInput.getUID(request, "uid");
        context.put("admin_email", email);
        context.put("admin_name", name);
        context.put("admin_pwd", pwd);
        context.put("admin_pwd_again", pwd_again);
        context.put("import", "true");

        if (name == null || name.equals("")) {
            context.put("updateStatus", "validate-failure");
            throw new IOException("Name is empty");
        }

        if (email == null || email.equals("")) {
            context.put("updateStatus", "validate-failure");
            throw new IOException("Email is empty");
        }

        if (uid == null || uid.equals("")) {
            context.put("updateStatus", "validate-failure");
            throw new IOException("Uid is empty");
        }

        if (!pwd.equals(pwd_again)) {
            context.put("updateStatus", "validate-failure");
            throw new IOException("Password and password again are not the same.");
        }

        if (email == null || email.length() == 0) {
            context.put("updateStatus", "validate-failure");
            throw new IOException("Email address is empty string.");
        }
    }

    /**
     * Commit parameter changes
     */
    public void update(HttpServletRequest request, HttpServletResponse response, Context context) throws IOException {
        IConfigStore config = CMS.getConfigStore();
        context.put("info", "");
        context.put("import", "true");

        String uid = HttpInput.getUID(request, "uid");
        String email = HttpInput.getEmail(request, "email");
        String name = HttpInput.getName(request, "name");
        String pwd = HttpInput.getPassword(request, "__pwd");
        String cert_request_type = HttpInput.getID(request, "cert_request_type");
        String subject = request.getParameter("subject");
        String cert_request = HttpInput.getCertRequest(request, "cert_request");
        String profileId = HttpInput.getID(request, "profileId");

        try {
            String type = config.getString(PRE_CA_TYPE, "");
            String subsystemtype = config.getString("cs.type", "");
            String selected_hierarchy = config.getString("preop.hierarchy.select", "");

            ISubsystem ca = CMS.getSubsystem("ca");

            if (ca == null) {
                context.put("ca", "false");
            } else {
                context.put("ca", "true");
            }
            context.put("caType", type);

            config.putString("preop.admin.uid", uid);
            config.putString("preop.admin.email", email);
            config.putString("preop.admin.name", name);
            ConfigurationUtils.createAdmin(uid, email, name, pwd);

            if (ca != null) {
                if (selected_hierarchy.equals("root")) {
                    CMS.debug("AdminPanel update:  " + "Root CA subsystem");
                } else {
                    CMS.debug("AdminPanel update:  " + "Subordinate CA subsystem");
                }

                ConfigurationUtils.createAdminCertificate(cert_request,
                        cert_request_type, subject);
            } else {
                String ca_hostname = null;
                int ca_port = -1;

                CMS.debug("AdminPanel update:  " + subsystemtype + " subsystem");

                if (type.equals("sdca")) {
                    ca_hostname = config.getString("preop.ca.hostname");
                    ca_port = config.getInteger("preop.ca.httpsport");
                } else {
                    ca_hostname = config.getString("securitydomain.host", "");
                    ca_port = config.getInteger("securitydomain.httpseeport");
                }

                ConfigurationUtils.submitAdminCertRequest(ca_hostname, ca_port,
                        profileId, cert_request_type, cert_request, subject);
            }

            CMS.reinit(IUGSubsystem.ID);
            config.commit(false);
        } catch (Exception e) {
            CMS.debug("AdminPanel update(): Exception thrown " + e);
            e.printStackTrace();
            context.put("updateStatus", "failure");
            throw new IOException("Error when adding admin user" + e);
        }

        context.put("updateStatus", "success");
    }

    /**
     * If validate() returns false, this method will be called.
     */
    public void displayError(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {

        context.put("title", "Administrator");
        context.put("panel", "admin/console/config/adminpanel.vm");
        ISubsystem ca = CMS.getSubsystem("ca");
        IConfigStore cs = CMS.getConfigStore();
        String type = "";
        String info = "";

        try {
            type = cs.getString("preop.ca.type", "");
        } catch (Exception e) {
        }
        if (ca == null && type.equals("otherca")) {
            info =
                    "Since you do not join the Redhat CA network, the administrator's certificate will not be generated automatically.";
        }
        context.put("info", info);
        context.put("admin_email", request.getParameter("email"));
        context.put("admin_name", request.getParameter("name"));
        context.put("admin_pwd", "");
        context.put("admin_pwd_again", "");
        context.put("admin_uid", request.getParameter("uid"));
    }

    public boolean shouldSkip() {
        try {
            IConfigStore c = CMS.getConfigStore();
            String s = c.getString("preop.subsystem.select", null);
            if (s != null && s.equals("clone")) {
                return true;
            }
        } catch (EBaseException e) {
        }

        return false;
    }
}