| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | . /usr/lib/alternc/functions.sh |
|---|
| 4 | |
|---|
| 5 | TEMPLATE_DIR="/etc/alternc/templates/apache2" |
|---|
| 6 | HOSTING_DIR="/usr/lib/alternc/hosting_functions" |
|---|
| 7 | |
|---|
| 8 | HTML_HOME="$ALTERNC_LOC/html" |
|---|
| 9 | VHOST_DIR="$ALTERNC_LOC/apache-vhost" |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | host_create() { |
|---|
| 13 | # Function to create a vhost for a website |
|---|
| 14 | # First, it look if there is a special file for |
|---|
| 15 | # this type of vhost |
|---|
| 16 | # If there isn't, it use the default function |
|---|
| 17 | # and the template file provided |
|---|
| 18 | |
|---|
| 19 | local VTYPE="$1" |
|---|
| 20 | |
|---|
| 21 | if [ -x "$HOSTING_DIR/hosting_$VTYPE.sh" ] ; then |
|---|
| 22 | # There is a script special for this type, |
|---|
| 23 | # I launch it and quit the host_create function |
|---|
| 24 | # (I precise to the script this is for a "enable" task) |
|---|
| 25 | "$HOSTING_DIR/hosting_$VTYPE.sh" "create" $@ |
|---|
| 26 | local returnval=$? |
|---|
| 27 | |
|---|
| 28 | # If the special script for this type exit with a code between |
|---|
| 29 | # 20 and 25, it means I have to continue like it didn't exist. |
|---|
| 30 | # It allow for example creation a script to exist only for deletion, |
|---|
| 31 | # or to do pre-inst or post-inst. |
|---|
| 32 | if [ $returnval -lt 20 ] || [ $returnval -gt 25 ] ; then |
|---|
| 33 | return |
|---|
| 34 | fi |
|---|
| 35 | fi |
|---|
| 36 | |
|---|
| 37 | # There is no special script, I use the standart template |
|---|
| 38 | # If I do not found template manualy define, I look |
|---|
| 39 | # If there is an existing template with the good name |
|---|
| 40 | |
|---|
| 41 | # First, usefull vars. Some may be empty or false, it's |
|---|
| 42 | # OK, it will be solve in the "case" below |
|---|
| 43 | local FQDN=$2 |
|---|
| 44 | local REDIRECT=$3 # Yes, TARGET_DIR and REDIRECT are the same |
|---|
| 45 | local TARGET_DIR=$3 # It's used by different template |
|---|
| 46 | local USER=$(get_account_by_domain $FQDN) |
|---|
| 47 | local user_letter=`print_user_letter "$USER"` |
|---|
| 48 | local DOCUMENT_ROOT="${HTML_HOME}/${user_letter}/${USER}/$TARGET_DIR" |
|---|
| 49 | local FILE_TARGET="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf" |
|---|
| 50 | |
|---|
| 51 | # In case VTYPE don't have the same name as the template file, |
|---|
| 52 | # here we can define it |
|---|
| 53 | local TEMPLATE='' |
|---|
| 54 | case $VTYPE in |
|---|
| 55 | # "example") |
|---|
| 56 | # TEMPLATE="$TEMPLATE_DIR/an-example.conf" |
|---|
| 57 | # ;; |
|---|
| 58 | *) |
|---|
| 59 | # No template found, look if there is some in the |
|---|
| 60 | # template dir |
|---|
| 61 | [ -r "$TEMPLATE_DIR/$VTYPE" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE" |
|---|
| 62 | [ ! "$TEMPLATE" ] && [ -r "$TEMPLATE_DIR/$VTYPE.conf" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE.conf" |
|---|
| 63 | ;; |
|---|
| 64 | esac |
|---|
| 65 | |
|---|
| 66 | # If TEMPLATE is empty, stop right here |
|---|
| 67 | [ ! "$TEMPLATE" ] && return 6 |
|---|
| 68 | |
|---|
| 69 | # Create a new conf file |
|---|
| 70 | local TMP_FILE=$(mktemp "/tmp/alternc_host.XXXXXX") |
|---|
| 71 | cp "$TEMPLATE" "$TMP_FILE" |
|---|
| 72 | |
|---|
| 73 | # Put the good value in the conf file |
|---|
| 74 | sed -i \ |
|---|
| 75 | -e "s#%%fqdn%%#$FQDN#g" \ |
|---|
| 76 | -e "s#%%document_root%%#$DOCUMENT_ROOT#g" \ |
|---|
| 77 | -e "s#%%redirect%%#$REDIRECT#g" \ |
|---|
| 78 | $TMP_FILE |
|---|
| 79 | |
|---|
| 80 | # Check if all is right in the conf file |
|---|
| 81 | # If not, put a debug message |
|---|
| 82 | local ISNOTGOOD=$(grep "%%" "$TMP_FILE") |
|---|
| 83 | [ "$ISNOTGOOD" ] && (echo "# There was a probleme in the generation : $ISNOTGOOD" > "$TMP_FILE" ) |
|---|
| 84 | |
|---|
| 85 | # Put the conf file in prod |
|---|
| 86 | mkdir -p "$(dirname "$FILE_TARGET")" |
|---|
| 87 | mv -f "$TMP_FILE" "$FILE_TARGET" |
|---|
| 88 | |
|---|
| 89 | # Execute post-install if there is some for this VTYPE |
|---|
| 90 | [ -x "$HOSTING_DIR/hosting_$VTYPE.sh" ] && "$HOSTING_DIR/hosting_$VTYPE.sh" "postint" $@ |
|---|
| 91 | |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | host_disable() { |
|---|
| 95 | host_change_enable "disable" $@ |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | host_enable() { |
|---|
| 99 | host_change_enable "enable" $@ |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | host_change_enable() { |
|---|
| 103 | # Function to enable or disable a host |
|---|
| 104 | local STATE=$1 |
|---|
| 105 | |
|---|
| 106 | # If there is a VTYPE precised and a specific script exist |
|---|
| 107 | if [ $3 ] ; then |
|---|
| 108 | local VTYPE=$3 |
|---|
| 109 | if [ -x "$HOSTING_DIR/hosting_$VTYPE.sh" ] ; then |
|---|
| 110 | "$HOSTING_DIR/hosting_$VTYPE.sh" $@ |
|---|
| 111 | return |
|---|
| 112 | fi |
|---|
| 113 | fi |
|---|
| 114 | |
|---|
| 115 | local FQDN=$2 |
|---|
| 116 | local USER=$(get_account_by_domain $FQDN) |
|---|
| 117 | local user_letter=`print_user_letter "$USER"` |
|---|
| 118 | local FENABLED="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf" |
|---|
| 119 | local FDISABLED="$FENABLED-disabled" |
|---|
| 120 | |
|---|
| 121 | case $STATE in |
|---|
| 122 | "enable") |
|---|
| 123 | local SOURCE="$FDISABLED" |
|---|
| 124 | local TARGET="$FENABLED" |
|---|
| 125 | ;; |
|---|
| 126 | "disable") |
|---|
| 127 | local TARGET="$FDISABLED" |
|---|
| 128 | local SOURCE="$FENABLED" |
|---|
| 129 | ;; |
|---|
| 130 | *) |
|---|
| 131 | return 1 |
|---|
| 132 | ;; |
|---|
| 133 | esac |
|---|
| 134 | |
|---|
| 135 | if [ ! -e "$TARGET" ] && [ -e "$SOURCE" ] ; then |
|---|
| 136 | # If the "target" file do not exist and the "source" file exist |
|---|
| 137 | mv -f "$SOURCE" "$TARGET" |
|---|
| 138 | else |
|---|
| 139 | return 2 |
|---|
| 140 | fi |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | host_delete() { |
|---|
| 144 | local FQDN=$1 |
|---|
| 145 | |
|---|
| 146 | # If there is a VTYPE precised and a specific script exist |
|---|
| 147 | if [ $2 ] ; then |
|---|
| 148 | local VTYPE=$2 |
|---|
| 149 | if [ -x "$HOSTING_DIR/hosting_$VTYPE.sh" ] ; then |
|---|
| 150 | "$HOSTING_DIR/hosting_$VTYPE.sh" "delete" $@ |
|---|
| 151 | local returnval=$? |
|---|
| 152 | # If the exit value of the VTYPE script is between 20 and 25, |
|---|
| 153 | # continue the delete like it didn't exist |
|---|
| 154 | if [ $returnval -lt 20 ] || [ $returnval -gt 25 ] ; then |
|---|
| 155 | return |
|---|
| 156 | fi |
|---|
| 157 | fi |
|---|
| 158 | fi |
|---|
| 159 | |
|---|
| 160 | local USER=`get_account_by_domain $FQDN` |
|---|
| 161 | local user_letter=`print_user_letter "$USER"` |
|---|
| 162 | local FENABLED="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf" |
|---|
| 163 | local FDISABLED="$FENABLED-disabled" |
|---|
| 164 | |
|---|
| 165 | [ -w "$FENABLED" ] && rm -f "$FENABLED" |
|---|
| 166 | [ -w "$FDISABLED" ] && rm -f "$FDISABLED" |
|---|
| 167 | } |
|---|
| 168 | |
|---|