Changeset 1921
- Timestamp:
- 09/09/07 20:21:57 (1 year ago)
- Files:
-
- alternc/trunk/src/functions.sh (modified) (1 diff)
- alternc/trunk/src/update_domains.sh (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
alternc/trunk/src/functions.sh
r1919 r1921 1 1 # some miscellaneous shell functions 2 3 print_domain_letter() { 4 local domain="$1" 5 6 local letter=`echo "$domain" | awk '{z=split($NF, a, ".") ; print substr(a[z-1], 1, 1)}'` 7 if [ -z "$letter" ]; then 8 letter="_" 9 fi 10 echo $letter 11 } 12 13 print_user_letter() { 14 local user="$1" 15 16 echo "$user" | awk '{print substr($1, 1, 1)}' 17 } 18 19 add_to_php_override() { 20 local fqdn="$1" 21 22 /usr/lib/alternc/basedir_prot.sh "$fqdn" >> "$DOMAIN_LOG_FILE" 23 } 24 25 remove_php_override() { 26 local fqdn="$1" 27 local letter=`print_domain_letter $fqdn` 28 29 sed -i "/$fqdn/d" $APACHECONF_DIR/override_php.conf 30 rm -f $APACHECONF_DIR/$letter/$fqdn 31 } 32 33 add_to_named_reload() { 34 local domain="$1" 35 local escaped_domain=`echo "$domain" | sed -e 's/\./\\\./g'` 36 37 if [ "domain" = "all" ] || grep -q "^all$" "$RELOAD_ZONES_TMP_FILE"; then 38 echo "all" > "$RELOAD_ZONES_TMP_FILE" 39 else 40 if ! grep -q "^${escaped_domain}$" "$RELOAD_ZONES_TMP_FILE"; then 41 echo "$domain" >> "$RELOAD_ZONES_TMP_FILE" 42 fi 43 fi 44 } 45 46 # we assume that the serial line contains the "serial string", eg.: 47 # 2005012703 ; serial 48 # 49 # returns 1 if file isn't readable 50 # returns 2 if we can't find the serial number 51 # returns 3 if a tempfile can't be created 52 increment_serial() { 53 local domain="$1" 54 local zone_file="$ZONES_DIR/$domain" 55 local current_serial 56 local new_serial 57 local date 58 local revision 59 local today 60 61 if [ ! -f "$zone_file" ]; then 62 return 1 63 fi 64 65 # the assumption is here 66 current_serial=`awk '/^..*serial/ {print $1}' < "$zone_file"` || return 2 67 if [ -z "$current_serial" ]; then 68 return 2 69 fi 70 71 date=`echo $current_serial | cut -c1-8` 72 revision=`echo $current_serial | sed s/"${date}0\?"/""/g` 73 today=`date +%Y%m%d` 74 # increment the serial number only if the date hasn't changed 75 if [ "$date" = "$today" ] ; then 76 revision=$(($revision + 1)) 77 else 78 revision=1 79 date=$today 80 fi 81 new_serial="$date`printf '%.2d' $revision`" 82 83 # replace serial number 84 cp -a -f "$zone_file" "$zone_file.$$" 85 awk -v "NEW_SERIAL=$new_serial" \ 86 '{if ($3 == "serial") 87 print " "NEW_SERIAL " ; serial" 88 else 89 print $0}' < "$zone_file" > "$zone_file.$$" 90 mv -f "$zone_file.$$" "$zone_file" 91 92 add_to_named_reload "$domain" 93 94 return 0 95 } 96 97 change_host_ip() { 98 local domain="$1" 99 local zone_file="$ZONES_DIR/$domain" 100 local ip="$2" 101 local host="$3" 102 local pattern 103 local a_line 104 105 if [ -z "$host" ]; then 106 host="@" 107 fi 108 a_line="$host IN A $ip" 109 pattern="^$host[[:space:]]*IN[[:space:]]*A[[:space:]]*.*\$" 110 if [ ! -f "$zone_file" ]; then 111 echo "Should change $host.$domain, but can't find $zone_file." 112 return 1 113 fi 114 if grep -q "$pattern" "$zone_file"; then 115 cp -a -f "$zone_file" "$zone_file.$$" 116 sed "s/$pattern/$a_line/" < "$zone_file" > "$zone_file.$$" 117 mv "$zone_file.$$" "$zone_file" 118 else 119 echo "$a_line" >> "$zone_file" 120 fi 121 add_to_named_reload "$domain" 122 } 123 124 add_host() { 125 local domain="$1" 126 local host_type="$2" 127 local host="$3" 128 local value="$4" 129 local user="$5" 130 local domain_letter=`print_domain_letter "$domain"` 131 local user_letter=`print_user_letter "$user"` 132 local ip 133 local fqdn 134 local vhost_directory 135 136 delete_host "$domain" "$host" 137 138 if [ "$host" = "@" -o -z "$host" ]; then 139 FQDN="$domain" 140 else 141 FQDN="$host.$domain" 142 fi 143 if [ "$host_type" != "$TYPE_IP" ]; then 144 add_to_php_override "$FQDN" 145 fi 146 147 if [ "$host_type" = "$TYPE_IP" ]; then 148 ip="$value" 149 else 150 ip="$PUBLIC_IP" 151 fi 152 if [ "$host" = "@" -o -z "$host" ]; then 153 change_host_ip "$domain" "$ip" || true 154 fqdn="$domain" 155 else 156 change_host_ip "$domain" "$ip" "$host" || true 157 fqdn="${host}.${domain}" 158 fi 159 160 vhost_directory="${HTTP_DNS}/${domain_letter}/${fqdn}" 161 htaccess_directory="${HTTP_DNS}/redir/${domain_letter}/${fqdn}" 162 163 case "$host_type" in 164 $TYPE_LOCAL) 165 ln -snf "${HTML_HOME}/${user_letter}/${user}${value}" \ 166 "$vhost_directory" 167 ;; 168 169 $TYPE_WEBMAIL) 170 ln -snf "${WEBMAIL_DIR}" "$vhost_directory" 171 ;; 172 173 $TYPE_URL) 174 mkdir -p "$htaccess_directory" 175 (echo "RewriteEngine on" 176 echo "RewriteRule (.*) ${value}\$1 [R,L]" 177 ) > "$htaccess_directory/.htaccess" 178 ln -snf "$htaccess_directory" "$vhost_directory" 179 ;; 180 181 $TYPE_IP) 182 rm -f "$vhost_directory" 183 rm -rf "$htaccess_directory/.htaccess" 184 ;; 185 186 *) 187 echo "Unknow type code: $type" >> "$DOMAIN_LOG_FILE" 188 ;; 189 esac 190 } 191 192 delete_host() { 193 local domain="$1" 194 local host="$2" 195 local domain_letter=`print_domain_letter "$domain"` 196 local fqdn 197 local escaped_host 198 local escaped_fqdn 199 200 if [ "$host" = "@" -o -z "$host" ]; then 201 fqdn="$domain" 202 escaped_host="" 203 else 204 fqdn="$host.$domain" 205 escaped_host=`echo "$host" | sed 's/\([\*|\.]\)/\\\\\1/g'` 206 fi 207 208 if [ -f "$ZONES_DIR/$domain" ] ; then 209 cp -a -f "$ZONES_DIR/$domain" "$ZONES_DIR/$domain.$$" 210 sed -e "/^$escaped_host[[:space:]]*IN[[:space:]]*A[[:space:]]/d" \ 211 < "$ZONES_DIR/$domain" > "$ZONES_DIR/$domain.$$" 212 mv "$ZONES_DIR/$domain.$$" "$ZONES_DIR/$domain" 213 increment_serial "$domain" 214 add_to_named_reload "$domain" 215 fi 216 217 rm -f "$APACHECONF_DIR/$domain_letter/$fqdn" 218 219 escaped_fqdn=`echo "$fqdn" | sed 's/\([\*|\.]\)/\\\\\1/g'` 220 221 cp -a -f "$OVERRIDE_PHP_FILE" "$OVERRIDE_PHP_FILE.$$" 222 sed -e "/\/${escaped_fqdn}\$/d" \ 223 < "$OVERRIDE_PHP_FILE" > "$OVERRIDE_PHP_FILE.$$" 224 mv "$OVERRIDE_PHP_FILE.$$" "$OVERRIDE_PHP_FILE" 225 226 rm -f "$HTTP_DNS/$domain_letter/$fqdn" 227 rm -rf "$HTTP_DNS/redir/$domain_letter/$fqdn" 228 } 229 230 231 init_zone() { 232 local domain="$1" 233 local escaped_domain=`echo "$domain" | sed -e 's/\./\\\./g'` 234 local zone_file="$ZONES_DIR/$domain" 235 local serial 236 237 if [ ! -f "$zone_file" ]; then 238 serial=`date +%Y%m%d`00 239 sed -e "s/@@DOMAINE@@/$domain/g;s/@@SERIAL@@/$serial/g" \ 240 < "$ZONE_TEMPLATE" > "$zone_file" 241 chgrp bind "$zone_file" 242 chmod 640 "$zone_file" 243 fi 244 if ! grep -q "\"$escaped_domain\"" "$NAMED_CONF_FILE"; then 245 cp -a -f "$NAMED_CONF_FILE" "$NAMED_CONF_FILE".prec 246 sed -e "s/@@DOMAINE@@/$domain/g" \ 247 < "$NAMED_TEMPLATE" >> "$NAMED_CONF_FILE" 248 add_to_named_reload "all" 249 fi 250 } 251 252 remove_zone() { 253 local domain="$1" 254 local escaped_domain=`echo "$domain" | sed -e 's/\./\\\./g'` 255 local zone_file="$ZONES_DIR/$domain" 256 257 if [ -f "$zone_file" ]; then 258 rm -f "$zone_file" 259 fi 260 261 if grep -q "\"$escaped_domain\"" "$NAMED_CONF_FILE"; then 262 cp -a -f "$NAMED_CONF_FILE" "$NAMED_CONF_FILE.prec" 263 cp -a -f "$NAMED_CONF_FILE" "$NAMED_CONF_FILE.$$" 264 # That's for multi-line template 265 #sed -e "/^zone \"$escaped_domain\"/,/^};/d" \ 266 # That's for one-line template 267 grep -v "^zone \"$escaped_domain\"" \ 268 < "$NAMED_CONF_FILE" > "$NAMED_CONF_FILE.$$" 269 mv -f "$NAMED_CONF_FILE.$$" "$NAMED_CONF_FILE" 270 add_to_named_reload "all" 271 fi 272 } 273 274 change_mx() { 275 local domain="$1" 276 local mx="$2" 277 local zone_file="$ZONES_DIR/$domain" 278 local pattern="^@*[[:space:]]*IN[[:space:]]*MX[[:space:]]*[[:digit:]]*[[:space:]].*\$" 279 local mx_line="@ IN MX 5 $mx." 280 281 # aller chercher le numéro de la ligne MX 282 # XXX: comportement inconnu si plusieurs matchs ou MX commenté 283 if grep -q "$pattern" "$zone_file"; then 284 cp -a -f "$zone_file" "$zone_file.$$" 285 sed -e "s/$pattern/$mx_line/" < "$zone_file" > "$zone_file.$$" 286 mv "$zone_file.$$" "$zone_file" 287 else 288 echo "$mx_line" >> "$zone_file" 289 fi 290 291 increment_serial "$domain" 292 add_to_named_reload "$domain" 293 } 294 295 2 296 3 297 # imprime le nom d'usager associé au domaine alternc/trunk/src/update_domains.sh
r1898 r1921 105 105 # Functions 106 106 # 107 108 print_domain_letter() { 109 local domain="$1" 110 111 local letter=`echo "$domain" | awk '{z=split($NF, a, ".") ; print substr(a[z-1], 1, 1)}'` 112 if [ -z "$letter" ]; then 113 letter="_" 114 fi 115 echo $letter 116 } 117 118 print_user_letter() { 119 local user="$1" 120 121 echo "$user" | awk '{print substr($1, 1, 1)}' 122 } 123 124 add_to_php_override() { 125 local fqdn="$1" 126 127 /usr/lib/alternc/basedir_prot.sh "$fqdn" >> "$DOMAIN_LOG_FILE" 128 } 129 130 remove_php_override() { 131 local fqdn="$1" 132 local letter=`print_domain_letter $fqdn` 133 134 sed -i "/$fqdn/d" $APACHECONF_DIR/override_php.conf 135 rm -f $APACHECONF_DIR/$letter/$fqdn 136 } 137 138 add_to_named_reload() { 139 local domain="$1" 140 local escaped_domain=`echo "$domain" | sed -e 's/\./\\\./g'` 141 142 if [ "domain" = "all" ] || grep -q "^all$" "$RELOAD_ZONES_TMP_FILE"; then 143 echo "all" > "$RELOAD_ZONES_TMP_FILE" 144 else 145 if ! grep -q "^${escaped_domain}$" "$RELOAD_ZONES_TMP_FILE"; then 146 echo "$domain" >> "$RELOAD_ZONES_TMP_FILE" 147 fi 148 fi 149 } 150 151 # we assume that the serial line contains the "serial string", eg.: 152 # 2005012703 ; serial 153 # 154 # returns 1 if file isn't readable 155 # returns 2 if we can't find the serial number 156 # returns 3 if a tempfile can't be created 157 increment_serial() { 158 local domain="$1" 159 local zone_file="$ZONES_DIR/$domain" 160 local current_serial 161 local new_serial 162 local date 163 local revision 164 local today 165 166 if [ ! -f "$zone_file" ]; then 167 return 1 168 fi 169 170 # the assumption is here 171 current_serial=`awk '/^..*serial/ {print $1}' < "$zone_file"` || return 2 172 if [ -z "$current_serial" ]; then 173 return 2 174 fi 175 176 date=`echo $current_serial | cut -c1-8` 177 revision=`echo $current_serial | sed s/"${date}0\?"/""/g` 178 today=`date +%Y%m%d` 179 # increment the serial number only if the date hasn't changed 180 if [ "$date" = "$today" ] ; then 181 revision=$(($revision + 1)) 182 else 183 revision=1 184 date=$today 185 fi 186 new_serial="$date`printf '%.2d' $revision`" 187 188 # replace serial number 189 cp -a -f "$zone_file" "$zone_file.$$" 190 awk -v "NEW_SERIAL=$new_serial" \ 191 '{if ($3 == "serial") 192 print " "NEW_SERIAL " ; serial" 193 else 194 print $0}' < "$zone_file" > "$zone_file.$$" 195 mv -f "$zone_file.$$" "$zone_file" 196 197 add_to_named_reload "$domain" 198 199 return 0 200 } 201 202 change_host_ip() { 203 local domain="$1" 204 local zone_file="$ZONES_DIR/$domain" 205 local ip="$2" 206 local host="$3" 207 local pattern 208 local a_line 209 210 if [ -z "$host" ]; then 211 host="@" 212 fi 213 a_line="$host IN A $ip" 214 pattern="^$host[[:space:]]*IN[[:space:]]*A[[:space:]]*.*\$" 215 if [ ! -f "$zone_file" ]; then 216 echo "Should change $host.$domain, but can't find $zone_file." 217 return 1 218 fi 219 if grep -q "$pattern" "$zone_file"; then 220 cp -a -f "$zone_file" "$zone_file.$$" 221 sed "s/$pattern/$a_line/" < "$zone_file" > "$zone_file.$$" 222 mv "$zone_file.$$" "$zone_file" 223 else 224 echo "$a_line" >> "$zone_file" 225 fi 226 add_to_named_reload "$domain" 227 } 228 229 add_host() { 230 local domain="$1" 231 local host_type="$2" 232 local host="$3" 233 local value="$4" 234 local user="$5" 235 local domain_letter=`print_domain_letter "$domain"` 236 local user_letter=`print_user_letter "$user"` 237 local ip 238 local fqdn 239 local vhost_directory 240 241 delete_host "$domain" "$host" 242 243 if [ "$host" = "@" -o -z "$host" ]; then 244 FQDN="$domain" 245 else 246 FQDN="$host.$domain" 247 fi 248 if [ "$host_type" != "$TYPE_IP" ]; then 249 add_to_php_override "$FQDN" 250 fi 251 252 if [ "$host_type" = "$TYPE_IP" ]; then 253 ip="$value" 254 else 255 ip="$PUBLIC_IP" 256 fi 257 if [ "$host" = "@" -o -z "$host" ]; then 258 change_host_ip "$domain" "$ip" || true 259 fqdn="$domain" 260 else 261 change_host_ip "$domain" "$ip" "$host" || true 262 fqdn="${host}.${domain}" 263 fi 264 265 vhost_directory="${HTTP_DNS}/${domain_letter}/${fqdn}" 266 htaccess_directory="${HTTP_DNS}/redir/${domain_letter}/${fqdn}" 267 268 case "$host_type" in 269 $TYPE_LOCAL) 270 ln -snf "${HTML_HOME}/${user_letter}/${user}${value}" \ 271 "$vhost_directory" 272 ;; 273 274 $TYPE_WEBMAIL) 275 ln -snf "${WEBMAIL_DIR}" "$vhost_directory" 276 ;; 277 278 $TYPE_URL) 279 mkdir -p "$htaccess_directory" 280 (echo "RewriteEngine on" 281 echo "RewriteRule (.*) ${value}\$1 [R,L]" 282 ) > "$htaccess_directory/.htaccess" 283 ln -snf "$htaccess_directory" "$vhost_directory" 284 ;; 285 286 $TYPE_IP) 287 rm -f "$vhost_directory" 288 rm -rf "$htaccess_directory/.htaccess" 289 ;; 290 291 *) 292 echo "Unknow type code: $type" >> "$DOMAIN_LOG_FILE" 293 ;; 294 esac 295 } 296 297 delete_host() { 298 local domain="$1" 299 local host="$2" 300 local domain_letter=`print_domain_letter "$domain"` 301 local fqdn 302 local escaped_host 303 local escaped_fqdn 304 305 if [ "$host" = "@" -o -z "$host" ]; then 306 fqdn="$domain" 307 escaped_host="" 308 else 309 fqdn="$host.$domain" 310 escaped_host=`echo "$host" | sed 's/\([\*|\.]\)/\\\\\1/g'` 311 fi 312 313 if [ -f "$ZONES_DIR/$domain" ] ; then 314 cp -a -f "$ZONES_DIR/$domain" "$ZONES_DIR/$domain.$$" 315 sed -e "/^$escaped_host[[:space:]]*IN[[:space:]]*A[[:space:]]/d" \ 316 < "$ZONES_DIR/$domain" > "$ZONES_DIR/$domain.$$" 317 mv "$ZONES_DIR/$domain.$$" "$ZONES_DIR/$domain" 318 increment_serial "$domain" 319 add_to_named_reload "$domain" 320 fi 321 322 rm -f "$APACHECONF_DIR/$domain_letter/$fqdn" 323 324 escaped_fqdn=`echo "$fqdn" | sed 's/\([\*|\.]\)/\\\\\1/g'` 325 326 cp -a -f "$OVERRIDE_PHP_FILE" "$OVERRIDE_PHP_FILE.$$" 327 sed -e "/\/${escaped_fqdn}\$/d" \ 328 < "$OVERRIDE_PHP_FILE" > "$OVERRIDE_PHP_FILE.$$" 329 mv "$OVERRIDE_PHP_FILE.$$" "$OVERRIDE_PHP_FILE" 330 331 rm -f "$HTTP_DNS/$domain_letter/$fqdn" 332 rm -rf "$HTTP_DNS/redir/$domain_letter/$fqdn" 333 } 334 335 336 init_zone() { 337 local domain="$1" 338 local escaped_domain=`echo "$domain" | sed -e 's/\./\\\./g'` 339 local zone_file="$ZONES_DIR/$domain" 340 local serial 341 342 if [ ! -f "$zone_file" ]; then 343 serial=`date +%Y%m%d`00 344 sed -e "s/@@DOMAINE@@/$domain/g;s/@@SERIAL@@/$serial/g" \ 345 < "$ZONE_TEMPLATE" > "$zone_file" 346 chgrp bind "$zone_file" 347 chmod 640 "$zone_file" 348 fi 349 if ! grep -q "\"$escaped_domain\"" "$NAMED_CONF_FILE"; then 350 cp -a -f "$NAMED_CONF_FILE" "$NAMED_CONF_FILE".prec 351 sed -e "s/@@DOMAINE@@/$domain/g" \ 352 < "$NAMED_TEMPLATE" >> "$NAMED_CONF_FILE" 353 add_to_named_reload "all" 354 fi 355 } 356 357 remove_zone() { 358 local domain="$1" 359 local escaped_domain=`echo "$domain" | sed -e 's/\./\\\./g'` 360 local zone_file="$ZONES_DIR/$domain" 361 362 if [ -f "$zone_file" ]; then 363 rm -f "$zone_file" 364 fi 365 366 if grep -q "\"$escaped_domain\"" "$NAMED_CONF_FILE"; then 367 cp -a -f "$NAMED_CONF_FILE" "$NAMED_CONF_FILE.prec" 368 cp -a -f "$NAMED_CONF_FILE" "$NAMED_CONF_FILE.$$" 369 # That's for multi-line template 370 #sed -e "/^zone \"$escaped_domain\"/,/^};/d" \ 371 # That's for one-line template 372 grep -v "^zone \"$escaped_domain\"" \ 373 < "$NAMED_CONF_FILE" > "$NAMED_CONF_FILE.$$" 374 mv -f "$NAMED_CONF_FILE.$$" "$NAMED_CONF_FILE" 375 add_to_named_reload "all" 376 fi 377 } 378 379 change_mx() { 380 local domain="$1" 381 local mx="$2" 382 local zone_file="$ZONES_DIR/$domain" 383 local pattern="^@*[[:space:]]*IN[[:space:]]*MX[[:space:]]*[[:digit:]]*[[:space:]].*\$" 384 local mx_line="@ IN MX 5 $mx." 385 386 # aller chercher le numéro de la ligne MX 387 # XXX: comportement inconnu si plusieurs matchs ou MX commenté 388 if grep -q "$pattern" "$zone_file"; then 389 cp -a -f "$zone_file" "$zone_file.$$" 390 sed -e "s/$pattern/$mx_line/" < "$zone_file" > "$zone_file.$$" 391 mv "$zone_file.$$" "$zone_file" 392 else 393 echo "$mx_line" >> "$zone_file" 394 fi 395 396 increment_serial "$domain" 397 add_to_named_reload "$domain" 398 } 399 107 . /usr/lib/alternc/functions.sh 400 108 401 109 ########################################################################
