001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server;
017:
018: import java.io.*;
019: import java.net.*;
020: import java.util.*;
021:
022: /**
023: * @version $Rev: 494654 $ $Date: 2007-01-09 15:45:36 -0800 $
024: */
025: public class ServiceAccessController implements ServerService {
026:
027: ServerService next;
028:
029: InetAddress[] allowedHosts;
030:
031: public ServiceAccessController(ServerService next) {
032: this .next = next;
033: }
034:
035: public void init(Properties props) throws Exception {
036:
037: parseAdminIPs(props);
038:
039: next.init(props);
040: }
041:
042: public void start() throws ServiceException {
043:
044: next.start();
045: }
046:
047: public void stop() throws ServiceException {
048:
049: next.stop();
050: }
051:
052: public void service(Socket socket) throws ServiceException,
053: IOException {
054:
055: next.service(socket);
056: }
057:
058: public void service(InputStream in, OutputStream out)
059: throws ServiceException, IOException {
060: throw new UnsupportedOperationException("service(in,out)");
061: }
062:
063: public String getName() {
064: return next.getName();
065: }
066:
067: public String getIP() {
068: return next.getIP();
069: }
070:
071: public int getPort() {
072: return next.getPort();
073: }
074:
075: public void checkHostsAuthorization(InetAddress client,
076: InetAddress server) throws SecurityException {
077:
078: boolean authorized = false;
079:
080: authorized = client.equals(server);
081:
082: for (int i = 0; i < allowedHosts.length && !authorized; i++) {
083: authorized = allowedHosts[i].equals(client);
084: }
085:
086: if (!authorized) {
087: throw new SecurityException("Host "
088: + client.getHostAddress()
089: + " is not authorized to access this service.");
090: }
091: }
092:
093: private void parseAdminIPs(Properties props) {
094: try {
095:
096: Vector addresses = new Vector();
097:
098: InetAddress[] localIps = InetAddress
099: .getAllByName("localhost");
100: for (int i = 0; i < localIps.length; i++) {
101: addresses.add(localIps[i]);
102: }
103:
104: String ipString = props.getProperty("only_from");
105: if (ipString != null) {
106: StringTokenizer st = new StringTokenizer(ipString, ",");
107: while (st.hasMoreTokens()) {
108: String address = null;
109: InetAddress ip = null;
110: try {
111: address = st.nextToken();
112: ip = InetAddress.getByName(address);
113: addresses.add(ip);
114: } catch (Exception e) {
115:
116: }
117: }
118: }
119:
120: allowedHosts = new InetAddress[addresses.size()];
121: addresses.copyInto(allowedHosts);
122:
123: } catch (Exception e) {
124:
125: }
126: }
127:
128: }
|