From 3b18fb5af2781e6236cad6f13ca6e5f157f0a6ce Mon Sep 17 00:00:00 2001 From: John Starks Date: Wed, 30 Mar 2016 12:25:32 -0700 Subject: [PATCH] Add OpenForBackup function This works with RunWithPrivileges to allow opening files as an administrator without access checks. --- backup.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/backup.go b/backup.go index bfefd42..8649351 100644 --- a/backup.go +++ b/backup.go @@ -26,10 +26,18 @@ const ( BackupReparseData BackupSparseBlock BackupTxfsData +) +const ( StreamSparseAttributes = uint32(8) ) +const ( + WRITE_DAC = 0x40000 + WRITE_OWNER = 0x80000 + ACCESS_SYSTEM_SECURITY = 0x1000000 +) + // BackupHeader represents a backup stream of a file. type BackupHeader struct { Id uint32 // The backup stream ID @@ -239,3 +247,20 @@ func (w *BackupFileWriter) Close() error { } return nil } + +// OpenForBackup opens a file or directory, potentially skipping access checks if the backup +// or restore privileges have been acquired. +// +// If the file opened was a directory, it cannot be used with Readdir(). +func OpenForBackup(path string, access uint32, share uint32, createmode uint32) (*os.File, error) { + winPath, err := syscall.UTF16FromString(path) + if err != nil { + return nil, err + } + h, err := syscall.CreateFile(&winPath[0], access, share, nil, createmode, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0) + if err != nil { + err = &os.PathError{Op: "open", Path: path, Err: err} + return nil, err + } + return os.NewFile(uintptr(h), path), nil +}