slitaz-tools view tinyutils/sshbox @ rev 435

improve firewall and iptables_rules (thanks gokhlayeh)
author Rohit Joshi <jozee@slitaz.org>
date Fri Mar 12 12:01:54 2010 +0000 (2010-03-12)
parents cf5de149f626
children 0f5e3af5a51d
line source
1 #! /bin/sh
2 #
3 # Gtkdialog box interafce for SSH connexions.
4 #
5 # Manage profile in .prf files store in $HOME/.sshbox
6 # (c) 2009 SliTaz GNU/Linux - GNU gpl v3
7 #
10 # Create config directory and init default var
11 export VERSION=0.2
12 export PROGNAME="SSHBox"
13 export PROG="$0"
14 export CONFIG_DIR=$HOME/.config/sshbox
15 export TEMP=/tmp/$(basename $PROG).out
17 # Create $CONFIG_DIR if it doesn't exist.
18 [ -d $HOME/$CONFIG_DIR/session ] || mkdir -p $CONFIG_DIR/session
20 # Set locale message if message file exist and $LANG is set.
21 # Default: English
22 if [ -f /usr/share/locale/${LANG%%_*}/LC_MESSAGES/sshbox.msg ]; then
23 . /usr/share/locale/${LANG%%_*}/LC_MESSAGES/sshbox.msg
24 fi
26 ##
27 # Function
28 ##
29 add_session(){
30 export GTK_ADD_PROFILE="
31 <window title=\"${TITLE_ADD_PROFILE:-Add new session}\" icon-name=\"sunny\">
32 <vbox>
33 <hbox>
34 <text>
35 <label>\"${LBL_SESSION:-Session}:\"</label>
36 </text>
37 <entry>
38 <variable>SESSION</variable>
39 </entry>
40 </hbox>
41 <hbox>
42 <text>
43 <label>\"${LBL_HOSTNAME:-Hostname}:\"</label>
44 </text>
45 <entry>
46 <variable>SSH_HOST</variable>
47 </entry>
49 </hbox>
50 <hbox>
51 <text>
52 <default>22</default>
53 <label>\"${LBL_PORT-Port}:\"</label>
54 </text>
55 <entry width_chars=\"5\" max-length=\"5\">
56 <default>22</default>
57 <variable>SSH_PORT</variable>
58 </entry>
59 </hbox>
60 <hbox>
61 <text>
62 <label>\"${LBL_USERNAME-Username}:\"</label>
63 </text>
64 <entry>
65 <variable>SSH_USER</variable>
66 </entry>
67 </hbox>
68 <hbox>
69 <button>
70 <label>${BTN_SAVE:-Save}</label>
71 <input file icon=\"gtk-floppy\"></input>
72 <action>$PROG save_session $SESSION</action>
73 <action>refresh:USER_HOST</action>
74 <action type=\"exit\">Exit</action>
75 </button>
76 <button cancel></button>
77 </hbox>
78 </vbox>
79 </window>
80 "
81 gtkdialog --center --program=GTK_ADD_PROFILE
82 }
84 gtk_manage_session(){
86 # Read th config file if exist
87 [ -f "$CONFIG_DIR/session/$SESSION.conf" ] && . "$CONFIG_DIR/session/$SESSION.conf"
89 export GTK_MANAGE_SESSION="
90 <window title=\"${TITLE_GTK_MANAGE:-Session} $SESSION\" icon-name=\"gnome-control-center\">
91 <vbox>
92 <hbox>
93 <text>
94 <label>\"${LBL_HOSTNAME:-Hostname}:\"</label>
95 </text>
96 <entry>
97 <input>\"echo $HOSTNAME\"</input>
98 <variable>SSH_HOST</variable>
99 </entry>
100 </hbox>
101 <hbox>
102 <text>
103 <default>22</default>
104 <label>\"${LBL_PORT-Port}:\"</label>
105 </text>
106 <entry width_chars=\"5\" max-length=\"5\">
107 <input>\"echo $PORT\"</input>
108 <variable>SSH_PORT</variable>
109 </entry>
110 </hbox>
111 <hbox>
112 <text>
113 <label>\"${LBL_USERNAME-Username}:\"</label>
114 </text>
115 <entry>
116 <input>\"echo $USER\"</input>
117 <variable>SSH_USER</variable>
118 </entry>
119 </hbox>
121 <hbox>
122 <button>
123 <label>${BTN_DELETE:-Delete}</label>
124 <input file icon=\"gtk-delete\"></input>
125 <action>$PROG delete_session $SESSION</action>
126 </button>
127 <button>
128 <label>${BTN_SAVE:-Modify}</label>
129 <input file icon=\"gtk-floppy\"></input>
130 <action>$PROG save_session</action>
131 </button>
132 <button>
133 <label>${BTN_CONNECT:-Connect}</label>
134 <input file icon=\"gtk-connect\"></input>
135 <action>$PROG ssh_connect</action>
136 </button>
137 <button cancel></button>
138 </hbox>
139 </vbox>
140 </window>
141 "
142 gtkdialog --center --program=GTK_MANAGE_SESSION
143 }
146 # Display SettingsBox.
147 gtk_settings(){
148 read_settings
149 export GTK_SETTINGS="
150 <window title=\"${TITLE_GTK_SETTINGS:-Properties}\" icon-name=\"gnome-control-center\">
151 <vbox>
152 <hbox>
153 <text use-markup=\"true\">
154 <label>\"${TXT_TERM:-Terminal}: \"</label>
155 </text>
156 <entry>
157 <input>echo $XTERM</input>
158 <variable>XTERM</variable>
159 </entry>
160 </hbox>
161 <hbox>
162 <text use-markup=\"true\">
163 <label>\"${TXT_SSH_CLIENT:-SSH Client}: \"</label>
164 </text>
165 <entry>
166 <input>echo $SSH_CLIENT</input>
167 <variable>SSH_CLIENT</variable>
168 </entry>
169 </hbox>
170 <hbox>
171 <button ok></button>
172 <button cancel></button>
173 </hbox>
174 </vbox>
175 </window>
176 "
177 gtkdialog --center --program=GTK_SETTINGS > /tmp/_out_
179 . /tmp/_out_
181 if [ "$EXIT" == "OK" ]; then
182 save_settings
183 fi
184 }
186 gtk_gen_key(){
187 # Gen private key with dropbearkey
188 #
189 export GTK_GEN_KEY="
190 <window title=\"${TITLE_GTK_MANAGE:-Session} $SESSION\" icon-name=\"gnome-control-center\">
191 <vbox>
192 <hbox>
194 </hbox>
195 <hbox>
196 <button>
197 <label>${BTN_CONNECT:-Connect}</label>
198 <input file icon=\"gtk-connect\"></input>
199 <action>$PROG ssh_connect</action>
200 </button>
201 <button cancel></button>
202 </hbox>
203 </vbox>
204 "
205 }
207 ssh_genkey(){
208 echo "nop"
209 }
211 list_ssh_key(){
213 # dropbearkey -y -f foo_dss | awk '/ssh-(dss|rsa)/ { print $3 "|" $1}'
215 for i in ${HOME}/.ssh/*.priv
216 do
217 [ -f $i ] && echo $(basename $i .priv)
218 done
219 }
221 list_host(){
222 if [ -f $HOME/.ssh/known_hosts ]; then
223 for i in "$(cat $HOME/.ssh/known_hosts)"; do echo "$i"| awk '{print $1}';done
224 fi
225 }
227 delete_host(){
228 cp $HOME/.ssh/known_hosts /tmp/f.tmp && \
229 awk -v host=$KNOWN_HOST 'match($0,host) == 0 {print $0}' /tmp/f.tmp > $HOME/.ssh/known_hosts
230 }
232 quick_add_session(){
233 export GTK_QUICK_ADD="
234 <window title=\"${TITLE_QUICK_ADD:-Enter session name}\" icon-name=\"gtk-dialog-question\">
235 <vbox>
236 <hbox>
237 <text>
238 <label>\"${TXT_SESSION_NAME:-Session}:\"</label>
239 </text>
240 <entry>
241 <variable>SESSION</variable>
242 </entry>
243 </hbox>
244 <hbox>
245 <button>
246 <label>${BTN_SAVE:-Save}</label>
247 <input file icon=\"gtk-apply\"></input>
248 <action type=\"exit\">OK</action>
249 </button>
250 <button cancel></button>
251 </hbox>
252 </vbox>
253 </window>
254 "
255 gtkdialog --center --program=GTK_QUICK_ADD > ${TEMP}
257 if ( grep -q 'EXIT="OK"' $TEMP ); then
258 . $TEMP
259 echo "HOSTNAME=${SSH_HOST}" > ${CONFIG_DIR}/session/${SESSION}.conf
260 echo "USER=${SSH_USER}" >> ${CONFIG_DIR}/session/${SESSION}.conf
261 echo "PORT=${SSH_PORT}" >> ${CONFIG_DIR}/session/${SESSION}.conf
262 fi
263 }
265 edit_session(){
266 echo "nop"
267 }
269 save_session(){
270 echo "HOSTNAME=${SSH_HOST}" > ${CONFIG_DIR}/session/${SESSION}.conf
271 echo "USER=${SSH_USER}" >> ${CONFIG_DIR}/session/${SESSION}.conf
272 echo "PORT=${SSH_PORT}" >> ${CONFIG_DIR}/session/${SESSION}.conf
273 }
275 delete_session(){
276 rm -f ${CONFIG_DIR}/session/${SESSION}.conf
277 }
279 list_sessions(){
281 for i in ${CONFIG_DIR}/session/*.conf
282 do
283 [ -f $i ] && echo "$(basename $i .conf)" | awk '{print"gtk-network|", $0}'
284 done
285 }
287 read_settings(){
288 # Read user settings.
289 # Init default settings if don't exist
290 if [ -f ${CONFIG_DIR}/sshbox.conf ]; then
291 . ${CONFIG_DIR}/sshbox.conf
292 else
293 echo "XTERM=xterm" > ${CONFIG_DIR}/sshbox.conf
294 echo "SSH_CLIENT=ssh" >> ${CONFIG_DIR}/sshbox.conf
295 fi
296 export XTERM SSH_CLIENT
297 }
299 read_last_connection(){
300 #Read last connexion
301 [ -f $CONFIG_DIR/last ] && . $CONFIG_DIR/last
302 }
304 save_settings(){
305 # Save user settings in user.pref.
306 echo "XTERM=${XTERM:-xterm}" > $CONFIG_DIR/sshbox.conf
307 echo "SSH_CLIENT=${SSH_CLIENT:-/usr/bin/ssh}" >> $CONFIG_DIR/sshbox.conf
308 }
310 ssh_connect(){
312 read_settings
314 #TODO: Move this to another place
315 # Save last used profile and start ssh connection
316 echo "HOSTNAME=$SSH_HOST" > $CONFIG_DIR/last
317 echo "USERNAME=$SSH_USER" >> $CONFIG_DIR/last
318 echo "KEY=$SSH_KEY" >> $CONFIG_DIR/last
320 $SSH_OPTIONS=""
322 [ ! -z $SSH_PORT ] && SSH_OPTION="-p $SSH_PORT"
324 # set terminal options
325 if [ "$XTERM" == "sakura" ]; then
326 OPTION="--title $SSH_USER@$SSH_HOST"
327 else
328 OPTION="-T $SSH_USER@$SSH_HOST"
329 fi
331 exec $XTERM $OPTION -e "$SSH_CLIENT $SSH_OPTIONS $SSH_USER@$SSH_HOST" &
332 }
334 gtk_about(){
335 export GTK_ABOUT="
336 <window title=\"${TITLE_ABOUT:-About} de $PROGNAMENAME\" icon-name=\"sunny\">
337 <vbox>
338 <pixmap>
339 <input file stock=\"gtk-network\"></input>
340 </pixmap>
341 <text use-markup=\"true\">
342 <label>\"<b>SSHBox v$VERSION</b>\"</label>
343 </text>
344 <text wrap=\"true\" width-chars=\"40\" use-markup=\"true\">
345 <label>\"
346 Gtkdialog box interface for SSH connections.
348 SliTaz GNU/Linux - GPL v3
349 \"
350 </label>
351 </text>
352 <hbox><button ok></button></hbox>
353 </vbox>
354 </window>
355 "
356 gtkdialog --center --program=GTK_ABOUT
357 }
359 main_dialog(){
361 read_settings
362 read_last_connection
364 export MAIN_DIALOG=" \
365 <window title=\"SSHBox\" icon-name=\"gtk-network\" resizable=^\"false\">
366 <vbox>
367 <hbox homogeneous=\"true\">
368 <text width-chars=\"60\">
369 <label>\"\"</label>
370 </text>
371 </hbox>
372 <notebook labels=\"${TITLE_MAIN:-Main}|${TITLE_SESSION:-Sessions}|${TITLE_HOST:-Host key}\">
374 <vbox>
375 <hbox spacing=\"43\">
376 <text use-markup=\"true\" >
377 <label>\"${TXT_HOST:-Host}:\"</label>
378 </text>
379 <entry>
380 <default>\"$HOSTNAME\"</default>
381 <variable>SSH_HOST</variable>
382 </entry>
383 </hbox>
384 <hbox>
385 <text use-markup=\"true\">
386 <label>\"${TXT_USER:-Username}:\"</label>
387 </text>
388 <entry>
389 <default>\"$USERNAME\"</default>
390 <variable>SSH_USER</variable>
391 </entry>
392 </hbox>
393 <hbox spacing=\"20\">
394 <text use-markup=\"true\">
395 <label>\"${TXT_KEY:-SSH Key}:\"</label>
396 </text>
397 <entry>
398 <default>\"\"</default>
399 <variable>SSH_KEY</variable>
400 </entry>
401 </hbox>
402 <hbox>
403 <button>
404 <label>${BTN_ADD:-Save session}</label>
405 <input file icon=\"gtk-save\"></input>
406 <action>$PROG quick_add_session</action>
407 <action>refresh:SESSION</action>
408 </button>
409 <button>
410 <label>${BTN_CONNECT:-Connect}</label>
411 <input file icon=\"gtk-connect\"></input>
412 <action>$PROG ssh_connect</action>
413 </button>
414 </hbox>
415 </vbox>
417 <vbox>
418 <hbox>
419 <tree headers_visible=\"false\" exported_column=\"0\">
420 <width>200</width><height>150</height>
421 <label>\"\"</label>
422 <variable>SESSION</variable>
423 <input stock_column=\"0\">$PROG list_sessions</input>
424 <action>$PROG gtk_manage_session $SESSION</action>
425 <action>refresh:SESSION</action>
426 </tree>
427 </hbox>
428 <hbox>
429 <text width-chars=\"45\">
430 <label>
431 \"
432 Double click on session name to manage SSH session.
433 \"
434 </label>
435 </text>
436 <button>
437 <label>${BTN_ADD_SESSION:-Add session}</label>
438 <input file icon=\"gtk-add\"></input>
439 <action>$PROG add_session</action>
440 <action>refresh:SESSION</action>
441 </button>
442 </hbox>
443 </vbox>
445 <vbox>
446 <hbox>
447 <tree headers_visible=\"false\" exported_column=\"0\">
448 <width>200</width><height>150</height>
449 <label>\"\"</label>
450 <variable>KNOWN_HOST</variable>
451 <input>$PROG list_host</input>
452 <action>$PROG delete_host $KNOWN_HOST</action>
453 <action>refresh:KNOWN_HOST</action>
454 </tree>
455 </hbox>
456 <hbox>
457 <text width-chars=\"60\">
458 <label>
459 \"Double click on host to remove it.\"
460 </label>
461 </text>
462 </hbox>
463 </vbox>
464 </notebook>
465 <hbox>
466 <button>
467 <label>${BTN_ABOUT:-About}</label>
468 <input file icon=\"gtk-about\"></input>
469 <action>$PROG gtk_about $PROGNAME</action>
470 </button>
471 <button>
472 <label>${BTN_SETTINGS:-Settings}</label>
473 <input file icon=\"gtk-preferences\"></input>
474 <action>$0 gtk_settings</action>
475 </button>
476 <button>
477 <label>${BTN_Exit:-Exit}</label>
478 <input file icon=\"exit\"></input>
479 <action type=\"exit\">Exit</action>
480 </button>
481 </hbox>
482 </vbox>
483 </window>
484 "
485 gtkdialog --center --program=MAIN_DIALOG
486 }
490 case $1 in
491 *_*)
492 $1 ;;
493 *)
494 main_dialog ;;
495 esac