Example usage for java.net SocketPermission SocketPermission

List of usage examples for java.net SocketPermission SocketPermission

Introduction

In this page you can find the example usage for java.net SocketPermission SocketPermission.

Prototype

SocketPermission(String host, int mask) 

Source Link

Usage

From source file:AccessTest.java

public static void main(String[] a) {
    SocketPermission sp = new SocketPermission("www.java2s.com", "connect");
    try {//from w  w  w.j  a v a 2s. c  o m
        AccessController.checkPermission(sp);
        System.out.println("Ok to open socket");
    } catch (AccessControlException ace) {
        System.out.println(ace);
    }
}

From source file:org.wildfly.test.manual.elytron.seccontext.AbstractSecurityContextPropagationTestBase.java

/**
 * Creates deployment with Entry bean - to be placed on the first server.
 *//*from  w  w  w.j  a  va 2  s.c  om*/
@Deployment(name = JAR_ENTRY_EJB, managed = false, testable = false)
@TargetsContainer(SERVER1)
public static Archive<?> createEntryBeanDeployment() {
    return ShrinkWrap.create(JavaArchive.class, JAR_ENTRY_EJB + ".jar")
            .addClasses(EntryBean.class, EntryBeanSFSB.class, Entry.class, WhoAmI.class, ReAuthnType.class,
                    SeccontextUtil.class, CallAnotherBeanInfo.class)
            .addAsManifestResource(createPermissionsXmlAsset(new ElytronPermission("authenticate"),
                    new ElytronPermission("getPrivateCredentials"), new ElytronPermission("getSecurityDomain"),
                    new SocketPermission(TestSuiteEnvironment.getServerAddressNode1() + ":8180",
                            "connect,resolve")),
                    "permissions.xml")
            .addAsManifestResource(Utils.getJBossEjb3XmlAsset("seccontext-entry"), "jboss-ejb3.xml");
}

From source file:com.sshtools.daemon.forwarding.ForwardingServer.java

/**
 *
 *
 * @param addressToBind/*from  www.  j a v  a  2 s .  co m*/
 * @param portToBind
 *
 * @throws ForwardingConfigurationException
 */
protected void addRemoteForwardingConfiguration(String addressToBind, int portToBind)
        throws ForwardingConfigurationException {
    // Is the server already listening
    Iterator it = remoteForwardings.iterator();
    ForwardingConfiguration config;

    while (it.hasNext()) {
        config = (ForwardingConfiguration) it.next();

        if (config.getAddressToBind().equals(addressToBind) && (config.getPortToBind() == portToBind)) {
            throw new ForwardingConfigurationException("The address and port are already in use!");
        }
    }

    config = new ForwardingConfiguration(addressToBind, portToBind);

    // Check the security mananger
    SecurityManager manager = System.getSecurityManager();

    if (manager != null) {
        try {
            manager.checkPermission(
                    new SocketPermission(addressToBind + ":" + String.valueOf(portToBind), "accept,listen"));
        } catch (SecurityException e) {
            throw new ForwardingConfigurationException("The security manager has denied listen permision on "
                    + addressToBind + ":" + String.valueOf(portToBind));
        }
    }

    try {
        ForwardingListener listener = new ServerForwardingListener(connection, addressToBind, portToBind);
        remoteForwardings.add(listener);
        listener.start();
    } catch (IOException ex) {
        throw new ForwardingConfigurationException(ex.getMessage());
    }
}

From source file:com.sshtools.j2ssh.forwarding.ForwardingClient.java

/**
 *
 *
 * @param uniqueName//from   w  w  w  .  j  a v a2  s .  co m
 * @param addressToBind
 * @param portToBind
 * @param hostToConnect
 * @param portToConnect
 *
 * @return
 *
 * @throws ForwardingConfigurationException
 */
