Exemple #1
0
  /**
   * Looks up Records in the Zone. This follows CNAMEs and wildcards.
   *
   * @param name The name to look up
   * @param type The type to look up
   * @return A SetResponse object
   * @see SetResponse
   */
  public SetResponse findRecords(Name name, short type) {
    SetResponse zr = null;

    Object o = findSets(name, type);
    if (o == null) {
      /* The name does not exist */
      if (name.isWild()) return SetResponse.ofType(SetResponse.NXDOMAIN);

      int labels = name.labels() - origin.labels();
      if (labels == 0) return SetResponse.ofType(SetResponse.NXDOMAIN);
      if (hasWild) {
        SetResponse sr;
        Name tname = name;
        do {
          sr = findRecords(tname.wild(1), type);
          if (!sr.isNXDOMAIN()) return sr;
          tname = new Name(tname, 1);
        } while (labels-- >= 1);
        return sr;
      } else return SetResponse.ofType(SetResponse.NXDOMAIN);
    }

    if (o instanceof TypeMap) {
      /* The name exists but the type does not. */
      return SetResponse.ofType(SetResponse.NXRRSET);
    }

    Object[] objects;
    RRset rrset;

    if (o instanceof RRset) {
      objects = null;
      rrset = (RRset) o;
    } else {
      objects = (Object[]) o;
      rrset = (RRset) objects[0];
    }

    if (name.equals(rrset.getName())) {
      if (type != Type.CNAME && type != Type.ANY && rrset.getType() == Type.CNAME)
        zr = new SetResponse(SetResponse.CNAME, rrset);
      else if (rrset.getType() == Type.NS && !name.equals(origin))
        zr = new SetResponse(SetResponse.DELEGATION, rrset);
      else {
        zr = new SetResponse(SetResponse.SUCCESSFUL);
        zr.addRRset(rrset);
        if (objects != null) {
          for (int i = 1; i < objects.length; i++) zr.addRRset((RRset) objects[i]);
        }
      }
    } else {
      if (rrset.getType() == Type.CNAME) return SetResponse.ofType(SetResponse.NXDOMAIN);
      else if (rrset.getType() == Type.DNAME) {
        zr = new SetResponse(SetResponse.DNAME, rrset);
      } else if (rrset.getType() == Type.NS) {
        zr = new SetResponse(SetResponse.DELEGATION, rrset);
      }
    }
    return zr;
  }
Exemple #2
0
  /** Parses the output of winipcfg or ipconfig. */
  private static void findWin(InputStream in) {
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    try {
      List lserver = new ArrayList();
      List lsearch = new ArrayList();
      String line = null;
      boolean readingServers = false;
      boolean readingSearches = false;
      while ((line = br.readLine()) != null) {
        StringTokenizer st = new StringTokenizer(line);
        if (!st.hasMoreTokens()) {
          readingServers = false;
          readingSearches = false;
          continue;
        }
        String s = st.nextToken();
        if (line.indexOf(":") != -1) {
          readingServers = false;
          readingSearches = false;
        }

        if (line.indexOf("Host Name") != -1) {
          while (st.hasMoreTokens()) s = st.nextToken();
          Name name;
          try {
            name = Name.fromString(s, null);
          } catch (TextParseException e) {
            continue;
          }
          if (name.labels() == 1) continue;
          addSearch(s, lsearch);
        } else if (line.indexOf("Primary Dns Suffix") != -1) {
          while (st.hasMoreTokens()) s = st.nextToken();
          if (s.equals(":")) continue;
          addSearch(s, lsearch);
          readingSearches = true;
        } else if (readingSearches || line.indexOf("DNS Suffix") != -1) {
          while (st.hasMoreTokens()) s = st.nextToken();
          if (s.equals(":")) continue;
          addSearch(s, lsearch);
          readingSearches = true;
        } else if (readingServers || line.indexOf("DNS Servers") != -1) {
          while (st.hasMoreTokens()) s = st.nextToken();
          if (s.equals(":")) continue;
          addServer(s, lserver);
          readingServers = true;
        }
      }

      if (servers == null && lserver.size() > 0)
        servers = (String[]) lserver.toArray(new String[lserver.size()]);
    } catch (IOException e) {
    } finally {
      try {
        br.close();
      } catch (IOException e) {
      }
    }
    return;
  }
Exemple #3
0
 public SIGBase(
     Name name,
     int type,
     int dclass,
     long ttl,
     int covered,
     int alg,
     long origttl,
     Date expire,
     Date timeSigned,
     int footprint,
     Name signer,
     byte[] signature) {
   super(name, type, dclass, ttl);
   Type.check(covered);
   checkU8("alg", alg);
   checkU8("labels", labels);
   TTL.check(origttl);
   checkU16("footprint", footprint);
   this.covered = covered;
   this.alg = alg;
   this.labels = name.labels();
   this.origttl = origttl;
   this.expire = expire;
   this.timeSigned = timeSigned;
   this.footprint = footprint;
   if (!signer.isAbsolute()) throw new RelativeNameException(signer);
   this.signer = signer;
   this.signature = signature;
 }
Exemple #4
0
  /**
   * Creates an array containing fields of the SIG record and the RRsets to be signed/verified.
   *
   * @param sig The SIG record used to sign/verify the rrset.
   * @param rrset The data to be signed/verified.
   * @return The data to be cryptographically signed or verified.
   */
  public static byte[] digestRRset(SIGRecord sig, RRset rrset) {
    DataByteOutputStream out = new DataByteOutputStream();
    digestSIG(out, sig);

    int size = rrset.size();
    byte[][] records = new byte[size][];

    Iterator it = rrset.rrs();
    Name name = rrset.getName();
    Name wild = null;
    if (name.labels() > sig.getLabels()) wild = name.wild(name.labels() - sig.getLabels());
    while (it.hasNext()) {
      Record rec = (Record) it.next();
      if (wild != null) rec = rec.withName(wild);
      records[--size] = rec.toWireCanonical();
    }
    Arrays.sort(records);
    for (int i = 0; i < records.length; i++) out.writeArray(records[i]);
    return out.toByteArray();
  }
