summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/csadmin/SecurityDomainPanel.java
blob: 160cc74c3f1b9858a17abf2610dc4d71045bb3d2 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// --- 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 org.apache.velocity.Template;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import javax.servlet.*;
import javax.servlet.http.*;
import org.mozilla.jss.crypto.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.util.*;
import com.netscape.certsrv.apps.*;
import com.netscape.certsrv.property.*;
import com.netscape.certsrv.base.*;
import com.netscape.cmsutil.crypto.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.security.cert.*;
import com.netscape.cmsutil.xml.*;
import org.w3c.dom.*;

import com.netscape.cms.servlet.wizard.*;

public class SecurityDomainPanel extends WizardPanelBase {

    public SecurityDomainPanel() {}

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

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

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

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

    public PropertySet getUsage() {
        PropertySet set = new PropertySet();
                                                                                
        /* XXX */
                                                                                
        return set;
    }

    /**
     * Display the panel.
     */
    public void display(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        context.put("title", "Security Domain");
        IConfigStore config = CMS.getConfigStore();
        String errorString = "";
        String default_admin_url = "";
        String name = "";
        String cstype = "";
        String systemdService = "";

        try {
            default_admin_url = config.getString("preop.securitydomain.admin_url", "");
            name = config.getString("preop.securitydomain.name", "");
            cstype = config.getString("cs.type", "");
            systemdService = config.getString("pkicreate.systemd.servicename", "");
        } catch (Exception e) {
            CMS.debug(e.toString());
        }
        if (isPanelDone()) {
            try {
                String s = config.getString("preop.securitydomain.select");

                if (s.equals("new")) {
                    context.put("check_newdomain", "checked");
                    context.put("check_existingdomain", "");
                } else if (s.equals("existing")) {
                    context.put("check_newdomain", "");
                    context.put("check_existingdomain", "checked");
                }
            } catch (Exception e) {
                CMS.debug(e.toString());
            }
        } else {
            context.put("check_newdomain", "checked");
            context.put("check_existingdomain", "");
        }

        try {
            context.put("cstype", config.getString("cs.type"));
            context.put("wizardname", config.getString("preop.wizard.name"));
            context.put("panelname", "Security Domain Configuration");
            context.put("systemname", config.getString("preop.system.name"));
            context.put("machineName", config.getString("machineName"));
            context.put("http_ee_port", CMS.getEENonSSLPort());
            context.put("https_agent_port", CMS.getAgentPort());
            context.put("https_ee_port", CMS.getEESSLPort());
            context.put("https_admin_port", CMS.getAdminPort());
            context.put("sdomainAdminURL", default_admin_url);
        } catch (EBaseException e) {}

        context.put("panel", "admin/console/config/securitydomainpanel.vm");
        context.put("errorString", errorString);

        // from default_admin_url, find hostname, if fully qualified, get
        // network domain name and generate default security domain name
        if (name.equals("") && (default_admin_url != null)) {
            try {
                URL u = new URL(default_admin_url);

                String hostname = u.getHost();
                StringTokenizer st = new StringTokenizer(hostname, ".");
                boolean first = true;
                int numTokens = st.countTokens();
                int count = 0;
                String defaultDomain = "";
                StringBuffer sb = new StringBuffer();
                while (st.hasMoreTokens()) {
                    count++;
                    String n = st.nextToken();
                    if (first) { //skip the hostname
                        first = false;
                        continue;
                    }
                    if (count == numTokens) // skip the last element (e.g. com)
                        continue;
                    sb.append((defaultDomain.length()==0)? "":" ");
                    sb.append(capitalize(n));
                }
                defaultDomain = sb.toString() + " "+ "Domain";
                name = defaultDomain;
                CMS.debug("SecurityDomainPanel: defaultDomain generated:"+ name);
            } catch (MalformedURLException e) {
                errorString = "Malformed URL";
                // not being able to come up with default domain name is ok
            }
        }
        context.put("sdomainName", name);

        if( default_admin_url != null ) {
            String r = null;

            try {
                // check to see if "default" security domain exists
                // on local machine
                URL u = new URL( default_admin_url );

                String hostname = u.getHost();
                int port = u.getPort();
                ConfigCertApprovalCallback
                certApprovalCallback = new ConfigCertApprovalCallback();
                r = pingCS( hostname, port, true, certApprovalCallback );
            } catch (Exception e) {
                CMS.debug( "SecurityDomainPanel: exception caught: "
                         + e.toString() );
            }
 
            if( r != null ) {
                // "default" security domain exists on local machine;
                // fill "sdomainURL" in with "default" security domain
                // as an initial "guess"
                CMS.debug( "SecurityDomainPanel: pingCS returns: "+r );
                context.put( "sdomainURL", default_admin_url );
            } else {
                // "default" security domain does NOT exist on local machine;
                // leave "sdomainURL" blank
                CMS.debug( "SecurityDomainPanel: pingCS no successful response" );
                context.put( "sdomainURL", "" );
            }
        }

        // Information for "existing" Security Domain CAs
        String initDaemon = "pki-cad";
        String instanceId = "<security_domain_instance_name>";
        String os = System.getProperty( "os.name" );
        if( os.equalsIgnoreCase( "Linux" ) ) {
            if (! systemdService.equals("")) {
                context.put( "initCommand", "/usr/bin/pkicontrol" );
                context.put( "instanceId", "ca " + 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 );
        }
    }

    public static String capitalize(String s) {
        if (s.length() == 0) {
            return s;
        } else {
            return s.substring(0,1).toUpperCase() + s.substring(1);
        }
    }

    /**
     * Checks if the given parameters are valid.
     */
    public void validate(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
       
        String select = HttpInput.getID(request, "choice");
        if (select.equals("newdomain")) {
            String name = HttpInput.getSecurityDomainName(request, "sdomainName");
            if (name == null || name.equals("")) {
                initParams(request, context);
                context.put("updateStatus", "validate-failure");
                throw new IOException("Missing name value for the security domain");
            }
        } else if (select.equals("existingdomain")) {
            CMS.debug( "SecurityDomainPanel: validating "
                     + "SSL Admin HTTPS . . ." );
            String admin_url = HttpInput.getURL( request, "sdomainURL" );
            if( admin_url == null || admin_url.equals("") ) {
                initParams( request, context );
                context.put("updateStatus", "validate-failure");
                throw new IOException( "Missing SSL Admin HTTPS url value "
                                     + "for the security domain" );
            } else {
                String r = null;

                try {
                    URL u = new URL( admin_url );

                    String hostname = u.getHost();
                    int admin_port = u.getPort();
                    ConfigCertApprovalCallback
                    certApprovalCallback = new ConfigCertApprovalCallback();
                    r = pingCS( hostname, admin_port, true,
                                certApprovalCallback );
                } catch( Exception e ) {
                    CMS.debug( "SecurityDomainPanel: exception caught: "
                             + e.toString() );
                    context.put("updateStatus", "validate-failure");
                    throw new IOException( "Illegal SSL Admin HTTPS url value "
                                         + "for the security domain" );
                }

                if (r != null) {
                    CMS.debug("SecurityDomainPanel: pingAdminCS returns: "
                             + r );
                    context.put( "sdomainURL", admin_url );
                } else {
                    CMS.debug( "SecurityDomainPanel: pingAdminCS "
                             + "no successful response for SSL Admin HTTPS" );
                    context.put( "sdomainURL", "" );
                }
            }
        }
    }

    public void initParams(HttpServletRequest request, Context context) 
                   throws IOException 
    {
        IConfigStore config = CMS.getConfigStore();
        try {
            context.put("cstype", config.getString("cs.type"));
        } catch (Exception e) {
        }

        String select = request.getParameter("choice");
        if (select.equals("newdomain")) {
            context.put("check_newdomain", "checked");
            context.put("check_existingdomain", "");
        } else if (select.equals("existingdomain")) {
            context.put("check_newdomain", ""); 
            context.put("check_existingdomain", "checked");
        }

        String name = request.getParameter("sdomainName");
        if (name == null)
            name = "";
        context.put("sdomainName", name);

        String admin_url = request.getParameter("sdomainURL");
        if (admin_url == null)
            admin_url = "";
        context.put("sdomainURL", admin_url);
    }

    /**
     * Commit parameter changes
     */
    public void update(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
        String errorString = "";
        String select = HttpInput.getID(request, "choice");

        if (select == null) {
            CMS.debug("SecurityDomainPanel: choice not found");
            context.put("updateStatus", "failure");
            throw new IOException("choice not found");
        }
        IConfigStore config = CMS.getConfigStore();

        if (select.equals("newdomain")) {
            config.putString("preop.securitydomain.select", "new");
            config.putString("securitydomain.select", "new");
            config.putString("preop.securitydomain.name", 
              HttpInput.getDomainName(request, "sdomainName"));
            config.putString("securitydomain.name", 
              HttpInput.getDomainName(request, "sdomainName"));
            config.putString("securitydomain.host", 
              CMS.getEENonSSLHost());
            config.putString("securitydomain.httpport", 
              CMS.getEENonSSLPort());
            config.putString("securitydomain.httpsagentport", 
              CMS.getAgentPort());
            config.putString("securitydomain.httpseeport", 
              CMS.getEESSLPort());
            config.putString("securitydomain.httpsadminport", 
              CMS.getAdminPort());

            // make sure the subsystem certificate is issued by the security  
            // domain
            config.putString("preop.cert.subsystem.type", "local");
            config.putString("preop.cert.subsystem.profile", "subsystemCert.profile");
   
            try {
                config.commit(false);
            } catch (EBaseException e) {}

            String instanceRoot = "";
            try {
                instanceRoot = config.getString("instanceRoot", "");
            } catch (Exception e) {
            }

        } else if (select.equals("existingdomain")) {
            config.putString("preop.securitydomain.select", "existing");
            config.putString("securitydomain.select", "existing");

            // make sure the subsystem certificate is issued by the security
            // domain
            config.putString("preop.cert.subsystem.type", "remote");
            config.putString("preop.cert.subsystem.profile", "caInternalAuthSubsystemCert");

            String admin_url = HttpInput.getURL(request, "sdomainURL");
            String hostname = "";
            int admin_port = -1;

            if( admin_url != null ) {
                try {
                    URL admin_u = new URL( admin_url );

                    hostname = admin_u.getHost();
                    admin_port = admin_u.getPort();
                } catch( MalformedURLException e ) {
                    errorString = "Malformed SSL Admin HTTPS URL";
                    context.put("updateStatus", "failure");
                    throw new IOException( errorString );
                }

                context.put( "sdomainURL", admin_url );
                config.putString( "securitydomain.host", hostname );
                config.putInteger( "securitydomain.httpsadminport",
                                   admin_port );
            }

            try {
                config.commit(false);
            } catch (EBaseException e) {}

            ConfigCertApprovalCallback certApprovalCallback = new ConfigCertApprovalCallback();
            updateCertChain( config, "securitydomain", hostname, admin_port,
                             true, context, certApprovalCallback );
        } else {
            CMS.debug("SecurityDomainPanel: invalid choice " + select);
            errorString = "Invalid choice";
            context.put("updateStatus", "failure");
            throw new IOException("invalid choice " + select);
        }

        try {
            config.commit(false);
        } catch (EBaseException e) {
        }

        try {
            context.put("cstype", config.getString("cs.type"));
            context.put("wizardname", config.getString("preop.wizard.name"));
            context.put("panelname", "Security Domain Configuration");
            context.put("systemname", config.getString("preop.system.name"));
        } catch (EBaseException e) {}

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

    /**
     * If validate() returns false, this method will be called.
     */
    public void displayError(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        IConfigStore config = CMS.getConfigStore();
        String default_admin_url = "";
        try {
            initParams(request, context);
        } catch (IOException e) {
        }

        try {
            default_admin_url = config.getString("preop.securitydomain.admin_url", "");
        } catch (Exception e) {}

        if( default_admin_url != null ) {
            String r = null;

            try {
                // check to see if "default" security domain exists
                // on local machine
                URL u = new URL( default_admin_url );

                String hostname = u.getHost();
                int port = u.getPort();
                ConfigCertApprovalCallback
                certApprovalCallback = new ConfigCertApprovalCallback();
                r = pingCS( hostname, port, true, certApprovalCallback );
            } catch (Exception e) {}
 
            if( r != null ) {
                // "default" security domain exists on local machine;
                // refill "sdomainURL" in with "default" security domain
                // as an initial "guess"
                context.put( "sdomainURL", default_admin_url );
            } else {
                // "default" security domain does NOT exist on local machine;
                // leave "sdomainURL" blank
                context.put( "sdomainURL", "" );
            }
        }

        try {
            context.put("machineName", config.getString("machineName"));
            context.put("http_ee_port", CMS.getEENonSSLPort());
            context.put("https_agent_port", CMS.getAgentPort());
            context.put("https_ee_port", CMS.getEESSLPort());
            context.put("https_admin_port", CMS.getAdminPort());
            context.put("sdomainAdminURL",
                        config.getString("preop.securitydomain.admin_url"));
        } catch (EBaseException e) {}

        // Information for "existing" Security Domain CAs
        String initDaemon = "pki-cad";
        String instanceId = "<security_domain_instance_name>";
        String os = System.getProperty( "os.name" );
        if( os.equalsIgnoreCase( "Linux" ) ) {
            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", "Security Domain");
        context.put("panel", "admin/console/config/securitydomainpanel.vm");
    }
}