le-italc-config.sh 12.4 KB
Newer Older
1
#!/bin/sh
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
# Copyright (C) 2004-2013 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of le-save-config
#
# le-save-config is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

#ASK FOR ROOT
if [ $(id -u) != 0 ]; then
    echo "This script must be run as root!"
26
    gksu -S -l -m "É necessário ter permissão de superusuário. Digite a senha:" "sh /usr/sbin/le-italc-config.sh"
27 28 29
    exit $?
fi

30 31 32 33 34
#GLOBAL VARIABLES
#Script path to be created for monoterminal:
SCRIPT_MONO_PATH="/etc/lightdm/greeter-exec-scripts/ica-auto-starter.sh"
#Script path to be created for multiterminal:
SCRIPT_MULT_PATH="/etc/profile.d/ica-start-multiterminal.sh"
35 36


37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#=================================UNCONFIG-FUNCTIONS===============================

unconfig_monoterminal () {
    if [ ! -f "$SCRIPT_MONO_PATH" ]; then
        zenity --warning --title="Aviso" --text="Não foi identificada nenhuma configuração monoterminal"
        return 1
    else
        #Remove key and generated script
        rm -f /etc/italc/public/teacher/key
        rm -f "$SCRIPT_MONO_PATH"
        return 0
    fi
}

unconfig_multiterminal () {
    if [ ! -f "$SCRIPT_MULT_PATH" ]; then
        zenity --warning --title="Aviso" --text="Não foi identificada nenhuma configuração multiterminal"
        return 1
    else
        #Remove key and generated script
        rm -f "/etc/italc/public/teacher/key"
        rm -f "$SCRIPT_MULT_PATH"
        return 0
    fi
    #This function does not remove computers entries from italc.
}
#====================================================================================
64 65

#==WELCOME===========================================================
66 67 68 69 70 71
zenity --question --title="Configurador do iTalc" \
       --height=100 --width=600 \
       --text="Bem vindo ao auto configurador do iTalc\! \
          \n\nExecute esse configurador primeiramente na máquina designada para ser o servidor do iTalc e em seguida nos terminais clientes. No caso de estar em um multiterminal apenas uma execução desse instalador é necessária para configuração da estação cliente e servidor. \
          \n\nClique em Continuar para prosseguir com a configuração" \
          --ok-label="Continuar" --cancel-label="Sair"
72
[ "$?" = "1" ] && exit 0 #Cancel Pressed
73 74 75
#====================================================================

#Configuration Variables Names:
76 77 78 79
# (Values: 1-True 0-False)
#
#  isMult = multiterminal or 'monoterminal'?
#  isClient = Client or Server? 
80 81

#QUESTIONS
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#If already exist a configuration
if [ -f "$SCRIPT_MONO_PATH" -o -f "$SCRIPT_MULT_PATH" ]; then
    #ASK: Is it configuration?
    zenity --question --title="Configurador do iTalc" \
           --text="Foi detectada uma configuração prévia do iTalc nessa máquina.
                   \nDeseja continuar? \
                   \nTodas as configurações anteriores serão desfeitas" \
           --ok-label="Sim" --cancel-label="Não"
    [ $? -eq 1 ] && exit 0
    if [ -f "$SCRIPT_MONO_PATH" ]; then
        unconfig_monoterminal
    else #Multiteminal
        unconfig_multiterminal
    fi
fi

#ASK: Is it multiterminal?
zenity --question --title="Configurador do iTalc" \
101
       --height=100 --width=450 \
102
       --text="Você está em qual tipo de estação?" \
103
       --ok-label="Multiterminal" --cancel-label="Monoterminal"
104
if [ $? -eq 0 ]; then #MULTITERMINAL
105
    isMult=1
106
    #ASK: How much terminals there is(for iTalc auto-adder)
107
    numTerm=$(
108 109 110 111 112 113
        zenity --title="Número de terminais" --height=330 \
            --text="Selecione o número de terminais do seu multiterminal:" \
            --list --radiolist \
            --column="Selecionado" \
            --column="Número de terminais" \
           "TRUE" "1" "" "2" "" "3" "" "4" "" "5" "" "6" "" "7" "" "8" "" "9"
114 115 116 117 118
    )
    if [ "$?" = "1" ]; then
        echo "Operação Cancelada"
        exit 4
    fi
119
else #MONOTERMINAL
120
    isMult=0
121
    #If it is MONOterminal it can be Client or Master configuration
122 123
    #ASK: Master or Client machine/terminal?
    zenity --question --title="Configurador do iTalc" \
124
           --height=100 --width=450 \
125
           --text="Esta estação vai ser usada como cliente ou servidora?" \
