source: dyndns/dyndns.sh

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

Converted it into a What is my IP thingy as well.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
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# = PREREQUISITES =
7# 1) Make sure your SOA is within the first 10 lines of your zone file and has
8# the format YYYYMMDDNN
9# 2) Make sure your DNS entries has zones like this to allow remote updating
10# example 60 IN A 192.0.32.10
11# example 60 IN TXT "dyndns"
12#
13# = INSTALL - SSH =
14# 1) Make sure to allow rndc reload in your sudoers file
15# ssh_user ALL=(ALL) NOPASSWD: /usr/sbin/rndc reload
16# 2) prefix your ssh authorized key to allow only this script
17# command="/home/ssh_user/dyndns/dyndns.sh"
18# 3) Call for the remote host from crontab:
19# ssh -a -p 1022 -i id_rsa dnsserver.example.net `hostname -s`
20#
21# = INSTALL - CGI =
22# 1) Put your secret password in ``.cgi_secret''
23# 2) Alter your apache configuration to allow executing the script
24# ScriptAlias /dyndns /path/to/dyndns/dyndns.sh
25# 1) Make sure to allow rndc reload in your sudoers file
26# www_user ALL=(ALL) NOPASSWD: /usr/sbin/rndc reload
27# 3) Call from the remote host from crontab:
28# fetch -q -o - "http://dnsserver.example.net/dyndns/`hostname -s`/SECRET/"
29#
30#
31# License: BSDLike - http://rickvanderzwet.nl/LICENSE
32# Rick van der Zwet <info@rickvanderzwet.nl>
33#
34ZONE='vanderzwet.net'
35ZONEFILE="/etc/namedb/master/$ZONE"
36CGI_SECRET_FILE="`dirname $0`/.cgi_secret"
37
38### NO USER EDITABLE PARTS BELOW HERE ###
39exec 2>&1
40if [ -n "$SSH_ORIGINAL_COMMAND" ]; then
41 # SSH specific details
42 HOSTNAME=`echo $SSH_ORIGINAL_COMMAND | tr -c -d '[a-zA-Z0-9\-_\.]' | cut -d ' ' -f 1 | cut -d '.' -f 1`
43 REMOTEIP=`echo $SSH_CLIENT | cut -d ' ' -f 1`
44else
45 # CGI specific details
46 HOSTNAME=`echo $REQUEST_URI | tr -c -d '[a-zA-Z0-9\-_\./]' | awk -F/ '{print $3}'`
47 SECRET=`echo $REQUEST_URI | tr -c -d '[a-zA-Z0-9\-_\./\!_]' | awk -F/ '{print $4}'`
48 REMOTEIP=$REMOTE_ADDR
49 echo "Content-Type: text/plain"
50 echo ""
51 CGI_SECRET=`cat $CGI_SECRET_FILE`
52 if [ -z "$CGI_SECRET" ]; then
53 echo "Secret not readable from $CGI_SECRET_FILE"
54 exit 1
55 fi
56 if [ "$SECRET" != "$CGI_SECRET" ]; then
57 echo "Secret invalid"
58 exit 1
59 fi
60fi
61
62# Tmpfile creation for editing 'in between'
63TMPFILE=`mktemp -t $(basename $0 .sh).XXX`
64trap "rm -f $TMPFILE; exit 1" 0 1 2 15
65cp $ZONEFILE $TMPFILE || exit 1
66
67
68# See whether there exists a dynamic entry for it, like this
69# example 60 IN A 192.0.32.10
70# example 60 IN TXT "dyndns"
71LINENR=`awk -v host=$HOSTNAME '{if ($1 == host) { if ($4 == "A") { line=NR } else if($5 ~ /dyndns/) {print line; exit} }}' $TMPFILE`
72if [ -z "$LINENR" ]; then
73 echo "$HOSTNAME does not exists or is not marked as dynamic"
74 exit 1
75fi
76
77# Do we need to update the entry
78OLDIP=`awk -v linenr=$LINENR 'NR==linenr {print $5}' $TMPFILE`
79if [ "$OLDIP" = "$REMOTEIP" ]; then
80 echo "No changes ($HOSTNAME.$ZONE IN A $REMOTEIP)"
81 exit 0
82fi
83
84# Update the entry
85sed -i '' "${LINENR}s/$OLDIP/$REMOTEIP/" $TMPFILE || exit 1
86
87# Update serial of zone name
88# YYYYMMDDNN where NN is from 00 till 99
89TODAY=`date "+%Y%m%d"`
90OLDSERIAL=`sed -n '1,10s/.*\([0-9]\{10\}\).*/\1/p' $TMPFILE`
91if [ -z "$OLDSERIAL" ]; then
92 echo "Error Unable to find SERIAL of zone"
93 exit 1
94fi
95if `echo $OLDSERIAL | grep -q "^$TODAY"`; then
96 if `echo $OLDSERIAL | grep -q '99$'`; then
97 echo "Sorry domain update limit reached no more updates for today"
98 exit 1
99 fi
100 NEWSERIAL=`expr $OLDSERIAL + 1`
101else
102 NEWSERIAL="${TODAY}00"
103fi
104sed -i '' "1,10s/$OLDSERIAL/$NEWSERIAL/g" $TMPFILE || exit 1
105
106# Install and activate
107if `/usr/sbin/named-checkzone -q $TMPFILE $ZONE`; then
108 echo "New zone failed to validate"
109 exit 1
110fi
111cp $TMPFILE $ZONEFILE || exit 1
112sudo /usr/sbin/rndc reload || exit 1
113echo "Entry updated ($HOSTNAME.$ZONE IN A $REMOTEIP)"
Note: See TracBrowser for help on using the repository browser.