-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder_funcs.go
571 lines (450 loc) · 11.5 KB
/
folder_funcs.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package gokwallet
import (
"github.com/godbus/dbus/v5"
)
/*
NewFolder returns a Folder. It requires a RecurseOpts
(you can use DefaultRecurseOpts, call NewRecurseOpts, or provide your own RecurseOpts struct).
It also requires a Wallet.
*/
func NewFolder(w *Wallet, name string, recursion *RecurseOpts) (folder *Folder, err error) {
if !w.isInit {
err = ErrInitWallet
return
}
folder = &Folder{
DbusObject: w.DbusObject,
Name: name,
Passwords: nil,
Maps: nil,
BinaryData: nil,
Unknown: nil,
Recurse: recursion,
wm: w.wm,
wallet: w,
isInit: false,
}
folder.isInit = true
if folder.Recurse.AllWalletItems ||
folder.Recurse.Passwords ||
folder.Recurse.Maps ||
folder.Recurse.Blobs ||
folder.Recurse.UnknownItems {
if err = folder.Update(); err != nil {
return
}
}
folder.isInit = true
return
}
/*
Delete will delete this Folder, and all its WalletItems, from the parent Wallet.
You may want to run Wallet.Update upon completion to update the Wallet.Folders cache if you're using it.
*/
func (f *Folder) Delete() (err error) {
if err = f.wallet.RemoveFolder(f.Name); err != nil {
return
}
f = nil
return
}
// HasEntry specifies if a Folder has an entry (WalletItem item) by the give entryName.
func (f *Folder) HasEntry(entryName string) (hasEntry bool, err error) {
var call *dbus.Call
if err = f.wallet.walletCheck(); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMHasEntry, 0, f.wallet.handle, f.Name, entryName, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&hasEntry); err != nil {
return
}
return
}
/*
KeyNotExist returns true if a key/entry name entryName does *not* exist.
Essentially the same as Folder.HasEntry, but whereas Folder.HasEntry requires the parent wallet
to be open/unlocked, Folder.KeyNotExist does not require this.
However, it's prone to somewhat unreliable results; it's best to use Folder.HasEntry wherever/whenever possible.
*/
func (f *Folder) KeyNotExist(entryName string) (doesNotExist bool, err error) {
var call *dbus.Call
if err = f.wallet.walletCheck(); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMKeyNotExist, 0, f.wallet.Name, f.Name, entryName,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&doesNotExist); err != nil {
return
}
return
}
// ListEntries lists all entries (WalletItem items) in a Folder (regardless of type) by name.
func (f *Folder) ListEntries() (entryNames []string, err error) {
var call *dbus.Call
if err = f.wallet.walletCheck(); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMEntryList, 0, f.wallet.handle, f.Name, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&entryNames); err != nil {
return
}
return
}
// RemoveEntry removes a WalletItem from a Folder given its entryName (key).
func (f *Folder) RemoveEntry(entryName string) (err error) {
var call *dbus.Call
var rslt int32
if err = f.wallet.walletCheck(); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMRemoveEntry, 0, f.wallet.handle, f.Name, entryName, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
return
}
// RenameEntry renames a WalletItem in a Folder from entryName to newEntryName.
func (f *Folder) RenameEntry(entryName, newEntryName string) (err error) {
var call *dbus.Call
var rslt int32
if err = f.wallet.walletCheck(); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMRenameEntry, 0, f.wallet.handle, f.Name, entryName, newEntryName, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
return
}
// Update runs all of the configured Update[type] methods for a Folder, depending on Folder.Recurse configuration.
func (f *Folder) Update() (err error) {
var errs []error = make([]error, 0)
if f.Recurse.AllWalletItems || f.Recurse.Passwords {
if err = f.UpdatePasswords(); err != nil {
errs = append(errs, err)
err = nil
}
}
if f.Recurse.AllWalletItems || f.Recurse.Maps {
if err = f.UpdateMaps(); err != nil {
errs = append(errs, err)
err = nil
}
}
if f.Recurse.AllWalletItems || f.Recurse.Blobs {
if err = f.UpdateBlobs(); err != nil {
errs = append(errs, err)
err = nil
}
}
if f.Recurse.AllWalletItems || f.Recurse.UnknownItems {
if err = f.UpdateUnknowns(); err != nil {
errs = append(errs, err)
err = nil
}
}
if errs != nil && len(errs) > 0 {
err = NewErrors(errs...)
}
return
}
// UpdateBlobs updates (populates) a Folder's Folder.BinaryData.
func (f *Folder) UpdateBlobs() (err error) {
var call *dbus.Call
var mapKeys []string
var isBlob bool
var variant dbus.Variant
var errs []error = make([]error, 0)
if err = f.wallet.walletCheck(); err != nil {
return
}
if !f.isInit {
err = ErrInitFolder
return
}
if call = f.Dbus.Call(
DbusWMEntriesList, 0, f.wallet.handle, f.Name, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&variant); err != nil {
return
}
mapKeys = bytemapKeys(variant)
f.BinaryData = make(map[string]*Blob, len(mapKeys))
for _, k := range mapKeys {
if isBlob, err = f.isType(k, KwalletdEnumTypeStream); err != nil {
errs = append(errs, err)
err = nil
continue
}
if !isBlob {
continue
}
if f.BinaryData[k], err = NewBlob(f, k, f.Recurse); err != nil {
errs = append(errs, err)
err = nil
continue
}
}
if errs != nil && len(errs) > 0 {
err = NewErrors(errs...)
}
return
}
// UpdateMaps updates (populates) a Folder's Folder.Maps.
func (f *Folder) UpdateMaps() (err error) {
var call *dbus.Call
var mapKeys []string
var variant dbus.Variant
var errs []error = make([]error, 0)
if err = f.wallet.walletCheck(); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMMapList, 0, f.wallet.handle, f.Name, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&variant); err != nil {
return
}
mapKeys = bytemapKeys(variant)
f.Maps = make(map[string]*Map, len(mapKeys))
for _, k := range mapKeys {
if f.Maps[k], err = NewMap(f, k, f.Recurse); err != nil {
errs = append(errs, err)
err = nil
continue
}
}
if errs != nil && len(errs) > 0 {
err = NewErrors(errs...)
}
return
}
// UpdatePasswords updates (populates) a Folder's Folder.Passwords.
func (f *Folder) UpdatePasswords() (err error) {
var call *dbus.Call
var mapKeys []string
var variant dbus.Variant
var errs []error = make([]error, 0)
if err = f.wallet.walletCheck(); err != nil {
return
}
if !f.isInit {
err = ErrInitFolder
return
}
if call = f.Dbus.Call(
DbusWMPasswordList, 0, f.wallet.handle, f.Name, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&variant); err != nil {
return
}
mapKeys = bytemapKeys(variant)
f.Passwords = make(map[string]*Password, len(mapKeys))
for _, k := range mapKeys {
if f.Passwords[k], err = NewPassword(f, k, f.Recurse); err != nil {
errs = append(errs, err)
err = nil
continue
}
}
if errs != nil && len(errs) > 0 {
err = NewErrors(errs...)
}
return
}
// UpdateUnknowns updates (populates) a Folder's Folder.Unknown.
func (f *Folder) UpdateUnknowns() (err error) {
var call *dbus.Call
var mapKeys []string
var isUnknown bool
var variant dbus.Variant
var errs []error = make([]error, 0)
if err = f.wallet.walletCheck(); err != nil {
return
}
if !f.isInit {
err = ErrInitFolder
return
}
if call = f.Dbus.Call(
DbusWMEntriesList, 0, f.wallet.handle, f.Name, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&variant); err != nil {
return
}
mapKeys = bytemapKeys(variant)
f.Unknown = make(map[string]*UnknownItem, len(mapKeys))
for _, k := range mapKeys {
if isUnknown, err = f.isType(k, KwalletdEnumTypeUnknown); err != nil {
errs = append(errs, err)
err = nil
continue
}
if !isUnknown {
continue
}
if f.Unknown[k], err = NewUnknownItem(f, k, f.Recurse); err != nil {
errs = append(errs, err)
err = nil
continue
}
}
if errs != nil && len(errs) > 0 {
err = NewErrors(errs...)
}
return
}
// WriteBlob adds or replaces a Blob to/in a Folder.
func (f *Folder) WriteBlob(entryName string, entryValue []byte) (b *Blob, err error) {
if err = f.WriteEntry(entryName, KwalletdEnumTypeStream, entryValue); err != nil {
return
}
if b, err = NewBlob(f, entryName, f.Recurse); err != nil {
return
}
return
}
/*
WriteEntry is used for adding/replacing a WalletItem as a general interface.
If possible, you'll want to use a item-type-specific method (e.g. Folder.WritePassword) as this one is a little unwieldy to use.
entryType must be the relevant KwalletdEnumType* constant (do not use KwalletdEnumTypeUnused).
*/
func (f *Folder) WriteEntry(entryName string, entryType kwalletdEnumType, entryValue []byte) (err error) {
var call *dbus.Call
var rslt int32
if err = f.wallet.walletCheck(); err != nil {
return
}
if entryType == KwalletdEnumTypeUnused {
err = ErrNoCreate
return
}
if call = f.Dbus.Call(
DbusWMWriteEntry, 0, f.wallet.handle, f.Name, entryName, entryValue, int32(entryType), f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
return
}
// WriteMap adds or replaces a Map to/in a Folder.
func (f *Folder) WriteMap(entryName string, entryValue map[string]string) (m *Map, err error) {
var call *dbus.Call
var rslt int32
var b []byte
if err = f.wallet.walletCheck(); err != nil {
return
}
if b, err = mapToBytes(entryValue); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMWriteMap, 0, f.wallet.handle, f.Name, entryName, b, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
if m, err = NewMap(f, entryName, f.Recurse); err != nil {
return
}
return
}
// WritePassword adds or replaces a Password to/in a Folder.
func (f *Folder) WritePassword(entryName, entryValue string) (p *Password, err error) {
var call *dbus.Call
var rslt int32
if err = f.wallet.walletCheck(); err != nil {
return
}
if call = f.Dbus.Call(
DbusWMWritePassword, 0, f.wallet.handle, f.Name, entryName, entryValue, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
if p, err = NewPassword(f, entryName, f.Recurse); err != nil {
return
}
return
}
// WriteUnknown adds or replaces an UnknownItem to/in a Folder.
func (f *Folder) WriteUnknown(entryName string, entryValue []byte) (u *UnknownItem, err error) {
if err = f.wallet.walletCheck(); err != nil {
return
}
if err = f.WriteEntry(entryName, KwalletdEnumTypeUnknown, entryValue); err != nil {
return
}
if u, err = NewUnknownItem(f, entryName, f.Recurse); err != nil {
return
}
return
}
// isType checks if a certain key keyName is of type typeCheck (via KwalletdEnumType*).
func (f *Folder) isType(keyName string, typeCheck kwalletdEnumType) (isOfType bool, err error) {
var call *dbus.Call
var entryType int32
if call = f.Dbus.Call(
DbusWMEntryType, 0, f.wallet.handle, f.Name, keyName, f.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&entryType); err != nil {
return
}
if int32(typeCheck) == entryType {
isOfType = true
}
return
}