126 127 128 129 130 131
           --ok-label="Cliente" --cancel-label="Servidor"
    if [ $? -eq 0 ]; then #Sim
        isClient=1
    else
        isClient=0
    fi
132 133
fi

134
#ASK: Confirm
135 136
[ "$isMult" = "1" ] && s1="Multiterminal" || s1="Monoterminal"
[ "$isClient" = "1" ] && s2="Cliente" ||  s2="Servidor"
137 138
[ "$isMult" = "1" ] && s2="Todas"

139
zenity --question --title="Configurador do iTalc" \
140 141 142
       --text="Verifique as informações: \
              \nTipo: $s1 \
              \nFunção: $s2" \
143
       --ok-label="Confirmar" --cancel-label="Cancelar" ##--cancel-label="Voltar"
144
[ "$?" = "1" ] && exit 0 #TODO: execute this script again 
145

146 147 148 149
##===================================================================================================================##
##---START_FUNCTIONS-------------------------------------------------------------------------------------------------##
##===================================================================================================================##

150
generate_script_monoterminal_client () {
151
    local g_file="$SCRIPT_MONO_PATH"
152 153 154 155 156 157 158
    echo "#!/bin/sh" > $g_file
    echo "ica &" >> $g_file
    
    chmod +x $g_file
}

generate_script_multiterminal () {
159
    #Create a script in profile.d  
160
 
161
    local g_file="$SCRIPT_MULT_PATH"
162 163 164
    echo "#!/bin/sh" > $g_file

    echo "#Privileged Users don't run ica" >> $g_file
165
    echo "if [ \"\$USER\" = \"admin\" ]; then" >> $g_file
166 167
    echo "    return 0" >> $g_file
    echo "fi" >> $g_file
168

169 170 171 172 173
    echo "#Do not execute ica on logout" >> $g_file
    echo "if ps -u \$USER -o comm | grep \"^ica\$\"; then" >> $g_file
    echo "    return 0" >> $g_file
    echo "fi" >> $g_file

174 175 176 177 178 179 180 181 182 183 184 185 186 187
    echo "#Resolve Port" >> $g_file
    echo "PORT=\"\$(( 11000 + \$(echo \$DISPLAY | awk -F \":\" '{print \$NF}' | cut -b 1) ))\"" >> $g_file

    echo "#ReSet the default port server for teacher(mobility to teachers computer)"
    echo "if [ \"\$USER\" = \"professor\" ]; then"
    echo "    italc_conf=\"/etc/xdg/iTALC Solutions/iTALC.conf\""
    echo "    if [ -e \"italc_conf\" ]; then #if file exist\""
    echo "        line=\$(sed -n '/CoreServerPort=/=' \"\$italc_conf\")"
    echo "        sed -i \"/CoreServerPort=/d\" \"\$italc_conf\""
    echo "        sed -i \"\${line}iCoreServerPort=\${PORT}\" \"\$italc_conf\""
    echo "    fi"
    echo "fi"
    
    echo "#Execute ICA" >> $g_file
188 189
    echo "if [ -n \"\$PORT\" ]; then" >> $g_file
    echo "    ica -port \$PORT &" >> $g_file
190 191 192
    echo "fi" >> $g_file

    chmod +x $g_file
193 194
}

195 196 197 198
config_client_monoterminal () {
    #Steps:
    ## 1. Ask for keys
    ## 2. Import keys
199
    zenity --question --title="Configurador do iTalc" \
200
        --text="Para configurar o cliente é necessário ter sido configurada a estação servidora e adquirido o arquivo Chave-Italc.\n\nClique em Selecionar arquivo e selecione Chave-Italc." \
201 202 203
        --ok-label="Selecionar Arquivo" --cancel-label="Sair"
    [ "$?" = "1" ] && exit 0

204
    while [ 1 ]; do
205
        local key_file="$(zenity --file-selection)"
206 207
        local key_filename="$( echo "$key_file" | awk -F "/" '{print $NF}')"
        if [ "$key_filename" != "Chave-Italc" ]; then
208
            zenity --question --title="Configurador do iTalc" --text="Arquivo inválido." \
209 210
                   --height=150 --width=250 \
                   --ok-label="Selecionar outro arquivo" --cancel-label="Sair"
211
            [ "$?" = "1" ] && exit 0
212
            continue
213
        fi
214
        break
215 216 217
    done
    imc -importpublickey "$key_file"
    chmod 755 /etc/italc/public -R
218

219
    generate_script_monoterminal_client #generate ica autorun script
220 221 222 223 224 225 226 227 228
}

