Do you use let's encrypt?





6369 votes ~ 21 comments

 

Updating bind serial numbers automatically

Posted by Steve on Fri 31 Mar 2006 at 19:50

If you work with the DNS server bind you'll probably be used to updating the serial number for your zone files manually after making changes. If you're an Emacs user there is a simple automatic way of doing the job.

Thanks to a blog posting by Tollef Fog Heen I learnt today that there is an Emacs mode for working with Zone files. (Perhaps I shouldn't be suprised; there seems to be a mode for everything!)

If you make changes to your zone file Emacs will automatically update the serial number for you, in the canonical format of YYYYMMDDXX (where YYYY is the current year, MM is the current month, DD is the current day, and XX is the revision number).

Simply add the string "-*- zone -*-" to the head of the file. For example:

;; -*- zone -*-
;;
;;  Zone file for example.org
;;
example.org 86400 IN SOA example.org. hostmaster.example.org. (
                                  2006033100   ; Serial YYYYMMDDXX
                                  10800        ; Refresh
                                  3600         ; Retry
                                  3600000      ; Expire
                                  86400 )      ; minimum
     IN      NS      ns1.example.org.
     IN      NS      ns2.example.org.

...

The next time you load the file in Emacs you'll see that you've got syntax highlighting, and if you write any changes to the file the Serial will be incremented appropriately.

Obviously this won't help you if you use another editor, or another nameserver, but it was a neat trick that I thought was worth sharing.

 

 


Re: Updating bind serial numbers automatically
Posted by JulienV (81.50.xx.xx) on Fri 31 Mar 2006 at 21:07
[ View Weblogs ]

Emacs vs vi... again and again, but this time it is a pure hazard!! Christoph Berg published quite the same thing for vi on his blog today: http://www.df7cb.de/blog/2006/03/31#2006-03-31-dns-serials

Cheers,
Julien

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by JulienV (81.50.xx.xx) on Fri 31 Mar 2006 at 21:10
[ View Weblogs ]
Oh, no, it was not a pure hazard. Tollef's post was an answer to Christoph Berg's ;-) Julien

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by haden (193.219.xx.xx) on Sat 1 Apr 2006 at 07:00
I'm using this script with vim:

"--- .../plugins/named.vim ----
function! UPDSERIAL(date, num)
if (strftime("%Y%m%d") == a:date)
return a:date . a:num+1
endif
return strftime("%Y%m%d") . '01'
endfunction

command Soa :%s/\(2[0-9]\{7}\)\([0-9]\{2}\); Serial/\=UPDSERIAL(submatch(1), submatch(2)) . '; Serial'/gc
"---- eof ---------------------

