Search in sources :

Example 1 with NSRecord

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

the class NameResolution method getNSRecordsFromNSField.

/**
   * retrieve all NS records and the corresponding A records from NS field of a JSON object.
   * the key "NS" contains a list of all NS records
   * the key "A" contains a list of all A records, which must be put into ADDITIONAL section
   * 
   */
private static JSONObject getNSRecordsFromNSField(JSONObject fieldResponseJson, String nameToResolve) {
    JSONObject obj = new JSONObject();
    JSONArray aList = new JSONArray();
    JSONArray nsList = new JSONArray();
    /**
       * Format of NS record in GNS:
       * {
       * 	"NS":
       * 		{
       * 			"record":[(ns1, addr1), (ns2, addr2), ...],
       * 			"ttl":int
       * 		}
       * }
       * 
       */
    if (fieldResponseJson.has("NS")) {
        JSONArray records = null;
        int ttl = 3600;
        try {
            JSONObject recordObj = fieldResponseJson.getJSONObject("NS");
            records = recordObj.getJSONArray(ManagedDNSServiceProxy.RECORD_FIELD);
            ttl = recordObj.getInt(ManagedDNSServiceProxy.TTL_FIELD);
        } catch (JSONException e) {
            // something is wrong with the JSON object, return null
            e.printStackTrace();
            return null;
        }
        // The records may contain multiple NS records
        for (int i = 0; i < records.length(); i++) {
            try {
                JSONArray record = records.getJSONArray(i);
                String ns = record.getString(0);
                // It must be an absolute name, i.e., the string must be ended  with a dot, e.g., example.com.
                if (!ns.endsWith(".")) {
                    ns = ns + ".";
                }
                NSRecord nsRecord = new NSRecord(new Name(nameToResolve), DClass.IN, ttl, new Name(ns));
                nsList.put(nsRecord);
                // address can be null as the domain name might use other service as its name server
                if (record.length() == 2) {
                    String address = record.getString(1);
                    ARecord nsARecord = new ARecord(new Name(ns), DClass.IN, ttl, InetAddress.getByName(address));
                    aList.put(nsARecord);
                } else {
                // no IP address in the record for the name server
                }
            } catch (JSONException | TextParseException | UnknownHostException e) {
                // do nothing and trash this record
                e.printStackTrace();
            }
        }
    } else {
    // No NS record, return null
    }
    try {
        obj.put("NS", nsList);
        obj.put("A", aList);
    } catch (JSONException e) {
        // return a null if JSON operation fails
        return null;
    }
    return obj;
}
Also used : ARecord(org.xbill.DNS.ARecord) JSONObject(org.json.JSONObject) UnknownHostException(java.net.UnknownHostException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) NSRecord(org.xbill.DNS.NSRecord) Name(org.xbill.DNS.Name) TextParseException(org.xbill.DNS.TextParseException)

Example 2 with NSRecord

use of org.xbill.DNS.NSRecord 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 3 with NSRecord

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

the class ServiceTest method testNS.

//    @Test
public void testNS() {
    DNSEntryForm nsForm = new DNSEntryForm();
    nsForm.setTtl(8455L);
    nsForm.setName("name3");
    nsForm.setDest("192.3.4.5");
    try {
        Collection<DNSRecord> arecords = configSvc.getDNSByType(DNSType.NS.getValue());
        for (Iterator<DNSRecord> iter = arecords.iterator(); iter.hasNext(); ) {
            DNSRecord arec = iter.next();
            NSRecord newrec = (NSRecord) Record.newRecord(Name.fromString(arec.getName()), arec.getType(), arec.getDclass(), arec.getTtl(), arec.getData());
            System.out.println("target : " + newrec.getTarget());
            System.out.println("name: " + newrec.getName());
        }
    } catch (Exception e) {
    }
}
Also used : DNSRecord(org.nhindirect.config.store.DNSRecord) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) DNSRecord(org.nhindirect.config.store.DNSRecord) NSRecord(org.xbill.DNS.NSRecord) TextParseException(org.xbill.DNS.TextParseException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException)

Example 4 with NSRecord

use of org.xbill.DNS.NSRecord 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 5 with NSRecord

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

the class DNSCertificateStore method lookupDNS.

