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

slitaz-config: we must set WIFI to yes when configured
author Christophe Lincoln <pankso@slitaz.org>
date Mon Apr 21 21:47:00 2014 +0200 (2014-04-21)
parents 7d858d9a512d
children aa153d70ece3
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=.*/WIFI=\"yes\"/" \
166 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
167 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
168 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"any\"/" \
169 /etc/network.conf
170 sleep 1
172 echo "XXX" && echo 60
173 echo -e "\nRestarting wifi interface..."
174 echo "XXX"
175 startd network.sh >/dev/null 2>&1 && sleep 2
177 echo "XXX" && echo 90
178 echo -e "\nChecking connection..."
179 ip=$(ifconfig | fgrep -A 1 "encap:Ethernet" | fgrep "inet" | \
180 cut -d ":" -f 2 | awk '{print $1}')
181 echo "XXX"
182 sleep 1
184 echo "XXX" && echo 100
185 if [ "$ip" ]; then
186 echo -e "\nIP address: $ip"
187 else
188 echo -e "\nUnable to connect..."
189 fi
190 echo "XXX" && sleep 2
192 } | dialog --title "{ Wi-Fi Config }" --gauge "" 8 ${width} 0
193 }
195 # Main Dialog menu
196 main_box() {
197 dialog \
198 --clear --title "$title" \
199 --ok-label "Exec" --cancel-label "Quit" \
200 --menu "" ${height} ${width} 14 \
201 "keyboard" "$(gettext 'System keyboard setting')" \
202 "locale" "$(gettext 'System language setting')" \
203 "wifi-setup" "$(gettext 'Wi-Fi network settings')" \
204 "add-user" "$(gettext 'Add a new user')" \
205 "root-passwd" "$(gettext 'Change root password')" \
206 "set-date" "$(gettext 'Set system date from the web')" \
207 "quit" "$(gettext 'Exit from SliTaz Config')" 2>${tmp}
209 # Handle options
210 opt=${?}
211 case "$opt" in
212 1|255) rm -rf ${tmpdir} && exit 0 ;;
213 esac
215 # Handle actions
216 action=$(cat $tmp)
217 case "$action" in
218 keyboard) tazkeymap ;;
219 locale) tazlocale ;;
220 wifi-setup) wifi_setup ;;
221 add-user) add_user ;;
222 root-passwd) root_passwd ;;
223 set-date) set_date ;;
224 quit) rm -rf ${tmpdir} && exit 0 ;;
225 esac
226 }
228 #
229 # Handle commands
230 #
232 case "$1" in
233 *_*)
234 # Execute functions
235 $@ ;;
236 *)
237 while true; do
238 main_box
239 done ;;
240 esac
242 # Clean exit
243 #rm -rf ${tmpdir}
244 exit 0