#!/bin/bash #^ PENDENT --> ^^ #.Y / 70 83 / / / / 105 / / / / / / ;A4_QUER.Y #.YT 3 2 0 0 3 3 1 / / / #.H!a!0:linux//io/com/net/dns/THIS!0.!c # # tbrü 14dec2012; # # dns_ THIS : dns-functions # +++++++++++++++++++++++++ # # MyHash("linux//io/com/net/dns/THIS") = 0x0CA7 # # * for every function: # * if returned result is not commented, the function returns 0 on no-error. SCRIPTS=root/bin . /$SCRIPTS/io/f_/THIS . /$SCRIPTS/string_/THIS # globals: Contact0x0CA7="public0x05bf.bluewin.ch." DynamicFileName0x0CA7="Dynamic.TXT" FirstTab0x0CA7=30 NameServer0x0CA7="router.int.thomas-r-bruecker.ch." SecondTab0x0CA7=34 SerialFileName0x0CA7="Serial.TXT" Server0x0CA7=$NameServer0x0CA7 StaticFileName0x0CA7="Static.TXT" ThirdTab0x0CA7=37 TypeFieldWidth0x0CA7=8 ZoneFileFolderPath0x0CA7="/var/named" dns_dotA() { # .A # $1 : host; # $2 : address; # * write a-record to stdout. local Address Host Address="$2" Host="$1" dns_dotResourceRecord A "$Host" "$Address" } dns_dotCNAME() { # .CNAME # $1 : nickName; # $2 : host; # * write ptr-record to stdout. local Host NickName Host="$2" NickName="$1" dns_dotResourceRecord CNAME "$NickName" "$Host" } dns_dotNS() { # .NS # * write ns-record to stdout. echo "" dns_dotResourceRecord NS "" "$NameServer0x0CA7\n" } dns_dotPTR() { # .PTR # $1 : specialName; # $2 : name; # * write ptr-record to stdout. local Name SpecialName Name="$2" SpecialName="$1" dns_dotResourceRecord PTR "$SpecialName" "$Name" } dns_dotResourceRecord() { # .ResourceRecord # $1 : type; # $2 : name; # $3 : data; # * write a resouce-record to stdout. # global: FirstTab0x0CA7 TypeFieldWidth0x0CA7 local Data Name Type Data="$3" Name="$2" Type="$1" echo -e "$(string_dotL "$Name" $FirstTab0x0CA7)IN $( \ string_dotL "$Type" $TypeFieldWidth0x0CA7 \ )${Data}" } dns_dotSOA() { # .SOA # $1 : zone-name. # $2 : serial# # * write soa-record to stdout. # global: Contact0x0CA7 SecondTab0x0CA7 Server0x0CA7 ThirdTab0x0CA7 local SecondTab Serial ZoneName let "SecondTab = SecondTab0x0CA7 + 1" Serial="$2" ZoneName="$1" echo -e "; \$ORIGIN $ZoneName\n" echo -e "\$TTL 1D" dns_dotResourceRecord SOA "@" "$Server0x0CA7 $Contact0x0CA7 (" echo "$(string_dotR $Serial $ThirdTab0x0CA7) ; serial" echo "$(string_dotR "3H" $ThirdTab0x0CA7) ; refresh" echo "$(string_dotR "1H" $ThirdTab0x0CA7) ; retry" echo "$(string_dotR "1W" $ThirdTab0x0CA7) ; expire" echo "$(string_dotR "1D" $ThirdTab0x0CA7) ; minimum" echo "$(string_dotR ")" $SecondTab)" # " : restore syntax-highlight. } dns_q_createFolder() { # ?createFolder # $1 : zone-path; # * maybe create the zone-path-folder and initialize it with the appropriate # files. # global: DynamicFileName0x0CA7 local ZonePath ZonePath="$1" if [ ! -d $ZonePath ] ; then /bin/mkdir "$ZonePath" /bin/touch "${ZonePath}/${DynamicFileName0x0CA7}" echo "1" > "${ZonePath}/${SerialFileName0x0CA7}" /bin/touch "${ZonePath}/${StaticFileName0x0CA7}" fi } dns_HOST() { # $1 : host-name; # $2 : zone-name; # $3 : ip-address; # * add host to completely to the dns-database. dns_addHost "$1" "$2" "$3" dns_addPTR "$1" "$2" "$3" dns_Restart } dns_Restart() { # restart named. /sbin/service named reload } dns_UpdateZone() { # $1 : zone-name; # * update the zone; # * rewrite the zone-file; # * restart named. dns_updateZone "$1" dns_Restart } dns_addHost() { # $1 : host-name; # $2 : zone-name; # $3 : ip-address; # * add host to "Dynamic.TXT" of the appropriate zone. local HostName IpAddress ZoneName ZonePath HostName="$1" IpAddress="$3" ZoneName="$2" ZonePath="$(dns_zone2ZonePath "$ZoneName")" dns_dotA $HostName $IpAddress >> "${ZonePath}/${DynamicFileName0x0CA7}" dns_updateZone "$ZoneName" } #^^ --> iNet iNet_4Parameters2ReverseIp() { # $1 : parameter1; # $2 : parameter2; # $3 : parameter3; # $4 : parameter4; # print reverse ip-address generated from the 4 parameters to stdout. echo "$4"."$3"."$2"."$1" } iNet_ipTo4Parameters() { # $1 : ip-address; # * convert ip-address to its 4 ^parts and write them to stdout. local IpAddress IpAddress="$1" echo $IpAddress | /bin/sed 's/\./ /g' } dns_addPTR() { # $1 : host-name; # $2 : zone-name; # $3 : ip-address; # * add host to "Dynamic.TXT" of the appropriate in-addr.arpa-zone. #^^ zone-name in .SOA is wrong. local ArpaZoneName HostName IpAddress SpecialName ZoneName ZonePath HostName="$1" IpAddress="$3" ZoneName="$2" ArpaZoneName="$(dns_zone2ArpaZone "$ZoneName")" ArpaZonePath="$(dns_zone2ZonePath "$ArpaZoneName")" SpecialName="$( iNet_4Parameters2ReverseIp \ $(iNet_ipTo4Parameters "$IpAddress") ).in-addr.arpa." dns_dotPTR "$SpecialName" "${HostName}.${ZoneName}." \ >> "${ArpaZonePath}/${DynamicFileName0x0CA7}" dns_updateZone "$ArpaZoneName" } dns_incSerial() { # $1 : zone-path; # * increment serial# and write it to stdout. local Serial ZonePath ZonePath="$1" Serial=$(dns_readSerial $ZonePath) let "Serial = Serial + 1" dns_writeSerial $Serial $ZonePath echo "$Serial" } dns_readSerial() { # $1 : zone-path; # * read serial# and write it to stdout. # global: SerialFileName0x0CA7 local ZonePath ZonePath="$1" cat "${ZonePath}/${SerialFileName0x0CA7}" } dns_updateZone() { # $1 : zone-name; # * update the zone and rewrite the zone-file. local Serial ZoneFilePath ZoneName ZonePath ZoneName="$1" ZonePath=$(dns_zone2ZonePath "$ZoneName") ZoneFilePath=$(dns_zone2ZoneFilePath "$ZoneName") Serial=$(dns_incSerial "$ZonePath") f_SortAndUnify "${ZonePath}/${DynamicFileName0x0CA7}" echo -e "; do not write to this file directly ^^...\n" \ > "$ZoneFilePath" dns_dotSOA "$ZoneName" $Serial >> "$ZoneFilePath" dns_dotNS >> "$ZoneFilePath" echo -e ";static records:\n" >> "$ZoneFilePath" cat "${ZonePath}/${StaticFileName0x0CA7}" >> "$ZoneFilePath" echo -e "\n\n;dynamic records:\n" >> "$ZoneFilePath" cat "${ZonePath}/${DynamicFileName0x0CA7}" >> "$ZoneFilePath" } dns_writeSerial() { # $1 : serial#; # $2 : zone-path; # * write serial# to serial#-file. # global: SerialFileName0x0CA7 local Serial ZonePath Serial="$1" ZonePath="$2" echo "$Serial" > "${ZonePath}/${SerialFileName0x0CA7}" } dns_zone2ArpaZone() { # $1 : zone-name; # * convert the zone-name to the zone-name of the corresponding # "in-addr.arpa"-zone and print it to stdout. # global: ZoneFileFolderPath0x0CA7 local ZoneName ZonePath ZoneName="$1" echo "${ZoneName}.in-addr.arpa" } dns_zone2ZoneFilePath() { # $1 : zone-name; # * convert the zone-name to the path of the zone-file and print it to stdout. # global: ZoneFileFolderPath0x0CA7 local ZoneName ZonePath ZoneName="$1" echo "${ZoneFileFolderPath0x0CA7}/${ZoneName}" } dns_zone2ZonePath() { # $1 : zone-name; # * convert the zone-name to the path to the folder of the zone-files; # * if the folder of the zone-path does not exist, create the folder; # * print zone-path to stdout. # global: ZoneFileFolderPath0x0CA7 local ZoneName ZonePath ZoneName="$1" ZonePath="${ZoneFileFolderPath0x0CA7}/${ZoneName}.d" dns_q_createFolder "$ZonePath" echo "$ZonePath" } dns_Test() { dns_HOST $1 int.thomas-r-bruecker.ch $2 # dns_addPTR $1 int.thomas-r-bruecker.ch $2 # dns_addHost $1 int.thomas-r-bruecker.ch $2 # dns_Restart # dns_UpdateZone int.thomas-r-bruecker.ch # dns_q_createFolder "${ZoneFileFolderPath0x0CA7}/$1" return iNet_4Parameters2ReverseIp $(iNet_ipTo4Parameters $1) dns_zone2ArpaZone int.thomas-r-bruecker.ch # dns_incSerial $AFolderPath0x0CA7 # dns_readSerial $AFolderPath0x0CA7 dns_dotSOA xxxx 1 dns_dotNS dns_dotA main-linux 169.254.1.1 dns_dotCNAME buche.henet.ch www.henet.ch dns_dotPTR 169.254.1.1 main-linux } dns_hostUsage() { echo "Usage: dns_ HOST " echo "* \" := " echo " | ;\"" echo "* \" := ;\"" echo echo "* add \"\"-declaration to dns-configuration." return 0 }