View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26  
27  */
28  package net.sf.jguard.core.authorization.policy;
29  
30  import java.io.File;
31  import java.security.Policy;
32  import java.util.Enumeration;
33  import java.util.Properties;
34  import java.util.logging.Level;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * utility class to deal with the {@link java.security.Policy} class.
40   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
41   */
42  public class PolicyHelper {
43  
44  	//well-known java policies
45  	public static final String GNU_JAVA_SECURITY_POLICY_FILE = "gnu.java.security.PolicyFile";
46  	private static final String COM_SUN_SECURITY_AUTH_POLICY_FILE = "com.sun.security.auth.PolicyFile";
47  	public static final String SUN_SECURITY_PROVIDER_POLICY_FILE = "sun.security.provider.PolicyFile";
48  
49  	private static Logger logger = LoggerFactory.getLogger(PolicyHelper.class.getName());
50  
51  	/**
52  	 * install the jGuardPolicy if the default policy of the platform is not
53  	 * a jGuardPolicy instance.
54  	 */
55  	public static void installPolicyOnJVM() {
56  
57  		     Policy runtimePolicy = Policy.getPolicy();
58  
59  	         //the jGuard Policy is not set as the policy provider
60  	        if(!(runtimePolicy.getClass().getName().equals(MultipleAppPolicy.class.getName()))){
61  
62  	        	logger.info("init() -  JGuardPolicy is not set as the policy provider . the actual policy provider is '"+ runtimePolicy.getClass().getName()+"' which is different of '"+MultipleAppPolicy.class.getName()+"' ");
63  	        	logger.info("init() -  if you want the jGuard policy 'governs' all java applications (one choice among others described in the jGuard documentation),");
64  	        	logger.info("init() -  please correct the 'policy.provider' property (policy.provider=net.sf.jguard.core.JGuardPolicy) in  your 'java.security' file,");
65  	        	logger.info("init() -  located in this directory: "+ System.getProperty("java.home") + File.separator+ "lib"+ File.separator + "security"+ File.separator);
66  
67  	            try {
68  	               //we set the old policy to the Sun's Policy implementation
69  	            	try {
70  						Class clazz = Class.forName(PolicyHelper.COM_SUN_SECURITY_AUTH_POLICY_FILE);
71  						//we have tested that the com.sun.security.auth.PolicyFile is reachable
72  						javax.security.auth.Policy.setPolicy((javax.security.auth.Policy)clazz.newInstance());
73  					} catch (ClassNotFoundException e) {
74  						logger.warn("com.sun.security.auth.PolicyFile is not reachable.\n we cannot set the old javax.security.auth.Policy implementation to it\n "+e.getMessage());
75  					}
76  
77  					//give to the new JGuardPolicy the old Policy instance
78  	            	Policy.setPolicy(new MultipleAppPolicy(Policy.getPolicy()));
79  
80  				} catch (InstantiationException e) {
81  					logger.error("init() -  Policy Implementation cannot be instantiated : InstantiationException"+e.getMessage(),e);
82  				} catch (IllegalAccessException e) {
83  					logger.error("init() -  Policy Implementation cannot be accessed : IllegalAccessException"+e.getMessage(),e);
84  				}catch(SecurityException e){
85  					logger.error("init() -  Policy Implementation cannot be defined : SecurityException . you haven't got the right to set the java policy"+e.getMessage(),e);
86  	            }
87  	        }
88  
89  	        try{
90  
91  	        	  logger.debug("System properties : \n");
92  				  Properties props = System.getProperties();
93  				  Enumeration enumeration = props.keys();
94  				  while(enumeration.hasMoreElements()){
95  					  String key = (String)enumeration.nextElement();
96  					  String value = (String)props.get(key);
97  					  logger.debug(key+"="+value);
98  				  }
99  
100 			}catch(SecurityException sex){
101 				logger.warn("you have not the permission to grab system properties ");
102 			}
103 
104 	}
105 
106 	 /**
107 	    * discover the default policy installed on the running platform.
108 	    * @return defaultPolicy class
109 	    */
110 	 public static Class findDefaultPolicy(){
111 	     //known default policies class => do you know other java.lang.security.Policy implementations?
112 	     String[] policies = {PolicyHelper.SUN_SECURITY_PROVIDER_POLICY_FILE,PolicyHelper.GNU_JAVA_SECURITY_POLICY_FILE};
113 	     Class defaultPolicyClass = null;
114 	     for(int i = 0; i< policies.length;i++){
115 	         try {
116 	             defaultPolicyClass = Class.forName(policies[i]);
117 	        } catch (ClassNotFoundException e) {
118 	        	logger.debug("findDefaultPolicy() - " + policies[i]+ " is not the defaultPolicy class ");
119 	            continue;
120 	        }
121 	        logger.debug("findDefaultPolicy() - " + policies[i]+ " is the defaultPolicy class ");
122 	        break;
123 	     }
124 	     if(null == defaultPolicyClass){
125 	    	 logger.debug("findDefaultPolicy() -  no defaultPolicy class has been found ");
126 	     }
127 	     return defaultPolicyClass;
128 	 }
129 
130 
131 }