author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 14342 | jdk/src/windows/classes/sun/security/krb5/internal/tools/KinitOptions.java@8435a30053c1 |
child 27946 | 9f99b93cbbb2 |
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 |
||
33 |
import sun.security.krb5.*; |
|
34 |
import sun.security.krb5.internal.*; |
|
35 |
import sun.security.krb5.internal.ccache.*; |
|
36 |
import java.io.File; |
|
37 |
import java.io.IOException; |
|
38 |
import java.util.StringTokenizer; |
|
39 |
import java.util.Vector; |
|
40 |
import java.io.BufferedReader; |
|
41 |
import java.io.InputStreamReader; |
|
42 |
import java.io.FileInputStream; |
|
43 |
||
44 |
/** |
|
45 |
* Maintains user-specific options or default settings when the user requests |
|
46 |
* a KDC ticket using Kinit. |
|
47 |
* |
|
48 |
* @author Yanni Zhang |
|
49 |
* @author Ram Marti |
|
50 |
*/ |
|
51 |
class KinitOptions { |
|
52 |
public boolean validate = false; |
|
53 |
||
54 |
// forwardable and proxiable flags have two states: |
|
55 |
// -1 - flag set to be not forwardable or proxiable; |
|
56 |
// 1 - flag set to be forwardable or proxiable. |
|
57 |
public short forwardable = -1; |
|
58 |
public short proxiable = -1; |
|
59 |
public boolean renew = false; |
|
60 |
public KerberosTime lifetime; |
|
61 |
public KerberosTime renewable_lifetime; |
|
62 |
public String target_service; |
|
63 |
public String keytab_file; |
|
64 |
public String cachename; |
|
65 |
private PrincipalName principal; |
|
66 |
public String realm; |
|
67 |
char[] password = null; |
|
68 |
public boolean keytab; |
|
69 |
private boolean DEBUG = Krb5.DEBUG; |
|
70 |
private boolean includeAddresses = true; // default. |
|
71 |
private boolean useKeytab = false; // default = false. |
|
72 |
private String ktabName; // keytab file name |
|
73 |
||
74 |
public KinitOptions() throws RuntimeException, RealmException { |
|
75 |
// no args were specified in the command line; |
|
76 |
// use default values |
|
77 |
cachename = FileCredentialsCache.getDefaultCacheName(); |
|
78 |
if (cachename == null) { |
|
79 |
throw new RuntimeException("default cache name error"); |
|
80 |
} |
|
81 |
principal = getDefaultPrincipal(); |
|
82 |
} |
|
83 |
||
84 |
public void setKDCRealm(String r) throws RealmException { |
|
85 |
realm = r; |
|
86 |
} |
|
87 |
||
88 |
public String getKDCRealm() { |
|
89 |
if (realm == null) { |
|
90 |
if (principal != null) { |
|
91 |
return principal.getRealmString(); |
|
92 |
} |
|
93 |
} |
|
94 |
return null; |
|
95 |
} |
|
96 |
||
97 |
public KinitOptions(String[] args) |
|
98 |
throws KrbException, RuntimeException, IOException { |
|
99 |
// currently we provide support for -f -p -c principal options |
|
100 |
String p = null; // store principal |
|
101 |
||
102 |
for (int i = 0; i < args.length; i++) { |
|
103 |
if (args[i].equals("-f")) { |
|
104 |
forwardable = 1; |
|
105 |
} else if (args[i].equals("-p")) { |
|
106 |
proxiable = 1; |
|
107 |
} else if (args[i].equals("-c")) { |
|
108 |
||
109 |
if (args[i + 1].startsWith("-")) { |
|
110 |
throw new IllegalArgumentException("input format " + |
|
111 |
" not correct: " + |
|
112 |
" -c option " + |
|
113 |
"must be followed " + |
|
114 |
"by the cache name"); |
|
115 |
} |
|
116 |
cachename = args[++i]; |
|
117 |
if ((cachename.length() >= 5) && |
|
118 |
cachename.substring(0, 5).equalsIgnoreCase("FILE:")) { |
|
119 |
cachename = cachename.substring(5); |
|
120 |
}; |
|
121 |
} else if (args[i].equals("-A")) { |
|
122 |
includeAddresses = false; |
|
123 |
} else if (args[i].equals("-k")) { |
|
124 |
useKeytab = true; |
|
125 |
} else if (args[i].equals("-t")) { |
|
126 |
if (ktabName != null) { |
|
127 |
throw new IllegalArgumentException |
|
128 |
("-t option/keytab file name repeated"); |
|
129 |
} else if (i + 1 < args.length) { |
|
130 |
ktabName = args[++i]; |
|
131 |
} else { |
|
132 |
throw new IllegalArgumentException |
|
133 |
("-t option requires keytab file name"); |
|
134 |
} |
|
135 |
||
136 |
useKeytab = true; |
|
137 |
} else if (args[i].equalsIgnoreCase("-help")) { |
|
138 |
printHelp(); |
|
139 |
System.exit(0); |
|
140 |
} else if (p == null) { // Haven't yet processed a "principal" |
|
141 |
p = args[i]; |
|
142 |
try { |
|
143 |
principal = new PrincipalName(p); |
|
144 |
} catch (Exception e) { |
|
145 |
throw new IllegalArgumentException("invalid " + |
|
146 |
"Principal name: " + p + |
|
147 |
e.getMessage()); |
|
148 |
} |
|
149 |
} else if (this.password == null) { |
|
150 |
// Have already processed a Principal, this must be a password |
|
151 |
password = args[i].toCharArray(); |
|
152 |
} else { |
|
153 |
throw new IllegalArgumentException("too many parameters"); |
|
154 |
} |
|
155 |
} |
|
156 |
// we should get cache name before getting the default principal name |
|
157 |
if (cachename == null) { |
|
158 |
cachename = FileCredentialsCache.getDefaultCacheName(); |
|
159 |
if (cachename == null) { |
|
160 |
throw new RuntimeException("default cache name error"); |
|
161 |
} |
|
162 |
} |
|
163 |
if (principal == null) { |
|
164 |
principal = getDefaultPrincipal(); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
PrincipalName getDefaultPrincipal() { |
|
169 |
||
170 |
// get default principal name from the cachename if it is |
|
171 |
// available. |
|
172 |
||
173 |
try { |
|
174 |
CCacheInputStream cis = |
|
175 |
new CCacheInputStream(new FileInputStream(cachename)); |
|
176 |
int version; |
|
177 |
if ((version = cis.readVersion()) == |
|
178 |
FileCCacheConstants.KRB5_FCC_FVNO_4) { |
|
179 |
cis.readTag(); |
|
180 |
} else { |
|
181 |
if (version == FileCCacheConstants.KRB5_FCC_FVNO_1 || |
|
182 |
version == FileCCacheConstants.KRB5_FCC_FVNO_2) { |
|
183 |
cis.setNativeByteOrder(); |
|
184 |
} |
|
185 |
} |
|
186 |
PrincipalName p = cis.readPrincipal(version); |
|
187 |
cis.close(); |
|
188 |
if (DEBUG) { |
|
189 |
System.out.println(">>>KinitOptions principal name from "+ |
|
190 |
"the cache is :" + p); |
|
191 |
} |
|
192 |
return p; |
|
193 |
} catch (IOException e) { |
|
194 |
// ignore any exceptions; we will use the user name as the |
|
195 |
// principal name |
|
196 |
if (DEBUG) { |
|
197 |
e.printStackTrace(); |
|
198 |
} |
|
199 |
} catch (RealmException e) { |
|
200 |
if (DEBUG) { |
|
201 |
e.printStackTrace(); |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
String username = System.getProperty("user.name"); |
|
206 |
if (DEBUG) { |
|
207 |
System.out.println(">>>KinitOptions default username is :" |
|
208 |
+ username); |
|
209 |
} |
|
13247 | 210 |
try { |
211 |
PrincipalName p = new PrincipalName(username); |
|
212 |
return p; |
|
213 |
} catch (RealmException e) { |
|
214 |
// ignore exception , return null |
|
215 |
if (DEBUG) { |
|
216 |
System.out.println ("Exception in getting principal " + |
|
217 |
"name " + e.getMessage()); |
|
218 |
e.printStackTrace(); |
|
2 | 219 |
} |
220 |
} |
|
221 |
return null; |
|
222 |
} |
|
223 |
||
224 |
||
225 |
void printHelp() { |
|
226 |
System.out.println("Usage: kinit " + |
|
227 |
"[-A] [-f] [-p] [-c cachename] " + |
|
228 |
"[[-k [-t keytab_file_name]] [principal] " + |
|
229 |
"[password]"); |
|
230 |
System.out.println("\tavailable options to " + |
|
231 |
"Kerberos 5 ticket request:"); |
|
232 |
System.out.println("\t -A do not include addresses"); |
|
233 |
System.out.println("\t -f forwardable"); |
|
234 |
System.out.println("\t -p proxiable"); |
|
235 |
System.out.println("\t -c cache name " + |
|
236 |
"(i.e., FILE:\\d:\\myProfiles\\mykrb5cache)"); |
|
237 |
System.out.println("\t -k use keytab"); |
|
238 |
System.out.println("\t -t keytab file name"); |
|
239 |
System.out.println("\t principal the principal name "+ |
|
240 |
"(i.e., qweadf@ATHENA.MIT.EDU qweadf)"); |
|
241 |
System.out.println("\t password " + |
|
242 |
"the principal's Kerberos password"); |
|
243 |
} |
|
244 |
||
245 |
public boolean getAddressOption() { |
|
246 |
return includeAddresses; |
|
247 |
} |
|
248 |
||
249 |
public boolean useKeytabFile() { |
|
250 |
return useKeytab; |
|
251 |
} |
|
252 |
||
253 |
public String keytabFileName() { |
|
254 |
return ktabName; |
|
255 |
} |
|
256 |
||
257 |
public PrincipalName getPrincipal() { |
|
258 |
return principal; |
|
259 |
} |
|
260 |
} |