1 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 36 37 public class GSSUPName { 38 private static java.util.logging.Logger _logger=null; 39 static{ 40 _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER); 41 } 42 public static final char AT_CHAR = '@'; 43 public static final String AT_STRING = "@"; 44 public static final char ESCAPE_CHAR = '\\'; 45 public static final String ESCAPE_STRING = "\\"; 46 47 48 private String username; private String realm; 51 public GSSUPName(String username, String realm) 52 { 53 54 this.username = username; 55 this.realm = realm; 56 } 57 58 61 public GSSUPName(byte[] GSSExportedName) 62 { 63 StringBuffer strbuf = new StringBuffer (""); 64 StringTokenizer strtok; 65 int realm_index = 0 ; int user_index = -1 ; String expname = ""; 68 String name_value = "" ; 69 String 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 expname = new String (exportedname, "UTF8"); 80 } catch (Exception 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 89 int at_index = expname.indexOf(AT_CHAR); 90 int esc_index = expname.indexOf(ESCAPE_CHAR); 91 92 if (at_index == -1) { 93 96 name_value = expname; 97 98 } else if (esc_index == -1 ) { 99 if (at_index != 0) { 100 103 name_value = expname.substring(0, at_index); 104 } 105 name_scope = expname.substring(at_index+1); 106 } else { 107 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 strbuf = new StringBuffer (""); 134 int starti = 0 ; int endi = 0 ; 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 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 strbuf = new StringBuffer (""); 166 167 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 (username); else { 175 176 178 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 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 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 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 getRealm() { 232 return realm; 233 } 234 235 public String getUser() { 236 return username; 237 } 238 239 public boolean equals(Object 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 249 public int hashCode() { 250 return username.hashCode() + realm.hashCode(); 251 } 252 253 254 public String toString() { 255 String s = "Username = " + username; 256 s = s + " Realm = " + realm; 257 return s; 258 } 259 260 261 private static void testGSSUP(String user, String 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 [] 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 |