Exemple #5
0
 /**
  * Creates an SIG Record from the given data
  *
  * @param covered The RRset type covered by this signature
  * @param alg The cryptographic algorithm of the key that generated the signature
  * @param origttl The original TTL of the RRset
  * @param expire The time at which the signature expires
  * @param timeSigned The time at which this signature was generated
  * @param footprint The footprint/key id of the signing key.
  * @param signer The owner of the signing key
  * @param signature Binary data representing the signature
  */
 public SIGRecord(
     Name name,
     short dclass,
     int ttl,
     int covered,
     int alg,
     int origttl,
     Date expire,
     Date timeSigned,
     int footprint,
     Name signer,
     byte[] signature) {
   this(name, dclass, ttl);
   this.covered = (short) covered;
   this.alg = (byte) alg;
   this.labels = name.labels();
   this.origttl = origttl;
   this.expire = expire;
   this.timeSigned = timeSigned;
   this.footprint = (short) footprint;
   this.signer = signer;
   this.signature = signature;
 }
Exemple #6
0
  /** Finds all matching sets or something that causes the lookup to stop. */
  protected Object findSets(Name name, short type) {
    Object bestns = null;
    Object o;
    Name tname;
    int labels;
    int olabels;
    int tlabels;

    if (!name.subdomain(origin)) return null;
    labels = name.labels();
    olabels = origin.labels();

    for (tlabels = olabels; tlabels <= labels; tlabels++) {
      if (tlabels == olabels) tname = origin;
      else if (tlabels == labels) tname = name;
      else tname = new Name(name, labels - tlabels);
      TypeMap nameInfo = findName(tname);
      if (nameInfo == null) continue;

      /* If this is an ANY lookup, return everything. */
      if (tlabels == labels && type == Type.ANY) return nameInfo.getAll();

      /* Look for an NS */
      if (tlabels > olabels || isCache) {
        o = nameInfo.get(Type.NS);
        if (o != null) {
          if (isCache) bestns = o;
          else return o;
        }
      }

      /* If this is the name, look for the actual type. */
      if (tlabels == labels) {
        o = nameInfo.get(type);
        if (o != null) return o;
      }

      /* If this is the name, look for a CNAME */
      if (tlabels == labels) {
        o = nameInfo.get(Type.CNAME);
        if (o != null) return o;
      }

      /* Look for a DNAME, unless this is the actual name */
      if (tlabels < labels) {
        o = nameInfo.get(Type.DNAME);
        if (o != null) return o;
      }

      /*
       * If this is the name and this is a cache, look for an
       * NXDOMAIN entry.
       */
      if (tlabels == labels && isCache) {
        o = nameInfo.get((short) 0);
        if (o != null) return o;
      }

      /*
       * If this is the name and we haven't matched anything,
       * just return the name.
       */
      if (tlabels == labels) return nameInfo;
    }
    if (bestns == null) return null;
    else return bestns;
  }
Exemple #7
0
  private synchronized SetResponse lookup(Name name, int type) {
    int labels;
    int olabels;
    int tlabels;
    RRset rrset;
    Name tname;
    Object types;
    SetResponse sr;

    if (!name.subdomain(origin)) return SetResponse.ofType(SetResponse.NXDOMAIN);

    labels = name.labels();
    olabels = origin.labels();

    for (tlabels = olabels; tlabels <= labels; tlabels++) {
      boolean isOrigin = (tlabels == olabels);
      boolean isExact = (tlabels == labels);

      if (isOrigin) tname = origin;
      else if (isExact) tname = name;
      else tname = new Name(name, labels - tlabels);

      types = exactName(tname);
      if (types == null) continue;

      /* If this is a delegation, return that. */
      if (!isOrigin) {
        RRset ns = oneRRset(types, Type.NS);
        if (ns != null) return new SetResponse(SetResponse.DELEGATION, ns);
      }

      /* If this is an ANY lookup, return everything. */
      if (isExact && type == Type.ANY) {
        sr = new SetResponse(SetResponse.SUCCESSFUL);
        RRset[] sets = allRRsets(types);
        for (int i = 0; i < sets.length; i++) sr.addRRset(sets[i]);
        return sr;
      }

      /*
       * If this is the name, look for the actual type or a CNAME.
       * Otherwise, look for a DNAME.
       */
      if (isExact) {
        rrset = oneRRset(types, type);
        if (rrset != null) {
          sr = new SetResponse(SetResponse.SUCCESSFUL);
          sr.addRRset(rrset);
          return sr;
        }
        rrset = oneRRset(types, Type.CNAME);
        if (rrset != null) return new SetResponse(SetResponse.CNAME, rrset);
      } else {
        rrset = oneRRset(types, Type.DNAME);
        if (rrset != null) return new SetResponse(SetResponse.DNAME, rrset);
      }

      /* We found the name, but not the type. */
      if (isExact) return SetResponse.ofType(SetResponse.NXRRSET);
    }

    if (hasWild) {
      for (int i = 0; i < labels - olabels; i++) {
        tname = name.wild(i + 1);

        types = exactName(tname);
        if (types == null) continue;

        rrset = oneRRset(types, type);
        if (rrset != null) {
          sr = new SetResponse(SetResponse.SUCCESSFUL);
          sr.addRRset(rrset);
          return sr;
        }
      }
    }

    return SetResponse.ofType(SetResponse.NXDOMAIN);
  }