1##################################################################
2# SecPanel
3#
4# Exporting profiles related code
5##################################################################
6
7proc export {mode form} {
8    global sites env widget configs
9
10    # source def-files
11    if {$mode == "def"} {
12	if {[$widget(defsites) index end] > 0} {
13	    source "$env(HOME)/.secpanel/profiles/default.profile"
14	    set actconn [$widget(defsites) get active]
15	    set host [lindex [split $sites($actconn) #] 0]
16
17	    set userentry [lindex [split $sites($actconn) #] 1]
18
19	    if {$userentry == "<ASKFORUSER>"} {
20		set user [askforuser]
21		if {$user == "#####"} {
22		    return
23		}
24	    } else {
25		set user $userentry
26	    }
27
28	    set title $actconn
29	} else {
30	    showmessage "No conncections available, please use \"New\"" ""
31	    return
32	}
33    }
34
35    # source spec-files
36    if {$mode == "spec"} {
37	if {[$widget(specsites) index end] > 0} {
38	    set actconn [retprof [$widget(specsites) get active]]
39	    source "$env(HOME)/.secpanel/profiles/$actconn.profile"
40	} else {
41	    showmessage "No conncections available, please use \"New\"" ""
42	    return
43	}
44    }
45
46    # forwardings
47    if {[array size lfs] > 0} {
48	foreach lf [array names lfs] {
49	    if {[regsub {<TARGET-HOST>} [lindex [split $lf :] 1] $host th]} {
50		append lf_tag  " -L [lindex [split $lf :] 0]:$th:[lindex [split $lf :] 2] "
51	    } else {
52		append lf_tag  " -L [lindex [split $lf :] 0]:[lindex [split $lf :] 1]:[lindex [split $lf :] 2] "
53	    }
54	}
55    } else {
56	set lf_tag " "
57    }
58
59    set localhost [info hostname]
60    if {[array size rfs] > 0} {
61	foreach rf [array names rfs] {
62	    if {[regsub {<LOCAL-HOST>} [lindex [split $rf :] 1] $localhost lh]} {
63		append rf_tag  " -R [lindex [split $rf :] 0]:$lh:[lindex [split $rf :] 2] "
64	    } else {
65		append rf_tag  " -R [lindex [split $rf :] 0]:[lindex [split $rf :] 1]:[lindex [split $rf :] 2] "
66	    }
67	}
68    } else {
69	set rf_tag " "
70    }
71
72    if {$user != ""} {
73	set user_tag "-l $user "
74    } else {
75	set user_tag " "
76    }
77
78    if {$port == 22 || $port == ""} {
79	set port_tag " "
80    } else {
81	set port_tag "-p $port "
82    }
83
84    if {$algo != "default" || $algo == ""} {
85	set algo_tag "-c $algo "
86    } else {
87	set algo_tag " "
88    }
89
90    if {$identity != ""} {
91	set ident_tag "-i $identity "
92    } else {
93	set ident_tag " "
94    }
95
96    if {$command != ""} {
97	set command_tag "$command"
98    } else {
99	set command_tag ""
100    }
101
102    if $compress {
103	# openssh
104	if {$configs(sshver) == "OpenSSH"} {
105	    set compressval_tag "-o \'CompressionLevel [set compressval]\' "
106	} else {
107	    set compressval_tag "-o CompressionLevel=$compressval "
108	}
109    } else {
110	set compressval_tag " "
111    }
112
113    array set bools {
114	"agentforward" "-a" \
115		"x11forward" "-x" \
116		"nopriv" "-P"  "verbose" "-v" \
117		"quiet" "-q" \
118		"fork" "-f" \
119		"gateway" "-g" \
120		"compress" "-C"
121    }
122
123    # foreach f [array names $bools]
124    foreach f {agentforward x11forward nopriv verbose \
125	    quiet fork gateway compress} {
126	if [set $f] {
127	    set [set f]_tag "$bools($f) "
128	} else {
129	    set [set f]_tag " "
130	}
131    }
132
133    # foreach f [array names $bools]
134    foreach f {agentforward x11forward nopriv verbose \
135	    quiet fork gateway compress} {
136	if [set $f] {
137	    set [set f]_tagssh "yes"
138	} else {
139	    set [set f]_tagssh "no"
140	}
141    }
142
143    set sshvertag "-[set sshverconnect] "
144    set ipvertag "-[set ipverconnect] "
145
146    # openssh
147    if {! $x11forward} {
148	if {$configs(sshver) == "OpenSSH"} {
149	    set x11forward_tag "-X "
150	}
151    }
152
153    set connstring "$configs(sshbin) $user_tag \
154	    $agentforward_tag $x11forward_tag $sshvertag $ipvertag $port_tag $algo_tag \
155	    $ident_tag $nopriv_tag $verbose_tag $quiet_tag \
156	    $fork_tag $gateway_tag $compress_tag $compressval_tag \
157	    $lf_tag $rf_tag $host $command_tag"
158
159
160    switch -exact $form {
161	"ssh" {
162	    puts "Host $host"
163	    puts "Compression $compress_tagssh"
164	    puts "CompressionLevel $compressval_tag"
165	    puts "ForwardAgent $agentforward_tagssh"
166	    puts "ForwardX11 $x11forward_tagssh"
167	    puts "GatewayPorts $gateway_tagssh"
168	    puts "HostName $host"
169	    puts "IdentityFile $ident_tag"
170	    puts "LocalForward"
171	    puts "Port $port_tag"
172	    puts "Protocol $sshvertag"
173	    puts "RemoteForward"
174	    puts "StrictHostKeyChecking"
175	    puts "User $user_tag"
176	}
177	"sh" {
178	    puts "#!/bin/sh"
179	    puts "#\n#Exported by SecPanel\n#"
180	    puts "$connstring"
181	}
182    }
183}
184