2005-08-14 02:08:48 +00:00
|
|
|
// $Id$
|
2005-08-13 23:18:03 +00:00
|
|
|
//
|
|
|
|
// Copyright (C) 2003 VeriSign, Inc.
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA
|
|
|
|
|
|
|
|
package com.verisignlabs.dnssec.security;
|
|
|
|
|
2005-08-14 02:08:48 +00:00
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.xbill.DNS.Master;
|
|
|
|
import org.xbill.DNS.Name;
|
|
|
|
import org.xbill.DNS.RRset;
|
|
|
|
import org.xbill.DNS.Record;
|
|
|
|
import org.xbill.DNS.Type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class contains a bunch of utility methods that are generally useful in
|
|
|
|
* manipulating zones.
|
|
|
|
*
|
|
|
|
* @author David Blacka (original)
|
|
|
|
* @author $Author$
|
|
|
|
* @version $Revision$
|
2005-08-13 23:18:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class ZoneUtils
|
|
|
|
{
|
2005-08-14 02:08:48 +00:00
|
|
|
/**
|
|
|
|
* Load a zone file.
|
|
|
|
*
|
2009-02-02 05:01:03 +00:00
|
|
|
* @param zonefile
|
|
|
|
* the filename/path of the zonefile to read.
|
|
|
|
* @param origin
|
|
|
|
* the origin to use for the zonefile (may be null if the origin is
|
|
|
|
* specified in the zone file itself).
|
2005-08-14 02:08:48 +00:00
|
|
|
* @return a {@link java.util.List} of {@link org.xbill.DNS.Record} objects.
|
2009-02-02 05:01:03 +00:00
|
|
|
* @throws IOException
|
|
|
|
* if something goes wrong reading the zone file.
|
2005-08-13 23:18:03 +00:00
|
|
|
*/
|
2011-02-12 21:25:42 +00:00
|
|
|
public static List<Record> readZoneFile(String zonefile, Name origin) throws IOException
|
2005-08-13 23:18:03 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
ArrayList<Record> records = new ArrayList<Record>();
|
2011-02-09 23:58:54 +00:00
|
|
|
Master m;
|
|
|
|
if (zonefile.equals("-"))
|
|
|
|
{
|
|
|
|
m = new Master(System.in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m = new Master(zonefile, origin);
|
|
|
|
}
|
2005-08-13 23:18:03 +00:00
|
|
|
|
|
|
|
Record r = null;
|
|
|
|
|
2005-08-14 02:08:48 +00:00
|
|
|
while ((r = m.nextRecord()) != null)
|
2005-08-13 23:18:03 +00:00
|
|
|
{
|
|
|
|
records.add(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
return records;
|
|
|
|
}
|
|
|
|
|
2005-08-14 02:08:48 +00:00
|
|
|
/**
|
|
|
|
* Write the records out into a zone file.
|
|
|
|
*
|
2009-02-02 05:01:03 +00:00
|
|
|
* @param records
|
|
|
|
* a {@link java.util.List} of {@link org.xbill.DNS.Record} objects
|
|
|
|
* forming a zone.
|
|
|
|
* @param zonefile
|
|
|
|
* the file to write to. If null or equal to "-", System.out is used.
|
2005-08-13 23:18:03 +00:00
|
|
|
*/
|
2011-02-12 21:25:42 +00:00
|
|
|
public static void writeZoneFile(List<Record> records, String zonefile) throws IOException
|
2005-08-13 23:18:03 +00:00
|
|
|
{
|
|
|
|
PrintWriter out = null;
|
|
|
|
|
|
|
|
if (zonefile == null || zonefile.equals("-"))
|
|
|
|
{
|
|
|
|
out = new PrintWriter(System.out);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out = new PrintWriter(new BufferedWriter(new FileWriter(zonefile)));
|
|
|
|
}
|
|
|
|
|
2011-02-12 21:25:42 +00:00
|
|
|
for (Record r : records)
|
2005-08-13 23:18:03 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
out.println(r);
|
2005-08-13 23:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
2005-08-14 02:08:48 +00:00
|
|
|
/**
|
|
|
|
* Given just the list of records, determine the zone name (origin).
|
|
|
|
*
|
2009-02-02 05:01:03 +00:00
|
|
|
* @param records
|
2011-02-12 21:25:42 +00:00
|
|
|
* a list of {@link org.xbill.DNS.Record} objects.
|
2010-12-05 23:08:13 +00:00
|
|
|
* @return the zone name, if found. null if one couldn't be found.
|
2005-08-13 23:18:03 +00:00
|
|
|
*/
|
2011-02-12 21:25:42 +00:00
|
|
|
public static Name findZoneName(List<Record> records)
|
2005-08-13 23:18:03 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
for (Record r : records)
|
2005-08-13 23:18:03 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
int type = r.getType();
|
2005-08-14 02:08:48 +00:00
|
|
|
|
2011-02-12 21:25:42 +00:00
|
|
|
if (type == Type.SOA) return r.getName();
|
2005-08-13 23:18:03 +00:00
|
|
|
}
|
2005-08-14 02:08:48 +00:00
|
|
|
|
2005-08-13 23:18:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-08-23 19:13:42 +00:00
|
|
|
|
2011-02-12 21:25:42 +00:00
|
|
|
public static List<Record> findRRs(List<Record> records, Name name, int type)
|
2009-02-05 05:04:30 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
List<Record> res = new ArrayList<Record>();
|
|
|
|
for (Record r : records)
|
2009-02-05 05:04:30 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
if (r.getName().equals(name) && r.getType() == type)
|
2009-02-05 05:04:30 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
res.add(r);
|
2009-02-05 05:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-23 19:13:42 +00:00
|
|
|
|
2009-02-05 05:04:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-08-23 19:13:42 +00:00
|
|
|
/** This is an alternate way to format an RRset into a string */
|
2011-02-12 21:25:42 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2009-08-23 19:13:42 +00:00
|
|
|
public static String rrsetToString(RRset rrset, boolean includeSigs)
|
|
|
|
{
|
2010-12-07 05:31:58 +00:00
|
|
|
StringBuilder out = new StringBuilder();
|
2009-08-23 19:13:42 +00:00
|
|
|
|
2011-02-12 21:25:42 +00:00
|
|
|
for (Iterator<Record> i = rrset.rrs(false); i.hasNext();)
|
2009-08-23 19:13:42 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
Record r = i.next();
|
2009-08-23 19:13:42 +00:00
|
|
|
out.append(r.toString());
|
|
|
|
out.append("\n");
|
|
|
|
}
|
2011-02-12 21:25:42 +00:00
|
|
|
|
2009-08-23 19:13:42 +00:00
|
|
|
if (includeSigs)
|
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
for (Iterator<Record> i = rrset.sigs(); i.hasNext();)
|
2009-08-23 19:13:42 +00:00
|
|
|
{
|
2011-02-12 21:25:42 +00:00
|
|
|
Record r = i.next();
|
2009-08-23 19:13:42 +00:00
|
|
|
out.append(r.toString());
|
|
|
|
out.append("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.toString();
|
|
|
|
}
|
|
|
|
}
|