serial is updated calling :Soa

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by Steve (82.41.xx.xx) on Sat 1 Apr 2006 at 23:16
[ View Steve's Scratchpad | View Weblogs ]

Thanks, thats a neat match for Vim.

The big difference is that using that plugin the user must remember to run :Soa or it won't be updated - the Emacs version will automatically do the update for the relevent files.

If it were possible to get the Vim script to run automatically on file saves for files matching a pattern (e.g. /etc/bind/db.*) then it might be more usable.

Any time you're required to do something manually there is a chance you'll forget..

Steve

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by Anonymous (69.128.xx.xx) on Mon 3 Apr 2006 at 00:13
You just need to put something like

autocmd! BufWrite * call s:Incr_Soa()

function s:Incr_Soa()
....

into soa.vim inside ~/.vim/plugin/soa.vim

You could also change it to
autocmd! BufWrite /etc/bind/* call s:Incr_Soa()

to only match on paths inside /etc/bind

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by Anonymous (69.128.xx.xx) on Mon 3 Apr 2006 at 00:19
Whups... the BufWrite event happens after the writing into the file takes place... try

autocmd BufWritePre /etc/bind/db.* call s:Incr_Soa()

which gets fired when the buffer is written, but before it goes into the file.... In any case, :help autocmd will tell you all you need to know

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by Anonymous (84.82.xx.xx) on Fri 10 Dec 2010 at 11:13
Hi,

thanks for this.

A tiny update to make sure it keeps the spacing anyone will use:

"--- .../plugins/named.vim ----
function! UPDSERIAL(date, num)
if (strftime("%Y%m%d") == a:date)
return a:date . a:num+1
endif
return strftime("%Y%m%d") . '01'
endfunction

command Soa :%s/\(2[0-9]\{7}\)\([0-9]\{2}\)\(\s*;\s*Serial\)/\=UPDSERIAL(subm atch(1), submatch(2)) . submatch(3)/gc
"---- eof ---------------------

Essentially I only change the command to match the '; Serial' in a more generic way.

Good luck and thanks again,
Gerke

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by gkephorus (84.82.xx.xx) on Fri 10 Dec 2010 at 11:16
(sorry, forgot to own up for this comment as I forgot to login)

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by uroboros (86.49.xx.xx) on Sun 2 Apr 2006 at 19:03
[ View Weblogs ]
That would be a nice thing if it does not lead to some sort of bad habbit. Consider (for instance) you are manually editing some zone files 50 times per day using such "clever" stuff. Consider you then occasionaly edit zone file on system where there is no such "inteligent" editor installed and you have classical vi only. On my opinion such situation will lead to forgetting serial number increasement, because you would be used to some "marvelous" editor is doing such things for you. Bad, bad thing!

--
If you're smart enough to ask this question, you're smart enough to RTFM and find out yourself.

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by Anonymous (170.224.xx.xx) on Wed 13 Mar 2013 at 16:56
While we're at it, let's just stop using vi, because you might find yourself on a system with only ed. Or hell, let's just stop using computers all together because we might one day need to remember how to calculate things by hand.

This sort of crypto-primitivism drives me crazy. Just because doing something a better way most of the time might occasionally cause us to have to remember something in the exceptional times, isn't a good argument for not improving how we do things.

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by wolodyj (195.136.xx.xx) on Mon 3 Apr 2006 at 10:24
Here is my gawk script (primitive but working), meybe someone find it usefull:


mgrzybowski@mi:~/skrypty/dns$ cat serial.awk
BEGIN {
serail_new=0
serial_old=0
}
{
if ($1 > 2000010101 && $1 < 2050010101)
{
serial_old=$1
tmp=strftime("%Y%m%d")
serial_new=tmp*100+1
while (serial_old >= serial_new)
{
serial_new++
}
print " "serial_new, " ",$2,$3,$4,$5,$6,$7,$8.$9
}
else{
print $0
}
}
END {
}

usage:
gawk -f serial.awk zone >> zone

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by wolodyj (195.136.xx.xx) on Mon 3 Apr 2006 at 10:32
ups :)
usage:
gawk -f serial.awk zone > zone.tmp
cp zone.tmp zone


[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by mar (217.11.xx.xx) on Mon 3 Apr 2006 at 16:10
[ View Weblogs ]
well, i use djbdns and it sends the serial number automatically for me :)

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by paulgear (203.206.xx.xx) on Tue 4 Apr 2006 at 21:45
Why is YYYYMMDDXX the canonical serial number format? I've always much preferred Unix times - easy to generate when you're creating DNS records in a script.

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by Steve (212.20.xx.xx) on Wed 5 Apr 2006 at 11:35
[ View Steve's Scratchpad | View Weblogs ]

I guess it just the defacto standard for people who work with bind - but why? I have no idea.

I know it is the recommended format in O'Reilly's DNS & Bind book, perhaps that has something to do with it?

Steve

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by paulgear (203.206.xx.xx) on Wed 5 Apr 2006 at 12:45
We have a script at work that detects when dhcpd.conf has changed and regenerates DNS from DHCP. So the sequence number could change more than 99 times in 1 day. That's why i prefer Unix times. I know there's no risk of overrun or my script dying badly, and i don't have to track the number of changes.

[ Parent | Reply to this comment ]

Re: Updating bind serial numbers automatically
Posted by Anonymous (84.163.xx.xx) on Sun 9 Apr 2006 at 15:36
The cricket book has certainly had an impact. The other factor probably is it's easy to handle for a human being.

Those who auto-admin their zone files in some way... well, they should know what they're doing anyway, which implies knowing when the "canonical" way is not the right way for them. ;)

Human beings also tend not to edit their zonefiles more than 100 times a day :)

[ Parent | Reply to this comment ]