public ForwardingConfiguration addLocalForwarding(String uniqueName, String addressToBind, int portToBind,
        String hostToConnect, int portToConnect) throws ForwardingConfigurationException {
    // Check that the name does not exist
    if (localForwardings.containsKey(uniqueName)) {
        throw new ForwardingConfigurationException("The configuration name already exists!");
    }

    // Check that the address to bind and port are not already being used
    Iterator it = localForwardings.values().iterator();
    ForwardingConfiguration config;

    while (it.hasNext()) {
        config = (ForwardingConfiguration) it.next();

        if (config.getAddressToBind().equals(addressToBind) && (config.getPortToBind() == portToBind)) {
            throw new ForwardingConfigurationException("The address and port are already in use");
        }
    }

    // Check the security mananger
    SecurityManager manager = System.getSecurityManager();

    if (manager != null) {
        try {
            manager.checkPermission(
                    new SocketPermission(addressToBind + ":" + String.valueOf(portToBind), "accept,listen"));
        } catch (SecurityException e) {
            throw new ForwardingConfigurationException("The security manager has denied listen permision on "
                    + addressToBind + ":" + String.valueOf(portToBind));
        }
    }

    // Create the configuration object
    ForwardingConfiguration cf = new ClientForwardingListener(uniqueName, connection, addressToBind, portToBind,
            hostToConnect, portToConnect);

    localForwardings.put(uniqueName, cf);

    return cf;
}

From source file:com.sshtools.j2ssh.forwarding.ForwardingClient.java

/**
 *
 *
 * @param uniqueName//from w  w  w .  jav  a  2  s.c  om
 * @param addressToBind
 * @param portToBind
 * @param hostToConnect
 * @param portToConnect
 *
 * @throws ForwardingConfigurationException
 */
public ForwardingConfiguration addRemoteForwarding(String uniqueName, String addressToBind, int portToBind,
        String hostToConnect, int portToConnect) throws ForwardingConfigurationException {
    // Check that the name does not exist
    if (remoteForwardings.containsKey(uniqueName)) {
        throw new ForwardingConfigurationException("The remote forwaring configuration name already exists!");
    }

    // Check that the address to bind and port are not already being used
    Iterator it = remoteForwardings.values().iterator();
    ForwardingConfiguration config;

    while (it.hasNext()) {
        config = (ForwardingConfiguration) it.next();

        if (config.getAddressToBind().equals(addressToBind) && (config.getPortToBind() == portToBind)) {
            throw new ForwardingConfigurationException(
                    "The remote forwarding address and port are already in use");
        }
    }

    // Check the security mananger
    SecurityManager manager = System.getSecurityManager();

    if (manager != null) {
        try {
            manager.checkPermission(
                    new SocketPermission(hostToConnect + ":" + String.valueOf(portToConnect), "connect"));
        } catch (SecurityException e) {
            throw new ForwardingConfigurationException("The security manager has denied connect permision on "
                    + hostToConnect + ":" + String.valueOf(portToConnect));
        }
    }

    // Create the configuration object
    ForwardingConfiguration cf = new ForwardingConfiguration(uniqueName, addressToBind, portToBind,
            hostToConnect, portToConnect);
    remoteForwardings.put(uniqueName, cf);
    return cf;
}

From source file:com.sshtools.j2ssh.forwarding.ForwardingClient.java

/**
 *
 *
 * @param fwd//from   w ww  . j a v a  2 s .c  o  m
 *
 * @throws ForwardingConfigurationException
 */
public void addRemoteForwarding(ForwardingConfiguration fwd) throws ForwardingConfigurationException {
    // Check that the name does not exist
    if (remoteForwardings.containsKey(fwd.getName())) {
        throw new ForwardingConfigurationException("The remote forwaring configuration name already exists!");
    }

    // Check that the address to bind and port are not already being used
    Iterator it = remoteForwardings.values().iterator();
    ForwardingConfiguration config;

    while (it.hasNext()) {
        config = (ForwardingConfiguration) it.next();

        if (config.getAddressToBind().equals(fwd.getAddressToBind())
                && (config.getPortToBind() == fwd.getPortToBind())) {
            throw new ForwardingConfigurationException(
                    "The remote forwarding address and port are already in use");
        }
    }

    // Check the security mananger
    SecurityManager manager = System.getSecurityManager();

    if (manager != null) {
        try {
            manager.checkPermission(new SocketPermission(
                    fwd.getHostToConnect() + ":" + String.valueOf(fwd.getPortToConnect()), "connect"));
        } catch (SecurityException e) {
            throw new ForwardingConfigurationException("The security manager has denied connect permision on "
                    + fwd.getHostToConnect() + ":" + String.valueOf(fwd.getPortToConnect()));
        }
    }

    // Create the configuration object
    remoteForwardings.put(fwd.getName(), fwd);
}

