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 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
40
41
42 public class PolicyHelper {
43
44
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
53
54
55 public static void installPolicyOnJVM() {
56
57 Policy runtimePolicy = Policy.getPolicy();
58
59
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
69 try {
70 Class clazz = Class.forName(PolicyHelper.COM_SUN_SECURITY_AUTH_POLICY_FILE);
71
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
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
108
109
110 public static Class findDefaultPolicy(){
111
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 }