source: zero-lliurex-dni/trunk/fuentes/zero-lliurex-dni.zlaunchers/zero-lliurex-dni.zmd @ 5019

Last change on this file since 5019 was 5019, checked in by jrpelegrina, 2 years ago

WIP in fix dni installation

File size: 9.4 KB
Line 
1#!/bin/bash
2
3#set -x
4
5## Global Variables
6###################
7
8export TEXTDOMAIN="zero-lliurex-dni"
9
10USER_NAME=$USER # User name
11USER_HOME="" # Home user folder
12FF_PROFILE_PATH="" # Absolute path to user firefox profile
13TH_PROFILE_PATH="" # Absolute path to user firefox profile
14LIBRARY="/usr/lib/libpkcs11-dnie.so"
15OLD_LIBRARY="/usr/lib/opensc-pkcs11.so /usr/lib/libpkcs11-dnie.so"
16ERROR=0
17
18
19
20# MSGS
21MSG_LIBRARY_NOT_FOUND="$(gettext 'Library not found:')"
22MSG_SOME_PROBLEM_FIREFOX="$(gettext 'Some problem with firefox profile')"
23MSG_ONCE_FIREFOX="$(gettext 'Start firefox once at least')"
24MSG_SOME_PROBLEM_THUNDER="$(gettext 'Some problem with Thunderbird profile')"
25MSG_ONCE_THUNDER="$(gettext 'Start Thunderbird once at least')"
26MSG_RUNNING_FIREFOX="$(gettext 'Firefox is running. Stop it!')"
27MSG_RUNNING_THUNDER="$(gettext 'Thunderbird is running. Stop it!')"
28MSG_NOT_FIREFOX="$(gettext 'Firefox is not installed')"
29MSG_NOT_THUNDER="$(gettext 'Thunderbird is not installed')"
30MSG_INSTALLING_PROGRESS="$(gettext 'Installing DNIe in your computer...')"
31MSG_ZENITY_FIREFOX_1="$(gettext 'Installing DNIe modules in your system')"
32MSG_ZENITY_FIREFOX_2="$(gettext 'DNIe modules in Firefox')"
33MSG_ZENITY_THUNDER_1="$(gettext 'DNIe modules in Thunderbird')"
34MSG_FINAL_ERROR="$(gettext 'Impossible to install DNIe module, correct the problems and execute this program again please.')"
35MSG_FINAL_OK="$(gettext 'DNIe modules are installed with success in Firefox and Thunderbird profile, for others users you have to execute this action again on each session.')"
36MSG_CREATING_THUNDER="$(gettext 'We have to create your Thunderbird profile, now it will be launched, then please close Thunderbird and installation continues')"
37
38#instalo paquetes necesarios de los repositorios
39
40zero-installer install libpkcs11-dnie
41# Si falla la instalacion del paquete se aborat el resto y da error
42if [ $? != 0 ] ; then
43  zenity --error --text="Impossible to install libpkcs11-dnie problems with repos. $MSG_FINAL_ERROR" 
44  exit 1
45fi
46
47
48#inicializo variable de error en un archivo de tmp ya que se produce una subshell en el zenity
49
50echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
51
52user_home(){
53   
54  USER_HOME=$(getent passwd "$USER_NAME" | cut -d: -f 6)
55
56}
57
58test_libpkcs11_dnie(){
59
60  if [ ! -f $LIBRARY ]; then
61    zenity --info --text="$MSG_LIBRARY_NOT_FOUND $LIBRARY"
62    exit 1
63    ERROR=1
64    echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
65  fi
66
67}
68
69
70init_secmod_firefox(){
71
72  if [ -f "$FF_PROFILE_PATH/secmod.db" ]; then
73    rm $FF_PROFILE_PATH/secmod.db
74  fi
75
76}
77
78
79init_secmod_thunderbird(){
80
81  if [ -f "$TH_PROFILE_PATH/secmod.db" ]; then
82    rm $TH_PROFILE_PATH/secmod.db
83  fi
84
85}
86
87test_running_firefox(){
88
89  is_running=$(ps -fe | grep /usr/lib/firefox/firefox | grep -v grep | wc -l)
90  if [ $is_running -gt 0 ]; then
91    return 1
92  else
93    return 0
94  fi
95
96}
97
98test_running_thunderbird(){
99
100  is_running=$(ps -fe | grep thunderbird | grep -v grep | wc -l)
101  if [ $is_running -gt 0 ]; then
102    while [ $is_running -gt 0 ] ; do
103      zenity --info --text="$MSG_RUNNING_THUNDER"
104      is_running=$(ps -fe | grep thunderbird | grep -v grep | wc -l)
105    done
106  fi
107  return 0
108       
109}
110
111test_installed_package(){
112
113  dpkg -s $1
114  iRESULT=$?
115  if [ $iRESULT -eq 1 ]; then
116    return 0
117  else
118    return 1
119  fi
120
121}
122
123## Firefox functions
124####################
125
126get_firefox_default_profile(){
127   
128   # Getting Mozilla products default profile
129   # http://kb.mozillazine.org/Profiles.ini_file
130   #
131   # Get default profile info
132   # This script must be capable of dealing with multiple profiles
133   # and relative or absolute paths, for now ... buff!!
134   #
135   # This script get Default path profile if Default=1 or
136   # get first profile in other case
137 
138  TMP_FILE=$(mktemp)
139  cat $FF_PATH/profiles.ini | awk -F ' *= *' '{ if ($1 ~ /^\[/) section=$1; else if ($1 !~ /^$/) print $1 section "=" "\"" $2 "\"" }' > $TMP_FILE
140  DEFAULT_EXISTS=$(grep ^Default $TMP_FILE)
141  if [ -n $DEFAULT_EXISTS ]; then
142    PROFILE_ID=$(cat $TMP_FILE | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
143  else
144    PROFILE_ID=$(echo $DEFAULT_EXISTS | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
145  fi
146  IS_RELATIVE=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^IsRelative | cut -d"=" -f 2 | sed -e 's/\"//g')
147  TMP_PATH=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^Path | cut -d"=" -f 2 | sed -e 's/\"//g')
148  if [ $IS_RELATIVE = "0" ]; then
149    FF_PROFILE_PATH=$TMP_PATH
150  else
151    FF_PROFILE_PATH=$FF_PATH/$TMP_PATH
152  fi
153  rm -f $TMP_FILE
154
155}
156
157get_thunderbird_default_profile(){
158   
159   # Getting Mozilla products default profile
160   # http://kb.mozillazine.org/Profiles.ini_file
161   #
162   # Get default profile info
163   # This script must be capable of dealing with multiple profiles
164   # and relative or absolute paths, for now ... buff!!
165   #
166   # This script get Default path profile if Default=1 or
167   # get first profile in other case
168 
169  TMP_FILE=$(mktemp)
170  cat $TH_PATH/profiles.ini | awk -F ' *= *' '{ if ($1 ~ /^\[/) section=$1; else if ($1 !~ /^$/) print $1 section "=" "\"" $2 "\"" }' > $TMP_FILE
171  DEFAULT_EXISTS=$(grep ^Default $TMP_FILE)
172  if [ -n $DEFAULT_EXISTS ]; then
173    PROFILE_ID=$(cat $TMP_FILE | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
174  else
175    PROFILE_ID=$(echo $DEFAULT_EXISTS | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
176  fi
177  IS_RELATIVE=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^IsRelative | cut -d"=" -f 2 | sed -e 's/\"//g')
178  TMP_PATH=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^Path | cut -d"=" -f 2 | sed -e 's/\"//g')
179  if [ $IS_RELATIVE = "0" ]; then
180    TH_PROFILE_PATH=$TMP_PATH
181  else
182    TH_PROFILE_PATH=$TH_PATH/$TMP_PATH
183  fi
184  rm -f $TMP_FILE
185
186}
187
188test_firefox_user_profile() {
189
190  if [ -d $FF_PATH ]; then
191    get_firefox_default_profile
192    if [ ! -d $FF_PROFILE_PATH ]; then
193      zenity --info --text="$MSG_SOME_PROBLEM_FIREFOX"
194      ERROR=1
195      echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
196      return 0
197    fi
198    return 1
199  else
200    zenity --info --text="$MSG_ONCE_FIREFOX"
201    ERROR=1
202    echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
203    exit 3
204  fi
205       
206}
207
208test_thunderbird_user_profile() {
209
210  if [ -d $TH_PATH ]; then
211    get_thunderbird_default_profile
212    if [ ! -d $TH_PROFILE_PATH ]; then
213      zenity --info --text="$MSG_SOME_PROBLEM_THUNDER" 
214      ERROR=1
215      echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
216      return 0
217    fi
218    return 1
219  else
220    zenity --info --text="$MSG_CREATING_THUNDER"
221    su $USER -c "thunderbird"
222    test_running_thunderbird
223    get_thunderbird_default_profile
224  fi
225       
226}
227
228test_firefox_dni_module(){
229
230  if [ -z $FF_MODULE_EXISTS ]; then
231    return 1
232  fi
233  return 0
234
235}
236
237test_thunderbird_dni_module(){
238
239  if [ -z $TH_MODULE_EXISTS ]; then
240    return 1
241  fi
242  return 0
243
244}
245
246remove_dni_module_firefox(){
247
248   su $USER -c "modutil -disable $FF_MODULE_NAME -force -dbdir $FF_PROFILE_PATH 2>&1"
249   su $USER -c "modutil -delete $FF_MODULE_NAME -force -dbdir $FF_PROFILE_PATH 2>&1"
250}
251
252remove_dni_module_thunderbird(){
253
254   su $USER -c "modutil -disable $TH_MODULE_NAME -force -dbdir $TH_PROFILE_PATH 2>&1"
255   su $USER -c "modutil -delete $TH_MODULE_NAME -force -dbdir $TH_PROFILE_PATH 2>&1"
256}
257
258enable_dni_module_firefox(){
259
260   su $USER -c "modutil -add $FF_MODULE_NAME -force -libfile $LIBRARY -dbdir $FF_PROFILE_PATH 2>&1"
261   su $USER -c "modutil -enable $FF_MODULE_NAME -force -dbdir $FF_PROFILE_PATH 2>&1"
262}
263
264enable_dni_module_thunderbird(){
265
266   su $USER -c "modutil -add $TH_MODULE_NAME -force -libfile $LIBRARY -dbdir $TH_PROFILE_PATH 2>&1"
267   su $USER -c "modutil -enable $TH_MODULE_NAME -force -dbdir $TH_PROFILE_PATH 2>&1"
268}
269
270do_firefox(){
271
272  test_installed_package "firefox"
273  if [ $? ]; then
274    test_running_firefox
275    if [ $? -gt 0 ]; then
276      ERROR=1
277      echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
278      zenity --info --text="$MSG_RUNNING_FIREFOX"
279      exit 2
280    else
281      test_firefox_user_profile
282      test_ff_up=$?
283      echo "--->"$test_ff_up"<---"
284      if [ $? ]; then
285        init_secmod_firefox
286        remove_dni_module_firefox
287        enable_dni_module_firefox
288      fi
289    fi
290  else
291    ERROR=1
292    echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
293    zenity --info --text="$MSG_NOT_FIREFOX"
294    exit 1
295  fi
296
297}
298
299do_thunderbird(){
300
301  test_installed_package "thunderbird"
302  if [ $? ]; then
303    test_running_thunderbird # and waits to close
304    test_thunderbird_user_profile # and create if doesnt exist
305    test_th_up=$?
306    echo "--->"$test_th_up"<---"
307    if [ $? ]; then
308      init_secmod_thunderbird
309      remove_dni_module_thunderbird
310      enable_dni_module_thunderbird
311    fi
312  else
313    zenity --info --text="$MSG_NOT_THUNDER"
314    ERROR=1
315    echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
316    exit 1
317  fi
318}
319
320## Get user home folder
321#######################
322
323
324## Test if dni library exists
325#############################
326
327
328# firefox
329(
330test_libpkcs11_dnie
331user_home
332echo "# $MSG_ZENITY_FIREFOX_1" ; sleep 1
333echo "20"
334echo "# $MSG_ZENITY_FIREFOX_2" ; FF_MODULE_NAME="DNI_E"
335echo "40"
336echo "# $MSG_ZENITY_FIREFOX_2"; FF_PATH="$USER_HOME/.mozilla/firefox"
337do_firefox
338echo "60"
339echo "# $MSG_ZENITY_THUNDER_1";TH_MODULE_NAME="DNI_E"
340echo "80"
341echo "# $MSG_ZENITY_THUNDER_1";TH_PATH="$USER_HOME/.thunderbird"
342do_thunderbird
343echo "100"
344) |
345zenity --progress \
346  --title="ZERO-LLIUREX-DNIe" \
347  --text="$MSG_INSTALLING_PROGRESS" \
348  --percentage=0 \
349  --auto-close \
350   --width=500 \
351  --auto-kill
352 
353#capturo el error a ver si todo fue bien o fallo algo
354ERROR=$(cat /tmp/zero-lliurex-dni.tmp)
355
356if [ $ERROR != 0 ] ; then 
357  zenity --error --text="$MSG_FINAL_ERROR" 
358  exit 1
359else
360  zenity --info --text="$MSG_FINAL_OK"
361fi
362exit 0
Note: See TracBrowser for help on using the repository browser.