-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockbox.lua
More file actions
327 lines (222 loc) · 5.75 KB
/
lockbox.lua
File metadata and controls
327 lines (222 loc) · 5.75 KB
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
function LockboxCreate(name, path, password, hint)
local lockbox, str
--initial lockbox setup
lockbox={}
lockbox.name=name
lockbox.password=password
lockbox.passhint=hint
lockbox.version=0
lockbox.suppress_errors=false
lockbox.items={}
if strutil.strlen(path) == 0 then lockbox.path=lockboxes:path(name)
else lockbox.path=path
end
-- from here on is member functions of 'lockbox'
lockbox.saveencrypted=function(self, data, Dest)
local str, Proc, S
Proc=openssl:open_encrypt(self.password)
if Proc ~= nil
then
S=Proc:get_stream()
--send data to openssl for encryption
S:writeln(data)
S:commit()
--process.sleep(5)
--read back the encrypted data
str=S:readdoc()
openssl:close_crypt(Proc)
--write encrypted data to our lockbox file
Dest:writeln(str)
end
end
lockbox.save_fmt_item=function(self, name, value)
return(name .. "=\"" .. strutil.quoteChars(value, "|\r\n\"") .. "\" ")
end
lockbox.save=function(self, do_sync)
local str, S, key, item, name, value
if strutil.strlen(self.password) == 0 then self.password=ui:ask_password("Password for "..self.name..": ~>") end
filesys.mkdirPath(self.path)
S=stream.STREAM(self.path, "w")
if S ~= nil
then
--save lockbox file header
self.filefmt="kv"
self.version=self.version + 1
S:writeln("name:"..self.name.."\n")
if strutil.strlen(self.passhint) > 0 then S:writeln("passhint:" .. self.passhint .. "\n") end
if self.version ~= nil then S:writeln("version:"..tostring(self.version) .. "\n") end
if self.filefmt ~= nil then S:writeln("filefmt:"..tostring(self.filefmt) .. "\n") end
S:writeln("machine_id:" .. hosts.my_id() .. "\n")
S:writeln("updated:"..time.format("%Y-%m-%dT%H:%M:%S").."\n\n")
str=""
for key,item in pairs(self.items)
do
str=str .. key .. " "
for name,value in pairs(item)
do
if name ~= "name" then str=str..self:save_fmt_item(name, value) end
end
str=str .. "\n"
end
self:saveencrypted(str, S)
S:close()
if dosync == true then sync:send(self) end
end
end
lockbox.add_item=function(self, item)
if strutil.strlen(item.updated) == 0 then item.updated=time.format("%Y/%m/%dT%H:%M:%S") end
self.items[item.name]=item
end
lockbox.add=function(self, name, value, notes, updated, item_type)
local item
name=strutil.quoteChars(name, "|")
item={}
if item.type ~= nil then item.type=item_type
else item.type=""
end
item.type=item_type
item.name=name
item.value=value
item.notes=notes
item.updated=updated
self:add_item(item)
end
lockbox.parse_kv=function(self, data, item)
local toks, key, str
toks=strutil.TOKENIZER(data, "=", "Q")
key=toks:next()
if strutil.strlen(key) > 0 then item[key]=strutil.unQuote(toks:next()) end
end
lockbox.add_kv_item=function(self, data)
local toks, tok
local item={}
--these will get overwritten if they exist in data
item.type=""
item.updated=time.format("%Y/%m/%dT%H:%M:%S")
-- go through data parsing name/value pairs
toks=strutil.TOKENIZER(data, "\\S", "Q")
item.name=toks:next()
tok=toks:next()
while tok ~= nil
do
self:parse_kv(tok, item)
tok=toks:next()
end
self.items[item.name]=item
end
lockbox.parse_item=function(self, data)
local toks, tok
lockbox:add_kv_item(data)
end
lockbox.parse_items=function(self, data)
local lines, line
lines=strutil.TOKENIZER(data, "\n")
line=lines:next()
while line ~= nil
do
self:parse_item(line)
line=lines:next()
end
end
lockbox.remove=function(self, key)
self.items[key]=nil
end
lockbox.readencrypted=function(self, encrypt)
local S, PtyS, Proc
local str=""
Proc=openssl:open_decrypt(self.password, nil, self.suppress_errors)
if Proc ~= nil
then
S=Proc:get_stream()
S:writeln(encrypt .. "\n") --some versions of openssl expect base64 encoded data to end with a newline
S:commit()
S:timeout(500)
str=S:readdoc()
if openssl:close_crypt(Proc) ~= true
then
if config:get("syslog") == "y" then syslog.alert("treasury.lua: incorrect password for lockbox '%s'", self.name) end
return nil
end
end
return str
end
lockbox.read_info=function(self, S)
local str, toks, tok
self.version=0
self.machine_id=""
str=S:readln()
while str ~= nil
do
str=strutil.trim(str)
if strutil.strlen(str) == 0 then break end
toks=strutil.TOKENIZER(str, ":")
tok=toks:next()
if tok=="name" then self.name=toks:remaining()
elseif tok=="machine_id" then self.machine_id=toks:remaining()
elseif tok=="updated" then self.updated=toks:remaining()
elseif tok=="version" then self.version=tonumber(toks:remaining())
elseif tok=="passhint" then self.passhint=toks:remaining()
elseif tok=="filefmt" then self.filefmt=toks:remaining()
end
str=S:readln()
end
end
lockbox.examine=function(self)
local S
S=stream.STREAM(self.path, "r")
if S ~= nil
then
self:read_info(S)
S:close()
return true
end
return false
end
lockbox.read=function(self)
local S
local str=""
local queried_password=false
S=stream.STREAM(self.path, "r")
if S ~= nil
then
self:read_info(S)
if strutil.strlen(self.password) == 0 and config:get("keyring") == "y" then self.password=keyring:get(self.name) end
if strutil.strlen(self.password) == 0
then
self.password=ui:ask_password("Password for "..self.name..": ~>", self.passhint)
queried_password=true
end
str=self:readencrypted(S:readdoc())
if queried_password == true and strutil.strlen(str) > 0 and config:get("keyring") ~= "n" then keyring:set(self.name, self.password) end
S:close()
end
return str
end
-- load without importing synced items
lockbox.load_items=function(self)
local str
str=self:read()
if str==nil then return false end
self:parse_items(str)
return true
end
lockbox.load=function(self)
local result
result=self:load_items()
sync:update(self)
return result
end
lockbox.get=function(self, name)
local key, item
for key,item in pairs(self.items)
do
if key==name then return item end
end
return nil
end
lockbox.destroy=function(self)
ScrubFile(self.path)
filesys.unlink(self.path)
end
return lockbox
end