From source file:org.wildfly.test.manual.elytron.seccontext.AbstractSecurityContextPropagationTestBase.java

/**
 * Creates deployment base with Entry servlet. It doesn't contain web.xml and related resources if needed (e.g. login page).
 */// w ww  . j av  a2s  .  c om
private static WebArchive createEntryServletDeploymentBase(String name) {
    return ShrinkWrap.create(WebArchive.class, name + ".war")
            .addClasses(EntryServlet.class, WhoAmIServlet.class, WhoAmI.class, ReAuthnType.class,
                    SeccontextUtil.class)
            .addAsManifestResource(createPermissionsXmlAsset(new ElytronPermission("authenticate"),
                    new ElytronPermission("getPrivateCredentials"), new ElytronPermission("getSecurityDomain"),
                    new SocketPermission(TestSuiteEnvironment.getServerAddressNode1() + ":8180",
                            "connect,resolve")),
                    "permissions.xml")
            .addAsWebInfResource(Utils.getJBossWebXmlAsset("seccontext-web"), "jboss-web.xml");
}

From source file:org.echocat.nodoodle.classloading.FileClassLoader.java

/**
 * This is a copy of {@link URLClassLoader#getPermissions(CodeSource)}.
 *
 * Returns the permissions for the given codesource object.
 * The implementation of this method first calls super.getPermissions
 * and then adds permissions based on the URL of the codesource.
 * <p>//from   ww w . j  a va 2 s  .  co m
 * If the protocol of this URL is "jar", then the permission granted
 * is based on the permission that is required by the URL of the Jar
 * file.
 * <p>
 * If the protocol is "file"
 * and the path specifies a file, then permission to read that
 * file is granted. If protocol is "file" and the path is
 * a directory, permission is granted to read all files
 * and (recursively) all files and subdirectories contained in
 * that directory.
 * <p>
 * If the protocol is not "file", then
 * to connect to and accept connections from the URL's host is granted.
 * @param codesource the codesource
 * @return the permissions granted to the codesource
 */
@Override
protected PermissionCollection getPermissions(CodeSource codesource) {
    final PermissionCollection perms = super.getPermissions(codesource);
    final URL url = codesource.getLocation();
    Permission p;
    URLConnection urlConnection;
    try {
        urlConnection = url.openConnection();
        p = urlConnection.getPermission();
    } catch (IOException ignored) {
        p = null;
        urlConnection = null;
    }
    if (p instanceof FilePermission) {
        // if the permission has a separator char on the end,
        // it means the codebase is a directory, and we need
        // to add an additional permission to read recursively
        String path = p.getName();
        if (path.endsWith(File.separator)) {
            path += "-";
            p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
        }
    } else if ((p == null) && (url.getProtocol().equals("file"))) {
        String path = url.getFile().replace('/', File.separatorChar);
        path = ParseUtil.decode(path);
        if (path.endsWith(File.separator)) {
            path += "-";
        }
        p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
    } else {
        URL locUrl = url;
        if (urlConnection instanceof JarURLConnection) {
            locUrl = ((JarURLConnection) urlConnection).getJarFileURL();
        }
        final String host = locUrl.getHost();
        if (host != null && (host.length() > 0)) {
            p = new SocketPermission(host, SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
        }
    }
    // make sure the person that created this class loader
    // would have this permission

    if (p != null) {
        final SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            final Permission fp = p;
            doPrivileged(new PrivilegedAction<Void>() {
                @Override
                public Void run() throws SecurityException {
                    sm.checkPermission(fp);
                    return null;
                }
            }, _acc);
        }
        perms.add(p);
    }
    return perms;
}

From source file:org.jboss.as.test.integration.naming.ldap.LdapUrlInSearchBaseTestCase.java

/**
 * Creates {@link WebArchive} with the {@link LdapUrlTestServlet}.
 *
 * @return// w ww .  j a  v  a 2s . co m
 */
@Deployment
public static WebArchive deployment() {
    final WebArchive war = ShrinkWrap.create(WebArchive.class, "ldap-test.war");
    war.addClasses(LdapUrlTestServlet.class);

    war.addAsManifestResource(createPermissionsXmlAsset(new SocketPermission("*:10389", "connect,resolve")),
            "permissions.xml");

    return war;
}