forked from dimbor-ru/freenx-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nxnode
executable file
·2434 lines (2264 loc) · 89.5 KB
/
nxnode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Free implementation of nxserver components
#
# nxnode does accept (for now):
#
# --startsession
# --terminate
# --smbmount
#
# Copyright (c) 2004 by Fabian Franz.
# (c) 2008-19 by Dmitry Borisov <[email protected]>
#
# License: GNU GPL, version 2
#
# SVN: $Id: nxnode 613 2008-09-01 20:42:31Z fabianx $
#
# 21.06.2004: - Full reconnection support
# Read the config file
. $(PATH=$(cd $(dirname $0) && pwd):$PATH which nxloadconfig) --userconf
#JJK: Added following 'if' stanza as a kluge since the following variables
#JJK: need to be set in cmd_node_smbmount node_umount_smb
#JJK: but they are currently set only in startsession which is called
#JJK: separately from nxserver via ssh so environment variables
#JJK: aren't preserved.
if [[ "$SAMBA_MOUNT_SHARE_PROTOCOL" = "cifs" || \
( "$SAMBA_MOUNT_SHARE_PROTOCOL" = "both" && \
`which "$COMMAND_SMBMOUNT_CIFS"` && `which "$COMMAND_SMBUMOUNT_CIFS"` ) \
]] > /dev/null 2>&1; then
COMMAND_SMBMOUNT=$COMMAND_SMBMOUNT_CIFS
COMMAND_SMBUMOUNT=$COMMAND_SMBUMOUNT_CIFS
SAMBA_MOUNT_SHARE_PROTOCOL="cifs"
fi
#
# -----------------------------------------------------------------------------
# Startup of nxnode
# -----------------------------------------------------------------------------
#
DELIM="NX>"
[ -n "$2" ] && DELIM="NX-$2>"
echo "$DELIM 1000 NXNODE - Version $NX_VERSION $NX_LICENSE"
if [ "$1" != "--check" -a "$1" != "--setkey" -a "$1" != "--agent" \
-a "$1" != "--slave" ]; then
read CMDLINE; CMDLINE="a=b&$CMDLINE"
fi
#
# -----------------------------------------------------------------------------
# Various helper functions
# -----------------------------------------------------------------------------
#
stringinstring() { case "$2" in *$1*) return 0 ;; esac; return 1; }
getparam() {
stringinstring "&$1=" "$CMDLINE" || return 1
echo "$CMDLINE" | tr "&" "\n" | egrep "^"$1"=" | \
awk -F= '{ VAL=$2 } END { print VAL }' | sed 's|%24|$|'
return 0
}
find_app() { set -- $*; which $1 2>/dev/null; }
getparam_sessionid() {
sessionid=$(getparam sessionid)
[ -n "$sessionid" ] || sessionid=$(getparam session_id)
if [ -z "$sessionid" ]; then
echo "NX> 500 Error: Fatal - Missing parameter session id." 1>&2
exit 1
fi
echo $sessionid
}
echo_secure() {
if [ "$NX_LOG_SECURE" = "0" ]; then echo "$@ "
else
echo "$@ " | sed -e 's/--cookie="[^"]*"/--cookie="******"/g' \
-e 's/\(--.*password=\)"[^"]*"/\1"******"/g'\
-e 's/password=[^&]*/password=******/g;'
fi
}
nxlog() {
[ "$NX_LOG_LEVEL" -lt 6 ] && return 0
echo_secure "$(date "+%d.%m %X"): $@" >> "$NX_USER_LOG_FILE"
}
if [ "$USER" = "nx" ]; then NX_USER_LOG_FILE="/var/log/nx/nxnode.log"
else
NX_USER_LOG_FILE="$USER_FAKE_HOME/.nx/nxnode.log"
mkdir -p "$USER_FAKE_HOME/.nx"
fi
if [ -n "$CMDLINE" ]; then
if [ "$USER" != "nx" ]; then
ssid4log=$(getparam uniqueid)
[ -z "$ssid4log" ] && ssid4log=$(getparam session_id)
[ -z "$ssid4log" ] && ssid4log=$(getparam sess_id)
[ -z "$ssid4log" ] && ssid4log=$(getparam sessionid)
ssid4log=$(echo "$ssid4log" | awk 'BEGIN {FS="-"} {i=NF; print $i}')
[ -n "$ssid4log" ] && \
NX_USER_LOG_FILE="$USER_FAKE_HOME/.nx/nxnode-$ssid4log.log"
fi
nxlog "$0 ($$): run nxnode with CMDLINE \"$CMDLINE\""
else
nxlog "$0 ($$): run nxnode with \"$@\""
fi
#
# -----------------------------------------------------------------------------
# Node functions module
# -----------------------------------------------------------------------------
#
norm_param() {
# arg: string
# dimbor: normalize string from hex (thnx 2Civil at unixforum.org) and convert
# charset if needed
#nxlog "$FUNCNAME ($$): starting with args \"$@\""
res=$(echo -e `echo $1 | sed -e 's/+/ /g;s/%/\\\\x/g;'`)
#nxlog "$FUNCNAME ($$): after normalize res == \"$res\""
#nxlog "$FUNCNAME ($$): WIN_CP_CONVERT_CHAIN== \"$WIN_CP_CONVERT_CHAIN\""
which iconv 1>/dev/null 2>&1; RC=$?
[ -n "$WIN_CP_CONVERT_CHAIN" -a $RC -ne 0 ] && {
nxlog "$FUNCNAME ($$): iconv not found, codepages converting is disabled."
echo $res; return
}
for cp_pair in $WIN_CP_CONVERT_CHAIN ; do
cp_from=$(echo "$cp_pair" | cut -d\> -f1)
[ -n "$cp_from" ] || cp_from="UTF-8"
cp_to=$(echo "$cp_pair" | cut -d\> -f2)
[ -n "$cp_to" ] || cp_to="UTF-8"
res=$(echo "$res" | iconv -f $cp_from -t $cp_to)
#nxlog "$FUNCNAME ($$): converting $cp_from > $cp_to == \"$res\""
done
#nxlog "$FUNCNAME ($$): return res === \"$res\""
echo $res
}
port2port_val() {
# arg: string with port description
# returns numerical port value
# dimbor: nxcomp from Arctica project returns supplemented description
# of (listening) port. It's string like "tcp:localhost:6001". While we only
# need a numeric value.
port=$(basename $(echo $1 | sed 's/:/\//g') 2>/dev/null)
echo $port
}
# dimbor: functions to control user's shares and printers in mode "per user"
# instead of "per session".
get_mport2sessdir() {
# args: session dir type="smb"|"ipp""
# ret: cups listening port or samba mount port
type=$2
smbport=$(cat $1/scripts/mport 2>/dev/null) # check smb mount port
cport=$(cat $1/session 2>/dev/null | grep "Listening to CUPS" \
| tail -n1 | cut -d\' -f2)
mport=""
if [ "$type" != "smb" -a -n "$cport" ]; then mport=$cport; type="ipp";
elif [ "$type" != "ipp" -a -n "$smbport" ]; then
mport=$smbport; type="smb";
fi
mport=$(echo "$mport" | awk -F: '{print $NF}')
nxlog "$FUNCNAME ($$): sessdir \"$1\"; mport == $mport \"$type\""
echo $mport
}
get_sessdir4res() {
# args: port - session's smb/ipp mount port OR filename of printer/share
# (flag-file) fl_current - if set then includes in search current session-dir
# if share is nx-controlled and nxagent from his session are leave,
# then returns name of session dir (except dir of own sesson)
# functuion used with $ENABLE_SHARE_MULTIMOUNT=1 or
# $ENABLE_CUPS_SERVER_MODE=1 modes only
nxlog "$FUNCNAME ($$): starting with args \"$@\"; sess_id == \"$sess_id\""
[ "$1" -gt 0 2>/dev/null ] && digarg=1 || digarg=0
AGENT_PIDS=$(ps -wo pid=,cmd= -C "nxagent" | awk '{print $1}')
for sessdir in $USER_FAKE_HOME/.nx/C-* ; do
[ -z "$2" -a "$sessdir" == "$USER_FAKE_HOME/.nx/C-$sess_id" ] && \
continue
NODE_AGENT_PID=$(cat "$sessdir/pids/agent" 2>/dev/null)
[ -z "$NODE_AGENT_PID" ] && continue
[ -z "$(echo "$AGENT_PIDS" | grep $NODE_AGENT_PID)" ] && continue
#nxlog "$FUNCNAME ($$): browse sessdir \"$sessdir\" for filename = \"$sessdir/scripts/$1\", digarg=$digarg"
if [ "$digarg" = "1" ]; then
curport=$(get_mport2sessdir "$sessdir")
[ "$curport" = "$1" ] && {
nxlog "$FUNCNAME ($$): found sessdir $sessdir for mport = $1"
echo "$sessdir"; return;
}
else
[ -f "$sessdir/scripts/$1" ] && {
nxlog "$FUNCNAME ($$): found sessdir $sessdir for filename = $1"
echo "$sessdir"; return;
}
fi
done
nxlog "$FUNCNAME ($$): target sessdir not found"
}
check_remote_printer() {
# args: type - "smb" or "ipp"
# short sharename/printername
# port - nx controlled mount port
# name - user name
# pass - password (optional)
# if shared printer accessible, returns 1, else - 0
nxlog "$FUNCNAME ($$): starting with args \"$@\""
if [ "$1" = "ipp" ]; then # checking ipp printer port only
res=0
for (( i=10; $i; --i )); do
nxlog "$FUNCNAME ($$): wait for ipp-port 127.0.0.1:$3 - $((10-i))"
$COMMAND_NETCAT -znvw 1 127.0.0.1 $3 2> /dev/null > /dev/null
[ $? -eq 0 ] && { res=1; break; }
sleep 1
done
nxlog "$FUNCNAME ($$): end with res = $res"
echo $res; return 0;
fi
[ -n "$(echo "$2" | grep "nocheck")" ] && {
res=1
nxlog "$FUNCNAME ($$): special sharename \"$2\" - stop checking, end with res = $res"
echo $res; return 0;
}
NXREDIR_LIBRARY="$PATH_LIB/freenx-server/libnxredir.so.0"
[ -n "$5" ] && upass="$4%$5" || upass="$4 -N"
[ "$SAMBA_MOUNT_SHARE_PROTOCOL" = "cifs" ] && dport=445 || dport=139
CMDSTR="/usr/bin/rpcclient -U '$upass' -c 'openprinter $2' -p $dport -I 127.0.0.1 127.0.0.1 2>&1"
nxlog "$FUNCNAME ($$): invoke command \"$CMDSTR\""
res=""; numa="0"; ret="0"
while [ "$ret" = "0" ]; do
# addprinter and addmount try to use one mport at the same time,
# we need to endure ;)
(( numa++ ))
res=$(NXSAMBA_PORT=$3 LD_PRELOAD=$NXREDIR_LIBRARY eval $CMDSTR)
nxlog "$FUNCNAME ($$): on attempt $numa says \"$res\""
[ -n "$(echo "$res" | grep successfully)" ] && { ret="1"; continue; }
[ "$numa" -gt 4 ] && ret="1" && res="timeout"
done
[ -n "$(echo $res | grep successfully)" ] && res=1 || res=0
nxlog "$FUNCNAME ($$): end with res = $res"
echo $res
}
check_local_pa_tunnel() {
# params: tunnel ip:port
# resample rate
# amount of channels
# return: pid of pa process
# This starts user's local pulseaudio server if not running and tune its own
# modules - (re)create tunneled sink and source to remote pa
pactl_bin=$(find_app "pactl")
[ -z "$pactl_bin" ] && {
nxlog "$FUNCNAME ($$): pactl tool not found. Pulseaudio may be not installed."
echo ""; return 1;
}
pactl_bin="env LANG=C $pactl_bin"
PA_BIN="$(which pulseaudio 2>/dev/null)"
[ -z "$PA_BIN" ] && {
nxlog "$FUNCNAME ($$): Local pulseaudio binary not found."
echo ""; return 1;
}
unset PULSE_SERVER; unset PULSE_SINK; unset PULSE_SOURCE
# automatic start user's pa is here (pa starting with default system settings)
errstr=$($pactl_bin info 2>&1 | grep "failure")
[ -n "$errstr" ] && {
nxlog "$FUNCNAME ($$): Local PA is not reachable ($errstr)."
echo ""; return 1;
}
retpid=$(ps -wo pid=,cmd= -U $USER | awk '($2=="'$PA_BIN'") {print $1}') #'
lpa_mods=$($pactl_bin list short 2>/dev/null)
tunn_opts="server=$1"
tunn_exists= $(echo "$lpa_mods" | awk '/'$tunn_opts'/ {print $0}')
[ -n "$tunn_exists" ] && {
nxlog "$FUNCNAME ($$): Right modules already exists: $tunn_exists"
echo $retpid; return 0;
}
mname="module-always-sink" # automatic null-sinks will be disabled
rmids=$(echo "$lpa_mods" | awk '($2=="'$mname'") {print $1}') #'
# any sinks and sources will be unloaded too
rmids="$rmids $(echo "$lpa_mods" | awk '/sink_name|source_name/ {print $1}')"
[ -n "$rmids" ] && {
errids=""; errstr=""
for i in $rmids; do
errstr2=$($pactl_bin unload-module $i 2>&1)
[ $? -ne 0 ] && { errids="$errids $i"; errstr="$errstr; $errstr2"; }
done
}
[ -n "$errids" ] && {
nxlog "$FUNCNAME ($$): Removing PA modules error. ids=\"$errids\"; \"$errstr\" "
}
mname_tunn_sink="module-tunnel-sink";
mname_tunn_source="module-tunnel-source"
rpa_info=$($pactl_bin -s $1 info 2>/dev/null)
[ -z "$rpa_info" ] && {
nxlog "$FUNCNAME ($$): Couldn't get defaults from REMOTE pulseaudio $1."
echo ""; return 1;
}
[ -n "$2" ] && {
tunn_opts="$tunn_opts rate=$2"
[ -n "$3" ] && {
tunn_opts="$tunn_opts channels=$3"
[ "$3" == "1" ] && tunn_opts="$tunn_opts channel_map=mono"
}
}
success=""
def_rsink=$(echo "$rpa_info" | \
awk -F": " '($1~/Default Sink/) {print $2}')
if [ -z "$def_rsink" -o "$def_rsink" == "(null)" ]; then
nxlog "$FUNCNAME ($$): Couldn't get default sink from REMOTE pulseaudio $1."
else # load module-tunnel-sink
rsi_args="sink_name=tcl_out sink=$def_rsink $tunn_opts"
errstr=$($pactl_bin load-module $mname_tunn_sink $rsi_args 2>&1)
[ $? -ne 0 ] && {
nxlog "$FUNCNAME ($$): Loading module $mname_tunn_sink error (\"$rsi_args\"); \"$errstr\""
} || {
success="1"
nxlog "$FUNCNAME ($$): Successfully loadded module $mname_tunn_sink (\"$rsi_args\")."
}
fi
def_rsource=$(echo "$rpa_info" | \
awk -F": " '($1~/Default Source/) {print $2}')
if [ -z "$def_rsource" -o "$def_rsource" == "(null)" -o \
"$def_rsource" == "$def_rsink.monitor" ]; then
nxlog "$FUNCNAME ($$): Couldn't get default source from REMOTE pulseaudio $1."
else # load module-tunnel-source
rso_args="source_name=tcl_in source=$def_rsource $tunn_opts"
errstr=$($pactl_bin load-module $mname_tunn_source $rso_args 2>&1)
[ $? -ne 0 ] && {
nxlog "$FUNCNAME ($$): Loading module $mname_tunn_source error (\"$rso_args\"); \"$errstr\""
} || {
success="1"
nxlog "$FUNCNAME ($$): Successfully loadded module $mname_tunn_source (\"$rso_args\")."
}
fi
[ -n "$success" ] && echo $retpid || echo ""
}
reconnect_pa() {
papid=$(cat "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/pa_pid" 2>/dev/null)
[ -z "$papid" ] && return
mmport=""; papars="";
AGENT_PIDS=$(ps -wo pid=,cmd= -C "nxagent" | awk '{print $1}')
for sessdir in $USER_FAKE_HOME/.nx/C-*; do # start port search
[ "$sessdir" == "$USER_FAKE_HOME/.nx/C-$sess_id" ] && continue
NODE_AGENT_PID=$(cat "$sessdir/pids/agent" 2>/dev/null)
[ -z "$NODE_AGENT_PID" ] && continue
[ -z "$(echo "$AGENT_PIDS" | grep $NODE_AGENT_PID)" ] && continue
mmport=$(cat "$sessdir/scripts/mmport" 2>/dev/null)
[ -n "$mmport" ] && break
done
if [ -n "$mmport" ]; then # try reconnect
papars=$(cat "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/params.pa" 2>/dev/null)
rate=$(echo "$papars" | awk -F":" '{print $1}')
channels=$(echo "$papars" | awk -F":" '{print $2}')
[ -z "$(check_local_pa_tunnel 127.0.0.1:$mmport $rate $channels)" ] && {
nxlog "$FUNCNAME ($$): Couldn't reconnect PA tunnel to port $mmport."
}
else # kill local PA
kill $papid 2>/dev/null
fi
}
# node_terminate_agent <session id>
node_terminate_agent() {
nxlog "$FUNCNAME ($$): starting"
NODE_AGENT_PID=$(cat "$USER_FAKE_HOME/.nx/C-$1/pids/agent" 2>/dev/null)
[ -n "$NODE_AGENT_PID" ] && { nxlog "$FUNCNAME ($$): killing NODE_AGENT_PID"
kill $NODE_AGENT_PID 2>/dev/null; }
if [ -z $(ps x| grep "nxagent" | \
awk '{print $1}' | grep $NODE_AGENT_PID) ]; then
nxlog "$FUNCNAME ($$): process list for 'nxagent' empty"
else
nxlog "$FUNCNAME ($$): killing NODE_AGENT_PID. Try #2"
kill $NODE_AGENT_PID 2>/dev/null
fi
nxlog "$FUNCNAME ($$): end"
}
# node_terminate_session <session id>
# Used local vars: $virtualdesktop, $rootless
# Used config vars: $COMMAND_XAUTH, $SESSION_LOG_CLEAN
node_terminate_session() {
#
# Cleanup session
#
nxlog "$FUNCNAME ($$): starting"
[ -d "$USER_FAKE_HOME/.nx/C-$1/" ] || {\
nxlog "$FUNCNAME ($$): Session directory not found."
return;
}
[ -e "$USER_FAKE_HOME/.nx/C-$1/terminating" ] && {\
nxlog "$FUNCNAME ($$): Terminating still in progress. Bye/"
return;
}
touch "$USER_FAKE_HOME/.nx/C-$1/terminating"
nxlog "$FUNCNAME ($$): Start terminating session \"$1\" with status \"$2\""
# Kill nxagent
NODE_AGENT_PID=$(cat "$USER_FAKE_HOME/.nx/C-$1/pids/agent" 2>/dev/null)
# JJK: Kill running services
# FF: Seems this is needed also here ...
# dimbor: ... but have to be a little more tender ;)
nxlog "$FUNCNAME ($$): call node_stop_services"
node_stop_services
if [ -n "$NODE_AGENT_PID" ]; then
nxlog "$FUNCNAME ($$): start killing of nxagent"
kill $NODE_AGENT_PID 2>/dev/null
wait $NODE_AGENT_PID
nxlog "$FUNCNAME ($$): nxagent ($NODE_AGENT_PID) is dead now"
fi
# Kill tail process
NODE_TAIL_PID=$(cat "$USER_FAKE_HOME/.nx/C-$sess_id/pids/tail" 2>/dev/null)
[ -n "$NODE_TAIL_PID" ] && { nxlog "$FUNCNAME ($$): kill tail process"
kill $NODE_TAIL_PID 2>/dev/null;
}
# Remove display information
nxlog "$FUNCNAME ($$): Remove display information"
NODE_DISPLAY=$(echo $1 | awk 'BEGIN {FS="-"} {i=NF-1; print $i}')
rm -f /tmp/.X$NODE_DISPLAY-lock; rm -f /tmp/.X11-unix/X$NODE_DISPLAY
# Remove magic cookie information
nxlog "$FUNCNAME ($$): Remove magic cookie information"
$COMMAND_XAUTH -v source "$USER_FAKE_HOME/.nx/C-$1/scripts/authority" >/dev/null 2>&1
# Preserve or remove session information
nxlog "$FUNCNAME ($$): Preserve or remove session information"
if [ "$SESSION_LOG_CLEAN" = "1" ]; then
nxlog "$FUNCNAME ($$): Clean session information."
rm -rf "$USER_FAKE_HOME/.nx/C-$1/"
rm -f "$USER_FAKE_HOME/.nx/nxnode-$1.log"
rm -f "$USER_FAKE_HOME/.nx/nxnode.log"
fi
if [ "$SESSION_LOG_CLEAN" = "0" -a "$2" = "failed" ] ; then
nxlog "$FUNCNAME ($$): Session failed. Rename session directory from \"$USER_FAKE_HOME/.nx/C-$1/\" to \"$USER_FAKE_HOME/.nx/F-C-$1\""
mv "$USER_FAKE_HOME/.nx/C-$1/" "$USER_FAKE_HOME/.nx/F-C-$1"
rm -f "$USER_FAKE_HOME/.nx/F-C-$1/terminating"
elif [ "$SESSION_LOG_CLEAN" = "0" -a "$2" != "failed" ] ; then
nxlog "$FUNCNAME ($$): Session terminated. Rename session directory from \"$USER_FAKE_HOME/.nx/C-$1/\" to \"$USER_FAKE_HOME/.nx/T-C-$1\""
mv "$USER_FAKE_HOME/.nx/C-$1/" "$USER_FAKE_HOME/.nx/T-C-$1"
rm -f "$USER_FAKE_HOME/.nx/T-C-$1/terminating"
fi
nxlog "$FUNCNAME ($$): end"
}
# node_fail_restore_session <session id>
# TODO: Kill still running tail -f process.
node_fail_restore_session() {
nxlog "$FUNCNAME ($$): starting"
echo "NX> 1004 Error: Could not resume session. nxagent process could not be found."
NODE_TAIL_PID=$(cat "$USER_FAKE_HOME/.nx/C-$sess_id/pids/tail" 2>/dev/null)
[ -n "$NODE_TAIL_PID" ] && kill $NODE_TAIL_PID
[ -n "$NODE_TAIL_PID" ] && echo "NX 1004> kill $NODE_TAIL_PID"
nxlog "$FUNCNAME ($$): call 'node_terminate_session \"$1\" \"failed\"'"
node_terminate_session "$1" "failed"
nxlog "$FUNCNAME ($$): end. Next is 'exit 1'"
exit 1
}
# node_suspend_session <session id>
node_suspend_session() {
nxlog "$FUNCNAME ($$): starting"
NODE_AGENT_PID=$(cat "$USER_FAKE_HOME/.nx/C-$1/pids/agent" 2>/dev/null)
if [ -n "$NODE_AGENT_PID" ]; then
nxlog "$FUNCNAME ($$): NODE_AGENT_PID=\"$NODE_AGENT_PID\". Killing..."
kill -0 $NODE_AGENT_PID || {
nxlog "$FUNCNAME ($$): end (0)"
return 1;
}
kill -HUP $NODE_AGENT_PID && {
nxlog "$FUNCNAME ($$): end (HUP)"
return 0;
}
else
nxlog "$FUNCNAME ($$): NODE_AGENT_PID is empty"
nxlog "$FUNCNAME ($$): end"
fi
return 1
}
# node_find_application <type>
# Used config vars: $COMMAND_START_KDE, $COMMAND_START_GNOME,
# $COMMAND_START_CDE, $COMMAND_XTERM, $USER_X_STARTUP_SCRIPT,
# $DEFAULT_X_SESSION
node_find_application() {
nxlog "$FUNCNAME ($$): starting with args \"$@\""
NODE_STARTX=""
case $1 in
shadow|windows|vnc)
:
;;
unix-kde)
NODE_STARTX=$COMMAND_START_KDE
[ "$KDE4_ENABLE" = "1" -a -n "$COMMAND_START_KDE" ] && \
NODE_STARTX=$COMMAND_START_KDE4
;;
unix-gnome)
NODE_STARTX=$COMMAND_START_GNOME
;;
unix-cde)
NODE_STARTX=$COMMAND_START_CDE
;;
windows-helper)
application="$PATH_BIN/nxdesktop_helper"
NODE_STARTX=$application
;;
unix-application|vnc-helper)
[ "$application" = "xterm" ] && application=$COMMAND_XTERM
NODE_STARTX=$application
;;
unix-console)
NODE_STARTX=$COMMAND_XTERM
;;
unix-default|*)
if [ -x "$HOME/$USER_X_STARTUP_SCRIPT" ]; then
NODE_STARTX="$HOME/$USER_X_STARTUP_SCRIPT"
elif which "$DEFAULT_X_SESSION" >/dev/null 2>&1; then
NODE_STARTX="$DEFAULT_X_SESSION"
else NODE_STARTX=$COMMAND_XTERM
fi
;;
esac
# dimbor: another personalyzed way to ACLS control and replace X-application
[ -d "$NX_ACL_DIR" -a -x "$PATH_BIN/nxacl.app" ] && {
nxlog "$FUNCNAME ($$): !!! call nxacl.app with source NODE_STARTX=\"$NODE_STARTX\""
NODE_STARTX=$($PATH_BIN/nxacl.app "$NODE_STARTX" "$CMDLINE")
}
nxlog "$FUNCNAME ($$): return NODE_STARTX=\"$NODE_STARTX\""
echo "$NODE_STARTX"
nxlog "$FUNCNAME ($$): end"
}
get_pa_sound_mode() {
# params: port of pa server
# ipaddr
[ -z "$2" ] && {
nxlog "$FUNCNAME ($$): Client's IP-address is not defined."
echo ""; return 1;
}
[ -z "$($COMMAND_NETCAT -nvz -w 1 $2 $1 2>&1 | grep "open")" ] && {
nxlog "$FUNCNAME ($$): Client's pulseaudio at $2:$1 is not reachable."
echo ""; return 1;
}
pactl_bin=$(find_app "pactl")
[ -z "$pactl_bin" ] && {
nxlog "$FUNCNAME ($$): pactl tool not found. Pulseaudio may be not installed."
echo ""; return 1;
}
pactl_bin="env LANG=C $pactl_bin -s $2:$1"
nxlog "$FUNCNAME ($$): $pactl_bin list short"
cl_pa_mods=$($pactl_bin list short)
#nxlog "$FUNCNAME ($$): $cl_pa_mods"
[ -n "$(echo "$cl_pa_mods" | grep "ts_receiver")" -a \
-n "$(echo "$cl_pa_mods" | grep "ts_sender")" ] && {
nxlog "$FUNCNAME ($$): Specials sink and source at $2:$1 are found."
echo "special"; return 0;
}
nxlog "$FUNCNAME ($$): Defaults sink and source at $2:$1 be used."
echo "default"; return 0;
}
# node_start_applications
# Used local vars: $type, $application, $sess_id, $mediahelper,
# $virtualdesktop, $rootless, $display
# Used config vars: <several>
node_start_applications() {
nxlog "$FUNCNAME ($$): starting"
# close input and output file descriptors
exec 0<&-
exec 1>&-
exec 2>&-
# Prepare application startup
export DISPLAY=:$display
if [ "$ENABLE_SOURCE_PROFILE" = "1" ]; then
nxlog "$FUNCNAME ($$): source profile"
. /etc/profile
fi
if [ "$ENABLE_SOURCE_BASH_PROFILE" = "1" ]; then
nxlog "$FUNCNAME ($$): source bash profile"
[ -f ~/.bash_profile ] && . ~/.bash_profile
[ -f /etc/bashrc ] && . /etc/bashrc
fi
nxlog "$FUNCNAME ($$): DISPLAY is $display"
cntr=$AGENT_STARTUP_TIMEOUT
if [ -n "$(which xdpyinfo 2>/dev/null)" ]; then
while [ -z "$(xdpyinfo 2>/dev/null | head)" ]; do
let "cntr -= 1"
[ $cntr -lt 0 ] && break
sleep 0.3s
nxlog "$FUNCNAME ($$): Sleep while get a valid DISPLAY ($cntr)"
done
else
nxlog "$FUNCNAME ($$): xdpyinfo not found. Waiting for DISPLAY is not possible!"
fi
#numlockx
if [ "$NUMLOCKX_STATUS" != "system" ]; then
nxlog "$FUNCNAME ($$): Run \"$NUMLOCKX $NUMLOCKX_STATUS\""
"$NUMLOCKX" "$NUMLOCKX_STATUS"
fi
mkdir -p "$USER_FAKE_HOME/.nx/C-$sess_id/pids/apps/"
# Which application do we start?
NODE_APPLICATION=$(node_find_application "$type")
# For rdesktop/VNC, there is no application to start
if [ -n "$NODE_APPLICATION" ]; then
nxlog "$FUNCNAME ($$): Got NODE_APPLICATION is \"$NODE_APPLICATION\""
else
nxlog "$FUNCNAME ($$): NODE_APPLICATION is empty"
return
fi
# Check if we want to use a mediahelper
mmode="$(echo "$mediahelper" | awk -F"-" '{print($1)}')"
rate="$(echo "$mediahelper" | awk -F"-" '{print($2)}')"
channels="$(echo "$mediahelper" | awk -F"-" '{print($3)}')"
mmport="$(cat "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/mmport" 2>/dev/null)"
if [ "$mmode" == "pa" -a -n "$mmport" ]; then
pa_remote="127.0.0.1:$mmport"
pa_pid="$(check_local_pa_tunnel $pa_remote $rate $channels)"
if [ -n "$pa_pid" ]; then
nxlog "$FUNCNAME ($$): Local pulseaudio is started ($pa_pid). Sound mode set to tunneled."
echo "$pa_pid" > "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/pa_pid"
params=$rate
[ -n "$channels" ] && params="$params:$channels"
[ -n "$params" ] && echo "$params" > "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/params.pa"
else
nxlog "$FUNCNAME ($$): Local pulseaudio NOT STARTED. Set PULSE_SERVER=127.0.0.1:$mmport"
export PULSE_SERVER="127.0.0.1:$mmport"
fi
elif [ "$ENABLE_DIRECT_NATIVE_PA_CHECK" = "1" ] ; then
native_sound_mode="$(get_pa_sound_mode 4713 $NXUSERIP)"
if [ -n "$native_sound_mode" ]; then
nxlog "$FUNCNAME ($$): Sound mode set to direct $native_sound_mode"
export PULSE_SERVER="$NXUSERIP:4713"
if [ "$native_sound_mode" = "special" ]; then
export PULSE_SINK="ts_receiver"
export PULSE_SOURCE="ts_sender.monitor"
fi
PADSP_BIN_PRELOAD="$(find_app "padsp")"
[ -n "$PADSP_BIN_PRELOAD" ] && {
NODE_APPLICATION="$PADSP_BIN_PRELOAD $NODE_APPLICATION"
} || nxlog "$FUNCNAME ($$): padsp script not found. Sound may be disabled."
fi
elif [ "$mediahelper" = "esd" ]; then
# Set Espeaker variable
let ESPEAKER=$display+7000
export ESPEAKER="127.0.0.1:$ESPEAKER"
# Do not spawn new ESD daemons
export ESD_NO_SPAWN="yes"
# Check for config file directive
if [ "$ENABLE_ESD_PRELOAD" = "1" -a \
-x "$(find_app $ESD_BIN_PRELOAD)" ]; then
nxlog "$FUNCNAME ($$): Preload \"$ESD_BIN_PRELOAD\""
NODE_APPLICATION="$ESD_BIN_PRELOAD $NODE_APPLICATION"
nxlog "$FUNCNAME ($$): NODE_APPLICATION is \"$NODE_APPLICATION\""
echo "Info: NXNODE - Using $ESD_BIN_PRELOAD wrapper script." >> "$USER_FAKE_HOME/.nx/C-$sess_id/session"
fi
elif [ "$mediahelper" = "artsd" ]; then
# Overwrite users mcoprc
echo -n "GlobalComm=Arts::X11GlobalComm" > $HOME/.mcoprc
if [ "$ENABLE_ARTSD_PRELOAD" = "1" -a \
-x "$(find_app $ARTSD_BIN_PRELOAD)" ]; then
NODE_APPLICATION="$ARTSD_BIN_PRELOAD $NODE_APPLICATION"
echo "Info: NXNODE - Using $ARTSD_BIN_PRELOAD wrapper script." >> "$USER_FAKE_HOME/.nx/C-$sess_id/session"
fi
fi
[ "$ENABLE_CUPS_SERVER_MODE" = "1" ] && {
CUPS_SERVER_SOCKET=$(LC_ALL=C /usr/bin/lpstat -H 2>/dev/null)
[ -r $CUPS_SERVER_SOCKET ] || CUPS_SERVER_SOCKET=""
} || CUPS_SERVER_SOCKET="$USER_FAKE_HOME/.nx/C-$sess_id/cups/cups.sock"
[ "$ENABLE_CUPS_SERVER_EXPORT" = "1" -a -n "$CUPS_SERVER_SOCKET" ] && {
[ "$cups" = "1" -o "$samba" = "1" ] && {
nxlog "$FUNCNAME ($$): export CUPS_SERVER=$CUPS_SERVER_SOCKET"
export CUPS_SERVER="$CUPS_SERVER_SOCKET"
}
}
if [ "$ENABLE_SAMBA_PRELOAD" = "1" -a -x "$PATH_BIN/nxredir" ]; then
let NXSAMBA_PORT=$display+3000
export NXSAMBA_PORT
nxlog "$FUNCNAME ($$): Preload SAMBA using nxredir. NXSAMBA_PORT is \"$NXSAMBA_PORT\""
NODE_APPLICATION="$PATH_BIN/nxredir $NODE_APPLICATION"
echo "Info: NXNODE - Using nxredir wrapper script to forward SMB ports 139 and 445 to port $NXSAMBA_PORT." >> "$USER_FAKE_HOME/.nx/C-$sess_id/session"
fi
# Do we need to PRELOAD any libraries?
[ "$virtualdesktop" = "0" -a "$rootless" != "1" ] && export LD_PRELOAD="$APPLICATION_LIBRARY_PRELOAD:$LD_PRELOAD"
# Should we start a window manager?
if [ "$virtualdesktop" = "1" -a "$type" = "unix-application" -a \
"$DEFAULT_X_WM" != "" -a -x "$(find_app $DEFAULT_X_WM)" ]; then
nxlog "$FUNCNAME ($$): start a window manager - \"DISPLAY=:$display $DEFAULT_X_WM\""
DISPLAY=:$display $DEFAULT_X_WM >>"$USER_FAKE_HOME/.nx/C-$sess_id/session" 2>&1 &
NODE_WM_PID=$!
nxlog "$FUNCNAME ($$): NODE_WM_PID=\"$NODE_WM_PID\""
fi
# Use Xsession to execute the Desktop session
case $type in
unix-gnome)
export STARTUP="$NODE_APPLICATION"
if [ "$login_method" = "GUEST" ]; then
NODE_APPLICATION=$COMMAND_GUEST_X_SESSION
elif [ "$BOOTSTRAP_X_SESSION" = "1" ]; then
NODE_APPLICATION=$COMMAND_GDM_X_SESSION
fi
;;
unix-kde|unix-cde)
export STARTUP="$NODE_APPLICATION"
if [ "$login_method" = "GUEST" ]; then
NODE_APPLICATION=$COMMAND_GUEST_X_SESSION
elif [ "$BOOTSTRAP_X_SESSION" = "1" ]; then
NODE_APPLICATION=$DEFAULT_X_SESSION
fi
;;
esac
SECOND_APP=""; NX_SAPP_PIDS0=""
[ $ENABLE_ROOTLESS_TERMINATE_SESSION = "1" -a "$rootless" = "1" ] && {
NAPP=$(basename $(echo "$NODE_APPLICATION" | awk '{print $1}'))
SECOND_APP=$(echo "$APP_WAIT_MAP" | awk 'BEGIN{FS=":"; RS=";"; ORS=""}
($1=="'$NAPP'") {print $2}')
[ -n "$SECOND_APP" ] && {
NX_SAPP_PIDS0=$(ps -w -o pid=,cmd= -C "$SECOND_APP" | awk '{print $1}')
nxlog "$FUNCNAME ($$): PID of $SECOND_APP will be waiting too. Initial: \"$NX_SAPP_PIDS0\""
}
}
# Startup the application
nxlog "$FUNCNAME ($$): Starting NODE_APPLICATION with /etc/nxserver/Xsession"
DISPLAY=:$display /etc/nxserver/Xsession $NODE_APPLICATION >> \
"$USER_FAKE_HOME/.nx/C-$sess_id/session" 2>&1 &
NODE_APP_PID=$!
nxlog "$FUNCNAME ($$): Start successful. NODE_APP_PID=$NODE_APP_PID"
mkdir -p "$USER_FAKE_HOME/.nx/C-$sess_id/pids/"
echo "$NODE_APP_PID" >"$USER_FAKE_HOME/.nx/C-$sess_id/pids/apps/$NODE_APP_PID"
nxlog "$FUNCNAME ($$): Waiting for NODE_APP_PID"
wait $NODE_APP_PID
nxlog "$FUNCNAME ($$): NODE_APP_PID finished"
# Kill or wait for the started window manager
[ -n "$NODE_WM_PID" ] && {
nxlog "$FUNCNAME ($$): NODE_WM_PID is not empty"
# kill the WM after application is finished?
[ "$KILL_DEFAULT_X_WM" = "1" ] && { nxlog "$FUNCNAME ($$): killing $NODE_WM_PID"
kill $NODE_WM_PID 2>/dev/null; }
# or just wait until it finishes?
[ "$KILL_DEFAULT_X_WM" = "1" ] || { nxlog "$FUNCNAME ($$): wait for $NODE_WM_PID is dead"
wait $NODE_WM_PID; }
}
nxlog "$FUNCNAME ($$): Clean pids/apps/$NODE_APP_PID"
rm -f "$USER_FAKE_HOME/.nx/C-$sess_id/pids/apps/$NODE_APP_PID"
sleep "$NODE_APP_WAIT_TIMEOUT"s
[ $ENABLE_ROOTLESS_TERMINATE_SESSION = "1" -a "$rootless" = "1" ] && {
if [ -n "$SECOND_APP" ] ; then
NX_SAPP_PIDS=""
NX_SAPP_PIDS1=$(ps -w -o pid=,cmd= -C "$SECOND_APP" | awk '{print $1}')
while read SPID; do
[ -z "$(echo "$NX_SAPP_PIDS0" | grep $SPID)" ] && \
NX_SAPP_PIDS="$NX_SAPP_PIDS${NX_SAPP_PIDS:+$' '}$SPID"
done <<< "$NX_SAPP_PIDS1"
nxlog "$FUNCNAME ($$): Waiting for $SECOND_APP PID(S) \"$NX_SAPP_PIDS\""
while [ -n "$(ps -o pid= -p $NX_SAPP_PIDS 2>/dev/null)" ]; do
sleep 1s
done
nxlog "$FUNCNAME ($$): PID(S) \"$NX_SAPP_PIDS\" finished"
node_terminate_session "$sess_id"
else node_terminate_session "$sess_id"
fi
}
# Do not terminate agent in case of rootless agent mode.
# The agent times out after a while by itself anyway.
if [ "$virtualdesktop" = "1" -o "$rootless" != "1" ] ; then
nxlog "$FUNCNAME ($$): Call node_terminate_agent for non-rootless or virtualdesktop session type"
nxlog "$FUNCNAME ($$): Call 'node_terminate_agent \"$sess_id\"'"
node_terminate_session "$sess_id"
fi
nxlog "$FUNCNAME ($$): end"
}
# node_persistent_session
# Is the user allowed to run a persistent session?
node_agent_persistent_session() {
P="-nopersistent"
# Guest sessions are always nonpersistent
if [ "$login_method" = "GUEST" ]; then
echo "$P"; return
fi
OLD_IFS=$IFS; IFS=","
if [ "$ENABLE_PERSISTENT_SESSION" = "all" ]; then P="-persistent"
else
for USERNAME in $ENABLE_PERSISTENT_SESSION; do
[ "${USERNAME:0:1}" != "@" ] && [ "$USER" = "$USERNAME" ] && \
P="-persistent" && break;
[ "${USERNAME:0:1}" = "@" ] && \
[ -z $(groups "$USER" | egrep "^${USERNAME:1}:") ] && \
P="-persistent" && break;
done
fi
for USERNAME in $DISABLE_PERSISTENT_SESSION; do
[ "${USERNAME:0:1}" != "@" ] && [ "$USER" = "$USERNAME" ] && \
P="-nopersistent" && break;
[ "${USERNAME:0:1}" = "@" ] && \
[ -z $(groups "$USER" | egrep "^${USERNAME:1}:") ] && \
P="-nopersistent" && break;
done
IFS=$OLD_IFS
echo "$P"
}
# node_start_agent
node_start_agent() {
# Ok, now we do some wicked fd magic.
#
# first part:
#
# nxagent's fd #2 -> fd #3
# second part:
#
# fd #1 -> #4
# fd #3 -> #1
# tee | node_start_monitor
# third part
# fd #4 -> #1
# => all output of nxagent goes to tee | node_start_monitor, while
# leaving all other output flow through like normally.
# preparations
nxlog "$FUNCNAME ($$): starting"
exec 3>&2
exec 4>&1
{
{
# Setup environment
if [ "$ENABLE_SOURCE_PROFILE" = "1" ]; then
nxlog "$FUNCNAME ($$): source profile"
. /etc/profile
fi
if [ "$ENABLE_SOURCE_BASH_PROFILE" = "1" ]; then
nxlog "$FUNCNAME ($$): source bash profile"
[ -f ~/.bash_profile ] && . ~/.bash_profile
[ -f /etc/bashrc ] && . /etc/bashrc
fi
export DISPLAY="nx/nx,options=$USER_FAKE_HOME/.nx/C-$sess_id/options:$display"
export XAUTHORITY="$USER_FAKE_HOME/.nx/C-$sess_id/authority"
export HOME="$USER_FAKE_HOME"
export NX_CLIENT="$PATH_BIN/nxdialog"
# Setup optional parameters for nxagent
# keyboard
K=""
# backwards compatibility
[ -n "$keyboard" ] && K="-keyboard $keyboard"
[ -n "$kbtype" ] && K="-kbtype $kbtype"
# backingstore
B=""
if [ -n "$backingstore" -a "$ENABLE_1_5_0_BACKEND" = "1" ]; then
[ "$backingstore" != 1 ] && B="-bs $backingstore"
[ "$backingstore" = 1 ] && B="+bs"
fi
# geometry
G=""
[ -n "$geometry" ] && G="-geometry $geometry"
# type of session
R="-D"; [ "$rootless" = "1" ] && R="-R"
# Setup fullscreen parameters
vncfullscreen=""
[ "$geometry" = "fullscreen" -a "$type" = "vnc" ] && \
vncfullscreen="-fullscreen" && G=""
[ "$geometry" = "fullscreen" -a "$type" = "windows" ] && \
G="-geometry `echo $screeninfo | cut -d"x" -f1,2`"
# Start the wanted nxagent
if [ "$type" = "windows" ]; then
nxlog "$FUNCNAME ($$): Type \"windows\""
# nxdesktop session (Windows RDP)
[ "$SET_LD_LIBRARY_PATH" = "1" ] && \
export LD_LIBRARY_PATH="$AGENT_LIBRARY_PATH:$LD_LIBRARY_PATH"
# Setup optional parameters
U=""; P=""; D=""
[ -n "$agent_user" ] && U="-u $agent_user"
[ -n "$agent_password" ] && P="-p -"
[ -n "$agent_domain" ] && D="-d $agent_domain"
# Start the agent
echo "$agent_password" | $PATH_BIN/nxdesktop \
-name "NX - $user@$SERVER_NAME:$display - $session (GPL Edition)" \
-option "$USER_FAKE_HOME/.nx/C-$sess_id/options" \
$K $G $U $P $D $agent_server $AGENT_EXTRA_OPTIONS_RDP 2>&3 &
elif [ "$type" = "vnc" ]; then
nxlog "$FUNCNAME ($$): Type \"vnc\""
# nxviewer session (VNC RFB)
[ "$SET_LD_LIBRARY_PATH" = "1" ] && \
export LD_LIBRARY_PATH="$AGENT_LIBRARY_PATH:$LD_LIBRARY_PATH"
# Setup password
mkdir -p "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/"
echo "$agent_password" | $PATH_BIN/nxpasswd \
"$USER_FAKE_HOME/.nx/C-$sess_id/scripts/.passwd" doit
# Start x11vnc
if [ -n "$shadowdisplay" ]; then
(
viewonly=""
[ "$ENABLE_INTERACTIVE_SESSION_SHADOWING" != "1" ] && \
viewonly="-viewonly"
DISPLAY="$shadowhost:$shadowdisplay" x11vnc -localhost \
$viewonly -timeout 120 -rfbauth \
"$USER_FAKE_HOME/.nx/C-$sess_id/scripts/.passwd" \
>"$USER_FAKE_HOME/.nx/C-$sess_id/scripts/.vnc_port" 2>&3 &
)
sleep 2
agent_port=$(cat "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/.vnc_port" \
| egrep "^PORT=" | cut -d'=' -f 2)
[ -z "agent_port" ] && agent_port="0"
# note the :: is not a mistake, but rather a hint for nxviewer
# to use this as a port and not interpret it as a display.
agent_server="127.0.0.1::$agent_port"
rm -f "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/.vnc_port"
fi
# Start the agent
$PATH_BIN/nxviewer -encodings tight hextile copyrect raw \
-passwd "$USER_FAKE_HOME/.nx/C-$sess_id/scripts/.passwd" \
-name "NX - $user@$SERVER_NAME:$display - $session (GPL Edition)" \
-option "$USER_FAKE_HOME/.nx/C-$sess_id/options" $vncfullscreen \
$G $K $agent_server $AGENT_EXTRA_OPTIONS_RFB 2>&3 &
elif [ "$R" = "-R" -a "$rootless" != "1" ]; then
# nxproxy single application mode session
nxlog "$FUNCNAME ($$): Start nxproxy for single application session mode"
[ "$SET_LD_LIBRARY_PATH" = "1" ] && \
export LD_LIBRARY_PATH="$PROXY_LIBRARY_PATH:$LD_LIBRARY_PATH"
nxlog "$FUNCNAME ($$): Start nxproxy by command: '$PATH_BIN/nxproxy -C :$display $PROXY_EXTRA_OPTIONS'"
$PATH_BIN/nxproxy -C :$display $PROXY_EXTRA_OPTIONS 2>&3 &
else
nxlog "$FUNCNAME ($$): NXAgent session type"
# nxagent session (X11)
[ "$SET_LD_LIBRARY_PATH" = "1" ] && \
export LD_LIBRARY_PATH="$AGENT_LIBRARY_PATH:$LD_LIBRARY_PATH"
# Setup optional parameters
P=$(node_agent_persistent_session)
FP=""; [ -n "$AGENT_FONT_SERVER" ] && FP="-fp $AGENT_FONT_SERVER"
if [ "$type" = "shadow" ]; then
nxlog "$FUNCNAME ($$): Type \"shadow\". Add some args to nxagent"
R="-S -shadow $shadowhost:$shadowdisplay -shadowmode $ENABLE_INTERACTIVE_SESSION_SHADOWING"
P="-nopersistent"
fi
# Start the agent
nxlog "$FUNCNAME ($$): env start `env`"
nxlog "$FUNCNAME ($$): env end"
nxlog "$FUNCNAME ($$): Start nxagent by command: '$COMMAND_NXAGENT $P $R -name \"NX - $user@$SERVER_NAME:$display - $session (GPL Edition)\" -option \"$USER_FAKE_HOME/.nx/C-$sess_id/options\" $B $FP $AGENT_EXTRA_OPTIONS_X :$display'"
#PATH="$PATH_BIN:$PATH" $COMMAND_NXAGENT $P $R -name "NX - $user@$SERVER_NAME:$display - $session (GPL Edition)" -option "$USER_FAKE_HOME/.nx/C-$sess_id/options" $K $G $B $FP $AGENT_EXTRA_OPTIONS_X :$display 2>&3 &
PATH="$PATH_BIN:$PATH" $COMMAND_NXAGENT $P $R \
-name "NX - $user@$SERVER_NAME:$display - $session (GPL Edition)" \
-option "$USER_FAKE_HOME/.nx/C-$sess_id/options" $B $FP \
$AGENT_EXTRA_OPTIONS_X :$display 2>&3 &
fi
# Wait for the agent
NODE_AGENT_PID=$!
mkdir -p "$USER_FAKE_HOME/.nx/C-$sess_id/pids/"
echo "$NODE_AGENT_PID" >"$USER_FAKE_HOME/.nx/C-$sess_id/pids/agent"
nxlog "$FUNCNAME ($$): Wait for NODE_AGENT_PID ($NODE_AGENT_PID)"
wait $NODE_AGENT_PID
NODE_AGENT_EXIT_STATUS=$?
nxlog "$FUNCNAME ($$): NODE_AGENT_EXIT_STATUS = \"$NODE_AGENT_EXIT_STATUS\""
NODE_FAILED=""
if [ $NODE_AGENT_EXIT_STATUS -ne 0 ]; then