Debian Codegraph @

gss/src/main/java/org/globus/gsi/gssapi/example/GssClient.java

raw
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.example;
16
17 import org.globus.net.SocketFactory;
18 import org.globus.gsi.gssapi.SSLUtil;
19 import org.globus.gsi.gssapi.GSSConstants;
20 import org.globus.gsi.GSIConstants;
21 import org.globus.gsi.gssapi.GlobusGSSManagerImpl;
22
23 import org.ietf.jgss.GSSManager;
24 import org.ietf.jgss.GSSName;
25 import org.ietf.jgss.GSSCredential;
26
27 import org.gridforum.jgss.ExtendedGSSContext;
28
29 import java.io.OutputStream;
30 import java.io.InputStream;
31 import java.net.Socket;
32
33 public class GssClient {
34
35 private static final String helpMsg =
36 "Where options are:\n" +
37 " -gss-mode mode\t\t\tmode is: 'ssl' or 'gsi' (default)\n" +
38 " -deleg-type type\t\ttype is: 'none', 'limited' (default), or 'full'\n" +
39 " -lifetime time\t\t\tLifetime of context. time is in seconds.\n" +
40 " -rejectLimitedProxy\t\tEnables checking for limited proxies (off by default)\n" +
41 " -anonymous\t\t\tDo not send certificates to the server\n " +
42 " -enable-conf\t\t\tEnables confidentiality (do encryption) (enabled by default)\n" +
43 " -disable-conf\t\t\tDisables confidentiality (no encryption)\n" +
44 " -auth auth\t\t\tIf auth is 'host' host authorization will be performed.\n" +
45 " \t\t\tIf auth is 'self' self authorization will be performed.\n" +
46 " \t\t\tOtherwise, identity authorization is performed.\n" +
47 " \t\t\tAuthorization is not performed by default.";
48
49 public static void main(String [] args) {
50
51 String usage = "Usage: java GssClient [options] host port";
52
53 GetOpts opts = new GetOpts(usage, helpMsg);
54
55 int pos = opts.parse(args);
56
57 if (pos + 2 > args.length) {
58 System.err.println(usage);
59 return;
60 }
61
62 String host = args[pos];
63 int port = Integer.parseInt(args[pos+1]);
64
65 // to make sure we use right impl
66 GSSManager manager = new GlobusGSSManagerImpl();
67
68 ExtendedGSSContext context = null;
69 Socket s = null;
70
71 try {
72 s = SocketFactory.getDefault().createSocket(host, port);
73
74 OutputStream out = s.getOutputStream();
75 InputStream in = s.getInputStream();
76
77 byte [] inToken = new byte[0];
78 byte [] outToken = null;
79
80 GSSName targetName = null;
81 if (opts.auth != null) {
82 if (opts.auth.equals("host")) {
83 targetName = manager.createName("host@" + host, GSSName.NT_HOSTBASED_SERVICE);
84 } else if (opts.auth.equals("self")) {
85 targetName = manager.createCredential(GSSCredential.INITIATE_ONLY).getName();
86 } else {
87 targetName = manager.createName(opts.auth, null);
88 }
89 }
90
91 context = (ExtendedGSSContext)manager.createContext(targetName,
92 GSSConstants.MECH_OID,
93 null,
94 opts.lifetime);
95
96 context.requestCredDeleg(opts.deleg);
97 context.requestConf(opts.conf);
98 context.requestAnonymity(opts.anonymity);
99
100 context.setOption(GSSConstants.GSS_MODE,
101 (opts.gsiMode) ?
102 GSIConstants.MODE_GSI :
103 GSIConstants.MODE_SSL);
104
105 if (opts.deleg) {
106 context.setOption(GSSConstants.DELEGATION_TYPE,
107 (opts.limitedDeleg) ?
108 GSIConstants.DELEGATION_TYPE_LIMITED :
109 GSIConstants.DELEGATION_TYPE_FULL);
110 }
111
112 context.setOption(GSSConstants.REJECT_LIMITED_PROXY,
113 new Boolean(opts.rejectLimitedProxy));
114
115 // Loop while there still is a token to be processed
116 while (!context.isEstablished()) {
117 outToken
118 = context.initSecContext(inToken, 0, inToken.length);
119
120 if (outToken != null) {
121 out.write(outToken);
122 out.flush();
123 }
124
125 if (!context.isEstablished()) {
126 inToken = SSLUtil.readSslMessage(in);
127 }
128 }
129
130 System.out.println("Context established.");
131 System.out.println("Initiator : " + context.getSrcName());
132 System.out.println("Acceptor : " + context.getTargName());
133 System.out.println("Lifetime : " + context.getLifetime());
134 System.out.println("Privacy : " + context.getConfState());
135 System.out.println("Anonymity : " + context.getAnonymityState());
136
137 String msg =
138 "POST ping/jobmanager HTTP/1.1\r\n" +
139 "Host: " + host + "\r\n" +
140 "Content-Type: application/x-globus-gram\r\n" +
141 "Content-Length: 0\r\n\r\n";
142
143 byte [] tmp = msg.getBytes();
144
145 outToken = context.wrap(tmp, 0, tmp.length, null);
146
147 out.write(outToken);
148 out.flush();
149
150 inToken = SSLUtil.readSslMessage(in);
151
152 outToken = context.unwrap(inToken, 0, inToken.length, null);
153
154 System.out.println(new String(outToken));
155
156 } catch (Exception e) {
157 e.printStackTrace();
158 } finally {
159 if (s != null) {
160 try { s.close(); } catch(Exception e) {}
161 }
162 if (context != null) {
163 try {
164 System.out.println("closing...");
165 context.dispose();
166 } catch (Exception e) {
167 e.printStackTrace();
168 }
169 }
170 }
171 }
172 }