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: */
017:
018: package javax.security.auth.kerberos;
019:
020: import java.io.IOException;
021: import java.io.Serializable;
022: import java.security.Permission;
023: import java.security.PermissionCollection;
024:
025: import org.apache.harmony.auth.internal.nls.Messages;
026:
027: public final class ServicePermission extends Permission implements
028: Serializable {
029:
030: private static final long serialVersionUID = -1227585031618624935L;
031:
032: private static final String INITIATE = "initiate"; //$NON-NLS-1$
033: private static final String ACCEPT = "accept"; //$NON-NLS-1$
034: private static final String INITIATE_ACCEPT = "initiate,accept"; //$NON-NLS-1$
035: private static final String[] ACTIONS_TABLE = {
036: "", ACCEPT, INITIATE, INITIATE_ACCEPT }; //$NON-NLS-1$
037:
038: private final static char ACCEPT_MASK = 1;
039: private final static char INITIATE_MASK = 2;
040:
041: private static final int INITIATE_LEN = INITIATE.length();
042: private static final int ACCEPT_LEN = ACCEPT.length();
043: private static final int MIN_LEN = Math.min(INITIATE_LEN,
044: ACCEPT_LEN);
045:
046: /**
047: * ACCEPT_MASK, INITIATE_ACCEPT or (INITIATE_ACCEPT | ACCEPT_MASK)
048: */
049: private String actions;
050:
051: // initialization of actions
052: private void initActions(String actions) {
053: if (actions == null || actions.length() < MIN_LEN) {
054: throw new IllegalArgumentException(Messages
055: .getString("auth.2E")); //$NON-NLS-1$
056: }
057:
058: char[] c_acts = actions.toCharArray();
059:
060: int result = 0;
061: int ptr = 0;
062:
063: int len6 = c_acts.length - ACCEPT_LEN;
064: int len8 = c_acts.length - INITIATE_LEN;
065:
066: do {
067: //skipping whitespaces
068: while (ptr <= len6
069: && (c_acts[ptr] == ' ' || c_acts[ptr] == '\t'
070: || c_acts[ptr] == '\n'
071: || c_acts[ptr] == 0x0B
072: || c_acts[ptr] == '\f' || c_acts[ptr] == '\r')) {
073: ++ptr;
074: }
075:
076: if (ptr > len6) {
077: // expect string "accept" or "initiate", not just white
078: // spaces
079: throw new IllegalArgumentException(Messages
080: .getString("auth.2E")); //$NON-NLS-1$
081: }
082:
083: //parsing string
084: if ((c_acts[ptr] == 'a' || c_acts[ptr] == 'A')
085: && (c_acts[ptr + 1] == 'c' || c_acts[ptr + 1] == 'C')
086: && (c_acts[ptr + 2] == 'c' || c_acts[ptr + 2] == 'C')
087: && (c_acts[ptr + 3] == 'e' || c_acts[ptr + 3] == 'E')
088: && (c_acts[ptr + 4] == 'p' || c_acts[ptr + 4] == 'P')
089: && (c_acts[ptr + 5] == 't' || c_acts[ptr + 5] == 'T')) {
090: result |= ACCEPT_MASK;
091: ptr += ACCEPT_LEN;
092: } else if (ptr <= len8
093: && (c_acts[ptr] == 'i' || c_acts[ptr] == 'I')
094: && (c_acts[ptr + 1] == 'n' || c_acts[ptr + 1] == 'N')
095: && (c_acts[ptr + 2] == 'i' || c_acts[ptr + 2] == 'I')
096: && (c_acts[ptr + 3] == 't' || c_acts[ptr + 3] == 'T')
097: && (c_acts[ptr + 4] == 'i' || c_acts[ptr + 4] == 'I')
098: && (c_acts[ptr + 5] == 'a' || c_acts[ptr + 5] == 'A')
099: && (c_acts[ptr + 6] == 't' || c_acts[ptr + 6] == 'T')
100: && (c_acts[ptr + 7] == 'e' || c_acts[ptr + 7] == 'E')) {
101: result |= INITIATE_MASK;
102: ptr += INITIATE_LEN;
103: } else {
104: throw new IllegalArgumentException(Messages
105: .getString("auth.2E")); //$NON-NLS-1$
106: }
107:
108: //skipping trailing whitespaces
109: while (ptr < c_acts.length
110: && (c_acts[ptr] == ' ' || c_acts[ptr] == '\t'
111: || c_acts[ptr] == '\n'
112: || c_acts[ptr] == 0x0B
113: || c_acts[ptr] == '\f' || c_acts[ptr] == '\r')) {
114: ptr++;
115: }
116:
117: if (ptr == c_acts.length) {
118: this .actions = ACTIONS_TABLE[result];
119: return;
120: }
121: } while (c_acts[ptr++] == ',');
122:
123: // unknown trailing symbol
124: throw new IllegalArgumentException(Messages
125: .getString("auth.2E")); //$NON-NLS-1$
126: }
127:
128: public ServicePermission(String name, String actions) {
129: super (name);
130:
131: initActions(actions);
132:
133: if (name == null) {
134: throw new NullPointerException(Messages
135: .getString("auth.2F")); //$NON-NLS-1$
136: }
137: if (name.trim().length() == 0) {
138: throw new IllegalArgumentException(Messages
139: .getString("auth.30")); //$NON-NLS-1$
140: }
141: }
142:
143: @Override
144: public boolean equals(Object obj) {
145: if (this == obj) {
146: return true;
147: }
148:
149: if (obj == null || ServicePermission.class != obj.getClass()) {
150: return false;
151: }
152: ServicePermission sp = (ServicePermission) obj;
153:
154: return actions == sp.actions && getName().equals(sp.getName());
155: }
156:
157: @Override
158: public int hashCode() {
159: return getName().hashCode() * actions.length();
160: }
161:
162: @Override
163: public String getActions() {
164: return actions;
165: }
166:
167: @Override
168: public boolean implies(Permission permission) {
169: if (this == permission) {
170: return true;
171: }
172:
173: if (permission == null
174: || ServicePermission.class != permission.getClass()) {
175: return false;
176: }
177:
178: ServicePermission sp = (ServicePermission) permission;
179: String name = getName();
180:
181: return (actions == INITIATE_ACCEPT || actions == sp.actions)
182: && (name.length() == 1 && name.charAt(0) == '*' || name
183: .equals(permission.getName()));
184: }
185:
186: @Override
187: public PermissionCollection newPermissionCollection() {
188: return new KrbServicePermissionCollection();
189: }
190:
191: private synchronized void writeObject(java.io.ObjectOutputStream s)
192: throws IOException {
193: s.defaultWriteObject();
194: }
195:
196: private synchronized void readObject(java.io.ObjectInputStream s)
197: throws IOException, ClassNotFoundException {
198: s.defaultReadObject();
199: initActions(getActions());
200: }
201: }
|