src/java.base/share/classes/java/net/SocketPermission.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 53018 8bf9268df0e2
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   148  */
   148  */
   149 
   149 
   150 public final class SocketPermission extends Permission
   150 public final class SocketPermission extends Permission
   151     implements java.io.Serializable
   151     implements java.io.Serializable
   152 {
   152 {
       
   153     @java.io.Serial
   153     private static final long serialVersionUID = -7204263841984476862L;
   154     private static final long serialVersionUID = -7204263841984476862L;
   154 
   155 
   155     /**
   156     /**
   156      * Connect to host:port
   157      * Connect to host:port
   157      */
   158      */
   284      * </pre>
   285      * </pre>
   285      *
   286      *
   286      * @param host the hostname or IP address of the computer, optionally
   287      * @param host the hostname or IP address of the computer, optionally
   287      * including a colon followed by a port or port range.
   288      * including a colon followed by a port or port range.
   288      * @param action the action string.
   289      * @param action the action string.
       
   290      *
       
   291      * @throws NullPointerException if any parameters are null
       
   292      * @throws IllegalArgumentException if the format of {@code host} is
       
   293      *         invalid, or if the {@code action} string is empty, malformed, or
       
   294      *         contains an action other than the specified possible actions
   289      */
   295      */
   290     public SocketPermission(String host, String action) {
   296     public SocketPermission(String host, String action) {
   291         super(getHost(host));
   297         super(getHost(host));
   292         // name initialized to getHost(host); NPE detected in getHost()
   298         // name initialized to getHost(host); NPE detected in getHost()
   293         init(getName(), getMask(action));
   299         init(getName(), getMask(action));
   586 
   592 
   587             // make sure we didn't just match the tail of a word
   593             // make sure we didn't just match the tail of a word
   588             // like "ackbarfaccept".  Also, skip to the comma.
   594             // like "ackbarfaccept".  Also, skip to the comma.
   589             boolean seencomma = false;
   595             boolean seencomma = false;
   590             while (i >= matchlen && !seencomma) {
   596             while (i >= matchlen && !seencomma) {
   591                 switch(a[i-matchlen]) {
   597                 switch (c = a[i-matchlen]) {
   592                 case ',':
       
   593                     seencomma = true;
       
   594                     break;
       
   595                 case ' ': case '\r': case '\n':
   598                 case ' ': case '\r': case '\n':
   596                 case '\f': case '\t':
   599                 case '\f': case '\t':
   597                     break;
   600                     break;
   598                 default:
   601                 default:
       
   602                     if (c == ',' && i > matchlen) {
       
   603                         seencomma = true;
       
   604                         break;
       
   605                     }
   599                     throw new IllegalArgumentException(
   606                     throw new IllegalArgumentException(
   600                             "invalid permission: " + action);
   607                             "invalid permission: " + action);
   601                 }
   608                 }
   602                 i--;
   609                 i--;
   603             }
   610             }
  1184     /**
  1191     /**
  1185      * WriteObject is called to save the state of the SocketPermission
  1192      * WriteObject is called to save the state of the SocketPermission
  1186      * to a stream. The actions are serialized, and the superclass
  1193      * to a stream. The actions are serialized, and the superclass
  1187      * takes care of the name.
  1194      * takes care of the name.
  1188      */
  1195      */
       
  1196     @java.io.Serial
  1189     private synchronized void writeObject(java.io.ObjectOutputStream s)
  1197     private synchronized void writeObject(java.io.ObjectOutputStream s)
  1190         throws IOException
  1198         throws IOException
  1191     {
  1199     {
  1192         // Write out the actions. The superclass takes care of the name
  1200         // Write out the actions. The superclass takes care of the name
  1193         // call getActions to make sure actions field is initialized
  1201         // call getActions to make sure actions field is initialized
  1198 
  1206 
  1199     /**
  1207     /**
  1200      * readObject is called to restore the state of the SocketPermission from
  1208      * readObject is called to restore the state of the SocketPermission from
  1201      * a stream.
  1209      * a stream.
  1202      */
  1210      */
       
  1211     @java.io.Serial
  1203     private synchronized void readObject(java.io.ObjectInputStream s)
  1212     private synchronized void readObject(java.io.ObjectInputStream s)
  1204          throws IOException, ClassNotFoundException
  1213          throws IOException, ClassNotFoundException
  1205     {
  1214     {
  1206         // Read in the action, then initialize the rest
  1215         // Read in the action, then initialize the rest
  1207         s.defaultReadObject();
  1216         s.defaultReadObject();
  1356      * Adds a permission to the SocketPermissions. The key for the hash is
  1365      * Adds a permission to the SocketPermissions. The key for the hash is
  1357      * the name in the case of wildcards, or all the IP addresses.
  1366      * the name in the case of wildcards, or all the IP addresses.
  1358      *
  1367      *
  1359      * @param permission the Permission object to add.
  1368      * @param permission the Permission object to add.
  1360      *
  1369      *
  1361      * @exception IllegalArgumentException - if the permission is not a
  1370      * @throws    IllegalArgumentException   if the permission is not a
  1362      *                                       SocketPermission
  1371      *                                       SocketPermission
  1363      *
  1372      *
  1364      * @exception SecurityException - if this SocketPermissionCollection object
  1373      * @throws    SecurityException   if this SocketPermissionCollection object
  1365      *                                has been marked readonly
  1374      *                                has been marked readonly
  1366      */
  1375      */
  1367     @Override
  1376     @Override
  1368     public void add(Permission permission) {
  1377     public void add(Permission permission) {
  1369         if (! (permission instanceof SocketPermission))
  1378         if (! (permission instanceof SocketPermission))
  1428             if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
  1437             if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
  1429                 effective |=  x.getMask();
  1438                 effective |=  x.getMask();
  1430                 if ((effective & desired) == desired) {
  1439                 if ((effective & desired) == desired) {
  1431                     return true;
  1440                     return true;
  1432                 }
  1441                 }
  1433                 needed = (desired ^ effective);
  1442                 needed = (desired & ~effective);
  1434             }
  1443             }
  1435         }
  1444         }
  1436         return false;
  1445         return false;
  1437     }
  1446     }
  1438 
  1447 
  1446     @SuppressWarnings("unchecked")
  1455     @SuppressWarnings("unchecked")
  1447     public Enumeration<Permission> elements() {
  1456     public Enumeration<Permission> elements() {
  1448         return (Enumeration)Collections.enumeration(perms.values());
  1457         return (Enumeration)Collections.enumeration(perms.values());
  1449     }
  1458     }
  1450 
  1459 
       
  1460     @java.io.Serial
  1451     private static final long serialVersionUID = 2787186408602843674L;
  1461     private static final long serialVersionUID = 2787186408602843674L;
  1452 
  1462 
  1453     // Need to maintain serialization interoperability with earlier releases,
  1463     // Need to maintain serialization interoperability with earlier releases,
  1454     // which had the serializable field:
  1464     // which had the serializable field:
  1455 
  1465 
  1461 
  1471 
  1462     /**
  1472     /**
  1463      * @serialField permissions java.util.Vector
  1473      * @serialField permissions java.util.Vector
  1464      *     A list of the SocketPermissions for this set.
  1474      *     A list of the SocketPermissions for this set.
  1465      */
  1475      */
       
  1476     @java.io.Serial
  1466     private static final ObjectStreamField[] serialPersistentFields = {
  1477     private static final ObjectStreamField[] serialPersistentFields = {
  1467         new ObjectStreamField("permissions", Vector.class),
  1478         new ObjectStreamField("permissions", Vector.class),
  1468     };
  1479     };
  1469 
  1480 
  1470     /**
  1481     /**
  1472      */
  1483      */
  1473     /*
  1484     /*
  1474      * Writes the contents of the perms field out as a Vector for
  1485      * Writes the contents of the perms field out as a Vector for
  1475      * serialization compatibility with earlier releases.
  1486      * serialization compatibility with earlier releases.
  1476      */
  1487      */
       
  1488     @java.io.Serial
  1477     private void writeObject(ObjectOutputStream out) throws IOException {
  1489     private void writeObject(ObjectOutputStream out) throws IOException {
  1478         // Don't call out.defaultWriteObject()
  1490         // Don't call out.defaultWriteObject()
  1479 
  1491 
  1480         // Write out Vector
  1492         // Write out Vector
  1481         Vector<SocketPermission> permissions = new Vector<>(perms.values());
  1493         Vector<SocketPermission> permissions = new Vector<>(perms.values());
  1486     }
  1498     }
  1487 
  1499 
  1488     /*
  1500     /*
  1489      * Reads in a Vector of SocketPermissions and saves them in the perms field.
  1501      * Reads in a Vector of SocketPermissions and saves them in the perms field.
  1490      */
  1502      */
       
  1503     @java.io.Serial
  1491     private void readObject(ObjectInputStream in)
  1504     private void readObject(ObjectInputStream in)
  1492         throws IOException, ClassNotFoundException
  1505         throws IOException, ClassNotFoundException
  1493     {
  1506     {
  1494         // Don't call in.defaultReadObject()
  1507         // Don't call in.defaultReadObject()
  1495 
  1508