protected Collection<X509Certificate> lookupDNS(String name) {
    String domain;
    String lookupName = name.replace('@', '.');
    Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
    // get the domain of the address
    int index;
    if ((index = name.indexOf("@")) > -1)
        domain = name.substring(index + 1);
    else
        domain = name;
    try {
        // try the configured servers first
        Lookup lu = new Lookup(new Name(lookupName), Type.CERT);
        // default retries is 3, limite to 2
        lu.setResolver(createExResolver(servers.toArray(new String[servers.size()]), retries, timeout));
        lu.setSearchPath((String[]) null);
        Record[] retRecords = null;
        try {
            retRecords = lu.run();
        } catch (Exception e) {
            LOGGER.warn("Error using recusive DNS CERT lookup for name " + lookupName + "\r\nFalling back to looking up NS record for a targeted search", e);
        }
        if (retRecords == null || retRecords.length == 0) {
            Name tempDomain;
            // try to find the resource's name server records
            // the address may be an alias so check if there is a CNAME record
            lu = new Lookup(new Name(lookupName), Type.CNAME);
            lu.setResolver(createExResolver(servers.toArray(new String[servers.size()]), retries, timeout));
            lu.setSearchPath((String[]) null);
            retRecords = lu.run();
            if (retRecords != null && retRecords.length > 0) {
                CNAMERecord cnameRect = (CNAMERecord) retRecords[0];
                tempDomain = cnameRect.getTarget();
            } else
                // not a CNAME						
                tempDomain = new Name(domain);
            // look for a name server records
            while (tempDomain.labels() > 1) {
                lu = new Lookup(tempDomain, Type.NS);
                lu.setResolver(createExResolver(servers.toArray(new String[servers.size()]), retries, timeout));
                lu.setSearchPath((String[]) null);
                retRecords = lu.run();
                if (retRecords != null && retRecords.length > 0)
                    break;
                tempDomain = new Name(tempDomain.toString().substring((tempDomain.toString().indexOf(".") + 1)));
            }
            if (retRecords == null || retRecords.length == 0)
                // can't find a name server... bail
                return retVal;
            String[] remoteServers = new String[retRecords.length];
            for (int i = 0; i < remoteServers.length - 0; ++i) {
                remoteServers[i] = ((NSRecord) retRecords[i]).getTarget().toString();
            }
            // search the name servers for the cert
            lu = new Lookup(new Name(lookupName), Type.CERT);
            ExtendedResolver remoteResolver = createExResolver(remoteServers, 2, 3);
            if (remoteResolver.getResolvers().length > 0) {
                lu.setResolver(remoteResolver);
                lu.setSearchPath((String[]) null);
                // CLEAR THE CACHE!!!  We are seeing instances where an NXRRSET is cached because
                // a DNS provider is trying to handle a request that it should be delegating
                // The purpose of bypassing the DNS provider and going directly to the NS server
                // is to avoid issues like this
                /*
					 * Change of heart on clearing the DNS cache.  Covering up the NXRRSET hides potential issues
					 * with incorrect DNS configuration.  It is important that NXRRSET issues are discovered and corrected
					 * so all participants in the community participate in a consistent manner.
					 */
                //lu.setCache(new Cache(DClass.IN));
                retRecords = lu.run();
            } else {
                // null out NS records
                retRecords = null;
            }
        }
        if (retRecords != null) {
            retVal = new ArrayList<X509Certificate>();
            for (Record rec : retRecords) {
                if (rec instanceof CERTRecord) {
                    CERTRecord certRec = (CERTRecord) rec;
                    switch(certRec.getCertType()) {
                        case CERTRecord.PKIX:
                            {
                                Certificate certToAdd = convertPKIXRecordToCert(certRec);
                                if (// may not be an X509Cert
                                certToAdd != null && certToAdd instanceof X509Certificate)
                                    retVal.add((X509Certificate) certToAdd);
                                break;
                            }
                        case CERTRecord.URI:
                            {
                                Certificate certToAdd = convertIPKIXRecordToCert(certRec);
                                if (// may not be an X509Cert
                                certToAdd != null && certToAdd instanceof X509Certificate)
                                    retVal.add((X509Certificate) certToAdd);
                                break;
                            }
                        default:
                            {
                                LOGGER.warn("Unknown CERT type " + certRec.getCertType() + " encountered for lookup name" + lookupName);
                            }
                    }
                }
            }
        } else if (// if this is an email address, do the search again and the host level
        domain.length() < name.length())
            retVal = lookupDNS(domain);
    } catch (Exception e) {
        e.printStackTrace();
        throw new NHINDException("", e);
    }
    // add or update the local cert store
    if (retVal != null && retVal.size() > 0 && localStoreDelegate != null) {
        for (X509Certificate cert : retVal) {
            if (localStoreDelegate != null) {
                if (localStoreDelegate.contains(cert))
                    localStoreDelegate.update(cert);
                else
                    localStoreDelegate.add(cert);
            }
        }
        try {
            if (cache != null)
                cache.put(name, retVal);
        } catch (CacheException e) {
        /*
				 * TODO: handle exception
				 */
        }
    }
    return retVal;
}
Also used : ExtendedResolver(org.xbill.DNS.ExtendedResolver) CacheException(org.apache.jcs.access.exception.CacheException) ArrayList(java.util.ArrayList) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) CacheException(org.apache.jcs.access.exception.CacheException) NHINDException(org.nhindirect.stagent.NHINDException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) Name(org.xbill.DNS.Name) CNAMERecord(org.xbill.DNS.CNAMERecord) CERTRecord(org.xbill.DNS.CERTRecord) Lookup(org.xbill.DNS.Lookup) CNAMERecord(org.xbill.DNS.CNAMERecord) CERTRecord(org.xbill.DNS.CERTRecord) NSRecord(org.xbill.DNS.NSRecord) Record(org.xbill.DNS.Record) NSRecord(org.xbill.DNS.NSRecord) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

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