config_master_monoterminal () {
    local tmp_dir_chave="$(mktemp -d)"
    imc -role teacher -createkeypair

    #Protect private keys
    chown $SUDO_USER /etc/italc/keys/ -R
    sudo -u $SUDO_USER chmod 700 /etc/italc/keys/private -R
229 230 231
    #Release public key
    sudo -u $SUDO_USER chmod 755 /etc/italc/keys/public -R    

232
    #Show folder with the key to copy
233
    cp /etc/italc/keys/public/teacher/key $tmp_dir_chave/Chave-Italc
234 235 236 237

    #Give key file to user
    chown $SUDO_USER $tmp_dir_chave -R

238
    sudo -u $SUDO_USER nautilus $tmp_dir_chave &
239
    sleep 2 #time to open nautilus
240
    zenity --info --title="Configurador do iTalc" --text="Copie o arquivo Chave-Italc para as estações clientes, ele será solicitado pelo configurador."
241 242

}
243
config_multiterminal () {
244

245
    #Generate public and private keys
246 247 248
    imc -role teacher -createkeypair

    #Protect private keys
249 250
    chown professor /etc/italc/keys/ -R
    sudo -u professor chmod 700 /etc/italc/keys/private -R
251
    #Release public key
252
    sudo -u professor chmod 755 /etc/italc/keys/public -R
253
   
254
    imc -setconfigvalue Authentication/LogonAuthenticationEnabled=0 #Don't ask for login to enter italc
255
    generate_script_multiterminal #ica autorun 
256

257 258 259 260
    if (who --users | awk -F " " '{print $1}' | grep -q "^professor$"); then #If teacher is logged in execute ica(no need for relogin)
        su professor -c "ica & disown"
    fi

261
}
262 263 264
add_computers_multiterminal () {
    local num_of_computers="$1"
    local configFile="/home/professor/.italc/GlobalConfig.xml"
265
    local configFileDir="/home/professor/.italc/"
266 267 268

    #Configuration File exists?
    if [ ! -f "$configFile" ]; then
269
        mkdir -p $configFileDir
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        touch $configFile
    fi
    bool_xml="0" # Is xml header missing?
    grep -q "<?xml version=" $configFile;
    if [ "$?" = "1" ]; then
        echo "<?xml version=\"1.0\"?>" >> $configFile
        echo "<!DOCTYPE italc-config-file>"  >> $configFile
        echo "<globalclientconfig version=\"2.0.1\">" >> $configFile
        echo "  <body>" >> $configFile
        bool_xml=1
        numSala="1"
    else
        # If xml exist resolve a name for classroom that does not match any already there
        for numSala in 1 2 3 4 5 6 7 8 9; do
            grep -q "Sala De Aula $numSala" $configFile
            [ "$?" != "0" ] && break
        done
        ##Remove ending
        sed -i '/<\/body>/d' $configFile
        sed -i '/<\/globalclientconfig>/d' $configFile
    fi
291

292 293 294 295 296 297 298 299 300 301 302 303 304
    ##Add Class and Hostnames
    echo "    <classroom name=\"Sala De Aula $numSala\">" >> $configFile
    for i in $(seq 1 $num_of_computers); do #add even the professor(since we don't know which display he will be at)
        rand_num="$(tr -cd 0-9 </dev/urandom | head -c 6)" #Get random number of 6 digit
        echo "      <client hostname=\"localhost:1100$i\" mac=\"\" type=\"0\" id=\"$rand_num\" name=\"Estação $i\"/>" >> $configFile
    done
    echo "    </classroom>" >> $configFile

    ##Refinish the file
    echo "  </body>" >> $configFile
    echo "</globalclientconfig>" >> $configFile

}
305
showHelp () {
306 307
    #Show help page
    su $SUDO_USER -c "sensible-browser /usr/local/share/doc/le-save-config/help.html"
308
    return 0
309 310
}

311 312 313 314
##===================================================================================================================##
##---END_FUNCTIONS---------------------------------------------------------------------------------------------------##
##===================================================================================================================##

315 316 317 318 319 320 321 322
#Call right configuration function
if [ "$isMult" = "0" ]; then
    if [ "$isClient" = "1" ]; then
        config_client_monoterminal
    else
        config_master_monoterminal
    fi
elif [ "$isMult" = "1" ]; then
323
    config_multiterminal
324 325
    add_computers_multiterminal "$numTerm"

326
    zenity --question --height=150 --width=350 \
327 328
    --title="Multiterminal configurado!" --text="AVISO: O usuário professor deve usar a estação conectada na placa onboard. \
                \n\nAlém disso, para que todas as configurações tomem efeito é necessário relogar todos os usuários alunos." \
329
    --ok-label="Ajuda" --cancel-label="Concluir"
330
    [ "$?" =  "0" ] && showHelp || exit 0
331
fi