KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > iiop > security > GSSUPName


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.iiop.security;
25
26 import java.util.*;
27 import java.util.logging.*;
28 import com.sun.logging.*;
29
30 /**
31  * This class implements the GSSAPI exported name functionality
32  * as required by CSIV2.
33  *
34  * @author Sekhar Vajjhala
35  */

36
37 public class GSSUPName {
38     private static java.util.logging.Logger JavaDoc _logger=null;
39     static{
40        _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER);
41         }
42     public static final char AT_CHAR = '@';
43     public static final String JavaDoc AT_STRING = "@";
44     public static final char ESCAPE_CHAR = '\\';
45     public static final String JavaDoc ESCAPE_STRING = "\\";
46
47
48     private String JavaDoc username; // username
49
private String JavaDoc realm; // realmname
50

51     public GSSUPName(String JavaDoc username, String JavaDoc realm)
52     {
53
54     this.username = username;
55     this.realm = realm;
56     }
57
58     /* Construct a GSSUPName from an exported name. This constructor
59      * is for use on the server side.
60      */

61     public GSSUPName(byte[] GSSExportedName)
62     {
63         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc("");
64         StringTokenizer strtok;
65         int realm_index = 0 ; // start of realm
66
int user_index = -1 ; // start of user
67
String JavaDoc expname = "";
68         String JavaDoc name_value = "" ;
69         String JavaDoc name_scope = "" ;
70         byte[] exportedname = {} ;
71
72         if(_logger.isLoggable(Level.FINE)){
73             _logger.log(Level.FINE,"Attempting to create a mechanism specific name from the exported name.");
74         }
75     
76         try {
77             exportedname = GSSUtils.importName(GSSUtils.GSSUP_MECH_OID, GSSExportedName);
78             // extract from the "UTF8" encoding
79
expname = new String JavaDoc(exportedname, "UTF8");
80         } catch (Exception JavaDoc e) {
81              _logger.log(Level.SEVERE,"iiop.importname_exception" , e);
82     }
83
84     if(_logger.isLoggable(Level.FINE)) {
85         _logger.log(Level.FINE,"Mechanism specific name: " + expname);
86     }
87         // Deterimine the starting indices of the username and realm name
88

89         int at_index = expname.indexOf(AT_CHAR);
90         int esc_index = expname.indexOf(ESCAPE_CHAR);
91
92         if (at_index == -1) {
93             /* scoped-username is of the form:
94              * scoped-username ::== name_value
95              */

96             name_value = expname;
97
98         } else if (esc_index == -1 ) {
99             if (at_index != 0) {
100             /* name_value is not null i.e. scoped-username is of the form:
101                  * scoped-username ::= name_value@name_scope
102          */

103                 name_value = expname.substring(0, at_index);
104         }
105             name_scope = expname.substring(at_index+1);
106     } else {
107         // exported name contains both AT_CHAR and ESCAPE_CHAR. Separate
108
// the username and the realm name. The start of a realm name
109
// is indicated by an AT_CHAR that is not preceded by an
110
// ESCAPE_CHAR.
111
// The username always starts at 0.
112

113             user_index = 0;
114             realm_index = 0;
115             int i = 0;
116         while ( (i = expname.indexOf(AT_CHAR, i)) != -1) {
117             if (expname.charAt(i-1) != ESCAPE_CHAR) {
118                     realm_index = i;
119                     break;
120         }
121                 i += 1;
122         }
123             name_value = expname.substring(user_index, realm_index);
124             name_scope = expname.substring(realm_index+1);
125     }
126
127     if(_logger.isLoggable(Level.FINE)) {
128         _logger.log(Level.FINE,"name_value: " + name_value + " ; name_scope: " + name_scope);
129     }
130
131         if ((name_value.length() > 0) && (at_index != -1)) {
132         // remove the ESCAPE_CHAR from the username
133
strbuf = new StringBuffer JavaDoc("");
134             int starti = 0 ; // start index
135
int endi = 0 ; // end index
136

137             while ((endi = name_value.indexOf(ESCAPE_CHAR, starti)) != -1) {
138                 strbuf.append(name_value.substring(starti, endi));
139                 starti = endi + 1;
140         }
141             strbuf.append(name_value.substring(starti));
142             name_value = strbuf.toString();
143     }
144
145         username = name_value;
146         realm = name_scope;
147     if(_logger.isLoggable(Level.FINE)) {
148             _logger.log(Level.FINE,"Constructed GSSUPName ( " + toString()+ " )");
149         }
150     }
151
152     /**
153      * returns and exported name as an array of 1 or more UTF8 characters.
154      */

