summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cms/servlet/csadmin/DonePanel.java
blob: e81afdd2f9068e27dd3c89d462e6c92805b6158e (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
// --- 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 java.math.BigInteger;

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.IConfigStore;
import com.netscape.certsrv.ocsp.IOCSPAuthority;
import com.netscape.certsrv.property.PropertySet;
import com.netscape.certsrv.usrgrp.IUGSubsystem;
import com.netscape.certsrv.usrgrp.IUser;
import com.netscape.cms.servlet.wizard.WizardServlet;
import com.netscape.cmsutil.util.Utils;

public class DonePanel extends WizardPanelBase {

    public static final BigInteger BIG_ZERO = new BigInteger("0");
    public static final Long MINUS_ONE = Long.valueOf(-1);
    public static final String RESTART_SERVER_AFTER_CONFIGURATION =
            "restart_server_after_configuration";
    public static final String PKI_SECURITY_DOMAIN = "pki_security_domain";

    public DonePanel() {
    }

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

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

    public boolean hasSubPanel() {
        return false;
    }

    public void cleanUp() throws IOException {
    }

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

        /* XXX */

        return set;
    }

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

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

        IConfigStore cs = CMS.getConfigStore();

        String select = "";
        String type = "";
        String instanceId = "";
        String instanceRoot = "";
        String systemdService = "";
        String ca_host = "";
        String sdtype = "";
        int state = 0;
        try {
            type = cs.getString("cs.type", "");
            instanceId = cs.getString("instanceId");
            instanceRoot = cs.getString("instanceRoot");
            select = cs.getString("preop.subsystem.select", "");
            systemdService = cs.getString("pkicreate.systemd.servicename", "");
            ca_host = cs.getString("preop.ca.hostname", "");
            sdtype = cs.getString("securitydomain.select", "");
            state = cs.getInteger("cs.state");
        } catch (Exception e) {
        }

        String initDaemon = "";
        if (type.equals("CA")) {
            initDaemon = "pki-cad";
        } else if (type.equals("KRA")) {
            initDaemon = "pki-krad";
        } else if (type.equals("OCSP")) {
            initDaemon = "pki-ocspd";
        } else if (type.equals("TKS")) {
            initDaemon = "pki-tksd";
        }
        String os = System.getProperty("os.name");
        if (os.equalsIgnoreCase("Linux")) {
            if (!systemdService.equals("")) {
                context.put("initCommand", "/bin/systemctl");
                context.put("instanceId", systemdService);
            } else {
                context.put("initCommand", "/sbin/service " + initDaemon);
                context.put("instanceId", instanceId);
            }
        } else {
            /* default case:  e. g. - ( os.equalsIgnoreCase( "SunOS" ) */
            context.put("initCommand", "/etc/init.d/" + initDaemon);
            context.put("instanceId", instanceId);
        }
        context.put("title", "Done");
        context.put("panel", "admin/console/config/donepanel.vm");
        context.put("host", CMS.getAdminHost());
        context.put("port", CMS.getAdminPort());
        context.put("systemType", type.toLowerCase());

        if (state == 1) {
            context.put("csstate", "1");
            return;
        } else
            context.put("csstate", "0");

        if (ca_host.equals(""))
            context.put("externalCA", "true");
        else
            context.put("externalCA", "false");

        // Create or update security domain

        try {
            if (sdtype.equals("new")) {
                ConfigurationUtils.createSecurityDomain();
            } else { //existing domain
                ConfigurationUtils.updateSecurityDomain();
            }

            cs.putString("service.securityDomainPort", CMS.getAgentPort());
            cs.putString("securitydomain.store", "ldap");
            cs.commit(false);
        } catch (Exception e) {
            CMS.debug("DonePanel - update(): Error while updating security domain: " + e);
            e.printStackTrace();
        }

        // push connector information to the CA
        try {
            if (type.equals("KRA") && !ca_host.equals("")) {
                ConfigurationUtils.updateConnectorInfo(CMS.getAgentHost(), CMS.getAgentPort());
                ConfigurationUtils.setupClientAuthUser();
            }
        } catch (Exception e) {
            context.put("info", "Failed to update connector information. "+e.getMessage());
            CMS.debug("DonePanel - update(): Error while pushing KRA connectot information to the CA: " + e);
            e.printStackTrace();
        }

        // import the CA certificate into the OCSP
        // configure the CRL Publishing to OCSP in CA
        try {
            if (type.equals("OCSP") && !ca_host.equals("")) {
                CMS.reinit(IOCSPAuthority.ID);
                ConfigurationUtils.importCACertToOCSP();
                ConfigurationUtils.updateOCSPConfig();
                ConfigurationUtils.setupClientAuthUser();
            }
        } catch (Exception e) {
            CMS.debug("DonePanel - update(): Error while configuring OCSP publishing on the CA: " + e);
            e.printStackTrace();
        }

        try {
            if (!select.equals("clone")) {
                if (type.equals("CA") || type.equals("KRA")) {
                    ConfigurationUtils.updateNextRanges();

                }
            }
        } catch (Exception e) {
            CMS.debug("DonePanel - update(): Error while updating serial number next ranges: " + e);
            e.printStackTrace();
        }

        try {
            if (select.equals("clone") && type.equalsIgnoreCase("CA") && ConfigurationUtils.isSDHostDomainMaster(cs)) {
                // cloning a domain master CA, the clone is also master of its domain
                CMS.debug("Cloning a domain master");
                cs.putString("securitydomain.host", CMS.getEESSLHost());
                cs.putString("securitydomain.httpport", CMS.getEENonSSLPort());
                cs.putString("securitydomain.httpsadminport", CMS.getAdminPort());
                cs.putString("securitydomain.httpsagentport", CMS.getAgentPort());
                cs.putString("securitydomain.httpseeport", CMS.getEESSLPort());
                cs.putString("securitydomain.select", "new");
            }
        } catch (Exception e) {
            CMS.debug("DonePanel - update(): Error in determining if security domain host is a master CA: " + e);
            e.printStackTrace();
        }

        String dbuser = null;
        try {
            dbuser = cs.getString("cs.type") + "-" + cs.getString("machineName") + "-"
                    + cs.getString("service.securePort");
            if (!sdtype.equals("new")) {
                ConfigurationUtils.setupDBUser(dbuser);
            }
            IUGSubsystem system = (IUGSubsystem) (CMS.getSubsystem(IUGSubsystem.ID));
            IUser user = system.getUser(dbuser);
            system.addCertSubjectDN(user);
        } catch (Exception e) {
            e.printStackTrace();
            CMS.debug("DonePanel - update(): Unable to create or update dbuser" + e);
        }

        cs.putInteger("cs.state", 1);
        try {
            ConfigurationUtils.removePreopConfigEntries();
        } catch (Exception e) {
            CMS.debug("DonePanel - update(): Caught exception saving preop variables: " + e);
        }

        // Create an empty file that designates the fact that although
        // this server instance has been configured, it has NOT yet
        // been restarted!
        String restart_server = instanceRoot + "/conf/"
                + RESTART_SERVER_AFTER_CONFIGURATION;
        if (!Utils.isNT()) {
            Utils.exec("touch " + restart_server);
            Utils.exec("chmod 00660 " + restart_server);
        }

        context.put("csstate", "1");
    }

    /**
     * Checks if the given parameters are valid.
     */
    public void validate(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
    }

    /**
     * Commit parameter changes
     */
    public void update(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
    }

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