1 /***
2 * Copyright 2003-2007 Greg Luck
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 package net.sf.jpam;
19
20 import java.util.List;
21 import java.util.Collections;
22 import java.util.Arrays;
23
24 /***
25 * A type-safe enum for PAM return values.
26 * <p/>
27 * Warning. When comparing values do not use <code>==</code>.
28 * Use the <code>.equals(Object o)</code> method.
29 * <p/>
30 * These are based on the Linux PAM projects return values.
31 *
32 * @author <a href="mailto:gregluck@users.sourceforge.net">Greg Luck</a>
33 * @version $Id: PamReturnValue.java 1 2006-12-31 23:27:21Z gregluck $
34 */
35 public class PamReturnValue {
36
37 /***
38 * A constant PamReturnValue
39 */
40 public static final PamReturnValue PAM_SUCCESS =
41 new PamReturnValue(0, "Successful function return.");
42 /***
43 * A constant PamReturnValue
44 */
45 public static final PamReturnValue PAM_OPEN_ERR =
46 new PamReturnValue(1, "dlopen() failure when dynamically loading a service module.");
47 /***
48 * A constant PamReturnValue
49 */
50 public static final PamReturnValue PAM_SYMBOL_ERR =
51 new PamReturnValue(2, "Symbol not found.");
52 /***
53 * A constant PamReturnValue
54 */
55 public static final PamReturnValue PAM_SERVICE_ERR =
56 new PamReturnValue(3, "Error in service module.");
57 /***
58 * A constant PamReturnValue
59 */
60 public static final PamReturnValue PAM_SYSTEM_ERR =
61 new PamReturnValue(4, "System error.");
62 /***
63 * A constant PamReturnValue
64 */
65 public static final PamReturnValue PAM_BUF_ERR =
66 new PamReturnValue(5, "Memory buffer error.");
67 /***
68 * A constant PamReturnValue
69 */
70 public static final PamReturnValue PAM_PERM_DENIED =
71 new PamReturnValue(6, "Permission denied.");
72 /***
73 * A constant PamReturnValue
74 */
75 public static final PamReturnValue PAM_AUTH_ERR =
76 new PamReturnValue(7, "Authentication failure.");
77 /***
78 * A constant PamReturnValue
79 */
80 public static final PamReturnValue PAM_CRED_INSUFFICIENT =
81 new PamReturnValue(8, "Can not access authentication data due to insufficient credentials.");
82 /***
83 * A constant PamReturnValue
84 */
85 public static final PamReturnValue PAM_AUTHINFO_UNAVAIL =
86 new PamReturnValue(9, "Underlying authentication service can not retrieve authentication information.");
87 /***
88 * A constant PamReturnValue
89 */
90 public static final PamReturnValue PAM_USER_UNKNOWN =
91 new PamReturnValue(10, "User not known to the underlying authentication module.");
92 /***
93 * A constant PamReturnValue
94 */
95 public static final PamReturnValue PAM_MAXTRIES =
96 new PamReturnValue(11, "An authentication service has maintained a retry "
97 + "count which has been reached. No further retries should be attempted.");
98 /***
99 * A constant PamReturnValue
100 */
101 public static final PamReturnValue PAM_NEW_AUTHTOK_REQD =
102 new PamReturnValue(12, "New authentication token required. This is normally returned if"
103 + " the machine security policies require that the password should be changed because"
104 + " the password is NULL or it has aged.");
105 /***
106 * A constant PamReturnValue
107 */
108 public static final PamReturnValue PAM_ACCT_EXPIRED =
109 new PamReturnValue(13, "User account has expired.");
110 /***
111 * A constant PamReturnValue
112 */
113 public static final PamReturnValue PAM_SESSION_ERR =
114 new PamReturnValue(14, "Can not make/remove an entry for the specified session.");
115 /***
116 * A constant PamReturnValue
117 */
118 public static final PamReturnValue PAM_CRED_UNAVAIL =
119 new PamReturnValue(15, "Underlying authentication service can not retrieve user credentials unavailable.");
120 /***
121 * A constant PamReturnValue
122 */
123 public static final PamReturnValue PAM_CRED_EXPIRED =
124 new PamReturnValue(16, "User credentials expired.");
125 /***
126 * A constant PamReturnValue
127 */
128 public static final PamReturnValue PAM_CRED_ERR =
129 new PamReturnValue(17, "Failure setting user credentials.");
130 /***
131 * A constant PamReturnValue
132 */
133 public static final PamReturnValue PAM_NO_MODULE_DATA =
134 new PamReturnValue(18, "No module specific data is present.");
135 /***
136 * A constant PamReturnValue
137 */
138 public static final PamReturnValue PAM_CONV_ERR =
139 new PamReturnValue(19, "Conversation error.");
140 /***
141 * A constant PamReturnValue
142 */
143 public static final PamReturnValue PAM_AUTHTOK_ERR =
144 new PamReturnValue(20, "Authentication token manipulation error.");
145 /***
146 * A constant PamReturnValue
147 */
148 public static final PamReturnValue PAM_AUTHTOK_RECOVER_ERR =
149 new PamReturnValue(21, "Authentication information cannot be recovered.");
150 /***
151 * A constant PamReturnValue
152 */
153 public static final PamReturnValue PAM_AUTHTOK_LOCK_BUSY =
154 new PamReturnValue(22, "Authentication token lock busy.");
155 /***
156 * A constant PamReturnValue
157 */
158 public static final PamReturnValue PAM_AUTHTOK_DISABLE_AGING =
159 new PamReturnValue(23, "Authentication token aging disabled.");
160 /***
161 * A constant PamReturnValue
162 */
163 public static final PamReturnValue PAM_TRY_AGAIN =
164 new PamReturnValue(24, "Preliminary check by password service.");
165 /***
166 * A constant PamReturnValue
167 */
168 public static final PamReturnValue PAM_IGNORE =
169 new PamReturnValue(25, "Ignore underlying account module regardless of whether the control flag"
170 + "is required, optional, or sufficient.");
171 /***
172 * A constant PamReturnValue
173 */
174 public static final PamReturnValue PAM_ABORT =
175 new PamReturnValue(26, "Critical error (?module fail now request).");
176 /***
177 * A constant PamReturnValue
178 */
179 public static final PamReturnValue PAM_AUTHTOK_EXPIRED =
180 new PamReturnValue(27, "User's authentication token has expired.");
181 /***
182 * A constant PamReturnValue
183 */
184 public static final PamReturnValue PAM_MODULE_UNKNOWN =
185 new PamReturnValue(28, "Module is not known.");
186 /***
187 * A constant PamReturnValue
188 */
189 public static final PamReturnValue PAM_BAD_ITEM =
190 new PamReturnValue(29, "Bad item passed to pam_*_item().");
191 /***
192 * A constant PamReturnValue
193 */
194 public static final PamReturnValue PAM_CONV_AGAIN =
195 new PamReturnValue(30, "Conversation function is event driven and data is not available yet.");
196 /***
197 * A constant PamReturnValue
198 */
199 public static final PamReturnValue PAM_INCOMPLETE =
200 new PamReturnValue(31, "Please call this function again to complete authentication stack. Before calling"
201 + " again, verify that conversation is completed.");
202
203
204 private static final PamReturnValue[] PRIVATE_VALUES =
205 {PAM_SUCCESS, PAM_OPEN_ERR, PAM_SYMBOL_ERR, PAM_SERVICE_ERR, PAM_SYSTEM_ERR,
206 PAM_BUF_ERR, PAM_PERM_DENIED, PAM_AUTH_ERR, PAM_CRED_INSUFFICIENT, PAM_AUTHINFO_UNAVAIL,
207 PAM_USER_UNKNOWN, PAM_MAXTRIES, PAM_NEW_AUTHTOK_REQD, PAM_ACCT_EXPIRED, PAM_SESSION_ERR,
208 PAM_CRED_UNAVAIL, PAM_CRED_EXPIRED, PAM_CRED_ERR, PAM_NO_MODULE_DATA, PAM_CONV_ERR,
209 PAM_AUTHTOK_ERR, PAM_AUTHTOK_RECOVER_ERR, PAM_AUTHTOK_LOCK_BUSY, PAM_AUTHTOK_DISABLE_AGING,
210 PAM_TRY_AGAIN, PAM_IGNORE, PAM_ABORT, PAM_AUTHTOK_EXPIRED, PAM_MODULE_UNKNOWN, PAM_BAD_ITEM,
211 PAM_CONV_AGAIN, PAM_INCOMPLETE
212 };
213
214 private final String description;
215 private final int id;
216
217 private PamReturnValue(int id, String description) {
218 this.id = id;
219 this.description = description;
220 }
221
222 /***
223 * Returns true if the supplied object is of the
224 * same type and has the same id.
225 */
226 public boolean equals(final Object o) {
227 if (this == o) {
228 return true;
229 }
230 if (!(o instanceof PamReturnValue)) {
231 return false;
232 }
233 final PamReturnValue pamReturnValue = (PamReturnValue) o;
234 if (id != pamReturnValue.id) {
235 return false;
236 }
237 return true;
238 }
239
240 /***
241 * Gets the PamReturnValue that matches the given id
242 * @param id a valid Integer with a value between 0 and 31
243 * @return the PamReturnValue matching the id
244 * @throws IllegalArgumentException if the id is outside the range of possible return values
245 */
246 public static PamReturnValue fromId(int id) throws IllegalArgumentException {
247 int maxId = VALUES.size() - 1;
248 if (id > maxId || id < 0) {
249 throw new IllegalArgumentException("id " + id + " is not between 0 and " + maxId);
250 }
251 return (PamReturnValue) VALUES.get(id);
252 }
253
254 /***
255 * @return a hash code for the object.
256 */
257 public int hashCode() {
258 return id;
259 }
260
261 /***
262 * @return the String description of the return value
263 */
264 public String toString() {
265 return description;
266 }
267
268 /***
269 * The enumeration of possible values
270 */
271 public static final List VALUES = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));
272 }
273
274