155     protected byte[] getExportedName() {
156
157         byte[] expname = {} ;
158         byte[] expname_utf8 = {} ;
159         StringTokenizer strtok ;
160
161     if(_logger.isLoggable(Level.FINE)) {
162         _logger.log(Level.FINE,"Going to create exported name for:" + toString());
163     }
164
165         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc("");
166        
167         /* Process the username for special characters AT_CHAR and ESCAPE_CHAR */
168
169         int at_index = username.indexOf(AT_CHAR);
170         int esc_index = username.indexOf(ESCAPE_CHAR);
171         
172         if ( (at_index == -1) && (esc_index == -1))
173         strbuf = new StringBuffer JavaDoc(username); // just copy - no processing required.
174
else {
175
176         // N.B. Order of processing is important
177

178         // Replace the ESCAPE_CHAR first
179
if (esc_index != -1) {
180                 strtok = new StringTokenizer(username, ESCAPE_STRING);
181                 while (strtok.hasMoreTokens()) {
182                     strbuf.append(strtok.nextToken());
183                     strbuf.append(ESCAPE_CHAR).append(ESCAPE_CHAR);
184         }
185         }
186
187             // Replace the AT_CHAR next
188
if (at_index != -1) {
189                 strtok = new StringTokenizer(username, AT_STRING);
190                 while (strtok.hasMoreTokens()) {
191                     strbuf.append(strtok.nextToken());
192                     strbuf.append(ESCAPE_CHAR).append(AT_CHAR);
193         }
194         }
195     }
196
197     if(_logger.isLoggable(Level.FINE)) {
198             _logger.log(Level.FINE,"username after processing for @ and \\: " + strbuf);
199     }
200
201         /** Do not append realm name: this ensures that we dont sent
202             "default" or "certificate" to another appserver.
203
204         // append an AT-CHAR only if realm is not null.
205         // NOTe: In the current implementation, realm will never
206         // be null. It is either "certificate" or "default".
207
208         if (realm.length() > 0) {
209             strbuf.append(AT_CHAR);
210             strbuf.append(realm);
211     }
212         **/

213
214     if(_logger.isLoggable(Level.FINE)) {
215             _logger.log(Level.FINE,"username and realm name : " + strbuf);
216     }
217         try {
218             expname_utf8 = strbuf.toString().getBytes("UTF8");
219             expname = GSSUtils.createExportedName(
220                        GSSUtils.GSSUP_MECH_OID, expname_utf8);
221         } catch (Exception JavaDoc e) {
222              _logger.log(Level.SEVERE,"iiop.createexportedname_exception" , e);
223     }
224
225     if(_logger.isLoggable(Level.FINE)) {
226         _logger.log(Level.FINE,"GSSUPName in exported format = " + GSSUtils.dumpHex(expname));
227     }
228         return expname;
229     }
230
231     public String JavaDoc getRealm() {
232     return realm;
233     }
234
235     public String JavaDoc getUser() {
236         return username;
237     }
238
239     public boolean equals(Object JavaDoc o) {
240     if(o instanceof GSSUPName) {
241         GSSUPName nm = (GSSUPName)o;
242         if (nm.getUser().equals(username) && nm.getRealm().equals(realm))
243         return true;
244     }
245     return false;
246     }
247
248     /* Return the hashCode. */
249     public int hashCode() {
250     return username.hashCode() + realm.hashCode();
251     }
252
253     /* String representation of the GSSUPname */
254     public String JavaDoc toString() {
255     String JavaDoc s = "Username = " + username;
256     s = s + " Realm = " + realm;
257     return s;
258     }
259
260
261     // used locally by this file for test purposes
262
private static void testGSSUP(String JavaDoc user, String JavaDoc realm)
263     {
264         GSSUPName gssname;
265         GSSUPName gssname1;
266
267         _logger.log(Level.FINE,"Running unit test for TestGSSUPName.");
268         _logger.log(Level.FINE,"Creating a GSSUPName instance");
269         gssname = new GSSUPName(user, realm);
270         _logger.log(Level.FINE,"GSSUPName : " + gssname.toString());
271         _logger.log(Level.FINE,"Obtaining an exported name form");
272         byte[] expname = gssname.getExportedName();
273         _logger.log(Level.FINE,"Creating a GSSUPName instance from exported name");
274         gssname1 = new GSSUPName(expname);
275         _logger.log(Level.FINE,"GSSUPName created from exported name: " + gssname1.toString());
276     }
277
278     public static void main(String JavaDoc[] args) {
279         testGSSUP("sekhar@vajjha@la@", "sun.com");
280         testGSSUP("sekhar", "sun.com");
281         testGSSUP("sekhar", "");
282         testGSSUP("", "sun.com");
283     }
284 }
285
Popular Tags