Skip to content

Instantly share code, notes, and snippets.

@lucashalbert
Created October 14, 2019 20:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lucashalbert/56e49c83e0d1aaa4131fe8723f766064 to your computer and use it in GitHub Desktop.
Save lucashalbert/56e49c83e0d1aaa4131fe8723f766064 to your computer and use it in GitHub Desktop.
Script to import an existing BIND DNS zone to IPA
#!/bin/bash
print_version() {
cat <<EOF
####################################################################################
#
# Author: Lucas Halbert <contactme@lhalbert.xyz>
# Date: 10/14/2019
# Last Edited: 10/14/2019
# Version: 2019.10.14
# Description: Parse a BIND DNS zone (minus the SOA) and import directly to IPA
# License: BSD 3-Clause License
#
# Copyright (c) 2016, Lucas Halbert
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################
EOF
}
print_changelog() {
cat <<EOF
####################################################################################
#
# Revisions: 2019.10.14 - Initial Draft
#
####################################################################################
EOF
}
print_usage() {
cat <<EOF
Usage: $(basename $0) $0 <zone name> <zone file>
Example: $(basename $0) subdomain.domain.tld subdomain_domain_tld.zone
EOF
}
# Function to add zone
function createZone() {
local zone_name=$1
# Add a master DNS zone
ipa dnszone-add ${zone_name}
}
# Function to add record to specified zone
function addRecordToZone() {
local zone_name=$1
local record_name=$2
local record_type=$3
local record_content=$4
if [[ ${record_type} == "A" ]]; then
local type="--a-rec"
elif [[ ${record_type} == "AAAA" ]]; then
local type="--aaaa-rec"
elif [[ ${record_type} == "CNAME" ]]; then
local type="--cname-rec"
elif [[ ${record_type} == "PTR" ]]; then
local type="--ptr-rec"
elif [[ ${record_type} == "SRV" ]]; then
local type="--srv-rec"
else
echo "Could not add ${record_type} record \"${record_name}=${record_content}\""
return
fi
#ipa dnsrecord-add example.com www --a-rec 10.10.10.10
ipa dnsrecord-add ${zone_name} ${record_name} ${type} ${record_content}
}
# Global variables
ZONE_NAME=$1
ZONE_FILE=$2
# Check if 2 arguments are provided
if [ "$#" -lt 2 ]; then
echo -e "Command requires two (2) arguments\n"
print_usage
exit 1
fi
# Check if zone file exists
if [ ! -f "${ZONE_FILE}" ]; then
echo "The file \"${ZONE_FILE}\" does not exist"
exit 2
fi
# Create the zone
createZone ${ZONE_NAME}
# Parse Zone file to insert records
while read line; do
RECORD_NAME=$(echo ${line} | cut -d" " -f1)
RECORD_TYPE=$(echo ${line} | cut -d" " -f3)
RECORD_CONTENT=$(echo ${line} | cut -d" " -f4)
RECORD_COMMENT=$(echo ${line} | cut -d";" -f2)
# Add record to zone
addRecordToZone ${ZONE_NAME} ${RECORD_NAME} ${RECORD_TYPE} ${RECORD_CONTENT}
done < ${ZONE_FILE}
@bsg273
Copy link

bsg273 commented Jan 15, 2024

What do you use to create the ZONE_FILE from the source IPA server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment