-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparse.go
442 lines (410 loc) · 14.6 KB
/
parse.go
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
// Go program - Autoconfiguration of wired and wireless networks on windows 7/8/10
package main
import (
"bytes"
"fmt"
"log"
"os"
"syscall"
"encoding/hex"
"io/ioutil"
"os/exec"
"path/filepath"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/lxn/win"
"github.com/nicksnyder/go-i18n/i18n"
"howett.net/plist"
)
const PROGRAM_NAME = "PacketFence Provisioning Agent"
const VERSION = "1.0.2"
const CERTUTIL_PROGRAM_PATH = "C:\\Windows\\System32\\certutil.exe"
const WIFI_PEAP_TEMPLATE_NAME = "wireless PEAP template"
const WIFI_TLS_TEMPLATE_NAME = "wireless TLS template"
const WIFI_OPEN_TEMPLATE_NAME = "wireless Open template"
const PROFILE_URL = "http://wireless-profiles.packetfence.org/profile.xml"
const EAPTYPE_PEAP = 25
const EAPTYPE_TLS = 13
// French language codes
const SUBLANG_FRENCH = 1036
const SUBLANG_FRENCH_CANADIAN = 3084
const SUBLANG_FRENCH_BELGIAN = 2060
const SUBLANG_FRENCH_LUXEMBOURG = 5132
const SUBLANG_FRENCH_MONACO = 6156
const SUBLANG_FRENCH_SWISS = 4108
var T i18n.TranslateFunc
var windowMsgBox walk.Form
var TempPATH string
var ProfileDownloaded string
var ProfileTemplated string
var PngFilePath string
type Template struct {
ProfileName string // replace by SSIDString
SsidStringToHex string // replace by hex SSIDString
IsSSIDBroadcast bool // replace by SSIDBroadcast
SecAuth string // replace by securityType, if securityType=None : securityType=open
OpenPasscode string // replace by "networkKey"
WifiKey string // replace by wifiKey
Encryption string // replace by encryption
CaToTrust string // replace by sha1 fingerprint
}
type Handle uintptr
func main() {
hideConsole()
log.Println("==================== PacketFence Provisioning Agent ===================")
// Set temp directory
TempPATH = os.Getenv("tmp")
if TempPATH == "" {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("invalidTempPATH"), walk.MsgBoxOK)
log.Println("Failed found a temporary directory")
os.Exit(1)
}
currentWorkingDirectory, err := os.Executable()
if err != nil {
walk.MsgBox(windowMsgBox, "Error", "Unable to get current working directory, please contact your local support.", walk.MsgBoxOK)
}
stableCurrentWorkingDirectory := filepath.Dir(currentWorkingDirectory)
// Internationalization (i18n)
localeInfo := win.GetThreadUILanguage()
log.Printf("User's locale is: %#x", localeInfo)
switch localeInfo {
case SUBLANG_FRENCH, SUBLANG_FRENCH_CANADIAN, SUBLANG_FRENCH_BELGIAN, SUBLANG_FRENCH_LUXEMBOURG, SUBLANG_FRENCH_MONACO, SUBLANG_FRENCH_SWISS:
languageFileName := "fr.json"
createLanguageFile(stableCurrentWorkingDirectory, FRENCH_TRANSLATION, languageFileName)
i18n.MustLoadTranslationFile(languageFileName)
T, _ = i18n.Tfunc("fr")
default:
languageFileName := "en.json"
createLanguageFile(stableCurrentWorkingDirectory, ENGLISH_TRANSLATION, languageFileName)
i18n.MustLoadTranslationFile(languageFileName)
T, _ = i18n.Tfunc("en")
}
walk.Resources.SetRootDirPath(TempPATH)
_, pfBg := base64ToPng(BACKGROUND_IMAGE_PF, TempPATH)
var mw1 *walk.MainWindow
if _, err := (MainWindow{
AssignTo: &mw1,
Title: fmt.Sprintf("%s - %s", PROGRAM_NAME, VERSION),
MinSize: Size{400, 400},
Layout: VBox{},
Background: SolidColorBrush{Color: walk.RGB(4, 5, 3)},
Children: []Widget{
ImageView{
Background: SolidColorBrush{Color: walk.RGB(4, 5, 3)},
Image: pfBg,
},
PushButton{
Background: SolidColorBrush{Color: walk.RGB(4, 5, 3)},
MinSize: Size{50, 50},
Text: "Configure",
OnClicked: func() {
Configure()
mw1.Close()
},
},
},
}.Run()); err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("errorMainWindow"), walk.MsgBoxOK)
log.Println("Failed opening main window: ", err)
os.Remove(TempPATH + "\\" + "pf_bg.png")
os.Exit(1)
}
os.Exit(0)
}
func Configure() {
var xmlPlistProfile map[string]interface{}
var eapType uint64
var userCertDecode string
var caFileBinary string
var wifiIndex int
var wiredIndex int
if TempPATH == "" {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("invalidTempPath"), walk.MsgBoxOK)
os.Exit(1)
}
// Download mobileconfig file
err := writeProfileToLocalFile("profile.xml", PROFILE_URL)
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotRetrieveProfileFile"), walk.MsgBoxOK)
log.Println("Failed loading profile: ", err)
os.Exit(1)
}
// Read xml profile, convert to string
data, err := ioutil.ReadFile("profile.xml")
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotReadProfileData"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Println("Failed reading profile: ", err)
os.Exit(1)
}
// Decode converted xml profile
dataToString := string(data)
buffer := bytes.NewReader([]byte(dataToString))
decoder := plist.NewDecoder(buffer)
err = decoder.Decode(&xmlPlistProfile)
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotDecodeProfileFile"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Println("Failed decoding profile: ", err)
os.Exit(1)
}
// Get data from the mobileconfig file
shouldConfigureWifi := false
shouldConfigureWired := false
// Get PayloadContent length
lengthPayloadContent := len(xmlPlistProfile["PayloadContent"].([]interface{}))
for i := 0; i < lengthPayloadContent; i++ {
payloadContent := xmlPlistProfile["PayloadContent"].([]interface{})[i].(map[string]interface{})
payloadType := payloadContent["PayloadType"].(string)
switch payloadType {
// Wireless configuration
case "com.apple.wifi.managed":
shouldConfigureWifi = true
// get dict index
wifiIndex = i
// Get the EAP type to avoid importing the RADIUS cert as a CA
eapClientConfiguration, ok := payloadContent["EAPClientConfiguration"].(map[string]interface{})
if ok {
eapType = eapClientConfiguration["AcceptEAPTypes"].([]interface{})[0].(uint64)
}
// Wired configuration
case "com.apple.firstactiveethernet.managed":
shouldConfigureWired = true
wiredIndex = i
eapClientConfiguration, ok := payloadContent["EAPClientConfiguration"].(map[string]interface{})
if ok {
eapType = eapClientConfiguration["AcceptEAPTypes"].([]interface{})[0].(uint64)
}
// User certificate configuration
case "com.apple.security.pkcs12":
userCert := payloadContent["PayloadContent"].(string)
userAuth := "certificate"
fileExtension := ".p12"
alertMessage := T("cannotGenerateCertificateFile")
userCertDecode, err = createCertTempFile(TempPATH, userCert, userAuth, fileExtension, alertMessage)
if err != nil {
log.Println("Failed creating profile: ", err)
os.Exit(1)
}
err = addCertToMachine(userCertDecode, CERTUTIL_PROGRAM_PATH)
if err != nil {
log.Println("Failed adding Cert: ", err)
os.Exit(1)
}
// Certificate of Authority configuration
case "com.apple.security.root":
if eapType == EAPTYPE_TLS {
caName := payloadContent["PayloadCertificateFileName"].(string)
caCert := payloadContent["PayloadContent"].(string)
fileExtension := ".cer"
alertMessage := T("cannotGenerateCAFile")
caFileBinary, err = createCertTempFile(TempPATH, caCert, caName, fileExtension, alertMessage)
if err != nil {
log.Println("Failed creating profile: ", err)
os.Exit(1)
}
err = addCAToMachine(caFileBinary, CERTUTIL_PROGRAM_PATH)
if err != nil {
log.Println("Failed adding CA: ", err)
os.Exit(1)
}
}
default:
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("Unexpected PayloadType {{.PayloadType}} please contact your local support.", map[string]interface{}{
"PayloadType": payloadType,
}), walk.MsgBoxOK)
log.Println("Unexpected PayloadType: ", payloadType)
os.Exit(1)
}
}
if shouldConfigureWifi {
configureWifi(xmlPlistProfile,wifiIndex,eapType,caFileBinary)
}
if shouldConfigureWired {
configureWired(xmlPlistProfile,wiredIndex,eapType,caFileBinary)
}
}
func configureWifi(xmlPlistProfile map[string]interface{}, wifiIndex int,eapType uint64,caFileBinary string){
var WLAN_ERROR_MESSAGE = T("wlanErrorMessage")
var templateToFile string
var elementsToReplaceInTemplate Template
var wifiKey string
profileFile := TempPATH + "\\template-out.xml"
// Get SSID information
payloadContent := xmlPlistProfile["PayloadContent"].([]interface{})[wifiIndex].(map[string]interface{})
ssidString := payloadContent["SSID_STR"].(string)
ssidStringToHex := hex.EncodeToString([]byte(ssidString))
ssidBroadcast := payloadContent["HIDDEN_NETWORK"].(bool)
securityType := payloadContent["EncryptionType"].(string)
if securityType == "None" {
securityType = "open"
}
addWLANProfileCommand := exec.Command("netsh", "wlan", "add", "profile", "filename="+profileFile, "user=all")
wlanSuccessMessage := T("The wireless profile was successfully added to the machine. \nPlease select your newly added profile {{.SsidString}} in the WiFi networks.", map[string]interface{}{
"SsidString": ssidString,
})
// Security of the SSID
eapClientConfiguration, ok := payloadContent["EAPClientConfiguration"].(map[string]interface{})
if ok {
eapType = eapClientConfiguration["AcceptEAPTypes"].([]interface{})[0].(uint64)
userAuth, ok := eapClientConfiguration["UserName"].(string)
if ok {
if userAuth == "" {
userAuth = "certificate"
}
} else {
userAuth = "certificate"
}
eapType = eapClientConfiguration["AcceptEAPTypes"].([]interface{})[0].(uint64)
if eapType == EAPTYPE_PEAP {
// Search specific fields in wintemplate and replace them
elementsToReplaceInTemplate = Template{
ProfileName: ssidString,
SsidStringToHex: ssidStringToHex,
IsSSIDBroadcast: ssidBroadcast,
SecAuth: "WPA2",
Encryption: "AES",
}
// executes the template
templateToFile, err := executeTemplate(WIFI_PEAP_TEMPLATE_NAME, WIFI_PEAP_TEMPLATE, elementsToReplaceInTemplate)
if err != nil {
log.Println("Failed executing template: ", err)
os.Exit(1)
}
// creates profile file with the executed template
err = createProfileFile(templateToFile)
if err != nil {
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
// adds the new profile to Windows with netsh command
addProfileToMachine(profileFile, addWLANProfileCommand, WLAN_ERROR_MESSAGE, wlanSuccessMessage)
}
if eapType == EAPTYPE_TLS {
caFingerprint, err := getCAFingerprint(caFileBinary)
if err != nil {
os.Remove(caFileBinary)
os.Remove("profile.xml")
log.Println("Unable to get CA fingerprint: ", err)
os.Exit(1)
}
elementsToReplaceInTemplate = Template{
ProfileName: ssidString,
SsidStringToHex: ssidStringToHex,
IsSSIDBroadcast: ssidBroadcast,
SecAuth: "WPA2",
Encryption: "AES",
CaToTrust: caFingerprint,
}
os.Remove(caFileBinary)
templateToFile, err = executeTemplate(WIFI_TLS_TEMPLATE_NAME, WIFI_TLS_TEMPLATE, elementsToReplaceInTemplate)
if err != nil {
log.Println("Failed executing template: ", err)
os.Exit(1)
}
err = createProfileFile(templateToFile)
if err != nil {
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, addWLANProfileCommand, WLAN_ERROR_MESSAGE, wlanSuccessMessage)
}
if (eapType != EAPTYPE_TLS) && (eapType != EAPTYPE_PEAP) {
// error handling
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("unexpectedEAPType"), walk.MsgBoxOK)
log.Println("Incorrect EAP type: ", eapType)
os.Exit(1)
}
} else {
wifiKey = payloadContent["Password"].(string)
log.Println("Security type: ", securityType)
switch securityType {
case "WEP":
elementsToReplaceInTemplate = Template{
ProfileName: ssidString,
SsidStringToHex: ssidStringToHex,
IsSSIDBroadcast: ssidBroadcast,
SecAuth: "open",
OpenPasscode: "passPhrase",
WifiKey: wifiKey,
Encryption: "WEP",
}
case "WPA":
elementsToReplaceInTemplate = Template{
ProfileName: ssidString,
SsidStringToHex: ssidStringToHex,
IsSSIDBroadcast: ssidBroadcast,
SecAuth: "WPA2PSK",
OpenPasscode: "passPhrase",
WifiKey: wifiKey,
Encryption: "AES",
}
default:
elementsToReplaceInTemplate = Template{
ProfileName: ssidString,
SsidStringToHex: ssidStringToHex,
IsSSIDBroadcast: ssidBroadcast,
SecAuth: "open",
OpenPasscode: "passPhrase",
WifiKey: wifiKey,
Encryption: "none",
}
}
templateToFile, err := executeTemplate(WIFI_OPEN_TEMPLATE_NAME, WIFI_OPEN_TEMPLATE, elementsToReplaceInTemplate)
if err != nil {
log.Println("Failed executing template: ", err)
os.Exit(1)
}
err = createProfileFile(templateToFile)
if err != nil {
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, addWLANProfileCommand, WLAN_ERROR_MESSAGE, wlanSuccessMessage)
}
}
func configureWired(xmlPlistProfile map[string]interface{}, wiredIndex int,eapType uint64,caFileBinary string){
var WIRED_ERROR_MESSAGE = T("wiredErrorMessage")
var WIRED_SUCCESS_MESSAGE = T("wiredSuccessMessage")
TempPATH := os.Getenv("tmp")
profileFile := TempPATH + "\\template-out.xml"
dot3svc := exec.Command("net", "start", "dot3svc")
dot3svc.Start()
if err := dot3svc.Wait(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
exitStatus := status.ExitStatus()
if exitStatus != 2 {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("dot3svcFail"), walk.MsgBoxOK)
log.Print("The Wired Autoconfig service could not be started.", err)
}
}
}
}
wiredNetshCommand := exec.Command("netsh", "lan", "add", "profile", "filename="+profileFile)
payloadContent := xmlPlistProfile["PayloadContent"].([]interface{})[wiredIndex].(map[string]interface{})
eapClientConfiguration := payloadContent["EAPClientConfiguration"].(map[string]interface{})
eapType = eapClientConfiguration["AcceptEAPTypes"].([]interface{})[0].(uint64)
if eapType == EAPTYPE_PEAP {
err := createProfileFile(WIRED_PEAP_TEMPLATE)
if err != nil {
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, wiredNetshCommand, WIRED_ERROR_MESSAGE, WIRED_SUCCESS_MESSAGE)
}
if eapType == EAPTYPE_TLS {
err := createProfileFile(WIRED_TLS_TEMPLATE)
if err != nil {
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, wiredNetshCommand, WIRED_ERROR_MESSAGE, WIRED_SUCCESS_MESSAGE)
}
if (eapType != EAPTYPE_TLS) && (eapType != EAPTYPE_PEAP) {
// error handling
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("unexpectedEAPType"), walk.MsgBoxOK)
log.Println("Incorrect EAP type: ", eapType)
os.Exit(1)
}
}