Debian Codegraph @

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