gitzone

git-based zone management tool for static and dynamic domains
git clone https://git.parazyd.org/gitzone
Log | Files | Refs

gitzone-install (3035B)


      1 #!/bin/sh
      2 #
      3 # gitzone - git-based zone file management tool for BIND
      4 #
      5 # Copyright (C) 2013 - 2019 Dyne.org Foundation
      6 #
      7 # This program is free software: you can redistribute it and/or modify
      8 # it under the terms of the GNU Affero General Public License as published by
      9 # the Free Software Foundation, either version 3 of the License, or
     10 # (at your option) any later version.
     11 #
     12 # This program is distributed in the hope that it will be useful,
     13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 # GNU Affero General Public License for more details.
     16 #
     17 # You should have received a copy of the GNU Affero General Public License
     18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     19 
     20 # quick script to install a new gitzone user / zones repository
     21 
     22 # first arg: username
     23 # secondo (optional) arg: ssh public key
     24 
     25 if [ -z "$1" ]; then
     26 	echo "usage: gitzone-install username [ id_rsa.pub ]";
     27 	exit 1
     28 fi
     29 
     30 if ! [ "$(id -u)" = 0 ]; then
     31 	echo "this script needs to be run as root."
     32 	exit 1
     33 fi
     34 
     35 user="$1"
     36 
     37 # check user argument
     38 if ! grep -q "^$user" /etc/passwd; then
     39 	echo "error: user not found: $user"
     40 	exit 1
     41 fi
     42 if ! [ -r "/home/$user" ]; then
     43 	echo "error: user home not found: /home/$user"
     44 	exit 1
     45 fi
     46 if [ -r "/home/$user/zones/$user/.git" ]; then
     47 	echo "error: gitzone already installed for user $user";
     48 else # create gitzone directory in user home
     49 	mkdir -p "/home/$user/zones/$user"
     50 	cd "/home/$user/zones/$user" || {
     51 		echo "error: could not cd to /home/$user/zones/$user"
     52 		exit 1
     53 	}
     54 	git init .
     55 	git config receive.denyCurrentBranch ignore
     56 	git config user.name "$user"
     57 	git config user.email "$user@$(hostname -f)"
     58 	ln -s /usr/libexec/gitzone/*receive* .git/hooks/
     59 	cd - >/dev/null
     60 	chown -R "$user:bind" "/home/$user/zones"
     61 	chmod -R o-rwx "/home/$user/zones"
     62 
     63 	# add user to bind group
     64 	usermod -aG bind "$user"
     65 
     66 	# add gitzone cache dir
     67 	mkdir -p "/var/cache/bind/$user"
     68 	chown "$user:bind" "/var/cache/bind/$user"
     69 	chmod o-rwx "/var/cache/bind/$user"
     70 
     71 	touch /etc/bind/named.conf.local
     72 	if ! grep -q "${user}.conf" /etc/bind/named.conf.local; then
     73 		cat <<EOF >> /etc/bind/named.conf.local
     74 include "/etc/bind/repos/${user}.conf";
     75 EOF
     76 	fi
     77 
     78 # success
     79 	cat <<EOF
     80 
     81 ### Gitzone installed for user $user
     82 ## git repository url (via ssh):
     83 
     84 	$user@$(hostname -f):zones/$user
     85 
     86 EOF
     87 
     88 fi # gitzone created
     89 
     90 cat <<EOF
     91 Don't forget to add configurations to bind!
     92 
     93 In /etc/bind/repos create ${user}.conf and put inside:
     94 
     95 zone "domain.com" {
     96 	type master;
     97 	notify yes;
     98 	file "/var/cache/bind/$user/domain.com";
     99 	allow-transfer { transfer; };
    100 };
    101 
    102 Then restart the bind9 daemon.
    103 EOF
    104 
    105 
    106 key="$2" # add ssh key
    107 if [ -z "$key" ]; then
    108 	exit 1
    109 fi
    110 if ! [ -r "$key" ]; then
    111 	echo "error: key not found $key"
    112 	exit 1
    113 fi
    114 mkdir -p "/home/$user/.ssh"
    115 touch "/home/$user/.ssh/authorized_keys"
    116 cat "$key" >> "/home/$user/.ssh/authorized_keys"
    117 chmod -R go-rwx "/home/$user/.ssh"
    118 chown -R "$user:$user" "/home/$user/.ssh"
    119 echo "ssh public key $key added for $user"