| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi.gssapi.net.example; |
| 16 |
|
| 17 |
import org.globus.gsi.GSIConstants; |
| 18 |
import org.globus.gsi.gssapi.GSSConstants; |
| 19 |
import org.globus.gsi.gssapi.GlobusGSSManagerImpl; |
| 20 |
import org.globus.gsi.gssapi.net.GssSocketFactory; |
| 21 |
import org.globus.gsi.gssapi.net.GssSocket; |
| 22 |
import org.globus.gsi.gssapi.auth.Authorization; |
| 23 |
import org.globus.gsi.gssapi.auth.GSSAuthorization; |
| 24 |
import org.globus.gsi.gssapi.auth.SelfAuthorization; |
| 25 |
import org.globus.gsi.gssapi.auth.HostAuthorization; |
| 26 |
import org.globus.gsi.gssapi.auth.IdentityAuthorization; |
| 27 |
|
| 28 |
import org.ietf.jgss.GSSManager; |
| 29 |
import org.ietf.jgss.GSSName; |
| 30 |
import org.ietf.jgss.GSSCredential; |
| 31 |
|
| 32 |
import org.gridforum.jgss.ExtendedGSSContext; |
| 33 |
|
| 34 |
import java.io.OutputStream; |
| 35 |
import java.io.InputStream; |
| 36 |
import java.io.BufferedReader; |
| 37 |
import java.io.InputStreamReader; |
| 38 |
import java.net.Socket; |
| 39 |
|
| 40 |
public class GssClient { |
| 41 |
|
| 42 |
private static final String helpMsg = |
| 43 |
"Where options are:\n" + |
| 44 |
" -gss-mode mode\t\t\tmode is: 'ssl' or 'gsi' (default)\n" + |
| 45 |
" -deleg-type type\t\ttype is: 'none', 'limited' (default), or 'full'\n" + |
| 46 |
" -lifetime time\t\t\tLifetime of context. time is in seconds.\n" + |
| 47 |
" -rejectLimitedProxy\t\tEnables checking for limited proxies (off by default)\n" + |
| 48 |
" -anonymous\t\t\tDo not send certificates to the server\n" + |
| 49 |
" -enable-conf\t\t\tEnables confidentiality (do encryption) (enabled by default)\n" + |
| 50 |
" -disable-conf\t\t\tDisables confidentiality (no encryption)\n" + |
| 51 |
" -auth auth\t\t\tIf auth is 'host' host authorization will be performed.\n" + |
| 52 |
" \t\t\tIf auth is 'self' self authorization will be performed.\n" + |
| 53 |
" \t\t\tOtherwise, identity authorization is performed.\n" + |
| 54 |
" \t\t\tAuthorization is not performed by default.\n" + |
| 55 |
" -wrap-mode mode\t\tmode is: 'ssl' (default) or 'gsi'"; |
| 56 |
|
| 57 |
|
| 58 |
private Authorization auth; |
| 59 |
private GSSName targetName; |
| 60 |
private static GSSCredential cred; |
| 61 |
|
| 62 |
private static GSSCredential getCredential(GSSManager manager) |
| 63 |
throws Exception { |
| 64 |
// return null if needed to automatically reload the default creds |
| 65 |
if (cred == null) { |
| 66 |
cred = manager.createCredential(GSSCredential.INITIATE_AND_ACCEPT); |
| 67 |
} |
| 68 |
return cred; |
| 69 |
} |
| 70 |
|
| 71 |
public static void main(String [] args) { |
| 72 |
|
| 73 |
String usage = "Usage: java GssClient [options] host port"; |
| 74 |
|
| 75 |
GetOpts opts = new GetOpts(usage, helpMsg); |
| 76 |
|
| 77 |
int pos = opts.parse(args); |
| 78 |
|
| 79 |
if (pos + 2 > args.length) { |
| 80 |
System.err.println(usage); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
String host = args[pos]; |
| 85 |
int port = Integer.parseInt(args[pos+1]); |
| 86 |
|
| 87 |
GssClient client = new GssClient(); |
| 88 |
|
| 89 |
Authorization auth = SelfAuthorization.getInstance(); |
| 90 |
|
| 91 |
if (opts.auth != null) { |
| 92 |
if (opts.auth.equals("host")) { |
| 93 |
client.auth = HostAuthorization.getInstance(); |
| 94 |
} else if (opts.auth.equals("self")) { |
| 95 |
client.auth = SelfAuthorization.getInstance(); |
| 96 |
} else { |
| 97 |
client.auth = new IdentityAuthorization(opts.auth); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// XXX: When doing delegation targetName cannot be null. |
| 102 |
// additional authorization will be performed after the handshake |
| 103 |
// in the socket code. |
| 104 |
if (opts.deleg) { |
| 105 |
if (auth instanceof GSSAuthorization) { |
| 106 |
GSSAuthorization gssAuth = (GSSAuthorization)auth; |
| 107 |
try { |
| 108 |
client.targetName = |
| 109 |
gssAuth.getExpectedName(null, host); |
| 110 |
} catch (Exception e) { |
| 111 |
e.printStackTrace(); |
| 112 |
return; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
client.connect(host, port, opts); |
| 118 |
} |
| 119 |
|
| 120 |
public void connect(String host, int port, GetOpts opts) { |
| 121 |
// to make sure we use right impl |
| 122 |
GSSManager manager = new GlobusGSSManagerImpl(); |
| 123 |
|
| 124 |
ExtendedGSSContext context = null; |
| 125 |
Socket s = null; |
| 126 |
|
| 127 |
try { |
| 128 |
context = (ExtendedGSSContext)manager.createContext( |
| 129 |
this.targetName, |
| 130 |
GSSConstants.MECH_OID, |
| 131 |
getCredential(manager), |
| 132 |
opts.lifetime); |
| 133 |
|
| 134 |
context.requestCredDeleg(opts.deleg); |
| 135 |
context.requestConf(opts.conf); |
| 136 |
context.requestAnonymity(opts.anonymity); |
| 137 |
|
| 138 |
context.setOption(GSSConstants.GSS_MODE, |
| 139 |
(opts.gsiMode) ? |
| 140 |
GSIConstants.MODE_GSI : |
| 141 |
GSIConstants.MODE_SSL); |
| 142 |
|
| 143 |
if (opts.deleg) { |
| 144 |
context.setOption(GSSConstants.DELEGATION_TYPE, |
| 145 |
(opts.limitedDeleg) ? |
| 146 |
GSIConstants.DELEGATION_TYPE_LIMITED : |
| 147 |
GSIConstants.DELEGATION_TYPE_FULL); |
| 148 |
} |
| 149 |
|
| 150 |
context.setOption(GSSConstants.REJECT_LIMITED_PROXY, |
| 151 |
new Boolean(opts.rejectLimitedProxy)); |
| 152 |
|
| 153 |
s = GssSocketFactory.getDefault().createSocket(host, port, context); |
| 154 |
((GssSocket)s).setWrapMode(opts.wrapMode); |
| 155 |
((GssSocket)s).setAuthorization(this.auth); |
| 156 |
|
| 157 |
OutputStream out = s.getOutputStream(); |
| 158 |
InputStream in = s.getInputStream(); |
| 159 |
|
| 160 |
System.out.println("Context established."); |
| 161 |
System.out.println("Initiator : " + context.getSrcName()); |
| 162 |
System.out.println("Acceptor : " + context.getTargName()); |
| 163 |
System.out.println("Lifetime : " + context.getLifetime()); |
| 164 |
System.out.println("Privacy : " + context.getConfState()); |
| 165 |
System.out.println("Anonymity : " + context.getAnonymityState()); |
| 166 |
|
| 167 |
String msg = |
| 168 |
"POST ping/jobmanager HTTP/1.1\r\n" + |
| 169 |
"Host: " + host + "\r\n" + |
| 170 |
"Content-Type: application/x-globus-gram\r\n" + |
| 171 |
"Content-Length: 0\r\n\r\n"; |
| 172 |
|
| 173 |
byte [] tmp = msg.getBytes(); |
| 174 |
|
| 175 |
out.write(tmp); |
| 176 |
out.flush(); |
| 177 |
|
| 178 |
String line = null; |
| 179 |
BufferedReader r = new BufferedReader(new InputStreamReader(in)); |
| 180 |
while ( (line = r.readLine()) != null ) { |
| 181 |
System.out.println(line); |
| 182 |
} |
| 183 |
|
| 184 |
} catch (Exception e) { |
| 185 |
e.printStackTrace(); |
| 186 |
} finally { |
| 187 |
if (s != null) { |
| 188 |
try { s.close(); } catch(Exception e) {} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |