1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.security;
18
19 import java.security.Security;
20
21 import org.apache.catalina.startup.CatalinaProperties;
22 import org.apache.juli.logging.Log;
23 import org.apache.juli.logging.LogFactory;
24
25 /**
26  * Util class to protect Catalina against package access and insertion.
27  * The code are been moved from Catalina.java
28  * @author the Catalina.java authors
29  */

30 public final class SecurityConfig{
31
32     private static final Object singletonLock = new Object();
33     private static volatile SecurityConfig singleton = null;
34
35     private static final Log log = LogFactory.getLog(SecurityConfig.class);
36
37
38     private static final String PACKAGE_ACCESS =  "sun.,"
39                                                 + "org.apache.catalina."
40                                                 + ",org.apache.jasper."
41                                                 + ",org.apache.coyote."
42                                                 + ",org.apache.tomcat.";
43
44     // FIX ME package "javax." was removed to prevent HotSpot
45     // fatal internal errors
46     private static final String PACKAGE_DEFINITION= "java.,sun."
47                                                 + ",org.apache.catalina."
48                                                 + ",org.apache.coyote."
49                                                 + ",org.apache.tomcat."
50                                                 + ",org.apache.jasper.";
51     /**
52      * List of protected package from conf/catalina.properties
53      */

54     private final String packageDefinition;
55
56
57     /**
58      * List of protected package from conf/catalina.properties
59      */

60     private final String packageAccess;
61
62
63     /**
64      * Create a single instance of this class.
65      */

66     private SecurityConfig() {
67         String definition = null;
68         String access = null;
69         try{
70             definition = CatalinaProperties.getProperty("package.definition");
71             access = CatalinaProperties.getProperty("package.access");
72         } catch (java.lang.Exception ex){
73             if (log.isDebugEnabled()){
74                 log.debug("Unable to load properties using CatalinaProperties", ex);
75             }
76         } finally {
77             packageDefinition = definition;
78             packageAccess = access;
79         }
80     }
81
82
83     /**
84      * Returns the singleton instance of that class.
85      * @return an instance of that class.
86      */

87     public static SecurityConfig newInstance(){
88         if (singleton == null) {
89             synchronized (singletonLock) {
90                 if (singleton == null) {
91                     singleton = new SecurityConfig();
92                 }
93             }
94         }
95         return singleton;
96     }
97
98
99     /**
100      * Set the security package.access value.
101      */

102     public void setPackageAccess(){
103         // If catalina.properties is missing, protect all by default.
104         if (packageAccess == null){
105             setSecurityProperty("package.access", PACKAGE_ACCESS);
106         } else {
107             setSecurityProperty("package.access", packageAccess);
108         }
109     }
110
111
112     /**
113      * Set the security package.definition value.
114      */

115      public void setPackageDefinition(){
116         // If catalina.properties is missing, protect all by default.
117          if (packageDefinition == null){
118             setSecurityProperty("package.definition", PACKAGE_DEFINITION);
119          } else {
120             setSecurityProperty("package.definition", packageDefinition);
121          }
122     }
123
124
125     /**
126      * Set the proper security property
127      * @param properties the package.* property.
128      */

129     private final void setSecurityProperty(String properties, String packageList){
130         if (System.getSecurityManager() != null){
131             String definition = Security.getProperty(properties);
132             if( definition != null && definition.length() > 0 ){
133                 if (packageList.length() > 0) {
134                     definition = definition + ',' + packageList;
135                 }
136             } else {
137                 definition = packageList;
138             }
139
140             Security.setProperty(properties, definition);
141         }
142     }
143
144
145 }
146
147
148