slitaz-tools view tinyutils/slitaz-config @ rev 865

slitaz-config: be quiet when setting passwd
author Christophe Lincoln <pankso@slitaz.org>
date Mon Apr 21 21:36:48 2014 +0200 (2014-04-21)
parents 5062327d8e93
children 4463981d5d1d
line source
1 #!/bin/sh
2 #
3 # SliTaz Config - A tool with all SliTaz Ncurses configs in one place for
4 # text mode systems (server, ARM devices)
5 #
6 # Copyright (C) 2014 SliTaz ARM - BSD License
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
9 . /lib/libtaz.sh
10 check_root
12 title="{ SliTaz Config }"
13 about="/usr/share/doc/slitaz/post-install.txt"
14 tmpdir="/tmp/$(basename $0)"
15 tmp="$tmpdir/$$"
16 height="20"
17 width="72"
19 # Use a tmp directory
20 mkdir -p ${tmpdir}
22 #
23 # GUI Functions
24 #
26 # Coded for the ARM first boot settings
27 about_post_install() {
28 dialog --cr-wrap \
29 --title "{ Post Installation }" \
30 --exit-label "Continue" \
31 --textbox "$about" ${height} ${width}
32 }
34 # Set root passwd
35 root_passwd() {
36 dialog --title "{ Root Password }" --colors \
37 --inputbox "\nEnter new password for \Zb\Z1root" \
38 12 ${width} 2>${tmp}
39 passwd=$(cat $tmp)
40 [ "$passwd" == "" ] && return 0
41 echo "root:$passwd" | chpasswd --md5 >/dev/null
42 }
44 # Add a new user
45 add_user() {
46 title="{ Add User }"
47 dialog --title "$title" --colors \
48 --inputbox "\nEnter login name for the new \Zb\Z4user" 12 ${width} 2>${tmp}
49 user=$(cat $tmp)
50 [ "$user" == "" ] && return 0
51 dialog --title "$title" --colors \
52 --inputbox "\nEnter password for user \Zb\Z4${user}" 12 ${width} 2>${tmp}
53 passwd=$(cat $tmp)
54 [ "$passwd" == "" ] && return 0
55 adduser -D -g "SliTaz User" -G users ${user}
56 echo "$user:$passwd" | chpasswd --md5 >/dev/null
57 # User groups
58 for group in audio cdrom video tty; do
59 addgroup ${user} ${group}
60 done
61 # Slim default user on post-install
62 if [ -f "/etc/slim.conf" ] && [ ! -f "/var/lib/slitaz/post-install" ]; then
63 sed -i s"/default_user .*/default_user $user/" /etc/slim.conf
64 fi
65 }
67 set_date() {
68 clear && newline
69 echo -n "Old date:"; date
70 rdate -s tick.greyware.com 2>/dev/null
71 echo -n "New date:"; date
72 sleep 4
73 }
75 # Catch ESSIDs and format output for Ncurses Dialog or GTK Yad tree.
76 # We get the list of networks by Cell and without spaces.
77 # Usage: detect_wifi --output=gtk (default output to dialog)
78 detect_wifi() {
79 . /etc/network.conf
80 ifconfig $WIFI_INTERFACE up
81 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
82 for i in $(iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep "Cell-" | awk '{print $1}')
83 do
84 scan=$(iwlist $WIFI_INTERFACE scan last | \
85 awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
86 sed s/"Cell "/Cell-/ | grep -A 5 "$i")
87 essid=$(echo $scan | cut -d '"' -f 2)
89 if echo "$scan" | grep -q Quality; then
90 quality=$(echo $scan | sed 's/.*Quality=\([^ ]*\).*/\1/' | sed 's/.*Quality:\([^ ]*\).*/\1/')
91 else
92 quality="$(_n '-----')"
93 fi
95 crypto=$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')
96 # Check encryption type
97 if echo "$scan" | grep -q WPA*; then
98 crypto="WPA"
99 fi
101 # Connected or not connected...
102 if ifconfig | grep -A 1 $WIFI_INTERFACE | \
103 grep -q inet && iwconfig $WIFI_INTERFACE | \
104 grep ESSID | grep -q -w "$essid"; then
105 status=connected
106 else
107 status="--"
108 fi
110 # Output
111 case "$output" in
112 gtk)
113 echo -e "$( _n 'any')\n$(_n 'N/A')\n$(_n 'none')\n$(_n '-')"
114 echo -e "$essid\n$quality\n$crypto\n$status" ;;
115 *)
116 echo "$essid" "QA_${quality}_KEY_${crypto}_${status}" ;;
117 esac
118 done
119 fi
120 }
122 # Wireless config so users dont have to edit any config files on post
123 # install to get connected. If the wired connection is used it will auto
124 # connect with DHCP so no need for a dialog frontend.
125 wifi_setup() {
126 . /etc/network.conf
127 dialog \
128 --clear --title "$title" \
129 --ok-label "Select" \
130 --menu "\n$(gettext 'Connect to a Wi-Fi network')" \
131 ${height} ${width} 14 \
132 "any" "Quality_N/A" \
133 $(detect_wifi) 2>${tmp}
135 # Handle options
136 case "${?}" in
137 1|255) rm -rf ${tmpdir} && exit 0 ;;
138 0) essid=$(cat $tmp) ;;
139 esac
141 # Check if we need to prompt user for an encrypted network
142 scan=$(iwlist $WIFI_INTERFACE scan | \
143 awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
144 sed s/"Cell "/Cell-/ | grep -A 5 "$essid")
145 if echo "$scan" | sed 's/.*key:\([^ ]*\).*/\1/' | grep -q WPA*; then
146 dialog --title "{ Wi-Fi Password }" --colors \
147 --inputbox "\nEnter wifi key (password) for \Zb\Z4${essid}" \
148 12 ${width} 2>${tmp}
149 key=$(cat $tmp)
150 [ "$key" == "" ] && return 0
151 fi
153 # Configure connection
154 {
155 echo "XXX" && echo 30
156 echo -e "\nShuting down network interfaces..."
157 echo "XXX"
158 stopd network.sh >/dev/null 2>&1 && sleep 2
160 echo "XXX" && echo 30
161 echo -e "\nConfiguring: /etc/network.conf..."
162 echo "XXX"
163 # WIFI_KEY_TYPE=any should work for WEP/WPA*
164 sed -i \
165 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
166 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
167 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"any\"/" \
168 /etc/network.conf
169 sleep 1
171 echo "XXX" && echo 60
172 echo -e "\nRestarting wifi interface..."
173 echo "XXX"
174 startd network.sh >/dev/null 2>&1 && sleep 2
176 echo "XXX" && echo 90
177 echo -e "\nChecking connection..."
178 ip=$(ifconfig | fgrep -A 1 "encap:Ethernet" | fgrep "inet" | \
179 cut -d ":" -f 2 | awk '{print $1}')
180 echo "XXX"
181 sleep 1
183 echo "XXX" && echo 100
184 if [ "$ip" ]; then
185 echo -e "\nIP address: $ip"
186 else
187 echo -e "\nUnable to connect..."
188 fi
189 echo "XXX" && sleep 2
191 } | dialog --title "{ Wi-Fi Config }" --gauge "" 8 ${width} 0
193 #iwlist $WIFI_INTERFACE scan essid $essid last
194 echo "CONF: $WIFI_INTERFACE $essid $passwd ${essid_crypto}"
195 }
197 # Main Dialog menu
198 main_box() {
199 dialog \
200 --clear --title "$title" \
201 --ok-label "Exec" --cancel-label "Quit" \
202 --menu "" ${height} ${width} 14 \
203 "keyboard" "$(gettext 'System keyboard setting')" \
204 "locale" "$(gettext 'System language setting')" \
205 "wifi-setup" "$(gettext 'Wi-Fi network settings')" \
206 "add-user" "$(gettext 'Add a new user')" \
207 "root-passwd" "$(gettext 'Change root password')" \
208 "set-date" "$(gettext 'Set system date from the web')" \
209 "quit" "$(gettext 'Exit from SliTaz Config')" 2>${tmp}
211 # Handle options
212 opt=${?}
213 case "$opt" in
214 1|255) rm -rf ${tmpdir} && exit 0 ;;
215 esac
217 # Handle actions
218 action=$(cat $tmp)
219 case "$action" in
220 keyboard) tazkeymap ;;
221 locale) tazlocale ;;
222 wifi-setup) wifi_setup ;;
223 add-user) add_user ;;
224 root-passwd) root_passwd ;;
225 set-date) set_date ;;
226 quit) rm -rf ${tmpdir} && exit 0 ;;
227 esac
228 }
230 #
231 # Handle commands
232 #
234 case "$1" in
235 *_*)
236 # Execute functions
237 $@ ;;
238 *)
239 while true; do
240 main_box
241 done ;;
242 esac
244 # Clean exit
245 #rm -rf ${tmpdir}
246 exit 0