ServicePermission.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.auth.kerberos;
  18. import java.io.IOException;
  19. import java.io.Serializable;
  20. import java.security.Permission;
  21. import java.security.PermissionCollection;
  22. import org.apache.harmony.auth.internal.nls.Messages;
  23. public final class ServicePermission extends Permission implements Serializable {
  24. private static final long serialVersionUID = -1227585031618624935L;
  25. private static final String INITIATE = "initiate"; //$NON-NLS-1$
  26. private static final String ACCEPT = "accept"; //$NON-NLS-1$
  27. private static final String INITIATE_ACCEPT = "initiate,accept"; //$NON-NLS-1$
  28. private static final String[] ACTIONS_TABLE = {"", ACCEPT, INITIATE, INITIATE_ACCEPT}; //$NON-NLS-1$
  29. private final static char ACCEPT_MASK = 1;
  30. private final static char INITIATE_MASK = 2;
  31. private static final int INITIATE_LEN = INITIATE.length();
  32. private static final int ACCEPT_LEN = ACCEPT.length();
  33. private static final int MIN_LEN = Math.min(INITIATE_LEN,ACCEPT_LEN);
  34. /**
  35. * ACCEPT_MASK, INITIATE_ACCEPT or (INITIATE_ACCEPT | ACCEPT_MASK)
  36. */
  37. private String actions;
  38. // initialization of actions
  39. private void initActions(String actions) {
  40. if (actions == null || actions.length() < MIN_LEN) {
  41. throw new IllegalArgumentException(Messages.getString("auth.2E")); //$NON-NLS-1$
  42. }
  43. char[] c_acts = actions.toCharArray();
  44. int result = 0;
  45. int ptr = 0;
  46. int len6 = c_acts.length - ACCEPT_LEN;
  47. int len8 = c_acts.length - INITIATE_LEN;
  48. do {
  49. //skipping whitespaces
  50. while (ptr <= len6
  51. && (c_acts[ptr] == ' ' || c_acts[ptr] == '\t'
  52. || c_acts[ptr] == '\n' || c_acts[ptr] == 0x0B
  53. || c_acts[ptr] == '\f' || c_acts[ptr] == '\r')) {
  54. ++ptr;
  55. }
  56. if (ptr > len6) {
  57. // expect string "accept" or "initiate", not just white
  58. // spaces
  59. throw new IllegalArgumentException(Messages.getString("auth.2E")); //$NON-NLS-1$
  60. }
  61. //parsing string
  62. if ((c_acts[ptr] == 'a' || c_acts[ptr] == 'A')
  63. && (c_acts[ptr + 1] == 'c' || c_acts[ptr + 1] == 'C')
  64. && (c_acts[ptr + 2] == 'c' || c_acts[ptr + 2] == 'C')
  65. && (c_acts[ptr + 3] == 'e' || c_acts[ptr + 3] == 'E')
  66. && (c_acts[ptr + 4] == 'p' || c_acts[ptr + 4] == 'P')
  67. && (c_acts[ptr + 5] == 't' || c_acts[ptr + 5] == 'T')) {
  68. result |= ACCEPT_MASK;
  69. ptr += ACCEPT_LEN;
  70. } else if (ptr <= len8
  71. && (c_acts[ptr] == 'i' || c_acts[ptr] == 'I')
  72. && (c_acts[ptr + 1] == 'n' || c_acts[ptr + 1] == 'N')
  73. && (c_acts[ptr + 2] == 'i' || c_acts[ptr + 2] == 'I')
  74. && (c_acts[ptr + 3] == 't' || c_acts[ptr + 3] == 'T')
  75. && (c_acts[ptr + 4] == 'i' || c_acts[ptr + 4] == 'I')
  76. && (c_acts[ptr + 5] == 'a' || c_acts[ptr + 5] == 'A')
  77. && (c_acts[ptr + 6] == 't' || c_acts[ptr + 6] == 'T')
  78. && (c_acts[ptr + 7] == 'e' || c_acts[ptr + 7] == 'E')) {
  79. result |= INITIATE_MASK;
  80. ptr += INITIATE_LEN;
  81. } else {
  82. throw new IllegalArgumentException(Messages.getString("auth.2E")); //$NON-NLS-1$
  83. }
  84. //skipping trailing whitespaces
  85. while (ptr < c_acts.length
  86. && (c_acts[ptr] == ' ' || c_acts[ptr] == '\t'
  87. || c_acts[ptr] == '\n' || c_acts[ptr] == 0x0B
  88. || c_acts[ptr] == '\f' || c_acts[ptr] == '\r')) {
  89. ptr++;
  90. }
  91. if (ptr == c_acts.length) {
  92. this.actions = ACTIONS_TABLE[result];
  93. return;
  94. }
  95. } while (c_acts[ptr++] == ',');
  96. // unknown trailing symbol
  97. throw new IllegalArgumentException(Messages.getString("auth.2E")); //$NON-NLS-1$
  98. }
  99. public ServicePermission(String name, String actions) {
  100. super(name);
  101. initActions(actions);
  102. if (name == null) {
  103. throw new NullPointerException(Messages.getString("auth.2F")); //$NON-NLS-1$
  104. }
  105. if (name.trim().length() == 0) {
  106. throw new IllegalArgumentException(Messages.getString("auth.30")); //$NON-NLS-1$
  107. }
  108. }
  109. @Override
  110. public boolean equals(Object obj) {
  111. if (this == obj) {
  112. return true;
  113. }
  114. if (obj == null || ServicePermission.class != obj.getClass()) {
  115. return false;
  116. }
  117. ServicePermission sp = (ServicePermission) obj;
  118. return actions == sp.actions && getName().equals(sp.getName());
  119. }
  120. @Override
  121. public int hashCode() {
  122. return getName().hashCode() * actions.length();
  123. }
  124. @Override
  125. public String getActions() {
  126. return actions;
  127. }
  128. @Override
  129. public boolean implies(Permission permission) {
  130. if (this == permission) {
  131. return true;
  132. }
  133. if (permission == null || ServicePermission.class != permission.getClass()) {
  134. return false;
  135. }
  136. ServicePermission sp = (ServicePermission) permission;
  137. String name = getName();
  138. return (actions == INITIATE_ACCEPT || actions == sp.actions)
  139. && (name.length() == 1 && name.charAt(0) == '*' || name.equals(permission.getName()));
  140. }
  141. @Override
  142. public PermissionCollection newPermissionCollection() {
  143. return new KrbServicePermissionCollection();
  144. }
  145. private synchronized void writeObject(java.io.ObjectOutputStream s)
  146. throws IOException {
  147. s.defaultWriteObject();
  148. }
  149. private synchronized void readObject(java.io.ObjectInputStream s)
  150. throws IOException, ClassNotFoundException {
  151. s.defaultReadObject();
  152. initActions(getActions());
  153. }
  154. }