-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_files_manager.go
224 lines (170 loc) · 4.42 KB
/
temp_files_manager.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
// Temporal files manager
package main
import (
"fmt"
"os"
"path"
"sync"
"time"
)
var (
temp_files_path = "./temp" // Path for vault temp files
unencrypted_temp_files_path = "./temp" // Path for unencrypted temp files (upload, ffmpeg, etc)
temp_files_prefix = "pmv_tmp_"
temp_files_lock = &sync.Mutex{}
temp_files_counter = 0
secure_temp_file_delete = os.Getenv("TEMP_FILE_DELETE_MODE") == "SECURE"
)
// Set vault temp files path
// tempPath - Temp files path
func SetTempFilesPath(tempPath string) {
temp_files_path = tempPath
// Create path if not exists
err := os.MkdirAll(temp_files_path, FOLDER_PERMISSION)
if err != nil {
LogError(err)
}
// Create unique prefix for each execution
timeNow := time.Now().UTC().UnixMilli()
temp_files_prefix = "pmv_tmp_" + fmt.Sprint(timeNow) + "_"
}
// Set unencrypted temp files path
// unencryptedTempPath - Unencrypted temp files path
func SetUnencryptedTempFilesPath(unencryptedTempPath string) {
unencrypted_temp_files_path = unencryptedTempPath
// Create path if not exists
err := os.MkdirAll(unencrypted_temp_files_path, FOLDER_PERMISSION)
if err != nil {
LogError(err)
}
}
// Clears vault temp path
func ClearTemporalFilesPath() {
err := os.RemoveAll(temp_files_path)
if err != nil {
LogError(err)
}
err = os.MkdirAll(temp_files_path, FOLDER_PERMISSION)
if err != nil {
LogError(err)
}
}
// Clears all unencrypted temp files
func ClearUnencryptedTempFilesPath() {
err := os.MkdirAll(unencrypted_temp_files_path, FOLDER_PERMISSION)
if err != nil {
LogError(err)
return
}
entries, err := os.ReadDir(unencrypted_temp_files_path)
if err != nil {
LogError(err)
return
}
for i := 0; i < len(entries); i++ {
if entries[i].Type().IsRegular() {
WipeTemporalFile(path.Join(unencrypted_temp_files_path, entries[i].Name()))
} else if entries[i].Type().IsDir() {
WipeTemporalPath(path.Join(unencrypted_temp_files_path, entries[i].Name()))
}
}
}
// Gets a name for a temporal file
// extension - File extension (without the dot)
// encrypted - True to use the vault temp path, false to use the unencrypted temp path
// Returns the path to the file
func GetTemporalFileName(extension string, encrypted bool) string {
temp_files_lock.Lock()
temp_files_counter++
fileName := temp_files_prefix + fmt.Sprint(temp_files_counter)
temp_files_lock.Unlock()
if extension != "" {
fileName += "." + extension
}
var baseFolder string
if encrypted {
baseFolder = temp_files_path
} else {
baseFolder = unencrypted_temp_files_path
}
return path.Join(baseFolder, fileName)
}
// Creates a temporal folder and returns the path
// encrypted - True to use the vault temp path, false to use the unencrypted temp path
// Returns the path to the folder (creates it)
func GetTemporalFolder(encrypted bool) (string, error) {
temp_files_lock.Lock()
temp_files_counter++
folderName := temp_files_prefix + fmt.Sprint(temp_files_counter)
temp_files_lock.Unlock()
var baseFolder string
if encrypted {
baseFolder = temp_files_path
} else {
baseFolder = unencrypted_temp_files_path
}
folderPath := path.Join(baseFolder, folderName)
err := os.MkdirAll(folderPath, FOLDER_PERMISSION)
if err != nil {
return "", err
}
return folderPath, nil
}
// Wipes file to prevent recovery (secure delete)
// file - File path
func WipeTemporalFile(file string) {
if !secure_temp_file_delete {
os.Remove(file)
return
}
f, err := os.OpenFile(file, os.O_WRONLY, FILE_PERMISSION)
if err != nil {
LogError(err)
os.Remove(file)
return
}
defer func() {
f.Close()
os.Remove(file)
}()
fileInfo, err := f.Stat()
if err != nil {
LogError(err)
return
}
fileSize := fileInfo.Size()
fileChunk := make([]byte, 1024*1024)
// Fill chunk with 0
for i := 0; i < len(fileChunk); i++ {
fileChunk[i] = 0
}
// Overwrite
bytesWritten := int64(0)
for bytesWritten < fileSize {
bytesToWrite := int64(len(fileChunk))
if bytesToWrite > (fileSize - bytesWritten) {
bytesToWrite = fileSize - bytesWritten
}
_, err = f.Write(fileChunk[:bytesToWrite])
if err != nil {
LogError(err)
return
}
bytesWritten += bytesToWrite
}
}
// Wipes unencrypted temp path (secure delete)
// p - Path
func WipeTemporalPath(p string) {
entries, err := os.ReadDir(p)
if err != nil {
LogError(err)
return
}
for i := 0; i < len(entries); i++ {
if entries[i].Type().IsRegular() {
WipeTemporalFile(path.Join(p, entries[i].Name()))
}
}
os.Remove(p)
}