-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.sh
executable file
·920 lines (807 loc) · 33.7 KB
/
session.sh
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
#!/usr/bin/env bash
#
# Copyright © 2008-2016 RAAF Technology bv
#
# This file is part of Session.
#
# Session is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Session is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Session. If not, see <http://www.gnu.org/licenses/>.
# Determine where $SESSION_HOME is.
if [ ! "$SESSION_HOME" ]; then
ABS="$PWD"
ORIG_IFS="$IFS"
IFS="/"
for DIR in $0; do
if [ -n "$DIR" ]; then
if [ "$DIR" = ".." ]; then
ABS="${ABS%/*}"
elif [ "$DIR" != "." ]; then
ABS="$ABS/$DIR"
fi
fi
done
unset DIR
IFS="$ORIG_IFS"
OFFSET="/"
if [ -e "$(dirname "$ABS")$OFFSET" ]; then
# Found, probably called relatively.
cd "$(dirname "$ABS")$OFFSET"
SESSION_HOME="$(pwd)"
unset ABS
elif [ -e "$(dirname "$0")$OFFSET" ]; then
# Found, probably called absolutely.
cd "$(dirname "$0")$OFFSET"
SESSION_HOME="$(pwd)"
fi
fi
# Initialize globals.
source "$SESSION_HOME/globals.sh"
# Set default permission mask and make sure directories exist.
umask 077
mkdir -p "$usrcfd" "$usrcfd/cfg" "$usrcfd/log" "$usrcfd/sys" "$usrcfd/tmp" "$usrcfd/tpl"
chmod 700 "$usrcfd" "$usrcfd/cfg" "$usrcfd/log" "$usrcfd/sys" "$usrcfd/tmp" "$usrcfd/tpl"
# Write settings to options.env if it does not exist yet.
if [ ! -e "$usropt" -a ! -e "$sysopt" ]; then
printf %s "\
terminal='$terminal'
desktop='$desktop'
browser='$browser'
smbtell='$smbtell'
sshtell='$sshtell'
smbsend='$smbsend'
sshsend='$sshsend'
execute='$execute'
access='$access'
create='$create'
destroy='$destroy'
send='$send'
resilient='$resilient'
timeout='$timeout'
titling='$titling'
tabbed='$tabbed'
color='$color'
agent='$agent'
privy='$privy'
debug='$debug'
width='$width'
height='$height'
" | sed 's/^[[:space:]]*//' > "$usropt"
elif [ ! -e "$usropt" -a -e "$sysopt" ]; then
cp "$sysopt" "$usropt"
fi
# Source global options.env, if existing.
if [ -e "$sysopt" ]; then
. "$sysopt"
fi
# Source local options.env, if existing (which may override global options).
if [ -e "$usropt" ]; then
. "$usropt"
fi
# Create backslashed user variable.
if [ "$user" ]; then
# $user was set directly, probably in options.env.
# This indicates the local user differs from the remote.
userDblBacksl="${user//\\/\\\\}"
localUserDblBacksl="${USER//\\/\\\\}"
elif [ "$USER" ]; then
# $USER was set in the environment.
userDblBacksl="${USER//\\/\\\\}"
localUserDblBacksl="${USER//\\/\\\\}"
elif [ "$USERNAME" ]; then
# $USERNAME was set in the environment.
userDblBacksl="${USERNAME//\\/\\\\}"
localUserDblBacksl="${USER//\\/\\\\}"
else
# If all else fails you get this.
userDblBacksl="unknown"
localUserDblBacksl="unknown"
fi
# Initialize functions.
source "$SESSION_HOME/functions.sh"
# Make sure at least user's session.conf exists.
if [ ! -e "$usrcff" ]; then
:> "$usrcff"
fi
# Get any included file names.
sysincs=$(for line in $(grep ^include $syscff 2> /dev/null); do expandVariables $(cutParentheses "$line");done)
usrincs=$(for line in $(grep ^include $usrcff 2> /dev/null); do expandVariables $(cutParentheses "$line");done)
allcff=""
for file in $syscff $usrcff $sysincs $usrincs; do
if [ -e "$file" ]; then
allcff="$allcff $file"
elif [ -e "$syscfd/cfg/$file" ]; then
allcff="$allcff $syscfd/cfg/$file"
elif [ -e "$usrcfd/cfg/$file" ]; then
allcff="$allcff $usrcfd/cfg/$file"
fi
done
# Write active session.conf from all existence verified config files.
sed -e '/^#/d' -e '/^include/d' -e '/^[[:space:]]*$/d' -e "s|\$user|$userDblBacksl|g" $allcff > "$config"
# Initialize bash quoted regexp behaviour.
if [ "$(echo "$SHELL" | grep -i "bash")" ]; then
handleQuotedRegExpBehaviour
fi
# Read in or create tools.required and tools.found.
if [ -e "$usrcfd/cfg/tools.required" -a -e "$usrcfd/cfg/tools.found" ]; then
. "$usrcfd/cfg/tools.required"
. "$usrcfd/cfg/tools.found"
elif [ "$1" != "reinit" ]; then
reportInfo "Detecting which executables are available"
toolFinder
. "$usrcfd/cfg/tools.required"
. "$usrcfd/cfg/tools.found"
fi
# Check if at least mandatory tools are available.
if [ "$tools_session" != "$tools_session_found" -a "$1" != "reinit" ]; then
reportError "One of the mandatory executables required by session was not found"
exit 1
fi
# Enable (*) or disable (0) execute functions.
if [ "$tools_execute" = "$tools_execute_found" -a "$execute" != 0 ]; then execute="true" ; else execute="false" ; fi
# Enable (*) or disable (0) access functions.
if [ "$tools_access" = "$tools_access_found" -a "$access" != 0 ]; then access="true" ; else access="false" ; fi
# Enable (*) or disable (0) send functions.
if [ "$tools_send" = "$tools_send_found" -a "$send" != 0 ]; then send="true" ; else send="false" ; fi
# Enable (*) or disable (0) create functions.
if [ "$create" != 0 ]; then create="true" ; else create="false" ; fi
# Enable (1) or disable (*) destroy functions.
if [ "$destroy" != 1 ]; then destroy="false" ; elif [ "$destroy" = 1 ]; then destroy="true" ; fi
# Enable (*) or disable (0) resilient (extremely high latency networks) mode.
if [ "$resilient" != 0 ]; then resilient="true" ; else unset resilient ; fi
# Enable (*) or disable (0) title setting for various access functions.
if [ "$titling" != 0 ]; then titling="true" ; else unset titling ; fi
# Enable (*) or disable (0) tabbed terminal mode for access functions.
if [ "$tabbed" != 0 ]; then tabbed="true" ; else unset tabbed ; fi
# Enable (*) or disable (0) color state output.
if [ "$color" != 0 ]; then color="true" ; else unset color ; fi
# Enable (*) or disable (0) ssh agent (ssh-agent).
if [ "$agent" != 0 ]; then agent="$agent" ; else unset agent ; fi
# Enable (*) or disable (0) privilege escalation for various routines.
# This enables the (optional) use of sudo or runas when running commands
# like nmap on unix-likes or when running tell --admin on localhost.
if [ "$privy" != 0 ]; then privy="$privesc" ; else unset privy ; fi
# Enable (*) or disable (0) debug logging.
# This makes all functions within session write about how they are
# called including arguments and some environment variables.
# Notice that we check for "--debug" on cmdline too. This is cheaper
# than calling parseParameters at this level.
if [ "$debug" != 0 ]; then debug="true" ; elif [[ "$*" =~ "--debug" ]]; then debug="true" ; else unset debug; fi
# Detect and initialize private key usage.
handleSshPrivateKeys
# Main case statement.
main="$1"
case "$main" in
addconf)
[ "$2" ] || { printUsageText ; exit 1 ; }
type="none"
name="$2"
osmt="none"
acmt="none"
exmt="none"
user="none"
admin="none"
addr="none"
vrmt="none"
port="none"
host="none"
svmt="none"
optionals_all="name osmt acmt exmt user admin addr"
optionals_hosts="vrmt vmhome vmdata vmuser vmpass vmport vmshared"
optionals_guests="host"
optionals_services="svmt port svstatus svstart svstop"
optionals_groups="members mode"
shift 2
mandatories="type" optionals="debug $optionals_all $optionals_hosts $optionals_guests $optionals_services $optionals_groups" silence="true" parseParameters "$@"
if [ "$type" = "host" -a "$vrmt" = "none" ]; then
mandatories="type osmt acmt exmt user admin addr" optionals="debug vrmt" parseParameters "$@"
tokenValidator "type,name,osmt,acmt,exmt,user,admin,addr,vrmt" || exit 1
addConf "$type $name($osmt,$acmt,$exmt,$user,$admin,$addr,$vrmt)" || exit 1
elif [ "$type" = "host" -a "$vrmt" != "none" ]; then
mandatories="type osmt acmt exmt user admin addr vrmt vmhome vmdata" optionals="debug" parseParameters "$@"
tokenValidator "type,name,osmt,acmt,exmt,user,admin,addr,vrmt" || exit 1
addConf "$type $name($osmt,$acmt,$exmt,$user,$admin,$addr,$vrmt)" || exit 1
opts="$usrcfd/sys/$name/options.env"
mkdir -p "$usrcfd/sys/$name"
printf "vmhome='$vmhome'\n" > "$opts"
printf "vmdata='$vmdata'\n" >> "$opts"
elif [ "$type" = "guest" ]; then
mandatories="type osmt acmt exmt user admin addr host" optionals="debug" parseParameters "$@"
tokenValidator "type,name,osmt,acmt,exmt,user,admin,addr,host" || exit 1
addConf "$type $name($osmt,$acmt,$exmt,$user,$admin,$addr,$host)" || exit 1
elif [ "$type" = "service" -a "$svmt" = "none" ]; then
mandatories="type svmt addr port host" optionals="debug acmt exmt user" parseParameters "$@"
tokenValidator "type,name,svmt,acmt,exmt,user,addr,port,host" || exit 1
addConf "$type $name($svmt,$acmt,$exmt,$user,$addr,$port,$host)" || exit 1
elif [ "$type" = "service" -a "$svmt" != "none" ]; then
mandatories="type svmt host svstatus svstart svstop" optionals="debug acmt exmt user addr port" parseParameters "$@"
tokenValidator "type,name,svmt,acmt,exmt,user,addr,port,host" || exit 1
addConf "$type $name($svmt,$acmt,$exmt,$user,$addr,$port,$host)" || exit 1
opts="$usrcfd/sys/$name/options.env"
mkdir -p "$usrcfd/sys/$name"
printf "svstatus='$svstatus'\n" > "$opts"
printf "svstart='$svstart'\n" >> "$opts"
printf "svstop='$svstop'\n" >> "$opts"
elif [ "$type" = "group" ]; then
mandatories="type members" optionals="debug mode" parseParameters "$@"
# Write out mode if it was given on the commandline and not default, always when opts exist.
opts="$usrcfd/sys/$name/options.env"
if [ "$mode" != "$defaultmode" -o -e "$opts" ]; then
mkdir -p "$usrcfd/sys/$name"
printf "mode='$mode'\n" > "$opts"
fi
# Make sure no double entities are in members.
ORIG_IFS="$IFS"
IFS=","
for member in $members ; do
if ! printf "$had\n" | grep -q "$member"; then
uniquemembers="$uniquemembers,$member"
fi
had="$member,$had"
done
IFS="$ORIG_IFS"
members="$(printf "$uniquemembers\n" | sed '/^$/d' | sed -e 's|,$||' -e 's|^,||')"
tokenValidator "type,name,members,mode" || exit 1
addConf "$type $name($members)" || exit 1
else
reportError "Invalid type $type specified"
exit 1
fi
exit $?
;;
modconf)
[ "$2" ] || { printUsageText ; exit 1 ; }
type="none"
name="$2"
osmt="none"
acmt="none"
exmt="none"
user="none"
admin="none"
addr="none"
vrmt="none"
port="none"
host="none"
svmt="none"
oldname="$name"
optionals_all="name osmt acmt exmt user admin addr"
optionals_hosts="vrmt vmhome vmdata vmuser vmpass vmport vmshared"
optionals_guests="host"
optionals_services="svmt port svstatus svstart svstop"
optionals_groups="members mode"
shift 2
mandatories="type" optionals="debug $optionals_all $optionals_hosts $optionals_guests $optionals_services $optionals_groups" silence="true" parseParameters "$@"
# Initialize newentry, read oldentry, set entry to oldentry to speed up tokenReader.
entry="$(entryReader "$oldname" "$usrcff")"
oldentry="$entry"
newentry=""
if [ ! "$oldentry" ]; then
reportError "Entry $oldname does not exist in $usrcff"
exit 1
fi
# Set oldtype so changing type works.
oldtype="$(tokenReader printVals "$oldname" type)"
reportDebug "Got old type: $oldtype"
# Call parse* to check for validity of tokens.
reportDebug "Setting tokens according to old entry"
if [ "$oldtype" = "host" ]; then
tokenReader setVars "$oldname" "osmt,acmt,exmt,user,admin,addr,vrmt"
elif [ "$oldtype" = "guest" ]; then
tokenReader setVars "$oldname" "osmt,acmt,exmt,user,admin,addr,host"
elif [ "$oldtype" = "service" ]; then
tokenReader setVars "$oldname" "svmt,acmt,exmt,user,addr,port,host"
elif [ "$oldtype" = "group" ]; then
tokenReader setVars "$oldname" "members"
else
reportError "Unknown type $oldtype in entry $oldentry"
exit 1
fi
# FIXME: Find a way to avoid running this twice.
mandatories="type" optionals="debug $optionals_all $optionals_hosts $optionals_guests $optionals_services $optionals_groups" silence="true" parseParameters "$@"
# If we're a host and vrmt was passed and not none, check for extra options.
if [ "$type" = "host" ]; then
# Set oldvrmt so changing vrmt works.
oldvrmt="$(tokenReader printVals "$oldname" vrmt)"
reportDebug "Got old vrmt: $oldvrmt"
if [ ! "$vrmt" ]; then vrmt="$oldvrmt" ; fi
if [ "$vrmt" != "none" ]; then
optional_extras="$optionals_hosts"
opts="$usrcfd/sys/$name/options.env"
oldopts="$usrcfd/sys/$oldname/options.env"
# Check for old options first if name changed, source if available
if [ "$name" != "$oldname" -a -e "$oldopts" ]; then
reportDebug "Found old option file: $oldopts"
for option in vmhome vmdata; do
if [ ! "$(cat "$oldopts" | grep -v "^#" | grep "$option=")" ]; then
reportDebug "Option $option not currently set, making mandatory"
mandatory_extras="$option $mandatory_extras"
fi
done
. "$oldopts"
# Name did not change, check for options, source if available
elif [ -e "$opts" ]; then
reportDebug "Found option file: $opts"
for option in vmhome vmdata; do
if [ ! "$(cat "$opts" | grep -v "^#" | grep "$option=")" ]; then
reportDebug "Option $option not currently set, making mandatory"
mandatory_extras="$option $mandatory_extras"
fi
done
. "$opts"
else
reportDebug "No option file found"
mkdir -p "$usrcfd/sys/$name"
mandatory_extras="vmhome vmdata"
fi
fi
fi
# If we're a service and svmt was passed and not none, check for extra options.
if [ "$type" = "service" ]; then
# Set oldsvmt so changing svmt works.
oldsvmt="$(tokenReader printVals "$oldname" svmt)"
reportDebug "Got old svmt: $oldsvmt"
if [ ! "$svmt" ]; then svmt="$oldsvmt" ; fi
if [ "$svmt" != "none" ]; then
opts="$usrcfd/sys/$name/options.env"
oldopts="$usrcfd/sys/$oldname/options.env"
# Check for old options first if name changed, source if available
if [ "$name" != "$oldname" -a -e "$oldopts" ]; then
reportDebug "Found old option file: $oldopts"
for option in svstatus svstart svstop; do
if [ ! "$(cat "$oldopts" | grep -v "^#" | grep "$option=")" ]; then
reportDebug "Option $option not currently set, making mandatory"
mandatory_extras="$option $mandatory_extras"
else
reportDebug "Option $option is already set, making optional"
optional_extras="$option $optional_extras"
fi
done
. "$oldopts"
# Name did not change, check for options, source if available
elif [ -e "$opts" ]; then
reportDebug "Found option file: $opts"
for option in svstatus svstart svstop; do
if [ ! "$(cat "$opts" | grep -v "^#" | grep "$option=")" ]; then
reportDebug "Option $option not currently set, making mandatory"
mandatory_extras="$option $mandatory_extras"
else
reportDebug "Option $option is already set, making optional"
optional_extras="$option $optional_extras"
fi
done
. "$opts"
else
reportDebug "No option file found"
mkdir -p "$usrcfd/sys/$name"
mandatory_extras="svstatus svstart svstop"
fi
fi
fi
# If we're a group, check for extra options.
if [ "$type" = "group" ]; then
opts="$usrcfd/sys/$name/options.env"
oldopts="$usrcfd/sys/$oldname/options.env"
# Check for old options first if name changed, source if available
if [ "$name" != "$oldname" -a -e "$oldopts" ]; then
reportDebug "Found old option file: $oldopts"
for option in mode; do
if [ ! "$(cat "$oldopts" | grep -v "^#" | grep "$option=")" ]; then
reportDebug "Option $option not currently set, making optional"
optional_extras="$option $optional_extras"
else
reportDebug "Option $option is already set, making optional"
optional_extras="$option $optional_extras"
fi
done
. "$oldopts"
# Name did not change, check for options, source if available
elif [ -e "$opts" ]; then
reportDebug "Found option file: $opts"
for option in mode; do
if [ ! "$(cat "$opts" | grep -v "^#" | grep "$option=")" ]; then
reportDebug "Option $option not currently set, making optional"
optional_extras="$option $optional_extras"
else
reportDebug "Option $option is already set, making optional"
optional_extras="$option $optional_extras"
fi
done
. "$opts"
else
reportDebug "No option file found"
mkdir -p "$usrcfd/sys/$name"
optional_extras="mode"
fi
fi
# If name was changed, make sure all references to this entity are updated also.
if [ ! "$name" = "$oldname" ]; then
reportDebug "Name changed from $oldname to $name, updating all references"
cat "$usrcff" \
| sed "s|,$oldname)|,$name)|g" \
| sed "s|($oldname,|($name,|g" \
| sed "s|,$oldname,|,$name,|g" \
| sed "s|($oldname)|($name)|g" \
> "$usrcfd/tmp/session.conf.tmp"
mv "$usrcfd/tmp/session.conf.tmp" "$usrcff"
if [ -e "$usrcfd/sys/$oldname" ]; then
reportInfo "Moving entry specific settings in $usrcfd/sys from $oldname to $name"
rm -f "$usrcfd/sys/$name/options.env" "$usrcfd/sys/$name/user.pwd" "$usrcfd/sys/$name/admin.pwd" 2> /dev/null
rmdir "$usrcfd/sys/$name" 2> /dev/null
mv "$usrcfd/sys/$oldname" "$usrcfd/sys/$name"
fi
fi
# Handle a HOST modification.
if [ "$type" = "host" ]; then
reportDebug "Target type: $type"
if [ "$oldtype" = "guest" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type vrmt $mandatory_extras" optionals="debug name osmt acmt exmt user admin addr $optional_extras" parseParameters "$@"
elif [ "$oldtype" = "service" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type osmt vrmt admin $mandatory_extras" optionals="debug name acmt exmt user addr $optional_extras" parseParameters "$@"
elif [ "$oldtype" = "group" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type osmt acmt exmt user admin addr vrmt $mandatory_extras" optionals="debug name $optional_extras" parseParameters "$@"
else
reportDebug "Setting default mandatories and optionals for $type"
mandatories="type $mandatory_extras" optionals="debug name osmt acmt exmt user admin addr $optional_extras" parseParameters "$@"
fi
# Write out optionals_host when vrmt is not none.
if [ ! "$vrmt" = "none" ]; then
printf "# options.env for $name. generated by modconf. do not edit.\n" > "$opts"
if [ "$vmhome" ]; then reportDebug "Setting vmhome=\'$vmhome\' in $opts"; printf "vmhome='$vmhome'\n" >> "$opts"; fi
if [ "$vmdata" ]; then reportDebug "Setting vmdata=\'$vmdata\' in $opts"; printf "vmdata='$vmdata'\n" >> "$opts"; fi
if [ "$vmuser" ]; then reportDebug "Setting vmuser=\'$vmuser\' in $opts"; printf "vmuser='$vmuser'\n" >> "$opts"; fi
if [ "$vmpass" ]; then reportDebug "Setting vmpass=\'*******\' in $opts"; printf "vmpass='$vmpass'\n" >> "$opts"; fi
if [ "$vmport" ]; then reportDebug "Setting vmport=\'$vmport\' in $opts"; printf "vmport='$vmport'\n" >> "$opts"; fi
if [ "$vmshared" ]; then reportDebug "Setting vmshared=\'$vmshared\' in $opts"; printf "vmshared='$vmshared'\n" >> "$opts"; fi
fi
# Rewrite newentry with changes from above.
newentry="$type $name($osmt,$acmt,$exmt,$user,$admin,$addr,$vrmt)"
reportDebug "Old entry is: $oldentry"
reportDebug "New entry is: $newentry"
tokenValidator "type,name,osmt,acmt,exmt,user,admin,addr,vrmt" || exit 1
# If validation succeeded and newentry is not equal to oldentry, add the entry.
if [ "$?" != 0 ]; then
reportError "Entry validation failed, not writing new entry"
exit 1
elif [ "$newentry" = "$oldentry" ]; then
reportInfo "No changes in entry, not writing new entry"
exit 0
else
delConf "$oldname"
addConf "$newentry"
fi
# Handle a GUEST modification.
elif [ "$type" = "guest" ]; then
reportDebug "Target type: $type"
if [ "$oldtype" = "host" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type host" optionals="debug name osmt acmt exmt user admin addr" parseParameters "$@"
elif [ "$oldtype" = "group" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type osmt acmt exmt user admin addr host" optionals="debug name" parseParameters "$@"
elif [ "$oldtype" = "service" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type osmt admin host" optionals="debug name acmt exmt user addr" parseParameters "$@"
else
reportDebug "Setting default mandatories and optionals for $type"
mandatories="type" optionals="debug name osmt acmt exmt user admin addr host" parseParameters "$@"
fi
# Rewrite newentry with changes from above.
newentry="$type $name($osmt,$acmt,$exmt,$user,$admin,$addr,$host)"
reportDebug "Old entry is: $oldentry"
reportDebug "New entry is: $newentry"
tokenValidator "type,name,osmt,acmt,exmt,user,admin,addr,host" || exit 1
# If validation succeeded and newentry is not equal to oldentry, add the entry.
if [ "$?" != 0 ]; then
reportError "Entry validation failed, not writing new entry"
exit 1
elif [ "$newentry" = "$oldentry" ]; then
reportInfo "No changes in entry, not writing new entry"
exit 0
else
delConf "$oldname"
addConf "$newentry"
fi
# Handle a SERVICE modification.
elif [ "$type" = "service" ]; then
reportDebug "Target type: $type"
if [ "$oldtype" = "host" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type svmt port host $mandatory_extras" optionals="debug name acmt exmt user addr $optional_extras" parseParameters "$@"
elif [ "$oldtype" = "guest" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type svmt port $mandatory_extras" optionals="debug name acmt exmt user addr host $optional_extras" parseParameters "$@"
elif [ "$oldtype" = "group" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type svmt acmt exmt user addr port host $mandatory_extras" optionals="debug name $optional_extras" parseParameters "$@"
else
reportDebug "Setting default mandatories and optionals for $type"
mandatories="type $mandatory_extras" optionals="debug name svmt acmt exmt user addr port host $optional_extras" parseParameters "$@"
fi
# Write out svstatus, svstart and svstop when svmt is not none.
if [ ! "$svmt" = "none" ]; then
reportInfo "Setting svstatus, svstart and svstop"
printf "svstatus='$svstatus'\n" > "$opts"
printf "svstart='$svstart'\n" >> "$opts"
printf "svstop='$svstop'\n" >> "$opts"
fi
# Rewrite newentry with changes from above.
newentry="$type $name($svmt,$acmt,$exmt,$user,$addr,$port,$host)"
reportDebug "Old entry is: $oldentry"
reportDebug "New entry is: $newentry"
tokenValidator "type,name,svmt,acmt,exmt,user,addr,port,host" || exit 1
# If validation succeeded and newentry is not equal to oldentry, add the entry.
if [ "$?" != 0 ]; then
reportError "Entry validation failed, not writing new entry"
exit 1
elif [ "$newentry" = "$oldentry" ]; then
reportInfo "No changes in entry, not writing new entry"
exit 0
else
delConf "$oldname"
addConf "$newentry"
fi
# Handle a GROUP modification.
elif [ "$type" = "group" ]; then
reportDebug "Target type: $type"
if [ "$oldtype" != "group" ]; then
reportDebug "Rewriting mandatories and optionals to reflect a $type since old type was $oldtype"
mandatories="type members" optionals="debug name mode" parseParameters "$@"
else
reportDebug "Setting default mandatories and optionals for $type"
mandatories="type" optionals="debug name members mode" parseParameters "$@"
fi
# Write out mode if it was given on the commandline and not default, always when opts exist.
opts="$usrcfd/sys/$name/options.env"
if [ "$mode" != "$defaultmode" -o -e "$opts" ]; then
reportInfo "Setting mode"
mkdir -p "$usrcfd/sys/$name"
printf "mode='$mode'\n" > "$opts"
fi
# Make sure no double entities are in members.
ORIG_IFS="$IFS"
IFS=","
for member in $members ; do
if ! printf "$had\n" | grep -q "$member"; then
uniquemembers="$uniquemembers,$member"
fi
had="$member,$had"
done
IFS="$ORIG_IFS"
members="$(printf "$uniquemembers\n" | sed '/^$/d' | sed -e 's|,$||' -e 's|^,||')"
# Rewrite newentry with changes from above.
newentry="$type $name($members)"
reportDebug "Old entry is: $oldentry"
reportDebug "New entry is: $newentry"
tokenValidator "type,name,members,mode" || exit 1
# If validation succeeded and newentry is not equal to oldentry, add the entry.
if [ "$?" != 0 ]; then
reportError "Entry validation failed, not writing new entry"
exit 1
elif [ "$newentry" = "$oldentry" ]; then
reportInfo "No changes in entry, not writing new entry"
exit 0
else
delConf "$oldname"
addConf "$newentry"
fi
else
reportError "Invalid type $type specified"
exit 1
fi
exit $?
;;
delconf)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug" silence="true" parseParameters "$@"
delConf "$name"
if [ -e "$usrcfd/sys/$name" ]; then
reportInfo "Removing entry specific settings in $usrcfd/sys"
rm -f "$usrcfd/sys/$name/options.env" "$usrcfd/sys/$name/user.pwd" "$usrcfd/sys/$name/admin.pwd" 2> /dev/null
rmdir "$usrcfd/sys/$name" 2> /dev/null
fi
exit $?
;;
discover)
[ "$2" ] || { printUsageText ; exit 1 ; }
input="$2"
shift 2
optionals="debug" silence="true" parseParameters "$@"
discoveryHelper "$input"
exit $?
;;
check|state|status)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug resilient mode" silence="true" parseParameters "$@"
mapEntryPoint "$name" "checkState,printState"
exit $?
;;
detail)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug resilient nostate nocheck nostatus" silence="true" parseParameters "$@"
parseEntry "$name" || exit 1
if [ "$type" -a "$type" != "group" ]; then
if [ "$nostate" -o "$nocheck" -o "$nostatus" ]; then
reportDebug "Not checking current state for $name"
else
checkState
fi
fi
printState "$name" long
exit $?
;;
start)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug resilient mode" silence="true" parseParameters "$@"
mapEntryPoint "$name" "checkState,startEntity,printState"
exit $?
;;
stop)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug resilient mode" silence="true" parseParameters "$@"
mapEntryPoint "$name" "checkState,stopEntity,printState"
exit $?
;;
restart|reboot)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug resilient mode" silence="true" parseParameters "$@"
mapEntryPoint "$name" "checkState,restartEntity,printState"
exit $?
;;
create)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
mandatories="numvcpu memsize dsksize guestos" optionals="debug resilient desc" parseParameters "$@"
if [ "$numvcpu" -lt 1 ]; then
reportError "Not enough cpus specified: $numvcpu"
exit 1
elif [ "$numvcpu" -gt 4 ]; then
reportError "Too many cpus specified: $numvcpu"
exit 1
fi
if [ "$memsize" -lt 32 ]; then
reportError "Less than 32 megabytes specified: $memsize"
exit 1
elif [ "$memsize" -gt 16384 ]; then
reportError "More than 16384 megabytes specified: $memsize"
exit 1
fi
if [ "$dsksize" -lt 1 ]; then
reportError "Less than 1 gigabytes specified: $dsksize"
exit 1
elif [ "$dsksize" -gt 1024 ]; then
reportError "More than 1 terabyte specified: $dsksize"
exit 1
fi
mapEntryPoint "$name" "checkState,createEntity,printState"
exit $?
;;
destroy)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug resilient" silence="true" parseParameters "$@"
mapEntryPoint "$name" "checkState,destroyEntity,printState"
exit $?
;;
access)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug user admin service tabbed console command parent mode resilient" silence="true" parseParameters "$@"
# Set parent mode, for guests and services.
if [ "$tabbed" ]; then
tabbed="true"
fi
# Set parent mode, for guests and services.
if [ "$parent" ]; then
parent="true"
fi
# Set console mode, for services.
if [ "$console" ]; then
console="true"
fi
if [ "$admin" ]; then
xsastype="admin"
elif [ "$service" ]; then
xsastype="service"
else
xsastype="user"
fi
mapEntryPoint "$name" "checkState,accessEntity"
exit $?
;;
tell)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
mandatories="command" optionals="debug user admin service console parent mode resilient" silence="true" parseParameters "$@"
if [ "$parent" ]; then
parent="true"
fi
if [ "$admin" ]; then
runastype="admin"
elif [ "$service" ]; then
runastype="service"
else
runastype="user"
fi
mapEntryPoint "$name" "checkState,runAs"
exit $?
;;
send)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
mandatories="source target" optionals="debug user admin service console parent mode resilient" silence="true" parseParameters "$@"
if [ "$parent" ]; then
parent="true"
fi
if [ "$admin" ]; then
sendastype="admin"
elif [ "$service" ]; then
sendastype="service"
else
sendastype="user"
fi
mapEntryPoint "$name" "checkState,sendAs"
exit $?
;;
key)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug" silence="true" parseParameters "$@"
mapEntryPoint "$name" "checkState,credAs"
exit $?
;;
reinit)
reportInfo "Removing tools.required and tools.found. Rerun session to redetect"
rm -f "$usrcfd/cfg/tools.required" "$usrcfd/cfg/tools.found"
exit $?
;;
list)
[ "$2" ] || { printUsageText ; exit 1 ; }
name="$2"
shift 2
optionals="debug verbose" silence="true" parseParameters "$@"
listHelper "$name"
exit $?
;;
version)
printVersion
exit $?
;;
help|--help)
printUsageText
exit $?
;;
*)
printUsageText
exit 1
;;
esac