/** * Test for DIRSERVER-191 */ @Test public void testAddAllIntName() throws LdapException, InvalidNameException { LdapName jName = new LdapName( "cn=four,cn=three,cn=two,cn=one" ); Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" ); assertSame( jName, jName.addAll( 0, new LdapName( "cn=zero,cn=zero.5" ) ) ); assertNotSame( aName, aName.add( new Dn( "cn=zero,cn=zero.5" ) ) ); assertNotSame( jName.toString(), aName.toString() ); assertSame( jName, jName.addAll( 2, new LdapName( "cn=zero,cn=zero.5" ) ) ); assertNotSame( aName, aName.add( new Dn( "cn=zero,cn=zero.5" ) ) ); assertNotSame( jName.toString(), aName.toString() ); assertSame( jName, jName.addAll( jName.size(), new LdapName( "cn=zero,cn=zero.5" ) ) ); assertNotSame( aName, aName.add( new Dn( "cn=zero,cn=zero.5" ) ) ); assertNotSame( jName.toString(), aName.toString() ); }
public String prettyPrintCertificate(X509Certificate cert, String newLine) throws InvalidNameException, CertificateEncodingException { StringBuilder sb = new StringBuilder(); sb.append("Subject ").append(cert.getSubjectDN()).append(newLine); sb.append(" Issuer ").append(cert.getIssuerDN()).append(newLine); sb.append(" CN ").append(getCommonName(cert)).append(newLine); sb.append(" From ").append(cert.getNotBefore().toString()).append(newLine); sb.append(" Util ").append(cert.getNotAfter().toString()).append(newLine); sb.append(" Serial ").append(cert.getSerialNumber().toString()).append(newLine); if (sha1 != null) { sha1.update(cert.getEncoded()); sb.append(" SHA1 ").append(toHexString(sha1.digest())).append(newLine); } if (md5 != null) { md5.update(cert.getEncoded()); sb.append(" MD5 ").append(toHexString(md5.digest())).append(newLine); } return sb.toString(); }
private String parseAttrType() throws InvalidNameException { final int beg = cur; while (cur < len) { char c = chars[cur]; if (Character.isLetterOrDigit(c) || c == '.' || c == '-' || c == ' ') { ++cur; } else { break; } } // Back out any trailing spaces. while ((cur > beg) && (chars[cur - 1] == ' ')) { --cur; } if (beg == cur) { throw new InvalidNameException("Invalid name: " + name); } return new String(chars, beg, cur - beg); }
/** * Retrieves the named object. * * @param strName the name of the object to look up * @return the object bound to name */ @Override protected Object doLookup(String strName) { Name name; try { name = getEscapedJndiName(strName); } catch (InvalidNameException e) { log.info(sm.getString("resources.invalidName", strName), e); return null; } if (name.isEmpty()) return this; Entry entry = treeLookup(name); if (entry == null) return null; ZipEntry zipEntry = entry.getEntry(); if (zipEntry.isDirectory()) return new WARDirContext(base, entry); else return new WARResource(entry.getEntry()); }
public LDAPResult getGroupResult(DirContext ctx, String groupID, String[] attrs) { if( !Check.isEmpty(groupIdField) ) { SingleFilter nv1 = new SingleFilter(OBJECTCLASS, getGroupObject()); SingleFilter nv2 = new SingleFilter(groupIdField, groupID); return searchFirstResultAllBases(ctx, new AndFilter(nv1, nv2), new LdapResultHitsCollector(attrs), true); } try { Name name = LDAP.parse(groupID); return new LDAPResult(name, getAttributes(ctx, name, attrs)); } catch( InvalidNameException e ) { LOGGER.debug(e, e); return null; } }
List<Rdn> parseDn() throws InvalidNameException { cur = 0; // ArrayList<Rdn> rdns = // new ArrayList<Rdn>(len / 3 + 10); // leave room for growth ArrayList<Rdn> rdns = new ArrayList<>(len / 3 + 10); // leave room for growth if (len == 0) { return rdns; } rdns.add(doParse(new Rdn())); while (cur < len) { if (chars[cur] == ',' || chars[cur] == ';') { ++cur; rdns.add(0, doParse(new Rdn())); } else { throw new InvalidNameException("Invalid name: " + name); } } return rdns; }
@Override public void bind(Name name, Object obj) throws NamingException { if (name.isEmpty()) { throw new InvalidNameException("Cannot bind empty name"); } Name nm = getMyComponents(name); String atom = nm.get(0); Object inter = iBindings.get(atom); if (nm.size() == 1) { if (inter != null) throw new NameAlreadyBoundException("Use rebind to override"); obj = NamingManager.getStateToBind(obj, new CompositeName().add(atom), this, iEnv); iBindings.put(atom, obj); } else { if (!(inter instanceof Context)) throw new NotContextException(atom + " does not name a context"); ((Context) inter).bind(nm.getSuffix(1), obj); } }
@Override public void rebind(Name name, Object obj) throws NamingException { if (name.isEmpty()) throw new InvalidNameException("Cannot bind empty name"); Name nm = getMyComponents(name); String atom = nm.get(0); if (nm.size() == 1) { obj = NamingManager.getStateToBind(obj, new CompositeName().add(atom), this, iEnv); iBindings.put(atom, obj); } else { Object inter = iBindings.get(atom); if (!(inter instanceof Context)) throw new NotContextException(atom + " does not name a context"); ((Context) inter).rebind(nm.getSuffix(1), obj); } }
@Override public void unbind(Name name) throws NamingException { if (name.isEmpty()) throw new InvalidNameException("Cannot unbind empty name"); Name nm = getMyComponents(name); String atom = nm.get(0); if (nm.size() == 1) { iBindings.remove(atom); } else { Object inter = iBindings.get(atom); if (!(inter instanceof Context)) throw new NotContextException(atom + " does not name a context"); ((Context) inter).unbind(nm.getSuffix(1)); } }
@Override public Context createSubcontext(Name name) throws NamingException { if (name.isEmpty()) throw new InvalidNameException("Cannot bind empty name"); Name nm = getMyComponents(name); String atom = nm.get(0); Object inter = iBindings.get(atom); if (nm.size() == 1) { if (inter != null) throw new NameAlreadyBoundException("Use rebind to override"); Context child = createCtx(this, atom, iEnv); iBindings.put(atom, child); return child; } else { if (!(inter instanceof Context)) throw new NotContextException(atom + " does not name a context"); return ((Context) inter).createSubcontext(nm.getSuffix(1)); } }
private int decodeName(int pos, DnsName n) throws InvalidNameException { if (msg[pos] == 0) { // end of name n.add(0, ""); return (pos + 1); } else if ((msg[pos] & 0xC0) != 0) { // name compression decodeName(getUShort(pos) & 0x3FFF, n); return (pos + 2); } else { // append a label int len = msg[pos++]; try { n.add(0, new String(msg, pos, len, "ISO-8859-1")); } catch (java.io.UnsupportedEncodingException e) { // assert false : "ISO-Latin-1 charset unavailable"; } return decodeName(pos + len, n); } }
private String decodeSoa(int pos) throws InvalidNameException { DnsName mname = new DnsName(); pos = decodeName(pos, mname); DnsName rname = new DnsName(); pos = decodeName(pos, rname); long serial = getUInt(pos); pos += 4; long refresh = getUInt(pos); pos += 4; long retry = getUInt(pos); pos += 4; long expire = getUInt(pos); pos += 4; long minimum = getUInt(pos); // now used as negative TTL pos += 4; return (mname + " " + rname + " " + serial + " " + refresh + " " + retry + " " + expire + " " + minimum); }
private String decodeNaptr(int pos) throws InvalidNameException { int order = getUShort(pos); pos += 2; int preference = getUShort(pos); pos += 2; StringBuffer flags = new StringBuffer(); pos += decodeCharString(pos, flags); StringBuffer services = new StringBuffer(); pos += decodeCharString(pos, services); StringBuffer regexp = new StringBuffer(rdlen); pos += decodeCharString(pos, regexp); DnsName replacement = decodeName(pos); return (order + " " + preference + " " + flags + " " + services + " " + regexp + " " + replacement); }
private AuthenticationIdentity constructAuthenticationIdentity(X509Certificate certificate) { AuthenticationIdentity identity = new AuthenticationIdentity(); try { LdapName ln = new LdapName(certificate.getSubjectDN().getName()); for(Rdn rdn : ln.getRdns()) { if(rdn.getType().equalsIgnoreCase("GIVENNAME")) { identity.setGivenName(rdn.getValue().toString()); } else if(rdn.getType().equalsIgnoreCase("SURNAME")) { identity.setSurName(rdn.getValue().toString()); } else if(rdn.getType().equalsIgnoreCase("SERIALNUMBER")) { identity.setIdentityCode(rdn.getValue().toString().split("-")[1]); } else if(rdn.getType().equalsIgnoreCase("C")) { identity.setCountry(rdn.getValue().toString()); } } return identity; } catch (InvalidNameException e) { logger.error("Error getting authentication identity from the certificate", e); throw new TechnicalErrorException("Error getting authentication identity from the certificate", e); } }
/** * Adds components to the end of remaining name. * * @param name The components to add. Can be null. * @see #getRemainingName * @see #setRemainingName * @see #appendRemainingComponent */ public void appendRemainingName(Name name) { // System.out.println("appendingRemainingName: " + name.toString()); // Exception e = new Exception(); // e.printStackTrace(); if (name != null) { if (this.remainingName != null) { try { this.remainingName.addAll(name); } catch (InvalidNameException e) { // ignore; shouldn't happen for composite name } } else { this.remainingName = (Name)(name.clone()); } } }
private String parseStringAttrValue() throws InvalidNameException { final int beg = cur; int esc = -1; // index of the most recently escaped character while ((cur < len) && !atTerminator()) { if (chars[cur] == '\\') { ++cur; // consume backslash, then what follows esc = cur; } ++cur; } if (cur > len) { // 'twas backslash followed by nothing throw new InvalidNameException("Invalid name: " + name); } // Trim off (unescaped) trailing whitespace. int end; for (end = cur; end > beg; end--) { if (!isWhitespace(chars[end - 1]) || (esc == end - 1)) { break; } } return new String(chars, beg, end - beg); }
private Rdn doParse(Rdn rdn) throws InvalidNameException { while (cur < len) { consumeWhitespace(); String attrType = parseAttrType(); consumeWhitespace(); if (cur >= len || chars[cur] != '=') { throw new InvalidNameException("Invalid name: " + name); } ++cur; // consume '=' consumeWhitespace(); String value = parseAttrValue(); consumeWhitespace(); rdn.put(attrType, Rdn.unescapeValue(value)); if (cur >= len || chars[cur] != '+') { break; } ++cur; // consume '+' } rdn.sort(); return rdn; }
private String parseQuotedAttrValue() throws InvalidNameException { final int beg = cur; ++cur; // consume '"' while ((cur < len) && chars[cur] != '"') { if (chars[cur] == '\\') { ++cur; // consume backslash, then what follows } ++cur; } if (cur >= len) { // no closing quote throw new InvalidNameException("Invalid name: " + name); } ++cur; // consume closing quote return new String(chars, beg, cur - beg); }
/** * Constructs an Rdn from the given attribute set. See * {@link javax.naming.directory.Attributes Attributes}. * <p> * The string attribute values are not interpreted as * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> * formatted RDN strings. That is, the values are used * literally (not parsed) and assumed to be unescaped. * * @param attrSet The non-null and non-empty attributes containing * type/value mappings. * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot * be used to construct a valid RDN. */ public Rdn(Attributes attrSet) throws InvalidNameException { if (attrSet.size() == 0) { throw new InvalidNameException("Attributes cannot be empty"); } entries = new ArrayList<>(attrSet.size()); NamingEnumeration<? extends Attribute> attrs = attrSet.getAll(); try { for (int nEntries = 0; attrs.hasMore(); nEntries++) { RdnEntry entry = new RdnEntry(); Attribute attr = attrs.next(); entry.type = attr.getID(); entry.value = attr.get(); entries.add(nEntries, entry); } } catch (NamingException e) { InvalidNameException e2 = new InvalidNameException( e.getMessage()); e2.initCause(e); throw e2; } sort(); // arrange entries for comparison }
/** * Constructs an Rdn from the given attribute set. See * {@link javax.naming.directory.Attributes Attributes}. * <p> * The string attribute values are not interpreted as * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> * formatted RDN strings. That is, the values are used * literally (not parsed) and assumed to be unescaped. * * @param attrSet The non-null and non-empty attributes containing * type/value mappings. * @throws InvalidNameException If contents of {@code attrSet} cannot * be used to construct a valid RDN. */ public Rdn(Attributes attrSet) throws InvalidNameException { if (attrSet.size() == 0) { throw new InvalidNameException("Attributes cannot be empty"); } entries = new ArrayList<>(attrSet.size()); NamingEnumeration<? extends Attribute> attrs = attrSet.getAll(); try { for (int nEntries = 0; attrs.hasMore(); nEntries++) { RdnEntry entry = new RdnEntry(); Attribute attr = attrs.next(); entry.type = attr.getID(); entry.value = attr.get(); entries.add(nEntries, entry); } } catch (NamingException e) { InvalidNameException e2 = new InvalidNameException( e.getMessage()); e2.initCause(e); throw e2; } sort(); // arrange entries for comparison }
/** * Compares this principal to the specified object. * * @param object The object to compare this principal against. * @return true if they are equal; false otherwise. */ public boolean equals(Object object) { if (this == object) { return true; } if (object instanceof LdapPrincipal) { try { return name.equals(getLdapName(((LdapPrincipal)object).getName())); } catch (InvalidNameException e) { return false; } } return false; }
/** * Returns true if the two provided DNs are equivalent, regardless of the order of the elements. Returns false if one or both are invalid DNs. * * Example: * * CN=test1, O=testOrg, C=US compared to CN=test1, O=testOrg, C=US -> true * CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test1, C=US -> true * CN=test1, O=testOrg, C=US compared to CN=test2, O=testOrg, C=US -> false * CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test2, C=US -> false * CN=test1, O=testOrg, C=US compared to -> false * compared to -> true * * @param dn1 the first DN to compare * @param dn2 the second DN to compare * @return true if the DNs are equivalent, false otherwise */ public static boolean compareDNs(String dn1, String dn2) { if (dn1 == null) { dn1 = ""; } if (dn2 == null) { dn2 = ""; } if (StringUtils.isEmpty(dn1) || StringUtils.isEmpty(dn2)) { return dn1.equals(dn2); } try { List<Rdn> rdn1 = new LdapName(dn1).getRdns(); List<Rdn> rdn2 = new LdapName(dn2).getRdns(); return rdn1.size() == rdn2.size() && rdn1.containsAll(rdn2); } catch (InvalidNameException e) { logger.warn("Cannot compare DNs: {} and {} because one or both is not a valid DN", dn1, dn2); return false; } }
private boolean matches(int beg, int end, Name n) { if (n instanceof LdapName) { LdapName ln = (LdapName) n; return doesListMatch(beg, end, ln.rdns); } else { for (int i = beg; i < end; i++) { Rdn rdn; String rdnString = n.get(i - beg); try { rdn = (new Rfc2253Parser(rdnString)).parseRdn(); } catch (InvalidNameException e) { return false; } if (!rdn.equals(rdns.get(i))) { return false; } } } return true; }
private static String getSubjectName(final X509Certificate cert) throws InvalidNameException { final String fullSubjectDn = cert.getSubjectX500Principal().getName(); final LdapName fullSubjectLn = new LdapName(fullSubjectDn); for (final Rdn rdn: fullSubjectLn.getRdns()) { if ("CN".equalsIgnoreCase(rdn.getType())) { return rdn.getValue().toString(); } } throw new InvalidNameException("Common name not found"); }
/** * Test for DIRSERVER-191 */ @Test public void testName() throws LdapException, InvalidNameException { LdapName jName = new javax.naming.ldap.LdapName( "cn=four,cn=three,cn=two,cn=one" ); Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" ); assertEquals( jName.toString(), "cn=four,cn=three,cn=two,cn=one" ); assertEquals( aName.toString(), "cn=four,cn=three,cn=two,cn=one" ); assertEquals( jName.toString(), aName.toString() ); }
/** * Test for DIRSERVER-191 */ @Test public void testGetPrefixName() throws LdapException, InvalidNameException { LdapName jName = new LdapName( "cn=four,cn=three,cn=two,cn=one" ); Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" ); assertEquals( jName.getPrefix( 0 ).toString(), aName.getAncestorOf( "cn=four,cn=three,cn=two,cn=one" ) .toString() ); assertEquals( jName.getPrefix( 1 ).toString(), aName.getAncestorOf( "cn=four,cn=three,cn=two" ).toString() ); assertEquals( jName.getPrefix( 2 ).toString(), aName.getAncestorOf( "cn=four,cn=three" ).toString() ); assertEquals( jName.getPrefix( 3 ).toString(), aName.getAncestorOf( "cn=four" ).toString() ); assertEquals( jName.getPrefix( 4 ).toString(), aName.getAncestorOf( "" ).toString() ); }
/** * Test for DIRSERVER-191 */ @Test public void testGetSuffix() throws LdapException, InvalidNameException { LdapName jName = new LdapName( "cn=four,cn=three,cn=two,cn=one" ); Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" ); assertEquals( jName.getSuffix( 0 ).toString(), aName.getDescendantOf( "" ).toString() ); assertEquals( jName.getSuffix( 1 ).toString(), aName.getDescendantOf( "cn=one" ).toString() ); assertEquals( jName.getSuffix( 2 ).toString(), aName.getDescendantOf( "cn=two,cn=one" ).toString() ); assertEquals( jName.getSuffix( 3 ).toString(), aName.getDescendantOf( "cn=three,cn=two,cn=one" ).toString() ); assertEquals( jName.getSuffix( 4 ).toString(), aName.getDescendantOf( "cn=four,cn=three,cn=two,cn=one" ) .toString() ); }
/** * Test for DIRSERVER-191. The Dn is immutable, thus we can't add a new Rdn * to a Dn, it simply creates a new one. */ @Test public void testAddStringName() throws LdapException, InvalidNameException { LdapName jName = new LdapName( "cn=four,cn=three,cn=two,cn=one" ); Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" ); assertSame( jName, jName.add( "cn=five" ) ); assertNotSame( aName, aName.add( "cn=five" ) ); assertNotSame( jName.toString(), aName.toString() ); }
/** * Test for DIRSERVER-191 */ @Test public void testAddAllName() throws LdapException, InvalidNameException { LdapName jName = new LdapName( "cn=four,cn=three,cn=two,cn=one" ); Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" ); assertSame( jName, jName.addAll( new LdapName( "cn=seven,cn=six" ) ) ); assertNotSame( aName, aName.add( new Dn( "cn=seven,cn=six" ) ) ); assertNotSame( jName.toString(), aName.toString() ); }
/** * Test for DIRSERVER-191 */ @Test public void testStartsWithName() throws LdapException, InvalidNameException { LdapName jName = new LdapName( "cn=four,cn=three,cn=two,cn=one" ); Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" ); assertEquals( jName.startsWith( new LdapName( "cn=seven,cn=six,cn=five" ) ), aName.isDescendantOf( new Dn( "cn=seven,cn=six,cn=five" ) ) ); assertEquals( jName.startsWith( new LdapName( "cn=three,cn=two,cn=one" ) ), aName.isDescendantOf( new Dn( "cn=three,cn=two,cn=one" ) ) ); }