1 | #!/bin/sh -
|
---|
2 | #
|
---|
3 | # KISS Dynamic DNS service (no locking (hint lockf wrapper), no advanced checking)
|
---|
4 | # 'ping' is based on ssh call or CGI call
|
---|
5 | #
|
---|
6 | # = INSTALL - SSH =
|
---|
7 | # 1) Make sure to allow rndc reload in your sudoers file
|
---|
8 | # sudo_user ALL=(ALL) NOPASSWD: /usr/sbin/rndc reload
|
---|
9 | # 2) prefix your ssh authorized key to allow only this script
|
---|
10 | # command="/home/rvdzwet/dyndns.sh"
|
---|
11 | # 3) Call for the remote host like this
|
---|
12 | # ssh -a -p 1022 -i id_rsa zweot.vanderzwet.net `hostname`
|
---|
13 | #
|
---|
14 | # = INSTALL - CGI =
|
---|
15 | # XXX:TODO
|
---|
16 | #
|
---|
17 | # = USAGE =
|
---|
18 | # Make sure your DNS entries has zones like this to allow remote updating
|
---|
19 | # example 60 IN A 192.0.32.10
|
---|
20 | # example 60 IN TXT "dyndns"
|
---|
21 | #
|
---|
22 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
23 | #
|
---|
24 | ZONE='vanderzwet.net'
|
---|
25 | ZONEFILE="/etc/namedb/master/$ZONE"
|
---|
26 |
|
---|
27 |
|
---|
28 | ### NO USER EDITABLE PARTS BELOW HERE ###
|
---|
29 | # SSH specific details
|
---|
30 | HOSTNAME=`echo $SSH_ORIGINAL_COMMAND | tr -c -d '[a-zA-Z0-9\-_\.]' | cut -d ' ' -f 1 | cut -d '.' -f 1`
|
---|
31 | REMOTEIP=`echo $SSH_CLIENT | cut -d ' ' -f 1`
|
---|
32 |
|
---|
33 | # Tmpfile creation for editing 'in between'
|
---|
34 | TMPFILE=`mktemp -t $(basename $0 .sh).XXX`
|
---|
35 | trap "rm -f $TMPFILE; exit 1" 0 1 2 15
|
---|
36 | cp $ZONEFILE $TMPFILE || exit 1
|
---|
37 |
|
---|
38 |
|
---|
39 | # See whether there exists a dynamic entry for it, like this
|
---|
40 | # example 60 IN A 192.0.32.10
|
---|
41 | # example 60 IN TXT "dyndns"
|
---|
42 | LINENR=`awk -v host=$HOSTNAME '{if ($1 == host) { if ($4 == "A") { line=NR } else if($5 ~ /dyndns/) {print line; exit} }}' $TMPFILE`
|
---|
43 | if [ -z "$LINENR" ]; then
|
---|
44 | echo "$HOSTNAME does not exists or is not marked as dynamic"
|
---|
45 | exit 1
|
---|
46 | fi
|
---|
47 |
|
---|
48 | # Do we need to update the entry
|
---|
49 | OLDIP=`awk -v linenr=$LINENR 'NR==linenr {print $5}' $TMPFILE`
|
---|
50 | if [ "$OLDIP" = "$REMOTEIP" ]; then
|
---|
51 | echo "No changes"
|
---|
52 | exit 0
|
---|
53 | fi
|
---|
54 |
|
---|
55 | # Update the entry
|
---|
56 | sed -i '' "${LINENR}s/$OLDIP/$REMOTEIP/" $TMPFILE || exit 1
|
---|
57 |
|
---|
58 | # Update serial of zone name
|
---|
59 | # YYYYMMDDNN where NN is from 00 till 99
|
---|
60 | TODAY=`date "+%Y%m%d"`
|
---|
61 | OLDSERIAL=`sed -n '1,10s/.*\([0-9]\{10\}\).*/\1/p' $TMPFILE`
|
---|
62 | if [ -z "$OLDSERIAL" ]; then
|
---|
63 | echo "Error Unable to find SERIAL of zone"
|
---|
64 | exit 1
|
---|
65 | fi
|
---|
66 | if `echo $OLDSERIAL | grep -q "^$TODAY"`; then
|
---|
67 | if `echo $OLDSERIAL | grep -q '99$'`; then
|
---|
68 | echo "Sorry domain update limit reached no more updates for today"
|
---|
69 | exit 1
|
---|
70 | fi
|
---|
71 | NEWSERIAL=`expr $OLDSERIAL + 1`
|
---|
72 | else
|
---|
73 | NEWSERIAL="${TODAY}00"
|
---|
74 | fi
|
---|
75 | sed -i '' "1,10s/$OLDSERIAL/$NEWSERIAL/g" $TMPFILE || exit 1
|
---|
76 |
|
---|
77 | # Install and activate
|
---|
78 | if \! `/usr/sbin/named-checkzone -q $TMPFILE $ZONE`; then
|
---|
79 | echo "New zone failed to validate"
|
---|
80 | exit 1
|
---|
81 | fi
|
---|
82 | cp $TMPFILE $ZONEFILE
|
---|
83 | sudo /usr/sbin/rndc reload
|
---|
84 | echo "Entry updated"
|
---|