| 1 | TEMPLATE_DIR="/etc/alternc/templates/apache2" |
|---|
| 2 | HOSTING_DIR="/usr/lib/alternc/hosting_functions" |
|---|
| 3 | |
|---|
| 4 | DATA_ROOT="/var/alternc" |
|---|
| 5 | HTML_HOME="$DATA_ROOT/html" |
|---|
| 6 | VHOST_DIR="$DATA_ROOT/apache-vhost" |
|---|
| 7 | VHOST_FILE="$VHOST_DIR/vhosts_all.conf" |
|---|
| 8 | |
|---|
| 9 | . /usr/lib/alternc/functions.sh |
|---|
| 10 | |
|---|
| 11 | host_create() { |
|---|
| 12 | # Function to create a vhost for a website |
|---|
| 13 | # First, it look if there is a special file for |
|---|
| 14 | # this type of vhost |
|---|
| 15 | # If there isn't, it use the default function |
|---|
| 16 | # and the template file provided |
|---|
| 17 | |
|---|
| 18 | local VTYPE="$1" |
|---|
| 19 | |
|---|
| 20 | if [ -x "$HOSTING_DIR/hosting_$VTYPE.sh" ] ; then |
|---|
| 21 | # There is a script special for this type, |
|---|
| 22 | # I launch it and quit the host_create function |
|---|
| 23 | # (I precise to the script this is for a "enable" task) |
|---|
| 24 | "$HOSTING_DIR/hosting_$VTYPE.sh" "create" $@ |
|---|
| 25 | return |
|---|
| 26 | fi |
|---|
| 27 | |
|---|
| 28 | # There is no special script, I use the standart template |
|---|
| 29 | # If I do not found template manualy define, I look |
|---|
| 30 | # If there is an existing template with the good name |
|---|
| 31 | |
|---|
| 32 | # First, usefull vars. Some may be empty or false, it's |
|---|
| 33 | # OK, it will be solve in the "case" below |
|---|
| 34 | local USER=$2 |
|---|
| 35 | local FQDN=$3 |
|---|
| 36 | local REDIRECT=$4 # Yes, TARGET_DIR and REDIRECT are the same |
|---|
| 37 | local TARGET_DIR=$4 # It's used by different template |
|---|
| 38 | local user_letter=`print_user_letter "$USER"` |
|---|
| 39 | local DOCUMENT_ROOT="${HTML_HOME}/${user_letter}/${USER}/$TARGET_DIR" |
|---|
| 40 | local FILE_TARGET="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf" |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | # panel.conf redirect.conf vhost.conf webmail.conf |
|---|
| 44 | case $VTYPE in |
|---|
| 45 | "vhost") |
|---|
| 46 | TEMPLATE="$TEMPLATE_DIR/vhost.conf" |
|---|
| 47 | ;; |
|---|
| 48 | *) |
|---|
| 49 | # No template found, look if there is some in the |
|---|
| 50 | # template dir |
|---|
| 51 | [ -r "$TEMPLATE_DIR/$VTYPE" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE" |
|---|
| 52 | [ ! "$TEMPLATE" ] && [ -r "$TEMPLATE_DIR/$VTYPE.conf" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE.conf" |
|---|
| 53 | ;; |
|---|
| 54 | esac |
|---|
| 55 | |
|---|
| 56 | # Create a new conf file |
|---|
| 57 | local TMP_FILE=$(mktemp "/tmp/alternc_host.XXXXXX") |
|---|
| 58 | cp "$TEMPLATE" "$TMP_FILE" |
|---|
| 59 | echo "#Username: $USER" |
|---|
| 60 | |
|---|
| 61 | # Put the good value in the conf file |
|---|
| 62 | sed -i \ |
|---|
| 63 | -e "s#%%fqdn%%#$FQDN#g" \ |
|---|
| 64 | -e "s#%%document_root%%#$DOCUMENT_ROOT#g" \ |
|---|
| 65 | -e "s#%%redirect%%#$REDIRECT#g" \ |
|---|
| 66 | $TMP_FILE |
|---|
| 67 | |
|---|
| 68 | # Check if all is right in the conf file |
|---|
| 69 | # If not, put a debug message |
|---|
| 70 | local ISNOTGOOD=$(grep "%%" "$TMP_FILE") |
|---|
| 71 | [ "$ISNOTGOOD" ] && (echo "# There was a probleme in the generation : $ISNOTGOOD" > "$TMP_FILE" |
|---|
| 72 | |
|---|
| 73 | # Put the conf file in prod |
|---|
| 74 | mkdir -p "$(dirname "$FILE_TARGET")" |
|---|
| 75 | mv -f "$TMP_FILE" "$FILE_TARGET" |
|---|
| 76 | |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | host_disable() { |
|---|
| 80 | host_change_enable "disable" $@ |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | host_enable() { |
|---|
| 84 | host_change_enable "enable" $@ |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | host_change_enable() { |
|---|
| 88 | # Function to enable or disable a host |
|---|
| 89 | local STATE=$1 |
|---|
| 90 | local FQDN=$2 |
|---|
| 91 | local USER=`get_account_by_domain $FQDN` |
|---|
| 92 | local user_letter=`print_user_letter "$USER"` |
|---|
| 93 | local FENABLED="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf" |
|---|
| 94 | local FDISABLED="$FENABLED-disabled" |
|---|
| 95 | |
|---|
| 96 | case $STATE in |
|---|
| 97 | "enable") |
|---|
| 98 | local SOURCE="$FDISABLED" |
|---|
| 99 | local TARGET="$FENABLED" |
|---|
| 100 | ;; |
|---|
| 101 | "disable") |
|---|
| 102 | local TARGET="$FDISABLED" |
|---|
| 103 | local SOURCE="$FENABLED" |
|---|
| 104 | ;; |
|---|
| 105 | *) |
|---|
| 106 | return 1 |
|---|
| 107 | ;; |
|---|
| 108 | esac |
|---|
| 109 | |
|---|
| 110 | if [ ! -e "$TARGET" ] && [ -e "$SOURCE" ] ; then |
|---|
| 111 | # If the "target" file do not exist and the "source" file exist |
|---|
| 112 | rename -f "$SOURCE" "$TARGET" |
|---|
| 113 | else |
|---|
| 114 | return 2 |
|---|
| 115 | fi |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | host_delete() { |
|---|
| 119 | local FQDN=$1 |
|---|
| 120 | local USER=`get_account_by_domain $FQDN` |
|---|
| 121 | local user_letter=`print_user_letter "$USER"` |
|---|
| 122 | local FENABLED="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf" |
|---|
| 123 | local FDISABLED="$FENABLED-disabled" |
|---|
| 124 | |
|---|
| 125 | [ -w "$FENABLED" ] && rm -f "$FENABLED" |
|---|
| 126 | [ -w "$FDISABLED" ] && rm -f "$FDISABLED" |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|