Search in sources :

Example 1 with CNAMERecord

use of org.xbill.DNS.CNAMERecord in project nhin-d by DirectProject.

the class DNSController method refreshModelFromService.

public void refreshModelFromService(Model model) {
    // GET A RECORDS
    Collection<DNSRecord> arecords = null;
    arecords = getDnsRecords(DNSType.A.getValue());
    final Collection<DNSEntryForm> aform = new ArrayList<DNSEntryForm>();
    if (arecords != null) {
        for (Iterator<DNSRecord> iter = arecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            try {
                final ARecord newrec = (ARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                aform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsARecordResults", aform);
    // GET A4 RECORDS
    Collection<DNSRecord> a4records = null;
    a4records = getDnsRecords(DNSType.AAAA.getValue());
    final Collection<DNSEntryForm> a4form = new ArrayList<DNSEntryForm>();
    if (a4records != null) {
        for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            try {
                final AAAARecord newrec = (AAAARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                a4form.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsA4RecordResults", a4form);
    // GET CNAME RECORDS
    Collection<DNSRecord> crecords = null;
    crecords = getDnsRecords(DNSType.CNAME.getValue());
    final Collection<DNSEntryForm> cform = new ArrayList<DNSEntryForm>();
    if (crecords != null) {
        for (Iterator<DNSRecord> iter = crecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                final CNAMERecord newrec = (CNAMERecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                cform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsCnameRecordResults", cform);
    // GET MX RECORDS
    Collection<DNSRecord> mxrecords = null;
    mxrecords = getDnsRecords(DNSType.MX.getValue());
    final Collection<DNSEntryForm> mxform = new ArrayList<DNSEntryForm>();
    if (mxrecords != null) {
        for (Iterator<DNSRecord> iter = mxrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                final MXRecord newrec = (MXRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setPriority(newrec.getPriority());
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                mxform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsMxRecordResults", mxform);
    // GET Cert RECORDS
    Collection<DNSRecord> certrecords = null;
    certrecords = getDnsRecords(DNSType.CERT.getValue());
    // get the thumbprint and assign
    // create a new collection 
    final Collection<SrvRecord> form = new ArrayList<SrvRecord>();
    CertContainer cont;
    if (certrecords != null) {
        for (Iterator<DNSRecord> iter = certrecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            final SrvRecord srv = new SrvRecord();
            srv.setCreateTime(t.getCreateTime());
            srv.setData(t.getData());
            srv.setDclass(t.getDclass());
            srv.setId(t.getId());
            srv.setName(t.getName());
            srv.setTtl(t.getTtl());
            srv.setType(t.getType());
            srv.setThumb("");
            try {
                final CERTRecord newrec = (CERTRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                String thumb = "";
                byte[] certData = newrec.getCert();
                if (certData != null) {
                    // get the owner from the certificate information
                    // first transform into a certificate
                    cont = toCertContainer(certData);
                    if (cont != null && cont.getCert() != null) {
                        Certificate cert2 = new Certificate();
                        cert2.setData(certData);
                        thumb = getThumbPrint(cont.getCert());
                        srv.setThumb(thumb);
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form.add(srv);
        }
    }
    model.addAttribute("dnsCertRecordResults", form);
    // GET SRV RECORDS
    Collection<DNSRecord> srvrecords = null;
    srvrecords = getDnsRecords(DNSType.SRV.getValue());
    // create a new collection 
    final Collection<SrvRecord> form2 = new ArrayList<SrvRecord>();
    if (srvrecords != null) {
        for (Iterator<DNSRecord> iter = srvrecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            final SrvRecord srv = new SrvRecord();
            try {
                SRVRecord srv4 = (SRVRecord) SRVRecord.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                srv.setCreateTime(t.getCreateTime());
                srv.setData(t.getData());
                srv.setDclass(t.getDclass());
                srv.setId(t.getId());
                srv.setName(t.getName());
                final String name = t.getName();
                // parse the name to get service, protocol, priority , weight,
                // port
                int firstpos = name.indexOf("_");
                if (firstpos == 0) {
                    // then this can be parsed as a srv record
                    // ("_"+SrvdnsForm.getService()+"._"+SrvdnsForm.getProtocol()+"._"+SrvdnsForm.getPriority()+"._"+SrvdnsForm.getWeight()+"._"+SrvdnsForm.getPort()+"._"+SrvdnsForm.getDest()+"."+SrvdnsForm.getName()
                    int secondpos = name.indexOf("._");
                    int thirdpos = name.indexOf(".", secondpos + 2);
                    // from first to second is service
                    final String service_ = name.substring(firstpos + 1, secondpos);
                    srv.setService(service_);
                    // from second to third is protocol
                    final String protocol_ = name.substring(secondpos + 2, thirdpos);
                    ;
                    srv.setProtocol(protocol_);
                    int last2pos = name.indexOf(".", thirdpos);
                    final String name_ = name.substring(last2pos + 1, name.length());
                    srv.setName(name_);
                }
                srv.setTtl(t.getTtl());
                srv.setType(t.getType());
                srv.setPort(srv4.getPort());
                srv.setWeight(srv4.getWeight());
                srv.setPriority("" + srv4.getPriority());
                srv.setTarget("" + srv4.getTarget().toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form2.add(srv);
        }
    }
    model.addAttribute("dnsSrvRecordResults", form2);
    // GET SOA RECORDS
    Collection<DNSRecord> soarecords = null;
    soarecords = getDnsRecords(DNSType.SOA.getValue());
    final Collection<DNSEntryForm> soaform = new ArrayList<DNSEntryForm>();
    if (soarecords != null) {
        for (Iterator<DNSRecord> iter = soarecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                final SOARecord newrec = (SOARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setAdmin("" + newrec.getAdmin());
                tmp.setExpire(newrec.getExpire());
                tmp.setMinimum(newrec.getMinimum());
                tmp.setRefresh(newrec.getRefresh());
                tmp.setRetry(newrec.getRetry());
                tmp.setSerial(newrec.getSerial());
                tmp.setDest("" + newrec.getHost());
                tmp.setDomain("" + newrec.getHost());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                soaform.add(tmp);
            } catch (TextParseException e) {
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsSOARecordResults", soaform);
    // GET NS RECORDS
    Collection<DNSRecord> nsrecords = null;
    nsrecords = getDnsRecords(DNSType.NS.getValue());
    final Collection<DNSEntryForm> nsform = new ArrayList<DNSEntryForm>();
    if (nsrecords != null) {
        for (Iterator<DNSRecord> iter = nsrecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            try {
                NSRecord newrec = (NSRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                nsform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsNSRecordResults", nsform);
    // *****************
    model.addAttribute("NSdnsForm", new DNSEntryForm());
    model.addAttribute("SoadnsForm", new DNSEntryForm());
    model.addAttribute("AdnsForm", new DNSEntryForm());
    model.addAttribute("AAdnsForm", new DNSEntryForm());
    model.addAttribute("CdnsForm", new DNSEntryForm());
    model.addAttribute("MXdnsForm", new DNSEntryForm());
    model.addAttribute("CertdnsForm", new DNSEntryForm());
    model.addAttribute("SrvdnsForm", new DNSEntryForm());
}
Also used : DNSRecord(org.nhindirect.config.model.DNSRecord) AAAARecord(org.xbill.DNS.AAAARecord) ArrayList(java.util.ArrayList) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) IOException(java.io.IOException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) CNAMERecord(org.xbill.DNS.CNAMERecord) SOARecord(org.xbill.DNS.SOARecord) AAAARecord(org.xbill.DNS.AAAARecord) ARecord(org.xbill.DNS.ARecord) CERTRecord(org.xbill.DNS.CERTRecord) MXRecord(org.xbill.DNS.MXRecord) NSRecord(org.xbill.DNS.NSRecord) DNSRecord(org.nhindirect.config.model.DNSRecord) SRVRecord(org.xbill.DNS.SRVRecord) SOARecord(org.xbill.DNS.SOARecord) TextParseException(org.xbill.DNS.TextParseException) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate)

Example 2 with CNAMERecord

use of org.xbill.DNS.CNAMERecord in project nhin-d by DirectProject.

the class MainController method refreshModelFromService.

public void refreshModelFromService(Model model) {
    // GET A RECORDS
    Collection<DNSRecord> arecords = null;
    arecords = getDnsRecords(DNSType.A.getValue());
    Collection<DNSEntryForm> aform = new ArrayList<DNSEntryForm>();
    if (arecords != null) {
        for (DNSRecord t : arecords) {
            try {
                ARecord newrec = (ARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                aform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsARecordResults", aform);
    // GET A4 RECORDS
    Collection<DNSRecord> a4records = null;
    a4records = getDnsRecords(DNSType.AAAA.getValue());
    Collection<DNSEntryForm> a4form = new ArrayList<DNSEntryForm>();
    if (a4records != null) {
        for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                AAAARecord newrec = (AAAARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                a4form.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsA4RecordResults", a4form);
    // GET CNAME RECORDS
    Collection<DNSRecord> crecords = null;
    crecords = getDnsRecords(DNSType.CNAME.getValue());
    Collection<DNSEntryForm> cform = new ArrayList<DNSEntryForm>();
    if (crecords != null) {
        for (Iterator<DNSRecord> iter = crecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                CNAMERecord newrec = (CNAMERecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                cform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsCnameRecordResults", cform);
    // GET MX RECORDS
    Collection<DNSRecord> mxrecords = null;
    mxrecords = getDnsRecords(DNSType.MX.getValue());
    Collection<DNSEntryForm> mxform = new ArrayList<DNSEntryForm>();
    if (mxrecords != null) {
        for (Iterator<DNSRecord> iter = mxrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                MXRecord newrec = (MXRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setPriority(newrec.getPriority());
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                mxform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsMxRecordResults", mxform);
    // GET Cert RECORDS
    Collection<DNSRecord> certrecords = null;
    certrecords = getDnsRecords(DNSType.CERT.getValue());
    // get the thumbprint and assign
    // create a new collection 
    Collection<SrvRecord> form = new ArrayList<SrvRecord>();
    CertContainer cont;
    if (certrecords != null) {
        for (Iterator<DNSRecord> iter = certrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            SrvRecord srv = new SrvRecord();
            srv.setCreateTime(t.getCreateTime());
            srv.setData(t.getData());
            srv.setDclass(t.getDclass());
            srv.setId(t.getId());
            srv.setName(t.getName());
            srv.setTtl(t.getTtl());
            srv.setType(t.getType());
            srv.setThumb("");
            try {
                CERTRecord newrec = (CERTRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                String thumb = "";
                byte[] certData = newrec.getCert();
                if (certData != null) {
                    // get the owner from the certificate information
                    // first transform into a certificate
                    cont = CertUtils.toCertContainer(certData);
                    if (cont != null && cont.getCert() != null) {
                        Certificate cert2 = new Certificate();
                        cert2.setData(certData);
                        thumb = getThumbPrint(cont.getCert());
                        srv.setThumb(thumb);
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form.add(srv);
        }
    }
    model.addAttribute("dnsCertRecordResults", form);
    // GET SRV RECORDS
    Collection<DNSRecord> srvrecords = null;
    srvrecords = getDnsRecords(DNSType.SRV.getValue());
    // create a new collection 
    Collection<SrvRecord> form2 = new ArrayList<SrvRecord>();
    if (srvrecords != null) {
        for (Iterator<DNSRecord> iter = srvrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            SrvRecord srv = new SrvRecord();
            try {
                SRVRecord srv4 = (SRVRecord) SRVRecord.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                srv.setCreateTime(t.getCreateTime());
                srv.setData(t.getData());
                srv.setDclass(t.getDclass());
                srv.setId(t.getId());
                srv.setName(t.getName());
                String name = t.getName();
                // parse the name to get service, protocol, priority , weight,
                // port
                int firstpos = name.indexOf("_");
                if (firstpos == 0) {
                    // then this can be parsed as a srv record
                    // ("_"+SrvdnsForm.getService()+"._"+SrvdnsForm.getProtocol()+"._"+SrvdnsForm.getPriority()+"._"+SrvdnsForm.getWeight()+"._"+SrvdnsForm.getPort()+"._"+SrvdnsForm.getDest()+"."+SrvdnsForm.getName()
                    int secondpos = name.indexOf("._");
                    int thirdpos = name.indexOf(".", secondpos + 2);
                    // from first to second is service
                    String service_ = name.substring(firstpos + 1, secondpos);
                    srv.setService(service_);
                    // from second to third is protocol
                    String protocol_ = name.substring(secondpos + 2, thirdpos);
                    ;
                    srv.setProtocol(protocol_);
                    int last2pos = name.indexOf(".", thirdpos);
                    String name_ = name.substring(last2pos + 1, name.length());
                    srv.setName(name_);
                }
                srv.setTtl(t.getTtl());
                srv.setType(t.getType());
                srv.setPort(srv4.getPort());
                srv.setWeight(srv4.getWeight());
                srv.setPriority("" + srv4.getPriority());
                srv.setTarget("" + srv4.getTarget().toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form2.add(srv);
        }
    }
    model.addAttribute("dnsSrvRecordResults", form2);
    // GET SOA RECORDS
    Collection<DNSRecord> soarecords = null;
    soarecords = getDnsRecords(DNSType.SOA.getValue());
    Collection<DNSEntryForm> soaform = new ArrayList<DNSEntryForm>();
    if (soarecords != null) {
        for (Iterator<DNSRecord> iter = soarecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                SOARecord newrec = (SOARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setAdmin("" + newrec.getAdmin());
                tmp.setExpire(newrec.getExpire());
                tmp.setMinimum(newrec.getMinimum());
                tmp.setRefresh(newrec.getRefresh());
                tmp.setRetry(newrec.getRetry());
                tmp.setSerial(newrec.getSerial());
                tmp.setDest("" + newrec.getHost());
                tmp.setDomain("" + newrec.getHost());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                soaform.add(tmp);
            } catch (TextParseException e) {
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsSOARecordResults", soaform);
    // GET NS RECORDS
    Collection<DNSRecord> nsrecords = null;
    nsrecords = getDnsRecords(DNSType.NS.getValue());
    Collection<DNSEntryForm> nsform = new ArrayList<DNSEntryForm>();
    if (nsrecords != null) {
        for (Iterator<DNSRecord> iter = nsrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                NSRecord newrec = (NSRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                nsform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsNSRecordResults", nsform);
    // *****************
    model.addAttribute("NSdnsForm", new DNSEntryForm());
    model.addAttribute("SoadnsForm", new DNSEntryForm());
    model.addAttribute("AdnsForm", new DNSEntryForm());
    model.addAttribute("AAdnsForm", new DNSEntryForm());
    model.addAttribute("CdnsForm", new DNSEntryForm());
    model.addAttribute("MXdnsForm", new DNSEntryForm());
    model.addAttribute("CertdnsForm", new DNSEntryForm());
    model.addAttribute("SrvdnsForm", new DNSEntryForm());
}
Also used : DNSRecord(org.nhindirect.config.model.DNSRecord) AAAARecord(org.xbill.DNS.AAAARecord) ArrayList(java.util.ArrayList) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) IOException(java.io.IOException) CertContainer(org.nhindirect.config.model.utils.CertUtils.CertContainer) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) CNAMERecord(org.xbill.DNS.CNAMERecord) SOARecord(org.xbill.DNS.SOARecord) AAAARecord(org.xbill.DNS.AAAARecord) ARecord(org.xbill.DNS.ARecord) CERTRecord(org.xbill.DNS.CERTRecord) MXRecord(org.xbill.DNS.MXRecord) NSRecord(org.xbill.DNS.NSRecord) DNSRecord(org.nhindirect.config.model.DNSRecord) SRVRecord(org.xbill.DNS.SRVRecord) SOARecord(org.xbill.DNS.SOARecord) TextParseException(org.xbill.DNS.TextParseException) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate)

Example 3 with CNAMERecord

use of org.xbill.DNS.CNAMERecord in project GNS by MobilityFirst.

the class NameResolution method lookupDnsCache.

/**
   * Look up the local dns server cache.
   * Returns a {@link Message}.
   *
   * @param query
   * @param dnsCache
   * @return a Message
   */
public static Message lookupDnsCache(Message query, Cache dnsCache) {
    // check for queries we can't handle
    int type = query.getQuestion().getType();
    // Was the query legitimate or implemented?
    if (!Type.isRR(type) && type != Type.ANY) {
        return errorMessage(query, Rcode.NOTIMP);
    }
    // extract the domain (guid) and field from the query
    final Name requestedName = query.getQuestion().getName();
    final byte[] rawName = requestedName.toWire();
    final String lookupName = querytoStringForGNS(rawName);
    NameResolution.getLogger().log(Level.FINER, "Looking up name in cache: {0}", lookupName);
    SetResponse lookupresult = dnsCache.lookupRecords(requestedName, Type.ANY, Credibility.NORMAL);
    if (lookupresult.isSuccessful()) {
        Message response = new Message(query.getHeader().getID());
        response.getHeader().setFlag(Flags.QR);
        if (query.getHeader().getFlag(Flags.RD)) {
            response.getHeader().setFlag(Flags.RA);
        }
        response.addRecord(query.getQuestion(), Section.QUESTION);
        response.getHeader().setFlag(Flags.AA);
        ArrayList<Name> cnameNames = new ArrayList<>();
        // Write the response
        for (RRset rrset : lookupresult.answers()) {
            NameResolution.getLogger().log(Level.FINE, "{0}\n", rrset.toString());
            Iterator<?> rrItr = rrset.rrs();
            while (rrItr.hasNext()) {
                Record curRecord = (Record) rrItr.next();
                response.addRecord(curRecord, Section.ANSWER);
                if (curRecord.getType() == Type.CNAME) {
                    cnameNames.add(((CNAMERecord) curRecord).getAlias());
                }
            }
        }
        if (cnameNames.isEmpty()) {
            return response;
        }
        // For all CNAMES in the response, add their A records
        for (Name cname : cnameNames) {
            NameResolution.getLogger().log(Level.FINE, "Looking up CNAME in cache: {0}", cname.toString());
            SetResponse lookUpResult = dnsCache.lookupRecords(cname, Type.ANY, Credibility.NORMAL);
            if (lookUpResult.isSuccessful()) {
                for (RRset rrset : lookUpResult.answers()) {
                    NameResolution.getLogger().log(Level.FINE, "{0}\n", rrset.toString());
                    Iterator<?> rrItr = rrset.rrs();
                    while (rrItr.hasNext()) {
                        Record curRecord = (Record) rrItr.next();
                        response.addRecord(curRecord, Section.ANSWER);
                    }
                }
            }
        }
        return response;
    } else {
        return errorMessage(query, Rcode.NOTIMP);
    }
}
Also used : SetResponse(org.xbill.DNS.SetResponse) Message(org.xbill.DNS.Message) ArrayList(java.util.ArrayList) RRset(org.xbill.DNS.RRset) CNAMERecord(org.xbill.DNS.CNAMERecord) ARecord(org.xbill.DNS.ARecord) Record(org.xbill.DNS.Record) NSRecord(org.xbill.DNS.NSRecord) MXRecord(org.xbill.DNS.MXRecord) Name(org.xbill.DNS.Name)

Example 4 with CNAMERecord

use of org.xbill.DNS.CNAMERecord in project GNS by MobilityFirst.

the class NameResolution method lookupGnsServer.

/**
   * Lookup the query in the GNS server.
   * @param addr 
   * @param query
   * @param handler
   * @return A message with either a good response or an error.
   */
public static Message lookupGnsServer(InetAddress addr, Message query, ClientRequestHandlerInterface handler) {
    // check for queries we can't handle
    int type = query.getQuestion().getType();
    // Was the query legitimate or implemented?
    if (!Type.isRR(type) && type != Type.ANY) {
        return errorMessage(query, Rcode.NOTIMP);
    }
    // extract the domain (guid) and field from the query
    final int fieldName = query.getQuestion().getType();
    final Name requestedName = query.getQuestion().getName();
    final byte[] rawName = requestedName.toWire();
    final String domainName = querytoStringForGNS(rawName);
    // The domain name must be an absolute name, i.e., ended with a dot
    assert (domainName.endsWith(".")) : "The domain name " + domainName + "to resolve is not an absolute name!";
    /**
     *  The query type or domain name can't be null, otherwise return an error message
     */
    if (Type.string(fieldName) == null || domainName == null) {
        return errorMessage(query, Rcode.NXDOMAIN);
    }
    NameResolution.getLogger().log(Level.FINE, "Trying GNS lookup for domain {0}, type {1}", new Object[] { domainName, Type.string(fieldName) });
    /**
     *  Create a response message, build the header first.
     *  The response is constructed later after GNS query.
     */
    Message response = new Message(query.getHeader().getID());
    response.getHeader().setFlag(Flags.QR);
    if (query.getHeader().getFlag(Flags.RD)) {
        response.getHeader().setFlag(Flags.RA);
    }
    response.addRecord(query.getQuestion(), Section.QUESTION);
    response.getHeader().setFlag(Flags.AA);
    /**
     * Request DNS fields of an alias and prepare a DNS response message 
     */
    ArrayList<String> fields = new ArrayList<>(Arrays.asList("A", "NS", "CNAME", "SOA", "PTR", "MX"));
    /**
     * <p>
     * RFC 1034: the additional section "carries RRs(Resource Records) which may be helpful in
     * 			using the RRs in the other section"
     * RFC 2181: data you put in the additional section can never be promoted into real answers.
     * 
     * <p>When a DNS client needs to look up a name used in a program, it queries DNS servers to resolve the name. 
     * Each query message the client sends contains three pieces of information, specifying a question for the server to answer:
     * 1. A specified DNS domain name, stated as a fully qualified domain name (FQDN).
     * 2. A specified query type, which can either specify a resource record (RR) by type or a specialized type of query operation.
     * 3. A specified class for the DNS domain name. For DNS servers running the Windows operating system, this should always be specified as the Internet (IN) class.
     * 
     * <p>The information is retrieved from GNS based on the queried domain.
     * <p>The response is constructed based on the query type,
     * 1. A: return A records in ANSWER section, NS records in AUTHORITY section, A records of name servers in ADDITIONAL section
     * 2. NS: return NS records in ANSWER section, A records of name servers in ADDITIONAL section
     * 3. MX: return MX records in ANSWER section, NS records in AUTHORITY section, A record of name servers in ADDITIONAL section
     * 4. CNAME: return CNAME records in in ANSWER section, NS records in AUTHORITY section, A record of name servers in ADDITIONAL section
     * 
     * Records in ADDITIONAL section is not required, we do a best-effort resolution for the names in ADDITIONAL section.
     */
    long resolveStart = System.currentTimeMillis();
    JSONObject fieldResponseJson = lookupGuidField(addr.getHostAddress().toString(), query.getHeader().getID(), domainName, null, fields, handler);
    if (fieldResponseJson == null) {
        NameResolution.getLogger().log(Level.FINE, "GNS lookup for domain {0} failed.", domainName);
        return errorMessage(query, Rcode.NXDOMAIN);
    }
    NameResolution.getLogger().log(Level.FINE, "fieldResponse all fields (NS, MX, CNAME, A): {0}", fieldResponseJson.toString());
    switch(fieldName) {
        case Type.NS:
            {
                JSONObject obj = getNSRecordsFromNSField(fieldResponseJson, domainName);
                if (obj != null) {
                    try {
                        JSONArray nsList = obj.getJSONArray("NS");
                        JSONArray aList = obj.getJSONArray("A");
                        for (int i = 0; i < nsList.length(); i++) {
                            response.addRecord((Record) nsList.get(i), Section.ANSWER);
                        }
                        for (int i = 0; i < aList.length(); i++) {
                            response.addRecord((Record) aList.get(i), Section.ADDITIONAL);
                        }
                    } catch (JSONException e) {
                    // do nothing, this happens only because some record is corrupted
                    }
                } else {
                    // I don't have the requested A record, you must ask a wrong guy
                    return errorMessage(query, Rcode.NXDOMAIN);
                }
            }
            break;
        case Type.A:
            {
                // Get A records from retrieved GNS record
                JSONArray aList = getARecordsFromAField(fieldResponseJson, domainName);
                if (aList != null) {
                    for (int i = 0; i < aList.length(); i++) {
                        try {
                            response.addRecord((Record) aList.get(i), Section.ANSWER);
                        } catch (JSONException e) {
                        // trash the record
                        }
                    }
                } else {
                    // I don't have the requested A record, you must ask a wrong guy
                    return errorMessage(query, Rcode.NXDOMAIN);
                }
                //Get NS record if we can
                JSONObject obj = getNSRecordsFromNSField(fieldResponseJson, domainName);
                if (obj != null) {
                    try {
                        JSONArray nsList = obj.getJSONArray("NS");
                        JSONArray aNSList = obj.getJSONArray("A");
                        for (int i = 0; i < nsList.length(); i++) {
                            response.addRecord((Record) nsList.get(i), Section.AUTHORITY);
                        }
                        for (int i = 0; i < aNSList.length(); i++) {
                            response.addRecord((Record) aNSList.get(i), Section.ADDITIONAL);
                        }
                    } catch (JSONException e) {
                    // do nothing, this happens only because some record is corrupted
                    }
                }
            }
            break;
        case Type.MX:
            {
                JSONObject obj = getMXRecordsFromMXField(fieldResponseJson, domainName);
                NameResolution.getLogger().log(Level.FINE, "MX record for domain {0} is {1}", new Object[] { domainName, obj });
                if (obj != null) {
                    try {
                        JSONArray mxList = obj.getJSONArray("MX");
                        JSONArray aList = obj.getJSONArray("A");
                        for (int i = 0; i < mxList.length(); i++) {
                            response.addRecord((Record) mxList.get(i), Section.ANSWER);
                        }
                        for (int i = 0; i < aList.length(); i++) {
                            response.addRecord((Record) aList.get(i), Section.ADDITIONAL);
                        }
                    } catch (JSONException e) {
                    // do nothing, this happens only because some record is corrupted
                    }
                } else {
                    // I don't have the requested MX record, you must ask a wrong guy
                    return errorMessage(query, Rcode.NXDOMAIN);
                }
                //Get NS record if we can
                obj = getNSRecordsFromNSField(fieldResponseJson, domainName);
                if (obj != null) {
                    try {
                        JSONArray nsList = obj.getJSONArray("NS");
                        JSONArray aNSList = obj.getJSONArray("A");
                        for (int i = 0; i < nsList.length(); i++) {
                            response.addRecord((Record) nsList.get(i), Section.AUTHORITY);
                        }
                        for (int i = 0; i < aNSList.length(); i++) {
                            response.addRecord((Record) aNSList.get(i), Section.ADDITIONAL);
                        }
                    } catch (JSONException e) {
                    // do nothing, this happens only because some record is corrupted
                    }
                }
            }
            break;
        case Type.CNAME:
            {
                if (fieldResponseJson.has("CNAME")) {
                    // get CNAME alias, no need to resolve it to an IP address
                    try {
                        String cname = fieldResponseJson.getString("CNAME");
                        // The cname must be an absolute name, i.e., ended with a dot
                        if (!cname.endsWith(".")) {
                            cname = cname + ".";
                        }
                        CNAMERecord cnameRecord = new CNAMERecord(new Name(domainName), DClass.IN, 60, new Name(cname));
                        response.addRecord(cnameRecord, Section.ANSWER);
                    } catch (JSONException | TextParseException e) {
                    }
                } else {
                    // I don't have the requested CNAME record, you must ask a wrong guy
                    return errorMessage(query, Rcode.NXDOMAIN);
                }
            }
            break;
        default:
            // we haven't implemented yet
            return errorMessage(query, Rcode.NOTIMPL);
    }
    DelayProfiler.updateDelay("ResolveName", resolveStart);
    NameResolution.getLogger().log(Level.FINER, "Outgoing response from GNS: {0}", response.toString());
    return response;
}
Also used : Message(org.xbill.DNS.Message) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Name(org.xbill.DNS.Name) CNAMERecord(org.xbill.DNS.CNAMERecord) JSONObject(org.json.JSONObject) CNAMERecord(org.xbill.DNS.CNAMERecord) ARecord(org.xbill.DNS.ARecord) Record(org.xbill.DNS.Record) NSRecord(org.xbill.DNS.NSRecord) MXRecord(org.xbill.DNS.MXRecord) JSONObject(org.json.JSONObject)

Example 5 with CNAMERecord

use of org.xbill.DNS.CNAMERecord in project opennms by OpenNMS.

the class DNSServer method addAnswer.

byte addAnswer(final Message response, final Name name, int type, int dclass, int iterations, int flags) {
    SetResponse sr;
    byte rcode = Rcode.NOERROR;
    if (iterations > 6)
        return Rcode.NOERROR;
    if (type == Type.SIG || type == Type.RRSIG) {
        type = Type.ANY;
        flags |= FLAG_SIGONLY;
    }
    final Zone zone = findBestZone(name);
    if (zone != null)
        sr = zone.findRecords(name, type);
    else {
        sr = getCache(dclass).lookupRecords(name, type, Credibility.NORMAL);
    }
    if (sr.isUnknown()) {
        addCacheNS(response, getCache(dclass), name);
    }
    if (sr.isNXDOMAIN()) {
        response.getHeader().setRcode(Rcode.NXDOMAIN);
        if (zone != null) {
            addSOA(response, zone);
            if (iterations == 0)
                response.getHeader().setFlag(Flags.AA);
        }
        rcode = Rcode.NXDOMAIN;
    } else if (sr.isNXRRSET()) {
        if (zone != null) {
            addSOA(response, zone);
            if (iterations == 0)
                response.getHeader().setFlag(Flags.AA);
        }
    } else if (sr.isDelegation()) {
        final RRset nsRecords = sr.getNS();
        addRRset(nsRecords.getName(), response, nsRecords, Section.AUTHORITY, flags);
    } else if (sr.isCNAME()) {
        final CNAMERecord cname = sr.getCNAME();
        addRRset(name, response, new RRset(cname), Section.ANSWER, flags);
        if (zone != null && iterations == 0)
            response.getHeader().setFlag(Flags.AA);
        rcode = addAnswer(response, cname.getTarget(), type, dclass, iterations + 1, flags);
    } else if (sr.isDNAME()) {
        final DNAMERecord dname = sr.getDNAME();
        RRset rrset = new RRset(dname);
        addRRset(name, response, rrset, Section.ANSWER, flags);
        final Name newname;
        try {
            newname = name.fromDNAME(dname);
        } catch (final NameTooLongException e) {
            return Rcode.YXDOMAIN;
        }
        rrset = new RRset(new CNAMERecord(name, dclass, 0, newname));
        addRRset(name, response, rrset, Section.ANSWER, flags);
        if (zone != null && iterations == 0)
            response.getHeader().setFlag(Flags.AA);
        rcode = addAnswer(response, newname, type, dclass, iterations + 1, flags);
    } else if (sr.isSuccessful()) {
        final RRset[] rrsets = sr.answers();
        for (int i = 0; i < rrsets.length; i++) addRRset(name, response, rrsets[i], Section.ANSWER, flags);
        if (zone != null) {
            addNS(response, zone, flags);
            if (iterations == 0)
                response.getHeader().setFlag(Flags.AA);
        } else
            addCacheNS(response, getCache(dclass), name);
    }
    return rcode;
}
Also used : SetResponse(org.xbill.DNS.SetResponse) CNAMERecord(org.xbill.DNS.CNAMERecord) NameTooLongException(org.xbill.DNS.NameTooLongException) DNAMERecord(org.xbill.DNS.DNAMERecord) Zone(org.xbill.DNS.Zone) RRset(org.xbill.DNS.RRset) Name(org.xbill.DNS.Name)

Aggregations

CNAMERecord (org.xbill.DNS.CNAMERecord)6 ArrayList (java.util.ArrayList)5 NSRecord (org.xbill.DNS.NSRecord)5 ARecord (org.xbill.DNS.ARecord)4 MXRecord (org.xbill.DNS.MXRecord)4 Name (org.xbill.DNS.Name)4 X509Certificate (java.security.cert.X509Certificate)3 CERTRecord (org.xbill.DNS.CERTRecord)3 Record (org.xbill.DNS.Record)3 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 CertificateEncodingException (javax.security.cert.CertificateEncodingException)2 ServiceException (org.nhindirect.common.rest.exceptions.ServiceException)2 Certificate (org.nhindirect.config.model.Certificate)2 DNSRecord (org.nhindirect.config.model.DNSRecord)2 DNSEntryForm (org.nhindirect.config.ui.form.DNSEntryForm)2 AAAARecord (org.xbill.DNS.AAAARecord)2 Message (org.xbill.DNS.Message)2 RRset (org.xbill.DNS.RRset)2 SOARecord (org.xbill.DNS.SOARecord)2