| 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.net.ServerSocketFactory; |
| 18 |
import org.globus.gsi.GSIConstants; |
| 19 |
import org.globus.gsi.gssapi.GlobusGSSCredentialImpl; |
| 20 |
import org.globus.gsi.gssapi.GSSConstants; |
| 21 |
import org.globus.gsi.gssapi.GlobusGSSManagerImpl; |
| 22 |
import org.globus.gsi.gssapi.net.GssSocketFactory; |
| 23 |
import org.globus.gsi.gssapi.net.GssSocket; |
| 24 |
|
| 25 |
import org.ietf.jgss.GSSManager; |
| 26 |
import org.ietf.jgss.GSSCredential; |
| 27 |
|
| 28 |
import org.gridforum.jgss.ExtendedGSSContext; |
| 29 |
|
| 30 |
import java.io.OutputStream; |
| 31 |
import java.io.InputStream; |
| 32 |
import java.io.BufferedReader; |
| 33 |
import java.io.InputStreamReader; |
| 34 |
import java.net.Socket; |
| 35 |
import java.net.ServerSocket; |
| 36 |
|
| 37 |
public class GssServer { |
| 38 |
|
| 39 |
private static final String helpMsg = |
| 40 |
"Where options are:\n" + |
| 41 |
" -gss-mode mode\t\t\tmode is: 'ssl' or 'gsi' (default)\n" + |
| 42 |
" -deleg-type type\t\ttype is: 'none', 'limited' (default), or 'full'\n" + |
| 43 |
" -lifetime time\t\t\tLifetime of context. time is in seconds.\n" + |
| 44 |
" -rejectLimitedProxy\t\tEnables checking for limited proxies. By default off\n" + |
| 45 |
" -anonymous\t\t\tDo not require client authentication\n" + |
| 46 |
" -enable-conf\t\t\tEnables confidentiality (do encryption) (enabled by default)\n" + |
| 47 |
" -disable-conf\t\t\tDisables confidentiality (no encryption)\n" + |
| 48 |
" -wrap-mode mode\t\tmode is: 'ssl' (default) or 'gsi'"; |
| 49 |
|
| 50 |
public static void main(String [] args) { |
| 51 |
|
| 52 |
String usage = "Usage: java GssServer [options] [port]"; |
| 53 |
|
| 54 |
GetOpts opts = new GetOpts(usage, helpMsg); |
| 55 |
|
| 56 |
int pos = opts.parse(args); |
| 57 |
int port = 0; |
| 58 |
|
| 59 |
if (pos < args.length) { |
| 60 |
port = Integer.parseInt(args[pos]); |
| 61 |
} |
| 62 |
|
| 63 |
ServerSocketFactory factory = ServerSocketFactory.getDefault(); |
| 64 |
try { |
| 65 |
ServerSocket server = factory.createServerSocket(port); |
| 66 |
System.out.println("Server running at: " + server.getLocalPort()); |
| 67 |
while(true) { |
| 68 |
Client c = new Client(server.accept(), opts); |
| 69 |
c.start(); |
| 70 |
} |
| 71 |
} catch (Exception e) { |
| 72 |
e.printStackTrace(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
class Client extends Thread { |
| 79 |
|
| 80 |
GetOpts opts; |
| 81 |
Socket s; |
| 82 |
|
| 83 |
private static GSSCredential cred; |
| 84 |
|
| 85 |
public Client(Socket s, GetOpts opts) { |
| 86 |
this.s = s; |
| 87 |
this.opts = opts; |
| 88 |
} |
| 89 |
|
| 90 |
private static GSSCredential getCredential(GSSManager manager) |
| 91 |
throws Exception { |
| 92 |
// return null if needed to automatically reload the default creds |
| 93 |
if (cred == null) { |
| 94 |
cred = manager.createCredential(GSSCredential.INITIATE_AND_ACCEPT); |
| 95 |
} |
| 96 |
return cred; |
| 97 |
} |
| 98 |
|
| 99 |
public void run() { |
| 100 |
|
| 101 |
System.out.println("client connected"); |
| 102 |
|
| 103 |
// to make sure we use right impl |
| 104 |
GSSManager manager = new GlobusGSSManagerImpl(); |
| 105 |
ExtendedGSSContext context = null; |
| 106 |
|
| 107 |
try { |
| 108 |
GSSCredential credd = getCredential(manager); |
| 109 |
context = (ExtendedGSSContext)manager.createContext(credd); |
| 110 |
|
| 111 |
context.requestConf(opts.conf); |
| 112 |
|
| 113 |
context.setOption(GSSConstants.GSS_MODE, |
| 114 |
(opts.gsiMode) ? |
| 115 |
GSIConstants.MODE_GSI : |
| 116 |
GSIConstants.MODE_SSL); |
| 117 |
|
| 118 |
context.setOption(GSSConstants.REJECT_LIMITED_PROXY, |
| 119 |
new Boolean(opts.rejectLimitedProxy)); |
| 120 |
|
| 121 |
context.setOption(GSSConstants.REQUIRE_CLIENT_AUTH, |
| 122 |
new Boolean(!opts.anonymity)); |
| 123 |
|
| 124 |
s = GssSocketFactory.getDefault().createSocket(s, null, 0, context); |
| 125 |
|
| 126 |
// server socket |
| 127 |
((GssSocket)s).setUseClientMode(false); |
| 128 |
((GssSocket)s).setWrapMode(opts.wrapMode); |
| 129 |
|
| 130 |
OutputStream out = s.getOutputStream(); |
| 131 |
InputStream in = s.getInputStream(); |
| 132 |
|
| 133 |
System.out.println("Context established."); |
| 134 |
System.out.println("Initiator : " + context.getSrcName()); |
| 135 |
System.out.println("Acceptor : " + context.getTargName()); |
| 136 |
System.out.println("Lifetime : " + context.getLifetime()); |
| 137 |
System.out.println("Privacy : " + context.getConfState()); |
| 138 |
|
| 139 |
GlobusGSSCredentialImpl cred = |
| 140 |
(GlobusGSSCredentialImpl)context.getDelegCred(); |
| 141 |
System.out.println("Delegated credential :"); |
| 142 |
if (cred != null) { |
| 143 |
System.out.println(cred.getX509Credential()); |
| 144 |
} else { |
| 145 |
System.out.println("None"); |
| 146 |
} |
| 147 |
|
| 148 |
String line = null; |
| 149 |
BufferedReader r = new BufferedReader(new InputStreamReader(in)); |
| 150 |
while ( (line = r.readLine()) != null ) { |
| 151 |
if (line.length() == 0) { |
| 152 |
break; |
| 153 |
} |
| 154 |
System.out.println(line); |
| 155 |
} |
| 156 |
|
| 157 |
byte[] msg = |
| 158 |
"HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n".getBytes(); |
| 159 |
|
| 160 |
out.write(msg); |
| 161 |
out.flush(); |
| 162 |
|
| 163 |
} catch (Exception e) { |
| 164 |
e.printStackTrace(); |
| 165 |
} finally { |
| 166 |
try { s.close(); } catch(Exception e) {} |
| 167 |
System.out.println("client disconnected"); |
| 168 |
} |
| 169 |
} |
| 170 |
} |