-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
379 lines (355 loc) · 10.9 KB
/
config.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
package main
import (
// "errors"
"errors"
"fmt"
"gopkg.in/gcfg.v1"
"log"
"os"
)
var (
config Config
debug bool = false
stable bool = false
stableblock uint64 = 0
clean bool = false
archive bool = false
)
type Config struct {
Defaults struct {
Port uint32
MatchRegExp string
LogFile string
LockDir string
ProxyServer string
ProxyPort uint32
InsecureCiphers bool
Debug bool
StableSize bool
StableDuration uint64
Clean bool
Archive bool
ArchiveDir string
PublicKeyRing string
PrivateKeyRing string
Encrypt bool
EncryptKeyId string
EncryptRegExp string
EncryptSuffix string
Decrypt bool
DecryptKeyId []string
DecryptPassphrase []string
DecryptRegExp string
CleanDecrypted bool
Reencrypt bool
EmailFailure bool
EmailSuccess bool
EmailTo string
EmailFrom string
EmailHost string
EmailPort int
}
Profile map[string]*Section
}
type Section struct {
Server string
Username string
Password string
Key string
Port uint32
MatchRegExp string
LocalDir string
RemoteDir string
LogFile string
LockDir string
ProxyServer string
ProxyPort uint32
InsecureCiphers bool
Debug bool
StableSize bool
StableDuration uint64
Clean bool
Archive bool
ArchiveDir string
PublicKeyRing string
PrivateKeyRing string
Encrypt bool
EncryptKeyId string
EncryptRegExp string
EncryptSuffix string
Decrypt bool
DecryptKeyId []string
DecryptPassphrase []string
DecryptRegExp string
CleanDecrypted bool
Reencrypt bool
EmailFailure bool
EmailSuccess bool
EmailTo string
EmailFrom string
EmailHost string
EmailPort int
}
func InitialiseConfig(file string) (*Section, error) {
var sectn = Section{}
err := gcfg.ReadFileInto(&config, file)
if err != nil {
return nil, err
}
if config.Defaults.Port == 0 {
config.Defaults.Port = 22
}
if config.Defaults.Port < 1 || config.Defaults.Port > 0xffff {
return nil, fmt.Errorf("port number out of range: %d", config.Defaults.Port)
}
if config.Defaults.MatchRegExp == "" {
config.Defaults.MatchRegExp = ".*"
}
if config.Defaults.LockDir == "" {
config.Defaults.LockDir = "./sftpsyncr.lock"
log.Printf("setting default lockdir to ./sftpsyncr.lock")
}
if config.Defaults.Debug {
debug = config.Defaults.Debug
}
if config.Defaults.StableSize {
stable = config.Defaults.StableSize
}
if config.Defaults.StableDuration == 0 {
config.Defaults.StableDuration = 60
stableblock = 60
}
if config.Defaults.Clean {
clean = config.Defaults.Clean
}
if config.Defaults.Archive {
archive = config.Defaults.Archive
}
if config.Profile[profile].Server == "" {
return nil, errors.New("required profile configurable server not set")
}
sectn.Server = config.Profile[profile].Server
if config.Profile[profile].Port == 0 {
sectn.Port = config.Defaults.Port
} else {
sectn.Port = config.Profile[profile].Port
}
if sectn.Port < 1 || sectn.Port > 0xffff {
return nil, fmt.Errorf("profile port number out of range: %d", sectn.Port)
}
if config.Profile[profile].Username == "" {
return nil, errors.New("required profile configurable username not set")
}
sectn.Username = config.Profile[profile].Username
if config.Profile[profile].Password == "" && config.Profile[profile].Key == "" {
if len(os.Getenv("SSH_AUTH_SOCK")) == 0 {
return nil, errors.New("set an auth method using an ssh-agent and SSH_AUTH_SOCK env, or by setting a key or a password in the config file.")
}
}
sectn.Password = config.Profile[profile].Password
sectn.Key = config.Profile[profile].Key
if config.Profile[profile].MatchRegExp == "" {
sectn.MatchRegExp = config.Defaults.MatchRegExp
}
if config.Profile[profile].LocalDir == "" {
return nil, errors.New("required profile configurable localdir not set")
}
sectn.LocalDir = config.Profile[profile].LocalDir
if config.Profile[profile].RemoteDir == "" {
return nil, errors.New("required profile configurable remotedir not set")
}
sectn.RemoteDir = config.Profile[profile].RemoteDir
if config.Profile[profile].LogFile == "" {
if config.Defaults.LogFile != "" {
sectn.LogFile = config.Defaults.LogFile
}
} else {
sectn.LogFile = config.Profile[profile].LogFile
}
if config.Profile[profile].LockDir == "" {
sectn.LockDir = config.Defaults.LockDir
} else {
sectn.LockDir = config.Profile[profile].LockDir
}
if config.Defaults.ProxyServer != "" && config.Defaults.ProxyPort == 0 {
return nil, errors.New("required configurable proxyport not set")
}
if config.Defaults.ProxyServer != "" {
if config.Profile[profile].ProxyServer == "" {
sectn.ProxyServer = config.Defaults.ProxyServer
sectn.ProxyPort = config.Defaults.ProxyPort
}
}
if config.Profile[profile].ProxyServer != "" && config.Profile[profile].ProxyPort == 0 {
return nil, errors.New("required profile configurable proxyport not set")
}
if config.Profile[profile].ProxyServer != "" {
sectn.ProxyServer = config.Profile[profile].ProxyServer
sectn.ProxyPort = config.Profile[profile].ProxyPort
if sectn.ProxyPort < 1 || sectn.ProxyPort > 0xffff {
return nil, fmt.Errorf("profile proxy port number out of range: %d", sectn.ProxyPort)
}
}
if config.Profile[profile].Debug {
debug = config.Profile[profile].Debug
}
if config.Profile[profile].StableSize {
stable = config.Profile[profile].StableSize
}
if config.Profile[profile].StableDuration == 0 {
sectn.StableDuration = config.Defaults.StableDuration
stableblock = config.Defaults.StableDuration
} else {
sectn.StableDuration = config.Profile[profile].StableDuration
stableblock = config.Profile[profile].StableDuration
}
if config.Profile[profile].Clean {
clean = config.Profile[profile].Clean
}
if config.Profile[profile].Archive {
archive = config.Profile[profile].Archive
}
if archive {
if config.Profile[profile].ArchiveDir == "" {
if config.Defaults.ArchiveDir == "" {
return nil, errors.New("if archive is set, profile configurable archivedir is required")
} else {
sectn.ArchiveDir = config.Defaults.ArchiveDir
}
} else {
sectn.ArchiveDir = config.Profile[profile].ArchiveDir
}
}
if config.Profile[profile].InsecureCiphers {
sectn.InsecureCiphers = config.Profile[profile].InsecureCiphers
}
if config.Defaults.InsecureCiphers {
sectn.InsecureCiphers = config.Defaults.InsecureCiphers
}
if config.Defaults.EmailHost == "" {
sectn.EmailHost = "localhost"
}
if config.Defaults.EmailPort == 0 {
sectn.EmailPort = 25
}
if config.Profile[profile].EmailHost != "" {
sectn.EmailHost = config.Profile[profile].EmailHost
}
if config.Profile[profile].EmailPort != 0 {
sectn.EmailPort = config.Profile[profile].EmailPort
}
if config.Defaults.EmailSuccess {
sectn.EmailSuccess = config.Defaults.EmailSuccess
}
if config.Profile[profile].EmailSuccess {
sectn.EmailSuccess = config.Profile[profile].EmailSuccess
}
if config.Defaults.EmailFailure {
sectn.EmailFailure = config.Defaults.EmailFailure
}
if config.Profile[profile].EmailFailure {
sectn.EmailFailure = config.Profile[profile].EmailFailure
}
if sectn.EmailSuccess || sectn.EmailFailure {
if config.Defaults.EmailTo != "" {
sectn.EmailTo = config.Defaults.EmailTo
}
if config.Defaults.EmailFrom != "" {
sectn.EmailFrom = config.Defaults.EmailFrom
}
if config.Profile[profile].EmailTo != "" {
sectn.EmailTo = config.Profile[profile].EmailTo
}
if config.Profile[profile].EmailFrom != "" {
sectn.EmailFrom = config.Profile[profile].EmailFrom
}
if sectn.EmailTo == "" {
return nil, errors.New("missing emailto configurable.")
}
if sectn.EmailFrom == "" {
return nil, errors.New("missing emailfrom configurable.")
}
}
// PGP settings
if config.Profile[profile].Decrypt && !sectn.Decrypt {
sectn.Decrypt = config.Profile[profile].Decrypt
}
if config.Profile[profile].Encrypt && !sectn.Encrypt {
sectn.Encrypt = config.Profile[profile].Encrypt
}
if config.Profile[profile].PublicKeyRing != "" && sectn.PublicKeyRing == "" {
sectn.PublicKeyRing = config.Profile[profile].PublicKeyRing
}
if config.Profile[profile].PrivateKeyRing != "" && sectn.PrivateKeyRing == "" {
sectn.PrivateKeyRing = config.Profile[profile].PrivateKeyRing
}
if config.Profile[profile].EncryptKeyId != "" && sectn.EncryptKeyId == "" {
sectn.EncryptKeyId = config.Profile[profile].EncryptKeyId
}
if len(config.Profile[profile].DecryptKeyId) > 0 && len(sectn.DecryptKeyId) == 0 {
sectn.DecryptKeyId = config.Profile[profile].DecryptKeyId
}
if config.Defaults.DecryptRegExp != "" {
sectn.DecryptRegExp = config.Defaults.DecryptRegExp
}
if config.Profile[profile].DecryptRegExp != "" {
sectn.DecryptRegExp = config.Profile[profile].DecryptRegExp
}
if sectn.DecryptRegExp == "" {
sectn.DecryptRegExp = ".*.gpg$"
}
if config.Defaults.EncryptRegExp != "" {
sectn.EncryptRegExp = config.Defaults.EncryptRegExp
}
if config.Profile[profile].EncryptRegExp != "" {
sectn.EncryptRegExp = config.Profile[profile].EncryptRegExp
}
if sectn.EncryptRegExp == "" {
sectn.EncryptRegExp = ".*"
}
if config.Defaults.EncryptSuffix != "" {
sectn.EncryptSuffix = config.Defaults.EncryptSuffix
}
if config.Profile[profile].EncryptSuffix != "" {
sectn.EncryptSuffix = config.Profile[profile].EncryptSuffix
}
if sectn.EncryptSuffix == "" {
sectn.EncryptSuffix = ".pgp"
}
if config.Defaults.CleanDecrypted {
sectn.CleanDecrypted = config.Defaults.CleanDecrypted
}
if config.Profile[profile].CleanDecrypted {
sectn.CleanDecrypted = config.Profile[profile].CleanDecrypted
}
if config.Defaults.Reencrypt {
sectn.Reencrypt = config.Defaults.Reencrypt
}
if config.Profile[profile].Reencrypt {
sectn.Reencrypt = config.Profile[profile].Reencrypt
}
if len(config.Profile[profile].DecryptPassphrase) > 0 && len(sectn.DecryptPassphrase) == 0 {
sectn.DecryptPassphrase = config.Profile[profile].DecryptPassphrase
}
if sectn.Decrypt && sectn.PrivateKeyRing == "" {
if config.Defaults.PrivateKeyRing != "" {
sectn.PrivateKeyRing = config.Defaults.PrivateKeyRing
} else {
return nil, errors.New("if decrypt is set, privatekeyring must also be set")
}
}
if sectn.Decrypt && len(sectn.DecryptKeyId) == 0 {
return nil, errors.New("if decrypt is set, decryptkeyid must also be set")
}
if sectn.Encrypt && sectn.PublicKeyRing == "" {
if config.Defaults.PublicKeyRing != "" {
sectn.PublicKeyRing = config.Defaults.PublicKeyRing
} else {
return nil, errors.New("if encrypt is set, publickeyring must also be set")
}
}
if sectn.Encrypt && sectn.EncryptKeyId == "" {
return nil, errors.New("if encrypt is set, encryptkeyid must also be set")
}
return §n, nil
}