[8780cbec] | 1 | import com.nailabs.abac.credential.*; |
---|
| 2 | import com.nailabs.abac.process.*; |
---|
| 3 | import com.nailabs.abac.trust.*; |
---|
| 4 | import com.nailabs.abac.test.*; |
---|
| 5 | import edu.stanford.peer.rbtm.credential.*; |
---|
| 6 | import java.io.*; |
---|
| 7 | import java.text.*; |
---|
| 8 | import java.util.*; |
---|
| 9 | import java.rmi.*; |
---|
| 10 | import javax.servlet.*; |
---|
| 11 | import javax.servlet.http.*; |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | public class AccessMediator implements Filter { |
---|
| 15 | private Entity eid = null; |
---|
| 16 | private EntityExpression target = null; |
---|
| 17 | NegotiatorFactory clientFactory = null; |
---|
| 18 | private FilterConfig conf = null; |
---|
| 19 | Calendar cal = Calendar.getInstance(); |
---|
| 20 | DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL); |
---|
| 21 | |
---|
| 22 | /** maximum no. of rmi lookup retries if a remote exception occurs */ |
---|
| 23 | private int MAX_RETRIES = 1; |
---|
| 24 | |
---|
| 25 | private void getClientFactory(String hostName) { |
---|
| 26 | StringBuffer url = new StringBuffer("//"); |
---|
| 27 | url.append(hostName).append("/"); |
---|
| 28 | url.append("ClientAuthenticationService"); |
---|
| 29 | //url.append("ClientRTMLService"); |
---|
| 30 | System.out.println("Attempting to contact" + url); |
---|
| 31 | try { |
---|
| 32 | clientFactory = |
---|
| 33 | (NegotiatorFactory)Naming.lookup(url.toString()); |
---|
| 34 | } |
---|
| 35 | catch(Exception ex) { |
---|
| 36 | ex.printStackTrace(); |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | private Negotiator getClient(String host, String client, |
---|
| 41 | RMINegotiator me) |
---|
| 42 | { |
---|
| 43 | for(int i = 0; i < MAX_RETRIES; i++) { |
---|
| 44 | try { |
---|
| 45 | return (Negotiator)clientFactory.getNegotiator(client, me); |
---|
| 46 | } |
---|
| 47 | catch(Exception ex) { |
---|
| 48 | ex.printStackTrace(); |
---|
| 49 | getClientFactory(host); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | return null; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** shared configuration hash table */ |
---|
| 56 | HashMap map = null; |
---|
| 57 | |
---|
| 58 | public void init(FilterConfig config) throws ServletException { |
---|
| 59 | String hashCode = config.getInitParameter("EntityHash"); |
---|
| 60 | String shortName = config.getInitParameter("EntityName"); |
---|
| 61 | String cfile = config.getInitParameter("PolicyFile"); |
---|
| 62 | String targetName = config.getInitParameter("PrimaryRole"); |
---|
| 63 | map = FrontierManager.loadConfiguration(cfile); |
---|
| 64 | //map = RtmlTest.loadConfiguration(cfile); |
---|
| 65 | config.getServletContext().log("map = " + map); |
---|
| 66 | conf = config; |
---|
| 67 | conf.getServletContext().log("Loading new filter for " + shortName + |
---|
| 68 | " from " + cfile + " with role " + |
---|
| 69 | targetName + "\n"); |
---|
| 70 | //RtmlFrontier.addFrontier(map); |
---|
| 71 | FrontierManager.addFrontier(map); |
---|
| 72 | //ResourcePolicy resourcePolicy = |
---|
| 73 | // (ResourcePolicy)map.get("ResourcePolicy"); |
---|
| 74 | |
---|
| 75 | try { |
---|
| 76 | //eid = new RtmlEntity(shortName, hashCode); |
---|
| 77 | //target = resourcePolicy.requires(targetName); |
---|
| 78 | eid = new SimpleEntity(shortName); |
---|
| 79 | target = StaticCredential.getRole(targetName); |
---|
| 80 | System.out.println("Frontiers: " + |
---|
| 81 | FrontierManager.getFrontiers().toString()); |
---|
| 82 | //Iterator i = FrontierManager.getFrontiers().iterator(); |
---|
| 83 | } |
---|
| 84 | catch(Exception ex) { |
---|
| 85 | ex.printStackTrace(); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public void destroy() { } |
---|
| 90 | |
---|
| 91 | public RtmlEntity getEntityId(HttpServletRequest request) { |
---|
| 92 | final String KEY = "EntityID"; |
---|
| 93 | final String HASH = "HashID"; |
---|
| 94 | String shortName = null; |
---|
| 95 | String hashCode = null; |
---|
| 96 | HttpSession session = request.getSession(); |
---|
| 97 | |
---|
| 98 | shortName = request.getParameter("id"); |
---|
| 99 | hashCode = request.getParameter("hash"); |
---|
| 100 | if(session.isNew()) { // new id bootstrapped |
---|
| 101 | session.setAttribute(KEY, shortName); |
---|
| 102 | } else if(shortName != null) { // forcing a new id manually |
---|
| 103 | session.invalidate(); |
---|
| 104 | session = request.getSession(); |
---|
| 105 | session.setAttribute(KEY, shortName); |
---|
| 106 | session.setAttribute(HASH, hashCode); |
---|
| 107 | } else { // use the session's entity id value |
---|
| 108 | shortName = (String)session.getAttribute(KEY); |
---|
| 109 | hashCode = (String)session.getAttribute(HASH); |
---|
| 110 | } |
---|
| 111 | try { |
---|
| 112 | System.out.println("Using RtmlEntity = " + shortName + "(" + |
---|
| 113 | hashCode + ")"); |
---|
| 114 | return new RtmlEntity(shortName, hashCode); |
---|
| 115 | } catch(Exception ex) { |
---|
| 116 | ex.printStackTrace(); |
---|
| 117 | } |
---|
| 118 | return null; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | public Entity getEntity(HttpServletRequest request) { |
---|
| 122 | final String KEY = "EntityID"; |
---|
| 123 | String shortName = null; |
---|
| 124 | HttpSession session = request.getSession(); |
---|
| 125 | |
---|
| 126 | shortName = request.getParameter("id"); |
---|
| 127 | if(session.isNew()) { // new id bootstrapped |
---|
| 128 | session.setAttribute(KEY, shortName); |
---|
| 129 | } else if(shortName != null) { // forcing a new id manually |
---|
| 130 | session.invalidate(); |
---|
| 131 | session = request.getSession(); |
---|
| 132 | session.setAttribute(KEY, shortName); |
---|
| 133 | } else { // use the session's entity id value |
---|
| 134 | shortName = (String)session.getAttribute(KEY); |
---|
| 135 | } |
---|
| 136 | try { |
---|
| 137 | System.out.println("Using SimpleEntity = " + shortName); |
---|
| 138 | SimpleEntity id = new SimpleEntity(shortName); |
---|
| 139 | return (Entity)id; |
---|
| 140 | } catch(Exception ex) { |
---|
| 141 | ex.printStackTrace(); |
---|
| 142 | } |
---|
| 143 | return null; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | public void doFilter(ServletRequest request, ServletResponse response, |
---|
| 147 | FilterChain chain) |
---|
| 148 | throws java.io.IOException, ServletException { |
---|
| 149 | Entity peer = getEntity((HttpServletRequest)request); |
---|
| 150 | //RtmlEntity peer = getEntityId((HttpServletRequest)request); |
---|
| 151 | System.out.print("Filtering request from id = " + peer ); |
---|
| 152 | System.out.println(" for resource of " + eid.toString()); |
---|
| 153 | HashMap localMap = (HashMap)map.clone(); |
---|
| 154 | localMap.put("peer", peer); |
---|
| 155 | NegotiationContext context = new NegotiationContext(map); |
---|
| 156 | boolean success = false; |
---|
| 157 | |
---|
| 158 | if(peer == null) { |
---|
| 159 | printMessage(response.getWriter(), |
---|
| 160 | "<h1>Authentication required!</h1>"); |
---|
| 161 | return; |
---|
| 162 | } |
---|
| 163 | try { |
---|
| 164 | String host = request.getRemoteHost(); |
---|
| 165 | RMINegotiator agent = new RMINegotiator(context); |
---|
| 166 | //context.getGraph().addObserver(observer); |
---|
| 167 | Negotiator peerAgent = getClient(host, peer.toString(), agent); |
---|
| 168 | for(int i = 0; i < 4 && peerAgent == null; i++) { |
---|
| 169 | peerAgent = getClient(host, peer.toString(), agent); |
---|
| 170 | } |
---|
| 171 | Entity realPeer = peerAgent.getSelf(); |
---|
| 172 | TrustTarget primary = new TrustTarget(eid, target, realPeer); |
---|
| 173 | agent.setPeer(peerAgent); |
---|
| 174 | agent.setRoot(primary); |
---|
| 175 | success = agent.negotiate(); |
---|
| 176 | } |
---|
| 177 | catch(Exception ex) { |
---|
| 178 | ex.printStackTrace(); |
---|
| 179 | } |
---|
| 180 | // allow or block this servlet |
---|
| 181 | if(!success) { |
---|
| 182 | printMessage(response.getWriter(), |
---|
| 183 | "<h1>Sorry, page cannot be displayed!</h1>"); |
---|
| 184 | return; |
---|
| 185 | } |
---|
| 186 | chain.doFilter(request, response); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | protected void printExpiration(PrintWriter out) { |
---|
| 191 | out.print("<meta http-equiv=\"Expires\" content=\""); |
---|
| 192 | out.print(formatter.format(cal.getTime())); |
---|
| 193 | out.println("\">"); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | protected void printMessage(PrintWriter out, String body) { |
---|
| 197 | out.print("<html>\n\t<head>\n\t"); |
---|
| 198 | printExpiration(out); |
---|
| 199 | out.println("\t</head>\n\t<body>"); |
---|
| 200 | out.println(body); |
---|
| 201 | out.println("\t</body>\n</html>"); |
---|
| 202 | out.flush(); |
---|
| 203 | return; |
---|
| 204 | |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | |
---|