|  | 
| 6 | 6 | package conf | 
| 7 | 7 | 
 | 
| 8 | 8 | import ( | 
|  | 9 | +	"io/ioutil" | 
| 9 | 10 | 	"log" | 
|  | 11 | +	"os" | 
| 10 | 12 | 	"path/filepath" | 
| 11 |  | -	"strings" | 
| 12 | 13 | 
 | 
| 13 | 14 | 	"golang.org/x/sys/windows" | 
| 14 | 15 | ) | 
| 15 | 16 | 
 | 
| 16 |  | -func maybeMigrate(c string) { | 
|  | 17 | +func maybeMigrateConfiguration(c string) { | 
| 17 | 18 | 	if disableAutoMigration { | 
| 18 | 19 | 		return | 
| 19 | 20 | 	} | 
| 20 |  | - | 
| 21 |  | -	vol := filepath.VolumeName(c) | 
| 22 |  | -	withoutVol := strings.TrimPrefix(c, vol) | 
| 23 |  | -	oldRoot := filepath.Join(vol, "\\windows.old") | 
| 24 |  | -	oldC := filepath.Join(oldRoot, withoutVol) | 
| 25 |  | - | 
| 26 |  | -	sd, err := windows.GetNamedSecurityInfo(oldRoot, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION) | 
| 27 |  | -	if err == windows.ERROR_PATH_NOT_FOUND || err == windows.ERROR_FILE_NOT_FOUND { | 
| 28 |  | -		return | 
| 29 |  | -	} | 
|  | 21 | +	oldRoot, err := windows.KnownFolderPath(windows.FOLDERID_LocalAppData, windows.KF_FLAG_DEFAULT) | 
| 30 | 22 | 	if err != nil { | 
| 31 |  | -		log.Printf("Not migrating configuration from ‘%s’ due to GetNamedSecurityInfo error: %v", oldRoot, err) | 
| 32 | 23 | 		return | 
| 33 | 24 | 	} | 
| 34 |  | -	owner, defaulted, err := sd.Owner() | 
|  | 25 | +	oldC := filepath.Join(oldRoot, "WireGuard", "Configurations") | 
|  | 26 | +	files, err := ioutil.ReadDir(oldC) | 
| 35 | 27 | 	if err != nil { | 
| 36 |  | -		log.Printf("Not migrating configuration from ‘%s’ due to GetSecurityDescriptorOwner error: %v", oldRoot, err) | 
| 37 |  | -		return | 
| 38 |  | -	} | 
| 39 |  | -	if defaulted || (!owner.IsWellKnown(windows.WinLocalSystemSid) && !owner.IsWellKnown(windows.WinBuiltinAdministratorsSid)) { | 
| 40 |  | -		log.Printf("Not migrating configuration from ‘%s’, as it is not explicitly owned by SYSTEM or Built-in Administrators, but rather ‘%v’", oldRoot, owner) | 
| 41 | 28 | 		return | 
| 42 | 29 | 	} | 
| 43 |  | -	err = windows.MoveFileEx(windows.StringToUTF16Ptr(oldC), windows.StringToUTF16Ptr(c), windows.MOVEFILE_COPY_ALLOWED) | 
| 44 |  | -	if err != nil { | 
| 45 |  | -		if err != windows.ERROR_FILE_NOT_FOUND && err != windows.ERROR_ALREADY_EXISTS { | 
| 46 |  | -			log.Printf("Not migrating configuration from ‘%s’ due to error when moving files: %v", oldRoot, err) | 
|  | 30 | +	for i := range files { | 
|  | 31 | +		if files[i].IsDir() { | 
|  | 32 | +			continue | 
| 47 | 33 | 		} | 
| 48 |  | -		return | 
|  | 34 | +		newPath := filepath.Join(c, files[i].Name()) | 
|  | 35 | +		newFile, err := os.OpenFile(newPath, os.O_EXCL | os.O_CREATE | os.O_WRONLY, 0600) | 
|  | 36 | +		if err != nil { | 
|  | 37 | +			continue | 
|  | 38 | +		} | 
|  | 39 | +		oldPath := filepath.Join(oldC, files[i].Name()) | 
|  | 40 | +		oldConfig, err := ioutil.ReadFile(oldPath) | 
|  | 41 | +		if err != nil { | 
|  | 42 | +			newFile.Close() | 
|  | 43 | +			os.Remove(newPath) | 
|  | 44 | +			continue | 
|  | 45 | +		} | 
|  | 46 | +		_, err = newFile.Write(oldConfig) | 
|  | 47 | +		if err != nil { | 
|  | 48 | +			newFile.Close() | 
|  | 49 | +			os.Remove(newPath) | 
|  | 50 | +			continue | 
|  | 51 | +		} | 
|  | 52 | +		newFile.Close() | 
|  | 53 | +		os.Remove(oldPath) | 
|  | 54 | +		log.Printf("Migrated configuration from ‘%s’ to ‘%s’", oldPath, newPath) | 
| 49 | 55 | 	} | 
| 50 |  | -	log.Printf("Migrated configuration from ‘%s’", oldRoot) | 
|  | 56 | +	os.Remove(oldC) | 
| 51 | 57 | } | 
0 commit comments