Ticket #318: fix_318-6.diff

File fix_318-6.diff, 21.6 kB (added by anarcat, 1 month ago)

avec cette patch, je peux faire des upgrades

  • debian/postinst

    old new  
    8383# Mail server hostname 
    8484DEFAULT_MX="" 
    8585 
    86 # MySQL configuration 
    87 MYSQL_HOST="" 
    88 MYSQL_DATABASE="" 
    89 MYSQL_USER="" 
    90 MYSQL_PASS="" 
     86# Note: MySQL username/password configuration now stored in /etc/alternc/mysql-root.cnf 
     87 
    9188# quels clients mysql sont permis (%, localhost, etc) 
    9289MYSQL_CLIENT="" 
    9390 
     
    119116    update_var alternc/ns2 NS2_HOSTNAME 
    120117    update_var alternc/bind_internal BIND_INTERNAL 
    121118    update_var alternc/default_mx DEFAULT_MX  
    122     update_var alternc/mysql/host MYSQL_HOST  
    123     update_var alternc/mysql/db MYSQL_DATABASE  
    124     update_var alternc/mysql/user MYSQL_USER  
    125     update_var alternc/mysql/password MYSQL_PASS  
    126119    update_var alternc/mysql/client MYSQL_CLIENT  
    127120    update_var alternc/alternc_location ALTERNC_LOC 
    128121    update_var alternc/mynetwork SMTP_RELAY_NETWORKS 
    129122    sed -e "$SED_SCRIPT" < $CONFIGFILE > $CONFIGFILE.tmp 
    130123    mv -f $CONFIGFILE.tmp $CONFIGFILE 
    131124 
     125    # Setup grants 
     126    db_get "alternc/mysql/host" 
     127    MYSQL_HOST="$RET" 
     128    if [ "$MYSQL_HOST" != "localhost" -o -e /usr/sbin/mysqld ]; then 
     129        # compatibility shims with my.cnf 
     130        host="$RET" 
     131        db_get "alternc/mysql/db" 
     132        database="$RET" 
     133        db_get "alternc/mysql/user" 
     134        user="$RET" 
     135        db_get "alternc/mysql/password" 
     136        password="$RET" 
     137         
     138        # we source (instead of forking) mysql.sh so that it gets the local environment above 
     139        . /usr/share/alternc/install/mysql.sh 
     140    fi 
     141 
    132142    # forget the password 
    133143    db_reset alternc/mysql/password || true 
    134144    db_fset alternc/mysql/password "seen" "false" || true 
  • debian/postrm

    old new  
    1515 
    1616case "$1" in 
    1717  purge) 
    18     rm -f /etc/alternc/local.sh /etc/alternc/bureau.conf 
     18    rm -f /etc/alternc/local.sh /etc/alternc/mysql.cnf /etc/alternc/bureau.conf 
    1919    rm -f /var/backups/alternc/etc-installed.tar.gz 
    2020 
    2121    # Purge database? 
  • debian/changelog

    old new  
    11alternc (0.9.7+dev) stable; urgency=low UNRELEASED 
    22 
     3  * move mysql configuration into a valid MySQL configuration file 
     4    (/etc/alternc/mysql.cnf). This fixes a serious security issue (#318) 
     5    where the MySQL root password was passed on the commandline.  Those 
     6    changes are pretty invasive and might break upgrades, cron jobs and 
     7    your cat... 
    38  * standardisation of the web interface, along with some esthetic changes, by 
    49    Marc Angles, sponsored by Koumbit 
    510  * styles can now be changed locally in admin/styles/base.css 
  • debian/config

    old new  
    3838    # source the current config 
    3939    . /etc/alternc/local.sh 
    4040fi 
     41if [ -r /etc/alternc/mysql.cnf ]; then 
     42    # make mysql configuration available as shell variables 
     43    # to convert from .cnf to shell syntax, we: 
     44    # * match only lines with "equal" in them (/=/) 
     45    # * remove whitespace around the = and add a left quote operator ' (;s) 
     46    # * add a right quote operator at the end of line (;s) 
     47    # * convert mysql variables into our MYSQL_ naming convention (;s) 
     48    # * print the result (;p) 
     49    eval `sed -n -e "/=/{s/ *= */='/;s/\$/'/;s/host/MYSQL_HOST/;s/user/MYSQL_LOGIN/;s/password/MYSQL_PASS/;s/database/MYSQL_DATABASE/;p}" /etc/alternc/mysql.cnf` 
     50fi 
    4151 
    4252# mettre les valeurs de local.sh comme "default" pour debconf 
    4353db_get alternc/hostingname 
  • bureau/class/local.php

    old new  
    1313 
    1414$config_file = fopen('/etc/alternc/local.sh', 'r'); 
    1515while (FALSE !== ($line = fgets($config_file))) { 
    16     if (ereg('^([A-Z0-9_]*)="([^"]*)"', $line, $regs)) { 
     16    if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $regs)) { 
    1717        $GLOBALS['L_'.$regs[1]] = $regs[2]; 
    1818        if (isset($compat[$regs[1]])) { 
    1919            $GLOBALS['L_'.$compat[$regs[1]]] = $regs[2]; 
     
    2222} 
    2323 
    2424fclose($config_file); 
     25 
     26$config_file = fopen('/etc/alternc/mysql.cnf', 'r'); 
     27while (FALSE !== ($line = fgets($config_file))) { 
     28    if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $regs)) { 
     29        switch ($regs[1]) { 
     30        case "user": 
     31            $GLOBALS['L_MYSQL_LOGIN'] = $regs[2]; 
     32            break; 
     33        case "password": 
     34            $GLOBALS['L_MYSQL_PWD'] = $regs[2]; 
     35            break; 
     36        case "host": 
     37            $GLOBALS['L_MYSQL_HOST'] = $regs[2]; 
     38            break; 
     39        case "database": 
     40            $GLOBALS['L_MYSQL_DATABASE'] = $regs[2]; 
     41            break; 
     42        } 
     43    } 
     44} 
     45 
     46fclose($config_file); 
  • src/functions.sh

    old new  
    309309        else 
    310310                # implantons localement ce que nous avons besoin, puisque admintools 
    311311                # n'est pas là 
    312                 mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -B -N -e \ 
     312                mysql --defaults-file=/etc/alternc/mysql.cnf -B -N -e \ 
    313313                'SELECT a.login FROM membres a, sub_domaines b WHERE a.uid = b.compte AND \ 
    314314                CONCAT(IF(sub="", "", CONCAT(sub, ".")), domaine) = "'"$1"'" LIMIT 1;' 
    315315        fi 
  • src/basedir_prot.sh

    old new  
    1818. /etc/alternc/local.sh 
    1919. /usr/lib/alternc/functions.sh 
    2020 
    21 if [ -z "$MYSQL_HOST" ] 
    22 then 
    23     MYSQL_HOST="localhost" 
    24 fi 
    25  
    2621echo -n "adding open_base_dir protection for:" 
    2722# boucle sur tous les domaines hébergés, ou sur les arguments de la 
    2823# ligne de commande 
  • src/sqlbackup.sh

    old new  
    2929 
    3030set -e 
    3131 
    32 # Get mysql user and password :  
    33 . /etc/alternc/local.sh 
    34  
    3532function dobck { 
    3633    local ext 
    3734    local i 
     
    6360        mv -f "${target_dir}/${db}.sql${ext}" \ 
    6461              "${target_dir}/${db}.sql.${i}${ext}" 2>/dev/null || true  
    6562        if [ "$compressed" -eq 1 ]; then 
    66             mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db" --add-drop-table --allow-keywords -Q -f -q -a -e | 
     63            mysqldump --defaults-file=/etc/alternc/mysql-alternc.cnf --add-drop-table --allow-keywords -Q -f -q -a -e | 
    6764                gzip -c > "${target_dir}/${db}.sql${ext}" 
    6865        else 
    69             mysqldump -h"$MYSQL_HOST" -u"$login" -p"$pass" "$db" --add-drop-table --allow-keywords -Q -f -q -a -e \ 
     66            mysqldump --defaults-file=/etc/alternc/mysql-alternc.cnf --add-drop-table --allow-keywords -Q -f -q -a -e \ 
    7067                > "${target_dir}/${db}.sql" 
    7168        fi 
    7269 
     
    8380    mode=1 
    8481fi 
    8582 
    86 /usr/bin/mysql -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASS" \ 
    87     "$MYSQL_DATABASE" -B << EOF | tail -n '+2' | dobck 
     83/usr/bin/mysql --defaults-file=/etc/alternc/mysql.cnf -B << EOF | tail -n '+2' | dobck 
    8884SELECT login, pass, db, bck_history, bck_gzip, bck_dir 
    8985  FROM db 
    9086 WHERE bck_mode=$mode; 
  • src/update_domains.sh

    old new  
    7474 
    7575. "$CONFIG_FILE" 
    7676 
    77 if [ -z "$MYSQL_HOST" -o -z "$MYSQL_DATABASE" -o -z "$MYSQL_USER" -o \ 
    78      -z "$MYSQL_PASS" -o -z "$DEFAULT_MX" -o -z "$PUBLIC_IP" ]; then 
     77if [ -z "$DEFAULT_MX" -o -z "$PUBLIC_IP" ]; then 
    7978    echo "Bad configuration. Please use:" 
    8079    echo "   dpkg-reconfigure alternc" 
    8180    exit 1 
     
    9695HTTP_DNS="$DATA_ROOT/dns" 
    9796HTML_HOME="$DATA_ROOT/html" 
    9897 
    99 MYSQL_SELECT="mysql -h${MYSQL_HOST} -u${MYSQL_USER} 
    100                     -p${MYSQL_PASS} -Bs ${MYSQL_DATABASE}" 
    101 MYSQL_DELETE="mysql -h${MYSQL_HOST} -u${MYSQL_USER} 
    102                     -p${MYSQL_PASS} ${MYSQL_DATABASE}" 
     98MYSQL_SELECT="mysql --defaults-file=/etc/alternc/mysql-alternc.cnf -Bs " 
     99MYSQL_DELETE="mysql --defaults-file=/etc/alternc/mysql-alternc.cnf " 
    103100 
    104101######################################################################## 
    105102# Functions 
  • src/fixperms.sh

    old new  
    6363    done 
    6464} 
    6565 
    66 mysql -h"$MYSQL_HOST" -p"$MYSQL_PASS" -u"$MYSQL_USER" "$MYSQL_DATABASE" -B -e "select uid,login from membres" |grep -v ^uid|doone 
     66mysql --defaults-file=/etc/alternc/mysql.cnf -B -e "select uid,login from membres" |grep -v ^uid|doone 
    6767 
  • tools/get_domains_by_account

    old new  
    7474# Have to get AlternC conf file : 
    7575! [ -f "$ALTERNC_CONF_FILE" ] && { echo $MISSING_CONF_FILE ; exit 1 ; } || . $ALTERNC_CONF_FILE 
    7676# Must have access to mysql to retreive accounts owning domains : 
    77 [ -z "$MYSQL_HOST" ] && MYSQL_HOST=localhost 
    78 $mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -e "select count(*) from domaines_standby;" > /dev/null 2>&1 
    79 [ "$?" != 0 ] && { echo "$MYSQL_UNREACHABLE_DATABASE" ; exit 1 ; } || mysql="$mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -B -N -e " 
     77mysql="$mysql --defaults-file=/etc/alternc/mysql.cnf -B -N -e" 
     78$mysql "select count(*) from domaines_standby;" > /dev/null 2>&1 
     79[ "$?" != 0 ] && { echo "$MYSQL_UNREACHABLE_DATABASE" ; exit 1 ; } 
    8080 
    8181# Does the stuff 
    8282$mysql "select concat(a.sub, if(a.sub=\"\",\"\", \".\"), a.domaine) from sub_domaines a, membres b where a.compte = b.uid and b.login = \"${1}\";" 
  • tools/top_http_users

    old new  
    168168# Have to get AlternC conf file : 
    169169[ -f "$ALTERNC_CONF_FILE" ] || { echo $MISSING_CONF_FILE ; exit 1 ; } && . $ALTERNC_CONF_FILE 
    170170# Must have access to mysql to retreive accounts owning domains : 
    171 [ -z "$MYSQL_HOST" ] && MYSQL_HOST=localhost 
    172 $mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -e "select count(*) from domaines_standby;" > /dev/null 2>&1 
    173 [ "$?" != 0 ] && { echo "$MYSQL_UNREACHABLE_DATABASE" ; exit 1 ; } || mysql="$mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -B -N -e " 
    174  
     171mysql="$mysql --defaults-file=/etc/alternc/mysql.cnf -B -N -e" 
     172$mysql "select count(*) from domaines_standby;" > /dev/null 2>&1 
     173[ "$?" != 0 ] && { echo "$MYSQL_UNREACHABLE_DATABASE" ; exit 1 ; } 
    175174# Prevents executing more than one shell at the same time 
    176175$lockfilecreate --retry 1 $LOCK_FILE 
    177176if [ $? != 0 ] 
  • tools/get_account_by_domain

    old new  
    7575[ "$1" = "-h" ] || [ "$1" = "--help" ] && { echo $HELP ; echo $USAGE ; exit 0 ; } 
    7676# Have to get AlternC conf file : 
    7777! [ -f "$ALTERNC_CONF_FILE" ] && { echo $MISSING_CONF_FILE ; exit 1 ; } || . $ALTERNC_CONF_FILE 
    78 # Must have access to mysql to retreive accounts owning domains : 
    79 [ -z "$MYSQL_HOST" ] && MYSQL_HOST=localhost 
    80 $mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -e "select count(*) from domaines_standby;" > /dev/null 2>&1 
    81 [ "$?" != 0 ] && { echo "$MYSQL_UNREACHABLE_DATABASE" ; exit 1 ; } || mysql="$mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -B -N -e " 
     78mysql="$mysql --defaults-file=/etc/alternc/mysql.cnf" 
     79$mysql -e "select count(*) from domaines_standby;" > /dev/null 2>&1 
     80[ "$?" != 0 ] && { echo "$MYSQL_UNREACHABLE_DATABASE" ; exit 1 ; } 
    8281 
    8382# Does the stuff 
    84 $mysql "select concat(a.login, \" (\", a.mail, \")\") from membres a, sub_domaines b where a.uid = b.compte and concat(if(sub=\"\", \"\", concat(sub, \".\")), domaine)  = \"${1}\";" 
     83$mysql -B -N -e "select concat(a.login, \" (\", a.mail, \")\") from membres a, sub_domaines b where a.uid = b.compte and concat(if(sub=\"\", \"\", concat(sub, \".\")), domaine)  = \"${1}\";" 
    8584 
    8685 
  • install/alternc.install

    old new  
    55# on a new server. THIS SCRIPT ERASE ALL DATA ON THE AlternC SYSTEM !! 
    66# YOU HAVE BEEN WARNED ! 
    77 
     8# This script now assumes it has MySQL connectivity through 
     9# /etc/alternc/mysql.cnf 
     10 
    811set -e  
    912 
    1013. /usr/lib/alternc/functions.sh 
     
    7881 
    7982. /etc/alternc/local.sh 
    8083 
     84# XXX: copy-paste from debian/config 
     85if [ -r /etc/alternc/mysql.cnf ]; then 
     86    # make mysql configuration available as shell variables 
     87    # to convert from .cnf to shell syntax, we: 
     88    # * match only lines with "equal" in them (/=/) 
     89    # * remove whitespace around the = and add a left quote operator ' (;s) 
     90    # * add a right quote operator at the end of line (;s) 
     91    # * convert mysql variables into our MYSQL_ naming convention (;s) 
     92    # * print the result (;p) 
     93    eval `sed -n -e "/=/{s/ *= */='/;s/\$/'/;s/host/MYSQL_HOST/;s/user/MYSQL_LOGIN/;s/password/MYSQL_PASS/;s/database/MYSQL_DATABASE/;p}" /etc/alternc/mysql.cnf` 
     94fi 
     95 
    8196WARNING="WARNING: Do not edit this file, edit the one in /etc/alternc/templates and launch alternc.install again." 
    8297 
    8398VERSION="`dpkg -s alternc | sed -n -e 's/^Version: \(.*\)/\1/p'`" 
     
    101116    MONITOR_IP="127.0.0.1" 
    102117fi 
    103118 
    104 SED_SCRIPT=" 
     119# XXX: I assume this is secure if /tmp is sticky (+t) 
     120# we should have a better way to deal with templating, of course. 
     121SED_SCRIPT=`mktemp` 
     122cat > $SED_SCRIPT <<EOF 
    105123s\\%%hosting%%\\$HOSTING\\; 
    106124s\\%%fqdn%%\\$FQDN\\; 
    107125s\\%%public_ip%%\\$PUBLIC_IP\\; 
     
    121139s\\%%fqdn_lettre%%\\$FQDN_LETTER\\; 
    122140s\\%%version%%\\$VERSION\\; 
    123141s\\%%ns2_ip%%\\$NS2_IP\\; 
    124 
     142EOF 
    125143 
    126144####################################################################### 
    127145# Backup configuration files 
     
    145163for file in $CONFIG_FILES; do 
    146164    TEMPLATE="$TEMPLATE_DIR/${file##etc/}" 
    147165    if [ -f "$TEMPLATE" ]; then 
    148         sed -e "$SED_SCRIPT" < $TEMPLATE > /$file 
     166        sed -f "$SED_SCRIPT" < $TEMPLATE > /$file 
    149167    fi 
    150168done 
     169rm -f $SED_SCRIPT 
    151170 
    152171####################################################################### 
    153172# Save installed files to check them during next install 
    154173# 
    155174tar -zcf "$INSTALLED_CONFIG_TAR" -C / $CONFIG_FILES 
    156175 
    157 ###################################################################### 
    158 # Initialize database 
    159 # 
    160 if [ "$MYSQL_HOST" != "localhost" -o -e /usr/sbin/mysqld ]; then 
    161     echo "Setup MySQL and database..." 
    162     /usr/share/alternc/install/mysql.sh "$MYSQL_HOST" "$MYSQL_USER" "$MYSQL_PASS" "$MYSQL_DATABASE" 
    163 fi 
    164  
    165176########################################################################  
    166177# Ad-hoc fixes 
    167178# 
     
    263274/usr/lib/alternc/basedir_prot.sh 
    264275 
    265276# Creating admin user if needed 
    266 HAS_ROOT="`mysql -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASS" "$MYSQL_DATABASE" -e "SELECT COUNT(*) FROM membres WHERE login = 'admin' OR login = 'root' and su = 1" | tail -1`" 
     277HAS_ROOT=`mysql --defaults-file=/etc/alternc/mysql.cnf -e "SELECT COUNT(*) FROM membres WHERE login = 'admin' OR login = 'root' and su = 1" | tail -1` 
    267278if [ "$HAS_ROOT" != "1" ]; then 
    268279    echo "Creating admin user..." 
    269280    echo "" 
  • install/upgrade_check.sh

    old new  
    4545                  # run the proper program to interpret the upgrade script 
    4646                  case "$ext" in 
    4747                  sql) 
    48                         mysql -f -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASS $MYSQL_DATABASE
     48                        mysql -f --defaults-file=/etc/alternc/mysql.cnf
    4949                        < $file || true 
    5050                        ;; 
    5151                  php) 
  • install/mysql.sh

    old new  
    1 #!/bin/sh  
     1#!/bin/sh 
    22# 
    33# $Id: mysql.sh,v 1.11 2006/01/11 22:51:28 anarcat Exp $ 
    44# ---------------------------------------------------------------------- 
     
    2828# USAGE : "mysql.sh loginroot passroot systemdb" 
    2929# ---------------------------------------------------------------------- 
    3030# 
     31  
     32# This script expects the following environment to exist: 
     33# * host 
     34# * user 
     35# * password 
     36# * database 
     37#  
     38# XXX: the sed script should be generated here 
     39# 
     40# So this file should generally be sourced like this: 
     41# . /usr/share/alternc/install/mysql.sh 
     42# 
     43# Those values are used to set the username/passwords... 
    3144 
    32 sqlserver="$1" 
    33 rootlogin="$2" 
    34 rootpass="$3" 
    35 systemdb="$4" 
     45# The grant all is the most important right needed in this script. 
     46echo "Granting users..." 
     47cat <<EOF 
     48host: $host 
     49user: $user 
     50password: $password 
     51database: $database 
     52EOF 
    3653 
    37 if [ -z "$rootlogin" -o -z "$rootpass" -o -z "$systemdb" ] 
    38 then 
    39     echo "Usage: mysql.sh <mysqlserver> <rootlogin> <rootpass> <systemdb>" 
    40     exit 1 
    41 fi 
     54MYSQL_CONFIG="/etc/alternc/mysql.cnf" 
    4255 
    43 mysql="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf -h$sqlserver " 
     56. /etc/alternc/local.sh 
     57grant="GRANT ALL ON *.* TO '$user'@'${MYSQL_CLIENT}' IDENTIFIED BY '$password' WITH GRANT OPTION" 
    4458 
    45 # The grant all is the most important right needed in this script. 
     59echo -n "Trying debian.cnf: " 
     60mysql="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf" 
    4661# If this call fail, we may be connected to a mysql-server version 5.0. 
    47 echo "Granting users " 
    48 # In that case, change mysql parameters and retry. Use root / nopassword. 
    49 $mysql -e "GRANT ALL ON *.* TO '$rootlogin'@'${MYSQL_CLIENT}' IDENTIFIED BY '$rootpass' WITH GRANT OPTION" 
    50 if [ "$?" -ne "0" ] 
     62# In that case, change mysql parameters and retry. Use root / nopassword. 
     63if ! $mysql <<EOF 
     64$grant 
     65EOF 
    5166then 
    52     echo "debian-sys-maintainer doesn't have the right credentials, assuming we're doing an upgrade" 
    53     mysql="/usr/bin/mysql -h$sqlserver -u$rootlogin -p$rootpass"  
    54     $mysql -e "GRANT ALL ON *.* TO '$rootlogin'@'${MYSQL_CLIENT}' IDENTIFIED BY '$rootpass' WITH GRANT OPTION" 
    55     if [ "$?" -ne "0" ]  
    56         then  
    57         echo "Still not working, assuming clean install and empty root password" 
    58         mysql="/usr/bin/mysql -h$sqlserver -uroot " 
    59         $mysql -e "GRANT ALL ON *.* TO '$rootlogin'@'${MYSQL_CLIENT}' IDENTIFIED BY '$rootpass' WITH GRANT OPTION" 
    60         if [ "$?" -ne "0" ]  
     67    echo "failed: debian-sys-maintainer doesn't have the right credentials" 
     68    echo -n "are we doing an upgrade? " 
     69    mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG" 
     70    if ! $mysql <<EOF 
     71$grant 
     72EOF 
     73    then  
     74        echo "No" 
     75        echo -n "Assuming clean install (empty root password)... " 
     76        mysql="/usr/bin/mysql -h$host -uroot " 
     77        if ! $mysql <<EOF 
     78$grant 
     79EOF 
    6180        then  
    62             echo "Can't grant system user $rootlogin, aborting";  
    63             exit 1  
     81            echo "Failed" 
     82            echo -n "Assuming pre 0.9.8 version... " 
     83            mysql="/usr/bin/mysql -h$MYSQL_HOST -u$MYSQL_LOGIN -p$MYSQL_PASS" 
     84            if ! $mysql <<EOF 
     85$grant 
     86EOF 
     87            then 
     88                echo "No." 
     89                echo "Can't grant system user $user, aborting";  
     90                exit 1  
     91            fi 
    6492        fi 
    6593    fi 
    6694fi 
     95echo "ok!" 
    6796 
    68 # Now we can use rootlogin and rootpass.  
    69 mysql="/usr/bin/mysql -h$sqlserver -u$rootlogin -p$rootpass"  
     97if [ -f $MYSQL_CONFIG ]; then 
     98    echo "Updating mysql configuration in $MYSQL_CONFIG" 
     99else 
     100    echo "Creating mysql configuration in $MYSQL_CONFIG" 
     101    cat > $MYSQL_CONFIG <<EOF 
     102# AlternC - Web Hosting System - MySQL Configuration 
     103# Automatically generated by AlternC configuration, do not edit 
     104# This file will be modified on package configuration 
     105# (e.g. upgrade or dpkg-reconfigure alternc) 
     106[client] 
     107EOF 
     108    chown root:www-data $MYSQL_CONFIG 
     109    chmod 640 $MYSQL_CONFIG 
     110fi 
    70111 
    71 echo "Setting AlternC '$systemdb' system table and privileges " 
    72 $mysql -e "CREATE DATABASE IF NOT EXISTS $systemdb;"  
     112# create a sed script to create/update the file 
     113function set_value() { 
     114    var=$1 
     115    RET=$2 
     116    grep -Eq "^ *$var=" $MYSQL_CONFIG || echo "$var=" >> $MYSQL_CONFIG 
     117    SED_SCRIPT="$SED_SCRIPT;s\\^ *$var=.*\\$var=\"$RET\"\\" 
     118
    73119 
    74 echo "Installing AlternC schema " 
    75 $mysql $systemdb < /usr/share/alternc/install/mysql.sql 
     120SED_SCRIPT="" 
     121# hostname was empty in older (pre-0.9.6?) versions 
     122if [ -z "$host" ]; then 
     123    host="localhost" 
     124fi 
     125set_value host $host 
     126set_value database $database 
     127set_value user $user 
     128set_value password $password 
    76129 
    77 /usr/bin/mysql -h$sqlserver -u$rootlogin -p$rootpass $systemdb -e "SHOW TABLES" >/dev/null && echo "MYSQL.SH OK!" || echo "MYSQL.SH FAILED!" 
     130# take extra precautions here with the mysql password: 
     131# put the sed script in a temporary file 
     132SED_SCRIPT_NAME=`mktemp` 
     133cat > $SED_SCRIPT_NAME <<EOF 
     134$SED_SCRIPT 
     135EOF 
     136sed -f "$SED_SCRIPT_NAME" < $MYSQL_CONFIG > $MYSQL_CONFIG.$$ 
     137mv -f $MYSQL_CONFIG.$$ $MYSQL_CONFIG 
     138rm -f $SED_SCRIPT_NAME 
     139 
     140echo "Checking for MySQL connectivity" 
     141# Now we should be able to use the mysql configuration 
     142mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG" 
     143 
     144$mysql -e "SHOW TABLES" >/dev/null && echo "MYSQL.SH OK!" || echo "MYSQL.SH FAILED!" 
     145 
     146# Final mysql setup: db schema 
     147 
     148echo "installing AlternC schema in $database..." 
     149$mysql -e "CREATE DATABASE IF NOT EXISTS $database;" || echo cannot create database 
     150$mysql < /usr/share/alternc/install/mysql.sql || echo cannot load database schema