author | mgronlun |
Sun, 15 Sep 2019 15:31:04 +0200 | |
branch | JEP-349-branch |
changeset 58158 | 369ebc2a11c2 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13247
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, 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 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
28 |
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
29 |
*/ |
|
30 |
||
31 |
package sun.security.krb5.internal.tools; |
|
32 |
||
9499 | 33 |
import java.io.File; |
2 | 34 |
import sun.security.krb5.*; |
35 |
import sun.security.krb5.internal.*; |
|
36 |
import sun.security.krb5.internal.ccache.*; |
|
37 |
import java.io.IOException; |
|
38 |
import java.util.Arrays; |
|
39 |
import sun.security.util.Password; |
|
9499 | 40 |
import javax.security.auth.kerberos.KeyTab; |
2 | 41 |
|
42 |
/** |
|
43 |
* Kinit tool for obtaining Kerberos v5 tickets. |
|
44 |
* |
|
45 |
* @author Yanni Zhang |
|
46 |
* @author Ram Marti |
|
47 |
*/ |
|
48 |
public class Kinit { |
|
49 |
||
50 |
private KinitOptions options; |
|
51 |
private static final boolean DEBUG = Krb5.DEBUG; |
|
52 |
||
53 |
/** |
|
54 |
* The main method is used to accept user command line input for ticket |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
55 |
* request. Read {@link KinitOptions#printHelp} for usages or call |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
56 |
* java sun.security.krb5.internal.tools.Kinit -help |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
57 |
* to bring up help menu. |
2 | 58 |
* <p> |
59 |
* We currently support only file-based credentials cache to |
|
60 |
* store the tickets obtained from the KDC. |
|
61 |
* By default, for all Unix platforms a cache file named |
|
32003 | 62 |
* {@code /tmp/krb5cc_<uid>} will be generated. The {@code <uid>} is the |
2 | 63 |
* numeric user identifier. |
64 |
* For all other platforms, a cache file named |
|
32003 | 65 |
* {@code <USER_HOME>/krb5cc_<USER_NAME>} would be generated. |
2 | 66 |
* <p> |
32003 | 67 |
* {@code <USER_HOME>} is obtained from {@code java.lang.System} |
2 | 68 |
* property <i>user.home</i>. |
32003 | 69 |
* {@code <USER_NAME>} is obtained from {@code java.lang.System} |
2 | 70 |
* property <i>user.name</i>. |
32003 | 71 |
* If {@code <USER_HOME>} is null the cache file would be stored in |
2 | 72 |
* the current directory that the program is running from. |
32003 | 73 |
* {@code <USER_NAME>} is operating system's login username. |
2 | 74 |
* It could be different from user's principal name. |
32003 | 75 |
* <p> |
2 | 76 |
* For instance, on Windows NT, it could be |
32003 | 77 |
* {@code c:\winnt\profiles\duke\krb5cc_duke}, in |
78 |
* which duke is the {@code <USER_NAME>} and {@code c:\winnt\profile\duke} is the |
|
79 |
* {@code <USER_HOME>}. |
|
80 |
* <p> |
|
2 | 81 |
* A single user could have multiple principal names, |
82 |
* but the primary principal of the credentials cache could only be one, |
|
83 |
* which means one cache file could only store tickets for one |
|
84 |
* specific user principal. If the user switches |
|
85 |
* the principal name at the next Kinit, the cache file generated for the |
|
86 |
* new ticket would overwrite the old cache file by default. |
|
87 |
* To avoid overwriting, you need to specify |
|
88 |
* a different cache file name when you request a |
|
89 |
* new ticket. |
|
32003 | 90 |
* <p> |
2 | 91 |
* You can specify the location of the cache file by using the -c option |
92 |
*/ |
|
93 |
||
94 |
public static void main(String[] args) { |
|
95 |
try { |
|
96 |
Kinit self = new Kinit(args); |
|
97 |
} |
|
98 |
catch (Exception e) { |
|
99 |
String msg = null; |
|
100 |
if (e instanceof KrbException) { |
|
101 |
msg = ((KrbException)e).krbErrorMessage() + " " + |
|
102 |
((KrbException)e).returnCodeMessage(); |
|
103 |
} else { |
|
104 |
msg = e.getMessage(); |
|
105 |
} |
|
106 |
if (msg != null) { |
|
107 |
System.err.println("Exception: " + msg); |
|
108 |
} else { |
|
109 |
System.out.println("Exception: " + e); |
|
110 |
} |
|
111 |
e.printStackTrace(); |
|
112 |
System.exit(-1); |
|
113 |
} |
|
114 |
return; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Constructs a new Kinit object. |
|
119 |
* @param args array of ticket request options. |
|
120 |
* Avaiable options are: -f, -p, -c, principal, password. |
|
121 |
* @exception IOException if an I/O error occurs. |
|
122 |
* @exception RealmException if the Realm could not be instantiated. |
|
123 |
* @exception KrbException if error occurs during Kerberos operation. |
|
124 |
*/ |
|
125 |
private Kinit(String[] args) |
|
126 |
throws IOException, RealmException, KrbException { |
|
127 |
if (args == null || args.length == 0) { |
|
128 |
options = new KinitOptions(); |
|
129 |
} else { |
|
130 |
options = new KinitOptions(args); |
|
131 |
} |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
132 |
switch (options.action) { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
133 |
case 1: |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
134 |
acquire(); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
135 |
break; |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
136 |
case 2: |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
137 |
renew(); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
138 |
break; |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
139 |
default: |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
140 |
throw new KrbException("kinit does not support action " |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
141 |
+ options.action); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
142 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
143 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
144 |
|
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
145 |
private void renew() |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
146 |
throws IOException, RealmException, KrbException { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
147 |
|
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
148 |
PrincipalName principal = options.getPrincipal(); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
149 |
String realm = principal.getRealmAsString(); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
150 |
CredentialsCache cache = CredentialsCache.getInstance(options.cachename); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
151 |
|
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
152 |
if (cache == null) { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
153 |
throw new IOException("Unable to find existing cache file " + |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
154 |
options.cachename); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
155 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
156 |
sun.security.krb5.internal.ccache.Credentials credentials = |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
157 |
cache.getCreds(PrincipalName.tgsService(realm, realm)); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
158 |
|
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
159 |
credentials = credentials.setKrbCreds() |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
160 |
.renew() |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
161 |
.toCCacheCreds(); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
162 |
|
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
163 |
cache = CredentialsCache.create(principal, options.cachename); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
164 |
if (cache == null) { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
165 |
throw new IOException("Unable to create the cache file " + |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
166 |
options.cachename); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
167 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
168 |
cache.update(credentials); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
169 |
cache.save(); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
170 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
171 |
|
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
172 |
private void acquire() |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
173 |
throws IOException, RealmException, KrbException { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
174 |
|
2 | 175 |
String princName = null; |
176 |
PrincipalName principal = options.getPrincipal(); |
|
177 |
if (principal != null) { |
|
178 |
princName = principal.toString(); |
|
179 |
} |
|
7183 | 180 |
KrbAsReqBuilder builder; |
2 | 181 |
if (DEBUG) { |
182 |
System.out.println("Principal is " + principal); |
|
183 |
} |
|
184 |
char[] psswd = options.password; |
|
185 |
boolean useKeytab = options.useKeytabFile(); |
|
186 |
if (!useKeytab) { |
|
187 |
if (princName == null) { |
|
188 |
throw new IllegalArgumentException |
|
189 |
(" Can not obtain principal name"); |
|
190 |
} |
|
191 |
if (psswd == null) { |
|
192 |
System.out.print("Password for " + princName + ":"); |
|
193 |
System.out.flush(); |
|
194 |
psswd = Password.readPassword(System.in); |
|
195 |
if (DEBUG) { |
|
196 |
System.out.println(">>> Kinit console input " + |
|
197 |
new String(psswd)); |
|
198 |
} |
|
199 |
} |
|
7183 | 200 |
builder = new KrbAsReqBuilder(principal, psswd); |
2 | 201 |
} else { |
202 |
if (DEBUG) { |
|
203 |
System.out.println(">>> Kinit using keytab"); |
|
204 |
} |
|
205 |
if (princName == null) { |
|
206 |
throw new IllegalArgumentException |
|
207 |
("Principal name must be specified."); |
|
208 |
} |
|
209 |
String ktabName = options.keytabFileName(); |
|
210 |
if (ktabName != null) { |
|
211 |
if (DEBUG) { |
|
212 |
System.out.println( |
|
213 |
">>> Kinit keytab file name: " + ktabName); |
|
214 |
} |
|
215 |
} |
|
216 |
||
9499 | 217 |
builder = new KrbAsReqBuilder(principal, ktabName == null |
218 |
? KeyTab.getInstance() |
|
219 |
: KeyTab.getInstance(new File(ktabName))); |
|
2 | 220 |
} |
221 |
||
222 |
KDCOptions opt = new KDCOptions(); |
|
223 |
setOptions(KDCOptions.FORWARDABLE, options.forwardable, opt); |
|
224 |
setOptions(KDCOptions.PROXIABLE, options.proxiable, opt); |
|
7183 | 225 |
builder.setOptions(opt); |
2 | 226 |
String realm = options.getKDCRealm(); |
227 |
if (realm == null) { |
|
228 |
realm = Config.getInstance().getDefaultRealm(); |
|
229 |
} |
|
230 |
||
231 |
if (DEBUG) { |
|
232 |
System.out.println(">>> Kinit realm name is " + realm); |
|
233 |
} |
|
234 |
||
13247 | 235 |
PrincipalName sname = PrincipalName.tgsService(realm, realm); |
7183 | 236 |
builder.setTarget(sname); |
2 | 237 |
|
238 |
if (DEBUG) { |
|
239 |
System.out.println(">>> Creating KrbAsReq"); |
|
240 |
} |
|
241 |
||
7183 | 242 |
if (options.getAddressOption()) |
243 |
builder.setAddresses(HostAddresses.getLocalAddresses()); |
|
2 | 244 |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
245 |
builder.setTill(options.lifetime); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
246 |
builder.setRTime(options.renewable_lifetime); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
25859
diff
changeset
|
247 |
|
7183 | 248 |
builder.action(); |
2 | 249 |
|
250 |
sun.security.krb5.internal.ccache.Credentials credentials = |
|
7183 | 251 |
builder.getCCreds(); |
252 |
builder.destroy(); |
|
253 |
||
2 | 254 |
// we always create a new cache and store the ticket we get |
255 |
CredentialsCache cache = |
|
256 |
CredentialsCache.create(principal, options.cachename); |
|
257 |
if (cache == null) { |
|
258 |
throw new IOException("Unable to create the cache file " + |
|
259 |
options.cachename); |
|
260 |
} |
|
261 |
cache.update(credentials); |
|
262 |
cache.save(); |
|
263 |
||
264 |
if (options.password == null) { |
|
265 |
// Assume we're running interactively |
|
266 |
System.out.println("New ticket is stored in cache file " + |
|
267 |
options.cachename); |
|
268 |
} else { |
|
269 |
Arrays.fill(options.password, '0'); |
|
270 |
} |
|
271 |
||
272 |
// clear the password |
|
273 |
if (psswd != null) { |
|
274 |
Arrays.fill(psswd, '0'); |
|
275 |
} |
|
276 |
options = null; // release reference to options |
|
277 |
} |
|
278 |
||
279 |
private static void setOptions(int flag, int option, KDCOptions opt) { |
|
280 |
switch (option) { |
|
281 |
case 0: |
|
282 |
break; |
|
283 |
case -1: |
|
284 |
opt.set(flag, false); |
|
285 |
break; |
|
286 |
case 1: |
|
287 |
opt.set(flag, true); |
|
288 |
} |
|
289 |
} |
|
290 |
} |