source: dyndns/dyndns.sh@ 186

Last change on this file since 186 was 186, checked in by Rick van der Zwet, 14 years ago

SSH version of dynamic DNS script

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 2.4 KB
RevLine 
[186]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#
24ZONE='vanderzwet.net'
25ZONEFILE="/etc/namedb/master/$ZONE"
26
27
28### NO USER EDITABLE PARTS BELOW HERE ###
29# SSH specific details
30HOSTNAME=`echo $SSH_ORIGINAL_COMMAND | tr -c -d '[a-zA-Z0-9\-_\.]' | cut -d ' ' -f 1 | cut -d '.' -f 1`
31REMOTEIP=`echo $SSH_CLIENT | cut -d ' ' -f 1`
32
33# Tmpfile creation for editing 'in between'
34TMPFILE=`mktemp -t $(basename $0 .sh).XXX`
35trap "rm -f $TMPFILE; exit 1" 0 1 2 15
36cp $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"
42LINENR=`awk -v host=$HOSTNAME '{if ($1 == host) { if ($4 == "A") { line=NR } else if($5 ~ /dyndns/) {print line; exit} }}' $TMPFILE`
43if [ -z "$LINENR" ]; then
44 echo "$HOSTNAME does not exists or is not marked as dynamic"
45 exit 1
46fi
47
48# Do we need to update the entry
49OLDIP=`awk -v linenr=$LINENR 'NR==linenr {print $5}' $TMPFILE`
50if [ "$OLDIP" = "$REMOTEIP" ]; then
51 echo "No changes"
52 exit 0
53fi
54
55# Update the entry
56sed -i '' "${LINENR}s/$OLDIP/$REMOTEIP/" $TMPFILE || exit 1
57
58# Update serial of zone name
59# YYYYMMDDNN where NN is from 00 till 99
60TODAY=`date "+%Y%m%d"`
61OLDSERIAL=`sed -n '1,10s/.*\([0-9]\{10\}\).*/\1/p' $TMPFILE`
62if [ -z "$OLDSERIAL" ]; then
63 echo "Error Unable to find SERIAL of zone"
64 exit 1
65fi
66if `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`
72else
73 NEWSERIAL="${TODAY}00"
74fi
75sed -i '' "1,10s/$OLDSERIAL/$NEWSERIAL/g" $TMPFILE || exit 1
76
77# Install and activate
78if \! `/usr/sbin/named-checkzone -q $TMPFILE $ZONE`; then
79 echo "New zone failed to validate"
80 exit 1
81fi
82cp $TMPFILE $ZONEFILE
83sudo /usr/sbin/rndc reload
84echo "Entry updated"
Note: See TracBrowser for help on using the repository browser.