Magellan Linux

Contents of /alx-src/branches/alxconf-060/functions/config_sessions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5013 - (show annotations) (download) (as text)
Wed Aug 7 12:29:09 2013 UTC (10 years, 8 months ago) by niro
File MIME type: application/x-sh
File size: 16449 byte(s)
-introduce wfica-launcher to prevent massive citrix session launches via double-click attacks
1 # $Id$
2 # configures ica-sessions on the host via mysql db settings
3
4 fix_whitespaces()
5 {
6 local var="$@"
7 echo "${var//\ /_}"
8 }
9
10 # helper function to create citrix session files
11 generate_ica_session_file()
12 {
13 local i
14 local num
15 local server
16 local ses_session
17 local ses_filename
18 local ses_username
19 local ses_domain
20 local ses_password
21 local ses_browseradrs
22 local ses_colors
23 local CONFIG
24
25 # very basic getops
26 for i in $*
27 do
28 case $1 in
29 --session) shift; ses_session="$1" ;;
30 --filename) shift; ses_filename="$1" ;;
31 --username) shift; ses_username="$1" ;;
32 --password) shift; ses_password="$1" ;;
33 --domain) shift; ses_domain="$1" ;;
34 --server) shift; ses_browseradrs="$1" ;;
35 --colordepth) shift; ses_colors="$1" ;;
36 esac
37 shift
38 done
39
40 # abort if session, filename or server not given
41 [[ -z ${ses_session} ]] && return 1
42 [[ -z ${ses_filename} ]] && return 1
43 [[ -z ${ses_browseradrs} ]] && return 1
44
45 # write session files
46 CONFIG="${ALX_ICA_SESSIONS}/${ses_filename}"
47 clearconfig
48
49 addconfig '[WFClient]'
50 addconfig 'Version=2'
51
52 # use ';' as ifs
53 declare -i i=0
54 for server in ${ses_browseradrs//;/ }
55 do
56 (( i++ ))
57 num="${i}"
58 # support newer ica-clients:
59 # the first address must be named TcpBrowserAddress, but not TcpBrowserAddress1 !!
60 [[ ${i} -eq 1 ]] && num=""
61 addconfig "TcpBrowserAddress${num}=${server}"
62 done
63
64 addconfig 'ScreenPercent=0'
65 addconfig '[ApplicationServers]'
66 addconfig "${ses_session}="
67 addconfig "[${ses_session}]"
68 addconfig "Address=${ses_session}"
69 addconfig "InitialProgram=#${ses_session}"
70
71 # convert to ica session file values
72 case ${ses_colors} in
73 24|32) ses_colors="8";;
74 16) ses_colors="4";;
75 8) ses_colors="2";;
76 *) ses_colors="4";; # default to 16bit
77 esac
78 addconfig "DesiredColor=${ses_colors}"
79 addconfig 'TransportDriver=TCP/IP'
80 addconfig 'WinStationDriver=ICA 3.0'
81 addconfig "ClearPassword=${ses_password}"
82 addconfig "Username=${ses_username}"
83 addconfig "Domain=${ses_domain}"
84 addconfig 'UseFullScreen=Yes'
85 addconfig 'NoWindowManager=True'
86 }
87
88 # generates a sh file to start programs
89 generate_program_sh()
90 {
91 local dest
92 local name
93 local exec
94 local param
95 local workdir
96 local CONFIG
97
98 # very basic getops
99 for i in $*
100 do
101 case $1 in
102 --name|-n) shift; name="$1" ;;
103 --exec|-x) shift; exec="$1" ;;
104 --param|-p) shift; param="$1" ;;
105 --dest|-d) shift; dest="$1" ;;
106 --workdir|-w) shift; workdir="$1" ;;
107 esac
108 shift
109 done
110
111 # abort if name, dest or exec not given
112 [[ -z ${name} ]] && return 1
113 [[ -z ${exec} ]] && return 1
114 [[ -z ${dest} ]] && return 1
115
116 CONFIG="${dest}"
117 addconfig "#!/bin/sh"
118 [ -n "${workdir}" ] && addconfig "cd ${workdir}"
119 addconfig "exec ${exec} ${param}"
120
121 chmod 0755 "${dest}"
122 }
123
124 # helper functions for generate all desktop icons
125 generate_icon()
126 {
127 local name
128 local icon
129 local command
130 local dest
131 local yres
132 local xres
133 local iwidth
134 local iheight
135 local deficon
136 local CONFIG
137 local utility
138
139 # very basic getops
140 for i in $*
141 do
142 case $1 in
143 --name|-n) shift; name="$1" ;;
144 --command|-c) shift; command="$1" ;;
145 --icon|-i) shift; icon="$1" ;;
146 --dest|-d) shift; dest="$1" ;;
147 --xres|-x) shift; xres="$1" ;;
148 --yres|-y) shift; yres="$1" ;;
149 --icon-width|-w) shift; iwidth="$1" ;;
150 --icon-height|-h) shift; iheight="$1" ;;
151 --default-icon) shift; deficon="$1" ;;
152 esac
153 shift
154 done
155
156 # some sanity checks:
157
158 # abort if name or command not given
159 [[ -z ${name} ]] && return 1
160 [[ -z ${command} ]] && return 1
161
162 [[ -z ${dest} ]] && dest="${ALX_UNPRIV_HOME}/.idesktop/${name}.lnk"
163
164 # use some defaults for icon, dest, {x,y}res
165 [[ -z ${xres} ]] && xres=50
166 [[ -z ${yres} ]] && xres=50
167 if [[ -z ${icon} ]] || [ ! -f ${icon} ]
168 then
169 # if no default icon is given use default.png
170 [[ -z ${deficon} ]] && deficon="default.png"
171 icon="${ALX_SESSIONS_ICONS}/${deficon}"
172 fi
173
174 CONFIG="${dest}"
175 clearconfig
176
177 addconfig 'table Icon'
178 addconfig " Caption: ${name}"
179 addconfig " Command: ${command}"
180 addconfig " Icon: ${icon}"
181 addconfig " X: ${xres}"
182 addconfig " Y: ${yres}"
183
184 # add these only if not zero
185 if [[ ! -z ${iwidth} ]] && [[ ! -z ${iheight} ]]
186 then
187 addconfig " Width: ${iwidth}"
188 addconfig " Height: ${iheight}"
189 fi
190
191 addconfig 'end'
192 }
193
194 generate_all_desktop_icons()
195 {
196 local session_list="$1"
197 local other_menuitem_list="$2"
198 local plugin_list="$3"
199 local res
200 local xres
201 local yres
202 local x
203 local y
204 local i
205 local name
206 local progsh_path
207 local utility
208 local dest
209 local rc
210
211 dest="${ALX_UNPRIV_HOME}/.idesktop"
212 rc="${ALX_UNPRIV_HOME}/.ideskrc"
213
214 # progsh path
215 progsh_path="${ALX_UNPRIV_HOME}/.alxprogs"
216
217 # get the resolution
218 res=$(mysqldo "select resolution from cfg_graphic where serial='${ALX_SERIAL}'")
219
220 # split res to x & y
221 xres="${res%x*}"
222 yres="${res#*x}"
223
224 # top left edge of the icon is given in config file
225 # remove a little bit to simulate the bottom-right edge
226 xres="$(( ${xres} - 120 ))"
227 yres="$(( ${yres} - 80 ))"
228
229 # clean desktop icon location
230 [ -d ${dest} ] && rm -rf ${dest}
231 [ -f ${rc} ] && rm -f ${rc}
232 install -d ${dest}
233
234 # default settings
235 declare -i x=50
236 declare -i y=50
237
238 # ica icons
239 for i in ${session_list}
240 do
241 # abort if empty
242 [[ -z ${i} ]] && continue
243
244 # get database information
245 evaluate_table cfg_sessions "where serial='${ALX_SERIAL}' and id='${i}'"
246
247 # new line if x > xres
248 if [ ${x} -ge ${xres} ]
249 then
250 x=50
251 y=$((${y} + 80))
252 fi
253
254 # new row if y > yres
255 if [ ${y} -ge ${yres} ]
256 then
257 x=$((${x} + 120))
258 y=50
259
260 # re-check x
261 [ ${x} -ge ${xres} ] && x=50
262 fi
263
264 generate_icon \
265 --name "${cfg_sessions_session}" \
266 --command "wfica-launcher ${ALX_ICA_SESSIONS}/$(fix_whitespaces ${cfg_sessions_filename})" \
267 --icon "${ALX_SESSIONS_ICONS}/$(fix_whitespaces ${cfg_sessions_session}).png" \
268 --dest "${dest}/$(fix_whitespaces ${cfg_sessions_session}).lnk" \
269 --xres "${x}" \
270 --yres "${y}"
271
272 y=$((${y} + 80))
273 done
274
275 for i in ${other_menuitem_list}
276 do
277 # abort if empty
278 [[ -z ${i} ]] && continue
279
280 # get database information
281 evaluate_table cfg_other_menuitems "where serial='${ALX_SERIAL}' and id='${i}'"
282
283 # new line if x > xres
284 if [ ${x} -ge ${xres} ]
285 then
286 x=50
287 y=$((${y} + 80))
288 fi
289
290 # new row if y > yres
291 if [ ${y} -ge ${yres} ]
292 then
293 x=$((${x} + 120))
294 y=50
295
296 # re-check x
297 [ ${x} -ge ${xres} ] && x=50
298 fi
299
300 generate_icon \
301 --name "${cfg_other_menuitems_name}" \
302 --command "${progsh_path}/$(fix_whitespaces ${cfg_other_menuitems_name})" \
303 --icon "${ALX_SESSIONS_ICONS}/$(fix_whitespaces ${cfg_other_menuitems_name}).png" \
304 --dest "${dest}/$(fix_whitespaces ${cfg_other_menuitems_name}).lnk" \
305 --xres "${x}" \
306 --yres "${y}" \
307 --default-icon "default_item.png"
308
309 y=$((${y} + 80))
310 done
311
312 for i in ${plugin_list}
313 do
314 # abort if empty
315 [[ -z ${i} ]] && continue
316
317 evaluate_table cfg_plugins "where serial='${ALX_SERIAL}' and id='${i}'"
318 if [[ -x ${ALX_PLUGINS}/${cfg_plugins_plugin}/plugin.sh ]]
319 then
320 eval $(${ALX_PLUGINS}/${cfg_plugins_plugin}/plugin.sh menuitem)
321
322 # abort if name or exec is empty
323 [[ -z ${PLUGIN_MENUITEM_NAME} ]] && continue
324 [[ -z ${PLUGIN_MENUITEM_EXEC} ]] && continue
325
326 # new line if x > xres
327 if [ ${x} -ge ${xres} ]
328 then
329 x=50
330 y=$((${y} + 80))
331 fi
332
333 # new row if y > yres
334 if [ ${y} -ge ${yres} ]
335 then
336 x=$((${x} + 120))
337 y=50
338
339 # re-check x
340 [ ${x} -ge ${xres} ] && x=50
341 fi
342
343 generate_icon \
344 --name "${PLUGIN_MENUITEM_NAME}" \
345 --command "${progsh_path}/$(fix_whitespaces ${PLUGIN_MENUITEM_NAME})" \
346 --icon "${ALX_SESSIONS_ICONS}/$(fix_whitespaces ${PLUGIN_MENUITEM_NAME}).png" \
347 --dest "${dest}/$(fix_whitespaces ${PLUGIN_MENUITEM_NAME}).lnk" \
348 --xres "${x}" \
349 --yres "${y}" \
350 --default-icon "default_item.png"
351
352 y=$((${y} + 80))
353 fi
354
355 # unset all variables
356 unset PLUGIN_MENUITEM_NAME
357 unset PLUGIN_MENUITEM_EXEC
358 unset PLUGIN_MENUITEM_PARAM
359 unset PLUGIN_MENUITEM_WORKDIR
360 unset PLUGIN_MENUITEM_ICON
361 done
362
363 # add shutdown, reboot icons
364 for i in shutdown reboot
365 do
366 # new line if x > xres
367 if [ ${x} -ge ${xres} ]
368 then
369 x=50
370 y=$((${y} + 80))
371 fi
372
373 # new row if y > yres
374 if [ ${y} -ge ${yres} ]
375 then
376 x=$((${x} + 120))
377 y=50
378
379 # re-check x
380 [ ${x} -ge ${xres} ] && x=50
381 fi
382
383 case ${i} in
384 shutdown) name="Herunterfahren" ;;
385 reboot) name="Neustarten" ;;
386 esac
387
388 generate_icon \
389 --name "${name}" \
390 --command "/usr/lib/alxconfig-ng/bin/user_${i}.sh" \
391 --icon "${ALX_SESSIONS_ICONS}/${i}.png" \
392 --dest "${dest}/${i}.lnk" \
393 --xres "${x}" \
394 --yres "${y}" \
395 --icon-width "40" \
396 --icon-height "40"
397
398 y=$((${y} + 80))
399 done
400
401 # last but not least gen a icon with some sys informations
402 local sysinfo
403 local hostname
404 local osversion
405
406 osversion="$(< /etc/mageversion)"
407 hostname=$(mysqldo "select hostname from cfg_network where serial='${ALX_SERIAL}'")
408 sysinfo="Hostname: ${hostname} Serial: #${ALX_SERIAL} OS: alx-${osversion} Kernel: $(uname -r)"
409
410 # now get the right position:
411 # restore orig values of xres
412 xres="$(( ${xres} + 120 ))"
413 # default y pos (full yres -22 = cur yres + 58 !)
414 yres="$(( ${yres} + 58 ))"
415 # middle of the screen
416 # (no txt - length required, xtdesk manage that itself)
417 xres="$(( ${xres} / 2))"
418
419 generate_icon \
420 --name "${sysinfo}" \
421 --command "exit 0" \
422 --icon "${ALX_SESSIONS_ICONS}/sysinfo.png" \
423 --dest "${dest}/sysinfo.lnk" \
424 --xres "${xres}" \
425 --yres "${yres}" \
426 --icon-width "1" \
427 --icon-height "1"
428 }
429
430 config_sessions()
431 {
432 local i
433 local all_ses_ids
434 local all_other_ids
435 local CONFIG
436 local screensaver_passwd_cmd
437 local fbinit
438 local fbkeys
439 local progsh_path
440
441 progsh_path="${ALX_UNPRIV_HOME}/.alxprogs"
442
443 # get all session ids from database
444 all_ses_ids=$(mysqldo "select id from cfg_sessions where serial='${ALX_SERIAL}'")
445 # get all other_menuitem ids from database
446 all_other_ids=$(mysqldo "select id from cfg_other_menuitems where serial='${ALX_SERIAL}'")
447 # get all plugin ids from database
448 all_plugin_ids=$(mysqldo "select id from cfg_plugins where serial='${ALX_SERIAL}'")
449
450 # get screensaver settings
451 evaluate_table cfg_screensaver
452 # get autostart settings
453 evaluate_table cfg_autostart
454 # get current color depth
455 evaluate_table cfg_graphic
456
457 # now setup fluxbox for user station
458
459 # create a fresh fluxbox directory
460 [ -d ${ALX_UNPRIV_HOME}/.fluxbox ] && rm -rf ${ALX_UNPRIV_HOME}/.fluxbox
461 install -d ${ALX_UNPRIV_HOME}/.fluxbox
462
463 # now generate fluxbox config files
464
465 # fluxbox main config
466 if [ -f ${ALX_SKELETONS}/fluxbox/init ]
467 then
468 fbinit="${ALX_SKELETONS}/fluxbox/init"
469 else
470 fbinit="/usr/share/fluxbox/init"
471 fi
472 cat ${fbinit} > ${ALX_UNPRIV_HOME}/.fluxbox/init
473
474 # fluxbox autostart
475 CONFIG="${ALX_UNPRIV_HOME}/.fluxbox/apps"
476 # do not show decorations on messages generated with xmessage
477 if [ -x /usr/bin/xmessage ]
478 then
479 addconfig '[app] (xmessage)'
480 addconfig ' [Deco] {NONE}'
481 addconfig '[end]'
482 fi
483 # add icon utility
484 [ -x /usr/bin/idesk ] && addconfig '[startup] {nohup idesk > /dev/null &}'
485 # add numlock utility
486 [ -x /usr/bin/numlockx ] && addconfig '[startup] {nohup numlockx on &}'
487
488 # add screensaver
489 if [[ ! -z ${cfg_screensaver_screensaver} ]] && [[ ! -z ${cfg_screensaver_screensaver_timeout} ]]
490 then
491 if [[ -z ${cfg_screensaver_password} ]] || [[ ${cfg_screensaver_password} = NULL ]]
492 then
493 screensaver_passwd_cmd="-nolock"
494 else
495 screensaver_passwd_cmd="-cpasswd $(cryptpw -m des ${cfg_screensaver_password})"
496 fi
497
498 addconfig "[startup] {nohup xautolock -time ${cfg_screensaver_timeout} -locker 'xlock -mode ${cfg_screensaver_screensaver} ${screensaver_passwd_cmd}' > /dev/null &}"
499 fi
500
501 # add plugins autostart
502 for i in ${all_plugin_ids}
503 do
504 evaluate_table cfg_plugins "where serial='${ALX_SERIAL}' and id='${i}'"
505 if [[ -x ${ALX_PLUGINS}/${cfg_plugins_plugin}/plugin.sh ]]
506 then
507 eval $(${ALX_PLUGINS}/${cfg_plugins_plugin}/plugin.sh autostart)
508
509 if [[ ${PLUGIN_AUTOSTART} = 1 ]]
510 then
511 addconfig "[startup] {${progsh_path}/$(fix_whitespaces ${PLUGIN_MENUITEM_NAME}) &}"
512 fi
513 # unset all variables
514 unset PLUGIN_AUTOSTART
515 unset PLUGIN_MENUITEM_NAME
516 fi
517 done
518
519 # add autostart session
520 if [[ ! -z ${cfg_autostart_session} ]]
521 then
522 # sleep one second to wait until busybox is fully initialized and the screen is really centered
523 addconfig "[startup] {sleep 1 && wfica-launcher ${ALX_ICA_SESSIONS}/$(fix_whitespaces ${cfg_autostart_session}) &}"
524 fi
525
526 # fluxbox hotkeys
527 if [ -f ${ALX_SKELETONS}/fluxbox/keys ]
528 then
529 fbkeys="${ALX_SKELETONS}/fluxbox/keys"
530 else
531 fbkeys="/usr/share/fluxbox/keys"
532 fi
533 cat ${fbkeys} > ${ALX_UNPRIV_HOME}/.fluxbox/keys
534
535 # generate a fluxbox menu
536 CONFIG="${ALX_UNPRIV_HOME}/.fluxbox/menu"
537 clearconfig
538
539 # fluxbox menu header
540 cat ${ALX_SKELETONS}/fluxbox/menu.header >> ${ALX_UNPRIV_HOME}/.fluxbox/menu
541 # now fix it with proper messages :P
542 local ver="$(< /etc/mageversion)"
543 sed -i "s:@CHANGEME@:alx-${ver} #${ALX_SERIAL}:g" ${ALX_UNPRIV_HOME}/.fluxbox/menu
544 # add a newline (maybe there is no crlf in the header)
545 addconfig
546
547 # first generate session files
548 for i in ${all_ses_ids}
549 do
550 evaluate_table cfg_sessions "where serial='${ALX_SERIAL}' and id='${i}'"
551 generate_ica_session_file \
552 --session "${cfg_sessions_session}" \
553 --filename "$(fix_whitespaces ${cfg_sessions_filename})" \
554 --username "${cfg_sessions_username}" \
555 --password "${cfg_sessions_password}" \
556 --domain "${cfg_sessions_domain}" \
557 --server "${cfg_sessions_browseradrs}" \
558 --colordepth "${cfg_graphic_depth}"
559
560 # fluxbox menusession
561 addconfig "[exec] (${cfg_sessions_session}) {wfica-launcher ${ALX_ICA_SESSIONS}/$(fix_whitespaces ${cfg_sessions_filename})}"
562 done
563
564 # delete all progs
565 [ -d ${progsh_path} ] && rm -rf ${progsh_path}
566 install -d ${progsh_path}
567
568 # add other menuitems
569 for i in ${all_other_ids}
570 do
571 evaluate_table cfg_other_menuitems "where serial='${ALX_SERIAL}' and id='${i}'"
572
573 # now echo config line for fluxbox-menu
574 # make it "configureable" :p
575 if [[ -n ${cfg_other_menuitems_workdir} ]]
576 then
577 workdir="--workdir ${cfg_other_menuitems_workdir}"
578 fi
579 if [[ -n ${cfg_other_menuitems_icon} ]]
580 then
581 cfg_other_menuitems_icon="<${cfg_other_menuitems_icon}>"
582 fi
583
584 # gen prog startup wrapper
585 generate_program_sh \
586 --name "${cfg_other_menuitems_name}" \
587 --exec "${cfg_other_menuitems_exec}" \
588 --param "${cfg_other_menuitems_param}" \
589 --dest "${progsh_path}/$(fix_whitespaces ${cfg_other_menuitems_name})" \
590 "${workdir}"
591
592 addconfig "[exec] (${cfg_other_menuitems_name}) {${progsh_path}/$(fix_whitespaces ${cfg_other_menuitems_name})} ${cfg_other_menuitems_icon}"
593 done
594
595 # add plugins
596 for i in ${all_plugin_ids}
597 do
598 evaluate_table cfg_plugins "where serial='${ALX_SERIAL}' and id='${i}'"
599 if [[ -x ${ALX_PLUGINS}/${cfg_plugins_plugin}/plugin.sh ]]
600 then
601 eval $(${ALX_PLUGINS}/${cfg_plugins_plugin}/plugin.sh menuitem)
602
603 # abort if name or exec is empty
604 [[ -z ${PLUGIN_MENUITEM_NAME} ]] && continue
605 [[ -z ${PLUGIN_MENUITEM_EXEC} ]] && continue
606
607 # now echo config line for fluxbox-menu
608 # make it "configureable" :p
609 if [[ -n ${PLUGIN_MENUITEM_WORKDIR} ]]
610 then
611 workdir="--workdir ${PLUGIN_MENUITEM_WORKDIR}"
612 fi
613 if [[ -n ${PLUGIN_MENUITEM_ICON} ]]
614 then
615 PLUGIN_MENUITEM_ICON="<${PLUGIN_MENUITEM_ICON}>"
616 fi
617
618 # gen prog startup wrapper
619 generate_program_sh \
620 --name "${PLUGIN_MENUITEM_NAME}" \
621 --exec "${PLUGIN_MENUITEM_EXEC}" \
622 --param "${PLUGIN_MENUITEM_PARAM}" \
623 --dest "${progsh_path}/$(fix_whitespaces ${PLUGIN_MENUITEM_NAME})" \
624 "${workdir}"
625
626 addconfig "[exec] (${PLUGIN_MENUITEM_NAME}) {${progsh_path}/$(fix_whitespaces ${PLUGIN_MENUITEM_NAME})} ${PLUGIN_MENUITEM_ICON}"
627 fi
628
629 # unset all variables
630 unset PLUGIN_MENUITEM_NAME
631 unset PLUGIN_MENUITEM_EXEC
632 unset PLUGIN_MENUITEM_PARAM
633 unset PLUGIN_MENUITEM_WORKDIR
634 unset PLUGIN_MENUITEM_ICON
635 done
636
637 # fluxbox menu footer
638 cat ${ALX_SKELETONS}/fluxbox/menu.footer >> ${ALX_UNPRIV_HOME}/.fluxbox/menu
639 # add a newline (maybe there is no crlf in the footer)
640 addconfig
641
642 # now it's a good time to generate *all* icons :)
643 generate_all_desktop_icons "${all_ses_ids}" "${all_other_ids}" "${all_plugin_ids}"
644
645 # fix permissions
646 chown ${ALX_UNPRIV_USER}:${ALX_UNPRIV_GROUP} ${ALX_UNPRIV_HOME}
647 chown ${ALX_UNPRIV_USER}:${ALX_UNPRIV_GROUP} ${ALX_UNPRIV_HOME}/.fluxbox
648 chmod 0755 ${ALX_UNPRIV_HOME}/.fluxbox
649 chmod 0755 ${ALX_UNPRIV_HOME}/.idesktop
650 }