zonefile

package
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2024 License: GPL-3.0, LGPL-3.0 Imports: 6 Imported by: 0

README

go-zonefile

Go package to edit DNS/Bind zone files (preserving formatting & comments)

go-zonefile is not finished: interfaces, documentation and convenience functions are missing. All the lexing and parsing is finished, though.

Online documentation and examples

Bundled as an example (and actually the original reason why I wrote this package) is the inc-zonefile-serial utility, which increments the serial in a zonefile:

package main

import (
	"bytes"
	"fmt"
	"github.com/bwesterb/go-zonefile"
	"io/ioutil"
	"os"
	"strconv"
)

// Increments the serial of a zonefile
func main() {
	if len(os.Args) != 2 {
		fmt.Println("Usage:", os.Args[0], "<path to zonefile>")
		os.Exit(1)
	}

	// Load zonefile
	data, ioerr := ioutil.ReadFile(os.Args[1])
	if ioerr != nil {
		fmt.Println(os.Args[1], ioerr)
		os.Exit(2)
	}

	zf, perr := zonefile.Load(data)
	if perr != nil {
		fmt.Println(os.Args[1], perr.LineNo(), perr)
		os.Exit(3)
	}

	// Find SOA entry
	ok := false
	for _, e := range zf.Entries() {
		if !bytes.Equal(e.Type(), []byte("SOA")) {
			continue
		}
		vs := e.Values()
		if len(vs) != 7 {
			fmt.Println("Wrong number of parameters to SOA line")
			os.Exit(4)
		}
		serial, err := strconv.Atoi(string(vs[2]))
		if err != nil {
			fmt.Println("Could not parse serial:", err)
			os.Exit(5)
		}
		e.SetValue(2, []byte(strconv.Itoa(serial+1)))
		ok = true
		break
	}
	if !ok {
		fmt.Println("Could not find SOA entry")
		os.Exit(6)
	}

	fh, err := os.OpenFile(os.Args[1], os.O_WRONLY, 0)
	if err != nil {
		fmt.Println(os.Args[1], err)
		os.Exit(7)
	}

	_, err = fh.Write(zf.Save())
	if err != nil {
		fmt.Println(os.Args[1], err)
		os.Exit(8)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(data []byte) (r *Zonefile, e *ParsingError)

Parse bytestring containing a zonefile

Types

type Entry

type Entry struct {
	IsControl bool // is this a ($INCLUDE, $TTL, ...) directoive?
	IsComment bool // is this a comment
	// contains filtered or unexported fields
}

Represents an entry in the zonefile

func (Entry) Class

func (e Entry) Class() []byte

Class returns the class for the entry.

func (Entry) Command

func (e Entry) Command() []byte

For a control entry, returns its command (e.g. $TTL, $ORIGIN, ...)

func (Entry) Comments

func (e Entry) Comments() (ret [][]byte)

Comments returns the comments for the entry.

func (Entry) Domain

func (e Entry) Domain() []byte

Domain returns the ownername for the entry.

func (*Entry) SetDomain

func (e *Entry) SetDomain(v []byte) error

Changes the domain in the entry

func (*Entry) SetTTL

func (e *Entry) SetTTL(v int) error

func (*Entry) SetValue

func (e *Entry) SetValue(i int, v []byte) error

Set the the ith value of the entry

func (Entry) TTL

func (e Entry) TTL() *int

TTL retrurns the TTL for the entry (if specified).

func (Entry) Type

func (e Entry) Type() []byte

Type returns the RR Type for the entry.

func (Entry) Values

func (e Entry) Values() (ret [][]byte)

Values returns the fields for the entry.

type ParsingError

type ParsingError struct {
	LineNo int
	ColNo  int
	// contains filtered or unexported fields
}

func (*ParsingError) Error

func (e *ParsingError) Error() string

type Zonefile

type Zonefile struct {
	// contains filtered or unexported fields
}

Represents a DNS masterfile a.k.a. a zonefile

func New

func New() (z *Zonefile)

Create a new empty zonefile

func (*Zonefile) Entries

func (z *Zonefile) Entries() (r []Entry)

List entries in the zonefile

func (*Zonefile) Save

func (z *Zonefile) Save() []byte

Write the zonefile to a bytearray

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL