Initial commit.
[manu/ddnsedit.git] / ddnsedit
1 #!/bin/bash
2 #
3 # Name: ddnsedit
4 # Description: Edit a dynamic dns zone with an editor, then update it by
5 # calling "nsupdate"
6 #
7 # Copyright (C) 2008  Emmanuel Lacour <elacour@home-dn.net>
8
9 #  This program is free software; you can redistribute it and/or modify
10 #  it under the terms of the GNU General Public License as published by
11 #  the Free Software Foundation; either version 2 of the License, or
12 #  (at your option) any later version.
13
14 #  This program is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18
19 #  You should have received a copy of the GNU General Public License
20 #  along with this program; if not, write to the Free Software
21 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
22 #  USA.
23
24
25
26 SERVER=$1
27 ZONE=$2
28
29
30 function usage {
31     echo "Usage: `basename $0` SERVER ZONE_NAME"
32     exit 0
33 }
34
35 if [ ! -n "$ZONE" ]
36 then
37     echo "Missing zone name!"
38     usage
39 fi
40
41 if [ ! -n "$SERVER" ]
42 then
43     echo "Missing server!"
44     usage
45 fi
46
47
48 # Cleanup zone name
49 if [ `echo "$ZONE" | grep -c '\.$'` -eq 0 ]
50 then
51     ZONE="$ZONE."
52 fi
53
54 FILE_O=`mktemp /tmp/nsedit_o.XXXXXX`
55 FILE_N=`mktemp /tmp/nsedit_n.XXXXXX`
56 FILE_D=`mktemp /tmp/nsedit_d.XXXXXX`
57 FILE_U=`mktemp /tmp/nsedit_u.XXXXXX`
58 FILE_ERR=`mktemp /tmp/nsedit_err.XXXXXX`
59
60 # Get the current zone
61 # (skip SOA wich cannot be updated by nsupdate)
62 dig @$SERVER $ZONE AXFR | grep -Ev '^;|^$' | grep -v 'IN        SOA     ' > $FILE_O
63
64 # Duplicate the zone
65 cat $FILE_O > $FILE_N
66
67 function main {
68     # Edit the zone
69     $EDITOR $FILE_N
70
71     # Diff between old and new file
72     diff -au $FILE_O $FILE_N | grep -E '^-|^\+' | grep -Ev '^--- |^\+\+\+ ' > $FILE_D
73
74     if [ ! -s $FILE_D ]
75     then
76         echo "Empty changes, exiting!"
77         exit 0
78     fi
79
80     # Prepare nsupdate file
81     echo "server $SERVER" > $FILE_U
82     # Diff between zones
83     sed 's/^-/update delete /g;s/^+/update add /g' $FILE_D >> $FILE_U
84     echo 'send' >> $FILE_U
85
86     echo "Do you really wan't to send the following updates?"
87     cat $FILE_U
88     echo -n "Answer (y/n):"
89     read a
90
91     if [ "$a" == "y" ]
92     then
93         nsupdate $FILE_U > $FILE_ERR 2>&1
94         if [ $? -ne 0 ]
95         then
96             echo "Update failed: "
97             cat $FILE_ERR
98             echo -n "Do you wan't to edit back you're file (y/n): "
99             read a
100             if [ "$a" == "y" ]
101             then
102                 main
103             else
104                 echo "Ok, exiting..."
105             fi
106         else
107             echo "Update succeded!"
108             cat $FILE_ERR
109         fi
110     else
111         echo "Ok, exiting..."
112     fi
113 }
114
115 main
116
117 rm -f $FILE_O $FILE_N $FILE_D $FILE_U $FILE_ERR