Debian Codegraph @

ldap2bind

raw ◀ ▶
7 Debian patches touch this file
1 #!/bin/sh
2
•3 # Should we run the cronjob
4 # DEFAULT: "false"
•5 RUN_DEPLOY="false"
6
7 # How the LDAP server can be accessed
8 # DEFAULT: ""
9 # Example: "ldap://localhost"
10 LDAP_URI=""
11
12 # Where the bind config is stored
13 # DEFAULT: "/etc/bind"
14 BIND_DIR="/etc/bind"
15
16 # Where the zonefiles are located
17 # DEFAULT: "/var/cache/bind"
18 BIND_DATA="/var/cache/bind"
19
20 # Time to live value for a and ptr records
21 # DEFAULT: 500 Seconds
22 TTL="500"
23
24 # Prefix for zone definition files
25 # DEFAULT: "db."
26 # The zone definition file for 0.168.192.in-addr.arpa is stored as 'db.0.168.192.in-addr.arpa'
27 PREFIX="db."
28
29 [ -r /etc/default/ldap2zone ] && . /etc/default/ldap2zone
30
31 # if shell is non-interactive (e.g. like when called via CRON) and RUN_DEPLOY is false, exit silently.
•32 if [ ! -t 1 ] && [ "$RUN_DEPLOY" = "false" ]; then
•33 exit 0
34 fi
35
36 case "$LDAP_URI" in
37 ldap://*|ldaps://*|ldapi://*) ;;
38 '')
39 # No LDAP server set, no need trying to generate a zone file.
40 exit 0
41 ;;
42 *) LDAP_URI="ldap://${LDAP_URI}" ;;
43 esac
44
45 LDAPSEARCH=`which ldapsearch`
46
47 if [ -z "${LDAPSEARCH}" ]; then
48 echo "ldapsearch program not in $PATH. Exiting..."
49 exit 1
50 fi
51
52 LDAP_URI_PARAM=${LDAP_URI:+"-H $LDAP_URI"}
53
54 if [ "$ALLOW_NOTIFY" ]; then
55 ALLOW_NOTIFY="$ALLOW_NOTIFY";
56 else ALLOW_NOTIFY=;
57 fi
58
59 if [ "$ALLOW_UPDATE" ]; then
60 ALLOW_UPDATE_PARAM="allow-update {$ALLOW_UPDATE};";
61 else ALLOW_UPDATE_PARAM=;
62 fi
63
64 if [ "$ALLOW_TRANSFER" ]; then
65 ALLOW_TRANSFER_PARAM="allow-transfer {$ALLOW_TRANSFER};";
66 else ALLOW_TRANSFER_PARAM=;
67 fi
68
•69 ZONES=`ldapsearch -LLL $LDAP_URI_PARAM -x "(objectClass=dNSZone)" zoneName | grep zoneName: | sort | uniq | awk '{print $2}'`
70 ldap2zone=`which ldap2zone`
71 rndc=`which rndc`
72
73 if [ -z "${ZONES}" ]; then
74 echo "No domains configured. Exiting..."
75 exit 0
76 fi
77
78 if [ -z "${rndc}" ]; then
79 echo "rndc program not in $PATH. Exiting..."
80 exit 1
81 fi
82
83 if [ -z "${ldap2zone}" ]; then
84 echo "ldap2zone program not in $PATH. Exiting..."
85 exit 1
86 fi
87
88 if [ ! -d $BIND_DIR ]; then
89 echo "The directory specified as $BIND_DIR does not exist. Exiting..."
90 exit 1
91 fi
92
93 if [ ! -d $BIND_DATA ]; then
94 echo "The directory specified as $BIND_DATA does not exist. Exiting..."
95 exit 1
96 fi
97
98
99 if [ -w $BIND_DIR/named.conf.ldap2zone ]; then
100 >${BIND_DIR}/named.conf.ldap2zone
101 for domain in $ZONES; do
102 cat << EOF >> ${BIND_DIR}/named.conf.ldap2zone
103 zone "${domain}" {
104 type master;
105 $ALLOW_NOTIFY
106 file "${BIND_DATA}/${PREFIX}${domain}";
107 $ALLOW_UPDATE_PARAM
108 $ALLOW_TRANSFER_PARAM
109 };
110 EOF
111 done
112 $rndc reconfig
113 fi
114
115 for domain in $ZONES; do
•116 TMPFILE=$(mktemp)
117 CURRENT=$BIND_DATA/${PREFIX}${domain}
118 OLD=$BIND_DATA/${PREFIX}${domain}.old-$$
119 if $ldap2zone $domain $LDAP_URI $TTL > $TMPFILE; then
120 lines=$(cat $TMPFILE | wc -l)
121 if [ $lines -gt 1 ] ; then
122 result=$(named-checkzone $domain $TMPFILE 2>&1)
•123 if [ $? -ne 0 ]; then
124 logger -t ldap2bind "Checking the zone '$domain' failed: $result. Not deploying.\n"
•125 rm $TMPFILE
126 continue
127 else
128 mv $CURRENT $OLD
129 mv $TMPFILE $CURRENT
130 fi
131 fi
132 fi
133
134 result=$($rndc reload $domain 2>&1)
135 if [ $? -ne 0 ]; then
•136 logger -t ldap2bind "Reloading the zone '$domain' failed: $result. Reverting.\n"
137 mv $CURRENT $CURRENT.broken
138 mv $OLD $CURRENT
139 else
•140 rm $OLD
141 logger -t ldap2bind "Reloading the zone '$domain' was successful\n"
142 fi
143 done