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
|
# File 'lib/ovpnmcgen.rb', line 10
def generate(inputs = {})
identifier = inputs[:identifier] || inputs[:host].split('.').reverse!.join('.')
port = inputs[:port] || 1194
certUUID = inputs[:cert_uuid] || SecureRandom.uuid.chomp.upcase
vpnUUID = inputs[:vpn_uuid] || SecureRandom.uuid.chomp.upcase
plistUUID = inputs[:profile_uuid] || SecureRandom.uuid.chomp.upcase
user, device, domain, host, proto, enableVOD = inputs[:user], inputs[:device], inputs[:host], inputs[:host], inputs[:proto], inputs[:enableVOD]
p12pass = inputs[:p12pass] || ''
trusted_ssids = inputs[:trusted_ssids] || false
untrusted_ssids = inputs[:untrusted_ssids] || false
remotes = inputs[:remotes] || false
vodDomains = inputs[:domains] || false
vpnName = inputs[:vpn_name] || "#{host}/VoD"
plistDescription = "OpenVPN Configuration Payload for #{user}-#{device}@#{host}"
trusted_ssids = Array(trusted_ssids) if trusted_ssids
untrusted_ssids = Array(untrusted_ssids) if untrusted_ssids
remotes = Array(remotes) if remotes
vodDomains = Array(vodDomains) if vodDomains
begin
ca_cert = File.readlines(inputs[:cafile]).map { |x| x.chomp }.join('\n')
rescue Errno::ENOENT
puts "CA file not found: #{inputs[:cafile]}!"
exit
end
begin
tls_crypt = File.readlines(inputs[:tlscryptfile]).map { |x| x.chomp }.join('\n')
rescue Errno::ENOENT
puts "TLS crypt file not found: #{inputs[:tlscryptfile]}!"
exit
end if inputs[:tlscryptfile]
begin
tls_auth = File.readlines(inputs[:tafile]).map { |x| x.chomp }.join('\n')
rescue Errno::ENOENT
puts "TLS file not found: #{inputs[:tafile]}!"
exit
end if inputs[:tafile]
begin
cert_file = File.readlines(inputs[:cert]).map { |x| x.chomp }.join('\n')
rescue Errno::ENOENT
puts "Cert file not found: #{inputs[:cert]}!"
exit
end if inputs[:cert]
begin
key_file = File.readlines(inputs[:key]).map { |x| x.chomp }.join('\n')
rescue Errno::ENOENT
puts "Key file not found: #{inputs[:key]}!"
exit
end if inputs[:key]
begin
p12file = Base64.encode64(File.read(inputs[:p12file]))
rescue Errno::ENOENT
puts "PKCS#12 file not found: #{inputs[:p12file]}!"
exit
end if inputs[:p12file]
unless inputs[:ovpnconfigfile].nil?
ovpnconfighash = Ovpnmcgen.getOVPNVendorConfigHash(inputs[:ovpnconfigfile])
plistDescription = "#{plistDescription}. Includes custom OpenVPN directives #{ovpnconfighash.to_s.gsub('"', '').gsub('=>', '=')}."
else ovpnconfighash = {
'client' => 'NOARGS',
'comp-lzo' => 'NOARGS',
'dev' => 'tun',
'remote-cert-tls' => 'server'
}
end
if remotes
ovpnconfighash['remote.1'] = "#{host} #{port} #{proto}"
remotes.each_with_index do |r, i|
ovpnconfighash["remote.#{i+2}"] = r
end
else
ovpnconfighash['remote'] = "#{host} #{port} #{proto}"
end
ovpnconfighash['ca'] = ca_cert
ovpnconfighash['tls-auth'] = tls_auth if inputs[:tafile]
ovpnconfighash['key-direction'] = '1' if inputs[:tafile]
ovpnconfighash['tls-crypt'] = tls_crypt if inputs[:tlscryptfile]
ovpnconfighash['cert'] = cert_file if inputs[:cert]
ovpnconfighash['key'] = key_file if inputs[:key]
ovpnconfighash['vpn-on-demand'] = '0' unless enableVOD
vpnOnDemandRules = Array.new
vodTrusted = { 'InterfaceTypeMatch' => 'WiFi',
'SSIDMatch' => trusted_ssids,
'Action' => 'Disconnect'
}
vodUntrusted = { 'InterfaceTypeMatch' => 'WiFi',
'SSIDMatch' => untrusted_ssids,
'Action' => 'Connect'
}
vodWifiOnly = { 'InterfaceTypeMatch' => 'WiFi',
'Action' => case inputs[:security_level]
when 'paranoid', 'high'
'Connect'
else 'Ignore'
end
}
vodDomainOnlyActionParam = {
'Domains' => vodDomains,
'DomainAction' => 'ConnectIfNeeded'
}
vodDomainOnlyActionParam['RequiredURLStringProbe'] = inputs[:domain_probe_url] if inputs[:domain_probe_url]
vodDomainOnly = { 'Action' => 'EvaluateConnection',
'ActionParameters' => [vodDomainOnlyActionParam]
}
vodCellularOnly = { 'InterfaceTypeMatch' => 'Cellular',
'Action' => case inputs[:security_level]
when 'paranoid'
'Connect'
else 'Ignore'
end
}
vodDefault = { 'Action' => 'Ignore'
}
vodTrusted['URLStringProbe'] =
vodUntrusted['URLStringProbe'] =
vodWifiOnly['URLStringProbe'] =
vodDomainOnly['URLStringProbe'] =
vodCellularOnly['URLStringProbe'] =
vodDefault['URLStringProbe'] =
inputs[:url_probe] if inputs[:url_probe]
vodTrusted['URLStringProbe'] = inputs[:trusted_ssids_probe_url] if inputs[:trusted_ssids_probe_url]
vpnOnDemandRules << vodTrusted if trusted_ssids
vpnOnDemandRules << vodUntrusted if untrusted_ssids
vpnOnDemandRules << vodWifiOnly
vpnOnDemandRules << vodDomainOnly if vodDomains
vpnOnDemandRules << vodCellularOnly << vodDefault
vpnOnDemandRules << { 'Action' => 'Ignore'
} if inputs[:url_probe]
cert = {
'Password' => p12pass,
'PayloadCertificateFileName' => "#{user}-#{device}.p12",
'PayloadContent' => StringData.new(p12file),
'PayloadDescription' => 'Provides device authentication (certificate or identity).',
'PayloadDisplayName' => "#{user}-#{device}.p12",
'PayloadIdentifier' => (inputs[:cert_uuid]) ? "com.apple.vpn.managed.#{certUUID}" : "#{identifier}.#{user}-#{device}.credential",
'PayloadOrganization' => domain,
'PayloadType' => 'com.apple.security.pkcs12',
'PayloadUUID' => certUUID,
'PayloadVersion' => 1
} if p12file
vpn = {
'PayloadDescription' => "Configures VPN settings, including authentication.",
'PayloadDisplayName' => "VPN (#{host}/VoD)",
'PayloadIdentifier' => (inputs[:vpn_uuid]) ? "com.apple.vpn.managed.#{certUUID}" : "#{identifier}.#{user}-#{device}.vpnconfig",
'PayloadOrganization' => domain,
'PayloadType' => 'com.apple.vpn.managed',
'PayloadUUID' => vpnUUID,
'PayloadVersion' => 1,
'UserDefinedName' => vpnName,
'VPN' => {
'AuthenticationMethod' => 'Certificate',
'OnDemandEnabled' => (enableVOD)? 1 : 0,
'OnDemandRules' => vpnOnDemandRules,
'PayloadCertificateUUID' => certUUID,
'RemoteAddress' => 'DEFAULT'
},
'VPNSubType' => (inputs[:v12compat])? 'net.openvpn.connect.app' : 'net.openvpn.OpenVPN-Connect.vpnplugin',
'VPNType' => 'VPN',
'VendorConfig' => ovpnconfighash
}
unless p12file
vpn['VPN']['AuthName'] = "#{user}-#{device}"
vpn['VPN']['AuthenticationMethod'] = 'Password'
vpn['VPN'].delete('PayloadCertificateUUID')
end
if inputs[:idle_timer]
vpn['VPN']['DisconnectOnIdle'] = 1
vpn['VPN']['DisconnectOnIdleTimer'] = inputs[:idle_timer]
end
plistPayloadContent = [vpn]
plistPayloadContent << cert if p12file
plist = {
'PayloadDescription' => plistDescription,
'PayloadDisplayName' => "#{host} OpenVPN #{user}@#{device}",
'PayloadIdentifier' => (inputs[:profile_uuid]) ? "com.apple.vpn.managed.#{plistUUID}" : "#{identifier}.#{user}-#{device}",
'PayloadOrganization' => domain,
'PayloadRemovalDisallowed' => false,
'PayloadType' => 'Configuration',
'PayloadUUID' => plistUUID,
'PayloadVersion' => 1,
'PayloadContent' => plistPayloadContent
}
return plist.to_plist
end
|