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