author | weijun |
Tue, 14 Aug 2018 22:39:34 +0800 | |
changeset 51398 | 3c389a284345 |
parent 47216 | 71c04702a3d5 |
child 58611 | 53ddf218eddd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* =========================================================================== |
|
28 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
29 |
* |
|
30 |
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
31 |
* =========================================================================== |
|
32 |
* |
|
33 |
*/ |
|
34 |
package sun.security.krb5.internal.ccache; |
|
35 |
||
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
36 |
import sun.security.action.GetPropertyAction; |
2 | 37 |
import sun.security.krb5.*; |
38 |
import sun.security.krb5.internal.*; |
|
39 |
import java.util.StringTokenizer; |
|
40 |
import java.util.Vector; |
|
41 |
import java.io.IOException; |
|
42 |
import java.io.File; |
|
43 |
import java.io.FileInputStream; |
|
44 |
import java.io.FileOutputStream; |
|
45 |
import java.io.BufferedReader; |
|
46 |
import java.io.InputStreamReader; |
|
47 |
||
48 |
/** |
|
49 |
* CredentialsCache stores credentials(tickets, session keys, etc) in a |
|
50 |
* semi-permanent store |
|
51 |
* for later use by different program. |
|
52 |
* |
|
53 |
* @author Yanni Zhang |
|
54 |
* @author Ram Marti |
|
55 |
*/ |
|
56 |
||
57 |
public class FileCredentialsCache extends CredentialsCache |
|
58 |
implements FileCCacheConstants { |
|
59 |
public int version; |
|
60 |
public Tag tag; // optional |
|
61 |
public PrincipalName primaryPrincipal; |
|
62 |
private Vector<Credentials> credentialsList; |
|
63 |
private static String dir; |
|
64 |
private static boolean DEBUG = Krb5.DEBUG; |
|
65 |
||
66 |
public static synchronized FileCredentialsCache acquireInstance( |
|
67 |
PrincipalName principal, String cache) { |
|
68 |
try { |
|
69 |
FileCredentialsCache fcc = new FileCredentialsCache(); |
|
70 |
if (cache == null) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
71 |
cacheName = FileCredentialsCache.getDefaultCacheName(); |
2 | 72 |
} else { |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
73 |
cacheName = FileCredentialsCache.checkValidation(cache); |
2 | 74 |
} |
75 |
if ((cacheName == null) || !(new File(cacheName)).exists()) { |
|
76 |
// invalid cache name or the file doesn't exist |
|
77 |
return null; |
|
78 |
} |
|
79 |
if (principal != null) { |
|
80 |
fcc.primaryPrincipal = principal; |
|
81 |
} |
|
82 |
fcc.load(cacheName); |
|
83 |
return fcc; |
|
84 |
} catch (IOException e) { |
|
85 |
// we don't handle it now, instead we return a null at the end. |
|
86 |
if (DEBUG) { |
|
87 |
e.printStackTrace(); |
|
88 |
} |
|
89 |
} catch (KrbException e) { |
|
90 |
// we don't handle it now, instead we return a null at the end. |
|
91 |
if (DEBUG) { |
|
92 |
e.printStackTrace(); |
|
93 |
} |
|
94 |
} |
|
95 |
return null; |
|
96 |
} |
|
97 |
||
98 |
public static FileCredentialsCache acquireInstance() { |
|
99 |
return acquireInstance(null, null); |
|
100 |
} |
|
101 |
||
102 |
static synchronized FileCredentialsCache New(PrincipalName principal, |
|
103 |
String name) { |
|
104 |
try { |
|
105 |
FileCredentialsCache fcc = new FileCredentialsCache(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
106 |
cacheName = FileCredentialsCache.checkValidation(name); |
2 | 107 |
if (cacheName == null) { |
108 |
// invalid cache name or the file doesn't exist |
|
109 |
return null; |
|
110 |
} |
|
111 |
fcc.init(principal, cacheName); |
|
112 |
return fcc; |
|
113 |
} |
|
114 |
catch (IOException e) { |
|
115 |
} |
|
116 |
catch (KrbException e) { |
|
117 |
} |
|
118 |
return null; |
|
119 |
} |
|
120 |
||
121 |
static synchronized FileCredentialsCache New(PrincipalName principal) { |
|
122 |
try { |
|
123 |
FileCredentialsCache fcc = new FileCredentialsCache(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
124 |
cacheName = FileCredentialsCache.getDefaultCacheName(); |
2 | 125 |
fcc.init(principal, cacheName); |
126 |
return fcc; |
|
127 |
} |
|
128 |
catch (IOException e) { |
|
129 |
if (DEBUG) { |
|
130 |
e.printStackTrace(); |
|
131 |
} |
|
132 |
} catch (KrbException e) { |
|
133 |
if (DEBUG) { |
|
134 |
e.printStackTrace(); |
|
135 |
} |
|
136 |
||
137 |
} |
|
138 |
return null; |
|
139 |
} |
|
140 |
||
141 |
private FileCredentialsCache() { |
|
142 |
} |
|
143 |
||
144 |
boolean exists(String cache) { |
|
145 |
File file = new File(cache); |
|
146 |
if (file.exists()) { |
|
147 |
return true; |
|
148 |
} else return false; |
|
149 |
} |
|
150 |
||
151 |
synchronized void init(PrincipalName principal, String name) |
|
152 |
throws IOException, KrbException { |
|
153 |
primaryPrincipal = principal; |
|
28553 | 154 |
try (FileOutputStream fos = new FileOutputStream(name); |
155 |
CCacheOutputStream cos = new CCacheOutputStream(fos)) { |
|
156 |
version = KRB5_FCC_FVNO_3; |
|
157 |
cos.writeHeader(primaryPrincipal, version); |
|
158 |
} |
|
2 | 159 |
load(name); |
160 |
} |
|
161 |
||
162 |
synchronized void load(String name) throws IOException, KrbException { |
|
163 |
PrincipalName p; |
|
28553 | 164 |
try (FileInputStream fis = new FileInputStream(name); |
165 |
CCacheInputStream cis = new CCacheInputStream(fis)) { |
|
166 |
version = cis.readVersion(); |
|
167 |
if (version == KRB5_FCC_FVNO_4) { |
|
168 |
tag = cis.readTag(); |
|
169 |
} else { |
|
170 |
tag = null; |
|
171 |
if (version == KRB5_FCC_FVNO_1 || version == KRB5_FCC_FVNO_2) { |
|
172 |
cis.setNativeByteOrder(); |
|
173 |
} |
|
174 |
} |
|
175 |
p = cis.readPrincipal(version); |
|
176 |
||
177 |
if (primaryPrincipal != null) { |
|
178 |
if (!(primaryPrincipal.match(p))) { |
|
179 |
throw new IOException("Primary principals don't match."); |
|
180 |
} |
|
181 |
} else |
|
182 |
primaryPrincipal = p; |
|
183 |
credentialsList = new Vector<Credentials>(); |
|
184 |
while (cis.available() > 0) { |
|
185 |
Credentials cred = cis.readCred(version); |
|
186 |
if (cred != null) { |
|
187 |
credentialsList.addElement(cred); |
|
188 |
} |
|
2 | 189 |
} |
190 |
} |
|
191 |
} |
|
192 |
||
193 |
||
194 |
/** |
|
195 |
* Updates the credentials list. If the specified credentials for the |
|
196 |
* service is new, add it to the list. If there is an entry in the list, |
|
197 |
* replace the old credentials with the new one. |
|
198 |
* @param c the credentials. |
|
199 |
*/ |
|
200 |
||
201 |
public synchronized void update(Credentials c) { |
|
202 |
if (credentialsList != null) { |
|
203 |
if (credentialsList.isEmpty()) { |
|
204 |
credentialsList.addElement(c); |
|
205 |
} else { |
|
206 |
Credentials tmp = null; |
|
207 |
boolean matched = false; |
|
208 |
||
209 |
for (int i = 0; i < credentialsList.size(); i++) { |
|
210 |
tmp = credentialsList.elementAt(i); |
|
211 |
if (match(c.sname.getNameStrings(), |
|
212 |
tmp.sname.getNameStrings()) && |
|
213 |
((c.sname.getRealmString()).equalsIgnoreCase( |
|
214 |
tmp.sname.getRealmString()))) { |
|
215 |
matched = true; |
|
216 |
if (c.endtime.getTime() >= tmp.endtime.getTime()) { |
|
217 |
if (DEBUG) { |
|
218 |
System.out.println(" >>> FileCredentialsCache " |
|
219 |
+ "Ticket matched, overwrite " |
|
220 |
+ "the old one."); |
|
221 |
} |
|
222 |
credentialsList.removeElementAt(i); |
|
223 |
credentialsList.addElement(c); |
|
224 |
} |
|
225 |
} |
|
226 |
} |
|
227 |
if (matched == false) { |
|
228 |
if (DEBUG) { |
|
229 |
System.out.println(" >>> FileCredentialsCache Ticket " |
|
230 |
+ "not exactly matched, " |
|
231 |
+ "add new one into cache."); |
|
232 |
} |
|
233 |
||
234 |
credentialsList.addElement(c); |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
public synchronized PrincipalName getPrimaryPrincipal() { |
|
241 |
return primaryPrincipal; |
|
242 |
} |
|
243 |
||
244 |
||
245 |
/** |
|
246 |
* Saves the credentials cache file to the disk. |
|
247 |
*/ |
|
248 |
public synchronized void save() throws IOException, Asn1Exception { |
|
28553 | 249 |
try (FileOutputStream fos = new FileOutputStream(cacheName); |
250 |
CCacheOutputStream cos = new CCacheOutputStream(fos)) { |
|
251 |
cos.writeHeader(primaryPrincipal, version); |
|
252 |
Credentials[] tmp = null; |
|
253 |
if ((tmp = getCredsList()) != null) { |
|
254 |
for (int i = 0; i < tmp.length; i++) { |
|
255 |
cos.addCreds(tmp[i]); |
|
256 |
} |
|
2 | 257 |
} |
258 |
} |
|
259 |
} |
|
260 |
||
261 |
boolean match(String[] s1, String[] s2) { |
|
262 |
if (s1.length != s2.length) { |
|
263 |
return false; |
|
264 |
} else { |
|
265 |
for (int i = 0; i < s1.length; i++) { |
|
266 |
if (!(s1[i].equalsIgnoreCase(s2[i]))) { |
|
267 |
return false; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
return true; |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Returns the list of credentials entries in the cache file. |
|
276 |
*/ |
|
277 |
public synchronized Credentials[] getCredsList() { |
|
278 |
if ((credentialsList == null) || (credentialsList.isEmpty())) { |
|
279 |
return null; |
|
280 |
} else { |
|
281 |
Credentials[] tmp = new Credentials[credentialsList.size()]; |
|
282 |
for (int i = 0; i < credentialsList.size(); i++) { |
|
283 |
tmp[i] = credentialsList.elementAt(i); |
|
284 |
} |
|
285 |
return tmp; |
|
286 |
} |
|
287 |
||
288 |
} |
|
289 |
||
13247 | 290 |
public Credentials getCreds(LoginOptions options, PrincipalName sname) { |
2 | 291 |
if (options == null) { |
13247 | 292 |
return getCreds(sname); |
2 | 293 |
} else { |
294 |
Credentials[] list = getCredsList(); |
|
295 |
if (list == null) { |
|
296 |
return null; |
|
297 |
} else { |
|
298 |
for (int i = 0; i < list.length; i++) { |
|
13247 | 299 |
if (sname.match(list[i].sname)) { |
2 | 300 |
if (list[i].flags.match(options)) { |
301 |
return list[i]; |
|
302 |
} |
|
303 |
} |
|
304 |
} |
|
305 |
} |
|
306 |
return null; |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
||
311 |
/** |
|
312 |
* Gets a credentials for a specified service. |
|
313 |
* @param sname service principal name. |
|
314 |
*/ |
|
13247 | 315 |
public Credentials getCreds(PrincipalName sname) { |
2 | 316 |
Credentials[] list = getCredsList(); |
317 |
if (list == null) { |
|
318 |
return null; |
|
319 |
} else { |
|
320 |
for (int i = 0; i < list.length; i++) { |
|
13247 | 321 |
if (sname.match(list[i].sname)) { |
2 | 322 |
return list[i]; |
323 |
} |
|
324 |
} |
|
325 |
} |
|
326 |
return null; |
|
327 |
} |
|
328 |
||
329 |
public Credentials getDefaultCreds() { |
|
330 |
Credentials[] list = getCredsList(); |
|
331 |
if (list == null) { |
|
332 |
return null; |
|
333 |
} else { |
|
334 |
for (int i = list.length-1; i >= 0; i--) { |
|
335 |
if (list[i].sname.toString().startsWith("krbtgt")) { |
|
336 |
String[] nameStrings = list[i].sname.getNameStrings(); |
|
337 |
// find the TGT for the current realm krbtgt/realm@realm |
|
13247 | 338 |
if (nameStrings[1].equals(list[i].sname.getRealm().toString())) { |
2 | 339 |
return list[i]; |
340 |
} |
|
341 |
} |
|
342 |
} |
|
343 |
} |
|
344 |
return null; |
|
345 |
} |
|
346 |
||
347 |
/* |
|
348 |
* Returns path name of the credentials cache file. |
|
349 |
* The path name is searched in the following order: |
|
350 |
* |
|
14202 | 351 |
* 1. KRB5CCNAME (bare file name without FILE:) |
2919
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
352 |
* 2. /tmp/krb5cc_<uid> on unix systems |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
353 |
* 3. <user.home>/krb5cc_<user.name> |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
354 |
* 4. <user.home>/krb5cc (if can't get <user.name>) |
2 | 355 |
*/ |
356 |
||
357 |
public static String getDefaultCacheName() { |
|
2919
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
358 |
|
2 | 359 |
String stdCacheNameComponent = "krb5cc"; |
360 |
String name; |
|
2919
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
361 |
|
14202 | 362 |
// The env var can start with TYPE:, we only support FILE: here. |
363 |
// http://docs.oracle.com/cd/E19082-01/819-2252/6n4i8rtr3/index.html |
|
2919
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
364 |
name = java.security.AccessController.doPrivileged( |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
365 |
new java.security.PrivilegedAction<String>() { |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
366 |
@Override |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
367 |
public String run() { |
14202 | 368 |
String cache = System.getenv("KRB5CCNAME"); |
369 |
if (cache != null && |
|
370 |
(cache.length() >= 5) && |
|
371 |
cache.regionMatches(true, 0, "FILE:", 0, 5)) { |
|
372 |
cache = cache.substring(5); |
|
373 |
} |
|
374 |
return cache; |
|
2919
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
375 |
} |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
376 |
}); |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
377 |
if (name != null) { |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
378 |
if (DEBUG) { |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
379 |
System.out.println(">>>KinitOptions cache name is " + name); |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
380 |
} |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
381 |
return name; |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
382 |
} |
85e23f3ff211
6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents:
2
diff
changeset
|
383 |
|
2 | 384 |
// get cache name from system.property |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
385 |
String osname = GetPropertyAction.privilegedGetProperty("os.name"); |
2 | 386 |
|
387 |
/* |
|
388 |
* For Unix platforms we use the default cache name to be |
|
25995
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
389 |
* /tmp/krb5cc_uid ; for all other platforms we use |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
390 |
* {user_home}/krb5cc_{user_name} |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
391 |
* Please note that for Windows we will use LSA to get |
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25995
diff
changeset
|
392 |
* the TGT from the default cache even before we come here; |
2 | 393 |
* however when we create cache we will create a cache under |
25995
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
394 |
* {user_home}/krb5cc_{user_name} for non-Unix platforms including |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
395 |
* Windows. |
2 | 396 |
*/ |
397 |
||
25995
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
398 |
if (osname != null && !osname.startsWith("Windows")) { |
34882 | 399 |
long uid = jdk.internal.misc.VM.getuid(); |
25995
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
400 |
if (uid != -1) { |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
401 |
name = File.separator + "tmp" + |
2 | 402 |
File.separator + stdCacheNameComponent + "_" + uid; |
25995
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
403 |
if (DEBUG) { |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
404 |
System.out.println(">>>KinitOptions cache name is " + |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
405 |
name); |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
406 |
} |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
407 |
return name; |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
408 |
} else { |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
409 |
if (DEBUG) { |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
410 |
System.out.println("Error in obtaining uid " + |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
411 |
"for Unix platforms " + |
6267b0694e45
8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents:
25859
diff
changeset
|
412 |
"Using user's home directory"); |
2 | 413 |
} |
414 |
} |
|
415 |
} |
|
416 |
||
417 |
// we did not get the uid; |
|
418 |
||
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
419 |
String user_name = GetPropertyAction.privilegedGetProperty("user.name"); |
2 | 420 |
|
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
421 |
String user_home = GetPropertyAction.privilegedGetProperty("user.home"); |
2 | 422 |
|
423 |
if (user_home == null) { |
|
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
424 |
user_home = GetPropertyAction.privilegedGetProperty("user.dir"); |
2 | 425 |
} |
426 |
||
427 |
if (user_name != null) { |
|
428 |
name = user_home + File.separator + |
|
429 |
stdCacheNameComponent + "_" + user_name; |
|
430 |
} else { |
|
431 |
name = user_home + File.separator + stdCacheNameComponent; |
|
432 |
} |
|
433 |
||
434 |
if (DEBUG) { |
|
435 |
System.out.println(">>>KinitOptions cache name is " + name); |
|
436 |
} |
|
437 |
||
438 |
return name; |
|
439 |
} |
|
440 |
||
441 |
public static String checkValidation(String name) { |
|
442 |
String fullname = null; |
|
443 |
if (name == null) { |
|
444 |
return null; |
|
445 |
} |
|
446 |
try { |
|
447 |
// get full path name |
|
448 |
fullname = (new File(name)).getCanonicalPath(); |
|
449 |
File fCheck = new File(fullname); |
|
450 |
if (!(fCheck.exists())) { |
|
451 |
// get absolute directory |
|
452 |
File temp = new File(fCheck.getParent()); |
|
453 |
// test if the directory exists |
|
454 |
if (!(temp.isDirectory())) |
|
455 |
fullname = null; |
|
456 |
temp = null; |
|
457 |
} |
|
458 |
fCheck = null; |
|
459 |
||
460 |
} catch (IOException e) { |
|
461 |
fullname = null; // invalid name |
|
462 |
} |
|
463 |
return fullname; |
|
464 |
} |
|
465 |
||
466 |
||
467 |
private static String exec(String c) { |
|
468 |
StringTokenizer st = new StringTokenizer(c); |
|
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7288
diff
changeset
|
469 |
Vector<String> v = new Vector<>(); |
2 | 470 |
while (st.hasMoreTokens()) { |
471 |
v.addElement(st.nextToken()); |
|
472 |
} |
|
473 |
final String[] command = new String[v.size()]; |
|
474 |
v.copyInto(command); |
|
475 |
try { |
|
476 |
||
477 |
Process p = |
|
478 |
java.security.AccessController.doPrivileged |
|
479 |
(new java.security.PrivilegedAction<Process> () { |
|
480 |
public Process run() { |
|
481 |
try { |
|
482 |
return (Runtime.getRuntime().exec(command)); |
|
483 |
} catch (java.io.IOException e) { |
|
484 |
if (DEBUG) { |
|
485 |
e.printStackTrace(); |
|
486 |
} |
|
487 |
return null; |
|
488 |
} |
|
489 |
} |
|
490 |
}); |
|
491 |
if (p == null) { |
|
21278 | 492 |
// exception occurred during executing the command |
2 | 493 |
return null; |
494 |
} |
|
495 |
||
496 |
BufferedReader commandResult = |
|
497 |
new BufferedReader |
|
498 |
(new InputStreamReader(p.getInputStream(), "8859_1")); |
|
499 |
String s1 = null; |
|
500 |
if ((command.length == 1) && |
|
501 |
(command[0].equals("/usr/bin/env"))) { |
|
502 |
while ((s1 = commandResult.readLine()) != null) { |
|
503 |
if (s1.length() >= 11) { |
|
504 |
if ((s1.substring(0, 11)).equalsIgnoreCase |
|
505 |
("KRB5CCNAME=")) { |
|
506 |
s1 = s1.substring(11); |
|
507 |
break; |
|
508 |
} |
|
509 |
} |
|
510 |
} |
|
511 |
} else s1 = commandResult.readLine(); |
|
512 |
commandResult.close(); |
|
513 |
return s1; |
|
514 |
} catch (Exception e) { |
|
515 |
if (DEBUG) { |
|
516 |
e.printStackTrace(); |
|
517 |
} |
|
518 |
} |
|
519 |
return null; |
|
520 |
} |
|
521 |
} |