- 论坛徽章:
- 0
|
OpenBSD下动态分配二级域名
作者:4Aiur [forAiur{at}msn{dot}com]
来源:bbs.chinaunix.com
文档版本: 0220060308
实现目的与前提条件:
方便没有固定IP地址的用户(ADSL),使其可以更换IP地址后自动获得二级域名。
NS服务器必须为静态IP,否则无法使用。
软件环境为OpenBSD3.8、BIND 9.3.1、Apache/1.3.29
第二版更正第一版没有验证码,有可能会被捣乱的问题 ^_^
一、服务器DNS配置
1.1 配置bind,建立主域。
#vi /var/named/conf/named.conf
// Master zones
//
zone "ddns.com" {
type master;
file "master/ddns.com.zone";
};
include "/etc/rndc.key";
1.2编写主控制域
#cat /var/named/master/ddns.com.zone
$ORIGIN ddns.com.
$TTL 600 ;10 minutes
@ IN SOA ddns.com. master.ddns.com. (
200600208 ; serial
43200 ; refresh (12 hours)
1800 ; retry (30 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
IN NS @
A 192.168.0.1
www A 192.168.0.1
mail A 192.168.0.2
bbs A 192.168.0.3
1.3配置rndc
#rndc-confgen -ac /var/named/etc/rndc.key
#chgrp named /var/named/etc/rndc.key
#chown 640 /var/named/etc/rndc.key
#named -4
#ln -s /var/named/etc/rndc.key /etc/rndc.key
#rndc status
number of zones: 11
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/1000
tcp clients: 0/100
server is up and running
二、服务器虚拟主机配置
#vi /var/www/conf/httpd.conf
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
LogFormat "%404a %404U" subdomain.db
NameVirtualHost 192.168.0.122
<VirtualHost 192.168.0.122>*
ServerAdmin master@ddns.com
DocumentRoot /var/www/htdocs/ddns.com
ServerName www.ddns.com
CustomLog logs/subdomain.db subdomain.db
Errlog logs/www.ddns.com-error_log
CustomLog logs/www.ddns.com-access_log common
</VirtualHost>
#apachectl graceful
三、客户端设置
#vi /root/bin/clientip.sh
wget http://www.ddns.com/tom/tom.ddns.com/tom
注:tom.ddns.com是验证码,根据自己情况更改,后面的脚本只提取验证码后面的数据作为二级域名。
#chmod 700 /root/bin/clientip.sh
#vi /etc/ppp/ppp.linkup
! sh -c "/root/bin/clientip.sh"
注:拨号后自动执行发送本机IP脚本
四、服务器定时更新二级域名记录
#crontab -e
*/10 * * * * /root/bin/dynamicdns.sh >/dev/null 2>&1
#cat /root/bin/dynamicdns.sh
#!/bin/sh
cd /tmp
rm -f subdomain.db
while read id code domain
do
grep \/"$id"\/"$code"\/"$domain" /var/www/logs/subdomain.db | tail -1>> subdomain.db
done < /var/named/account.db
if [ "`cat subdomain.db`" = "" ]
then
:
exit
else
sed "s/\// /g" subdomain.db >domaindb.new
nawk '{ printf "%-15s %-5s %-5s\n",$4,"A",$1 }' domaindb.new >/var/named/domaindb
cat /var/named/zone.base > zone.tmp
cat /var/named/domaindb >>zone.tmp
cat zone.tmp > /var/named/master/ddns.com.zone
rndc reload ddns.com
rm -f domaindb.new
rm -f subdomain.db
rm -f zone.tmp
fi
本文涉及到的文件有:
account.db #帐号、验证码与二级域名
subdomain.db #子域 ipaddress、帐号、验证码、二级域名(apache logs)
4aiur.com.zone #生成的正向解析文件
[root@ tmp]#cat /var/named/account.db
4aiur 4aiur.ddns.com 4aiur
4aiur 4aiur.ddns.com blog
bob bob.ddns.com bob
tom tom.ddns.com tom
[root@ tmp]#cat /var/www/logs/subdomain.db
- -
- -
192.168.111.119 /bob/qwerqwer/blog
192.168.111.116 /4aiur/4aiur.ddns.com/4aiur
192.168.111.119 /bob/bob.ddns.com/bob
192.168.111.119 /bob/bob.ddns.com/blog
192.168.111.11 /4aiur/4aiur.ddns.com/4aiur
192.168.111.119 /bob/bob.ddns.com/dfas
192.168.111.119 /bo/bob.ddns.comb/bob
192.168.111.119 /bob/bob.ddns.com/blog
192.168.1.119 /4aiur/4aiur.ddns.com/4aiur
192.168.111.119 /4aiur/4aiur.ddns.com/4aiur
192.168.111.119 /4aiur/4aiur.ddns.com/blog
192.168.111.119 /4aiur/4aiur.ddns.com/asd
- -
- -
192.168.111.119 /tom/tom.ddns.com/tom
192.168.111.119 /tom/tom.ddns.com/toasd
- -
[root@ tmp]#cat subdomain.db
192.168.111.119 /4aiur/4aiur.ddns.com/4aiur
192.168.111.119 /4aiur/4aiur.ddns.com/blog
192.168.111.119 /bob/bob.ddns.com/bob
192.168.111.119 /tom/tom.ddns.com/tom
[root@ tmp]#cat domaindb.new
192.168.111.119 4aiur 4aiur.ddns.com 4aiur
192.168.111.119 4aiur 4aiur.ddns.com blog
192.168.111.119 bob bob.ddns.com bob
192.168.111.119 tom tom.ddns.com tom
[root@ tmp]#cat zone.tmp
$ORIGIN ddns.com.
$TTL 600 ;10 minutes
@ IN SOA ddns.com. master.ddns.com. (
200600208 ; serial
43200 ; refresh (12 hours)
1800 ; retry (30 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
IN NS @
A 192.168.0.1
www A 192.168.0.1
mail A 192.168.0.2
bbs A 192.168.0.3
4aiur A 192.168.111.119
blog A 192.168.111.119
bob A 192.168.111.119
tom A 192.168.111.119
[root@ tmp]#
[[i] 本帖最后由 4Aiur 于 2006-3-23 16:32 编辑 [/i]] |
|